I’d like to iteratate over all histograms in a given root file. What is the best way to go about this?
I would think the slickest way is to somehow get a list of all TH1F’s in a file, and then just iterate over that list. (My motivation for that is because I’d like to then find corresponding histograms in another file.)
However, the only current way I know how to do this is to deal with the histograms as we find them by iterating over everything in a root file. Is there a better way than the following?
[code]void recurseOverKeys( TDirectory *target ) {
// Figure out where we are
TString path( (char*)strstr( target->GetPath(), “:” ) );
path.Remove( 0, 2 );
sourceFile->cd( path );
TDirectory *current_sourcedir = gDirectory;
TKey *key;
TIter nextkey(current_sourcedir->GetListOfKeys());
while (key = (TKey*)nextkey()) {
obj = key->ReadObj();
// Check if this is a 1D histogram or a directory
if (obj->IsA()->InheritsFrom("TH1F")) {
TH1F *htemp = (TH1F*)obj;
// Do something to htemp here!
} else if ( obj->IsA()->InheritsFrom( "TDirectory" ) ) {
// it's a subdirectory
cout << "Found subdirectory " << obj->GetName() << endl;
// obj is now the starting point of another round of merging
// obj still knows its depth within the target file via
// GetPath(), so we can still figure out where we are in the recursion
recurseOverKeys( (TDirectory*)obj );
} // end of IF a TDriectory
}
}
[/code]