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/Definitions/Algebra.hpp"
0012 #include "Acts/EventData/TrackParameters.hpp"
0013 #include "Acts/Utilities/Result.hpp"
0014 
0015 #include <boost/container/flat_map.hpp>  // TODO use flat unordered map
0016 #include <boost/functional/hash.hpp>
0017 
0018 namespace Acts {
0019 
0020 /// @class AdaptiveGridTrackDensity
0021 /// @brief Implements a 1D (no time seeding) / 2D (time seeding)
0022 /// grid that is filled with track densities.
0023 /// Each track is modelled by a 2D / 3D Gaussian distribution in the
0024 /// d0-z0 / d0-z0-t0 plane, which is evaluated at d0=0. Therefore,
0025 /// each track effectively lives in 1D / 2D.
0026 /// The position of the highest track density (of either a single
0027 /// bin or the sum of a certain region) can be determined.
0028 /// Single tracks can be cached and removed from the overall density.
0029 /// Unlike in the GaussianGridTrackDensity, the overall density map
0030 /// grows adaptively when tracks densities are added to the grid.
0031 class AdaptiveGridTrackDensity {
0032  public:
0033   /// The first (second) integer indicates the bin's z (t) position
0034   using Bin = std::pair<std::int32_t, std::int32_t>;
0035   /// Mapping between bins and track densities
0036   using DensityMap = boost::container::flat_map<Bin, float>;
0037   /// Coordinates in the z-t plane; the t value will be set to 0 if time
0038   /// vertex seeding is disabled
0039   using ZTPosition = std::pair<double, double>;
0040   /// z-t position of a maximum and its width
0041   using ZTPositionAndWidth = std::pair<ZTPosition, double>;
0042   /// Optional grid size range
0043   using GridSizeRange =
0044       std::pair<std::optional<std::uint32_t>, std::optional<std::uint32_t>>;
0045 
0046   /// The configuration struct
0047   struct Config {
0048     /// Spatial extent of a bin in d0 and z0 direction, should always be set to
0049     /// a positive value
0050     double spatialBinExtent = 15 * UnitConstants::um;
0051 
0052     /// Number of standard deviations that the grid covers in z direction
0053     double nSpatialTrkSigmas = 3.0;
0054 
0055     /// Temporal extent of a bin, not used if useTime == true
0056     double temporalBinExtent = 19 * UnitConstants::mm;
0057 
0058     /// Number of standard deviations that the grid covers in t direction, not
0059     /// used if useTime == true
0060     double nTemporalTrkSigmas = 3.0;
0061 
0062     /// Spatial window for filling the density map
0063     std::pair<double, double> spatialWindow = {-250 * UnitConstants::mm,
0064                                                250 * UnitConstants::mm};
0065     /// Temporal window for filling the density map
0066     std::pair<double, double> temporalWindow = {-10 * UnitConstants::ns,
0067                                                 10 * UnitConstants::ns};
0068 
0069     /// Optional minimal and maximal number of bins in z direction
0070     GridSizeRange spatialTrkGridSizeRange = {std::nullopt, std::nullopt};
0071     /// Optional minimal and maximal number of bins in time direction
0072     GridSizeRange temporalTrkGridSizeRange = {std::nullopt, std::nullopt};
0073 
0074     /// Flag indicating whether to use time information in track density
0075     /// calculation
0076     bool useTime = false;
0077 
0078     /// Do NOT use just the z-bin with the highest
0079     /// track density, but instead check (up to)
0080     /// first three density maxima (only those that have
0081     /// a maximum relative deviation of 'relativeDensityDev'
0082     /// from the main maximum) and take the z-bin of the
0083     /// maximum with the highest surrounding density sum
0084     bool useHighestSumZPosition = false;
0085 
0086     /// The maximum relative density deviation from the main
0087     /// maximum to consider the second and third maximum for
0088     /// the highest-sum approach from above
0089     double maxRelativeDensityDev = 0.01;
0090   };
0091 
0092   /// Constructor
0093   /// @param cfg The configuration parameters
0094   explicit AdaptiveGridTrackDensity(const Config& cfg);
0095 
0096   /// @brief Returns the z and t coordinate of maximum (surrounding)
0097   /// track density
0098   /// @note if time vertex seeding is not enabled, the t coordinate
0099   /// will be set to 0.
0100   ///
0101   /// @param densityMap Map between bins and corresponding density
0102   /// values
0103   ///
0104   /// @return The z and t coordinates of maximum track density
0105   Result<ZTPosition> getMaxZTPosition(DensityMap& densityMap) const;
0106 
0107   /// @brief Returns the z-t position of maximum track density
0108   /// and the estimated z-width of the maximum
0109   ///
0110   /// @param densityMap Map between bins and corresponding density
0111   /// values
0112   ///
0113   /// @return The z-t position of the maximum track density and
0114   /// its width
0115   Result<ZTPositionAndWidth> getMaxZTPositionAndWidth(
0116       DensityMap& densityMap) const;
0117 
0118   /// @brief Adds a single track to the overall grid density
0119   ///
0120   /// @param trk The track to be added
0121   /// @param mainDensityMap Map between bins and corresponding density
0122   ///
0123   /// @return The density map of the track that was added
0124   DensityMap addTrack(const BoundTrackParameters& trk,
0125                       DensityMap& mainDensityMap) const;
0126 
0127   /// @brief Removes a track from the overall grid density.
0128   ///
0129   /// @param trackDensityMap Map between bins and corresponding density
0130   /// @note The track density comes from a single track
0131   /// @param mainDensityMap Map between bins and corresponding density
0132   /// @note The track density comes from an arbitrary number of tracks
0133   void subtractTrack(const DensityMap& trackDensityMap,
0134                      DensityMap& mainDensityMap) const;
0135 
0136   // TODO this should not be public
0137   /// @brief Calculates the bin center from the bin number
0138   /// @param bin Bin number
0139   /// @param binExtent Bin extent
0140   /// @return Bin center
0141   static double getBinCenter(std::int32_t bin, double binExtent);
0142 
0143  private:
0144   Config m_cfg;
0145 
0146   /// @brief Calculates the bin number corresponding to a d, z, or time value
0147   /// @param value d, z, or time value
0148   /// @param binExtent Bin extent
0149   /// @return Bin number
0150   static std::int32_t getBin(double value, double binExtent);
0151 
0152   /// @brief Calculates the grid size in z or time direction
0153   /// @param sigma Standard deviation of the track density
0154   /// @param trkSigmas Number of standard deviations that the grid
0155   ///        covers in z or time direction
0156   /// @param binExtent Bin extent
0157   /// @param trkGridSizeRange Optional minimal and maximal number of bins
0158   ///        in z or time direction
0159   /// @return Grid size
0160   static std::uint32_t getTrkGridSize(double sigma, double trkSigmas,
0161                                       double binExtent,
0162                                       const GridSizeRange& trkGridSizeRange);
0163 
0164   /// @brief Calculates the bin number corresponding to a z value
0165   /// @param value z value
0166   /// @return Bin number
0167   std::int32_t getSpatialBin(double value) const;
0168   /// @brief Calculates the bin number corresponding to a time value
0169   /// @param value Time value
0170   /// @return Bin number
0171   std::int32_t getTemporalBin(double value) const;
0172 
0173   /// @brief Calculates the spatial bin center corresponding to a bin number
0174   /// @param bin Bin number
0175   /// @return Bin center
0176   double getSpatialBinCenter(std::int32_t bin) const;
0177   /// @brief Calculates the temporal bin center corresponding to a bin number
0178   /// @param bin Bin number
0179   /// @return Bin center
0180   double getTemporalBinCenter(std::int32_t bin) const;
0181 
0182   /// @brief Calculates the grid size in z direction
0183   /// @param sigma Standard deviation of the track density
0184   /// @return Grid size
0185   std::uint32_t getSpatialTrkGridSize(double sigma) const;
0186   /// @brief Calculates the grid size in time direction
0187   /// @param sigma Standard deviation of the track density
0188   /// @return Grid size
0189   std::uint32_t getTemporalTrkGridSize(double sigma) const;
0190 
0191   /// @brief Finds the maximum density of a DensityMap
0192   /// @param densityMap Map between bins and corresponding density
0193   /// values
0194   /// @return Iterator of the map entry with the highest density
0195   DensityMap::const_iterator highestDensityEntry(
0196       const DensityMap& densityMap) const;
0197 
0198   /// @brief Function that creates a track density map, i.e., a map from bins
0199   /// to the corresponding density values for a single track.
0200   ///
0201   /// @param impactParams vector containing d0, z0, and t0 of the track
0202   /// @param centralBin Central z and t bin of the track (where its
0203   /// density is the highest)
0204   /// @param cov 3x3 impact parameter covariance matrix
0205   /// @param spatialTrkGridSize Number of bins in z direction
0206   /// @param temporalTrkGridSize Number of bins in time direction
0207   ///
0208   /// @return The track density map
0209   DensityMap createTrackGrid(const Acts::Vector3& impactParams,
0210                              const Bin& centralBin,
0211                              const Acts::SquareMatrix3& cov,
0212                              std::uint32_t spatialTrkGridSize,
0213                              std::uint32_t temporalTrkGridSize) const;
0214 
0215   /// @brief Function that estimates the seed width in z direction based
0216   /// on the full width at half maximum (FWHM) of the maximum density peak
0217   /// @note This only works if the maximum is sufficiently isolated since
0218   /// overlapping neighboring peaks might lead to an overestimation of the
0219   /// seed width.
0220   ///
0221   /// @param densityMap Map from bins to corresponding track density
0222   /// @param maxZT z-t position of the maximum density value
0223   ///
0224   /// @return The width
0225   Result<double> estimateSeedWidth(const DensityMap& densityMap,
0226                                    const ZTPosition& maxZT) const;
0227 
0228   /// @brief Checks (up to) first three density maxima that have a
0229   /// maximum relative deviation of 'relativeDensityDev' from the
0230   /// global maximum. Returns the bin of the maximum that has the
0231   /// highest surrounding density in z direction.
0232   ///
0233   /// @param densityMap Map between bins and corresponding density values
0234   ///
0235   /// @return The bin corresponding to the highest surrounding density
0236   Bin highestDensitySumBin(DensityMap& densityMap) const;
0237 
0238   /// @brief Calculates the density sum of a bin and its two neighboring bins
0239   /// in z direction
0240   ///
0241   /// @param densityMap Map between bins and corresponding density values
0242   /// @param bin Bin whose neighbors in z we want to sum up
0243   ///
0244   /// @return The density sum
0245   double getDensitySum(const DensityMap& densityMap, const Bin& bin) const;
0246 };
0247 
0248 }  // namespace Acts