Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-20 07:59:41

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/Units.hpp"
0013 #include "Acts/Geometry/GeometryContext.hpp"
0014 #include "Acts/Geometry/ILayerBuilder.hpp"
0015 #include "Acts/Geometry/LayerCreator.hpp"
0016 #include "Acts/Geometry/ProtoLayerHelper.hpp"
0017 #include "Acts/Geometry/SurfaceBinningMatcher.hpp"
0018 #include "Acts/Utilities/BinningType.hpp"
0019 #include "Acts/Utilities/Logger.hpp"
0020 #include "ActsPlugins/Root/ITGeoIdentifierProvider.hpp"
0021 
0022 #include <algorithm>
0023 #include <array>
0024 #include <climits>
0025 #include <functional>
0026 #include <memory>
0027 #include <string>
0028 #include <tuple>
0029 #include <utility>
0030 #include <vector>
0031 
0032 class TGeoMatrix;
0033 class TGeoVolume;
0034 class TGeoNode;
0035 
0036 namespace Acts {
0037 class Surface;
0038 class ISurfaceMaterial;
0039 class ITGeoIdentifierProvider;
0040 class LayerCreator;
0041 class ProtoLayerHelper;
0042 }  // namespace Acts
0043 
0044 namespace ActsPlugins {
0045 
0046 class TGeoDetectorElement;
0047 class ITGeoDetectorElementSplitter;
0048 
0049 /// @class TGeoLayerBuilder
0050 ///
0051 /// This parses the gGeoManager and looks for a defined combination
0052 /// of volume with contained sensitive detector element. The association
0053 /// is done by matching the names of the TGeoNode / TGeoVolume to the search
0054 /// string.
0055 ///
0056 /// The parsing can be restricted to a given parse volume (in r and z),
0057 /// and given some splitting parameters the surfaces can be automatically
0058 /// split into layers.
0059 class TGeoLayerBuilder : public Acts::ILayerBuilder {
0060  public:
0061   ///  Helper config structs for volume parsing
0062   struct LayerConfig {
0063    public:
0064     using RangeConfig =
0065         std::pair<Acts::AxisDirection, std::pair<double, double>>;
0066     /// Type alias for range configuration with axis direction and bounds
0067 
0068     using SplitConfig = std::pair<Acts::AxisDirection, double>;
0069     /// Type alias for split configuration with axis direction and position
0070 
0071     /// Identify the search volume by name
0072     std::string volumeName = "";
0073     /// Identify the sensor(s) by name
0074     std::vector<std::string> sensorNames = {};
0075     /// The local axis definition of TGeo object to Acts::Surface
0076     std::string localAxes = "XYZ";
0077     /// Parse ranges: parameter and ranges
0078     std::vector<RangeConfig> parseRanges = {};
0079     /// Layer splitting: parameter and tolerance
0080     std::vector<SplitConfig> splitConfigs = {};
0081     /// The envelope to be built around the layer
0082     std::pair<double, double> envelope = {1 * Acts::UnitConstants::mm,
0083                                           1 * Acts::UnitConstants::mm};
0084     /// Binning setup in l0: nbins (-1 -> automated), axis binning type
0085     std::vector<std::pair<int, Acts::BinningType>> binning0 = {
0086         {-1, Acts::equidistant}};
0087     /// Binning setup in l1: nbins (-1 -> automated), axis binning type
0088     std::vector<std::pair<int, Acts::BinningType>> binning1 = {
0089         {-1, Acts::equidistant}};
0090 
0091     // Default constructor
0092     LayerConfig() = default;
0093   };
0094 
0095   /// Type alias for detector element factory function
0096   using ElementFactory = std::function<std::shared_ptr<TGeoDetectorElement>(
0097       const TGeoDetectorElement::Identifier&, const TGeoNode&,
0098       const TGeoMatrix& tGeoMatrix, const std::string& axes, double scalor,
0099       std::shared_ptr<const Acts::ISurfaceMaterial> material)>;
0100 
0101   /// Default factory function for creating TGeoDetectorElement instances.
0102   /// @param identifier Detector element identifier
0103   /// @param tGeoNode ROOT geometry node
0104   /// @param tGeoMatrix ROOT geometry transformation matrix
0105   /// @param axes String describing the axes configuration
0106   /// @param scalor Scaling factor for geometry transformations
0107   /// @param material Surface material to be assigned to the element
0108   /// @return Shared pointer to the created TGeoDetectorElement
0109   static std::shared_ptr<TGeoDetectorElement> defaultElementFactory(
0110       const TGeoDetectorElement::Identifier& identifier,
0111       const TGeoNode& tGeoNode, const TGeoMatrix& tGeoMatrix,
0112       const std::string& axes, double scalor,
0113       std::shared_ptr<const Acts::ISurfaceMaterial> material);
0114 
0115   /// @struct Config
0116   /// @brief nested configuration struct for steering of the layer builder
0117   struct Config {
0118     /// String based identification
0119     std::string configurationName = "undefined";
0120     /// Unit conversion
0121     double unit = 1 * Acts::UnitConstants::cm;
0122     /// Create an identifier from TGeoNode
0123     std::shared_ptr<const ITGeoIdentifierProvider> identifierProvider = nullptr;
0124     /// Split TGeoElement if a splitter is provided
0125     std::shared_ptr<const ITGeoDetectorElementSplitter>
0126         detectorElementSplitter = nullptr;
0127     /// Factory for creating detector elements based on TGeoNodes
0128     ElementFactory detectorElementFactory = defaultElementFactory;
0129     /// Layer creator
0130     std::shared_ptr<const Acts::LayerCreator> layerCreator = nullptr;
0131     /// ProtoLayer helper
0132     std::shared_ptr<const Acts::ProtoLayerHelper> protoLayerHelper = nullptr;
0133     /// Configuration is always | n | c | p |
0134     std::array<std::vector<LayerConfig>, 3> layerConfigurations;
0135     /// Split tolerances in R
0136     std::array<double, 3> layerSplitToleranceR = {-1., -1., -1.};
0137     /// Split tolerances in Z
0138     std::array<double, 3> layerSplitToleranceZ = {-1., -1., -1.};
0139     /// Automated binning & tolerances
0140     bool autoSurfaceBinning = false;
0141     /// The surface binning matcher
0142     Acts::SurfaceBinningMatcher surfaceBinMatcher;
0143   };
0144 
0145   /// Constructor
0146   /// @param config is the configuration struct
0147   /// @param logger the local logging instance
0148   explicit TGeoLayerBuilder(const Config& config,
0149                             std::unique_ptr<const Acts::Logger> logger =
0150                                 Acts::getDefaultLogger("TGeoLayerBuilder",
0151                                                        Acts::Logging::INFO));
0152 
0153   /// Destructor
0154   ~TGeoLayerBuilder() override;
0155 
0156   /// LayerBuilder interface method - returning the layers at negative side
0157   ///
0158   /// @param gctx the geometry context for this build call
0159   ///
0160   /// @return Vector of negative-side layers
0161   const Acts::LayerVector negativeLayers(
0162       const Acts::GeometryContext& gctx) const final;
0163 
0164   /// LayerBuilder interface method - returning the central layers
0165   ///
0166   /// @param gctx the geometry context for this build call
0167   ///
0168   /// @return Vector of central layers
0169   const Acts::LayerVector centralLayers(
0170       const Acts::GeometryContext& gctx) const final;
0171 
0172   /// LayerBuilder interface method - returning the layers at positive side
0173   ///
0174   /// @param gctx the geometry context for this build call
0175   ///
0176   /// @return Vector of positive-side layers
0177   const Acts::LayerVector positiveLayers(
0178       const Acts::GeometryContext& gctx) const final;
0179 
0180   /// Name identification
0181   /// @return Reference to the configuration name string
0182   const std::string& identification() const final;
0183 
0184   /// Set the configuration object
0185   /// @param config is the configuration struct
0186   void setConfiguration(const Config& config);
0187 
0188   /// Get the configuration object
0189   /// @return Copy of the current configuration
0190   Config getConfiguration() const;
0191 
0192   /// Set logging instance
0193   /// @param newLogger Logger instance to use for debug output
0194   void setLogger(std::unique_ptr<const Acts::Logger> newLogger);
0195 
0196   /// Return the created detector elements
0197   /// @return Reference to vector of detector elements
0198   const std::vector<std::shared_ptr<const TGeoDetectorElement>>&
0199   detectorElements() const;
0200 
0201  private:
0202   /// Configuration object
0203   Config m_cfg;
0204 
0205   /// layer types
0206   std::array<std::string, 3> m_layerTypes = {"Negative", "Central", "Positive"};
0207 
0208   /// Private access to the logger
0209   const Acts::Logger& logger() const { return *m_logger; }
0210 
0211   /// Logging instance
0212   std::unique_ptr<const Acts::Logger> m_logger;
0213 
0214   /// @todo make clear where the TGeoDetectorElement lives
0215   std::vector<std::shared_ptr<const TGeoDetectorElement>> m_elementStore;
0216 
0217   /// Private helper method : build layers
0218   ///
0219   /// @param gctx the geometry context of this call
0220   /// @param layers is goint to be filled
0221   /// @param type is the indication which ones to build -1 | 0 | 1
0222   void buildLayers(const Acts::GeometryContext& gctx, Acts::LayerVector& layers,
0223                    int type = 0);
0224 
0225   /// Private helper method : register splitting input
0226   void registerSplit(std::vector<double>& parameters, double test,
0227                      double tolerance, std::pair<double, double>& range) const;
0228 };
0229 
0230 inline void TGeoLayerBuilder::registerSplit(
0231     std::vector<double>& parameters, double test, double tolerance,
0232     std::pair<double, double>& range) const {
0233   bool found = false;
0234   // min/max setting
0235   range.first = std::min(range.first, test);
0236   range.second = std::max(range.second, test);
0237   // Loop and find the split parameters
0238   for (const auto& splitPar : parameters) {
0239     if (std::abs(test - splitPar) < tolerance) {
0240       found = true;
0241     }
0242   }
0243   if (!found) {
0244     parameters.push_back(test);
0245   }
0246 }
0247 
0248 inline TGeoLayerBuilder::Config TGeoLayerBuilder::getConfiguration() const {
0249   return m_cfg;
0250 }
0251 
0252 inline const std::vector<std::shared_ptr<const TGeoDetectorElement>>&
0253 TGeoLayerBuilder::detectorElements() const {
0254   return m_elementStore;
0255 }
0256 
0257 inline const std::string& TGeoLayerBuilder::identification() const {
0258   return m_cfg.configurationName;
0259 }
0260 
0261 }  // namespace ActsPlugins