Error adding branch

If I want to store an array (c array) in a TTree, I need to store the dimension in an other branch. This is ok, and I can use variable length array, the problem is when the array length is stored in a friend TTree. For example:

#include "TROOT.h"
#include "TTree.h"
#include "TFile.h"

int main(int argc,char **argv)
{

    // Fill a branch of a tree with "random" integers

    TFile tree_file("tree_file.root","recreate");
    TTree *tree = new TTree("tree","main tree");
    int n = 10;
    const Int_t n_entries = 100;
    tree->Branch("n",&n,"n/I");

    for (int i=0;i<n_entries;++i) {
	n = ( 7 * n + 6 ) % 101;
	tree->Fill();
    }
    
    // and write on a file

    tree_file.cd();
    tree_file.Write();

    // Create a second tree
    
    TFile second_file("friend_file.root","recreate");
    TTree *second_tree = new TTree("second_tree","second tree");
    
    // the fist is now a friend of the second
    second_tree->AddFriend(tree);

    Float_t array[100];
    Float_t array2[100];

    Int_t j;    

    second_tree->Branch("j",&j,"j/I");    
    // this is ok:
    second_tree->Branch("Ns",array,"Ns[j]/F");
    //this is not ok. Why? n is a branch of a friend!
    second_tree->Branch("N2s",array2,"N2s[n]/F");   
    // output: Error in <TBranch::TBranch>: Illegal leaf: N2s/N2s[n]/F

    return 0;
}

I don’t know if is it a bug, or maybe I’m asking to much from ROOT.

Hi,

You must have the ‘size’ of an array stored in the same TTree (otherwise the TTree without the ‘size’ branch will not be readable without the ‘main’ tree).
I recommend you simply duplicate the information in both the main and friend tree.

Cheers,
Philippe