Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-23 07:47:18

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