CMS 3D CMS Logo

FitWithRooFit.cc
Go to the documentation of this file.
3 
4 // Import TH1 histogram into a RooDataHist
5 rooPair FitWithRooFit::importTH1(TH1* histo, const double& inputXmin, const double& inputXmax) {
6  double xMin = inputXmin;
7  double xMax = inputXmax;
8  if ((xMin == xMax) && xMin == 0) {
9  xMin = histo->GetXaxis()->GetXmin();
10  xMax = histo->GetXaxis()->GetXmax();
11  }
12  // Declare observable x
13  RooRealVar x("InvMass", "di-muon mass M(#mu^{+}#mu^{-}) [GeV]", xMin, xMax);
14  // Create a binned dataset that imports contents of TH1 and associates its contents to observable 'x'
15  return (std::make_pair(x, new RooDataHist("dh", "dh", x, RooFit::Import(*histo))));
16 }
17 
18 // Plot and fit a RooDataHist fitting signal and background
20  const TString signalType,
21  const TString backgroundType,
22  const double& xMin,
23  const double& xMax,
24  bool sumW2Error) {
26 
27  rooPair imported = importTH1(histo, xMin, xMax);
28  RooRealVar x(imported.first);
29  RooDataHist* dh = imported.second;
30 
31  // Make plot of binned dataset showing Poisson error bars (RooFit default)
32  RooPlot* frame = x.frame(RooFit::Title("di-muon mass M(#mu^{+}#mu^{-}) [GeV]"));
33  frame->SetName(TString(histo->GetName()) + "_frame");
34  dh->plotOn(frame);
35 
36  // Build the composite model
37  RooAbsPdf* model = buildModel(&x, signalType, backgroundType);
38 
39  std::unique_ptr<RooAbsReal> chi2{model->createChi2(*dh, RooFit::DataError(RooAbsData::SumW2))};
40 
41  // Fit the composite model
42  // -----------------------
43  // Fit with likelihood
44  if (!useChi2_) {
45  if (sumW2Error)
46  model->fitTo(*dh, RooFit::Save(), RooFit::SumW2Error(kTRUE));
47  else
48  model->fitTo(*dh);
49  }
50  // Fit with chi^2
51  else {
52  edm::LogWarning("FitWithRooFit") << "FITTING WITH CHI^2";
53  RooMinimizer m(*chi2);
54  m.migrad();
55  m.hesse();
56  // RooFitResult* r_chi2_wgt = m.save();
57  }
58  model->plotOn(frame, RooFit::LineColor(kRed));
59  model->plotOn(frame, RooFit::Components(backgroundType), RooFit::LineStyle(kDashed));
60  model->paramOn(frame,
61  RooFit::Label("fit result"),
62  RooFit::Layout(0.65, 0.90, 0.90),
63  RooFit::Format("NEU", RooFit::AutoPrecision(2)));
64 
65  // TODO: fix next lines to get the prob(chi2) (ndof should be dynamically set according to the choosen pdf)
66  // double chi2 = xframe.chiSquare("model","data",ndof);
67  // double ndoff = xframeGetNbinsX();
68  // double chi2prob = TMath::Prob(chi2,ndoff);
69 
70  // P l o t a n d f i t a R o o D a t a H i s t w i t h i n t e r n a l e r r o r s
71  // ---------------------------------------------------------------------------------------------
72 
73  // If histogram has custom error (i.e. its contents is does not originate from a Poisson process
74  // but e.g. is a sum of weighted events) you can data with symmetric 'sum-of-weights' error instead
75  // (same error bars as shown by ROOT)
76  RooPlot* frame2 = x.frame(RooFit::Title("di-muon mass M(#mu^{+}#mu^{-}) [GeV]"));
77  dh->plotOn(frame2, RooFit::DataError(RooAbsData::SumW2));
78  model->plotOn(frame2, RooFit::LineColor(kRed));
79  model->plotOn(frame2, RooFit::Components(backgroundType), RooFit::LineStyle(kDashed));
80  model->paramOn(frame2,
81  RooFit::Label("fit result"),
82  RooFit::Layout(0.65, 0.90, 0.90),
83  RooFit::Format("NEU", RooFit::AutoPrecision(2)));
84 
85  // Please note that error bars shown (Poisson or SumW2) are for visualization only, the are NOT used
86  // in a maximum likelihood fit
87  //
88  // A (binned) ML fit will ALWAYS assume the Poisson error interpretation of data (the mathematical definition
89  // of likelihood does not take any external definition of errors). Data with non-unit weights can only be correctly
90  // fitted with a chi^2 fit (see rf602_chi2fit.C)
91 
92  // Draw all frames on a canvas
93  // if( canvas == 0 ) {
94  // canvas = new TCanvas("rf102_dataimport","rf102_dataimport",800,800);
95  // canvas->cd(1);
96  // }
97  // canvas->Divide(2,1);
98  // canvas->cd(1) ; frame->Draw();
99  // canvas->cd(2) ; frame2->Draw();
100 
101  frame2->Draw();
102 }
103 
104 // Initialization methods for all the parameters
106  const double& value, const double& min, const double& max, const TString& name, const TString& title) {
107  if (mean_ != nullptr)
108  delete mean_;
109  mean_ = new RooRealVar(name, title, value, min, max);
111 }
112 
114  const double& value, const double& min, const double& max, const TString& name, const TString& title) {
115  if (mean2_ != nullptr)
116  delete mean2_;
117  mean2_ = new RooRealVar(name, title, value, min, max);
119 }
120 
122  const double& value, const double& min, const double& max, const TString& name, const TString& title) {
123  if (mean3_ != nullptr)
124  delete mean3_;
125  mean3_ = new RooRealVar(name, title, value, min, max);
127 }
128 
130  const double& value, const double& min, const double& max, const TString& name, const TString& title) {
131  if (sigma_ != nullptr)
132  delete sigma_;
133  sigma_ = new RooRealVar(name, title, value, min, max);
135 }
136 
138  const double& value, const double& min, const double& max, const TString& name, const TString& title) {
139  if (sigma2_ != nullptr)
140  delete sigma2_;
141  sigma2_ = new RooRealVar(name, title, value, min, max);
143 }
144 
146  const double& value, const double& min, const double& max, const TString& name, const TString& title) {
147  if (sigma3_ != nullptr)
148  delete sigma3_;
149  sigma3_ = new RooRealVar(name, title, value, min, max);
151 }
152 
154  const double& value, const double& min, const double& max, const TString& name, const TString& title) {
155  if (gamma_ != nullptr)
156  delete gamma_;
157  gamma_ = new RooRealVar(name, title, value, min, max);
159 }
160 
162  const double& value, const double& min, const double& max, const TString& name, const TString& title) {
163  if (gaussFrac_ != nullptr)
164  delete gaussFrac_;
165  gaussFrac_ = new RooRealVar(name, title, value, min, max);
167 }
168 
170  const double& value, const double& min, const double& max, const TString& name, const TString& title) {
171  if (gaussFrac2_ != nullptr)
172  delete gaussFrac2_;
173  gaussFrac2_ = new RooRealVar(name, title, value, min, max);
175 }
176 
178  const double& value, const double& min, const double& max, const TString& name, const TString& title) {
179  if (expCoeffa0_ != nullptr)
180  delete expCoeffa0_;
181  expCoeffa0_ = new RooRealVar(name, title, value, min, max);
183 }
184 
186  const double& value, const double& min, const double& max, const TString& name, const TString& title) {
187  if (expCoeffa1_ != nullptr)
188  delete expCoeffa1_;
189  expCoeffa1_ = new RooRealVar(name, title, value, min, max);
191 }
192 
194  const double& value, const double& min, const double& max, const TString& name, const TString& title) {
195  if (expCoeffa2_ != nullptr)
196  delete expCoeffa2_;
197  expCoeffa2_ = new RooRealVar(name, title, value, min, max);
199 }
200 
202  const double& value, const double& min, const double& max, const TString& name, const TString& title) {
203  if (fsig_ != nullptr)
204  delete fsig_;
205  fsig_ = new RooRealVar(name, title, value, min, max);
207 }
208 
210  const double& value, const double& min, const double& max, const TString& name, const TString& title) {
211  if (a0_ != nullptr)
212  delete a0_;
213  a0_ = new RooRealVar(name, title, value, min, max);
214  initVal_a0 = value;
215 }
216 
218  const double& value, const double& min, const double& max, const TString& name, const TString& title) {
219  if (a1_ != nullptr)
220  delete a1_;
221  a1_ = new RooRealVar(name, title, value, min, max);
222  initVal_a1 = value;
223 }
224 
226  const double& value, const double& min, const double& max, const TString& name, const TString& title) {
227  if (a2_ != nullptr)
228  delete a2_;
229  a2_ = new RooRealVar(name, title, value, min, max);
230  initVal_a2 = value;
231 }
232 
234  const double& value, const double& min, const double& max, const TString& name, const TString& title) {
235  if (a3_ != nullptr)
236  delete a3_;
237  a3_ = new RooRealVar(name, title, value, min, max);
238  initVal_a3 = value;
239 }
240 
242  const double& value, const double& min, const double& max, const TString& name, const TString& title) {
243  if (a4_ != nullptr)
244  delete a4_;
245  a4_ = new RooRealVar(name, title, value, min, max);
246  initVal_a4 = value;
247 }
248 
250  const double& value, const double& min, const double& max, const TString& name, const TString& title) {
251  if (a5_ != nullptr)
252  delete a5_;
253  a5_ = new RooRealVar(name, title, value, min, max);
254  initVal_a5 = value;
255 }
256 
258  const double& value, const double& min, const double& max, const TString& name, const TString& title) {
259  if (a6_ != nullptr)
260  delete a6_;
261  a6_ = new RooRealVar(name, title, value, min, max);
262  initVal_a6 = value;
263 }
264 
266  const double& value, const double& min, const double& max, const TString& name, const TString& title) {
267  if (alpha_ != nullptr)
268  delete alpha_;
269  alpha_ = new RooRealVar(name, title, value, min, max);
271 }
272 
274  const double& value, const double& min, const double& max, const TString& name, const TString& title) {
275  if (n_ != nullptr)
276  delete n_;
277  n_ = new RooRealVar(name, title, value, min, max);
278  initVal_n = value;
279 }
280 
282  const double& value, const double& min, const double& max, const TString& name, const TString& title) {
283  if (fGCB_ != nullptr)
284  delete fGCB_;
285  fGCB_ = new RooRealVar(name, title, value, min, max);
287 }
288 
290  if (mean_ != nullptr)
291  mean_->setVal(initVal_mean);
292  if (mean2_ != nullptr)
293  mean2_->setVal(initVal_mean2);
294  if (mean3_ != nullptr)
295  mean3_->setVal(initVal_mean3);
296  if (sigma_ != nullptr)
297  sigma_->setVal(initVal_sigma);
298  if (sigma2_ != nullptr)
299  sigma2_->setVal(initVal_sigma2);
300  if (sigma3_ != nullptr)
301  sigma3_->setVal(initVal_sigma3);
302  if (gamma_ != nullptr)
303  gamma_->setVal(initVal_gamma);
304  if (gaussFrac_ != nullptr)
305  gaussFrac_->setVal(initVal_gaussFrac);
306  if (gaussFrac2_ != nullptr)
308  if (expCoeffa0_ != nullptr)
310  if (expCoeffa1_ != nullptr)
312  if (expCoeffa2_ != nullptr)
314  if (fsig_ != nullptr)
315  fsig_->setVal(initVal_fsig);
316  if (a0_ != nullptr)
317  a0_->setVal(initVal_a0);
318  if (a1_ != nullptr)
319  a1_->setVal(initVal_a1);
320  if (a2_ != nullptr)
321  a2_->setVal(initVal_a2);
322  if (a3_ != nullptr)
323  a3_->setVal(initVal_a3);
324  if (a4_ != nullptr)
325  a4_->setVal(initVal_a4);
326  if (a5_ != nullptr)
327  a5_->setVal(initVal_a5);
328  if (a6_ != nullptr)
329  a6_->setVal(initVal_a6);
330  if (alpha_ != nullptr)
331  alpha_->setVal(initVal_alpha);
332  if (n_ != nullptr)
333  n_->setVal(initVal_n);
334  if (fGCB_ != nullptr)
335  fGCB_->setVal(initVal_fGCB);
336 }
337 
339 RooAbsPdf* FitWithRooFit::buildSignalModel(RooRealVar* x, const TString& signalType) {
340  RooAbsPdf* signal = nullptr;
341  if (signalType == "gaussian") {
342  // Fit a Gaussian p.d.f to the data
343  if ((mean_ == nullptr) || (sigma_ == nullptr)) {
344  edm::LogError("FitWithRooFit")
345  << "Error: one or more parameters are not initialized. Please be sure to initialize mean and sigma";
346  exit(1);
347  }
348  signal = new RooGaussian("gauss", "gauss", *x, *mean_, *sigma_);
349  } else if (signalType == "doubleGaussian") {
350  // Fit with double gaussian
351  if ((mean_ == nullptr) || (sigma_ == nullptr) || (sigma2_ == nullptr)) {
352  edm::LogError("FitWithRooFit")
353  << "Error: one or more parameters are not initialized. Please be sure to initialize mean, sigma and sigma2";
354  exit(1);
355  }
356  RooGaussModel* gaussModel = new RooGaussModel("gaussModel", "gaussModel", *x, *mean_, *sigma_);
357  RooGaussModel* gaussModel2 = new RooGaussModel("gaussModel2", "gaussModel2", *x, *mean_, *sigma2_);
358  signal = new RooAddModel("doubleGaussian", "double gaussian", RooArgList(*gaussModel, *gaussModel2), *gaussFrac_);
359  } else if (signalType == "tripleGaussian") {
360  // Fit with triple gaussian
361  if ((mean_ == nullptr) || (mean2_ == nullptr) || (mean3_ == nullptr) || (sigma_ == nullptr) ||
362  (sigma2_ == nullptr) || (sigma3_ == nullptr)) {
363  edm::LogError("FitWithRooFit")
364  << "Error: one or more parameters are not initialized. Please be sure to initialize mean, mean2, "
365  "mean3, sigma, sigma2, sigma3";
366  exit(1);
367  }
368  RooGaussModel* gaussModel = new RooGaussModel("gaussModel", "gaussModel", *x, *mean_, *sigma_);
369  RooGaussModel* gaussModel2 = new RooGaussModel("gaussModel2", "gaussModel2", *x, *mean2_, *sigma2_);
370  RooGaussModel* gaussModel3 = new RooGaussModel("gaussModel3", "gaussModel3", *x, *mean3_, *sigma3_);
371  signal = new RooAddModel("tripleGaussian",
372  "triple gaussian",
373  RooArgList(*gaussModel, *gaussModel2, *gaussModel3),
374  RooArgList(*gaussFrac_, *gaussFrac2_));
375  } else if (signalType == "breitWigner") {
376  // Fit a Breit-Wigner
377  if ((mean_ == nullptr) || (gamma_ == nullptr)) {
378  edm::LogError("FitWithRooFit")
379  << "Error: one or more parameters are not initialized. Please be sure to initialize mean and gamma";
380  exit(1);
381  }
382  signal = new RooBreitWigner("breiWign", "breitWign", *x, *mean_, *gamma_);
383  } else if (signalType == "relBreitWigner") {
384  // Fit a relativistic Breit-Wigner
385  if ((mean_ == nullptr) || (gamma_ == nullptr)) {
386  edm::LogError("FitWithRooFit")
387  << "Error: one or more parameters are not initialized. Please be sure to initialize mean and gamma";
388  exit(1);
389  }
390  signal = new RooGenericPdf("Relativistic Breit-Wigner",
391  "RBW",
392  "@0/(pow(@0*@0 - @1*@1,2) + @2*@2*@0*@0*@0*@0/(@1*@1))",
393  RooArgList(*x, *mean_, *gamma_));
394  } else if (signalType == "voigtian") {
395  // Fit a Voigtian
396  if ((mean_ == nullptr) || (sigma_ == nullptr) || (gamma_ == nullptr)) {
397  edm::LogError("FitWithRooFit")
398  << "Error: one or more parameters are not initialized. Please be sure to initialize mean, sigma and gamma";
399  exit(1);
400  }
401  signal = new RooVoigtian("voigt", "voigt", *x, *mean_, *gamma_, *sigma_);
402  } else if (signalType == "crystalBall") {
403  // Fit a CrystalBall
404  if ((mean_ == nullptr) || (sigma_ == nullptr) || (alpha_ == nullptr) || (n_ == nullptr)) {
405  edm::LogError("FitWithRooFit")
406  << "Error: one or more parameters are not initialized. Please be sure to initialize mean, sigma, "
407  "alpha and n";
408  exit(1);
409  }
410  signal = new RooCBShape("crystalBall", "crystalBall", *x, *mean_, *sigma_, *alpha_, *n_);
411  } else if (signalType == "breitWignerTimesCB") {
412  // Fit a Breit Wigner convoluted with a CrystalBall
413  if ((mean_ == nullptr) || (mean2_ == nullptr) || (sigma_ == nullptr) || (gamma_ == nullptr) ||
414  (alpha_ == nullptr) || (n_ == nullptr)) {
415  edm::LogError("FitWithRooFit")
416  << "Error: one or more parameters are not initialized. Please be sure to initialize mean, mean2, "
417  "sigma, gamma, alpha and n";
418  exit(1);
419  }
420  RooAbsPdf* bw = new RooBreitWigner("breiWigner", "breitWigner", *x, *mean_, *gamma_);
421  RooAbsPdf* cb = new RooCBShape("crystalBall", "crystalBall", *x, *mean2_, *sigma_, *alpha_, *n_);
422  signal = new RooFFTConvPdf("breitWignerTimesCB", "breitWignerTimesCB", *x, *bw, *cb);
423  } else if (signalType == "relBreitWignerTimesCB") {
424  // Fit a relativistic Breit Wigner convoluted with a CrystalBall
425  if ((mean_ == nullptr) || (mean2_ == nullptr) || (sigma_ == nullptr) || (gamma_ == nullptr) ||
426  (alpha_ == nullptr) || (n_ == nullptr)) {
427  edm::LogError("FitWithRooFit")
428  << "Error: one or more parameters are not initialized. Please be sure to initialize mean, mean2, "
429  "sigma, gamma, alpha and n";
430  exit(1);
431  }
432  RooGenericPdf* bw = new RooGenericPdf("Relativistic Breit-Wigner",
433  "RBW",
434  "@0/(pow(@0*@0 - @1*@1,2) + @2*@2*@0*@0*@0*@0/(@1*@1))",
435  RooArgList(*x, *mean_, *gamma_));
436  RooAbsPdf* cb = new RooCBShape("crystalBall", "crystalBall", *x, *mean2_, *sigma_, *alpha_, *n_);
437  signal = new RooFFTConvPdf("relBreitWignerTimesCB", "relBreitWignerTimesCB", *x, *bw, *cb);
438  } else if (signalType == "gaussianPlusCrystalBall") {
439  // Fit a Gaussian + CrystalBall with the same mean
440  if ((mean_ == nullptr) || (sigma_ == nullptr) || (alpha_ == nullptr) || (n_ == nullptr) || (sigma2_ == nullptr) ||
441  (fGCB_ == nullptr)) {
442  edm::LogError("FitWithRooFit")
443  << "Error: one or more parameters are not initialized. Please be sure to initialize mean, sigma, "
444  "sigma2, alpha, n and fGCB";
445  exit(1);
446  }
447  RooAbsPdf* tempCB = new RooCBShape("crystalBall", "crystalBall", *x, *mean_, *sigma_, *alpha_, *n_);
448  RooAbsPdf* tempGaussian = new RooGaussian("gauss", "gauss", *x, *mean_, *sigma2_);
449 
450  signal =
451  new RooAddPdf("gaussianPlusCrystalBall", "gaussianPlusCrystalBall", RooArgList(*tempCB, *tempGaussian), *fGCB_);
452  } else if (signalType == "voigtianPlusCrystalBall") {
453  // Fit a Voigtian + CrystalBall with the same mean
454  if ((mean_ == nullptr) || (sigma_ == nullptr) || (gamma_ == nullptr) || (alpha_ == nullptr) || (n_ == nullptr) ||
455  (sigma2_ == nullptr) || (fGCB_ == nullptr)) {
456  edm::LogError("FitWithRooFit")
457  << "Error: one or more parameters are not initialized. Please be sure to initialize mean, gamma, "
458  "sigma, sigma2, alpha, n and fGCB";
459  exit(1);
460  }
461  RooAbsPdf* tempVoigt = new RooVoigtian("voigt", "voigt", *x, *mean_, *gamma_, *sigma_);
462  RooAbsPdf* tempCB = new RooCBShape("crystalBall", "crystalBall", *x, *mean_, *sigma2_, *alpha_, *n_);
463 
464  signal =
465  new RooAddPdf("voigtianPlusCrystalBall", "voigtianPlusCrystalBall", RooArgList(*tempCB, *tempVoigt), *fGCB_);
466  } else if (signalType == "breitWignerPlusCrystalBall") {
467  // Fit a Breit-Wigner + CrystalBall with the same mean
468  if ((mean_ == nullptr) || (gamma_ == nullptr) || (alpha_ == nullptr) || (n_ == nullptr) || (sigma2_ == nullptr) ||
469  (fGCB_ == nullptr)) {
470  edm::LogError("FitWithRooFit")
471  << "Error: one or more parameters are not initialized. Please be sure to initialize mean, gamma, "
472  "sigma, alpha, n and fGCB";
473  exit(1);
474  }
475  RooAbsPdf* tempBW = new RooBreitWigner("breitWign", "breitWign", *x, *mean_, *gamma_);
476  RooAbsPdf* tempCB = new RooCBShape("crystalBall", "crystalBall", *x, *mean_, *sigma2_, *alpha_, *n_);
477 
478  signal =
479  new RooAddPdf("breitWignerPlusCrystalBall", "breitWignerPlusCrystalBall", RooArgList(*tempCB, *tempBW), *fGCB_);
480  }
481 
482  else if (signalType != "") {
483  edm::LogError("FitWithRooFit") << "Unknown signal function: " << signalType << ". Signal will not be in the model";
484  }
485  return signal;
486 }
487 
489 RooAbsPdf* FitWithRooFit::buildBackgroundModel(RooRealVar* x, const TString& backgroundType) {
490  RooAbsPdf* background = nullptr;
491  if (backgroundType == "exponential") {
492  // Add an exponential for the background
493  if ((expCoeffa1_ == nullptr) || (fsig_ == nullptr)) {
494  edm::LogError("FitWithRooFit")
495  << "Error: one or more parameters are not initialized. Please be sure to initialize expCoeffa1 and fsig";
496  exit(1);
497  }
498  background = new RooExponential("exponential", "exponential", *x, *expCoeffa1_);
499  }
500 
501  if (backgroundType == "exponentialpol") {
502  // Add an exponential for the background
503  if ((expCoeffa0_ == nullptr) || (expCoeffa1_ == nullptr) || (expCoeffa2_ == nullptr) || (fsig_ == nullptr)) {
504  edm::LogError("FitWithRooFit")
505  << "Error: one or more parameters are not initialized. Please be sure to initialize expCoeff and fsig";
506  exit(1);
507  }
508  background = new RooGenericPdf("exponential",
509  "exponential",
510  "TMath::Exp(@1+@2*@0+@3*@0*@0)",
511  RooArgList(*x, *expCoeffa0_, *expCoeffa1_, *expCoeffa2_));
512  }
513 
514  else if (backgroundType == "chebychev0") {
515  // Add a linear background
516  if (a0_ == nullptr) {
517  edm::LogError("FitWithRooFit")
518  << "Error: one or more parameters are not initialized. Please be sure to initialize a0";
519  exit(1);
520  }
521  background = new RooChebychev("chebychev0", "chebychev0", *x, *a0_);
522  } else if (backgroundType == "chebychev1") {
523  // Add a 2nd order chebychev polynomial background
524  if ((a0_ == nullptr) || (a1_ == nullptr)) {
525  edm::LogError("FitWithRooFit")
526  << "Error: one or more parameters are not initialized. Please be sure to initialize a0 and a1";
527  exit(1);
528  }
529  background = new RooChebychev("chebychev1", "chebychev1", *x, RooArgList(*a0_, *a1_));
530  } else if (backgroundType == "chebychev3") {
531  // Add a 3rd order chebychev polynomial background
532  if ((a0_ == nullptr) || (a1_ == nullptr) || (a2_ == nullptr) || (a3_ == nullptr)) {
533  edm::LogError("FitWithRooFit")
534  << "Error: one or more parameters are not initialized. Please be sure to initialize a0, a1, a2 and a3";
535  exit(1);
536  }
537  background = new RooChebychev("3rdOrderPol", "3rdOrderPol", *x, RooArgList(*a0_, *a1_, *a2_, *a3_));
538  }
539 
540  else if (backgroundType == "chebychev6") {
541  // Add a 6th order chebychev polynomial background
542  if ((a0_ == nullptr) || (a1_ == nullptr) || (a2_ == nullptr) || (a3_ == nullptr) || (a4_ == nullptr) ||
543  (a5_ == nullptr) || (a6_ == nullptr)) {
544  edm::LogError("FitWithRooFit")
545  << "Error: one or more parameters are not initialized. Please be sure to initialize a0, a1, a2, a3, "
546  "a4, a5 and a6";
547  exit(1);
548  }
549  background =
550  new RooChebychev("6thOrderPol", "6thOrderPol", *x, RooArgList(*a0_, *a1_, *a2_, *a3_, *a4_, *a5_, *a6_));
551  }
552 
553  return background;
554 }
555 
557 RooAbsPdf* FitWithRooFit::buildModel(RooRealVar* x, const TString& signalType, const TString& backgroundType) {
558  RooAbsPdf* model = nullptr;
559 
560  RooAbsPdf* signal = buildSignalModel(x, signalType);
561  RooAbsPdf* background = buildBackgroundModel(x, backgroundType);
562 
563  if ((signal != nullptr) && (background != nullptr)) {
564  // Combine signal and background pdfs
565  edm::LogPrint("FitWithRooFit") << "Building model with signal and backgound";
566  model = new RooAddPdf("model", "model", RooArgList(*signal, *background), *fsig_);
567  } else if (signal != nullptr) {
568  edm::LogPrint("FitWithRooFit") << "Building model with signal";
569  model = signal;
570  } else if (background != nullptr) {
571  edm::LogPrint("FitWithRooFit") << "Building model with backgound";
572  model = background;
573  } else {
574  edm::LogWarning("FitWithRooFit") << "Nothing to fit";
575  exit(0);
576  }
577  return model;
578 }
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 * n_
RooRealVar * mean_
double initVal_expCoeffa0
RooRealVar * gaussFrac2_
RooRealVar * a0_
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 * 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")
Log< level::Error, false > LogError
RooRealVar * mean2_
RooRealVar * sigma3_
RooRealVar * a6_
RooRealVar * expCoeffa2_
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
void initMean3(const double &value, const double &min, const double &max, const TString &name="mean3", const TString &title="mean3")
double initVal_sigma2
void initA4(const double &value, const double &min, const double &max, const TString &name="a4", const TString &title="a4")
Definition: value.py:1
Log< level::Warning, true > LogPrint
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")
void initA5(const double &value, const double &min, const double &max, const TString &name="a5", const TString &title="a5")
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 * 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")
float x
RooRealVar * a5_
void initExpCoeffA2(const double &value, const double &min, const double &max, const TString &name="expCoeffa2", const TString &title="expCoeffa2")
Log< level::Warning, false > LogWarning
double initVal_gaussFrac
void initSigma(const double &value, const double &min, const double &max, const TString &name="sigma", const TString &title="sigma")
dh
Definition: cuy.py:354
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
def exit(msg="")