Getting list of histograms from a root file which has bunch of directories in it

I want to get list of histogram stored in a directory in a root file.

To print the list do:

directory->ls();

Well, I actually wanna put them in a vector, then I can loop over in order to draw

If your file has no subdirectory this will do:

void loopdir11() {
   TFile *f1 = TFile::Open("hsimple.root");
   std::vector<TH1*> vec;
   for(auto k : *f1->GetListOfKeys()) {
      TKey *key = static_cast<TKey*>(k);
      TClass *cl = gROOT->GetClass(key->GetClassName());
      if (!cl->InheritsFrom("TH1")) continue;
      vec->push_back( (TH1*)key->ReadObj() );
   }
}

Otherwise see tutorials/io/readCode.C

Cheers,
Philippe.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.