Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-13 08:19:31

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