00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "TH1.h"
00011 #include "TF1.h"
00012 #include "TH2D.h"
00013 #include "TF2.h"
00014 #include "TCanvas.h"
00015 #include "TStopwatch.h"
00016 #include "TSystem.h"
00017 #include "TRandom3.h"
00018 #include "TVirtualFitter.h"
00019 #include "TPaveLabel.h"
00020 #include "TStyle.h"
00021
00022
00023 TF2 *fitFcn;
00024 TH2D *histo;
00025
00026
00027 Double_t gaus2D(Double_t *x, Double_t *par) {
00028 double t1 = x[0] - par[1];
00029 double t2 = x[1] - par[2];
00030 return par[0]* exp( - 0.5 * ( t1*t1/( par[3]*par[3]) + t2*t2 /( par[4]*par[4] ) ) ) ;
00031 }
00032
00033
00034 Double_t fitFunction(Double_t *x, Double_t *par) {
00035 return gaus2D(x,par);
00036 }
00037
00038 void fillHisto(int n =10000) {
00039
00040 gRandom = new TRandom3();
00041 for (int i = 0; i < n; ++i) {
00042 double x = gRandom->Gaus(2,3);
00043 double y = gRandom->Gaus(-1,4);
00044 histo->Fill(x,y,1.);
00045 }
00046 }
00047
00048 void DoFit(const char* fitter, TVirtualPad *pad, Int_t npass) {
00049 TStopwatch timer;
00050 TVirtualFitter::SetDefaultFitter(fitter);
00051 pad->SetGrid();
00052 fitFcn->SetParameters(100,0,0,2,7);
00053 fitFcn->Update();
00054
00055 timer.Start();
00056 histo->Fit("fitFcn","0");
00057 timer.Stop();
00058
00059 histo->Draw();
00060 Double_t cputime = timer.CpuTime();
00061 printf("%s, npass=%d : RT=%7.3f s, Cpu=%7.3f s\n",fitter,npass,timer.RealTime(),cputime);
00062 TPaveLabel *p = new TPaveLabel(0.5,0.7,0.85,0.8,Form("%s CPU= %g s",fitter,cputime),"brNDC");
00063 p->Draw();
00064 pad->Update();
00065 }
00066
00067 void minuit2FitBench2D(int n = 100000) {
00068 TH1::AddDirectory(kFALSE);
00069 TCanvas *c1 = new TCanvas("c1","Fitting Demo",10,10,900,900);
00070 c1->Divide(2,2);
00071
00072 fitFcn = new TF2("fitFcn",fitFunction,-10,10,-10,10,5);
00073
00074 gStyle->SetOptFit();
00075 gStyle->SetStatY(0.6);
00076
00077
00078 histo = new TH2D("h2","2D Gauss",100,-10,10,100,-10,10);
00079 fillHisto(n);
00080
00081 int npass=0;
00082
00083
00084 c1->cd(1);
00085 DoFit("Minuit",gPad,npass);
00086
00087
00088 c1->cd(2);
00089 DoFit("Fumili",gPad,npass);
00090
00091
00092 c1->cd(3);
00093 DoFit("Minuit2",gPad,npass);
00094
00095
00096 c1->cd(4);
00097 DoFit("Fumili2",gPad,npass);
00098 }