How to use TPyBufferFactory from python

I have a problem with reading a Double_t buffer from tree using a python script.

I tried numpy arrays (with different endians, sizes, etc), list, but that failed (it gave almost zeroes or very large numbers, while TTree:Show gave normal values).

I want to use a ‘direct’ TPyBufferFactory. How can I do that?

I tried to import that from ROOT (missing there), PyROOT (this is not a module), but that failed.


ROOT Version = 6.12/06
Fedora Core 27, gcc7.3.1


Hi,

just to be sure: are you trying to read a C array persistified in a column of a ROOT dataset in PyROOT? If yes, how exactly are you doing that?

Cheers,
D

I’m reading a Double_t array from a root tree. Here is my function:

def get_directions_from_root_tree_DC4(filename):

import ROOT 
from ROOT import TTree #, PyBuffer_FromReadWriteMemory
fil = ROOT.TFile(filename)
myTree = fil.Get("RecoBAMAInfoTree")
myTree.Show(0)
ev_index = 0
import numpy
for entry in myTree:
    print "entry.Vertex = ", entry.VertexX
    # vertex_arr = list(entry.VertexX)
    # vertex_arr = numpy.frombuffer(entry.VertexX, count=4)
    vertex_arr = numpy.ndarray(4, 'd', entry.VertexX)
    # vertex_arr = numpy.ndarray(4, 'f', entry.VertexX)
    print "vertex_arr = ", vertex_arr
    # print "entry.Vertex, cast = ", ctypes.cast(entry.VertexX, c_double)
    # ctypes.ArgumentError: argument 1: <type 'exceptions.TypeError'>: wrong type
    print "entry.x = ", entry.VertexX[0]
    break

Output:


TClass::Init:0: RuntimeWarning: no dictionary for class LightInfo is available
======> EVENT:0

VertexX[4] = -240.231 , 1169.72 , -944.572 , 72.1845

entry.Vertex = <Double_t buffer, size 4>
vertex_arr = [ 6.90978091e-310 3.13151306e-294 4.68832683e-310 6.18076123e-321]
entry.x = 6.90978090561e-310

Hi @ynikitenko, can you try what is suggested here:

In that example, there was a 2D array of doubles that was read back into Python and reconverted with np.reshape.

Enric

Thank you. Unfortunately my tree is filled independently in the experiment. I can’t refill that, I have to use the data I possess.
I ‘solved’ this problem by reading the tree from C++ (I generated a class), and writing that to text files to be further analysed by python.

Hi @ynikitenko,

Thanks for reporting back, the example I posted not only writes the branches, but reads them back and formats them with np.reshape. If the branches of your tree are double arrays (i.e. written like in part 1 of the example) you should be able to read them back like in part 2.

you could give uproot a try.
or rootpy.

the first is a pure-python (no C++/ROOT needed) the second one is a more (num)pythonic interface to PyROOT.

if none of these options works, I could have a look into providing a python-module to my Go package that reads ROOT files (rootio.)

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