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