Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-05 08:18:37

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 // Detray core include(s)
0010 #include "detray/core/detector.hpp"
0011 
0012 // Detray IO include(s)
0013 #include "detray/io/frontend/detector_reader.hpp"
0014 #include "detray/io/frontend/detector_reader_config.hpp"
0015 
0016 // Detray algebra plugin + detector metadata
0017 #include "algebra/array.hpp"
0018 #include "detray/definitions/algebra.hpp"
0019 #include "detray/detectors/default_metadata.hpp"
0020 
0021 // Vecmem include(s)
0022 #include <vecmem/memory/memory_resource.hpp>
0023 
0024 // Pybind11 include(s)
0025 #include <pybind11/pybind11.h>
0026 #include <pybind11/stl.h>
0027 
0028 // System include(s)
0029 #include <memory>
0030 #include <sstream>
0031 #include <string>
0032 #include <utility>
0033 
0034 namespace py = pybind11;
0035 
0036 namespace {
0037 
0038 using reader_config_t = detray::io::detector_reader_config;
0039 using scalar_t = DETRAY_CUSTOM_SCALARTYPE;
0040 using algebra_t = detray::array<scalar_t>;
0041 using detector_t = detray::detector<detray::default_metadata<algebra_t>>;
0042 
0043 template <typename T>
0044 std::string to_string(const T &obj) {
0045   std::ostringstream os;
0046   os << obj;
0047   return os.str();
0048 }
0049 
0050 /// Read a detector (default metadata) into @p mr as configured by @p cfg .
0051 ///
0052 /// The memory resource object is automatically kept alive at least as long as
0053 /// the returned detector object.
0054 std::pair<py::object, detray::name_map> read_detector(
0055     std::shared_ptr<vecmem::memory_resource> mr, const reader_config_t &cfg) {
0056   auto [det, names] = detray::io::read_detector<detector_t>(*mr, cfg);
0057 
0058   py::object detector = py::cast(std::move(det));
0059   py::detail::keep_alive_impl(detector, py::cast(std::move(mr)));
0060 
0061   return {std::move(detector), std::move(names)};
0062 }
0063 
0064 }  // namespace
0065 
0066 PYBIND11_MODULE(DetrayIoPythonBindings, m) {
0067   m.doc() = "Detray io bindings";
0068 
0069   py::class_<reader_config_t>(m, "DetectorReaderConfig")
0070       .def(py::init<>())
0071       .def_property_readonly("files", &reader_config_t::files, "Input files")
0072       .def_property(
0073           "doCheck", [](const reader_config_t &c) { return c.do_check(); },
0074           [](reader_config_t &c, bool v) { c.do_check(v); },
0075           "Do detector consistency check")
0076       .def_property(
0077           "verboseCheck",
0078           [](const reader_config_t &c) { return c.verbose_check(); },
0079           [](reader_config_t &c, bool v) { c.verbose_check(v); },
0080           "Verbosity of the detector consistency check")
0081       .def(
0082           "addFile",
0083           [](reader_config_t &c, const std::string &f) -> reader_config_t & {
0084             return c.add_file(f);
0085           },
0086           py::arg("fileName"), py::return_value_policy::reference_internal,
0087           "Add an input file")
0088       .def("__repr__", &to_string<reader_config_t>);
0089 
0090   m.def("readDetector", &read_detector, py::arg("memoryResource"),
0091         py::arg("config"),
0092         "Read a detector using the given memory resource as configured by a "
0093         "DetectorReaderConfig. The returned detector keeps the memory resource "
0094         "alive for as long as it is used");
0095 }