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