Returned value of SetBranchAddress()


I’m reading a TTree from a file. I’m using SetBranchAddress() and the returned value is 0:

TTree *T = (TTree*)_file0->Get("mytree");
double myvar;
T->SetBranchAddress("VAR",&myvar); // returns 0

Now, if I read the same tree with TChain methods, SetBranchAddress() returns 5:

TChain *C = new TChain("mytree");
double myvar;
C->SetBranchAddress("VAR",&myvar); // returns 5

Why is the returned value different? How can I make SetBranchAddress() return 0 when there is a perfect match?

Thank you

0 Means SetBranchAddress succeeded. See: ROOT: tree/tree/src/TTree.cxx Source File

Hi Adrian,

Because TChain doesn’t actually go and open the underlying TTree when you call SetBranchAddress, it defers that operation to when you actually ask for an entry number (e.g. to avoid loading tree number 0 only to skip it and read the first data from tree number 10).

You can force a load of the underlying TTree and then call SetBranchAddress on the TChain. At that point since there already is a loaded TTree the SetBranchAddress call should behave the same as if you called it on the actual TTree (@pcanal can correct me if I’m wrong):

C->LoadTree(0); // pre-load the first tree
C->SetBranchAddress(...) // returns 0


This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.