Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:10:01

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2022 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 http://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/Concepts.hpp"
0019 #include "Acts/Utilities/HashedString.hpp"
0020 
0021 #include <any>
0022 #include <cassert>
0023 #include <cstddef>
0024 #include <memory>
0025 #include <stdexcept>
0026 #include <string>
0027 #include <type_traits>
0028 #include <unordered_map>
0029 #include <utility>
0030 #include <vector>
0031 
0032 namespace Acts {
0033 class Surface;
0034 template <typename T>
0035 struct IsReadOnlyTrackContainer;
0036 
0037 namespace detail_vtc {
0038 
0039 class VectorTrackContainerBase {
0040  public:
0041   using IndexType = MultiTrajectoryTraits::IndexType;
0042   static constexpr auto kInvalid = MultiTrajectoryTraits::kInvalid;
0043   static constexpr auto MeasurementSizeMax =
0044       MultiTrajectoryTraits::MeasurementSizeMax;
0045 
0046   using Parameters =
0047       typename detail_lt::Types<eBoundSize, false>::CoefficientsMap;
0048   using Covariance =
0049       typename detail_lt::Types<eBoundSize, false>::CovarianceMap;
0050 
0051   using ConstParameters =
0052       typename detail_lt::Types<eBoundSize, true>::CoefficientsMap;
0053   using ConstCovariance =
0054       typename detail_lt::Types<eBoundSize, true>::CovarianceMap;
0055 
0056  protected:
0057   VectorTrackContainerBase() = default;
0058 
0059   VectorTrackContainerBase(const VectorTrackContainerBase& other);
0060 
0061   VectorTrackContainerBase(VectorTrackContainerBase&& other) = default;
0062 
0063   // BEGIN INTERFACE HELPER
0064 
0065   template <bool EnsureConst, typename T>
0066   static std::any component_impl(T& instance, HashedString key,
0067                                  IndexType itrack) {
0068     using namespace Acts::HashedStringLiteral;
0069     if constexpr (EnsureConst) {
0070       static_assert(std::is_const_v<std::remove_reference_t<T>>,
0071                     "Is not const");
0072     }
0073 
0074     using namespace Acts::HashedStringLiteral;
0075     switch (key) {
0076       case "tipIndex"_hash:
0077         return &instance.m_tipIndex[itrack];
0078       case "stemIndex"_hash:
0079         return &instance.m_stemIndex[itrack];
0080       case "params"_hash:
0081         return &instance.m_params[itrack];
0082       case "cov"_hash:
0083         return &instance.m_cov[itrack];
0084       case "nMeasurements"_hash:
0085         return &instance.m_nMeasurements[itrack];
0086       case "nHoles"_hash:
0087         return &instance.m_nHoles[itrack];
0088       case "chi2"_hash:
0089         return &instance.m_chi2[itrack];
0090       case "ndf"_hash:
0091         return &instance.m_ndf[itrack];
0092       case "nOutliers"_hash:
0093         return &instance.m_nOutliers[itrack];
0094       case "nSharedHits"_hash:
0095         return &instance.m_nSharedHits[itrack];
0096       default:
0097         auto it = instance.m_dynamic.find(key);
0098         if (it == instance.m_dynamic.end()) {
0099           throw std::runtime_error("Unable to handle this component");
0100         }
0101 
0102         std::conditional_t<EnsureConst, const detail::DynamicColumnBase*,
0103                            detail::DynamicColumnBase*>
0104             col = it->second.get();
0105         assert(col && "Dynamic column is null");
0106         return col->get(itrack);
0107     }
0108   }
0109 
0110   bool checkConsistency() const {
0111     std::size_t size = m_tipIndex.size();
0112 
0113     bool result = true;
0114     result = result && m_tipIndex.size() == size;
0115     assert(result);
0116     result = result && m_stemIndex.size() == size;
0117     assert(result);
0118     result = result && m_particleHypothesis.size() == size;
0119     assert(result);
0120     result = result && m_params.size() == size;
0121     assert(result);
0122     result = result && m_cov.size() == size;
0123     assert(result);
0124     result = result && m_referenceSurfaces.size() == size;
0125     assert(result);
0126     result = result && m_nMeasurements.size() == size;
0127     assert(result);
0128     result = result && m_nHoles.size() == size;
0129     assert(result);
0130     result = result && m_chi2.size() == size;
0131     assert(result);
0132     result = result && m_ndf.size() == size;
0133     assert(result);
0134     result = result && m_nOutliers.size() == size;
0135     assert(result);
0136     result = result && m_nSharedHits.size() == size;
0137 
0138     for (const auto& [key, col] : m_dynamic) {
0139       (void)key;
0140       result = result && col->size() == size;
0141     }
0142 
0143     return result;
0144   }
0145 
0146  public:
0147   constexpr bool hasColumn_impl(HashedString key) const {
0148     using namespace Acts::HashedStringLiteral;
0149     switch (key) {
0150       default:
0151         return m_dynamic.find(key) != m_dynamic.end();
0152     }
0153   }
0154 
0155   const Surface* referenceSurface_impl(IndexType itrack) const {
0156     return m_referenceSurfaces[itrack].get();
0157   }
0158 
0159   ParticleHypothesis particleHypothesis_impl(IndexType itrack) const {
0160     return m_particleHypothesis[itrack];
0161   }
0162 
0163   std::size_t size_impl() const {
0164     assert(checkConsistency());
0165     return m_tipIndex.size();
0166   }
0167 
0168   detail::DynamicKeyRange<detail::DynamicColumnBase> dynamicKeys_impl() const {
0169     return {m_dynamic.begin(), m_dynamic.end()};
0170   }
0171 
0172   // END INTERFACE HELPER
0173 
0174   std::vector<IndexType> m_tipIndex;
0175   std::vector<IndexType> m_stemIndex;
0176   std::vector<ParticleHypothesis> m_particleHypothesis;
0177   std::vector<typename detail_lt::Types<eBoundSize>::Coefficients> m_params;
0178   std::vector<typename detail_lt::Types<eBoundSize>::Covariance> m_cov;
0179   std::vector<std::shared_ptr<const Surface>> m_referenceSurfaces;
0180 
0181   std::vector<unsigned int> m_nMeasurements;
0182   std::vector<unsigned int> m_nHoles;
0183   std::vector<float> m_chi2;
0184   std::vector<unsigned int> m_ndf;
0185   std::vector<unsigned int> m_nOutliers;
0186   std::vector<unsigned int> m_nSharedHits;
0187 
0188   std::unordered_map<HashedString, std::unique_ptr<detail::DynamicColumnBase>>
0189       m_dynamic;
0190   std::vector<HashedString> m_dynamicKeys;
0191 };
0192 
0193 }  // namespace detail_vtc
0194 
0195 class VectorTrackContainer;
0196 class ConstVectorTrackContainer;
0197 
0198 template <>
0199 struct IsReadOnlyTrackContainer<VectorTrackContainer> : std::false_type {};
0200 
0201 class VectorTrackContainer final : public detail_vtc::VectorTrackContainerBase {
0202  public:
0203   VectorTrackContainer() : VectorTrackContainerBase{} {}
0204   VectorTrackContainer(const VectorTrackContainer& other) = default;
0205   VectorTrackContainer(VectorTrackContainer&&) = default;
0206 
0207   VectorTrackContainer(const ConstVectorTrackContainer& other);
0208 
0209  public:
0210   // BEGIN INTERFACE
0211 
0212   std::any component_impl(HashedString key, IndexType itrack) {
0213     return detail_vtc::VectorTrackContainerBase::component_impl<false>(
0214         *this, key, itrack);
0215   }
0216 
0217   std::any component_impl(HashedString key, IndexType itrack) const {
0218     return detail_vtc::VectorTrackContainerBase::component_impl<true>(
0219         *this, key, itrack);
0220   }
0221 
0222   IndexType addTrack_impl();
0223 
0224   void removeTrack_impl(IndexType itrack);
0225 
0226   template <typename T>
0227   constexpr void addColumn_impl(const std::string& key) {
0228     Acts::HashedString hashedKey = hashString(key);
0229     m_dynamic.insert({hashedKey, std::make_unique<detail::DynamicColumn<T>>()});
0230   }
0231 
0232   Parameters parameters(IndexType itrack) {
0233     return Parameters{m_params[itrack].data()};
0234   }
0235 
0236   ConstParameters parameters(IndexType itrack) const {
0237     return ConstParameters{m_params[itrack].data()};
0238   }
0239 
0240   Covariance covariance(IndexType itrack) {
0241     return Covariance{m_cov[itrack].data()};
0242   }
0243 
0244   ConstCovariance covariance(IndexType itrack) const {
0245     return ConstCovariance{m_cov[itrack].data()};
0246   }
0247 
0248   void copyDynamicFrom_impl(IndexType dstIdx, HashedString key,
0249                             const std::any& srcPtr);
0250 
0251   void ensureDynamicColumns_impl(
0252       const detail_vtc::VectorTrackContainerBase& other);
0253 
0254   void reserve(IndexType size);
0255   void clear();
0256 
0257   void setReferenceSurface_impl(IndexType itrack,
0258                                 std::shared_ptr<const Surface> surface) {
0259     m_referenceSurfaces[itrack] = std::move(surface);
0260   }
0261 
0262   void setParticleHypothesis_impl(
0263       IndexType itrack, const ParticleHypothesis& particleHypothesis) {
0264     m_particleHypothesis[itrack] = particleHypothesis;
0265   }
0266 
0267   // END INTERFACE
0268 };
0269 
0270 ACTS_STATIC_CHECK_CONCEPT(TrackContainerBackend, VectorTrackContainer);
0271 
0272 class ConstVectorTrackContainer;
0273 
0274 template <>
0275 struct IsReadOnlyTrackContainer<ConstVectorTrackContainer> : std::true_type {};
0276 
0277 class ConstVectorTrackContainer final
0278     : public detail_vtc::VectorTrackContainerBase {
0279  public:
0280   ConstVectorTrackContainer() : VectorTrackContainerBase{} {}
0281 
0282   ConstVectorTrackContainer(const ConstVectorTrackContainer& other) = default;
0283   ConstVectorTrackContainer(const VectorTrackContainer& other)
0284       : VectorTrackContainerBase{other} {
0285     assert(checkConsistency());
0286   }
0287 
0288   ConstVectorTrackContainer(ConstVectorTrackContainer&&) = default;
0289   ConstVectorTrackContainer(VectorTrackContainer&& other)
0290       : VectorTrackContainerBase{std::move(other)} {
0291     assert(checkConsistency());
0292   }
0293 
0294  public:
0295   // BEGIN INTERFACE
0296 
0297   std::any component_impl(HashedString key, IndexType itrack) const {
0298     return detail_vtc::VectorTrackContainerBase::component_impl<true>(
0299         *this, key, itrack);
0300   }
0301 
0302   ConstParameters parameters(IndexType itrack) const {
0303     return ConstParameters{m_params[itrack].data()};
0304   }
0305 
0306   ConstCovariance covariance(IndexType itrack) const {
0307     return ConstCovariance{m_cov[itrack].data()};
0308   }
0309 
0310   // END INTERFACE
0311 };
0312 
0313 ACTS_STATIC_CHECK_CONCEPT(ConstTrackContainerBackend,
0314                           ConstVectorTrackContainer);
0315 
0316 inline VectorTrackContainer::VectorTrackContainer(
0317     const ConstVectorTrackContainer& other)
0318     : VectorTrackContainerBase{other} {
0319   assert(checkConsistency());
0320 }
0321 
0322 }  // namespace Acts