Logo ROOT  
Reference Guide
No Matches
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_fit
3/// \notebook -js
5/// \macro_image
6/// \macro_output
7/// \macro_code
9/// \author Lorenzo Moneta
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"
24TF2 *fitFcn;
25TH2D *histo;
27// Quadratic background function
28double gaus2D(double *x, double *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] ) ) ) ;
34// Sum of background and peak function
35double fitFunction(double *x, double *par) {
36 return gaus2D(x,par);
39void fillHisto(int n =10000) {
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 }
49void DoFit(const char* fitter, TVirtualPad *pad, int npass) {
50 TStopwatch timer;
52 pad->SetGrid();
53 fitFcn->SetParameters(100,0,0,2,7);
54 fitFcn->Update();
56 timer.Start();
57 histo->Fit("fitFcn","0");
58 timer.Stop();
60 histo->Draw();
61 double 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();
68void minuit2FitBench2D(int n = 100000) {
69 TH1::AddDirectory(false);
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);
76 gStyle->SetStatY(0.6);
79 histo = new TH2D("h2","2D Gauss",100,-10,10,100,-10,10);
80 fillHisto(n);
82 int npass=0;
84 //with Minuit
85 c1->cd(1);
86 DoFit("Minuit",gPad,npass);
88 //with Fumili
89 c1->cd(2);
90 DoFit("Fumili",gPad,npass);
92 //with Minuit2
93 c1->cd(3);
94 DoFit("Minuit2",gPad,npass);
96 //with Fumili2
97 c1->cd(4);
98 DoFit("Fumili2",gPad,npass);
winID h TVirtualViewer3D TVirtualGLPainter p
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2467
R__EXTERN TStyle * gStyle
Definition TStyle.h:433
#define gPad
The Canvas class.
Definition TCanvas.h:23
virtual void Update()
Called by functions such as SetRange, SetNpx, SetParameters to force the deletion of the associated h...
Definition TF1.cxx:3613
virtual void SetParameters(const Double_t *params)
Definition TF1.h:650
A 2-Dim function with parameters.
Definition TF2.h:29
static void AddDirectory(Bool_t add=kTRUE)
Sets the flag controlling the automatic add of histograms in memory.
Definition TH1.cxx:1267
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:3901
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3067
2-D histogram with a double per channel (see TH1 documentation)}
Definition TH2.h:301
Int_t Fill(Double_t) override
Invalid Fill method.
Definition TH2.cxx:347
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition TObject.cxx:274
A Pave (see TPave) with a text centered in the Pave.
Definition TPaveLabel.h:20
Random number generator class based on M.
Definition TRandom3.h:27
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:274
Stopwatch class.
Definition TStopwatch.h:28
Double_t RealTime()
Stop the stopwatch (if it is running) and return the realtime (in seconds) passed between the start a...
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
Double_t CpuTime()
Stop the stopwatch (if it is running) and return the cputime (in seconds) passed between the start an...
void Stop()
Stop the stopwatch.
void SetStatY(Float_t y=0)
Definition TStyle.h:395
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:1589
static void SetDefaultFitter(const char *name="")
static: set name of default fitter
TVirtualPad is an abstract base class for the Pad and Canvas classes.
Definition TVirtualPad.h:51
virtual void SetGrid(Int_t valuex=1, Int_t valuey=1)=0
virtual void Update()=0
RVec< PromoteType< T > > exp(const RVec< T > &v)
Definition RVec.hxx:1800
Double_t y[n]
Definition legend1.C:17
return c1
Definition legend1.C:41
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
auto * t1
Definition textangle.C:20