# Convert root file to txt, csv file

**URL:** <https://root-forum.cern.ch/t/convert-root-file-to-txt-csv-file/58729>\
**Category:** Newbie\
**Created:** [March 26, 2024, 7:57am UTC](https://root-forum.cern.ch/t/convert-root-file-to-txt-csv-file/58729 "2024-03-26T07:57:58Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![TianYi](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/57b2e6/32.png) [@TianYi](https://root-forum.cern.ch/u/TianYi)\
**Post date:** [March 26, 2024, 7:57am UTC](https://root-forum.cern.ch/t/convert-root-file-to-txt-csv-file/58729/1 "2024-03-26T07:57:58Z")

</div>

Hi all, I would like to convert the root file to a more intuitive way of storing data, such as txt, csv file format, can anyone solve this problem, thank you very much (Linux, Ubuntu 16.04, ROOT 6.10/02)

---

<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:** [March 26, 2024, 8:00am UTC](https://root-forum.cern.ch/t/convert-root-file-to-txt-csv-file/58729/2 "2024-03-26T08:00:28Z")

</div>

Hi,

Welcome to the ROOT community!

I do not think ROOT provides something out of the box to achieve that, but it should be enough to read your binary ROOT files in Python or C++ to then create the ASCII file in the format you see fit.  
I just feel compelled to write that, while you can easily achieve human readable files, you will unavoidably end up with an I/O system which is far less efficient than the one natively provided by ROOT.

Best,  
Danilo

---

<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:** [March 26, 2024, 8:10am UTC](https://root-forum.cern.ch/t/convert-root-file-to-txt-csv-file/58729/3 "2024-03-26T08:10:22Z")

</div>

You can easily get a readable text version of some ROOT objects using dedicated methods like `Print()` (for histograms and graph) or `Scan()` for trees. This is useful to visually check the data but, as Danilo said, not recommended for storage (specially histograms and tress).

---

<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:** [March 26, 2024, 9:51am UTC](https://root-forum.cern.ch/t/convert-root-file-to-txt-csv-file/58729/4 "2024-03-26T09:51:48Z")

</div>

some (loosely related) ideas:

- rootls
- [GitHub - ivankp/rootbr: Command-line utility for inspecting contents of CERN ROOT files.](https://github.com/ivankp/rootbr)
- [TBrowser alternative for non-X11 mode](https://root-forum.cern.ch/t/tbrowser-alternative-for-non-x11-mode/20754)
- [TGraph SaveAs .csv · Issue #9491 · root-project/root · GitHub](https://github.com/root-project/root/issues/9491)
- [THistSaveAs txt or csv · Issue #10474 · root-project/root · GitHub](https://github.com/root-project/root/issues/10474)

---

<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:** [April 6, 2024, 6:48pm UTC](https://root-forum.cern.ch/t/convert-root-file-to-txt-csv-file/58729/5 "2024-04-06T18:48:20Z")

</div>

there’s a simple `root2csv` command in the `groot` package (a [Go](https://go.dev)-based set of libraries and commands).

see:

- [root2csv command - go-hep.org/x/hep/cmd/root2csv - Go Packages](https://pkg.go.dev/go-hep.org/x/hep/cmd/root2csv)
- [https://go-hep.org/dist/latest/](https://go-hep.org/dist/latest/)

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [April 12, 2024, 6:59am UTC](https://root-forum.cern.ch/t/convert-root-file-to-txt-csv-file/58729/6 "2024-04-12T06:59:23Z")

</div>

See also

[https://root.cern/doc/master/df026\_\_AsNumpyArrays\_8py.html](https://root.cern/doc/master/df026__AsNumpyArrays_8py.html)

```python
import ROOT

df = ROOT.RDataFrame("mytree", "myfile.root")
npy_arrays = df.AsNumpy()

```

You can then easily output any type of trivial data format from the numpy arrays.

Also,

> (Linux, Ubuntu 16.04, ROOT 6.10/02)

This is an ancient system and ROOT version, I highly suggest you update.

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![Dipanuta\_Behera](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dipanuta_behera/32/36558_2.png) [@Dipanuta\_Behera](https://root-forum.cern.ch/u/Dipanuta_Behera)\
**Post date:** [February 24, 2025, 4:38am UTC](https://root-forum.cern.ch/t/convert-root-file-to-txt-csv-file/58729/7 "2025-02-24T04:38:48Z")

</div>

Can anybody help me out how to form a TFrame ? such that to save the data in ascii ?  
Till now I have .root file which contains a Photon spectrum graph .But I am unable to convert .root file to .ascii or .csv or .dat file .

---

<div class="post-metadata">

**Author:** ![Dipanuta\_Behera](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dipanuta_behera/32/36558_2.png) [@Dipanuta\_Behera](https://root-forum.cern.ch/u/Dipanuta_Behera)\
**Post date:** [February 24, 2025, 4:58am UTC](https://root-forum.cern.ch/t/convert-root-file-to-txt-csv-file/58729/8 "2025-02-24T04:58:46Z")

</div>

“Because a canvas is a ROOT object, it can be saved into a ROOT file as any other ROOT object (or user class inheriting from the common base ROOT class). However, it is also possible to automatically generate and save the C++ code that would produce the same result.” This is from ROOT Manual . So how to generate c++ code automatically ?

---

<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 24, 2025, 5:18am UTC](https://root-forum.cern.ch/t/convert-root-file-to-txt-csv-file/58729/9 "2025-02-24T05:18:18Z")

</div>

On the canvas window, just click on the menu File → Save As… and give it a name with a `.C` extension (e.g. “myfile.C”); or with File → Save and choose the one with .C (in this case the name is taken from the name of the TCanvas); or, in code, `c->SaveAs("myfile.C");` (where `c` is the pointer to the canvas).

---

<div class="post-metadata">

**Author:** ![Dipanuta\_Behera](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dipanuta_behera/32/36558_2.png) [@Dipanuta\_Behera](https://root-forum.cern.ch/u/Dipanuta_Behera)\
**Post date:** [February 24, 2025, 5:40am UTC](https://root-forum.cern.ch/t/convert-root-file-to-txt-csv-file/58729/10 "2025-02-24T05:40:53Z")

</div>

Thanks @dastudillo , I saved the .C file successfully . So now can you suggest how to proceed further to get a .csv or .dat or .ascii file to get the data of the graph ?

---

<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 24, 2025, 5:54am UTC](https://root-forum.cern.ch/t/convert-root-file-to-txt-csv-file/58729/11 "2025-02-24T05:54:05Z")

</div>

[https://root.cern/doc/master/classTGraph.html#a88e093c3929ce11977b9e3d085e23793](https://root.cern/doc/master/classTGraph.html#a88e093c3929ce11977b9e3d085e23793)  
e.g.

```auto
gr->SaveAs("mygraphdata.csv");

```

---

<div class="post-metadata">

**Author:** ![Dipanuta\_Behera](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dipanuta_behera/32/36558_2.png) [@Dipanuta\_Behera](https://root-forum.cern.ch/u/Dipanuta_Behera)\
**Post date:** [February 24, 2025, 8:23am UTC](https://root-forum.cern.ch/t/convert-root-file-to-txt-csv-file/58729/12 "2025-02-24T08:23:51Z")

</div>

Thanks @dastudillo .  
Do i need to add this in same file .C ?

void TGraph::SaveAs ( const char \* filename = graph,  
Option\_t \* option = ) const

Though I tried and error was prompted in terminal.

error :  
In file included from input\_line\_51:1:  
/home/dipa/root/bin/Canvas\_1.C:152:17: error: definition or redeclaration of ‘SaveAs’ not allowed inside a function  
void TGraph::SaveAs ( const char \* root = “graph”,  
~~~~~~~~ ^  
/home/dipa/root/bin/Canvas\_1.C:153:26: error: expected ‘;’ at end of declaration  
Option\_t \* bin = “,” )  
^  
;  
How to resolve this ?

Thanks

---

<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 25, 2025, 5:27am UTC](https://root-forum.cern.ch/t/convert-root-file-to-txt-csv-file/58729/13 "2025-02-25T05:27:37Z")

</div>

I gave you above the only line you need to add.  
Full example:

```auto
 {
  TCanvas *c1 = new TCanvas("c1","c1",500,400);

  const Int_t n = 20;
  Double_t x[n], y[n];
  for (Int_t i=0;i<n;i++) {
    x[i] = i*0.1;
    y[i] = 10*sin(x[i]+0.2);
    printf(" i %i %f %f \n",i,x[i],y[i]);
  }
  TGraph *gr1 = new TGraph(n,x,y);
  gr1->SetMarkerStyle(21);
  gr1->SetTitle("Graph");
  gr1->GetXaxis()->SetTitle("X title");
  gr1->GetYaxis()->SetTitle("Y title");

  gr1->Draw("ACP");
  gr1->SaveAs("graph_data.csv");
}

```

and compare the screen printout from this example to the file `graph_data.csv`

---

<div class="post-metadata">

**Author:** ![Dipanuta\_Behera](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dipanuta_behera/32/36558_2.png) [@Dipanuta\_Behera](https://root-forum.cern.ch/u/Dipanuta_Behera)\
**Post date:** [February 27, 2025, 4:35am UTC](https://root-forum.cern.ch/t/convert-root-file-to-txt-csv-file/58729/14 "2025-02-27T04:35:27Z")

</div>

Thank you @dastudillo for the help.  
The above example helped to resolve my program errors .Thank you .
