File indexing completed on 2023-03-17 11:26:45
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
0010
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
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
0033 int binIndex(int i) const override {
0034 int ind = i % theNbins;
0035 return ind < 0 ? ind + theNbins : ind;
0036 }
0037
0038
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