Reading STL bool vector

Dear experts,

I need to read a tree that has an STL vector of bools stored. Now because of the template specialisation for the bool type, the usual way of accessing the data via [] or at() don’t work. I tried accessing the data via iterators :

            i = tree.LepisID.begin()
            while i != tree.LepisID.end() :
                print i, dir(i)

resulting in a StopIteration on the first element. The dir() of the container nor of the element accessible via at (<ROOT._Bit_iterator object at …>) did not show any clue on how to get to the data.

Thus: is there a way to extract the boolean information out of the vector?


Hi Philipp,

this example will get you started:

import ROOT
ROOT.gInterpreter.ProcessLine('std::vector<bool> v {0,1,1,1,1};')
for b in ROOT.v:
    print bool(b)


Hello Danilo,

indeed using the for loop syntax and the bool-casting works. Could you mention the key-words or concepts that cause the for-syntax to work? To me it is not really clear how it differs from the manual iterator syntax I mentioned in the first post.

Many thanks for the very prompt help!

Is fix your code like is C++: i = tree.LepisID.begin() while i != tree.LepisID.end() : print bool(i.__deref__()) i = i.__preinc__()
All said, next() is call postinc and postinc is broken. Argue is bug.

But note! Need measure: most case for-loop very slow because overheads. Not normal vector: is optimize with stride and size like memory buffer and then very fast. Do not know vector.