// \file /// \ingroup tutorial_roofit /// \notebook -nodraw /// Likelihood and minimization: setting up a chi^2 fit to a binned dataset /// /// \macro_output /// \macro_code // \author 07/2008 - Wouter Verkerke // Modified by M. Shapiro to show problem with Chisq fit with limited range #include "RooRealVar.h" #include "RooDataSet.h" #include "RooGaussian.h" #include "RooConstVar.h" #include "RooChebychev.h" #include "RooAddPdf.h" #include "RooChi2Var.h" #include "TCanvas.h" #include "TAxis.h" #include "RooPlot.h" #include "RooFitResult.h" #include "TText.h" #include using namespace RooFit; void rf602_chi2fit_modified() { // S e t u p m o d e l // --------------------- // Declare observable x RooRealVar x("x", "x", 0, 10); // Create two Gaussian PDFs g1(x,mean1,sigma) anf g2(x,mean2,sigma) and their parameters RooRealVar mean("mean", "mean of gaussians", 5); RooRealVar sigma1("sigma1", "width of gaussians", 0.5); RooRealVar sigma2("sigma2", "width of gaussians", 1); RooGaussian sig1("sig1", "Signal component 1", x, mean, sigma1); RooGaussian sig2("sig2", "Signal component 2", x, mean, sigma2); // Build Chebychev polynomial p.d.f. RooRealVar a0("a0", "a0", 0.5, 0., 1.); RooRealVar a1("a1", "a1", 0.2, 0., 1.); RooChebychev bkg("bkg", "Background", x, RooArgSet(a0, a1)); // Sum the signal components into a composite signal p.d.f. RooRealVar sig1frac("sig1frac", "fraction of component 1 in signal", 0.8, 0., 1.); RooAddPdf sig("sig", "Signal", RooArgList(sig1, sig2), sig1frac); // Sum the composite signal and background RooRealVar bkgfrac("bkgfrac", "fraction of background", 0.5, 0., 1.); RooAddPdf model("model", "g1+g2+a", RooArgList(bkg, sig), bkgfrac); // C r e a t e b i n n e d d a t a s e t // ----------------------------------------- RooDataSet *d = model.generate(x, 10000); RooDataHist *dh = d->binnedClone(); x.setRange("R1",2,9); // Construct a chi^2 of the data and the model. // When a p.d.f. is used in a chi^2 fit, the probability density scaled // by the number of events in the dataset to obtain the fit function // If model is an extended p.d.f, the expected number events is used // instead of the observed number of events. RooFitResult *fitres = model.chi2FitTo(*dh,DataError(RooAbsData::SumW2),Save()); // model.chi2FitTo(*dh); TCanvas *c = new TCanvas("test", "test", 800, 400); c->Divide(2,2); c->cd(1); RooPlot *xframe = x.frame(Title("Test Chi2FitTo")); model.paramOn(xframe, Layout(0.55)); dh->plotOn(xframe); model.plotOn(xframe, LineWidth(3)); ostringstream myInfo; myInfo << "chisq/dof = " << xframe->chiSquare(fitres->floatParsFinal().size()); string info = myInfo.str(); TText *txt = new TText(0.8, 400, info.c_str()); txt->SetTextSize(0.05); txt->SetTextColor(kBlue); xframe->addObject(txt); xframe->Draw(); c->cd(3); fitres = model.fitTo(*dh, Save()); RooPlot *xframe3 = x.frame(Title("Test. Likelikhood")); model.paramOn(xframe3, Layout(0.55)); dh->plotOn(xframe3); model.plotOn(xframe3, LineWidth(3)); ostringstream myInfo3; myInfo3 << "chisq/dof = " << xframe3->chiSquare(fitres->floatParsFinal().size()); string info3 = myInfo3.str(); TText *txt3 = new TText(0.8, 400, info3.c_str()); txt3->SetTextSize(0.05); txt3->SetTextColor(kBlue); xframe3->addObject(txt3); xframe3->Draw(); c->cd(2); fitres = model.chi2FitTo(*dh,Range("R1"),DataError(RooAbsData::SumW2), Save()); RooPlot *xframe1 = x.frame(Title("Test.Chi2FitTo Limited range")); model.paramOn(xframe1, Layout(0.55)); dh->plotOn(xframe1); model.plotOn(xframe1, LineWidth(3)); ostringstream myInfo1; myInfo1 << "chisq/dof = " << xframe1->chiSquare(fitres->floatParsFinal().size()); string info1 = myInfo1.str(); TText *txt1 = new TText(0.8, 400, info1.c_str()); txt1->SetTextSize(0.05); txt1->SetTextColor(kBlue); xframe1->addObject(txt1); xframe1->Draw(); c->cd(4); fitres = model.fitTo(*dh,Range("R1"), Save()); RooPlot *xframe4 = x.frame(Title("Test. Likelikhood Limited Range")); model.paramOn(xframe4, Layout(0.55)); dh->plotOn(xframe4); model.plotOn(xframe4, Range("R1"), NormRange("R1"), LineWidth(3)); ostringstream myInfo4; myInfo4 << "chisq/dof = " << xframe4->chiSquare(fitres->floatParsFinal().size()); string info4 = myInfo4.str(); TText *txt4 = new TText(0.8, 400, info.c_str()); txt4->SetTextSize(0.05); txt4->SetTextColor(kBlue); xframe4->addObject(txt4); xframe4->Draw(); }