Iteration over a directory

Dear all,

I am trying to iterate over all histograms in a directory with:

TFile* f1 = new TFile("file.root"); TList* tdf_obs = f1->GetDirectory("path/to/directory")->GetList(); TIterator* obs_it = tdf_obs->MakeIterator(); TIter iter(obs_it); std::for_each( iter.Begin(), iter.End(), fn );


void fn(TObject* histobj) { std::cout << histobj->GetName() << std::endl; TH2D* hist = (TH2D*)histobj->Clone(); std::cout << "information" << std::endl; }

However the list that I make on the second line is empty even though I know that there are TH2Ds in the folder. E.g. the result of ls:

TDirectoryFile* Folder Folder
KEY: TH2D 0_Image;1 0_Image
KEY: TH2D 1_Image;1 1_Image

Does anybody know what could be causing this?



GetList only return the list of already loaded (and attached) objects in the directory.
You actually want to iterate over the list of keys:TFile* f1 = new TFile("file.root"); TList* tdf_obs = f1->GetDirectory("path/to/directory")->GetListOfKeys(); // no need to call MakeIterator explicitly. TIter iter(tdf_obs); std::for_each( iter.Begin(), iter.End(), fn );[code]and fn becomes:[code]void fn(TObject* key) { std::cout << key->GetName() << std::endl; TH2D* hist = (TH2D*)key->ReadObj(); std::cout << "information" << std::endl; }