ROOT - Introduction

Rafał Staszewski

TCanvas

Canvas on which plots are drawn.

In [1]:
TCanvas c("c", "canvas name", 1000, 400);
In [2]:
c.Draw(); // Usually not needed
In [3]:
cout << "123";
123

TH1F

One-dimentional histogram

In [4]:
TH1F h("h_name", "title; x axis; y axis", 100, 0, 1);
In [5]:
h.Draw();
In [6]:
c.Draw(); // Usually not needed
In [7]:
gPad->ls();
Canvas Name=c Title=canvas name Option=
 TCanvas fXlowNDC=0 fYlowNDC=0 fWNDC=1 fHNDC=1 Name= c Title= canvas name Option=
  OBJ: TList	TList	Doubly linked list : 0
   TFrame  X1= 0.000000 Y1=0.000000 X2=1.000000 Y2=1.050000
   OBJ: TH1F	h_name	title : 0 at: 0x7f011d0a1398
   OBJ: TPaveText	title  	X1= 0.466165 Y1=1.102500 X2=0.533835 Y2=1.174688
In [8]:
h.Fill(0.1);
h.Fill(0.7);
h.Fill(0.7);
In [9]:
c.Draw(); // needed in the framework used to make these slides

TGraph

In [10]:
double x[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} ;
double y[10]= {0, 1, 4, 9, 16, 25, 36, 49, 64, 81} ;
TGraph g(10, x, y);
In [11]:
g.Draw("APL");
c.Draw(); // needed in the framework used to make these slides

TRandom

In [12]:
TRandom gen;
In [13]:
for(int i=0; i<10; i++){
    cout << gen.Gaus() << " ";
}
-0.15351 0.134604 -0.612583 0.551902 0.72399 -0.655643 -3.09195 0.974073 0.0811312 -1.17592 
In [14]:
TH1F h_gaus("h_gaus", ";x;#entries", 100, -10, 10);
In [15]:
for(int i=0; i<1000000; i++){
    double x = gen.Gaus(-0.5, 2);
    h_gaus.Fill(x);
}
h_gaus.Draw();
c.Draw();

TH2F

In [16]:
TH2F h_2dgaus("h_2dgaus", ";y;x;#entries", 100, -10, 10, 100, -10, 10);
In [17]:
for(int i=0; i<1000000; i++){
    double x = gen.Gaus();
    double y = gen.Gaus();
    h_2dgaus.Fill(x, y);
}
In [18]:
h_2dgaus.Draw();
c.Draw();
In [19]:
h_2dgaus.Draw("colz");
c.Draw();
In [20]:
h_2dgaus.Draw("lego");
c.Draw();

Drawing multiple histograms

In [21]:
TH1F h1("h1", "h1 title", 100, -10, 10);
TH1F h2("h2", "h2 title", 100, -10, 10);
TH1F h3("h3", "h3 title", 100, -10, 10);

for(int i=0; i<1000000; i++){
    h1.Fill(gen.Gaus());
    h2.Fill(gen.Gaus(1, 1));
    h3.Fill(gen.Gaus(0, 2));
}
In [22]:
h1.Draw();
h2.Draw();
h3.Draw();
c.Draw();
In [23]:
h1.Draw();
h2.Draw("same");
h3.Draw("same");

c.Draw();
In [24]:
h1.SetLineWidth(4);
h2.SetLineColor(2);
h3.SetLineStyle(3);
c.Draw();

TLegend

In [25]:
TLegend leg(0.2, 0.6, 0.35, 0.8);
leg.AddEntry(&h1, "dist 1", "l");
leg.AddEntry(&h2, "dist 2", "l");
leg.AddEntry(&h3, "dist 3", "l");
In [26]:
leg.Draw();
c.Draw();

SetOptStat

In [27]:
h1.Draw();
c.Draw();
In [28]:
gStyle->SetOptStat(0);
In [29]:
h1.Draw();
c.Draw();

TFile

In [30]:
TFile f_out("file.root", "recreate");
TH1F h_abc("h_abc", "", 100, 0, 10);
h_abc.Fill(0.5); h_abc.Fill(2.5);
f_out.Write();
f_out.Close();
In [31]:
TFile f_in("file.root");
TH1F * hist = (TH1F*)f_in.Get("h_abc");
hist->Draw();
c.Draw();

TTree

In [32]:
TFile f("data.root");
In [33]:
f.ls();
TFile**		data.root	
 TFile*		data.root	
  KEY: TTree	transactions;1	Tree with transaction data
In [34]:
cout << transactions->GetEntries();
100
In [35]:
transactions->Show(5);
======> EVENT:5
 n               = 8
 color           = 2, 
                  2, 2, 2, 2, 1, 
                  1, 1
 mass            = 5.84571, 
                  5.42304, 6.18322, 7.24752, 7.49148, 2.95826, 
                  3.0323, 2.81804
In [36]:
transactions->Show(9);
======> EVENT:9
 n               = 4
 color           = 4, 
                  4, 0, 2
 mass            = 7.84794, 
                  9.94601, 2.33502, 1.52197
In [37]:
transactions->Draw("n");
c.Draw();
In [38]:
transactions->Draw("mass:color", "mass>4","colz");
c.Draw();