Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-11 09:31:37

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/MultiTrajectory.hpp"
0013 #include "Acts/EventData/ParticleHypothesis.hpp"
0014 #include "Acts/EventData/TrackContainer.hpp"
0015 #include "Acts/EventData/TrackContainerBackendConcept.hpp"
0016 #include "Acts/EventData/detail/DynamicColumn.hpp"
0017 #include "Acts/EventData/detail/DynamicKeyIterator.hpp"
0018 #include "Acts/Utilities/HashedString.hpp"
0019 
0020 #include <any>
0021 #include <cassert>
0022 #include <cstddef>
0023 #include <memory>
0024 #include <stdexcept>
0025 #include <string>
0026 #include <type_traits>
0027 #include <unordered_map>
0028 #include <utility>
0029 #include <vector>
0030 
0031 namespace Acts {
0032 class Surface;
0033 template <typename T>
0034 struct IsReadOnlyTrackContainer;
0035 
0036 namespace detail_vtc {
0037 
0038 class VectorTrackContainerBase {
0039  public:
0040   using IndexType = TrackIndexType;
0041   static constexpr auto kInvalid = kTrackIndexInvalid;
0042 
0043   using Parameters =
0044       typename detail_tsp::FixedSizeTypes<eBoundSize, false>::CoefficientsMap;
0045   using Covariance =
0046       typename detail_tsp::FixedSizeTypes<eBoundSize, false>::CovarianceMap;
0047 
0048   using ConstParameters =
0049       typename detail_tsp::FixedSizeTypes<eBoundSize, true>::CoefficientsMap;
0050   using ConstCovariance =
0051       typename detail_tsp::FixedSizeTypes<eBoundSize, true>::CovarianceMap;
0052 
0053  protected:
0054   VectorTrackContainerBase() = default;
0055 
0056   VectorTrackContainerBase(const VectorTrackContainerBase& other);
0057 
0058   VectorTrackContainerBase(VectorTrackContainerBase&& other) = default;
0059 
0060   // BEGIN INTERFACE HELPER
0061 
0062   /// @cond
0063   template <bool EnsureConst, typename T>
0064   static std::any component_impl(T& instance, HashedString key,
0065                                  IndexType itrack) {
0066     if constexpr (EnsureConst) {
0067       static_assert(std::is_const_v<std::remove_reference_t<T>>,
0068                     "Is not const");
0069     }
0070 
0071     using namespace Acts::HashedStringLiteral;
0072     switch (key) {
0073       case "tipIndex"_hash:
0074         return &instance.m_tipIndex[itrack];
0075       case "stemIndex"_hash:
0076         return &instance.m_stemIndex[itrack];
0077       case "params"_hash:
0078         return &instance.m_params[itrack];
0079       case "cov"_hash:
0080         return &instance.m_cov[itrack];
0081       case "nMeasurements"_hash:
0082         return &instance.m_nMeasurements[itrack];
0083       case "nHoles"_hash:
0084         return &instance.m_nHoles[itrack];
0085       case "chi2"_hash:
0086         return &instance.m_chi2[itrack];
0087       case "ndf"_hash:
0088         return &instance.m_ndf[itrack];
0089       case "nOutliers"_hash:
0090         return &instance.m_nOutliers[itrack];
0091       case "nSharedHits"_hash:
0092         return &instance.m_nSharedHits[itrack];
0093       case "nSplitHits"_hash:
0094         return &instance.m_nSplitHits[itrack];
0095       default:
0096         auto it = instance.m_dynamic.find(key);
0097         if (it == instance.m_dynamic.end()) {
0098           throw std::runtime_error("Unable to handle this component");
0099         }
0100 
0101         std::conditional_t<EnsureConst, const detail::DynamicColumnBase*,
0102                            detail::DynamicColumnBase*>
0103             col = it->second.get();
0104         assert(col && "Dynamic column is null");
0105         return col->get(itrack);
0106     }
0107   }
0108 
0109   bool checkConsistency() const {
0110     std::size_t size = m_tipIndex.size();
0111 
0112     bool result = true;
0113     result = result && m_tipIndex.size() == size;
0114     assert(result);
0115     result = result && m_stemIndex.size() == size;
0116     assert(result);
0117     result = result && m_particleHypothesis.size() == size;
0118     assert(result);
0119     result = result && m_params.size() == size;
0120     assert(result);
0121     result = result && m_cov.size() == size;
0122     assert(result);
0123     result = result && m_referenceSurfaces.size() == size;
0124     assert(result);
0125     result = result && m_nMeasurements.size() == size;
0126     assert(result);
0127     result = result && m_nHoles.size() == size;
0128     assert(result);
0129     result = result && m_chi2.size() == size;
0130     assert(result);
0131     result = result && m_ndf.size() == size;
0132     assert(result);
0133     result = result && m_nOutliers.size() == size;
0134     assert(result);
0135     result = result && m_nSharedHits.size() == size;
0136     assert(result);
0137     result = result && m_nSplitHits.size() == size;
0138 
0139     for (const auto& [key, col] : m_dynamic) {
0140       static_cast<void>(key);
0141       result = result && col->size() == size;
0142     }
0143 
0144     return result;
0145   }
0146 
0147  public:
0148   constexpr bool hasColumn_impl(HashedString key) const {
0149     using namespace Acts::HashedStringLiteral;
0150     switch (key) {
0151       case "tipIndex"_hash:
0152       case "stemIndex"_hash:
0153       case "params"_hash:
0154       case "cov"_hash:
0155       case "nMeasurements"_hash:
0156       case "nHoles"_hash:
0157       case "chi2"_hash:
0158       case "ndf"_hash:
0159       case "nOutliers"_hash:
0160       case "nSharedHits"_hash:
0161       case "nSplitHits"_hash:
0162         return true;
0163       default:
0164         return m_dynamic.contains(key);
0165     }
0166   }
0167 
0168   const Surface* referenceSurface_impl(IndexType itrack) const {
0169     return m_referenceSurfaces[itrack].get();
0170   }
0171 
0172   ParticleHypothesis particleHypothesis_impl(IndexType itrack) const {
0173     return m_particleHypothesis[itrack];
0174   }
0175 
0176   std::size_t size_impl() const {
0177     assert(checkConsistency());
0178     return m_tipIndex.size();
0179   }
0180 
0181   detail::DynamicKeyRange<detail::DynamicColumnBase> dynamicKeys_impl() const {
0182     return {m_dynamic.begin(), m_dynamic.end()};
0183   }
0184 
0185   /// @endcond
0186 
0187   // END INTERFACE HELPER
0188 
0189   std::vector<IndexType> m_tipIndex;
0190   std::vector<IndexType> m_stemIndex;
0191   std::vector<ParticleHypothesis> m_particleHypothesis;
0192   std::vector<typename detail_tsp::FixedSizeTypes<eBoundSize>::Coefficients>
0193       m_params;
0194   std::vector<typename detail_tsp::FixedSizeTypes<eBoundSize>::Covariance>
0195       m_cov;
0196   std::vector<std::shared_ptr<const Surface>> m_referenceSurfaces;
0197 
0198   std::vector<unsigned int> m_nMeasurements;
0199   std::vector<unsigned int> m_nHoles;
0200   std::vector<float> m_chi2;
0201   std::vector<unsigned int> m_ndf;
0202   std::vector<unsigned int> m_nOutliers;
0203   std::vector<unsigned int> m_nSharedHits;
0204   std::vector<unsigned int> m_nSplitHits;
0205 
0206   std::unordered_map<HashedString, std::unique_ptr<detail::DynamicColumnBase>>
0207       m_dynamic;
0208   std::vector<HashedString> m_dynamicKeys;
0209 };
0210 
0211 }  // namespace detail_vtc
0212 
0213 class VectorTrackContainer;
0214 class ConstVectorTrackContainer;
0215 
0216 template <>
0217 struct IsReadOnlyTrackContainer<VectorTrackContainer> : std::false_type {};
0218 
0219 /// Track container backend using std::vector for storage
0220 class VectorTrackContainer final : public detail_vtc::VectorTrackContainerBase {
0221  public:
0222   VectorTrackContainer() : VectorTrackContainerBase{} {}
0223   /// Copy constructor
0224   /// @param other The container to copy
0225   VectorTrackContainer(const VectorTrackContainer& other) = default;
0226   /// Move constructor
0227   VectorTrackContainer(VectorTrackContainer&&) = default;
0228 
0229   /// Construct from const container
0230   /// @param other Const container to copy from
0231   explicit VectorTrackContainer(const ConstVectorTrackContainer& other);
0232 
0233  public:
0234   // BEGIN INTERFACE
0235   /// @cond
0236 
0237   std::any component_impl(HashedString key, IndexType itrack) {
0238     return detail_vtc::VectorTrackContainerBase::component_impl<false>(
0239         *this, key, itrack);
0240   }
0241 
0242   std::any component_impl(HashedString key, IndexType itrack) const {
0243     return detail_vtc::VectorTrackContainerBase::component_impl<true>(
0244         *this, key, itrack);
0245   }
0246 
0247   IndexType addTrack_impl();
0248 
0249   void removeTrack_impl(IndexType itrack);
0250 
0251   template <typename T>
0252   constexpr void addColumn_impl(const std::string_view& key) {
0253     HashedString hashedKey = hashStringDynamic(key);
0254     auto insertItr = m_dynamic.insert(
0255         {hashedKey, std::make_unique<detail::DynamicColumn<T>>()});
0256     if (insertItr.second && size() > 0ul) {
0257       insertItr.first->second->resize(size());
0258     }
0259   }
0260 
0261   Parameters parameters(IndexType itrack) {
0262     return Parameters{m_params[itrack].data()};
0263   }
0264 
0265   ConstParameters parameters(IndexType itrack) const {
0266     return ConstParameters{m_params[itrack].data()};
0267   }
0268 
0269   Covariance covariance(IndexType itrack) {
0270     return Covariance{m_cov[itrack].data()};
0271   }
0272 
0273   ConstCovariance covariance(IndexType itrack) const {
0274     return ConstCovariance{m_cov[itrack].data()};
0275   }
0276 
0277   void copyDynamicFrom_impl(IndexType dstIdx, HashedString key,
0278                             const std::any& srcPtr);
0279 
0280   void ensureDynamicColumns_impl(
0281       const detail_vtc::VectorTrackContainerBase& other);
0282 
0283   /// Set the reference surface for a track
0284   /// @param itrack Track index
0285   /// @param surface Reference surface to set
0286   void setReferenceSurface_impl(IndexType itrack,
0287                                 std::shared_ptr<const Surface> surface) {
0288     m_referenceSurfaces[itrack] = std::move(surface);
0289   }
0290 
0291   /// Set the particle hypothesis for a track
0292   /// @param itrack Track index
0293   /// @param particleHypothesis Particle hypothesis to set
0294   void setParticleHypothesis_impl(
0295       IndexType itrack, const ParticleHypothesis& particleHypothesis) {
0296     m_particleHypothesis[itrack] = particleHypothesis;
0297   }
0298 
0299   /// @endcond
0300   // END INTERFACE
0301 
0302   /// Reserve space for tracks
0303   /// @param size Number of tracks to reserve space for
0304   void reserve(IndexType size);
0305 
0306   /// Clear all tracks
0307   void clear();
0308 
0309   /// Get the number of tracks in the container
0310   /// @return Number of tracks
0311   std::size_t size() const;
0312 };
0313 
0314 static_assert(TrackContainerBackend<VectorTrackContainer>,
0315               "VectorTrackContainer does not fulfill TrackContainerBackend");
0316 
0317 class ConstVectorTrackContainer;
0318 
0319 template <>
0320 struct IsReadOnlyTrackContainer<ConstVectorTrackContainer> : std::true_type {};
0321 
0322 /// Read-only vector-backed track container.
0323 class ConstVectorTrackContainer final
0324     : public detail_vtc::VectorTrackContainerBase {
0325  public:
0326   ConstVectorTrackContainer() : VectorTrackContainerBase{} {}
0327 
0328   /// Copy constructor
0329   /// @param other The container to copy from
0330   ConstVectorTrackContainer(const ConstVectorTrackContainer& other) = default;
0331   /// Copy constructor from VectorTrackContainer
0332   /// @param other The container to copy from
0333   explicit ConstVectorTrackContainer(const VectorTrackContainer& other)
0334       : VectorTrackContainerBase{other} {
0335     assert(checkConsistency());
0336   }
0337 
0338   /// Move constructor
0339   ConstVectorTrackContainer(ConstVectorTrackContainer&&) = default;
0340   /// Move constructor from VectorTrackContainer
0341   /// @param other The container to move from
0342   explicit ConstVectorTrackContainer(VectorTrackContainer&& other)
0343       : VectorTrackContainerBase{std::move(other)} {
0344     assert(checkConsistency());
0345   }
0346 
0347  public:
0348   // BEGIN INTERFACE
0349 
0350   /// Get a component from a track
0351   /// @param key The component key
0352   /// @param itrack The track index
0353   /// @return Component value as std::any
0354   std::any component_impl(HashedString key, IndexType itrack) const {
0355     return detail_vtc::VectorTrackContainerBase::component_impl<true>(
0356         *this, key, itrack);
0357   }
0358 
0359   /// Get parameters for a track
0360   /// @param itrack The track index
0361   /// @return Parameters vector
0362   ConstParameters parameters(IndexType itrack) const {
0363     return ConstParameters{m_params[itrack].data()};
0364   }
0365 
0366   /// Get covariance for a track
0367   /// @param itrack The track index
0368   /// @return Covariance matrix
0369   ConstCovariance covariance(IndexType itrack) const {
0370     return ConstCovariance{m_cov[itrack].data()};
0371   }
0372 
0373   // END INTERFACE
0374 };
0375 
0376 static_assert(
0377     TrackContainerBackend<ConstVectorTrackContainer>,
0378     "ConstVectorTrackContainer does not fulfill TrackContainerBackend");
0379 
0380 inline VectorTrackContainer::VectorTrackContainer(
0381     const ConstVectorTrackContainer& other)
0382     : VectorTrackContainerBase{other} {
0383   assert(checkConsistency());
0384 }
0385 
0386 }  // namespace Acts