ROOT Exercise Session B

This is the guide for the ROOT Course exercise session B. The exercises cover the following areas:

Session B covers histograms, and the input/output capabilities.

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

Question B1:          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.same.

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.

Question B2:          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)Viewer and the TH1::Draw options (pg. 81) to answer the next question.

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.

Cuts and Contour Graphs

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

 

Locate theGet the script FirstContour.C. from exercises_files/day2

 Study it to answer the following questions

Question B6:          What is done by these lines?

   TObjArray *contours =

     (TObjArray*)gROOT->GetListOfSpecials()->FindObject("contours");

   TList *lcontour1 = (TList*)contours->At(0);

   TGraph *gc1 = (TGraph*)lcontour1->First();

 

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

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?

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.

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. If you can not find hrandom.C in the tutorials directory you can find it at: http://www-root.fnal.gov/root/class/examples/

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 morefill the histograms with two more nfills  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 CPU time:

root [] .x hrandom1.C++

 

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

 

Extras

Question B14:      Write and compile scripts that reproduce the pictures in questions A13 to A16.