Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-19 09:13:36

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