From $ROOTSYS/tutorials/io/loopdir.C

void loopdir() {
   // example of script to loop on all the objects of a ROOT file directory
   // and print on Postscript all TH1 derived objects
   // This script uses the file generated by tutorial hsimple.C
   //Author: Rene Brun
   
   TString dir = gSystem->UnixPathName(__FILE__);
   dir.ReplaceAll("loopdir.C","../hsimple.C");
   dir.ReplaceAll("/./","/");
   if (!gInterpreter->IsLoaded(dir.Data())) gInterpreter->LoadMacro(dir.Data());
   TFile *f1 = (TFile*)gROOT->ProcessLineFast("hsimple(1)");
   TIter next(f1->GetListOfKeys());
   TKey *key;
   TCanvas c1;
   c1.Print("hsimple.ps[");
   while ((key = (TKey*)next())) {
      TClass *cl = gROOT->GetClass(key->GetClassName());
      if (!cl->InheritsFrom("TH1")) continue;
      TH1 *h = (TH1*)key->ReadObj();
      h->Draw();
      c1.Print("hsimple.ps");
   }
   c1.Print("hsimple.ps]");
}
      
 loopdir.C:1
 loopdir.C:2
 loopdir.C:3
 loopdir.C:4
 loopdir.C:5
 loopdir.C:6
 loopdir.C:7
 loopdir.C:8
 loopdir.C:9
 loopdir.C:10
 loopdir.C:11
 loopdir.C:12
 loopdir.C:13
 loopdir.C:14
 loopdir.C:15
 loopdir.C:16
 loopdir.C:17
 loopdir.C:18
 loopdir.C:19
 loopdir.C:20
 loopdir.C:21
 loopdir.C:22
 loopdir.C:23
 loopdir.C:24
 loopdir.C:25
 loopdir.C:26