Exercises 2
Exercises 2
This is a guide for the ROOT exercise sessions at the CERN School of computing. The
exercises cover the following areas:
Session A covers three ways you can use ROOT: the command line, the script
processor, and the graphical user interface (GUI). We also show you how to generate
a PostScript file.
Session B covers histograms, and the input/output capabilities.
Session C is an example of an analysis using real physics data.
Session D is an example of a simulations and an event display.
Session B:
Histograms 75 – 92
Input/Output 98 – 108, 179 – 181, 211
Tree Viewer 181
Event Lists 194 – 208
Session C:
An Example Analysis 200 – 207, 218 – 224
Session D:
http://cscsrv01/rootroot.cern.ch/
The Atlas Event Display
root/Atlfast.html
11/27/23 Exercises 1
C++ Refresher
ROOT is a C++ framework; it is not only written in C++, it also uses C++ as the scripting
language. To use ROOT effectively you will need to know the basics of C++. The User's
Guide chapter 3 "A Little C++" page 25, explains the basic concepts.
In addition, there is a "C++ Guide for ROOT Users" at:
http://www-pat.fnal.gov/root/CPlusPlus/index.html
Setup
You do not have to do any setup for the exercises. The login script will take care of setting
the environment variables.
They are: $ROOTSYS, $H1 and $ATLFAST. It will also add the needed directories to
$LD_LIBRARY_PATH and $PATH.
LD_LIBRARY_PATH must contain $ROOTSYS/lib, and $PATH must contain the current
directory (.) and $ROOTSYS/bin.
To understand the installation and building of ROOT as you would do it on your own
machine see Appendix A in the ROOT User's Guide, or the ROOT website:
http://cscsrv01/rootroot.cern.ch/root/Availability.html
> root
root[] .q
To run a script:
root [] .x <scriptname.C>
11/27/23 Exercises 2
Session A
Fit a graph
Start with simple C expressions, defining simple type variables, making additions,
multiplications, up to more complex operations.
root [] 35 + 89.3
const double)1.24299999999999997e+02
root [] float x = 45.6
root [] float y = 56.2 + sqrt(x);
root [] float z = x+y;
root [] x
(float)4.55999984741210938e+01
root [] y
(float)6.29527778625488281e+01
root [] z
(float)1.08552780151367188e+02
ROOT has several function classes. The most basic is the TF1. Note that all class names
in ROOT start with "T". "F" is for function, and "1" is for one-dimensional. Locate the class
description page for TF1 on the ROOT website.
http://cscsrv01/rootroot.cern.ch/root/htmldoc/TF1.html
The constructor arguments are: the name (f1), and expression (sin(x)/x) and a lower
and upper limit (0,10).
11/27/23 Exercises 3
You can use the tab key to complete the command or list the argument. For example to
list all the methods of f1 type:
root [] f1.<TAB>
You can recall commands with the up-arrow and down-arrows. You can use emacs
commands to move the cursor on the command line.
Find the appropriate methods in the TF1 class description and call them with the f1
object to answer the following questions:
Question A1: What is the value of the function derivative at x=2?
Question A2: What is the function integral between 0 and 3?
Question A3: What is the value of the function at x=1.2456789?
Quit the ROOT session and create a new directory $HOME/tutorials where you have
write permissions. Copy the contents of the $ROOTSYS/tutorials to this new directory.
You need to do this so that you can change and save your work.
Change directory to $HOME/tutorials.
mkdir $HOME/tutorials
cd $HOME/tutorials
cp $ROOTSYS/tutorials/*.* .
Open the file graph.C with your favorite editor. This is an example of an un-named
script. An un-named script is a collection of commands enclosed in "{ }". All variables
defined in an un-named script are available on the global scope including the command
line.
Execute the script graph.C on the command line:
root [] .x graph.C
You can see that the script prints the coordinates of each point to the command line
window. Redirect the output with the ">", and verify that the coordinates.log was
written.
11/27/23 Exercises 4
Question A5: Copy graph.C to graph2.C and change it to:
1. Center the title of the axis.
2. Add two more points to the graph, one at (2.5,6 and 3,4)
Question A6: How would you modify the script graph2.C to draw two arrows
starting from the same point at x=1, y = -5, one pointing to the point 6, the other
pointing to the last point.
11/27/23 Exercises 5
Using the GUI
Fitting a Graph
Find the information in the User's Guide about the command history file to answer the
next question.
11/27/23 Exercises 6
Question A11: How can you find all the commands you have entered so far?
11/27/23 Exercises 7
A Named Script
A named script differs from an un-named script in that it contains function definitions. The
function with the same name as the file is executed when the .x filename command is
given. All variables on the stack are local in scope, and the variables created on the heap
are still global.
Copy the graph2.C file into graph3.C; edit this file, replacing the first line “{” by “void
graph3() {” and execute the script.
root [] .x graph3.C
Comment the line out that creates the variable gr on the heap and add a line to create it
on the stack:
// gr = new TGraph(n,x,y);
TGraph gr(n,x,y);
root[] gROOT->Reset()
root[] .x graph3.C
11/27/23 Exercises 8
Session B
In this session you will:
Fit and rotate a histogram
Use ACLiC
Fitting a Histogram
Exit the current ROOT session and start a new one. Create a ROOT Browser Object and
select the File:Open menu item.
root[] TBrowser b;
Open the file hsimple.root. Double click on the ROOT files folder to see
hsimple.root. Double click on hsimple.root and you can see the contents of the
file. Find the tree ntuple and double click on it. You can see several variables.
Question B2: How do you histogram the variable pz from the ntuple in
hsimple.root.
On the ROOT command line, start the Tree Viewer for the ntuple.
root[] ntuple->StartViewer();
Use the information in the User's Guide on the Tree Viewer (pg. 181) and the TH1::Draw
options (pg. 81) to answer the next question.
11/27/23 Exercises 9
Question B3: Using the Tree Viewer how would you make a profile histogram of
pz versus px.
Question B4: Fit the profile with a polynomial of degree 2.
Question B5: Using the Tree Viewer show py versus px with the condition that
px^2 + py^2 < 20 using different graphics options (see User's Guide pg.
81).
Use the option contz,list as the final draw option. On the command line
reset the global color palette. Notice that the Draw command from the Tree
Viewer was printed to the command line. Now you can use the up-arrow to
recall the command.
root[] gStyle->SetPalette(1)
root[] ntuple->Draw("py:pz","px*px+py*py< 20","contz,list",
25000, 0);
TObjArray *contours =
(TObjArray*)gROOT->GetListOfSpecials()-
>FindObject("contours");
TList *lcontour1 = (TList*)contours->At(0);
TGraph *gc1 = (TGraph*)lcontour1->First();
while(1) {
Double_t x = -4 +8*gRandom->Rndm();
Double_t y = -4 +8*gRandom->Rndm();
if (cutg->IsInside(x,y)) {
pm->SetPoint(np,x,y);
np++;
if (np == npmax) break;
}
}
11/27/23 Exercises 10
Making an Event List
Use the User's Guide (pg. 198) or the TTree class description to look up the syntax to
create an event list. From the command line and using the ntuple in file
hsimple.root, generate a TEventList with entries having pz > 10. Print the lists of
events on the screen.
Question B8: How many events are in the list?
Use this list to display the px for the events in the list.
Look at the history file and see the commands that were generated. From the history file,
take the commands that you typed in interactively and make a script.
Question B9: Write a script to do what you just did interactively: open
hsimple.root, make an event list with entries having pz > 10, print the
event list and the number of entries on the screen.
Question B10: What is the RMS of the selected px distribution?
Execute the tutorial h1draw.C. Rotate the Lego histogram in the top right part by left
clicking on it and dragging the mouse.
Question B11: Using a command, how do you get the current viewing angle
theta?
Hint: the viewing angle, theta is an attribute of TPad.
Filling Histograms
This next exercise will show you how to fill histograms and take time measurements.
Open the file hrandom.C. Study how it fills 2 histograms and prints the time it takes to fill
them. Execute the script.
root [] .x hrandom.C
While this is executing, study the script and notice how it is written so it can be compiled.
Question B12: Copy hrandom.C to hrandom1.C and modify it to add two more
histograms using TRandom2 and TRandom3 to fill them. For each case, print
the CPU time spent in the random generator.
Start a new ROOT session and run the same script using ACLiC. You should see a
considerable improvement in the CUP time:
root [] .x hrandom1.C++
11/27/23 Exercises 11
Session C
In this session you will:
Study an example analysis from DESY
The data are taken from the DESY H1 micro-DSTs. The example data file is in your
directory $H1/dstarmb.root.
First, you need to understand how the TTree::MakeSelector method creates a class
that loops over the entries in the TTree. Please read Chapter 12 in the User's Guide
"Example Analysis" pg.218.
Change directory to $HOME/tutorials, the directory you copied in the last session.
Now open the script $HOME/tutorials/h1analysis.C and read the comments.
The script shows how to use a class (the skeleton has been automatically generated by
the TTree::MakeSelector method) to loop on the files, perform some cuts and fill
two histograms with some physics quantities.
Start a root session and open the file $H1/dstarmb.root.
Using the TBrowser, display some of the variables in the TTree named h42.
root[] h42->Process("h1analysis.C")
Save the first canvas with the dm_d histogram as a ROOT file ("c1.root") with the
File:Save as canvas.root menu option. Start a new session. Read the saved
ROOT file.
11/27/23 Exercises 13
root[] .q
root
root[] TFile f("c1.root")
root[] c1->Draw()
Computing Integrals
11/27/23 Exercises 14
Session D
In this session you will:
Study an example simulation from ATLAS
mkdir $HOME/atlfast
cd $HOME/atlfast
cp $ATLFAST/* .
Now you have the source files for the ATLAS simulation and event display. Go to the its
documentation page:
http://cscsrv01/rootroot.cern.ch/root/Atlfast.html
Please read this page now. Also, look at the class index at:
http://cscsrv01/rootroot.cern.ch/root/html224/atlfast/USER_Index.html
What is rootcint?
Find the calls to rootcint in the Make-Macros file. From it you can see that it creates
the file atlfastCint.cxx. Open the file atlfastCint.cxx with the editor. Note the
comments on the top of the file. Then find the methods that were created for ATLFast.
11/27/23 Exercises 15
> grep ATLFast:: atlfastCint.cxx
obj = (ATLFast *) buf.ReadObject(ATLFast::Class());
void ATLFast::ShowMembers(TMemberInspector &R__insp,char *R__parent)
TClass *R__cl = ATLFast::IsA();
const char *ATLFast::Class_Name()
static ATLFast::R__Init __gR__InitATLFast;
Look at the three methods that were automatically created by rootcint for ATLFast.
Question D1: What is the code generated by rootcint in the makefile
doing?
Question D2: How would you call the script umain.C to generate 1000 events
of the type: WH,H->bb,W->lnu?
Execute this script.
In a new session open the new ROOT file (altfast.root) and find the TFile method
to print the compression factor:
Question D3: What is the size and compression factor of the generated file,
atlfast.root?
Quit the ROOT session and look at some events in a new session:
root [] .x display.C
Look at some events: using different projections, using the X3D and OpenGL viewers.
Click on some tracks and inspect the data.
To inspect a pi+ particle:
1. Click on Front View
2. Right-click on a blue line to bring up the context menu of the pi+ particle
11/27/23 Exercises 16
Histograms of the Particles and Jets
Using the Inspect menu on the event display canvas, start a browser. Make a new canvas
by selecting the File menu: New Canvas item.
In the browser, select the ATLFast folder in the left pane. Then browse the Tree named
“T”, go the “Electrons” folder and display the Pt of the reconstructed electrons in the
new canvas (do the same for jets).
Question D4: How many reconstructed electrons do you get?
Question D5: How many reconstructed jets do you get?
Browse the BigBang folder in the Atlfast folder. Enlarge the browser window and
move the divider so you can clearly see the subfolder hierarchy. Take for example, the
H0_7 folder, double click deeper into the folder to see more.
Browse the histograms.
Browse and inspect the Makers. For example the ElectronMaker:
1. In the left panel of the browser, find the BigBang/ElectronMaker
folder.
Question D6: What does this hierarchy signify? What is it that you see?
Start a new canvas and generate the inheritance diagrams of all the ATLFast classes
with the following commands:
Question D7: Using the context menu, how can you show all possible inter-
class relationships?
Congratulations, you have come to the end of the exercises. Enjoy, and ask lot's of
questions.
11/27/23 Exercises 17