CMS 3D CMS Logo

GenericBinFinderInZ.h
Go to the documentation of this file.
1 #ifndef BinningTools_GenericBinFinderInZ_H
2 #define BinningTools_GenericBinFinderInZ_H
3 
12 #include <cmath>
13 #include <vector>
14 
15 template <class T, class G>
17 public:
18  typedef typename std::vector<const G*>::const_iterator ConstItr;
20 
22  theBins.reserve(theNbins);
23  theBorders.reserve(theNbins - 1);
24  for (ConstItr i = first; i < last - 1; i++) {
25  theBins.push_back((**i).position().z());
26  theBorders.push_back(((**i).position().z() + (**(i + 1)).position().z()) / 2.);
27  }
28  theBins.push_back((**(last - 1)).position().z());
29 
30  theZOffset = theBorders.front();
31  theZStep = (theBorders.back() - theBorders.front()) / (theNbins - 2);
32  }
33 
35  int binIndex(T z) const override {
36  int bin = binIndex(int((z - theZOffset) / theZStep) + 1);
37 
38  // check left border
39  if (bin > 0) {
40  if (z < theBorders[bin - 1]) {
41  // z is to the left of the left border, the correct bin is left
42  for (int i = bin - 1;; i--) {
43  if (i <= 0)
44  return 0;
45  if (z > theBorders[i - 1])
46  return i;
47  }
48  }
49  } else
50  return 0;
51 
52  // check right border
53  if (bin < theNbins - 1) {
54  if (z > theBorders[bin]) {
55  // z is to the right of the right border, the correct bin is right
56  for (int i = bin + 1;; i++) {
57  if (i >= theNbins - 1)
58  return theNbins - 1;
59  if (z < theBorders[i])
60  return i;
61  }
62  }
63  } else
64  return theNbins - 1;
65 
66  // if we arrive here it means that the bin is ok
67  return bin;
68  }
69 
71  int binIndex(int i) const override { return std::min(std::max(i, 0), theNbins - 1); }
72 
74  T binPosition(int ind) const override { return theBins[binIndex(ind)]; }
75 
76  static double pi() { return 3.141592653589793238; }
77  static double twoPi() { return 2. * pi(); }
78 
79 private:
80  int theNbins;
83  std::vector<float> theBorders;
84  std::vector<T> theBins;
85 };
86 #endif
GenericBinFinderInZ(ConstItr first, ConstItr last)
int binIndex(T z) const override
returns an index in the valid range for the bin closest to Z
std::vector< T > theBins
std::vector< const G * >::const_iterator ConstItr
int binIndex(int i) const override
returns an index in the valid range
static int position[264][3]
Definition: ReadPGInfo.cc:289
long double T
std::vector< float > theBorders
T binPosition(int ind) const override
the middle of the bin.