ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
fit2.C
Go to the documentation of this file.
1 #include "TF2.h"
2 #include "TH2.h"
3 #include "TMath.h"
4 
5 // Fitting a 2-D histogram
6 // This tutorial illustrates :
7 // - how to create a 2-d function
8 // - fill a 2-d histogram randomly from this function
9 // - fit the histogram
10 // - display the fitted function on top of the histogram
11 //
12 // This example can be executed via the interpreter or ACLIC
13 // root > .x fit2.C
14 // root > .x fit2.C++
15 //Author: Rene Brun
16 
18  Double_t r1 = Double_t((x[0]-par[1])/par[2]);
19  Double_t r2 = Double_t((x[1]-par[3])/par[4]);
20  return par[0]*TMath::Exp(-0.5*(r1*r1+r2*r2));
21 }
23  Double_t *p1 = &par[0];
24  Double_t *p2 = &par[5];
25  Double_t *p3 = &par[10];
26  Double_t result = g2(x,p1) + g2(x,p2) + g2(x,p3);
27  return result;
28 }
29 
30 void fit2() {
31  const Int_t npar = 15;
32  Double_t f2params[npar] =
33  {100,-3,3,-3,3,160,0,0.8,0,0.9,40,4,0.7,4,0.7};
34  TF2 *f2 = new TF2("f2",fun2,-10,10,-10,10, npar);
35  f2->SetParameters(f2params);
36 
37  //Create an histogram and fill it randomly with f2
38  TH2F *h2 = new TH2F("h2","from f2",40,-10,10,40,-10,10);
39  Int_t nentries = 100000;
40  h2->FillRandom("f2",nentries);
41  //Fit h2 with original function f2
42  Float_t ratio = 4*nentries/100000;
43  f2params[ 0] *= ratio;
44  f2params[ 5] *= ratio;
45  f2params[10] *= ratio;
46  f2->SetParameters(f2params);
47  h2->Fit("f2");
48  f2->Draw("cont1 same");
49 }
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
static double p3(double t, double a, double b, double c, double d)
Double_t fun2(Double_t *x, Double_t *par)
Definition: fit2.C:22
float Float_t
Definition: RtypesCore.h:53
tuple f2
Definition: surfaces.py:24
void fit2()
Definition: fit2.C:30
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)
TH2D * h2
Definition: fit2dHist.C:45
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:256
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 g2(Double_t *x, Double_t *par)
Definition: fit2.C:17
Double_t Exp(Double_t x)
Definition: TMath.h:495
double Double_t
Definition: RtypesCore.h:55
int nentries
Definition: THbookFile.cxx:89
double result[121]
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
unsigned int r2[N_CITIES]
Definition: simanTSP.cxx:322