Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:12:17

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 #include "Acts/Plugins/DD4hep/DD4hepBlueprintFactory.hpp"
0010 
0011 #include "Acts/Detector/GeometryIdGenerator.hpp"
0012 #include "Acts/Detector/IndexedRootVolumeFinderBuilder.hpp"
0013 #include "Acts/Plugins/DD4hep/DD4hepBinningHelpers.hpp"
0014 #include "Acts/Plugins/DD4hep/DD4hepConversionHelpers.hpp"
0015 #include "Acts/Utilities/BinningData.hpp"
0016 #include "Acts/Utilities/StringHelpers.hpp"
0017 
0018 #include <sstream>
0019 
0020 Acts::Experimental::DD4hepBlueprintFactory::DD4hepBlueprintFactory(
0021     const Config& cfg, std::unique_ptr<const Logger> mlogger)
0022     : m_cfg(cfg), m_logger(std::move(mlogger)) {
0023   ACTS_DEBUG("UnitLength conversion factor (DD4hep -> Acts): " << unitLength);
0024 }
0025 
0026 std::unique_ptr<Acts::Experimental::Blueprint::Node>
0027 Acts::Experimental::DD4hepBlueprintFactory::create(
0028     Cache& cache, const GeometryContext& gctx,
0029     const dd4hep::DetElement& dd4hepElement) const {
0030   ACTS_DEBUG("Drawing a blueprint from the DD4hep element '"
0031              << dd4hepElement.name() << "'.");
0032 
0033   // Create the root node
0034   std::vector<double> bValues = {0., 150., 1000.};
0035   std::vector<AxisDirection> binning = {Acts::AxisDirection::AxisR};
0036   auto root = std::make_unique<Acts::Experimental::Blueprint::Node>(
0037       dd4hepElement.name(), Acts::Transform3::Identity(),
0038       Acts::VolumeBounds::eCylinder, bValues, binning);
0039 
0040   // Recursively parse the tree
0041   recursiveParse(cache, *root, gctx, dd4hepElement);
0042   // Return the top node
0043   return root;
0044 }
0045 
0046 void Acts::Experimental::DD4hepBlueprintFactory::recursiveParse(
0047     Cache& cache, Blueprint::Node& mother, const GeometryContext& gctx,
0048     const dd4hep::DetElement& dd4hepElement, unsigned int hiearchyLevel) const {
0049   // This will allow to skip empty hierarchy levels
0050   Blueprint::Node* current = &mother;
0051   unsigned int hierarchyAddOn = 0;
0052 
0053   std::string ofs(hiearchyLevel * 2u, ' ');
0054 
0055   // Node types
0056   std::vector<std::string> nodeTypes = {"acts_world", "acts_container",
0057                                         "acts_volume"};
0058   for (const auto& nType : nodeTypes) {
0059     // Check if it complies with the given definition
0060     bool ntt = getParamOr<bool>(nType, dd4hepElement, false);
0061     if (ntt) {
0062       ACTS_DEBUG(ofs << "ACTS node '" << nType
0063                      << "' attached to dd4hep element '" << dd4hepElement.name()
0064                      << "',");
0065       // Extract potential internal builders and tools
0066       auto [internalsBuilder, rootsFinderBuilder, geoIdGenerator, auxInt,
0067             extOpt] =
0068           extractInternals(cache.dd4hepStore, gctx, dd4hepElement, nType);
0069       // Extract the bounds type, values and binning
0070       auto [transform, bValueType, bValues, binning, auxExt] =
0071           extractExternals(gctx, dd4hepElement, nType, extOpt);
0072       // Screen output of position and shape
0073       ACTS_DEBUG(ofs << " - translation  : "
0074                      << toString(transform.translation()));
0075       ACTS_DEBUG(ofs << " - bounds type  : " << bValueType);
0076       ACTS_DEBUG(ofs << " - bound values : " << toString(bValues));
0077       // If it is not the world node, create a new one
0078       if (nType == "acts_world") {
0079         mother.transform = transform;
0080         mother.boundsType = bValueType;
0081         mother.boundaryValues = bValues;
0082         mother.binning = binning;
0083 
0084       } else if (nType == "acts_container") {
0085         // Creating the branch node
0086         auto branch = std::make_unique<Acts::Experimental::Blueprint::Node>(
0087             dd4hepElement.name(), transform, bValueType, bValues, binning);
0088         current = branch.get();
0089         mother.add(std::move(branch));
0090 
0091       } else if (nType == "acts_volume") {
0092         // Crreating a leaf node
0093         auto leaf = std::make_unique<Acts::Experimental::Blueprint::Node>(
0094             dd4hepElement.name(), transform, bValueType, bValues);
0095         current = leaf.get();
0096         mother.add(std::move(leaf));
0097       }
0098       // Current is set now appropriately, adding auxiliary information
0099       if (!auxExt.empty()) {
0100         ACTS_VERBOSE(ofs << " - " << auxExt);
0101         current->auxiliary.push_back(auxExt);
0102       }
0103       // Adding the internals builder - if present
0104       if (internalsBuilder != nullptr) {
0105         ACTS_VERBOSE(ofs << " - " << auxInt[0u]);
0106         current->internalsBuilder = internalsBuilder;
0107       }
0108       // Adding root finder builder - if present
0109       if (rootsFinderBuilder != nullptr) {
0110         ACTS_VERBOSE(ofs << " - " << auxInt[1u]);
0111         current->rootVolumeFinderBuilder = rootsFinderBuilder;
0112       }
0113 
0114       // Check for proto material for the portals, max portal number
0115       // can be changed in configuration
0116       for (unsigned int p = 0u; p < m_cfg.maxPortals; ++p) {
0117         std::string pmName = "acts_portal_proto_material_" + std::to_string(p);
0118         auto protoMaterial = getParamOr<bool>(pmName, dd4hepElement, false);
0119         if (protoMaterial) {
0120           ACTS_VERBOSE(ofs << " - proto material binning for portal " << p
0121                            << " found");
0122           auto pmProtoBinnings = DD4hepBinningHelpers::convertBinning(
0123               dd4hepElement, pmName + "_binning");
0124           current->portalMaterialBinning[p] =
0125               BinningDescription{pmProtoBinnings};
0126           ACTS_VERBOSE(ofs << " - binning description is "
0127                            << current->portalMaterialBinning[p].toString());
0128         }
0129       }
0130 
0131       // Adding geo Id generator - if present
0132       if (geoIdGenerator != nullptr) {
0133         ACTS_VERBOSE(ofs << " - " << auxInt[2u]);
0134         current->geoIdGenerator = geoIdGenerator;
0135       }
0136     }
0137   }
0138 
0139   // Step down to the children - not possible for leaf nodes
0140   const dd4hep::DetElement::Children& children = dd4hepElement.children();
0141   if (!children.empty()) {
0142     ACTS_VERBOSE(ofs << "dd4hep element '" << dd4hepElement.name() << "' has "
0143                      << children.size() << " children.");
0144     for (auto& child : children) {
0145       dd4hep::DetElement dd4hepChild = child.second;
0146       recursiveParse(cache, *current, gctx, dd4hepChild,
0147                      hiearchyLevel + hierarchyAddOn);
0148     }
0149   }
0150 }
0151 
0152 std::tuple<Acts::Transform3, Acts::VolumeBounds::BoundsType,
0153            std::vector<double>, std::vector<Acts::AxisDirection>, std::string>
0154 Acts::Experimental::DD4hepBlueprintFactory::extractExternals(
0155     [[maybe_unused]] const GeometryContext& gctx,
0156     const dd4hep::DetElement& dd4hepElement, const std::string& baseName,
0157     const std::optional<Extent>& extOpt) const {
0158   std::string aux = "";
0159 
0160   /// Get the transform - extract from values first
0161   auto transform = extractTransform(dd4hepElement, baseName, unitLength);
0162 
0163   // Get the bounds type
0164   auto bValueInt =
0165       getParamOr<int>(baseName + "_type", dd4hepElement,
0166                       static_cast<int>(VolumeBounds::BoundsType::eOther));
0167   auto bValueType = static_cast<VolumeBounds::BoundsType>(bValueInt);
0168   std::vector<double> bValues = {};
0169 
0170   // Get the bound values from parsed internals if possible
0171   if (extOpt.has_value() && bValueType == VolumeBounds::BoundsType::eCylinder) {
0172     // Set as defaults
0173     bValues = {0., 0., 0.};
0174     auto parsedExtent = extOpt.value();
0175     if (parsedExtent.constrains(AxisDirection::AxisR)) {
0176       bValues[0u] = std::floor(parsedExtent.min(AxisDirection::AxisR));
0177       bValues[1u] = std::ceil(parsedExtent.max(AxisDirection::AxisR));
0178     }
0179     if (parsedExtent.constrains(AxisDirection::AxisZ)) {
0180       double minZ = parsedExtent.min(AxisDirection::AxisZ) > 0.
0181                         ? std::floor(parsedExtent.min(AxisDirection::AxisZ))
0182                         : std::ceil(parsedExtent.min(AxisDirection::AxisZ));
0183       double maxZ = parsedExtent.max(AxisDirection::AxisZ) > 0.
0184                         ? std::floor(parsedExtent.max(AxisDirection::AxisZ))
0185                         : std::ceil(parsedExtent.max(AxisDirection::AxisZ));
0186       bValues[2u] = 0.5 * (maxZ - minZ);
0187       transform.translation().z() = 0.5 * (maxZ + minZ);
0188     }
0189     ACTS_VERBOSE("   cylindrical bounds determined from internals as "
0190                  << toString(bValues));
0191   }
0192 
0193   // Get the bounds values from the series if not found before
0194   if (bValues.empty()) {
0195     bValues =
0196         extractSeries<double>(dd4hepElement, baseName + "_bvalues", unitLength);
0197     ACTS_VERBOSE(" - cylindrical determined from variant parameters as "
0198                  << toString(bValues));
0199   }
0200 
0201   // Get the binning values
0202   auto binningString =
0203       getParamOr<std::string>(baseName + "_binning", dd4hepElement, "");
0204   std::vector<AxisDirection> bBinning =
0205       Acts::stringToAxisDirections(binningString);
0206   if (!binningString.empty()) {
0207     aux += "vol. binning : " + binningString;
0208   }
0209   // Return the tuple
0210   return {transform, bValueType, bValues, bBinning, aux};
0211 }
0212 
0213 std::tuple<std::shared_ptr<const Acts::Experimental::IInternalStructureBuilder>,
0214            std::shared_ptr<const Acts::Experimental::IRootVolumeFinderBuilder>,
0215            std::shared_ptr<const Acts::Experimental::IGeometryIdGenerator>,
0216            std::array<std::string, 3u>, std::optional<Acts::Extent>>
0217 Acts::Experimental::DD4hepBlueprintFactory::extractInternals(
0218     Acts::DD4hepDetectorElement::Store& dd4hepStore,
0219     const GeometryContext& gctx, const dd4hep::DetElement& dd4hepElement,
0220     const std::string& baseName) const {
0221   // Return objects
0222   std::shared_ptr<const Acts::Experimental::IInternalStructureBuilder>
0223       internalsBuilder = nullptr;
0224   std::shared_ptr<const Acts::Experimental::IRootVolumeFinderBuilder>
0225       rootsFinderBuilder = nullptr;
0226   std::shared_ptr<const Acts::Experimental::IGeometryIdGenerator>
0227       geoIdGenerator = nullptr;
0228   /// The hand-over information for externals
0229   std::optional<Extent> ext = std::nullopt;
0230   /// Auxiliary information
0231   std::array<std::string, 3u> aux = {"", "", ""};
0232 
0233   // Check for internal structure builder
0234   auto internals =
0235       Acts::getParamOr<bool>(baseName + "_internals", dd4hepElement, false);
0236   if (internals) {
0237     auto internalsType = Acts::getParamOr<std::string>(
0238         baseName + "_internals_type", dd4hepElement, "");
0239     if (internalsType == "layer") {
0240       aux[0u] = "int. struct : layer";
0241       // Create a new layer builder
0242       DD4hepLayerStructure::Options lOptions;
0243       lOptions.name = dd4hepElement.name();
0244       // Check whether internal/sensitive surfaces should have directly
0245       // translated material
0246       auto convertMaterial = Acts::getParamOr<bool>(
0247           "acts_surface_material_conversion", dd4hepElement, false);
0248       lOptions.conversionOptions.convertMaterial = convertMaterial;
0249       // Check if the extent should be measured
0250       auto interenalsMeasure = Acts::getParamOr<std::string>(
0251           baseName + "_internals_measure", dd4hepElement, "");
0252       auto internalsClearance =
0253           unitLength *
0254           Acts::getParamOr<double>(baseName + "_internals_clearance",
0255                                    dd4hepElement, 0.);
0256       auto internalAxisDirections = stringToAxisDirections(interenalsMeasure);
0257       if (!internalAxisDirections.empty()) {
0258         ACTS_VERBOSE(" - internals extent measurement requested");
0259         Extent internalsExtent;
0260         ExtentEnvelope clearance = ExtentEnvelope::Zero();
0261         for (const auto& bv : internalAxisDirections) {
0262           ACTS_VERBOSE("   -> measuring extent for " << axisDirectionName(bv));
0263           ACTS_VERBOSE("   -> with clearance :" << internalsClearance);
0264           clearance[bv] = {internalsClearance, internalsClearance};
0265         }
0266         internalsExtent.setEnvelope(clearance);
0267         lOptions.extent = internalsExtent;
0268         lOptions.extentConstraints = internalAxisDirections;
0269       }
0270       // Create the builder from the dd4hep element
0271       auto [ib, extOpt] = m_cfg.layerStructure->builder(
0272           dd4hepStore, gctx, dd4hepElement, lOptions);
0273       internalsBuilder = std::move(ib);
0274       if (extOpt.has_value()) {
0275         ACTS_VERBOSE(" - internals extent measured as "
0276                      << extOpt.value().toString());
0277       }
0278       ext = extOpt;
0279     }
0280   }
0281 
0282   // Check for root volume finder
0283   auto rootFinder = Acts::getParamOr<std::string>(
0284       baseName + "_root_volume_finder", dd4hepElement, "");
0285   if (rootFinder == "indexed") {
0286     aux[1u] = "root finder : indexed";
0287     std::vector<AxisDirection> binning = {AxisDirection::AxisZ,
0288                                           AxisDirection::AxisR};
0289     rootsFinderBuilder =
0290         std::make_shared<Acts::Experimental::IndexedRootVolumeFinderBuilder>(
0291             binning);
0292   }
0293 
0294   // Check for geo Id generator
0295   auto geoIdGen =
0296       Acts::getParamOr<std::string>(baseName + "_geo_id", dd4hepElement, "");
0297   if (geoIdGen == "incremental") {
0298     aux[2u] = "geo_id gen. : incremental";
0299     Acts::Experimental::GeometryIdGenerator::Config geoIdCfg;
0300     geoIdGenerator =
0301         std::make_shared<Acts::Experimental::GeometryIdGenerator>(geoIdCfg);
0302   } else if (geoIdGen == "container") {
0303     aux[2u] = "geo_id gen. : container";
0304     Acts::Experimental::GeometryIdGenerator::Config geoIdCfg;
0305     geoIdCfg.containerMode = true;
0306     geoIdCfg.containerId =
0307         Acts::getParamOr<int>(baseName + "_geo_id_base", dd4hepElement, 1);
0308     geoIdGenerator =
0309         std::make_shared<Acts::Experimental::GeometryIdGenerator>(geoIdCfg);
0310   }
0311 
0312   return {internalsBuilder, rootsFinderBuilder, geoIdGenerator, aux, ext};
0313 }