.png data into rootree with PyROOT

Hey there,

I am trying to fill a root tree with 2d-arrays gained from .png pictures.
It works perfectly fine if I only store one picture. But my script crashes as soon as I loop over different .png files, saying

SystemError: int TTree::Fill() => problem in C++; program state has been reset

Seems like I haven’t realy understood how PyROOT does the tree filling.
Do you have any idea how to proceed?

Looking forward to your suggestions,
Cheers

test.py (1.4 KB)

Hi @schroesa

The issue is that you define your branches once by linking them to numpy arrays, but at every iteration you reassign thoise numpy arrays and the previous ones are destroyed.

I would start by moving this:

        datatree.Branch('width', width, 'width/I')
        datatree.Branch('height', height, 'height/I')
        datatree.Branch('PID', PID, 'PID/I')
        datatree.Branch('image', dataarray, 'image[height]['+str(width)+']/D')

outside of the loop on the files. The definition of the branches needs to be done only once. This means you need to move the definition of the corresponding numpy arrays outside of the loop too. This also means that, what you need to do is, instead of creating new numpy arrays at every iteration, just create them once before the loop and refill those same numpy arrays at every iteration.

Cheers,

Enric

1 Like

Thank you very much Enric!
I not only could get my data into the tree but also understood a bit more about python.

The only thing I don’t like about the solution now is that I have to know the size of the .png file beforehand. That’s why I tried to read out this information in the index==0 condition. But I could easily extract this information in another function or so.

Thanks!
Sarah

You could make that "image" branch a one-dimensional array image[size], with an additional branch "width" and "size" (where you store width*height).