# Converting root file to csv using root2csv

**URL:** <https://root-forum.cern.ch/t/converting-root-file-to-csv-using-root2csv/42583>\
**Category:** Newbie\
**Tags:** root\
**Created:** [December 3, 2020, 7:01pm UTC](https://root-forum.cern.ch/t/converting-root-file-to-csv-using-root2csv/42583 "2020-12-03T19:01:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hawi\_Phys](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hawi_phys/32/24182_2.png) [@Hawi\_Phys](https://root-forum.cern.ch/u/Hawi_Phys)\
**Post date:** [December 3, 2020, 7:01pm UTC](https://root-forum.cern.ch/t/converting-root-file-to-csv-using-root2csv/42583/1 "2020-12-03T19:01:05Z")

</div>

* * *

_Please read [tips for efficient and successful posting](https://root-forum.cern.ch/t/tips-for-efficient-and-successful-posting/28292) and [posting code](https://root-forum.cern.ch/t/posting-code-read-this-first/28293)_

_ROOT Version:_ Not Provided  
_Platform:_ Not Provided  
_Compiler:_ Not Provided

* * *

Hello All,

I’m having trouble running the root2csv code posted by @sbinet I’d also like to convert a root file into a csv. Assistance would be greatly appreciated.

When I try running the following command:  
root-ls -t ./

My terminal says -bash: root-ls: command not found

---

<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:** [December 3, 2020, 8:08pm UTC](https://root-forum.cern.ch/t/converting-root-file-to-csv-using-root2csv/42583/2 "2020-12-03T20:08:23Z")

</div>

you need to also retrieve/download the `root-ls` command (_e.g._ from [https://go-hep.org/dist/v0.27.0/](https://go-hep.org/dist/v0.27.0/)).

or, if you have a Go toolkit installed (see [here](https://golang.org/doc/install) for installation instructions):

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

```

hth,  
-s

---

<div class="post-metadata">

**Author:** ![Hawi\_Phys](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hawi_phys/32/24182_2.png) [@Hawi\_Phys](https://root-forum.cern.ch/u/Hawi_Phys)\
**Post date:** [December 3, 2020, 9:50pm UTC](https://root-forum.cern.ch/t/converting-root-file-to-csv-using-root2csv/42583/4 "2020-12-03T21:50:51Z")

</div>

Ok so I have a root-ls directory in my cmd and I pasted a main.go file that I got from the godoc. Now when running root-ls -t ./small-flat-tree.root from the cmd directory it says the same thing -bash: root-ls: command not found. I’ll keep trying and updating as I go along. Thanks again for the help! 🙂

Update: Whenever I run main.go from the root2csv directory it exits with:  
Usage of /var/folders/p3/pvlzcs2x4sndnpq1dvh03ldc0000gn/T/go-build053216966/b001/exe/main:  
-f string  
path to input ROOT file name  
-o string  
path to output CSV file name (default “output.csv”)  
-t string  
name of the tree to convert (default “tree”)  
root2csv: missing input ROOT filename argument  
exit status 1

The issue now would be identifying the source of the error.

---

<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:** [December 4, 2020, 9:06am UTC](https://root-forum.cern.ch/t/converting-root-file-to-csv-using-root2csv/42583/5 "2020-12-04T09:06:03Z")

</div>

ok.  
so, first a few clarifying things.

- `root-ls`, `root2csv`, … all the commands from [go-hep.org/x/hep](https://go-hep.org/x/hep) are written in [Go](https://golang.org). Go is a compiled programming language, so one needs a compiler (_e.g._ one that can be installed from [golang.org](https://golang.org)) to compile Go source code into an executable for a given operating system + architecture (_e.g.:_ `linux` + 64bits machine).

- `go get go-hep.org/x/hep/cmd/root2csv` will fetch the Go code for `root2csv`, compile it and install the resulting binary under some directory (by default `$(go env GOPATH)/bin`).

- Go has this nice property of being able to produce (by default) statically compiled binaries/executables, so a binary that has been compiled on machine A (a Linux/64b machine) can be easily copied to machine B (another Linux/64b machine) w/o needing to install a bunch of things.

so, with that out of the way, let’s try this step by step, assuming you have not installed Go and that you are on a Linux machine:

```auto
# retrieve the root-ls and root2csv binaries for Linux/64b
$> cd /some/where
$> curl -O -L https://go-hep.org/dist/v0.27.0/root-ls-linux_amd64.exe
  % Total % Received % Xferd Average Speed Time Time Time Current
                                 Dload Upload Total Spent Left Speed
100 16.9M 100 16.9M 0 0 2134k 0 0:00:08 0:00:08 --:--:-- 2447k

## rename to something shorter
$> mv root-ls-linux_amd64.exe root-ls

## make it exectubale
$> chmod +x ./root-ls

## test it
$> ./root-ls -h
Usage: root-ls [options] file1.root [file2.root [...]]

ex:
 $> root-ls ./testdata/graphs.root

options:
  -profile string
    	filename of cpuprofile
  -sinfos
    	print StreamerInfos
  -t	print Tree(s) (recursively)

```

at this point, you have a working `root-ls` executable.  
(you can do the same for the `root2csv` executable.)

and you can test it with your favorite ROOT file:

```auto
$> cd /some/where
$> ./root-ls -t /path/to/your/favorite/file.root

```

if you have a Go installation ready and correctly installed, the following would work as well:

```auto
$> go get -v go-hep.org/x/hep/groot/cmd/root-ls
$> go get -v go-hep.org/x/hep/cmd/root2csv

## it should have installed the resulting binaries under $GOPATH/bin.
$> go env GOPATH
/some/path

$> /some/path/bin/root-ls -h
Usage: root-ls [options] file1.root [file2.root [...]]

ex:
 $> root-ls ./testdata/graphs.root

options:
  -profile string
    	filename of cpuprofile
  -sinfos
    	print StreamerInfos
  -t	print Tree(s) (recursively)

```

ditto for `root2csv`.

if you put that `$GOPATH/bin` directory as part of your `$PATH` environment variable, the binaries produced by `go get` will be readily available from your shell w/o having to type the full path.

and here is an example of `root2csv` usage, with your favorite ROOT file, assuming it contains a `TTree` `"mytree"`:

```auto
$> root2csv -f /path/to/your/favorite/file.root -o my-output.csv -t mytree

```

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:** [December 8, 2020, 9:52am UTC](https://root-forum.cern.ch/t/converting-root-file-to-csv-using-root2csv/42583/6 "2020-12-08T09:52:06Z")

</div>

did that work?

---

<div class="post-metadata">

**Author:** ![Hawi\_Phys](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/hawi_phys/32/24182_2.png) [@Hawi\_Phys](https://root-forum.cern.ch/u/Hawi_Phys)\
**Post date:** [December 8, 2020, 5:59pm UTC](https://root-forum.cern.ch/t/converting-root-file-to-csv-using-root2csv/42583/7 "2020-12-08T17:59:27Z")

</div>

Hello, currently writing finals so haven’t tried it out yet. Will update as soon as I do so! Probably end of week. Cheers!
