CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/CondFormats/PhysicsToolsObjects/interface/Histogram2D.h

Go to the documentation of this file.
00001 #ifndef CondFormats_PhysicsToolsObjects_Histogram2D_h
00002 #define CondFormats_PhysicsToolsObjects_Histogram2D_h
00003 
00004 #include <utility>
00005 #include <vector>
00006 #include <cmath>
00007 
00008 #include "CondFormats/PhysicsToolsObjects/interface/Histogram.h"
00009 
00010 namespace PhysicsTools {
00011 namespace Calibration {
00012 
00013 template<typename Value_t, typename AxisX_t = Value_t,
00014          typename AxisY_t = AxisX_t>
00015 class Histogram2D {
00016     public:
00017         typedef Range<AxisX_t> RangeX;
00018         typedef Range<AxisY_t> RangeY;
00019 
00020         Histogram2D();
00021 
00022         Histogram2D(const Histogram2D &orig);
00023 
00024         template<typename OValue_t, typename OAxisX_t, typename OAxisY_t>
00025         Histogram2D(const Histogram2D<OValue_t, OAxisX_t, OAxisY_t> &orig);
00026 
00027         Histogram2D(const std::vector<AxisX_t> &binULimitsX,
00028                     const std::vector<AxisY_t> &binULimitsY);
00029 
00030         template<typename OAxisX_t, typename OAxisY_t>
00031         Histogram2D(const std::vector<OAxisX_t> &binULimitsX,
00032                     const std::vector<OAxisY_t> &binULimitsY);
00033 
00034         template<typename OAxisX_t, typename OAxisY_t>
00035         Histogram2D(const std::vector<OAxisX_t> &binULimitsX,
00036                     unsigned int nBinsY,
00037                     const PhysicsTools::Calibration::Range<OAxisY_t> &rangeY);
00038 
00039         template<typename OAxisX_t, typename OAxisY_t>
00040         Histogram2D(unsigned int nBinsX,
00041                     const PhysicsTools::Calibration::Range<OAxisX_t> &rangeX,
00042                     const std::vector<OAxisY_t> &binULimitsY);
00043 
00044         template<typename OAxisX_t, typename OAxisY_t>
00045         Histogram2D(unsigned int nBinsX,
00046                     const PhysicsTools::Calibration::Range<OAxisX_t> &rangeX,
00047                     unsigned int nBinsY,
00048                     const PhysicsTools::Calibration::Range<OAxisY_t> &rangeY);
00049 
00050         Histogram2D(unsigned int nBinsX, AxisX_t minX, AxisX_t maxX,
00051                     unsigned int nBinsY, AxisY_t minY, AxisY_t maxY);
00052 
00053         ~Histogram2D();
00054 
00055         Histogram2D &operator = (const Histogram2D &orig);
00056 
00057         template<typename OValue_t, typename OAxisX_t, typename OAxisY_t>
00058         Histogram2D &operator = (const Histogram2D<OValue_t,
00059                                  OAxisX_t, OAxisY_t> &orig);
00060 
00061         void reset();
00062 
00063         const std::vector<AxisX_t> upperLimitsX() const { return binULimitsX; }
00064         const std::vector<AxisY_t> upperLimitsY() const { return binULimitsY; }
00065 
00066         inline int bin2D(int binX, int binY) const
00067         { return binY * stride + binX; }
00068 
00069         Value_t binContent(int bin) const { return binValues[bin]; }
00070         Value_t binContent(int binX, int binY) const
00071         { return binValues[bin2D(binX, binY)]; }
00072         Value_t value(AxisX_t x, AxisY_t y) const
00073         { return binContent(findBin(x, y)); }
00074         Value_t normalizedValue(AxisX_t x, AxisY_t y) const
00075         { return binContent(findBin(x, y)) / normalization(); }
00076         Value_t normalizedXValue(AxisX_t x, AxisY_t y) const;
00077         Value_t normalizedYValue(AxisX_t x, AxisY_t y) const;
00078 
00079         Value_t binError(int bin) const { return std::sqrt(binContent(bin)); }
00080         Value_t binError(int binX, int binY) const
00081         { return binError(bin2D(binX, binY)); }
00082         Value_t error(AxisX_t x, AxisY_t y) const
00083         { return binError(findBin(x, y)); }
00084         Value_t normalizedError(AxisX_t x, AxisY_t y) const
00085         { return std::sqrt(binContent(findBin(x, y))) / normalization(); }
00086         Value_t normalizedXError(AxisX_t x, AxisY_t y) const;
00087         Value_t normalizedYError(AxisX_t x, AxisY_t y) const;
00088 
00089         void setBinContent(int bin, Value_t value);
00090         void setBinContent(int binX, int binY, Value_t value)
00091         { setBinContent(bin2D(binX, binY), value); }
00092         void fill(AxisX_t x, AxisY_t y, Value_t weight = 1.0);
00093 
00094         bool empty() const { return binValues.empty(); }
00095         bool hasEquidistantBinsX() const { return binULimitsX.empty(); }
00096         bool hasEquidistantBinsY() const { return binULimitsY.empty(); }
00097         int numberOfBinsX() const { return stride - 2; }
00098         int numberOfBinsY() const { return binValues.size() / stride - 2; }
00099         int numberOfBins() const
00100         { return numberOfBinsX() * numberOfBinsY(); }
00101 
00102         inline const std::vector<Value_t> &values() const
00103         { return binValues; }
00104 
00105         void setValues(const std::vector<Value_t> &values);
00106 
00107         template<typename OValue_t>
00108         void setValues(const std::vector<OValue_t> &values);
00109 
00110         inline RangeX rangeX() const { return limitsX; }
00111         inline RangeY rangeY() const { return limitsY; }
00112         RangeX binRangeX(int binX) const;
00113         RangeY binRangeY(int binY) const;
00114         std::pair<RangeX, RangeY> binRange(int bin) const;
00115         std::pair<RangeX, RangeY> binRange(int binX, int binY) const
00116         { return binRange(bin2D(binX, binY)); }
00117 
00118         int findBinX(AxisX_t x) const;
00119         int findBinY(AxisY_t y) const;
00120         int findBin(AxisX_t x, AxisY_t y) const
00121         { return bin2D(findBinX(x), findBinY(y)); }
00122         Value_t normalization() const;
00123         Value_t normalizationX(int binY) const;
00124         Value_t normalizationY(int binX) const;
00125 
00126     protected:
00127         unsigned int                    stride;
00128         std::vector<AxisX_t>            binULimitsX;
00129         std::vector<AxisY_t>            binULimitsY;
00130         std::vector<Value_t>            binValues;
00131         RangeX                          limitsX;
00132         RangeY                          limitsY;
00133 
00134         // transient cache variables
00135         mutable Value_t                 total;
00136         mutable bool                    totalValid;
00137         mutable std::vector<Value_t>    rowTotal;
00138         mutable std::vector<Value_t>    columnTotal;
00139 };
00140 
00141 typedef Histogram2D<float>  HistogramF2D;
00142 typedef Histogram2D<double> HistogramD2D;
00143 
00144 // wrap vectors of histograms so that CondDB can use them as top-level objects
00145 
00146 struct VHistogramD2D {
00147   std::vector<PhysicsTools::Calibration::HistogramD2D>  vHist;
00148   std::vector<double> vValues;
00149 };
00150 
00151 } // namespace Calibration
00152 } // namespace PhysicsTools
00153 
00154 #include "CondFormats/PhysicsToolsObjects/interface/Histogram2D.icc"
00155 
00156 #endif // CondFormats_PhysicsToolsObjects_Histogram2D_h