# ROOT tree to CSV file format

**URL:** <https://root-forum.cern.ch/t/root-tree-to-csv-file-format/27633>\
**Category:** ROOT\
**Tags:** tree\
**Created:** [January 19, 2018, 8:33am UTC](https://root-forum.cern.ch/t/root-tree-to-csv-file-format/27633 "2018-01-19T08:33:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![wellatiger](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wellatiger/32/15619_2.png) [@wellatiger](https://root-forum.cern.ch/u/wellatiger)\
**Post date:** [January 19, 2018, 8:33am UTC](https://root-forum.cern.ch/t/root-tree-to-csv-file-format/27633/1 "2018-01-19T08:33:48Z")

</div>

Hello everyone,

I want to convert a root file into .csv file format. Could someone provide the best way to go about this?.

Many Thanks,

---

<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:** [January 19, 2018, 8:57am UTC](https://root-forum.cern.ch/t/root-tree-to-csv-file-format/27633/2 "2018-01-19T08:57:34Z")

</div>

Hi,

I think there is no out-of-the-box procedure for that: you’ll have to implement the converter yourself.  
What we provide, on the other hand, is the opposite, i.e. seamless csv reading into ROOT:

- C++ example: [https://root.cern/doc/master/tdf014\_\_CSVDataSource\_8C.html](https://root.cern/doc/master/tdf014__CSVDataSource_8C.html)
- Python example: [https://root.cern/doc/master/tdf014\_\_CSVDataSource\_8py.html](https://root.cern/doc/master/tdf014__CSVDataSource_8py.html)

Cheers,  
D

---

<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 19, 2018, 10:09am UTC](https://root-forum.cern.ch/t/root-tree-to-csv-file-format/27633/3 "2018-01-19T10:09:23Z")

</div>

depending on the layout of the TTree stored in your ROOT file, `go-hep/rootio` might help do what you want.

I’ve just whipped up a little naive `root2csv` command:

- [https://godoc.org/go-hep.org/x/hep/cmd/root2csv](https://godoc.org/go-hep.org/x/hep/cmd/root2csv)

_e.g.:_

```auto
$> root-ls -t ./small-flat-tree.root 
=== [./small-flat-tree.root] ===
version: 60806
TTree tree my tree title (entries=100)
  Int32 "Int32/I" TBranch
  Int64 "Int64/L" TBranch
  UInt32 "UInt32/i" TBranch
  UInt64 "UInt64/l" TBranch
  Float32 "Float32/F" TBranch
  Float64 "Float64/D" TBranch
  Str "Str/C" TBranch
  ArrayInt32 "ArrayInt32[10]/I" TBranch
  ArrayInt64 "ArrayInt64[10]/L" TBranch
  ArrayUInt32 "ArrayInt32[10]/i" TBranch
  ArrayUInt64 "ArrayInt64[10]/l" TBranch
  ArrayFloat32 "ArrayFloat32[10]/F" TBranch
  ArrayFloat64 "ArrayFloat64[10]/D" TBranch
  N "N/I" TBranch
  SliceInt32 "SliceInt32[N]/I" TBranch
  SliceInt64 "SliceInt64[N]/L" TBranch
  SliceUInt32 "SliceInt32[N]/i" TBranch
  SliceUInt64 "SliceInt64[N]/l" TBranch
  SliceFloat32 "SliceFloat32[N]/F" TBranch
  SliceFloat64 "SliceFloat64[N]/D" TBranch

```

and now, converting it:

```auto
$> root2csv -o out.csv -t tree -f ./small-flat-tree.root
root2csv: scanning leaves...
root2csv: >>> "ArrayInt32" [10]int32 not supported
root2csv: >>> "ArrayInt64" [10]int64 not supported
root2csv: >>> "ArrayInt32" [10]int32 not supported
root2csv: >>> "ArrayInt64" [10]int64 not supported
root2csv: >>> "ArrayFloat32" [10]float32 not supported
root2csv: >>> "ArrayFloat64" [10]float64 not supported
root2csv: >>> "SliceInt32" []int32 not supported
root2csv: >>> "SliceInt64" []int64 not supported
root2csv: >>> "SliceInt32" []int32 not supported
root2csv: >>> "SliceInt64" []int64 not supported
root2csv: >>> "SliceFloat32" []float32 not supported
root2csv: >>> "SliceFloat64" []float64 not supported
root2csv: scanning leaves... [done]

$> head out.csv 
## Automatically generated from "./small-flat-tree.root"
Int32;Int64;UInt32;UInt64;Float32;Float64;Str;N
0;0;0;0;0;0;evt-000;0
1;1;1;1;1;1;evt-001;1
2;2;2;2;2;2;evt-002;2
3;3;3;3;3;3;evt-003;3
4;4;4;4;4;4;evt-004;4
5;5;5;5;5;5;evt-005;5
6;6;6;6;6;6;evt-006;6
7;7;7;7;7;7;evt-007;7

```

hth,  
-s

---

<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 19, 2018, 12:40pm UTC](https://root-forum.cern.ch/t/root-tree-to-csv-file-format/27633/4 "2018-01-19T12:40:27Z")

</div>

forgot to say that, if you want to test it, you either need to have [Go](https://golang.org) installed and then do:

```auto
$> go get go-hep.org/x/hep/cmd/root2csv

```

or (if you haven’t installed Go already, but really, you should as it is such a nice language :P) directly download the binary for your platform I put there:

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

---

<div class="post-metadata">

**Author:** ![wellatiger](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wellatiger/32/15619_2.png) [@wellatiger](https://root-forum.cern.ch/u/wellatiger)\
**Post date:** [January 19, 2018, 1:46pm UTC](https://root-forum.cern.ch/t/root-tree-to-csv-file-format/27633/5 "2018-01-19T13:46:07Z")

</div>

many thanks for your help

---

<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 19, 2018, 3:49pm UTC](https://root-forum.cern.ch/t/root-tree-to-csv-file-format/27633/6 "2018-01-19T15:49:28Z")

</div>

(if that was directed to me: thanks 🙂 )  
(also, feel free to bug me if you need some more features (like writing or not the CSV header, changing the default CSV-delimiter, etc…) or – god forbid! – find a bug. that’s quite possible, I really hacked it together in a few minutes)

---

<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:** [February 2, 2018, 3:49pm UTC](https://root-forum.cern.ch/t/root-tree-to-csv-file-format/27633/7 "2018-02-02T15:49:34Z")

</div>

Closed on February 2, 2018, 3:49pm UTC

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