|
||||
File indexing completed on 2025-01-18 09:11:00
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 /// This file implements the tools for a hough transform. 0010 0011 #pragma once 0012 0013 #include "Acts/Utilities/Delegate.hpp" 0014 #include "Acts/Utilities/Grid.hpp" 0015 #include "Acts/Utilities/Logger.hpp" 0016 #include "Acts/Utilities/Result.hpp" 0017 0018 #include <algorithm> 0019 #include <array> 0020 #include <map> 0021 #include <optional> 0022 #include <set> 0023 #include <span> 0024 #include <unordered_set> 0025 0026 namespace Acts::HoughTransformUtils { 0027 0028 /// this type is responsible for encoding the parameters of our hough space 0029 using CoordType = double; 0030 0031 // this type is used to encode hit counts. 0032 // Floating point to allow hit weights to be applied 0033 using YieldType = float; 0034 0035 /// @brief this function represents a mapping of a coordinate point in detector space to a line in 0036 /// hough space. Given the value of the first hough coordinate, it shall return 0037 /// the corresponding second coordinate according to the line parametrisation. 0038 /// Should be implemented by the user. 0039 /// @tparam PointType: The class representing a point in detector space (can differ between implementations) 0040 template <class PointType> 0041 using LineParametrisation = 0042 std::function<CoordType(CoordType, const PointType&)>; 0043 0044 /// @brief struct to define the ranges of the hough histogram. 0045 /// Used to move between parameter and bin index coordinates. 0046 /// Disconnected from the hough plane binning to be able to re-use 0047 /// a plane with a given binning for several parameter ranges 0048 struct HoughAxisRanges { 0049 CoordType xMin = 0.0f; // minimum value of the first coordinate 0050 CoordType xMax = 0.0f; // maximum value of the first coordinate 0051 CoordType yMin = 0.0f; // minimum value of the second coordinate 0052 CoordType yMax = 0.0f; // maximum value of the second coordinate 0053 }; 0054 0055 /// convenience functions to link bin indices to axis coordinates 0056 0057 /// @brief find the bin index corresponding to a certain abscissa 0058 /// of the coordinate axis, based on the axis limits and binning. 0059 /// @param min: Start of axis range 0060 /// @param max: End of axis range 0061 /// @param nSteps: Number of bins in axis 0062 /// @param val: value to find the corresponding bin for 0063 /// @return the bin number. 0064 /// No special logic to prevent over-/underflow, checking these is 0065 /// left to the caller 0066 inline int binIndex(double min, double max, unsigned nSteps, double val) { 0067 return static_cast<int>((val - min) / (max - min) * nSteps); 0068 } 0069 // Returns the lower bound of the bin specified by step 0070 /// @param min: Start of axis range 0071 /// @param max: End of axis range 0072 /// @param nSteps: Number of bins in axis 0073 /// @param binIndex: The index of the bin 0074 /// @return the parameter value at the lower bin edge. 0075 /// No special logic to prevent over-/underflow, checking these is 0076 /// left to the caller 0077 inline double lowerBinEdge(double min, double max, unsigned nSteps, 0078 std::size_t binIndex) { 0079 return min + (max - min) * binIndex / nSteps; 0080 } 0081 // Returns the lower bound of the bin specified by step 0082 /// @param min: Start of axis range 0083 /// @param max: End of axis range 0084 /// @param nSteps: Number of bins in axis 0085 /// @param binIndex: The index of the bin 0086 /// @return the parameter value at the bin center. 0087 /// No special logic to prevent over-/underflow, checking these is 0088 /// left to the caller 0089 inline double binCenter(double min, double max, unsigned nSteps, 0090 std::size_t binIndex) { 0091 return min + (max - min) * 0.5 * (2 * binIndex + 1) / nSteps; 0092 } 0093 0094 /// @brief data class for the information to store for each 0095 /// cell of the hough histogram. 0096 /// @tparam identifier_t: Type of the identifier to associate to the hits 0097 /// Should be sortable. Used to uniquely identify each 0098 /// hit and to eventually return the list of hits per cell 0099 template <class identifier_t> 0100 class HoughCell { 0101 public: 0102 /// @brief construct the cell as empty 0103 HoughCell() = default; 0104 /// @brief add an entry to this cell 0105 /// @param identifier: Identifier of the hit (used to distinguish hits from another) 0106 /// @param layer: Layer of the hit (used when counting layers) 0107 /// @param weight: Optional weight to assign to the hit 0108 void fill(const identifier_t& identifier, unsigned int layer, 0109 YieldType weight = 1.); 0110 /// @brief access the number of layers with hits compatible with this cell 0111 YieldType nLayers() const { return m_nLayers; } 0112 /// @brief access the number of unique hits compatible with this cell 0113 YieldType nHits() const { return m_nHits; } 0114 /// @brief access the span of layers compatible with this cell 0115 std::span<const unsigned, std::dynamic_extent> getLayers() const; 0116 // /// @brief access the pan of unique hits compatible with this cell 0117 std::span<const identifier_t, std::dynamic_extent> getHits() const; 0118 0119 /// @brief reset this cell, removing any existing content. 0120 void reset(); 0121 0122 private: 0123 /// (weighted) number of layers with hits on this cell 0124 YieldType m_nLayers{0}; 0125 /// (weighted) number of unique hits on this cell 0126 YieldType m_nHits{0}; 0127 0128 /// index for the hits -- keeps track of vector's size 0129 std::size_t m_iHit{0}; 0130 /// index for the layers -- keeps track of vector's size 0131 std::size_t m_iLayer{0}; 0132 0133 /// a batch to resize the vector of the hits or the layers 0134 std::size_t m_assignBatch{20}; 0135 0136 /// vector of layers with hits on this cell 0137 std::vector<unsigned> m_layers{std::vector<unsigned>(m_assignBatch)}; 0138 0139 /// vector of hits on this cell 0140 std::vector<identifier_t> m_hits{std::vector<identifier_t>(m_assignBatch)}; 0141 }; 0142 0143 /// @brief Configuration - number of bins in each axis. 0144 /// The Hough plane is agnostic of how the bins map to 0145 /// coordinates, allowing to re-use a plane for several 0146 /// (sub) detectors of different dimensions if the bin number 0147 /// remains applicable 0148 struct HoughPlaneConfig { 0149 std::size_t nBinsX = 0; // number of bins in the first coordinate 0150 std::size_t nBinsY = 0; // number of bins in the second coordinate 0151 }; 0152 0153 /// @brief Representation of the hough plane - the histogram used 0154 /// for the hough transform with methods to fill and evaluate 0155 /// the histogram. Templated to a class used as identifier for the hits 0156 template <class identifier_t> 0157 class HoughPlane { 0158 public: 0159 /// @brief hough histogram representation as a 2D-indexable vector of hough cells 0160 using Axis = 0161 Acts::Axis<Acts::AxisType::Equidistant, Acts::AxisBoundaryType::Bound>; 0162 using HoughHist = Grid<HoughCell<identifier_t>, Axis, Axis>; 0163 using Index = typename HoughHist::index_t; 0164 0165 /// @brief instantiate the (empty) hough plane 0166 /// @param cfg: configuration 0167 HoughPlane(const HoughPlaneConfig& cfg); 0168 0169 /// fill and reset methods to modify the grid content 0170 0171 /// @brief add one measurement to the hough plane 0172 /// @tparam PointType: Type of the objects to use when adding measurements (e.g. experiment EDM object) 0173 /// @param measurement: The measurement to add 0174 /// @param axisRanges: Ranges of the hough axes, used to map the bin numbers to parameter values 0175 /// @param linePar: The function y(x) parametrising the hough space line for a given measurement 0176 /// @param widthPar: The function dy(x) parametrising the width of the y(x) curve 0177 /// for a given measurement 0178 /// @param identifier: The unique identifier for the given hit 0179 /// @param layer: A layer index for this hit 0180 /// @param weight: An optional weight to assign to this hit 0181 template <class PointType> 0182 void fill(const PointType& measurement, const HoughAxisRanges& axisRanges, 0183 LineParametrisation<PointType> linePar, 0184 LineParametrisation<PointType> widthPar, 0185 const identifier_t& identifier, unsigned layer = 0, 0186 YieldType weight = 1.0f); 0187 /// @brief resets the contents of the grid. Can be used to avoid reallocating the histogram 0188 /// when switching regions / (sub)detectors 0189 void reset(); 0190 0191 //// user-facing accessors 0192 0193 /// @brief get the layers with hits in one cell of the histogram 0194 /// @param xBin: bin index in the first coordinate 0195 /// @param yBin: bin index in the second coordinate 0196 /// @return the set of layer indices that have hits for this cell 0197 std::unordered_set<unsigned> layers(std::size_t xBin, 0198 std::size_t yBin) const { 0199 return m_houghHist.atLocalBins({xBin, yBin}).layers(); 0200 } 0201 0202 /// @brief get the (weighted) number of layers with hits in one cell of the histogram 0203 /// @param xBin: bin index in the first coordinate 0204 /// @param yBin: bin index in the second coordinate 0205 /// @return the (weighed) number of layers that have hits for this cell 0206 YieldType nLayers(std::size_t xBin, std::size_t yBin) const { 0207 return m_houghHist.atLocalBins({xBin, yBin}).nLayers(); 0208 } 0209 0210 /// @brief get the identifiers of all hits in one cell of the histogram 0211 /// @param xBin: bin index in the first coordinate 0212 /// @param yBin: bin index in the second coordinate 0213 /// @return the set of identifiers of the hits for this cell 0214 std::unordered_set<identifier_t> hitIds(std::size_t xBin, 0215 std::size_t yBin) const { 0216 const auto hits_span = m_houghHist.atLocalBins({xBin, yBin}).getHits(); 0217 0218 return std::unordered_set<identifier_t>(hits_span.begin(), hits_span.end()); 0219 } 0220 /// @brief access the (weighted) number of hits in one cell of the histogram from bin's coordinates 0221 /// @param xBin: bin index in the first coordinate 0222 /// @param yBin: bin index in the second coordinate 0223 /// @return the (weighted) number of hits for this cell 0224 YieldType nHits(std::size_t xBin, std::size_t yBin) const { 0225 return m_houghHist.atLocalBins({xBin, yBin}).nHits(); 0226 } 0227 0228 /// @brief access the (weighted) number of hits in one cell of the histogram from globalBin index 0229 /// @param globalBin: global bin index 0230 /// @return the (weighted) number of hits for this cell 0231 YieldType nHits(std::size_t globalBin) const { 0232 return m_houghHist.at(globalBin).nHits(); 0233 } 0234 0235 /// @brief get the number of bins on the first coordinate 0236 std::size_t nBinsX() const { return m_cfg.nBinsX; } 0237 /// @brief get the number of bins on the second coordinate 0238 std::size_t nBinsY() const { return m_cfg.nBinsY; } 0239 0240 /// @brief get the maximum number of (weighted) hits seen in a single 0241 /// cell across the entire histrogram. 0242 YieldType maxHits() const { return m_maxHits; } 0243 0244 /// @brief get the list of cells with non-zero content. 0245 /// Useful for peak-finders in sparse data 0246 /// to avoid looping over all cells 0247 const std::unordered_set<std::size_t>& getNonEmptyBins() const { 0248 return m_touchedBins; 0249 } 0250 0251 /// @brief get the coordinates of the bin given the global bin index 0252 Index axisBins(std::size_t globalBin) const { 0253 return m_houghHist.localBinsFromGlobalBin(globalBin); 0254 } 0255 0256 /// @brief get the globalBin index given the coordinates of the bin 0257 std::size_t globalBin(Index indexBin) const { 0258 return m_houghHist.globalBinFromLocalBins(indexBin); 0259 } 0260 0261 /// @brief get the bin indices of the cell containing the largest number 0262 /// of (weighted) hits across the entire histogram 0263 std::pair<std::size_t, std::size_t> locMaxHits() const { 0264 return m_maxLocHits; 0265 } 0266 0267 /// @brief get the maximum number of (weighted) layers with hits seen 0268 /// in a single cell across the entire histrogram. 0269 YieldType maxLayers() const { return m_maxLayers; } 0270 0271 /// @brief get the bin indices of the cell containing the largest number 0272 /// of (weighted) layers with hits across the entire histogram 0273 std::pair<std::size_t, std::size_t> locMaxLayers() const { 0274 return m_maxLocLayers; 0275 } 0276 0277 private: 0278 /// @brief Helper method to fill a bin of the hough histogram. 0279 /// Updates the internal helper data structures (maximum tracker etc). 0280 /// @param binX: bin number along x 0281 /// @param binY: bin number along y 0282 /// @param identifier: hit identifier 0283 /// @param layer: layer index 0284 /// @param w: optional hit weight 0285 void fillBin(std::size_t binX, std::size_t binY, 0286 const identifier_t& identifier, unsigned layer, double w = 1.0f); 0287 0288 YieldType m_maxHits = 0.0f; // track the maximum number of hits seen 0289 YieldType m_maxLayers = 0.0f; // track the maximum number of layers seen 0290 0291 /// track the location of the maximum in hits 0292 std::pair<std::size_t, std::size_t> m_maxLocHits = {0, 0}; 0293 /// track the location of the maximum in layers 0294 std::pair<std::size_t, std::size_t> m_maxLocLayers = {0, 0}; 0295 0296 std::size_t m_assignBatch{20}; 0297 0298 /// track the bins with non-trivial content 0299 std::unordered_set<std::size_t> m_touchedBins{}; 0300 0301 std::size_t m_iBin = 0; 0302 0303 HoughPlaneConfig m_cfg; // the configuration object 0304 HoughHist m_houghHist; // the histogram data object 0305 }; 0306 0307 /// example peak finders. 0308 namespace PeakFinders { 0309 /// configuration for the LayerGuidedCombinatoric peak finder 0310 struct LayerGuidedCombinatoricConfig { 0311 YieldType threshold = 3.0f; // min number of layers to obtain a maximum 0312 int localMaxWindowSize = 0; // Only create candidates from a local maximum 0313 }; 0314 0315 /// @brief Peak finder inspired by ATLAS ITk event filter developments. 0316 /// Builds peaks based on layer counts and allows for subsequent resolution 0317 /// of the combinatorics by building multiple candidates per peak if needed. 0318 /// In flat regions, peak positions are moved towards smaller values of the 0319 /// second and first coordinate. 0320 /// @tparam identifier_t: The identifier type to use. Should match the one used in the hough plane. 0321 template <class identifier_t> 0322 class LayerGuidedCombinatoric { 0323 public: 0324 /// @brief data class representing the found maxima. 0325 /// Here, we just have a list of cluster identifiers 0326 struct Maximum { 0327 std::unordered_set<identifier_t> hitIdentifiers = 0328 {}; // identifiers of contributing hits 0329 }; 0330 /// @brief constructor 0331 /// @param cfg: Configuration object 0332 LayerGuidedCombinatoric(const LayerGuidedCombinatoricConfig& cfg); 0333 0334 /// @brief main peak finder method. 0335 /// @param plane: Filled hough plane to search 0336 /// @return vector of found maxima 0337 std::vector<Maximum> findPeaks(const HoughPlane<identifier_t>& plane) const; 0338 0339 private: 0340 /// @brief check if a given bin is a local maximum. 0341 /// @param plane: The filled hough plane 0342 /// @param xBin: x bin index 0343 /// @param yBin: y bin index 0344 /// @return true if a maximum, false otherwise 0345 bool passThreshold(const HoughPlane<identifier_t>& plane, std::size_t xBin, 0346 std::size_t yBin) const; // did we pass extensions? 0347 0348 LayerGuidedCombinatoricConfig m_cfg; // configuration data object 0349 }; 0350 /// @brief Configuration for the IslandsAroundMax peak finder 0351 struct IslandsAroundMaxConfig { 0352 YieldType threshold = 0353 3.0f; // min number of weigted hits required in a maximum 0354 YieldType fractionCutoff = 0355 0; // Fraction of the global maximum at which to cut off maxima 0356 std::pair<CoordType, CoordType> minSpacingBetweenPeaks = { 0357 0.0f, 0.0f}; // minimum distance of a new peak from existing peaks in 0358 // parameter space 0359 }; 0360 /// @brief Peak finder inspired by ATLAS muon reconstruction. 0361 /// Looks for regions above a given fraction of the global maximum 0362 /// hit count and connects them into islands comprising adjacent bins 0363 /// above the threshold. Peak positions are averaged across cells in the island, 0364 /// weighted by hit counts 0365 /// @tparam identifier_t: The identifier type to use. Should match the one used in the hough plane. 0366 template <class identifier_t> 0367 class IslandsAroundMax { 0368 public: 0369 /// @brief data struct representing a local maximum. 0370 /// Comes with a position estimate and a list of hits within the island 0371 struct Maximum { 0372 CoordType x = 0; // x value of the maximum 0373 CoordType y = 0; // y value of the maximum 0374 CoordType wx = 0; // x width of the maximum 0375 CoordType wy = 0; // y width of the maximum 0376 std::unordered_set<identifier_t> hitIdentifiers = 0377 {}; // identifiers of contributing hits 0378 }; 0379 /// @brief constructor. 0380 /// @param cfg: configuration object 0381 IslandsAroundMax(const IslandsAroundMaxConfig& cfg); 0382 0383 /// @brief main peak finder method. 0384 /// @param plane: The filled hough plane to search 0385 /// @param ranges: The axis ranges used for mapping between parameter space and bins. 0386 /// @return List of the found maxima 0387 std::vector<Maximum> findPeaks(const HoughPlane<identifier_t>& plane, 0388 const HoughAxisRanges& ranges); 0389 0390 private: 0391 /// @brief method to incrementally grow an island by adding adjacent cells 0392 /// Performs a breadth-first search for neighbours above threshold and adds 0393 /// them to candidate. Stops when no suitable neighbours are left. 0394 /// @param houghPlane: The current hough Plane we are looking for maxima 0395 /// @param inMaximum: List of cells found in the island. Incrementally populated by calls to the method 0396 /// @param toExplore: List of the global Bin indices of neighbour cell candidates left to explore. Method will not do anything once this is empty 0397 /// @param threshold: the threshold to apply to check if a cell should be added to an island 0398 /// @param yieldMap: A map of the hit content of above-threshold cells. Used cells will be set to empty content to avoid re-use by subsequent calls 0399 void extendMaximum(const HoughPlane<identifier_t>& houghPlane, 0400 std::vector<std::array<std::size_t, 2>>& inMaximum, 0401 std::vector<std::size_t>& toExplore, YieldType threshold, 0402 std::unordered_map<std::size_t, YieldType>& yieldMap); 0403 0404 IslandsAroundMaxConfig m_cfg; // configuration data object 0405 0406 /// @brief array of steps to consider when exploring neighbouring cells. 0407 const std::array<std::pair<int, int>, 8> m_stepDirections{ 0408 std::make_pair(-1, -1), std::make_pair(0, -1), std::make_pair(1, -1), 0409 std::make_pair(-1, 0), std::make_pair(1, 0), std::make_pair(-1, 1), 0410 std::make_pair(0, 1), std::make_pair(1, 1)}; 0411 }; 0412 } // namespace PeakFinders 0413 } // namespace Acts::HoughTransformUtils 0414 0415 #include "HoughTransformUtils.ipp"
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |