Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:31:58

0001 #ifndef PeriodicBinFinderInPhi_H
0002 #define PeriodicBinFinderInPhi_H
0003 
0004 #include "Utilities/BinningTools/interface/BaseBinFinder.h"
0005 
0006 #include <algorithm>
0007 #include <cmath>
0008 
0009 /** Periodic Bin Finder around a circle for (almost) equidistant bins.
0010  *  Phi is the angle on the circle in radians. 
0011  */
0012 
0013 template <class T>
0014 class PeriodicBinFinderInPhi final : public BaseBinFinder<T> {
0015 public:
0016   PeriodicBinFinderInPhi() {}
0017 
0018   PeriodicBinFinderInPhi(T firstPhi, int nbins)
0019       : theNbins(nbins),
0020         thePhiStep(twoPiC / T(nbins)),
0021         theInvPhiStep(T(1) / thePhiStep),
0022         thePhiOffset(firstPhi - T(0.5) * thePhiStep) {}
0023 
0024   /// returns an index in the valid range for the bin that contains phi
0025   int binIndex(T phi) const override {
0026     T tmp = std::fmod((phi - thePhiOffset), twoPiC) * theInvPhiStep;
0027     if (tmp < 0)
0028       tmp += theNbins;
0029     return std::min(int(tmp), theNbins - 1);
0030   }
0031 
0032   /// returns an index in the valid range, modulo Nbins
0033   int binIndex(int i) const override {
0034     int ind = i % theNbins;
0035     return ind < 0 ? ind + theNbins : ind;
0036   }
0037 
0038   /// the middle of the bin in radians
0039   T binPosition(int ind) const override { return thePhiOffset + thePhiStep * (T(ind) + T(0.5)); }
0040 
0041   static constexpr T pi() { return piC; }
0042   static constexpr T twoPi() { return twoPiC; }
0043 
0044 private:
0045   static constexpr T piC = 3.141592653589793238;
0046   static constexpr T twoPiC = 2 * piC;
0047 
0048   int theNbins = 0;
0049   T thePhiStep = 0;
0050   T theInvPhiStep = 0;
0051   T thePhiOffset = 0;
0052 };
0053 #endif