Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-27 07:55: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     /// Counter for volume geometry ID assignment
0048     unsigned int volumeCounter = 0u;
0049     /// Counter for portal geometry ID assignment
0050     unsigned int portalCounter = 0u;
0051     /// Counter for surface geometry ID assignment
0052     unsigned int surfaceCounter = 0u;
0053   };
0054 
0055   /// @brief Constructor with config
0056   ///
0057   /// @param cfg is the geometry configuration object
0058   /// @param mlogger is the logging instance
0059   explicit GeometryIdMapper(const Config& cfg,
0060                             std::unique_ptr<const Logger> mlogger =
0061                                 getDefaultLogger("GeometryIdMapper",
0062                                                  Logging::INFO))
0063       : m_cfg(cfg), m_logger(std::move(mlogger)) {}
0064 
0065   ~GeometryIdMapper() override = default;
0066 
0067   /// @brief Interface method to generate a geometry id cache
0068   /// @return a geometry id cache wrapped in a std::any object
0069   IGeometryIdGenerator::GeoIdCache generateCache() const final {
0070     return Cache{0u, 0u, 0u};
0071   }
0072 
0073   /// @brief Method for assigning a geometry id to a detector volume
0074   ///
0075   /// @param cache is the cache object for e.g. object counting
0076   /// @param dVolume the detector volume to assign the geometry id to
0077   void assignGeometryId(IGeometryIdGenerator::GeoIdCache& cache,
0078                         DetectorVolume& dVolume) const final {
0079     auto& sCache = std::any_cast<Cache&>(cache);
0080     /// Retrieve the source id for the detector volume
0081     SourceIdentifier vID = m_cfg.sourceCapture(dVolume);
0082     auto source = m_cfg.sourceTargetMap.find(vID);
0083     if (source != m_cfg.sourceTargetMap.end()) {
0084       dVolume.assignGeometryId(source->second);
0085       ACTS_VERBOSE("Assigning geometry id " << source->second << " to volume "
0086                                             << dVolume.name() << " with id "
0087                                             << vID);
0088       sCache.volumeCounter++;
0089     }
0090 
0091     // Portals
0092     std::ranges::for_each(dVolume.portalPtrs(), [&](auto& portal) {
0093       assignGeometryId(cache, *portal);
0094     });
0095 
0096     // Surfaces
0097     std::ranges::for_each(dVolume.surfacePtrs(), [&](auto& surface) {
0098       assignGeometryId(cache, *surface);
0099     });
0100 
0101     // Sub volumes
0102     std::ranges::for_each(dVolume.volumePtrs(), [&](auto& volume) {
0103       assignGeometryId(cache, *volume);
0104     });
0105   }
0106 
0107   /// @brief Method for assigning a geometry id to a portal
0108   ///
0109   /// @param cache is the cache object for e.g. object counting
0110   /// @param portal the portal to assign the geometry id to
0111   void assignGeometryId(IGeometryIdGenerator::GeoIdCache& cache,
0112                         Portal& portal) const final {
0113     auto& sCache = std::any_cast<Cache&>(cache);
0114     /// Retrieve the source id for the portal
0115     SourceIdentifier pID = m_cfg.sourceCapture(portal);
0116     auto source = m_cfg.sourceTargetMap.find(pID);
0117     if (source != m_cfg.sourceTargetMap.end()) {
0118       portal.surface().assignGeometryId(source->second);
0119       ACTS_VERBOSE("Assigning geometry id " << source->second << " to portal "
0120                                             << " with id " << pID);
0121       sCache.portalCounter++;
0122     }
0123   }
0124 
0125   /// @brief Method for assigning a geometry id to a surface
0126   ///
0127   /// @param cache is the cache object for e.g. object counting
0128   /// @param surface the surface to assign the geometry id to
0129   void assignGeometryId(IGeometryIdGenerator::GeoIdCache& cache,
0130                         Surface& surface) const final {
0131     auto& sCache = std::any_cast<Cache&>(cache);
0132     /// Retrieve the source id for the surface
0133     SourceIdentifier sID = m_cfg.sourceCapture(surface);
0134     auto source = m_cfg.sourceTargetMap.find(sID);
0135     if (source != m_cfg.sourceTargetMap.end()) {
0136       ACTS_VERBOSE("Assigning geometry id " << source->second << " to surface "
0137                                             << " with id " << sID);
0138       surface.assignGeometryId(source->second);
0139       sCache.surfaceCounter++;
0140     }
0141   }
0142 
0143  private:
0144   /// Configuration object
0145   Config m_cfg;
0146 
0147   /// Private access method to the logger
0148   const Logger& logger() const { return *m_logger; }
0149 
0150   /// logging instance
0151   std::unique_ptr<const Logger> m_logger;
0152 };
0153 
0154 }  // namespace Acts::Experimental