CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FastTemplate.h
Go to the documentation of this file.
1 #ifndef HiggsAnalysis_CombinedLimit_FastH1
2 #define HiggsAnalysis_CombinedLimit_FastH1
3 
4 #include <TH1.h>
5 #include <TH2.h>
6 #include <algorithm>
7 
8 class FastTemplate {
9  public:
10  typedef double T;
11  FastTemplate() : size_(0), values_(0) {}
12  FastTemplate(unsigned int size) : size_(size), values_(new T[size_]) {}
13  FastTemplate(const FastTemplate &other) : size_(other.size_), values_(size_ ? new T[size_] : 0) { if (size_) CopyValues(other); }
14  FastTemplate(const TH1 &other) : size_(other.GetNbinsX()), values_(new T[size_]) { CopyValues(other); }
15  FastTemplate(const TH2 &other) : size_(other.GetNbinsX()*other.GetNbinsY()), values_(new T[size_]) { CopyValues(other); }
16  FastTemplate & operator=(const FastTemplate &other) {
17  if (size_ != other.size_) {
18  delete [] values_; size_ = other.size_; values_ = new T[size_];
19  }
20  CopyValues(other); return *this;
21  }
22  FastTemplate & operator=(const TH1 &other) {
23  if (size_ != unsigned(other.GetNbinsX())) {
24  delete [] values_; size_ = other.GetNbinsX(); values_ = new T[size_];
25  }
26  CopyValues(other); return *this;
27  }
28  ~FastTemplate() { delete [] values_; }
29  void Resize(unsigned int newsize) {
30  if (newsize != size_) {
31  delete [] values_; size_ = newsize; values_ = new T[size_];
32  }
33  }
34  T Integral() const ;
35  void Scale(T factor) ;
36  void Clear() ;
37  void CopyValues(const FastTemplate &other) ;
38  void CopyValues(const TH1 &other) ;
39  void CopyValues(const TH2 &other) ;
40  T & operator[](unsigned int i) { return values_[i]; }
41  const T & operator[](unsigned int i) const { return values_[i]; }
42  const unsigned int size() const { return size_; }
43 
45  void Log();
47  void Exp();
49  void Subtract(const FastTemplate &reference);
51  void LogRatio(const FastTemplate &reference);
53  static void SumDiff(const FastTemplate &h1, const FastTemplate &h2, FastTemplate &sum, FastTemplate &diff);
55  void Meld(const FastTemplate & diff, const FastTemplate & sum, T x, T y) ;
57  void CropUnderflows(T minimum=1e-9);
58 
59  void Dump() const ;
60  protected:
61  unsigned int size_;
63 };
64 class FastHisto : public FastTemplate {
65  public:
67  FastHisto(const TH1 &hist) ;
68  FastHisto(const FastHisto &other) ;
69  FastHisto & operator=(const FastHisto &other) {
70  if (size_ != other.size_) {
71  FastHisto fh(other);
72  swap(fh);
73  } else CopyValues(other);
74  return *this;
75  }
76  FastHisto & operator=(const TH1 &other) {
77  if (size_ != unsigned(other.GetNbinsX())) {
78  FastHisto fh(other);
79  swap(fh);
80  } else CopyValues(other);
81  CopyValues(other); return *this;
82  }
83  ~FastHisto() { delete [] binEdges_; delete [] binWidths_; }
84  void swap(FastHisto &other) {
85  std::swap(size_, other.size_);
86  std::swap(values_, other.values_);
89  }
90  T GetAt(const T &x) const ;
91  T IntegralWidth() const ;
92  void Normalize() {
93  T sum = IntegralWidth();
94  if (sum > 0) Scale(1.0f/sum);
95  }
96 
97  void Dump() const ;
98  private:
101 
102 };
103 class FastHisto2D : public FastTemplate {
104  public:
106  FastHisto2D(const TH2 &hist, bool normXonly=false) ;
107  FastHisto2D(const FastHisto2D &other) ;
108  FastHisto2D & operator=(const FastHisto2D &other) {
109  if (binX_ != other.binY_ || binY_ != other.binY_) {
110  FastHisto2D fh(other);
111  swap(fh);
112  } else CopyValues(other);
113  return *this;
114  }
115  ~FastHisto2D() { delete [] binEdgesX_; delete [] binEdgesY_; delete [] binWidths_; }
116  void swap(FastHisto2D &other) {
117  std::swap(binX_, other.binX_);
118  std::swap(binY_, other.binY_);
119  std::swap(size_, other.size_);
120  std::swap(values_, other.values_);
124  }
125  T GetAt(const T &x, const T &y) const ;
126  T IntegralWidth() const ;
127  void Normalize() {
128  T sum = IntegralWidth();
129  if (sum > 0) Scale(1.0f/sum);
130  }
132  void NormalizeXSlices() ;
133 
134  void Dump() const ;
135  private:
136  unsigned int binX_, binY_;
140 
141 };
142 
143 #endif
T * binEdges_
Definition: FastTemplate.h:99
int i
Definition: DBlmapReader.cc:9
const unsigned int size() const
Definition: FastTemplate.h:42
void Subtract(const FastTemplate &reference)
*this = *this - reference
void Normalize()
Definition: FastTemplate.h:92
FastTemplate & operator=(const FastTemplate &other)
Definition: FastTemplate.h:16
FastTemplate(unsigned int size)
Definition: FastTemplate.h:12
T & operator[](unsigned int i)
Definition: FastTemplate.h:40
T * binWidths_
Definition: FastTemplate.h:100
void Resize(unsigned int newsize)
Definition: FastTemplate.h:29
void CropUnderflows(T minimum=1e-9)
protect from underflows (*this = max(*this, minimum));
T IntegralWidth() const
Definition: FastTemplate.cc:74
T GetAt(const T &x) const
Definition: FastTemplate.cc:68
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
void Log()
*this = log(*this)
FastHisto & operator=(const FastHisto &other)
Definition: FastTemplate.h:69
double f[11][100]
void Dump() const
T GetAt(const T &x, const T &y) const
void NormalizeXSlices()
For each X, normalize along Y.
void Dump() const
Definition: FastTemplate.cc:80
void CopyValues(const FastTemplate &other)
Definition: FastTemplate.cc:22
FastTemplate & operator=(const TH1 &other)
Definition: FastTemplate.h:22
void Exp()
*this = exp(*this)
T IntegralWidth() const
T Integral() const
Definition: FastTemplate.cc:8
void Normalize()
Definition: FastTemplate.h:127
FastTemplate(const TH2 &other)
Definition: FastTemplate.h:15
void Dump() const
Definition: FastTemplate.cc:39
void swap(FastHisto2D &other)
Definition: FastTemplate.h:116
FastTemplate(const TH1 &other)
Definition: FastTemplate.h:14
const T & operator[](unsigned int i) const
Definition: FastTemplate.h:41
unsigned int binX_
Definition: FastTemplate.h:136
FastTemplate(const FastTemplate &other)
Definition: FastTemplate.h:13
unsigned int size_
Definition: FastTemplate.h:61
static void SumDiff(const FastTemplate &h1, const FastTemplate &h2, FastTemplate &sum, FastTemplate &diff)
assigns sum and diff
void LogRatio(const FastTemplate &reference)
*this = log(*this)/(reference)
void swap(FastHisto &other)
Definition: FastTemplate.h:84
FastHisto & operator=(const TH1 &other)
Definition: FastTemplate.h:76
x
Definition: VDTMath.h:216
long double T
void Scale(T factor)
Definition: FastTemplate.cc:14
void Meld(const FastTemplate &diff, const FastTemplate &sum, T x, T y)
Does this += x * (diff + (sum)*y)
FastHisto2D & operator=(const FastHisto2D &other)
Definition: FastTemplate.h:108
unsigned int binY_
Definition: FastTemplate.h:136