Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-13 08:15:23

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/Units.hpp"
0012 #include "Acts/EventData/TrackContainer.hpp"
0013 #include "Acts/EventData/TrackContainerFrontendConcept.hpp"
0014 #include "Acts/EventData/TrackProxyConcept.hpp"
0015 #include "Acts/EventData/TrackStateProxy.hpp"
0016 #include "Acts/Utilities/Delegate.hpp"
0017 #include "Acts/Utilities/Logger.hpp"
0018 
0019 #include <cstddef>
0020 #include <map>
0021 #include <memory>
0022 #include <numbers>
0023 #include <string>
0024 #include <tuple>
0025 #include <vector>
0026 
0027 #include <boost/container/flat_map.hpp>
0028 #include <boost/container/flat_set.hpp>
0029 
0030 namespace Acts {
0031 
0032 /// Generic implementation of the score based ambiguity resolution.
0033 /// The alhorithm is based on the following steps:
0034 /// 1) Compute the initial state of the tracks
0035 /// 2) Compute the score of each track
0036 /// 3) Removes hits that are not good enough for each track
0037 /// 4) Remove tracks that have a score below a certain threshold or not have
0038 /// enough hits
0039 /// 5) Remove tracks that are not good enough based on cuts Contains method for
0040 /// data preparations
0041 
0042 class ScoreBasedAmbiguityResolution {
0043  public:
0044   /// @brief Detector configuration struct : contains the configuration for each detector
0045   ///
0046   /// The configuration can be saved in a json file and loaded from there.
0047   ///
0048   struct DetectorConfig {
0049     /// Weight for hits in track scoring
0050     int hitsScoreWeight = 0;
0051     /// Weight for holes in track scoring
0052     int holesScoreWeight = 0;
0053     /// Weight for outliers in track scoring
0054     int outliersScoreWeight = 0;
0055     /// Weight for other track states in scoring
0056     int otherScoreWeight = 0;
0057 
0058     /// Eta bin boundaries for this detector
0059     std::vector<double> etaBins = {-5, 5};
0060 
0061     /// Minimum required hits per eta bin
0062     std::vector<std::size_t> minHitsPerEta = {0};
0063 
0064     /// Maximum allowed holes per eta bin
0065     std::vector<std::size_t> maxHolesPerEta = {0};
0066 
0067     /// Maximum allowed outliers per eta bin
0068     std::vector<std::size_t> maxOutliersPerEta = {0};
0069 
0070     /// Maximum allowed shared hits per eta bin
0071     std::vector<std::size_t> maxSharedHitsPerEta = {0};
0072 
0073     /// Maximum allowed hits for tracks in this detector
0074     std::size_t maxHits = 0;
0075     /// Maximum allowed holes for tracks in this detector
0076     std::size_t maxHoles = 0;
0077 
0078     /// if true, the shared hits are considered as bad hits for this detector
0079     bool sharedHitsFlag = false;
0080 
0081     /// Unique identifier for this detector configuration
0082     std::size_t detectorId = 0;
0083 
0084     /// a list of values from  0 to 1, the higher number of hits, higher value
0085     /// in the list is multiplied to ambuiguity score applied only if
0086     /// useAmbiguityScoring is true
0087     std::vector<double> factorHits = {1.0};
0088 
0089     /// a list of values from  0 to 1, the higher number of holes, lower value
0090     /// in the list is multiplied to ambuiguity score applied only if
0091     /// useAmbiguityScoring is true
0092     std::vector<double> factorHoles = {1.0};
0093   };
0094 
0095   /// @brief  TrackFeatures struct : contains the features that are counted for each track.
0096   ///
0097   /// The trackFeatures is used to compute the score of each track
0098   struct TrackFeatures {
0099     /// Number of hits on this track
0100     std::size_t nHits = 0;
0101     /// Number of holes on this track
0102     std::size_t nHoles = 0;
0103     /// Number of outliers on this track
0104     std::size_t nOutliers = 0;
0105     /// Number of hits shared with other tracks
0106     std::size_t nSharedHits = 0;
0107   };
0108 
0109   /// Enumeration of track state types for ambiguity resolution
0110   enum class TrackStateTypes : std::uint8_t {
0111     // A measurement not yet used in any other track
0112     UnsharedHit,
0113     // A measurement shared with another track
0114     SharedHit,
0115     // A hit that needs to be removed from the track
0116     RejectedHit,
0117     // An outlier, to be copied in case
0118     Outlier,
0119     // Other trackstate types to be copied in case
0120     OtherTrackStateType
0121   };
0122 
0123   /// @brief Configuration struct : contains the configuration for the ambiguity resolution.
0124   struct Config {
0125     /// Map from volume IDs to detector configuration indices
0126     std::map<std::size_t, std::size_t> volumeMap = {{0, 0}};
0127     /// Detector-specific configuration settings
0128     std::vector<DetectorConfig> detectorConfigs;
0129     /// minimum score for any track
0130     double minScore = 0;
0131     /// minimum score for shared tracks
0132     double minScoreSharedTracks = 0;
0133     /// maximum number of shared tracks per measurement
0134     std::size_t maxSharedTracksPerMeasurement = 10;
0135     /// maximum number of shared hit per track
0136     std::size_t maxShared = 5;
0137     /// minimum number of unshared hits per track
0138     std::size_t minUnshared = 5;
0139 
0140     // if true, the ambiguity score is computed based on a different function.
0141     /// Flag to enable alternative ambiguity scoring algorithm
0142     bool useAmbiguityScoring = false;
0143   };
0144 
0145   /// @brief Optionals struct: contains the optional cuts, weights and score to be applied.
0146   ///
0147   /// The default cuts and scoring has only a basic set of cuts and
0148   /// score-modifiers. For more flexibility users can define custom cuts and
0149   /// scores using this structure.
0150   template <TrackProxyConcept track_proxy_t>
0151   struct Optionals {
0152     /// Type alias for optional track cuts function
0153     using OptionalCuts = std::function<bool(const track_proxy_t&)>;
0154 
0155     /// Type alias for optional score modifier function
0156     using OptionalScoreModifier =
0157         std::function<void(const track_proxy_t&, double&)>;
0158 
0159     /// Type alias for optional hit selection function
0160     using OptionalHitSelection = std::function<void(
0161         const track_proxy_t&,
0162         const typename track_proxy_t::ConstTrackStateProxy&, TrackStateTypes&)>;
0163 
0164     /// Custom track selection cuts to apply
0165     std::vector<OptionalCuts> cuts = {};
0166     /// Custom track score modifiers/weights
0167     std::vector<OptionalScoreModifier> weights = {};
0168 
0169     /// applied only if useAmbiguityScoring is true
0170     std::vector<OptionalScoreModifier> scores = {};
0171     /// Custom hit selection functions for track states
0172     std::vector<OptionalHitSelection> hitSelections = {};
0173   };
0174 
0175   /// Constructor with configuration and optional logger
0176   /// @param cfg Configuration object for the ambiguity resolution
0177   /// @param logger Logger instance for output, defaults to INFO level
0178   explicit ScoreBasedAmbiguityResolution(
0179       const Config& cfg,
0180       std::unique_ptr<const Logger> logger =
0181           getDefaultLogger("ScoreBasedAmbiguityResolution", Logging::INFO))
0182       : m_cfg{cfg}, m_logger{std::move(logger)} {}
0183 
0184   /// Compute the initial state of the tracks.
0185   ///
0186   /// @param tracks is the input track container
0187   /// @return trackFeaturesVectors is the trackFeatures map from detector ID to trackFeatures
0188   template <TrackContainerFrontend track_container_t>
0189   std::vector<std::vector<TrackFeatures>> computeInitialState(
0190       const track_container_t& tracks) const;
0191 
0192   /// Compute the score of each track.
0193   ///
0194   /// @param tracks is the input track container
0195   /// @param trackFeaturesVectors is the trackFeatures map from detector ID to trackFeatures
0196   /// @param optionals is the user defined optional cuts to be applied.
0197   /// @return a vector of scores for each track
0198   template <TrackContainerFrontend track_container_t>
0199   std::vector<double> simpleScore(
0200       const track_container_t& tracks,
0201       const std::vector<std::vector<TrackFeatures>>& trackFeaturesVectors,
0202       const Optionals<typename track_container_t::ConstTrackProxy>& optionals =
0203           {}) const;
0204 
0205   /// Compute the score of each track based on the ambiguity function.
0206   ///
0207   /// @param tracks is the input track container
0208   /// @param trackFeaturesVectors is the trackFeatures map from detector ID to trackFeatures
0209   /// @param optionals is the user defined optional cuts to be applied.
0210   /// @return a vector of scores for each track
0211   template <TrackContainerFrontend track_container_t>
0212   std::vector<double> ambiguityScore(
0213       const track_container_t& tracks,
0214       const std::vector<std::vector<TrackFeatures>>& trackFeaturesVectors,
0215       const Optionals<typename track_container_t::ConstTrackProxy>& optionals =
0216           {}) const;
0217 
0218   /// Rejects Tracks based on eta dependent cuts.
0219   ///
0220   /// @param detector is the detector configuration object
0221   /// @param trackFeatures is the trackFeatures object for a specific detector
0222   /// @param eta is the eta of the track
0223   /// @return true if the track is rejected, false otherwise
0224   bool etaBasedCuts(const DetectorConfig& detector,
0225                     const TrackFeatures& trackFeatures,
0226                     const double& eta) const;
0227 
0228   /// Remove hits that are not good enough for each track and removes tracks
0229   /// that have a score below a certain threshold or not enough hits.
0230   ///
0231   /// @brief Remove tracks that are not good enough based on cuts
0232   /// @param track is the input track
0233   /// @param trackScore is the score of each track
0234   /// @param measurementsPerTrack is the list of measurements for each track
0235   /// @param nTracksPerMeasurement is the number of tracks per measurement
0236   /// @param optionalHitSelections is the optional hit selections to be applied
0237   /// @return a vector of IDs of the tracks we want to keep
0238   template <TrackProxyConcept track_proxy_t>
0239   bool getCleanedOutTracks(
0240       const track_proxy_t& track, const double& trackScore,
0241       const std::vector<std::size_t>& measurementsPerTrack,
0242       const std::map<std::size_t, std::size_t>& nTracksPerMeasurement,
0243       const std::vector<std::function<
0244           void(const track_proxy_t&,
0245                const typename track_proxy_t::ConstTrackStateProxy&,
0246                TrackStateTypes&)>>& optionalHitSelections = {}) const;
0247 
0248   /// Remove tracks that are bad based on cuts and weighted scores.
0249   ///
0250   /// @brief Remove tracks that are not good enough
0251   /// @param tracks is the input track container
0252   /// @param sourceLinkHash is the  source links
0253   /// @param sourceLinkEquality is the equality function for the source links
0254   /// @param optionals are the optional cuts and score modifiers to be applied
0255   /// @return a vector of IDs of the tracks we want to keep
0256   template <TrackContainerFrontend track_container_t,
0257             typename source_link_hash_t, typename source_link_equality_t>
0258   std::vector<int> solveAmbiguity(
0259       const track_container_t& tracks, source_link_hash_t sourceLinkHash,
0260       source_link_equality_t sourceLinkEquality,
0261       const Optionals<typename track_container_t::ConstTrackProxy>& optionals =
0262           {}) const;
0263 
0264  private:
0265   Config m_cfg;
0266 
0267   /// Logging instance
0268   std::unique_ptr<const Logger> m_logger = nullptr;
0269 
0270   /// Private access to logging instance
0271   const Logger& logger() const;
0272 };
0273 
0274 }  // namespace Acts
0275 
0276 #include "Acts/AmbiguityResolution/ScoreBasedAmbiguityResolution.ipp"