Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-06-30 07:51:39

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 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 https://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   explicit GeometryIdGenerator(const Config& cfg,
0074                                std::unique_ptr<const Logger> mlogger =
0075                                    getDefaultLogger("GeometryIdGenerator",
0076                                                     Logging::INFO))
0077       : m_cfg(cfg), m_logger(std::move(mlogger)) {}
0078 
0079   ~GeometryIdGenerator() override = default;
0080 
0081   /// @brief Interface method to generate a geometry id cache
0082   /// @return a geometry id cache wrapped in a std::any object
0083   IGeometryIdGenerator::GeoIdCache generateCache() const final;
0084 
0085   /// @brief Method for assigning a geometry id to a detector volume
0086   ///
0087   /// @param cache is the cache object for e.g. object counting
0088   /// @param dVolume the detector volume to assign the geometry id to
0089   void assignGeometryId(IGeometryIdGenerator::GeoIdCache& cache,
0090                         DetectorVolume& dVolume) const final;
0091 
0092   /// @brief Method for assigning a geometry id to a portal
0093   ///
0094   /// @param cache is the cache object for e.g. object counting
0095   /// @param portal the portal to assign the geometry id to
0096   void assignGeometryId(IGeometryIdGenerator::GeoIdCache& cache,
0097                         Portal& portal) const final;
0098 
0099   /// @brief Method for assigning a geometry id to a surface
0100   ///
0101   /// @param cache is the cache object for e.g. object counting
0102   /// @param surface the surface to assign the geometry id to
0103   void assignGeometryId(IGeometryIdGenerator::GeoIdCache& cache,
0104                         Surface& surface) const final;
0105 
0106  private:
0107   /// @brief Helper method to get the volume id from the cache
0108   ///
0109   /// @param cache the provided cache
0110   /// @param incrementLayer if true, the layer counter is incremented
0111   ///
0112   /// @return a valid geometry identifier
0113   GeometryIdentifier volumeId(Cache& cache, bool incrementLayer = true) const;
0114 
0115   /// Configuration object
0116   Config m_cfg;
0117 
0118   /// Private access method to the logger
0119   const Logger& logger() const { return *m_logger; }
0120 
0121   /// logging instance
0122   std::unique_ptr<const Logger> m_logger;
0123 };
0124 
0125 /// This is a chained tgeometry id generator that will be in seuqnce
0126 /// @tparam generators_t the generators that will be called in sequence
0127 ///
0128 /// @note the generators are expected to be of pointer type
0129 template <typename... generators_t>
0130 class ChainedGeometryIdGenerator : public IGeometryIdGenerator {
0131  public:
0132   struct Cache {
0133     /// The caches
0134     std::array<IGeometryIdGenerator::GeoIdCache, sizeof...(generators_t)>
0135         storage;
0136   };
0137 
0138   /// The stored generators
0139   std::tuple<generators_t...> generators;
0140 
0141   /// Constructor for chained generators_t in a tuple, this will unroll
0142   /// the tuple and call them in sequence
0143   ///
0144   /// @param gens the updators to be called in chain
0145   /// @param mlogger is the logging instance
0146   explicit ChainedGeometryIdGenerator(
0147       const std::tuple<generators_t...>&& gens,
0148       std::unique_ptr<const Logger> mlogger =
0149           getDefaultLogger("ChainedGeometryIdGenerator", Logging::INFO))
0150       : generators(std::move(gens)), m_logger(std::move(mlogger)) {}
0151 
0152   /// @brief Interface method to generate a geometry id cache
0153   /// @return a geometry id cache wrapped in a std::any object
0154   IGeometryIdGenerator::GeoIdCache generateCache() const final {
0155     // Unfold the tuple and add the attachers
0156     Cache cache;
0157     std::size_t it = 0;
0158     std::apply(
0159         [&](auto&&... generator) {
0160           ((cache.storage[it++] = generator->generateCache()), ...);
0161         },
0162         generators);
0163     return cache;
0164   }
0165 
0166   /// @brief Method for assigning a geometry id to a detector volume
0167   ///
0168   /// @param cache is the cache object for e.g. object counting
0169   /// @param dVolume the detector volume to assign the geometry id to
0170   void assignGeometryId(IGeometryIdGenerator::GeoIdCache& cache,
0171                         DetectorVolume& dVolume) const final {
0172     ACTS_VERBOSE("Assigning chained geometry id to volume.");
0173     assign(cache, dVolume);
0174   }
0175 
0176   /// @brief Method for assigning a geometry id to a portal
0177   ///
0178   /// @param cache is the cache object for e.g. object counting
0179   /// @param portal the portal to assign the geometry id to
0180   void assignGeometryId(IGeometryIdGenerator::GeoIdCache& cache,
0181                         Portal& portal) const final {
0182     ACTS_VERBOSE("Assigning chained geometry id to portal.");
0183     assign(cache, portal);
0184   }
0185 
0186   /// @brief Method for assigning a geometry id to a surface
0187   ///
0188   /// @param cache is the cache object for e.g. object counting
0189   /// @param surface the surface to assign the geometry id to
0190   void assignGeometryId(IGeometryIdGenerator::GeoIdCache& cache,
0191                         Surface& surface) const final {
0192     ACTS_VERBOSE("Assigning chained geometry id to surface.");
0193     assign(cache, surface);
0194   }
0195 
0196  private:
0197   /// @brief Helper to run through the chain of generators
0198   ///
0199   /// @tparam gometry_object_t the geometry object type
0200   ///
0201   /// @param cache object with the array of sub caches
0202   /// @param object the object to assign the geometry id to
0203   template <typename gometry_object_t>
0204   void assign(IGeometryIdGenerator::GeoIdCache& cache,
0205               gometry_object_t& object) const {
0206     std::size_t it = 0;
0207     auto& sCache = std::any_cast<Cache&>(cache);
0208     std::apply(
0209         [&](auto&&... generator) {
0210           (generator->assignGeometryId(sCache.storage[it++], object), ...);
0211         },
0212         generators);
0213   }
0214 
0215   /// Private access method to the logger
0216   const Logger& logger() const { return *m_logger; }
0217 
0218   /// logging instance
0219   std::unique_ptr<const Logger> m_logger;
0220 };
0221 
0222 }  // namespace Experimental
0223 }  // namespace Acts