Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:10:50

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 = MultiTrajectoryTraits::IndexType;
0041   static constexpr auto kInvalid = MultiTrajectoryTraits::kInvalid;
0042   static constexpr auto MeasurementSizeMax =
0043       MultiTrajectoryTraits::MeasurementSizeMax;
0044 
0045   using Parameters =
0046       typename detail_lt::FixedSizeTypes<eBoundSize, false>::CoefficientsMap;
0047   using Covariance =
0048       typename detail_lt::FixedSizeTypes<eBoundSize, false>::CovarianceMap;
0049 
0050   using ConstParameters =
0051       typename detail_lt::FixedSizeTypes<eBoundSize, true>::CoefficientsMap;
0052   using ConstCovariance =
0053       typename detail_lt::FixedSizeTypes<eBoundSize, true>::CovarianceMap;
0054 
0055  protected:
0056   VectorTrackContainerBase() = default;
0057 
0058   VectorTrackContainerBase(const VectorTrackContainerBase& other);
0059 
0060   VectorTrackContainerBase(VectorTrackContainerBase&& other) = default;
0061 
0062   // BEGIN INTERFACE HELPER
0063 
0064   template <bool EnsureConst, typename T>
0065   static std::any component_impl(T& instance, HashedString key,
0066                                  IndexType itrack) {
0067     using namespace Acts::HashedStringLiteral;
0068     if constexpr (EnsureConst) {
0069       static_assert(std::is_const_v<std::remove_reference_t<T>>,
0070                     "Is not const");
0071     }
0072 
0073     using namespace Acts::HashedStringLiteral;
0074     switch (key) {
0075       case "tipIndex"_hash:
0076         return &instance.m_tipIndex[itrack];
0077       case "stemIndex"_hash:
0078         return &instance.m_stemIndex[itrack];
0079       case "params"_hash:
0080         return &instance.m_params[itrack];
0081       case "cov"_hash:
0082         return &instance.m_cov[itrack];
0083       case "nMeasurements"_hash:
0084         return &instance.m_nMeasurements[itrack];
0085       case "nHoles"_hash:
0086         return &instance.m_nHoles[itrack];
0087       case "chi2"_hash:
0088         return &instance.m_chi2[itrack];
0089       case "ndf"_hash:
0090         return &instance.m_ndf[itrack];
0091       case "nOutliers"_hash:
0092         return &instance.m_nOutliers[itrack];
0093       case "nSharedHits"_hash:
0094         return &instance.m_nSharedHits[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 
0137     for (const auto& [key, col] : m_dynamic) {
0138       (void)key;
0139       result = result && col->size() == size;
0140     }
0141 
0142     return result;
0143   }
0144 
0145  public:
0146   constexpr bool hasColumn_impl(HashedString key) const {
0147     using namespace Acts::HashedStringLiteral;
0148     switch (key) {
0149       default:
0150         return m_dynamic.contains(key);
0151     }
0152   }
0153 
0154   const Surface* referenceSurface_impl(IndexType itrack) const {
0155     return m_referenceSurfaces[itrack].get();
0156   }
0157 
0158   ParticleHypothesis particleHypothesis_impl(IndexType itrack) const {
0159     return m_particleHypothesis[itrack];
0160   }
0161 
0162   std::size_t size_impl() const {
0163     assert(checkConsistency());
0164     return m_tipIndex.size();
0165   }
0166 
0167   detail::DynamicKeyRange<detail::DynamicColumnBase> dynamicKeys_impl() const {
0168     return {m_dynamic.begin(), m_dynamic.end()};
0169   }
0170 
0171   // END INTERFACE HELPER
0172 
0173   std::vector<IndexType> m_tipIndex;
0174   std::vector<IndexType> m_stemIndex;
0175   std::vector<ParticleHypothesis> m_particleHypothesis;
0176   std::vector<typename detail_lt::FixedSizeTypes<eBoundSize>::Coefficients>
0177       m_params;
0178   std::vector<typename detail_lt::FixedSizeTypes<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_view& key) {
0228     HashedString hashedKey = hashStringDynamic(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   std::size_t size() const;
0257 
0258   void setReferenceSurface_impl(IndexType itrack,
0259                                 std::shared_ptr<const Surface> surface) {
0260     m_referenceSurfaces[itrack] = std::move(surface);
0261   }
0262 
0263   void setParticleHypothesis_impl(
0264       IndexType itrack, const ParticleHypothesis& particleHypothesis) {
0265     m_particleHypothesis[itrack] = particleHypothesis;
0266   }
0267 
0268   // END INTERFACE
0269 };
0270 
0271 static_assert(TrackContainerBackend<VectorTrackContainer>,
0272               "VectorTrackContainer does not fulfill TrackContainerBackend");
0273 
0274 class ConstVectorTrackContainer;
0275 
0276 template <>
0277 struct IsReadOnlyTrackContainer<ConstVectorTrackContainer> : std::true_type {};
0278 
0279 class ConstVectorTrackContainer final
0280     : public detail_vtc::VectorTrackContainerBase {
0281  public:
0282   ConstVectorTrackContainer() : VectorTrackContainerBase{} {}
0283 
0284   ConstVectorTrackContainer(const ConstVectorTrackContainer& other) = default;
0285   ConstVectorTrackContainer(const VectorTrackContainer& other)
0286       : VectorTrackContainerBase{other} {
0287     assert(checkConsistency());
0288   }
0289 
0290   ConstVectorTrackContainer(ConstVectorTrackContainer&&) = default;
0291   ConstVectorTrackContainer(VectorTrackContainer&& other)
0292       : VectorTrackContainerBase{std::move(other)} {
0293     assert(checkConsistency());
0294   }
0295 
0296  public:
0297   // BEGIN INTERFACE
0298 
0299   std::any component_impl(HashedString key, IndexType itrack) const {
0300     return detail_vtc::VectorTrackContainerBase::component_impl<true>(
0301         *this, key, itrack);
0302   }
0303 
0304   ConstParameters parameters(IndexType itrack) const {
0305     return ConstParameters{m_params[itrack].data()};
0306   }
0307 
0308   ConstCovariance covariance(IndexType itrack) const {
0309     return ConstCovariance{m_cov[itrack].data()};
0310   }
0311 
0312   // END INTERFACE
0313 };
0314 
0315 static_assert(
0316     TrackContainerBackend<ConstVectorTrackContainer>,
0317     "ConstVectorTrackContainer does not fulfill TrackContainerBackend");
0318 
0319 inline VectorTrackContainer::VectorTrackContainer(
0320     const ConstVectorTrackContainer& other)
0321     : VectorTrackContainerBase{other} {
0322   assert(checkConsistency());
0323 }
0324 
0325 }  // namespace Acts