RE: CopyTree & SetBranchAddress

From: Philippe Canal <>
Date: Thu, 08 Jun 2006 11:58:27 -0500


The problem is your case is that TChain::ResetBranchAddresses was not properly working (this is now fixed in the CVS repository).

To work around the issue, you can simply add

just before doing the cloning.


-----Original Message-----
From: [] On Behalf Of Vassili Maroussov
Sent: Wednesday, June 07, 2006 12:04 PM
Subject: [ROOT] CopyTree & SetBranchAddress

Dear ROOTers,

I'm wondering why CopyTree(Cuts) is affected by SetBranchAddress(...). Consider a fragment like

template <class T>
void func(TTree *tree, const char *bname, ...)

   T b_val;
   TBranch *b_ref;
   tree->SetBranchAddress(bname, &b_val);

//Here is a code, that doesn't reset the branch address...

int main()

   TFile inp_file("inpfile.root", "READ");    TTree *tree = (TTree*)inp_file.Get("tree");

   func(tree, "branchname", ...);

   TFile out_file("outfile.root", "RECREATE");    TString Cuts = "..."; //some cuts, maybe "", then all tree should be copied

   TTree *newtree = tree->CopyTree(Cuts);    newtree->SetName("newtree");

One may discover the branch "branchname" in "newtree" will be filled with 0's. Is it a bug or a feature?

More unexpected things appear if to try a similar program structure (SetBranchAddress to a local variable in a function) with TChain. In that case even ResetBranchAddresses() doesn't help. Please consider the sample program (test_CopyTree.cxx) attached. It is expected to do the following:

  1. produce 2 files with a simple tree with one branch with floats, another with integers; integers are the sequence 0,1,2,3,...,9999;
  2. add files produced to a TChain;
  3. find 95% intervals for branches by means of getting each branch in an STL vector, sorting, etc.;
  4. copy TChain into a TTree with Cuts that correspond to 95% intervals;
  5. save the TTree in a new file.

What I observed in reality (test_out.txt) is the following:

What is wrong with the sample code? I used ROOT 5.06 and gcc version 3.2.3 20030502 (Red Hat Linux 3.2.3-54), SLC3. The code was compiled with:

g++ -o test_CopyTree test_CopyTree.cxx `root-config --cflags --libs`

With the best regards,

Vassili. Received on Thu Jun 08 2006 - 19:02:21 MEST

This archive was generated by hypermail 2.2.0 : Mon Jan 01 2007 - 16:31:58 MET