Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-06-22 08:01:24

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/Geometry/StaticBlueprintNode.hpp"
0013 #include "Acts/Utilities/Logger.hpp"
0014 #include "ActsPlugins/DD4hep/DD4hepDetectorElement.hpp"
0015 #include "ActsPlugins/Root/TGeoAxes.hpp"
0016 
0017 #include <format>
0018 #include <functional>
0019 #include <memory>
0020 #include <optional>
0021 #include <span>
0022 #include <string>
0023 #include <string_view>
0024 #include <vector>
0025 
0026 #include <DD4hep/Detector.h>
0027 
0028 namespace dd4hep {
0029 class DetElement;
0030 }  // namespace dd4hep
0031 
0032 namespace ActsPlugins::DD4hep {
0033 
0034 /// Backend adapter that maps a DD4hep detector hierarchy onto the ACTS
0035 /// experimental blueprint-builder interface.
0036 ///
0037 /// The backend exposes DD4hep detector elements as blueprint input elements
0038 /// and provides the conversions needed to create ACTS surfaces, detector
0039 /// elements, and optional layer transforms.
0040 class DD4hepBackend {
0041  public:
0042   /// Identifier string used in diagnostics emitted by the generic blueprint
0043   /// builder.
0044   static constexpr std::string_view kIdentifier = "DD4hepBackend";
0045 
0046   /// DD4hep detector-element handle type consumed by the generic builder.
0047   using Element = dd4hep::DetElement;
0048   /// Axis-definition type forwarded to ROOT-based surface conversion helpers.
0049   using AxisDefinition = TGeoAxes;
0050   /// Layer-specific configuration forwarded from the generic builder to the
0051   /// DD4hep backend.
0052   struct LayerSpec {
0053     /// Sensitive-surface axis convention.
0054     std::optional<AxisDefinition> axes;
0055     /// Optional axis convention used to derive the layer transform.
0056     std::optional<AxisDefinition> layerAxes;
0057     /// Optional explicit layer name override.
0058     std::optional<std::string> layerName;
0059   };
0060   /// Concrete ACTS detector-element implementation used for DD4hep geometry.
0061   using DetectorElement = DD4hepDetectorElement;
0062   /// Shared pointer to a DD4hep-backed ACTS detector element.
0063   using DetectorElementPtr = std::shared_ptr<DetectorElement>;
0064   /// Factory that creates detector elements for converted DD4hep sensitives.
0065   using DetectorElementFactory = std::function<DetectorElementPtr(
0066       const Element& detElement, AxisDefinition axes, double lengthScale)>;
0067 
0068   /// Default detector-element factory used when @ref Config::elementFactory is
0069   /// not overridden.
0070   /// @param detElement DD4hep sensitive detector element to wrap.
0071   /// @param axes Axis convention used for surface conversion.
0072   /// @param lengthScale Unit scale applied during geometry conversion.
0073   /// @return Newly created DD4hep-backed ACTS detector element.
0074   static DetectorElementPtr defaultElementFactory(const Element& detElement,
0075                                                   AxisDefinition axes,
0076                                                   double lengthScale);
0077 
0078   /// Configuration of the DD4hep backend instance.
0079   struct Config {
0080     /// Factory used to create ACTS detector elements from DD4hep sensitives.
0081     DetectorElementFactory elementFactory = defaultElementFactory;
0082     /// DD4hep detector description that owns the world hierarchy.
0083     const dd4hep::Detector* dd4hepDetector;
0084     /// Unit scale applied when converting DD4hep lengths to ACTS units.
0085     double lengthScale = 1.0;
0086     /// Geometry context used when constructing ACTS detector elements.
0087     std::reference_wrapper<const Acts::GeometryContext> gctx;
0088   };
0089 
0090   /// Construct the DD4hep backend.
0091   /// @param cfg Backend configuration and DD4hep detector handle.
0092   /// @param logger Logger used for diagnostics.
0093   explicit DD4hepBackend(const Config& cfg, const Acts::Logger& logger);
0094 
0095   /// Create an ACTS detector element from a DD4hep sensitive element.
0096   /// @param detElement DD4hep sensitive element to convert.
0097   /// @param axes Axis convention used for the converted surface.
0098   /// @return Shared pointer to the created detector element.
0099   DetectorElementPtr createDetectorElement(const Element& detElement,
0100                                            AxisDefinition axes) const;
0101 
0102   /// Convert a set of DD4hep sensitive elements into ACTS surfaces.
0103   /// @param sensitives Sensitive DD4hep elements belonging to one layer.
0104   /// @param layerSpec Layer configuration controlling axes and naming.
0105   /// @return Converted ACTS surfaces for the given sensitives.
0106   std::vector<std::shared_ptr<Acts::Surface>> makeSurfaces(
0107       std::span<const Element> sensitives, const LayerSpec& layerSpec) const;
0108 
0109   /// Derive the layer transform from a DD4hep detector element when possible.
0110   /// @param element DD4hep element providing the geometric context.
0111   /// @param layerSpec Layer configuration controlling the transform lookup.
0112   /// @return The derived layer transform, or `std::nullopt` if none is
0113   ///         available.
0114   std::optional<Acts::Transform3> lookupLayerTransform(
0115       const Element& element, const LayerSpec& layerSpec) const;
0116 
0117   /// Create a static beampipe blueprint node from the DD4hep world geometry.
0118   /// @return Shared pointer to the generated beampipe node.
0119   std::shared_ptr<Acts::Experimental::StaticBlueprintNode> makeBeampipe() const;
0120 
0121   /// Return the DD4hep world detector element.
0122   /// @return Root detector element of the DD4hep hierarchy.
0123   Element world() const;
0124   /// Return the fully qualified DD4hep name of an element.
0125   /// @param element DD4hep detector element to inspect.
0126   /// @return Full element name.
0127   std::string nameOf(const Element& element) const;
0128   /// Return the direct DD4hep child elements of a parent element.
0129   /// @param parent Parent DD4hep detector element.
0130   /// @return Direct children of @p parent.
0131   std::vector<Element> children(const Element& parent) const;
0132   /// Return the direct parent DD4hep element of a child element.
0133   /// @param element Child DD4hep detector element.
0134   /// @return Parent detector element.
0135   Element parent(const Element& element) const;
0136 
0137   /// Check whether a DD4hep element represents a sensitive detector element.
0138   /// @param element DD4hep detector element to classify.
0139   /// @return `true` if the element is sensitive.
0140   bool isSensitive(const Element& element) const;
0141   /// Check whether a DD4hep element represents a barrel sub-detector.
0142   /// @param element DD4hep detector element to classify.
0143   /// @return `true` if the element is tagged as barrel.
0144   bool isBarrel(const Element& element) const;
0145   /// Check whether a DD4hep element represents an endcap sub-detector.
0146   /// @param element DD4hep detector element to classify.
0147   /// @return `true` if the element is tagged as endcap.
0148   bool isEndcap(const Element& element) const;
0149   /// Check whether a DD4hep element is part of the tracking detector.
0150   /// @param element DD4hep detector element to classify.
0151   /// @return `true` if the element belongs to the tracker.
0152   bool isTracker(const Element& element) const;
0153 
0154   /// Retrieves a named integer constant from the DD4hep detector description.
0155   /// The name is constructed by formatting @p fmt with @p args.
0156   /// @tparam Args Types used to format the constant name.
0157   /// @param fmt Format string used to construct the DD4hep constant name.
0158   /// @param args Format arguments substituted into @p fmt.
0159   /// @return Integer constant value stored in the DD4hep detector description.
0160   template <typename... Args>
0161   int constant(std::format_string<Args...> fmt, Args&&... args) const {
0162     return m_cfg.dd4hepDetector->constant<int>(
0163         std::format(fmt, std::forward<Args>(args)...));
0164   }
0165 
0166   /// Return the logger associated with this backend.
0167   /// @return Logger used for diagnostics.
0168   const Acts::Logger& logger() const { return *m_logger; }
0169 
0170  private:
0171   Config m_cfg;
0172   const Acts::Logger* m_logger;
0173 };
0174 
0175 using BlueprintBuilder = Acts::Experimental::BlueprintBuilder<DD4hepBackend>;
0176 using ElementLayerAssembler =
0177     Acts::Experimental::ElementLayerAssembler<DD4hepBackend>;
0178 using SensorLayerAssembler =
0179     Acts::Experimental::SensorLayerAssembler<DD4hepBackend>;
0180 using SensorLayer = Acts::Experimental::SensorLayer<DD4hepBackend>;
0181 using BarrelEndcapAssembler =
0182     Acts::Experimental::BarrelEndcapAssembler<DD4hepBackend>;
0183 
0184 }  // namespace ActsPlugins::DD4hep
0185 
0186 // Explicit instantiation: suppress implicit instantiation in TUs that include
0187 // this header. Definitions are instantiated in BlueprintBuilder.cpp.
0188 // Placed at global scope so we open ::Acts::Experimental, not a nested Acts.
0189 namespace Acts::Experimental {
0190 extern template class BlueprintBuilder<ActsPlugins::DD4hep::DD4hepBackend>;
0191 extern template class ElementLayerAssembler<ActsPlugins::DD4hep::DD4hepBackend>;
0192 extern template class SensorLayerAssembler<ActsPlugins::DD4hep::DD4hepBackend>;
0193 extern template class SensorLayer<ActsPlugins::DD4hep::DD4hepBackend>;
0194 extern template class BarrelEndcapAssembler<ActsPlugins::DD4hep::DD4hepBackend>;
0195 }  // namespace Acts::Experimental