CMS 3D CMS Logo

GeneralBinFinderInZforGeometricSearchDet.h
Go to the documentation of this file.
1 #ifndef GeneralBinFinderInZ_H
2 #define GeneralBinFinderInZ_H
3 
10 #include <cmath>
11 
12 #include<cassert>
13 
14 template <class T>
16 public:
17 
19 
21  std::vector<const GeometricSearchDet*>::const_iterator first,
22  std::vector<const GeometricSearchDet*>::const_iterator last) :
23  theNbins( last-first -1) // -1!
24  {
25  theBins.reserve(theNbins);
26  theBorders.reserve(theNbins);
27  for (auto i=first; i<last-1; i++) {
28  theBins.push_back((**i).position().z());
29  theBorders.push_back(((**i).position().z() +
30  (**(i+1)).position().z()) / 2.);
31  }
32  assert(theNbins==int(theBorders.size()));
33 
34  theZOffset = theBorders.front();
35  theZStep = (theBorders.back() - theBorders.front()) / (theNbins-1);
36  theInvZStep = 1./theZStep;
37  }
38 
40  virtual int binIndex( T z) const {
41  int bin = int((z-theZOffset)*theInvZStep)+1;
42  if (bin<=0) return 0;
43  if (bin>=theNbins) return theNbins;
44 
45  // check left border
46  if ( z < theBorders[bin-1]) {
47  // z is to the left of the left border, the correct bin is left
48  for (auto i=bin-1; i>0; i--) {
49  if ( z > theBorders[i-1]) return i;
50  }
51  return 0;
52  }
53 
54  // check right border
55  if ( z > theBorders[bin]) {
56  // z is to the right of the right border, the correct bin is right
57  for (int i=bin+1; i<theNbins; i++) {
58  if ( z < theBorders[i]) return i;
59  }
60  return theNbins;
61  }
62  // if we arrive here it means that the bin is ok
63  return bin;
64  }
65 
67  virtual int binIndex( int i) const {
68  return std::min( std::max( i, 0), theNbins);
69  }
70 
72  virtual T binPosition( int ind) const {
73  return theBins[binIndex(ind)];
74  }
75 
76 
77 private:
78 
79  int theNbins=0; // -1
83  std::vector<float> theBorders;
84  std::vector<T> theBins;
85 };
86 #endif
virtual T binPosition(int ind) const
the middle of the bin.
GeneralBinFinderInZforGeometricSearchDet(std::vector< const GeometricSearchDet * >::const_iterator first, std::vector< const GeometricSearchDet * >::const_iterator last)
virtual int binIndex(int i) const
returns an index in the valid range
T min(T a, T b)
Definition: MathUtil.h:58
bin
set the eta bin as selection string.
long double T
virtual int binIndex(T z) const
returns an index in the valid range for the bin closest to Z