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