Cannot access TTree leafs of a trees friend

Hi,
when I add a TTree with the same branches and leafs as friend to another tree, I cannot access the leafes of the friend.
I thought the “dot” notation would help, but this example

  file = new TFile("treefriend.root");

  t1 = (TTree*) gDirectory->Get("Tree1");
  t2 = (TTree*) gDirectory->Get("Tree2");

  t1->AddFriend(t2);
  
  t1->Scan("esumhcal:Tree1.esumhcal:Tree2.esumhcal");

results in this output:

************************************************
*    Row   *  esumhcal * Tree1.esu * Tree2.esu *
************************************************
*        0 * 2.7286152 *   1510656 *   1411968 *
*        1 * 0.6848975 *   1411968 *   2589504 *
*        2 * 8.0482616 *   1411968 *   1412160 *
*        3 * 0.9172403 *   2589504 *   2589504 *
*        4 * 1.3630818 *   1313472 *   1411776 *
*        5 * 7.5417842 *   1411776 *   1313856 *
*        6 * 4.0797786 *   1510272 *   1510656 *
*        7 * 1.6302832 *   1017792 *   1313856 *
************************************************

(a demo root file is attached to this post)

What is the correct way to access the leafs of the friend tree?
Cheers, J.
treefriend.root (23.3 KB)

After some further investigation, I was able to figure out that the reason for my problem seems to be the TTree. By using TTree::CloneTree, I was able to create a tree with one Branch (attached to this post), that illustrates the problem:

root [2] Tree1->Print()
******************************************************************************
*Tree    :Tree1     : bigtree                                                *
*Entries :       10 : Total =            1103 bytes  File  Size =        465 *
*        :          : Tree compression factor =   1.00                       *
******************************************************************************
*Br    0 :nhitsahc  : nhitsahc/I:                                            *
*Entries :       10 : Total  Size=       1991 bytes  One basket in memory    *
*Baskets :        0 : Basket Size=      32000 bytes  Compression=   1.00     *
*............................................................................*
root [3] Tree1->Scan("nhitsahc:Tree1.xx:Tree1.nhitsahc")
************************************************
*    Row   *  nhitsahc *  Tree1.xx * Tree1.nhi *
************************************************
*        0 *        42 *  18025408 *  18025408 *
*        1 *        21 *  18713344 *  18713344 *
*        2 *        87 *  18713344 *  18713344 *
*        3 *        27 *  19010560 *  19010560 *
*        4 *        33 *  18811456 *  18811456 *
*        5 *        74 *  18811648 *  18811648 *
*        6 *        67 *  18222016 *  18222016 *
*        7 *        27 *  19204096 *  19204096 *
*        8 *        47 *  18222400 *  18222400 *
*        9 *        22 *  18615040 *  18615040 *
************************************************

What I would expect would be an error message like:

Error in <TTreeFormula::Compile>: Bad numerical expression : "Tree1.xx"
Is there a switch that causes this behavior of the TTree, or is this a bug in ROOT?
Cheers, J.
comp.root (4.65 KB)

Hi,

Thanks for this very concise example.

There is 2 issues. Your file contains one leaf with no name (zero length name). See TTree1->GetListOfLeaves()->ls();

In addition there was a bug in the ROOT code that meant that Tree1.BadName was matching this no-name leaf. This bug has been fixed in the CVS repository.

Cheers,
Philippe

Hi,
thank you for your answer. In the meantime, I had a look in the code which created the tree. With help of your answer, i was able to spot the line causing the trouble.

The malicious branch was booked in this line:

    hostTree->Branch("nhitshcal"  ,&_nHits      ,"nhitshcal/I:"  );

Note the additional colon after the leaf name. Maybe ROOT should give an error or a warning at runtime to prevent errors like this.

Cheers, J.

Good point. We will add a protection in cases like this one.

Rene

A warning message has been added to the svn trunk and the missing name are replace by __noname1, __noname2, etc (where the number is the index of the leaf in the list of leaf of the branch).

Cheers,
Philippe