# Convert root file to something readable by python

**URL:** <https://root-forum.cern.ch/t/convert-root-file-to-something-readable-by-python/48262>\
**Category:** Newbie\
**Created:** [January 4, 2022, 7:08pm UTC](https://root-forum.cern.ch/t/convert-root-file-to-something-readable-by-python/48262 "2022-01-04T19:08:50Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tmikhail](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/tmikhail/32/26564_2.png) [@Tmikhail](https://root-forum.cern.ch/u/Tmikhail)\
**Post date:** [January 4, 2022, 7:08pm UTC](https://root-forum.cern.ch/t/convert-root-file-to-something-readable-by-python/48262/1 "2022-01-04T19:08:50Z")

</div>

Hello all,  
I’ve been given a project in which I am supposed to implement several machine learning algorithms on data obtained by another group. However, the data is in root format, and I am trying to convert it into something more easily read by machine learning algorithms. I have tried to print the various graphs to txt files using a command I found on these forums, hp0-\>Print(“all”); dump.txt  
where hp0 is the name of the graph and dump.txt is an empty txt file in the same directory, but I get the error “undeclared identifier dump”.  
I have also tried the dump command in root, but it gives less than useful information. It outputs:

root [2] ==\> Dumping object at: 0x00005578ccd0a590, name=TFrame, class=TFrame

fBorderSize 1 window box bordersize in pixels  
fBorderMode 0 Bordermode (-1=down, 0 = no border, 1=up)  
\*fTip -\>0 ! tool tip associated with box  
fX1 0 X of 1st point  
fY1 -0.228786 Y of 1st point  
fX2 20 X of 2nd point  
fY2 0.265047 Y of 2nd point  
fResizing false ! True if box is being resized  
fUniqueID 0 object unique identifier  
fBits 0x03000008 bit field status word  
fLineColor 1 Line color  
fLineStyle 1 Line style  
fLineWidth 1 Line width  
fFillColor 0 Fill area color  
fFillStyle 1001 Fill area style

So, I would like to know how I should convert the file.  
Thanks in advance!

\_ROOT Version:6  
_Platform:_ Ubuntu20.04  
\_Compiler: NA

* * *

---

<div class="post-metadata">

**Author:** ![bellenot](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/bellenot/32/29217_2.png) [@bellenot](https://root-forum.cern.ch/u/bellenot)\
**Post date:** [January 4, 2022, 9:24pm UTC](https://root-forum.cern.ch/t/convert-root-file-to-something-readable-by-python/48262/2 "2022-01-04T21:24:30Z")

</div>

I think @etejedor and @moneta can give some advice for this

---

<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 4, 2022, 11:36pm UTC](https://root-forum.cern.ch/t/convert-root-file-to-something-readable-by-python/48262/3 "2022-01-04T23:36:56Z")

</div>

depending on the type of data you want to convert, there may be a couple of solutions available from [Go-HEP](https://go-hep.org):

- a `TTree` (if its structure is compatible) can be converted to a `numpy` array file via [root2npy](https://go-hep.org/x/hep/cmd/root2npy)
- a `TTree` can also be converted to `arrow` via [root2arrow](https://go-hep.org/x/hep/cmd/root2arrow)
- `TH{1,2}X` and `TGraph{,{,Asymm}Errors}` can be converted to text (using the [YODA ASCII format](https://yoda.hepforge.org/)) via [root2yoda](https://go-hep.org/x/hep/cmd/root2yoda)

binaries for `Go-HEP` are available here:

- [https://go-hep.org/dist/latest/](https://go-hep.org/dist/latest/)

alternatively, there is also the [uproot4](https://github.com/scikit-hep/uproot4) pure-Python package that can read/write ROOT files.

hth,  
-s

---

<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:** [January 5, 2022, 8:24am UTC](https://root-forum.cern.ch/t/convert-root-file-to-something-readable-by-python/48262/4 "2022-01-05T08:24:19Z")

</div>

Hi,  
with native ROOT you can use RDataFrame’s AsNumpy method, [here is a tutorial](https://root.cern.ch/doc/master/df026__AsNumpyArrays_8py.html).

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![etejedor](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/e79b87/32.png) [@etejedor](https://root-forum.cern.ch/u/etejedor)\
**Post date:** [January 5, 2022, 8:27am UTC](https://root-forum.cern.ch/t/convert-root-file-to-something-readable-by-python/48262/5 "2022-01-05T08:27:35Z")

</div>

Hello,

With ROOT, you can dump the contents of a ROOT file into NumPy arrays with the `AsNumpy` method of RDataFrame. Here’s a tutorial:

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

Basically you would create an RDataFrame from a tree in one or more files, then use `AsNumpy` to store in NumPy arrays the columns (branches) of the dataset that you would like to have. What you get from `AsNumpy` is a dictionary with one key-value for each column name - NumPy array for that column.

---

<div class="post-metadata">

**Author:** ![Tmikhail](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/tmikhail/32/26564_2.png) [@Tmikhail](https://root-forum.cern.ch/u/Tmikhail)\
**Post date:** [January 25, 2022, 4:55pm UTC](https://root-forum.cern.ch/t/convert-root-file-to-something-readable-by-python/48262/6 "2022-01-25T16:55:22Z")

</div>

Thanks for the help  
I’ve been trying to use the root2npy command, but it does not seem to produce the file. Here is the output

```auto
./root2npy -f /home/octopus/Documents/Work/Thesis/Data/ColliderSimulation/LHC1/test023.root -t GammaC -o /home/octopus/Documents/Work/Thesis/Data/ColliderSimulation/LHC1/output.npz
root2npy: scanning leaves...
root2npy: scanning leaves... [done]
panic: rtree: unknown Tree implementation *rtree.tntuple

goroutine 1 [running]:
go-hep.org/x/hep/groot/rtree.newReader({0x7f0798bf29f8, 0xc0001c71e0}, {0xc0001d1680, 0x5, 0x8}, 0x51109f, 0xc000140000, 0x19)
	/home/octopus/go/pkg/mod/go-hep.org/x/hep@v0.29.2/groot/rtree/reader.go:261 +0x1a8
go-hep.org/x/hep/groot/rtree.NewReader({0x7f0798bf29f8, 0xc0001c71e0}, {0xc0001d1680, 0x5, 0x8}, {0x0, 0x0, 0x0})
	/home/octopus/go/pkg/mod/go-hep.org/x/hep@v0.29.2/groot/rtree/reader.go:66 +0x199
main.process({0x7ffd530097e7, 0x4b}, {0x7ffd5300978c, 0x4d}, {0x7ffd530097dd, 0x6})
	/home/octopus/Documents/Work/Thesis/Data/ColliderSimulation/LHC1/test.go:199 +0x43a
main.main()
	/home/octopus/Documents/Work/Thesis/Data/ColliderSimulation/LHC1/test.go:160 +0x185

```

The contents of the ROOT file are:

```auto
TFile** test023.root	
 TFile* test023.root	
  KEY: TNtuple	v2;471	v2 [current cycle]
  KEY: TNtuple	v2;470	v2 [backup cycle]
  KEY: TNtuple	GammaA;226	GammaA [current cycle]
  KEY: TNtuple	GammaA;225	GammaA [backup cycle]
  KEY: TNtuple	GammaC;1	GammaC
  KEY: TProfile	hp0;1	hp0
  KEY: TProfile	hp1;1	hp1
  KEY: TProfile	hp2;1	hp2
  KEY: TProfile	hp3;1	hp3
  KEY: TProfile	hp4;1	hp4
  KEY: TProfile	hp5;1	hp5

```

---

<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 25, 2022, 5:58pm UTC](https://root-forum.cern.ch/t/convert-root-file-to-something-readable-by-python/48262/7 "2022-01-25T17:58:29Z")

</div>

thanks for the report.

do you have a small ROOT file that I could play with?

---

<div class="post-metadata">

**Author:** ![Tmikhail](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/tmikhail/32/26564_2.png) [@Tmikhail](https://root-forum.cern.ch/u/Tmikhail)\
**Post date:** [January 25, 2022, 6:05pm UTC](https://root-forum.cern.ch/t/convert-root-file-to-something-readable-by-python/48262/8 "2022-01-25T18:05:18Z")

</div>

No, thank you for the help  
The files I have are between 10-25 gb, so I suppose that would not work. I am somewhat new to root, is there a way I could move an TNtuple to its own file and try to upload that?

---

<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 25, 2022, 6:35pm UTC](https://root-forum.cern.ch/t/convert-root-file-to-something-readable-by-python/48262/9 "2022-01-25T18:35:18Z")

</div>

nevermind, I have a fix 🙂

- [groot/{rcmd,riofs,rtree}: add rtree.Reader support for TNtuple by sbinet · Pull Request #887 · go-hep/hep · GitHub](https://github.com/go-hep/hep/pull/887)

(I’ll wait for the tests to complete and post a binary for you to test somewhere)

---

<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 25, 2022, 6:38pm UTC](https://root-forum.cern.ch/t/convert-root-file-to-something-readable-by-python/48262/10 "2022-01-25T18:38:31Z")

</div>

here it is:

- [https://cern.ch/binet/root2npy-with-tntuple-support](https://cern.ch/binet/root2npy-with-tntuple-support)

I’ll cut a new Go-HEP release tomorrow morning (CERN time).

hth,  
-s

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [January 25, 2022, 6:42pm UTC](https://root-forum.cern.ch/t/convert-root-file-to-something-readable-by-python/48262/11 "2022-01-25T18:42:41Z")

</div>

@sbinet [TNtupleD](https://root.cern/doc/master/classTNtupleD.html), too?

---

<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 25, 2022, 6:55pm UTC](https://root-forum.cern.ch/t/convert-root-file-to-something-readable-by-python/48262/12 "2022-01-25T18:55:39Z")

</div>

not in that PR (yet).

I’ll add support for TNtupleD shortly.

---

<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 25, 2022, 7:11pm UTC](https://root-forum.cern.ch/t/convert-root-file-to-something-readable-by-python/48262/13 "2022-01-25T19:11:21Z")

</div>

@Wile_E_Coyote done. (I’ve also updated the previous binary with `TNtupleD` support. _hth_)

---

<div class="post-metadata">

**Author:** ![Tmikhail](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/tmikhail/32/26564_2.png) [@Tmikhail](https://root-forum.cern.ch/u/Tmikhail)\
**Post date:** [January 26, 2022, 11:22am UTC](https://root-forum.cern.ch/t/convert-root-file-to-something-readable-by-python/48262/14 "2022-01-26T11:22:13Z")

</div>

Thanks for the file!  
However, sadly, now I’m running into a different error.

```auto
./root2npy -f /home/octopus/Documents/Work/Thesis/Data/ColliderSimulation/LHC1/test023.root -t GammaC -o /home/octopus/Documents/Work/Thesis/Data/ColliderSimulation/LHC1/output.npz
root2npy: scanning leaves...
root2npy: scanning leaves... [done]
panic: reflect: slice index out of range

goroutine 1 [running]:
reflect.Value.Index({0xaf51c0?, 0xc0000ac7f8?, 0x68358b?}, 0x444e73?)
	/home/binet/sdk/go/src/reflect/value.go:1366 +0x16d
github.com/sbinet/npyio/npy.shapeFrom({0xaf51c0?, 0xc0000ac7f8?, 0x42ebd6?})
	/home/binet/work/gonum/pkg/mod/github.com/sbinet/npyio@v0.5.2/npy/writer.go:520 +0x185
github.com/sbinet/npyio/npy.Write({0xdfef80, 0xc0001dd710}, {0xaf51c0?, 0xc0000ac7f8?})
	/home/binet/work/gonum/pkg/mod/github.com/sbinet/npyio@v0.5.2/npy/writer.go:40 +0x1a7
github.com/sbinet/npyio.Write(...)
	/home/binet/work/gonum/pkg/mod/github.com/sbinet/npyio@v0.5.2/npyio.go:125
main.process({0x7ffdca9d87df, 0x4b}, {0x7ffdca9d8784, 0x4d}, {0x7ffdca9d87d5, 0x6})
	/home/binet/work/gonum/src/go-hep.org/x/hep/cmd/root2npy/main.go:225 +0xb69
main.main()
	/home/binet/work/gonum/src/go-hep.org/x/hep/cmd/root2npy/main.go:160 +0x185

```

Sorry for all the trouble

---

<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 26, 2022, 11:53am UTC](https://root-forum.cern.ch/t/convert-root-file-to-something-readable-by-python/48262/15 "2022-01-26T11:53:33Z")

</div>

@Tmikhail no trouble at all, it’s useful to get Go-HEP more robust and battlefield-tested.

here is another binary:

- [https://cern.ch/binet/root2npy-verbose](https://cern.ch/binet/root2npy-verbose)

that should get me more informations as to what is happening.

---

<div class="post-metadata">

**Author:** ![Tmikhail](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/tmikhail/32/26564_2.png) [@Tmikhail](https://root-forum.cern.ch/u/Tmikhail)\
**Post date:** [January 26, 2022, 12:01pm UTC](https://root-forum.cern.ch/t/convert-root-file-to-something-readable-by-python/48262/16 "2022-01-26T12:01:10Z")

</div>

Here is the output of the new script:

```auto
./root2npy -f /home/octopus/Documents/Work/Thesis/Data/ColliderSimulation/LHC1/test023.root -t GammaC -o /home/octopus/Documents/Work/Thesis/Data/ColliderSimulation/LHC1/output.npz
root2npy: scanning leaves...
root2npy: scanning leaves... [done]
root2npy: could not get shape from []float32: []
err: reflect: slice index out of range
panic: reflect: slice index out of range [recovered]
	panic: reflect: slice index out of range

goroutine 1 [running]:
github.com/sbinet/npyio/npy.shapeFrom.func1()
	/home/binet/work/gonum/src/github.com/sbinet/npyio/npy/writer.go:528 +0x125
panic({0xb00d20, 0xdff530})
	/home/binet/sdk/go/src/runtime/panic.go:838 +0x207
reflect.Value.Index({0xaf5520?, 0xc00000c828?, 0x2?}, 0xc000026e98?)
	/home/binet/sdk/go/src/reflect/value.go:1366 +0x16d
github.com/sbinet/npyio/npy.shapeFrom({0xaf5520?, 0xc00000c828?, 0x42eed6?})
	/home/binet/work/gonum/src/github.com/sbinet/npyio/npy/writer.go:531 +0x205
github.com/sbinet/npyio/npy.Write({0xdffee0, 0xc0001c9710}, {0xaf5520?, 0xc00000c828?})
	/home/binet/work/gonum/src/github.com/sbinet/npyio/npy/writer.go:41 +0x1a7
github.com/sbinet/npyio.Write(...)
	/home/binet/work/gonum/src/github.com/sbinet/npyio/npyio.go:125
main.process({0x7ffd31cb37df, 0x4b}, {0x7ffd31cb3784, 0x4d}, {0x7ffd31cb37d5, 0x6})
	/home/binet/work/gonum/src/go-hep.org/x/hep/cmd/root2npy/main.go:225 +0xb69
main.main()
	/home/binet/work/gonum/src/go-hep.org/x/hep/cmd/root2npy/main.go:160 +0x185

```

---

<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 26, 2022, 12:10pm UTC](https://root-forum.cern.ch/t/convert-root-file-to-something-readable-by-python/48262/17 "2022-01-26T12:10:51Z")

</div>

@Tmikhail ok.  
that’s what I thought.

let’s move this conversation over there:

- [cmd/root2npy: add support for writing empty slices · Issue #891 · go-hep/hep · GitHub](https://github.com/go-hep/hep/issues/891)

(if you have a github account, that is. otherwise, let’s continue privately with direct messages via `root-forum`)

---

<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:** [January 26, 2022, 12:14pm UTC](https://root-forum.cern.ch/t/convert-root-file-to-something-readable-by-python/48262/18 "2022-01-26T12:14:49Z")

</div>

Btw this should work in ROOT:

```auto
import ROOT
dictionary_of_numpy_arrays = ROOT.RDataFrame("GammaC", "test023.root").AsNumpy()

```

Cheers,  
Enrico

---

<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 26, 2022, 1:31pm UTC](https://root-forum.cern.ch/t/convert-root-file-to-something-readable-by-python/48262/19 "2022-01-26T13:31:33Z")

</div>

For archives’ sake, this was fixed w/ `sbinet/npyio@v0.6.0` and collected into `Go-HEP@v0.30.1`.

one can download the binaries from:

- [/dist/v0.30.1](https://go-hep.org/dist/v0.30.1)

(or directly using the Go SDK)

---

<div class="post-metadata">

**Author:** ![roy.brener](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/roy.brener/32/25033_2.png) [@roy.brener](https://root-forum.cern.ch/u/roy.brener)\
**Post date:** [October 20, 2024, 7:18am UTC](https://root-forum.cern.ch/t/convert-root-file-to-something-readable-by-python/48262/20 "2024-10-20T07:18:01Z")

</div>

Hi,  
On that issue, is it possible to do the reverse _i.e._ convert a npz file to ROOT?  
Cheers,  
Roy

[Next page](https://root-forum.cern.ch/t/convert-root-file-to-something-readable-by-python/48262.md?page=2)
