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?
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 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).
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.