Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/Acts/Detector/GeometryIdMapper.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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