# How to extract data from geant4 root

**URL:** <https://root-forum.cern.ch/t/how-to-extract-data-from-geant4-root/20914>\
**Category:** ROOT\
**Created:** [April 5, 2016, 7:39am UTC](https://root-forum.cern.ch/t/how-to-extract-data-from-geant4-root/20914 "2016-04-05T07:39:40Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeonyboy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jeonyboy/32/19388_2.png) [@jeonyboy](https://root-forum.cern.ch/u/jeonyboy)\
**Post date:** [April 5, 2016, 7:39am UTC](https://root-forum.cern.ch/t/how-to-extract-data-from-geant4-root/20914/1 "2016-04-05T07:39:40Z")

</div>

Hello, I am trying to extract data from geant4 root file called inelastic.root. What I want is numbered data for x-axis and y-axis.

I tried to follow [How to extract a data from dna.root file?](https://root-forum.cern.ch/t/how-to-extract-a-data-from-dna-root-file/16182/1) post here but I was able to do the geant4dna file posted there but unable to do so even when I change the code to match my file name.

Can someone help? I am a complete newbie user to root as well as coding.

I attached my root file for reference. Thank you for your help!  
[inelastic.root](https://root-forum.cern.ch/uploads/default/original/2X/9/98b2b944d54da93febbb92181370b112c48956a4.root) (33.6 KB)

---

<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:** [April 5, 2016, 7:49am UTC](https://root-forum.cern.ch/t/how-to-extract-data-from-geant4-root/20914/2 "2016-04-05T07:49:35Z")

</div>

Hi,

The file contains 1D histograms (TH1D):

`C:\Users\bellenot\rootdev>root -l
root [0] TFile *f = TFile::Open("inelastic.root")
root [1] f->ls()
TFile** inelastic.root
 TFile* inelastic.root
  KEY: TH1D 2;1 kinetic energy of gamma
  KEY: TH1D 4;1 kinetic energy of protons
  KEY: TH1D 5;1 kinetic energy of deuterons
  KEY: TH1D 6;1 kinetic energy of alphas
  KEY: TH1D 7;1 kinetic energy of nuclei
  KEY: TH1D 10;1 Q = Ekin out - Ekin in
root [2]
`  
You can also start a TBrowser and inspect/draw the content of the file (see the screenshot below):

 ![](https://root-forum.cern.ch/uploads/default/original/2X/a/af045a2760d57611f722231e35f34e38c29d1857.jpg)  

Cheers, Bertrand.

---

<div class="post-metadata">

**Author:** ![jeonyboy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jeonyboy/32/19388_2.png) [@jeonyboy](https://root-forum.cern.ch/u/jeonyboy)\
**Post date:** [April 5, 2016, 7:54am UTC](https://root-forum.cern.ch/t/how-to-extract-data-from-geant4-root/20914/3 "2016-04-05T07:54:13Z")

</div>

[quote=“bellenot”]Hi,

The file contains 1D histograms (TH1D):

`C:\Users\bellenot\rootdev>root -l
root [0] TFile *f = TFile::Open("inelastic.root")
root [1] f->ls()
TFile** inelastic.root
 TFile* inelastic.root
  KEY: TH1D 2;1 kinetic energy of gamma
  KEY: TH1D 4;1 kinetic energy of protons
  KEY: TH1D 5;1 kinetic energy of deuterons
  KEY: TH1D 6;1 kinetic energy of alphas
  KEY: TH1D 7;1 kinetic energy of nuclei
  KEY: TH1D 10;1 Q = Ekin out - Ekin in
root [2]
`  
You can also start a TBrowser and inspect/draw the content of the file (see the screenshot below):

[attachment=0]TBrowser.jpg[/attachment]  
Cheers, Bertrand.[/quote]

Hi, first thanks for your reply. I think my purpose wasn’t clear though, I am able to draw the graph already but I want to extract the graph into a file with data only i.e text file or excel file.

---

<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:** [April 5, 2016, 8:23am UTC](https://root-forum.cern.ch/t/how-to-extract-data-from-geant4-root/20914/4 "2016-04-05T08:23:44Z")

</div>

OK, then you can print the content of the histogram in a text file (here h2.txt):

`root [0] .> h2.txt
root [1] TFile *f = TFile::Open("inelastic.root");
root [2] TH1D *h2 = (TH1D *)f->Get("2");
root [3] h2->Print("all");
root [4] .>
root [5] .q
`  
Cheers, Bertrand.

---

<div class="post-metadata">

**Author:** ![jeonyboy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jeonyboy/32/19388_2.png) [@jeonyboy](https://root-forum.cern.ch/u/jeonyboy)\
**Post date:** [April 6, 2016, 1:48am UTC](https://root-forum.cern.ch/t/how-to-extract-data-from-geant4-root/20914/5 "2016-04-06T01:48:51Z")

</div>

[quote=“bellenot”]OK, then you can print the content of the histogram in a text file (here h2.txt):

`root [0] .> h2.txt
root [1] TFile *f = TFile::Open("inelastic.root");
root [2] TH1D *h2 = (TH1D *)f->Get("2");
root [3] h2->Print("all");
root [4] .>
root [5] .q
`  
Cheers, Bertrand.[/quote]

Thank you so much! I followed your code and the output was displayed on the screen but not in the text file. I can just copy and paste from here though. Thank you again!

---

<div class="post-metadata">

**Author:** ![jeonyboy](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jeonyboy/32/19388_2.png) [@jeonyboy](https://root-forum.cern.ch/u/jeonyboy)\
**Post date:** [April 6, 2016, 1:49am UTC](https://root-forum.cern.ch/t/how-to-extract-data-from-geant4-root/20914/6 "2016-04-06T01:49:18Z")

</div>

[quote=“bellenot”]OK, then you can print the content of the histogram in a text file (here h2.txt):

`root [0] .> h2.txt
root [1] TFile *f = TFile::Open("inelastic.root");
root [2] TH1D *h2 = (TH1D *)f->Get("2");
root [3] h2->Print("all");
root [4] .>
root [5] .q
`  
Cheers, Bertrand.[/quote]

Thank you so much! I followed your code and the output was displayed on the screen but not in the text file. I can just copy and paste from here though. Thank you again!

 ![](https://root-forum.cern.ch/uploads/default/original/2X/c/c4745f62c06cf0d8f2dc66e02e475a350fd27cdf.PNG)

---

<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:** [April 6, 2016, 7:13am UTC](https://root-forum.cern.ch/t/how-to-extract-data-from-geant4-root/20914/7 "2016-04-06T07:13:49Z")

</div>

Hi,

[quote=“jeonyboy”]Thank you so much! I followed your code and the output was displayed on the screen but not in the text file. I can just copy and paste from here though. Thank you again!  
[/quote]  
You’re welcome! And since you are using ROOT 5, you should do following:

`
TFile *f = TFile::Open("inelastic.root");
TH1D *h2 = (TH1D *)f->Get("2");
h2->Print("all"); > h2.txt`  
Cheers, Bertrand.

---

<div class="post-metadata">

**Author:** ![Khalid\_Raza](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/khalid_raza/32/35880_2.png) [@Khalid\_Raza](https://root-forum.cern.ch/u/Khalid_Raza)\
**Post date:** [March 21, 2024, 5:29am UTC](https://root-forum.cern.ch/t/how-to-extract-data-from-geant4-root/20914/8 "2024-03-21T05:29:20Z")

</div>

```auto
root [0] root [0] .> h2.txt root [1] TFile *f = TFile::Open("/Desktop/inelastic.ROOT_prompt_0:1:11: error: expected unqualified-idoot [3] h2->Print("all"); rootroot [0] .> h2.txt root [1] TFile *f = TFile::Open("/Desktop/inelastic.root"); root [2] TH1D *h2 = (TH1D *)f->Get("2"); root [3] h2->Print("all"); root [4] .> root [5] .q
          ^
ROOT_prompt_0:1:13: error: use of undeclared identifier 'h2'
root [0] .> h2.txt root [1] TFile *f = TFile::Open("/Desktop/inelastic.root"); root [2] TH1D *h2 = (TH1D *)f->Get("2"); root [3] h2->Print("all"); root [4] .> root [5] .q
            ^
ROOT_prompt_0:1:80: error: use of undeclared identifier 'root'
root [0] .> h2.txt root [1] TFile *f = TFile::Open("/Desktop/inelastic.root"); root [2] TH1D *h2 = (TH1D *)f->Get("2"); root [3] h2->Print("all"); root [4] .> root [5] .q
                                                                               ^
ROOT_prompt_0:1:121: error: use of undeclared identifier 'root'
root [0] .> h2.txt root [1] TFile *f = TFile::Open("/Desktop/inelastic.root"); root [2] TH1D *h2 = (TH1D *)f->Get("2"); root [3] h2->Print("all"); root [4] .> root [5] .q
                                                                                                                        ^
ROOT_prompt_0:1:148: error: use of undeclared identifier 'root'
root [0] .> h2.txt root [1] TFile *f = TFile::Open("/Desktop/inelastic.root"); root [2] TH1D *h2 = (TH1D *)f->Get("2"); root [3] h2->Print("all"); root [4] .> root [5] .q
                                                                                                                                                   ^
ROOT_prompt_0:1:158: error: expected unqualified-id
root [0] .> h2.txt root [1] TFile *f = TFile::Open("/Desktop/inelastic.root"); root [2] TH1D *h2 = (TH1D *)f->Get("2"); root [3] h2->Print("all"); root [4] .> root [5] .q
                                                                                                                                                             ^
ROOT_prompt_0:1:160: error: use of undeclared identifier 'root'
root [0] .> h2.txt root [1] TFile *f = TFile::Open("/Desktop/inelastic.root"); root [2] TH1D *h2 = (TH1D *)f->Get("2"); root [3] h2->Print("all"); root [4] .> root [5] .q

```

When i give this code then this error will be created.

---

<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:** [March 21, 2024, 8:18am UTC](https://root-forum.cern.ch/t/how-to-extract-data-from-geant4-root/20914/9 "2024-03-21T08:18:46Z")

</div>

Welcome to the root forum.

why do you have:

```auto
root [0] .> h2.txt

```

?  
Don’t do that and it will be fine.
