Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-02 07:51:52

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/DD4hepDetectorStructure.hpp"
0010 
0011 #include "Acts/Detector/CylindricalContainerBuilder.hpp"
0012 #include "Acts/Detector/DetectorBuilder.hpp"
0013 #include "Acts/Detector/GeometryIdGenerator.hpp"
0014 #include "Acts/Detector/detail/BlueprintDrawer.hpp"
0015 #include "Acts/Detector/detail/BlueprintHelper.hpp"
0016 #include "Acts/Plugins/DD4hep/DD4hepBlueprintFactory.hpp"
0017 #include "Acts/Plugins/DD4hep/DD4hepDetectorSurfaceFactory.hpp"
0018 #include "Acts/Plugins/DD4hep/DD4hepLayerStructure.hpp"
0019 
0020 #include <fstream>
0021 
0022 #include <DD4hep/DetElement.h>
0023 
0024 Acts::Experimental::DD4hepDetectorStructure::DD4hepDetectorStructure(
0025     std::unique_ptr<const Logger> mlogger)
0026     : m_logger(std::move(mlogger)) {}
0027 
0028 std::tuple<std::shared_ptr<const Acts::Experimental::Detector>,
0029            Acts::DD4hepDetectorElement::Store>
0030 Acts::Experimental::DD4hepDetectorStructure::construct(
0031     const GeometryContext& gctx, const dd4hep::DetElement& dd4hepElement,
0032     const Options& options) const {
0033   ACTS_DEBUG("Building detector from " << dd4hepElement.name());
0034 
0035   // Return objects
0036   std::shared_ptr<const Detector> detector = nullptr;
0037   DD4hepDetectorElement::Store detectorStore;
0038 
0039   // Set up the tools
0040   DD4hepDetectorSurfaceFactory::Config surfaceFactoryConfig;
0041   auto surfaceFactory = std::make_shared<DD4hepDetectorSurfaceFactory>(
0042       surfaceFactoryConfig,
0043       getDefaultLogger("DD4hepDetectorSurfaceFactory", options.logLevel));
0044 
0045   auto layerStructure = std::make_shared<DD4hepLayerStructure>(
0046       std::move(surfaceFactory),
0047       getDefaultLogger("DD4hepLayerStructure", options.logLevel));
0048 
0049   // Draw the blue print from the dd4hep detector element tree
0050   DD4hepBlueprintFactory::Config bpdCfg{layerStructure};
0051   DD4hepBlueprintFactory::Cache bpdCache;
0052 
0053   DD4hepBlueprintFactory dd4hepBlueprintDrawer(
0054       bpdCfg, getDefaultLogger("DD4hepBlueprintFactory", options.logLevel));
0055   auto dd4hepBlueprint =
0056       dd4hepBlueprintDrawer.create(bpdCache, gctx, dd4hepElement);
0057   detectorStore = bpdCache.dd4hepStore;
0058 
0059   // Draw the raw graph
0060   if (!options.emulateToGraph.empty()) {
0061     ACTS_DEBUG("Writing the initial bluepring to file before gap filling.");
0062     std::ofstream bpi(options.emulateToGraph + "_initial.dot");
0063     detail::BlueprintDrawer::dotStream(bpi, *dd4hepBlueprint);
0064     bpi.close();
0065   }
0066 
0067   if (dd4hepBlueprint->boundsType == VolumeBounds::eCylinder) {
0068     ACTS_DEBUG("Cylindrical detector building detected.");
0069 
0070     // Now fill the gaps
0071     detail::BlueprintHelper::fillGaps(*dd4hepBlueprint);
0072 
0073     // Draw the synchronized graph
0074     if (!options.emulateToGraph.empty()) {
0075       ACTS_DEBUG("Writing the final bluepring to file.");
0076       std::ofstream bpf(options.emulateToGraph + "_final.dot");
0077       detail::BlueprintDrawer::dotStream(bpf, *dd4hepBlueprint);
0078       bpf.close();
0079       // Return without building
0080       return {detector, detectorStore};
0081     }
0082 
0083     // Create a Cylindrical detector builder from this blueprint
0084     auto detectorBuilder = std::make_shared<CylindricalContainerBuilder>(
0085         *dd4hepBlueprint, options.logLevel);
0086 
0087     // Detector builder
0088     DetectorBuilder::Config dCfg;
0089     dCfg.auxiliary =
0090         "*** DD4hep : auto generated cylindrical detector builder  ***";
0091     dCfg.name = "Cylindrical detector from DD4hep blueprint";
0092     dCfg.builder = detectorBuilder;
0093     dCfg.geoIdGenerator = options.geoIdGenerator != nullptr
0094                               ? options.geoIdGenerator
0095                               : dd4hepBlueprint->geoIdGenerator;
0096     dCfg.materialDecorator = options.materialDecorator;
0097     detector = DetectorBuilder(dCfg, getDefaultLogger("DD4hepDetectorBuilder",
0098                                                       options.logLevel))
0099                    .construct(gctx);
0100   } else {
0101     throw std::invalid_argument(
0102         "DD4hepDetectorStructure: Only cylindrical detectors are (currently) "
0103         "supported.");
0104   }
0105   return {detector, detectorStore};
0106 }