Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:11:13

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/Definitions/Units.hpp"
0013 #include "Acts/EventData/TrackParameters.hpp"
0014 #include "Acts/MagneticField/MagneticFieldContext.hpp"
0015 #include "Acts/Utilities/Logger.hpp"
0016 #include "Acts/Utilities/Result.hpp"
0017 #include "Acts/Vertexing/AMVFInfo.hpp"
0018 #include "Acts/Vertexing/AdaptiveMultiVertexFitter.hpp"
0019 #include "Acts/Vertexing/IVertexFinder.hpp"
0020 #include "Acts/Vertexing/ImpactPointEstimator.hpp"
0021 #include "Acts/Vertexing/TrackLinearizer.hpp"
0022 #include "Acts/Vertexing/VertexingOptions.hpp"
0023 
0024 #include <functional>
0025 #include <type_traits>
0026 
0027 namespace Acts {
0028 
0029 /// @brief Implements an iterative vertex finder
0030 class AdaptiveMultiVertexFinder final : public IVertexFinder {
0031   using VertexFitter = AdaptiveMultiVertexFitter;
0032   using VertexFitterState = VertexFitter::State;
0033 
0034  public:
0035   /// Configuration struct
0036   struct Config {
0037     /// @brief Config constructor
0038     ///
0039     /// @param fitter The vertex fitter
0040     /// @param sfinder The seed finder
0041     /// @param ipEst ImpactPointEstimator
0042     /// @param bIn Input magnetic field
0043     Config(VertexFitter fitter, std::shared_ptr<const IVertexFinder> sfinder,
0044            ImpactPointEstimator ipEst,
0045            std::shared_ptr<const MagneticFieldProvider> bIn)
0046         : vertexFitter(std::move(fitter)),
0047           seedFinder(std::move(sfinder)),
0048           ipEstimator(std::move(ipEst)),
0049           bField{std::move(bIn)} {}
0050 
0051     /// Vertex fitter
0052     VertexFitter vertexFitter;
0053 
0054     /// Vertex seed finder
0055     std::shared_ptr<const IVertexFinder> seedFinder;
0056 
0057     /// ImpactPointEstimator
0058     ImpactPointEstimator ipEstimator;
0059 
0060     std::shared_ptr<const MagneticFieldProvider> bField;
0061 
0062     /// Max z interval used for adding tracks to fit:
0063     /// When adding a new vertex to the multi vertex fit,
0064     /// only the tracks whose z at PCA is closer
0065     /// to the seeded vertex than tracksMaxZinterval
0066     /// are added to this new vertex.
0067     ///
0068     /// Note: If you cut too hard, you cut out
0069     /// the good cases where the seed finder is not
0070     /// reliable, but the fit would be still able to converge
0071     /// towards the right vertex. If you cut too soft, you
0072     /// consider a lot of tracks which just slow down the fit.
0073     double tracksMaxZinterval = 3. * Acts::UnitConstants::mm;
0074 
0075     /// Maximum allowed significance of track position to vertex seed to
0076     /// consider track as compatible to vertex. If useTime is set to true, the
0077     /// time coordinate also contributes to the significance and
0078     /// tracksMaxSignificance needs to be increased. 5 corresponds to a p-value
0079     /// of ~0.92 using `chi2(x=5,ndf=2)`
0080     double tracksMaxSignificance = 5.;
0081 
0082     /// Max chi2 value for which tracks are considered compatible with
0083     /// the fitted vertex. These tracks are removed from the seedTracks
0084     /// after the fit has been performed.
0085     double maxVertexChi2 = 18.42;
0086 
0087     /// Perform a 'real' multi-vertex fit as intended by the algorithm.
0088     /// If switched to true, always all (!) tracks are considered to be
0089     /// added to the new vertex candidate after seeding. If switched to
0090     /// false, only the seedTracks, i.e. all tracks that are considered
0091     /// as outliers of previously fitted vertices, are used.
0092     bool doRealMultiVertex = true;
0093 
0094     /// Decides if you want to use the ```vertexCompatibility``` of the
0095     ///  track (set to true) or the ```chi2Track``` (set to false) as an
0096     /// estimate for a track being an outlier or not.
0097     /// In case the track refitting is switched on in the AMVFitter, you
0098     /// may want to use the refitted ```chi2Track```.
0099     bool useFastCompatibility = true;
0100 
0101     /// Maximum significance on the distance between two vertices
0102     /// to allow merging of two vertices.
0103     /// 3 corresponds to a p-value of ~0.92 using `chi2(x=3,ndf=1)`
0104     double maxMergeVertexSignificance = 3.;
0105 
0106     /// Minimum weight a track has to have to be considered a compatible
0107     /// track with a vertex candidate.
0108     ///
0109     /// Note: This value has to be the same as the one in the AMVFitter.
0110     double minWeight = 0.0001;
0111 
0112     /// Maximal number of iterations in the finding procedure
0113     int maxIterations = 100;
0114 
0115     /// Include also single track vertices
0116     bool addSingleTrackVertices = false;
0117 
0118     /// If doFullSplitting == true, we check the 3D distance (if useTime ==
0119     /// false) or the 4D distance (if useTime == true) of the vertices to
0120     /// determine whether they are merged.
0121     /// If doFullSplitting == false, we check the z distance (if useTime ==
0122     /// false) or the z-t distance (if useTime == true) of the vertices to
0123     /// determine whether they are merged.
0124     bool doFullSplitting = false;
0125 
0126     /// Maximum vertex contamination value
0127     double maximumVertexContamination = 0.5;
0128 
0129     /// Use seed vertex as a constraint for the fit
0130     bool useSeedConstraint = true;
0131 
0132     /// Variances of the 4D vertex position before the vertex fit if no beamspot
0133     /// constraint is provided
0134     Vector4 initialVariances = Vector4::Constant(1e+8);
0135 
0136     /// Default fitQuality for constraint vertex in case no beamspot
0137     /// constraint is provided
0138     std::pair<double, double> defaultConstrFitQuality{0., -3.};
0139 
0140     /// Use the full available vertex covariance information after
0141     /// seeding for the IP estimation. In original implementation
0142     /// this is not (!) done, however, this is probably not correct.
0143     /// So definitely consider setting this to true.
0144     bool useVertexCovForIPEstimation = false;
0145 
0146     /// Use time information when assigning tracks to vertices. If this is set
0147     /// to true, useTime of the vertex fitter configuration should also be set
0148     /// to true, and time seeding should be enabled.
0149     bool useTime = false;
0150 
0151     /// If set to true, the vertex finder will not break the finding loop.
0152     /// Some seeders are not able to cope with this therefore this is
0153     /// disabled by default.
0154     bool doNotBreakWhileSeeding = false;
0155 
0156     /// Function to extract parameters from InputTrack
0157     InputTrack::Extractor extractParameters;
0158   };
0159 
0160   /// State struct for fulfilling interface
0161   struct State {
0162     std::reference_wrapper<const MagneticFieldContext> magContext;
0163 
0164     IVertexFinder::State seedFinderState;
0165   };
0166 
0167   /// @brief Constructor for user-defined InputTrack_t type !=
0168   /// BoundTrackParameters
0169   ///
0170   /// @param cfg Configuration object
0171   /// @param logger The logging instance
0172   AdaptiveMultiVertexFinder(Config cfg,
0173                             std::unique_ptr<const Logger> logger =
0174                                 getDefaultLogger("AdaptiveMultiVertexFinder",
0175                                                  Logging::INFO))
0176       : m_cfg(std::move(cfg)), m_logger(std::move(logger)) {
0177     if (!m_cfg.extractParameters.connected()) {
0178       throw std::invalid_argument(
0179           "AdaptiveMultiVertexFinder: "
0180           "No function to extract parameters "
0181           "from InputTrack provided.");
0182     }
0183 
0184     if (!m_cfg.seedFinder) {
0185       throw std::invalid_argument(
0186           "AdaptiveMultiVertexFinder: "
0187           "No vertex fitter provided.");
0188     }
0189   }
0190 
0191   AdaptiveMultiVertexFinder(AdaptiveMultiVertexFinder&&) = default;
0192 
0193   /// @brief Function that performs the adaptive
0194   /// multi-vertex finding
0195   ///
0196   /// @param allTracks Input track collection
0197   /// @param vertexingOptions Vertexing options
0198   /// @param anyState The state object
0199   ///
0200   /// @return Vector of all reconstructed vertices
0201   Result<std::vector<Vertex>> find(
0202       const std::vector<InputTrack>& allTracks,
0203       const VertexingOptions& vertexingOptions,
0204       IVertexFinder::State& anyState) const override;
0205 
0206   IVertexFinder::State makeState(
0207       const Acts::MagneticFieldContext& mctx) const override {
0208     return IVertexFinder::State{
0209         State{mctx, IVertexFinder::State{m_cfg.seedFinder->makeState(mctx)}}};
0210   }
0211 
0212   void setTracksToRemove(
0213       IVertexFinder::State& /*state*/,
0214       const std::vector<InputTrack>& /*removedTracks*/) const override {
0215     // Nothing to do here
0216   }
0217 
0218  private:
0219   /// Configuration object
0220   Config m_cfg;
0221 
0222   /// Logging instance
0223   std::unique_ptr<const Logger> m_logger;
0224 
0225   /// Private access to logging instance
0226   const Logger& logger() const { return *m_logger; }
0227 
0228   /// @brief Calls the seed finder and sets constraints on the found seed
0229   /// vertex if desired
0230   ///
0231   /// @param trackVector All tracks to be used for seeding
0232   /// @param currentConstraint Vertex constraint
0233   /// @param vertexingOptions Vertexing options
0234   /// @param seedFinderState The seed finder state
0235   /// @param removedSeedTracks Seed track that have been removed
0236   /// from seed track collection in last iteration
0237   ///
0238   /// @return The seed vertex
0239   Result<std::optional<Vertex>> doSeeding(
0240       const std::vector<InputTrack>& trackVector, Vertex& currentConstraint,
0241       const VertexingOptions& vertexingOptions,
0242       IVertexFinder::State& seedFinderState,
0243       const std::vector<InputTrack>& removedSeedTracks) const;
0244 
0245   /// @brief Sets constraint vertex after seeding
0246   ///
0247   /// @param currentConstraint Vertex constraint
0248   /// @param useVertexConstraintInFit Indicates whether constraint is used during vertex fit
0249   /// @param seedVertex Seed vertex
0250   void setConstraintAfterSeeding(Vertex& currentConstraint,
0251                                  bool useVertexConstraintInFit,
0252                                  Vertex& seedVertex) const;
0253 
0254   /// @brief Calculates the IP significance of a track to a given vertex
0255   ///
0256   /// @param track The track
0257   /// @param vtx The vertex
0258   /// @param vertexingOptions Vertexing options
0259   ///
0260   /// @return The IP significance
0261   Result<double> getIPSignificance(
0262       const InputTrack& track, const Vertex& vtx,
0263       const VertexingOptions& vertexingOptions) const;
0264 
0265   /// @brief Adds compatible track to vertex candidate
0266   ///
0267   /// @param tracks The tracks
0268   /// @param vtx The vertex candidate
0269   /// @param[out] fitterState The vertex fitter state
0270   /// @param vertexingOptions Vertexing options
0271   Result<void> addCompatibleTracksToVertex(
0272       const std::vector<InputTrack>& tracks, Vertex& vtx,
0273       VertexFitterState& fitterState,
0274       const VertexingOptions& vertexingOptions) const;
0275 
0276   /// @brief Method that tries to recover from cases where no tracks
0277   /// were added to the vertex candidate after seeding
0278   ///
0279   /// @param allTracks The tracks to be considered (either origTrack or
0280   /// seedTracks)
0281   /// @param seedTracks The seed tracks
0282   /// @param[out] vtx The vertex candidate
0283   /// @param currentConstraint Vertex constraint
0284   /// @param[out] fitterState The vertex fitter state
0285   /// @param vertexingOptions Vertexing options
0286   ///
0287   /// return True if recovery was successful, false otherwise
0288   Result<bool> canRecoverFromNoCompatibleTracks(
0289       const std::vector<InputTrack>& allTracks,
0290       const std::vector<InputTrack>& seedTracks, Vertex& vtx,
0291       const Vertex& currentConstraint, VertexFitterState& fitterState,
0292       const VertexingOptions& vertexingOptions) const;
0293 
0294   /// @brief Method that tries to prepare the vertex for the fit
0295   ///
0296   /// @param allTracks The tracks to be considered (either origTrack or
0297   /// seedTracks)
0298   /// @param seedTracks The seed tracks
0299   /// @param[out] vtx The vertex candidate
0300   /// @param currentConstraint Vertex constraint
0301   /// @param[out] fitterState The vertex fitter state
0302   /// @param vertexingOptions Vertexing options
0303   ///
0304   /// @return True if preparation was successful, false otherwise
0305   Result<bool> canPrepareVertexForFit(
0306       const std::vector<InputTrack>& allTracks,
0307       const std::vector<InputTrack>& seedTracks, Vertex& vtx,
0308       const Vertex& currentConstraint, VertexFitterState& fitterState,
0309       const VertexingOptions& vertexingOptions) const;
0310 
0311   /// @brief Method that checks if vertex is a good vertex and if
0312   /// compatible tracks are available
0313   ///
0314   /// @param vtx The vertex candidate
0315   /// @param seedTracks The seed tracks
0316   /// @param fitterState The vertex fitter state
0317   /// @param useVertexConstraintInFit Indicates whether constraint is used in the vertex fit
0318   ///
0319   /// @return pair(nCompatibleTracks, isGoodVertex)
0320   std::pair<int, bool> checkVertexAndCompatibleTracks(
0321       Vertex& vtx, const std::vector<InputTrack>& seedTracks,
0322       VertexFitterState& fitterState, bool useVertexConstraintInFit) const;
0323 
0324   /// @brief Method that removes all tracks that are compatible with
0325   /// current vertex from seedTracks
0326   ///
0327   /// @param vtx The vertex candidate
0328   /// @param[out] seedTracks The seed tracks
0329   /// @param fitterState The vertex fitter state
0330   /// @param[out] removedSeedTracks Collection of seed track that will be
0331   /// removed
0332   void removeCompatibleTracksFromSeedTracks(
0333       Vertex& vtx, std::vector<InputTrack>& seedTracks,
0334       VertexFitterState& fitterState,
0335       std::vector<InputTrack>& removedSeedTracks) const;
0336 
0337   /// @brief Method that tries to remove an incompatible track
0338   /// from seed tracks after removing a compatible track failed.
0339   ///
0340   /// @param vtx The vertex candidate
0341   /// @param[out] seedTracks The seed tracks
0342   /// @param fitterState The vertex fitter state
0343   /// @param[out] removedSeedTracks Collection of seed track that will be
0344   /// removed
0345   /// @param[in] geoCtx The geometry context to access global positions
0346   ///
0347   /// @return Incompatible track was removed
0348   bool removeTrackIfIncompatible(Vertex& vtx,
0349                                  std::vector<InputTrack>& seedTracks,
0350                                  VertexFitterState& fitterState,
0351                                  std::vector<InputTrack>& removedSeedTracks,
0352                                  const GeometryContext& geoCtx) const;
0353 
0354   /// @brief Method that evaluates if the new vertex candidate should
0355   /// be kept, i.e. saved, or not
0356   ///
0357   /// @param vtx The vertex candidate
0358   /// @param allVertices All so far found vertices
0359   /// @param fitterState The vertex fitter state
0360   ///
0361   /// @return Keep new vertex
0362   Result<bool> keepNewVertex(Vertex& vtx,
0363                              const std::vector<Vertex*>& allVertices,
0364                              VertexFitterState& fitterState) const;
0365 
0366   /// @brief Method that evaluates if the new vertex candidate is
0367   /// merged with one of the previously found vertices
0368   ///
0369   /// @param vtx The vertex candidate
0370   /// @param allVertices All vertices that were found so far
0371   ///
0372   /// @return Bool indicating whether the vertex is merged
0373   Result<bool> isMergedVertex(const Vertex& vtx,
0374                               const std::vector<Vertex*>& allVertices) const;
0375 
0376   /// @brief Method that deletes last vertex from list of all vertices
0377   /// and refits all vertices afterwards
0378   ///
0379   /// @param vtx The last added vertex which will be removed
0380   /// @param allVertices Vector containing the unique_ptr to vertices
0381   /// @param allVerticesPtr Vector containing the actual addresses
0382   /// @param fitterState The current vertex fitter state
0383   /// @param vertexingOptions Vertexing options
0384   Result<void> deleteLastVertex(
0385       Vertex& vtx, std::vector<std::unique_ptr<Vertex>>& allVertices,
0386       std::vector<Vertex*>& allVerticesPtr, VertexFitterState& fitterState,
0387       const VertexingOptions& vertexingOptions) const;
0388 
0389   /// @brief Prepares the output vector of vertices
0390   ///
0391   /// @param allVerticesPtr Vector of pointers to vertices
0392   /// @param fitterState The vertex fitter state
0393   ///
0394   /// @return The output vertex collection
0395   Result<std::vector<Vertex>> getVertexOutputList(
0396       const std::vector<Vertex*>& allVerticesPtr,
0397       VertexFitterState& fitterState) const;
0398 };
0399 
0400 }  // namespace Acts