Документ взят из кэша поисковой машины. Адрес оригинального документа : http://acat02.sinp.msu.ru/presentations/brun/Exercises_2.doc
Дата изменения: Sat Jul 6 23:58:08 2002
Дата индексирования: Mon Oct 1 20:17:55 2012
Кодировка:


ROOT Exercises

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.

References and Reading Material


To maximize the available time and the access to the ROOT development
team we recommend you read parts of the User's Guide before the exercise
sessions. To point you to the specific pages we include the pages for
each session.
You may also want to read the exercise guide in its entirety to prepare
for the exercises.


|Subject |Pages in User's Guide |
|Session A: | |
|Introduction |3 - 10 |
|Command Line |11 - 16, 18 - 20 |
|Scripts |37 - 39, 45, 47 - 49 |
|GUI, Context Menus, |13 - 15, 79 |
|Draw Panel | |
|Fitting |79 |
|PostScript |174 -175 |
| | |
|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: | |
|The Atlas Event |http://cscsrv01/rootroot.|
|Display |cern.ch/root/Atlfast.html|


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



Start and Quit ROOT

To start a ROOT session:


> root
To exit at any time:


root[] .q
To run a script:


root [] .x


Session A



In this session you will:
. Use the ROOT command line
. Write an un-named and a named script
. Use the GUI to create objects and change their attributes.
. Save your canvas to a PostScript file
. Fit a graph

The ROOT Command Line

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

Creating a Function Object from the Command Line

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


You will see several constructors, one of which as four argument.
TF1 TF1(const char* name, const char* formula, Double_t xmin = 0,
Double_t xmax = 1)
Create the following 1-D function and draw it with:
root [] TF1 f1("f1","sin(x)/x",0,10);
root [] f1.Draw();
The constructor arguments are: the name (f1), and expression (sin(x)/x)
and a lower and upper limit (0,10).
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.
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:

1: What is the value of the function derivative at x=2?

2: What is the function integral between 0 and 3?

3: What is the value of the function at x=1.2456789?

Writing and Running an Un-Named Script

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.


root [] .x graph.C; > coordinates.log
root [] .! more coordinates.log
The ".!" command prefix allows you to enter a shell command.
Find the class description page for TGraph to answer the following
questions:
http://cscsrv01/rootroot.cern.ch/root/htmldoc/TGraph.html

4: The draw command specifies the option ACP, what does this mean?

5: 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)

6: 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.

Using the GUI


7: Use the context menu to change the graph line thickness, line color,
marker style, and marker color. Change the background of the canvas. Zoom
the graph so that it starts at 0.5 and ends at 3.5. The finished canvas
should look like this:

8: With the GUI Editor add another arrow, this time from x= 3, y = 0 to
x=2, y=-5.
Pull down the File menu and select Save as canvas.ps. This creates a
PostScript file of the canvas named after the canvas name (c1.ps). Print
the file on the local printer.

Fitting a Graph

Re-execute the script macro2graph2.C. We have a graph of two functions;
weWe would like to add a fit for each section of the graph. Look at the
TGraph::Fit method on the TGraph class description page. At first fit the
graph with the GUI:
1.Select the graph with the mouse
2. Right-click to bring up the context menu.
3. Select FitPanel.
Fit the first part with a polynomial of degree 4, and the second part
with a polynomial of degree 1, using the slider. In the end, use the "Add
to list of function" and "same picture" options to see both fits on the
graph.

9: How would you modify the first script graph.C adding a loop to
fit the graph with polynomials of degree 2 to 7 keeping all the
fits on the canvas and assigning a different color to each
function? The added statements should not produce any output in
the alphanumeric window.
To access the list of fitted functions for the graph, you can do:
root [] TList *lfits = gr->GetListOfFunctions();
root [] lfits->ls();
To get a pointer to the fitted function with a polynomial of degree 4, do

root [] TF1 *pol4 = (TF1*)lfits->FindObject("pol4")
Or
root [] TF1 *pol4 = gr->GetFunction("pol4");

10: Why is it necessary to cast to (TF1*) in the first case?

The Command History

Find the information in the User's Guide about the command history file
to answer the next question.

11: How can you find all the commands you have entered so far?

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);
Now, reset the ROOT environment and execute graph3 again:
root[] gROOT->Reset()
root[] .x graph3.C

12: What happened and why?



Session B

In this session you will:
. Fit and rotate a histogram
. Use the Object Browser and the Tree Viewer
. Make a profile and contour graphs
. Build an event list from a cut
. Fill a histograms with random numbers
. Use ACLiC

Fitting a Histogram

Execute the tutorials hsimple.C and hsum.C.

1: In the canvas generated by hsum.C, how would you add the fit of
the second signal s2 using a Landau distribution in a sub-range?
Keep the original histograms are kept on the same picture.

The Object Browser

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.



2: How do you histogram the variable pz from the ntuple in
hsimple.root.

Using the Tree Viewer

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.



3: Using the Tree Viewer how would you make a profile histogram of
pz versus px.

4: Fit the profile with a polynomial of degree 2.

Cuts and Contour Graphs


5: 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);
Locate the script FirstContour.C. Study it to anser the following
questions

6: What is done by these lines?


TObjArray *contours =
(TObjArray*)gROOT->GetListOfSpecials()->FindObject("contours");
TList *lcontour1 = (TList*)contours->At(0);
TGraph *gc1 = (TGraph*)lcontour1->First();


7: What does this loop do in FirstContour.C?


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;
}
}

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.

8: 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.

9: 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.

10: What is the RMS of the selected px distribution?

Rotating Lego Plots

Execute the tutorial h1draw.C. Rotate the Lego histogram in the top right
part by left clicking on it and dragging the mouse.

11: 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.

12: 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.

Using the Compiler interface ACLiC

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++


13: Copy the hrandom1.C script to hrandom2.C and modify it so to
draw the histogram and display it after the script has finished.



Session C

In this session you will:
. Study an example analysis from DESY
. Learn about the TTree::MakeSelector method to automatically
create a class that loops over each entry in a tree.
. Save and retrieve a canvas to and from a ROOT file
. Compute the integral of a histogram
. Compute the integral of a function within a range
The data are taken from the DESY H1 micro-DSTs. The example data file is
in your directory $H1/dstarmb.root.

Example Script from TTree::MakeSelector

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.


root[] TFile f("$H1/dstarmb.root");
Using the TBrowser, display some of the variables in the TTree named h42.


root[] new TBrowser();
Execute the script h1analysis.C on the h42 tree.

root[] h42->Process("h1analysis.C")

Saving and Retrieving a Canvas

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.


root[] .q
root
root[] TFile f("c1.root")
root[] c1->Draw()


Computing Integrals

Get a pointer to the Dstar histogram named "hdmd".
Get a pointer to the fitted function "f5".
Compute hint = the histogram bin integral.
Compute fint = the f5 integral where the function is positive (0.139 -
0.17).
Use the TH1 and TF1 class description web page to find the right method
to calculate the integral for each object.
http://cscsrv01/rootroot.cern.ch/root/htmldoc/TH1.html
http://cscsrv01/rootroot.cern.ch/root/htmldoc/TF1.html


1: What is the value of ratio = fint/hint?


Session D

In this session you will:
. Study an example simulation from ATLAS
. Learn about rootcint
. Run the simulation
. Play with the Event Display
. Make a class diagram of the user classes
Make a directory called $HOME/atlfast.
Copy the contents of $ROOTSYS/altfast $ATLFAST into the new directory
and expand the tar file.


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
Look up the class description for ATLFast the base class.

Compiling ALTFast Classes with ROOT


Look at the makefile and Make-Macros to understand what happens.
libAtlfast.so contains the simulation/reconstruction classes.
libAtlfastg.so contains the event display classes.

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.


> 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.

1: What is the code generated by rootcint in the makefile doing?

Close the altfastCint.cxx file.


Making ATLFast Events

Open the $ATLFAST/README file or the atlfast web page:

http://cscsrv01/rootroot.cern.ch/root/Atlfast.html

2: 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:


root[] TFile f("atlfast.root")
root[] f.


3: What is the size and compression factor of the generated file,
atlfast.root?

The Event Display

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
3. Select Inspect in the context menu
4. Follow the red links to inspect the specifics of the particle
Use these same steps to inspect different particles in different views.
Browse through a few events selecting next and previous.

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).

4: How many reconstructed electrons do you get?

5: How many reconstructed jets do you get?

Browsing Big Bang

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.
2. Right-click to bring up the context menu
3. Select the Inspect menu item
In the right panel, you see the list of electrons for the displayed
event.

6: What does this hierarchy signify? What is it that you see?

Making a Class Diagram

Start a new canvas and generate the inheritance diagrams of all the
ATLFast classes with the following commands:
root [] TCanvas *diag = new
TCanvas("diag","inheritances",10,10,1000,700);
root [] TClassTree ct;
root [] ct.Draw("ATLF*");

7: 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.