TTree extraction from a TFile

Hello. I have been having problems with this, and I hope someone can help me out.

I have a ROOT file that contains a tree called “I0” (except I “don’t know” what this name is in the program, and I am trying to get a pointer to this tree). What I am doing is this:

TFile *f1 = new TFile("file.root","READ"); TList *list = f1->GetList(); TTree *Itree = (TTree*)(list->First());

This process works fine using the CINT interpreter in ROOT (by going into ROOT and typing in each line separately in the interpreter). However, when I do this in my program and run it (using “root file.c”) the TTree “Itree” does not seem to give a pointer to a tree. What I mean by this is that I get:

when I try to access the tree like:

However, just to test things, I put a “cout” in the program to print the location of the pointer to “I0” (to compare this pointer to “Itree”, which should be the same), and everything worked after that! So, this is what I changed:

TFile *f1 = new TFile("file.root","READ"); TList *list = f1->GetList(); cout<<I0<<endl; TTree *Itree = (TTree*)(list->First());

After this, I was able to access the tree “Itree” just fine and I got no problems at all (and “I0” and “Itree” point to the same object). What is causing this? And, if something like this is required each time, how would I do it without knowing the name of the TTree (which is “I0” in this case)?

Thank you for your help.

Corey

Hi,

The list you are referring to only contains objects that have been accessed already. When Cint encounters a variable that was not declared (like IO) it checks the current directory (the file in your case) for an object of that same name. That’s why IO exists in the list all of a sudden - it was loaded because the tree was accessed.

What you should do is the following:TIter iKey(f1->GetListOfKeys()); TKey* key = 0; TTree* tree = 0; while (!tree && (key = (TKey*)iKey())) if (!strcmp(key->GetClassName(), "TTree")) file->GetObject(key->GetName(), tree);
Cheers, Axel.