TTree Arrays Filled with Numpy Arrays

I have a python script which fills a TTree with numbers read from an ascii file. I use the ProcessLine() to create a struct (which I instantiate with the name β€œs”) with lines like:

Float_t timeidx["+str(NSAMPLES)+"];\
Float_t V1["+str(NSAMPLES)+"],V2["+str(NSAMPLES)+"],V3["+str(NSAMPLES)+"],V4["+str(NSAMPLES)+"];\

Later, I have a numpy array which contains the values that I would like to put into the ClusterTimesTrue branch of the TTree. But if I try

                        s.timeidx = numpy.array(range(NSAMPLES),dtype=int)
                        s.V1 = numpy.array(signal_data,dtype=float)

with signal_data and V#_clean being numpy arrays, while noisetree.V3 is a member of an identical struct as β€œs”. I then get errors:

Traceback (most recent call last): File "./", line 262, in <module> s.V1 = numpy.array(signal_data,dtype=float) AttributeError: 'numpy.ndarray' object has no attribute 'typecode' and given element size (8) do not match needed (4)
I think this is because python floats are 8 bytes, but there is no built-in 4 byte float, so I don’t know what to put for the dtype to make it work. It could be a completely different problem though.

My current work-around is to loop over range(len(signal_data)) and individually fill the elements of s.V1[i], which is very slow.

Does anyone have experience with filling tree branches with numpy arrays?


if you want to stick with 4-byte floats, then the dtype should be numpy.float32. (numpy.float is simply python builtin float.)


1 Like