Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-12 07:51:49

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     bool useTime = false;
0075 
0076     /// Do NOT use just the z-bin with the highest
0077     /// track density, but instead check (up to)
0078     /// first three density maxima (only those that have
0079     /// a maximum relative deviation of 'relativeDensityDev'
0080     /// from the main maximum) and take the z-bin of the
0081     /// maximum with the highest surrounding density sum
0082     bool useHighestSumZPosition = false;
0083 
0084     /// The maximum relative density deviation from the main
0085     /// maximum to consider the second and third maximum for
0086     /// the highest-sum approach from above
0087     double maxRelativeDensityDev = 0.01;
0088   };
0089 
0090   /// Constructor
0091   explicit AdaptiveGridTrackDensity(const Config& cfg);
0092 
0093   /// @brief Returns the z and t coordinate of maximum (surrounding)
0094   /// track density
0095   /// @note if time vertex seeding is not enabled, the t coordinate
0096   /// will be set to 0.
0097   ///
0098   /// @param densityMap Map between bins and corresponding density
0099   /// values
0100   ///
0101   /// @return The z and t coordinates of maximum track density
0102   Result<ZTPosition> getMaxZTPosition(DensityMap& densityMap) const;
0103 
0104   /// @brief Returns the z-t position of maximum track density
0105   /// and the estimated z-width of the maximum
0106   ///
0107   /// @param densityMap Map between bins and corresponding density
0108   /// values
0109   ///
0110   /// @return The z-t position of the maximum track density and
0111   /// its width
0112   Result<ZTPositionAndWidth> getMaxZTPositionAndWidth(
0113       DensityMap& densityMap) const;
0114 
0115   /// @brief Adds a single track to the overall grid density
0116   ///
0117   /// @param trk The track to be added
0118   /// @param mainDensityMap Map between bins and corresponding density
0119   ///
0120   /// @return The density map of the track that was added
0121   DensityMap addTrack(const BoundTrackParameters& trk,
0122                       DensityMap& mainDensityMap) const;
0123 
0124   /// @brief Removes a track from the overall grid density.
0125   ///
0126   /// @param trackDensityMap Map between bins and corresponding density
0127   /// @note The track density comes from a single track
0128   /// @param mainDensityMap Map between bins and corresponding density
0129   /// @note The track density comes from an arbitrary number of tracks
0130   void subtractTrack(const DensityMap& trackDensityMap,
0131                      DensityMap& mainDensityMap) const;
0132 
0133   // TODO this should not be public
0134   /// @brief Calculates the bin center from the bin number
0135   /// @param bin Bin number
0136   /// @param binExtent Bin extent
0137   /// @return Bin center
0138   static double getBinCenter(std::int32_t bin, double binExtent);
0139 
0140  private:
0141   Config m_cfg;
0142 
0143   /// @brief Calculates the bin number corresponding to a d, z, or time value
0144   /// @param value d, z, or time value
0145   /// @param binExtent Bin extent
0146   /// @return Bin number
0147   static std::int32_t getBin(double value, double binExtent);
0148 
0149   /// @brief Calculates the grid size in z or time direction
0150   /// @param sigma Standard deviation of the track density
0151   /// @param trkSigmas Number of standard deviations that the grid
0152   ///        covers in z or time direction
0153   /// @param binExtent Bin extent
0154   /// @param trkGridSizeRange Optional minimal and maximal number of bins
0155   ///        in z or time direction
0156   /// @return Grid size
0157   static std::uint32_t getTrkGridSize(double sigma, double trkSigmas,
0158                                       double binExtent,
0159                                       const GridSizeRange& trkGridSizeRange);
0160 
0161   /// @brief Calculates the bin number corresponding to a z value
0162   /// @param value z value
0163   /// @return Bin number
0164   std::int32_t getSpatialBin(double value) const;
0165   /// @brief Calculates the bin number corresponding to a time value
0166   /// @param value Time value
0167   /// @return Bin number
0168   std::int32_t getTemporalBin(double value) const;
0169 
0170   /// @brief Calculates the spatial bin center corresponding to a bin number
0171   /// @param bin Bin number
0172   /// @return Bin center
0173   double getSpatialBinCenter(std::int32_t bin) const;
0174   /// @brief Calculates the temporal bin center corresponding to a bin number
0175   /// @param bin Bin number
0176   /// @return Bin center
0177   double getTemporalBinCenter(std::int32_t bin) const;
0178 
0179   /// @brief Calculates the grid size in z direction
0180   /// @param sigma Standard deviation of the track density
0181   /// @return Grid size
0182   std::uint32_t getSpatialTrkGridSize(double sigma) const;
0183   /// @brief Calculates the grid size in time direction
0184   /// @param sigma Standard deviation of the track density
0185   /// @return Grid size
0186   std::uint32_t getTemporalTrkGridSize(double sigma) const;
0187 
0188   /// @brief Finds the maximum density of a DensityMap
0189   /// @param densityMap Map between bins and corresponding density
0190   /// values
0191   /// @return Iterator of the map entry with the highest density
0192   DensityMap::const_iterator highestDensityEntry(
0193       const DensityMap& densityMap) const;
0194 
0195   /// @brief Function that creates a track density map, i.e., a map from bins
0196   /// to the corresponding density values for a single track.
0197   ///
0198   /// @param impactParams vector containing d0, z0, and t0 of the track
0199   /// @param centralBin Central z and t bin of the track (where its
0200   /// density is the highest)
0201   /// @param cov 3x3 impact parameter covariance matrix
0202   /// @param spatialTrkGridSize Number of bins in z direction
0203   /// @param temporalTrkGridSize Number of bins in time direction
0204   ///
0205   /// @return The track density map
0206   DensityMap createTrackGrid(const Acts::Vector3& impactParams,
0207                              const Bin& centralBin,
0208                              const Acts::SquareMatrix3& cov,
0209                              std::uint32_t spatialTrkGridSize,
0210                              std::uint32_t temporalTrkGridSize) const;
0211 
0212   /// @brief Function that estimates the seed width in z direction based
0213   /// on the full width at half maximum (FWHM) of the maximum density peak
0214   /// @note This only works if the maximum is sufficiently isolated since
0215   /// overlapping neighboring peaks might lead to an overestimation of the
0216   /// seed width.
0217   ///
0218   /// @param densityMap Map from bins to corresponding track density
0219   /// @param maxZT z-t position of the maximum density value
0220   ///
0221   /// @return The width
0222   Result<double> estimateSeedWidth(const DensityMap& densityMap,
0223                                    const ZTPosition& maxZT) const;
0224 
0225   /// @brief Checks (up to) first three density maxima that have a
0226   /// maximum relative deviation of 'relativeDensityDev' from the
0227   /// global maximum. Returns the bin of the maximum that has the
0228   /// highest surrounding density in z direction.
0229   ///
0230   /// @param densityMap Map between bins and corresponding density values
0231   ///
0232   /// @return The bin corresponding to the highest surrounding density
0233   Bin highestDensitySumBin(DensityMap& densityMap) const;
0234 
0235   /// @brief Calculates the density sum of a bin and its two neighboring bins
0236   /// in z direction
0237   ///
0238   /// @param densityMap Map between bins and corresponding density values
0239   /// @param bin Bin whose neighbors in z we want to sum up
0240   ///
0241   /// @return The density sum
0242   double getDensitySum(const DensityMap& densityMap, const Bin& bin) const;
0243 };
0244 
0245 }  // namespace Acts