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

From: Rene Brun (Rene.Brun@cern.ch)
Date: Wed Jul 04 2001 - 09:09:21 MEST


Hi Kate,

If you have a top level branch corresponding to Event* event,
you need to set the top level branch address only. The address of all the sub
branches will be automatically set, eg:

  Event *event = 0; //important to set teh pointer to 0;
  myTree->SetBranchAddress("event",&event);

  myTree->GetEntry(0); This will fill your Event object. You can now use
                       normal C++ to navigate in your Event object.
This procedure is the same if you use split mode or not.

For more information, see the chapters on Trees in the Users Guide.

Rene Brun

Kate Scholberg wrote:
> 
> Thanks for the reply.
> 
> >
> > Are you sure that instead of:
> >
> > int* evptr = 0;
> > amstree->SetBranchAddress("Eventno",&evptr)
> >
> > you should may be have :
> >
> >
> > EventNtuple02* event = 0;
> > amstree->SetBranchAddress("event02",&event);
> >
> > For sure int* must be wrong.
> >
> 
> Yes, the latter works.  But in the former, I am trying to access a
> sub-branch directly.  (I'm actually working with some code that
> supposedly used to work, and which contains a construction similar to
> the former).
> 
> The tree has split branches.
> 
> The Root web page says about the split option:
> 
> "If you run the program above and set split = 1, then the statement:
> tree->Branch("event", "Event", &event, bsize,split);
> will generate several branches, one for each data member of the Event class."
> 
> I've been assuming that this means that each data member of the
> EventNtuple02 class, e.g.  Eventno, becomes a branch unto itself, and
> behaves like any other branch (and so should have its address set with a
> pointer of its respective data member type). Indeed, TTree::GetBranch
> seems to find the sub-branch successfully.
> 
> I have tried also things like
> amstree->SetBranchAddress("Eventno", &(event02->Eventno));
> but still no luck-- nothing gets read by GetEvent for this case.
> 
> Probably I'm misunderstanding what the difference is between split
> tree branches and regular branches is.  What am I missing?
> 
> Kate.
> 
> > Rene Brun
> >
> > Kate Scholberg wrote:
> > >
> > > Hi,
> > >
> > > I have a root file containing trees generated with
> > > split branches,
> > >
> > > For instance:
> > >
> > > EventNtuple02 _event02;
> > > static void *pev2=(void*)&_event02;
> > > TBranch *b2=_tree->Branch("event02", "EventNtuple02",  &pev2, 64000,1);
> > >
> > > The EventNtuple02 class is a simple one, containing basic data types
> > >
> > > class EventNtuple02: public TObject {
> > > public:
> > >   int Eventno;
> > >   int Run;
> > > ...
> > > };
> > >
> > > When I try to read the file in a standard way, as follows:
> > >
> > > {
> > >
> > >   // Load shared lib etc..
> > >
> > >   TFile file("prv3.root","UPDATE");
> > >   TTree *amstree = (TTree*)file->Get("AMSRoot");
> > >   EventNtuple02* event02 = new EventNtuple02;
> > >   amstree->SetBranchAddress("event02",&event02);
> > >
> > >   int nevent = amstree.GetEntries();
> > >
> > >   for(int i=0;i<nevent;i++)
> > >     {
> > >
> > >       amstree->GetEvent(i);
> > >       printf("Run, event %d %d\n",event02->Run, event02->Event);
> > >
> > >     }
> > >
> > > }
> > >
> > > No problem... all fine.
> > >
> > > But what I would like to do is access directly one of the sub-branches
> > > of the event02 branch.
> > >
> > > int* evptr = 0;
> > > amstree->SetBranchAddress("Eventno",&evptr)
> > >
> > > This seems to find the sub-branch correctly.
> > > Also if I get the sub-branch with
> > >  TBranch *evbranch = amstree->GetBranch("Eventno");
> > > it Prints OK.
> > >
> > > However, then
> > >
> > > amstree.GetEvent(i)
> > >
> > > doesn't seem to read anything into evptr.
> > >
> > > (and doing explicitly evbranch->GetEntry(i,1) returns nbytes=1).
> > >
> > > Is this the expected behavior for sub-branches?
> > >
> > > I haven't looked into the source, but from my not-extremely-thorough
> > > reading of the docs I would expect SetAddress to create an int, have
> > > evptr point to it, and GetEvent would put stuff into it from the
> > > branch.
> > >
> > > Same behavior if the int evptr points to is already instantiated,
> > > i.e. int* evptr= new int;
> > >
> > > Same behavior on Linux and OSF1, with Root versions 2.22, 2.25 and 3.00
> > >
> > > Kate Scholberg
> > > schol@mit.edu
> >



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