[ROOT] How to read individual leafes from one or more trees?

From: cstrato (cstrato@aon.at)
Date: Sun Sep 08 2002 - 19:26:15 MEST


Dear Rooters

Suppose, that I have filled tree(s) with one branch for:
class MyClass: public TObject {
    TString   fName;
    Int__t    fNum
    Double_t  fValue1
    Double_t  fValue2
}

The following example exports all leafes from one tree:
void Export(const char *treename, const char *outname)
{
    output << "Name" << "\t" << "Number" << "\t"
           << "Value1" << "\t" << "Value2" << endl;

    MyClass *myclass = 0;
    TTree *tree = (TTree*)gDirectory->Get(treename);
    tree->SetBranchAddress("MyBranch", &myclass);

    ofstream output(outname, ios::out);
    Int_t entries = (Int_t)(tree->GetEntries());
    for (Int_t i=0; i<entries; i++) {
       tree->GetEntry(i);
       output << myclass->GetName()   << "\t"
              << myclass->GetNumber() << "\t"
              << myclass->GetValue1() << "\t"
              << myclass->GetValue2() << endl;
    }
    output.close();
}

Now I would like to expand this function in two ways:
1, I want to export selected leafes only, e.g.
    Export("mytree", "outname", "fNum:fValue2")
2, I want to export selected leaves from all trees in a file, e.g.
    Export("*", "outname", "fValue1")

ad 1) Export selected leafes:
I could simply do: if("fNum") myclass->GetNumber(), but:
analogously to be able to read selected branches, is there
a similar way to read selected leafes?
I have tried the following but got a bus error:
    MyClass *myclass = 0;
    TTree *tree = (TTree*)gDirectory->Get(treename);
    TBranch *branch = 0;
    branch = tree->GetBranch("MyBranch");
    if (branch) branch->SetAddress(&myclass);

    TLeaf *name = branch->GetLeaf("fName");
    TLeaf *num  = branch->GetLeaf("fNum");
    Int_t entries = (Int_t)(tree->GetEntries());
    for (Int_t i=0; i<entries; i++) {
       branch->GetEntry(i);
       output << name->GetValue(i) << sep
              << num->GetValue(i)  << endl;
    }
What is wrong with this code, and how can I read selected leafes?
It would be great if there would be a function such as
TBranch::GetLeafEntry(entry, leafname)?

ad 2) Export chains of trees
I could try to loop over all trees in TFile, but the question
is if there is a simpler and more elegant way to do it, where
I could take advantage of TChain or of tree friends?

Thank you in advance for your help.

Best regards
Christian
_._._._._._._._._._._._._._._._
C.h.i.s.t.i.a.n S.t.r.a.t.o.w.a
V.i.e.n.n.a       A.u.s.t.r.i.a
_._._._._._._._._._._._._._._._



This archive was generated by hypermail 2b29 : Sat Jan 04 2003 - 23:51:08 MET