Branch with collection

Dear ROOT developers and users,

I am using ver4.04 on Linux.
I found that there is a TTree::Branch() fuction to create a branch holding a collection of an object:

Int_t Branch(TCollection *list, Int_t bufsize, Int_t splitlevel, const char *name),

and wondered if this is a more elegant way than using variable length array of simple data types.
This function is also mentioned a little in the users manual
but I could not find any example.
Could some one show me how to use and intended usage of this function?


Kame

Hi,

if at all possible you should use a TClonesArray of objects, not arrays of simple data types. This is widely documented, e.g. in the Event tutorial.

An “under-documented” method often means it’s not meant to be used if you don’t know what it’s about :slight_smile: The Branch method you’re referring to can be used to create a branch containing a collection (e.g. a TList) of objects. But this is far less powerful than using the regular TClonesArray approach, and it is replaced by more general methods like
template TBranch *Branch(const char *name, T **addobj, Int_t bufsize=32000, Int_t splitlevel=99) (which finds out by itself what kind of object is has to store).

Cheers, Axel.

Instead of having a top level class like
class Event {
A *a;
B *b;
C *d; etc
where A, B, C, etc are your objects derived from TObject,
you can built a collection, eg
TList *myevent = new TList();
myevent->Add(a);
myevent->Add(b);
myevent->Add©; etc
and create your top level branch with
tree.Branch(myevent,32000,99);
This is more dynamic than the first solution. In a large collaboration,
people can easily add a new object (or collection of objects)
in the list. There is no need to modify the function creating
the Tree and the branch.

Rene

Hi Rere and Axel

Thank you both for the repiles.

I suppose the intended usage of the Branch method is diffrerent from what I expected.
I thought it can be used for writing variable length data event by event.
I tried a code like

struct Track {
  Track() {}
  Track(double ty_, double x_) : tx(tx_), ty(ty_) {}
  Float_t tx, ty;
};

int main()
{
  TFile f("test.root", "recreate");
  
  TObjArray *trks = new TObjArray;
  trks->SetOwner(true);
  TTree tree("tree", "P152 BaseTrack");
  tree.Branch(trks);

  ifstream is("input_data_file");
  int data_length;
  while (is >> data_length) {
    trks.Clear();
    // Read track data with variable length
    Float_t tx, ty;
    for (int i = 0; i < data_length; ++i) { 
      is >> tx >> ty;
      trks->Add(new BaseTrack(tx, ty));
    }
    tree.Fill();
  }
  f.Write();
  return 0;
}

But no data member was accecssible via Tree::Draw() method.

Thank you again.

Kame

Proceed as indicated by Axel using a TClonresArray.
see $ROOTSYS/test/MainEvent.cxx

Rene