Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-15 09:42:30

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/MagneticField/MagneticFieldProvider.hpp"
0012 #include "Acts/Utilities/AnnealingUtility.hpp"
0013 #include "Acts/Utilities/Logger.hpp"
0014 #include "Acts/Utilities/Result.hpp"
0015 #include "Acts/Vertexing/AMVFInfo.hpp"
0016 #include "Acts/Vertexing/ImpactPointEstimator.hpp"
0017 #include "Acts/Vertexing/TrackAtVertex.hpp"
0018 #include "Acts/Vertexing/TrackLinearizer.hpp"
0019 #include "Acts/Vertexing/Vertex.hpp"
0020 #include "Acts/Vertexing/VertexingError.hpp"
0021 #include "Acts/Vertexing/VertexingOptions.hpp"
0022 
0023 #include <algorithm>
0024 
0025 namespace Acts {
0026 
0027 /// @class AdaptiveMultiVertexFitter
0028 /// @brief Implements an adaptive multi-vertex fitter as described
0029 ///   in detail in Section 5.3.5 in:
0030 ///   Ref. (1): CERN-THESIS-2010-027, Author: Piacquadio, Giacinto:
0031 ///   `Identification of b-jets and investigation of the discovery potential
0032 ///   of a Higgs boson in the WH−−>lvbb¯ channel with the ATLAS experiment`
0033 ///
0034 class AdaptiveMultiVertexFitter {
0035  public:
0036   /// @brief The fitter state
0037   struct State {
0038     /// Constructor for multi-vertex fitter state
0039     /// @param field Magnetic field provider for track extrapolation
0040     /// @param magContext Magnetic field context for field evaluations
0041     State(const MagneticFieldProvider& field,
0042           const Acts::MagneticFieldContext& magContext)
0043         : ipState{field.makeCache(magContext)},
0044           fieldCache(field.makeCache(magContext)) {}
0045 
0046     /// Vertex collection to be fitted
0047     std::vector<Vertex*> vertexCollection;
0048 
0049     /// Annealing state for thermodynamic track weighting
0050     AnnealingUtility::State annealingState;
0051 
0052     /// Impact point estimator state for track parameter calculations
0053     ImpactPointEstimator::State ipState;
0054 
0055     /// Magnetic field cache for field evaluations during fitting
0056     MagneticFieldProvider::Cache fieldCache;
0057 
0058     /// Map storing vertex information for each vertex in the fit
0059     /// @todo Does this have to be a mutable pointer?
0060     std::map<Vertex*, VertexInfo> vtxInfoMap;
0061 
0062     /// Multimap connecting tracks to their associated vertices
0063     std::multimap<InputTrack, Vertex*> trackToVerticesMultiMap;
0064 
0065     /// Map storing track-at-vertex information for each track-vertex pair
0066     std::map<std::pair<InputTrack, Vertex*>, TrackAtVertex> tracksAtVerticesMap;
0067 
0068     /// Adds a vertex to trackToVerticesMultiMap
0069     /// @param vtx Vertex to add to the multimap with its associated tracks
0070     void addVertexToMultiMap(Vertex& vtx) {
0071       for (auto trk : vtxInfoMap[&vtx].trackLinks) {
0072         trackToVerticesMultiMap.emplace(trk, &vtx);
0073       }
0074     }
0075 
0076     /// Removes a vertex from trackToVerticesMultiMap
0077     /// @param vtx Vertex to remove from the multimap along with its track associations
0078     void removeVertexFromMultiMap(Vertex& vtx) {
0079       for (auto iter = trackToVerticesMultiMap.begin();
0080            iter != trackToVerticesMultiMap.end();) {
0081         if (iter->second == &vtx) {
0082           iter = trackToVerticesMultiMap.erase(iter);
0083         } else {
0084           ++iter;
0085         }
0086       }
0087     }
0088 
0089     /// Remove a vertex from the vertex collection
0090     /// @param vtxToRemove Vertex to remove from the collection
0091     /// @param logger Logger for diagnostic messages
0092     /// @return Result indicating success or failure of the removal operation
0093     Result<void> removeVertexFromCollection(Vertex& vtxToRemove,
0094                                             const Logger& logger) {
0095       auto it = std::ranges::find(vertexCollection, &vtxToRemove);
0096       // Check if the value was found before erasing
0097       if (it == vertexCollection.end()) {
0098         ACTS_ERROR("vtxToRemove is not part of vertexCollection.");
0099         return VertexingError::ElementNotFound;
0100       }
0101       // Erase the element if found
0102       vertexCollection.erase(it);
0103       return {};
0104     }
0105   };
0106 
0107   struct Config {
0108     /// @brief Config constructor
0109     ///
0110     /// @param est ImpactPointEstimator
0111     explicit Config(ImpactPointEstimator est) : ipEst(std::move(est)) {}
0112 
0113     // ImpactPointEstimator
0114     ImpactPointEstimator ipEst;
0115 
0116     /// Annealing tool used for a thermodynamic annealing scheme for the
0117     /// track weight factors in such a way that with high temperature values
0118     /// (at the beginning) only a slight preference is given to tracks
0119     /// compatible with the estimated vertex position. With lower temperatures
0120     /// the weighting get stricter such that all incompatible tracks will be
0121     /// dropped at the end while keeping all compatible tracks with a weight=1.
0122     ///   Ref. (1): CERN-THESIS-2010-027, Author: Piacquadio, Giacinto:
0123     ///   `Identification of b-jets and investigation of the discovery potential
0124     ///   of a Higgs boson in the WH−−>lvbb¯ channel with the ATLAS experiment`
0125     AnnealingUtility annealingTool;
0126 
0127     // Number of max iterations
0128     unsigned int maxIterations{30};
0129 
0130     // Max distance to linearization point allowed
0131     // without relinearization
0132     double maxDistToLinPoint{0.5};
0133 
0134     // Minimum track weight needed for track to be considered
0135     double minWeight{0.0001};
0136 
0137     // Max relative shift of vertex during one iteration
0138     double maxRelativeShift{0.01};
0139 
0140     // Do smoothing after multivertex fit
0141     bool doSmoothing{false};
0142 
0143     // Use time information when calculating the vertex compatibility
0144     bool useTime{false};
0145 
0146     // Function to extract parameters from InputTrack
0147     InputTrack::Extractor extractParameters;
0148 
0149     TrackLinearizer trackLinearizer;
0150   };
0151 
0152   /// @brief Constructor for user-defined InputTrack_t type !=
0153   /// BoundTrackParameters
0154   ///
0155   /// @param cfg Configuration object
0156   /// object
0157   /// @param logger The logging instance
0158   explicit AdaptiveMultiVertexFitter(
0159       Config cfg, std::unique_ptr<const Logger> logger = getDefaultLogger(
0160                       "AdaptiveMultiVertexFitter", Logging::INFO));
0161 
0162   /// @brief Adds a new vertex to an existing multi-vertex fit.
0163   /// 1. The 3D impact parameters are calculated for all tracks associated
0164   /// with newVertex.
0165   /// 2. A list of all vertices that are connected with newVertex via shared
0166   /// tracks is created. It also considers indirect connections (e.g., vertex A
0167   /// which shares a track with vertex B which, in turn, shares a track with
0168   /// newVertex).
0169   /// 3. The multivertex fit is performed for all vertices on said list.
0170   ///
0171   /// @param state Fitter state
0172   /// @param newVertices Vertex to be added to fit
0173   /// @param vertexingOptions Vertexing options
0174   ///
0175   /// @return Result<void> object
0176   Result<void> addVtxToFit(State& state,
0177                            const std::vector<Vertex*>& newVertices,
0178                            const VertexingOptions& vertexingOptions) const;
0179 
0180   /// @brief Performs a simultaneous fit of all vertices in
0181   /// state.vertexCollection
0182   ///
0183   /// @param state Fitter state
0184   /// @param vertexingOptions Vertexing options
0185   ///
0186   /// @return Result<void> object
0187   Result<void> fit(State& state,
0188                    const VertexingOptions& vertexingOptions) const;
0189 
0190  private:
0191   /// Configuration object
0192   const Config m_cfg;
0193 
0194   /// Logging instance
0195   std::unique_ptr<const Logger> m_logger;
0196 
0197   /// Private access to logging instance
0198   const Logger& logger() const { return *m_logger; }
0199 
0200   /// @brief Tests if vertex is already in list of vertices or not
0201   ///
0202   /// @param vtx Vertex to test
0203   /// @param verticesVec Vector of vertices to search
0204   ///
0205   /// @return True if vtx is already in verticesVec
0206   bool isAlreadyInList(Vertex* vtx,
0207                        const std::vector<Vertex*>& verticesVec) const;
0208 
0209   /// @brief 1) Calls ImpactPointEstimator::estimate3DImpactParameters
0210   /// for all tracks that are associated with vtx (i.e., all elements
0211   /// of the trackLinks vector in the VertexInfo of vtx).
0212   /// 2) Saves the 3D impact parameters in the VertexInfo of vtx.
0213   ///
0214   /// @param state Vertex fitter state
0215   /// @param vtx Vertex object
0216   /// @param vertexingOptions Vertexing options
0217   Result<void> prepareVertexForFit(
0218       State& state, Vertex* vtx,
0219       const VertexingOptions& vertexingOptions) const;
0220 
0221   /// @brief Sets the vertexCompatibility for all TrackAtVertex objects
0222   /// at the current vertex
0223   ///
0224   /// @param state Fitter state
0225   /// @param currentVtx Current vertex
0226   /// @param vertexingOptions Vertexing options
0227   Result<void> setAllVertexCompatibilities(
0228       State& state, Vertex* currentVtx,
0229       const VertexingOptions& vertexingOptions) const;
0230 
0231   /// @brief Sets weights to the track according to Eq.(5.46) in Ref.(1)
0232   ///  and updates the vertices by calling the VertexUpdater
0233   ///
0234   /// @param state Fitter state
0235   /// @param vertexingOptions Vertexing options
0236   Result<void> setWeightsAndUpdate(
0237       State& state, const VertexingOptions& vertexingOptions) const;
0238 
0239   /// @brief Collects the compatibility values of the track `trk`
0240   /// wrt to all of its associated vertices
0241   ///
0242   /// @param state Fitter state
0243   /// @param trk Track
0244   ///
0245   /// @return Vector of compatibility values
0246   std::vector<double> collectTrackToVertexCompatibilities(
0247       State& state, const InputTrack& trk) const;
0248 
0249   /// @brief Determines if any vertex position has shifted more than
0250   /// m_cfg.maxRelativeShift in the last iteration
0251   ///
0252   /// @param state Fitter state
0253   ///
0254   /// @return False if at least one shift was larger than maxRelativeShift
0255   bool checkSmallShift(State& state) const;
0256 
0257   /// @brief Updates tracks for current vertex with knowledge
0258   /// of current vertex position
0259   ///
0260   /// @param state Fitter state
0261   void doVertexSmoothing(State& state) const;
0262 
0263   /// @brief Logs vertices in state.vertexCollection and associated tracks
0264   ///
0265   /// @param state Fitter state
0266   /// @param geoContext Geometry context
0267   void logDebugData(const State& state,
0268                     const GeometryContext& geoContext) const;
0269 };
0270 
0271 }  // namespace Acts