Table of Contents

ROOT:  General

  1. Where can I get the ROOT User's Guide?
  2. Machine Independent Types - What are Int_t, Char_t, etc?

Interactive ROOT: Setup

  1. How do I set ROOT environment variables?
  2. How do I setup ROOT to allow viewing memory allocation?

Interactive ROOT:  General

  1. What is the length limit on the command line in ROOT?
  2. What happens if I insert (or do not insert) a semicolon (;) at the end of an interactive command?
Interactive ROOT:  Plotting & Histogramming
  1. How can I plot the contents of a tree?
  2. How can I apply cuts when using TTree:Draw ?
  3. How do I access a histogram that is currently loaded in my ROOT session?
  4. How do I force the y-axis to a particular range?
  5. How do I force the x-axis to a particular range?
  6. How do I modify the binning in a histogram?
  7. How do I modify the number of tick marks along an axis?
  8. How do to divide a canvas and fill it?
  9. How do I set an axis to use log scale?
  10. How do I insert a date on a canvas?
  11. How do I setup the statistics printed on a histogram?
  12. Displaying Fit Parmeters in a histogram statistics box.
  13. How do I get the integral of a Histogram?

Interactive ROOT: Viewing the contents of TTrees and TNtuples

  1. How do I dump the contents of a TTree to the screen?
  2. How do I start up the TreeViewer?
  3. How do I obtain a list of Branch or Leaf names?

Interactive ROOT:  Bugs under Windows

  1. Why does ROOT 3.00.06 crash so often under Windows?
  2. The results of TTree::Draw are inconsistent  - what is going on???

Batch ROOT:  Creating programs that use the ROOT libraries

  1. Compiling a program that uses ROOT
  2. Using > ROOT 2.22, I cannot successfully retrieve a TTree stored in a root file - what's up?

ROOT TTrees and TNtuples

  1. How do I create a new ntuple with more than 14 entries?
  2. How do I link multiple ROOT files together?
  3. How do I merge multiple TTrees (or TNtuples) into one file?
  4. How do I create an event list?
  5. How do I process a TTree using a user defined function for cutting events?
  6. How do I create a TTree which contains a C structure?

ROOT: Analysis

  1. How do I use Minuit with ROOT?

ROOT: Object Creation and Destruction:  Using new & delete 

  1. Deleting TTrees

ROOT: Advanced 

  1. How does one read the contents of any ROOT file ?
  2. Do you have some tips on user-defined ROOT class organization ?
  3. How can I make system calls?
  4. How do I write my own ROOT class?
  5. How do I check the version of a ROOT class?

ROOT:  General

Where can I get the ROOT User's Guide?

It is available from the ROOT home page.

Back to Top

Machine Independent Types - What are Int_t, Char_t, etc?

Since different machines may have different lengths for the same type - ROOT provides a set of predefined types that are guaranteed to be machine independent.  For a full list of the available types see the Rtypes.h file in your $ROOTSYS/include directory.

Back to Top

Interactive Root: Setup

How do I setup ROOT environment variables ?

A file called ".rootrc" is used to setup your environment variables.  The system rootrc file is called system.rootrc (it is located under $ROOTSYS/etc).  

The RootAnalysis package, now contains a GLAST specific .rootrc file which loads our libraries and macros.  The options described in your home copy of ".rootrc" will be merged with the global options described in $(ROOTSYS)/system.rootrc.  Precedence is given to your local settings.

Back to Top

How do I setup ROOT to allow viewing of memory allocation ?

At time it is useful to be able to view the heap.  This is especially useful to check to see if your user-defined ROOT classes behave properly when creating and deleting.  To be able to view the heap, one must modify their .rootrc file - used by ROOT to setup the ROOT environment.  Specifically, you want to make sure that two items are set to 1 (one):
Root.MemStat and Root.ObjectStat  

# Activate memory statistics (size and cnt is used to trap allocation of
# blocks of a certain size after cnt times)
Root.MemStat: 1
Root.MemStat.size: -1
Root.MemStat.cnt: -1
Root.ObjectStat: 1

Then while in your ROOT interactive session, type:
gObjectTable->Print( )
This will display the contents of the memory table.

Back to Top

Interactive ROOT:  General

What is the length limit on the length of the command line in ROOT ?

CINT has a limit of 1024 characters for its input - hence ROOT's command line is also limited to 1024 characters.

Back to Top

What happens if I insert (or do not insert) a semicolon (;) at the end of an interactive command line?

When using ROOT interactively, some of the common rules for C++ have been modified for this interactive environment.  Semicolons (;) are not required at the end of command lines when using ROOT interactively.  If you do not include the semicolon, then the return value of the command will be printed to the command window.  If you do include the semicolon (;), then the return value is not printed to the command window.  Here is an example:

root [3] TTree *t = (TTree*)f.Get("PDR/t1")
root [4] t->GetEntries()
(const Stat_t)3.44180000000000000e+004
root [5] t->GetEntries();

Back to Top

Interactive Root:  Plotting & Histogramming

How can I plot the contents of a tree?

One can use the "Draw" command.  "Draw" will work for both a "flat" ntuple TTree or for a more complex TTree structure.  The only difference is that accessing a specific piece of data within a complex TTree will require more information to be provided to the Draw command.

Here is an example:
TFile f("myRootFile.root");
TTree *t = (TTree*)f.Get("T");
t->Draw("Event_ID");

Back to Top

How can I apply cuts when using TTree::Draw ?

You can apply cuts when executing a TTree::Draw command.  The cuts would be the second argument:
t->Draw("Event_ID", "Event_ID < 12");

There are some restrictions when using TTree::Draw.  Only a subset of all possible C++ operators and math functions are available for use in the cut string.  Here is a table of the accepted routines and operators.  Please note that TMath::Min is not one of the accepted operators.

Back to Top

How do I access a histogram that is currently loaded in my ROOT session ?

TH1F *myHist = (TH1F*) gDirectory->Get("name of your histogram")

Back to Top

How do I force the y-axis to a particular range ?

Use TH1F::SetMinimum(minValue) and TH1F::SetMaximum(maxValue)

myHist->SetMaximum(1000);

You can do this either before or after you fill the histogram.  If done after, the histogram is filled and drawn to the screen, be sure to refresh the window to see the results of the new settings.

Back to Top

How do I force the x-axis to a particular range ?

This would seem to be an easy thing to do!!  There are 2 cases:

1.) You have already filled a histogram

To zoom an already filled histogram do:
h->GetXaxis()->SetRange(binmin,binmax);

where binmin,binmax are the lower and upper bin number.
You can find the correspondance between axis values and bin numbers via
TAxis::FindBin

2.) You have NOT already filled a histogram

h->GetXaxis()->SetLimits(xmin,xmax);

Please see this exchange on RootTalk.

Back to Top

How do I modify the binning of a histogram?

myHist->GetXaxis( )->Set(nbins, xmin, xmax)

Back to Top

How do I modify the number of tick marks along an axis ?

myHist->GetXaxis( )->SetNdivisions(n)

Back to Top

How do I display the date on a canvas?

gStyle->SetOptDate(1) or (2)

Back to Top

How do I divide a canvas and fill it ?

First, divide the canvas:

Interactively:

    Right click on the canvas
    Choose Properties
    Choose Divide, and fill in the nx and ny attributes.

OR from the command line:

    c1.Divide(nx, ny)

To Fill, you must first specify which pad you wish to access:  

    With the mouse: put the cursor over the pad you want to access, and click the middle (or wheel) button

    From Command Line:   padN->cd( );  (pad3->cd( ) for example)

Back to Top

How do I set a plot to use log scale?

There are 2 ways, either by clicking on the plot itself or from the Root command line:
1.) Right click on the plot, near the axis of interest and choose 'setlogx' or 'setlogy'

2.) From the command line, access the attributes of the canvas:
c1->SetLogy( )   or  c1->SetLogx( )

Back to Top

How do I setup the statistics printed on a histogram ?

gStyle->SetOptStat( )
See this description in the ROOT reference pages. 

Back to Top

How do I display fit parameters in a histogram statistics box?

See gStyle->SetOptFit( )
Here is a description in the ROOT reference pages.

Please note, that if you have fit multiple functions to a histogram..it is not immediately obvious how to get the fit parameters from a specific fit displayed.  It seems that one method that may work is to do:

gStyle->SetOptStat(0);
gStyle->SetOptFit(0);
// Do you fitting, plotting etc..
// When you fit the function whose parameters you wish to display, the do:
gStyle->SetOptFit(0101);  //or whatever you wish

This seems to work.

Back to Top

How do I get the integral of a histogram?

Use the Integral( ) method.  And note the following from the Root 3.00.06 release notes:

Add an optional argument to the Integral functions. If the argument is not specified, the function is backward compatible and returns the sum of the bin contents. If the argument option="width", the integral computed is the sum of the bin contents times the bin width in x,y or z. The new argument is important to compute correctly the integral of histograms with variable bins size.

Back to Top

Interactive ROOT: Viewing the contents of TTrees and TNtuples

How do I dump the contents of a TTree to the screen?

Open your root file and retrieve the TTree.
Start up a TBrowser window.
Right click on the name of the TTree in the TBrowser window.
Choose "Scan"
A dialog box will appear that will allow you to choose how many entries to
scan (and the staring event number), in the selection box you can provide a
simple cuts, and in the varexp box..you can specify the names of certain
columns that you wish to view. 

The output will pop up in the Root command window.

Back to Top

How do I start up the TreeViewer?

Open your root file and retrieve the TTree.
Start up a TBrowser window.
Right click on the name of the TTree in the TBrowser window.
Choose "StartViewer"

Back to Top

How do I obtain a list of Branch or Leaf names?

myTree->GetListOfBranches()->Print();
myTree->GetListOfLeaves()->Print();

Back to Top

Interactive ROOT:  Bugs under Windows

Why does ROOT 3.00.06 crash so often under Windows?

There are known problems with the TBrowser in ROOT 3.00.06.  In fact, just moving the TBrowser under Windows can cause ROOT to crash.  This was a simple bug in the program and has already been fixed in the latest version of ROOT, 3.01/06.

Back to Top

TTree::Draw produces inconsistent results - what is going on??

This was a bug in ROOT <= 3.00.06, if there are more than 16 operands provided as a cut in a TTree::Draw.  This has been fixed as of ROOT 3.01/06.

See this post to RootTalk.

Back to Top

Batch Root:  creating programs that use the Root libraries

How do I compile a program that uses ROOT ?

The ROOT distribution comes with a set of programs and a Makefile in the test directory.  This Makefile handles all of the supported ROOT platforms.  A simplified version of this Makefile and an associated C++ program, testMain.cxx, are available.  

The program simply sets up ROOT, creates a TFile which stores a 1-D histogram and a simple TNtuple.  You can try this example out by downloading the Makefile and the testMain.cxx file onto your machine.  Update the ARCH variable at the top of Makefile (it is currently setup for linux) and then type "make".  A program called testMain will be produced.  Run the program by typing:  "testMain".  A new ROOT file will be generated called "myTest.root".

An alternative method of compiling a program is to use CMT.
First you will use CMT to create a new package:

  1. cmt create testMain v1
  2. Next copy the testMain.cxx into the new testMain/v1/src directory
  3. Finally update the requirements file in the testMain/v1/cmt directory to look like this:
    package testMain
    version v1
    use ROOT 

    macro_append ROOT_linkopts " ${ROOT_PATH}/lib/libHist.so " \
    VisualC " ${ROOT_PATH}/lib/libHist.lib "

    application testMain testMain.cxx

That's it - now use CMT to compile the program and run it.

Using > ROOT 2.22, I cannot successfully retrieve a TTree stored in a root file - what's up ?

This is seemingly a Windows problem, in that the Tree library is not being loaded properly.  This is only a problem when creating a stand-alone program that uses the Root TTree library.  To fix this problem, include the "TSystem.h" Root include file, and then at the beginning of your program, explicitly load the Root Tree library, using the call:  gSystem->Load("libTree.dll");

For more information see this entry in the RootTalk Digest.

Back to Top

ROOT Trees and NTuples

How do I create a new ntuple with more than 14 entries ?

An ntuple (TNtuple) in ROOT is just a simple TTree, where each branch contains floating point (Float_t) data.  At one time, there was an upper limit on the number of entries that were allowed within a TNtuple, as of Root 2.22/10 this is no longer the case.  Don't believe it?  Try:

TNtuple* myNtuple = new TNtuple("name", "title", "a:b:c:d:e:f:g:h:i:j:k:l:m:n:o:p", 64000);
Float_t filler[16];
filler[15] = 30.5;
myNtuple->Fill(filler);
myNTuple->Draw("p")

The trick is that there are now two ways to fill a TNtuple, call TNtuple::Fill(Float_t *x), where the array x can contain as many elements as you desire.

Back to Top

How do I link multiple ROOT files together?

Use TChain!

TChain will allow you to chain Root files - with the same internal structure - so that you may treat them as one contiguous Root file.  The RootAnalysis package contains a macro called chainTrees.cx which can chain an ROOT files together.  See this link, for an example of how to use TChain. 

Note: Once you have linked multiple Root files together, you will need to use both run number and event ids to uniquely identify events.

Back to Top

How do I merge multiple TTree or TNtuples into one new file ?

Once you have a TChain setup - see previous answer concerning linking multiple files together - you can use TChain::Merge to create a new ROOT file containing the contents of all the chained TTrees.  Suppose chain is a TChain variable that has already been setup:
chain->Merge("myNewOutputFile.root");
This will create a new output file, containing the contents of all the chained TTrees.

Note:  In ROOT 3.00.06 and 3.01.06, TChain::Merge will not allow you to store your chained TTrees into a subdirectory of the output file.  This will be fixed in 3.02.

Back to Top

How do I create an event list?

The RootAnalysis package contain an example macro, createEventList.cxx which demonstrates how to setup a TEventList.  An event list is handy to restrict event processing to those events that satisfy some particular cut.  

Please also see the description from the GRUG Feature of the Week.

Once an event list is setup - you may want to restrict processing to a specific TEventList.

Back to Top

How do I process a TTree using a user-defined function for cutting events?

Use TTree::MakeSelector and TTree::Process

TFile f("myFile.root");
TTree *t = (TTree*)f.Get("PDR/t1");
t->MakeSelector("mySelector");

// This creates 2 new files mySelector.C and mySelector.h
// Then you fill MySelector.C's ProcessCuts and ProcessFill routines
t->Process("mySelector.C++"); 
// to run your tree through the selector and create the histograms

Back to Top

How do I create a TTree which contains a C structure?

ROOT does provide the facility to store C structure in ROOT files - though this is not recommended in general - it would be best to use classes instead.  However, if you find yourself in need of storing C structures there are some tips to keep in mind.

The ROOT User's Guide provides a full example of setting up a TTree to store a C structure.

Back to Top


ROOT Analysis

How do I use Minuit with ROOT ?

ROOT provides a C++ translation of the original Fortran Minuit library.  Regis Terrier provided a nice introduction to using Minuit - it was a ROOT Feature of the week.

Back to Top

ROOT Creation and Destruction:  Using new & delete

Deleting TTrees

When you have retrieved a TTree object from a ROOT file - you may be tempted to delete the TTree object.  Such as:
TFile *f = new TFile("myRootFile.root");
TTree *t = (TTree*)f->Get("T");
// Read the contents of the file and now we're ready to end
delete t;
delete f;

This code may actually cause a segmentation fault.  Why?  The TTree object will be deleted when you delete the TFile object.  See the following explanation from Rene Brun:

> TFile *file = new TFile(...); // open a file
> TTree *tree = new TTree(...); // create a tree
> TBranch *branch=tree->Branch(...); // create a branch
> [...]
> file->Write();
> delete file; // close the file

> Do I have to call also:
> delete tree;
> delete branch;
> ----


No, you do not have to delete tree and branch.
The branch is automatically deleted by the Tree destructor.
The Tree is automatically deleted by the TFile destructor unless you have
called tree.SetDirectory(0);

Rene Brun

Back to Top


ROOT Advanced

How do I read the contents of any ROOT file ?

Each ROOT file stores a list of TKeys that specify the contents of the file.  One can then search that list of TKeys to determine the contents of the file.  Each TKey includes the object's name and it's "type" or class.

Here is an example of searching for a TTree named "t1" in a ROOT file.

Back to Top

Are there any tips for ROOT class organization?

Rene Brun provided some bits in a RootTalk post.

Back to Top

How can I make system calls?

The TSystem class provides many of the standard UNIX system calls that one might want to use.

Back to Top

How do I write a ROOT class?

Please see this page which discusses this topic.

Back to Top

How do I check the version of a ROOT class?

Version numbers are maintained for each ROOT class.  If you desire to check the version of a particular class that has been loaded from a shared library, do the following:
McPositionHit::Class( )->GetStreamerInfo( )->GetClassVersion( )

One may also desire to check the version of a particular class stored within a ROOT file.  This can be done, first open the file - without explicitly loading the associated ROOT library containing the class definitions:
TFile f("my.root")
You will receive warning messages that the ROOT classes are not found.  Now you can check the version via a call such as:
gROOT->GetClass("McPositionHit")->GetStreamerInfo( )->GetClassVersion( )

Back to Top


The Root Groupies
Copyright 2000. All rights reserved.
Revised: July 08, 2002 .