# GetListOfKeys from a tree

**URL:** <https://root-forum.cern.ch/t/getlistofkeys-from-a-tree/39913>\
**Category:** Newbie\
**Tags:** tree, python, root\
**Created:** [June 15, 2020, 12:31am UTC](https://root-forum.cern.ch/t/getlistofkeys-from-a-tree/39913 "2020-06-15T00:31:01Z")\
**Posts on this page:** 3\
**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:** [June 15, 2020, 12:31am UTC](https://root-forum.cern.ch/t/getlistofkeys-from-a-tree/39913/1 "2020-06-15T00:31:02Z")

</div>

Dear,

How do you get the name of variables in a tree using GetListOfKeys()?

> file = ROOT.TFile(Path+rootfile,“read”)  
> #how to switch to the tree?  
> rootkeys = file.GetListOfKeys()  
> for key in rootkeys:  
> #here I want to print the name of variables inside the ttree  
> print key.GetName()

Many thanks!

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [June 15, 2020, 6:43am UTC](https://root-forum.cern.ch/t/getlistofkeys-from-a-tree/39913/2 "2020-06-15T06:43:49Z")

</div>

> [@Newbie](#):
>
> #here I want to print the name of variables inside the ttree

Maybe @etejedor has another method, but here is a simple way:

```auto
>>> file = ROOT.TFile('hsimple.root')
>>> ntuple = gROOT.FindObject('ntuple')
>>> ntuple.Print()
******************************************************************************
*Tree :ntuple : Demo ntuple *
*Entries : 25000 : Total = 504554 bytes File Size = 401797 *
* : : Tree compression factor = 1.25 *
******************************************************************************
*Br 0 :px : Float_t *
*Entries : 25000 : Total Size= 100830 bytes File Size = 93378 *
*Baskets : 5 : Basket Size= 32000 bytes Compression= 1.07 *
*............................................................................*
*Br 1 :py : Float_t *
*Entries : 25000 : Total Size= 100830 bytes File Size = 93373 *
*Baskets : 5 : Basket Size= 32000 bytes Compression= 1.07 *
*............................................................................*
*Br 2 :pz : Float_t *
*Entries : 25000 : Total Size= 100830 bytes File Size = 91694 *
*Baskets : 5 : Basket Size= 32000 bytes Compression= 1.09 *
*............................................................................*
*Br 3 :random : Float_t *
*Entries : 25000 : Total Size= 100866 bytes File Size = 90643 *
*Baskets : 5 : Basket Size= 32000 bytes Compression= 1.11 *
*............................................................................*
*Br 4 :i : Float_t *
*Entries : 25000 : Total Size= 100821 bytes File Size = 31885 *
*Baskets : 5 : Basket Size= 32000 bytes Compression= 3.15 *
*............................................................................*
>>>
```

---

<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:** [June 15, 2020, 8:34am UTC](https://root-forum.cern.ch/t/getlistofkeys-from-a-tree/39913/3 "2020-06-15T08:34:41Z")

</div>

What @bellenot suggested is a good option.

As for your question on how to switch to the tree, you can do `t = file.name_of_the_tree`, and then if you want to know the branches it contains you do `t.GetListOfBranches()` and you inspect that.
