# How many Trees are in a root file?

**URL:** <https://root-forum.cern.ch/t/how-many-trees-are-in-a-root-file/32477>\
**Category:** Newbie\
**Tags:** tree, root\
**Created:** [January 28, 2019, 8:23pm UTC](https://root-forum.cern.ch/t/how-many-trees-are-in-a-root-file/32477 "2019-01-28T20:23:44Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nyx](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@Nyx](https://root-forum.cern.ch/u/Nyx)\
**Post date:** [January 28, 2019, 8:23pm UTC](https://root-forum.cern.ch/t/how-many-trees-are-in-a-root-file/32477/1 "2019-01-28T20:23:45Z")

</div>

Is there a function to get the number of Trees in a root file?

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [January 29, 2019, 4:08am UTC](https://root-forum.cern.ch/t/how-many-trees-are-in-a-root-file/32477/2 "2019-01-29T04:08:51Z")

</div>

Hi,

you can loop on the keys holding objects stored in a ROOT files and get their type name, e.g. “TTree” as follows:

```auto
for (auto&& keyAsObj : file->GetListOfKeys()){
 auto key = (TKey*) keyAsObj;
 cout << key->GetName() << " " << key->GetClassName() << endl;
}

```

if it is enough for you, you can achieve this with a command line utility, rootls

```auto
rootls myfile.root

```

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![sbinet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sbinet/32/13166_2.png) [@sbinet](https://root-forum.cern.ch/u/sbinet)\
**Post date:** [January 29, 2019, 8:35am UTC](https://root-forum.cern.ch/t/how-many-trees-are-in-a-root-file/32477/3 "2019-01-29T08:35:38Z")

</div>

Not sure this solution is addressing trees hidden in deep directories’ hierarchies. Is it?

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [January 29, 2019, 8:57am UTC](https://root-forum.cern.ch/t/how-many-trees-are-in-a-root-file/32477/4 "2019-01-29T08:57:31Z")

</div>

Welcome to the Newbie category!  
Here we take it easy and assume the case is the simplest possible 🙂  
With recursion this would look like

```auto
void listRecursive(TDirectoryFile *f, unsigned int nindent = 0) {
   const std::string indent(nindent, ' ');
   for (auto &&keyAsObj : *f->GetListOfKeys())
   {
      auto key = (TKey *)keyAsObj;
      cout << indent << key->GetName() << " " << key->GetClassName() << endl;
      if (auto d = dynamic_cast<TDirectoryFile*>(key->ReadObj())) {
         listRecursive(d, nindent + 2);
      }
   }
};

void myMacro()
{

   TFile file("a.root");
   listRecursive(&file);
}

```

but let’s not dive into solutions of problems if we do not know they exist!

Cheers,  
D

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [January 29, 2019, 9:01am UTC](https://root-forum.cern.ch/t/how-many-trees-are-in-a-root-file/32477/5 "2019-01-29T09:01:01Z")

</div>

People sometimes use [TFolder](https://root.cern.ch/doc/master/classTFolder.html) based structures, which your scan does not cover (so, some trees may be missing in the list).

Also, if you scan [TKey](https://root.cern.ch/doc/master/classTKey.html) instances, you will get all “cycles” listed separately (so, some trees may appear many times in the list).

---

<div class="post-metadata">

**Author:** ![sbinet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sbinet/32/13166_2.png) [@sbinet](https://root-forum.cern.ch/u/sbinet)\
**Post date:** [January 29, 2019, 11:25am UTC](https://root-forum.cern.ch/t/how-many-trees-are-in-a-root-file/32477/6 "2019-01-29T11:25:02Z")

</div>

also, `rootls` from ROOT doesn’t handle subdirs nor recursion:

```auto
$> root --version -b -q
   ------------------------------------------------------------
  | Welcome to ROOT 6.14/04 http://root.cern.ch |
  | (c) 1995-2018, The ROOT Team |
  | Built for linuxx8664gcc |
  | From tags/v6-14-04@v6-14-04, Aug 23 2018, 17:00:44 |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q' |
   ------------------------------------------------------------

$> rootls -t -l ./dirs-6.14.00.root
TDirectoryFile Jul 03 11:08 2018 dir1 "dir1"
TDirectoryFile Jul 03 11:08 2018 dir2 "dir2"
TDirectoryFile Jul 03 11:08 2018 dir3 "dir3"

```

[groot/cmd/root-ls](https://go-hep.org/x/hep/groot/cmd/root-ls) does 🙂

```auto
$> root-ls -t ./dirs-6.14.00.root
=== [./dirs-6.14.00.root] ===
version: 61400
TDirectoryFile dir1 dir1 (cycle=1)
  TDirectoryFile dir11 dir11 (cycle=1)
    TH1F h1 h1 (cycle=1)
TDirectoryFile dir2 dir2 (cycle=1)
TDirectoryFile dir3 dir3 (cycle=1)

```

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [January 29, 2019, 12:52pm UTC](https://root-forum.cern.ch/t/how-many-trees-are-in-a-root-file/32477/7 "2019-01-29T12:52:33Z")

</div>

> [@sbinet](#):
>
> also, `rootls` from ROOT doesn’t handle subdirs nor recursion:

It is not fully true. Try:

```auto
rootls "dirs-6.14.00.root:*" 

```

---

<div class="post-metadata">

**Author:** ![sbinet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/sbinet/32/13166_2.png) [@sbinet](https://root-forum.cern.ch/u/sbinet)\
**Post date:** [January 29, 2019, 1:20pm UTC](https://root-forum.cern.ch/t/how-many-trees-are-in-a-root-file/32477/8 "2019-01-29T13:20:22Z")

</div>

I stand corrected 🙂

but it’s not super useful either:

```auto
$> rootls -l "dirs-6.14.00.root:*" 
dir1 :
  TDirectoryFile Jul 03 11:08 2018 dir11 "dir11"
dir2 :
dir3 :

$> rootls -l "dirs-6.14.00.root:*/*" 
TH1F Jul 03 11:08 2018 h1 "h1"

```
