File indexing completed on 2024-04-06 12:02:26
0001 #ifndef CondFormats_PhysicsToolsObjects_Histogram2D_h
0002 #define CondFormats_PhysicsToolsObjects_Histogram2D_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 <utility>
0011 #include <vector>
0012 #include <cmath>
0013
0014 #include "CondFormats/PhysicsToolsObjects/interface/Histogram.h"
0015
0016 namespace PhysicsTools {
0017 namespace Calibration {
0018
0019 template <typename Value_t, typename AxisX_t = Value_t, typename AxisY_t = AxisX_t>
0020 class Histogram2D {
0021 public:
0022 typedef Range<AxisX_t> RangeX;
0023 typedef Range<AxisY_t> RangeY;
0024
0025 Histogram2D();
0026
0027 Histogram2D(const Histogram2D &orig);
0028
0029 template <typename OValue_t, typename OAxisX_t, typename OAxisY_t>
0030 Histogram2D(const Histogram2D<OValue_t, OAxisX_t, OAxisY_t> &orig);
0031
0032 Histogram2D(const std::vector<AxisX_t> &binULimitsX, const std::vector<AxisY_t> &binULimitsY);
0033
0034 template <typename OAxisX_t, typename OAxisY_t>
0035 Histogram2D(const std::vector<OAxisX_t> &binULimitsX, const std::vector<OAxisY_t> &binULimitsY);
0036
0037 template <typename OAxisX_t, typename OAxisY_t>
0038 Histogram2D(const std::vector<OAxisX_t> &binULimitsX,
0039 unsigned int nBinsY,
0040 const PhysicsTools::Calibration::Range<OAxisY_t> &rangeY);
0041
0042 template <typename OAxisX_t, typename OAxisY_t>
0043 Histogram2D(unsigned int nBinsX,
0044 const PhysicsTools::Calibration::Range<OAxisX_t> &rangeX,
0045 const std::vector<OAxisY_t> &binULimitsY);
0046
0047 template <typename OAxisX_t, typename OAxisY_t>
0048 Histogram2D(unsigned int nBinsX,
0049 const PhysicsTools::Calibration::Range<OAxisX_t> &rangeX,
0050 unsigned int nBinsY,
0051 const PhysicsTools::Calibration::Range<OAxisY_t> &rangeY);
0052
0053 Histogram2D(unsigned int nBinsX, AxisX_t minX, AxisX_t maxX, unsigned int nBinsY, AxisY_t minY, AxisY_t maxY);
0054
0055 ~Histogram2D();
0056
0057 Histogram2D &operator=(const Histogram2D &orig);
0058
0059 template <typename OValue_t, typename OAxisX_t, typename OAxisY_t>
0060 Histogram2D &operator=(const Histogram2D<OValue_t, OAxisX_t, OAxisY_t> &orig);
0061
0062 void reset();
0063
0064 const std::vector<AxisX_t> upperLimitsX() const { return binULimitsX; }
0065 const std::vector<AxisY_t> upperLimitsY() const { return binULimitsY; }
0066
0067 inline int bin2D(int binX, int binY) const { return binY * stride + binX; }
0068
0069 Value_t binContent(int bin) const { return binValues[bin]; }
0070 Value_t binContent(int binX, int binY) const { return binValues[bin2D(binX, binY)]; }
0071 Value_t value(AxisX_t x, AxisY_t y) const { return binContent(findBin(x, y)); }
0072 Value_t normalizedValue(AxisX_t x, AxisY_t y) const { return binContent(findBin(x, y)) / normalization(); }
0073 Value_t normalizedXValue(AxisX_t x, AxisY_t y) const;
0074 Value_t normalizedYValue(AxisX_t x, AxisY_t y) const;
0075
0076 Value_t binError(int bin) const { return std::sqrt(binContent(bin)); }
0077 Value_t binError(int binX, int binY) const { return binError(bin2D(binX, binY)); }
0078 Value_t error(AxisX_t x, AxisY_t y) const { return binError(findBin(x, y)); }
0079 Value_t normalizedError(AxisX_t x, AxisY_t y) const {
0080 return std::sqrt(binContent(findBin(x, y))) / normalization();
0081 }
0082 Value_t normalizedXError(AxisX_t x, AxisY_t y) const;
0083 Value_t normalizedYError(AxisX_t x, AxisY_t y) const;
0084
0085 void setBinContent(int bin, Value_t value);
0086 void setBinContent(int binX, int binY, Value_t value) { setBinContent(bin2D(binX, binY), value); }
0087 void fill(AxisX_t x, AxisY_t y, Value_t weight = 1.0);
0088
0089 bool empty() const { return binValues.empty(); }
0090 bool hasEquidistantBinsX() const { return binULimitsX.empty(); }
0091 bool hasEquidistantBinsY() const { return binULimitsY.empty(); }
0092 int numberOfBinsX() const { return stride - 2; }
0093 int numberOfBinsY() const { return binValues.size() / stride - 2; }
0094 int numberOfBins() const { return numberOfBinsX() * numberOfBinsY(); }
0095
0096 inline const std::vector<Value_t> &values() const { return binValues; }
0097
0098 void setValues(const std::vector<Value_t> &values);
0099
0100 template <typename OValue_t>
0101 void setValues(const std::vector<OValue_t> &values);
0102
0103 inline RangeX rangeX() const { return limitsX; }
0104 inline RangeY rangeY() const { return limitsY; }
0105 RangeX binRangeX(int binX) const;
0106 RangeY binRangeY(int binY) const;
0107 std::pair<RangeX, RangeY> binRange(int bin) const;
0108 std::pair<RangeX, RangeY> binRange(int binX, int binY) const { return binRange(bin2D(binX, binY)); }
0109
0110 int findBinX(AxisX_t x) const;
0111 int findBinY(AxisY_t y) const;
0112 int findBin(AxisX_t x, AxisY_t y) const { return bin2D(findBinX(x), findBinY(y)); }
0113 Value_t normalization() const;
0114 Value_t normalizationX(int binY) const;
0115 Value_t normalizationY(int binX) const;
0116
0117 protected:
0118 unsigned int stride;
0119 std::vector<AxisX_t> binULimitsX;
0120 std::vector<AxisY_t> binULimitsY;
0121 std::vector<Value_t> binValues;
0122 RangeX limitsX;
0123 RangeY limitsY;
0124
0125
0126 mutable Value_t total COND_TRANSIENT;
0127 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
0128 mutable std::atomic<bool> totalValid COND_TRANSIENT;
0129 #else
0130 mutable bool totalValid COND_TRANSIENT;
0131 #endif
0132 mutable std::vector<Value_t> rowTotal COND_TRANSIENT;
0133 mutable std::vector<Value_t> columnTotal COND_TRANSIENT;
0134
0135 COND_SERIALIZABLE;
0136 };
0137
0138 typedef Histogram2D<float> HistogramF2D;
0139 typedef Histogram2D<double> HistogramD2D;
0140
0141
0142
0143 struct VHistogramD2D {
0144 std::vector<PhysicsTools::Calibration::HistogramD2D> vHist;
0145 std::vector<double> vValues;
0146
0147 COND_SERIALIZABLE;
0148 };
0149
0150 }
0151 }
0152
0153 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
0154 #include "CondFormats/PhysicsToolsObjects/interface/Histogram2D.icc"
0155 #endif
0156
0157 #endif