Looping over histograms

Hi, I have a number of histograms that I manually extract from a .root file using

TFile *file = TFile::Open("myfile.root"); TH1F *h1 = (TH1F*)file->Get("FirstHistogram"); TH1F *h2 = (TH1F*)file->Get("SecondHistogram"); ...
and so on for n histograms. Now, I want to manipulate them all in the same way (using basic operations on histograms and saving to png format). How can I loop over the histograms?

Ideally, I’d like something like this

for ( ??? ) { // Increment the integer j in the name of the histogram hj->DoStuff(); // Act on histogram j }
How do I set up the for loop?

Hi,

you can get the list of keys of your file and loop on them:

for (auto&& keyAsObj : file->GetListOfKeys()){
 auto key = (TKey*) keyAsObj;
 cout << key->GetName() << " " << key->GetClassName() << endl;
}

Cheers,
Danilo

Hi Danilo,

I get Error: illegal label name auto&& keyAsObj
on the line of the for.

Hi,

if this is root5, you’ll have to loop over the keys using c++98 syntax.

Cheers,
Danilo

This is indeed ROOT 5.
How do I do that?

you can find the documentation about how to loop over a TList here: root.cern.ch/root/html/TList.html