Back to home page

EIC code displayed by LXR

 
 

    


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

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 class ScoreBasedAmbiguityResolution {
0042  public:
0043   /// @brief Detector configuration struct : contains the configuration for each detector
0044   ///
0045   /// The configuration can be saved in a json file and loaded from there.
0046   ///
0047   struct DetectorConfig {
0048     int hitsScoreWeight = 0;
0049     int holesScoreWeight = 0;
0050     int outliersScoreWeight = 0;
0051     int otherScoreWeight = 0;
0052 
0053     std::size_t minHits = 0;
0054     std::size_t maxHits = 0;
0055     std::size_t maxHoles = 0;
0056     std::size_t maxOutliers = 0;
0057     std::size_t maxSharedHits = 0;
0058 
0059     /// if true, the shared hits are considered as bad hits for this detector
0060     bool sharedHitsFlag = false;
0061 
0062     std::size_t detectorId = 0;
0063 
0064     /// a list of values from  0 to 1, the higher number of hits, higher value
0065     /// in the list is multiplied to ambuiguity score applied only if
0066     /// useAmbiguityFunction is true
0067     std::vector<double> factorHits = {1.0};
0068 
0069     /// a list of values from  0 to 1, the higher number of holes, lower value
0070     /// in the list is multiplied to ambuiguity score applied only if
0071     /// useAmbiguityFunction is true
0072     std::vector<double> factorHoles = {1.0};
0073   };
0074 
0075   /// @brief  TrackFeatures struct : contains the features that are counted for each track.
0076   ///
0077   /// The trackFeatures is used to compute the score of each track
0078   struct TrackFeatures {
0079     std::size_t nHits = 0;
0080     std::size_t nHoles = 0;
0081     std::size_t nOutliers = 0;
0082     std::size_t nSharedHits = 0;
0083   };
0084 
0085   enum class TrackStateTypes : std::uint8_t {
0086     // A measurement not yet used in any other track
0087     UnsharedHit,
0088     // A measurement shared with another track
0089     SharedHit,
0090     // A hit that needs to be removed from the track
0091     RejectedHit,
0092     // An outlier, to be copied in case
0093     Outlier,
0094     // Other trackstate types to be copied in case
0095     OtherTrackStateType
0096   };
0097 
0098   /// @brief Configuration struct : contains the configuration for the ambiguity resolution.
0099   struct Config {
0100     std::map<std::size_t, std::size_t> volumeMap = {{0, 0}};
0101     std::vector<DetectorConfig> detectorConfigs;
0102     /// minimum score for any track
0103     double minScore = 0;
0104     /// minimum score for shared tracks
0105     double minScoreSharedTracks = 0;
0106     /// maximum number of shared tracks per measurement
0107     std::size_t maxSharedTracksPerMeasurement = 10;
0108     /// maximum number of shared hit per track
0109     std::size_t maxShared = 5;
0110 
0111     double pTMin = 0 * UnitConstants::GeV;
0112     double pTMax = 1e5 * UnitConstants::GeV;
0113 
0114     double phiMin = -std::numbers::pi * UnitConstants::rad;
0115     double phiMax = std::numbers::pi * UnitConstants::rad;
0116 
0117     double etaMin = -5;
0118     double etaMax = 5;
0119 
0120     // if true, the ambiguity score is computed based on a different function.
0121     bool useAmbiguityFunction = false;
0122   };
0123 
0124   /// @brief OptionalCuts struct : contains the optional cuts to be applied.
0125   ///
0126   /// The optional cuts,weights and score are used to remove tracks that are not
0127   /// good enough, based on some criteria. Users are free to add their own cuts
0128   /// with the help of this struct.
0129   template <TrackProxyConcept track_proxy_t>
0130   struct OptionalCuts {
0131     using OptionalFilter = std::function<bool(const track_proxy_t&)>;
0132 
0133     using OptionalScoreModifier =
0134         std::function<void(const track_proxy_t&, double&)>;
0135 
0136     using OptionalHitSelection = std::function<void(
0137         const track_proxy_t&,
0138         const typename track_proxy_t::ConstTrackStateProxy&, TrackStateTypes&)>;
0139 
0140     std::vector<OptionalFilter> cuts = {};
0141     std::vector<OptionalScoreModifier> weights = {};
0142 
0143     /// applied only if useAmbiguityFunction is true
0144     std::vector<OptionalScoreModifier> scores = {};
0145     std::vector<OptionalHitSelection> hitSelections = {};
0146   };
0147 
0148   ScoreBasedAmbiguityResolution(
0149       const Config& cfg,
0150       std::unique_ptr<const Logger> logger =
0151           getDefaultLogger("ScoreBasedAmbiguityResolution", Logging::INFO))
0152       : m_cfg{cfg}, m_logger{std::move(logger)} {}
0153 
0154   /// Compute the initial state of the tracks.
0155   ///
0156   /// @param tracks is the input track container
0157   /// @return trackFeaturesVectors is the trackFeatures map from detector ID to trackFeatures
0158   template <TrackContainerFrontend track_container_t>
0159   std::vector<std::vector<TrackFeatures>> computeInitialState(
0160       const track_container_t& tracks) const;
0161 
0162   /// Compute the score of each track.
0163   ///
0164   /// @param tracks is the input track container
0165   /// @param trackFeaturesVectors is the trackFeatures map from detector ID to trackFeatures
0166   /// @param optionalCuts is the user defined optional cuts to be applied.
0167   /// @return a vector of scores for each track
0168   template <TrackContainerFrontend track_container_t>
0169   std::vector<double> simpleScore(
0170       const track_container_t& tracks,
0171       const std::vector<std::vector<TrackFeatures>>& trackFeaturesVectors,
0172       const OptionalCuts<typename track_container_t::ConstTrackProxy>&
0173           optionalCuts = {}) const;
0174 
0175   /// Compute the score of each track based on the ambiguity function.
0176   ///
0177   /// @param tracks is the input track container
0178   /// @param trackFeaturesVectors is the trackFeatures map from detector ID to trackFeatures
0179   /// @param optionalCuts is the user defined optional cuts to be applied.
0180   /// @return a vector of scores for each track
0181   template <TrackContainerFrontend track_container_t>
0182   std::vector<double> ambiguityScore(
0183       const track_container_t& tracks,
0184       const std::vector<std::vector<TrackFeatures>>& trackFeaturesVectors,
0185       const OptionalCuts<typename track_container_t::ConstTrackProxy>&
0186           optionalCuts = {}) const;
0187 
0188   /// Remove hits that are not good enough for each track and removes tracks
0189   /// that have a score below a certain threshold or not enough hits.
0190   ///
0191   /// @brief Remove tracks that are not good enough based on cuts
0192   /// @param track is the input track
0193   /// @param trackScore is the score of each track
0194   /// @param measurementsPerTrack is the list of measurements for each track
0195   /// @param nTracksPerMeasurement is the number of tracks per measurement
0196   /// @param optionalHitSelections is the optional hit selections to be applied
0197   /// @return a vector of IDs of the tracks we want to keep
0198   template <TrackProxyConcept track_proxy_t>
0199   bool getCleanedOutTracks(
0200       const track_proxy_t& track, const double& trackScore,
0201       const std::vector<std::size_t>& measurementsPerTrack,
0202       const std::map<std::size_t, std::size_t>& nTracksPerMeasurement,
0203       const std::vector<std::function<
0204           void(const track_proxy_t&,
0205                const typename track_proxy_t::ConstTrackStateProxy&,
0206                TrackStateTypes&)>>& optionalHitSelections = {}) const;
0207 
0208   /// Remove tracks that are bad based on cuts and weighted scores.
0209   ///
0210   /// @brief Remove tracks that are not good enough
0211   /// @param tracks is the input track container
0212   /// @param sourceLinkHash is the  source links
0213   /// @param sourceLinkEquality is the equality function for the source links
0214   /// @param optionalCuts is the optional cuts to be applied
0215   /// @return a vector of IDs of the tracks we want to keep
0216   template <TrackContainerFrontend track_container_t,
0217             typename source_link_hash_t, typename source_link_equality_t>
0218   std::vector<int> solveAmbiguity(
0219       const track_container_t& tracks, source_link_hash_t sourceLinkHash,
0220       source_link_equality_t sourceLinkEquality,
0221       const OptionalCuts<typename track_container_t::ConstTrackProxy>&
0222           optionalCuts = {}) const;
0223 
0224  private:
0225   Config m_cfg;
0226 
0227   /// Logging instance
0228   std::unique_ptr<const Logger> m_logger = nullptr;
0229 
0230   /// Private access to logging instance
0231   const Logger& logger() const;
0232 };
0233 
0234 }  // namespace Acts
0235 
0236 #include "Acts/AmbiguityResolution/ScoreBasedAmbiguityResolution.ipp"