Solutions to Exercises For "Introdcution to ROOT"

 

 


Exercise #1:

 


To display the histogram

1.  :> root

2.  browser = new TBrowser();

3.  Select the Open menu item from the File menu

4.  Select “Example.root” and Open

5.  Double click on ROOT Files

6.  Double click on Example.root

7.  Double click on myTree

8.  Double click on the xmain

To show the Event Status bar:

1.  Select the Event Status menu item in the Options menu.

2.  The Event Status bar shows the title of the selected object, the name, the coordinate in pixels, and other object specific info.

To add error bars

1.  Select the horizontal part of the histogram

2.  Right click to get the context menu

3.  Select the Draw Panel

4.  To draw the error bars

5.  On the Draw Panel select “E1:errors/edges” and “Same Picture”

6.  Click Draw

To add the gaussian fit

1.  Select the horizontal part of the histogram

2.  Right click to get the context menu

3.  Select the Fit Panel

4.  Select “gaus” and “Same Picture”

5.  Select Fit

 

To add color and fill

1.  Select the horizontal part of the histogram

2.  Right click to get the context menu

3.  Select the “SetFillAttributes”

4.  Select a color and your favorite pattern

5.  Select Apply

To zoom the x-axis:

1.  Left click on the -2  axis point (the cursor changes to a hand)

2.  Drag the mouse to the +2 axis point

3.  Release

To add a Pad and draw a histogram in it

1.  Select the Editor item from the Edit menu, this brings up the Editor. If you have the Editor already open just click on it to bring it into the foreground

2.  Select the Pad from the Editor

3.  Left Click and hold on the canvas where you want the upper left corner of the new Pad to be.

4.  Drag until the outline fits the size you want for the new Pad

5.  Make the new pad the active one by clicking on it with the middle mouse button

6.  Go to your browser and double click on xmain.

7.  Check your canvas and you should see a histogram in your new Pad.

 

Exercise #1B


 To display the fit statistics for the histogram above

1.  Select the menu item Show Fit Parameters in the Options menu on the canvas.

2.  Right click on the TPaveStats object to bring up the context menu

3.  Select SetOptFit, which shows you a parameter box.

4.      Enter 1 into the parameter box, and click OK.

 


To add a line of text and change the font to Greek.

1.  Select the Editor menu item from the Edit menu on the canvas.

2.  Left click on the Text/Latex Button in the Editor palette

3.  Left click on the canvas where you want to place the text

4.  Type "abcdef" and hit the ENTER key

5.  Right click on the text object you just created

6.  Select the SetTextAttributes item. This brings up the Text Attribute panel.

7.  On the Text Attribute panel select Greek-medium-r-normal and select Apply.

 


 

 


Exercise #2:


 


To create a Tree Viewer

If you already have Example.root open skip to step 6.

1.  :> root

2.  browser = new TBrowser();

3.  In the Browser, select the Open menu item from the File menu

4.  Select “Example.root” and Open

5.  Navigate to MyTree in the Example.root file

6.  In the Browser, right click on myTree. The context menu for the tree appears.

7.  Select StartViewer

 

To Draw the 2D lego Plot:

1.  In the Tree Viewer select the white box marked xmain and drag it into the X box.

2.  Drag the xs1 variable into the Y box.

3.  Right click on the white box in the Gopt and select SetLabel

4.  Set the label to ‘lego’

5.  Select Draw

 

To add a weight

1.  In the Tree Viewer locate the white box in the Weight-Selection box.

2.  Right click on this box and select SetLabel

3.  Type ‘xs1 > 0’ and select OK.

4.  Select Draw

 

To rotate the lego plot

1.  Place the cursor on the 3D box. It will change to a rotating arrow.

2.  Left click and rotate the entire box.


 


Exercise #2B:

 

 

To create a new variable

1.  Right click on the blue background of the Tree Viewer to bring up it's context menu.

2.  Select CreateNewVar from the context menu. This brings up the CreateNewVar dialog box.

3.  Enter xs1 * xmain into the text field and select OK

4.  The new variable box is in the very upper left corner of the Tree Viewer. It maybe a little difficult to select it.

 

To draw a surface using Gouraud shading

1.  Drag the new variable into the Y box.

2.  Drag the xs1 variable into the X box.

3.  Right click on the white box in the Gopt and select SetLabel from the context menu.

4.  Type SURF4 in the text box.

5.  Select Draw.

 

Exercise #2C   

To save the canvas you have created in Exercise 2B as a .root file.

1.  Select the Save as canvas.root menu item in the File menu on the canvas.

To view your saved canvas in a new session

1.  Enter .q to exit the root session and root to enter a new session.

2.  Open the file c1.root file from with the Browser

3.  On the command line type:
c1->Draw();

 

To save the canvas you created as a .C macro and save the histogram in a file..

1.  Select the Save as canvas.C menu item in the File menu on the canvas.

2.  We have to save the histogram separately because saving the canvas as a macro only saves the commands to build the canvas look and feel including the GUI objects on the canvas. However it does not save the data for the histogram. To find the histogram in the root session type this line on the command line:

    TH1* htemp = (TH1*)gROOT->FindObject("htemp");

3.  To verify that it is the correct one:

    htemp->Print();

4.  To create a new file for the histogram

    TFile * f = new TFile("histfile.root","RECREATE");

5.  Write the histogram to the file explicitly

    root [7] htemp->Write();

6.  Close the file

    root [8] f->Close();

 

Quit the current root session and start another session. Display the saved canvas by executing the macro you saved.

 

1.  Enter .q to exit the root session. Then start a new session.

2.  The macro does not save the histogram but refers to it. So we have to open the file in which we have saved the histogram before we can execute the macro to display it. On the command line type:
    
TFile * f = new TFile("histfile.root","READ");

 

3.  Now execute the generated macro:
    
.X c1.C


 

 

Exercise #3:

To find the draw options on the histogram

1.  Go to the Root System Site:
http://root.cern.ch/

2.  Find the Class Categories and follow this link:
http://root.cern.ch/root/Categories.html

3.  Find the Histogram classes  http://root.cern.ch/root/html/H_Index.html

4.  Click on the TH1 Dim1 histogram base class
http://root.cern.ch/root/html/TH1.html

5.  Find the public methods and locate the Draw method:
http://root.cern.ch/root/html/TH1.html#TH1:Draw
You can now see all the options for the 1D and 2D histogram Draw method.

You could also have looked up any other histogram classes, and traced it back to the base class. For example:

1.  In the Browser, right click on the mainHistogram;1. This displays the context menu.

2.  Note the class name in bold at the top of the context menu: TH1F.

3.  Go to the root system site and find the Classes and Members Reference Guide: http://root.cern.ch/root/html/ClassIndex.html

4.  Find the TH1F class: http://root.cern.ch/root/html/TH1F.html

5.  You see there is no public method called Draw(). This means the class inherits it from it's parent class. At the top of the page the parent class is identified in this syntax:
 class TH1F : public TH1, public TArrayF
meaning
 class <name> : public <parent>, public <parent>

6.  You can see that TH1 is the parent for the TH1F class. Click on TH1.
http://root.cern.ch/root/html/TH1.html

7.  Find the public methods and locate the Draw method:
 
http://root.cern.ch/root/html/TH1.html#TH1:Draw
You can now see all the options for the 1D and 2D histogram Draw method.
 

Exercise #3b

To convert the file toyz.rz to a root file.

 

At the system prompt type:

 

h2root toyz.rz  toyz.root

 

Open toyz.root with the Browser and view the file and the variables to explore it.


 

 


Exercise #4

 


To add a histogram:

1.  Open the Example.C file in your favorite editor.

2.  Add the following declaration:
TH1F      *myHistogram;
Float_t  myfloat;

3.  Create a new histogram:

myHistogram     = new TH1F("myHistogram",

                           "Sum of Signals",    

                           100,-4,4);

4.  Set the Attributes of the histogram and draw it:

   myHistogram->SetFillColor(40);

   myHistogram->Draw("same");

5.  In the for loop generate a value for myfloat, and fill myHistogram with it.

     myfloat = gRandom->Landau(1,0.3);

     myHistogram->Fill(myfloat,0.2);

6.  Also in the for loop add the new float to the histogram that contains the sum of all:

     totalHistogram->Fill(myfloat,0.2);

 

7.  Save the file and run it:
root [0]> .X Example.C

 

Note: see next page for full solution

 

 

//--------------------------------------------------------

// Exercise for Root 101

//--------------------------------------------------------

 

void Example(Int_t nfill=10000, UInt_t seed=0)

{

  Float_t  xs1, xs2, xmain, myfloat;

  TFile    *hfile;

  TTree    *tree;

  TBranch  *xmainBranch;

  TBranch  *xs1Branch;

  TBranch  *xs2Branch;

  TCanvas  *theCanvas;

  TH1F      *totalHistogram;

  TH1F      *mainHistogram;

  TH1F      *s1Histogram;

  TH1F      *s2Histogram;

  TH1F      *myHistogram;

  

  // Create a file

  hfile = new TFile("Example.root","RECREATE","ROOT file");

 

 

  // Create the tree branches, one for each variable

  tree        = new TTree("myTree","Example ROOT tree");

  xmainBranch = tree->Branch("xmain", &xmain, "xmain", 256000);

  xs1Branch   = tree->Branch("xs1",   &xs1,   "xs1",   256000);

  xs2Branch   = tree->Branch("xs2",   &xs2,   "xs2",   256000);

 

  // Create the canvas

  theCanvas = new TCanvas("TheCanvas","Example",200,10,600,400);

  theCanvas->SetGrid();

 

  // Create some histograms

  totalHistogram  = new TH1F( "totalHistogram",

"Total Distribution",100,-4,4);

  mainHistogram   = new TH1F( "mainHistogram ",

"Main Contributor",  100,-4,4);

  s1Histogram     = new TH1F( "s1Histogram",   

"First Signal",      100,-4,4);

  s2Histogram     = new TH1F( "s2Histogram",   

"Second Signal",     100,-4,4);

  myHistogram     = new TH1F( "myHistogram",  

"Sum of Signals",    100,-4,4);

  

  // makes sure that the sum of squares of weights will be stored

  totalHistogram->Sumw2();

 

  totalHistogram->SetMarkerStyle(21);

  totalHistogram->SetMarkerSize(0.7);

  totalHistogram->SetMaximum(nfill/20.);

  totalHistogram->Draw("e1p");

 

mainHistogram->SetFillColor(16); 

mainHistogram->Draw("same");

 

s2Histogram->SetFillColor(46);

s2Histogram->Draw("same");

 

s1Histogram->SetFillColor(42);

s1Histogram->Draw("same");

 

myHistogram->SetFillColor(40);

myHistogram->Draw("same");

 

// Refresh the canvas to draw the axis and grid

theCanvas->Update();

 

// Fill histograms randomly

gRandom->SetSeed(seed);

 

for (Int_t i=0; i< nfill; i++) {

      // generate values

xmain = gRandom->Gaus(-1,1.5);

xs1   = gRandom->Gaus(-0.5,0.5);

xs2   = gRandom->Landau(1,0.15);

myfloat = gRandom->Landau(1,0.3);

       

// fill the histograms

mainHistogram->Fill(xmain);

s1Histogram->Fill(xs1,0.3);

s2Histogram->Fill(xs2,0.2);

totalHistogram->Fill(xmain);

totalHistogram->Fill(xs1,0.3);

totalHistogram->Fill(xs2,0.2);

totalHistogram->Fill(myfloat,0.2);

myHistogram->Fill(myfloat,0.2);

       

            // write the variables to the tree

tree->Fill();

}

 

// Draw the canvas

theCanvas->Modified();

theCanvas->Update();

hfile->Append(theCanvas);

 

// save the file

hfile->Write();

 

// For a batch file would want to delete the file we created.

// this cleans up the memory.  It does two things:

//     1. closes the file

//     2. removes the file and all it's objects from memory

// Because it removes all the objects associated with the file

// from memory the histograms are no longer accesible

// and hence no longer displayed. Histograms and trees are

// automatically associated with a file.

// In this example the Canvas was explicitly

// stored, so it will also dissappear. In order to view the histograms

// created here, the "delete hfile" line is commented out.

 

//delete hfile;

 

}