CMS 3D CMS Logo

HGCalLayerTiles.h
Go to the documentation of this file.
1 // Authors: Felice Pantaleo - felice.pantaleo@cern.ch, Olivie Abigail Franklova - olivie.abigail.franklova@cern.ch
2 // Date: 03/2019
3 
4 #ifndef RecoLocalCalo_HGCalRecProducers_HGCalLayerTiles_h
5 #define RecoLocalCalo_HGCalRecProducers_HGCalLayerTiles_h
6 
11 
12 #include <vector>
13 #include <array>
14 #include <cmath>
15 #include <algorithm>
16 #include <cassert>
17 
18 template <typename T, typename WRAPPER>
20 public:
21  typedef T type;
29  void fill(const std::vector<float>& dim1, const std::vector<float>& dim2) {
30  auto cellsSize = dim1.size();
31  for (unsigned int i = 0; i < cellsSize; ++i) {
32  auto idx = getGlobalBin(dim1[i], dim2[i]);
33  tiles_[idx].push_back(i);
34  }
35  }
42  int getDim1Bin(float dim) const {
43  constexpr float dimRange = T::maxDim1 - T::minDim1;
44  static_assert(dimRange >= 0.);
45  constexpr float r = T::nColumns / dimRange;
46  int dimBin = (dim - T::minDim1) * r;
47  dimBin = std::clamp(dimBin, 0, T::nColumns - 1);
48  return dimBin;
49  }
50 
57  int getDim2Bin(float dim2) const {
58  if constexpr (std::is_same_v<WRAPPER, NoPhiWrapper>) {
59  constexpr float dimRange = T::maxDim2 - T::minDim2;
60  static_assert(dimRange >= 0.);
61  constexpr float r = T::nRows / dimRange;
62  int dimBin = (dim2 - T::minDim2) * r;
63  dimBin = std::clamp(dimBin, 0, T::nRows - 1);
64  return dimBin;
65  } else {
66  auto normPhi = normalizedPhi(dim2);
67  constexpr float r = T::nRows * M_1_PI * 0.5f;
68  int phiBin = (normPhi + M_PI) * r;
69  return phiBin;
70  }
71  }
72 
75 
76  inline float distance2(float dim1Cell1, float dim2Cell1, float dim1Cell2, float dim2Cell2) const { // distance squared
77  float d1 = dim1Cell1 - dim1Cell2;
78  float d2 = dim2Cell1 - dim2Cell2;
79  if constexpr (std::is_same_v<WRAPPER, PhiWrapper>) {
80  d2 = reco::deltaPhi(dim2Cell1, dim2Cell2);
81  }
82  return std::fmaf(d1, d1, d2 * d2);
83  }
84  int getGlobalBin(float dim1, float dim2) const { return getDim1Bin(dim1) + getDim2Bin(dim2) * T::nColumns; }
85 
86  int getGlobalBinByBin(int dim1Bin, int dim2Bin) const { return dim1Bin + dim2Bin * T::nColumns; }
87 
88  std::array<int, 4> searchBox(float dim1Min, float dim1Max, float dim2Min, float dim2Max) const {
89  if constexpr (std::is_same_v<WRAPPER, PhiWrapper>) {
90  if (dim1Max - dim1Min < 0) {
91  return std::array<int, 4>({{0, 0, 0, 0}});
92  }
93  }
94  int dim1BinMin = getDim1Bin(dim1Min);
95  int dim1BinMax = getDim1Bin(dim1Max);
96  int dim2BinMin = getDim2Bin(dim2Min);
97  int dim2BinMax = getDim2Bin(dim2Max);
98  if constexpr (std::is_same_v<WRAPPER, PhiWrapper>) {
99  // If the search window cross the phi-bin boundary, add T::nPhiBins to the
100  // MAx value. This guarantees that the caller can perform a valid doule
101  // loop on eta and phi. It is the caller responsibility to perform a module
102  // operation on the phiBin values returned by this function, to explore the
103  // correct bins.
104  if (dim2BinMax < dim2BinMin) {
105  dim2BinMax += T::nRows;
106  }
107  }
108  return std::array<int, 4>({{dim1BinMin, dim1BinMax, dim2BinMin, dim2BinMax}});
109  }
110 
111  void clear() {
112  for (auto& t : tiles_)
113  t.clear();
114  }
115 
116  const std::vector<int>& operator[](int globalBinId) const { return tiles_[globalBinId]; }
117 
118 private:
119  std::array<std::vector<int>, T::nTiles> tiles_;
120 };
121 
125 #endif
constexpr double deltaPhi(double phi1, double phi2)
Definition: deltaPhi.h:26
int getGlobalBin(float dim1, float dim2) const
std::array< int, 4 > searchBox(float dim1Min, float dim1Max, float dim2Min, float dim2Max) const
int getDim2Bin(float dim2) const
compute bin for dim2 (y or phi)
void fill(const std::vector< float > &dim1, const std::vector< float > &dim2)
fill the tile
constexpr T normalizedPhi(T phi)
Definition: normalizedPhi.h:8
int getDim1Bin(float dim) const
compute bin for dim1 (x or eta)
int getGlobalBinByBin(int dim1Bin, int dim2Bin) const
float distance2(float dim1Cell1, float dim2Cell1, float dim1Cell2, float dim2Cell2) const
#define M_PI
const std::vector< int > & operator[](int globalBinId) const
std::array< std::vector< int >, T::nTiles > tiles_
static constexpr float d1
long double T