In the previous sections we've made use
of the mouse and clicked our way through ROOT. You must be curious by now, how
these things are done on the root command line. In this section we show you how
to do the things we did with the mouse, from the command line with root
commands.
Root commands come in three flavors,
C++ commands, CINT commands, and Shell commands. Here are examples of all
three:
1)
C++ syntax:
root[0] TBrowser * b = new
TBrowser();
2)
CINT commands start with a “.”
root [1].?
// this command will
list all the CINT commands
root [2].X
[filename]
// load [filename]
and execute function [filename]
root [3].L
[filename]
// load [filename]
3)
SHELL commands start with a “.!”
root[4] .! ls
This command will list the contents of the current shell
directory
C++ commands are combined to build
macros, the scripts that you will later be writing in order to create, modify
and display your final output. The
commands are case sensitive, so make sure you've entered them properly.
When you enter commands, ROOT provides
a very helpful tab-tab feature. You
type in a variable followed by an arrow, for example:
gStyle-> and then enter a tab
This prints out on your display a
listing of all the methods and members of gStyle.
This lists all of the things that you
can do with gStyle at this point.
Now you can continue by typing in:
gStyle->
SetOpt<tab>
It will display a list of all the
methods and members that start with SetOpt.
Now type:
gStyle->
SetOptS<tab>
It completes the line to say:
gStyle-> SetOptStat. Type
gStyle->
SetOptStat( <tab>
A list of the parameters that are expected for this method will appear. If the method has multiple signatures all of them are displayed.
A history of the commands that you
entered during ROOT sessions is stored in text file called
$ROOTSYS/.root_hist. Commands can be
copied out of this text file and placed into the text file that you create as
the script (the series of running commands) of your macro.
Before we start I would like to explain a couple of things
about the syntax used on the root command line that always puzzles people.
First, you have the option to leave off the semicolon (;) at the end of a root
command when working on the command line. The difference between having it and
leaving it off is that when you leave it off the return value of the command
will be printed on the next line. For example:
root [0] 23+5 // no semicolon prints the return value
(int)28
root [1] 23+5; // semicolon no return value is printed
root [2]
The second leniency of CINT is that is allows you to use "." and
"->" interchangeably. If you have learned C++, this will bother
you and look strange. A pointer p still works when used as p.Print() rather
than p->Print(). This is a well-meaning feature of CINT that takes getting
used to, especially for the C++ expert.
ROOT
Command |
What
does this command do? |
.q |
End
a ROOT session |
TFile
*my = new TFile("htt_175a.root"); |
Open
a file |
TBrowser
*b = new TBrowser(); |
Open
the ROOT Object Browser |
my->ls(); |
List
contents of a file called "my" |
my->cd("TOPPLOT"); gDirectory->ls(); |
Change
directory to make TOPPLOT the current directory (the directory which contains
the ntuples) List
contents of current directory, now
TOPPLOT |
h21->Print(); |
To
find names of variables in the ntuple file |
ROOT
Command |
What
does this command do? |
h21->Draw("L1_pz"); |
To
display the variable "L1_pz" as a 1-D histogram |
c1->Print(); |
To
print the display of canvas "c1".
ROOT saves it as a ps file to print later. |
h21->Draw("L1_pz>>myh1"); |
To
display the variable "L1_pz" and save to a named histogram called
"myh1" |
Here are some examples of commands for
adding text to your display, setting the fill colors of objects, and the color
and style of markers that are used for drawing a histogram with error bars. To
see a menu of colors available to use in ROOT with the numbers assigned to
them, you go to the "Colors"
option under canvas item menu "View". For marker styles, you go to "Markers", also under canvas item
menu "View".
ROOT
Command |
What
does this command do? |
t1 = new
TpaveLabel(-235,1217,-44,1387, "A label","br"); t1->SetFillColor(6); myh1->Draw(); t1->Draw(); |
Create a pave label Set a fill color for it Display canvas "myh1" Display the label on the canvas |
myh1->SetFillColor(2); myh1->Draw(); |
Set the fill color of "myh1" to red and display |
h21->SetMarkerColor(4); h21->SetMarkerStyle(8); h21->Draw("L1_py","","e1"); |
Set marker color of "h21" to blue Set marker style of "h21" to a filled circle Draw "L1_py" with
error bars (Other displays are
"e2", "e3", and "e4") |
ROOT
Command |
What
does this command do? |
myh1->GetXaxis()->SetTitle("X-Axis"); |
Give the x-axis the name
"X-Axis" |
myh1->GetXaxis()->CenterTitle(1); |
Center the name of the x-axis. |
myh1->GetYaxis()->SetTitle("Y-Axis"); |
Give the y-axis the name
"Y-Axis" |
myh1->GetYaxis()->CenterTitle(1); |
Center the name of the y-axis. |
ROOT
Command |
What
does this command do? |
h21->Draw("L1_px","(L1_px>0)
"); |
Display
"L1_px" where "L1_px" is greater than zero |
h21->Draw("L1_py","(L1_px<0)
"); |
Display
"L1_py" where "L1_px" is less than zero |
h21->Draw("L1_px","(L1_py<.5)
&& (L1_pz>10)"); |
Chained
cuts |
ROOT
Command |
What
does this command do? |
gStyle->SetOptStat(111111); |
Set
statistics to display under and overflow |
ROOT
Command |
What
does this command do? |
myh1->Fit("gaus"); |
Fit
the saved histogram " myh1" with a gaussian fit. |
myh1->Fit("pol9"); |
Fit
the histogram " myh1" with a polynomial fit of degree N, in this
example pol9. |
f1=
new TF1("f1","abs(sin(x)/x)",0,10); f1->Draw(); |
Create
a user-designed function and draw it |
TF1
*e1 = new TF1("e1","expo",0,200); myh1->Fit("e1","R"); e1->SetRange(0,200); myh1->Fit("e1","R"); |
Declare
a exponential function to fit over a sub range (Note:
the "R" in the Fit command stands for
"range". If you leave out
the "R" as an argument in
the Fit command, the range of fit is the current range of the histogram(in
this case, the full range) To reset
the range to what you want, you would have to use the SetRange command: If
"R" previously left out, to reset the range of fit "e1"
from 0 to 200 Then
enter the fit command again, using "R'" option) |
TF1
*g1 = new TF1("g1","gaus",-200.,200); TF1
*g2 = new TF1("g2","gaus",-200., 0); myh1->Fit("g1","R"); myh1->Fit("g2","R+"); |
Display
2 subranges of fits on one display using built in function gaussian function |
f2
= new TF1("f2","[0]/(x*x+[1]*[1])" ,-100,100); f2->SetParameters(1000000,30); myh1->Fit("f2","R"); |
Create
a user designed function to use in a fit Set
initial values of parameter Fit
the function to histogram "myh1" |
ROOT
Command |
What
does this command do? |
myc1=
new TCanvas("myc1","My canvas"); |
Manually
create a new canvas |
myc1->Divide(2,2);
myc1_1->cd(); h21->Draw("L1_px>>his1"); myc1_2->cd(); his1->DrawCopy("lego1"); myc1_3->cd(); his1->Fit("gaus"); myc1_4->cd(); myc1_4->SetLogy(1); his1->Draw(); |
Automatically
generate 4 pads in the new canvas
"myc1" and then set the display for each pad Set pad1 "myc1_1" as active pad and
display standard histogram view of "L1_px", saving the display as a named histogram
"his1" Set
pad2 active, and draw a copy of "his1" as lego view Set
pad3 active , and draw a copy of "his1" with a gaussian fit Set
pad4 active and set to display a logarithmic histogram. Display "L1_px" in it. |
ROOT
Command |
What
does this command do? |
gEnv->Print(); |
Display
the settings of the current ROOT environment |
gObjectTable->Print(); |
Display
list of objects known in current ROOT session |
gStyle->SetOptStat(111111); |
Set
statistics to full display |
Here, let's just start afresh. We'll quit ROOT and start a new session
where we will open our example root file and again change the directory to
TOPPLOT in order to work with 2 variables. ROOT keeps a history of the commands
you have entered in a file that is called root_history.txt and you can recall
previous commands by using the arrow keys on your keyboard
ROOT
Command |
What
does this command do? |
TFile
*my = new TFile("htt_175a.root"); my->cd("TOPPLOT"); h21->Draw("L1_px"); h21->Draw("L1_py",
,"same"); |
To
also display "L1_py" on the current display that shows L1_px |
h21->Draw("L1_px:L1_py"); |
Display
L1_px and L1_py as a scatter plot |
h21->Draw("L1_px:L1_py",
,"lego"); |
Draw
as a lego plot |
h21->Draw("L1_px:L1_py",
"L1_pz<50"); |
Draw
with a cut |
h21->Draw("L1_px:L1_py",
"L1_pz<50","cont"); |
Draw
with cut as a contour plot |
Although we haven't covered this in the
tutorial, here is an example of commands to create a Root file. Files
can contain any kind of ROOT objects, such as histograms or canvases. When you
create a histogram or root tree, ROOT will automatically attach it to the ROOT
file. Other objects, such as a new canvas that you create and want to put it
into a saved ROOT file, have to be explicitly attached to the file.
The choices for the second parameter on
the TFile constructor are: RECREATE, NEW, CREATE, READ, and UPDATE. The NEW and
CREATE have the same effect. They will create a new file and open it for
writing, but if the file already exists it is not opened. RECREATE will also
create a new file and if the file already exists it will be overwritten. UPDATE
will open an existing file for writing. READ will open an existing file as read
only.
In the box below are commands to create
a new ROOT file, and then create and save a new histogram and a new canvas in
the ROOT file that you have created.
ROOT
Interactive Command |
What
does this command do? |
gROOT->Reset(); TCanvas
*mc1 = new TCanvas("mc1","My canvas"); TFile
*hfile = new TFile("newfile.root","RECREATE"); TH1F
*hr = new
TH1F("hr"," Random",20,0,10); gRandom->SetSeed(); { for ( Int_t i=0; i<500; i++) { Float_t random =
gRandom->Rndm(1)*10; hr->Fill(random); } } hr->Draw(); mc1->Modified(); mc1->Update(); hfile->Append(mc1); hfile->Write(); |
Clear
ROOT Create
a new canvas Create
a new .root file that we name as
"newfile.root" Create
a new histogram "hr" and fill the histogram with random numbers To start a multi line command from the
command line, you type an opening curly bracket. Draw
the histogram "hr" Update
the canvas "mc1" Explicitly
attach the canvas to the file (If
the object is a histogram or a ntuple/tree, ROOT automatically attaches it to
the file) Save the file |
TDirectory
*d = hfile->mkdir("MyTop"); TCanvas
*mc2= new TCanvas("mc2","My canvas"); f1=
new TF1("f1","abs(sin(x)/x)",0,10); f1->Draw(); d->Append(mc2); gDirectory->Append(mc2); hfile->Write(); hfile->Close(); |
Create
a subdirectory of your ROOT file with a pointer named "d" Make
a canvas and fill with a display Add
the canvas to the subdirectory Another
way to add canvas to subdirectory Save
file Close
file |
At this point, you might want to try
out ROOT using a ntuple file of your own.
There is a program called h2root
which converts files created with PAW into ".root" files that can be
used with ROOT. To do the conversion,
at the system prompt, you just have to type:
h2root
filename.hbook filename.root
If your file contained a HBOOK ntuple,
it is automatically converted into a ROOT tree. In ROOT terms, it becomes an
object of the TTree class. If your file
had a HBOOK histogram, it is automatically converted into a histogram object of
ROOT's TH1F class.
Once your file is converted you can
start using it with ROOT.
ROOT's official homepage from CERN
contains a Tutorial file, and one of the tutorials is an example of a macro
that reads data from an ASCII file and creates a root file with a histogram and
a ntuple. This tutorial is located at:
http://root.cern.ch/root/html/examples/basic.C.html
You can modify this macro and use it
for your own ASCII files.
In the above examples of ROOT commands
there is an example for putting a pave label into a histogram display. The easiest way to do this is to use the
graphic interface, just as we did in Part 1. You can set up a Pave Label using
the Editor that is located under the
canvas menu item Edit . Once you have the display set up in a way
you like, you can use the option Save as
canvas.C , located under the canvas menu item File. This creates a
skeletal macro with the default name of the canvas you have on display followed
by ".C" (e.g. c1.C). The skeletal macro would have the ROOT command
coding for the pave label with its position on the canvas. The macro will require some editing, but
can be a useful basis for a macro that you would create for your own use.
Additionally, a history of the commands that you entered during ROOT sessions
is stored in text file called root_history.txt. Commands can be copied out of this text file
and placed into the text file that you create as the script (the series of
running commands) of your macro.
We
hope this tutorial has given you a small introduction to ROOT and that you will
now be interested in going out into the wide world of ROOT to learn and make
use of its more advanced functionality when using it as a physics analysis
tool. There is a wealth of material
about ROOT available via the web, particularly through the homepage of ROOT
where the creators of ROOT, Rene Brun ,Fons Rademakers and their team, have
provided an enormous amount of documentation .
The ROOT Homepage is the official ROOT web site. The location is: http://root.cern.ch. Some very useful locations within this site to the beginner user
are given below. All of them can be
accessed from the listing that appears on the Homepage.
Search the ROOT Web Site. For
on-line help or a search on a particular topic it's very useful to use this
facility. The location is: http://root.cern.ch/root/Search.phtml
The ROOT Tutorials are a
description of tools that you will be using in ROOT for a PAW style
analysis. It gives examples of creating
histograms/ntuples, fits, functions,etc. with sample macros. . The
location is: http://root.cern.ch/root/Tutorials.html
The ROOT How To's are an
introduction to the infrastructure of ROOT and give examples of how to write code within ROOT classes. . The location is: http://root.cern.ch/root/Howto.html
Classes and Members Reference
Guide is an index to ROOT classes with full class description, including options to commands using the class. The
location is http://root.cern.ch/root/html/ClassIndex.html
The Publication List is a list of ROOT related
publications. At the bottom of the list is non official ROOT documentation. It
is located at: http://root.cern.ch/root/Publications.html
There
is a ROOT talk mailing list. Subscribe
via:
http://root.cern.ch/cgi-bin/registration.pl
To
submit a bug report: http://pcroot.cern.ch/root-bugs.
You
can search and browse the Bug Database before submitting your own bug report.
1.
Log on to cdfsga, fnalu, fnpat1, or d02ka
2.
On cdfsga type:
setup root
v2_23_11 –q KCC_3_3
You
may need to set up your environment. At the system prompt type:
source
~cdfsoft/.cshrc
On
fcdfsgi2, root should be already setup
On
fnalu type:
setup root
v2_23_11
On
d02ka type:
setup root
v2_23_11 -q KCC_3_3:exception
On
fnpat1 type:
setup root
v2_23_11 -q KCC_3_3
3.
Startup root by typing at the system prompt:
root
4.
Quit root by typing .q at the root prompt
root[0] .q
Axis: give the axis a title |
myh1->GetXaxis()->SetTitle("X-Axis"); |
Axis: center the title |
myh1->GetXaxis()->CenterTitle(1); |
Browser: create a ROOT Browser |
TBrowser *b = new TBrowser(); |
Canvas: manually create a
canvas |
myc1=new
TCanvas("myc1","My canvas"); |
Canvas: automatically generate
4 pads |
myc1->Divide(2,2); |
Canvas: set pad1
"myc1_1" as active pad |
myc1_1->cd(); |
Canvas: print the display of a
canvas |
c1->Print(); |
Canvas: refresh canvas
"mc1" on screen |
mc1->Update(); |
Cuts: examples |
h21->Draw("L1_py","(L1_px<0)
"); h21->Draw("L1_px","(L1_py<.5)
&& (L1_pz>10)"); |
Directory: list contents of
current directory |
gDirectory->ls(); |
Directory: change directory |
my->cd("TOPPLOT"); |
Directory: create a
subdirectory |
TDirectory *d =
hfile->mkdir("MyTop"); |
Directory: add canvas
"mc2" to subdir Directory: add object to current directory |
d->Append(mc2); gDirectory->Append(mc2); |
End a ROOT session |
.q |
File: Open a file |
TFile *my = new
TFile("htt_175a.root"); |
Files: create new .root file |
TFile *hfile = new
TFile("newfile.root","RECREATE"); |
Files: save file |
hfile->Write(); |
Files: close file |
hfile->Close(); |
File: list contents of a file |
my->ls(); |
Files: attach object to a file |
hfile->Append(mc1); |
Fits: fit histogram
"his1" with a gaussian fit |
his1->Fit("gaus"); |
Fits: fit histogram with a
polynomial fit |
his1->Fit("pol9"); |
Functions: create user designed function |
f1= new
TF1("f1","abs(sin(x)/x)",0,10); |
Functions: exponential function
to fit over a subrange |
TF1
*e1 = new TF1("e1","expo",0,200); myh1->Fit("e1","R"); |
Function : set initial values
of a parameter |
f2->SetParameters(1000000,30); |
Global variables: current
environment |
gEnv->Print(); |
Global var: list of objects in current session |
gObjectTable->Print(); |
Global variable: clearing ROOT |
gROOT->Reset(); |
Histogram: draw histogram
"his1" |
his1->Draw(); |
Histogram: draw a copy as lego
view |
his1->DrawCopy("lego1"); |
Histogram : Draw with error
bars |
h21->Draw("L1_py","","e1"); |
Histogram: set a fill color |
t1->SetFillColor(6); |
Histogram: set marker color |
h21->SetMarkerColor(2); |
Histogram: set marker style |
h21->SetMarkerStyle(8); |
Histogram: set to log. scale |
myc1_4->SetLogy(1); |
Histogram: save display as a
named hist. |
h21->Draw("L1_px>>his1"); |
Histogram: create & fill
with random nos. |
TH1F
*hr = new
TH1F("hr"," Random",20,0,10); gRandom->SetSeed(); { for ( Int_t i=0; i<500; i++) { Float_t random =
gRandom->Rndm(1)*10; hr->Fill(random); } } |
Pave label: Create a pave label |
t1 = new
TPaveLabel(-235,1217,-44,1387, "A label","br"); |
Range: set range of a fit |
e1->SetRange(0,200); |
Range: fit
with range option |
myh1->Fit("e1","R"); |
Range: set 2 subranges of fits
on one display |
TF1
*g1 = new TF1("g1","gaus",-200.,200); TF1
*g2 = new TF1("g2","gaus",-200., 0); myh1->Fit("g1","R"); myh1->Fit("g2","R+"); |
Scatter plot: display
"L1_px" and "L1_py" |
h21->Draw("L1_px:L1_py"); |
Statistics: set to display
under and overflow |
gStyle->SetOptStat(111111); |
Tree: create a TTree Viewer |
h21->StartViewer(); |
Tree: display as a 1-D
histogram |
h21->Draw("L1_pz"); |
Tree: display & save as
named histogram |
h21->Draw("L1_pz>>myh1"); |
Tree: find
names of variables in file |
h21->Print(); |
Tree: " py" on same
display as " px" |
h21->Draw("py",
,"same"); |
Tree: 2 -D histogram as a
scatter plot |
h21->Draw("L1_px:L1_py"); |
Tree: 2-D scatter plot
histogram with a cut |
h21->Draw("L1_px:L1_py",
"L1_pz<50"); |
Tree: 2-D contour histogram
with cut |
h21->Draw("L1_px:L1_py",
"L1_pz<50","cont"); |
Tree: 2-D as a lego plot |
h21->Draw("L1_px:L1_py",
,"lego"); |
The
ROOT canvas has a drop down menu which includes: File, Edit, View,
Options, Inspect and Classes. There are also Context Menus for options
File : Most of the options for this canvas menu item are fairly
self evident, but we'd particularly like to call attention to some of the Save as options
Option |
What
does it do? |
Save as
canvas.C |
Generates
a macro automatically which contain the ROOT command statements that
correspond to the picture that you created interactively in canvas
"c1" |
Save as
canvas.gif |
Saves
the canvas display as a .gif file |
Save as
canvas.ps |
Saves
the canvas display as a postscript file |
Save as
canvas.eps |
Saves
as an encapsulated postscript file |
Edit : has options to Clear Pad and Clear Canvas. Also has option Editor
Option |
What
does it do? |
Editor |
Brings
up the built-in graphics editor window which you can use to draw and edit
basic primitives (arcs, arrows, text, paves, etc) starting
from an empty canvas or as an addition to your current display A
good description of how to use the editor is located at: http://root.cern.ch/root/HowtoEdit.html |
View : Includes among
the options:
Option |
What
does it do? |
Colors |
Brings
up a separate window display of the colors available to use in ROOT with the
numbers assigned to them |
Markers |
Brings
up a separate window display of the marker types available to use in ROOT
with the numbers assigned to them |
Options : Includes among
the options: Show Statistics, Show Histogram Title, and
Show Fit Parameters which set the
respective items to either be displayed or not displayed on the canvas. Other useful options, which we made use of
in Part 1, include:
Option |
What
does it do? |
Event Status |
Displays
the Event Status bar at the bottom of the canvas display window. Gives information about coordinate points
on the canvas |
Refresh |
Refreshes
the canvas display |
Option |
What
does it do? |
ROOT |
Brings
up the Inspect window which gives information about all TROOT members being
used in the current ROOT session |
Start
Browser |
Brings
up a new ROOT Browser window. Can be used by clicking to display and modify attributes of
objects that are part of the current ROOT session. |
Classes : has an option for
Class Tree. It is more applicable for
non-novice users of ROOT.