%%cpp -d #include "TH1.h" #include "TF1.h" #include "TH2D.h" #include "TF2.h" #include "TCanvas.h" #include "TStopwatch.h" #include "TSystem.h" #include "TRandom3.h" #include "TVirtualFitter.h" #include "TPaveLabel.h" #include "TStyle.h" TF2 *fitFcn; TH2D *histo; %%cpp -d double gaus2D(double *x, double *par) { double t1 = x[0] - par[1]; double t2 = x[1] - par[2]; return par[0]* exp( - 0.5 * ( t1*t1/( par[3]*par[3]) + t2*t2 /( par[4]*par[4] ) ) ) ; } %%cpp -d double fitFunction(double *x, double *par) { return gaus2D(x,par); } %%cpp -d void fillHisto(int n =10000) { gRandom = new TRandom3(); for (int i = 0; i < n; ++i) { double x = gRandom->Gaus(2,3); double y = gRandom->Gaus(-1,4); histo->Fill(x,y,1.); } } %%cpp -d void DoFit(const char* fitter, TVirtualPad *pad, int npass) { TStopwatch timer; TVirtualFitter::SetDefaultFitter(fitter); pad->SetGrid(); fitFcn->SetParameters(100,0,0,2,7); fitFcn->Update(); timer.Start(); histo->Fit("fitFcn","0"); timer.Stop(); histo->Draw(); double cputime = timer.CpuTime(); printf("%s, npass=%d : RT=%7.3f s, Cpu=%7.3f s\n",fitter,npass,timer.RealTime(),cputime); TPaveLabel *p = new TPaveLabel(0.5,0.7,0.85,0.8,Form("%s CPU= %g s",fitter,cputime),"brNDC"); p->Draw(); pad->Update(); } int n = 100000; TH1::AddDirectory(false); TCanvas *c1 = new TCanvas("c1","Fitting Demo",10,10,900,900); c1->Divide(2,2); fitFcn = new TF2("fitFcn",fitFunction,-10,10,-10,10,5); gStyle->SetOptFit(); gStyle->SetStatY(0.6); histo = new TH2D("h2","2D Gauss",100,-10,10,100,-10,10); fillHisto(n); int npass=0; c1->cd(1); DoFit("Minuit",gPad,npass); c1->cd(2); DoFit("Fumili",gPad,npass); c1->cd(3); DoFit("Minuit2",gPad,npass); c1->cd(4); DoFit("Fumili2",gPad,npass); %jsroot on gROOT->GetListOfCanvases()->Draw()