ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SimpleFitting.C
Go to the documentation of this file.
1 //Create an exponential fitting
2 //The idea is to create a set of numbers x,y with the function x^3 and some noise from ROOT,
3 //fit the function to get the exponent (which must be near 3) and plot the points with noise,
4 //the known function and the fitted function
5 //Author: Omar Zapata
6 #include<TRInterface.h>
7 #include<TRandom.h>
8 
10  TCanvas *c1 = new TCanvas("c1","Curve Fitting",700,500);
11  c1->SetGrid();
12 
13  // draw a frame to define the range
14  TMultiGraph *mg = new TMultiGraph();
15 
16  // create the first plot (points with gaussian noise)
17  const Int_t n = 24;
18  Double_t x1[n] ;
19  Double_t y1[n] ;
20  //Generate the points along a X^3 with noise
21  TRandom rg;
22  rg.SetSeed(520);
23  for (Int_t i = 0; i < n; i++) {
24  x1[i] = rg.Uniform(0, 1);
25  y1[i] = TMath::Power(x1[i], 3) + rg.Gaus() * 0.06;
26  }
27 
28  TGraph *gr1 = new TGraph(n,x1,y1);
29  gr1->SetMarkerColor(kBlue);
30  gr1->SetMarkerStyle(8);
31  gr1->SetMarkerSize(1);
32  mg->Add(gr1);
33 
34  // create the second plot
35  TF1 *f_known=new TF1("f_known","pow(x,3)",0,1);
36  TGraph *gr2 = new TGraph(f_known);
37  gr2->SetMarkerColor(kRed);
38  gr2->SetMarkerStyle(8);
39  gr2->SetMarkerSize(1);
40  mg->Add(gr2);
41  //passing data to Rfot fitting
43  r["x"]<<TVectorD(n, x1);
44  r["y"]<<TVectorD(n, y1);
45  //creating a R data frame
46  r<<"ds<-data.frame(x=x,y=y)";
47  //fitting x and y to X^power using Nonlinear Least Squares
48  r<<"m <- nls(y ~ I(x^power),data = ds, start = list(power = 1),trace = T)";
49  //getting the exponent
50  Double_t power;
51  r["summary(m)$coefficients[1]"]>>power;
52 
53  TF1 *f_fitted=new TF1("f_fitted","pow(x,[0])",0,1);
54  f_fitted->SetParameter(0,power);
55  //plotting the fitted function
56  TGraph *gr3 = new TGraph(f_fitted);
57  gr3->SetMarkerColor(kGreen);
58  gr3->SetMarkerStyle(8);
59  gr3->SetMarkerSize(1);
60 
61  mg->Add(gr3);
62  mg->Draw("ap");
63 
64  //displaying basic results
65  TPaveText *pt = new TPaveText(0.1,0.6,0.5,0.9,"brNDC");
66  pt->SetFillColor(18);
67  pt->SetTextAlign(12);
68  pt->AddText("Fitting x^power ");
69  pt->AddText(" \"Blue\" Points with gaussian noise to be fitted");
70  pt->AddText(" \"Red\" Known function x^3");
71  TString fmsg;
72  fmsg.Form(" \"Green\" Fitted function with power=%.4lf",power);
73  pt->AddText(fmsg);
74  pt->Draw();
75  c1->Update();
76  return c1;
77 }
virtual void Draw(Option_t *option="")
Draw this pavetext with its current attributes.
Definition: TPaveText.cxx:211
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
TCanvas * c1
Definition: legend1.C:2
Definition: Rtypes.h:61
A TMultiGraph is a collection of TGraph (or derived) objects.
Definition: TMultiGraph.h:37
virtual TText * AddText(Double_t x1, Double_t y1, const char *label)
Add a new Text line to this pavetext at given coordinates.
Definition: TPaveText.cxx:160
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
Definition: Rtypes.h:61
virtual void SetSeed(UInt_t seed=0)
Set the random generator seed.
Definition: TRandom.cxx:568
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
Definition: TMath.h:501
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
TCanvas * SimpleFitting()
Definition: SimpleFitting.C:9
virtual void Draw(Option_t *chopt="")
Draw this multigraph with its current attributes.
TVectorT< Double_t > TVectorD
Definition: TVectorDfwd.h:24
virtual void SetTextAlign(Short_t align=11)
Definition: TAttText.h:55
TPaveText * pt
ROOT::R::TRInterface & r
Definition: Object.C:4
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition: TString.cxx:2308
virtual void SetMarkerStyle(Style_t mstyle=1)
Definition: TAttMarker.h:53
virtual void SetMarkerSize(Size_t msize=1)
Definition: TAttMarker.h:54
The Canvas class.
Definition: TCanvas.h:48
static const double x1[5]
A Pave (see TPave) with text, lines or/and boxes inside.
Definition: TPaveText.h:35
double Double_t
Definition: RtypesCore.h:55
static TRInterface & Instance()
static method to get an TRInterface instance reference
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
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:53
Definition: Rtypes.h:61
virtual void SetParameter(Int_t param, Double_t value)
Definition: TF1.h:424
virtual void Add(TGraph *graph, Option_t *chopt="")
Add a new graph to the list of graphs.
virtual void Update()
Update canvas pad buffers.
Definition: TCanvas.cxx:2179
const Int_t n
Definition: legend1.C:16