ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
fitLinear.C
Go to the documentation of this file.
1 #include "TGraphErrors.h"
2 #include "TF1.h"
3 #include "TRandom.h"
4 #include "TCanvas.h"
5 #include "TLegend.h"
6 #include "TMath.h"
7 
8 
10 
11 void fitLinear()
12 {
13  //Example of fitting with a linear function, using TLinearFitter
14  //This example is for a TGraphErrors, but it can also be used
15  //when fitting a histogram, a TGraph2D or a TMultiGraph
16  //Author: Anna Kreshuk
17 
18  Int_t n = 40;
19  Double_t *x = new Double_t[n];
20  Double_t *y = new Double_t[n];
21  Double_t *e = new Double_t[n];
22  TCanvas *myc = new TCanvas("myc",
23  "Fitting 3 TGraphErrors with linear functions");
24  myc->SetFillColor(42);
25  myc->SetGrid();
26 
27  //Generate points along a 3rd degree polynomial:
28  makePoints(n, x, y, e, 3);
29  TGraphErrors *gre3 = new TGraphErrors(n, x, y, 0, e);
30  gre3->Draw("a*");
31  //Fit the graph with the predefined "pol3" function
32  gre3->Fit("pol3");
33  //Access the fit resuts
34  TF1 *f3 = gre3->GetFunction("pol3");
35  f3->SetLineWidth(1);
36 
37  //Generate points along a sin(x)+sin(2x) function
38  makePoints(n, x, y, e, 2);
39  TGraphErrors *gre2=new TGraphErrors(n, x, y, 0, e);
40  gre2->Draw("*same");
41  gre2->SetMarkerColor(kBlue);
42  gre2->SetLineColor(kBlue);
43  //The fitting function can be predefined and passed to the Fit function
44  //The "++" mean that the linear fitter should be used, and the following
45  //formula is equivalent to "[0]*sin(x) + [1]*sin(2*x)"
46  //A function, defined this way, is in no way different from any other TF1,
47  //it can be evaluated, drawn, you can get its parameters, etc.
48  //The fit result (parameter values, parameter errors, chisquare, etc) are
49  //written into the fitting function.
50  TF1 *f2 = new TF1("f2", "sin(x) ++ sin(2*x)", -2, 2);
51  gre2->Fit(f2);
52  f2 = gre2->GetFunction("f2");
53  f2->SetLineColor(kBlue);
54  f2->SetLineWidth(1);
55 
56  //Generate points along a -2+exp(-x) function
57  makePoints(n, x, y, e, 4);
58  TGraphErrors *gre4=new TGraphErrors(n, x, y, 0, e);
59  gre4->Draw("*same");
60  gre4->SetMarkerColor(kRed);
61  gre4->SetLineColor(kRed);
62  //If you don't want to define the function, you can just pass the string
63  //with the the formula:
64  gre4->Fit("1 ++ exp(-x)");
65  //Access the fit results:
66  TF1 *f4 = gre4->GetFunction("1 ++ exp(-x)");
67  f4->SetName("f4");
68  f4->SetLineColor(kRed);
69  f4->SetLineWidth(1);
70 
71  TLegend *leg = new TLegend(0.3, 0.7, 0.65, 0.9);
72  leg->AddEntry(gre3, " -7 + 2*x*x + x*x*x", "p");
73  leg->AddEntry(gre2, "sin(x) + sin(2*x)", "p");
74  leg->AddEntry(gre4, "-2 + exp(-x)", "p");
75  leg->Draw();
76  leg->SetFillColor(42);
77 
78 
79 }
80 
82 {
83  Int_t i;
84  TRandom r;
85 
86  if (p==2) {
87  for (i=0; i<n; i++) {
88  x[i] = r.Uniform(-2, 2);
89  y[i]=TMath::Sin(x[i]) + TMath::Sin(2*x[i]) + r.Gaus()*0.1;
90  e[i] = 0.1;
91  }
92  }
93  if (p==3) {
94  for (i=0; i<n; i++) {
95  x[i] = r.Uniform(-2, 2);
96  y[i] = -7 + 2*x[i]*x[i] + x[i]*x[i]*x[i]+ r.Gaus()*0.1;
97  e[i] = 0.1;
98  }
99  }
100  if (p==4) {
101  for (i=0; i<n; i++) {
102  x[i] = r.Uniform(-2, 2);
103  y[i]=-2 + TMath::Exp(-x[i]) + r.Gaus()*0.1;
104  e[i] = 0.1;
105  }
106  }
107 }
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:1024
virtual void SetLineWidth(Width_t lwidth)
Definition: TAttLine.h:57
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:35
void f4()
Definition: na49.C:60
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:235
Definition: Rtypes.h:61
virtual void SetName(const char *name)
Change (i.e.
Definition: TNamed.cxx:128
tuple f2
Definition: surfaces.py:24
virtual void Draw(Option_t *option="")
Draw this legend with its current attributes.
Definition: TLegend.cxx:373
void f3()
Definition: na49.C:50
int Int_t
Definition: RtypesCore.h:41
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:740
void makePoints(Int_t n, Double_t *x, Double_t *y, Double_t *e, Int_t p)
Definition: fitLinear.C:81
Double_t x[n]
Definition: legend1.C:17
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:29
virtual void SetGrid(Int_t valuex=1, Int_t valuey=1)
Definition: TPad.h:326
virtual void SetMarkerColor(Color_t mcolor=1)
Definition: TAttMarker.h:51
virtual void SetLineColor(Color_t lcolor)
Definition: TAttLine.h:54
ROOT::R::TRInterface & r
Definition: Object.C:4
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
TF1 * GetFunction(const char *name) const
Return pointer to function with name.
Definition: TGraph.cxx:1431
void fitLinear()
Definition: fitLinear.C:11
The Canvas class.
Definition: TCanvas.h:48
Double_t Exp(Double_t x)
Definition: TMath.h:495
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:280
leg
Definition: legend1.C:34
Double_t y[n]
Definition: legend1.C:17
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:606
1-Dim function class
Definition: TF1.h:149
Double_t Sin(Double_t)
Definition: TMath.h:421
A TGraphErrors is a TGraph with error bars.
Definition: TGraphErrors.h:28
Definition: Rtypes.h:61
const Int_t n
Definition: legend1.C:16