TF1
, TGraph
, TRandom3
, TH1
, TH2
, TFile
ROOT is a software toolkit which provides building blocks for
ROOT is written mainly in C++
ROOT can be imagined as a family of building blocks for a variety of activities, for example:
ROOT is shipped with an interpreter, CLING
Can interpret “macros” (non compiled programs)
ROOT web site: the source of information and help for ROOT users
Similar to C++:
Basic types: first letter is capitalised and have suffix “_t”:
Int_t
Float_t
Double_t
Long64_t
The ROOT data types are used in order to make user code and ROOT code more platform independent.
Names of root classes start with “T” e.g. : TFile, TTree, TH1F, TGraph, …
TH1F
- 1D Histogram filled using floating precision dataTH1D
- 1D Histogram filled using double precision dataTFile
– a file containing persistent dataTDirectory
– a directory (useful to keep a TFile tidy/organised)TTree
– can store per-event info in branches and leavesTF1
– 1-dimensional function, TF2, …TString
– a ROOT string object (better than a C/C++ string)start up root by typing this at the shell prompt
root
root file.root
//load a ROOT fileroot ana.cpp
//execute a shell scriptquit root
root[n] .q
1 + 1
(int) 2
sqrt(3.)
(double) 1.7320508
1 > 2
(bool) false
ROOT allows not only to type in C++ statements, but also advanced mathematical functions, which live in the TMath namespace.
TMath::Pi()
(double) 3.1415927
TMath::Erf(.2)
(double) 0.22270259
Declare variables and used a for control structure. Tab-completion available!
double x = .5;
int N = 30;
double gs = 0;
for (int i=0; i<N; i++) gs += TMath::Power(x,i);
double aa = TMath::Abs(gs - (1-TMath::Power(x,N-1))/(1-x));
cout << aa <<endl;
1.86265e-09
%jsroot on
TCanvas *c1 = new TCanvas; //for juypter
The $.$ and $\to$ operators are used to access methods and members of objects and pointers to objects
Example:
TF1 f1("f1","sin(x)/x",0.,10.);
f1.Draw();
TF1 *f1 = new TF1("f1","sin(x)/x",0.,10.); //auto f1 = new TF1("f1","sin(x)/x",0.,10.);
f1->Draw();
The class TF1 represents one dimensional functions (e.g. f(x) ):
TF1 *f1 = new TF1("f1", "sin(x)/x", 0., 10.);//name, formula, min, max
f1->Draw();
c1->Draw();//for juypter-notebook
TF1 *f2 = new TF1("f2","[0]*sin([1]*x)/x",0.,10.);
f2->SetParameters(1,3);
f2->Draw();
c1->Draw();
f2->Eval(3)
(double) 0.13737283
for(int i=0; i<10; i++)
cout << i<< ", " << f2->Eval(i) <<endl;
0, nan 1, 0.14112 2, -0.139708 3, 0.137373 4, -0.134143 5, 0.130058 6, -0.125165 7, 0.119522 8, -0.113197 9, 0.106264
A TGraph is an object made of two arrays X and Y with npoints each.
double x[50], y[50];
int n = 50;
for (int i=0; i<n; i++) {
x[i] = 1 + i * 0.2;
y[i] = 10 * sin(2*x[i])/x[i];
}
TGraph *g = new TGraph(n,x,y);
cout << "Default name of a TGraph: "<< g->GetName() <<endl;
g->SetName("g");
cout <<"Current name of the TGraph: " << g->GetName() <<endl;
g->SetTitle("Graph title; X title; Y title");
g->Draw("AL*");
c1->Draw();
Default name of a TGraph: Graph Current name of the TGraph: g
An alternative way to fill and access a TGraph
int n = 50;
TGraph *g = new TGraph;
for (int i=0; i<n; i++) {
double x = 1 + i * 0.2;
double y = 10 * sin(2*x)/x;
g->SetPoint(i,x,y);
}
g->Draw("AL*");
c1->Draw();
cout << "Number of Points in the TGraph: "<< g->GetN() <<endl;
double *x;
double *y;
x = g->GetX();
y = g->GetY();
for(int i=0;i<g->GetN();i++) {
cout<< i << " " << x[i] << " " << y[i] << endl;
}
Number of Points in the TGraph: 50 0 1 9.09297 1 1.2 5.62886 2 1.4 2.39277 3 1.6 -0.364838 4 1.8 -2.45845 5 2 -3.78401 6 2.2 -4.32546 7 2.4 -4.15069 8 2.6 -3.3979 9 2.8 -2.25452 10 3 -0.931385 11 3.2 0.364216 12 3.4 1.45327 13 3.6 2.20463 14 3.8 2.54716 15 4 2.4734 16 4.2 2.03476 17 4.4 1.32936 18 4.6 0.484543 19 4.8 -0.363181 20 5 -1.08804 21 5.2 -1.59197 22 5.4 -1.81655 23 5.6 -1.74853 24 5.8 -1.41867 25 6 -0.894288 26 6.2 -0.267104 27 6.4 0.361734 28 6.6 0.897081 29 6.8 1.26347 30 7 1.41515 31 7.2 1.34119 32 7.4 1.06521 33 7.6 0.639998 34 7.8 0.138146 35 8 -0.359879 36 8.2 -0.778179 37 8.4 -1.05663 38 8.6 -1.15919 39 8.8 -1.07823 40 9 -0.83443 41 9.2 -0.472354 42 9.4 -0.0526975 43 9.6 0.35762 44 9.8 0.695881 45 10 0.912945 46 10.2 0.980189 47 10.4 0.893072 48 10.6 0.670907 49 10.8 0.35301
cout << g->Eval(2.1) << endl; //linear interpolation
cout << g->Eval(2.1,0,"S") << endl; //spline interpolation
-4.05474 -4.15015
The predefined functions:
g->Fit("f2");
c1->Draw();
**************************************** Minimizer is Minuit / Migrad Chi2 = 7.78076e-08 NDf = 48 Edm = 1.55616e-07 NCalls = 63 p0 = 10.0001 +/- 2.55669e-05 p1 = 2 +/- 8.07647e-07
TRandom3
The following methods are provided to generate random numbers distributed according to some basic distributions:
gRandom
is a pointer to the current random number generator.
for(int i=0; i<10; i++)
cout<< gRandom->Rndm() << " ";
cout<<endl;
0.999742 0.16291 0.282618 0.947201 0.231657 0.484974 0.957477 0.744305 0.540044 0.739953
TRandom3 *random = new TRandom3(0);
for(int i=0; i<10; i++)
cout << random->Gaus(0,1) << " ";
cout << endl;
1.05396 2.29056 -1.03573 -1.22822 0.378638 0.994402 -0.455801 -1.15413 -0.421177 -0.00649778
double x, y, z;
for(int i=0;i<10;i++) {
random->Sphere(x,y,z,1.);
cout << Form("%i: (%f, %f, %f)",i,x,y,z)<<endl;
}
0: (0.680044, 0.663785, 0.311335) 1: (0.827886, -0.339533, -0.446454) 2: (-0.810450, -0.496339, 0.311155) 3: (0.173254, 0.239302, -0.955363) 4: (-0.215053, 0.937207, -0.274580) 5: (0.175228, -0.451156, 0.875073) 6: (-0.965195, -0.194583, 0.174747) 7: (-0.009645, 0.340577, -0.940167) 8: (0.376217, 0.703227, 0.603268) 9: (0.448386, 0.832595, -0.325171)
TH1F *h = new TH1F("h", "hist", 100, -2, 6.);
for (int i=0; i<10000; i++)
h->Fill(random->Gaus(2,1));
h->Draw();
c1->Draw();
h->Fit("gaus");
c1->Draw();
FCN=72.9035 FROM MIGRAD STATUS=CONVERGED 63 CALLS 64 TOTAL EDM=7.3964e-09 STRATEGY= 1 ERROR MATRIX ACCURATE EXT PARAMETER STEP FIRST NO. NAME VALUE ERROR SIZE DERIVATIVE 1 Constant 3.20225e+02 3.94688e+00 1.34918e-02 -3.47114e-05 2 Mean 2.00479e+00 9.97000e-03 4.18510e-05 -1.84274e-03 3 Sigma 9.89865e-01 7.12924e-03 8.17151e-06 -5.20168e-02
TRandom3 *rand1 = new TRandom3(0);
TH2F *h2 = new TH2F("h2", "hist", 100, -2., 2., 100, -2, 2);
for (int i=0;i<100000;i++) {
h2->Fill(rand1->Gaus(0,0.5), rand1->Gaus(0,0.2));
}
h2->Draw("colz");
c1->Draw();
// Drawing.cpp
#include "TCanvas.h"
#include "TH2F.h"
#include "TRandom3.h"
void Drawing()
{
TH2F *h3 = new TH2F("h3", "hist", 100, -2., 2., 100, -2, 2);
TRandom3 *rand1 = new TRandom3(0);
for (int i=0; i<100000; i++) {
h3->Fill(rand1->Gaus(0,0.5),rand1->Gaus(0,0.2));
}
h3->Draw("colz");
c1->Draw();
}
How to execute a script
root Drawing.cpp
.x Drawing.cpp
.L Drawing.cpp
; Drawing();
.x Drawing.cpp
c1->Draw();//for juypter
TCanvas *c1 = new TCanvas;//for juypter
TFile *fout = new TFile("output.root","recreate");
g->Write();
h->Write();
h2->Write();
fout->Close();
!ls -lha *.root # !shell command
-rw-r--r-- 1 zhli staff 3.4M Apr 12 07:53 out.root -rw-r--r-- 1 zhli staff 16K Apr 12 08:11 output.root -rw-r--r-- 1 zhli staff 3.4M Apr 12 07:53 tree.root
TFile *fin = new TFile("output.root");
fin->ls(); //ROOT shell: .ls
TFile** output.root TFile* output.root KEY: TGraph g;1 Graph title KEY: TH1F h;1 hist KEY: TH2F h2;1 hist
c1->Clear();
g->Draw("*");
c1->Draw();
h2->Draw("colz");
c1->Draw();