# Go-HEP: write support for TH1x, TH2x and TGraphs in go-hep/rootio

**URL:** <https://root-forum.cern.ch/t/go-hep-write-support-for-th1x-th2x-and-tgraphs-in-go-hep-rootio/30615>\
**Category:** Evolution Proposals, Apps & Ideas\
**Created:** [September 10, 2018, 4:11pm UTC](https://root-forum.cern.ch/t/go-hep-write-support-for-th1x-th2x-and-tgraphs-in-go-hep-rootio/30615 "2018-09-10T16:11:28Z")\
**Posts on this page:** 2\
**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:** [September 10, 2018, 4:11pm UTC](https://root-forum.cern.ch/t/go-hep-write-support-for-th1x-th2x-and-tgraphs-in-go-hep-rootio/30615/1 "2018-09-10T16:11:28Z")

</div>

as alluded to during the ROOT Users’ workshop:

- [https://indico.cern.ch/event/697389/contributions/3102807/attachments/1713054/2762448/Writing\_files\_with\_uproot.pdf](https://indico.cern.ch/event/697389/contributions/3102807/attachments/1713054/2762448/Writing_files_with_uproot.pdf) (page 5)

[GoHEP](https://go-hep.org) `v0.15.0` has now support for (also) writing ROOT files:

- `TObjString`, `TObject`, `TNamed`,
- `TH1x`, `TH2x`, `TGraph`, `TGraph{,Asymm}Errors`

it also ships with a new `root-cp` command that can extract objects from a ROOT file and save them into a new one.

`go-hep/rootio` also supports writing compressed ROOT files (`zlib`, `LZ4`, `LZMA`).

Here is the full announcement: [https://go-hep.org/news/release-0.15.0/](https://go-hep.org/news/release-0.15.0/)

and here is a simple example:

```auto
func main() {
	w, err := rootio.Create("out.root", rootio.WithLZ4(flate.BestCompression))
	if err != nil {
		log.Fatal(err)
	}
	defer w.Close()

	v := rootio.NewObjString("Hello World from Go-HEP!")

	err = w.Put("my-obj-string", v)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("keys: %d\n", len(w.Keys()))

	err = w.Close()
	if err != nil {
		log.Fatalf("could not close file: %v", err)
	}
}

```

(Go-HEP tests make sure that the file is also correctly read-back from ROOT/C++)

also:

```auto
$> root-cp -h
Usage: root-cp [options] file1.root [file2.root [...]] out.root

ex:
 $> root-cp f.root out.root
 $> root-cp f1.root f2.root f3.root out.root
 $> root-cp f1.root:hist.* f2.root:h2 out.root

options:

$> root-cp ./testdata/graphs.root:g.* out.root
$> root-cp root://xrootd.example.org/file.root:hist.* out.root

```

Next steps are:

- `TTree`, `TBranch{,Element}`, `TLeaf{,Element}` and `TBasket`
- interoperability of `TTree` with Apache Arrow (both read & write to leverage the whole Apache Arrow ecosystem)
- profiling (CPU/Memory)

Enjoy 🙂

(and happy to get any kind of feedback)

---

<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:** [September 11, 2018, 11:47am UTC](https://root-forum.cern.ch/t/go-hep-write-support-for-th1x-th2x-and-tgraphs-in-go-hep-rootio/30615/2 "2018-09-11T11:47:15Z")

</div>

BTW: like for all GoHEP releases, this works on all Go-supported platforms:

- linux-x86 (32b, 64b)
- linux-x86 (s390x)
- linux-arm (32b, 64b)
- darwin (32b, 64b)
- windows (32b, 64b)
- freebsd, openbsd (32b, 64b)
- dragonfly (64b)
- plan9 (32b,64b)

and even:

- WebAssembly (wasm)

so if you are on Windows-64b and want to write ROOT files… then you can use GoHEP (or uproot) 🙂

PS: the complete list is actually:

```auto
$> go tool dist list
android/386
android/amd64
android/arm
android/arm64
darwin/386
darwin/amd64
darwin/arm
darwin/arm64
dragonfly/amd64
freebsd/386
freebsd/amd64
freebsd/arm
js/wasm
linux/386
linux/amd64
linux/arm
linux/arm64
linux/mips
linux/mips64
linux/mips64le
linux/mipsle
linux/ppc64
linux/ppc64le
linux/riscv64
linux/s390x
nacl/386
nacl/amd64p32
nacl/arm
netbsd/386
netbsd/amd64
netbsd/arm
openbsd/386
openbsd/amd64
openbsd/arm
plan9/386
plan9/amd64
plan9/arm
solaris/amd64
windows/386
windows/amd64

```
