Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:10:04

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