Re: Iterator over all objects?

From: Rene Brun (Rene.Brun@cern.ch)
Date: Tue Jun 09 1998 - 10:50:35 MEST


Martin Purschke wrote:
> 
> Hi,
> 
> at some point during a ROOT session I want to selectively store objects
> of a certain class to a file (such as all TH1F's or so). I unsuccesfully
> tried to ask the gRoot object for all the other objects it knows about.
> I can only get the classes, but not a list of all objects. If  I had an
> Iterator which gives me all objects, I would open a file, then loop over
> all the objects, see if it is of the class I'm interested in, and then
> call object->Write().
> Any hints?
> 

Hi Martin,
When objects such as TH1, TTree are created, they are automatically
added to a list of objects in memory associated with the current
directory. You can also add your own objects to this list (in the object
constructor) with a statement like
   object->AppendDirectory();
You can access the list with
   TList *list = gDirectory->GetList();
   list->ls(); // or iterate over the list

In the piece of code below, I show how to iterate over this list
and write only objects derived from TH1 in the file.

   TIter next(list);
   TObject *obj;
   while((obj = (TObject*) next())) {
      if (obj->InheritsFrom(TH1::Class)) obj->Write();
   }

Rene Brun



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:34:33 MET