00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "TH1.h"
00012 #include "TF1.h"
00013 #include "TCanvas.h"
00014 #include "TStopwatch.h"
00015 #include "TSystem.h"
00016 #include "TRandom3.h"
00017 #include "TVirtualFitter.h"
00018 #include "TPaveLabel.h"
00019 #include "TStyle.h"
00020 #include "TMath.h"
00021 #include "TROOT.h"
00022 #include "TFrame.h"
00023
00024
00025
00026
00027 TF1 *fitFcn;
00028 TH1 *histo;
00029
00030
00031 Double_t background(Double_t *x, Double_t *par) {
00032 return par[0] + par[1]*x[0] + par[2]*x[0]*x[0];
00033 }
00034
00035
00036 Double_t lorentzianPeak(Double_t *x, Double_t *par) {
00037 return (0.5*par[0]*par[1]/TMath::Pi()) /
00038 TMath::Max( 1.e-10,(x[0]-par[2])*(x[0]-par[2]) + .25*par[1]*par[1]);
00039 }
00040
00041
00042 Double_t fitFunction(Double_t *x, Double_t *par) {
00043 return background(x,par) + lorentzianPeak(x,&par[3]);
00044 }
00045
00046 void DoFit(const char* fitter, TVirtualPad *pad, Int_t npass) {
00047 printf("\n*********************************************************************************\n");
00048 printf("\t %s \n",fitter);
00049 printf("*********************************************************************************\n");
00050
00051 gRandom = new TRandom3();
00052 TStopwatch timer;
00053
00054 TVirtualFitter::SetDefaultFitter(fitter);
00055
00056 pad->SetGrid();
00057 pad->SetLogy();
00058 fitFcn->SetParameters(1,1,1,6,.03,1);
00059 fitFcn->Update();
00060 std::string title = std::string(fitter) + " fit bench";
00061 histo = new TH1D(fitter,title.c_str(),200,0,3);
00062
00063 timer.Start();
00064 for (Int_t pass=0;pass<npass;pass++) {
00065 if (pass%100 == 0) printf("pass : %d\n",pass);
00066 fitFcn->SetParameters(1,1,1,6,.03,1);
00067 for (Int_t i=0;i<5000;i++) {
00068 histo->Fill(fitFcn->GetRandom());
00069 }
00070 histo->Fit(fitFcn,"Q0");
00071 }
00072
00073 histo->Fit(fitFcn,"EV");
00074 timer.Stop();
00075
00076 (histo->GetFunction("fitFcn"))->SetLineColor(kRed+3);
00077 gPad->SetFillColor(kYellow-10);
00078
00079
00080 Double_t cputime = timer.CpuTime();
00081 printf("%s, npass=%d : RT=%7.3f s, Cpu=%7.3f s\n",fitter,npass,timer.RealTime(),cputime);
00082 TPaveLabel *p = new TPaveLabel(0.45,0.7,0.88,0.8,Form("%s CPU= %g s",fitter,cputime),"brNDC");
00083 p->Draw();
00084 p->SetTextColor(kRed+3);
00085 p->SetFillColor(kYellow-8);
00086 pad->Update();
00087 }
00088
00089 void minuit2FitBench(Int_t npass=20) {
00090 TH1::AddDirectory(kFALSE);
00091 TCanvas *c1 = new TCanvas("FitBench","Fitting Demo",10,10,900,900);
00092 c1->Divide(2,2);
00093 c1->SetFillColor(kYellow-9);
00094
00095 fitFcn = new TF1("fitFcn",fitFunction,0,3,6);
00096 fitFcn->SetNpx(200);
00097 gStyle->SetOptFit();
00098 gStyle->SetStatY(0.6);
00099
00100
00101 c1->cd(1);
00102 DoFit("Minuit",gPad,npass);
00103
00104
00105 c1->cd(2);
00106 DoFit("Fumili",gPad,npass);
00107
00108
00109 c1->cd(3);
00110 DoFit("Minuit2",gPad,npass);
00111
00112
00113 c1->cd(4);
00114 DoFit("Fumili2",gPad,npass);
00115
00116 c1->SaveAs("FitBench.root");
00117 }