CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch1/src/HiggsAnalysis/CombinedLimit/interface/FastTemplate.h

Go to the documentation of this file.
00001 #ifndef HiggsAnalysis_CombinedLimit_FastH1
00002 #define HiggsAnalysis_CombinedLimit_FastH1
00003 
00004 #include <TH1.h>
00005 #include <TH2.h>
00006 #include <algorithm>
00007 
00008 class FastTemplate {
00009     public:
00010         typedef double T;
00011         FastTemplate() : size_(0), values_(0) {}
00012         FastTemplate(unsigned int size) : size_(size), values_(new T[size_]) {}
00013         FastTemplate(const FastTemplate &other) : size_(other.size_), values_(size_ ? new T[size_] :  0) { if (size_) CopyValues(other); }
00014         FastTemplate(const TH1 &other) : size_(other.GetNbinsX()), values_(new T[size_]) { CopyValues(other); }
00015         FastTemplate(const TH2 &other) : size_(other.GetNbinsX()*other.GetNbinsY()), values_(new T[size_]) { CopyValues(other); }
00016         FastTemplate & operator=(const FastTemplate &other) { 
00017             if (size_ != other.size_) { 
00018                 delete [] values_; size_ = other.size_; values_ = new T[size_];
00019             }
00020             CopyValues(other); return *this; 
00021         }
00022         FastTemplate & operator=(const TH1 &other) { 
00023             if (size_ != unsigned(other.GetNbinsX())) { 
00024                 delete [] values_; size_ = other.GetNbinsX(); values_ = new T[size_];
00025             }
00026             CopyValues(other); return *this;  
00027         }
00028         ~FastTemplate() { delete [] values_; }
00029         void Resize(unsigned int newsize) {
00030             if (newsize != size_) {
00031                 delete [] values_; size_ = newsize; values_ = new T[size_];
00032             }
00033         }
00034         T Integral() const ;
00035         void Scale(T factor) ;
00036         void Clear() ; 
00037         void CopyValues(const FastTemplate &other) ;
00038         void CopyValues(const TH1 &other) ;
00039         void CopyValues(const TH2 &other) ;
00040         T & operator[](unsigned int i) { return values_[i]; }
00041         const T & operator[](unsigned int i) const { return values_[i]; }
00042         const unsigned int size() const { return size_; }
00043         
00045         void Log();
00047         void Exp();
00049         void Subtract(const FastTemplate &reference);
00051         void LogRatio(const FastTemplate &reference);
00053         static void SumDiff(const FastTemplate &h1, const FastTemplate &h2, FastTemplate &sum, FastTemplate &diff);
00055         void Meld(const FastTemplate & diff, const FastTemplate & sum, T x, T y) ;
00057         void CropUnderflows(T minimum=1e-9);
00058 
00059         void Dump() const ;
00060     protected:
00061         unsigned int size_; 
00062         T *values_;
00063 };
00064 class FastHisto : public FastTemplate {
00065     public:
00066         FastHisto() : FastTemplate(), binEdges_(0), binWidths_(0) {}
00067         FastHisto(const TH1 &hist) ;
00068         FastHisto(const FastHisto &other) ;
00069         FastHisto & operator=(const FastHisto &other) { 
00070             if (size_ != other.size_) {
00071                 FastHisto fh(other);
00072                 swap(fh);
00073             } else CopyValues(other); 
00074             return *this; 
00075         }
00076         FastHisto & operator=(const TH1 &other) { 
00077             if (size_ != unsigned(other.GetNbinsX())) { 
00078                 FastHisto fh(other);
00079                 swap(fh);
00080             } else CopyValues(other); 
00081             CopyValues(other); return *this;  
00082         }
00083         ~FastHisto() { delete [] binEdges_; delete [] binWidths_; }
00084         void swap(FastHisto &other) {
00085             std::swap(size_, other.size_);
00086             std::swap(values_, other.values_);
00087             std::swap(binWidths_, other.binWidths_);
00088             std::swap(binEdges_, other.binEdges_);
00089         }
00090         T GetAt(const T &x) const ;
00091         T IntegralWidth() const ;
00092         void Normalize() {
00093             T sum = IntegralWidth();
00094             if (sum > 0) Scale(1.0f/sum);
00095         }
00096 
00097         void Dump() const ;
00098     private:
00099         T *binEdges_;
00100         T *binWidths_;
00101     
00102 };
00103 class FastHisto2D : public FastTemplate {
00104     public:
00105         FastHisto2D() : FastTemplate(), binX_(0), binY_(0), binEdgesX_(0), binEdgesY_(0), binWidths_(0) {}
00106         FastHisto2D(const TH2 &hist, bool normXonly=false) ;
00107         FastHisto2D(const FastHisto2D &other) ;
00108         FastHisto2D & operator=(const FastHisto2D &other) { 
00109             if (binX_ != other.binY_ || binY_ != other.binY_) {
00110                 FastHisto2D fh(other);
00111                 swap(fh);
00112             } else CopyValues(other); 
00113             return *this; 
00114         }
00115         ~FastHisto2D() { delete [] binEdgesX_; delete [] binEdgesY_; delete [] binWidths_; }
00116         void swap(FastHisto2D &other) {
00117             std::swap(binX_, other.binX_);
00118             std::swap(binY_, other.binY_);
00119             std::swap(size_, other.size_);
00120             std::swap(values_, other.values_);
00121             std::swap(binWidths_, other.binWidths_);
00122             std::swap(binEdgesX_, other.binEdgesX_);
00123             std::swap(binEdgesY_, other.binEdgesY_);
00124         }
00125         T GetAt(const T &x, const T &y) const ;
00126         T IntegralWidth() const ;
00127         void Normalize() {
00128             T sum = IntegralWidth();
00129             if (sum > 0) Scale(1.0f/sum);
00130         }
00132         void NormalizeXSlices() ;
00133 
00134         void Dump() const ;
00135     private:
00136         unsigned int binX_, binY_;
00137         T *binEdgesX_;
00138         T *binEdgesY_;
00139         T *binWidths_;
00140     
00141 };
00142 
00143 #endif