# Loop over subdirectories of a rootfile in pyRoot

**URL:** <https://root-forum.cern.ch/t/loop-over-subdirectories-of-a-rootfile-in-pyroot/32666>\
**Category:** Newbie\
**Created:** [February 9, 2019, 2:22pm UTC](https://root-forum.cern.ch/t/loop-over-subdirectories-of-a-rootfile-in-pyroot/32666 "2019-02-09T14:22:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Newbie](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Newbie](https://root-forum.cern.ch/u/Newbie)\
**Post date:** [February 9, 2019, 2:22pm UTC](https://root-forum.cern.ch/t/loop-over-subdirectories-of-a-rootfile-in-pyroot/32666/1 "2019-02-09T14:22:06Z")

</div>

Hi,

I have the following structure in a root file containing subdirectories with TH1’s:

rootfile.root  
subdir1  
h\_1  
h\_2  
…  
subdir2  
subdir3  
…

I want to normalise all the histograms with a scale factor, is there any clean way to loop over all the subdirectories and histograms without hardcoding in pyRoot? I tried “keys” but it gives only the names of the subdirectories and I couldn’t get the TH1’s.

Thanks!

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [February 12, 2019, 11:17am UTC](https://root-forum.cern.ch/t/loop-over-subdirectories-of-a-rootfile-in-pyroot/32666/2 "2019-02-12T11:17:25Z")

</div>

Hello,  
Can you check if the final solution discussed here works for you?

> [@Loop over all objects in a ROOT file](https://root-forum.cern.ch/t/loop-over-all-objects-in-a-root-file/10807/5):
>
> Since I found both replies being broken in my case. In particular, IsFolder() returns True for TTree, GetListOfKeys() chokes with TTree, etc, so pasting my longer solution here: import ROOT def Map(tf, browsable\_to, tpath=None): """ Maps objets as dict[obj\_name][0] using a TFile (tf) and TObject to browse. """ m = {} for k in browsable\_to.GetListOfKeys(): n = k.GetName() if tpath == None: m[n] = [tf.Get(n)] else: m[n] = [tf.Ge…

Cheers,  
Enric
