CMS 3D CMS Logo

Histogram2D.h
Go to the documentation of this file.
1 #ifndef CondFormats_PhysicsToolsObjects_Histogram2D_h
2 #define CondFormats_PhysicsToolsObjects_Histogram2D_h
3 
5 
6 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
7 #include <atomic>
8 #endif
9 
10 #include <utility>
11 #include <vector>
12 #include <cmath>
13 
15 
16 namespace PhysicsTools {
17  namespace Calibration {
18 
19  template <typename Value_t, typename AxisX_t = Value_t, typename AxisY_t = AxisX_t>
20  class Histogram2D {
21  public:
24 
25  Histogram2D();
26 
27  Histogram2D(const Histogram2D &orig);
28 
29  template <typename OValue_t, typename OAxisX_t, typename OAxisY_t>
31 
32  Histogram2D(const std::vector<AxisX_t> &binULimitsX, const std::vector<AxisY_t> &binULimitsY);
33 
34  template <typename OAxisX_t, typename OAxisY_t>
35  Histogram2D(const std::vector<OAxisX_t> &binULimitsX, const std::vector<OAxisY_t> &binULimitsY);
36 
37  template <typename OAxisX_t, typename OAxisY_t>
38  Histogram2D(const std::vector<OAxisX_t> &binULimitsX,
39  unsigned int nBinsY,
41 
42  template <typename OAxisX_t, typename OAxisY_t>
43  Histogram2D(unsigned int nBinsX,
45  const std::vector<OAxisY_t> &binULimitsY);
46 
47  template <typename OAxisX_t, typename OAxisY_t>
48  Histogram2D(unsigned int nBinsX,
50  unsigned int nBinsY,
52 
53  Histogram2D(unsigned int nBinsX, AxisX_t minX, AxisX_t maxX, unsigned int nBinsY, AxisY_t minY, AxisY_t maxY);
54 
55  ~Histogram2D();
56 
57  Histogram2D &operator=(const Histogram2D &orig);
58 
59  template <typename OValue_t, typename OAxisX_t, typename OAxisY_t>
61 
62  void reset();
63 
64  const std::vector<AxisX_t> upperLimitsX() const { return binULimitsX; }
65  const std::vector<AxisY_t> upperLimitsY() const { return binULimitsY; }
66 
67  inline int bin2D(int binX, int binY) const { return binY * stride + binX; }
68 
69  Value_t binContent(int bin) const { return binValues[bin]; }
70  Value_t binContent(int binX, int binY) const { return binValues[bin2D(binX, binY)]; }
71  Value_t value(AxisX_t x, AxisY_t y) const { return binContent(findBin(x, y)); }
72  Value_t normalizedValue(AxisX_t x, AxisY_t y) const { return binContent(findBin(x, y)) / normalization(); }
73  Value_t normalizedXValue(AxisX_t x, AxisY_t y) const;
74  Value_t normalizedYValue(AxisX_t x, AxisY_t y) const;
75 
76  Value_t binError(int bin) const { return std::sqrt(binContent(bin)); }
77  Value_t binError(int binX, int binY) const { return binError(bin2D(binX, binY)); }
78  Value_t error(AxisX_t x, AxisY_t y) const { return binError(findBin(x, y)); }
79  Value_t normalizedError(AxisX_t x, AxisY_t y) const {
80  return std::sqrt(binContent(findBin(x, y))) / normalization();
81  }
82  Value_t normalizedXError(AxisX_t x, AxisY_t y) const;
83  Value_t normalizedYError(AxisX_t x, AxisY_t y) const;
84 
85  void setBinContent(int bin, Value_t value);
86  void setBinContent(int binX, int binY, Value_t value) { setBinContent(bin2D(binX, binY), value); }
87  void fill(AxisX_t x, AxisY_t y, Value_t weight = 1.0);
88 
89  bool empty() const { return binValues.empty(); }
90  bool hasEquidistantBinsX() const { return binULimitsX.empty(); }
91  bool hasEquidistantBinsY() const { return binULimitsY.empty(); }
92  int numberOfBinsX() const { return stride - 2; }
93  int numberOfBinsY() const { return binValues.size() / stride - 2; }
94  int numberOfBins() const { return numberOfBinsX() * numberOfBinsY(); }
95 
96  inline const std::vector<Value_t> &values() const { return binValues; }
97 
98  void setValues(const std::vector<Value_t> &values);
99 
100  template <typename OValue_t>
101  void setValues(const std::vector<OValue_t> &values);
102 
103  inline RangeX rangeX() const { return limitsX; }
104  inline RangeY rangeY() const { return limitsY; }
105  RangeX binRangeX(int binX) const;
106  RangeY binRangeY(int binY) const;
107  std::pair<RangeX, RangeY> binRange(int bin) const;
108  std::pair<RangeX, RangeY> binRange(int binX, int binY) const { return binRange(bin2D(binX, binY)); }
109 
110  int findBinX(AxisX_t x) const;
111  int findBinY(AxisY_t y) const;
112  int findBin(AxisX_t x, AxisY_t y) const { return bin2D(findBinX(x), findBinY(y)); }
113  Value_t normalization() const;
114  Value_t normalizationX(int binY) const;
115  Value_t normalizationY(int binX) const;
116 
117  protected:
118  unsigned int stride;
119  std::vector<AxisX_t> binULimitsX;
120  std::vector<AxisY_t> binULimitsY;
121  std::vector<Value_t> binValues;
124 
125  // transient cache variables
126  mutable Value_t total COND_TRANSIENT; //CMS-THREADING protected by totalValid
127 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
128  mutable std::atomic<bool> totalValid COND_TRANSIENT;
129 #else
130  mutable bool totalValid COND_TRANSIENT;
131 #endif
132  mutable std::vector<Value_t> rowTotal COND_TRANSIENT;
133  mutable std::vector<Value_t> columnTotal COND_TRANSIENT;
134 
136  };
137 
140 
141  // wrap vectors of histograms so that CondDB can use them as top-level objects
142 
143  struct VHistogramD2D {
144  std::vector<PhysicsTools::Calibration::HistogramD2D> vHist;
145  std::vector<double> vValues;
146 
148  };
149 
150  } // namespace Calibration
151 } // namespace PhysicsTools
152 
153 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
154 #include "CondFormats/PhysicsToolsObjects/interface/Histogram2D.icc"
155 #endif
156 
157 #endif // CondFormats_PhysicsToolsObjects_Histogram2D_h
Value_t normalizationY(int binX) const
std::vector< PhysicsTools::Calibration::HistogramD2D > vHist
Definition: Histogram2D.h:144
Value_t binContent(int bin) const
Definition: Histogram2D.h:69
std::vector< Value_t > columnTotal
Definition: Histogram2D.h:133
std::pair< RangeX, RangeY > binRange(int binX, int binY) const
Definition: Histogram2D.h:108
Value_t normalizationX(int binY) const
Value_t binError(int bin) const
Definition: Histogram2D.h:76
int bin2D(int binX, int binY) const
Definition: Histogram2D.h:67
Definition: weight.py:1
void setBinContent(int bin, Value_t value)
Value_t binContent(int binX, int binY) const
Definition: Histogram2D.h:70
Histogram2D< float > HistogramF2D
Definition: Histogram2D.h:138
std::pair< RangeX, RangeY > binRange(int bin) const
Value_t value(AxisX_t x, AxisY_t y) const
Definition: Histogram2D.h:71
std::vector< AxisX_t > binULimitsX
Definition: Histogram2D.h:119
void fill(AxisX_t x, AxisY_t y, Value_t weight=1.0)
Value_t binError(int binX, int binY) const
Definition: Histogram2D.h:77
T sqrt(T t)
Definition: SSEVec.h:19
std::vector< Value_t > binValues
Definition: Histogram2D.h:121
int findBin(AxisX_t x, AxisY_t y) const
Definition: Histogram2D.h:112
Value_t error(AxisX_t x, AxisY_t y) const
Definition: Histogram2D.h:78
Definition: value.py:1
std::vector< AxisY_t > binULimitsY
Definition: Histogram2D.h:120
const std::vector< AxisX_t > upperLimitsX() const
Definition: Histogram2D.h:64
Histogram2D< double > HistogramD2D
Definition: Histogram2D.h:139
#define COND_TRANSIENT
Definition: Serializable.h:63
void setValues(const std::vector< Value_t > &values)
#define COND_SERIALIZABLE
Definition: Serializable.h:39
Histogram2D & operator=(const Histogram2D &orig)
Value_t normalizedError(AxisX_t x, AxisY_t y) const
Definition: Histogram2D.h:79
Value_t normalizedXError(AxisX_t x, AxisY_t y) const
const std::vector< Value_t > & values() const
Definition: Histogram2D.h:96
Value_t normalizedYValue(AxisX_t x, AxisY_t y) const
const std::vector< AxisY_t > upperLimitsY() const
Definition: Histogram2D.h:65
float x
Value_t normalizedXValue(AxisX_t x, AxisY_t y) const
Value_t normalizedValue(AxisX_t x, AxisY_t y) const
Definition: Histogram2D.h:72
void setBinContent(int binX, int binY, Value_t value)
Definition: Histogram2D.h:86
Value_t normalizedYError(AxisX_t x, AxisY_t y) const