Logo ROOT   6.12/07
Reference Guide
minuit2FitBench2D.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_fit
3 /// \notebook -js
4 ///
5 /// \macro_image
6 /// \macro_output
7 /// \macro_code
8 ///
9 /// \author Lorenzo Moneta
10 
11 #include "TH1.h"
12 #include "TF1.h"
13 #include "TH2D.h"
14 #include "TF2.h"
15 #include "TCanvas.h"
16 #include "TStopwatch.h"
17 #include "TSystem.h"
18 #include "TRandom3.h"
19 #include "TVirtualFitter.h"
20 #include "TPaveLabel.h"
21 #include "TStyle.h"
22 
23 
24 TF2 *fitFcn;
25 TH2D *histo;
26 
27 // Quadratic background function
28 Double_t gaus2D(Double_t *x, Double_t *par) {
29  double t1 = x[0] - par[1];
30  double t2 = x[1] - par[2];
31  return par[0]* exp( - 0.5 * ( t1*t1/( par[3]*par[3]) + t2*t2 /( par[4]*par[4] ) ) ) ;
32 }
33 
34 // Sum of background and peak function
35 Double_t fitFunction(Double_t *x, Double_t *par) {
36  return gaus2D(x,par);
37 }
38 
39 void fillHisto(int n =10000) {
40 
41  gRandom = new TRandom3();
42  for (int i = 0; i < n; ++i) {
43  double x = gRandom->Gaus(2,3);
44  double y = gRandom->Gaus(-1,4);
45  histo->Fill(x,y,1.);
46  }
47 }
48 
49 void DoFit(const char* fitter, TVirtualPad *pad, Int_t npass) {
50  TStopwatch timer;
52  pad->SetGrid();
53  fitFcn->SetParameters(100,0,0,2,7);
54  fitFcn->Update();
55 
56  timer.Start();
57  histo->Fit("fitFcn","0");
58  timer.Stop();
59 
60  histo->Draw();
61  Double_t cputime = timer.CpuTime();
62  printf("%s, npass=%d : RT=%7.3f s, Cpu=%7.3f s\n",fitter,npass,timer.RealTime(),cputime);
63  TPaveLabel *p = new TPaveLabel(0.5,0.7,0.85,0.8,Form("%s CPU= %g s",fitter,cputime),"brNDC");
64  p->Draw();
65  pad->Update();
66 }
67 
68 void minuit2FitBench2D(int n = 100000) {
70  TCanvas *c1 = new TCanvas("c1","Fitting Demo",10,10,900,900);
71  c1->Divide(2,2);
72  // create a TF1 with the range from 0 to 3 and 6 parameters
73  fitFcn = new TF2("fitFcn",fitFunction,-10,10,-10,10,5);
74  //fitFcn->SetNpx(200);
75  gStyle->SetOptFit();
76  gStyle->SetStatY(0.6);
77 
78 
79  histo = new TH2D("h2","2D Gauss",100,-10,10,100,-10,10);
80  fillHisto(n);
81 
82  int npass=0;
83 
84  //with Minuit
85  c1->cd(1);
86  DoFit("Minuit",gPad,npass);
87 
88  //with Fumili
89  c1->cd(2);
90  DoFit("Fumili",gPad,npass);
91 
92  //with Minuit2
93  c1->cd(3);
94  DoFit("Minuit2",gPad,npass);
95 
96  //with Fumili2
97  c1->cd(4);
98  DoFit("Fumili2",gPad,npass);
99 }
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:628
Random number generator class based on M.
Definition: TRandom3.h:27
Double_t RealTime()
Stop the stopwatch (if it is running) and return the realtime (in seconds) passed between the start a...
Definition: TStopwatch.cxx:110
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Definition: TStopwatch.cxx:58
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition: TRandom.cxx:256
return c1
Definition: legend1.C:41
R__EXTERN TStyle * gStyle
Definition: TStyle.h:402
virtual void Update()=0
static void SetDefaultFitter(const char *name="")
static: set name of default fitter
TVirtualPad * cd(Int_t subpadnumber=0)
Set current canvas & pad.
Definition: TCanvas.cxx:688
Double_t CpuTime()
Stop the stopwatch (if it is running) and return the cputime (in seconds) passed between the start an...
Definition: TStopwatch.cxx:125
int Int_t
Definition: RtypesCore.h:41
void SetStatY(Float_t y=0)
Definition: TStyle.h:371
static void AddDirectory(Bool_t add=kTRUE)
Sets the flag controlling the automatic add of histograms in memory.
Definition: TH1.cxx:1225
void Stop()
Stop the stopwatch.
Definition: TStopwatch.cxx:77
Double_t x[n]
Definition: legend1.C:17
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition: TVirtualPad.h:49
A Pave (see TPave) with a text centered in the Pave.
Definition: TPaveLabel.h:20
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2969
void SetOptFit(Int_t fit=1)
The type of information about fit parameters printed in the histogram statistics box can be selected ...
Definition: TStyle.cxx:1218
char * Form(const char *fmt,...)
virtual void Update()
Called by functions such as SetRange, SetNpx, SetParameters to force the deletion of the associated h...
Definition: TF1.cxx:3556
A 2-Dim function with parameters.
Definition: TF2.h:29
R__EXTERN TRandom * gRandom
Definition: TRandom.h:62
const Bool_t kFALSE
Definition: RtypesCore.h:88
The Canvas class.
Definition: TCanvas.h:31
auto * t1
Definition: textangle.C:20
double Double_t
Definition: RtypesCore.h:55
Double_t y[n]
Definition: legend1.C:17
THist< 2, double, THistStatContent, THistStatUncertainty > TH2D
Definition: THist.hxx:290
virtual void Divide(Int_t nx=1, Int_t ny=1, Float_t xmargin=0.01, Float_t ymargin=0.01, Int_t color=0)
Automatic pad generation by division.
Definition: TPad.cxx:1153
#define gPad
Definition: TVirtualPad.h:285
double exp(double)
Int_t Fill(Double_t)
Invalid Fill method.
Definition: TH2.cxx:292
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="", Double_t xmin=0, Double_t xmax=0)
Fit histogram with function fname.
Definition: TH1.cxx:3688
const Int_t n
Definition: legend1.C:16
virtual void SetGrid(Int_t valuex=1, Int_t valuey=1)=0
2-D histogram with a double per channel (see TH1 documentation)}
Definition: TH2.h:290
Stopwatch class.
Definition: TStopwatch.h:28