Logo ROOT   6.08/07
Reference Guide
fit2.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_fit
3 /// \notebook
4 /// Fitting a 2-D histogram
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
10 ///
11 /// This example can be executed via the interpreter or ACLIC
12 ///
13 /// ~~~{.cpp}
14 /// root > .x fit2.C
15 /// root > .x fit2.C++
16 /// ~~~
17 ///
18 /// \macro_image
19 /// \macro_output
20 /// \macro_code
21 ///
22 /// \author Rene Brun
23 
24 #include "TF2.h"
25 #include "TH2.h"
26 #include "TMath.h"
27 
29  Double_t r1 = Double_t((x[0]-par[1])/par[2]);
30  Double_t r2 = Double_t((x[1]-par[3])/par[4]);
31  return par[0]*TMath::Exp(-0.5*(r1*r1+r2*r2));
32 }
33 Double_t fun2(Double_t *x, Double_t *par) {
34  Double_t *p1 = &par[0];
35  Double_t *p2 = &par[5];
36  Double_t *p3 = &par[10];
37  Double_t result = g2(x,p1) + g2(x,p2) + g2(x,p3);
38  return result;
39 }
40 
41 void fit2() {
42  const Int_t npar = 15;
43  Double_t f2params[npar] =
44  {100,-3,3,-3,3,160,0,0.8,0,0.9,40,4,0.7,4,0.7};
45  TF2 *f2 = new TF2("f2",fun2,-10,10,-10,10, npar);
46  f2->SetParameters(f2params);
47 
48  //Create an histogram and fill it randomly with f2
49  TH2F *h2 = new TH2F("h2","from f2",40,-10,10,40,-10,10);
50  Int_t nentries = 100000;
51  h2->FillRandom("f2",nentries);
52  //Fit h2 with original function f2
53  Float_t ratio = 4*nentries/100000;
54  f2params[ 0] *= ratio;
55  f2params[ 5] *= ratio;
56  f2params[10] *= ratio;
57  f2->SetParameters(f2params);
58  h2->Fit("f2");
59  f2->Draw("cont1 same");
60 }
double par[1]
Definition: unuranDistr.cxx:38
virtual void SetParameters(const Double_t *params)
Definition: TF1.h:439
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:216
static double p3(double t, double a, double b, double c, double d)
float Float_t
Definition: RtypesCore.h:53
int Int_t
Definition: RtypesCore.h:41
Double_t x[n]
Definition: legend1.C:17
static double p2(double t, double a, double b, double c)
tomato 2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:255
unsigned int r1[N_CITIES]
Definition: simanTSP.cxx:321
static double p1(double t, double a, double b)
A 2-Dim function with parameters.
Definition: TF2.h:33
Double_t Exp(Double_t x)
Definition: TMath.h:495
double Double_t
Definition: RtypesCore.h:55
int nentries
Definition: THbookFile.cxx:89
double f2(const double *x)
double result[121]
THist< 2, float, THistStatContent, THistStatUncertainty > TH2F
Definition: THist.hxx:308
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:3563
unsigned int r2[N_CITIES]
Definition: simanTSP.cxx:322