Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:03:10

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/Detector.hpp"
0010 
0011 #include "Acts/Geometry/DetectorElementBase.hpp"
0012 #include "Acts/Geometry/TrackingGeometry.hpp"
0013 #include "Acts/Material/IMaterialDecorator.hpp"
0014 #include "Acts/Plugins/Python/Utilities.hpp"
0015 #include "Acts/Utilities/BinningType.hpp"
0016 #include "ActsExamples/DetectorCommons/Detector.hpp"
0017 #include "ActsExamples/DetectorCommons/StructureSelector.hpp"
0018 #include "ActsExamples/Framework/IContextDecorator.hpp"
0019 #include "ActsExamples/GenericDetector/AlignedGenericDetector.hpp"
0020 #include "ActsExamples/GenericDetector/GenericDetector.hpp"
0021 #include "ActsExamples/TGeoDetector/TGeoDetector.hpp"
0022 #include "ActsExamples/TelescopeDetector/TelescopeDetector.hpp"
0023 #include "ActsExamples/Utilities/Options.hpp"
0024 
0025 #include <memory>
0026 #include <optional>
0027 #include <string>
0028 #include <utility>
0029 #include <vector>
0030 
0031 #include <pybind11/pybind11.h>
0032 #include <pybind11/stl.h>
0033 #include <pybind11/stl/filesystem.h>
0034 
0035 namespace py = pybind11;
0036 using namespace ActsExamples;
0037 
0038 namespace Acts::Python {
0039 
0040 void addDetector(Context& ctx) {
0041   auto [m, mex] = ctx.get("main", "examples");
0042 
0043   {
0044     py::class_<Detector, std::shared_ptr<Detector>>(mex, "DetectorBase")
0045         .def("nominalGeometryContext", &Detector::nominalGeometryContext)
0046         .def("trackingGeometry", &Detector::trackingGeometry)
0047         .def("gen2Geometry", &Detector::gen2Geometry)
0048         .def("contextDecorators", &Detector::contextDecorators)
0049         .def("__enter__",
0050              [](const std::shared_ptr<Detector>& self) { return self; })
0051         .def("__exit__",
0052              [](std::shared_ptr<Detector>& self,
0053                 const std::optional<py::object>&,
0054                 const std::optional<py::object>&,
0055                 const std::optional<py::object>&) { self.reset(); });
0056   }
0057 
0058   {
0059     py::class_<StructureSelector, std::shared_ptr<StructureSelector>>(
0060         mex, "StructureSelector")
0061         .def(py::init<std::shared_ptr<const Acts::TrackingGeometry>>())
0062         .def("selectSurfaces", &StructureSelector::selectSurfaces)
0063         .def("selectedTransforms", &StructureSelector::selectedTransforms);
0064   }
0065 
0066   {
0067     auto d =
0068         py::class_<GenericDetector, Detector, std::shared_ptr<GenericDetector>>(
0069             mex, "GenericDetector")
0070             .def(py::init<const GenericDetector::Config&>());
0071 
0072     auto c = py::class_<GenericDetector::Config>(d, "Config").def(py::init<>());
0073     ACTS_PYTHON_STRUCT(c, buildLevel, logLevel, surfaceLogLevel, layerLogLevel,
0074                        volumeLogLevel, buildProto, materialDecorator, gen3,
0075                        graphvizFile);
0076   }
0077 
0078   {
0079     auto ad = py::class_<AlignedGenericDetector, GenericDetector,
0080                          std::shared_ptr<AlignedGenericDetector>>(
0081                   mex, "AlignedGenericDetector")
0082                   .def(py::init<const GenericDetector::Config&>());
0083   }
0084 
0085   {
0086     auto d =
0087         py::class_<TelescopeDetector, Detector,
0088                    std::shared_ptr<TelescopeDetector>>(mex, "TelescopeDetector")
0089             .def(py::init<const TelescopeDetector::Config&>());
0090 
0091     auto c =
0092         py::class_<TelescopeDetector::Config>(d, "Config").def(py::init<>());
0093     ACTS_PYTHON_STRUCT(c, positions, stereos, offsets, bounds, thickness,
0094                        surfaceType, binValue, materialDecorator, logLevel);
0095   }
0096 
0097   {
0098     auto d = py::class_<TGeoDetector, Detector, std::shared_ptr<TGeoDetector>>(
0099                  mex, "TGeoDetector")
0100                  .def(py::init<const TGeoDetector::Config&>());
0101 
0102     py::class_<Options::Interval>(mex, "Interval")
0103         .def(py::init<>())
0104         .def(py::init<std::optional<double>, std::optional<double>>())
0105         .def_readwrite("lower", &Options::Interval::lower)
0106         .def_readwrite("upper", &Options::Interval::upper);
0107 
0108     auto c = py::class_<TGeoDetector::Config>(d, "Config").def(py::init<>());
0109 
0110     c.def_property("jsonFile", nullptr,
0111                    [](TGeoDetector::Config& cfg, const std::string& file) {
0112                      cfg.readJson(file);
0113                    });
0114 
0115     py::enum_<TGeoDetector::Config::SubVolume>(c, "SubVolume")
0116         .value("Negative", TGeoDetector::Config::SubVolume::Negative)
0117         .value("Central", TGeoDetector::Config::SubVolume::Central)
0118         .value("Positive", TGeoDetector::Config::SubVolume::Positive);
0119 
0120     py::enum_<Acts::BinningType>(c, "BinningType")
0121         .value("equidistant", Acts::BinningType::equidistant)
0122         .value("arbitrary", Acts::BinningType::arbitrary);
0123 
0124     auto volume =
0125         py::class_<TGeoDetector::Config::Volume>(c, "Volume").def(py::init<>());
0126     ACTS_PYTHON_STRUCT(
0127         volume, name, binToleranceR, binTolerancePhi, binToleranceZ,
0128         cylinderDiscSplit, cylinderNZSegments, cylinderNPhiSegments,
0129         discNRSegments, discNPhiSegments, itkModuleSplit, barrelMap, discMap,
0130         splitPatterns, layers, subVolumeName, sensitiveNames, sensitiveAxes,
0131         rRange, zRange, splitTolR, splitTolZ, binning0, binning1);
0132 
0133     auto regTriplet = [&c](const std::string& name, auto v) {
0134       using type = decltype(v);
0135       py::class_<TGeoDetector::Config::LayerTriplet<type>>(c, name.c_str())
0136           .def(py::init<>())
0137           .def(py::init<type>())
0138           .def(py::init<type, type, type>())
0139           .def_readwrite("negative",
0140                          &TGeoDetector::Config::LayerTriplet<type>::negative)
0141           .def_readwrite("central",
0142                          &TGeoDetector::Config::LayerTriplet<type>::central)
0143           .def_readwrite("positive",
0144                          &TGeoDetector::Config::LayerTriplet<type>::positive)
0145           .def("at", py::overload_cast<TGeoDetector::Config::SubVolume>(
0146                          &TGeoDetector::Config::LayerTriplet<type>::at));
0147     };
0148 
0149     regTriplet("LayerTripletBool", true);
0150     regTriplet("LayerTripletString", std::string{""});
0151     regTriplet("LayerTripletVectorString", std::vector<std::string>{});
0152     regTriplet("LayerTripletInterval", Options::Interval{});
0153     regTriplet("LayerTripletDouble", double{5.5});
0154     regTriplet("LayerTripletVectorBinning",
0155                std::vector<std::pair<int, Acts::BinningType>>{});
0156 
0157     ACTS_PYTHON_STRUCT(c, surfaceLogLevel, layerLogLevel, volumeLogLevel,
0158                        fileName, buildBeamPipe, beamPipeRadius,
0159                        beamPipeHalflengthZ, beamPipeLayerThickness,
0160                        beamPipeEnvelopeR, layerEnvelopeR, unitScalor,
0161                        materialDecorator, volumes);
0162 
0163     patchKwargsConstructor(c);
0164   }
0165 
0166   {
0167     py::class_<Acts::DetectorElementBase,
0168                std::shared_ptr<Acts::DetectorElementBase>>(
0169         mex, "DetectorElementBase");
0170   }
0171 }
0172 
0173 }  // namespace Acts::Python