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