Back to home page

EIC code displayed by LXR

 
 

    


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

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