# Loop over all objects in a ROOT file

**URL:** <https://root-forum.cern.ch/t/loop-over-all-objects-in-a-root-file/10807>\
**Category:** ROOT\
**Created:** [August 20, 2010, 11:28am UTC](https://root-forum.cern.ch/t/loop-over-all-objects-in-a-root-file/10807 "2010-08-20T11:28:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rmbianchi](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/r/b4bc9f/32.png) [@rmbianchi](https://root-forum.cern.ch/u/rmbianchi)\
**Post date:** [August 20, 2010, 11:28am UTC](https://root-forum.cern.ch/t/loop-over-all-objects-in-a-root-file/10807/1 "2010-08-20T11:28:32Z")

</div>

After having looked for a hint in the forum, I found this thread  
[http://root.cern.ch/phpBB3//viewtopic.php?f=3&t=7246&p=30333](http://root.cern.ch/phpBB3//viewtopic.php?f=3&t=7246&p=30333)

Here I post my version of the small pyroot function, where you can specify a folder path inside the ROOT file.

```auto
def GetKeyNames( self, dir = "" ):
        self.cd(dir)
        return [key.GetName() for key in gDirectory.GetListOfKeys()]
TFile.GetKeyNames = GetKeyNames

keyList = f.GetKeyNames(internalPath)
print "\nKeys in file:", keyList

```

I post it here, with an explicit subject, since it might be useful to other users as well.

Cheers,

Ric.

---

<div class="post-metadata">

**Author:** ![godtripp](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/g/46a35a/32.png) [@godtripp](https://root-forum.cern.ch/u/godtripp)\
**Post date:** [March 8, 2011, 10:37pm UTC](https://root-forum.cern.ch/t/loop-over-all-objects-in-a-root-file/10807/2 "2011-03-08T22:37:37Z")

</div>

This is great, Thanks!

---

<div class="post-metadata">

**Author:** ![ala\_mages](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/2acd7d/32.png) [@ala\_mages](https://root-forum.cern.ch/u/ala_mages)\
**Post date:** [May 24, 2012, 1:15pm UTC](https://root-forum.cern.ch/t/loop-over-all-objects-in-a-root-file/10807/3 "2012-05-24T13:15:48Z")

</div>

Today i needed somehow to have into a list all the objects’ paths so i can access any object with dDirectory.Get(‘path\_to\_object’)(in python code NOT in c++).

So i needed to have dynamically all the object’s path from the root file, that how you list all the objects’ path (directories,subdirectories etc all of them,not just the names of the keys in the current path)

[code]def getObjectsPaths(self):  
mylist = []  
for key in gDirectory.GetListOfKeys():  
mypath = gDirectory.GetPathStatic()  
self.filterKey(key,mypath,mylist)  
[gDirectory.cd](http://gDirectory.cd)(mypath)

```
return mylist

```

def filterKey( self, mykey , currentpath, tolist):  
if mykey.IsFolder():  
if not currentpath.endswith(’/’):  
currentpath+=’/’

```
     topath = currentpath+mykey.GetName()  
     self.cd(topath)
     for key in gDirectory.GetListOfKeys():
          self.filterKey(key,topath,tolist)
 else:
     tolist.append(gDirectory.GetPathStatic()+'/'+mykey.GetName())
     return

```

TFile.filterKey = filterKey  
TFile.getObjectsPaths = getObjectsPaths

refFile = TFile( ‘my\_file.root’ )

yo = refFile.getObjectsPaths()

for path in yo:  
print path[/code]

I know maybe it’s a dirty way to do it , or maybe there is a simpler-better way to do it but i’m not willing to read documentation because i don’t really use ROOT( i just need it in some parts into my code) , i thought that maybe someone would find this piece of code useful , because there are no python example codes using pyROOT and for this simple thing i killed some hours just looking a way to do it just because there are no examples in python

---

<div class="post-metadata">

**Author:** ![andybuckley](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/andybuckley/32/11180_2.png) [@andybuckley](https://root-forum.cern.ch/u/andybuckley)\
**Post date:** [November 18, 2015, 10:52pm UTC](https://root-forum.cern.ch/t/loop-over-all-objects-in-a-root-file/10807/4 "2015-11-18T22:52:25Z")

</div>

I just found this thread and thought I’d post an alternative, I think neater solution. All the intelligence is in the ~7 line getall function. I’ve not checked whether it’s particularly memory efficient since it runs perfectly fast for my current need, which is to just list ROOT all file contents… but being a generator it at least doesn’t try to put all the objects in memory at the same time!

```auto
import ROOT

def getall(d, basepath="/"):
    "Generator function to recurse into a ROOT file/dir and yield (path, obj) pairs"
    for key in d.GetListOfKeys():
        kname = key.GetName()
        if key.IsFolder():
            # TODO: -> "yield from" in Py3
            for i in getall(d.Get(kname), basepath+kname+"/"):
                yield i
        else:
            yield basepath+kname, d.Get(kname)

# Demo
ROOT.gROOT.SetBatch(True)
f = ROOT.TFile("mydata.root")
for k, o in getall(f):
    print o.ClassName(), k

# Output
# TH1D /Nominal/Vertex/RawLeadPt0
# TH2D /Nominal/Vertex/RawSumPt_leadPt_To0
# ...
# TH1D /Nominal/Vertex/RawSumPt_DeltaPhi_lexc0
# TH2D /Nominal/Vertex/RawSumPt_Ramp_TrMin_TrMax0
# ...
# TH1D /Up_Material/Vertex/RawLeadPt0
# TH2D /Up_Material/Vertex/RawSumPt_leadPt_To0
# ...
```

I’ve expanded this into a “rootls” script at [pastebin.com/GebcyHY9](http://pastebin.com/GebcyHY9) (cf. YODA’s yodals).

---

<div class="post-metadata">

**Author:** ![odysei](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/o/51bf81/32.png) [@odysei](https://root-forum.cern.ch/u/odysei)\
**Post date:** [September 27, 2017, 10:46am UTC](https://root-forum.cern.ch/t/loop-over-all-objects-in-a-root-file/10807/5 "2017-09-27T10:46:15Z")

</div>

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:

```python
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.Get(tpath + "/" + n)]
    return m

def Expand_deep_TDirs(tf, to_map, tpath=None):
    """
    A recursive deep-mapping function that expands into TDirectory(ies)
    """
    names = sorted(to_map.keys())
    for n in names:
        if len(to_map[n]) != 1:
            continue
        if tpath == None:
            tpath_ = n
        else:
            tpath_ = tpath + "/" + n
        
        tobject = to_map[n][0]
        if type(tobject) is ROOT.TDirectoryFile:
            m = Map(tf, tobject, tpath_)
            to_map[n].append(m)
            Expand_deep_TDirs(tf, m, tpath_)

def Map_TFile(filename, deep_maps=None):
    """
    Maps an input file as TFile into a dictionary(ies) of objects and names.
    Structure: dict[name][0] == object, dict[name][1] == deeper dict.
    """
    if deep_maps == None:
        deep_maps = {}
    if not type(deep_maps) is dict:
        return deep_maps
    
    f = ROOT.TFile(filename)
    m = Map(f, f)
    Expand_deep_TDirs(f, m)

    deep_maps[filename] = [f]
    deep_maps[filename].append(m)
    
    return deep_maps

```

---

<div class="post-metadata">

**Author:** ![bethlong06](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bethlong06/32/22982_2.png) [@bethlong06](https://root-forum.cern.ch/u/bethlong06)\
**Post date:** [April 26, 2024, 1:49pm UTC](https://root-forum.cern.ch/t/loop-over-all-objects-in-a-root-file/10807/6 "2024-04-26T13:49:52Z")

</div>

This is very useful but as far as I can make out it lists everything in the root file, ignoring the `basepath`.  
I think if you add `sd = d.Get(basepath)` and then do `for key in sd.GetListOfKeys():` it should work, although I then got an error with `o.ClassName()` (I’m not sure what this was doing…)  
This works for me, anyway:

```auto
def getall(d, basepath="/"):
    "Generator function to recurse into a ROOT file/dir and yield (path, obj) pairs"
    sd = d.Get(basepath)
    for key in sd.GetListOfKeys():
        kname = key.GetName()
        if key.IsFolder():
            # TODO: -> "yield from" in Py3
            for i in getall(d.Get(kname), basepath+kname+"/"):
                yield i
        else:
            yield basepath+kname, d.Get(kname)

```
