Reading a tree with c-struct

Hi,
this might be a stupid question, but I haven’t been able to find an easy solution…
I have a tree containing a c struct like

struct event_t{
          int ieta;
          int iphi;
          float energy;
        };

and I would like to read it back from a python script. What I tried is something like:

class event_t: def __init__(self,e=0,p=0,en=0.): self.ieta = e self.iphi = p self.energy = en #-------------- event = event_t() theBranch = tree.GetBranch("mybranch") theBranch.SetAddress(event) for entry in range(10): tree.GetEntry(entry) print " "+str(entry)+") value = "+str(event.energy)
but I get the error

TypeError: void TBranch::SetAddress(void* add) => could not convert argument 1
Please, could you give me any hint?
Thank you in advance.

davide

Davide,

the problem with an approach like this (which I’d love if it were possible):class event_t: def __init__(self,e=0,p=0,en=0.): self.ieta = e self.iphi = p self.energy = en
is that this doesn’t provide sufficient information to construct a piece of memory that can be mapped on from C++. For example, the use of ‘0’ as a literal integer, means that all three variables point to the exact same python object (and would be assumed to be of type int). Taking its address and moving it to C++ to be changed there upon reading the tree, would mean that 0 would no longer be zero, which is not desirable.

Furthermore, even if ieta and friends could be set by address, an assignment on the python side would collect that piece of memory, and segfaults would occur on reading/writing.

Putting the whole object by address would not work either, because the memory layout of the python object is completely different that that of a C++ object. Use of the weave extension module may work, but I haven’t yet tried that.

There are two ways around this: you can either access the branch directly (if a dictionary has been loaded), e.g. “tree.mybranch” in your particular case here, which doesn’t require SetBranchAddress(). Or you can use gROOT.ProcessLine() to interpret the class, therefore have a dictionary and be able to create event_t objects, which addresses can be set. For an example of that, please see the $ROOTSYS/tutorials/staff.py file.

HTH,
Wim