Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-19 09:23:17

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2023 CERN for the benefit of the Acts project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include "Acts/Detector/interface/IGeometryIdGenerator.hpp"
0012 #include "Acts/Geometry/GeometryIdentifier.hpp"
0013 #include "Acts/Utilities/Logger.hpp"
0014 
0015 #include <any>
0016 
0017 namespace Acts {
0018 
0019 class Surface;
0020 
0021 namespace Experimental {
0022 
0023 class Portal;
0024 class DetectorVolume;
0025 
0026 /// @brief This is the default implementation of the geometry id generator
0027 ///
0028 /// It is a simple counter based generator that assigns ids to the geometry
0029 /// and increments the counter for each object type.
0030 ///
0031 /// Sub counters, i.e. for the sensitive surfaces, are reset after the volume
0032 /// call, such that a new volume or layer volume would start from counter 0
0033 /// again.
0034 ///
0035 /// If the generator is configured to override existing ids, it will do so
0036 /// and not respect previously assigned ids.
0037 ///
0038 /// If the generator is configured in container mode, it will increase the
0039 /// layer id for each layer volume with confined surfaces.
0040 ///
0041 class GeometryIdGenerator final : public IGeometryIdGenerator {
0042  public:
0043   /// @brief  Nested config struct
0044   struct Config {
0045     /// Container mode
0046     bool containerMode = false;
0047     /// Container id (if container mode), will not be incremented
0048     unsigned int containerId = 0u;
0049     /// Resetting mode
0050     bool resetSubCounters = true;
0051     /// Force override existing ids
0052     bool overrideExistingIds = false;
0053   };
0054 
0055   /// @brief Nested cache struct
0056   struct Cache {
0057     /// Cache count of the volume, for non-container mode
0058     unsigned int volumeCount = 0u;
0059     /// Cache count of the layer volume, for container mode
0060     unsigned int layerCount = 0u;
0061     /// Cache count of the portal surfaces
0062     unsigned int portalCount = 0u;
0063     /// Cache count of passive surfaces
0064     unsigned int passiveCount = 0u;
0065     /// Cache count of sensitive surfaces
0066     unsigned int sensitiveCount = 0u;
0067   };
0068 
0069   /// @brief Constructor with config
0070   ///
0071   /// @param cfg is the geometry configuration object
0072   /// @param mlogger is the logging instance
0073   GeometryIdGenerator(const Config& cfg,
0074                       std::unique_ptr<const Logger> mlogger = getDefaultLogger(
0075                           "GeometryIdGenerator", Logging::INFO))
0076       : m_cfg(cfg), m_logger(std::move(mlogger)) {}
0077 
0078   ~GeometryIdGenerator() override = default;
0079 
0080   /// @brief Interface method to generate a geometry id cache
0081   /// @return a geometry id cache wrapped in a std::any object
0082   IGeometryIdGenerator::GeoIdCache generateCache() const final;
0083 
0084   /// @brief Method for assigning a geometry id to a detector volume
0085   ///
0086   /// @param cache is the cache object for e.g. object counting
0087   /// @param dVolume the detector volume to assign the geometry id to
0088   void assignGeometryId(IGeometryIdGenerator::GeoIdCache& cache,
0089                         DetectorVolume& dVolume) const final;
0090 
0091   /// @brief Method for assigning a geometry id to a portal
0092   ///
0093   /// @param cache is the cache object for e.g. object counting
0094   /// @param portal the portal to assign the geometry id to
0095   void assignGeometryId(IGeometryIdGenerator::GeoIdCache& cache,
0096                         Portal& portal) const final;
0097 
0098   /// @brief Method for assigning a geometry id to a surface
0099   ///
0100   /// @param cache is the cache object for e.g. object counting
0101   /// @param surface the surface to assign the geometry id to
0102   void assignGeometryId(IGeometryIdGenerator::GeoIdCache& cache,
0103                         Surface& surface) const final;
0104 
0105  private:
0106   /// @brief Helper method to get the volume id from the cache
0107   ///
0108   /// @param cache the provided cache
0109   /// @param incrementLayer if true, the layer counter is incremented
0110   ///
0111   /// @return a valid geometry identifier
0112   GeometryIdentifier volumeId(Cache& cache, bool incrementLayer = true) const;
0113 
0114   /// Configuration object
0115   Config m_cfg;
0116 
0117   /// Private access method to the logger
0118   const Logger& logger() const { return *m_logger; }
0119 
0120   /// logging instance
0121   std::unique_ptr<const Logger> m_logger;
0122 };
0123 
0124 /// This is a chained tgeometry id generator that will be in seuqnce
0125 /// @tparam generators_t the generators that will be called in sequence
0126 ///
0127 /// @note the generators are expected to be of pointer type
0128 template <typename... generators_t>
0129 class ChainedGeometryIdGenerator : public IGeometryIdGenerator {
0130  public:
0131   struct Cache {
0132     /// The caches
0133     std::array<IGeometryIdGenerator::GeoIdCache, sizeof...(generators_t)>
0134         storage;
0135   };
0136 
0137   /// The stored generators
0138   std::tuple<generators_t...> generators;
0139 
0140   /// Constructor for chained generators_t in a tuple, this will unroll
0141   /// the tuple and call them in sequence
0142   ///
0143   /// @param gens the updators to be called in chain
0144   /// @param mlogger is the logging instance
0145   ChainedGeometryIdGenerator(const std::tuple<generators_t...>&& gens,
0146                              std::unique_ptr<const Logger> mlogger =
0147                                  getDefaultLogger("ChainedGeometryIdGenerator",
0148                                                   Logging::INFO))
0149       : generators(std::move(gens)), m_logger(std::move(mlogger)) {}
0150 
0151   /// @brief Interface method to generate a geometry id cache
0152   /// @return a geometry id cache wrapped in a std::any object
0153   IGeometryIdGenerator::GeoIdCache generateCache() const final {
0154     // Unfold the tuple and add the attachers
0155     Cache cache;
0156     std::size_t it = 0;
0157     std::apply(
0158         [&](auto&&... generator) {
0159           ((cache.storage[it++] = generator->generateCache()), ...);
0160         },
0161         generators);
0162     return cache;
0163   }
0164 
0165   /// @brief Method for assigning a geometry id to a detector volume
0166   ///
0167   /// @param cache is the cache object for e.g. object counting
0168   /// @param dVolume the detector volume to assign the geometry id to
0169   void assignGeometryId(IGeometryIdGenerator::GeoIdCache& cache,
0170                         DetectorVolume& dVolume) const final {
0171     ACTS_VERBOSE("Assigning chained geometry id to volume.");
0172     assign(cache, dVolume);
0173   }
0174 
0175   /// @brief Method for assigning a geometry id to a portal
0176   ///
0177   /// @param cache is the cache object for e.g. object counting
0178   /// @param portal the portal to assign the geometry id to
0179   void assignGeometryId(IGeometryIdGenerator::GeoIdCache& cache,
0180                         Portal& portal) const final {
0181     ACTS_VERBOSE("Assigning chained geometry id to portal.");
0182     assign(cache, portal);
0183   }
0184 
0185   /// @brief Method for assigning a geometry id to a surface
0186   ///
0187   /// @param cache is the cache object for e.g. object counting
0188   /// @param surface the surface to assign the geometry id to
0189   void assignGeometryId(IGeometryIdGenerator::GeoIdCache& cache,
0190                         Surface& surface) const final {
0191     ACTS_VERBOSE("Assigning chained geometry id to surface.");
0192     assign(cache, surface);
0193   }
0194 
0195  private:
0196   /// @brief Helper to run through the chain of generators
0197   ///
0198   /// @tparam gometry_object_t the geometry object type
0199   ///
0200   /// @param cache object with the array of sub caches
0201   /// @param object the object to assign the geometry id to
0202   template <typename gometry_object_t>
0203   void assign(IGeometryIdGenerator::GeoIdCache& cache,
0204               gometry_object_t& object) const {
0205     std::size_t it = 0;
0206     auto& sCache = std::any_cast<Cache&>(cache);
0207     std::apply(
0208         [&](auto&&... generator) {
0209           (generator->assignGeometryId(sCache.storage[it++], object), ...);
0210         },
0211         generators);
0212   }
0213 
0214   /// Private access method to the logger
0215   const Logger& logger() const { return *m_logger; }
0216 
0217   /// logging instance
0218   std::unique_ptr<const Logger> m_logger;
0219 };
0220 
0221 }  // namespace Experimental
0222 }  // namespace Acts