CMS 3D CMS Logo

Histogram3D.h
Go to the documentation of this file.
1 #ifndef CondFormats_PhysicsToolsObjects_Histogram3D_h
2 #define CondFormats_PhysicsToolsObjects_Histogram3D_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 
22  template <typename Value_t, typename AxisX_t = Value_t, typename AxisY_t = AxisX_t, typename AxisZ_t = AxisX_t>
23 
24  class Histogram3D {
25  public:
29 
30  Histogram3D();
31 
32  Histogram3D(const Histogram3D &orig);
33 
34  template <typename OValue_t, typename OAxisX_t, typename OAxisY_t, typename OAxisZ_t>
36 
37  Histogram3D(const std::vector<AxisX_t> &binULimitsX,
38  const std::vector<AxisY_t> &binULimitsY,
39  const std::vector<AxisZ_t> &binULimitsZ);
40 
41  template <typename OAxisX_t, typename OAxisY_t, typename OAxisZ_t>
42  Histogram3D(const std::vector<OAxisX_t> &binULimitsX,
43  const std::vector<OAxisY_t> &binULimitsY,
44  const std::vector<OAxisZ_t> &binULimitsZ);
45 
46  /*
47  //TO BE CONVERTED FROM HISTO2D TO HISTO3D
48  template<typename OAxisX_t, typename OAxisY_t>
49  Histogram3D(const std::vector<OAxisX_t> &binULimitsX,
50  unsigned int nBinsY,
51  const PhysicsTools::Calibration::Range<OAxisY_t> &rangeY);
52 
53  template<typename OAxisX_t, typename OAxisY_t>
54  Histogram3D(unsigned int nBinsX,
55  const PhysicsTools::Calibration::Range<OAxisX_t> &rangeX,
56  const std::vector<OAxisY_t> &binULimitsY);
57 
58  template<typename OAxisX_t, typename OAxisY_t>
59  Histogram3D(unsigned int nBinsX,
60  const PhysicsTools::Calibration::Range<OAxisX_t> &rangeX,
61  unsigned int nBinsY,
62  const PhysicsTools::Calibration::Range<OAxisY_t> &rangeY);
63 */
64  Histogram3D(unsigned int nBinsX,
65  AxisX_t minX,
66  AxisX_t maxX,
67  unsigned int nBinsY,
68  AxisY_t minY,
69  AxisY_t maxY,
70  unsigned int nBinsZ,
71  AxisZ_t minZ,
72  AxisZ_t maxZ);
73 
74  ~Histogram3D();
75 
76  Histogram3D &operator=(const Histogram3D &orig);
77 
78  template <typename OValue_t, typename OAxisX_t, typename OAxisY_t, typename OAxisZ_t>
80 
81  void reset();
82 
83  const std::vector<AxisX_t> upperLimitsX() const { return binULimitsX; }
84  const std::vector<AxisY_t> upperLimitsY() const { return binULimitsY; }
85  const std::vector<AxisZ_t> upperLimitsZ() const { return binULimitsZ; }
86 
87  inline int bin3D(int binX, int binY, int binZ) const
88  // { return (((binY * strideY) + binX) * strideX) + binZ; }
89  {
90  return binZ * strideX * strideY + binY * strideX + binX;
91  }
92 
93  Value_t binContent(int bin) const { return binValues[bin]; }
94  Value_t binContent(int binX, int binY, int binZ) const { return binValues[bin3D(binX, binY, binZ)]; }
95  Value_t value(AxisX_t x, AxisY_t y, AxisY_t z) const { return binContent(findBin(x, y, z)); }
96  Value_t normalizedValue(AxisX_t x, AxisY_t y, AxisZ_t z) const {
97  return binContent(findBin(x, y, z)) / normalization();
98  }
99 
100  //TO BE CONVERTED FROM HISTO2D TO HISTO3D
101  // Value_t normalizedXValue(AxisX_t x, AxisY_t y, AxisZ_t z) const;
102  // Value_t normalizedYValue(AxisX_t x, AxisY_t y, AxisZ_t z) const;
103  // Value_t normalizedZValue(AxisX_t x, AxisY_t y, AxisZ_t z) const;
104 
105  Value_t binError(int bin) const { return std::sqrt(binContent(bin)); }
106  Value_t binError(int binX, int binY, int binZ) const { return binError(bin3D(binX, binY, binZ)); }
107  Value_t error(AxisX_t x, AxisY_t y, AxisZ_t z) const { return binError(findBin(x, y, z)); }
108  Value_t normalizedError(AxisX_t x, AxisY_t y, AxisY_t z) const {
109  return std::sqrt(binContent(findBin(x, y, z))) / normalization();
110  }
111 
112  //TO BE CONVERTED FROM HISTO2D TO HISTO3D
113  // Value_t normalizedXError(AxisX_t x, AxisY_t y, AxisZ_t z) const;
114  // Value_t normalizedYError(AxisX_t x, AxisY_t y, AxisZ_t z) const;
115 
116  void setBinContent(int bin, Value_t value);
117  void setBinContent(int binX, int binY, int binZ, Value_t value) { setBinContent(bin3D(binX, binY, binZ), value); }
118  void fill(AxisX_t x, AxisY_t y, AxisZ_t z, Value_t weight = 1.0);
119 
120  bool empty() const { return binValues.empty(); }
121  bool hasEquidistantBinsX() const { return binULimitsX.empty(); }
122  bool hasEquidistantBinsY() const { return binULimitsY.empty(); }
123  bool hasEquidistantBinsZ() const { return binULimitsZ.empty(); }
124  int numberOfBinsX() const { return strideX - 2; }
125  int numberOfBinsY() const { return strideY - 2; }
126  int numberOfBinsZ() const { return binValues.size() / (strideX * strideY) - 2; }
127  int numberOfBins() const { return numberOfBinsX() * numberOfBinsY() * numberOfBinsZ(); }
128 
129  inline const std::vector<Value_t> &values() const { return binValues; }
130 
131  void setValues(const std::vector<Value_t> &values);
132 
133  template <typename OValue_t>
134  void setValues(const std::vector<OValue_t> &values);
135 
136  inline RangeX rangeX() const { return limitsX; }
137  inline RangeY rangeY() const { return limitsY; }
138  inline RangeZ rangeZ() const { return limitsZ; }
139  RangeX binRangeX(int binX) const;
140  RangeY binRangeY(int binY) const;
141  RangeZ binRangeZ(int binZ) const;
142 
143  //TO BE CONVERTED FROM HISTO2D TO HISTO3D
144  /* std::pair<RangeX, RangeY> binRange(int bin) const;
145  std::pair<RangeX, RangeY> binRange(int binX, int binY) const
146  { return binRange(bin2D(binX, binY)); }
147 */
148  int findBinX(AxisX_t x) const;
149  int findBinY(AxisY_t y) const;
150  int findBinZ(AxisZ_t z) const;
151  int findBin(AxisX_t x, AxisY_t y, AxisZ_t z) const { return bin3D(findBinX(x), findBinY(y), findBinZ(z)); }
152  Value_t normalization() const;
153 
154  //TO BE CONVERTED FROM HISTO2D TO HISTO3D
155  // Value_t normalizationX(int binY) const;
156  // Value_t normalizationY(int binX) const;
157 
158  protected:
159  unsigned int strideX;
160  unsigned int strideY;
161  std::vector<AxisX_t> binULimitsX;
162  std::vector<AxisY_t> binULimitsY;
163  std::vector<AxisZ_t> binULimitsZ;
164  std::vector<Value_t> binValues;
168 
169  // transient cache variables
170  mutable Value_t total COND_TRANSIENT; //CMS-THREADING protected by totalValid
171 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
172  mutable std::atomic<bool> totalValid COND_TRANSIENT;
173 #else
174  mutable bool totalValid COND_TRANSIENT;
175 #endif
176  mutable std::vector<Value_t> sliceTotal COND_TRANSIENT;
177  mutable std::vector<Value_t> rowTotal COND_TRANSIENT;
178  mutable std::vector<Value_t> columnTotal COND_TRANSIENT;
179 
181  };
182 
185 
186  } // namespace Calibration
187 } // namespace PhysicsTools
188 
189 #if !defined(__CINT__) && !defined(__MAKECINT__) && !defined(__REFLEX__)
190 #include "CondFormats/PhysicsToolsObjects/interface/Histogram3D.icc"
191 #endif
192 
193 #endif // CondFormats_PhysicsToolsObjects_Histogram3D_h
int bin3D(int binX, int binY, int binZ) const
Definition: Histogram3D.h:87
std::vector< Value_t > sliceTotal
Definition: Histogram3D.h:176
const std::vector< AxisY_t > upperLimitsY() const
Definition: Histogram3D.h:84
const std::vector< AxisZ_t > upperLimitsZ() const
Definition: Histogram3D.h:85
std::vector< Value_t > columnTotal
Definition: Histogram3D.h:178
Value_t binContent(int bin) const
Definition: Histogram3D.h:93
Definition: weight.py:1
void setValues(const std::vector< Value_t > &values)
const std::vector< Value_t > & values() const
Definition: Histogram3D.h:129
Histogram3D< double > HistogramD3D
Definition: Histogram3D.h:184
Value_t binContent(int binX, int binY, int binZ) const
Definition: Histogram3D.h:94
T sqrt(T t)
Definition: SSEVec.h:23
Value_t normalizedValue(AxisX_t x, AxisY_t y, AxisZ_t z) const
Definition: Histogram3D.h:96
Value_t binError(int binX, int binY, int binZ) const
Definition: Histogram3D.h:106
Definition: value.py:1
std::vector< AxisZ_t > binULimitsZ
Definition: Histogram3D.h:163
std::vector< AxisX_t > binULimitsX
Definition: Histogram3D.h:161
#define COND_TRANSIENT
Definition: Serializable.h:63
std::vector< Value_t > binValues
Definition: Histogram3D.h:164
void fill(AxisX_t x, AxisY_t y, AxisZ_t z, Value_t weight=1.0)
#define COND_SERIALIZABLE
Definition: Serializable.h:39
Histogram3D< float > HistogramF3D
Definition: Histogram3D.h:183
Value_t normalizedError(AxisX_t x, AxisY_t y, AxisY_t z) const
Definition: Histogram3D.h:108
const std::vector< AxisX_t > upperLimitsX() const
Definition: Histogram3D.h:83
Value_t value(AxisX_t x, AxisY_t y, AxisY_t z) const
Definition: Histogram3D.h:95
float x
int findBin(AxisX_t x, AxisY_t y, AxisZ_t z) const
Definition: Histogram3D.h:151
Histogram3D & operator=(const Histogram3D &orig)
std::vector< AxisY_t > binULimitsY
Definition: Histogram3D.h:162
Value_t error(AxisX_t x, AxisY_t y, AxisZ_t z) const
Definition: Histogram3D.h:107
void setBinContent(int binX, int binY, int binZ, Value_t value)
Definition: Histogram3D.h:117
void setBinContent(int bin, Value_t value)