File indexing completed on 2025-09-17 09:20:13
0001 #ifndef YODA_BINESTIMATORS_H
0002 #define YODA_BINESTIMATORS_H
0003
0004 #include "YODA/Utils/fastlog.h"
0005 #include "YODA/Utils/MathUtils.h"
0006 #include <cstdlib>
0007
0008 namespace YODA {
0009
0010
0011
0012
0013
0014 struct BinEstimator {
0015
0016
0017 virtual ~BinEstimator() {}
0018
0019
0020 size_t estindex(double x) const {
0021 if (_N == 0) return 0;
0022 const int i = _est(x);
0023 if (i < 0) return 0;
0024 const size_t i2 = (size_t) i;
0025 if (i2 >= _N) return _N+1;
0026 return i2 + 1;
0027 }
0028
0029
0030 size_t operator() (double x) const {
0031 return estindex(x);
0032 }
0033
0034 protected:
0035
0036
0037
0038 virtual int _est(double x) const = 0;
0039
0040
0041 size_t _N;
0042 };
0043
0044
0045
0046
0047
0048
0049 struct LinBinEstimator : public BinEstimator {
0050
0051
0052 LinBinEstimator(size_t nbins, double xlow, double xhigh) {
0053 _N = nbins;
0054 _c = xlow;
0055 _m = (double) nbins / (xhigh - xlow);
0056 }
0057
0058
0059 LinBinEstimator(const LinBinEstimator& other) {
0060 _N = other._N;
0061 _c = other._c;
0062 _m = other._m;
0063 }
0064
0065
0066 int _est(double x) const {
0067 int res = (int) floor(_m * (x - _c));
0068 return res;
0069 }
0070
0071 protected:
0072 double _c, _m;
0073 };
0074
0075
0076
0077
0078
0079
0080
0081
0082 class LogBinEstimator : public BinEstimator {
0083 public:
0084
0085
0086 LogBinEstimator(size_t nbins, double xlow, double xhigh) {
0087 _N = nbins;
0088 _c = Utils::fastlog2(xlow);
0089 _m = nbins / (Utils::fastlog2(xhigh) - _c);
0090 }
0091
0092
0093 int _est(double x) const {
0094 if (x <= 0.) return -1;
0095 int res = (int) floor(_m * (Utils::fastlog2(x) - _c));
0096 return res;
0097
0098 }
0099
0100 protected:
0101 double _c, _m;
0102 };
0103 }
0104
0105 #endif