Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:27:59

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2020 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 http://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 #include "Acts/Definitions/Algebra.hpp"
0011 #include "Acts/EventData/TrackParameters.hpp"
0012 #include "Acts/Utilities/Result.hpp"
0013 
0014 namespace Acts {
0015 /// @class GaussianGridTrackDensity
0016 /// @brief Implements a 1-dim density grid to be filled with
0017 /// track Gaussian distributions. Each single track is modelled
0018 /// as a 2(!)-dim Gaussian distribution grid in the d0-z0 plane,
0019 /// but only the overlap with the z-axis (i.e. a 1-dim density
0020 /// vector) needs to be calculated.
0021 /// The position of the highest track density (of either a single
0022 /// bin or the sum of a certain region) can be determined.
0023 /// Single tracks can be cached and removed from the overall density.
0024 class GaussianGridTrackDensity {
0025  public:
0026   using MainGridVector = Eigen::Matrix<float, Eigen::Dynamic, 1>;
0027   using TrackGridVector = Eigen::Matrix<float, Eigen::Dynamic, 1>;
0028 
0029   /// The configuration struct
0030   struct Config {
0031     /// @param zMinMax_ The minimum and maximum z-values (in mm) that
0032     ///                 should be covered by the main 1-dim density grid along
0033     ///                 the z-axis
0034     /// @param mainGridSize_ The size of the z-axis 1-dim main density grid
0035     /// @param trkGridSize_ The 2(!)-dim grid size of a single track, i.e.
0036     /// a single track is modelled as a (trkGridSize x trkGridSize) grid
0037     /// in the d0-z0 plane. Note: trkGridSize has to be an odd value.
0038     /// @note The value of @p zMinMax_ together with @p mainGridSize_ determines the
0039     /// overall bin size to be used as seen below
0040     Config(float zMinMax_ = 100, int mainGridSize_ = 2000,
0041            int trkGridSize_ = 15)
0042         : mainGridSize(mainGridSize_),
0043           trkGridSize(trkGridSize_),
0044           zMinMax(zMinMax_) {
0045       binSize = 2. * zMinMax / mainGridSize;
0046 
0047       if (trkGridSize % 2 == 0) {
0048         throw std::runtime_error(
0049             "GaussianGridTrackDensity: trkGridSize has to be an odd value!");
0050       }
0051       if (mainGridSize < trkGridSize) {
0052         throw std::runtime_error(
0053             "GaussianGridTrackDensity: mainGridSize has to be bigger than "
0054             "trkGridSize!");
0055       }
0056     }
0057 
0058     int mainGridSize;
0059     int trkGridSize;
0060 
0061     // Min and max z value of big grid
0062     float zMinMax;  // mm
0063 
0064     // Z size of one single bin in grid
0065     float binSize;  // mm
0066 
0067     // Do NOT use just the z-bin with the highest
0068     // track density, but instead check the (up to)
0069     // first three density maxima (only those that have
0070     // a maximum relative deviation of 'relativeDensityDev'
0071     // from the main maximum) and take the z-bin of the
0072     // maximum with the highest surrounding density sum
0073     bool useHighestSumZPosition = false;
0074 
0075     // The maximum relative density deviation from the main
0076     // maximum to consider the second and third maximum for
0077     // the highest-sum approach from above
0078     float maxRelativeDensityDev = 0.01;
0079   };
0080 
0081   GaussianGridTrackDensity(const Config& cfg) : m_cfg(cfg) {}
0082 
0083   /// @brief Returns the z position of maximum track density
0084   ///
0085   /// @param mainGrid The main 1-dim density grid along the z-axis
0086   ///
0087   /// @return The z position of maximum track density
0088   Result<float> getMaxZPosition(MainGridVector& mainGrid) const;
0089 
0090   /// @brief Returns the z position of maximum track density and
0091   /// the estimated width
0092   ///
0093   /// @param mainGrid The main 1-dim density grid along the z-axis
0094   ///
0095   /// @return The z position of maximum track density and width
0096   Result<std::pair<float, float>> getMaxZPositionAndWidth(
0097       MainGridVector& mainGrid) const;
0098 
0099   /// @brief Adds a single track to the overall grid density
0100   ///
0101   /// @param trk The track to be added
0102   /// @param mainGrid The main 1-dim density grid along the z-axis
0103   ///
0104   /// @return A pair storing information about the z-bin position
0105   /// the track was added (int) and the 1-dim density contribution
0106   /// of the track itself
0107   std::pair<int, TrackGridVector> addTrack(const BoundTrackParameters& trk,
0108                                            MainGridVector& mainGrid) const;
0109 
0110   /// @brief Removes a track from the overall grid density
0111   ///
0112   /// @param zBin The center z-bin position the track needs to be
0113   /// removed from
0114   /// @param trkGrid The 1-dim density contribution of the track
0115   /// @param mainGrid The main 1-dim density grid along the z-axis
0116   void removeTrackGridFromMainGrid(int zBin, const TrackGridVector& trkGrid,
0117                                    MainGridVector& mainGrid) const;
0118 
0119   const Config& config() const { return m_cfg; }
0120 
0121  private:
0122   /// @brief Helper function that actually adds the track to the
0123   /// main density grid
0124   ///
0125   /// @param zBin The center z-bin position the track
0126   /// @param trkGrid The 1-dim density contribution of the track
0127   /// @param mainGrid The main 1-dim density grid along the z-axis
0128   void addTrackGridToMainGrid(int zBin, const TrackGridVector& trkGrid,
0129                               MainGridVector& mainGrid) const;
0130 
0131   /// @brief Helper function that modifies the main density grid
0132   /// (either adds or removes a track)
0133   ///
0134   /// @param zBin The center z-bin position the track
0135   /// @param trkGrid The 1-dim density contribution of the track
0136   /// @param mainGrid The main 1-dim density grid along the z-axis
0137   /// @param modifyModeSign Sign that determines the mode of modification,
0138   /// +1 for adding a track, -1 for removing a track
0139   void modifyMainGridWithTrackGrid(int zBin, const TrackGridVector& trkGrid,
0140                                    MainGridVector& mainGrid,
0141                                    int modifyModeSign) const;
0142 
0143   /// @brief Function that creates a 1-dim track grid (i.e. a vector)
0144   /// with the correct density contribution of a track along the z-axis
0145   ///
0146   /// @param d0 Transverse impact parameter
0147   /// @param distCtrZ The distance in z0 from the track position to its
0148   /// bin center in the 2-dim grid
0149   /// @param cov The track covariance matrix
0150   TrackGridVector createTrackGrid(float d0, float distCtrZ,
0151                                   const Acts::SquareMatrix2& cov) const;
0152 
0153   /// @brief Function that estimates the seed width based on the FWHM of
0154   /// the maximum density peak
0155   /// @note This only works if the maximum is sufficiently isolated since
0156   /// overlapping neighboring peaks might lead to an overestimation of the
0157   /// seed width.
0158   ///
0159   /// @param mainGrid The main 1-dim density grid along the z-axis
0160   /// @param maxZ z-position of the maximum density value
0161   ///
0162   /// @return The width
0163   Result<float> estimateSeedWidth(MainGridVector& mainGrid, float maxZ) const;
0164 
0165   /// @brief Helper to retrieve values according to a 2-dim normal distribution
0166   /// @note This function is defined in coordinate system centered around d0 and z0
0167   float normal2D(float d, float z, const SquareMatrix2& cov) const;
0168 
0169   /// @brief Checks the (up to) first three density maxima (only those that have
0170   /// a maximum relative deviation of 'relativeDensityDev' from the main
0171   /// maximum) and take the z-bin of the maximum with the highest surrounding
0172   /// density
0173   ///
0174   /// @param mainGrid The main 1-dim density grid along the z-axis
0175   ///
0176   /// @return The z-bin position
0177   int getHighestSumZPosition(MainGridVector& mainGrid) const;
0178 
0179   /// @brief Calculates the density sum of a z-bin and its two neighboring bins
0180   /// as needed for 'getHighestSumZPosition'
0181   ///
0182   /// @param mainGrid The main 1-dim density grid along the z-axis
0183   /// @param pos The center z-bin position
0184   ///
0185   /// @return The sum
0186   double getDensitySum(const MainGridVector& mainGrid, int pos) const;
0187 
0188   Config m_cfg;
0189 };
0190 
0191 }  // namespace Acts