CMS 3D CMS Logo

MultiHistoChiSquare.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_Utilities_MultiHistoChiSquare_h
2 #define PhysicsTools_Utilities_MultiHistoChiSquare_h
4 #include "TMath.h"
5 #include "TH1.h"
6 
7 namespace fit {
8  namespace helper {
10  } // namespace helper
11 
12  template <typename T1,
19  public:
21  template <typename TT1, typename TT2, typename TT3, typename TT4, typename TT5, typename TT6>
23  TH1 *histo1,
24  TT2 &t2,
25  TH1 *histo2,
26  TT3 &t3,
27  TH1 *histo3,
28  TT4 &t4,
29  TH1 *histo4,
30  TT5 &t5,
31  TH1 *histo5,
32  TT6 &t6,
33  TH1 *histo6,
34  double rangeMin,
35  double rangeMax)
36  : chi1_(t1, histo1, rangeMin, rangeMax),
37  chi2_(t2, histo2, rangeMin, rangeMax),
38  chi3_(t3, histo3, rangeMin, rangeMax),
39  chi4_(t4, histo4, rangeMin, rangeMax),
40  chi5_(t5, histo5, rangeMin, rangeMax),
41  chi6_(t6, histo6, rangeMin, rangeMax) {}
42  double operator()() const {
43  double chi2 = chi1_() + chi2_() + chi3_() + chi4_() + chi5_() + chi6_();
44  static size_t count = 0;
45  ++count;
46  if (count % 10 == 0)
47  return chi2;
48  }
49  void setHistos(TH1 *histo1, TH1 *histo2, TH1 *histo3, TH1 *histo4, TH1 *histo5, TH1 *histo6) {
50  chi1_.setHistos(histo1);
51  chi2_.setHistos(histo2);
52  chi3_.setHistos(histo3);
53  chi4_.setHistos(histo4);
54  chi5_.setHistos(histo5);
55  chi6_.setHistos(histo6);
56  }
57  size_t numberOfBins() const {
58  return chi1_.numberOfBins() + chi2_.numberOfBins() + chi3_.numberOfBins() + chi4_.numberOfBins() +
59  chi5_.numberOfBins() + chi6_.numberOfBins();
60  }
61  T1 &function1() { return chi1_.function(); }
62  const T1 &function1() const { return chi1_.function(); }
63  T2 &function2() { return chi2_.function(); }
64  const T2 &function2() const { return chi2_.function(); }
65  T3 &function3() { return chi3_.function(); }
66  const T3 &function3() const { return chi3_.function(); }
67  T4 &function4() { return chi4_.function(); }
68  const T4 &function4() const { return chi4_.function(); }
69  T5 &function5() { return chi5_.function(); }
70  const T5 &function5() const { return chi5_.function(); }
71  T6 &function6() { return chi6_.function(); }
72  const T6 &function6() const { return chi6_.function(); }
73 
74  private:
75  T1 chi1_;
76  T2 chi2_;
77  T3 chi3_;
78  T4 chi4_;
79  T5 chi5_;
80  T6 chi6_;
81  };
82 
83  template <typename T1, typename T2, typename T3, typename T4, typename T5>
84  class MultiHistoChiSquare<T1, T2, T3, T4, T5, helper::MultiHistoChiSquareNoArg> {
85  public:
87  template <typename TT1, typename TT2, typename TT3, typename TT4, typename TT5>
89  TH1 *histo1,
90  TT2 &t2,
91  TH1 *histo2,
92  TT3 &t3,
93  TH1 *histo3,
94  TT4 &t4,
95  TH1 *histo4,
96  TT5 &t5,
97  TH1 *histo5,
98  double rangeMin,
99  double rangeMax)
100  : chi1_(t1, histo1, rangeMin, rangeMax),
101  chi2_(t2, histo2, rangeMin, rangeMax),
102  chi3_(t3, histo3, rangeMin, rangeMax),
103  chi4_(t4, histo4, rangeMin, rangeMax),
104  chi5_(t5, histo5, rangeMin, rangeMax) {}
105  double operator()() const {
106  double chi2 = chi1_() + chi2_() + chi3_() + chi4_() + chi5_();
107  static size_t count = 0;
108  ++count;
109  return chi2;
110  }
111  void setHistos(TH1 *histo1, TH1 *histo2, TH1 *histo3, TH1 *histo4, TH1 *histo5) {
112  chi1_.setHistos(histo1);
113  chi2_.setHistos(histo2);
114  chi3_.setHistos(histo3);
115  chi4_.setHistos(histo4);
116  chi5_.setHistos(histo5);
117  }
118  size_t numberOfBins() const {
119  return chi1_.numberOfBins() + chi2_.numberOfBins() + chi3_.numberOfBins() + chi4_.numberOfBins() +
120  chi5_.numberOfBins();
121  }
122  T1 &function1() { return chi1_.function(); }
123  const T1 &function1() const { return chi1_.function(); }
124  T2 &function2() { return chi2_.function(); }
125  const T2 &function2() const { return chi2_.function(); }
126  T3 &function3() { return chi3_.function(); }
127  const T3 &function3() const { return chi3_.function(); }
128  T4 &function4() { return chi4_.function(); }
129  const T4 &function4() const { return chi4_.function(); }
130  T5 &function5() { return chi5_.function(); }
131  const T5 &function5() const { return chi5_.function(); }
132 
133  private:
134  T1 chi1_;
135  T2 chi2_;
136  T3 chi3_;
137  T4 chi4_;
138  T5 chi5_;
139  };
140 
141  template <typename T1, typename T2, typename T3, typename T4>
142  class MultiHistoChiSquare<T1, T2, T3, T4, helper::MultiHistoChiSquareNoArg, helper::MultiHistoChiSquareNoArg> {
143  public:
145  template <typename TT1, typename TT2, typename TT3, typename TT4>
147  TH1 *histo1,
148  TT2 &t2,
149  TH1 *histo2,
150  TT3 &t3,
151  TH1 *histo3,
152  TT4 &t4,
153  TH1 *histo4,
154  double rangeMin,
155  double rangeMax)
156  : chi1_(t1, histo1, rangeMin, rangeMax),
157  chi2_(t2, histo2, rangeMin, rangeMax),
158  chi3_(t3, histo3, rangeMin, rangeMax),
159  chi4_(t4, histo4, rangeMin, rangeMax) {}
160  double operator()() const {
161  double chi2 = chi1_() + chi2_() + chi3_() + chi4_();
162  static size_t count = 0;
163  ++count;
164  return chi2;
165  }
166  void setHistos(TH1 *histo1, TH1 *histo2, TH1 *histo3, TH1 *histo4) {
167  chi1_.setHistos(histo1);
168  chi2_.setHistos(histo2);
169  chi3_.setHistos(histo3);
170  chi4_.setHistos(histo4);
171  }
172  size_t numberOfBins() const {
173  return chi1_.numberOfBins() + chi2_.numberOfBins() + chi3_.numberOfBins() + chi4_.numberOfBins();
174  }
175  T1 &function1() { return chi1_.function(); }
176  const T1 &function1() const { return chi1_.function(); }
177  T2 &function2() { return chi2_.function(); }
178  const T2 &function2() const { return chi2_.function(); }
179  T3 &function3() { return chi3_.function(); }
180  const T3 &function3() const { return chi3_.function(); }
181  T4 &function4() { return chi4_.function(); }
182  const T4 &function4() const { return chi4_.function(); }
183 
184  private:
185  T1 chi1_;
186  T2 chi2_;
187  T3 chi3_;
188  T4 chi4_;
189  };
190 
191  template <typename T1, typename T2, typename T3>
193  T2,
194  T3,
195  helper::MultiHistoChiSquareNoArg,
198  public:
200  template <typename TT1, typename TT2, typename TT3>
202  TT1 &t1, TH1 *histo1, TT2 &t2, TH1 *histo2, TT3 &t3, TH1 *histo3, double rangeMin, double rangeMax)
203  : chi1_(t1, histo1, rangeMin, rangeMax),
204  chi2_(t2, histo2, rangeMin, rangeMax),
205  chi3_(t3, histo3, rangeMin, rangeMax) {}
206  double operator()() const { return chi1_() + chi2_() + chi3_(); }
207  void setHistos(TH1 *histo1, TH1 *histo2, TH1 *histo3) {
208  chi1_.setHistos(histo1);
209  chi2_.setHistos(histo2);
210  chi3_.setHistos(histo3);
211  }
212  size_t numberOfBins() const { return chi1_.numberOfBins() + chi2_.numberOfBins() + chi3_.numberOfBins(); }
213  T1 &function1() { return chi1_.function(); }
214  const T1 &function1() const { return chi1_.function(); }
215  T2 &function2() { return chi2_.function(); }
216  const T2 &function2() const { return chi2_.function(); }
217  T3 &function3() { return chi3_.function(); }
218  const T3 &function3() const { return chi3_.function(); }
219 
220  private:
221  T1 chi1_;
222  T2 chi2_;
223  T3 chi3_;
224  };
225 
226  template <typename T1, typename T2>
228  T2,
229  helper::MultiHistoChiSquareNoArg,
233  public:
235  template <typename TT1, typename TT2>
236  MultiHistoChiSquare(TT1 &t1, TH1 *histo1, TT2 &t2, TH1 *histo2, double rangeMin, double rangeMax)
237  : chi1_(t1, histo1, rangeMin, rangeMax), chi2_(t2, histo2, rangeMin, rangeMax) {}
238  double operator()() const { return chi1_() + chi2_(); }
239  void setHistos(TH1 *histo1, TH1 *histo2) {
240  chi1_.setHistos(histo1);
241  chi2_.setHistos(histo2);
242  }
243  size_t numberOfBins() const { return chi1_.numberOfBins() + chi2_.numberOfBins(); }
244 
245  private:
246  T1 chi1_;
247  T2 chi2_;
248  };
249 
250  template <typename T1>
252  helper::MultiHistoChiSquareNoArg,
257  public:
259  template <typename TT1>
260  MultiHistoChiSquare(TT1 &t1, TH1 *histo1, double rangeMin, double rangeMax)
261  : chi1_(t1, histo1, rangeMin, rangeMax) {}
262  double operator()() const { return chi1_(); }
263  void setHistos(TH1 *histo1) { chi1_.setHistos(histo1); }
264  size_t numberOfBins() const { return chi1_.numberOfBins(); }
265 
266  private:
267  T1 chi1_;
268  };
269 
270  template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
271  struct RootMinuitResultPrinter<MultiHistoChiSquare<T1, T2, T3, T4, T5, T6> > {
272  static void print(double amin,
273  unsigned int numberOfFreeParameters,
275  unsigned int ndof = f.numberOfBins() - numberOfFreeParameters;
276  std::cout << "chi-squared/n.d.o.f. = " << amin << "/" << ndof << " = " << amin / ndof
277  << "; prob: " << TMath::Prob(amin, ndof) << std::endl;
278  }
279  };
280 } // namespace fit
281 
282 #endif
Definition: helper.py:1
MultiHistoChiSquare(TT1 &t1, TH1 *histo1, TT2 &t2, TH1 *histo2, TT3 &t3, TH1 *histo3, double rangeMin, double rangeMax)
const T2 & function2() const
MultiHistoChiSquare(TT1 &t1, TH1 *histo1, TT2 &t2, TH1 *histo2, TT3 &t3, TH1 *histo3, TT4 &t4, TH1 *histo4, TT5 &t5, TH1 *histo5, double rangeMin, double rangeMax)
MultiHistoChiSquare(TT1 &t1, TH1 *histo1, TT2 &t2, TH1 *histo2, TT3 &t3, TH1 *histo3, TT4 &t4, TH1 *histo4, TT5 &t5, TH1 *histo5, TT6 &t6, TH1 *histo6, double rangeMin, double rangeMax)
double f[11][100]
const T4 & function4() const
const T6 & function6() const
void setHistos(TH1 *histo1, TH1 *histo2, TH1 *histo3, TH1 *histo4, TH1 *histo5, TH1 *histo6)
MultiHistoChiSquare(TT1 &t1, TH1 *histo1, TT2 &t2, TH1 *histo2, TT3 &t3, TH1 *histo3, TT4 &t4, TH1 *histo4, double rangeMin, double rangeMax)
const T1 & function1() const
const T3 & function3() const
const T5 & function5() const
void setHistos(TH1 *histo1, TH1 *histo2, TH1 *histo3, TH1 *histo4, TH1 *histo5)
static void print(double amin, unsigned int numberOfFreeParameters, const MultiHistoChiSquare< T1, T2, T3, T4, T5, T6 > &f)