# How to convert root graphs into .dat format?

**URL:** <https://root-forum.cern.ch/t/how-to-convert-root-graphs-into-dat-format/32184>\
**Category:** Newbie\
**Created:** [January 7, 2019, 10:23am UTC](https://root-forum.cern.ch/t/how-to-convert-root-graphs-into-dat-format/32184 "2019-01-07T10:23:32Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aqsa\_Shaikh](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@Aqsa\_Shaikh](https://root-forum.cern.ch/u/Aqsa_Shaikh)\
**Post date:** [January 7, 2019, 10:23am UTC](https://root-forum.cern.ch/t/how-to-convert-root-graphs-into-dat-format/32184/1 "2019-01-07T10:23:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [January 7, 2019, 10:33am UTC](https://root-forum.cern.ch/t/how-to-convert-root-graphs-into-dat-format/32184/2 "2019-01-07T10:33:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Aqsa\_Shaikh](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@Aqsa\_Shaikh](https://root-forum.cern.ch/u/Aqsa_Shaikh)\
**Post date:** [January 7, 2019, 10:56am UTC](https://root-forum.cern.ch/t/how-to-convert-root-graphs-into-dat-format/32184/3 "2019-01-07T10:56:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pamputt](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pamputt/32/12526_2.png) [@pamputt](https://root-forum.cern.ch/u/pamputt)\
**Post date:** [January 7, 2019, 12:12pm UTC](https://root-forum.cern.ch/t/how-to-convert-root-graphs-into-dat-format/32184/4 "2019-01-07T12:12:39Z")

</div>

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

```auto
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

```

---

<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:** [January 7, 2019, 2:00pm UTC](https://root-forum.cern.ch/t/how-to-convert-root-graphs-into-dat-format/32184/5 "2019-01-07T14:00:26Z")

</div>

or (with ROOT 5 on the command line):

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

```

with ROOT 6:

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

```

---

<div class="post-metadata">

**Author:** ![Aqsa\_Shaikh](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@Aqsa\_Shaikh](https://root-forum.cern.ch/u/Aqsa_Shaikh)\
**Post date:** [January 8, 2019, 6:43am UTC](https://root-forum.cern.ch/t/how-to-convert-root-graphs-into-dat-format/32184/6 "2019-01-08T06:43:32Z")

</div>

This is how I am creating my TH1F object

```auto
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.

---

<div class="post-metadata">

**Author:** ![Aqsa\_Shaikh](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@Aqsa\_Shaikh](https://root-forum.cern.ch/u/Aqsa_Shaikh)\
**Post date:** [January 8, 2019, 6:45am UTC](https://root-forum.cern.ch/t/how-to-convert-root-graphs-into-dat-format/32184/7 "2019-01-08T06:45:49Z")

</div>

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

```auto
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

---

<div class="post-metadata">

**Author:** ![pamputt](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pamputt/32/12526_2.png) [@pamputt](https://root-forum.cern.ch/u/pamputt)\
**Post date:** [January 8, 2019, 7:52am UTC](https://root-forum.cern.ch/t/how-to-convert-root-graphs-into-dat-format/32184/8 "2019-01-08T07:52:36Z")

</div>

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

```auto
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

```

---

<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:** [January 8, 2019, 8:39am UTC](https://root-forum.cern.ch/t/how-to-convert-root-graphs-into-dat-format/32184/9 "2019-01-08T08:39:03Z")

</div>

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](https://godoc.org/go-hep.org/x/hep/groot/cmd/root-dump) to print – in ASCII art – the content of histograms (1D and 2D):

```auto
$> 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)
BEGIN YODA_HISTO1D /h1d-var
Path=/h1d-var
Title=h1d-var
Type=Histo1D
# 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
END YODA_HISTO1D

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

```

`root-dump` converts `TH{1,2}x` histograms to [YODA](https://yoda.hepforge.org/) 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…

hth,  
-s

---

<div class="post-metadata">

**Author:** ![Aqsa\_Shaikh](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@Aqsa\_Shaikh](https://root-forum.cern.ch/u/Aqsa_Shaikh)\
**Post date:** [January 8, 2019, 9:27am UTC](https://root-forum.cern.ch/t/how-to-convert-root-graphs-into-dat-format/32184/10 "2019-01-08T09:27:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Aqsa\_Shaikh](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@Aqsa\_Shaikh](https://root-forum.cern.ch/u/Aqsa_Shaikh)\
**Post date:** [January 8, 2019, 9:30am UTC](https://root-forum.cern.ch/t/how-to-convert-root-graphs-into-dat-format/32184/11 "2019-01-08T09:30:26Z")

</div>

> [@sbinet](#):
>
> 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…

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

> [@sbinet](#):
>
> one can use [root-dump](https://godoc.org/go-hep.org/x/hep/groot/cmd/root-dump) to print – in ASCII art – the content of histograms (1D and 2D):

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

---

<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:** [January 8, 2019, 9:59am UTC](https://root-forum.cern.ch/t/how-to-convert-root-graphs-into-dat-format/32184/12 "2019-01-08T09:59:01Z")

</div>

> [@Aqsa\_Shaikh](#):
>
> > one can use [root-dump](https://godoc.org/go-hep.org/x/hep/groot/cmd/root-dump) to print – in ASCII art – the content of histograms (1D and 2D):
> 
> If possible could you step by step tell me how to do this?

`root-dump` is a [Go](https://golang.org) program.  
once you’ve installed `Go` (see: [Download and install - The Go Programming Language](https://golang.org/doc/install)), you just have to do:

```auto
$> go get go-hep.org/x/hep/groot/cmd/root-dump

```

and you’re good to go.

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

---

<div class="post-metadata">

**Author:** ![pamputt](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pamputt/32/12526_2.png) [@pamputt](https://root-forum.cern.ch/u/pamputt)\
**Post date:** [January 8, 2019, 9:59am UTC](https://root-forum.cern.ch/t/how-to-convert-root-graphs-into-dat-format/32184/13 "2019-01-08T09:59:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Aqsa\_Shaikh](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@Aqsa\_Shaikh](https://root-forum.cern.ch/u/Aqsa_Shaikh)\
**Post date:** [January 8, 2019, 10:07am UTC](https://root-forum.cern.ch/t/how-to-convert-root-graphs-into-dat-format/32184/14 "2019-01-08T10:07:15Z")

</div>

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

```auto
https://root-forum.cern.ch/t/error-while-running-a-macro-to-view-and-convert-root-data-into-text-file/32195

```

---

<div class="post-metadata">

**Author:** ![Aqsa\_Shaikh](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/50afbb/32.png) [@Aqsa\_Shaikh](https://root-forum.cern.ch/u/Aqsa_Shaikh)\
**Post date:** [January 8, 2019, 10:08am UTC](https://root-forum.cern.ch/t/how-to-convert-root-graphs-into-dat-format/32184/15 "2019-01-08T10:08:20Z")

</div>

> [@sbinet](#):
>
> that said, it won’t be faster than what @pamputt advised…

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