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