Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-06-30 07:52:42

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/Detector/GeometryIdGenerator.hpp"
0010 #include "Acts/Plugins/DD4hep/DD4hepDetectorElement.hpp"
0011 #include "Acts/Plugins/DD4hep/DD4hepDetectorStructure.hpp"
0012 #include "Acts/Plugins/DD4hep/DD4hepFieldAdapter.hpp"
0013 #include "Acts/Plugins/DD4hep/DD4hepIdentifierMapper.hpp"
0014 #include "Acts/Plugins/Python/Utilities.hpp"
0015 #include "Acts/Utilities/Logger.hpp"
0016 #include "ActsExamples/DD4hepDetector/AlignedDD4hepDetectorElement.hpp"
0017 #include "ActsExamples/DD4hepDetector/DD4hepDetector.hpp"
0018 
0019 #include <memory>
0020 #include <utility>
0021 
0022 #include <DD4hep/DetElement.h>
0023 #include <DD4hep/Fields.h>
0024 #include <pybind11/functional.h>
0025 #include <pybind11/pybind11.h>
0026 #include <pybind11/stl.h>
0027 
0028 namespace py = pybind11;
0029 using namespace ActsExamples;
0030 using namespace Acts::Python;
0031 
0032 PYBIND11_MODULE(ActsPythonBindingsDD4hep, m) {
0033   {
0034     py::class_<Acts::DD4hepDetectorElement, Acts::DetectorElementBase,
0035                std::shared_ptr<Acts::DD4hepDetectorElement>>(
0036         m, "DD4hepDetectorElement");
0037 
0038     py::class_<ActsExamples::AlignedDD4hepDetectorElement,
0039                Acts::DD4hepDetectorElement,
0040                std::shared_ptr<ActsExamples::AlignedDD4hepDetectorElement>>(
0041         m, "AlignedDD4hepDetectorElement");
0042   }
0043 
0044   {
0045     py::class_<dd4hep::DetElement, std::shared_ptr<dd4hep::DetElement>>(
0046         m, "DD4hepDetElement");
0047   }
0048 
0049   {
0050     auto f =
0051         py::class_<DD4hepDetector, Detector, std::shared_ptr<DD4hepDetector>>(
0052             m, "DD4hepDetector")
0053             .def(py::init<const DD4hepDetector::Config&>())
0054             .def_property_readonly("field", &DD4hepDetector::field);
0055 
0056     auto c = py::class_<DD4hepDetector::Config>(f, "Config").def(py::init<>());
0057     ACTS_PYTHON_STRUCT(
0058         c, logLevel, dd4hepLogLevel, xmlFileNames, name, bTypePhi, bTypeR,
0059         bTypeZ, envelopeR, envelopeZ, defaultLayerThickness, materialDecorator,
0060         alignmentDecorator, geometryIdentifierHook, detectorElementFactory);
0061     patchKwargsConstructor(c);
0062 
0063     m.def("alignedDD4hepDetectorElementFactory",
0064           &ActsExamples::alignedDD4hepDetectorElementFactory);
0065   }
0066 
0067   {
0068     py::class_<Acts::DD4hepFieldAdapter, Acts::MagneticFieldProvider,
0069                std::shared_ptr<Acts::DD4hepFieldAdapter>>(m,
0070                                                           "DD4hepFieldAdapter");
0071   }
0072 
0073   {
0074     m.def("createDD4hepIdGeoIdMap",
0075           [](const Acts::TrackingGeometry& tGeometry)
0076               -> std::map<Acts::DD4hepDetectorElement::DD4hepVolumeID,
0077                           Acts::GeometryIdentifier> {
0078             // The surface visitor
0079             struct DD4hepIdGrabber {
0080               std::map<Acts::DD4hepDetectorElement::DD4hepVolumeID,
0081                        Acts::GeometryIdentifier>
0082                   dd4hepIdGeoIdMap;
0083 
0084               void operator()(const Acts::Surface* surface) {
0085                 const auto* dde = surface->associatedDetectorElement();
0086                 const auto* dd4hepDetElement =
0087                     dynamic_cast<const Acts::DD4hepDetectorElement*>(dde);
0088                 // Check if it is valid
0089                 if (dd4hepDetElement != nullptr) {
0090                   dd4hep::DDSegmentation::VolumeID dd4hepID =
0091                       dd4hepDetElement->sourceElement().volumeID();
0092                   auto geoID = surface->geometryId();
0093                   dd4hepIdGeoIdMap[dd4hepID] = geoID;
0094                 }
0095               }
0096             };
0097 
0098             // Create an instance
0099             DD4hepIdGrabber dd4hepIdGrabber;
0100             // Visit the surfaces & return what you have
0101             tGeometry.visitSurfaces(dd4hepIdGrabber);
0102             return dd4hepIdGrabber.dd4hepIdGeoIdMap;
0103           });
0104   }
0105 
0106   {
0107     using Options = Acts::Experimental::DD4hepDetectorStructure::Options;
0108     auto o = py::class_<Options>(m, "DD4hepDetectorOptions").def(py::init<>());
0109     ACTS_PYTHON_STRUCT(o, logLevel, emulateToGraph, geoIdGenerator,
0110                        materialDecorator);
0111 
0112     patchKwargsConstructor(o);
0113 
0114     m.def(
0115         "attachDD4hepGeoIdMapper",
0116         [](Acts::Experimental::DD4hepDetectorStructure::Options& options,
0117            const std::map<Acts::DD4hepDetectorElement::DD4hepVolumeID,
0118                           Acts::GeometryIdentifier>& dd4hepIdGeoIdMap) {
0119           // The Geo mapper
0120           auto geoIdMapper =
0121               std::make_shared<const Acts::DD4hepIdentifierMapper>(
0122                   Acts::DD4hepIdentifierMapper::Config{dd4hepIdGeoIdMap},
0123                   Acts::getDefaultLogger("GeometryIdMapper", options.logLevel));
0124 
0125           // A remaining recursive logger
0126           auto geoIdGenerator =
0127               std::make_shared<const Acts::Experimental::GeometryIdGenerator>(
0128                   Acts::Experimental::GeometryIdGenerator::Config{},
0129                   Acts::getDefaultLogger("GeometryIdGenerator",
0130                                          options.logLevel));
0131 
0132           std::tuple<
0133               std::shared_ptr<const Acts::Experimental::GeometryIdGenerator>,
0134               std::shared_ptr<const Acts::DD4hepIdentifierMapper>>
0135               chainedGenerators = {geoIdGenerator, geoIdMapper};
0136 
0137           auto chainedGeoIdGenerator = std::make_shared<
0138               const Acts::Experimental::ChainedGeometryIdGenerator<
0139                   std::shared_ptr<
0140                       const Acts::Experimental::GeometryIdGenerator>,
0141                   std::shared_ptr<const Acts::DD4hepIdentifierMapper>>>(
0142               std::move(chainedGenerators),
0143               Acts::getDefaultLogger("ChainedGeometryIdGenerator",
0144                                      options.logLevel));
0145 
0146           options.geoIdGenerator = chainedGeoIdGenerator;
0147         });
0148   }
0149 }