CMS 3D CMS Logo

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