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/Digitization/MeasurementCreation.hpp"
0010 #include "ActsExamples/EventData/IndexSourceLink.hpp"
0011 #include "ActsExamples/EventData/Measurement.hpp"
0012 #include "ActsPython/Utilities/ProxyTether.hpp"
0013 #include "ActsPython/Utilities/WhiteBoardRegistry.hpp"
0014 
0015 #include <vector>
0016 
0017 #include <pybind11/pybind11.h>
0018 #include <pybind11/stl.h>
0019 
0020 namespace py = pybind11;
0021 
0022 using namespace ActsExamples;
0023 
0024 namespace ActsPython {
0025 
0026 void addEventDataMeasurements(py::module& mex) {
0027   py::class_<IndexSourceLink>(mex, "IndexSourceLink")
0028       .def(py::init<Acts::GeometryIdentifier, Index>(), py::arg("geometryId"),
0029            py::arg("index"))
0030       .def_static(
0031           "fromSourceLink",
0032           [](Acts::SourceLink const& sl) { return sl.get<IndexSourceLink>(); })
0033       .def("toSourceLink",
0034            [](const IndexSourceLink& self) { return Acts::SourceLink(self); })
0035       .def("index", &IndexSourceLink::index)
0036       .def("geometryId", &IndexSourceLink::geometryId);
0037 
0038   // The measurement proxy is bound as a ProxyTether (see ProxyTether.hpp); the
0039   // type-erased alive-check lets both MeasurementContainer and
0040   // MeasurementSubset produce the same bound proxy type.
0041   using MeasTether = ProxyTether<ConstVariableBoundMeasurementProxy>;
0042   constexpr auto mcAlive = &ownerAlive<MeasurementContainer>;
0043   constexpr auto msAlive = &ownerAlive<MeasurementSubset>;
0044 
0045   // Register iterator types before the __iter__ bindings that return them.
0046   bindIndexIteratorTether<MeasurementContainer>(
0047       mex, "_MeasurementContainerIterator");
0048   bindIndexIteratorTether<MeasurementSubset>(mex, "_MeasurementSubsetIterator");
0049 
0050   using MeasProxy = ConstVariableBoundMeasurementProxy;
0051   py::class_<MeasTether>(mex, "ConstVariableBoundMeasurementProxy")
0052       .def_property_readonly(
0053           "geometryId", tetheredRead<MeasTether>(
0054                             [](const MeasProxy& s) { return s.geometryId(); }))
0055       .def_property_readonly(
0056           "size",
0057           tetheredRead<MeasTether>([](const MeasProxy& s) { return s.size(); }))
0058       .def_property_readonly("index",
0059                              tetheredRead<MeasTether>(
0060                                  [](const MeasProxy& s) { return s.index(); }))
0061       .def_property_readonly("fullParameters",
0062                              tetheredRead<MeasTether>([](const MeasProxy& s) {
0063                                return s.fullParameters();
0064                              }))
0065       .def_property_readonly("fullCovariance",
0066                              tetheredRead<MeasTether>([](const MeasProxy& s) {
0067                                return s.fullCovariance();
0068                              }))
0069       .def_property_readonly(
0070           "subspaceIndices",
0071           tetheredRead<MeasTether>([](const MeasProxy& self) {
0072             auto indices = self.subspaceHelper().indices();
0073             return std::vector<int>(indices.begin(), indices.end());
0074           }));
0075 
0076   auto measurementContainer =
0077       py::classh<MeasurementContainer>(mex, "MeasurementContainer")
0078           .def(py::init([]() { return MeasurementContainer(); }))
0079           .def("__len__", &MeasurementContainer::size)
0080           .def("reserve", &MeasurementContainer::reserve)
0081           .def(
0082               "emplaceMeasurement",
0083               [](py::object self, Acts::GeometryIdentifier geometryId,
0084                  const std::vector<int>& indices,
0085                  const std::vector<double>& par,
0086                  const std::vector<double>& cov) -> MeasTether {
0087                 auto& container = self.cast<MeasurementContainer&>();
0088                 if (indices.size() != par.size() ||
0089                     indices.size() != cov.size()) {
0090                   throw std::invalid_argument(
0091                       "Indices, parameters, and variances must have the same "
0092                       "size");
0093                 }
0094 
0095                 std::vector<Acts::BoundIndices> boundIndices;
0096                 for (auto i : indices) {
0097                   if (i < 0 || i >= static_cast<int>(Acts::eBoundSize)) {
0098                     throw std::out_of_range("Subspace index out of range");
0099                   }
0100                   boundIndices.push_back(static_cast<Acts::BoundIndices>(i));
0101                 }
0102 
0103                 // Use existing helpers to convert the input to the measurement
0104                 DigitizedParameters dParams;
0105                 dParams.indices = boundIndices;
0106                 dParams.values = par;
0107                 dParams.variances = cov;
0108                 return MeasTether{
0109                     self,
0110                     ConstVariableBoundMeasurementProxy{
0111                         createMeasurement(container, geometryId, dParams)},
0112                     mcAlive};
0113               },
0114               py::arg("geometryId"), py::arg("indices"), py::arg("parameters"),
0115               py::arg("covariance"))
0116           .def("__getitem__",
0117                [](py::object self, MeasurementContainer::Index idx) {
0118                  const auto& container =
0119                      self.cast<const MeasurementContainer&>();
0120                  return MeasTether{self, container.getMeasurement(idx),
0121                                    mcAlive};
0122                })
0123           .def("__iter__", [](py::object self) {
0124             return IndexIteratorTether<MeasurementContainer>{
0125                 self, 0,
0126                 [](const py::object& owner, MeasurementContainer& c,
0127                    std::size_t i) {
0128                   const MeasurementContainer& cc = c;
0129                   return py::cast(
0130                       MeasTether{owner, cc.getMeasurement(i),
0131                                  &ownerAlive<MeasurementContainer>});
0132                 }};
0133           });
0134 
0135   WhiteBoardRegistry::registerClass(measurementContainer);
0136 
0137   // bind measurement subset
0138   auto measurementSubset =
0139       py::classh<MeasurementSubset>(mex, "MeasurementSubset")
0140           .def(py::init(
0141                    [](const MeasurementContainer& container,
0142                       const std::vector<MeasurementContainer::Index>& indices) {
0143                      return MeasurementSubset(container, indices);
0144                    }),
0145                py::keep_alive<0, 1>(), py::arg("container"), py::arg("indices"))
0146           .def("__len__",
0147                [](const MeasurementSubset& self) { return self.size(); })
0148           .def("__getitem__",
0149                [](py::object self, std::size_t i) {
0150                  const auto& subset = self.cast<const MeasurementSubset&>();
0151                  if (i >= subset.size()) {
0152                    throw py::index_error("index out of range");
0153                  }
0154                  return MeasTether{self, subset.at(i), msAlive};
0155                })
0156           .def("__iter__",
0157                [](py::object self) {
0158                  return IndexIteratorTether<MeasurementSubset>{
0159                      self, 0,
0160                      [](const py::object& owner, MeasurementSubset& c,
0161                         std::size_t i) {
0162                        const MeasurementSubset& cc = c;
0163                        return py::cast(MeasTether{
0164                            owner, cc.at(i), &ownerAlive<MeasurementSubset>});
0165                      }};
0166                })
0167           .def(
0168               "getMeasurement",
0169               [](py::object self, MeasurementContainer::Index idx) {
0170                 const auto& subset = self.cast<const MeasurementSubset&>();
0171                 return MeasTether{self, subset.getMeasurement(idx), msAlive};
0172               },
0173               py::arg("index"));
0174 
0175   WhiteBoardRegistry::registerClass(measurementSubset);
0176 }
0177 
0178 }  // namespace ActsPython