Re: How to loop over histos in file

From: Rene Brun (Rene.Brun@cern.ch)
Date: Mon Jun 15 1998 - 16:09:29 MEST


Norbert Danneberg wrote:
> 
> HI Rooters,
> 
> I try too loop over all histos within a file using the following code :
> 
> BrowseFile(Char_t *FileName){
> 
>   f1 = new TFile(FileName);
>   TList *MyList = f1->GetList();
>   TIter next(MyList);
>   MyList->ls();
>   TObject *obj;
>   while((obj = (TObject*)next())){
>     if(obj->InheritsFrom(TH1::Class)) obj->Draw();
>   }
>   c1.Update();
>   while(!getchar());
> 
>   f1.Close();
>   f1.Delete();
> }
> 
> It seems f1->GetList does not return the content of the file. What am I
> doing wrong ??
> 

In your example, you loop on objects in memory. What you want
is to loop on all objects on the file.
You will find below a small example illustrating how to do it.
For more elaborated examples, see the code of TDirectory itself.
For example:

 
http://root.cern.ch/root/html/src/TDirectory.cxx.html#TDirectory:Browse


{
   TFile *file = new TFile("hsimple.root");
   TObject *obj;
   TKey *key;
   TIter next( file->GetListOfKeys());
   while ((key = (TKey *) next())) {
      obj = file->Get(key->GetName()); // copy object to memory
      // do something with obj
      printf(" found object:%s\n",key->GetName());
   }
}

Rene Brun



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