Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-11 09:40:21

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/Units.hpp"
0012 #include "Acts/Detector/ProtoSupport.hpp"
0013 #include "Acts/Geometry/Extent.hpp"
0014 #include "Acts/Surfaces/Surface.hpp"
0015 #include "Acts/Utilities/Logger.hpp"
0016 #include "Acts/Utilities/ProtoAxis.hpp"
0017 
0018 #include <tuple>
0019 #include <vector>
0020 
0021 #include <DD4hep/DD4hepUnits.h>
0022 
0023 class TGeoMatrix;
0024 
0025 namespace dd4hep {
0026 class DetElement;
0027 }
0028 
0029 namespace Acts {
0030 
0031 class ISurfaceMaterial;
0032 
0033 }  // namespace Acts
0034 
0035 namespace ActsPlugins {
0036 class DD4hepDetectorElement;
0037 
0038 /// A factory to convert DD4hep DetectorElements into sensitive
0039 /// of passive surfaces which are filled into a Cache object,
0040 /// also the create DD4hepDetector elements are provided
0041 ///
0042 class DD4hepDetectorSurfaceFactory {
0043  public:
0044   /// DD4hepDetectorElement construction factory
0045   using ElementFactory = std::function<std::shared_ptr<DD4hepDetectorElement>(
0046       const dd4hep::DetElement&, const std::string&, double, bool,
0047       std::shared_ptr<const Acts::ISurfaceMaterial>)>;
0048 
0049   /// Collect the sensitive surface & detector element
0050   using DD4hepSensitiveSurface =
0051       std::tuple<std::shared_ptr<DD4hepDetectorElement>,
0052                  std::shared_ptr<Acts::Surface>>;
0053 
0054   /// Collect the passive surfaces, bool whether it should be
0055   /// added as an "always try, i.e. assignToAll=true" surface
0056   using DD4hepPassiveSurface = std::tuple<std::shared_ptr<Acts::Surface>, bool>;
0057 
0058   /// Configuration struct
0059   struct Config {
0060     /// The factory to create the DD4hepDetectorElement
0061     ElementFactory detectorElementFactory =
0062         [](const dd4hep::DetElement& detElem, const std::string& axes,
0063            double scalor, bool isDisc,
0064            const std::shared_ptr<const Acts::ISurfaceMaterial>& material) {
0065           return std::make_shared<DD4hepDetectorElement>(detElem, axes, scalor,
0066                                                          isDisc, material);
0067         };
0068   };
0069 
0070   /// Nested cache that records the conversion status
0071   struct Cache {
0072     /// The created detector elements - for the detector store
0073     std::vector<DD4hepSensitiveSurface> sensitiveSurfaces;
0074     /// The created non-const surfaces - for further processing,
0075     std::vector<DD4hepPassiveSurface> passiveSurfaces;
0076     /// matching and conversion statistics: surfaces
0077     std::size_t convertedSurfaces = 0;
0078     /// matching and conversion statistics: materials
0079     std::size_t convertedMaterials = 0;
0080     /// The collected binnings
0081     std::vector<std::tuple<Acts::DirectedProtoAxis, std::size_t>> binnings = {};
0082     /// The collected supports
0083     std::vector<Acts::Experimental::ProtoSupport> supports = {};
0084     /// Optionally provide an Extent object to measure the sensitives
0085     std::optional<Acts::Extent> sExtent = std::nullopt;
0086     /// Optionally provide an Extent object to measure the passive
0087     std::optional<Acts::Extent> pExtent = std::nullopt;
0088     /// Optionally provide an Extent constraints to measure the layers
0089     std::vector<Acts::AxisDirection> extentConstraints = {};
0090     /// The approximination of a circle quarter for extent measuring
0091     std::size_t nExtentQSegments = 1u;
0092   };
0093 
0094   /// Nested options struct to steer the conversion
0095   struct Options {
0096     /// Convert sensitive surfaces
0097     bool convertSensitive = true;
0098     /// Convert passive surfaces
0099     bool convertPassive = true;
0100     /// Convert material directly
0101     bool convertMaterial = false;
0102     /// New reference material thickness for surfaces
0103     double surfaceMaterialThickness = 1 * Acts::UnitConstants::mm;
0104   };
0105 
0106   /// The DD4hep detector element factory
0107   ///
0108   /// @param config the configuration struct
0109   /// @param mlogger a screen output logger
0110   explicit DD4hepDetectorSurfaceFactory(
0111       const Config& config,
0112       std::unique_ptr<const Acts::Logger> mlogger = Acts::getDefaultLogger(
0113           "DD4hepDetectorSurfaceFactory", Acts::Logging::INFO));
0114 
0115   /// Construction method of the detector elements
0116   ///
0117   /// @param cache [in,out] into which the Elements are filled
0118   /// @param gctx the geometry context
0119   /// @param dd4hepElement the detector element representing the super structure
0120   /// @param options to steer the conversion
0121   ///
0122   /// @note this method will call the recursive construction
0123   void construct(Cache& cache, const Acts::GeometryContext& gctx,
0124                  const dd4hep::DetElement& dd4hepElement,
0125                  const Options& options);
0126 
0127  private:
0128   /// Auto-calculate the unit length conversion
0129   static constexpr double unitLength =
0130       Acts::UnitConstants::mm / dd4hep::millimeter;
0131 
0132   /// The configuration of the factory
0133   Config m_config;
0134 
0135   /// Logging instance
0136   std::unique_ptr<const Acts::Logger> m_logger;
0137 
0138   /// Private access to the logger
0139   const Acts::Logger& logger() const { return *m_logger; }
0140 
0141   /// Construction method of the detector elements - recursive walk down
0142   ///
0143   /// @param cache [in,out] into which the Elements are filled
0144   /// @param gctx the geometry context
0145   /// @param dd4hepElement the detector element representing the super structure
0146   /// @param options to steer the conversion
0147   /// @param level the current level of the tree, used for log message output
0148   ///
0149   /// @note this method is called recursively
0150   void recursiveConstruct(Cache& cache, const Acts::GeometryContext& gctx,
0151                           const dd4hep::DetElement& dd4hepElement,
0152                           const Options& options, int level);
0153 
0154   /// Method to convert a single sensitive detector element
0155   ///
0156   /// @param cache [in,out] into which the Elements are filled
0157   /// @param gctx the geometry context
0158   /// @param dd4hepElement the detector element
0159   /// @param options to steer the conversion
0160   ///
0161   /// @note the cache is handed through in order to optionally measure the
0162   /// extent of the sensitive surface and register it
0163   ///
0164   /// @return a created detector element and surface
0165   DD4hepSensitiveSurface constructSensitiveComponents(
0166       Cache& cache, const Acts::GeometryContext& gctx,
0167       const dd4hep::DetElement& dd4hepElement, const Options& options) const;
0168 
0169   /// Method to convert a single sensitive detector element
0170   ///
0171   /// @param cache [in,out] into which the Elements are filled
0172   /// @param gctx the geometry context
0173   /// @param dd4hepElement the detector element
0174   /// @param options to steer the conversion
0175   ///
0176   /// @return a created surface
0177   DD4hepPassiveSurface constructPassiveComponents(
0178       Cache& cache, const Acts::GeometryContext& gctx,
0179       const dd4hep::DetElement& dd4hepElement, const Options& options) const;
0180 
0181   /// Attach surface material if present
0182   ///
0183   /// @param gctx the geometry context
0184   /// @param prefix the acts prefix for the variant parameter string
0185   /// @param dd4hepElement the detector element
0186   /// @param surface the surface to attach the material to
0187   /// @param thickness the thickness of the condensed component
0188   /// @param options to steer the conversion
0189   ///
0190   /// @note the cache is handed through in order to optionally measure the
0191   /// extent of the sensitive surface and register it
0192   ///
0193   /// @note void function that also checks the options if the attachment should be applied
0194   void attachSurfaceMaterial(const Acts::GeometryContext& gctx,
0195                              const std::string& prefix,
0196                              const dd4hep::DetElement& dd4hepElement,
0197                              Acts::Surface& surface, double thickness,
0198                              const Options& options) const;
0199 };
0200 
0201 }  // namespace ActsPlugins