Logo ROOT   6.10/09
Reference Guide
fit2a.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_fit
3 /// \notebook
4 /// Fitting a 2-D histogram (a variant)
5 /// This tutorial illustrates :
6 /// - how to create a 2-d function
7 /// - fill a 2-d histogram randomly from this function
8 /// - fit the histogram
9 /// - display the fitted function on top of the histogram (lego-plot)
10 /// using a surface plot in a sub-range of the histogram.
11 ///
12 /// This example can be executed via the interpreter or/and the compiler
13 ///
14 /// ~~~{.cpp}
15 /// root > .x fit2a.C
16 /// root > .x fit2a.C++
17 /// ~~~
18 ///
19 /// \macro_image
20 /// \macro_output
21 /// \macro_code
22 ///
23 /// \author Rene Brun
24 
25 #include "TF2.h"
26 #include "TH2.h"
27 #include "TCutG.h"
28 #include "TMath.h"
29 #include "TCanvas.h"
30 #include "TStyle.h"
31 
33  Double_t r1 = Double_t((x[0]-par[1])/par[2]);
34  Double_t r2 = Double_t((x[1]-par[3])/par[4]);
35  return par[0]*TMath::Exp(-0.5*(r1*r1+r2*r2));
36 }
37 Double_t fun2(Double_t *x, Double_t *par) {
38  Double_t *p1 = &par[0];
39  Double_t *p2 = &par[5];
40  Double_t *p3 = &par[10];
41  Double_t result = g2(x,p1) + g2(x,p2) + g2(x,p3);
42  return result;
43 }
44 
45 TCanvas *fit2a() {
46  TCanvas *c = new TCanvas();
48  gStyle->SetPalette(57);
49  const Int_t npar = 15;
50  Double_t f2params[npar] = {100,-3,3,-3,3,160,0,0.8,0,0.9,40,4,0.7,4,0.7};
51  TF2 *f2 = new TF2("f2",fun2,-10,10,-10,10, npar);
52  f2->SetParameters(f2params);
53 
54  //Create an histogram and fill it randomly with f2
55  TH2F *h2 = new TH2F("h2","From f2",40,-10,10,40,-10,10);
56  Int_t nentries = 100000;
57  h2->FillRandom("f2",nentries);
58  //Fit h2 with original function f2
59  Float_t ratio = 4*nentries/100000;
60  f2params[ 0] *= ratio;
61  f2params[ 5] *= ratio;
62  f2params[10] *= ratio;
63  f2->SetParameters(f2params);
64  h2->Fit("f2","N");
65  TCutG *cutg = new TCutG("cutg",5);
66  cutg->SetPoint(0,-7,-7);
67  cutg->SetPoint(1, 2,-7);
68  cutg->SetPoint(2, 2, 2);
69  cutg->SetPoint(3,-7, 2);
70  cutg->SetPoint(4,-7,-7);
71  h2->Draw("lego2 0");
72  h2->SetFillColor(38);
73  f2->SetNpx(80);
74  f2->SetNpy(80);
75  f2->Draw("surf1 same bb [cutg]");
76  return c;
77 }
double par[1]
Definition: unuranDistr.cxx:38
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:588
virtual void FillRandom(const char *fname, Int_t ntimes=5000)
Fill histogram following distribution in function fname.
Definition: TH2.cxx:597
virtual void Draw(Option_t *option="")
Draw this function with its current attributes.
Definition: TF2.cxx:217
virtual void SetNpx(Int_t npx=100)
Set the number of points used to draw the function.
Definition: TF1.cxx:3202
static double p3(double t, double a, double b, double c, double d)
float Float_t
Definition: RtypesCore.h:53
R__EXTERN TStyle * gStyle
Definition: TStyle.h:402
int Int_t
Definition: RtypesCore.h:41
Graphical cut class.
Definition: TCutG.h:20
Double_t x[n]
Definition: legend1.C:17
static double p2(double t, double a, double b, double c)
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2851
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:37
tomato 2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:249
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:908
static double p1(double t, double a, double b)
A 2-Dim function with parameters.
Definition: TF2.h:29
The Canvas class.
Definition: TCanvas.h:31
Double_t Exp(Double_t x)
Definition: TMath.h:622
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:2156
double f2(const double *x)
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:1267
double result[121]
THist< 2, float, THistStatContent, THistStatUncertainty > TH2F
Definition: THist.hxx:317
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:3564
const Bool_t kTRUE
Definition: RtypesCore.h:91
void SetPalette(Int_t ncolors=kBird, Int_t *colors=0, Float_t alpha=1.)
See TColor::SetPalette.
Definition: TStyle.cxx:1460
unsigned int r2[N_CITIES]
Definition: simanTSP.cxx:322