ROOT Exercise Session A

This is the guide for the ROOT Course exercise session A. 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). Graphics style management. Basic graph manipulation and fitting. We also show you how to generate a PostScript file.

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 6 "A Little C++", explains the basic concepts.

In addition, there is a "C++ Guide for ROOT Users" at:

http://www-root.fnal.gov/root/CPlusPlus/index.html

Setup

To install ROOT on your own machine see Appendix A in the ROOT User's Guide, or the ROOT website:

http://root.cern.ch/root/Availability.html

Make sure you correctly set the ROOTSYS, PATH and LD_LIBRARY_PATH environment variables.

The files needed for the exercises can be found either in $ROOTSYS/tutorials or in ftp://root.cern.ch/ftp/exercises.tar.gz. 

 



Start and Quit ROOT

To start a ROOT session:

> root

To exit at any time:

root[] .q

To run a script:

root [] .x <scriptname.C>


 

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://root.cern.ch/root/html/TF1.html
http://root.cern.ch/root/html/TF1.html


You will see several constructors, one of which has four arguments.

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

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://root.cern.ch/root/html/TGraph.html

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

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.

 


Using the GUI

Question A7:                                      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:

Question A8:                                      With the GUI Editor add another arrow, this time from  x = 3, y = 0 to  x = 2, y = 5-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 graph2.C. We 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.

Question A9:         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”);

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

Question A11:     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

Question A12:     What happened and why?

Extras

Question A13:     Using hsimple.root, reproduce this picture:

Question A14:     Using hsimple.root reproduce this picture:

Question A15:     Using hsimeple.root reproduce this picture:

Question A16:     Using hsimple.root, reproduce this picture: