Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /acts/Python/Plugins/src/DD4hep.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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/Geometry/GeometryIdentifier.hpp"
0011 #include "Acts/Geometry/TrackingGeometry.hpp"
0012 #include "Acts/Surfaces/Surface.hpp"
0013 #include "ActsPlugins/DD4hep/DD4hepDetectorElement.hpp"
0014 #include "ActsPlugins/DD4hep/DD4hepDetectorStructure.hpp"
0015 #include "ActsPlugins/DD4hep/DD4hepFieldAdapter.hpp"
0016 #include "ActsPlugins/DD4hep/DD4hepIdentifierMapper.hpp"
0017 #include "ActsPython/Utilities/Helpers.hpp"
0018 #include "ActsPython/Utilities/Macros.hpp"
0019 
0020 #include <memory>
0021 #include <string>
0022 
0023 #include <DD4hep/DetElement.h>
0024 #include <DD4hep/Fields.h>
0025 #include <pybind11/pybind11.h>
0026 
0027 namespace py = pybind11;
0028 using namespace pybind11::literals;
0029 
0030 PYBIND11_MODULE(ActsPluginsPythonBindingsDD4hep, dd4hep) {
0031   using namespace Acts;
0032   using namespace ActsPlugins;
0033   using namespace ActsPython;
0034 
0035   // Basic bindings
0036   {
0037     // The DD4hep Detector Element
0038     py::class_<dd4hep::DetElement, std::shared_ptr<dd4hep::DetElement>>(
0039         dd4hep, "DD4hepDetElement");
0040 
0041     // The Acts:: glue detector element
0042     py::class_<DD4hepDetectorElement, DetectorElementBase,
0043                std::shared_ptr<DD4hepDetectorElement>>(dd4hep,
0044                                                        "DD4hepDetectorElement");
0045 
0046     py::class_<DD4hepFieldAdapter, MagneticFieldProvider,
0047                std::shared_ptr<DD4hepFieldAdapter>>(dd4hep,
0048                                                     "DD4hepFieldAdapter");
0049   }
0050 
0051   // Helper method
0052   {
0053     dd4hep.def(
0054         "createDD4hepIdGeoIdMap",
0055         [](const TrackingGeometry& tGeometry)
0056             -> std::map<DD4hepDetectorElement::DD4hepVolumeID,
0057                         GeometryIdentifier> {
0058           // The surface visitor
0059           struct DD4hepIdGrabber {
0060             std::map<DD4hepDetectorElement::DD4hepVolumeID, GeometryIdentifier>
0061                 dd4hepIdGeoIdMap;
0062 
0063             void operator()(const Surface* surface) {
0064               const auto* dde = surface->associatedDetectorElement();
0065               const auto* dd4hepDetElement =
0066                   dynamic_cast<const DD4hepDetectorElement*>(dde);
0067               // Check if it is valid
0068               if (dd4hepDetElement != nullptr) {
0069                 dd4hep::DDSegmentation::VolumeID dd4hepID =
0070                     dd4hepDetElement->sourceElement().volumeID();
0071                 auto geoID = surface->geometryId();
0072                 dd4hepIdGeoIdMap[dd4hepID] = geoID;
0073               }
0074             }
0075           };
0076 
0077           // Create an instance
0078           DD4hepIdGrabber dd4hepIdGrabber;
0079           // Visit the surfaces & return what you have
0080           tGeometry.visitSurfaces(dd4hepIdGrabber);
0081           return dd4hepIdGrabber.dd4hepIdGeoIdMap;
0082         });
0083   }
0084 
0085   // Gen2 geometry building - will be removed
0086   {
0087     using Options = DD4hepDetectorStructure::Options;
0088     auto o =
0089         py::class_<Options>(dd4hep, "DD4hepDetectorOptions").def(py::init<>());
0090     ACTS_PYTHON_STRUCT(o, logLevel, emulateToGraph, geoIdGenerator,
0091                        materialDecorator);
0092 
0093     patchKwargsConstructor(o);
0094 
0095     dd4hep.def(
0096         "attachDD4hepGeoIdMapper",
0097         [](DD4hepDetectorStructure::Options& options,
0098            const std::map<DD4hepDetectorElement::DD4hepVolumeID,
0099                           GeometryIdentifier>& dd4hepIdGeoIdMap) {
0100           // The Geo mapper
0101           auto geoIdMapper = std::make_shared<const DD4hepIdentifierMapper>(
0102               DD4hepIdentifierMapper::Config{dd4hepIdGeoIdMap},
0103               getDefaultLogger("GeometryIdMapper", options.logLevel));
0104 
0105           // A remaining recursive logger
0106           auto geoIdGenerator =
0107               std::make_shared<const Experimental::GeometryIdGenerator>(
0108                   Experimental::GeometryIdGenerator::Config{},
0109                   getDefaultLogger("GeometryIdGenerator", options.logLevel));
0110 
0111           std::tuple<std::shared_ptr<const Experimental::GeometryIdGenerator>,
0112                      std::shared_ptr<const DD4hepIdentifierMapper>>
0113               chainedGenerators = {geoIdGenerator, geoIdMapper};
0114 
0115           auto chainedGeoIdGenerator =
0116               std::make_shared<const Experimental::ChainedGeometryIdGenerator<
0117                   std::shared_ptr<const Experimental::GeometryIdGenerator>,
0118                   std::shared_ptr<const DD4hepIdentifierMapper>>>(
0119                   std::move(chainedGenerators),
0120                   getDefaultLogger("ChainedGeometryIdGenerator",
0121                                    options.logLevel));
0122 
0123           options.geoIdGenerator = chainedGeoIdGenerator;
0124         });
0125   }
0126 }