File indexing completed on 2026-08-16 08:17:49
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include "detray/core/detector.hpp"
0011
0012
0013 #include "detray/io/frontend/detector_reader.hpp"
0014 #include "detray/io/frontend/detector_reader_config.hpp"
0015
0016
0017 #include "algebra/array.hpp"
0018 #include "detray/definitions/algebra.hpp"
0019 #include "detray/detectors/default_metadata.hpp"
0020
0021
0022 #include <vecmem/memory/host_memory_resource.hpp>
0023
0024
0025 #include <pybind11/pybind11.h>
0026 #include <pybind11/stl.h>
0027
0028
0029 #include <memory>
0030 #include <string>
0031 #include <utility>
0032
0033 namespace py = pybind11;
0034
0035 #define STRINGIFY(x) #x
0036
0037 #define STRINGIFY_HELPER(x) STRINGIFY(x)
0038
0039 namespace {
0040
0041 using algebra_t = detray::array<DETRAY_CUSTOM_SCALARTYPE>;
0042 using detector_t = detray::detector<detray::default_metadata<algebra_t>>;
0043
0044
0045 struct detector_handle {
0046 std::unique_ptr<vecmem::host_memory_resource> memory_resource;
0047 detector_t detector;
0048 };
0049
0050
0051 std::pair<detector_handle, detray::name_map> read_detector(
0052 const std::string &file_name) {
0053 auto mr = std::make_unique<vecmem::host_memory_resource>();
0054
0055 detray::io::detector_reader_config cfg{};
0056 cfg.add_file(file_name);
0057
0058 auto [det, names] = detray::io::read_detector<detector_t>(*mr, cfg);
0059
0060 return {detector_handle{std::move(mr), std::move(det)}, std::move(names)};
0061 }
0062
0063 }
0064
0065 PYBIND11_MODULE(DetrayPythonBindings, m) {
0066 m.doc() = "Detray core bindings";
0067
0068 py::class_<detector_handle>(
0069 m, "DetectorDefaultMetadata" STRINGIFY_HELPER(DETRAY_CUSTOM_SCALARTYPE))
0070 .def(
0071 "n_volumes",
0072 [](const detector_handle &d) { return d.detector.volumes().size(); },
0073 "Number of volumes in the detector")
0074 .def(
0075 "n_surfaces",
0076 [](const detector_handle &d) { return d.detector.surfaces().size(); },
0077 "Number of surfaces in the detector");
0078 py::class_<detray::name_map>(m, "NameMap");
0079
0080 m.def("readDetector", &read_detector, py::arg("fileName"),
0081 "Read a detector from a JSON file");
0082 }