Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-16 08:17:49

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/host_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 <string>
0031 #include <utility>
0032 
0033 namespace py = pybind11;
0034 
0035 #define STRINGIFY(x) #x
0036 /// Expand macro and turn its value into a string.
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 /// Owns a detector together with the memory resource its data lives in.
0045 struct detector_handle {
0046   std::unique_ptr<vecmem::host_memory_resource> memory_resource;
0047   detector_t detector;
0048 };
0049 
0050 /// Read a detector (default metadata) from a JSON file.
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 }  // namespace
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 }