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 "Acts/EventData/AnyTrackProxy.hpp"
0010 #include "Acts/EventData/AnyTrackStateProxy.hpp"
0011 #include "Acts/EventData/TrackStatePropMask.hpp"
0012 #include "Acts/Utilities/HashedString.hpp"
0013 #include "ActsExamples/EventData/Track.hpp"
0014 
0015 #include <pybind11/pybind11.h>
0016 #include <pybind11/stl.h>
0017 
0018 #include "EventDataHelpers.hpp"
0019 
0020 namespace py = pybind11;
0021 
0022 using namespace ActsExamples;
0023 using namespace ActsPython::EventDataHelpers;
0024 
0025 namespace {
0026 
0027 /// Bind the surface shared by the concrete and Any track proxies: all four
0028 /// (`ConstTrackProxy`/`TrackProxy`/`AnyConstTrackProxy`/`AnyMutableTrackProxy`)
0029 /// publicly inherit the same `TrackProxyCommon` base.
0030 template <typename Cls>
0031 void bindTrackProxyCommon(Cls& cls) {
0032   using Proxy = typename Cls::type;
0033   cls.def_property_readonly("index", &Proxy::index)
0034       .def_property_readonly("referenceSurface", &Proxy::referenceSurface)
0035       .def_property_readonly("hasReferenceSurface", &Proxy::hasReferenceSurface)
0036       .def_property_readonly("particleHypothesis", &Proxy::particleHypothesis)
0037       .def_property_readonly("nTrackStates", &Proxy::nTrackStates)
0038       .def_property_readonly("isForwardLinked", &Proxy::isForwardLinked)
0039       .def_property_readonly("loc0", &Proxy::loc0)
0040       .def_property_readonly("loc1", &Proxy::loc1)
0041       .def_property_readonly("phi", &Proxy::phi)
0042       .def_property_readonly("theta", &Proxy::theta)
0043       .def_property_readonly("time", &Proxy::time)
0044       .def_property_readonly("qOverP", &Proxy::qOverP)
0045       .def_property_readonly("charge", &Proxy::charge)
0046       .def_property_readonly("absoluteMomentum", &Proxy::absoluteMomentum)
0047       .def_property_readonly("transverseMomentum", &Proxy::transverseMomentum)
0048       .def_property_readonly("direction", &Proxy::direction)
0049       .def_property_readonly("momentum", &Proxy::momentum)
0050       .def_property_readonly("fourMomentum", &Proxy::fourMomentum)
0051       .def(
0052           "hasColumn",
0053           [](const Proxy& self, const std::string& key) {
0054             return self.hasColumn(Acts::hashStringDynamic(key));
0055           },
0056           py::arg("key"))
0057       .def_property_readonly("trackStates",
0058                              py::cpp_function(
0059                                  [](Proxy& self) {
0060                                    auto range = self.trackStates();
0061                                    return py::make_iterator(range.begin(),
0062                                                             range.end());
0063                                  },
0064                                  py::keep_alive<0, 1>()))
0065       .def_property_readonly("trackStatesReversed",
0066                              py::cpp_function(
0067                                  [](Proxy& self) {
0068                                    auto range = self.trackStatesReversed();
0069                                    return py::make_iterator(range.begin(),
0070                                                             range.end());
0071                                  },
0072                                  py::keep_alive<0, 1>()))
0073       .def_property_readonly(
0074           "innermostTrackState",
0075           [](Proxy& self) { return self.innermostTrackState(); })
0076       .def_property_readonly("outermostTrackState", [](Proxy& self) {
0077         return self.outermostTrackState();
0078       });
0079 
0080   // tipIndex/stemIndex/nMeasurements/nHoles/nOutliers/nSharedHits/nSplitHits/
0081   // chi2/nDoF are overloaded on const (by value) vs. mutable (by reference);
0082   // the accessor lambdas below select the overload via self's constness.
0083   bindScalar(cls, "tipIndex",
0084              [](auto& self) -> decltype(auto) { return self.tipIndex(); });
0085   bindScalar(cls, "stemIndex",
0086              [](auto& self) -> decltype(auto) { return self.stemIndex(); });
0087   bindScalar(cls, "nMeasurements",
0088              [](auto& self) -> decltype(auto) { return self.nMeasurements(); });
0089   bindScalar(cls, "nHoles",
0090              [](auto& self) -> decltype(auto) { return self.nHoles(); });
0091   bindScalar(cls, "nOutliers",
0092              [](auto& self) -> decltype(auto) { return self.nOutliers(); });
0093   bindScalar(cls, "nSharedHits",
0094              [](auto& self) -> decltype(auto) { return self.nSharedHits(); });
0095   bindScalar(cls, "nSplitHits",
0096              [](auto& self) -> decltype(auto) { return self.nSplitHits(); });
0097   bindScalar(cls, "chi2",
0098              [](auto& self) -> decltype(auto) { return self.chi2(); });
0099   bindScalar(cls, "nDoF",
0100              [](auto& self) -> decltype(auto) { return self.nDoF(); });
0101   bindEigen(cls, "parameters",
0102             [](auto& self) -> decltype(auto) { return self.parameters(); });
0103   bindEigen(cls, "covariance",
0104             [](auto& self) -> decltype(auto) { return self.covariance(); });
0105 }
0106 
0107 /// Bind the extra surface only the concrete `ConstTrackProxy`/`TrackProxy`
0108 /// have: `AnyTrackProxy` is a value-mutate handle, not a track-building API,
0109 /// so it lacks even the `referenceSurface`/`particleHypothesis` setters.
0110 template <typename Cls>
0111 void bindTrackProxyVector(Cls& cls) {
0112   using Proxy = typename Cls::type;
0113   cls.def_property_readonly("createParametersAtReference",
0114                             &Proxy::createParametersAtReference);
0115 
0116   if constexpr (!Proxy::ReadOnly) {
0117     cls.def_property("referenceSurface", &Proxy::referenceSurface,
0118                      &Proxy::setReferenceSurface)
0119         .def_property("particleHypothesis", &Proxy::particleHypothesis,
0120                       &Proxy::setParticleHypothesis)
0121         .def("appendTrackState", &Proxy::appendTrackState,
0122              py::arg("mask") = Acts::TrackStatePropMask::All,
0123              py::keep_alive<0, 1>())
0124         .def("linkForward", &Proxy::linkForward)
0125         .def("reverseTrackStates", &Proxy::reverseTrackStates,
0126              py::arg("invertJacobians") = false)
0127         // copyFrom*() are member function templates, so explicit
0128         // instantiation per source type replaces what would otherwise need a
0129         // lambda (a template has no single address to bind).
0130         .def("copyFrom", &Proxy::template copyFrom<ConstTrackProxy>)
0131         .def("copyFrom", &Proxy::template copyFrom<TrackProxy>)
0132         .def("copyFromWithoutStates",
0133              &Proxy::template copyFromWithoutStates<ConstTrackProxy>)
0134         .def("copyFromWithoutStates",
0135              &Proxy::template copyFromWithoutStates<TrackProxy>)
0136         .def("copyFromShallow",
0137              &Proxy::template copyFromShallow<ConstTrackProxy>)
0138         .def("copyFromShallow", &Proxy::template copyFromShallow<TrackProxy>);
0139   }
0140 }
0141 
0142 }  // namespace
0143 
0144 namespace ActsPython {
0145 
0146 void addEventDataTracks(py::module& mex) {
0147   py::class_<ConstTrackProxy> constTrackProxy(mex, "ConstTrackProxy");
0148   bindTrackProxyCommon(constTrackProxy);
0149   bindTrackProxyVector(constTrackProxy);
0150 
0151   py::class_<TrackProxy> trackProxy(mex, "TrackProxy");
0152   bindTrackProxyCommon(trackProxy);
0153   bindTrackProxyVector(trackProxy);
0154 
0155   py::classh<Acts::AnyConstTrackProxy> anyConstTrack(mex, "AnyConstTrackProxy");
0156   bindTrackProxyCommon(anyConstTrack);
0157   anyConstTrack.def(py::init<ConstTrackProxy>());
0158   py::implicitly_convertible<ConstTrackProxy, Acts::AnyConstTrackProxy>();
0159 
0160   py::classh<Acts::AnyMutableTrackProxy> anyMutableTrack(
0161       mex, "AnyMutableTrackProxy");
0162   bindTrackProxyCommon(anyMutableTrack);
0163   anyMutableTrack.def(py::init<TrackProxy>());
0164   py::implicitly_convertible<TrackProxy, Acts::AnyMutableTrackProxy>();
0165 }
0166 
0167 }  // namespace ActsPython