Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-08 08:00:33

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/Geometry/BlueprintBuilder.hpp"
0012 #include "Acts/Utilities/Logger.hpp"
0013 #include "ActsPlugins/Root/TGeoAxes.hpp"
0014 #include "ActsPlugins/Root/TGeoDetectorElement.hpp"
0015 
0016 #include <functional>
0017 #include <memory>
0018 #include <optional>
0019 #include <span>
0020 #include <string>
0021 #include <string_view>
0022 #include <vector>
0023 
0024 #include "TGeoMatrix.h"
0025 
0026 class TGeoNode;
0027 
0028 namespace ActsPlugins {
0029 
0030 /// Backend adapter for building ACTS blueprints from a TGeo geometry tree.
0031 class TGeoBlueprintBuilderBackend {
0032  public:
0033   /// Identifier used by the generic blueprint infrastructure.
0034   static constexpr std::string_view kIdentifier = "TGeoBlueprintBuilderBackend";
0035 
0036   /// Context for one node in the TGeo hierarchy.
0037   struct NodeContext {
0038     /// Current TGeo node.
0039     const TGeoNode* node = nullptr;
0040     /// Parent node context in the hierarchy (null for the root element).
0041     std::shared_ptr<const NodeContext> parent = nullptr;
0042   };
0043 
0044   /// Lightweight backend element that points to a node context.
0045   struct Element {
0046     /// Shared node context for this element.
0047     std::shared_ptr<const NodeContext> context = nullptr;
0048 
0049     friend bool operator==(const Element& lhs, const Element& rhs) {
0050       const NodeContext* lc = lhs.context.get();
0051       const NodeContext* rc = rhs.context.get();
0052 
0053       while (lc != nullptr && rc != nullptr) {
0054         if (lc->node != rc->node) {
0055           return false;
0056         }
0057         lc = lc->parent.get();
0058         rc = rc->parent.get();
0059       }
0060 
0061       return lc == nullptr && rc == nullptr;
0062     }
0063   };
0064 
0065   /// Axis definitions used for detector element and layer surface creation.
0066   using AxisDefinition = TGeoAxes;
0067   /// Optional layer configuration used during layer assembly.
0068   struct LayerSpec {
0069     /// Optional override for sensitive element axes.
0070     std::optional<AxisDefinition> axes;
0071     /// Optional override for the assembled layer surface axes.
0072     std::optional<AxisDefinition> layerAxes;
0073     /// Optional override for the assembled layer name.
0074     std::optional<std::string> layerName;
0075   };
0076 
0077   /// Concrete detector element type used by this backend.
0078   using DetectorElement = TGeoDetectorElement;
0079   /// Shared pointer to the detector element type.
0080   using DetectorElementPtr = std::shared_ptr<DetectorElement>;
0081   /// Factory function used to instantiate detector elements.
0082   using DetectorElementFactory = std::function<DetectorElementPtr(
0083       const TGeoDetectorElement::Identifier&, const TGeoNode&,
0084       const TGeoMatrix&, AxisDefinition, double,
0085       std::shared_ptr<const Acts::ISurfaceMaterial>)>;
0086   /// Predicate used to classify geometry elements as sensitive.
0087   using ElementPredicate = std::function<bool(const Element&)>;
0088   /// Provider that maps an element to a detector element identifier.
0089   using IdentifierProvider =
0090       std::function<TGeoDetectorElement::Identifier(const Element&)>;
0091 
0092   /// Default detector element factory implementation.
0093   /// @param identifier Identifier assigned to the detector element.
0094   /// @param tGeoNode Source TGeo node.
0095   /// @param tGeoMatrix Global transform matrix of the source node.
0096   /// @param axes Axis definition used to build the ACTS surface.
0097   /// @param lengthScale Scale factor converting TGeo lengths to ACTS units.
0098   /// @param material Optional surface material assigned to created surfaces.
0099   /// @return Shared detector element instance.
0100   static DetectorElementPtr defaultElementFactory(
0101       const TGeoDetectorElement::Identifier& identifier,
0102       const TGeoNode& tGeoNode, const TGeoMatrix& tGeoMatrix,
0103       AxisDefinition axes, double lengthScale,
0104       std::shared_ptr<const Acts::ISurfaceMaterial> material);
0105 
0106   /// Runtime configuration for the TGeo blueprint backend.
0107   struct Config {
0108     /// Root node of the input TGeo geometry tree.
0109     const TGeoNode* root = nullptr;
0110     /// Scale factor converting TGeo lengths into ACTS units.
0111     double lengthScale = 1.0;
0112     /// Factory used to construct detector elements for sensitive nodes.
0113     DetectorElementFactory elementFactory = defaultElementFactory;
0114     /// Predicate deciding whether an element should be treated as sensitive.
0115     ElementPredicate sensitivePredicate = {};
0116     /// Provider used to compute detector element identifiers.
0117     IdentifierProvider identifierProvider = {};
0118   };
0119 
0120   /// Construct the backend from configuration and logger.
0121   /// @param cfg Backend configuration.
0122   /// @param logger Logger instance used for diagnostics.
0123   explicit TGeoBlueprintBuilderBackend(const Config& cfg,
0124                                        const Acts::Logger& logger);
0125 
0126   /// Create a detector element for a sensitive geometry element.
0127   /// @param element Sensitive element to convert.
0128   /// @param axes Axis definition used to build the detector element surface.
0129   /// @return Shared detector element instance.
0130   DetectorElementPtr createDetectorElement(const Element& element,
0131                                            AxisDefinition axes) const;
0132 
0133   /// Build ACTS surfaces for a set of sensitive elements.
0134   /// @param sensitives Sensitive elements that contribute surfaces.
0135   /// @param layerSpec Optional layer assembly overrides.
0136   /// @return Created ACTS surfaces.
0137   std::vector<std::shared_ptr<Acts::Surface>> makeSurfaces(
0138       std::span<const Element> sensitives, const LayerSpec& layerSpec) const;
0139 
0140   /// Look up an optional transform to use for the assembled layer.
0141   /// @param element Reference element for the lookup.
0142   /// @param layerSpec Optional layer assembly overrides.
0143   /// @return Layer transform if one can be determined.
0144   std::optional<Acts::Transform3> lookupLayerTransform(
0145       const Element& element, const LayerSpec& layerSpec) const;
0146 
0147   /// Access the world/root element of the configured TGeo tree.
0148   /// @return Root element of the geometry hierarchy.
0149   Element world() const;
0150   /// Retrieve the node name for an element.
0151   /// @param element Element whose node name is requested.
0152   /// @return Node name.
0153   std::string nameOf(const Element& element) const;
0154   /// Retrieve the direct children of a parent element.
0155   /// @param parent Parent element.
0156   /// @return Direct child elements.
0157   std::vector<Element> children(const Element& parent) const;
0158   /// Retrieve the parent element of an element.
0159   /// @param element Element whose parent is requested.
0160   /// @return Parent element.
0161   Element parent(const Element& element) const;
0162 
0163   /// Check whether an element is considered sensitive.
0164   /// @param element Element to classify.
0165   /// @return True if the element is sensitive.
0166   bool isSensitive(const Element& element) const;
0167 
0168   /// Access the underlying TGeo node for an element.
0169   /// @param element Element whose node is requested.
0170   /// @return Referenced TGeo node.
0171   const TGeoNode& nodeOf(const Element& element) const;
0172   /// Compute the world transform of an element.
0173   /// @param element Element whose transform is requested.
0174   /// @return Global transformation matrix.
0175   TGeoHMatrix transformOf(const Element& element) const;
0176   /// Compute the full geometry path of an element.
0177   /// @param element Element whose path is requested.
0178   /// @return Full geometry path.
0179   std::string pathOf(const Element& element) const;
0180 
0181   /// Access the backend logger.
0182   /// @return Backend logger instance.
0183   const Acts::Logger& logger() const { return *m_logger; }
0184 
0185  private:
0186   const NodeContext& contextOf(const Element& element) const;
0187   Element makeElement(const TGeoNode& node,
0188                       std::shared_ptr<const NodeContext> parent) const;
0189   TGeoDetectorElement::Identifier defaultIdentifierFor(
0190       const Element& element) const;
0191 
0192   Config m_cfg;
0193   const Acts::Logger* m_logger = nullptr;
0194   Element m_world = {};
0195   mutable std::vector<DetectorElementPtr> m_detectorElementStore = {};
0196 };
0197 
0198 /// Blueprint builder specialization for the TGeo backend.
0199 using BlueprintBuilder =
0200     Acts::Experimental::BlueprintBuilder<TGeoBlueprintBuilderBackend>;
0201 /// Element-layer assembler specialization for the TGeo backend.
0202 using ElementLayerAssembler =
0203     Acts::Experimental::ElementLayerAssembler<TGeoBlueprintBuilderBackend>;
0204 /// Sensor-layer assembler specialization for the TGeo backend.
0205 using SensorLayerAssembler =
0206     Acts::Experimental::SensorLayerAssembler<TGeoBlueprintBuilderBackend>;
0207 /// Sensor-layer alias for the TGeo backend.
0208 using SensorLayer =
0209     Acts::Experimental::SensorLayer<TGeoBlueprintBuilderBackend>;
0210 
0211 }  // namespace ActsPlugins
0212 
0213 namespace Acts::Experimental {
0214 extern template class BlueprintBuilder<
0215     ActsPlugins::TGeoBlueprintBuilderBackend>;
0216 extern template class ElementLayerAssembler<
0217     ActsPlugins::TGeoBlueprintBuilderBackend>;
0218 extern template class SensorLayerAssembler<
0219     ActsPlugins::TGeoBlueprintBuilderBackend>;
0220 extern template class SensorLayer<ActsPlugins::TGeoBlueprintBuilderBackend>;
0221 }  // namespace Acts::Experimental