Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
minuit2FitBench2D.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_fit
3/// \notebook -js
4/// Minuit2 fit 2D benchmark.
5///
6/// \macro_image
7/// \macro_output
8/// \macro_code
9///
10/// \author Lorenzo Moneta
11
12#include "TH1.h"
13#include "TF1.h"
14#include "TH2D.h"
15#include "TF2.h"
16#include "TCanvas.h"
17#include "TStopwatch.h"
18#include "TSystem.h"
19#include "TRandom3.h"
20#include "TVirtualFitter.h"
21#include "TPaveLabel.h"
22#include "TStyle.h"
23
24
25TF2 *fitFcn;
26TH2D *histo;
27
28// Quadratic background function
29double gaus2D(double *x, double *par) {
30 double t1 = x[0] - par[1];
31 double t2 = x[1] - par[2];
32 return par[0]* exp( - 0.5 * ( t1*t1/( par[3]*par[3]) + t2*t2 /( par[4]*par[4] ) ) ) ;
33}
34
35// Sum of background and peak function
36double fitFunction(double *x, double *par) {
37 return gaus2D(x,par);
38}
39
40void fillHisto(int n =10000) {
41
42 gRandom = new TRandom3();
43 for (int i = 0; i < n; ++i) {
44 double x = gRandom->Gaus(2,3);
45 double y = gRandom->Gaus(-1,4);
46 histo->Fill(x,y,1.);
47 }
48}
49
50void DoFit(const char* fitter, TVirtualPad *pad, int npass) {
53 pad->SetGrid();
54 fitFcn->SetParameters(100,0,0,2,7);
55 fitFcn->Update();
56
57 timer.Start();
58 histo->Fit("fitFcn","0");
59 timer.Stop();
60
61 histo->Draw();
62 double cputime = timer.CpuTime();
63 printf("%s, npass=%d : RT=%7.3f s, Cpu=%7.3f s\n",fitter,npass,timer.RealTime(),cputime);
64 TPaveLabel *p = new TPaveLabel(0.5,0.7,0.85,0.8,Form("%s CPU= %g s",fitter,cputime),"brNDC");
65 p->Draw();
66 pad->Update();
67}
68
69void minuit2FitBench2D(int n = 100000) {
70 TH1::AddDirectory(false);
71 TCanvas *c1 = new TCanvas("c1","Fitting Demo",10,10,900,900);
72 c1->Divide(2,2);
73 // create a TF1 with the range from 0 to 3 and 6 parameters
74 fitFcn = new TF2("fitFcn",fitFunction,-10,10,-10,10,5);
75 //fitFcn->SetNpx(200);
77 gStyle->SetStatY(0.6);
78
79
80 histo = new TH2D("h2","2D Gauss",100,-10,10,100,-10,10);
81 fillHisto(n);
82
83 int npass=0;
84
85 //with Minuit
86 c1->cd(1);
87 DoFit("Minuit",gPad,npass);
88
89 //with Fumili
90 c1->cd(2);
91 DoFit("Fumili",gPad,npass);
92
93 //with Minuit2
94 c1->cd(3);
95 DoFit("Minuit2",gPad,npass);
96
97 //with Fumili2
98 c1->cd(4);
99 DoFit("Fumili2",gPad,npass);
100}
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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:2489
R__EXTERN TStyle * gStyle
Definition TStyle.h:436
#define gPad
The Canvas class.
Definition TCanvas.h:23
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:1294
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:3898
void Draw(Option_t *option="") override
Draw this histogram with options.
Definition TH1.cxx:3066
2-D histogram with a double per channel (see TH1 documentation)
Definition TH2.h:357
Int_t Fill(Double_t) override
Invalid Fill method.
Definition TH2.cxx:393
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:275
Stopwatch class.
Definition TStopwatch.h:28
void SetStatY(Float_t y=0)
Definition TStyle.h:398
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:1593
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
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