12 RooRealVar
x(
"InvMass",
"di-muon mass M(#mu^{+}#mu^{-}) [GeV]",
xMin,
xMax);
14 return std::make_unique<RooDataHist>(
"dh",
"dh",
x, RooFit::Import(*
histo));
19 TH1*
histo,
const TString signalType,
const TString backgroundType,
double xMin,
double xMax,
bool sumW2Error) {
23 RooRealVar
x(*static_cast<RooRealVar*>(
dh->get()->find(
"x")));
26 RooPlot*
frame =
x.frame(RooFit::Title(
"di-muon mass M(#mu^{+}#mu^{-}) [GeV]"));
27 frame->SetName(TString(
histo->GetName()) +
"_frame");
31 std::unique_ptr<RooAbsPdf>
model =
buildModel(&
x, signalType, backgroundType);
33 std::unique_ptr<RooAbsReal>
chi2{
model->createChi2(*
dh, RooFit::DataError(RooAbsData::SumW2))};
39 model->fitTo(*
dh, RooFit::SumW2Error(sumW2Error));
44 RooMinimizer
m(*
chi2);
50 model->plotOn(
frame, RooFit::Components(backgroundType), RooFit::LineStyle(kDashed));
53 RooFit::Layout(0.65, 0.90, 0.90),
54 RooFit::Format(
"NEU", RooFit::AutoPrecision(2)));
67 RooPlot* frame2 =
x.frame(RooFit::Title(
"di-muon mass M(#mu^{+}#mu^{-}) [GeV]"));
68 dh->plotOn(frame2, RooFit::DataError(RooAbsData::SumW2));
69 model->plotOn(frame2, RooFit::LineColor(kRed));
70 model->plotOn(frame2, RooFit::Components(backgroundType), RooFit::LineStyle(kDashed));
71 model->paramOn(frame2,
73 RooFit::Layout(0.65, 0.90, 0.90),
74 RooFit::Format(
"NEU", RooFit::AutoPrecision(2)));
212 auto initParam = [](std::unique_ptr<RooRealVar>&
var,
double val) {
244 if (signalType ==
"gaussian") {
248 <<
"Error: one or more parameters are not initialized. Please be sure to initialize mean and sigma";
251 return std::make_unique<RooGaussian>(
"gauss",
"gauss", *
x, *
mean_, *
sigma_);
252 }
else if (signalType ==
"doubleGaussian") {
256 <<
"Error: one or more parameters are not initialized. Please be sure to initialize mean, sigma and sigma2";
259 RooGaussModel* gaussModel =
new RooGaussModel(
"gaussModel",
"gaussModel", *
x, *
mean_, *
sigma_);
260 RooGaussModel* gaussModel2 =
new RooGaussModel(
"gaussModel2",
"gaussModel2", *
x, *
mean_, *
sigma2_);
261 RooArgList
components{*gaussModel, *gaussModel2};
265 }
else if (signalType ==
"tripleGaussian") {
270 <<
"Error: one or more parameters are not initialized. Please be sure to initialize mean, mean2, " 271 "mean3, sigma, sigma2, sigma3";
274 RooGaussModel* gaussModel =
new RooGaussModel(
"gaussModel",
"gaussModel", *
x, *
mean_, *
sigma_);
275 RooGaussModel* gaussModel2 =
new RooGaussModel(
"gaussModel2",
"gaussModel2", *
x, *
mean2_, *
sigma2_);
276 RooGaussModel* gaussModel3 =
new RooGaussModel(
"gaussModel3",
"gaussModel3", *
x, *
mean3_, *
sigma3_);
277 RooArgList
components{*gaussModel, *gaussModel2, *gaussModel3};
278 auto out = std::make_unique<RooAddModel>(
282 }
else if (signalType ==
"breitWigner") {
286 <<
"Error: one or more parameters are not initialized. Please be sure to initialize mean and gamma";
289 return std::make_unique<RooBreitWigner>(
"breiWign",
"breitWign", *
x, *
mean_, *
gamma_);
290 }
else if (signalType ==
"relBreitWigner") {
294 <<
"Error: one or more parameters are not initialized. Please be sure to initialize mean and gamma";
297 return std::make_unique<RooGenericPdf>(
"Relativistic Breit-Wigner",
299 "@0/(pow(@0*@0 - @1*@1,2) + @2*@2*@0*@0*@0*@0/(@1*@1))",
301 }
else if (signalType ==
"voigtian") {
305 <<
"Error: one or more parameters are not initialized. Please be sure to initialize mean, sigma and gamma";
309 }
else if (signalType ==
"crystalBall") {
313 <<
"Error: one or more parameters are not initialized. Please be sure to initialize mean, sigma, " 317 return std::make_unique<RooCBShape>(
"crystalBall",
"crystalBall", *
x, *
mean_, *
sigma_, *
alpha_, *
n_);
318 }
else if (signalType ==
"breitWignerTimesCB") {
321 (
alpha_ ==
nullptr) || (
n_ ==
nullptr)) {
323 <<
"Error: one or more parameters are not initialized. Please be sure to initialize mean, mean2, " 324 "sigma, gamma, alpha and n";
327 RooAbsPdf* bw =
new RooBreitWigner(
"breiWigner",
"breitWigner", *
x, *
mean_, *
gamma_);
329 auto out = std::make_unique<RooFFTConvPdf>(
"breitWignerTimesCB",
"breitWignerTimesCB", *
x, *bw, *cb);
330 out->addOwnedComponents({*bw, *cb});
332 }
else if (signalType ==
"relBreitWignerTimesCB") {
335 (
alpha_ ==
nullptr) || (
n_ ==
nullptr)) {
337 <<
"Error: one or more parameters are not initialized. Please be sure to initialize mean, mean2, " 338 "sigma, gamma, alpha and n";
341 RooGenericPdf* bw =
new RooGenericPdf(
"Relativistic Breit-Wigner",
343 "@0/(pow(@0*@0 - @1*@1,2) + @2*@2*@0*@0*@0*@0/(@1*@1))",
346 auto out = std::make_unique<RooFFTConvPdf>(
"relBreitWignerTimesCB",
"relBreitWignerTimesCB", *
x, *bw, *cb);
347 out->addOwnedComponents({*bw, *cb});
349 }
else if (signalType ==
"gaussianPlusCrystalBall") {
352 (
fGCB_ ==
nullptr)) {
354 <<
"Error: one or more parameters are not initialized. Please be sure to initialize mean, sigma, " 355 "sigma2, alpha, n and fGCB";
358 RooAbsPdf* tempCB =
new RooCBShape(
"crystalBall",
"crystalBall", *
x, *
mean_, *
sigma_, *
alpha_, *
n_);
359 RooAbsPdf* tempGaussian =
new RooGaussian(
"gauss",
"gauss", *
x, *
mean_, *
sigma2_);
360 RooArgList
components{*tempCB, *tempGaussian};
362 auto out = std::make_unique<RooAddPdf>(
"gaussianPlusCrystalBall",
"gaussianPlusCrystalBall",
components, *
fGCB_);
365 }
else if (signalType ==
"voigtianPlusCrystalBall") {
370 <<
"Error: one or more parameters are not initialized. Please be sure to initialize mean, gamma, " 371 "sigma, sigma2, alpha, n and fGCB";
374 RooAbsPdf* tempVoigt =
new RooVoigtian(
"voigt",
"voigt", *
x, *
mean_, *
gamma_, *
sigma_);
375 RooAbsPdf* tempCB =
new RooCBShape(
"crystalBall",
"crystalBall", *
x, *
mean_, *
sigma2_, *
alpha_, *
n_);
378 auto out = std::make_unique<RooAddPdf>(
"voigtianPlusCrystalBall",
"voigtianPlusCrystalBall",
components, *
fGCB_);
381 }
else if (signalType ==
"breitWignerPlusCrystalBall") {
384 (
fGCB_ ==
nullptr)) {
386 <<
"Error: one or more parameters are not initialized. Please be sure to initialize mean, gamma, " 387 "sigma, alpha, n and fGCB";
390 RooAbsPdf* tempBW =
new RooBreitWigner(
"breitWign",
"breitWign", *
x, *
mean_, *
gamma_);
391 RooAbsPdf* tempCB =
new RooCBShape(
"crystalBall",
"crystalBall", *
x, *
mean_, *
sigma2_, *
alpha_, *
n_);
395 std::make_unique<RooAddPdf>(
"breitWignerPlusCrystalBall",
"breitWignerPlusCrystalBall",
components, *
fGCB_);
400 else if (signalType !=
"") {
401 edm::LogError(
"FitWithRooFit") <<
"Unknown signal function: " << signalType <<
". Signal will not be in the model";
408 if (backgroundType ==
"exponential") {
412 <<
"Error: one or more parameters are not initialized. Please be sure to initialize expCoeffa1 and fsig";
415 return std::make_unique<RooExponential>(
"exponential",
"exponential", *
x, *
expCoeffa1_);
418 if (backgroundType ==
"exponentialpol") {
422 <<
"Error: one or more parameters are not initialized. Please be sure to initialize expCoeff and fsig";
425 return std::make_unique<RooGenericPdf>(
"exponential",
427 "TMath::Exp(@1+@2*@0+@3*@0*@0)",
431 else if (backgroundType ==
"chebychev0") {
433 if (
a0_ ==
nullptr) {
435 <<
"Error: one or more parameters are not initialized. Please be sure to initialize a0";
438 return std::make_unique<RooChebychev>(
"chebychev0",
"chebychev0", *
x, *
a0_);
439 }
else if (backgroundType ==
"chebychev1") {
441 if ((
a0_ ==
nullptr) || (
a1_ ==
nullptr)) {
443 <<
"Error: one or more parameters are not initialized. Please be sure to initialize a0 and a1";
446 return std::make_unique<RooChebychev>(
"chebychev1",
"chebychev1", *
x, RooArgList{*
a0_, *
a1_});
447 }
else if (backgroundType ==
"chebychev3") {
449 if ((
a0_ ==
nullptr) || (
a1_ ==
nullptr) || (
a2_ ==
nullptr) || (
a3_ ==
nullptr)) {
451 <<
"Error: one or more parameters are not initialized. Please be sure to initialize a0, a1, a2 and a3";
454 return std::make_unique<RooChebychev>(
"3rdOrderPol",
"3rdOrderPol", *
x, RooArgList{*
a0_, *
a1_, *
a2_, *
a3_});
457 else if (backgroundType ==
"chebychev6") {
459 if ((
a0_ ==
nullptr) || (
a1_ ==
nullptr) || (
a2_ ==
nullptr) || (
a3_ ==
nullptr) || (
a4_ ==
nullptr) ||
460 (
a5_ ==
nullptr) || (
a6_ ==
nullptr)) {
462 <<
"Error: one or more parameters are not initialized. Please be sure to initialize a0, a1, a2, a3, " 466 return std::make_unique<RooChebychev>(
474 const TString& signalType,
475 const TString& backgroundType) {
476 std::unique_ptr<RooAbsPdf>
model;
481 if ((signal !=
nullptr) && (background !=
nullptr)) {
483 edm::LogPrint(
"FitWithRooFit") <<
"Building model with signal and backgound";
484 RooArgList
components{*signal.release(), *background.release()};
487 }
else if (signal !=
nullptr) {
488 edm::LogPrint(
"FitWithRooFit") <<
"Building model with signal";
490 }
else if (background !=
nullptr) {
491 edm::LogPrint(
"FitWithRooFit") <<
"Building model with backgound";
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")
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_gaussFrac2
void reinitializeParameters()
std::unique_ptr< RooRealVar > a6_
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")
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)
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_
void initExpCoeffA2(double value, double min, double max, const TString &name="expCoeffa2", const TString &title="expCoeffa2")
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")
std::unique_ptr< RooRealVar > expCoeffa2_
std::unique_ptr< RooRealVar > alpha_
void initMean(double value, double min, double max, const TString &name="mean", const TString &title="mean")
void initA4(double value, double min, double max, const TString &name="a4", const TString &title="a4")