Branch with array of variable length

hello

Is it possible to make a Tree in pyRoot with a branch that has a variable length array? and then assign a different value to each member of the array?

for example, as is done in /root/tutorials/tree2.C - a .py version of this example would solve my problem :slight_smile:

I am able to make Trees in pyRoot, but when I try to add one with a variable length array, I get the error:

or sometimes

thanks

Hi,

sorry for replying so late, but I’m just back from vacation and going through a backlog.

Attached is a quick & dirty transliteration of tree2.C into tree2.py. I have problems on my Mac causing both tree2.C and tree2.py to give a bus error, but that’s only with the display part, not the tree writing/reading. Also, I’m running whatever development version of ROOT was on my Mac. :slight_smile: There is one known issue with accessing ranged branches python-style (i.e. directly into the tree, setting a branch address works fine) in 5.12, but the example doesn’t do that anyway.

I’ll clean up the example and I’ll ask for it to be added to the tutorials directory.

HTH,
Wim
tree2.py (5.76 KB)

Hi Wim,

I noticed in your code (which I used as a template), you say a struct is an sub-optimal way to interface Python/ROOT together. Could you suggest the optimal way? Relatedly whats the best way to store lists (not necessarily python lists, just ordered collections), to TTree’s?

We have been using

Unfortunately things like:

break and one needs to do:

which is a pain in the butt! Thanks so much for the recommendation!

Josh

Josh,

missing a couple of things … where did I say that structs are sub-optimal? They are as used in the example, b/c it’s an interpreted strict; in general it does not matter, however.

Easiest ordered collection would be the use of an std::vector. You can assign a python list, but it’d have to live as a PyObject* on the C++ side and it wouldn’t be very efficient.

Using SetBranchAddress() with your own struct or vector is the most efficient use of TTree.

How does the struct.myVector[i] = 5 assignment break? It should work if the dict is available, and if it weren’t, setitem wouldn’t work either.

Cheers,
Wim