Using Root for interactive analysis of a Root file

and peeking at Kanga files

NOTE: These pages are still being worked on. They do contain errors. Please send me a note when something does not work for you.

These pages are not meant as a substitute for the excellent tutorials 101 and 102 found at Fermilab (have a look at their `Getting started with ROOT', too). Rather, these pages should provide (1) a crashcourse in how to get started with ROOT in BaBar and (2) a cookbook where you can find answers to questions on how to get things done in ROOT.

There is a hypernews where you can and should ask questions on the interactive usage of ROOT.
A mailing list provides information on what releases of ROOT are available at SLAC.
Another webpage collects (links to) information on ROOT.

The following sections are covered in this tutorial:

The Environment - a few definitions and concepts
Starting and terminating ROOT - the essentials
GUIs: Looking at the Output of the Beta Tutorial - to play around and get started
CINT in brief - look at the TAB features provided by ROOT
Customizing your environment - get rid of the default style by switching to "Plain"
How to get things done in ROOT - all the stuff you need to know to make plots within a reasonable time
How to look at KANGA files - plot tag contents and check for MC truth

The Environment
What do people mean when talking about ROOT?

This tutorial is on the interactive usage of ROOT which can happen in several ways, among them

Browser - interactive GUI-based operations on ROOT-files
TreeViewer - interactive GUI-based operations on ROOT-Trees
Editor - add graphics elements to a Canvas
DrawPanel - GUI to draw histograms
FitPanel - GUI to simple-minded fitting
Inspect - peek at attributes of objects

The following is an oversimplification, but good enough for a start:




HIGZ window


a zone in the HIGZ window



In interactive usage, the semicolon at the end of a statement is not required (but it is required in macros!)
-> can be abbreviated with . (example: hist->Draw() is equivalent to hist.Draw())

The following two lines are equivalent by definition and in order to save typing

b = new TClass(...);
TClass *b = new TClass(...);

If an object is not declared upon first usage, CINT searches ROOT's global scope for an object with an identical name and sets up a pointer to it.

Starting and terminating ROOT
Start ROOT (best after having done a srtpath in a testrelease) with

unix-prompt> bbrroot

Terminate ROOT with

root[0] .q
Note the '.' (a dot) in front of the 'q'.

You can suppress the startup logo with bbrroot -l. (It can be useful when you have programs running that took most of the colormap of your computer, the logo will show it immediately and you won't ponder on that very strange appearance of a 2-d histogram you might be looking at later.)

GUIs: Looking at the Output of the Beta Tutorial
The output of your Beta tutorial should be a rootfile called framework.root. (You can copy it from ~ursl/tutorial/framework.root, if you did not make it yourself.) If you prefer to look at another file, you can convert any of your hbook files to a rootfile with

h2root hbkfile.hbook

Start ROOT in the directory where your output rootfile is and then start the ROOT Browser with

root[0] TBrowser b

Open the file by selecting File->Open in the menu bar and choosing the rootfilename. You'll see something similar to (the small gifs are links to full-size html pages. Click on them in case you can't read the details.)

Look at the contents of this file by double-clicking on ROOT files and then double-clicking on the filename of your rootfile. The display will now contain several objects in the right hand side of the browser:

Display a histogram by double clicking on its name,e.g. h1d3:

Click on Options in the menu bar and select Event Status. Click on the Canvas again and point to the histogram line with the mouse pointer. You should see a display of the bin contents (and other things) in the lowest row of the canvas.

Get the Editor by clicking on Edit->Editor in the menu bar of the canvas containing the histogram:

Get a DrawPanel by moving the mouse pointer to the histogram line in any bin (the pointer should change from a cross to a pointer), clicking the right mouse button and choosing DrawPanel:

Get a FitPanel by moving the mouse pointer to the histogram line in any bin (the pointer should change from a cross to a pointer), clicking the right mouse button and choosing FitPanel:

Back in the ROOT Browser: Show the variables contained in the tree by double clicking on the tree icon (ntp1):

Display the variable by double-clicking on its name:

Try clicking the right mouse button on the ntp1 icon, you'll get a context menu. To start the GUI for working with Trees, choose StartViewer from this context menu and select OK in the window prompting you for the width (ww) and height (wh) of the GUI.

and start playing (and read the FermiLab tutorials on how to work with this ...)

CINT in brief

CINT is quite sophisticated, but not perfect. You can crash it badly. You will restart ROOT much more often than PAW. You need some experience to know when you can continue with CINT after having had an error. Some people (sometimes) would not bet on results obtained with CINT.Keep that in mind when your macro gets complicated. Nothing beats the security and reassurance of a trustworthy C++ compiler.

That said, CINT is just wonderful for all the interactive display stuff usually needed for getting plots into a form suited for presentation.

CINT commands always start with a dot `.'. The most important ones include





.x bla.C

Load and evaluate statements in the file bla.C

.L bla.C

Load macro bla.C

.! shellcommand

Execute shellcommand in shell


Get list of CINT commands

CINT completes partially typed commands (absolutely wonderful!), provides you with a list of arguments a given function expects (wonderful, again!) and completes filenames you have to enter (how could PAW have survived so long without these essential features?). Try

root[0] TFi<TAB>
root[1] TFile f(<TAB>
root[2] TFile f("fr<TAB>

where <TAB> indicates you have to hit the TAB key.

CINT allows you to scroll through your history of commands with the arrow keys (just like PAW). It keeps this history between sessions and you can scroll through previous sessions (unlike PAW). Very handy.

CINT also provides you with a search facility in its history, accessible with <Ctrl-r>. Try

root[3] <CTRL-r> TF

Customizing your setup

ROOT version: BaBar's releases are tied to specific root versions (or vice versa). This infomation is kept in SoftRelTools/ SLAC's ROOT installations are decoupled from BaBar software and are kept in


$ROOTSYS: Messing around with $ROOTSYS can be dangerous and time consuming - there are plenty of incompatibilities between different ROOT versions ... But: ROOT advances quite fast and sometimes you need the latest and greatest release (which is usually installed at SLAC within a reasonable time). In this case you need to set $ROOTSYS manually and make sure that LD_LIBRARY_PATH contains the correct shlib path. The following is an imperfect way to achieve this:

setenv ROOTSYS /afs/slac/package/cernroot/22312
addpath2 PATH $ROOTSYS/bin

Warning: Altering you LD_LIBRARY_PATH in such a simple-minded way can have side effects.
If you decide to set $ROOTSYS yourself, it's best to have it point to a specific version and not to /afs/slac/package/cernroot/newroot, which can change at short notice and is not guaranteed to work.

.rootrc: A file used for setting up system defaults. Keep it as short as possible. Information contained therein can include

Rint.Load: rootalias.C
Rint.Logon: rootlogon.C
# Path used by dynamic loader to find shared libraries and macros
Unix.*.Root.DynamicPath: .:$(ROOTSYS)/lib
# Path where to look for TrueType fonts
Unix.*.Root.UseTTFonts: true
Unix.*.Root.TTFontPath: $(ROOTSYS)/ttf/fonts

rootlogon.C: The equivalent of pawlogon.kumac. Keep it as short as possible. The following can serve as an example (note the braces):

// gROOT->Reset();
gROOT->LoadMacro("/u/ec/ursl/macros/functions.C"); // Have some (interpreted) functions predefined

gSystem.Load("/u/ec/ursl/macros/lib/"); // Load some pre-compiled functions

gROOT->SetStyle("Default"); // just keep this and the next three for information al purposes
gROOT->SetStyle("Plain"); // still the best
gROOT->ForceStyle(); // Let this determine what style to use, not with what the rootfile was saved.

gStyle->SetOptStat(111111);// Show overflow and underflow as well
gStyle->SetPalette(1); // Get better colors than in the (useless) default

HowTo get things done in ROOT:
If you are looking for examples on how to define functions:
Looking for (simple) operations for manipulating and histogramming from trees: Trees
Introductory howto's for working with histograms:Histograms
Fitting Histograms
The fine points of transforming a histogram to something presentable: Making Plots
Stuff that does not fit into any category: Miscellaneous

How to peek at Kanga files

Thanks to Marcel Kunze, Leif Wilden and Rolf Dubitzky for providing the following:

root [0] TFile f("$BFROOT/kanga/EventStore/groups/SP/0020/0000/M8.6.2bV01x6F-R8.6.2bV01/00200059/Kanga862d/P0000/KanGA/SPKanga-micro.root")

root [1]
TFile** /afs/slac/...SP/0020/0000/M8.6.2bV01x6F-R8.6.2bV01/00200059/Kanga862d/P0000/KanGA/SPKanga-micro.root
TFile* /afs/slac/...SP/0020/0000/M8.6.2bV01x6F-R8.6.2bV01/00200059/Kanga862d/P0000/KanGA/SPKanga-micro.root
KEY: TTree microDST;1 ROOT output stream

root[2] microDST->Print();
... lots of output ...
root[3] microDST->Print(); > bla.txt // to dump it into a file (note the semicolon)
cout << microDST->GetEntries() << endl; // How many entries are in this file?

Looking at the tag database:

root[5] microDST->Draw("TAG_f_R2"); // Draws R2 for all events
root[6] microDST->Draw("TAG_f_R2","TAG_b_isBBbarLike>0"); // Draws R2 for all events with tag isBBbarLike
root[7] microDST->Draw("TAG_f_R2:TAG_f_thrustMag","TAG_b_isBBbarLike>0", "box"); // a 2-d example

and the micro (nobody claims that this is sensible):

root[8] microDST->Draw("_array._calQual._energy", "_array._calQual._nCrystals>0")

Does it contain MC truth?

root[9] microDST->GetBranch("BtaStdHep")!=0