CMS 3D CMS Logo

GeneralBinFinderInPhi.h
Go to the documentation of this file.
1 #ifndef TrackingTools_DetLayers_GeneralBinFinderInPhi_H
2 #define TrackingTools_DetLayers_GeneralBinFinderInPhi_H
3 
14 
15 #include <vector>
16 
17 template <class T>
19 public:
20  typedef PhiBorderFinder::Det Det; //FIXME!!!
21 
23 
26  theBorders = bf.phiBorders();
27  theBins = bf.phiBins();
28  theNbins = theBins.size();
29  }
30 
32  GeneralBinFinderInPhi(std::vector<Det*>::const_iterator first, std::vector<Det*>::const_iterator last)
33  : theNbins(last - first) {
34  std::vector<const Det*> dets(first, last);
35  PhiBorderFinder bf(dets);
36  theBorders = bf.phiBorders();
37  theBins = bf.phiBins();
38  theNbins = theBins.size();
39  }
40 
41  ~GeneralBinFinderInPhi() override {}
42 
45  int binIndex(T phi) const override {
46  const std::string metname = "Muon|RecoMuon|RecoMuonDetLayers|GeneralBinFinderInPhi";
47 
48  static const T epsilon = 10 * std::numeric_limits<T>::epsilon();
49  // Assume -pi, pi range in pi (which is the case for Geom::Phi
50 
51  LogTrace(metname) << "GeneralBinFinderInPhi::binIndex,"
52  << " Nbins: " << theNbins;
53 
54  for (int i = 0; i < theNbins; i++) {
55  T cur = theBorders[i];
56  T next = theBorders[binIndex(i + 1)];
57  T phi_ = phi;
58 
59  LogTrace(metname) << "bin: " << i << " border min " << cur << " border max: " << next << " phi: " << phi_;
60 
61  if (cur > next) // we are crossing the pi edge: so move the edge to 0!
62  {
63  cur = positiveRange(cur);
65  phi_ = positiveRange(phi_);
66  }
67  if (phi_ > cur - epsilon && phi_ < next)
68  return i;
69  }
70  throw cms::Exception("UnexpectedState") << "GeneralBinFinderInPhi::binIndex( T phi) bin not found!";
71  }
72 
74  int binIndex(int i) const override {
75  int ind = i % (int)theNbins;
76  return (ind < 0) ? ind + theNbins : ind;
77  }
78 
80  T binPosition(int ind) const override { return theBins[binIndex(ind)]; }
81 
82 private:
83  int theNbins;
84  std::vector<T> theBorders;
85  std::vector<T> theBins;
86 
87  // returns a positive angle; does NOT reduce the range to 2 pi
88  inline T positiveRange(T phi) const { return (phi > 0) ? phi : phi + Geom::twoPi(); }
89 };
90 #endif
const std::vector< double > & phiBins() const
The centers of the Dets.
GeneralBinFinderInPhi(const PhiBorderFinder &bf)
Construct from an already initialized PhiBorderFinder.
const std::string metname
int binIndex(int i) const override
Returns an index in the valid range, modulo Nbins.
PhiBorderFinder::Det Det
#define LogTrace(id)
const std::vector< double > & phiBorders() const
Definition: DetRod.h:13
int binIndex(T phi) const override
GeneralBinFinderInPhi(std::vector< Det *>::const_iterator first, std::vector< Det *>::const_iterator last)
Construct from the list of Det*.
T binPosition(int ind) const override
the middle of the bin in radians
long double T
constexpr double twoPi()
Definition: Pi.h:32