List of all histograms in a file

I want to write code following this scheme:

1. open root file 2. get list of all histograms in this file 3. loop over list 3a. draw histogram list[i]

However, I don’t know how to perform step 2. Is there any way to do that?
The file containes histograms only.

Thanks a lot!
Cheers,
Martin

1 Like

see in $ROOTSYS/tutorials/io/loopdir.C

Jan

First comment: In my installation I found loopdir.C at $ROOTSYS/tutorials/loopdir.C
I am using 5.13/02.

Second comment: I have a root file with a subdirectory which has many histograms which can be seen in TBrowser. I find that when I use the keys method I only get the key for the subdirectory. The keys to the histograms are never returned. It would seem that I need to set my directory to the subdirectory and then fetch the keys. I tried this with this code:

TFile *f = new TFile(“T0Classic_classic_t0_fitter.root”);
f->cd(“classic_t0_fitter”);
TIter next(f->GetListOfKeys());
TKey key;
nhist=0;
while ((key = (TKey
)next())) {
nhist++;
cout << "Key " << nhist << endl;
cout << " Classname " <GetClassName() << endl;
cout << " Title " <GetTitle() << endl;
}

This produces this output:

Key 1
Classname TDirectory
Title classic_t0_fitter

However, there are 100’s of histrograms in the “classic_t0_fitter” subdirectory which I am not getting the keys for. What do I need to do differently to get them?

Thanks much

Hi diehl,

  1. $ROOTSYS/tutorials/io/loopdir.C for ROOT >= 5.14.00
  2. see this link root.cern.ch/phpBB2/viewtopic.php?t=1659

Jan

Thanks Jan for the quick reply. I also found a local root expert who helped me solve my problem. The problem was this line of my code:

f->cd(“subdirectory”)
TIter next(f->GetListOfKeys());

I thought that the GetListofKeys() would retrieve the keys of the subdirectory. However, I only get the list of keys in the highest directory level of the file, which in my case is just one subdirectory.

What one needs to do is this:

f->cd(“subdirectory”)
TIter next(gDirectory->GetListOfKeys());

gDirectory points at the current directory which I’ve cd’ed to.

Thanks for the help, Edward Diehl

See root.cern.ch/root/htmldoc/TFileIter.html also

[code]{
TFile *f = new TFile(“test.root”);
TFileIter readObj(f);
// the number of the object available directly from “MyDataSet.root"
Int_t size = readObj.TotalKeys();
printf(” The total number of the objects: %d\n",size);

//------------------------------------------------------
// Loop over all objects, read them in to memory one by one

printf(" – > Loop over all objects < – \n");
// Attention: ROOTForum corrupted the following C++ “for” statement !!!
// See the link above for the correct text !!!
for( readObj = 0; int(readObj) GetName()
,nextObject->IsA()->GetName()
,(const char *)readObj
);
delete nextObject;
}[/code]

Valeri,

Two remarks
-That is not what the user wants. He needs to navigate in sub-directories too.
-when posting code as you do, check the button “Disable html in post”

Rene

[quote=“brun”]Two remarks
-That is not what the user wants. He needs to navigate in sub-directories too.[/quote]OO :bulb: Yes :bulb: I have to fix this immidiately :exclamation: . My solution was narrowed by local :blush: needs

[quote=“brun”]-when posting code as you do, check the button “Disable html in post”[/quote]Thank you Rene, May be this should be default. I believe (I do know) many people have had the same problem and none thought it can be fixed and even worth to complain.

Valeri,

To set the “disable html” option pernanently on, click on the “Profile” button
at the top of the page.

Fons changed the default for all new users to disable html.

Rene