# How can I convert .root file to .txt file?

**URL:** <https://root-forum.cern.ch/t/how-can-i-convert-root-file-to-txt-file/21371>\
**Category:** ROOT\
**Created:** [June 17, 2016, 7:31pm UTC](https://root-forum.cern.ch/t/how-can-i-convert-root-file-to-txt-file/21371 "2016-06-17T19:31:28Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashok.tiwari](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/bc79bd/32.png) [@ashok.tiwari](https://root-forum.cern.ch/u/ashok.tiwari)\
**Post date:** [June 17, 2016, 7:31pm UTC](https://root-forum.cern.ch/t/how-can-i-convert-root-file-to-txt-file/21371/1 "2016-06-17T19:31:28Z")

</div>

😃 Help required! I need help to convert a root file into text file.

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [June 17, 2016, 8:07pm UTC](https://root-forum.cern.ch/t/how-can-i-convert-root-file-to-txt-file/21371/2 "2016-06-17T20:07:37Z")

</div>

Hi,

this depends very much on the content of the rootfile. In general I would not encourage at all this step as you would loose several benefits such as runtime performance and data compression.

Danilo

---

<div class="post-metadata">

**Author:** ![ashok.tiwari](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/bc79bd/32.png) [@ashok.tiwari](https://root-forum.cern.ch/u/ashok.tiwari)\
**Post date:** [June 18, 2016, 12:07am UTC](https://root-forum.cern.ch/t/how-can-i-convert-root-file-to-txt-file/21371/3 "2016-06-18T00:07:21Z")

</div>

[quote=“dpiparo”]Hi,

this depends very much on the content of the rootfile. In general I would not encourage at all this step as you would loose several benefits such as runtime performance and data compression.

Danilo[/quote]

Hi Danilo,

Thank you for your reply. I have for example this (attached, results.root) rootfile, what is the way to convert into text file? I hope you will help me.  
[results.root](https://root-forum.cern.ch/uploads/default/original/2X/f/fff976a8b4ef35d042109196804e8a3535ef3744.root) (53.9 KB)

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [June 18, 2016, 9:06am UTC](https://root-forum.cern.ch/t/how-can-i-convert-root-file-to-txt-file/21371/4 "2016-06-18T09:06:33Z")

</div>

Hi,

so you would like to transform a tree created from a txt file (“novaCosmogenic data from given txt file”) in a txt file.  
I still don’t think it’s a good idea, but you can transverse the tree easily like this, in Python:

```auto
f = ROOT.TFile("results.root")
for e in f.T:
    print e.evtN, e.partID, ...., e.pz
```

Danilo

---

<div class="post-metadata">

**Author:** ![ashok.tiwari](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/bc79bd/32.png) [@ashok.tiwari](https://root-forum.cern.ch/u/ashok.tiwari)\
**Post date:** [June 20, 2016, 2:14pm UTC](https://root-forum.cern.ch/t/how-can-i-convert-root-file-to-txt-file/21371/5 "2016-06-20T14:14:18Z")

</div>

[quote=“dpiparo”]Hi,

so you would like to transform a tree created from a txt file (“novaCosmogenic data from given txt file”) in a txt file.  
I still don’t think it’s a good idea, but you can transverse the tree easily like this, in Python:

```auto
f = ROOT.TFile("results.root")
for e in f.T:
    print e.evtN, e.partID, ...., e.pz
```

Danilo[/quote]

Hello,

Is it possible to convert this using ROOT? I am asking because I don’t know about Python.

Ashok

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [June 20, 2016, 2:18pm UTC](https://root-forum.cern.ch/t/how-can-i-convert-root-file-to-txt-file/21371/6 "2016-06-20T14:18:29Z")

</div>

Hi,

you can have a look to the TTreeReader tutorial to see how one reads a tree in ROOT in C++: [root.cern.ch/doc/master/hsimpleReader\_8C.html](https://root.cern.ch/doc/master/hsimpleReader_8C.html)

Danilo

---

<div class="post-metadata">

**Author:** ![ashok.tiwari](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/bc79bd/32.png) [@ashok.tiwari](https://root-forum.cern.ch/u/ashok.tiwari)\
**Post date:** [June 20, 2016, 2:29pm UTC](https://root-forum.cern.ch/t/how-can-i-convert-root-file-to-txt-file/21371/7 "2016-06-20T14:29:29Z")

</div>

Hello,

I know we can read data using ROOT but I am interested to convert rootfile into .txt file, so that we can handle that text file using others program too. I need to provide this data to those who don’t know the ROOT and C++.

Ashok

---

<div class="post-metadata">

**Author:** ![Danilo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/danilo/32/34528_2.png) [@Danilo](https://root-forum.cern.ch/u/Danilo)\
**Post date:** [June 20, 2016, 2:42pm UTC](https://root-forum.cern.ch/t/how-can-i-convert-root-file-to-txt-file/21371/8 "2016-06-20T14:42:47Z")

</div>

The tutorial shows what you asked, i.e. how you read the rootfile in C++.  
You’ll just have to print out the variables on screen.

---

<div class="post-metadata">

**Author:** ![ashok.tiwari](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/bc79bd/32.png) [@ashok.tiwari](https://root-forum.cern.ch/u/ashok.tiwari)\
**Post date:** [June 20, 2016, 2:47pm UTC](https://root-forum.cern.ch/t/how-can-i-convert-root-file-to-txt-file/21371/9 "2016-06-20T14:47:31Z")

</div>

Excellent, I got it. Thank you.

Ashok

---

<div class="post-metadata">

**Author:** ![papagaj](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/papagaj/32/19701_2.png) [@papagaj](https://root-forum.cern.ch/u/papagaj)\
**Post date:** [August 1, 2016, 1:41pm UTC](https://root-forum.cern.ch/t/how-can-i-convert-root-file-to-txt-file/21371/10 "2016-08-01T13:41:12Z")

</div>

Please, can you tell me how do I use this code to convert the root histogram into an .ascii file?  
Thank you.

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [May 19, 2022, 1:40pm UTC](https://root-forum.cern.ch/t/how-can-i-convert-root-file-to-txt-file/21371/11 "2022-05-19T13:40:52Z")

</div>

> [@papagaj](#):
>
> an you tell me how do I use this code to convert the root histogram into an .ascii file?  
> Thank you.

Related: [THistSaveAs txt or csv · Issue #10474 · root-project/root · GitHub](https://github.com/root-project/root/issues/10474)  
[root2csv command - go-hep.org/x/hep/cmd/root2csv - Go Packages](https://pkg.go.dev/go-hep.org/x/hep/cmd/root2csv)
