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 #include "detray/navigation/volume_graph.hpp"
0015 #include "detray/propagator/propagation_config.hpp"
0016
0017
0018 #include <vecmem/memory/host_memory_resource.hpp>
0019 #include <vecmem/memory/memory_resource.hpp>
0020
0021
0022 #include <pybind11/pybind11.h>
0023 #include <pybind11/stl.h>
0024
0025
0026 #include <array>
0027 #include <cstddef>
0028 #include <memory>
0029 #include <sstream>
0030 #include <stdexcept>
0031 #include <string>
0032 #include <string_view>
0033
0034 namespace py = pybind11;
0035
0036 #define STRINGIFY(x) #x
0037
0038 #define STRINGIFY_HELPER(x) STRINGIFY(x)
0039
0040 namespace {
0041
0042
0043 using scalar_t = DETRAY_CUSTOM_SCALARTYPE;
0044 using algebra_t = detray::array<scalar_t>;
0045
0046
0047 using detector_t = detray::host::detector<detray::default_metadata<algebra_t>>;
0048 using toy_detector_t = detray::host::detector<detray::toy_metadata<algebra_t>>;
0049 using volume_descriptor_t = detector_t::volume_type;
0050 using volume_container_t = detector_t::volume_container;
0051 using surface_descriptor_t = detector_t::surface_type;
0052 using surface_store_t = detector_t::surface_lookup_container;
0053 using surface_container_t = detector_t::surface_container;
0054 using geometry_context_t = detector_t::geometry_context;
0055 using transform_store_t = detector_t::transform_container;
0056 using mask_store_t = detector_t::mask_container;
0057 using material_store_t = detector_t::material_container;
0058 using accelerator_store_t = detector_t::accelerator_container;
0059
0060
0061 using intersection_config_t = detray::intersection::config;
0062 using navigation_config_t = detray::navigation::config;
0063 using volume_graph_t = detray::volume_graph<detector_t>;
0064
0065
0066 using stepping_config_t = detray::stepping::config;
0067 using propagation_config_t = detray::propagation::config;
0068
0069 template <typename T>
0070 std::string to_string(const T &obj) {
0071 std::ostringstream os;
0072 os << obj;
0073 return os.str();
0074 }
0075
0076
0077 template <typename Container, typename Element>
0078 void bind_const_vector(py::module_ &m, const char *name) {
0079 py::class_<Container>(m, name)
0080 .def("__len__", [](const Container &c) { return c.size(); })
0081 .def(
0082 "__getitem__",
0083 [](const Container &c, py::ssize_t i) -> const Element & {
0084 const auto n = static_cast<py::ssize_t>(c.size());
0085 if (i < 0) {
0086 i += n;
0087 }
0088 if (i < 0 || i >= n) {
0089 throw py::index_error();
0090 }
0091 return c[static_cast<std::size_t>(i)];
0092 },
0093 py::arg("index"), py::return_value_policy::reference_internal,
0094 "Element at a given index")
0095 .def(
0096 "__iter__",
0097 [](const Container &c) {
0098 return py::make_iterator<
0099 py::return_value_policy::reference_internal,
0100 decltype(c.begin()), decltype(c.end()), const Element &>(
0101 c.begin(), c.end());
0102 },
0103 py::keep_alive<0, 1>(), "Iterate over the elements");
0104 }
0105
0106 }
0107
0108
0109
0110
0111 PYBIND11_MAKE_OPAQUE(volume_container_t)
0112 PYBIND11_MAKE_OPAQUE(surface_container_t)
0113
0114 PYBIND11_MODULE(detray_core_python_bindings, m) {
0115 m.doc() = "Detray core bindings";
0116
0117
0118
0119
0120 py::class_<volume_descriptor_t>(m, "VolumeDescriptor");
0121 py::class_<surface_descriptor_t>(m, "SurfaceDescriptor");
0122 bind_const_vector<surface_store_t, surface_descriptor_t>(m, "SurfaceStore");
0123 bind_const_vector<volume_container_t, volume_descriptor_t>(m,
0124 "VolumeContainer");
0125 bind_const_vector<surface_container_t, surface_descriptor_t>(
0126 m, "SurfaceContainer");
0127 py::class_<geometry_context_t>(m, "GeometryContext")
0128 .def(py::init<>())
0129 .def(py::init<detray::dindex>(), py::arg("index"))
0130 .def_property_readonly("index", &geometry_context_t::get,
0131 "Index into the geometry data store")
0132 .def("__repr__", [](const geometry_context_t &gctx) {
0133 return "GeometryContext(index=" + std::to_string(gctx.get()) + ")";
0134 });
0135 py::class_<transform_store_t>(m, "TransformStore");
0136 py::class_<mask_store_t>(m, "MaskStore");
0137 py::class_<material_store_t>(m, "MaterialStore");
0138 py::class_<accelerator_store_t>(m, "AcceleratorStore");
0139 py::class_<detray::name_map>(m, "NameMap")
0140 .def(py::init<>())
0141 .def_property(
0142 "detector_name",
0143 [](const detray::name_map &n) { return n.get_detector_name(); },
0144 [](detray::name_map &n, std::string_view name) {
0145 n.set_detector_name(name);
0146 },
0147 "Name of the detector")
0148 .def("empty", &detray::name_map::empty,
0149 "Whether no volume names are mapped")
0150 .def(
0151 "__contains__",
0152 [](const detray::name_map &n, detray::dindex index) {
0153 return n.contains(index);
0154 },
0155 py::arg("index"), "Whether a volume index is mapped")
0156 .def(
0157 "__contains__",
0158 [](const detray::name_map &n, std::string_view name) {
0159 return n.contains(name);
0160 },
0161 py::arg("name"), "Whether a volume name is mapped")
0162 .def(
0163 "__setitem__",
0164 [](detray::name_map &n, detray::dindex index,
0165 const std::string &name) { n.emplace(index, name); },
0166 py::arg("index"), py::arg("name"), "Map a volume index to a name")
0167 .def(
0168 "__getitem__",
0169 [](const detray::name_map &n, detray::dindex index) -> std::string {
0170 try {
0171 return n.at(index);
0172 } catch (const std::out_of_range &) {
0173 throw py::key_error(std::to_string(index));
0174 }
0175 },
0176 py::arg("index"), "Volume name at a volume index")
0177 .def(
0178 "__getitem__",
0179 [](const detray::name_map &n,
0180 std::string_view name) -> detray::dindex {
0181 try {
0182 return n.at(name);
0183 } catch (const std::out_of_range &) {
0184 throw py::key_error(std::string{name});
0185 }
0186 },
0187 py::arg("name"), "Volume index at a volume name")
0188 .def("clear", &detray::name_map::clear, "Clear detector and volume names")
0189 .def("clear_names", &detray::name_map::clear_names,
0190 "Clear volume names, keep the detector name")
0191 .def("__repr__", [](const detray::name_map &n) {
0192 return "NameMap(detector_name='" + n.get_detector_name() + "')";
0193 });
0194
0195 py::class_<vecmem::memory_resource, std::shared_ptr<vecmem::memory_resource>>(
0196 m, "MemoryResource");
0197 py::class_<vecmem::host_memory_resource, vecmem::memory_resource,
0198 std::shared_ptr<vecmem::host_memory_resource>>(
0199 m, "HostMemoryResource")
0200 .def(py::init<>());
0201
0202 py::class_<detector_t>(
0203 m, "DetectorDefaultMetadata" STRINGIFY_HELPER(DETRAY_CUSTOM_SCALARTYPE))
0204 .def(
0205 "name",
0206 [](const detector_t &d, const detray::name_map &names) {
0207 return d.name(names);
0208 },
0209 py::arg("names"), "Detector name")
0210 .def_property_readonly(
0211 "volumes",
0212 [](const detector_t &d) -> const volume_container_t & {
0213 return d.volumes();
0214 },
0215 py::return_value_policy::reference_internal, "All volumes")
0216 .def_property_readonly(
0217 "surfaces",
0218 [](const detector_t &d) -> const surface_store_t & {
0219 return d.surfaces();
0220 },
0221 py::return_value_policy::reference_internal, "All surfaces")
0222 .def_property_readonly(
0223 "portals",
0224 [](const detector_t &d) -> const surface_container_t & {
0225 return d.portals();
0226 },
0227 py::return_value_policy::reference_internal, "All portals")
0228 .def_property_readonly(
0229 "transform_store",
0230 [](const detector_t &d) -> const transform_store_t & {
0231 return d.transform_store();
0232 },
0233 py::return_value_policy::reference_internal, "Transform store")
0234 .def_property_readonly(
0235 "mask_store",
0236 [](const detector_t &d) -> const mask_store_t & {
0237 return d.mask_store();
0238 },
0239 py::return_value_policy::reference_internal, "Mask store")
0240 .def_property_readonly(
0241 "material_store",
0242 [](const detector_t &d) -> const material_store_t & {
0243 return d.material_store();
0244 },
0245 py::return_value_policy::reference_internal, "Material store")
0246 .def_property_readonly(
0247 "accelerator_store",
0248 [](const detector_t &d) -> const accelerator_store_t & {
0249 return d.accelerator_store();
0250 },
0251 py::return_value_policy::reference_internal, "Accelerator store")
0252 .def("__repr__", [](const detector_t &d) { return to_string(d); });
0253
0254 py::class_<toy_detector_t>(
0255 m, "DetectorToyMetadata" STRINGIFY_HELPER(DETRAY_CUSTOM_SCALARTYPE));
0256
0257
0258
0259
0260 py::class_<intersection_config_t>(m, "IntersectionConfig")
0261 .def(py::init<>())
0262 .def_readwrite("min_mask_tolerance",
0263 &intersection_config_t::min_mask_tolerance,
0264 "Minimum mask tolerance")
0265 .def_readwrite("max_mask_tolerance",
0266 &intersection_config_t::max_mask_tolerance,
0267 "Maximum mask tolerance")
0268 .def_readwrite("mask_tolerance_scalor",
0269 &intersection_config_t::mask_tolerance_scalor,
0270 "Mask tolerance scale factor")
0271 .def_readwrite("path_tolerance", &intersection_config_t::path_tolerance,
0272 "Tolerance to decide when a track is on a surface")
0273 .def_readwrite("overstep_tolerance",
0274 &intersection_config_t::overstep_tolerance,
0275 "How far behind the track position to look for candidates")
0276 .def("__repr__", &to_string<intersection_config_t>);
0277
0278 py::class_<navigation_config_t>(m, "NavigationConfig")
0279 .def(py::init<>())
0280 .def_readwrite("intersection", &navigation_config_t::intersection,
0281 "Intersection configuration")
0282 .def_readwrite(
0283 "search_window", &navigation_config_t::search_window,
0284 "Search window size for grid based acceleration structures")
0285 .def_readwrite(
0286 "accumulated_error", &navigation_config_t::accumulated_error,
0287 "Percentage of total track path to assume as accumulated error")
0288 .def_readwrite(
0289 "n_scattering_stddev", &navigation_config_t::n_scattering_stddev,
0290 "No. of standard deviations to assume to model the scattering noise")
0291 .def_readwrite("estimate_scattering_noise",
0292 &navigation_config_t::estimate_scattering_noise,
0293 "Add adaptive mask tolerance to navigation")
0294 .def("__repr__", &to_string<navigation_config_t>);
0295
0296 py::class_<volume_graph_t>(m, "VolumeGraph")
0297 .def(py::init<const detector_t &>(), py::arg("detector"),
0298 py::keep_alive<1, 2>(),
0299 "Build the volume graph of a detector. The detector is kept alive "
0300 "for as long as the graph is used")
0301 .def("to_dot_string", &volume_graph_t::to_dot_string,
0302 "The volume linking description in DOT syntax")
0303 .def("__repr__", &volume_graph_t::to_string);
0304
0305
0306
0307
0308 py::class_<stepping_config_t>(m, "SteppingConfig")
0309 .def(py::init<>())
0310 .def_readwrite("min_stepsize", &stepping_config_t::min_stepsize,
0311 "Minimum step size")
0312 .def_readwrite("rk_error_tol", &stepping_config_t::rk_error_tol,
0313 "Runge-Kutta numeric error tolerance")
0314 .def_readwrite("step_constraint", &stepping_config_t::step_constraint,
0315 "Step size constraint")
0316 .def_readwrite("path_limit", &stepping_config_t::path_limit,
0317 "Maximum path length of track")
0318 .def_readwrite("max_rk_updates", &stepping_config_t::max_rk_updates,
0319 "Maximum number of Runge-Kutta step trials")
0320 .def_readwrite("use_mean_loss", &stepping_config_t::use_mean_loss,
0321 "Use mean energy loss (Bethe), otherwise use most "
0322 "probable energy loss (Landau)")
0323 .def_readwrite("use_eloss_gradient",
0324 &stepping_config_t::use_eloss_gradient,
0325 "Use energy loss gradient in error propagation")
0326 .def_readwrite("use_field_gradient",
0327 &stepping_config_t::use_field_gradient,
0328 "Use field gradient in error propagation")
0329 .def_readwrite("do_covariance_transport",
0330 &stepping_config_t::do_covariance_transport,
0331 "Do covariance transport")
0332 .def("__repr__", &to_string<stepping_config_t>);
0333
0334 py::class_<propagation_config_t>(m, "PropagationConfig")
0335 .def(py::init<>())
0336 .def_readwrite("navigation", &propagation_config_t::navigation,
0337 "Navigation configuration")
0338 .def_readwrite("stepping", &propagation_config_t::stepping,
0339 "Stepping configuration")
0340 .def("__repr__", &to_string<propagation_config_t>);
0341 }