How to convert root graphs into .dat format?

I am a GEANT4 user I get my output in file.root format, The graphs are in TH1F format. I would like to convert these into .dat format for further refining the output.

platform : CentOS 7
root version: ROOT 5.34/36

What is this .dat format you’re referring to?

I want the numerical value of the graph points in data format (txt format).
x f(x)
1 0.1
2 0.4
3 0.5
4 0.7
5 0.8

This piece of code should work
with “g” the name of your TGraph object.

ofstream outputfile("myOutput.dat");
int N = g->GetN(); //get the number of points of the graph
double *x = g->GetX(); //get the X points
double *y = g->GetY();
for(int i=0 ; i<N ; ++i)
  outputfile << i << "\t" << x[i] << "\t" << y[i] << endl; //filling the output file with the graph values
outputfile.close(); // closing the output file

or (with ROOT 5 on the command line):

root [0] gr->Print(); > myOutput.dat

with ROOT 6:

root [0] .> myOutput.dat

This is how I am creating my TH1F object

Energy_GAMMA = new TH1F("Energy_GAMMA","Energy_GAMMA;Energy (MeV);Counts",30,0,30);

What should be my g in this situation?
The Root file which is generated through my code is ROOTAnalysis.root.

Where should I mention that I wanted convert graphs plotted in ROOTAnalysis.root should be converted in a text file.

While running this through command line, I don’t understand where am I mentioning which root file is to converted into txt file.

Energy_GAMMA = new TH1F("Energy_GAMMA","Energy_GAMMA;Energy (MeV);Counts",30,0,30);

This is how I am creating TH1F object and the root file generated has name ROOTAnalysis.root

Ok, so with these additional informations, the code would be the following (you are not using a graph but an 1D histogram).

ofstream outputfile("myOutput.dat");
for(int i=0 ; i<30 ; ++i) //30 is your number of bins
  outputfile << i << "\t" << Energy_GAMMA->GetBinCenter(i) << "\t" << Energy_GAMMA->GetBinContent(i) << endl; //filling the output file with the histogramvalues
outputfile.close(); // closing the output file

note that a histogram is much more than just points on a 2D plane.
it carries a bunch of statistical informations.

one can use root-dump to print – in ASCII art – the content of histograms (1D and 2D):

$> root-ls gauss-h1.root 
=== [gauss-h1.root] ===
version: 60806
TH1D    h1d     h1d     (cycle=1)
TH1F    h1f     h1f     (cycle=1)
TH1F    h1d-var h1d-var (cycle=1)
TH1F    h1f-var h1f-var (cycle=1)

$> root-dump -name=h1d-var gauss-h1.root
>>> file[gauss-h1.root]
key[000]: h1d;1 "h1d" (TH1D)
key[001]: h1f;1 "h1f" (TH1F)
key[002]: h1d-var;1 "h1d-var" (TH1F)
# Mean: 2.812016e-02
# Area: 1.100600e+04
# ID	 ID	 sumw	 sumw2	 sumwx	 sumwx2	 numEntries
Total   	Total   	1.100600e+04	1.211000e+04	3.094905e+02	7.128989e+04	10004
Underflow	Underflow	2.000000e+00	2.000000e+00	0.000000e+00	0.000000e+00	2
Overflow	Overflow	4.000000e+00	8.000000e+00	0.000000e+00	0.000000e+00	2
# xlow	 xhigh	 sumw	 sumw2	 sumwx	 sumwx2	 numEntries
-4.000000e+00	-3.200000e+00	6.600000e+00	7.260000e+00	0.000000e+00	0.000000e+00	6
-3.200000e+00	-2.400000e+00	7.259995e+01	7.986000e+01	0.000000e+00	0.000000e+00	66
-2.400000e+00	-1.600000e+00	5.434013e+02	5.977400e+02	0.000000e+00	0.000000e+00	494
-1.600000e+00	-8.000000e-01	1.708276e+03	1.879130e+03	0.000000e+00	0.000000e+00	1553
-8.000000e-01	0.000000e+00	3.130664e+03	3.443660e+03	0.000000e+00	0.000000e+00	2846
0.000000e+00	8.000000e-01	3.136165e+03	3.449710e+03	0.000000e+00	0.000000e+00	2851
8.000000e-01	1.600000e+00	1.753375e+03	1.928740e+03	0.000000e+00	0.000000e+00	1594
1.600000e+00	2.400000e+00	5.401014e+02	5.941100e+02	0.000000e+00	0.000000e+00	491
2.400000e+00	3.200000e+00	1.011999e+02	1.113200e+02	0.000000e+00	0.000000e+00	92
3.200000e+00	4.000000e+00	7.700000e+00	8.470000e+00	0.000000e+00	0.000000e+00	7

key[003]: h1f-var;1 "h1f-var" (TH1F)

root-dump converts TH{1,2}x histograms to YODA histograms.

That said, it seems you want to extract that data to plot it with another plotting system.
Which one is it? There is perhaps a less convoluted way to do that…


Great thanks!! well this works.

But the problem it is making my simulation very slow. Since I am incorporating it within my program. So basically, I am running a geant4 program (particle tracking software) and within that, for every event it is going through the for loop and filling the respective bin.

No, Actually I need to perform some calculations over the data. And for that I need to have x and f(x) value.

If possible could you step by step tell me how to do this?

root-dump is a Go program.
once you’ve installed Go (see:, you just have to do:

$> go get

and you’re good to go.

that said, it won’t be faster than what @pamputt advised…

1 Like

I would say you should split the work. GEANT4 is designed for doing physics simulation (not analysis). So you should do you simulation and get a ROOT file as output file. Then, you write a ROOT macro to analyse your GEANT4 data. You should not do analysis work in GEANT4 itself because it will slow down the simulation (as you experienced).

I did exactly that. I got a macro written from one of my seniors for his program, but it is crashing.
I have added the query regarding the crash in the following link

Oh! Thanks for your help though. I will give it a try.