test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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  theNbins( last-first)
23  {
24  theBins.reserve(theNbins);
25  for (ConstItr i=first; i<last-1; i++) {
26  theBins.push_back((**i).position().z());
27  theBorders.push_back(((**i).position().z() +
28  (**(i+1)).position().z()) / 2.);
29  }
30 
31  theZOffset = theBorders.front();
32  theZStep = (theBorders.back() - theBorders.front()) / (theNbins-2);
33  }
34 
36  virtual int binIndex( T z) const {
37  int bin = binIndex( int((z-theZOffset)/theZStep)+1);
38 
39  // check left border
40  if (bin > 0) {
41  if ( z < theBorders[bin-1]) {
42  // z is to the left of the left border, the correct bin is left
43  for (int i=bin-1; ; i--) {
44  if (i <= 0) return 0;
45  if ( z > theBorders[i-1]) return i;
46  }
47  }
48  }
49  else return 0;
50 
51  // check right border
52  if (bin < theNbins-1) {
53  if ( z > theBorders[bin]) {
54  // z is to the right of the right border, the correct bin is right
55  for (int i=bin+1; ; i++) {
56  if (i >= theNbins-1) return theNbins-1;
57  if ( z < theBorders[i]) return i;
58  }
59  }
60  }
61  else return theNbins-1;
62 
63  // if we arrive here it means that the bin is ok
64  return bin;
65  }
66 
68  virtual int binIndex( int i) const {
69  return std::min( std::max( i, 0), theNbins-1);
70  }
71 
73  virtual T binPosition( int ind) const {
74  return theBins[binIndex(ind)];
75  }
76 
77  static double pi() { return 3.141592653589793238;}
78  static double twoPi() { return 2.*pi();}
79 
80 private:
81 
82  int theNbins;
85  std::vector<float> theBorders;
86  std::vector<T> theBins;
87 };
88 #endif
int i
Definition: DBlmapReader.cc:9
GenericBinFinderInZ(ConstItr first, ConstItr last)
virtual T binPosition(int ind) const
the middle of the bin.
T min(T a, T b)
Definition: MathUtil.h:58
std::vector< T > theBins
std::vector< const G * >::const_iterator ConstItr
virtual int binIndex(T z) const
returns an index in the valid range for the bin closest to Z
long double T
std::vector< float > theBorders
virtual int binIndex(int i) const
returns an index in the valid range