Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:10:45

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/Definitions/Algebra.hpp"
0012 #include "Acts/Definitions/Common.hpp"
0013 #include "Acts/Detector/DetectorVolume.hpp"
0014 #include "Acts/Detector/DetectorVolumeVisitorConcept.hpp"
0015 #include "Acts/Geometry/GeometryContext.hpp"
0016 #include "Acts/Geometry/GeometryHierarchyMap.hpp"
0017 #include "Acts/Geometry/GeometryIdentifier.hpp"
0018 #include "Acts/Navigation/NavigationDelegates.hpp"
0019 #include "Acts/Surfaces/SurfaceVisitorConcept.hpp"
0020 #include "Acts/Utilities/Delegate.hpp"
0021 
0022 #include <cstddef>
0023 #include <memory>
0024 #include <stdexcept>
0025 #include <string>
0026 #include <unordered_map>
0027 #include <vector>
0028 
0029 namespace Acts {
0030 
0031 class Surface;
0032 
0033 namespace Experimental {
0034 struct NavigationState;
0035 
0036 class Detector : public std::enable_shared_from_this<Detector> {
0037  protected:
0038   /// Create a detector from volumes
0039   ///
0040   /// @param name the detecor name
0041   /// @param rootVolumes the volumes contained by this detector
0042   /// @param detectorVolumeFinder is a Delegate to find the associated volume
0043   ///
0044   /// @note will throw an exception if volumes vector is empty
0045   /// @note will throw an exception if duplicate volume names exist
0046   /// @note will throw an exception if the delegate is not connected
0047   Detector(std::string name,
0048            std::vector<std::shared_ptr<DetectorVolume>> rootVolumes,
0049            ExternalNavigationDelegate detectorVolumeFinder) noexcept(false);
0050 
0051  public:
0052   /// Factory for producing memory managed instances of Detector.
0053   static std::shared_ptr<Detector> makeShared(
0054       std::string name,
0055       std::vector<std::shared_ptr<DetectorVolume>> rootVolumes,
0056       ExternalNavigationDelegate detectorVolumeFinder);
0057 
0058   /// Retrieve a @c std::shared_ptr for this surface (non-const version)
0059   ///
0060   /// @note Will error if this was not created through the @c makeShared factory
0061   ///       since it needs access to the original reference. In C++14 this is
0062   ///       undefined behavior (but most likely implemented as a @c bad_weak_ptr
0063   ///       exception), in C++17 it is defined as that exception.
0064   /// @note Only call this if you need shared ownership of this object.
0065   ///
0066   /// @return The shared pointer
0067   std::shared_ptr<Detector> getSharedPtr();
0068 
0069   /// Retrieve a @c std::shared_ptr for this surface (const version)
0070   ///
0071   /// @note Will error if this was not created through the @c makeShared factory
0072   ///       since it needs access to the original reference. In C++14 this is
0073   ///       undefined behavior, but most likely implemented as a @c bad_weak_ptr
0074   ///       exception, in C++17 it is defined as that exception.
0075   /// @note Only call this if you need shared ownership of this object.
0076   ///
0077   /// @return The shared pointer
0078   std::shared_ptr<const Detector> getSharedPtr() const;
0079 
0080   /// Non-const access to the root volumes
0081   ///
0082   /// @return the root volume shared pointer
0083   std::vector<std::shared_ptr<DetectorVolume>>& rootVolumePtrs();
0084 
0085   /// Const access to the root volumes
0086   ///
0087   /// @return a vector to const DetectorVolume raw pointers
0088   const std::vector<const DetectorVolume*>& rootVolumes() const;
0089 
0090   /// Non-const access to the root volume
0091   ///
0092   /// @return the volumes shared pointer store
0093   std::vector<std::shared_ptr<DetectorVolume>>& volumePtrs();
0094 
0095   /// Const access to sub volumes
0096   ///
0097   /// @return a vector to const DetectorVolume raw pointers
0098   const std::vector<const DetectorVolume*>& volumes() const;
0099 
0100   /// Const access to the hierarchy map of all sensitive surfaces
0101   ///
0102   /// @return the map which can be queried with GeometryID for ranges
0103   const GeometryHierarchyMap<const Surface*>& sensitiveHierarchyMap() const;
0104 
0105   /// Search for a surface with the given identifier.
0106   ///
0107   /// @param id is the geometry identifier of the surface
0108   /// @retval nullptr if no such surface exists
0109   /// @retval pointer to the found surface otherwise.
0110   const Surface* findSurface(GeometryIdentifier id) const;
0111 
0112   /// @brief Visit all reachable surfaces of the detector
0113   ///
0114   /// @tparam visitor_t Type of the callable visitor
0115   ///
0116   /// @param visitor will be handed to each root volume,
0117   /// eventually contained volumes within the root volumes are
0118   /// handled by the root volume
0119   ///
0120   /// @note if a context is needed for the visit, the vistitor has to provide
0121   /// it, e.g. as a private member
0122   ///
0123   /// @note due to the fact that portals can be shared between volumes, multiple
0124   /// visits may occur, duplicated addressing needs to be taken care of by the
0125   /// visitor
0126   template <SurfaceVisitor visitor_t>
0127   void visitSurfaces(visitor_t&& visitor) const {
0128     for (const auto& v : rootVolumes()) {
0129       v->template visitSurfaces<visitor_t>(std::forward<visitor_t>(visitor));
0130     }
0131   }
0132 
0133   /// @brief Visit all reachable surfaces of the detector - non-const
0134   ///
0135   /// @tparam visitor_t Type of the callable visitor
0136   ///
0137   /// @param visitor will be handed to each root volume,
0138   /// eventually contained volumes within the root volumes are
0139   /// handled by the root volume
0140   ///
0141   /// @note if a context is needed for the visit, the vistitor has to provide
0142   /// it, e.g. as a private member
0143   ///
0144   /// @note due to the fact that this doesn't run over root volumes, and
0145   /// due to the fact that portals can be shared between volumes, multiple
0146   /// visits may occur, duplicated addressing needs to be taken care of by the
0147   template <MutableSurfaceVisitor visitor_t>
0148   void visitMutableSurfaces(visitor_t&& visitor) {
0149     for (auto& v : volumePtrs()) {
0150       v->template visitMutableSurfaces<visitor_t>(
0151           std::forward<visitor_t>(visitor));
0152     }
0153   }
0154 
0155   /// @brief Visit all reachable detector volumes of the detector
0156   ///
0157   /// @tparam visitor_t Type of the callable visitor
0158   ///
0159   /// @param visitor will be handed to each root volume,
0160   /// eventually contained volumes within the root volumes are
0161   /// handled by the root volume
0162   ///
0163   /// @note if a context is needed for the visit, the vistitor has to provide
0164   /// it, e.g. as a private member
0165   template <DetectorVolumeVisitor visitor_t>
0166   void visitVolumes(visitor_t&& visitor) const {
0167     for (const auto& v : rootVolumes()) {
0168       v->template visitVolumes<visitor_t>(std::forward<visitor_t>(visitor));
0169     }
0170   }
0171 
0172   /// @brief Visit all reachable detector volumes of the detector - non-const
0173   ///
0174   /// @tparam visitor_t Type of the callable visitor
0175   ///
0176   /// @param visitor will be handed to each root volume,
0177   /// eventually contained volumes within the root volumes are
0178   /// handled by the root volume
0179   ///
0180   /// @note if a context is needed for the visit, the vistitor has to provide
0181   /// it, e.g. as a private member
0182   ///
0183   /// @note that due to non running over root volumes, multiple visits
0184   /// may occur, duplicated addressing needs to be taken care of by the
0185   /// visitor
0186   template <MutableDetectorVolumeVisitor visitor_t>
0187   void visitMutableVolumes(visitor_t&& visitor) {
0188     for (const auto& v : volumePtrs()) {
0189       v->template visitMutableVolumes<visitor_t>(
0190           std::forward<visitor_t>(visitor));
0191     }
0192   }
0193 
0194   /// Update the current volume of a given navigation state
0195   ///
0196   /// @param gctx is the Geometry context of the call
0197   /// @param nState [in, out] is the navigation state
0198   ///
0199   void updateDetectorVolume(const GeometryContext& gctx,
0200                             NavigationState& nState) const;
0201 
0202   /// Find a volume from a position
0203   ///
0204   /// @param gctx is the Geometry context of the call
0205   /// @param position is the position of the call
0206   ///
0207   /// @note this creates internally a NavigationState object
0208   ///
0209   /// @return the volume pointer or nullptr (if outside)
0210   const DetectorVolume* findDetectorVolume(const GeometryContext& gctx,
0211                                            const Vector3& position) const;
0212 
0213   /// Find a volume by name
0214   ///
0215   /// @param name with which the volume is searched for
0216   ///
0217   /// @return the volume pointer or nullptr (if not found)
0218   const DetectorVolume* findDetectorVolume(const std::string& name) const;
0219 
0220   /// Update the volume finder
0221   ///
0222   /// @param detectorVolumeFinder the new volume finder
0223   void updateDetectorVolumeFinder(
0224       ExternalNavigationDelegate detectorVolumeFinder);
0225 
0226   /// Const access to the volume finder
0227   const ExternalNavigationDelegate& detectorVolumeFinder() const;
0228 
0229   /// Return the name of the detector
0230   const std::string& name() const;
0231 
0232  private:
0233   /// Name of the detector
0234   std::string m_name;
0235 
0236   /// Root volumes
0237   DetectorVolume::ObjectStore<std::shared_ptr<DetectorVolume>> m_rootVolumes;
0238 
0239   /// Volume store (internal/external)
0240   DetectorVolume::ObjectStore<std::shared_ptr<DetectorVolume>> m_volumes;
0241 
0242   /// A volume finder delegate
0243   ExternalNavigationDelegate m_volumeFinder;
0244 
0245   /// Name/index map to find volumes by name and detect duplicates
0246   std::unordered_map<std::string, std::size_t> m_volumeNameIndex;
0247 
0248   /// Geometry Id hierarchy map of all sensitive surfaces
0249   GeometryHierarchyMap<const Surface*> m_sensitiveHierarchyMap;
0250 };
0251 
0252 }  // namespace Experimental
0253 }  // namespace Acts