Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:02:26

0001 #ifndef CondFormats_PhysicsToolsObjects_Histogram_h
0002 #define CondFormats_PhysicsToolsObjects_Histogram_h
0003 
0004 #include "CondFormats/Serialization/interface/Serializable.h"
0005 
0006 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
0007 #include <atomic>
0008 #endif
0009 
0010 #include <vector>
0011 #include <cmath>
0012 
0013 namespace PhysicsTools {
0014   namespace Calibration {
0015 
0016     template <typename Axis_t>
0017     struct Range {
0018       inline Range() {}
0019 
0020       template <typename OAxis_t>
0021       inline Range(const Range<OAxis_t> &orig) : min(orig.min), max(orig.max) {}
0022 
0023       inline Range(Axis_t min, Axis_t max) : min(min), max(max) {}
0024 
0025       ~Range() {}
0026 
0027       inline Axis_t width() const { return max - min; }
0028 
0029       Axis_t min, max;
0030 
0031       COND_SERIALIZABLE;
0032     };
0033 
0034     template <typename Value_t, typename Axis_t = Value_t>
0035     class Histogram {
0036     public:
0037       typedef PhysicsTools::Calibration::Range<Axis_t> Range;
0038 
0039       Histogram();
0040 
0041       Histogram(const Histogram &orig);
0042 
0043       template <typename OValue_t, typename OAxis_t>
0044       Histogram(const Histogram<OValue_t, OAxis_t> &orig);
0045 
0046       Histogram(const std::vector<Axis_t> &binULimits);
0047 
0048       template <typename OAxis_t>
0049       Histogram(const std::vector<OAxis_t> &binULimits);
0050 
0051       template <typename OAxis_t>
0052       Histogram(unsigned int nBins, const PhysicsTools::Calibration::Range<OAxis_t> &range);
0053 
0054       Histogram(unsigned int nBins, Axis_t min, Axis_t max);
0055 
0056       ~Histogram();
0057 
0058       Histogram &operator=(const Histogram &orig);
0059 
0060       template <typename OValue_t, typename OAxis_t>
0061       Histogram &operator=(const Histogram<OValue_t, OAxis_t> &orig);
0062 
0063       void reset();
0064 
0065       const std::vector<Axis_t> upperLimits() const { return binULimits; }
0066 
0067       Value_t binContent(int bin) const { return binValues[bin]; }
0068       Value_t value(Axis_t x) const { return binContent(findBin(x)); }
0069       Value_t normalizedValue(Axis_t x) const { return binContent(findBin(x)) / normalization(); }
0070 
0071       Value_t binError(int bin) const { return std::sqrt(binContent(bin)); }
0072       Value_t error(Axis_t x) const { return binError(findBin(x)); }
0073       Value_t normalizedError(Axis_t x) const { return std::sqrt(binContent(findBin(x))) / normalization(); }
0074 
0075       void setBinContent(int bin, Value_t value);
0076       void fill(Axis_t x, Value_t weight = 1.0);
0077 
0078       bool empty() const { return binValues.empty(); }
0079       bool hasEquidistantBins() const { return binULimits.empty(); }
0080       int numberOfBins() const { return binValues.size() - 2; }
0081 
0082       inline const std::vector<Value_t> &values() const { return binValues; }
0083 
0084       void setValues(const std::vector<Value_t> &values);
0085 
0086       template <typename OValue_t>
0087       void setValues(const std::vector<OValue_t> &values);
0088 
0089       inline Range range() const { return limits; }
0090       Range binRange(int bin) const;
0091 
0092       int findBin(Axis_t x) const;
0093       Value_t normalization() const;
0094 
0095       Value_t integral(Axis_t hBound, Axis_t lBound = 0.0, int mode = 1) const;
0096       Value_t normalizedIntegral(Axis_t hBound, Axis_t lBound = 0.0, int mode = 1) const {
0097         return integral(hBound, lBound, mode) / normalization();
0098       }
0099 
0100     protected:
0101       std::vector<Axis_t> binULimits;
0102       std::vector<Value_t> binValues;
0103       Range limits;
0104 
0105       // transient cache variables
0106       mutable Value_t total COND_TRANSIENT;  //CMS-THREADING protected by totalValid
0107 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
0108       mutable std::atomic<bool> totalValid COND_TRANSIENT;
0109 #else
0110       mutable bool totalValid COND_TRANSIENT;
0111 #endif
0112 
0113       COND_SERIALIZABLE;
0114     };
0115 
0116     typedef Histogram<float> HistogramF;
0117     typedef Histogram<double> HistogramD;
0118 
0119   }  // namespace Calibration
0120 }  // namespace PhysicsTools
0121 
0122 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
0123 #include "CondFormats/PhysicsToolsObjects/interface/Histogram.icc"
0124 #endif
0125 
0126 #endif  // CondFormats_PhysicsToolsObjects_Histogram_h