# How to extract data from a root file

**URL:** <https://root-forum.cern.ch/t/how-to-extract-data-from-a-root-file/39651>\
**Category:** Newbie\
**Created:** [May 28, 2020, 6:43pm UTC](https://root-forum.cern.ch/t/how-to-extract-data-from-a-root-file/39651 "2020-05-28T18:43:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![esh187](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/esh187/32/24961_2.png) [@esh187](https://root-forum.cern.ch/u/esh187)\
**Post date:** [May 28, 2020, 6:43pm UTC](https://root-forum.cern.ch/t/how-to-extract-data-from-a-root-file/39651/1 "2020-05-28T18:43:22Z")

</div>

I have a root file from Geant4 but I do no know how to extract the data from it in text form or anything.

_ROOT Version:_ v6.18.04  
_OS_ : Windows 10

---

<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:** [May 28, 2020, 7:03pm UTC](https://root-forum.cern.ch/t/how-to-extract-data-from-a-root-file/39651/2 "2020-05-28T19:03:40Z")

</div>

[Geant4 User Forum](https://geant4-forum.web.cern.ch/)

[ROOT Forum -\> Search -\> geant4](https://root-forum.cern.ch/search?q=geant4%20order%3Alatest)

---

<div class="post-metadata">

**Author:** ![esh187](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/esh187/32/24961_2.png) [@esh187](https://root-forum.cern.ch/u/esh187)\
**Post date:** [May 31, 2020, 3:26pm UTC](https://root-forum.cern.ch/t/how-to-extract-data-from-a-root-file/39651/3 "2020-05-31T15:26:42Z")

</div>

There is way too much information on there for it to be able to help me. I am able to access root files using TBrowser but I cannot seem to draw any plots using that data.

---

<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:** [May 31, 2020, 3:41pm UTC](https://root-forum.cern.ch/t/how-to-extract-data-from-a-root-file/39651/4 "2020-05-31T15:41:17Z")

</div>

[ROOT Primer](https://root.cern.ch/root/htmldoc/guides/primer/ROOTPrimer.html)

---

<div class="post-metadata">

**Author:** ![esh187](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/esh187/32/24961_2.png) [@esh187](https://root-forum.cern.ch/u/esh187)\
**Post date:** [June 1, 2020, 2:25pm UTC](https://root-forum.cern.ch/t/how-to-extract-data-from-a-root-file/39651/5 "2020-06-01T14:25:57Z")

</div>

I have this code:

> root -l human\_phantom\_t3.root  
> TBrowser b  
> .ls  
> TFile\*\* human\_phantom\_t3.root  
> TFile\* human\_phantom\_t3.root  
> TDirectoryFile\* human\_phantom\_ntuple human\_phantom\_ntuple  
> OBJ: TTree 1 3Dedep : 0 at: 0x7fcc40a08670  
> OBJ: TTree 1 3Dedep : 0 at: 0x7fcc408a8f90  
> OBJ: TTree 1 3Dedep : 0 at: 0x7fcc409c8590  
> OBJ: TTree 1 3Dedep : 0 at: 0x7fcc37740570  
> OBJ: TTree 1 3Dedep : 0 at: 0x7fcc37753260  
> OBJ: TTree 1 3Dedep : 0 at: 0x7fcc408f1350
> 
> KEY: TTree 1;1 3Dedep
> 
> KEY: TDirectoryFile human\_phantom\_ntuple;1 human\_phantom\_ntuple
> 
> root [12] human\_phantom\_ntuple-\>Draw(“edep”)
> 
> root [13] human\_phantom\_ntuple-\>Draw(“organID”)
> 
> root [14] human\_phantom\_ntuple-\>Draw(“organID:edep”)

I don’t get this

> KEY: TDirectoryFile human\_phantom\_ntuple;1 human\_phantom\_ntuple

Neither can I seem to run the last three lines of code

---

<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:** [June 1, 2020, 5:20pm UTC](https://root-forum.cern.ch/t/how-to-extract-data-from-a-root-file/39651/6 "2020-06-01T17:20:02Z")

</div>

Hi @esh187,  
`human_phantom_ntuple` is a `TDirectoryFile`, i.e. a sub-directory in the file. In the ROOT prompt, you can do `_file0->cd("human_phantom_ntuple"); _file0->ls();` to see the contents of that sub-directory.

Hope this helps!  
Enrico

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [June 3, 2020, 6:26am UTC](https://root-forum.cern.ch/t/how-to-extract-data-from-a-root-file/39651/7 "2020-06-03T06:26:41Z")

</div>

> [@esh187](#):
>
> TBrowser b

I see you also opened a TBrowser. The window opened by this command allows you to navigate easily in the file `human_phantom_t3.root` and therefore understand its structure.

---

<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:** [June 3, 2020, 11:52am UTC](https://root-forum.cern.ch/t/how-to-extract-data-from-a-root-file/39651/8 "2020-06-03T11:52:00Z")

</div>

you can also get a [`tree(1)`](https://linux.die.net/man/1/tree) like view of your file with `groot`'s [`root-ls`](https://pkg.go.dev/go-hep.org/x/hep/groot/cmd/root-ls):

```auto
$> root-ls -t ../testdata/dirs-6.14.00.root ../testdata/B2HHH.root 
=== [../testdata/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)

=== [../testdata/B2HHH.root] ===
version: 60806
  TTree DecayTree (entries=8556118)
    B_FlightDistance "B_FlightDistance/D" TBranch
    B_VertexChi2 "B_VertexChi2/D" TBranch
    H1_PX "H1_PX/D" TBranch
    H1_PY "H1_PY/D" TBranch
    H1_PZ "H1_PZ/D" TBranch
    H1_ProbK "H1_ProbK/D" TBranch
    H1_ProbPi "H1_ProbPi/D" TBranch
    H1_Charge "H1_Charge/I" TBranch
    H1_isMuon "H1_isMuon/I" TBranch
    H1_IpChi2 "H1_IpChi2/D" TBranch
    H2_PX "H2_PX/D" TBranch
    H2_PY "H2_PY/D" TBranch
    H2_PZ "H2_PZ/D" TBranch
    H2_ProbK "H2_ProbK/D" TBranch
    H2_ProbPi "H2_ProbPi/D" TBranch
    H2_Charge "H2_Charge/I" TBranch
    H2_isMuon "H2_isMuon/I" TBranch
    H2_IpChi2 "H2_IpChi2/D" TBranch
    H3_PX "H3_PX/D" TBranch
    H3_PY "H3_PY/D" TBranch
    H3_PZ "H3_PZ/D" TBranch
    H3_ProbK "H3_ProbK/D" TBranch
    H3_ProbPi "H3_ProbPi/D" TBranch
    H3_Charge "H3_Charge/I" TBranch
    H3_isMuon "H3_isMuon/I" TBranch
    H3_IpChi2 "H3_IpChi2/D" TBranch
  TTree DecayTree (entries=8506680)
    B_FlightDistance "B_FlightDistance/D" TBranch
    B_VertexChi2 "B_VertexChi2/D" TBranch
    H1_PX "H1_PX/D" TBranch
    H1_PY "H1_PY/D" TBranch
    H1_PZ "H1_PZ/D" TBranch
    H1_ProbK "H1_ProbK/D" TBranch
    H1_ProbPi "H1_ProbPi/D" TBranch
    H1_Charge "H1_Charge/I" TBranch
    H1_isMuon "H1_isMuon/I" TBranch
    H1_IpChi2 "H1_IpChi2/D" TBranch
    H2_PX "H2_PX/D" TBranch
    H2_PY "H2_PY/D" TBranch
    H2_PZ "H2_PZ/D" TBranch
    H2_ProbK "H2_ProbK/D" TBranch
    H2_ProbPi "H2_ProbPi/D" TBranch
    H2_Charge "H2_Charge/I" TBranch
    H2_isMuon "H2_isMuon/I" TBranch
    H2_IpChi2 "H2_IpChi2/D" TBranch
    H3_PX "H3_PX/D" TBranch
    H3_PY "H3_PY/D" TBranch
    H3_PZ "H3_PZ/D" TBranch
    H3_ProbK "H3_ProbK/D" TBranch
    H3_ProbPi "H3_ProbPi/D" TBranch
    H3_Charge "H3_Charge/I" TBranch
    H3_isMuon "H3_isMuon/I" TBranch
    H3_IpChi2 "H3_IpChi2/D" TBranch

```
