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.


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)


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);
RooFitResult *fitr = model.fitTo(data, Extended(kTRUE),Save());
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));
Double_t chi2 = xframe->chiSquare(“model”, “data”, 3);
long fitnll = fitr->minNll();
cout<<"chi2 = “<<chi2<<” Nll = "<<fitnll<<endl;


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 … fit.C.html