CMS 3D CMS Logo

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