# Go-HEP - v0.4: reading ROOT files from Go (and more)

**URL:** <https://root-forum.cern.ch/t/go-hep-v0-4-reading-root-files-from-go-and-more/25854>\
**Category:** Evolution Proposals, Apps & Ideas\
**Created:** [August 8, 2017, 4:44pm UTC](https://root-forum.cern.ch/t/go-hep-v0-4-reading-root-files-from-go-and-more/25854 "2017-08-08T16:44:38Z")\
**Posts on this page:** 9\
**Page:** 1

<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:** [August 8, 2017, 4:44pm UTC](https://root-forum.cern.ch/t/go-hep-v0-4-reading-root-files-from-go-and-more/25854/1 "2017-08-08T16:44:38Z")

</div>

hi there,

just to let you know I have just released `Go-HEP v0.4`:

- [https://go-hep.org/news/release-0.4/](https://go-hep.org/news/release-0.4/)
- code: [https://github.com/go-hep/hep/releases/tag/v0.4](https://github.com/go-hep/hep/releases/tag/v0.4)

the instructions to install [Go](https://golang.org/doc/install) and `Go-HEP` are here:

- [https://go-hep.org/getting-started/](https://go-hep.org/getting-started/)

Version `v0.4` sports:

- reading/decoding of `ROOT` files
- extracting `TH1x` and `TH2x` from `ROOT` files
- reading “simple” `TTree`s (flat trees containing builtins and arrays/slices of builtins, event-trees of builtins, arrays/slices/`std::vector`s of builtins/simple-structs, `std::string`s and `TString`s)
- `root-ls`: a command to inspect `ROOT` files
- `root-srv`: a command that launches a local web-server from which one can inspect `ROOT` files and plot `TH1x`, `TH2x` and `TTree`s
- `root2npy`: a command that converts “simple” `TTree`s into `numpy` data files
- `root2yoda`: a command that converts `ROOT` files into [YODA](https://yoda.hepforge.org/) files

and that’s just the purely `ROOT`-related stuff.

With [Go-HEP](https://go-hep.org), [Gonum](https://gonum.org) (`numpy/scipy` for Go) and [Go](https://golang.org) you can:

- create nice plots,
- do BLAS/LAPACK and linear algebra
- do stats (univariate, multivariate, histograms, …)
- etc…

Come to Go for the concurrency, and stay for everything else 🙂

Cheers,  
Sebastien.

---

<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:** [August 8, 2017, 6:26pm UTC](https://root-forum.cern.ch/t/go-hep-v0-4-reading-root-files-from-go-and-more/25854/2 "2017-08-08T18:26:26Z")

</div>

Hi,  
this sounds great, especially the `root2npy` tool I think is something many people might benefit from.

I am no go expert, and I took a look at your website to see how a parallel loop over TTree events looks with this framework, but could not find anything. Could you point me to an example please 🙂 ?

---

<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:** [August 9, 2017, 10:31am UTC](https://root-forum.cern.ch/t/go-hep-v0-4-reading-root-files-from-go-and-more/25854/3 "2017-08-09T10:31:29Z")

</div>

FYI, I have updated the doc for `root2npy`:

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

wrt reading TTrees: the central type to do such a thing in Go-HEP is `rootio.Scanner`.  
here are some examples:

- [https://godoc.org/go-hep.org/x/hep/rootio#example-Scanner](https://godoc.org/go-hep.org/x/hep/rootio#example-Scanner)
- [https://godoc.org/go-hep.org/x/hep/rootio#example-Scanner--WithVars](https://godoc.org/go-hep.org/x/hep/rootio#example-Scanner--WithVars)

as can be seen in the first example, the looping is performed like so:

```auto
type Data struct {
    I64 int64 `rootio:"Int64"`
    F64 float64 `rootio:"Float64"`
    Str string `rootio:"Str"`
    ArrF64 [10]float64 `rootio:"ArrayFloat64"`
    N int32 `rootio:"N"`
    SliF64 []float64 `rootio:"SliceFloat64"`
}

sc, err := rootio.NewScanner(tree, &Data{})
if err != nil {
    log.Fatal(err)
}
defer sc.Close()

for sc.Next() {
    var data Data
    err := sc.Scan(&data)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("entry[%d]: %+v\n", sc.Entry(), data)
    if sc.Entry() == 9 {
        break
    }
}

```

you create a new data type `Data`, decorated to bind TTree branch names (`"Int64"`) with struct fields (`I64`).  
the `rootio.Scanner` will take care of populating the branch’s value into the corresponding `Data`'s field value.

Modifying that loop to make it concurrent (and thus, with multi-cores, parallel) is as easy as:

```auto
pipe := make(chan Data)
// create a goroutine to extract data from the tree
// this will execute asynchronously
go func() {
    for sc.Next() {
        var data Data
        err := sc.Scan(&data)
        if err != nil {
            log.Fatal(err)
        }

        fmt.Printf("entry[%d]: %+v\n", sc.Entry(), data)
    }
    // signal we are done with feeding data
    close(pipe)
}()

```

then, one can create multiple consumers, feeding off that `pipe` channel.

```auto
results := make(chan MyResultType)
for i := 0; i < runtime.NumCPU()*2; i++ {
    go process(pipe, results)
}

// iterate over the results
for result := range results {
    fmt.Printf("result=%v\n", result)
}

```

where `process` is a regular `Go` function:

```auto
type MyResultType float64

func process(in chan Data, out chan MyResultType) {
    // extract data from the input channel
    for v := range in {
        // send some data down the line
        out <- MyResultType(math.Sqrt(v.F64))
    }
}

```

and voila.

here are a few pointers about the `Go` concurrency model:

- [https://talks.golang.org/2012/waza.slide#1](https://talks.golang.org/2012/waza.slide#1)
- [https://talks.golang.org/2012/concurrency.slide#1](https://talks.golang.org/2012/concurrency.slide#1)

(I can’t recommand the videos enough: Rob Pike is a great speaker.)

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:** [August 9, 2017, 2:02pm UTC](https://root-forum.cern.ch/t/go-hep-v0-4-reading-root-files-from-go-and-more/25854/4 "2017-08-09T14:02:17Z")

</div>

BTW, the `root-srv` command that is shipped with `go-hep/rootio` and serves a web server to inspect a ROOT file, is available on Google’s AppEngine:

- [http://rootio-inspector.appspot.com/](http://rootio-inspector.appspot.com/)

(a slightly older (~4months) version is also served using CERN’s OpenShift: [http://cern.ch/go-rootio](http://cern.ch/go-rootio))

---

<div class="post-metadata">

**Author:** ![wlav](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/f07891/32.png) [@wlav](https://root-forum.cern.ch/u/wlav)\
**Post date:** [August 9, 2017, 2:55pm UTC](https://root-forum.cern.ch/t/go-hep-v0-4-reading-root-files-from-go-and-more/25854/5 "2017-08-09T14:55:32Z")

</div>

For ROOT to numpy conversion, see: [https://pypi.python.org/pypi/root\_numpy](https://pypi.python.org/pypi/root_numpy)

---

<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:** [August 9, 2017, 3:10pm UTC](https://root-forum.cern.ch/t/go-hep-v0-4-reading-root-files-from-go-and-more/25854/6 "2017-08-09T15:10:25Z")

</div>

Different use-case I think?  
One is a standalone command-line tool to quickly convert your (simple) data-set, the other is a python library with many more features but that requires you write your own script to do the conversion exactly as you want it.

In other words, you could write sbinet’s `root2npy` _with_ `root_numpy`.

---

<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:** [August 9, 2017, 3:34pm UTC](https://root-forum.cern.ch/t/go-hep-v0-4-reading-root-files-from-go-and-more/25854/7 "2017-08-09T15:34:56Z")

</div>

@wlav,

yes.  
there’s also: [https://github.com/diana-hep/c2numpy](https://github.com/diana-hep/c2numpy)

the nice things about `root2npy` – if I may say so myself – are:

- it doesn’t require ROOT5 nor ROOT6 to be installed
- it doesn’t require Python2 nor Python3 to be installed
- it’s completely statically compiled (and can be easily cross-compiled to Linux, Windows, Darwin, …), so deployment is just a simple `scp|Putty|curl` away.

You just need `git` and `go`, and you’re good to go.

That said, `root2npy` isn’t able to read all the TTrees in the HEP creation. yet.

Here are a few cross-compiled `root2npy` binaries:

```auto

/afs/cern.ch/user/b/binet/public/go-hep/root2npy-darwin-amd64.exe
/afs/cern.ch/user/b/binet/public/go-hep/root2npy-linux-386.exe
/afs/cern.ch/user/b/binet/public/go-hep/root2npy-linux-amd64.exe
/afs/cern.ch/user/b/binet/public/go-hep/root2npy-linux-arm64.exe
/afs/cern.ch/user/b/binet/public/go-hep/root2npy-windows-386.exe
/afs/cern.ch/user/b/binet/public/go-hep/root2npy-windows-amd64.exe

```

they were cross-compiled with the official Go toolchain like so:

```auto
GOOS=linux GOARCH=amd64 go build -o root2npy-linux-amd64.exe
GOOS=linux GOARCH=386 go build -o root2npy-linux-386.exe
GOOS=linux GOARCH=arm64 go build -o root2npy-linux-arm64.exe
GOOS=darwin GOARCH=amd64 go build -o root2npy-darwin-amd64.exe
GOOS=windows GOARCH=amd64 go build -o root2npy-windows-amd64.exe
GOOS=windows GOARCH=386 go build -o root2npy-windows-386.exe

```

---

<div class="post-metadata">

**Author:** ![wlav](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/f07891/32.png) [@wlav](https://root-forum.cern.ch/u/wlav)\
**Post date:** [August 9, 2017, 4:46pm UTC](https://root-forum.cern.ch/t/go-hep-v0-4-reading-root-files-from-go-and-more/25854/8 "2017-08-09T16:46:59Z")

</div>

Ah, by transliterating ROOT core class headers and including them, you don’t need ROOT installed. So e.g. $ROOTSYS/include/TAttFill.h becomes (attfill.go):

```auto
type attfill struct {
	color int16
	style int16
}

```

and is then textually committed into go-hep, with an associated streamer to read two int16’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:** [August 9, 2017, 5:06pm UTC](https://root-forum.cern.ch/t/go-hep-v0-4-reading-root-files-from-go-and-more/25854/9 "2017-08-09T17:06:06Z")

</div>

yep.

this was the first step to get me going.  
the end game, now that I can more or less correctly decode TStreamers, is to automatically generate type and the associated streamers, sans having to go the pain of writing and maintaining all that tedious TFoo.cxx -\> foo.go code.

and the ultimate `HEP-world Gomination` is to be able to actually **write** ROOT files/trees.
