#include "TH1.h" #include "RooDataHist.h" #include "RooRealVar.h" #include "RooGaussian.h" #include "RooMinuit.h" #include "RooChi2Var.h" #include "RooPlot.h" #include "TRandom3.h" void testRooFitChi2() { // simple test of fitting using RooFit TH1D * h = new TH1D("h","Gaussian Data",100,-10,10); // fill the histo with gaussian data TRandom3 r; for (int i = 0; i < 10000; ++i) h->Fill(r.Gaus(0,3.) ); RooRealVar x("x","x",-10,10) ; RooDataHist data("data","dataset with x",x,h) ; RooRealVar mean("mean","Mean of Gaussian",0,-10,10) ; RooRealVar sigma("sigma","Width of Gaussian",3,-10,10) ; RooGaussian gauss("gauss","gauss(x,mean,sigma)",x,mean,sigma) ; RooPlot* xframe = x.frame() ; data.plotOn(xframe) ; // uncomment here for a binned likelihood fit (RooFit default) // gauss.fitTo(data); // need to create Chi2 function // NOTE: ROOFIT uses zero bins by setting a Poisson 66% error on each bins (err ~ 1.1) RooChi2Var chi2("chi2","chi2",gauss,data) ; RooMinuit m(chi2) ; m.fit("mh") ; gauss.plotOn(xframe) ; xframe->Draw(); }