CMS 3D CMS Logo

HGCalLayerTiles.h
Go to the documentation of this file.
1 // Authors: Felice Pantaleo - felice.pantaleo@cern.ch
2 // Date: 03/2019
3 
4 #ifndef RecoLocalCalo_HGCalRecProducers_HGCalLayerTiles_h
5 #define RecoLocalCalo_HGCalRecProducers_HGCalLayerTiles_h
6 
10 
11 #include <vector>
12 #include <array>
13 #include <cmath>
14 #include <algorithm>
15 #include <cassert>
16 
17 template <typename T>
19 public:
20  typedef T type;
21  void fill(const std::vector<float>& x,
22  const std::vector<float>& y,
23  const std::vector<float>& eta,
24  const std::vector<float>& phi,
25  const std::vector<bool>& isSi) {
26  auto cellsSize = x.size();
27  for (unsigned int i = 0; i < cellsSize; ++i) {
28  tiles_[getGlobalBin(x[i], y[i])].push_back(i);
29  if (!isSi[i]) {
30  tiles_[getGlobalBinEtaPhi(eta[i], phi[i])].push_back(i);
31  }
32  }
33  }
34 
35  int getXBin(float x) const {
36  constexpr float xRange = T::maxX - T::minX;
37  static_assert(xRange >= 0.);
38  constexpr float r = T::nColumns / xRange;
39  int xBin = (x - T::minX) * r;
40  xBin = std::clamp(xBin, 0, T::nColumns - 1);
41  return xBin;
42  }
43 
44  int getYBin(float y) const {
45  constexpr float yRange = T::maxY - T::minY;
46  static_assert(yRange >= 0.);
47  constexpr float r = T::nRows / yRange;
48  int yBin = (y - T::minY) * r;
49  yBin = std::clamp(yBin, 0, T::nRows - 1);
50  return yBin;
51  }
52 
53  int getEtaBin(float eta) const {
54  constexpr float etaRange = T::maxEta - T::minEta;
55  static_assert(etaRange >= 0.);
56  constexpr float r = T::nColumnsEta / etaRange;
57  int etaBin = (eta - T::minEta) * r;
58  etaBin = std::clamp(etaBin, 0, T::nColumnsEta - 1);
59  return etaBin;
60  }
61 
62  int getPhiBin(float phi) const {
63  auto normPhi = normalizedPhi(phi);
64  constexpr float r = T::nRowsPhi * M_1_PI * 0.5f;
65  int phiBin = (normPhi + M_PI) * r;
66  return phiBin;
67  }
68 
71 
72  int getGlobalBin(float x, float y) const { return getXBin(x) + getYBin(y) * T::nColumns; }
73 
74  int getGlobalBinByBin(int xBin, int yBin) const { return xBin + yBin * T::nColumns; }
75 
76  int getGlobalBinEtaPhi(float eta, float phi) const {
77  return T::nColumns * T::nRows + getEtaBin(eta) + getPhiBin(phi) * T::nColumnsEta;
78  }
79 
80  int getGlobalBinByBinEtaPhi(int etaBin, int phiBin) const {
81  return T::nColumns * T::nRows + etaBin + phiBin * T::nColumnsEta;
82  }
83 
84  std::array<int, 4> searchBox(float xMin, float xMax, float yMin, float yMax) const {
85  int xBinMin = getXBin(xMin);
86  int xBinMax = getXBin(xMax);
87  int yBinMin = getYBin(yMin);
88  int yBinMax = getYBin(yMax);
89  return std::array<int, 4>({{xBinMin, xBinMax, yBinMin, yBinMax}});
90  }
91 
92  std::array<int, 4> searchBoxEtaPhi(float etaMin, float etaMax, float phiMin, float phiMax) const {
93  if (etaMax - etaMin < 0) {
94  return std::array<int, 4>({{0, 0, 0, 0}});
95  }
96  int etaBinMin = getEtaBin(etaMin);
97  int etaBinMax = getEtaBin(etaMax);
98  int phiBinMin = getPhiBin(phiMin);
99  int phiBinMax = getPhiBin(phiMax);
100  // If the search window cross the phi-bin boundary, add T::nPhiBins to the
101  // MAx value. This guarantees that the caller can perform a valid doule
102  // loop on eta and phi. It is the caller responsibility to perform a module
103  // operation on the phiBin values returned by this function, to explore the
104  // correct bins.
105  if (phiBinMax < phiBinMin) {
106  phiBinMax += T::nRowsPhi;
107  }
108 
109  return std::array<int, 4>({{etaBinMin, etaBinMax, phiBinMin, phiBinMax}});
110  }
111 
112  void clear() {
113  for (auto& t : tiles_)
114  t.clear();
115  }
116 
117  const std::vector<int>& operator[](int globalBinId) const { return tiles_[globalBinId]; }
118 
119 private:
120  std::array<std::vector<int>, T::nTiles> tiles_;
121 };
122 
125 #endif
int getGlobalBinByBin(int xBin, int yBin) const
int getGlobalBinByBinEtaPhi(int etaBin, int phiBin) const
constexpr T normalizedPhi(T phi)
Definition: normalizedPhi.h:8
const std::vector< int > & operator[](int globalBinId) const
int getXBin(float x) const
int getEtaBin(float eta) const
int getYBin(float y) const
std::array< std::vector< int >, T::nTiles > tiles_
#define M_PI
std::array< int, 4 > searchBox(float xMin, float xMax, float yMin, float yMax) const
void fill(const std::vector< float > &x, const std::vector< float > &y, const std::vector< float > &eta, const std::vector< float > &phi, const std::vector< bool > &isSi)
int getGlobalBin(float x, float y) const
int getPhiBin(float phi) const
int getGlobalBinEtaPhi(float eta, float phi) const
long double T
std::array< int, 4 > searchBoxEtaPhi(float etaMin, float etaMax, float phiMin, float phiMax) const