CMS 3D CMS Logo

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