TChain, clones, and adding new branches

Hi,

We are trying to read in a TChain, make a clone of it so that all of its branches are copied, and then add new branches to the cloned tree. Every branch has its branch address explicitly set, so that the variables can be accessed or in the case of new branches, calculated. From there, for each event, we do TChain::GetEntry and also update the variables for the new branches separatel and do a TTree::Fill, etc. However, it seems that when moving to the second TTree in the TChain, I must be losing the branch addresses, since the next call to TChain::GetEntry fails, pointing to the TBufferFile::ReadFloat call. All of the branches are “simple”, at most
they contain a three element array of floats, otherwise, they are floats, ints, etc.

Here is a snippet of what we do at set up:

TChain *ch = new TChain(treename.c_str()); for each input file ch->Add(filename); Long64_t numEntries = ch->GetEntries(); TTree *clone = ch->CloneTree(0);

further down we actually update the branch addresses, and add new branches to the clone tree:

for each branch:

TBranch *br = clone->GetBranch(bname.c_str()); if (!br) clone->Branch(branchName.c_str(),const_cast<void*>(pval)); else ch->SetBranchAddress(branchName.c_str(),const_cast<void*>(pval));

Now, I realize setting the branch addresses of the TChain after cloning may present a problem… I’d prefer to set the branch addresses beforehand, but as it stands I cannot easily do that. Perhaps I need to explicitly set the branch address for the cloned tree as well?

Then for each event, I make a call to
TChain::GetEntry(ievent);
to load the next event, and presumably update the variables referenced in the cloned tree, then calculate the values for the new branches, do a TTree::Fill and so on to the end of the TChain.

Some specific questions:

Is there anything wrong with add new branches to the cloned TTree?

Calling TChain::SetBranchAddress after TChain::CloneTree is not a particularly good idea? Will those branch addresses propagate to the clones or not?

I was hoping that using TChain::SetBranchAddress would keep the branch addresses set for all TTrees in the TChain - is that an incorrect assumption?

Take care,
Heather

Nope, nothing wrong with it :slight_smile:

[quote]Calling TChain::SetBranchAddress after TChain::CloneTree is not a particularly good idea? Will those branch addresses propagate to the clones or not? [/quote]Yes, it is supposed to propagate along to the clone.

[quote]
I was hoping that using TChain::SetBranchAddress would keep the branch addresses set for all TTrees in the TChain - is that an incorrect assumption?[/quote]Yes.

However you wrote:

clone->Branch(branchName.c_str(),const_cast<void*>(pval));which may or may not be your actual code :slight_smile:. As it is, it does not tell the TTree what is the type of the data to be acquired!

Cheers,
Philippe.

Hi Philippe,

[quote]I was hoping that using TChain::SetBranchAddress would keep the branch addresses set for all TTrees in the TChain - is that an incorrect assumption?
Yes.[/quote]

Just to be clear - I need to reset the branch addresses of the cloned tree each time we move/load a new tree in the chain?

[quote]However you wrote:
Code:
clone->Branch(branchName.c_str(),const_cast<void*>(pval));
which may or may not be your actual code Smile. As it is, it does not tell the TTree what is the type of the data to be acquired!
[/quote]

Right - I didn’t copy that, I was just randomly typing - it’s correct in the code. Thanks :slight_smile:

Take care,
Heather

[quote]Just to be clear - I need to reset the branch addresses of the cloned tree each time we move/load a new tree in the chain? [/quote]No, you should not need to.

Cheers,
Philippe