CMS 3D CMS Logo

FitWithRooFit.h
Go to the documentation of this file.
1 #ifndef Alignment_OfflineValidation_FitWithRooFit_h
2 #define Alignment_OfflineValidation_FitWithRooFit_h
3 
4 #ifndef __CINT__
5 #include "RooGlobalFunc.h"
6 #endif
7 #include "TCanvas.h"
8 #include "TTree.h"
9 #include "TH1D.h"
10 #include "TRandom.h"
11 #include "RooRealVar.h"
12 #include "RooDataSet.h"
13 #include "RooGaussian.h"
14 #include "RooVoigtian.h"
15 #include "RooExponential.h"
16 #include "RooPlot.h"
17 #include "RooDataHist.h"
18 #include "RooAddPdf.h"
19 #include "RooChebychev.h"
20 #include "RooGenericPdf.h"
21 #include "RooGaussModel.h"
22 #include "RooAddModel.h"
23 #include "RooPolynomial.h"
24 #include "RooCBShape.h"
25 #include "RooMinimizer.h"
26 #include "RooBreitWigner.h"
27 #include "RooFFTConvPdf.h"
28 
47 namespace {
48  typedef std::pair<RooRealVar, RooDataHist*> rooPair;
49 }
50 
52 public:
54  : useChi2_(false),
55  mean_(nullptr),
56  mean2_(nullptr),
57  mean3_(nullptr),
58  sigma_(nullptr),
59  sigma2_(nullptr),
60  sigma3_(nullptr),
61  gamma_(nullptr),
62  gaussFrac_(nullptr),
63  gaussFrac2_(nullptr),
64  expCoeffa0_(nullptr),
65  expCoeffa1_(nullptr),
66  expCoeffa2_(nullptr),
67  fsig_(nullptr),
68  a0_(nullptr),
69  a1_(nullptr),
70  a2_(nullptr),
71  a3_(nullptr),
72  a4_(nullptr),
73  a5_(nullptr),
74  a6_(nullptr),
75  alpha_(nullptr),
76  n_(nullptr),
77  fGCB_(nullptr) {}
78 
79  rooPair importTH1(TH1* histo, const double& inputXmin, const double& inputXmax);
80 
81  void fit(TH1* histo,
82  const TString signalType,
83  const TString backgroundType,
84  const double& xMin = 0.,
85  const double& xMax = 0.,
86  bool sumW2Error = false);
87 
88  void initMean(const double& value,
89  const double& min,
90  const double& max,
91  const TString& name = "mean",
92  const TString& title = "mean");
93 
94  void initMean2(const double& value,
95  const double& min,
96  const double& max,
97  const TString& name = "mean2",
98  const TString& title = "mean2");
99 
100  void initMean3(const double& value,
101  const double& min,
102  const double& max,
103  const TString& name = "mean3",
104  const TString& title = "mean3");
105 
106  void initSigma(const double& value,
107  const double& min,
108  const double& max,
109  const TString& name = "sigma",
110  const TString& title = "sigma");
111 
112  void initSigma2(const double& value,
113  const double& min,
114  const double& max,
115  const TString& name = "sigma2",
116  const TString& title = "sigma2");
117 
118  void initSigma3(const double& value,
119  const double& min,
120  const double& max,
121  const TString& name = "sigma3",
122  const TString& title = "sigma3");
123 
124  void initGamma(const double& value,
125  const double& min,
126  const double& max,
127  const TString& name = "gamma",
128  const TString& title = "gamma");
129 
130  void initGaussFrac(const double& value,
131  const double& min,
132  const double& max,
133  const TString& name = "GaussFrac",
134  const TString& title = "GaussFrac");
135 
136  void initGaussFrac2(const double& value,
137  const double& min,
138  const double& max,
139  const TString& name = "GaussFrac2",
140  const TString& title = "GaussFrac2");
141 
142  void initExpCoeffA0(const double& value,
143  const double& min,
144  const double& max,
145  const TString& name = "expCoeffa0",
146  const TString& title = "expCoeffa0");
147 
148  void initExpCoeffA1(const double& value,
149  const double& min,
150  const double& max,
151  const TString& name = "expCoeffa1",
152  const TString& title = "expCoeffa1");
153 
154  void initExpCoeffA2(const double& value,
155  const double& min,
156  const double& max,
157  const TString& name = "expCoeffa2",
158  const TString& title = "expCoeffa2");
159 
160  void initFsig(const double& value,
161  const double& min,
162  const double& max,
163  const TString& name = "fsig",
164  const TString& title = "signal fraction");
165 
166  void initA0(const double& value,
167  const double& min,
168  const double& max,
169  const TString& name = "a0",
170  const TString& title = "a0");
171 
172  void initA1(const double& value,
173  const double& min,
174  const double& max,
175  const TString& name = "a1",
176  const TString& title = "a1");
177 
178  void initA2(const double& value,
179  const double& min,
180  const double& max,
181  const TString& name = "a2",
182  const TString& title = "a2");
183 
184  void initA3(const double& value,
185  const double& min,
186  const double& max,
187  const TString& name = "a3",
188  const TString& title = "a3");
189 
190  void initA4(const double& value,
191  const double& min,
192  const double& max,
193  const TString& name = "a4",
194  const TString& title = "a4");
195 
196  void initA5(const double& value,
197  const double& min,
198  const double& max,
199  const TString& name = "a5",
200  const TString& title = "a5");
201 
202  void initA6(const double& value,
203  const double& min,
204  const double& max,
205  const TString& name = "a6",
206  const TString& title = "a6");
207 
208  void initAlpha(const double& value,
209  const double& min,
210  const double& max,
211  const TString& name = "alpha",
212  const TString& title = "alpha");
213 
214  void initN(
215  const double& value, const double& min, const double& max, const TString& name = "n", const TString& title = "n");
216 
217  void initFGCB(const double& value,
218  const double& min,
219  const double& max,
220  const TString& name = "fGCB",
221  const TString& title = "fGCB");
222 
223  inline RooRealVar* mean() { return mean_; }
224  inline RooRealVar* mean2() { return mean2_; }
225  inline RooRealVar* mean3() { return mean3_; }
226  inline RooRealVar* sigma() { return sigma_; }
227  inline RooRealVar* sigma2() { return sigma2_; }
228  inline RooRealVar* sigma3() { return sigma3_; }
229  inline RooRealVar* gamma() { return gamma_; }
230  inline RooRealVar* gaussFrac() { return gaussFrac_; }
231  inline RooRealVar* gaussFrac2() { return gaussFrac2_; }
232  inline RooRealVar* expCoeffa0() { return expCoeffa0_; }
233  inline RooRealVar* expCoeffa1() { return expCoeffa1_; }
234  inline RooRealVar* expCoeffa2() { return expCoeffa2_; }
235  inline RooRealVar* fsig() { return fsig_; }
236  inline RooRealVar* a0() { return a0_; }
237  inline RooRealVar* a1() { return a1_; }
238  inline RooRealVar* a2() { return a2_; }
239  inline RooRealVar* a3() { return a3_; }
240  inline RooRealVar* a4() { return a4_; }
241  inline RooRealVar* a5() { return a5_; }
242  inline RooRealVar* a6() { return a6_; }
243  inline RooRealVar* alpha() { return alpha_; }
244  inline RooRealVar* n() { return n_; }
245  inline RooRealVar* fGCB() { return fGCB_; }
246 
247  void reinitializeParameters();
248 
249  RooAbsPdf* buildSignalModel(RooRealVar* x, const TString& signalType);
250  RooAbsPdf* buildBackgroundModel(RooRealVar* x, const TString& backgroundType);
251  RooAbsPdf* buildModel(RooRealVar* x, const TString& signalType, const TString& backgroundType);
252 
253  bool useChi2_;
254 
255 protected:
256  // Declare all variables
257  RooRealVar* mean_;
258  RooRealVar* mean2_;
259  RooRealVar* mean3_;
260  RooRealVar* sigma_;
261  RooRealVar* sigma2_;
262  RooRealVar* sigma3_;
263  RooRealVar* gamma_;
264  RooRealVar* gaussFrac_;
265  RooRealVar* gaussFrac2_;
266  RooRealVar* expCoeffa0_;
267  RooRealVar* expCoeffa1_;
268  RooRealVar* expCoeffa2_;
269  RooRealVar* fsig_;
270  RooRealVar* a0_;
271  RooRealVar* a1_;
272  RooRealVar* a2_;
273  RooRealVar* a3_;
274  RooRealVar* a4_;
275  RooRealVar* a5_;
276  RooRealVar* a6_;
277  RooRealVar* alpha_;
278  RooRealVar* n_;
279  RooRealVar* fGCB_;
280 
281  // Initial values
282  double initVal_mean;
294  double initVal_fsig;
295  double initVal_a0;
296  double initVal_a1;
297  double initVal_a2;
298  double initVal_a3;
299  double initVal_a4;
300  double initVal_a5;
301  double initVal_a6;
303  double initVal_n;
304  double initVal_fGCB;
305 };
306 
307 #endif
void initGaussFrac2(const double &value, const double &min, const double &max, const TString &name="GaussFrac2", const TString &title="GaussFrac2")
void initGaussFrac(const double &value, const double &min, const double &max, const TString &name="GaussFrac", const TString &title="GaussFrac")
void initA6(const double &value, const double &min, const double &max, const TString &name="a6", const TString &title="a6")
RooRealVar * mean3()
RooRealVar * n_
RooRealVar * mean_
double initVal_expCoeffa0
RooRealVar * gaussFrac2_
RooRealVar * a0_
RooRealVar * sigma()
RooRealVar * sigma2_
double initVal_mean2
void initA1(const double &value, const double &min, const double &max, const TString &name="a1", const TString &title="a1")
RooRealVar * a4_
RooRealVar * sigma_
void initFsig(const double &value, const double &min, const double &max, const TString &name="fsig", const TString &title="signal fraction")
RooRealVar * expCoeffa0()
RooRealVar * fGCB_
void fit(TH1 *histo, const TString signalType, const TString backgroundType, const double &xMin=0., const double &xMax=0., bool sumW2Error=false)
RooAbsPdf * buildModel(RooRealVar *x, const TString &signalType, const TString &backgroundType)
Build the model to fit.
RooAbsPdf * buildSignalModel(RooRealVar *x, const TString &signalType)
Build the model for the specified signal type.
void initGamma(const double &value, const double &min, const double &max, const TString &name="gamma", const TString &title="gamma")
RooRealVar * mean2_
RooRealVar * a6()
RooRealVar * sigma3_
RooRealVar * a6_
RooRealVar * a3()
RooRealVar * expCoeffa2_
RooRealVar * fsig()
void initA3(const double &value, const double &min, const double &max, const TString &name="a3", const TString &title="a3")
double initVal_expCoeffa1
double initVal_mean
double initVal_gamma
double initVal_alpha
double initVal_gaussFrac2
RooRealVar * mean3_
void reinitializeParameters()
double initVal_sigma3
RooRealVar * gaussFrac()
RooRealVar * sigma3()
RooRealVar * sigma2()
RooRealVar * mean()
RooRealVar * gaussFrac2()
void initMean3(const double &value, const double &min, const double &max, const TString &name="mean3", const TString &title="mean3")
RooRealVar * a0()
RooRealVar * n()
double initVal_sigma2
RooRealVar * mean2()
void initA4(const double &value, const double &min, const double &max, const TString &name="a4", const TString &title="a4")
Definition: value.py:1
RooRealVar * expCoeffa1()
RooRealVar * gamma()
void initMean(const double &value, const double &min, const double &max, const TString &name="mean", const TString &title="mean")
RooAbsPdf * buildBackgroundModel(RooRealVar *x, const TString &backgroundType)
Build the model for the specified background type.
void initN(const double &value, const double &min, const double &max, const TString &name="n", const TString &title="n")
double initVal_expCoeffa2
RooRealVar * a1_
RooRealVar * alpha_
rooPair importTH1(TH1 *histo, const double &inputXmin, const double &inputXmax)
Definition: FitWithRooFit.cc:5
void initA0(const double &value, const double &min, const double &max, const TString &name="a0", const TString &title="a0")
void initAlpha(const double &value, const double &min, const double &max, const TString &name="alpha", const TString &title="alpha")
RooRealVar * fsig_
void initSigma3(const double &value, const double &min, const double &max, const TString &name="sigma3", const TString &title="sigma3")
RooRealVar * fGCB()
void initA5(const double &value, const double &min, const double &max, const TString &name="a5", const TString &title="a5")
RooRealVar * expCoeffa2()
void initExpCoeffA0(const double &value, const double &min, const double &max, const TString &name="expCoeffa0", const TString &title="expCoeffa0")
RooRealVar * a2_
double initVal_fGCB
RooRealVar * expCoeffa1_
void initExpCoeffA1(const double &value, const double &min, const double &max, const TString &name="expCoeffa1", const TString &title="expCoeffa1")
RooRealVar * a4()
RooRealVar * a3_
void initA2(const double &value, const double &min, const double &max, const TString &name="a2", const TString &title="a2")
double initVal_fsig
void initSigma2(const double &value, const double &min, const double &max, const TString &name="sigma2", const TString &title="sigma2")
void initFGCB(const double &value, const double &min, const double &max, const TString &name="fGCB", const TString &title="fGCB")
RooRealVar * alpha()
RooRealVar * a5_
RooRealVar * a1()
RooRealVar * a2()
void initExpCoeffA2(const double &value, const double &min, const double &max, const TString &name="expCoeffa2", const TString &title="expCoeffa2")
double initVal_gaussFrac
void initSigma(const double &value, const double &min, const double &max, const TString &name="sigma", const TString &title="sigma")
RooRealVar * a5()
void initMean2(const double &value, const double &min, const double &max, const TString &name="mean2", const TString &title="mean2")
double initVal_mean3
RooRealVar * gamma_
RooRealVar * expCoeffa0_
RooRealVar * gaussFrac_
double initVal_sigma