Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-19 09:42:10

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     /// @brief Bin estimator
0012     ///
0013     /// Base class for guessing the right bin index for a given value. The
0014     /// better the guess, the less time spent looking.
0015     struct BinEstimator {
0016 
0017       /// Virtual destructor needed for inheritance
0018       virtual ~BinEstimator() {}
0019 
0020       /// Return offset bin index estimate, with 0 = underflow and Nbins+1 = overflow
0021       size_t estindex(double x) const {
0022         if (_N == 0) return 0;
0023         const int i = _est(x);
0024         if (i < 0) return 0;
0025         const size_t i2 = (size_t) i;
0026         if (i2 >= _N) return _N+1;
0027         return i2 + 1;
0028       }
0029 
0030       /// Return offset bin index estimate, with 0 = underflow and Nbins+1 = overflow
0031       size_t operator() (double x) const {
0032         return estindex(x);
0033       }
0034 
0035     protected:
0036 
0037       /// Make an int-valued estimate of bin index
0038       /// @note No range checking or underflow offset
0039       virtual int _est(double x) const = 0;
0040 
0041       /// Number of bins
0042       size_t _N;
0043     };
0044 
0045 
0046     /// @brief Linear bin estimator
0047     ///
0048     /// This class handles guessing a index bin with a hypothesis of uniformly
0049     /// spaced bins on a linear scale.
0050     struct LinBinEstimator : public BinEstimator {
0051 
0052       /// Constructor
0053       LinBinEstimator(size_t nbins, double xlow, double xhigh) {
0054         _N = nbins;
0055         _c = xlow;
0056         _m = (double) nbins / (xhigh - xlow);
0057       }
0058 
0059       /// Copy constructor
0060       LinBinEstimator(const LinBinEstimator& other) {
0061         _N = other._N;
0062         _c = other._c;
0063         _m = other._m;
0064       }
0065 
0066       /// Call operator returns estimated bin index (offset so 0 == underflow)
0067       int _est(double x) const {
0068         int res = (int) floor(_m * (x - _c));
0069         return res;
0070       }
0071 
0072     protected:
0073       double _c, _m;
0074     };
0075 
0076 
0077     /// @brief Logarithmic bin estimator
0078     ///
0079     /// This class handles guessing a bin index with a hypothesis of uniformly
0080     /// spaced bins on a logarithmic scale.
0081     ///
0082     /// @todo Make a generalised version of this with a transform function
0083     class LogBinEstimator : public BinEstimator {
0084     public:
0085 
0086       /// Constructor
0087       LogBinEstimator(size_t nbins, double xlow, double xhigh) {
0088         _N = nbins;
0089         _c = Utils::fastlog2(xlow);
0090         _m = nbins / (Utils::fastlog2(xhigh) - _c);
0091       }
0092 
0093       /// Call operator returns estimated bin index (offset so 0 == underflow)
0094       int _est(double x) const {
0095         if (x <= 0.)  return -1;
0096         int res = (int) floor(_m * (Utils::fastlog2(x) - _c));
0097         return res;
0098 
0099       }
0100 
0101     protected:
0102       double _c, _m;
0103     };
0104 
0105 
0106 }
0107 
0108 #endif