1 #ifndef FitWithRooFit_cc 2 #define FitWithRooFit_cc 5 #include "RooGlobalFunc.h" 11 #include "RooRealVar.h" 12 #include "RooDataSet.h" 13 #include "RooGaussian.h" 14 #include "RooVoigtian.h" 15 #include "RooExponential.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" 48 typedef std::pair<RooRealVar, RooDataHist*> rooPair;
80 rooPair
importTH1(TH1*
histo,
const double& inputXmin,
const double& inputXmax) {
81 double xMin = inputXmin;
82 double xMax = inputXmax;
90 return (std::make_pair(
x,
new RooDataHist(
"dh",
"dh",
x, RooFit::Import(*
histo))));
95 const TString signalType,
96 const TString backgroundType,
97 const double&
xMin = 0.,
98 const double&
xMax = 0.,
99 bool sumW2Error =
false) {
103 RooRealVar
x(imported.first);
104 RooDataHist*
dh = imported.second;
107 RooPlot*
frame =
x.frame(RooFit::Title(
"Imported TH1 with Poisson error bars"));
108 frame->SetName(TString(
histo->GetName()) +
"_frame");
114 std::unique_ptr<RooAbsReal>
chi2{
model->createChi2(*
dh, RooFit::DataError(RooAbsData::SumW2))};
121 model->fitTo(*
dh, RooFit::Save(), RooFit::SumW2Error(kTRUE));
127 std::cout <<
"FITTING WITH CHI^2" << std::endl;
128 RooMinimizer
m(*
chi2);
134 model->plotOn(
frame, RooFit::Components(backgroundType), RooFit::LineStyle(kDotted), RooFit::LineColor(kRed));
148 RooPlot* frame2 =
x.frame(RooFit::Title(
"Imported TH1 with internal errors"));
149 dh->plotOn(frame2, RooFit::DataError(RooAbsData::SumW2));
150 model->plotOn(frame2);
151 model->plotOn(frame2, RooFit::Components(backgroundType), RooFit::LineColor(kRed));
152 model->paramOn(frame2,
RooFit::Label(
"fit result"), RooFit::Format(
"NEU", RooFit::AutoPrecision(2)));
177 const TString&
name =
"mean",
178 const TString&
title =
"mean") {
179 if (
mean_ !=
nullptr)
187 const TString&
name =
"mean2",
188 const TString&
title =
"mean2") {
197 const TString&
name =
"mean3",
198 const TString&
title =
"mean3") {
207 const TString&
name =
"sigma",
208 const TString&
title =
"sigma") {
217 const TString&
name =
"sigma2",
218 const TString&
title =
"sigma2") {
227 const TString&
name =
"sigma3",
228 const TString&
title =
"sigma3") {
237 const TString&
name =
"gamma",
238 const TString&
title =
"gamma") {
247 const TString&
name =
"GaussFrac",
248 const TString&
title =
"GaussFrac") {
257 const TString&
name =
"GaussFrac2",
258 const TString&
title =
"GaussFrac2") {
267 const TString&
name =
"expCoeffa0",
268 const TString&
title =
"expCoeffa0") {
277 const TString&
name =
"expCoeffa1",
278 const TString&
title =
"expCoeffa1") {
287 const TString&
name =
"expCoeffa2",
288 const TString&
title =
"expCoeffa2") {
297 const TString&
name =
"fsig",
298 const TString&
title =
"signal fraction") {
299 if (
fsig_ !=
nullptr)
307 const TString&
name =
"a0",
308 const TString&
title =
"a0") {
317 const TString&
name =
"a1",
318 const TString&
title =
"a1") {
327 const TString&
name =
"a2",
328 const TString&
title =
"a2") {
337 const TString&
name =
"a3",
338 const TString&
title =
"a3") {
347 const TString&
name =
"a4",
348 const TString&
title =
"a4") {
357 const TString&
name =
"a5",
358 const TString&
title =
"a5") {
367 const TString&
name =
"a6",
368 const TString&
title =
"a6") {
377 const TString&
name =
"alpha",
378 const TString&
title =
"alpha") {
387 const TString&
name =
"n",
388 const TString&
title =
"n") {
397 const TString&
name =
"fGCB",
398 const TString&
title =
"fGCB") {
399 if (
fGCB_ !=
nullptr)
406 if (
mean_ !=
nullptr)
430 if (
fsig_ !=
nullptr)
450 if (
fGCB_ !=
nullptr)
467 inline RooRealVar*
a0() {
return a0_; }
468 inline RooRealVar*
a1() {
return a1_; }
469 inline RooRealVar*
a2() {
return a2_; }
470 inline RooRealVar*
a3() {
return a3_; }
471 inline RooRealVar*
a4() {
return a4_; }
472 inline RooRealVar*
a5() {
return a5_; }
473 inline RooRealVar*
a6() {
return a6_; }
475 inline RooRealVar*
n() {
return n_; }
480 RooAbsPdf* signal =
nullptr;
481 if (signalType ==
"gaussian") {
484 std::cout <<
"Error: one or more parameters are not initialized. Please be sure to initialize mean and sigma" 488 signal =
new RooGaussian(
"gauss",
"gauss", *
x, *
mean_, *
sigma_);
489 }
else if (signalType ==
"doubleGaussian") {
493 <<
"Error: one or more parameters are not initialized. Please be sure to initialize mean, sigma and sigma2" 497 RooGaussModel* gaussModel =
new RooGaussModel(
"gaussModel",
"gaussModel", *
x, *
mean_, *
sigma_);
498 RooGaussModel* gaussModel2 =
new RooGaussModel(
"gaussModel2",
"gaussModel2", *
x, *
mean_, *
sigma2_);
499 signal =
new RooAddModel(
"doubleGaussian",
"double gaussian", RooArgList(*gaussModel, *gaussModel2), *
gaussFrac_);
500 }
else if (signalType ==
"tripleGaussian") {
504 std::cout <<
"Error: one or more parameters are not initialized. Please be sure to initialize mean, mean2, " 505 "mean3, sigma, sigma2, sigma3" 509 RooGaussModel* gaussModel =
new RooGaussModel(
"gaussModel",
"gaussModel", *
x, *
mean_, *
sigma_);
510 RooGaussModel* gaussModel2 =
new RooGaussModel(
"gaussModel2",
"gaussModel2", *
x, *
mean2_, *
sigma2_);
511 RooGaussModel* gaussModel3 =
new RooGaussModel(
"gaussModel3",
"gaussModel3", *
x, *
mean3_, *
sigma3_);
512 signal =
new RooAddModel(
"tripleGaussian",
514 RooArgList(*gaussModel, *gaussModel2, *gaussModel3),
516 }
else if (signalType ==
"breitWigner") {
519 std::cout <<
"Error: one or more parameters are not initialized. Please be sure to initialize mean and gamma" 523 signal =
new RooBreitWigner(
"breiWign",
"breitWign", *
x, *
mean_, *
gamma_);
524 }
else if (signalType ==
"relBreitWigner") {
527 std::cout <<
"Error: one or more parameters are not initialized. Please be sure to initialize mean and gamma" 531 signal =
new RooGenericPdf(
"Relativistic Breit-Wigner",
533 "@0/(pow(@0*@0 - @1*@1,2) + @2*@2*@0*@0*@0*@0/(@1*@1))",
535 }
else if (signalType ==
"voigtian") {
539 <<
"Error: one or more parameters are not initialized. Please be sure to initialize mean, sigma and gamma" 544 }
else if (signalType ==
"crystalBall") {
547 std::cout <<
"Error: one or more parameters are not initialized. Please be sure to initialize mean, sigma, " 553 }
else if (signalType ==
"breitWignerTimesCB") {
556 (
alpha_ ==
nullptr) || (
n_ ==
nullptr)) {
557 std::cout <<
"Error: one or more parameters are not initialized. Please be sure to initialize mean, mean2, " 558 "sigma, gamma, alpha and n" 562 RooAbsPdf* bw =
new RooBreitWigner(
"breiWigner",
"breitWigner", *
x, *
mean_, *
gamma_);
564 signal =
new RooFFTConvPdf(
"breitWignerTimesCB",
"breitWignerTimesCB", *
x, *bw, *cb);
565 }
else if (signalType ==
"relBreitWignerTimesCB") {
568 (
alpha_ ==
nullptr) || (
n_ ==
nullptr)) {
569 std::cout <<
"Error: one or more parameters are not initialized. Please be sure to initialize mean, mean2, " 570 "sigma, gamma, alpha and n" 574 RooGenericPdf* bw =
new RooGenericPdf(
"Relativistic Breit-Wigner",
576 "@0/(pow(@0*@0 - @1*@1,2) + @2*@2*@0*@0*@0*@0/(@1*@1))",
579 signal =
new RooFFTConvPdf(
"relBreitWignerTimesCB",
"relBreitWignerTimesCB", *
x, *bw, *cb);
580 }
else if (signalType ==
"gaussianPlusCrystalBall") {
583 (
fGCB_ ==
nullptr)) {
584 std::cout <<
"Error: one or more parameters are not initialized. Please be sure to initialize mean, sigma, " 585 "sigma2, alpha, n and fGCB" 589 RooAbsPdf* tempCB =
new RooCBShape(
"crystalBall",
"crystalBall", *
x, *
mean_, *
sigma_, *
alpha_, *
n_);
590 RooAbsPdf* tempGaussian =
new RooGaussian(
"gauss",
"gauss", *
x, *
mean_, *
sigma2_);
592 signal =
new RooAddPdf(
593 "gaussianPlusCrystalBall",
"gaussianPlusCrystalBall", RooArgList(*tempCB, *tempGaussian), *
fGCB_);
594 }
else if (signalType ==
"voigtianPlusCrystalBall") {
598 std::cout <<
"Error: one or more parameters are not initialized. Please be sure to initialize mean, gamma, " 599 "sigma, sigma2, alpha, n and fGCB" 603 RooAbsPdf* tempVoigt =
new RooVoigtian(
"voigt",
"voigt", *
x, *
mean_, *
gamma_, *
sigma_);
604 RooAbsPdf* tempCB =
new RooCBShape(
"crystalBall",
"crystalBall", *
x, *
mean_, *
sigma2_, *
alpha_, *
n_);
607 new RooAddPdf(
"voigtianPlusCrystalBall",
"voigtianPlusCrystalBall", RooArgList(*tempCB, *tempVoigt), *
fGCB_);
608 }
else if (signalType ==
"breitWignerPlusCrystalBall") {
611 (
fGCB_ ==
nullptr)) {
612 std::cout <<
"Error: one or more parameters are not initialized. Please be sure to initialize mean, gamma, " 613 "sigma, alpha, n and fGCB" 617 RooAbsPdf* tempBW =
new RooBreitWigner(
"breitWign",
"breitWign", *
x, *
mean_, *
gamma_);
618 RooAbsPdf* tempCB =
new RooCBShape(
"crystalBall",
"crystalBall", *
x, *
mean_, *
sigma2_, *
alpha_, *
n_);
620 signal =
new RooAddPdf(
621 "breitWignerPlusCrystalBall",
"breitWignerPlusCrystalBall", RooArgList(*tempCB, *tempBW), *
fGCB_);
624 else if (signalType !=
"") {
625 std::cout <<
"Unknown signal function: " << signalType <<
". Signal will not be in the model" << std::endl;
632 RooAbsPdf* background =
nullptr;
633 if (backgroundType ==
"exponential") {
637 <<
"Error: one or more parameters are not initialized. Please be sure to initialize expCoeffa1 and fsig" 641 background =
new RooExponential(
"exponential",
"exponential", *
x, *
expCoeffa1_);
644 if (backgroundType ==
"exponentialpol") {
647 std::cout <<
"Error: one or more parameters are not initialized. Please be sure to initialize expCoeff and fsig" 651 background =
new RooGenericPdf(
"exponential",
653 "TMath::Exp(@1+@2*@0+@3*@0*@0)",
657 else if (backgroundType ==
"chebychev0") {
659 if (
a0_ ==
nullptr) {
660 std::cout <<
"Error: one or more parameters are not initialized. Please be sure to initialize a0" << std::endl;
663 background =
new RooChebychev(
"chebychev0",
"chebychev0", *
x, *
a0_);
664 }
else if (backgroundType ==
"chebychev1") {
666 if ((
a0_ ==
nullptr) || (
a1_ ==
nullptr)) {
667 std::cout <<
"Error: one or more parameters are not initialized. Please be sure to initialize a0 and a1" 671 background =
new RooChebychev(
"chebychev1",
"chebychev1", *
x, RooArgList(*
a0_, *
a1_));
672 }
else if (backgroundType ==
"chebychev3") {
674 if ((
a0_ ==
nullptr) || (
a1_ ==
nullptr) || (
a2_ ==
nullptr) || (
a3_ ==
nullptr)) {
675 std::cout <<
"Error: one or more parameters are not initialized. Please be sure to initialize a0, a1, a2 and a3" 679 background =
new RooChebychev(
"3rdOrderPol",
"3rdOrderPol", *
x, RooArgList(*
a0_, *
a1_, *
a2_, *
a3_));
682 else if (backgroundType ==
"chebychev6") {
684 if ((
a0_ ==
nullptr) || (
a1_ ==
nullptr) || (
a2_ ==
nullptr) || (
a3_ ==
nullptr) || (
a4_ ==
nullptr) ||
685 (
a5_ ==
nullptr) || (
a6_ ==
nullptr)) {
686 std::cout <<
"Error: one or more parameters are not initialized. Please be sure to initialize a0, a1, a2, a3, " 699 RooAbsPdf*
buildModel(RooRealVar*
x,
const TString& signalType,
const TString& backgroundType) {
700 RooAbsPdf*
model =
nullptr;
705 if ((signal !=
nullptr) && (background !=
nullptr)) {
707 std::cout <<
"Building model with signal and backgound" << std::endl;
708 model =
new RooAddPdf(
"model",
"model", RooArgList(*signal, *background), *
fsig_);
709 }
else if (signal !=
nullptr) {
710 std::cout <<
"Building model with signal" << std::endl;
712 }
else if (background !=
nullptr) {
713 std::cout <<
"Building model with backgound" << std::endl;
716 std::cout <<
"Nothing to fit" << std::endl;
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")
double initVal_expCoeffa0
void initA1(const double &value, const double &min, const double &max, const TString &name="a1", const TString &title="a1")
void initFsig(const double &value, const double &min, const double &max, const TString &name="fsig", const TString &title="signal fraction")
RooRealVar * expCoeffa0()
void fit(TH1 *histo, const TString signalType, const TString backgroundType, const double &xMin=0., const double &xMax=0., bool sumW2Error=false)
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")
void initA3(const double &value, const double &min, const double &max, const TString &name="a3", const TString &title="a3")
double initVal_expCoeffa1
double initVal_gaussFrac2
void reinitializeParameters()
RooRealVar * gaussFrac2()
void initMean3(const double &value, const double &min, const double &max, const TString &name="mean3", const TString &title="mean3")
void initA4(const double &value, const double &min, const double &max, const TString &name="a4", const TString &title="a4")
RooRealVar * expCoeffa1()
void initMean(const double &value, const double &min, const double &max, const TString &name="mean", const TString &title="mean")
void initN(const double &value, const double &min, const double &max, const TString &name="n", const TString &title="n")
double initVal_expCoeffa2
rooPair importTH1(TH1 *histo, const double &inputXmin, const double &inputXmax)
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")
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")
RooRealVar * expCoeffa2()
void initExpCoeffA0(const double &value, const double &min, const double &max, const TString &name="expCoeffa0", const TString &title="expCoeffa0")
RooAbsPdf * buildModel(RooRealVar *x, const TString &signalType, const TString &backgroundType)
Build the model to fit.
RooAbsPdf * buildBackgroundModel(RooRealVar *x, const TString &backgroundType)
Build the model for the specified background type.
void initExpCoeffA1(const double &value, const double &min, const double &max, const TString &name="expCoeffa1", const TString &title="expCoeffa1")
void initA2(const double &value, const double &min, const double &max, const TString &name="a2", const TString &title="a2")
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")
void initExpCoeffA2(const double &value, const double &min, const double &max, const TString &name="expCoeffa2", const TString &title="expCoeffa2")
void initSigma(const double &value, const double &min, const double &max, const TString &name="sigma", const TString &title="sigma")
void initMean2(const double &value, const double &min, const double &max, const TString &name="mean2", const TString &title="mean2")