Re: [ROOT] sub-branch SetAddress/GetEvent behavior question

From: Rene Brun (Rene.Brun@cern.ch)
Date: Fri Jul 06 2001 - 10:07:33 MEST


Hi Kate,

Thanks for sending me your test macro and your file.
I have modified your macro to make it working. see below.
Note the following:
If you want to read one or a few subbranches, you have two possibilities:
 1- the most efficient
    Get a pointer to the branch(es), eg
      TBranch *brevnt = amstree->GetBranch("Eventno");
    Set the branch address
      int evnt; brevnt->SetAddress(&evnt);
    In the loop on entries, do:
      brevnt->GetEntry(i); //this will fill evnt

 2- slightly slower (example shown below)
    Disable all branches
    Activate the branch(es) you want to read
    Set the branch address
    In the loop on entries, do:
      amstree->GetEntry(i); //only active branches will be read

//============your macro slightly modified
void testread()
{
  TFile file("prv3.root");
  TTree *amstree = (TTree*)file->Get("AMSRoot");
  int nevent = amstree->GetEntries();
  printf("nevent %d\n",nevent);

  int evtno = 0;
  amstree->SetBranchStatus("*",0);
  amstree->SetBranchStatus("Eventno",1);
  amstree->SetBranchAddress("Eventno", &evtno);

   nevent = 10;
   for(int i=0;i<nevent;i++) {
      amstree->GetEntry(i);
      printf("i %d, evtno=%d\n",i,evtno);
    }
}

Rene Brun
Kate Scholberg wrote:
> 
> > You should not declare the variable as int* if it is an int.
> > In your case, if you just want to read the branch "EventNo", do:
> >  static int evtnr;
> >  TBranch *branch = amstree->GetBranch("EventNo");
> >  branch->SetAddress(&evtnr);
> >  branch->GetEntry(0); to read the first entry
> > now the variable evtnr should contain the value of EventNo for the first
> > entry.
> > It is in general a bad idea to use local variables to store the result
> > of a Tree entry. You better put your variable(s) inside a class.
> > That is what TTree::MakeClass is doing for you.
> >
> > It is likely that you did not declare int evtnr as a static variable.
> > In case you exit/enter again from/in the function where this variable is
> > declared, the variable has been destroyed meanwhile.
> 
> Yes, I know!  This is just a tiny test CINT program to check the
> action of GetEntry.  I don't believe the int gets destroyed because it
> never leaves the scope (doesn't matter if I put address of int or of
> pointer, tried both, still nothing read). ..  I am not understanding
> something, though.. what does SetBranchAddress do in the two cases?
> 
> In the main compiled code I'm trying to debug, the variable to be read is
> static.  The read still does not work (supposedly it *used* to work, though,
> which I do not understand!)
> 
> Kate.



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