Hi there,
I have a question regarding a TFile. Is there a way to loop over all histograms in the TDirectories, within a TFile?
The structure of my TFile is as follows:
TFile
TDirectory "Dir1"
TH1 "dir1_histo1"
.
.
TDirectory "Dir2"
TH1 "dir2_histo1"
I started looking at the BaBar website and have constructed the following; But I think I would also benefit for other people comments.
Many thanks,
Colin.
//-------------------------------------------------------------
//read TFile
TFile *infile = new TFile(inputfile);
//loop counter(s)
int i=0;
int j=0;
TIter nextkey(gDirectory->GetListOfKeys());
while (key = (TKey*)nextkey()) {
std::cout << "i: " << i << std::endl;
++i;
obj = key->ReadObj();
std::string name = obj->GetName();
std::cout << "name: " << name << std::endl;
if ( obj->IsA()->InheritsFrom("TDirectory") ){
TIter nextkey(gDirectory->GetListOfKeys());
while (key = (TKey*)nextkey()) {
obj = key->ReadObj();
if (obj->IsA()->InheritsFrom("TH1")) {
h = (TH1*)obj;
std::cout << h->GetName() << std::endl;
std::cout << "j: " << j << std::endl;
++j;
}
}
}
}//while