Back to home page

EIC code displayed by LXR

 
 

    


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