CMS 3D CMS Logo

MagBinFinders.h
Go to the documentation of this file.
1 
2 // A set of binfiders adapted from CommonReco/DetLayers.
3 // FIXME: This file should eventually disappear and binfinders in
4 // CommonReco/DetLayers modified to be general enough!!!
5 
6 namespace MagBinFinders {
7  template <class T> class GeneralBinFinderInR;
8  template <class T> class GeneralBinFinderInZ;
9 }
10 
11 
12 //----------------------------------------------------------------------
13 #ifndef GeneralBinFinderInR_H
14 #define GeneralBinFinderInR_H
15 
22 //#include "CommonReco/DetLayers/interface/RBorderFinder.h"
23 #include <cmath>
24 #include <vector>
25 
26 template <class T>
28 public:
29 
31 
32  GeneralBinFinderInR(std::vector<T>& borders) :
33  theNbins(borders.size()),
34  theBorders(borders) {
35  // FIXME: compute bin positions.
36 // for (vector<T>::const_iterator i=theBorders.begin();
37 // i<theBorders.end(); ++i) {
38 // theBorders.push_back(((*i) + (*(i+1))) / 2.);
39 
40 // cout << "GeneralBinFinderInR_ " << theNbins << " " << theBorders.size() << " " << (int) this << endl;
41 }
42 
43 
44 // /// Construct from an already initialized RBorderFinder
45 // GeneralBinFinderInR(const RBorderFinder& bf) {
46 // theBorders=bf.RBorders();
47 // theBins=bf.RBins();
48 // theNbins=theBins.size();
49 // }
50 
51 // /// Construct from the list of Det*
52 // GeneralBinFinderInR(vector<Det*>::const_iterator first,
53 // vector<Det*>::const_iterator last)
54 // : theNbins( last-first)
55 // {
56 // vector<Det*> dets(first,last);
57 // RBorderFinder bf(dets);
58 // theBorders=bf.phiBorders();
59 // theBins=bf.phiBins();
60 // theNbins=theBins.size();
61 // }
62 
63 
66  virtual int binIndex( T R) const {
67  int i;
68  for (i = 0; i<theNbins; ++i) {
69  if (R < theBorders[i]){ // FIXME: one can be skipped?
70  break;
71  }
72  }
73  return binIndex(i-1);
74  }
75 
77  virtual int binIndex( int i) const {
78  return std::min( std::max( i, 0), theNbins-1);
79  }
80 
82  virtual T binPosition( int ind) const {
83  return theBins[binIndex(ind)];
84  }
85 
86 
87 private:
88  int theNbins;
89  std::vector<T> theBorders;
90  std::vector<T> theBins;
91 
92 };
93 #endif
94 //----------------------------------------------------------------------
95 
96 
97 
98 //----------------------------------------------------------------------
99 #ifndef GeneralBinFinderInZ_H
100 #define GeneralBinFinderInZ_H
101 
110 #include <cmath>
111 
112 template <class T>
114 public:
115 
116  GeneralBinFinderInZ() : theNbins(0), theZStep(0), theZOffset(0) {}
117 
118  GeneralBinFinderInZ(std::vector<T>& borders) :
119  theNbins(borders.size()),
120  theBorders(++(borders.begin()),borders.end()) { // Skip border of 1. bin
121  // FIXME: crashes for borders.size()==1 (trivial case)
122  // FIXME set theBins!!!
123  theZOffset = theBorders.front();
124  if (theNbins>2) {
125  theZStep = (theBorders.back() - theBorders.front()) / (theNbins-2);
126  } else {
127  theZStep = 1.; // Not relevant in this case...
128  }
129 
130 // cout << "GeneralBinFinderInZ " << theBorders.size()
131 // << " " << theZStep << endl;
132  }
133 
134  // FIXME: ??? theNbins e theBorders hanno size theNbins -1.
135  // theBorders[1] e' l'inizio del secondo bin !!!
136 
137 // GeneralBinFinderInZ(vector<Det*>::const_iterator first,
138 // vector<Det*>::const_iterator last) :
139 // theNbins( last-first)
140 // {
141 // theBins.reserve(theNbins);
142 // for (vector<Det*>::const_iterator i=first; i<last-1; ++i) {
143 // theBins.push_back((**i).position().z());
144 // theBorders.push_back(((**i).position().z() +
145 // (**(i+1)).position().z()) / 2.);
146 // }
147 
148 // theZOffset = theBorders.front();
149 // theZStep = (theBorders.back() - theBorders.front()) / (theNbins-2);
150 // }
151 
153  virtual int binIndex(T z) const {
154  int bin = binIndex(int((z-theZOffset)/theZStep)+1);
155 
156  // check left border
157  if (bin > 0) {
158  if (z < theBorders[bin-1]) {
159  // z is to the left of the left border, the correct bin is left
160  for (int i=bin-1; ; --i) {
161  if (i <= 0) return 0;
162  if ( z > theBorders[i-1]) return i;
163  }
164  }
165  }
166  else return 0;
167 
168  // check right border
169  if (bin < theNbins-1) {
170  if ( z > theBorders[bin]) {
171  // z is to the right of the right border, the correct bin is right
172  for (int i=bin+1; ; ++i) {
173  if (i >= theNbins-1) return theNbins-1;
174  if ( z < theBorders[i]) return i;
175  }
176  }
177  }
178  else return theNbins-1;
179 
180  // if we arrive here it means that the bin is ok
181  return bin;
182  }
183 
185  virtual int binIndex( int i) const {
186  return std::min( std::max( i, 0), theNbins-1);
187  }
188 
190  virtual T binPosition( int ind) const {
191  return theBins[binIndex(ind)];
192  }
193 
194  static double pi() { return 3.141592653589793238;}
195  static double twoPi() { return 2.*pi();}
196 
197 private:
198 
199  int theNbins;
202  std::vector<T> theBorders;
203  std::vector<T> theBins;
204 };
205 #endif
206 //----------------------------------------------------------------------
size
Write out results.
virtual int binIndex(T R) const
Definition: MagBinFinders.h:66
virtual int binIndex(T z) const
returns an index in the valid range for the bin closest to Z
virtual int binIndex(int i) const
Returns an index in the valid range.
Definition: MagBinFinders.h:77
const Double_t pi
GeneralBinFinderInR(std::vector< T > &borders)
Definition: MagBinFinders.h:32
#define end
Definition: vmac.h:37
T min(T a, T b)
Definition: MathUtil.h:58
bin
set the eta bin as selection string.
#define begin
Definition: vmac.h:30
virtual int binIndex(int i) const
returns an index in the valid range
GeneralBinFinderInZ(std::vector< T > &borders)
long double T
virtual T binPosition(int ind) const
The middle of the bin.
Definition: MagBinFinders.h:82
virtual T binPosition(int ind) const
the middle of the bin.