// @(#)root/minuit2:$Id: minuit2FitBench2D.C 20881 2007-11-19 11:23:56Z rdm $ // Author: L. Moneta 10/2005 /********************************************************************** * * * Copyright (c) 2005 ROOT Foundation, CERN/PH-SFT * * * **********************************************************************/ #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; // Quadratic background function Double_t gaus2D(Double_t *x, Double_t *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] ) ) ) ; } // Sum of background and peak function Double_t fitFunction(Double_t *x, Double_t *par) { return gaus2D(x,par); } 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.); } } void DoFit(const char* fitter, TVirtualPad *pad, Int_t 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_t 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(); } void minuit2FitBench2D(int n = 100000) { TH1::AddDirectory(kFALSE); TCanvas *c1 = new TCanvas("c1","Fitting Demo",10,10,900,900); c1->Divide(2,2); // create a TF1 with the range from 0 to 3 and 6 parameters fitFcn = new TF2("fitFcn",fitFunction,-10,10,-10,10,5); //fitFcn->SetNpx(200); gStyle->SetOptFit(); gStyle->SetStatY(0.6); histo = new TH2D("h2","2D Gauss",100,-10,10,100,-10,10); fillHisto(n); int npass=0; //with Minuit c1->cd(1); DoFit("Minuit",gPad,npass); //with Fumili c1->cd(2); DoFit("Fumili",gPad,npass); //with Minuit2 c1->cd(3); DoFit("Minuit2",gPad,npass); //with Fumili2 c1->cd(4); DoFit("Fumili2",gPad,npass); } minuit2FitBench2D.C:1 minuit2FitBench2D.C:2 minuit2FitBench2D.C:3 minuit2FitBench2D.C:4 minuit2FitBench2D.C:5 minuit2FitBench2D.C:6 minuit2FitBench2D.C:7 minuit2FitBench2D.C:8 minuit2FitBench2D.C:9 minuit2FitBench2D.C:10 minuit2FitBench2D.C:11 minuit2FitBench2D.C:12 minuit2FitBench2D.C:13 minuit2FitBench2D.C:14 minuit2FitBench2D.C:15 minuit2FitBench2D.C:16 minuit2FitBench2D.C:17 minuit2FitBench2D.C:18 minuit2FitBench2D.C:19 minuit2FitBench2D.C:20 minuit2FitBench2D.C:21 minuit2FitBench2D.C:22 minuit2FitBench2D.C:23 minuit2FitBench2D.C:24 minuit2FitBench2D.C:25 minuit2FitBench2D.C:26 minuit2FitBench2D.C:27 minuit2FitBench2D.C:28 minuit2FitBench2D.C:29 minuit2FitBench2D.C:30 minuit2FitBench2D.C:31 minuit2FitBench2D.C:32 minuit2FitBench2D.C:33 minuit2FitBench2D.C:34 minuit2FitBench2D.C:35 minuit2FitBench2D.C:36 minuit2FitBench2D.C:37 minuit2FitBench2D.C:38 minuit2FitBench2D.C:39 minuit2FitBench2D.C:40 minuit2FitBench2D.C:41 minuit2FitBench2D.C:42 minuit2FitBench2D.C:43 minuit2FitBench2D.C:44 minuit2FitBench2D.C:45 minuit2FitBench2D.C:46 minuit2FitBench2D.C:47 minuit2FitBench2D.C:48 minuit2FitBench2D.C:49 minuit2FitBench2D.C:50 minuit2FitBench2D.C:51 minuit2FitBench2D.C:52 minuit2FitBench2D.C:53 minuit2FitBench2D.C:54 minuit2FitBench2D.C:55 minuit2FitBench2D.C:56 minuit2FitBench2D.C:57 minuit2FitBench2D.C:58 minuit2FitBench2D.C:59 minuit2FitBench2D.C:60 minuit2FitBench2D.C:61 minuit2FitBench2D.C:62 minuit2FitBench2D.C:63 minuit2FitBench2D.C:64 minuit2FitBench2D.C:65 minuit2FitBench2D.C:66 minuit2FitBench2D.C:67 minuit2FitBench2D.C:68 minuit2FitBench2D.C:69 minuit2FitBench2D.C:70 minuit2FitBench2D.C:71 minuit2FitBench2D.C:72 minuit2FitBench2D.C:73 minuit2FitBench2D.C:74 minuit2FitBench2D.C:75 minuit2FitBench2D.C:76 minuit2FitBench2D.C:77 minuit2FitBench2D.C:78 minuit2FitBench2D.C:79 minuit2FitBench2D.C:80 minuit2FitBench2D.C:81 minuit2FitBench2D.C:82 minuit2FitBench2D.C:83 minuit2FitBench2D.C:84 minuit2FitBench2D.C:85 minuit2FitBench2D.C:86 minuit2FitBench2D.C:87 minuit2FitBench2D.C:88 minuit2FitBench2D.C:89 minuit2FitBench2D.C:90 minuit2FitBench2D.C:91 minuit2FitBench2D.C:92 minuit2FitBench2D.C:93 minuit2FitBench2D.C:94 minuit2FitBench2D.C:95 minuit2FitBench2D.C:96 minuit2FitBench2D.C:97 minuit2FitBench2D.C:98 minuit2FitBench2D.C:99 |
|