Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-13 08:18:27

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