Re: [ROOT] Get TTree entries

From: Rene Brun (Rene.Brun@cern.ch)
Date: Wed Aug 29 2001 - 18:37:35 MEST


Hi Tommaso,

It looks like you are trying to program a la Fortran in C++.
You should use a class to hold all your variables.
Instead of defining one branche per variable, setting the address for
each variable, you can do all that in a much simpler way by simply
creating one top level branch for your object.

See an example in $ROOTSYS/test/Event.h,cxx
and $ROOTSYS/tutorials/tree4.C

On an existing Tree, you can also use TTree::MakeClass to automatically
generate the class skeleton for your Tree.
See the Users Guide.

Rene Brun

Tommaso Chiarusi wrote:
> 
> Hi rooters,
> My problem concerns retriving the values from several TTrees, all with
> the same structure.
> 
> So lets assume I have data.root that is a trees composed file.
> Inside data.root I have the following TTrees
> 
> t1,t2,t3, ... etc.
> 
> Every TTree is composed by N branches, each representing one variable.
> 
> In order to retrive the first TTree's data I do the following:
> 
> // ------------------------------STEP i
> 
>  TTree *TREE1 = (TTree*)f->Get("t1"); // initialising the TREE
>  Int_t entries = (Int_t)TREE1->GetEntries();
> 
> // ------------------------------STEP ii
> 
> // I declare the components of TREE1
> 
>  Double_t var1=0;
>  Double_t var2=0;
>  Double_t var3=0;
>        .
>        .
>        .
> // and so on (NOTE THAT THIS could be a long set of declaration!!!!!)!
> 
> // ------------------------------STEP iii
> //then I associate each branch of TREE1 to each previously
> //declared variable:
> 
>  TREE1->SetBranchAddress("var1",&var1);
>  TREE1->SetBranchAddress("var2",&var2);
> 
> //.... and so on for all branches/variables (NOTE AGAIN that this could
> be a long list of assignments!).
> 
> // ------------------------------STEP iv
> //Then in order to do my analysis I loop over all the entries:
> 
>  for (Int_t ki=0;ki<entries;ki++)
>    {
>      TREE1->GetEntry(ki);
> 
>         do my stuff!
>    }
> 
> NOW!!
> passing to the next TTrees t2,t3, ...  etc I managed to make the program
> working good  with defining new TREE2 TREE3... etc only!
> 
> This means that I had to rewrite all the times the assignements between
> branches and variables (STEP iii) thus making longer and longer and
> heavier and heavier the code!
> 
> Are there other alternative methods (for example resetting the content
> of the TREE1 with the command
> TREE1->Reset();)
> and then repeating only the STEP i and STEP iv ?
> I've already tried this strategy, but it doesn't seem to work!
> 
> Meanwhile, thanks  to all
> Tommaso



This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:50:58 MET