How to extract data from geant4 root

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? 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 (33.6 KB)

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):


Cheers, Bertrand.

[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.

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=“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!

[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!


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.