# Looping over dir inside a root file

**URL:** <https://root-forum.cern.ch/t/looping-over-dir-inside-a-root-file/46410>\
**Category:** Newbie\
**Created:** [August 23, 2021, 6:37pm UTC](https://root-forum.cern.ch/t/looping-over-dir-inside-a-root-file/46410 "2021-08-23T18:37:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aman\_Upadhyay](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aman_upadhyay/32/23958_2.png) [@Aman\_Upadhyay](https://root-forum.cern.ch/u/Aman_Upadhyay)\
**Post date:** [August 23, 2021, 6:37pm UTC](https://root-forum.cern.ch/t/looping-over-dir-inside-a-root-file/46410/1 "2021-08-23T18:37:20Z")

</div>

Hello Rooters,

I have a root file with many directories with names DF\_XXXXX, as shown in the attached image. They all have parts of the same tree. I am not sure why this tree was split like this, but now I have this file, and I need to loop over all the folders to extract information from the tree. Is there a way I can do that without painstakingly hardcoding the name of each folder?

Thank you for your time!

* * *

ROOT Version:6.24/02  
Platform:(NAME=“HefftorLinux”,ID\_LIKE=“arch”,VERSION\_ID=v2021-02-05)  
Compiler: gcc 11.1.0

* * *

![HefftorLinux_2021-08-24_00-02-29](https://root-forum.cern.ch/uploads/default/original/3X/d/e/dea67d29f85f2a43afcfb5c9f55a1f4855177666.png)

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [August 24, 2021, 7:43am UTC](https://root-forum.cern.ch/t/looping-over-dir-inside-a-root-file/46410/2 "2021-08-24T07:43:42Z")

</div>

Hi @Aman_Upadhyay ,  
given a `TFile` you can loop over its entries with

```cpp
for (auto k : *file.GetListOfKeys())

```

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![Aman\_Upadhyay](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aman_upadhyay/32/23958_2.png) [@Aman\_Upadhyay](https://root-forum.cern.ch/u/Aman_Upadhyay)\
**Post date:** [August 24, 2021, 2:50pm UTC](https://root-forum.cern.ch/t/looping-over-dir-inside-a-root-file/46410/3 "2021-08-24T14:50:51Z")

</div>

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![Aman\_Upadhyay](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/aman_upadhyay/32/23958_2.png) [@Aman\_Upadhyay](https://root-forum.cern.ch/u/Aman_Upadhyay)\
**Post date:** [August 24, 2021, 9:59pm UTC](https://root-forum.cern.ch/t/looping-over-dir-inside-a-root-file/46410/4 "2021-08-24T21:59:49Z")

</div>

Hi,

I tried to and it gave me the address as TObjects. When using my code to Get TTrees

```auto
TFile T("AnalysisResults_trees.root");
	for (auto k : *T.GetListOfKeys()){
	TTree *t1 = (TTree*)k->Get("My_tree");

```

It returns an error:

```auto
/home/train/configure.C:85:25: error: no member named 'Get' in 'TObject'
        TTree *t1 = (TTree*)k->Get("My_tree");

```

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [August 25, 2021, 7:50am UTC](https://root-forum.cern.ch/t/looping-over-dir-inside-a-root-file/46410/5 "2021-08-25T07:50:58Z")

</div>

Yes `k` in that snippet is a `TObject*`, because [GetListOfKeys](https://root.cern/doc/master/classTDirectoryFile.html#a809cd04ee265ab10db534c808c1d8b89) returns a [TList](https://root.cern/doc/master/classTList.html) pointer and iterating over a `TList` yields `TObject*`. You can use `k->GetName()` to get the key value (the names of the objects in the file).

If you just want to extract `"My_tree"` from the file:

```cpp
TTree* tree = T.Get<TTree>("My_tree");
assert(tree != nullptr);

```

Cheers,  
Enrico
