I can read TChain with 1 TTree, but not with the 2!

Hi,

I try to read (in a C++ program) few integer numbers from a TTree. The TTree is complex and I do not have dictionary for it. The numbers which I want to access can be easely seen with the TTree::Scan() call. So all I want is to read them in a program. I achived this with the following logic:

for( loop over events in tree )
{
        tree.GetEntry(entry);
        TObjArray *leaves = tree.GetListOfLeaves();
        TObject  *o_run   = leaves->FindObject("run");
        TLeaf *run   = dynamic_cast<TLeaf*>(o_run);
        int the_run = int(run  ->GetValue());
}

It works perfectly if my ‘ttree’ is a TChain with 1 file inside, but it crashes if I have 2 files!!
What do I miss? Or is there a better solution?

Thank you very much in advance,
Alexander Zvyagin.

Hi,

Leaves and Branches are part of the TTree and are deleted whenever the chain pass to the new file (i.e. your ‘run’ pointer becomes invalid as soon as the chain start reading the 2nd file). In your case, you might be able to simply use:

int the_run; tree.SetBranchAddress("run",&the_run); for( loop over events in tree ) { tree.GetEntry(entry); }

Cheers,
Philippe.

PS. Then again it looks like you are retrieve the leaf pointer for each loop, so your problem might be different.

Hi,

of course I tried SetBranchAddress(), but it did not work (I believe because “run” is not a branch!)

And yes, I take the list of leaves for every event to be sure that it is refreshed when a new TTree will be loaded…

But anyway thanks for looking into the problem,
Alexander.

[quote=“pcanal”]Hi,

Leaves and Branches are part of the TTree and are deleted whenever the chain pass to the new file (i.e. your ‘run’ pointer becomes invalid as soon as the chain start reading the 2nd file). In your case, you might be able to simply use:

int the_run; tree.SetBranchAddress("run",&the_run); for( loop over events in tree ) { tree.GetEntry(entry); }

Cheers,
Philippe.

PS. Then again it looks like you are retrieve the leaf pointer for each loop, so your problem might be different.[/quote]

[quote]And yes, I take the list of leaves for every event to be sure that it is refreshed when a new TTree will be loaded… [/quote]Then I don’t know what the problem is. If you provide a running script, I will look into it further.

Cheers,
Philippe

Hello Philippe,

thank you for looking into the problem. I experimented with my code (without success), and sometimes I can read also 2 files in a chain. Anyway I decided to write a short script which demontrates my original problem (with SetBranchAddress). If you copy the two attached files (the script itself and the root data file) into a directory and then run, you will see:

root -b -q 1.root r.C
.....
The TTree::Scan() gives:
*        0 *     24966 *
*        1 *     32550 *
.....
And my read is:
For the tree entry 0 event number is 0
For the tree entry 1 event number is 0

[quote=“pcanal”][quote]And yes, I take the list of leaves for every event to be sure that it is refreshed when a new TTree will be loaded… [/quote]Then I don’t know what the problem is. If you provide a running script, I will look into it further.

Cheers,
Philippe[/quote]
1.root (13.6 KB)
r.C (657 Bytes)

Hi,

In the case of the file you send me, the branch event_spill is actually a sub-branch part of the object branch ‘Event’.

In order to be able to set the sub-branch of an object branch independently you must call tree->SetMakeClass(1);If this is a chain you need to make sure to call this on the chain.

Cheers,
Philippe.

Dear Philippe,

tree->SetMakeClass(1);
Thanks, it helped!!

Alexander.