25.1. A. ROOT Command Cheat Sheet
Table of Contents
Interactive Session Basics
In a typical terminal, you start ROOT with:
root
To start without the splash screen and banner, use:
root -l
To execute a macro at startup:
root -l mymacro.C
To exit ROOT:
.q
or
gApplication->Terminate();
To execute a single command from the shell without entering the interactive prompt:
root -l -q 'mymacro.C'
or
root -l -q 'mymacro.C("arg1", 5)'
Use the command history with the up and down arrow keys. You can also save the session command log with:
.logon and .logoff for automatic scripts, or:
.x to rerun macros.
The most useful meta commands at the ROOT prompt are:
| Command | Purpose |
|---|---|
.x file.C | Run a macro interpreted |
.L file.C | Load a macro, keep definitions in memory |
.L file.C+ | Compile macro with ACLiC |
.L file.C++ | Force recompilation |
.q | Quit ROOT |
.help | Help on ROOT meta commands |
.files | List open ROOT files |
.pwd | Print current working directory |
Use gROOT and gApplication as global pointers to the ROOT environment and application, and gDirectory for the current directory (often a file or memory).
Always use .q to exit ROOT so that open files are properly closed and buffered data is written.
Getting Help
ROOT provides several built in ways to get help during a session.
To list available classes and global objects in the current session:
.class
gROOT->GetListOfClasses()->Print();
To inspect a class dictionary from the prompt:
TH1::Class()->Dump();
or simply:
TH1::Class();
To see methods of a class interactively:
TH1::Class()->GetListOfMethods()->Print();
To browse help and documentation in a web browser, use:
help (from a system shell, not ROOT) to locate manual pages, or visit the ROOT reference guide online.
Inside ROOT, to load the object browser:
new TBrowser;
This opens a graphical browser to inspect files, histograms, trees, and canvases.
Files and Directories
Below is a compact list of the most commonly used commands for handling ROOT files and directories.
Typical file operations:
| Command / Code | Description |
|---|---|
TFile *f = TFile::Open("a.root"); | Open an existing ROOT file |
TFile *f = new TFile("a.root","RECREATE"); | Create / overwrite file |
f->Write(); | Write all objects in memory to file |
f->Close(); | Close file |
f->ls(); | List contents of current file |
gDirectory->pwd(); | Print current directory path |
gDirectory->ls(); | List contents of current directory |
TDirectory *d = f->mkdir("subdir"); | Create new subdirectory |
f->cd("subdir"); | Change to directory inside file |
TH1 h = (TH1)f->Get("hname"); | Retrieve object from file |
When using TFile::Open, always check the pointer:
if (!f || f->IsZombie()) { / handle error / }
Always close a TFile with f->Close(); or by deleting the pointer to ensure all objects are written correctly.
Histograms
Useful commands to create, fill, draw, and inspect histograms.
Creation, filling, and drawing:
| Command / Code | Description |
|---|---|
TH1F *h = new TH1F("h","Title",100,0,10); | 1D float histogram |
TH1D *h = new TH1D("h","Title",100,0,10); | 1D double histogram |
h->Fill(x); | Fill one entry at value x |
h->Fill(x, w); | Fill with weight w |
h->Draw(); | Draw histogram on current canvas |
h->Draw("E"); | Draw with error bars |
h->Sumw2(); | Store proper bin error sums |
Basic properties and bin access:
| Command / Code | Description |
|---|---|
h->GetEntries(); | Number of entries |
h->GetMean(); | Mean of distribution |
h->GetRMS(); | RMS of distribution |
int bin = h->FindBin(x); | Get bin number for value x |
h->GetBinContent(bin); | Read content of bin |
h->SetBinContent(bin, value); | Set content of bin |
h->GetBinError(bin); | Bin error |
h->SetBinError(bin, err); | Set bin error manually |
Combining and scaling histograms:
| Command / Code | Description |
|---|---|
h->Scale(factor); | Multiply all bins by factor |
h->Integral(); | Integral over all bins |
h1->Add(h2); | h1 += h2 bin by bin |
h1->Add(h2, w); | h1 += w * h2 |
h1->Divide(h2); | Bin by bin division |
Call h->Sumw2(); before filling if you plan to scale or add histograms and need correct bin errors.
Graphs
Basic commands to create and plot graphs.
Creating and filling graphs:
| Command / Code | Description |
|---|---|
TGraph *g = new TGraph(); | Empty X Y graph |
g->SetPoint(i, x, y); | Set point index i to (x, y) |
TGraph *g = new TGraph(n, xarr, yarr); | From arrays |
g->Draw("AP"); | Axes plus points |
g->Draw("ALP"); | Axes, line, points |
Graphs with errors:
| Command / Code | Description |
|---|---|
TGraphErrors *ge = new TGraphErrors(n, x, y, ex, ey); | Symmetric errors |
TGraphAsymmErrors *gae = new TGraphAsymmErrors(n,x,y,exl,exh,eyl,eyh); | Asymmetric errors |
ge->Draw("AP"); | Draw with error bars |
Combining graphs:
| Command / Code | Description |
|---|---|
TMultiGraph *mg = new TMultiGraph(); | Multiple graphs container |
mg->Add(g1, "LP"); | Add graph with draw option |
mg->Add(g2, "P"); | Add second graph |
mg->Draw("A"); | Draw axes and all graphs |
Canvases and Drawing
Use TCanvas to create and manage plotting surfaces.
Basic canvas operations:
| Command / Code | Description |
|---|---|
TCanvas *c = new TCanvas("c","Title",800,600); | New canvas |
c->cd(); | Make canvas current |
c->Update(); | Force redraw |
c->Clear(); | Clear canvas |
c->Divide(nx, ny); | Divide into pads |
c->cd(pad_number); | Select pad |
Drawing multiple objects:
| Command / Code | Description |
|---|---|
h1->Draw(); | Draw first object |
h2->Draw("SAME"); | Draw on top of existing plot |
h2->Draw("HIST SAME"); | Histogram as line on same pad |
Logarithmic axes:
| Command / Code | Description |
|---|---|
gPad->SetLogx(); | Logarithmic X axis |
gPad->SetLogy(); | Logarithmic Y axis |
gPad->SetLogz(); | Logarithmic Z axis for color |
Saving plots:
| Command / Code | Description |
|---|---|
c->SaveAs("plot.png"); | Save as PNG |
c->SaveAs("plot.pdf"); | Save as PDF |
c->SaveAs("plot.svg"); | Save as SVG |
c->SaveAs("plot.root"); | Save canvas in ROOT format |
Basic Styling
Quick commands to adjust appearance without going into full styling chapters.
Axis titles and ranges:
| Command / Code | Description |
|---|---|
h->GetXaxis()->SetTitle("x [units]"); | Set X axis title |
h->GetYaxis()->SetTitle("Counts"); | Set Y axis title |
h->GetXaxis()->SetRangeUser(xmin, xmax); | Restrict X axis range |
h->SetTitle("Histogram title;X;Y"); | Title and axis labels in one string |
Line and marker styles:
| Command / Code | Description |
|---|---|
h->SetLineColor(kRed); | Change line color |
h->SetLineWidth(2); | Thicker line |
h->SetMarkerStyle(20); | Set marker type |
h->SetMarkerSize(1.2); | Larger markers |
h->SetMarkerColor(kBlue); | Marker color |
Legends and text:
| Command / Code | Description |
|---|---|
TLegend *leg = new TLegend(0.6,0.7,0.9,0.9); | New legend box |
leg->AddEntry(h1, "Data", "lep"); | Add entry, line error point style |
leg->Draw(); | Draw legend |
TLatex latex; latex.DrawLatex(x, y, "Label"); | Draw LaTeX style text in NDC or user coords |
When mixing histogram and graph drawing, always draw the object that should define axes first. Others must use "SAME" to overlay.
Functions and Fitting
Quick reference for TF1 creation and using the fit machinery.
Creating functions:
| Command / Code | Description |
|---|---|
TF1 *f = new TF1("f","gaus", xmin, xmax); | Built in Gaussian |
TF1 *f = new TF1("f","pol1", xmin, xmax); | Linear polynomial |
TF1 f = new TF1("f","[0]exp(-x/[1])", xmin, xmax); | Custom expression with parameters |
f->SetParameters(p0, p1, ...); | Set initial parameter values |
f->SetParNames("A","tau"); | Parameter names |
f->Eval(x); | Evaluate function at x |
f->Draw(); | Draw function |
Fitting histograms and graphs:
| Command / Code | Description |
|---|---|
h->Fit("gaus"); | Fit with built in Gaussian |
h->Fit("f"); | Fit with user defined TF1 *f |
h->Fit("f","R"); | Fit only in function range |
g->Fit("pol1"); | Fit graph with straight line |
TF1 *fit = h->GetFunction("gaus"); | Access last fit function |
Reading fit results:
| Command / Code | Description |
|---|---|
fit->GetParameter(i); | Parameter i |
fit->GetParError(i); | Uncertainty on parameter |
fit->GetChisquare(); | Chi square |
fit->GetNDF(); | Degrees of freedom |
Always check fit status by examining the return value of h->Fit(...) and by inspecting GetChisquare() and GetNDF() before trusting the results.
TTrees and Event Data
Essential TTree commands for basic event based analysis.
Creating and filling a tree:
| Command / Code | Description |
|---|---|
TTree *t = new TTree("t","Tree title"); | New tree |
Double_t x; t->Branch("x", &x, "x/D"); | Scalar branch |
for (...) { x = value; t->Fill(); } | Fill entries in a loop |
t->Write(); | Write tree to current file |
Reading an existing tree from a file:
| Command / Code | Description |
|---|---|
TFile *f = TFile::Open("data.root"); | Open file |
TTree t = (TTree)f->Get("t"); | Get tree |
t->Print(); | Print structure |
t->Scan(); | Text dump of entries |
Setting branch addresses and looping:
| Command / Code | Description |
|---|---|
Double_t x; t->SetBranchAddress("x", &x); | Connect variable to branch |
Long64_t n = t->GetEntries(); | Number of entries |
for (Long64_t i=0; i<n; ++i) { t->GetEntry(i); ... } | Event loop |
Quick plotting from a tree:
| Command / Code | Description |
|---|---|
t->Draw("x"); | Histogram of x |
t->Draw("y:x"); | 2D scatter plot y vs x |
t->Draw("x", "x>0"); | Histogram of x with selection cut |
t->Draw("y>>h(100,0,10)", "x>0"); | Fill named histogram with selection |
When using SetBranchAddress, the variable that receives the data must stay in scope for the entire loop. Never use a local variable that goes out of scope while the tree is still being read.
Random Numbers and Simple Simulation
Quick access commands for random number generation.
Random engines:
| Command / Code | Description |
|---|---|
TRandom *r = gRandom; | Default global random generator |
gRandom->SetSeed(0); | Seed with a time dependent seed |
gRandom->SetSeed(12345); | Fixed seed for reproducibility |
TRandom3 r3(0); | Mersenne Twister engine |
Common distributions:
| Command / Code | Description |
|---|---|
gRandom->Uniform(); | Uniform in (0, 1) |
gRandom->Uniform(a, b); | Uniform in (a, b) |
gRandom->Gaus(mean, sigma); | Gaussian |
gRandom->Poisson(mean); | Poisson |
gRandom->Exp(tau); | Exponential with mean tau |
Typical simulation loop:
TH1F *h = new TH1F("h","Gaussian",100,-5,5);
for (int i=0; i<100000; ++i) {
double x = gRandom->Gaus(0,1);
h->Fill(x);
}
h->Draw();PyROOT Shortcuts
Some very basic commands for using ROOT from Python.
Starting PyROOT simply requires importing ROOT:
import ROOTFrequently used short forms:
| Python Code | Description |
|---|---|
f = ROOT.TFile.Open("a.root") | Open ROOT file |
h = ROOT.TH1F("h","Title",100,0,10) | Create histogram |
h.Fill(x) | Fill bin |
c = ROOT.TCanvas("c","c",800,600) | New canvas |
h.Draw() | Draw histogram |
c.SaveAs("plot.png") | Save plot |
Access a TTree and draw:
t = f.Get("t")
t.Draw("x")In PyROOT, object ownership and memory management are handled differently from C++, so be careful when mixing C++ and Python code in the same session.
Useful Global Objects
A final short reference to global objects often used in commands.
| Object | Type | Purpose |
|---|---|---|
gROOT | TROOT* | Top-level ROOT system manager |
gApplication | TApplication* | GUI event loop manager |
gDirectory | TDirectory* | Current directory or file |
gPad | TPad* | Current pad or canvas |
gStyle | TStyle* | Global plotting style |
gRandom | TRandom* | Global random number generator |
These pointers are always available in a ROOT session and let you configure or query the environment quickly.
Views: 13
KAHIBARO