Logo ROOT   6.14/05
Reference Guide
fitLinear.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_fit
3 /// \notebook -js
4 /// Example of fitting with a linear function, using TLinearFitter
5 /// This example is for a TGraphErrors, but it can also be used
6 /// when fitting a histogram, a TGraph2D or a TMultiGraph
7 ///
8 /// \macro_image
9 /// \macro_output
10 /// \macro_code
11 ///
12 /// \author Anna Kreshuk
13 
14 #include "TGraphErrors.h"
15 #include "TF1.h"
16 #include "TRandom.h"
17 #include "TCanvas.h"
18 #include "TLegend.h"
19 #include "TMath.h"
20 
21 
22 void makePoints(Int_t n, Double_t *x, Double_t *y, Double_t *e, Int_t p);
23 
24 void fitLinear()
25 {
26  Int_t n = 40;
27  Double_t *x = new Double_t[n];
28  Double_t *y = new Double_t[n];
29  Double_t *e = new Double_t[n];
30  TCanvas *myc = new TCanvas("myc",
31  "Fitting 3 TGraphErrors with linear functions");
32  myc->SetGrid();
33 
34  //Generate points along a 3rd degree polynomial:
35  makePoints(n, x, y, e, 3);
36  TGraphErrors *gre3 = new TGraphErrors(n, x, y, 0, e);
37  gre3->Draw("a*");
38  //Fit the graph with the predefined "pol3" function
39  gre3->Fit("pol3");
40  //Access the fit resuts
41  TF1 *f3 = gre3->GetFunction("pol3");
42  f3->SetLineWidth(1);
43 
44  //Generate points along a sin(x)+sin(2x) function
45  makePoints(n, x, y, e, 2);
46  TGraphErrors *gre2=new TGraphErrors(n, x, y, 0, e);
47  gre2->Draw("*same");
48  gre2->SetMarkerColor(kBlue);
49  gre2->SetLineColor(kBlue);
50  //The fitting function can be predefined and passed to the Fit function
51  //The "++" mean that the linear fitter should be used, and the following
52  //formula is equivalent to "[0]*sin(x) + [1]*sin(2*x)"
53  //A function, defined this way, is in no way different from any other TF1,
54  //it can be evaluated, drawn, you can get its parameters, etc.
55  //The fit result (parameter values, parameter errors, chisquare, etc) are
56  //written into the fitting function.
57  TF1 *f2 = new TF1("f2", "sin(x) ++ sin(2*x)", -2, 2);
58  gre2->Fit(f2);
59  f2 = gre2->GetFunction("f2");
60  f2->SetLineColor(kBlue);
61  f2->SetLineWidth(1);
62 
63  //Generate points along a -2+exp(-x) function
64  makePoints(n, x, y, e, 4);
65  TGraphErrors *gre4=new TGraphErrors(n, x, y, 0, e);
66  gre4->Draw("*same");
67  gre4->SetMarkerColor(kRed);
68  gre4->SetLineColor(kRed);
69  //If you don't want to define the function, you can just pass the string
70  //with the the formula:
71  gre4->Fit("1 ++ exp(-x)");
72  //Access the fit results:
73  TF1 *f4 = gre4->GetFunction("1 ++ exp(-x)");
74  f4->SetName("f4");
75  f4->SetLineColor(kRed);
76  f4->SetLineWidth(1);
77 
78  TLegend *leg = new TLegend(0.3, 0.7, 0.65, 0.9);
79  leg->AddEntry(gre3, " -7 + 2*x*x + x*x*x", "p");
80  leg->AddEntry(gre2, "sin(x) + sin(2*x)", "p");
81  leg->AddEntry(gre4, "-2 + exp(-x)", "p");
82  leg->Draw();
83 
84 }
85 
86 void makePoints(Int_t n, Double_t *x, Double_t *y, Double_t *e, Int_t p)
87 {
88  Int_t i;
89  TRandom r;
90 
91  if (p==2) {
92  for (i=0; i<n; i++) {
93  x[i] = r.Uniform(-2, 2);
94  y[i]=TMath::Sin(x[i]) + TMath::Sin(2*x[i]) + r.Gaus()*0.1;
95  e[i] = 0.1;
96  }
97  }
98  if (p==3) {
99  for (i=0; i<n; i++) {
100  x[i] = r.Uniform(-2, 2);
101  y[i] = -7 + 2*x[i]*x[i] + x[i]*x[i]*x[i]+ r.Gaus()*0.1;
102  e[i] = 0.1;
103  }
104  }
105  if (p==4) {
106  for (i=0; i<n; i++) {
107  x[i] = r.Uniform(-2, 2);
108  y[i]=-2 + TMath::Exp(-x[i]) + r.Gaus()*0.1;
109  e[i] = 0.1;
110  }
111  }
112 }
virtual TFitResultPtr Fit(const char *formula, Option_t *option="", Option_t *goption="", Axis_t xmin=0, Axis_t xmax=0)
Fit this graph with function with name fname.
Definition: TGraph.cxx:1050
virtual void SetLineWidth(Width_t lwidth)
Set the line width.
Definition: TAttLine.h:43
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:23
TF1 * GetFunction(const char *name) const
Return pointer to function with name.
Definition: TGraph.cxx:1457
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:256
Definition: Rtypes.h:59
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition: TNamed.cxx:140
virtual void Draw(Option_t *option="")
Draw this legend with its current attributes.
Definition: TLegend.cxx:423
int Int_t
Definition: RtypesCore.h:41
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:745
Double_t x[n]
Definition: legend1.C:17
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:27
virtual void SetGrid(Int_t valuex=1, Int_t valuey=1)
Definition: TPad.h:327
virtual void SetMarkerColor(Color_t mcolor=1)
Set the marker color.
Definition: TAttMarker.h:38
virtual void SetLineColor(Color_t lcolor)
Set the line color.
Definition: TAttLine.h:40
ROOT::R::TRInterface & r
Definition: Object.C:4
The Canvas class.
Definition: TCanvas.h:31
Double_t Exp(Double_t x)
Definition: TMath.h:726
double Double_t
Definition: RtypesCore.h:55
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition: TLegend.cxx:330
leg
Definition: legend1.C:34
Double_t y[n]
Definition: legend1.C:17
you should not use this method at all Int_t Int_t Double_t Double_t Double_t e
Definition: TRolke.cxx:630
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:627
1-Dim function class
Definition: TF1.h:211
Double_t Sin(Double_t)
Definition: TMath.h:636
A TGraphErrors is a TGraph with error bars.
Definition: TGraphErrors.h:26
Definition: Rtypes.h:59
const Int_t n
Definition: legend1.C:16