# How to convert .root to .txt file?

**URL:** <https://root-forum.cern.ch/t/how-to-convert-root-to-txt-file/30534>\
**Category:** ROOT\
**Created:** [September 4, 2018, 5:22am UTC](https://root-forum.cern.ch/t/how-to-convert-root-to-txt-file/30534 "2018-09-04T05:22:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Donghyeon\_Lee](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/d/b2d939/32.png) [@Donghyeon\_Lee](https://root-forum.cern.ch/u/Donghyeon_Lee)\
**Post date:** [September 4, 2018, 5:22am UTC](https://root-forum.cern.ch/t/how-to-convert-root-to-txt-file/30534/1 "2018-09-04T05:22:39Z")

</div>

* * *

_ROOT Version:_ 6.10  
_Platform:_ Ubuntu 16.04  
_Compiler:_ Not Provided

* * *

I got some ‘dump.root’ file and want to change the information to the text file.  
The program I used, which is ‘GATE’, recommend me to use the commands in the followings:

///////////////////////////////////////////////////////////////////////////////////

```cpp
//
// Name this file "dump.cxx" and use as:
//
// root [0] .x dump.cxx(); > dump.txt
//
// Produces "dump.txt" and "dump.xml" files.
//

void dump(const char *fname = "dump.root",
          const char *nname = "ntuple")
{
  if (!fname || !(*fname) || !nname || !(*nname)) return; // just a precaution
  
  TFile *f = TFile::Open(fname, "READ");
  if (!f || f->IsZombie()) { delete f; return; } // just a precaution
  
  TTree *t; f->GetObject(nname, t);
  if (!t) { delete f; return; } // just a precaution
  
  t->SetScanField(0);
  t->Scan("*");
  t->SaveAs("dump.xml");
  
  delete f; // no longer needed (automatically deletes "t")
}

```

//////////////////////////////////////////////////////////////////////////////////////

It DIDN’T work and got some errors.

And I checked the similar question asked by someone before. But I couldn’t solve.

the similar question link:

> [@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/2):
>
> // // Name this file "dump.cxx" and use as: // // root [0] .x dump.cxx(); \> dump.txt // // Produces "dump.txt" and "dump.xml" files. // void dump(const char \*fname = "dna.root", const char \*nname = "ntuple") { if (!fname || !(\*fname) || !nname || !(\*nname)) return; // just a precaution TFile \*f = TFile::Open(fname, "READ"); if ((!f) || f-\>IsZombie()) { delete f; return; } // just a precaution TTree \*t; f-\>GetObject(nname, t); if (!t) { delete f; return; } // just a pre…

the error I got

 ![error1](https://root-forum.cern.ch/uploads/default/original/3X/f/a/fa310a4d805c57e035970ebaf680fa5d1eaa7365.jpeg)

What is the problem??

I sincerely appreciate your attention.

---

<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:** [September 4, 2018, 6:13am UTC](https://root-forum.cern.ch/t/how-to-convert-root-to-txt-file/30534/2 "2018-09-04T06:13:04Z")

</div>

Do you mean to convert root graphs data into .dat/.txt file?

If yes then I do the following.

I use the following codes (attached tar file) to convert my histograms in .dat format

Steps:

1. $ tar xvzf browser.tar.gz
2. $ cd browser
3. $ make

This will make an executable named general. Copy the executable in the directory where you have the root file  
4) $ ./general

Another window will open. in that window do the following,  
5) Use the browser button to locate, open and draw the histogram.  
6) Use ASCII button to create the ascii dat (graph.dat)

[browser\_ubuntu.tar.gz](https://root-forum.cern.ch/uploads/default/original/3X/d/3/d3bd41e13d9afbc7c20ea1b4d394818bacdd19e1.gz) (6.1 KB)

---

<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:** [September 4, 2018, 7:43am UTC](https://root-forum.cern.ch/t/how-to-convert-root-to-txt-file/30534/3 "2018-09-04T07:43:47Z")

</div>

alternatively, you could use [go-hep.org/x/hep/rootio/cmd/root-dump](https://go-hep.org/x/hep/rootio/cmd/root-dump), from [Go-HEP](https://go-hep.org).

examples:

```auto
$> root-ls graphs.root 
=== [graphs.root] ===
version: 60806
TGraph tg graph without errors (cycle=1)
TGraphErrors tge graph with errors (cycle=1)
TGraphAsymmErrors tgae graph with asymmetric errors (cycle=1)

$> root-dump graphs.root
>>> file[graphs.root]
key[000]: tg;1 "graph without errors" (TGraph)
BEGIN YODA_SCATTER2D /tg
Path=/tg
Title=graph without errors
Type=Scatter2D
# xval xerr- xerr+ yval yerr- yerr+
1.000000e+00	0.000000e+00	0.000000e+00	2.000000e+00	0.000000e+00	0.000000e+00
2.000000e+00	0.000000e+00	0.000000e+00	4.000000e+00	0.000000e+00	0.000000e+00
3.000000e+00	0.000000e+00	0.000000e+00	6.000000e+00	0.000000e+00	0.000000e+00
4.000000e+00	0.000000e+00	0.000000e+00	8.000000e+00	0.000000e+00	0.000000e+00
END YODA_SCATTER2D

key[001]: tge;1 "graph with errors" (TGraphErrors)
BEGIN YODA_SCATTER2D /tge
[...]

$> 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 gauss-h1.root
>>> file[gauss-h1.root]
key[000]: h1d;1 "h1d" (TH1D)
BEGIN YODA_HISTO1D /h1d
Path=/h1d
Title=h1d
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.260000e+01	7.986000e+01	0.000000e+00	0.000000e+00	66
-2.400000e+00	-1.600000e+00	5.434000e+02	5.977400e+02	0.000000e+00	0.000000e+00	494
-1.600000e+00	-8.000000e-01	1.708300e+03	1.879130e+03	0.000000e+00	0.000000e+00	1553
-8.000000e-01	2.220446e-16	3.130600e+03	3.443660e+03	0.000000e+00	0.000000e+00	2846
0.000000e+00	8.000000e-01	3.136100e+03	3.449710e+03	0.000000e+00	0.000000e+00	2851
8.000000e-01	1.600000e+00	1.753400e+03	1.928740e+03	0.000000e+00	0.000000e+00	1594
1.600000e+00	2.400000e+00	5.401000e+02	5.941100e+02	0.000000e+00	0.000000e+00	491
2.400000e+00	3.200000e+00	1.012000e+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[001]: h1f;1 "h1f" (TH1F)
[...]

```

it should work with most of `TTrees` as well.

you can install it (once you have [Go](https://golang.org/doc/install)) like so:

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

```

otherwise, I have compiled it for a couple of OS/Arch here:

- [https://cern.ch/binet/go-hep/rootio/root-dump-darwin-amd64.exe](https://cern.ch/binet/go-hep/rootio/root-dump-darwin-amd64.exe)
- [https://cern.ch/binet/go-hep/rootio/root-dump-linux-amd64.exe](https://cern.ch/binet/go-hep/rootio/root-dump-linux-amd64.exe)
- [https://cern.ch/binet/go-hep/rootio/root-dump-windows-amd64.exe](https://cern.ch/binet/go-hep/rootio/root-dump-windows-amd64.exe)

hth,  
-s

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [September 4, 2018, 7:35pm UTC](https://root-forum.cern.ch/t/how-to-convert-root-to-txt-file/30534/4 "2018-09-04T19:35:50Z")

</div>

> [@Donghyeon\_Lee](#):
>
> It DIDN’T work and got some errors.

hummm … in ROOT 6, the usage of this script should be:

```auto
.> dump.txt
.x dump.cxx
.>

```

The `.x dump.cxx(); > dump.txt` part is a retired interface supported only in ROOT 5 and older.

Alternative use the following script:

```auto
// Name this file "dump.cxx" and use as:
//
// root [0] .x dump.cxx("dump.txt")
//
// Produces "dump.txt" and "dump.xml" files.
//

void dump(const char *outputname = "dump.txt",
          const char *fname = "dump.root",
          const char *nname = "ntuple")
{
  if (!outputname || !(*outputname) || !fname || !(*fname) || !nname || !(*nname)) return; // just a precaution
  
  TFile *f = TFile::Open(fname, "READ");
  if (!f || f->IsZombie()) { delete f; return; } // just a precaution
  
  TTree *t; f->GetObject(nname, t);
  if (!t) { delete f; return; } // just a precaution
  
  t->SetScanField(0);
  ((TTreePlayer*)t->GetPlayer())->SetScanRedirect(kTRUE);
  ((TTreePlayer*)t->GetPlayer())->SetScanFileName(outputname);
  t->Scan("*");
  t->SaveAs("dump.xml");
  
  delete f; // no longer needed (automatically deletes "t")
}

```

Cheers,  
Philippe.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [September 18, 2018, 7:38pm UTC](https://root-forum.cern.ch/t/how-to-convert-root-to-txt-file/30534/5 "2018-09-18T19:38:20Z")

</div>

Closed on September 18, 2018, 7:38pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
