Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2026-07-16 08:08:34

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 SurfacePlacementBase {
0038  public:
0039   DetectorElementStub() = default;
0040   const Transform3& localToGlobalTransform(
0041       const GeometryContext& /*gctx*/) const override {
0042     return m_transform;
0043   }
0044 
0045   /// Is the detector element a sensitive element
0046   bool isSensitive() const override { return true; }
0047   /// Return surface representation - const return pattern
0048   const Surface& surface() const override {
0049     throw std::runtime_error("Not implemented");
0050   }
0051 
0052   /// Non-const return pattern
0053   Surface& surface() override { throw std::runtime_error("Not implemented"); }
0054 
0055  private:
0056   Transform3 m_transform{Transform3::Identity()};
0057 };
0058 
0059 }  // namespace Test
0060 
0061 /// @brief Add the navigation bindings to a module.
0062 /// @param m the module to add the bindings to
0063 void addNavigation(py::module_& m) {
0064   {
0065     auto tryAll =
0066         py::class_<TryAllNavigationPolicy>(m, "TryAllNavigationPolicy");
0067     using Config = TryAllNavigationPolicy::Config;
0068     auto c = py::class_<Config>(tryAll, "Config").def(py::init<>());
0069     ACTS_PYTHON_STRUCT(c, portals, sensitives);
0070   }
0071 
0072   py::class_<NavigationPolicyFactory, std::shared_ptr<NavigationPolicyFactory>>(
0073       m, "NavigationPolicyFactory")
0074       // only to mirror the C++ API
0075       .def_static("make", []() { return NavigationPolicyFactory{}; })
0076       .def("add",
0077            [](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            [](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            [](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", [](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->assignSurfacePlacement(*detElem);
0129 
0130         vol1->addSurface(std::move(surface));
0131 
0132         std::unique_ptr<INavigationPolicy> result =
0133             self->build(GeometryContext::dangerouslyDefaultConstruct(), *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 ActsPython