# How to obtain fit goodness in ROOFIT?

Hi all:
How can i obtain chi2/d.o.f after a fitting by roofit?
The roofit use likelihood method to fit a pdf to data.

can chi2 = xframe->chiSquare() work? it seems the value of it
is not right for i only have 70 bins but the chiSquare() alway greater than 1000 and the fit seems ok.

Hi,

RooPlot::chiSquare() should give you the chi2/dof. You can provide it the number of floating parameters if the curve is the result of a fit to adjust the ndof. If you have multiple curves or dataset, the chi^2 is calculated from the last added dataset and curve. If this is not what you want you can explicitly specify the names of the curve and histogram in the RooPlot you want compared.

Double_t chiSquare(int nFitParam=0) ;
Double_t chiSquare(const char* pdfname, const char* histname, int nFitParam=0) const ;

Plot and histogram names are automatically chosen (do e.g. RooPlot::print()), but you can also choose them yourself by adding a Name(â€śblahâ€ť) argument to each plotOn call.

If you still see problems can you send me an example that reproduces the problem (or send the a ROOT file with the RooPlot in question)

Wouter

1 Like

The following lines are my code.
But the chi2 is â€ś-1â€ť nowâ€¦

RooAddPdf model(â€śmodelâ€ť, â€śmodelâ€ť, RooArgList(sig1,bkg), RooArgList(nsig1,nbkg));

RooDataHist* data = new RooDataHist(â€śdataâ€ť, â€śPhoton Energy(GeV)â€ť, energy, eg);

TCanvas *myC = new TCanvas(â€śmyCâ€ť, â€śmyCâ€ť, 1);
myC->Divide(1,1);
myC->cd(1);
RooFitResult *fitr = model.fitTo(data, Extended(kTRUE),Save());
RooPlot
xframe = energy.frame();
Format(â€śNELUâ€ť, AutoPrecision(2)), Layout(0.1,0.4,0.9));
data->plotOn(xframe, MarkerColor(kBlack));
model.plotOn(xframe, DrawOption(â€śLâ€ť));
model.plotOn(xframe, Components(â€śbkgâ€ť), LineColor(kBlack), LineStyle(kDashed));LineStyle(kDashed));
xframe->Draw();
Double_t chi2 = xframe->chiSquare(â€śmodelâ€ť, â€śdataâ€ť, 3);
long fitnll = fitr->minNll();
cout<<"chi2 = â€ś<<chi2<<â€ť Nll = "<<fitnll<<endl;

Hi,

This may work. Please try this.

None of these methods work for me, Iâ€™m unable do get the chi2.

I do exactly the

Double_t chi2 = frame->chiSquare(â€śmodelâ€ť,â€śdataâ€ť,3);

but I keep getting a

[#0] ERROR:InputArguments â€“ RooPlot::findObject(frame_Dimuon Mass_36331e0) cannot find object data
[#0] ERROR:InputArguments â€“ RooPlot::chiSquare(frame_Dimuon Mass_36331e0) cannot find histogram

I have also tried the solutions from this thread: Roofit: failing to find object in frame

but they didnâ€™t work either.

See the last lines of
root.cern.ch/root/html534/tutor â€¦ fit.C.html