Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-05 08:11:22

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/Algebra.hpp"
0012 #include "Acts/EventData/TrackParameters.hpp"
0013 #include "Acts/MagneticField/MagneticFieldContext.hpp"
0014 #include "Acts/MagneticField/MagneticFieldProvider.hpp"
0015 #include "Acts/Utilities/Logger.hpp"
0016 #include "Acts/Utilities/Result.hpp"
0017 #include "Acts/Vertexing/FullBilloirVertexFitter.hpp"
0018 #include "Acts/Vertexing/HelicalTrackLinearizer.hpp"
0019 #include "Acts/Vertexing/IVertexFinder.hpp"
0020 #include "Acts/Vertexing/ImpactPointEstimator.hpp"
0021 #include "Acts/Vertexing/TrackLinearizer.hpp"
0022 #include "Acts/Vertexing/Vertex.hpp"
0023 #include "Acts/Vertexing/VertexingOptions.hpp"
0024 
0025 #include <functional>
0026 
0027 namespace Acts {
0028 
0029 /// @class IterativeVertexFinder
0030 ///
0031 /// @brief Implements an iterative vertex finder
0032 ///
0033 ////////////////////////////////////////////////////////////
0034 ///
0035 /// Brief description of the algorithm implemented:
0036 /// Iterative vertex finder which iteratively finds and fits vertices:
0037 /// 1. A list of seed tracks (`seedTracks`, which is the same as the
0038 ///   input track list to the finder at the very first iteration) is used
0039 ///   to retrieve a single vertex seed using the ZScanVertexFinder.
0040 /// 2. All tracks compatible with the current vertex seed are kept and used
0041 ///   for fitting the single vertex.
0042 /// 3.1 If the vertex is a 'good' vertex (i.e. meets requirements) and no
0043 ///   track reassignment after first fit is required, go to step 4. If vertex
0044 ///   is not a good vertex, remove all tracks in tracksToFit from seedTracks.
0045 /// 3.2 If vertex meets requirements and track reassignment after first fit
0046 ///   is required, iterate over all previously found vertices ("old vertex")
0047 ///   and over all their tracksAtVertex. Compare compatibility of each track
0048 ///   with old vertex and current vertex. If track is more compatible with
0049 ///   current vertex, remove track from old vertex, put track back to
0050 ///   tracksToFit and refit current vertex with additional track.
0051 /// 4. If good vertex, `removeUsedCompatibleTracks` method is called, which
0052 ///   removes all used tracks that are compatible with the fitted vertex
0053 ///   from `tracksToFit` and `seedTracks`. It also removes outliers tracks
0054 ///   from tracksAtVertex if not compatible.
0055 /// 5. Add vertex to vertexCollection
0056 /// 6. Repeat until no seedTracks are left or max. number of vertices found
0057 class IterativeVertexFinder final : public IVertexFinder {
0058   using VertexFitter = FullBilloirVertexFitter;
0059 
0060  public:
0061   /// Configuration struct
0062   struct Config {
0063     /// @brief Config constructor
0064     ///
0065     /// @param fitter Vertex fitter
0066     /// @param sfinder The seed finder
0067     /// @param est ImpactPointEstimator
0068     Config(VertexFitter fitter, std::shared_ptr<IVertexFinder> sfinder,
0069            ImpactPointEstimator est)
0070         : vertexFitter(std::move(fitter)),
0071           seedFinder(std::move(sfinder)),
0072           ipEst(std::move(est)) {}
0073 
0074     /// Vertex fitter
0075     VertexFitter vertexFitter;
0076 
0077     /// Track linearizer
0078     TrackLinearizer trackLinearizer;
0079 
0080     /// Vertex seed finder
0081     std::shared_ptr<IVertexFinder> seedFinder;
0082 
0083     /// ImpactPointEstimator
0084     ImpactPointEstimator ipEst;
0085 
0086     /// Vertex finder configuration variables.
0087     /// Tracks that are within a distance of
0088     ///
0089     /// significanceCutSeeding * sqrt(sigma(d0)^2+sigma(z0)^2)
0090     ///
0091     /// are considered compatible with the vertex.
0092     double significanceCutSeeding = 10;
0093     double maximumChi2cutForSeeding = 36.;
0094     int maxVertices = 50;
0095 
0096     /// Assign a certain fraction of compatible tracks to a different (so-called
0097     /// split) vertex if boolean is set to true.
0098     bool createSplitVertices = false;
0099     /// Inverse of the fraction of tracks that will be assigned to the split
0100     /// vertex. E.g., if splitVerticesTrkInvFraction = 2, about 50% of
0101     /// compatible tracks will be assigned to the split vertex.
0102     int splitVerticesTrkInvFraction = 2;
0103     bool reassignTracksAfterFirstFit = false;
0104     bool doMaxTracksCut = false;
0105     int maxTracks = 5000;
0106     double cutOffTrackWeight = 0.01;
0107     /// If `reassignTracksAfterFirstFit` is set this threshold will be used to
0108     /// decide if a track should be checked for reassignment to other vertices
0109     double cutOffTrackWeightReassign = 1;
0110 
0111     /// Function to extract parameters from InputTrack
0112     InputTrack::Extractor extractParameters;
0113 
0114     /// Magnetic field provider
0115     std::shared_ptr<const MagneticFieldProvider> field;
0116   };
0117 
0118   /// State struct
0119   struct State {
0120     State(const MagneticFieldProvider& field,
0121           const Acts::MagneticFieldContext& _magContext)
0122         : magContext(_magContext),
0123           ipState{field.makeCache(magContext)},
0124           fieldCache(field.makeCache(magContext)) {}
0125 
0126     std::reference_wrapper<const Acts::MagneticFieldContext> magContext;
0127 
0128     /// The IP estimator state
0129     ImpactPointEstimator::State ipState;
0130 
0131     MagneticFieldProvider::Cache fieldCache;
0132   };
0133 
0134   /// @brief Constructor for user-defined InputTrack type
0135   ///
0136   /// @param cfg Configuration object
0137   /// @param logger The logging instance
0138   explicit IterativeVertexFinder(Config cfg,
0139                                  std::unique_ptr<const Logger> logger =
0140                                      getDefaultLogger("IterativeVertexFinder",
0141                                                       Logging::INFO));
0142 
0143   /// @brief Finds vertices corresponding to input trackVector
0144   ///
0145   /// @param trackVector Input tracks
0146   /// @param vertexingOptions Vertexing options
0147   /// @param anyState State for fulfilling interfaces
0148   ///
0149   /// @return Collection of vertices found by finder
0150   Result<std::vector<Vertex>> find(
0151       const std::vector<InputTrack>& trackVector,
0152       const VertexingOptions& vertexingOptions,
0153       IVertexFinder::State& anyState) const override;
0154 
0155   IVertexFinder::State makeState(
0156       const MagneticFieldContext& mctx) const override {
0157     return IVertexFinder::State{State{*m_cfg.field, mctx}};
0158   }
0159 
0160   void setTracksToRemove(
0161       IVertexFinder::State& /*anyState*/,
0162       const std::vector<InputTrack>& /*removedTracks*/) const override {
0163     // Nothing to do here
0164   }
0165 
0166  private:
0167   /// Configuration object
0168   const Config m_cfg;
0169 
0170   /// Logging instance
0171   std::unique_ptr<const Logger> m_logger;
0172 
0173   /// Private access to logging instance
0174   const Logger& logger() const { return *m_logger; }
0175 
0176   /// @brief Method that calls seed finder to retrieve a vertex seed
0177   ///
0178   /// @param state The state object
0179   /// @param seedTracks Seeding tracks
0180   /// @param vertexingOptions Vertexing options
0181   ///
0182   /// @return Vertex seed
0183   Result<std::optional<Vertex>> getVertexSeed(
0184       State& state, const std::vector<InputTrack>& seedTracks,
0185       const VertexingOptions& vertexingOptions) const;
0186 
0187   /// @brief Removes all tracks in tracksToRemove from seedTracks
0188   ///
0189   /// @param tracksToRemove Tracks to be removed from seedTracks
0190   /// @param seedTracks List to remove tracks from
0191   void removeTracks(const std::vector<InputTrack>& tracksToRemove,
0192                     std::vector<InputTrack>& seedTracks) const;
0193 
0194   /// @brief Function for calculating how compatible
0195   /// a given track is to a given vertex
0196   ///
0197   /// @param params Track parameters
0198   /// @param vertex The vertex
0199   /// @param perigeeSurface The perigee surface at vertex position
0200   /// @param vertexingOptions Vertexing options
0201   /// @param state The state object
0202   Result<double> getCompatibility(const BoundTrackParameters& params,
0203                                   const Vertex& vertex,
0204                                   const Surface& perigeeSurface,
0205                                   const VertexingOptions& vertexingOptions,
0206                                   State& state) const;
0207 
0208   /// @brief Function that removes used tracks compatible with
0209   /// current vertex (`vertex`) from `tracksToFit` and `seedTracks`
0210   /// as well as outliers from vertex.tracksAtVertex
0211   ///
0212   /// @param vertex Current vertex
0213   /// @param tracksToFit Tracks used to fit `vertex`
0214   /// @param seedTracks Tracks used for vertex seeding
0215   /// @param vertexingOptions Vertexing options
0216   /// @param state The state object
0217   Result<void> removeUsedCompatibleTracks(
0218       Vertex& vertex, std::vector<InputTrack>& tracksToFit,
0219       std::vector<InputTrack>& seedTracks,
0220       const VertexingOptions& vertexingOptions, State& state) const;
0221 
0222   /// @brief Function that fills vector with tracks compatible with seed vertex
0223   ///
0224   /// @param seedTracks List of all available tracks used for seeding
0225   /// @param seedVertex Seed vertex
0226   /// @param tracksToFitOut Tracks to fit
0227   /// @param tracksToFitSplitVertexOut Tracks to fit to split vertex
0228   /// @param vertexingOptions Vertexing options
0229   /// @param state The state object
0230   Result<void> fillTracksToFit(
0231       const std::vector<InputTrack>& seedTracks, const Vertex& seedVertex,
0232       std::vector<InputTrack>& tracksToFitOut,
0233       std::vector<InputTrack>& tracksToFitSplitVertexOut,
0234       const VertexingOptions& vertexingOptions, State& state) const;
0235 
0236   /// @brief Function that reassigns tracks from other vertices
0237   ///        to the current vertex if they are more compatible
0238   ///
0239   /// @param vertexCollection Collection of vertices
0240   /// @param currentVertex Current vertex to assign tracks to
0241   /// @param tracksToFit Tracks to fit vector
0242   /// @param seedTracks Seed tracks vector
0243   /// @param origTracks Vector of original track objects
0244   /// @param vertexingOptions Vertexing options
0245   /// @param state The state object
0246   ///
0247   /// @return Bool if currentVertex is still a good vertex
0248   Result<bool> reassignTracksToNewVertex(
0249       std::vector<Vertex>& vertexCollection, Vertex& currentVertex,
0250       std::vector<InputTrack>& tracksToFit, std::vector<InputTrack>& seedTracks,
0251       const std::vector<InputTrack>& origTracks,
0252       const VertexingOptions& vertexingOptions, State& state) const;
0253 
0254   /// @brief Counts all tracks that are significant for a vertex
0255   ///
0256   /// @param vtx The vertex
0257   ///
0258   /// @return Number of significant tracks
0259   int countSignificantTracks(const Vertex& vtx) const;
0260 };
0261 
0262 }  // namespace Acts