CMS 3D CMS Logo

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