Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-18 09:33:01

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/Definitions/TrackParametrization.hpp"
0012 #include "Acts/EventData/BoundTrackParameters.hpp"
0013 #include "Acts/EventData/MultiTrajectory.hpp"
0014 #include "Acts/EventData/MultiTrajectoryBackendConcept.hpp"
0015 #include "Acts/EventData/ParticleHypothesis.hpp"
0016 #include "Acts/EventData/TrackContainerBackendConcept.hpp"
0017 #include "Acts/EventData/TrackProxyCommon.hpp"
0018 #include "Acts/EventData/TrackProxyConcept.hpp"
0019 #include "Acts/EventData/TrackStatePropMask.hpp"
0020 #include "Acts/Utilities/HashedString.hpp"
0021 #include "Acts/Utilities/Holders.hpp"
0022 #include "Acts/Utilities/TypeTraits.hpp"
0023 
0024 #include <iterator>
0025 #include <optional>
0026 
0027 namespace Acts {
0028 
0029 template <TrackContainerBackend track_container_t,
0030           CommonMultiTrajectoryBackend traj_t,
0031           template <typename> class holder_t>
0032   requires HolderFor<holder_t, track_container_t> && HolderFor<holder_t, traj_t>
0033 class TrackContainer;
0034 
0035 template <bool read_only>
0036 class AnyTrack;
0037 
0038 /// Proxy class representing a single track.
0039 /// This class provides a **view** into an associated @ref TrackContainer, and
0040 /// has **reference semantics**. You can think of it as a pointer to a vector
0041 /// of tracks, which exposes an object-oriented interface for accessing the
0042 /// track properties.
0043 ///
0044 /// @tparam track_container_t the container backend
0045 /// @tparam trajectory_t the track state container backend
0046 /// @tparam holder_t ownership management class for the backend
0047 /// @tparam read_only true if this track container is not mutable
0048 template <typename track_container_t, typename trajectory_t,
0049           template <typename> class holder_t, bool read_only = true>
0050 class TrackProxy
0051     : public TrackProxyCommon<
0052           TrackProxy<track_container_t, trajectory_t, holder_t, read_only>,
0053           typename track_container_t::IndexType, read_only> {
0054   using Base = TrackProxyCommon<
0055       TrackProxy<track_container_t, trajectory_t, holder_t, read_only>,
0056       typename track_container_t::IndexType, read_only>;
0057 
0058  public:
0059   /// Indicates whether this track proxy is read-only or if it can be
0060   /// modified
0061   static constexpr bool ReadOnly = read_only;
0062 
0063   /// The track container backend given as a template parameter
0064   using Container = track_container_t;
0065 
0066   /// The track state container backend given as a template parameter
0067   using Trajectory = trajectory_t;
0068 
0069   /// The index type of the track container
0070   using IndexType = TrackIndexType;
0071 
0072   /// Sentinel value that indicates an invalid index
0073   static constexpr IndexType kInvalid = kTrackIndexInvalid;
0074 
0075   /// Alias for the mutable version of this track proxy, with the same backends
0076   using MutableTrackProxy =
0077       TrackProxy<track_container_t, trajectory_t, holder_t, false>;
0078 
0079   /// Alias for the const version of this track proxy, with the same backends
0080   using ConstTrackProxy =
0081       TrackProxy<track_container_t, trajectory_t, holder_t, true>;
0082 
0083   /// Alias for an associated const track proxy, with the same backends
0084   using ConstProxyType = ConstTrackProxy;
0085 
0086   /// Alias for an associated mutable track state proxy, with the same backends
0087   using TrackStateProxy = typename Trajectory::TrackStateProxy;
0088 
0089   /// Alias for an associated const track state proxy, with the same backends
0090   using ConstTrackStateProxy = typename Trajectory::ConstTrackStateProxy;
0091 
0092   /// Map-type for a bound parameter vector. This has reference semantics, i.e.
0093   /// points at a matrix by an internal pointer.
0094   using Parameters =
0095       typename detail_tsp::FixedSizeTypes<eBoundSize, false>::CoefficientsMap;
0096 
0097   /// Same as @ref Parameters, but with const semantics
0098   using ConstParameters =
0099       typename detail_tsp::FixedSizeTypes<eBoundSize, true>::CoefficientsMap;
0100 
0101   /// Map-type for a bound covariance. This has reference semantics, i.e.
0102   /// points at a matrix by an internal pointer.
0103   using Covariance =
0104       typename detail_tsp::FixedSizeTypes<eBoundSize, false>::CovarianceMap;
0105 
0106   /// Same as @ref Covariance, but with const semantics
0107   using ConstCovariance =
0108       typename detail_tsp::FixedSizeTypes<eBoundSize, true>::CovarianceMap;
0109 
0110 #ifndef DOXYGEN
0111   friend TrackContainer<Container, Trajectory, holder_t>;
0112   friend MutableTrackProxy;
0113   friend ConstTrackProxy;
0114   // Track proxies are friends, not food!
0115   template <typename A, typename B, template <typename> class H, bool R>
0116   friend class TrackProxy;
0117   template <bool R>
0118   friend class AnyTrackProxy;
0119 #endif
0120 
0121   /// @anchor track_proxy_construct
0122   /// @name Constructors and assignment operator
0123   ///
0124   /// Public constructors and assignment operators for @c TrackProxy only
0125   /// allow construction from another @c TrackProxy. You should generally
0126   /// not have to construct @c TrackProxy manually.
0127   ///
0128   /// @{
0129 
0130   /// Copy constructor: const to const or mutable to mutable
0131   /// @param other the other track proxy
0132   TrackProxy(const TrackProxy& other) = default;
0133 
0134   /// Copy assignment operator: const to const or mutable to mutable
0135   /// @param other the other track proxy
0136   /// @return reference to this track proxy
0137   TrackProxy& operator=(const TrackProxy& other) = default;
0138 
0139   /// Constructor from mutable track proxy
0140   /// @note Only available if the track proxy is read-only
0141   /// @param other the other track proxy
0142   explicit TrackProxy(const MutableTrackProxy& other)
0143     requires ReadOnly
0144       : m_container{other.m_container}, m_index{other.m_index} {}
0145 
0146   /// Copy assignment operator from mutable track proxy
0147   /// @note Only available if the track proxy is read-only
0148   /// @param other the other track proxy
0149   /// @return reference to this track proxy
0150   TrackProxy& operator=(const MutableTrackProxy& other)
0151     requires ReadOnly
0152   {
0153     m_container = other.m_container;
0154     m_index = other.m_index;
0155     return *this;
0156   }
0157 
0158   /// @}
0159 
0160   /// Equality operator with another track proxy
0161   /// Checks the container identity and the track index
0162   /// @param other Other track proxy to compare with
0163   /// @return True if the track proxies refer to the same track
0164   bool operator==(const TrackProxy& other) const {
0165     return &(*m_container) == &(*other.m_container) && m_index == other.m_index;
0166   }
0167 
0168   /// @anchor track_proxy_props
0169   /// @name TrackProxy properties
0170   /// Methods that give access to the properties of a track represented by
0171   /// @c TrackProxy.
0172   ///
0173   /// Many of these methods come in a @c const and a non-@c const version. The
0174   /// non-@c const version is only available if you have an instance of
0175   /// @c TrackProxy that does not have the @c read_only template parameter set to
0176   /// @c true, even if you hold it as an lvalue.
0177   ///
0178   /// @{
0179 
0180   /// Get the reference surface of the track (e.g. the perigee)
0181   /// @return the reference surface
0182   const Surface& referenceSurface() const {
0183     return *m_container->container().referenceSurface_impl(m_index);
0184   }
0185 
0186   // NOLINTBEGIN(performance-unnecessary-value-param)
0187   // looks like a false-positive. clang-tidy believes `srf` is not movable.
0188   /// Set a new reference surface for this track
0189   /// @param srf The surface to set
0190   void setReferenceSurface(std::shared_ptr<const Surface> srf)
0191     requires(!ReadOnly)
0192   {
0193     m_container->container().setReferenceSurface_impl(m_index, std::move(srf));
0194   }
0195   // NOLINTEND(performance-unnecessary-value-param)
0196 
0197   /// Returns whether the track has a reference surface or not
0198   /// @return whether a surface exists or not
0199   bool hasReferenceSurface() const {
0200     // @TODO: This could be more efficient
0201     return m_container->container().referenceSurface_impl(m_index) != nullptr;
0202   }
0203 
0204   /// Get the parameters of the track at the reference surface (e.g. perigee).
0205   /// Const version
0206   /// @return Proxy vector for the parameters
0207   ConstParameters parameters() const {
0208     return m_container->parameters(m_index);
0209   }
0210 
0211   /// Get the covariance of the track at the reference surface (e.g. perigee).
0212   /// Const version
0213   /// @return Proxy matrix for the covariance
0214   ConstCovariance covariance() const {
0215     return m_container->covariance(m_index);
0216   }
0217 
0218   /// Get the parameters of the track at the reference surface (e.g. perigee).
0219   /// Mutable version
0220   /// @note Only available if the track proxy is not read-only
0221   /// @return Proxy vector for the parameters
0222   Parameters parameters()
0223     requires(!ReadOnly)
0224   {
0225     return m_container->parameters(m_index);
0226   }
0227 
0228   /// Get the covariance of the track at the reference surface (e.g. perigee).
0229   /// Mutable version
0230   /// @note Only available if the track proxy is not read-only
0231   /// @return Proxy matrix for the covariance
0232   Covariance covariance()
0233     requires(!ReadOnly)
0234   {
0235     return m_container->covariance(m_index);
0236   }
0237 
0238   /// Get the particle hypothesis
0239   /// @return the particle hypothesis
0240   ParticleHypothesis particleHypothesis() const {
0241     return m_container->container().particleHypothesis_impl(m_index);
0242   }
0243 
0244   /// Set a new particle hypothesis for this track
0245   /// @note Only available if the track proxy is not read-only
0246   /// @param particleHypothesis The particle hypothesis to set
0247   void setParticleHypothesis(const ParticleHypothesis& particleHypothesis)
0248     requires(!ReadOnly)
0249   {
0250     m_container->container().setParticleHypothesis_impl(m_index,
0251                                                         particleHypothesis);
0252   }
0253 
0254   using Base::absoluteMomentum;
0255   using Base::charge;
0256   using Base::chi2;
0257   using Base::direction;
0258   using Base::fourMomentum;
0259   using Base::isForwardLinked;
0260   using Base::loc0;
0261   using Base::loc1;
0262   using Base::momentum;
0263   using Base::nDoF;
0264   using Base::nHoles;
0265   using Base::nMeasurements;
0266   using Base::nOutliers;
0267   using Base::nSharedHits;
0268   using Base::nSplitHits;
0269   using Base::phi;
0270   using Base::qOverP;
0271   using Base::stemIndex;
0272   using Base::theta;
0273   using Base::time;
0274   using Base::tipIndex;
0275   using Base::transverseMomentum;
0276 
0277   /// Return the number of track states associated to this track
0278   /// @note This is calculated by iterating over the track states which is
0279   ///       somewhat expensive. Consider caching this value if you need It
0280   ///       more than once.
0281   /// @return The number of track states
0282   unsigned int nTrackStates() const {
0283     // @TODO: This should probably be cached, distance is expensive
0284     //        without random access
0285     if (tipIndex() == kInvalid) {
0286       // no tip index -> no track states
0287       return 0;
0288     }
0289     auto tsRange = trackStatesReversed();
0290     return std::distance(tsRange.begin(), tsRange.end());
0291   }
0292 
0293   /// Return the index of this track in the track container
0294   /// @note This is separate from the tip index
0295   /// @return the track index
0296   IndexType index() const { return m_index; }
0297 
0298   /// @}
0299 
0300   /// @anchor track_proxy_track_states
0301   /// @name TrackProxy track state access
0302   /// Methods that give access to the track states of a track represented by @c TrackProxy.
0303   /// @{
0304 
0305   /// Return a const track state proxy to the outermost track state
0306   /// @return The outermost track state proxy
0307   ConstTrackStateProxy outermostTrackState() const {
0308     return m_container->trackStateContainer().getTrackState(tipIndex());
0309   }
0310 
0311   /// Return a mutable track state proxy to the outermost track state
0312   /// @return The outermost track state proxy
0313   TrackStateProxy outermostTrackState()
0314     requires(!ReadOnly)
0315   {
0316     return m_container->trackStateContainer().getTrackState(tipIndex());
0317   }
0318 
0319   /// Return a const track state proxy to the innermost track state
0320   /// @note This is only available, if the track is forward linked
0321   /// @return The innermost track state proxy
0322   auto innermostTrackState() const {
0323     using proxy_t = decltype(m_container->trackStateContainer().getTrackState(
0324         std::declval<IndexType>()));
0325 
0326     IndexType stem = component<IndexType, detail_tp::kStemIndexKey>();
0327     if (stem == kInvalid) {
0328       return std::optional<proxy_t>{};
0329     } else {
0330       return std::optional<proxy_t>{
0331           m_container->trackStateContainer().getTrackState(stem)};
0332     }
0333   }
0334 
0335   /// Return a mutable track state proxy to the innermost track state
0336   /// @note This is only available, if the track is forward linked
0337   /// @note Only available if the track proxy is not read-only
0338   /// @return The innermost track state proxy
0339   auto innermostTrackState()
0340     requires(!ReadOnly)
0341   {
0342     using proxy_t = decltype(m_container->trackStateContainer().getTrackState(
0343         std::declval<IndexType>()));
0344 
0345     IndexType stem = component<IndexType>(detail_tp::kStemIndexKey);
0346     if (stem == kInvalid) {
0347       return std::optional<proxy_t>{};
0348     } else {
0349       return std::optional<proxy_t>{
0350           m_container->trackStateContainer().getTrackState(stem)};
0351     }
0352   }
0353 
0354   /// Get a range over the track states of this track. Return value is
0355   /// compatible with range based for loop. Const version
0356   /// @note This range is from the outside inwards!
0357   /// @return Track state range to iterate over
0358   auto trackStatesReversed() const {
0359     return m_container->reverseTrackStateRange(m_index);
0360   }
0361 
0362   /// Get a range over the track states of this track. Return value is
0363   /// compatible with range based for loop. Mutable version
0364   /// @note Only available if the track proxy is not read-only
0365   /// @note This range is from the outside inwards!
0366   /// @return Track state range to iterate over
0367   auto trackStatesReversed()
0368     requires(!ReadOnly)
0369   {
0370     return m_container->reverseTrackStateRange(m_index);
0371   }
0372 
0373   /// Get a range over the track states of this track. Return value is
0374   /// compatible with range based for loop. This overload returns a const-only
0375   /// track state range, which means you cannot modify the track states obtained
0376   /// in the iteration.
0377   /// @note This range is from the inside out!
0378   /// @warning This access direction is only possible if the track states are
0379   ///          **forward-linked**.
0380   /// @return Track state range to iterate over
0381   auto trackStates() const {
0382     return m_container->forwardTrackStateRange(m_index);
0383   }
0384 
0385   /// Get a range over the track states of this track.
0386   /// Return value is compatible with range based for loop.
0387   /// This overload returns a mutable track state range, which means you
0388   /// can modify the track states obtained in the iteration.
0389   /// @note Only available if the track proxy is not read-only
0390   /// @note This range is from the inside out!
0391   /// @warning This access direction is only possible if the track states are
0392   ///          **forward-linked**.
0393   /// @return Track state range to iterate over
0394   auto trackStates()
0395     requires(!ReadOnly)
0396   {
0397     return m_container->forwardTrackStateRange(m_index);
0398   }
0399 
0400   /// @}
0401 
0402   /// @anchor track_proxy_track_state_manipulation
0403   /// @name TrackProxy track state manipulation
0404   /// Methods that manipulate the track states of a track represented by @c
0405   /// TrackProxy.
0406   ///
0407   /// **Copy Methods Overview:**
0408   ///
0409   /// Three main copy methods are available with different behaviors:
0410   /// - @c copyFrom(): Deep copy including all track states (creates new track
0411   ///                 states)
0412   /// - @c copyFromWithoutStates(): Copy only track properties, invalidate
0413   ///                               track state indices
0414   /// - @c copyFromShallow(): Shallow copy sharing the same track states (copy
0415   ///                         indices only)
0416   ///
0417   /// Choose based on your needs:
0418   /// - Use @c copyFrom() for independent track copies with separate track
0419   ///   states
0420   /// - Use @c copyFromWithoutStates() to update track metadata without
0421   ///   affecting trajectories
0422   /// - Use @c copyFromShallow() for lightweight copies when track states can
0423   ///   be shared
0424   /// @{
0425 
0426   /// Forward connect a track.
0427   /// This means setting indices from the inside out on all track states.
0428   /// @note Only available if the track proxy is not read-only
0429   void linkForward()
0430     requires(!ReadOnly)
0431   {
0432     IndexType last = kInvalid;
0433     for (auto ts : trackStatesReversed()) {
0434       ts.template component<IndexType>(detail_tp::kNextKey) = last;
0435       last = ts.index();
0436     }
0437     stemIndex() = last;
0438   }
0439 
0440   /// Append a track state to this track.
0441   /// This will modify the tip index to point at the newly created track state,
0442   /// which will be directly after the previous track state at tip index.
0443   /// @note Only available if the track proxy is not read-only
0444   /// @param mask The allocation prop mask for the new track state
0445   /// @return The newly added track state
0446   auto appendTrackState(TrackStatePropMask mask = TrackStatePropMask::All)
0447     requires(!ReadOnly)
0448   {
0449     auto& tsc = m_container->trackStateContainer();
0450     auto ts = tsc.makeTrackState(mask, tipIndex());
0451     tipIndex() = ts.index();
0452     return ts;
0453   }
0454 
0455   /// Create a complete deep copy of another track, including all track states.
0456   /// This creates new track states in the destination trajectory and copies
0457   /// all data from the source track states. The track state sequence order
0458   /// is preserved.
0459   ///
0460   /// **Implementation details:**
0461   /// - Track states are initially copied in reversed order for efficiency
0462   /// - The track state links are then updated using reverseTrackStates()
0463   /// - As a consequence, the resulting track is forward-linked
0464   ///
0465   /// **What gets copied:**
0466   /// - All track-level properties (parameters, covariance, particle hypothesis,
0467   ///   etc.)
0468   /// - Reference surface (shared pointer is copied)
0469   /// - Track summary data (nMeasurements, nHoles, chi2, etc.)
0470   /// - All dynamic track columns
0471   /// - Complete sequence of track states with all their data
0472   /// - All dynamic track state columns
0473   ///
0474   /// **Result:**
0475   /// - The destination track will have newly created track states
0476   /// - tipIndex() and stemIndex() will point to the new track
0477   /// states
0478   /// - Track state indices will be different from the source
0479   /// - All track state data will be identical to the source
0480   /// - The track will be forward-linked (stemIndex() will be valid)
0481   ///
0482   /// @note Only available if the track proxy is not read-only
0483   /// @note Both track containers must have compatible dynamic columns
0484   /// @tparam track_proxy_t the other track proxy's type
0485   /// @param other The source track proxy to copy from
0486   template <TrackProxyConcept track_proxy_t>
0487   void copyFrom(const track_proxy_t& other)
0488     requires(!ReadOnly)
0489   {
0490     copyFromWithoutStates(other);
0491 
0492     // append track states (cheap), but they're in the wrong order
0493     for (const auto& srcTrackState : other.trackStatesReversed()) {
0494       auto destTrackState = appendTrackState(srcTrackState.getMask());
0495       destTrackState.copyFrom(srcTrackState, Acts::TrackStatePropMask::All,
0496                               true);
0497     }
0498 
0499     // reverse using standard linked list reversal algorithm
0500     reverseTrackStates();
0501   }
0502 
0503   /// Copy track-level properties from another track, but not the track states.
0504   /// This copies all track metadata and properties but leaves the track state
0505   /// sequence unchanged. Useful when you want to copy track properties to an
0506   /// existing track that may already have track states.
0507   ///
0508   /// **What gets copied:**
0509   /// - Track parameters at reference surface
0510   /// - Covariance matrix at reference surface
0511   /// - Particle hypothesis
0512   /// - Reference surface (shared pointer is copied)
0513   /// - Track summary data (nMeasurements, nHoles, nOutliers, nSharedHits,
0514   /// nSplitHits, chi2, nDoF)
0515   /// - All dynamic track columns
0516   ///
0517   /// **What does NOT get copied:**
0518   /// - Track states (existing track states remain unchanged in the container)
0519   ///
0520   /// **Result:**
0521   /// - All track-level properties are updated to match the source
0522   /// - tipIndex() and stemIndex() are set to kInvalid (track states
0523   /// become inaccessible)
0524   /// - Existing track states remain in the container but are no longer linked
0525   /// to this track
0526   /// - nTrackStates() will return 0 due to invalid indices
0527   ///
0528   /// @note Only available if the track proxy is not read-only
0529   /// @note Both track containers must have compatible dynamic columns
0530   /// @tparam track_proxy_t the other track proxy's type
0531   /// @param other The source track proxy to copy properties from
0532   template <TrackProxyConcept track_proxy_t>
0533   void copyFromWithoutStates(const track_proxy_t& other)
0534     requires(!ReadOnly)
0535   {
0536     setParticleHypothesis(other.particleHypothesis());
0537 
0538     if (other.hasReferenceSurface()) {
0539       setReferenceSurface(other.referenceSurface().getSharedPtr());
0540       parameters() = other.parameters();
0541       covariance() = other.covariance();
0542     } else {
0543       setReferenceSurface(nullptr);
0544     }
0545 
0546     nMeasurements() = other.nMeasurements();
0547     nHoles() = other.nHoles();
0548     nOutliers() = other.nOutliers();
0549     nSharedHits() = other.nSharedHits();
0550     nSplitHits() = other.nSplitHits();
0551     chi2() = other.chi2();
0552     nDoF() = other.nDoF();
0553 
0554     m_container->copyDynamicFrom(m_index, other.m_container->container(),
0555                                  other.m_index);
0556 
0557     tipIndex() = kInvalid;
0558     stemIndex() = kInvalid;
0559   }
0560 
0561   /// Create a shallow copy from another track, sharing the same track states.
0562   /// This copies all track-level properties and makes the destination track
0563   /// point to the same track state sequence as the source. The track states
0564   /// themselves are not duplicated - both tracks will reference the same
0565   /// track state objects in memory.
0566   ///
0567   /// **What gets copied:**
0568   /// - All track-level properties (parameters, covariance, particle hypothesis,
0569   /// etc.)
0570   /// - Reference surface (shared pointer is copied)
0571   /// - Track summary data (nMeasurements, nHoles, chi2, etc.)
0572   /// - All dynamic track columns
0573   /// - tipIndex() and stemIndex() (track state linking information)
0574   ///
0575   /// **What gets shared (not duplicated):**
0576   /// - Track states (both tracks reference the same track state objects)
0577   ///
0578   /// **Result:**
0579   /// - The destination track will have the same nTrackStates() as the source
0580   /// - Both tracks will iterate over the same track state sequence
0581   /// - Modifications to track states will be visible in both tracks
0582   /// - Track state indices will be identical between tracks
0583   /// - The destination track will have a different track index than the source
0584   ///
0585   /// @warning Modifying track states through either track will affect both tracks
0586   ///          since they share the same track state objects
0587   /// @warning It is the user's responsibility to ensure that the tip and stem
0588   ///          indices from the source track are valid in the destination
0589   ///          track's track state container. No validation is performed -
0590   ///          invalid indices will lead to undefined behavior when accessing
0591   ///          track states
0592   /// @note Only available if the track proxy is not read-only
0593   /// @note Both track containers must have compatible dynamic columns
0594   /// @tparam track_proxy_t the other track proxy's type
0595   /// @param other The source track proxy to create a shallow copy from
0596   template <TrackProxyConcept track_proxy_t>
0597   void copyFromShallow(const track_proxy_t& other)
0598     requires(!ReadOnly)
0599   {
0600     copyFromWithoutStates(other);
0601     tipIndex() = other.tipIndex();
0602     stemIndex() = other.stemIndex();
0603   }
0604 
0605   /// Reverse the ordering of track states for this track
0606   /// Afterwards, the previous endpoint of the track state sequence will be
0607   /// the "innermost" track state
0608   /// @note Only available if the track proxy is not read-only
0609   /// @note This is dangerous with branching track state sequences, as it will break them
0610   /// @note This also automatically forward-links the track!
0611   /// @param invertJacobians Whether to invert the Jacobians of the track states
0612   /// @return With @p invertJacobians, the inverse of the old first jacobian,
0613   ///         which has no slot in the reversed sequence. Empty otherwise, for
0614   ///         a track without states, or if that jacobian is zero.
0615   std::optional<BoundMatrix> reverseTrackStates(bool invertJacobians = false)
0616     requires(!ReadOnly)
0617   {
0618     IndexType current = tipIndex();
0619     IndexType next = kInvalid;
0620     IndexType prev = kInvalid;
0621 
0622     stemIndex() = tipIndex();
0623 
0624     // @TODO: Maybe refactor to not need this variable if invertJacobians == false
0625     BoundMatrix nextJacobian;
0626 
0627     while (current != kInvalid) {
0628       auto ts = m_container->trackStateContainer().getTrackState(current);
0629       prev = ts.previous();
0630       ts.template component<IndexType>(detail_tp::kNextKey) = prev;
0631       ts.previous() = next;
0632       if (invertJacobians) {
0633         if (next != kInvalid) {
0634           BoundMatrix curJacobian = ts.jacobian();
0635           ts.jacobian() = nextJacobian.inverse();
0636           nextJacobian = curJacobian;
0637         } else {
0638           nextJacobian = ts.jacobian();
0639           ts.jacobian().setZero();
0640         }
0641       }
0642       next = current;
0643       tipIndex() = current;
0644       current = prev;
0645     }
0646 
0647     // `nextJacobian` holds the jacobian of the old first state
0648     if (!invertJacobians || next == kInvalid || nextJacobian.isZero()) {
0649       return std::nullopt;
0650     }
0651     return nextJacobian.inverse();
0652   }
0653 
0654   /// @}
0655 
0656   /// @anchor track_proxy_generic_component
0657   /// @name TrackProxy generic component access
0658   /// Methods that give access to generic components of a track represented by
0659   /// @c TrackProxy.  Internally, a compile-time hash of the component name is
0660   /// used to identify which component is being requested. Most of the named
0661   /// methods in @ref track_proxy_props "TrackProxy properties" use these
0662   /// methods to retrieve the actual data.
0663   ///
0664   /// A number of overloads exist, where you can either supply the
0665   /// @ref HashedString @c key as a template parameter or a runtime argument.  The
0666   /// former has the advantage of being guaranteed to be evaluated at
0667   /// compile-time.
0668   ///
0669   /// @{
0670 
0671   /// Retrieve a mutable reference to a component
0672   /// @tparam T The type of the component to access
0673   /// @tparam key String key for the component to access
0674   /// @return Mutable reference to the component given by @p key
0675   template <typename T, HashedString key>
0676   constexpr T& component()
0677     requires(!ReadOnly)
0678   {
0679     return m_container->template component<T, key>(m_index);
0680   }
0681 
0682   /// Retrieve a mutable reference to a component
0683   /// @tparam T The type of the component to access
0684   /// @param key String key for the component to access
0685   /// @return Mutable reference to the component given by @p key
0686   template <typename T>
0687   constexpr T& component(HashedString key)
0688     requires(!ReadOnly)
0689   {
0690     return m_container->template component<T>(key, m_index);
0691   }
0692 
0693   /// Retrieve a mutable reference to a component
0694   /// @tparam T The type of the component to access
0695   /// @param key String key for the component to access
0696   /// @note This might hash the @p key at runtime instead of compile-time
0697   /// @return Mutable reference to the component given by @p key
0698   template <typename T>
0699   constexpr T& component(std::string_view key)
0700     requires(!ReadOnly)
0701   {
0702     return m_container->template component<T>(hashStringDynamic(key), m_index);
0703   }
0704 
0705   /// Retrieve a const reference to a component
0706   /// @tparam T The type of the component to access
0707   /// @tparam key String key for the component to access
0708   /// @return Const reference to the component given by @p key
0709   template <typename T, HashedString key>
0710   constexpr const T& component() const {
0711     return m_container->template component<T, key>(m_index);
0712   }
0713 
0714   /// Check whether a dynamic column exists
0715   /// @param key String key for the component to check
0716   /// @return whether the column exists
0717   bool hasColumn(HashedString key) const { return m_container->hasColumn(key); }
0718 
0719   /// Retrieve a const reference to a component
0720   /// @tparam T The type of the component to access
0721   /// @param key String key for the component to access
0722   /// @return Const reference to the component given by @p key
0723   template <typename T>
0724   constexpr const T& component(HashedString key) const {
0725     return m_container->template component<T>(key, m_index);
0726   }
0727 
0728   /// Retrieve a const reference to a component
0729   /// @tparam T The type of the component to access
0730   /// @param key String key for the component to access
0731   /// @note This might hash the @p key at runtime instead of compile-time
0732   /// @return Const reference to the component given by @p key
0733   template <typename T>
0734   constexpr const T& component(std::string_view key) const {
0735     return m_container->template component<T>(hashStringDynamic(key), m_index);
0736   }
0737 
0738   /// @}
0739 
0740   /// Return the track parameters at the reference surface
0741   /// @note The parameters are created on the fly
0742   /// @return the track parameters
0743   BoundTrackParameters createParametersAtReference() const {
0744     return BoundTrackParameters(referenceSurface().getSharedPtr(), parameters(),
0745                                 covariance(), particleHypothesis());
0746   }
0747 
0748   /// Convert a track state into track parameters
0749   /// @note The parameters are created on the fly
0750   /// @param trackState Track state to convert to parameters
0751   /// @return the track parameters
0752   BoundTrackParameters createParametersFromState(
0753       const ConstTrackStateProxy& trackState) const {
0754     return BoundTrackParameters(trackState.referenceSurface().getSharedPtr(),
0755                                 trackState.parameters(),
0756                                 trackState.covariance(), particleHypothesis());
0757   }
0758 
0759   /// Return a reference to the track container backend, mutable version.
0760   /// @note Only available if the track proxy is not read-only
0761   /// @return reference to the track container backend
0762   auto& container()
0763     requires(!ReadOnly)
0764   {
0765     return *m_container;
0766   }
0767 
0768   /// Return a reference to the track container backend, const version.
0769   /// @return reference to the track container backend
0770   const auto& container() const { return *m_container; }
0771 
0772  private:
0773   TrackProxy(
0774       const_if_t<ReadOnly, TrackContainer<Container, Trajectory, holder_t>>&
0775           container,
0776       IndexType itrack)
0777       : m_container{&container}, m_index{itrack} {}
0778 
0779   detail_lt::TransitiveConstPointer<
0780       const_if_t<ReadOnly, TrackContainer<Container, Trajectory, holder_t>>>
0781       m_container;
0782   IndexType m_index;
0783 };
0784 
0785 }  // namespace Acts