RooFit Calculating chi2

Hello Experts

I am aware that this topic has been discussed several times, but I can not make out the final answer.
I wish to calculate the chi2/dof for a crystal ball fit on my histogram.

I used these 2 methods:
RooChi2Var chi2 (“chi2”, “chi2”, sum, *datah);
Double_t chi2_val = chi2.getVal();
Double_t finalchi2_val=chi2_val/(20.0);
std::cout<<" This is the final value of chi2/DOF :"<<finalchi2_val<<std::endl;


The value reported is:
This is the final value of chi2/DOF :40.806


and then this:

std::cout<<“Let us check another value of chi2 :”<chiSquare(“sum”,“datah”,4)<<std::endl;

With this I get an error mesage as:


[#0] ERROR:InputArguments – RooPlot::findObject(frame_Top Mass_8311cb0) cannot find object cbfit
[#0] ERROR:InputArguments – RooPlot::chiSquare(frame_Top Mass_8311cb0) cannot find curve
Let us check another value of chi2 :-1


any insights?

The entire code is posted below:


void fit2CrystalBall(TString filename="", TString fitplotname="", TString pullplotname="", TString residplotname="")
{
gROOT->SetStyle(“Plain”);
gStyle->SetOptFit(1111);
// get the file and histo you wish to fit
TFile *f = new TFile("/export/share/data2/jveatch/testarea/TopAnalysis/rel18/d5pd/v03/JMR.2015.04.01/JMR.2015.04.01.NOM.root");
// TFile *f = new TFile(filename);
if(!f) std::cout<<"No file found "<<std::endl;
TH1D histo = (TH1D) f->Get(“data_sig_LeadFJetM2p5GeV_cakt10_fjTop_300in”);

if(!histo) std::cout<<“No histo found”<<std::endl;

//------------------------------------
// define x range and frame
//------------------------------------

RooRealVar x(“Top Mass”,“Mass”,100,250.,“GeV/c^{2}”) ;
RooPlot* frame = x.frame(Title(“Crystall Ball fit for M_{t}”)) ;
TCanvas *c1 = new TCanvas(“c1”,“c1”,25,71,400,400);

//------------------------------------
// import histo
//------------------------------------

RooDataHist *datah;
datah = new RooDataHist(“datah”,“datah”,x,histo);
datah->plotOn(frame,Name(“datah”),
DataError(RooAbsData::SumW2),MarkerColor(kRed),MarkerSize(0.8),XErrorSize(0.));

//------------------------------------
// define Crystal Ball
//------------------------------------

RooRealVar cbmean(“cb mean”,“cb_mean”,173.2,165,175);
RooRealVar cbwidth(“cb width”,“cb_width”,10,0,15);
RooRealVar cbn(“cb n”,“cb_n”,150,100,180);
RooRealVar cbalpha(“cb alpha”,“cb_alpha”,5,0,10.);
RooCBShape cb(“cb”,“cb”,x,cbmean,cbwidth,cbalpha,cbn) ;

// cbalpha.setVal(10);
// cbalpha.setConstant(kTRUE);
cbn.setVal(5);
cbn.setConstant(kTRUE);

RooRealVar nsig(“N.top”,“nsignal”,2500,2000,3000);
RooExtendPdf esig(“esig”,“esig”,cb,nsig);

RooAddPdf sum(“cbfit”,“crystal ball fit”,RooArgList(esig));

RooFitResult* r;
x.setRange(“fitrange”,140.,200.);
r = sum.fitTo(*datah,Range(“fitrange”),Extended(kTRUE),SumW2Error(kTRUE),Save()) ;
sum.paramOn(frame,Layout(0.1,0.4,0.875));
frame->getAttText()->SetTextSize(0.03);

Int_t floated = static_cast<Int_t>(r->floatParsFinal().getSize());
std::cout<<“These are the # of final Pars :”<<floated<<std::endl;

sum.chi2FitTo(*datah);
// Adding some chi2 values
RooChi2Var chi2 (“chi2”, “chi2”, sum, *datah);
Double_t chi2_val = chi2.getVal();
Double_t finalchi2_val=chi2_val/(20.0);
std::cout<<" This is the final value of chi2/DOF :"<<finalchi2_val<<std::endl;

// Put the Chi2 label on the plot
TPaveLabel *t1=new TPaveLabel(0.15,0.53,0.35,0.62, Form("#chi^{2}/dof = %f",finalchi2_val),“brNDC”);
t1->SetFillColor(0);
t1->SetTextSize(0.3);
frame->addObject(t1);

//------------------------------------
// plot
//------------------------------------
sum.plotOn(frame,Components(“cb”),LineColor(kGreen)) ;
frame->Draw() ;
c1->SaveAs(fitplotname);

r->Print();
r->correlationMatrix().Print() ;
std::cout<<“Let us check another value of chi2 :”<chiSquare(“cbfit”,“datah”,4)<<std::endl;

// Construct a histogroam with pullHist
RooHist* hpull=frame->pullHist();
RooHist* hresid=frame->residHist();
// Create a new frame to draw the residual distribution and add the distribution to the frame
RooPlot* frame2 = x.frame(Title(“Residual Distribution”)) ;
frame2->addPlotable(hresid,“P”);

// Create a new frame to draw the pull distribution and add the distribution to the frame
RooPlot* frame3 = x.frame(Title(“Pull Distribution”)) ;
frame3->addPlotable(hpull,“P”) ;

TCanvas* c = new TCanvas(“pull”,“pull”,25,71,400,400);
frame3->Draw() ;c->SaveAs(pullplotname);

TCanvas* c2 = new TCanvas(“resid”,“resid”,25,71,400,400);
frame2->Draw() ;c2->SaveAs(residplotname);

// c->Divide(2) ;
// c->cd(1) ; gPad->SetLeftMargin(0.15) ; frame2->GetYaxis()->SetTitleOffset(1.6) ; frame2->Draw() ;
// c->cd(2) ; gPad->SetLeftMargin(0.15) ; frame3->GetYaxis()->SetTitleOffset(1.6) ; frame3->Draw() ;
// c->SaveAs(pullplotname);
}//end of method for CrytalBall Fitting for top


Much Thanks
Ruchika

Hi,

See my previous answer 4 ways to compute chi2 -> 4 different numbers in RooFit

Concerning the error, I think you are not plotting the full pdf but only the “cb” component. Just add a line

sum.plotOn(frame) ;

Best Regards

Lorenzo