ROOT: General
Interactive ROOT: Viewing the contents of TTrees and TNtuples
Interactive ROOT: Bugs under Windows
Batch ROOT: Creating programs that use the ROOT libraries
ROOT: Object Creation and Destruction: Using new & delete
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.
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.
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.
CINT has a limit of 1024 characters for its input - hence ROOT's command line is also limited to 1024 characters.
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();
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");
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.
TH1F *myHist = (TH1F*) gDirectory->Get("name of your histogram")
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.
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.
myHist->GetXaxis( )->Set(nbins, xmin, xmax)
myHist->GetXaxis( )->SetNdivisions(n)
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)
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( )
gStyle->SetOptStat( )
See this
description in the ROOT reference pages.
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.
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.
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.
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"
myTree->GetListOfBranches()->Print();
myTree->GetListOfLeaves()->Print();
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.
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.
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:
That's it - now use CMT to compile the program and run it.
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.
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.
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.
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.
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.
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
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.
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.
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
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.
Rene Brun provided some bits in a RootTalk post.
The TSystem class provides many of the standard UNIX system calls that one might want to use.