File indexing completed on 2026-11-26 09:23:20
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include "detray/core/detector.hpp"
0011 #include "detray/definitions/algebra.hpp"
0012 #include "detray/detectors/default_metadata.hpp"
0013 #include "detray/detectors/toy_metadata.hpp"
0014
0015
0016 #include "detray/io/frontend/detector_reader_config.hpp"
0017 #include "detray/io/frontend/detector_writer_config.hpp"
0018 #include "detray/io/json/detector_reader.hpp"
0019 #include "detray/io/json/detector_writer.hpp"
0020
0021
0022 #include <vecmem/memory/memory_resource.hpp>
0023
0024
0025 #include <pybind11/pybind11.h>
0026 #include <pybind11/stl.h>
0027
0028
0029 #include <memory>
0030 #include <sstream>
0031 #include <string>
0032 #include <utility>
0033
0034 namespace py = pybind11;
0035
0036 namespace {
0037
0038 using scalar_t = DETRAY_CUSTOM_SCALARTYPE;
0039 using algebra_t = detray::array<scalar_t>;
0040 using detector_t = detray::host::detector<detray::default_metadata<algebra_t>>;
0041 using toy_detector_t = detray::host::detector<detray::toy_metadata<algebra_t>>;
0042 using reader_config_t = detray::io::detector_reader_config;
0043 using writer_config_t = detray::io::detector_writer_config;
0044
0045 template <typename T>
0046 std::string to_string(const T &obj) {
0047 std::ostringstream os;
0048 os << obj;
0049 return os.str();
0050 }
0051
0052
0053
0054
0055
0056 std::pair<py::object, detray::name_map> read_detector(
0057 std::shared_ptr<vecmem::memory_resource> mr, const reader_config_t &cfg) {
0058 auto [det, names] = detray::io::read_detector_json<detector_t>(*mr, cfg);
0059
0060 py::object detector = py::cast(std::move(det));
0061 py::detail::keep_alive_impl(detector, py::cast(std::move(mr)));
0062
0063 return {std::move(detector), std::move(names)};
0064 }
0065
0066 }
0067
0068 PYBIND11_MODULE(detray_io_python_bindings, m) {
0069 m.doc() = "Detray io bindings";
0070
0071 py::class_<reader_config_t>(m, "DetectorReaderConfig")
0072 .def(py::init<>())
0073 .def_property_readonly("files", &reader_config_t::files, "Input files")
0074 .def_property(
0075 "do_check", [](const reader_config_t &c) { return c.do_check(); },
0076 [](reader_config_t &c, bool v) { c.do_check(v); },
0077 "Do detector consistency check")
0078 .def_property(
0079 "verbose_check",
0080 [](const reader_config_t &c) { return c.verbose_check(); },
0081 [](reader_config_t &c, bool v) { c.verbose_check(v); },
0082 "Verbosity of the detector consistency check")
0083 .def_property(
0084 "deduplicate",
0085 [](const reader_config_t &c) { return c.deduplicate(); },
0086 [](reader_config_t &c, bool v) { c.deduplicate(v); },
0087 "Data deduplication in the detector")
0088 .def(
0089 "add_file",
0090 [](reader_config_t &c, const std::string &f) -> reader_config_t & {
0091 return c.add_file(f);
0092 },
0093 py::arg("file_name"), py::return_value_policy::reference_internal,
0094 "Add an input file")
0095 .def(
0096 "add_files",
0097 [](reader_config_t &c, const py::args &args) -> reader_config_t & {
0098 std::vector<std::string> files{};
0099 for (const auto &arg : args) {
0100 files.push_back(arg.cast<std::string>());
0101 }
0102 return c.add_files(std::move(files));
0103 },
0104 py::return_value_policy::reference_internal,
0105 "Append multiple input files")
0106 .def("__repr__", &to_string<reader_config_t>);
0107
0108 m.def("read_detector_json", &read_detector, py::arg("memory_resource"),
0109 py::arg("config"),
0110 "Read a detector using the given memory resource as configured by a "
0111 "DetectorReaderConfig. The returned detector keeps the memory resource "
0112 "alive for as long as it is used");
0113
0114 py::class_<writer_config_t>(m, "DetectorWriterConfig")
0115 .def(py::init<>())
0116 .def_property(
0117 "path", [](const writer_config_t &c) { return c.path(); },
0118 [](writer_config_t &c, const std::string &p) { c.path(p); },
0119 "Path to the output files")
0120 .def_property(
0121 "replace_files",
0122 [](const writer_config_t &c) { return c.replace_files(); },
0123 [](writer_config_t &c, bool b) { c.replace_files(b); },
0124 "Replace files in case they already exist")
0125 .def_property(
0126 "compactify_json",
0127 [](const writer_config_t &c) { return c.compactify_json(); },
0128 [](writer_config_t &c, bool b) { c.compactify_json(b); },
0129 "Compactify the json output")
0130 .def_property(
0131 "write_material",
0132 [](const writer_config_t &c) { return c.write_material(); },
0133 [](writer_config_t &c, bool b) { c.write_material(b); },
0134 "Whether to write the material to file")
0135 .def_property(
0136 "write_grids",
0137 [](const writer_config_t &c) { return c.write_grids(); },
0138 [](writer_config_t &c, bool b) { c.write_grids(b); },
0139 "Whether to write the accelerator grids to file")
0140 .def("__repr__", &to_string<writer_config_t>);
0141
0142 m.def(
0143 "write_detector_json",
0144 [](toy_detector_t &det, const typename toy_detector_t::name_map &names,
0145 writer_config_t &cfg) {
0146 detray::io::write_detector_json(det, names, cfg);
0147 },
0148 py::arg("detector"), py::arg("names"), py::arg("config"),
0149 "Write a detector to file as configured by a DetectorWriterConfig. The "
0150 "config is modified in place if the detector carries no material");
0151 }