ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
fit2a.C
Go to the documentation of this file.
1 #include "TF2.h"
2 #include "TH2.h"
3 #include "TCutG.h"
4 #include "TMath.h"
5 #include "TCanvas.h"
6 #include "TStyle.h"
7 
8 
9 //+ Fitting a 2-D histogram (a variant)
10 // This tutorial illustrates :
11 // - how to create a 2-d function
12 // - fill a 2-d histogram randomly from this function
13 // - fit the histogram
14 // - display the fitted function on top of the histogram (lego-plot)
15 // using a surface plot in a sub-range of the histogram.
16 //
17 // This example can be executed via the interpreter or/and the compiler
18 // root > .x fit2a.C
19 // root > .x fit2a.C++
20 //Author: Rene Brun
21 
23  Double_t r1 = Double_t((x[0]-par[1])/par[2]);
24  Double_t r2 = Double_t((x[1]-par[3])/par[4]);
25  return par[0]*TMath::Exp(-0.5*(r1*r1+r2*r2));
26 }
28  Double_t *p1 = &par[0];
29  Double_t *p2 = &par[5];
30  Double_t *p3 = &par[10];
31  Double_t result = g2(x,p1) + g2(x,p2) + g2(x,p3);
32  return result;
33 }
34 
36  TCanvas *c = new TCanvas();
38  gStyle->SetPalette(57);
39  const Int_t npar = 15;
40  Double_t f2params[npar] = {100,-3,3,-3,3,160,0,0.8,0,0.9,40,4,0.7,4,0.7};
41  TF2 *f2 = new TF2("f2",fun2,-10,10,-10,10, npar);
42  f2->SetParameters(f2params);
43 
44  //Create an histogram and fill it randomly with f2
45  TH2F *h2 = new TH2F("h2","From f2",40,-10,10,40,-10,10);
46  Int_t nentries = 100000;
47  h2->FillRandom("f2",nentries);
48  //Fit h2 with original function f2
49  Float_t ratio = 4*nentries/100000;
50  f2params[ 0] *= ratio;
51  f2params[ 5] *= ratio;
52  f2params[10] *= ratio;
53  f2->SetParameters(f2params);
54  h2->Fit("f2","N");
55  TCutG *cutg = new TCutG("cutg",5);
56  cutg->SetPoint(0,-7,-7);
57  cutg->SetPoint(1, 2,-7);
58  cutg->SetPoint(2, 2, 2);
59  cutg->SetPoint(3,-7, 2);
60  cutg->SetPoint(4,-7,-7);
61  h2->Draw("lego2 0");
62  h2->SetFillColor(38);
63  f2->SetNpx(80);
64  f2->SetNpy(80);
65  f2->Draw("surf1 same bb [cutg]");
66  return c;
67 }
double par[1]
Definition: unuranDistr.cxx:38
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:432
virtual void FillRandom(const char *fname, Int_t ntimes=5000)
Fill histogram following distribution in function fname.
Definition: TH2.cxx:592
virtual void Draw(Option_t *option="")
Draw this function with its current attributes.
Definition: TF2.cxx:216
virtual void SetNpx(Int_t npx=100)
Set the number of points used to draw the function.
Definition: TF1.cxx:3116
static double p3(double t, double a, double b, double c, double d)
float Float_t
Definition: RtypesCore.h:53
return c
R__EXTERN TStyle * gStyle
Definition: TStyle.h:423
tuple f2
Definition: surfaces.py:24
int Int_t
Definition: RtypesCore.h:41
Graphical cut class.
Definition: TCutG.h:29
Double_t x[n]
Definition: legend1.C:17
static double p2(double t, double a, double b, double c)
TH2D * h2
Definition: fit2dHist.C:45
Double_t fun2(Double_t *x, Double_t *par)
Definition: fit2a.C:27
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:256
unsigned int r1[N_CITIES]
Definition: simanTSP.cxx:321
virtual void SetNpy(Int_t npy=100)
Set the number of points used to draw the function.
Definition: TF2.cxx:907
static double p1(double t, double a, double b)
A 2-Dim function with parameters.
Definition: TF2.h:33
The Canvas class.
Definition: TCanvas.h:48
Double_t Exp(Double_t x)
Definition: TMath.h:495
double Double_t
Definition: RtypesCore.h:55
int nentries
Definition: THbookFile.cxx:89
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
Definition: TGraph.cxx:2127
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition: TStyle.cxx:1252
double result[121]
const Bool_t kTRUE
Definition: Rtypes.h:91
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:3607
void SetPalette(Int_t ncolors=kBird, Int_t *colors=0, Float_t alpha=1.)
See TColor::SetPalette.
Definition: TStyle.cxx:1445
unsigned int r2[N_CITIES]
Definition: simanTSP.cxx:322
TCanvas * fit2a()
Definition: fit2a.C:35
Double_t g2(Double_t *x, Double_t *par)
Definition: fit2a.C:22