# ROOT file -\> txt format

**URL:** <https://root-forum.cern.ch/t/root-file-txt-format/48959>\
**Category:** ROOT\
**Created:** [February 26, 2022, 8:15pm UTC](https://root-forum.cern.ch/t/root-file-txt-format/48959 "2022-02-26T20:15:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Spring\_127](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ac8455/32.png) [@Spring\_127](https://root-forum.cern.ch/u/Spring_127)\
**Post date:** [February 26, 2022, 8:15pm UTC](https://root-forum.cern.ch/t/root-file-txt-format/48959/1 "2022-02-26T20:15:06Z")

</div>

Hi,

I want to convert root file into text file,  
Does anyone know how to convert a ROOT file to text format?

Thanks a lot

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [February 27, 2022, 4:37am UTC](https://root-forum.cern.ch/t/root-file-txt-format/48959/2 "2022-02-27T04:37:36Z")

</div>

A ROOT file can contain many types of objects, so you have to be more specific on what you want to export. In any case, there are many examples on this forum, just search for the specific thing you want to export, e.g. [Search results for 'export histogram to text' - ROOT Forum](https://root-forum.cern.ch/search?q=export%20histogram%20to%20text)

---

<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:** [February 27, 2022, 7:33am UTC](https://root-forum.cern.ch/t/root-file-txt-format/48959/3 "2022-02-27T07:33:31Z")

</div>

depending on what the ROOT file contains, `root2csv`, `root2yoda` or `root-dump` may be suitable:

- [https://go-hep.org/x/hep/cmd/root2csv](https://go-hep.org/x/hep/cmd/root2csv)
- [https://go-hep.org/x/hep/cmd/root2yoda](https://go-hep.org/x/hep/cmd/root2yoda)
- [https://go-hep.org/x/hep/groot/cmd/root-dump](https://go-hep.org/x/hep/groot/cmd/root-dump)

`root2csv` is suitable for converting simple ROOT TTrees into CSV data.  
`root2yoda` can convert TH{1,2} histograms into their [YODA](https://yoda.hepforge.org/) form.  
`root-dump` can display almost every thing into a somewhat human format.

these commands are available from the [Go-HEP](https://go-hep.org) project:

- [https://go-hep.org/dist/latest](https://go-hep.org/dist/latest)

hth,  
-s

---

<div class="post-metadata">

**Author:** ![Spring\_127](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ac8455/32.png) [@Spring\_127](https://root-forum.cern.ch/u/Spring_127)\
**Post date:** [February 27, 2022, 10:16am UTC](https://root-forum.cern.ch/t/root-file-txt-format/48959/4 "2022-02-27T10:16:56Z")

</div>

Hi,  
Thank you for your reply. I want to convert the TTree root file as Edep and interaction coordinates in the root file as ntuples and histograms to an .txt or .Ascii file.  
Thanks alot for help

Spring

---

<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:** [February 28, 2022, 2:32pm UTC](https://root-forum.cern.ch/t/root-file-txt-format/48959/5 "2022-02-28T14:32:24Z")

</div>

You’re generally better off using the ROOT file format rather than ASCII text which another tool then needs to read again… If you “insist” in doing this you can simply use `tree->Scan()` and redirect the output, or write a little script that prints the column values that you care about.

@eguiraud that mightbe a nice feature for RDataFrame, too: `Print()` printing all or the given column values, maybe for every n-th entry, or the first n entries. Especially with `Define()` etc this could be nice!

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [February 28, 2022, 2:36pm UTC](https://root-forum.cern.ch/t/root-file-txt-format/48959/6 "2022-02-28T14:36:56Z")

</div>

> [@Axel](#):
>
> that mightbe a nice feature for RDataFrame, too: `Print()` printing all or the given column values, maybe for every n-th entry, or the first n entries.

Done! That’s [Display](https://root.cern/doc/master/classROOT_1_1RDF_1_1RInterface.html#a7fb6ccf99d69bee7c519dcfe25bfa16b) 🙂

---

<div class="post-metadata">

**Author:** ![Spring\_127](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ac8455/32.png) [@Spring\_127](https://root-forum.cern.ch/u/Spring_127)\
**Post date:** [March 2, 2022, 8:44am UTC](https://root-forum.cern.ch/t/root-file-txt-format/48959/7 "2022-03-02T08:44:00Z")

</div>

Thanks for the reply, I need a script to convert all root data to a text file to convert to txt format

---

<div class="post-metadata">

**Author:** ![Spring\_127](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ac8455/32.png) [@Spring\_127](https://root-forum.cern.ch/u/Spring_127)\
**Post date:** [March 2, 2022, 9:48am UTC](https://root-forum.cern.ch/t/root-file-txt-format/48959/8 "2022-03-02T09:48:33Z")

</div>

I ran the following script in ROOT 6,

.\> dump.txt  
.x dump.cxx  
.\>

But its txt output was empty, it only contained the following:

[0mroot [2] .x dump.cxx  
[0m [0mroot [3]

I do not know why it creates the .txt file but it does not write the data of the root file, can anyone help?

++++++++++++++++++++++++

//  
// Products “dump.txt” and “dump.xml” files.  
//

void dump (const char \* outputname = “dump.txt”,  
const char \* fname = “output.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”)  
}

+++++++++++++++++++++++

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 2, 2022, 10:11am UTC](https://root-forum.cern.ch/t/root-file-txt-format/48959/9 "2022-03-02T10:11:37Z")

</div>

First, list the contents of your file:  
`[...]$ rootls -l yourfile.root`

---

<div class="post-metadata">

**Author:** ![Spring\_127](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ac8455/32.png) [@Spring\_127](https://root-forum.cern.ch/u/Spring_127)\
**Post date:** [March 2, 2022, 10:34am UTC](https://root-forum.cern.ch/t/root-file-txt-format/48959/10 "2022-03-02T10:34:05Z")

</div>

rootls -l output.root

I typed in the terminal showed the following error:

Traceback (most recent call last):  
File “/ usr / local / bin / rootls”, line 10, in   
import cmdLineUtils  
ImportError: No module named cmdLineUtils

Did not display list the contents of my file.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 2, 2022, 10:38am UTC](https://root-forum.cern.ch/t/root-file-txt-format/48959/11 "2022-03-02T10:38:03Z")

</div>

Then try:  
`[...]$ root -b -l -q yourfile.root -e 'gFile->ls();'`

---

<div class="post-metadata">

**Author:** ![Spring\_127](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ac8455/32.png) [@Spring\_127](https://root-forum.cern.ch/u/Spring_127)\
**Post date:** [March 2, 2022, 10:52am UTC](https://root-forum.cern.ch/t/root-file-txt-format/48959/12 "2022-03-02T10:52:55Z")

</div>

Thank you very much for your reply, it is now showing the content:

Attaching file output\_t0.root as \_file0 …  
(TFile \*) 0x55eb9649e640  
TFile \*\* output\_t0.root  
TFile \* output\_t0.root  
KEY: TDirectoryFile ntuple; 1 ntuple

Now how do I put this content in a txt file?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 2, 2022, 10:59am UTC](https://root-forum.cern.ch/t/root-file-txt-format/48959/14 "2022-03-02T10:59:34Z")

</div>

Try:  
`[...]$ root -b -l -q output_t0.root -e 'gFile->cd("ntuple"); gDirectory->ls();'`

---

<div class="post-metadata">

**Author:** ![Spring\_127](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ac8455/32.png) [@Spring\_127](https://root-forum.cern.ch/u/Spring_127)\
**Post date:** [March 2, 2022, 11:02am UTC](https://root-forum.cern.ch/t/root-file-txt-format/48959/15 "2022-03-02T11:02:52Z")

</div>

Attaching file output\_t0.root as \_file0…  
(TFile _) 0x55f8915f8650  
TDirectoryFile_ ntuple ntuple  
KEY: TTree ntuple\_1;1 physical\_stage  
KEY: TTree ntuple\_2;1 chem\_stage

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 2, 2022, 11:06am UTC](https://root-forum.cern.ch/t/root-file-txt-format/48959/16 "2022-03-02T11:06:00Z")

</div>

Try (creates “`dump.txt`” and “`dump.xml`”):  
`[...]$ root -b -l -q 'dump.cxx("dump.txt", "output_t0.root", "ntuple/ntuple_1")'`

---

<div class="post-metadata">

**Author:** ![Spring\_127](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ac8455/32.png) [@Spring\_127](https://root-forum.cern.ch/u/Spring_127)\
**Post date:** [March 2, 2022, 11:36am UTC](https://root-forum.cern.ch/t/root-file-txt-format/48959/17 "2022-03-02T11:36:52Z")

</div>

Processing dump.cxx(“dump.txt”, “output\_t0.root”, “ntuple/ntuple\_1”)…  
File \<dump.txt\> created  
Error in TBranch::TBranch::WriteBasketImpl: basket’s WriteBuffer failed.

Error in TBranch::TBranch::WriteBasketImpl: basket’s WriteBuffer failed.

Error in TBranch::TBranch::WriteBasketImpl: basket’s WriteBuffer failed.

Error in TBranch::TBranch::WriteBasketImpl: basket’s WriteBuffer failed.

Error in TBranch::TBranch::WriteBasketImpl: basket’s WriteBuffer failed.

Error in TBranch::TBranch::WriteBasketImpl: basket’s WriteBuffer failed.

Error in TBranch::TBranch::WriteBasketImpl: basket’s WriteBuffer failed.

Error in TBranch::TBranch::WriteBasketImpl: basket’s WriteBuffer failed.

Info in TTree::SaveAs: ROOT file dump.xml has been created

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 2, 2022, 11:39am UTC](https://root-forum.cern.ch/t/root-file-txt-format/48959/18 "2022-03-02T11:39:50Z")

</div>

I have no idea where these “`basket's WriteBuffer failed`” errors come from (maybe @pcanal could guess).  
But, at least, you now have some “`dump.txt`” and “`dump.xml`” to play with.

---

<div class="post-metadata">

**Author:** ![Spring\_127](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/ac8455/32.png) [@Spring\_127](https://root-forum.cern.ch/u/Spring_127)\
**Post date:** [March 2, 2022, 12:31pm UTC](https://root-forum.cern.ch/t/root-file-txt-format/48959/19 "2022-03-02T12:31:46Z")

</div>

Thank you very much for your valuable guidance, the physical stage in ntuple1 contains 5 branches, do you think this error is not because ntuple 1 branches are not considered?

---

<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:** [March 2, 2022, 1:52pm UTC](https://root-forum.cern.ch/t/root-file-txt-format/48959/20 "2022-03-02T13:52:42Z")

</div>

> [@Spring\_127](#):
>
> t-\> SaveAs (“dump.xml”);

This (writing a TTree directly in xml format) is not supported.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 2, 2022, 1:56pm UTC](https://root-forum.cern.ch/t/root-file-txt-format/48959/21 "2022-03-02T13:56:27Z")

</div>

@pcanal [TObject::SaveAs](https://root.cern/doc/master/classTObject.html#ab15fdcb1ba88f591bfdaec8b0c512ff5) should explicitly say “not supported” instead of issuing mysterious errors.

[Next page](https://root-forum.cern.ch/t/root-file-txt-format/48959.md?page=2)
