Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-26 09:25:00

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 #include "ActsExamples/EventData/Track.hpp"
0010 #include "ActsPython/Utilities/WhiteBoardRegistry.hpp"
0011 
0012 #include <memory>
0013 #include <string>
0014 
0015 #include <pybind11/numpy.h>
0016 #include <pybind11/pybind11.h>
0017 #include <pybind11/stl.h>
0018 
0019 namespace py = pybind11;
0020 
0021 using namespace ActsExamples;
0022 
0023 namespace ActsPython {
0024 
0025 void addEventDataTrackContainers(py::module& mex) {
0026   // Mark a numpy array as non-writeable and return it.
0027   const auto readOnly = [](auto arr) {
0028     arr.attr("flags").attr("writeable") = py::bool_(false);
0029     return arr;
0030   };
0031 
0032   // Factory for zero-copy 1-D views over a plain SoA column.
0033   // `accessor` is called with the backend and must return a const-ref to the
0034   // desired std::vector member.  dtype and stride are derived automatically.
0035   const auto col1D = [readOnly](auto accessor) {
0036     return [accessor, readOnly](const py::object& self_py) {
0037       const auto& backend =
0038           self_py.cast<const ConstTrackContainer&>().container();
0039       const auto N = static_cast<py::ssize_t>(backend.size_impl());
0040       const auto& vec = accessor(backend);
0041       using T = typename std::decay_t<decltype(vec)>::value_type;
0042       if (N == 0) {
0043         return readOnly(py::array_t<T>(py::ssize_t{0}));
0044       }
0045       return readOnly(py::array_t<T>({N}, {static_cast<py::ssize_t>(sizeof(T))},
0046                                      vec.data(), self_py));
0047     };
0048   };
0049 
0050   auto constTrackContainer =
0051       py::classh<ConstTrackContainer>(mex, "ConstTrackContainer")
0052           .def("__len__", &ConstTrackContainer::size)
0053           .def(
0054               "__iter__",
0055               [](const ConstTrackContainer& self) {
0056                 return py::make_iterator(self.begin(), self.end());
0057               },
0058               py::keep_alive<0, 1>())
0059           .def("__getitem__",
0060                py::overload_cast<ConstTrackContainer::IndexType>(
0061                    &ConstTrackContainer::getTrack, py::const_),
0062                py::keep_alive<0, 1>())
0063           .def("getTrack",
0064                py::overload_cast<ConstTrackContainer::IndexType>(
0065                    &ConstTrackContainer::getTrack, py::const_),
0066                py::arg("index"), py::keep_alive<0, 1>())
0067           .def(
0068               "hasColumn",
0069               [](const ConstTrackContainer& self, const std::string& key) {
0070                 return self.hasColumn(key);
0071               },
0072               py::arg("key"))
0073           // Build an independent, fully mutable copy; the source is left
0074           // untouched (unlike TrackContainer::makeConst, which moves out of
0075           // its backends) since a ConstTrackContainer may be shared.
0076           .def("makeMutable",
0077                [](const ConstTrackContainer& self) {
0078                  return TrackContainer{
0079                      std::make_shared<Acts::VectorTrackContainer>(
0080                          self.container()),
0081                      std::make_shared<Acts::VectorMultiTrajectory>(
0082                          self.trackStateContainer())};
0083                })
0084 
0085           // Zero-copy numpy array views of the underlying SoA columns.
0086           // The returned arrays are read-only and keep the container alive via
0087           // the numpy base-object mechanism as long as any array is alive.
0088 
0089           // shape (N, 6), float64 — bound track parameters [loc0, loc1, phi,
0090           // theta, q/p, t]. Strides account for potential Eigen alignment
0091           // padding between entries.
0092           .def_property_readonly(
0093               "parameters",
0094               [readOnly](const py::object& self_py) -> py::array_t<double> {
0095                 using CoeffsType = Acts::detail_tsp::FixedSizeTypes<
0096                     Acts::eBoundSize>::Coefficients;
0097                 const auto& backend =
0098                     self_py.cast<const ConstTrackContainer&>().container();
0099                 const auto N = static_cast<py::ssize_t>(backend.size_impl());
0100                 if (N == 0) {
0101                   return readOnly(py::array_t<double>({N, py::ssize_t{6}}));
0102                 }
0103                 return readOnly(py::array_t<double>(
0104                     {N, py::ssize_t{6}},
0105                     {static_cast<py::ssize_t>(sizeof(CoeffsType)),
0106                      static_cast<py::ssize_t>(sizeof(double))},
0107                     backend.m_params[0].data(), self_py));
0108               })
0109 
0110           // shape (N, 6, 6), float64, column-major sub-matrices (Eigen
0111           // default). arr[k, i, j] is row i, column j of track k's covariance.
0112           .def_property_readonly(
0113               "covariance",
0114               [readOnly](const py::object& self_py) -> py::array_t<double> {
0115                 using CovType = Acts::detail_tsp::FixedSizeTypes<
0116                     Acts::eBoundSize>::Covariance;
0117                 const auto& backend =
0118                     self_py.cast<const ConstTrackContainer&>().container();
0119                 const auto N = static_cast<py::ssize_t>(backend.size_impl());
0120                 if (N == 0) {
0121                   return readOnly(
0122                       py::array_t<double>({N, py::ssize_t{6}, py::ssize_t{6}}));
0123                 }
0124                 // Eigen column-major: row stride = 1 double, col stride = 6
0125                 // doubles.
0126                 constexpr py::ssize_t dbl = sizeof(double);
0127                 return readOnly(py::array_t<double>(
0128                     {N, py::ssize_t{6}, py::ssize_t{6}},
0129                     {static_cast<py::ssize_t>(sizeof(CovType)), dbl, 6 * dbl},
0130                     backend.m_cov[0].data(), self_py));
0131               })
0132 
0133           .def_property_readonly(
0134               "tipIndex",
0135               col1D([](const auto& b) -> const auto& { return b.m_tipIndex; }))
0136           .def_property_readonly(
0137               "stemIndex",
0138               col1D([](const auto& b) -> const auto& { return b.m_stemIndex; }))
0139           .def_property_readonly("nMeasurements",
0140                                  col1D([](const auto& b) -> const auto& {
0141                                    return b.m_nMeasurements;
0142                                  }))
0143           .def_property_readonly(
0144               "nHoles",
0145               col1D([](const auto& b) -> const auto& { return b.m_nHoles; }))
0146           .def_property_readonly(
0147               "chi2",
0148               col1D([](const auto& b) -> const auto& { return b.m_chi2; }))
0149           .def_property_readonly("ndf", col1D([](const auto& b) -> const auto& {
0150                                    return b.m_ndf;
0151                                  }))
0152           .def_property_readonly(
0153               "nOutliers",
0154               col1D([](const auto& b) -> const auto& { return b.m_nOutliers; }))
0155           .def_property_readonly("nSharedHits",
0156                                  col1D([](const auto& b) -> const auto& {
0157                                    return b.m_nSharedHits;
0158                                  }))
0159           .def_property_readonly("nSplitHits",
0160                                  col1D([](const auto& b) -> const auto& {
0161                                    return b.m_nSplitHits;
0162                                  }));
0163 
0164   WhiteBoardRegistry::registerClass(constTrackContainer);
0165 
0166   py::class_<TrackContainer>(mex, "TrackContainer")
0167       .def(py::init([]() {
0168         return TrackContainer{std::make_shared<Acts::VectorTrackContainer>(),
0169                               std::make_shared<Acts::VectorMultiTrajectory>()};
0170       }))
0171       .def(
0172           py::init([](const ConstTrackContainer& other) {
0173             return TrackContainer{
0174                 std::make_shared<Acts::VectorTrackContainer>(other.container()),
0175                 std::make_shared<Acts::VectorMultiTrajectory>(
0176                     other.trackStateContainer())};
0177           }),
0178           py::arg("other"))
0179       .def("__len__", &TrackContainer::size)
0180       .def(
0181           "__iter__",
0182           [](TrackContainer& self) {
0183             return py::make_iterator(self.begin(), self.end());
0184           },
0185           py::keep_alive<0, 1>())
0186       .def("__getitem__",
0187            py::overload_cast<TrackContainer::IndexType>(
0188                &TrackContainer::getTrack),
0189            py::keep_alive<0, 1>())
0190       .def("getTrack",
0191            py::overload_cast<TrackContainer::IndexType>(
0192                &TrackContainer::getTrack),
0193            py::arg("index"), py::keep_alive<0, 1>())
0194       .def("makeTrack", &TrackContainer::makeTrack, py::keep_alive<0, 1>())
0195       .def("removeTrack", &TrackContainer::removeTrack, py::arg("index"))
0196       .def("clear", &TrackContainer::clear)
0197       .def(
0198           "hasColumn",
0199           [](const TrackContainer& self, const std::string& key) {
0200             return self.hasColumn(key);
0201           },
0202           py::arg("key"))
0203       .def(
0204           "ensureDynamicColumns",
0205           [](TrackContainer& self, const ConstTrackContainer& other) {
0206             self.ensureDynamicColumns(other);
0207           },
0208           py::arg("other"))
0209       .def(
0210           "ensureDynamicColumns",
0211           [](TrackContainer& self, const TrackContainer& other) {
0212             self.ensureDynamicColumns(other);
0213           },
0214           py::arg("other"))
0215       .def("makeConst", [](TrackContainer& self) {
0216         return ConstTrackContainer{
0217             std::make_shared<Acts::ConstVectorTrackContainer>(
0218                 std::move(self.container())),
0219             std::make_shared<Acts::ConstVectorMultiTrajectory>(
0220                 std::move(self.trackStateContainer()))};
0221       });
0222 }
0223 
0224 }  // namespace ActsPython