Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-13 07:50:04

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/DetectorVolume.hpp"
0012 #include "Acts/Detector/Portal.hpp"
0013 #include "Acts/Detector/interface/IGeometryIdGenerator.hpp"
0014 #include "Acts/Geometry/GeometryIdentifier.hpp"
0015 #include "Acts/Surfaces/Surface.hpp"
0016 
0017 #include <map>
0018 #include <ranges>
0019 
0020 namespace Acts::Experimental {
0021 
0022 /// @brief This is a mapper of geometry ids, which can be used to
0023 /// assign predefined geometry ids to objects
0024 ///
0025 /// @tparam SourceIdentifier is the type of the source identifier
0026 /// @tparam SourceCapture is the type of the source capture function/struct
0027 ///
0028 /// The source capture function/struct is a callable object/function that
0029 /// can navigate from the provided surface to the source identifier. Usually
0030 /// this would happen via the associated detector element.
0031 ///
0032 /// The only requirement is that the source identifier can be established
0033 /// from the object that receives the target geometry id itself.
0034 template <typename SourceIdentifier, typename SourceCapture>
0035 class GeometryIdMapper final : public IGeometryIdGenerator {
0036  public:
0037   /// @brief  Nested config struct
0038   struct Config {
0039     /// The source identifier to target geometry Id map
0040     std::map<SourceIdentifier, GeometryIdentifier> sourceTargetMap;
0041     /// The source capture function
0042     SourceCapture sourceCapture = SourceCapture();
0043   };
0044 
0045   /// @brief Cache object
0046   struct Cache {
0047     unsigned int volumeCounter = 0u;
0048     unsigned int portalCounter = 0u;
0049     unsigned int surfaceCounter = 0u;
0050   };
0051 
0052   /// @brief Constructor with config
0053   ///
0054   /// @param cfg is the geometry configuration object
0055   /// @param mlogger is the logging instance
0056   explicit GeometryIdMapper(const Config& cfg,
0057                             std::unique_ptr<const Logger> mlogger =
0058                                 getDefaultLogger("GeometryIdMapper",
0059                                                  Logging::INFO))
0060       : m_cfg(cfg), m_logger(std::move(mlogger)) {}
0061 
0062   ~GeometryIdMapper() override = default;
0063 
0064   /// @brief Interface method to generate a geometry id cache
0065   /// @return a geometry id cache wrapped in a std::any object
0066   IGeometryIdGenerator::GeoIdCache generateCache() const final {
0067     return Cache{0u, 0u, 0u};
0068   }
0069 
0070   /// @brief Method for assigning a geometry id to a detector volume
0071   ///
0072   /// @param cache is the cache object for e.g. object counting
0073   /// @param dVolume the detector volume to assign the geometry id to
0074   void assignGeometryId(IGeometryIdGenerator::GeoIdCache& cache,
0075                         DetectorVolume& dVolume) const final {
0076     auto& sCache = std::any_cast<Cache&>(cache);
0077     /// Retrieve the source id for the detector volume
0078     SourceIdentifier vID = m_cfg.sourceCapture(dVolume);
0079     auto source = m_cfg.sourceTargetMap.find(vID);
0080     if (source != m_cfg.sourceTargetMap.end()) {
0081       dVolume.assignGeometryId(source->second);
0082       ACTS_VERBOSE("Assigning geometry id " << source->second << " to volume "
0083                                             << dVolume.name() << " with id "
0084                                             << vID);
0085       sCache.volumeCounter++;
0086     }
0087 
0088     // Portals
0089     std::ranges::for_each(dVolume.portalPtrs(), [&](auto& portal) {
0090       assignGeometryId(cache, *portal);
0091     });
0092 
0093     // Surfaces
0094     std::ranges::for_each(dVolume.surfacePtrs(), [&](auto& surface) {
0095       assignGeometryId(cache, *surface);
0096     });
0097 
0098     // Sub volumes
0099     std::ranges::for_each(dVolume.volumePtrs(), [&](auto& volume) {
0100       assignGeometryId(cache, *volume);
0101     });
0102   }
0103 
0104   /// @brief Method for assigning a geometry id to a portal
0105   ///
0106   /// @param cache is the cache object for e.g. object counting
0107   /// @param portal the portal to assign the geometry id to
0108   void assignGeometryId(IGeometryIdGenerator::GeoIdCache& cache,
0109                         Portal& portal) const final {
0110     auto& sCache = std::any_cast<Cache&>(cache);
0111     /// Retrieve the source id for the portal
0112     SourceIdentifier pID = m_cfg.sourceCapture(portal);
0113     auto source = m_cfg.sourceTargetMap.find(pID);
0114     if (source != m_cfg.sourceTargetMap.end()) {
0115       portal.surface().assignGeometryId(source->second);
0116       ACTS_VERBOSE("Assigning geometry id " << source->second << " to portal "
0117                                             << " with id " << pID);
0118       sCache.portalCounter++;
0119     }
0120   }
0121 
0122   /// @brief Method for assigning a geometry id to a surface
0123   ///
0124   /// @param cache is the cache object for e.g. object counting
0125   /// @param surface the surface to assign the geometry id to
0126   void assignGeometryId(IGeometryIdGenerator::GeoIdCache& cache,
0127                         Surface& surface) const final {
0128     auto& sCache = std::any_cast<Cache&>(cache);
0129     /// Retrieve the source id for the surface
0130     SourceIdentifier sID = m_cfg.sourceCapture(surface);
0131     auto source = m_cfg.sourceTargetMap.find(sID);
0132     if (source != m_cfg.sourceTargetMap.end()) {
0133       ACTS_VERBOSE("Assigning geometry id " << source->second << " to surface "
0134                                             << " with id " << sID);
0135       surface.assignGeometryId(source->second);
0136       sCache.surfaceCounter++;
0137     }
0138   }
0139 
0140  private:
0141   /// Configuration object
0142   Config m_cfg;
0143 
0144   /// Private access method to the logger
0145   const Logger& logger() const { return *m_logger; }
0146 
0147   /// logging instance
0148   std::unique_ptr<const Logger> m_logger;
0149 };
0150 
0151 }  // namespace Acts::Experimental