Newbie: reading a tree from a root file

I am trying to load a tree from a root file. I am using Root v4.0.4.

Following the User’s guide (v4.0.8) in Section 12 “A Simple TTree” I have used staff.C from the tutorials which creates a tree and writes it a file staff.root. This works.

Now I want to open and examine the tree from this file.

The Users Guide says to do this:

root [] TFile f(“staff.root”)
root [] tree->Show(10)

This generates the error:

Error: Symbol tree is not defined in current scope FILE:(tmpfile)
LINE:1 (…)

which is not surprising to me since I can’t see that tree was ever initialized, so it would seem that the User’s Guide is in error.

So I look in some the User’s Guide and try to figure how to initialize the tree pointer.

Extrapolating from an example from section 12 “Reading the Tree”, I try the following:

root [] TTree tree = (TTree)f->Get(“staff”)

Note that staff.root has a tree “staff” in it.

This command generates no errors, and generates a non-zero pointer. However, if one tries to use the pointer, such as with tree->Show(10) you get the error:

Error: illegal pointer to class object tree 0x0 97 FILE:(tmpfile) LINE:1
*** Interpreter error recovered ***

So, what command do I need to use to get the pointer to a tree in a root file?

Hi Diehl (Tom?),

[quote=“diehl”]The Users Guide says to do this:
root [] TFile f(“staff.root”)
root [] tree->Show(10)
This generates the error:

Error: Symbol tree is not defined in current scope FILE:(tmpfile)
LINE:1 (…)
[…]
root [] TTree tree = (TTree)f->Get(“staff”)

Note that staff.root has a tree “staff” in it.
[/quote]
As “.x staff.C” or “gFile->Print()” shows you, the tree is called “T”:

[code]root [3] gFile->Print()
TFile: name=staff.root, title=, option=READ


*Tree :T : staff data from ascii file *
*Entries : 3354 : Total = 245319 bytes File Size = 59884 *

  •    :          : Tree compression factor =   2.90                       *
    

******************************************************************************[/code]
Using c++ you could write

TTree* tree=(TTree*)f.Get("T"); tree->Show(10);
There’s a nice Cint extension, automatically declaring vars for all objects in the current directory (more precisely: if a var is unknown, it will look in the current directory trying to find an object with the same name). This is what the Users Guide was referring to - only it used the wrong tree name (I’ll send a note about that to rootdev). So this works, too:

Note that this second option will not work when compiling code - so I’d use it interactively but never in a script.
Axel.

Thank you for your reply. I also note that the staff.C tutorial is changed from what is given in the root manual which was part of my confusion. I had expected that the staff.root would contain a single tree called “staff”, when it in fact contains a tree called “T” with 3 branches.