Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-16 08:13:57

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/Geometry/CylinderVolumeBounds.hpp"
0010 #include "Acts/Geometry/NavigationPolicyFactory.hpp"
0011 #include "Acts/Geometry/TrackingVolume.hpp"
0012 #include "Acts/Navigation/SurfaceArrayNavigationPolicy.hpp"
0013 #include "Acts/Navigation/TryAllNavigationPolicy.hpp"
0014 #include "Acts/Plugins/Python/Utilities.hpp"
0015 #include "Acts/Surfaces/CylinderBounds.hpp"
0016 #include "Acts/Surfaces/CylinderSurface.hpp"
0017 #include "Acts/Surfaces/SurfaceArray.hpp"
0018 #include "Acts/Utilities/Logger.hpp"
0019 
0020 #include <memory>
0021 #include <stdexcept>
0022 #include <utility>
0023 
0024 #include <boost/core/demangle.hpp>
0025 #include <pybind11/pybind11.h>
0026 #include <pybind11/stl.h>
0027 
0028 namespace py = pybind11;
0029 using namespace pybind11::literals;
0030 
0031 namespace Acts::Python {
0032 
0033 namespace Test {
0034 class DetectorElementStub : public DetectorElementBase {
0035  public:
0036   DetectorElementStub() : DetectorElementBase() {}
0037 
0038   const Transform3& transform(const GeometryContext& /*gctx*/) const override {
0039     return m_transform;
0040   }
0041 
0042   /// Return surface representation - const return pattern
0043   const Surface& surface() const override {
0044     throw std::runtime_error("Not implemented");
0045   }
0046 
0047   /// Non-const return pattern
0048   Surface& surface() override { throw std::runtime_error("Not implemented"); }
0049 
0050   /// Returns the thickness of the module
0051   /// @return double that indicates the thickness of the module
0052   double thickness() const override { return 0; }
0053 
0054  private:
0055   Transform3 m_transform;
0056 };
0057 
0058 }  // namespace Test
0059 
0060 void addNavigation(Context& ctx) {
0061   auto m = ctx.get("main");
0062 
0063   {
0064     auto tryAll =
0065         py::class_<TryAllNavigationPolicy>(m, "TryAllNavigationPolicy");
0066     using Config = TryAllNavigationPolicy::Config;
0067     auto c = py::class_<Config>(tryAll, "Config").def(py::init<>());
0068     ACTS_PYTHON_STRUCT(c, portals, sensitives);
0069   }
0070 
0071   py::class_<Acts::NavigationPolicyFactory,
0072              std::shared_ptr<Acts::NavigationPolicyFactory>>(
0073       m, "NavigationPolicyFactory")
0074       // only to mirror the C++ API
0075       .def_static("make", []() { return Acts::NavigationPolicyFactory{}; })
0076       .def("add",
0077            [](Acts::NavigationPolicyFactory* self, const py::object& cls) {
0078              auto mod = py::module_::import("acts");
0079              if (py::object o = mod.attr("TryAllNavigationPolicy"); cls.is(o)) {
0080                return std::move(*self).template add<TryAllNavigationPolicy>();
0081              } else {
0082                throw std::invalid_argument(
0083                    "Unknown navigation policy class: " +
0084                    cls.attr("__name__").cast<std::string>());
0085              }
0086            })
0087 
0088       .def("add",
0089            [](Acts::NavigationPolicyFactory* self, const py::object& cls,
0090               const SurfaceArrayNavigationPolicy::Config& config) {
0091              auto mod = py::module_::import("acts");
0092              if (py::object o = mod.attr("SurfaceArrayNavigationPolicy");
0093                  !cls.is(o)) {
0094                throw std::invalid_argument(
0095                    "Unknown navigation policy class: " +
0096                    cls.attr("__name__").cast<std::string>());
0097              }
0098 
0099              return std::move(*self).template add<SurfaceArrayNavigationPolicy>(
0100                  config);
0101            })
0102 
0103       .def("add",
0104            [](Acts::NavigationPolicyFactory* self, const py::object& cls,
0105               const TryAllNavigationPolicy::Config& config) {
0106              auto mod = py::module_::import("acts");
0107              if (py::object o = mod.attr("TryAllNavigationPolicy");
0108                  !cls.is(o)) {
0109                throw std::invalid_argument(
0110                    "Unknown navigation policy class: " +
0111                    cls.attr("__name__").cast<std::string>());
0112              }
0113 
0114              return std::move(*self).template add<TryAllNavigationPolicy>(
0115                  config);
0116            })
0117 
0118       .def("_buildTest", [](Acts::NavigationPolicyFactory* self) {
0119         auto vol1 = std::make_shared<TrackingVolume>(
0120             Transform3::Identity(),
0121             std::make_shared<CylinderVolumeBounds>(30, 40, 100));
0122         vol1->setVolumeName("TestVolume");
0123 
0124         auto detElem = std::make_unique<Test::DetectorElementStub>();
0125 
0126         auto surface = Surface::makeShared<CylinderSurface>(
0127             Transform3::Identity(), std::make_shared<CylinderBounds>(30, 40));
0128         surface->assignDetectorElement(*detElem);
0129 
0130         vol1->addSurface(std::move(surface));
0131 
0132         std::unique_ptr<INavigationPolicy> result =
0133             self->build(GeometryContext{}, *vol1,
0134                         *getDefaultLogger("Test", Logging::VERBOSE));
0135       });
0136 
0137   {
0138     auto saPolicy = py::class_<SurfaceArrayNavigationPolicy>(
0139         m, "SurfaceArrayNavigationPolicy");
0140 
0141     using LayerType = SurfaceArrayNavigationPolicy::LayerType;
0142     py::enum_<LayerType>(saPolicy, "LayerType")
0143         .value("Cylinder", LayerType::Cylinder)
0144         .value("Disc", LayerType::Disc)
0145         .value("Plane", LayerType::Plane);
0146 
0147     using Config = SurfaceArrayNavigationPolicy::Config;
0148     auto c = py::class_<Config>(saPolicy, "Config").def(py::init<>());
0149     ACTS_PYTHON_STRUCT(c, layerType, bins);
0150   }
0151 }
0152 
0153 }  // namespace Acts::Python