Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-26 09:23:20

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 #include "detray/definitions/algebra.hpp"
0012 #include "detray/detectors/default_metadata.hpp"
0013 #include "detray/propagator/propagation_config.hpp"
0014 
0015 // Detray test include(s)
0016 #include "detray/test/common/build_toy_detector.hpp"
0017 #include "detray/test/common/event_generator/uniform_track_generator_config.hpp"
0018 #include "detray/test/cpu/material_scan.hpp"
0019 #include "detray/test/cpu/material_validation.hpp"
0020 #include "detray/test/framework/register_checks.hpp"
0021 #include "detray/test/framework/test_configuration.hpp"
0022 #include "detray/test/framework/whiteboard.hpp"
0023 #include "detray/test/validation/material_validation_config.hpp"
0024 
0025 // GTest include(s)
0026 #include <gtest/gtest.h>
0027 
0028 // Vecmem include(s)
0029 #include <vecmem/memory/memory_resource.hpp>
0030 
0031 // Pybind11 include(s)
0032 #include <pybind11/pybind11.h>
0033 #include <pybind11/stl.h>
0034 
0035 // System include(s)
0036 #include <cstddef>
0037 #include <cstdint>
0038 #include <functional>
0039 #include <memory>
0040 #include <sstream>
0041 #include <stdexcept>
0042 #include <string>
0043 #include <tuple>
0044 #include <utility>
0045 
0046 namespace py = pybind11;
0047 
0048 namespace {
0049 
0050 using scalar_t = detray::test::scalar;
0051 using algebra_t = detray::test::algebra;
0052 using detector_t = detray::host::detector<detray::test::default_metadata>;
0053 using propagation_config_t = detray::propagation::config;
0054 using toy_det_config_t = detray::toy_det_config<scalar_t>;
0055 using test_config_t = detray::test::configuration<scalar_t>;
0056 using uniform_track_generator_config_t =
0057     detray::uniform_track_generator_config<scalar_t>;
0058 using material_scan_config_t = detray::test::material_scan<detector_t>::config;
0059 using material_validation_config_t =
0060     detray::test::material_validation_config<algebra_t>;
0061 
0062 template <typename T>
0063 std::string to_string(const T &obj) {
0064   std::ostringstream os;
0065   os << obj;
0066   return os.str();
0067 }
0068 
0069 /// Run gtest for checks registered in @p register_checks_fn
0070 ///
0071 /// Since the gtest state is global, this implementation can be run only once.
0072 int run_gtest(const std::function<void()> &register_checks_fn) {
0073   static bool already_run = false;
0074   if (already_run) {
0075     throw std::runtime_error("gtest can only be run once per process");
0076   }
0077   already_run = true;
0078 
0079   ::testing::InitGoogleTest();
0080   register_checks_fn();
0081   return RUN_ALL_TESTS();
0082 }
0083 
0084 /// Run the CPU material validation for the given detector.
0085 int run_material_validation(const detector_t &det,
0086                             const detray::name_map &names,
0087                             const material_scan_config_t &scan_cfg,
0088                             const material_validation_config_t &val_cfg) {
0089   detector_t::geometry_context ctx{};
0090   auto wb = std::make_shared<detray::test::whiteboard>();
0091 
0092   return run_gtest([&] {
0093     detray::test::register_checks<detray::test::material_scan>(
0094         det, names, scan_cfg, ctx, wb);
0095     detray::test::register_checks<detray::test::material_validation>(
0096         det, names, val_cfg, ctx, wb);
0097   });
0098 }
0099 
0100 /// Build a toy detector using @p mr as configured by @p cfg .
0101 ///
0102 /// The memory resource object is automatically kept alive at least as long as
0103 /// the returned detector object.
0104 std::pair<py::object, detray::name_map> build_toy_detector(
0105     std::shared_ptr<vecmem::memory_resource> mr, const toy_det_config_t &cfg) {
0106   auto [det, names] = detray::build_toy_detector<algebra_t>(*mr, cfg);
0107 
0108   py::object detector = py::cast(std::move(det));
0109   py::detail::keep_alive_impl(detector, py::cast(std::move(mr)));
0110 
0111   return {std::move(detector), std::move(names)};
0112 }
0113 
0114 }  // namespace
0115 
0116 PYBIND11_MODULE(detray_tests_python_bindings, m) {
0117   m.doc() = "Detray tests bindings";
0118 
0119   py::class_<toy_det_config_t>(m, "ToyDetectorConfig")
0120       .def(py::init<>())
0121       .def_property(
0122           "n_brl_layers",
0123           [](const toy_det_config_t &c) { return c.n_brl_layers(); },
0124           [](toy_det_config_t &c, unsigned int n) { c.n_brl_layers(n); },
0125           "Number of barrel layers [0-4]")
0126       .def_property(
0127           "n_edc_layers",
0128           [](const toy_det_config_t &c) { return c.n_edc_layers(); },
0129           [](toy_det_config_t &c, unsigned int n) { c.n_edc_layers(n); },
0130           "Number of endcap layers on either side [0-7]")
0131       .def_property(
0132           "use_material_maps",
0133           [](const toy_det_config_t &c) { return c.use_material_maps(); },
0134           [](toy_det_config_t &c, bool b) { c.use_material_maps(b); },
0135           "Put material maps on the portals instead of homogeneous material "
0136           "on the modules")
0137       .def("__repr__", &to_string<toy_det_config_t>);
0138 
0139   m.def("build_toy_detector", &build_toy_detector, py::arg("memory_resource"),
0140         py::arg("config"),
0141         "Build a toy detector using the given memory resource, as configured "
0142         "by a ToyDetectorConfig. The returned detector keeps the memory "
0143         "resource alive for as long as it is used");
0144 
0145   py::class_<uniform_track_generator_config_t>(m, "UniformTrackGeneratorConfig")
0146       .def(py::init<>())
0147       .def_property(
0148           "seed",
0149           [](const uniform_track_generator_config_t &c) { return c.seed(); },
0150           [](uniform_track_generator_config_t &c, std::uint64_t s) {
0151             c.seed(s);
0152           },
0153           "Monte-Carlo seed")
0154       .def(
0155           "n_tracks",
0156           [](const uniform_track_generator_config_t &c) {
0157             return c.n_tracks();
0158           },
0159           "Total number of tracks")
0160       .def_property(
0161           "phi_range",
0162           [](const uniform_track_generator_config_t &c) {
0163             const auto r = c.phi_range();
0164             return std::make_pair(r[0], r[1]);
0165           },
0166           [](uniform_track_generator_config_t &c,
0167              const std::pair<scalar_t, scalar_t> &r) {
0168             c.phi_range(r.first, r.second);
0169           },
0170           "Phi range (min, max) (native rad)")
0171       .def_property(
0172           "theta_range",
0173           [](const uniform_track_generator_config_t &c) {
0174             const auto r = c.theta_range();
0175             return std::make_pair(r[0], r[1]);
0176           },
0177           [](uniform_track_generator_config_t &c,
0178              const std::pair<scalar_t, scalar_t> &r) {
0179             c.theta_range(r.first, r.second);
0180           },
0181           "Theta range (min, max) (native rad)")
0182       .def_property(
0183           "eta_range",
0184           [](const uniform_track_generator_config_t &c) {
0185             const auto r = c.eta_range();
0186             return std::make_pair(r[0], r[1]);
0187           },
0188           [](uniform_track_generator_config_t &c,
0189              const std::pair<scalar_t, scalar_t> &r) {
0190             c.eta_range(r.first, r.second);
0191           },
0192           "Eta range (min, max)")
0193       .def_property(
0194           "phi_steps",
0195           [](const uniform_track_generator_config_t &c) {
0196             return c.phi_steps();
0197           },
0198           [](uniform_track_generator_config_t &c, std::size_t n) {
0199             c.phi_steps(n);
0200           },
0201           "Number of phi steps")
0202       .def_property(
0203           "theta_steps",
0204           [](const uniform_track_generator_config_t &c) {
0205             return c.theta_steps();
0206           },
0207           [](uniform_track_generator_config_t &c, std::size_t n) {
0208             c.theta_steps(n);
0209           },
0210           "Number of theta steps")
0211       .def_property(
0212           "eta_steps",
0213           [](const uniform_track_generator_config_t &c) {
0214             return c.eta_steps();
0215           },
0216           [](uniform_track_generator_config_t &c, std::size_t n) {
0217             c.eta_steps(n);
0218           },
0219           "Number of eta steps")
0220       .def_property(
0221           "uniform_eta",
0222           [](const uniform_track_generator_config_t &c) {
0223             return c.uniform_eta();
0224           },
0225           [](uniform_track_generator_config_t &c, bool b) { c.uniform_eta(b); },
0226           "Whether to step uniformly in eta")
0227       .def_property(
0228           "origin",
0229           [](const uniform_track_generator_config_t &c) {
0230             const auto &o = c.origin();
0231             return std::make_tuple(o[0], o[1], o[2]);
0232           },
0233           [](uniform_track_generator_config_t &c,
0234              const std::tuple<scalar_t, scalar_t, scalar_t> &o) {
0235             c.origin(std::get<0>(o), std::get<1>(o), std::get<2>(o));
0236           },
0237           "Track origin")
0238       .def(
0239           "p_T",
0240           [](uniform_track_generator_config_t &c, scalar_t p)
0241               -> uniform_track_generator_config_t & { return c.p_T(p); },
0242           py::arg("p"), py::return_value_policy::reference,
0243           "Set the transverse momentum magnitude")
0244       .def(
0245           "p_tot",
0246           [](uniform_track_generator_config_t &c, scalar_t p)
0247               -> uniform_track_generator_config_t & { return c.p_tot(p); },
0248           py::arg("p"), py::return_value_policy::reference,
0249           "Set the total momentum magnitude")
0250       .def(
0251           "mom_range",
0252           [](const uniform_track_generator_config_t &c) {
0253             const auto r = c.mom_range();
0254             return std::make_pair(r[0], r[1]);
0255           },
0256           "Momentum range")
0257       .def_property(
0258           "randomize_charge",
0259           [](const uniform_track_generator_config_t &c) {
0260             return c.randomize_charge();
0261           },
0262           [](uniform_track_generator_config_t &c, bool b) {
0263             c.randomize_charge(b);
0264           },
0265           "Randomly flip the charge sign")
0266       .def_property(
0267           "time",
0268           [](const uniform_track_generator_config_t &c) { return c.time(); },
0269           [](uniform_track_generator_config_t &c, scalar_t t) { c.time(t); },
0270           "Track time")
0271       .def_property(
0272           "charge",
0273           [](const uniform_track_generator_config_t &c) { return c.charge(); },
0274           [](uniform_track_generator_config_t &c, scalar_t q) { c.charge(q); },
0275           "Track charge")
0276       .def(
0277           "is_pT",
0278           [](const uniform_track_generator_config_t &c) { return c.is_pT(); },
0279           "Whether the momentum magnitude is interpreted as transverse")
0280       .def("__repr__", &to_string<uniform_track_generator_config_t>);
0281 
0282   py::class_<test_config_t>(m, "TestConfig")
0283       .def_property(
0284           "tol", [](const test_config_t &c) { return c.tol(); },
0285           [](test_config_t &c, scalar_t t) { c.tol(t); },
0286           "Tolerance to compare two floating point values")
0287       .def_property(
0288           "propagation",
0289           [](test_config_t &c) -> propagation_config_t & {
0290             return c.propagation();
0291           },
0292           [](test_config_t &c, const propagation_config_t &v) {
0293             c.propagation() = v;
0294           },
0295           py::return_value_policy::reference_internal,
0296           "Propagation configuration");
0297 
0298   py::class_<material_validation_config_t, test_config_t>(
0299       m, "MaterialValidationConfig")
0300       .def(py::init<>())
0301       .def_property(
0302           "name",
0303           [](const material_validation_config_t &c) { return c.name(); },
0304           [](material_validation_config_t &c, const std::string &n) {
0305             c.name(n);
0306           },
0307           "Name of the test")
0308       .def_property(
0309           "material_file",
0310           [](const material_validation_config_t &c) {
0311             return c.material_file();
0312           },
0313           [](material_validation_config_t &c, const std::string &f) {
0314             c.material_file(f);
0315           },
0316           "Name of the output file with the navigation material traces")
0317       .def_property(
0318           "n_tracks",
0319           [](const material_validation_config_t &c) { return c.n_tracks(); },
0320           [](material_validation_config_t &c, std::size_t n) { c.n_tracks(n); },
0321           "Maximal number of test tracks to run")
0322       .def_property(
0323           "relative_error",
0324           [](const material_validation_config_t &c) {
0325             return c.relative_error();
0326           },
0327           [](material_validation_config_t &c, scalar_t re) {
0328             c.relative_error(re);
0329           },
0330           "Allowed relative discrepancy between truth and navigation material")
0331       .def("__repr__", &to_string<material_validation_config_t>);
0332 
0333   py::class_<material_scan_config_t, test_config_t>(m, "MaterialScanConfig")
0334       .def(py::init<>())
0335       .def_property(
0336           "name", [](const material_scan_config_t &c) { return c.name(); },
0337           [](material_scan_config_t &c, const std::string &n) { c.name(n); },
0338           "Name of the test")
0339       .def_property(
0340           "material_file",
0341           [](const material_scan_config_t &c) { return c.material_file(); },
0342           [](material_scan_config_t &c, const std::string &f) {
0343             c.material_file(f);
0344           },
0345           "Name of the output file with the material traces")
0346       .def_property(
0347           "overlaps_removal",
0348           [](const material_scan_config_t &c) { return c.overlaps_removal(); },
0349           [](material_scan_config_t &c, bool o) { c.overlaps_removal(o); },
0350           "Perform overlaps removal")
0351       .def_property(
0352           "overlaps_tol",
0353           [](const material_scan_config_t &c) { return c.overlaps_tol(); },
0354           [](material_scan_config_t &c, scalar_t t) { c.overlaps_tol(t); },
0355           "Tolerance for considering surfaces to be overlapping")
0356       .def_property(
0357           "track_generator",
0358           [](material_scan_config_t &c) -> uniform_track_generator_config_t & {
0359             return c.track_generator();
0360           },
0361           [](material_scan_config_t &c,
0362              const uniform_track_generator_config_t &v) {
0363             c.track_generator() = v;
0364           },
0365           py::return_value_policy::reference_internal,
0366           "Track generator configuration")
0367       .def("__repr__", &to_string<material_scan_config_t>);
0368 
0369   m.def("run_material_validation", &run_material_validation,
0370         py::arg("detector"), py::arg("names"), py::arg("scan_config"),
0371         py::arg("validation_config"),
0372         "Run the CPU material validation for the given detector.");
0373 }