Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-03-28 07:45:17

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 #pragma once
0010 
0011 #include "Acts/EventData/SubspaceHelpers.hpp"
0012 #include "Acts/EventData/TrackStatePropMask.hpp"
0013 #include "Acts/EventData/TrackStateType.hpp"
0014 #include "Acts/EventData/Types.hpp"
0015 #include "Acts/Utilities/EigenConcepts.hpp"
0016 #include "Acts/Utilities/HashedString.hpp"
0017 
0018 #include <algorithm>
0019 #include <ranges>
0020 #include <string_view>
0021 
0022 namespace Acts {
0023 
0024 namespace detail_tsp {
0025 inline constexpr HashedString kPreviousKey = hashString("previous");
0026 inline constexpr HashedString kChi2Key = hashString("chi2");
0027 inline constexpr HashedString kPathLengthKey = hashString("pathLength");
0028 inline constexpr HashedString kTypeFlagsKey = hashString("typeFlags");
0029 inline constexpr HashedString kPredictedKey = hashString("predicted");
0030 inline constexpr HashedString kFilteredKey = hashString("filtered");
0031 inline constexpr HashedString kSmoothedKey = hashString("smoothed");
0032 inline constexpr HashedString kJacobianKey = hashString("jacobian");
0033 inline constexpr HashedString kProjectorKey = hashString("projector");
0034 inline constexpr HashedString kUncalibratedKey =
0035     hashString("uncalibratedSourceLink");
0036 inline constexpr HashedString kCalibratedKey = hashString("calibrated");
0037 inline constexpr HashedString kCalibratedCovKey = hashString("calibratedCov");
0038 inline constexpr HashedString kMeasDimKey = hashString("measdim");
0039 inline constexpr HashedString kNextKey = hashString("next");
0040 
0041 }  // namespace detail_tsp
0042 
0043 /// Common CRTP implementation shared by track state proxy front-ends.
0044 /// This base class provides access to track state components including
0045 /// predicted, filtered, and smoothed parameters and covariances, as well as
0046 /// calibrated measurement data and various metadata. The derived proxy must
0047 /// expose the underlying storage access methods.
0048 ///
0049 /// @tparam Derived The proxy implementation inheriting from this base
0050 /// @tparam read_only Whether the proxy provides mutable access
0051 template <typename Derived, bool read_only>
0052 class TrackStateProxyCommon {
0053  protected:
0054   /// Index type for track states
0055   using IndexType = Acts::TrackIndexType;
0056 
0057   /// Cast to derived class
0058   /// @return Reference to derived proxy
0059   constexpr Derived& derived() { return static_cast<Derived&>(*this); }
0060   /// Cast to derived class (const)
0061   /// @return Const reference to derived proxy
0062   constexpr const Derived& derived() const {
0063     return static_cast<const Derived&>(*this);
0064   }
0065 
0066  public:
0067   /// Retrieve the previous track state index in the linked trajectory.
0068   /// @return Index of the previous state or `kTrackIndexInvalid`.
0069   TrackIndexType previous() const {
0070     return derived()
0071         .template component<TrackIndexType, detail_tsp::kPreviousKey>();
0072   }
0073 
0074   /// Retrieve a mutable reference to the previous track state index.
0075   /// @return Mutable index of the previous state.
0076   TrackIndexType& previous()
0077     requires(!read_only)
0078   {
0079     return derived()
0080         .template component<TrackIndexType, detail_tsp::kPreviousKey>();
0081   }
0082 
0083   /// Check whether this state links to a previous state.
0084   /// @return True if the previous index is valid.
0085   bool hasPrevious() const { return previous() != kTrackIndexInvalid; }
0086 
0087   /// Check for presence of predicted track parameters.
0088   /// @return True if the predicted component exists.
0089   bool hasPredicted() const { return derived().has(detail_tsp::kPredictedKey); }
0090 
0091   /// Check for presence of filtered track parameters.
0092   /// @return True if the filtered component exists.
0093   bool hasFiltered() const { return derived().has(detail_tsp::kFilteredKey); }
0094 
0095   /// Check for presence of smoothed track parameters.
0096   /// @return True if the smoothed component exists.
0097   bool hasSmoothed() const { return derived().has(detail_tsp::kSmoothedKey); }
0098 
0099   /// Check for presence of a transport Jacobian.
0100   /// @return True if a Jacobian is stored.
0101   bool hasJacobian() const { return derived().has(detail_tsp::kJacobianKey); }
0102 
0103   /// Check for presence of a measurement projector.
0104   /// @return True if projector indices are stored.
0105   bool hasProjector() const { return derived().has(detail_tsp::kProjectorKey); }
0106 
0107   /// Check for presence of an uncalibrated source link.
0108   /// @return True if an uncalibrated source link is stored.
0109   bool hasUncalibratedSourceLink() const {
0110     return derived().has(detail_tsp::kUncalibratedKey);
0111   }
0112 
0113   /// Check for presence of calibrated measurement data.
0114   /// @return True if calibrated measurements exist.
0115   bool hasCalibrated() const {
0116     return derived().has(detail_tsp::kCalibratedKey);
0117   }
0118 
0119   /// @name Type aliases for parameter and covariance access
0120   /// @{
0121 
0122   /// Mutable Eigen map type for bound track parameters.
0123   using ParametersMap =
0124       typename detail_tsp::FixedSizeTypes<eBoundSize, false>::CoefficientsMap;
0125   /// Const Eigen map type for bound track parameters.
0126   using ConstParametersMap =
0127       typename detail_tsp::FixedSizeTypes<eBoundSize, true>::CoefficientsMap;
0128   /// Mutable Eigen map type for bound track parameter covariance.
0129   using CovarianceMap =
0130       typename detail_tsp::FixedSizeTypes<eBoundSize, false>::CovarianceMap;
0131   /// Const Eigen map type for bound track parameter covariance.
0132   using ConstCovarianceMap =
0133       typename detail_tsp::FixedSizeTypes<eBoundSize, true>::CovarianceMap;
0134 
0135   /// Mutable Eigen map type for calibrated measurements (dynamic size).
0136   using EffectiveCalibratedMap =
0137       typename detail_tsp::DynamicSizeTypes<false>::CoefficientsMap;
0138   /// Const Eigen map type for calibrated measurements (dynamic size).
0139   using ConstEffectiveCalibratedMap =
0140       typename detail_tsp::DynamicSizeTypes<true>::CoefficientsMap;
0141   /// Mutable Eigen map type for calibrated measurement covariance (dynamic
0142   /// size).
0143   using EffectiveCalibratedCovarianceMap =
0144       typename detail_tsp::DynamicSizeTypes<false>::CovarianceMap;
0145   /// Const Eigen map type for calibrated measurement covariance (dynamic size).
0146   using ConstEffectiveCalibratedCovarianceMap =
0147       typename detail_tsp::DynamicSizeTypes<true>::CovarianceMap;
0148 
0149   /// @}
0150 
0151   /// Access the predicted parameter vector.
0152   /// @return Bound parameter map for the predicted state.
0153   ConstParametersMap predicted() const {
0154     assert(hasPredicted());
0155     const auto parIndex =
0156         derived().template component<IndexType>(detail_tsp::kPredictedKey);
0157     return derived().parametersAtIndex(parIndex);
0158   }
0159 
0160   /// Access the predicted parameter vector.
0161   /// @return Mutable bound parameter map for the predicted state.
0162   ParametersMap predicted()
0163     requires(!read_only)
0164   {
0165     assert(hasPredicted());
0166     const auto parIndex =
0167         derived().template component<IndexType>(detail_tsp::kPredictedKey);
0168     return derived().parametersAtIndexMutable(parIndex);
0169   }
0170 
0171   /// Access the predicted covariance matrix.
0172   /// @return Bound covariance map for the predicted state.
0173   ConstCovarianceMap predictedCovariance() const {
0174     assert(hasPredicted());
0175     const auto covIndex =
0176         derived().template component<IndexType>(detail_tsp::kPredictedKey);
0177     return derived().covarianceAtIndex(covIndex);
0178   }
0179 
0180   /// Access the predicted covariance matrix.
0181   /// @return Mutable bound covariance map for the predicted state.
0182   CovarianceMap predictedCovariance()
0183     requires(!read_only)
0184   {
0185     assert(hasPredicted());
0186     const auto covIndex =
0187         derived().template component<IndexType>(detail_tsp::kPredictedKey);
0188     return derived().covarianceAtIndexMutable(covIndex);
0189   }
0190 
0191   /// Access the filtered parameter vector.
0192   /// @return Bound parameter map for the filtered state.
0193   ConstParametersMap filtered() const {
0194     assert(hasFiltered());
0195     const auto parIndex =
0196         derived().template component<IndexType>(detail_tsp::kFilteredKey);
0197     return derived().parametersAtIndex(parIndex);
0198   }
0199 
0200   /// Access the filtered parameter vector.
0201   /// @return Mutable bound parameter map for the filtered state.
0202   ParametersMap filtered()
0203     requires(!read_only)
0204   {
0205     const auto parIndex =
0206         derived().template component<IndexType>(detail_tsp::kFilteredKey);
0207     return derived().parametersAtIndexMutable(parIndex);
0208   }
0209 
0210   /// Access the filtered covariance matrix.
0211   /// @return Bound covariance map for the filtered state.
0212   ConstCovarianceMap filteredCovariance() const {
0213     assert(hasFiltered());
0214     const auto covIndex =
0215         derived().template component<IndexType>(detail_tsp::kFilteredKey);
0216     return derived().covarianceAtIndex(covIndex);
0217   }
0218 
0219   /// Access the filtered covariance matrix.
0220   /// @return Mutable bound covariance map for the filtered state.
0221   CovarianceMap filteredCovariance()
0222     requires(!read_only)
0223   {
0224     const auto covIndex =
0225         derived().template component<IndexType>(detail_tsp::kFilteredKey);
0226     return derived().covarianceAtIndexMutable(covIndex);
0227   }
0228 
0229   /// Access the smoothed parameter vector.
0230   /// @return Bound parameter map for the smoothed state.
0231   ConstParametersMap smoothed() const {
0232     assert(hasSmoothed());
0233     const auto parIndex =
0234         derived().template component<IndexType>(detail_tsp::kSmoothedKey);
0235     return derived().parametersAtIndex(parIndex);
0236   }
0237 
0238   /// Access the smoothed parameter vector.
0239   /// @return Mutable bound parameter map for the smoothed state.
0240   ParametersMap smoothed()
0241     requires(!read_only)
0242   {
0243     assert(hasSmoothed());
0244     const auto parIndex =
0245         derived().template component<IndexType>(detail_tsp::kSmoothedKey);
0246     return derived().parametersAtIndexMutable(parIndex);
0247   }
0248 
0249   /// Access the smoothed covariance matrix.
0250   /// @return Bound covariance map for the smoothed state.
0251   ConstCovarianceMap smoothedCovariance() const {
0252     assert(hasSmoothed());
0253     const auto covIndex =
0254         derived().template component<IndexType>(detail_tsp::kSmoothedKey);
0255     return derived().covarianceAtIndex(covIndex);
0256   }
0257 
0258   /// Access the smoothed covariance matrix.
0259   /// @return Mutable bound covariance map for the smoothed state.
0260   CovarianceMap smoothedCovariance()
0261     requires(!read_only)
0262   {
0263     assert(hasSmoothed());
0264     const auto covIndex =
0265         derived().template component<IndexType>(detail_tsp::kSmoothedKey);
0266     return derived().covarianceAtIndexMutable(covIndex);
0267   }
0268 
0269   /// Retrieve the accumulated path length.
0270   /// @return Path length stored on the state.
0271   double pathLength() const {
0272     return derived().template component<double, detail_tsp::kPathLengthKey>();
0273   }
0274 
0275   /// Retrieve a mutable reference to the accumulated path length.
0276   /// @return Mutable path length.
0277   double& pathLength()
0278     requires(!read_only)
0279   {
0280     return derived().template component<double, detail_tsp::kPathLengthKey>();
0281   }
0282 
0283   /// Retrieve the track-state type flags.
0284   /// @return Bit mask describing the state type.
0285   ConstTrackStateTypeMap typeFlags() const {
0286     const auto& raw = derived()
0287                           .template component<ConstTrackStateTypeMap::raw_type,
0288                                               detail_tsp::kTypeFlagsKey>();
0289     return ConstTrackStateTypeMap{raw};
0290   }
0291 
0292   /// Retrieve mutable track-state type flags.
0293   /// @return Mutable bit mask describing the state type.
0294   MutableTrackStateTypeMap typeFlags()
0295     requires(!read_only)
0296   {
0297     auto& raw = derived()
0298                     .template component<MutableTrackStateTypeMap::raw_type,
0299                                         detail_tsp::kTypeFlagsKey>();
0300     return MutableTrackStateTypeMap{raw};
0301   }
0302 
0303   /// Retrieve the local chi2 contribution.
0304   /// @return Chi2 value associated with this state.
0305   float chi2() const {
0306     return derived().template component<float, detail_tsp::kChi2Key>();
0307   }
0308 
0309   /// Retrieve a mutable reference to the local chi2 contribution.
0310   /// @return Mutable chi2 value.
0311   float& chi2()
0312     requires(!read_only)
0313   {
0314     return derived().template component<float, detail_tsp::kChi2Key>();
0315   }
0316 
0317   /// Decode the measurement projector indices.
0318   /// @return Bound parameter indices used for projection.
0319   BoundSubspaceIndices projectorSubspaceIndices() const {
0320     assert(hasProjector());
0321     const auto& serialized =
0322         derived()
0323             .template component<SerializedSubspaceIndices,
0324                                 detail_tsp::kProjectorKey>();
0325     return deserializeSubspaceIndices<eBoundSize>(serialized);
0326   }
0327 
0328   /// Returns the projector subspace indices
0329   /// @return The projector subspace indices
0330   template <std::size_t measdim>
0331   SubspaceIndices<measdim> projectorSubspaceIndices() const {
0332     BoundSubspaceIndices boundSubspace = projectorSubspaceIndices();
0333     SubspaceIndices<measdim> subspace;
0334     std::copy(boundSubspace.begin(), boundSubspace.begin() + measdim,
0335               subspace.begin());
0336     return subspace;
0337   }
0338 
0339   /// Creates a variable size subspace helper
0340   /// @return The subspace helper
0341   VariableBoundSubspaceHelper projectorSubspaceHelper() const {
0342     BoundSubspaceIndices boundSubspace = projectorSubspaceIndices();
0343     std::span<std::uint8_t> validSubspaceIndices(
0344         boundSubspace.begin(),
0345         boundSubspace.begin() + derived().calibratedSize());
0346     return VariableBoundSubspaceHelper(validSubspaceIndices);
0347   }
0348 
0349   /// Creates a fixed size subspace helper
0350   /// @return The subspace helper
0351   template <std::size_t measdim>
0352   FixedBoundSubspaceHelper<measdim> projectorSubspaceHelper() const {
0353     SubspaceIndices<measdim> subspace = projectorSubspaceIndices<measdim>();
0354     return FixedBoundSubspaceHelper<measdim>(subspace);
0355   }
0356 
0357   /// Store subspace indices describing the measurement projector.
0358   /// @tparam index_range_t Range of indices to encode.
0359   /// @param subspaceIndices Collection of bound indices forming the projector rows.
0360   template <std::ranges::sized_range index_range_t>
0361   void setProjectorSubspaceIndices(const index_range_t& subspaceIndices)
0362     requires(!read_only &&
0363              std::convertible_to<std::ranges::range_value_t<index_range_t>,
0364                                  std::uint8_t>)
0365   {
0366     assert(derived().template has<detail_tsp::kProjectorKey>());
0367     assert(subspaceIndices.size() <= eBoundSize);
0368     BoundSubspaceIndices boundSubspace{};
0369     std::transform(subspaceIndices.begin(), subspaceIndices.end(),
0370                    boundSubspace.begin(),
0371                    [](auto i) { return static_cast<std::uint8_t>(i); });
0372     derived()
0373         .template component<SerializedSubspaceIndices,
0374                             detail_tsp::kProjectorKey>() =
0375         serializeSubspaceIndices(boundSubspace);
0376   }
0377 
0378   /// Compute the property mask describing which components are present.
0379   /// @return Bit mask of available properties.
0380   TrackStatePropMask getMask() const {
0381     using PM = TrackStatePropMask;
0382     PM mask = PM::None;
0383     if (hasPredicted()) {
0384       mask |= PM::Predicted;
0385     }
0386     if (hasFiltered()) {
0387       mask |= PM::Filtered;
0388     }
0389     if (hasSmoothed()) {
0390       mask |= PM::Smoothed;
0391     }
0392     if (hasJacobian()) {
0393       mask |= PM::Jacobian;
0394     }
0395     if (hasCalibrated()) {
0396       mask |= PM::Calibrated;
0397     }
0398     return mask;
0399   }
0400 
0401   /// Access the best available parameters (smoothed, filtered, or predicted).
0402   /// @return Bound parameter map for the state.
0403   ConstParametersMap parameters() const {
0404     if (hasSmoothed()) {
0405       return smoothed();
0406     } else if (hasFiltered()) {
0407       return filtered();
0408     }
0409     return predicted();
0410   }
0411 
0412   /// Access the best available covariance (smoothed, filtered, or predicted).
0413   /// @return Bound covariance map for the state.
0414   ConstCovarianceMap covariance() const {
0415     if (hasSmoothed()) {
0416       return smoothedCovariance();
0417     } else if (hasFiltered()) {
0418       return filteredCovariance();
0419     }
0420     return predictedCovariance();
0421   }
0422 
0423   /// Access the calibrated measurement values with runtime dimension.
0424   /// @return Eigen map referencing the calibrated measurement vector.
0425   ConstEffectiveCalibratedMap effectiveCalibrated() const {
0426     const double* data = derived().calibratedData();
0427     const auto size = derived().calibratedSize();
0428     return ConstEffectiveCalibratedMap{data, size};
0429   }
0430 
0431   /// Access mutable calibrated measurement values with runtime dimension.
0432   /// @return Eigen map referencing the calibrated measurement vector.
0433   EffectiveCalibratedMap effectiveCalibrated()
0434     requires(!read_only)
0435   {
0436     double* data = derived().calibratedDataMutable();
0437     const auto size = derived().calibratedSize();
0438     return EffectiveCalibratedMap{data, size};
0439   }
0440 
0441   /// Access the calibrated covariance with runtime dimension.
0442   /// @return Eigen map referencing the measurement covariance matrix.
0443   ConstEffectiveCalibratedCovarianceMap effectiveCalibratedCovariance() const {
0444     const double* data = derived().calibratedCovarianceData();
0445     const auto size = derived().calibratedSize();
0446     return ConstEffectiveCalibratedCovarianceMap{data, size, size};
0447   }
0448 
0449   /// Access mutable calibrated covariance with runtime dimension.
0450   /// @return Eigen map referencing the measurement covariance matrix.
0451   EffectiveCalibratedCovarianceMap effectiveCalibratedCovariance()
0452     requires(!read_only)
0453   {
0454     double* data = derived().calibratedCovarianceDataMutable();
0455     const auto size = derived().calibratedSize();
0456     return EffectiveCalibratedCovarianceMap{data, size, size};
0457   }
0458 
0459   /// Access calibrated measurement data with compile-time dimension.
0460   /// @tparam measdim Measurement dimension.
0461   /// @return Eigen map referencing the calibrated measurement vector.
0462   template <std::size_t measdim>
0463   typename TrackStateTraits<measdim, true>::Calibrated calibrated() const {
0464     assert(derived().calibratedSize() == static_cast<TrackIndexType>(measdim));
0465     const double* data = derived().calibratedData();
0466     return typename TrackStateTraits<measdim, true>::Calibrated(data);
0467   }
0468 
0469   /// Access calibrated measurement data with compile-time dimension.
0470   /// @tparam measdim Measurement dimension.
0471   /// @return Mutable Eigen map referencing the calibrated measurement vector.
0472   template <std::size_t measdim>
0473   typename TrackStateTraits<measdim, false>::Calibrated calibrated()
0474     requires(!read_only)
0475   {
0476     assert(derived().calibratedSize() == static_cast<TrackIndexType>(measdim));
0477     double* data = derived().calibratedDataMutable();
0478     return typename TrackStateTraits<measdim, false>::Calibrated(data);
0479   }
0480 
0481   /// Access calibrated covariance data with compile-time dimension.
0482   /// @tparam measdim Measurement dimension.
0483   /// @return Eigen map referencing the covariance matrix.
0484   template <std::size_t measdim>
0485   typename TrackStateTraits<measdim, true>::CalibratedCovariance
0486   calibratedCovariance() const {
0487     assert(derived().calibratedSize() == static_cast<TrackIndexType>(measdim));
0488     const double* data = derived().calibratedCovarianceData();
0489     return typename TrackStateTraits<measdim, true>::CalibratedCovariance(data);
0490   }
0491 
0492   /// Access calibrated covariance data with compile-time dimension.
0493   /// @tparam measdim Measurement dimension.
0494   /// @return Mutable Eigen map referencing the covariance matrix.
0495   template <std::size_t measdim>
0496   typename TrackStateTraits<measdim, false>::CalibratedCovariance
0497   calibratedCovariance()
0498     requires(!read_only)
0499   {
0500     assert(derived().calibratedSize() == static_cast<TrackIndexType>(measdim));
0501     double* data = derived().calibratedCovarianceDataMutable();
0502     return
0503         typename TrackStateTraits<measdim, false>::CalibratedCovariance(data);
0504   }
0505 
0506   /// Allocate and initialize calibrated data from static-size Eigen objects.
0507   /// @tparam val_t Eigen vector type holding calibrated values.
0508   /// @tparam cov_t Eigen matrix type holding the covariance.
0509   /// @param val Vector to copy into the calibrated storage.
0510   /// @param cov Covariance matrix to copy into the calibrated storage.
0511   template <typename val_t, typename cov_t>
0512   void allocateCalibrated(const Eigen::DenseBase<val_t>& val,
0513                           const Eigen::DenseBase<cov_t>& cov)
0514     requires(!read_only && Concepts::eigen_base_is_fixed_size<val_t> &&
0515              Concepts::eigen_bases_have_same_num_rows<val_t, cov_t> &&
0516              Concepts::eigen_base_is_square<cov_t> &&
0517              Eigen::PlainObjectBase<val_t>::RowsAtCompileTime <=
0518                  static_cast<std::underlying_type_t<BoundIndices>>(eBoundSize))
0519   {
0520     constexpr std::size_t measdim =
0521         static_cast<std::size_t>(val_t::RowsAtCompileTime);
0522     derived().allocateCalibrated(measdim);
0523     calibrated<measdim>() = val;
0524     calibratedCovariance<measdim>() = cov;
0525   }
0526 };
0527 
0528 }  // namespace Acts