How can a C++ vector<char,allocator<char> > object be accessed in Python?

I am accessing some C++ objects stored in a ROOT TTree using PyROOT. In PyROOT, something like a C++ vector can be looped over in the same way that a Python list can be looped over.

for eventIndex, event in enumerate(variablesTree): for status in event.el_isTight: print(status)

One of these objects (with the name el_isTight) is represented by the following printout:

<class '__main__.vector<char,allocator<char> >'>

Since it is a vector, I can loop over it successfully and I can get its length (which is correct), however, I don’t know how to deal with its elements. When I print one of the elements of the vector, I get a representation like the following:

How could I try to access this type of object? Conceptually, I expect it to be effectively a Boolean or something with a 0 or 1 value (with each element of the vector corresponding to a characteristic of one electron in an event).

Hi,

try something like in the following snippet:

import ROOT
v = ROOT.vector('char')()
for i in (1,1,30,0,0,1,1): v.push_back(i)
# this mimics your loop on the vector elements
for el in map(ord,v):
    print type(el), el

Cheers,
Danilo

1 Like

Danilo, thank you very much for your help on that. Your solution worked perfectly. :slight_smile: