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