Mini ROOT Tutorial

This is a quick ROOT tutorial wich includes a 1.5 h introductory talk, ROOT_Tutorial.pdf in Serbian, and a 1.5 h hands on tutorial, which is shown below. The aim of this tutorial is to show a user how to strart interactive sessions, prepare ROOT macros, include ROOT into C++ programs and find additional information on the ROOT site and forums.

1. Introduction

ROOT is a object oriented framework for data analysis in HEP. This framework allows inclusion of libraries instead of tedious writing of demanding code, it is robust and modulars and enables a user to focus on physics analysis rather than on solving technical problems. As a object oriented program its encapsulatulation enables re-use of written methods, sub-classes and inheritance allow for extension of functionalities, class hierarchy allows usage via objects with their complexity hidden in the ROOT class definitions. Here are someuseful links:

2. Setup

On lxplus there are different ROOT versions installed. For working on programs wich require frequent ROOT version updates and other CERN libraries, working on lxplus is a good choice. ROOT can be also installed on Linux, Mac or Windows platform. Follow the instructions from ROOT manual at http://root.cern.ch/drupal/content/installing-root-source. For the work on the standalone analysis, this will probably speed up the development stage of work, while running on large samples can be prepared for different systems. However, swithcing from different versions can be time consuming since local instalation lasts for a couple of hours.

2.1 Setup at lxplus

In this tutorial the ROOT setup starts with its sourcing on lxplus. This can be done by preparing a source script. First log into a lxplus machine with:

ssh -X <username>@lxplus.cern.ch

Now create a script which sources a selected ROOT version:

cd ~/

emacs iniroot.sh&

chmod u+x iniroot.sh

Lets switch to zsh with typing:

zsh

Now edit the file by adding the following lines:

#!bin/zsh

source /afs/cern.ch/sw/lcg/external/gcc/4.3.2/x86_64-slc5/setup.sh

cd /afs/cern.ch/sw/lcg/app/releases/ROOT/5.34.03/x86_64-slc5-gcc43-opt/root/

source bin/thisroot.sh

cd -

Save the file and source it by:

source iniroot.sh

Now one can start a ROOT session. For the purpos of this tutorial first create a folder where all tutorial material will be stored:

mkdir <your favorite path>/ROOT_tutorial

cd <your favorite path>/ROOT_tutorial

Start the program with:

root

To exit use:

.q

Every future session starts by sourcing ROOT.

2.2 Tasks

  • Which ROOT was sourced?

  • How can the ROOT version be changed?

  • Which root version is being used?

  • How can the most up to date ROOT version be found?

2.3 Solutions

  • Look at the iniroot.sh script and read the version. It is 5.34.03.

  • By changing the ROOT path we are sourcing different vesions. Available ROOT versions at lxplus can be found by looking at:

ls /afs/cern.ch/sw/lcg/app/releases/ROOT/

  • Start root and read the version. It is 5.34.03.

http://root.cern.ch/drupal/content/downloading-root

3. Interactive Session

Interactive sessions are very handy for quick checks of files and in the development stage of the analysis when one needs to look up a method which can be used. Here we will investigate TBrowser to browse through ROOT files and some basic and essential commands in the interractive session.

For the purpos of this tutorail go to your tutorial folder and download the example file from the ROOT site with:

cd <your favorite path>/ROOT_tutorial

cp /afs/cern.ch/work/m/mamuzic/public/tutorial.tar.gz .

gtar -zxvf tutorial.tar.gz

3.1 TBrowser

For faster startup ROOT can be used without the logo using the -l otion. Now start the program with the downloaded file:

root -l NONE/SB.root

Next, open the TBrowser with, where the b is a object name for this session, use tab completion:

TBrowser b

Investigate the following by clicking:

  • Look at the eventdata.root file, navigate through its structure.
  • Double click on TMVA_NEW
  • Under View open Editor, toolbar and Event Statusbar
  • Edit the canvas
  • Zoom in the x-scale
  • Read out x and y values of the graph
  • Save the graph as png and .C

3.2 Command Line

Now investigate the same root file from command line:

.ls

TMVA_NEW->Scan()

TMVA_NEW->Print()

TMVA_NEW->Scan("met")

TMVA_NEW->Scan("met:metomeff")

Investigate the ROOT options for eg. by typing:

TH1F * h

h-> and press tab. In a similar way investigate TH2F, TFile, TTree, TCanvas etc.

4. ROOT macro using TFile, TTree and TCanvas

It is easier to put into a file a list of commandes for ROOT. This is dove by making a ROOT macro. Open a new file with:

emacs JesJer.C&

Prepare a method with the same name in the file:

void JesJer() {}

The following commands should be placed into this method. First open a file (adjust the path to your path):

TFile *fnone = new TFile("../TMVA/15698_ttree/NONE/SB.root");

Next, declare a TTree

TTree * tnone = (TTree* )((fnone)->Get("TMVA_NEW"));

Then slect a branch:

Double_t metnone;

tnone->SetBranchAddress("met",&metnone);

Next, prepare a histogram and its line color:

TH1F *hnone = new TH1F("hnone", "NONE", 20, 0, 1500000);

hnone->SetLineColor(kBlack);

Repeat the same for JESPLUS, JESMINUS, JER, and then read out the entries and fill them into the histogram:

for (Long64_t i = 0; i < tnone->GetEntries(); ++i) {

tnone->GetEntry(i);

hnone->Fill(metnone);

tjesplus->GetEntry(i);

hjesplus->Fill(metjesplus);

tjesminus->GetEntry(i);

hjesminus->Fill(metjesminus);

tjer->GetEntry(i);

hjer->Fill(metjer);

}

Next is to Create a canvas and draw the histograms:

TCanvas *c1 = new TCanvas("c1","", 50, 0, 700, 500);

c1->cd();

hjer->Draw();

hjesplus->Draw("SAME");

hjesminus->Draw("SAME");

Save the JesJer.C macro file. Load and excecute it in the root prompt with:

.x JesJer()

You can make additional changes in the root canvas, and then save it as .C, .pdf, .eps or other possible format.

5. Including ROOT Into a C++ program, example with TGraph

First create a C++ test.cpp program which links to ROOT libraries:

#include <iostream>

#include <sstream>

#include <string>

#include <vector>

#include <TROOT.h>

#include <TSystem.h>

#include <TStyle.h>

#include <TFile.h>

#include <TCanvas.h>

#include <TGraph.h>

#include <TGraph2D.h>

#include <TH2F.h>

using namespace std;

int main(int argc, char* argv[]) {

cout << "Hello World." << endl;

return 0; }

Now compile, link and run:

g++ test.cpp -g -o test `root-config --cflags --glibs` && ./test

Now you are ready to prepare your TGraph and TCanvas in the main function. First prepare some points data with arrays:

int x[10] = {1,2,3,4,5,6,7,8,9,10};

int y[10] = {10,20,30,40,50,60,70,80,90,100};

int z[10] = {100,200,300,400,500,600,700,800,900,1000};

Then prepare a 2D and a 3D TGraph and TGraph2D respectively:

TGraph* graph = new TGraph(10, x, y);

TGraph2D* graph2D = new TGraph2D(10, x, y, z);

Prepare the canvas, this time divide it into two pads:

TCanvas* c1 = new TCanvas("c1","", 50, 0, 700, 500);

c1->Divide(2,1);

Now butifiey the graphs a bit and draw:

c1->cd(1);

graph->SetTitle("TGraph");

graph->SetMarkerStyle(20);

graph->SetMarkerSize(1);

graph->Draw("AP");

c1->cd(2);

graph2D->SetTitle("TGraph2D");

graph2D->SetMarkerStyle(20);

graph2D->SetMarkerSize(1);

graph2D->Draw("AP");

Now print it into a file:

c1->Print("Graph.pdf");

Now save, compile, link and run the test.cpp. Open the output file with:

gimp Graph.pdf

You can set some global settings using gStyle. Add the following lines at the beginning of the main:

gStyle->SetPalette(1);

gStyle->SetOptStat(0);

gStyle->SetPaintTextFormat("5.1f");

gStyle->SetTextSize(1.5);

gStyle->SetTitleOffset(1.1, "y");

gStyle->SetTitleOffset(1.1, "z");

Now make the plots nicer. Don't forget to use the command line ROOT and the ROOT site.

6. Run ROOT on Batch

ROOT can be used on lxplus Batch machines. It is useful for long and parallel processing. Here a simple example will be shown.

First prepare the Batch script. Open a file and add execute rights to it:

emacs batch.sh

chmod u+x batch.sh

Now add the lines which copy your current folder to batch. Make sure to adjust the paths correctly:

### CERN ############################

# 8nm, 1nh, 8nh, 1nd, 2nd, 1nw, 2nw #

# use with bsub -q 8nh batch.sh     #

#!/bin/zsh                          #

#####################################

cd $TMPDIR

mkdir JUDI

cd JUDI

export MYBATCHDIR=/afs/cern.ch/work/m/mamuzic/BATCH/Tutorial

export WORKDIR=`pwd`

echo "Working on a machine:" `uname -a`

Now initialize ROOT:

source /afs/cern.ch/sw/lcg/external/gcc/4.3.2/x86_64-slc5/setup.sh

cd /afs/cern.ch/sw/lcg/app/releases/ROOT/5.34.00/x86_64-slc5-gcc43-opt/root/

source bin/thisroot.sh

cd -

Next prepare the code:

cd $WORKDIR

cp /afs/cern.ch/work/m/mamuzic/public/test.cpp .

g++ test.cpp -g -o test `root-config --cflags --glibs` && ./test

And finally copy the result to your area:

rfcp *.pdf $MYBATCHDIR/.

Save the batch.sh file and send it to the lxplus farm queue with:

bsub -q 1nh batch.sh

Check your email and look for your output file in the $MYBATCHDIR.

You have completed the basic ROOT tutorial. Good luck with your analysis and don't forget to use interactive sessions and the ROOT site.

Topic attachments
I Attachment History Action Size Date Who Comment
PDFpdf ROOT_Tutorial.pdf r1 manage 1894.2 K 2013-01-01 - 19:16 JuditaMamuzic  
Edit | Attach | Watch | Print version | History: r14 < r13 < r12 < r11 < r10 | Backlinks | Raw View | WYSIWYG | More topic actions
Topic revision: r14 - 2014-06-11 - TaylorSmith
 
    • Cern Search Icon Cern Search
    • TWiki Search Icon TWiki Search
    • Google Search Icon Google Search

    Sandbox All webs login

This site is powered by the TWiki collaboration platform Powered by PerlCopyright & 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
or Ideas, requests, problems regarding TWiki? use Discourse or Send feedback