Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /acts/Core/include/Acts/EventData/TrackContainer.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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/DynamicColumnInfo.hpp"
0012 #include "Acts/EventData/MultiTrajectory.hpp"
0013 #include "Acts/EventData/MultiTrajectoryBackendConcept.hpp"
0014 #include "Acts/EventData/TrackContainerBackendConcept.hpp"
0015 #include "Acts/EventData/TrackProxy.hpp"
0016 #include "Acts/EventData/Types.hpp"
0017 #include "Acts/EventData/Utils.hpp"
0018 #include "Acts/Utilities/HashedString.hpp"
0019 #include "Acts/Utilities/Holders.hpp"
0020 #include "Acts/Utilities/TypeTraits.hpp"
0021 #include "Acts/Utilities/detail/ContainerIterator.hpp"
0022 
0023 #include <any>
0024 #include <string>
0025 #include <string_view>
0026 
0027 namespace Acts {
0028 
0029 template <typename T>
0030 struct IsReadOnlyTrackContainer;
0031 
0032 /// Track container interface class. This type represents a collections of
0033 /// tracks. It uses a backend to store both the actual tracks and the
0034 /// associated track states.
0035 /// @tparam track_container_t the track container backend
0036 /// @tparam traj_t the track state container backend
0037 /// @tparam holder_t ownership management class for the backend
0038 template <TrackContainerBackend track_container_t,
0039           CommonMultiTrajectoryBackend traj_t,
0040           template <typename> class holder_t = RefHolder>
0041   requires HolderFor<holder_t, track_container_t> && HolderFor<holder_t, traj_t>
0042 class TrackContainer {
0043  public:
0044   /// Indicates if this track container is read-only, or if it can be modified
0045   static constexpr bool ReadOnly =
0046       IsReadOnlyTrackContainer<track_container_t>::value;
0047 
0048   /// Indicates if the track state container is read-only, or if it can be
0049   /// modified
0050   static constexpr bool TrackStateReadOnly =
0051       IsReadOnlyMultiTrajectory<traj_t>::value;
0052 
0053   static_assert(ReadOnly == TrackStateReadOnly,
0054                 "Either both track container and track state container need to "
0055                 "be readonly or both have to be readwrite");
0056 
0057   /// The index type of the track container, taken from the container backend
0058   using IndexType = TrackIndexType;
0059 
0060   /// Sentinel value that indicates an invalid index
0061   static constexpr IndexType kInvalid = kTrackIndexInvalid;
0062 
0063   /// Alias for the mutable version of a track proxy, with the same backends as
0064   /// this container
0065   using TrackProxy =
0066       Acts::TrackProxy<track_container_t, traj_t, holder_t, false>;
0067 
0068   /// Alias for the const version of a track proxy, with the same backends as
0069   /// this container
0070   using ConstTrackProxy =
0071       Acts::TrackProxy<track_container_t, traj_t, holder_t, true>;
0072 
0073   static_assert(ConstTrackProxyConcept<ConstTrackProxy>,
0074                 "ConstTrackProxy must fulfill the TrackProxyConcept");
0075   static_assert(ReadOnly || MutableTrackProxyConcept<TrackProxy>,
0076                 "TrackProxy must fulfill the TrackProxyConcept");
0077 
0078   /// Type alias for track container backend type
0079   using TrackContainerBackend = track_container_t;
0080   /// Type alias for track state container backend type
0081   using TrackStateContainerBackend = traj_t;
0082 
0083   /// Type alias for mutable track state proxy from multi-trajectory
0084   using TrackStateProxy = typename MultiTrajectory<traj_t>::TrackStateProxy;
0085   /// Type alias for const track state proxy from multi-trajectory
0086   using ConstTrackStateProxy =
0087       typename MultiTrajectory<traj_t>::ConstTrackStateProxy;
0088 
0089   /// Type alias for size type of track container
0090   using size_type = IndexType;
0091   /// Type alias for mutable iterator over tracks in container
0092   using iterator =
0093       detail::ContainerIterator<TrackContainer, TrackProxy, IndexType, false>;
0094   /// Type alias for const iterator over tracks in container
0095   using const_iterator =
0096       detail::ContainerIterator<TrackContainer, ConstTrackProxy, IndexType,
0097                                 true>;
0098 
0099 #ifndef DOXYGEN
0100   friend TrackProxy;
0101   friend ConstTrackProxy;
0102 #endif
0103 
0104   /// @anchor track_container_construction
0105   /// @name TrackContainer construction
0106   ///
0107   /// Constructors for the track container by using a set of backends
0108   /// (track + track state). The container can either take ownership of the
0109   /// backends or just hold references to them. This is driven by the @c
0110   /// holder_t template parameter, where you can also supply a custom holder
0111   /// type. Template deduction is used to try to guess the correct holder type.
0112   ///
0113   /// @{
0114 
0115   /// Constructor from a track container backend and a track state container
0116   /// backend
0117   /// @param container the track container backend
0118   /// @param traj the track state container backend
0119   TrackContainer(holder_t<track_container_t> container, holder_t<traj_t> traj)
0120       : m_container{std::move(container)}, m_traj{std::move(traj)} {}
0121 
0122   /// Constructor from references to a track container backend and to a track
0123   /// state container backend
0124   /// @note The track container will not assume ownership over the backends in this case.
0125   ///       You need to ensure suitable lifetime
0126   /// @param container the track container backend
0127   /// @param traj the track state container backend
0128   TrackContainer(auto& container, auto& traj)
0129     requires(std::same_as<holder_t<track_container_t>,
0130                           RefHolder<track_container_t>> &&
0131              std::same_as<holder_t<traj_t>, RefHolder<traj_t>>)
0132       : m_container{&container}, m_traj{&traj} {}
0133 
0134   /// Constructor from const references to a track container backend and to a
0135   /// track state container backend
0136   /// @note The track container will not assume ownership over the backends in this case.
0137   ///       You need to ensure suitable lifetime
0138   /// @param container the track container backend
0139   /// @param traj the track state container backend
0140   TrackContainer(const auto& container, const auto& traj)
0141     requires(std::same_as<holder_t<track_container_t>,
0142                           ConstRefHolder<track_container_t>> &&
0143              std::same_as<holder_t<traj_t>, ConstRefHolder<traj_t>> &&
0144              ReadOnly && TrackStateReadOnly)
0145       : m_container{&container}, m_traj{&traj} {}
0146 
0147   /// @}
0148 
0149   /// @anchor track_container_track_access
0150   /// @name TrackContainer track (proxy) access and manipulation
0151   ///
0152   /// These methods allow accessing tracks, i.e. adding or retrieving a track
0153   /// proxy that points at a specific track in the container.
0154   ///
0155   /// @{
0156 
0157   /// Get a const track proxy for a track index
0158   /// @param itrack the track index in the container
0159   /// @return A const track proxy for the index
0160   ConstTrackProxy getTrack(IndexType itrack) const { return {*this, itrack}; }
0161 
0162   /// Get a mutable track proxy for a track index
0163   /// @note Only available if the track container is not read-only
0164   /// @param itrack the track index in the container
0165   /// @return A mutable track proxy for the index
0166   TrackProxy getTrack(IndexType itrack)
0167     requires(!ReadOnly)
0168   {
0169     return {*this, itrack};
0170   }
0171 
0172   /// Get a const track proxy for a track index
0173   /// @param itrack the track index in the container
0174   /// @return A const track proxy for the index
0175   ConstTrackProxy at(IndexType itrack) const { return getTrack(itrack); }
0176 
0177   /// Get a mutable track proxy for a track index
0178   /// @note Only available if the track container is not read-only
0179   /// @param itrack the track index in the container
0180   /// @return A mutable track proxy for the index
0181   TrackProxy at(IndexType itrack)
0182     requires(!ReadOnly)
0183   {
0184     return {*this, itrack};
0185   }
0186 
0187   /// Add a track to the container. Note this only creates the logical track and
0188   /// allocates memory. You can combine this with @c getTrack to obtain a track proxy
0189   /// @note Only available if the track container is not read-only
0190   /// @return the index to the newly added track
0191   IndexType addTrack()
0192     requires(!ReadOnly)
0193   {
0194     auto track = getTrack(m_container->addTrack_impl());
0195     track.tipIndex() = kInvalid;
0196     return track.index();
0197   }
0198 
0199   /// Add a track to the container and return a track proxy to it
0200   /// This effectively calls @c addTrack and @c getTrack
0201   /// @note Only available if the track container is not read-only
0202   /// @return a track proxy to the newly added track
0203   TrackProxy makeTrack()
0204     requires(!ReadOnly)
0205   {
0206     return getTrack(addTrack());
0207   }
0208 
0209   /// Remove a track at index @p itrack from the container
0210   /// @note Only available if the track container is not read-only
0211   /// @note This invalidates track proxies that point to tracks with larger
0212   ///       indices than @p itrack!
0213   /// @param itrack The index of the track to remove
0214   void removeTrack(IndexType itrack)
0215     requires(!ReadOnly)
0216   {
0217     m_container->removeTrack_impl(itrack);
0218   }
0219 
0220   /// Get a mutable iterator to the first track in the container
0221   /// @note Only available if the track container is not read-only
0222   /// @return a mutable iterator to the first track
0223   iterator begin()
0224     requires(!ReadOnly)
0225   {
0226     return iterator{*this, 0};
0227   }
0228 
0229   /// Get a past-the-end iterator for this container
0230   /// @note Only available if the track container is not read-only
0231   /// @return a past-the-end iterator
0232   iterator end()
0233     requires(!ReadOnly)
0234   {
0235     return iterator{*this, size()};
0236   }
0237 
0238   /// Get an const iterator to the first track in the container
0239   /// @return a const iterator to the first track
0240   const_iterator begin() const { return const_iterator{*this, 0}; }
0241 
0242   /// Get a past-the-end iterator for this container
0243   /// @return a past-the-end iterator
0244   const_iterator end() const { return const_iterator{*this, size()}; }
0245 
0246   /// @}
0247 
0248   /// @anchor track_container_columns
0249   /// @name TrackContainer column management
0250   /// TrackContainer can manage a set of common static columns, and dynamic
0251   /// columns that can be added at runtime. This set of methods allows you to
0252   /// manage the dynamic columns.
0253   /// @{
0254 
0255   /// Add a dynamic column to the track container
0256   /// @note Only available if the track container is not read-only
0257   /// @param key the name of the column to be added
0258   template <typename T>
0259   constexpr void addColumn(std::string_view key)
0260     requires(!ReadOnly)
0261   {
0262     m_container->template addColumn_impl<T>(key);
0263   }
0264 
0265   /// Check if this track container has a specific dynamic column
0266   /// @param key the key to check for
0267   /// @return true if the column exists
0268   constexpr bool hasColumn(const std::string& key) const {
0269     return m_container->hasColumn_impl(hashStringDynamic(key));
0270   }
0271 
0272   /// Check if a this track container has a specific dynamic column
0273   /// @param key the key to check for
0274   /// @return true if the column exists
0275   constexpr bool hasColumn(HashedString key) const {
0276     return m_container->hasColumn_impl(key);
0277   }
0278 
0279   /// Iterate over dynamic track columns in unspecified order.
0280   /// @return An input range of DynamicColumnInfo. Keep the backend alive and
0281   /// its column schema unchanged while using the range or its borrowed names.
0282   detail::DynamicColumnInfoRange auto dynamicColumns() const
0283     requires requires(const track_container_t& backend) {
0284       { backend.dynamicColumns_impl() } -> detail::DynamicColumnInfoRange;
0285     }
0286   {
0287     return container().dynamicColumns_impl();
0288   }
0289 
0290   /// Helper function to make this track container match the dynamic columns of
0291   /// another one. This will only work if the track container supports this
0292   /// source, and depends on the implementation details of the dynamic columns
0293   /// of the container
0294   /// @note Only available if the track container is not read-only
0295   /// @tparam other_track_container_t Type of the other track container
0296   /// @param other The other track container
0297   template <typename other_track_container_t>
0298   void ensureDynamicColumns(const other_track_container_t& other)
0299     requires(!ReadOnly)
0300   {
0301     container().ensureDynamicColumns_impl(other.container());
0302     trackStateContainer().ensureDynamicColumns_impl(
0303         other.trackStateContainer());
0304   }
0305 
0306   /// @}
0307 
0308   /// @anchor track_congtainer_backend_access
0309   /// @name TrackContainer backend access
0310   /// These methods allow accessing the backend of the track container. In most
0311   /// cases, this is not necessary for interacting with the container.
0312   /// @{
0313 
0314   /// Get a mutable reference to the track container backend
0315   /// @note Only available if the track container is not read-only
0316   /// @return a mutable reference to the backend
0317   auto& container()
0318     requires(!ReadOnly)
0319   {
0320     return *m_container;
0321   }
0322 
0323   /// Get a const reference to the track container backend
0324   /// @return a const reference to the backend
0325   const auto& container() const { return *m_container; }
0326 
0327   /// Get a mutable reference to the track state container backend
0328   /// @note Only available if the track container is not read-only
0329   /// @return a mutable reference to the backend
0330   auto& trackStateContainer()
0331     requires(!ReadOnly)
0332   {
0333     return *m_traj;
0334   }
0335 
0336   /// Retrieve the holder of the track state container
0337   /// @return The track state container including it's holder
0338   /// @note Only available if the track container is not read-only
0339   auto& trackStateContainerHolder()
0340     requires(!ReadOnly)
0341   {
0342     return m_traj;
0343   }
0344 
0345   /// Get a const reference to the track state container backend
0346   /// @return a const reference to the backend
0347   const auto& trackStateContainer() const { return *m_traj; }
0348 
0349   /// Retrieve the holder of the track state container
0350   /// @return The track state container including it's holder
0351   const auto& trackStateContainerHolder() const { return m_traj; }
0352 
0353   /// @}
0354 
0355   /// Get the size (number of tracks) of the track container
0356   /// @return the sixe
0357   constexpr IndexType size() const { return m_container->size_impl(); }
0358 
0359   /// Clear the content of the track container
0360   /// @note Only available if the track container is not read-only
0361   void clear()
0362     requires(!ReadOnly)
0363   {
0364     m_container->clear();
0365     m_traj->clear();
0366   }
0367 
0368  protected:
0369   /// @brief Get mutable reference to track component using compile-time key
0370   /// @tparam T Component type to retrieve
0371   /// @tparam key Hashed string key for the component
0372   /// @param itrack Track index to get component for
0373   /// @return Mutable reference to the component of type T
0374   template <typename T, HashedString key>
0375   constexpr T& component(IndexType itrack)
0376     requires(!ReadOnly)
0377   {
0378     return *std::any_cast<T*>(container().component_impl(key, itrack));
0379   }
0380 
0381   /// @brief Get mutable reference to track component using runtime key
0382   /// @tparam T Component type to retrieve
0383   /// @param key Hashed string key for the component
0384   /// @param itrack Track index to get component for
0385   /// @return Mutable reference to the component of type T
0386   template <typename T>
0387   constexpr T& component(HashedString key, IndexType itrack)
0388     requires(!ReadOnly)
0389   {
0390     return *std::any_cast<T*>(container().component_impl(key, itrack));
0391   }
0392 
0393   /// @brief Get const reference to track component using compile-time key
0394   /// @tparam T Component type to retrieve
0395   /// @tparam key Hashed string key for the component
0396   /// @param itrack Track index to get component for
0397   /// @return Const reference to the component of type T
0398   template <typename T, HashedString key>
0399   constexpr const T& component(IndexType itrack) const {
0400     return *std::any_cast<const T*>(container().component_impl(key, itrack));
0401   }
0402 
0403   /// @brief Get const reference to track component using runtime key
0404   /// @tparam T Component type to retrieve
0405   /// @param key Hashed string key for the component
0406   /// @param itrack Track index to get component for
0407   /// @return Const reference to the component of type T
0408   template <typename T>
0409   constexpr const T& component(HashedString key, IndexType itrack) const {
0410     return *std::any_cast<const T*>(container().component_impl(key, itrack));
0411   }
0412 
0413   /// Get mutable parameters for a track
0414   /// @param itrack Track index to get parameters for
0415   /// @return Mutable parameters object for the track
0416   constexpr typename TrackProxy::Parameters parameters(IndexType itrack)
0417     requires(!ReadOnly)
0418   {
0419     return container().parameters(itrack);
0420   }
0421 
0422   /// Get const parameters for a track
0423   /// @param itrack Track index to get parameters for
0424   /// @return Const parameters object for the track
0425   constexpr typename ConstTrackProxy::ConstParameters parameters(
0426       IndexType itrack) const {
0427     return container().parameters(itrack);
0428   }
0429 
0430   /// @brief Get mutable covariance matrix for a track
0431   /// @param itrack Track index to get covariance for
0432   /// @return Mutable covariance matrix for the track
0433   constexpr typename TrackProxy::Covariance covariance(IndexType itrack)
0434     requires(!ReadOnly)
0435   {
0436     return container().covariance(itrack);
0437   }
0438 
0439   /// @brief Get const covariance matrix for a track
0440   /// @param itrack Track index to get covariance for
0441   /// @return Const covariance matrix for the track
0442   constexpr typename ConstTrackProxy::ConstCovariance covariance(
0443       IndexType itrack) const {
0444     return container().covariance(itrack);
0445   }
0446 
0447   /// @brief Get mutable range for iterating track states in reverse order
0448   /// @param itrack Track index to get state range for
0449   /// @return Range object for reverse iteration over track states from tip to stem
0450   auto reverseTrackStateRange(IndexType itrack)
0451     requires(!ReadOnly)
0452   {
0453     auto tip = component<IndexType, hashString("tipIndex")>(itrack);
0454     return m_traj->reverseTrackStateRange(tip);
0455   }
0456 
0457   /// @brief Get const range for iterating track states in reverse order
0458   /// @param itrack Track index to get state range for
0459   /// @return Const range object for reverse iteration over track states from tip to stem
0460   auto reverseTrackStateRange(IndexType itrack) const {
0461     auto tip = component<IndexType, hashString("tipIndex")>(itrack);
0462     return m_traj->reverseTrackStateRange(tip);
0463   }
0464 
0465   /// @brief Get mutable range for iterating track states in forward order
0466   /// @param itrack Track index to get state range for
0467   /// @return Range object for forward iteration over track states from stem to tip
0468   /// @throws std::invalid_argument if track has no stem index
0469   auto forwardTrackStateRange(IndexType itrack)
0470     requires(!ReadOnly)
0471   {
0472     auto stem = component<IndexType, hashString("stemIndex")>(itrack);
0473     if (stem == kInvalid) {
0474       throw std::invalid_argument{"Track has no stem index"};
0475     }
0476     return m_traj->forwardTrackStateRange(stem);
0477   }
0478 
0479   /// @brief Get const range for iterating track states in forward order
0480   /// @param itrack Track index to get state range for
0481   /// @return Const range object for forward iteration over track states from stem to tip
0482   /// @throws std::invalid_argument if track has no stem index
0483   auto forwardTrackStateRange(IndexType itrack) const {
0484     auto stem = component<IndexType, hashString("stemIndex")>(itrack);
0485     if (stem == kInvalid) {
0486       throw std::invalid_argument{"Track has no stem index"};
0487     }
0488     return m_traj->forwardTrackStateRange(stem);
0489   }
0490 
0491  private:
0492   template <typename T>
0493   void copyDynamicFrom(IndexType dstIdx, const T& src, IndexType srcIdx)
0494     requires(!ReadOnly)
0495   {
0496     const auto& dynamicKeys = src.dynamicKeys_impl();
0497     for (const auto key : dynamicKeys) {
0498       std::any srcPtr = src.component_impl(key, srcIdx);
0499       container().copyDynamicFrom_impl(dstIdx, key, srcPtr);
0500     }
0501   }
0502 
0503   const_if_t<ReadOnly, holder_t<track_container_t>> m_container;
0504   const_if_t<ReadOnly, holder_t<traj_t>> m_traj;
0505 };
0506 
0507 /// Deduction guide for TrackContainer with lvalue references
0508 /// @param container Track container reference
0509 /// @param traj Trajectory reference
0510 template <TrackContainerBackend track_container_t, typename traj_t>
0511 TrackContainer(track_container_t& container, traj_t& traj)
0512     -> TrackContainer<track_container_t, traj_t, RefHolder>;
0513 
0514 /// Deduction guide for TrackContainer with const references
0515 /// @param container Const track container reference
0516 /// @param traj Const trajectory reference
0517 template <TrackContainerBackend track_container_t, typename traj_t>
0518 TrackContainer(const track_container_t& container, const traj_t& traj)
0519     -> TrackContainer<track_container_t, traj_t, ConstRefHolder>;
0520 
0521 /// Deduction guide for TrackContainer with rvalue references
0522 /// @param container Track container rvalue reference
0523 /// @param traj Trajectory rvalue reference
0524 template <TrackContainerBackend track_container_t, typename traj_t>
0525 TrackContainer(track_container_t&& container, traj_t&& traj)
0526     -> TrackContainer<track_container_t, traj_t, ValueHolder>;
0527 
0528 }  // namespace Acts