 ROOT   Reference Guide SimpleFitting.C File Reference

## Detailed Description  Create an exponential fitting The idea is to create a set of numbers x,y with the function x^3 and some noise from ROOT, fit the function to get the exponent (which must be near 3) and plot the points with noise, the known function and the fitted function

#include<TRInterface.h>
#include<TRandom.h>
TCanvas *SimpleFitting(){
TCanvas *c1 = new TCanvas("c1","Curve Fitting",700,500);
c1->SetGrid();
// draw a frame to define the range
// create the first plot (points with gaussian noise)
const Int_t n = 24;
Double_t y1[n] ;
//Generate the points along a X^3 with noise
TRandom rg;
rg.SetSeed(520);
for (Int_t i = 0; i < n; i++) {
x1[i] = rg.Uniform(0, 1);
y1[i] = TMath::Power(x1[i], 3) + rg.Gaus() * 0.06;
}
TGraph *gr1 = new TGraph(n,x1,y1);
gr1->SetMarkerColor(kBlue);
gr1->SetMarkerStyle(8);
gr1->SetMarkerSize(1);
// create the second plot
TF1 *f_known=new TF1("f_known","pow(x,3)",0,1);
TGraph *gr2 = new TGraph(f_known);
gr2->SetMarkerColor(kRed);
gr2->SetMarkerStyle(8);
gr2->SetMarkerSize(1);
//passing data to Rfot fitting
r["x"]<<TVectorD(n, x1);
r["y"]<<TVectorD(n, y1);
//creating a R data frame
r<<"ds<-data.frame(x=x,y=y)";
//fitting x and y to X^power using Nonlinear Least Squares
r<<"m <- nls(y ~ I(x^power),data = ds, start = list(power = 1),trace = T)";
//getting the exponent
Double_t power;
r["summary(m)\$coefficients"]>>power;
TF1 *f_fitted=new TF1("f_fitted","pow(x,)",0,1);
f_fitted->SetParameter(0,power);
//plotting the fitted function
TGraph *gr3 = new TGraph(f_fitted);
gr3->SetMarkerColor(kGreen);
gr3->SetMarkerStyle(8);
gr3->SetMarkerSize(1);
mg->Draw("ap");
//displaying basic results
TPaveText *pt = new TPaveText(0.1,0.6,0.5,0.9,"brNDC");
pt->SetFillColor(18);
pt->SetTextAlign(12);
pt->AddText(" \"Blue\" Points with gaussian noise to be fitted");
TString fmsg;
fmsg.Form(" \"Green\" Fitted function with power=%.4lf",power);
pt->Draw();
c1->Update();
return c1;
}

Definition in file SimpleFitting.C.

TGeant4Unit::mg
static constexpr double mg
Definition: TGeant4SystemOfUnits.h:210
n
const Int_t n
Definition: legend1.C:16
TRandom::Gaus
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:263
kGreen
@ kGreen
Definition: Rtypes.h:66
r
ROOT::R::TRInterface & r
Definition: Object.C:4
ROOT::R::TRInterface
Definition: TRInterface.h:137
TRandom.h
Int_t
int Int_t
Definition: RtypesCore.h:45
TRandom::Uniform
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:635
TString
Definition: TString.h:136
x1
static const double x1
Definition: RooGaussKronrodIntegrator1D.cxx:346
TF1::SetParameter
virtual void SetParameter(Int_t param, Double_t value)
Definition: TF1.h:630
TRInterface.h
TPaveText::Draw
virtual void Draw(Option_t *option="")
Draw this pavetext with its current attributes.
Definition: TPaveText.cxx:234
TString::Form
void Form(const char *fmt,...)
Formats a string using a printf style format descriptor.
Definition: TString.cxx:2289
TRandom
Definition: TRandom.h:27
TMath::Power
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
Definition: TMath.h:735
TRandom::SetSeed
virtual void SetSeed(ULong_t seed=0)
Set the random generator seed.
Definition: TRandom.cxx:597
kRed
@ kRed
Definition: Rtypes.h:66
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:183
TMultiGraph
A TMultiGraph is a collection of TGraph (or derived) objects.
Definition: TMultiGraph.h:36
Double_t
double Double_t
Definition: RtypesCore.h:59
TGraph
Definition: TGraph.h:41
TCanvas
Definition: TCanvas.h:23
TVectorD
TVectorT< Double_t > TVectorD
Definition: TVectorDfwd.h:22
kBlue
@ kBlue
Definition: Rtypes.h:66
ROOT::R::TRInterface::Instance
static TRInterface & Instance()
static method to get an TRInterface instance reference
Definition: TRInterface.cxx:185
TPaveText
Definition: TPaveText.h:21
TF1
1-Dim function class
Definition: TF1.h:212
pt
TPaveText * pt
Definition: entrylist_figure1.C:7
c1
return c1
Definition: legend1.C:41