Vector of short

Hi,

I’m used to declare a vector of int’s like this:

Now I would like to declare a vector of unsigned short’s. Knowing that Python does not have short’s natively, I was thinking about using the ROOT UShort_t class, using something like:

The code above does not work; do you know how to access (if it’s possible) that class from Python?

Thanks a lot,

Ric.

Ric,

UShort_t isn’t known, so one should use the full name:v = ROOT.std.vector( "unsigned short" )()
(note the extra set of parentheses to instantiate the object; the first just gets the specific template instantiation of the class).

Cheers,
WIm

Many thanks Wim!! :slight_smile:

And have a nice day!

Ric.

Mmm…sorry Wim, but I have a problem updating the value of an element of the vector of unsigned short.

If I try to do this below I get an error:

>>> import ROOT
>>> v = ROOT.std.vector( "unsigned short" )()
>>> v.push_back(1)
>>> v.push_back(1)
>>> v[1]
1
>>> v[1] = 0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NotImplementedError: no __setitem__ handler for return type (unsigned short&)

But if I do the same with a vector of “normal” int’s is working fine.

>>> v = ROOT.std.vector( int )()
>>> v.push_back(1)
>>> v.push_back(1)
>>> v[1]
1
>>> v[1] = 0
>>> v[1]
0
>>> 

Have you any idea about that?

Thanks a lot again for your help :slight_smile:

Ric.

Ric,

sorry, seems I missed that one (or there might be a good reason for its absense: an int is easier to deal with). A possible python-side solution (albeit that it will be slow):[code]from ROOT import std

def setitem( self, idx, value ):
if idx < self.size():
b = self.begin()
while idx:
b = b.preinc()
idx -= 1
self.insert( b, value )
self.erase( b.preinc() )
else:
raise IndexError( ‘index out of range’ )

std.vector( ‘unsigned short’ ).setitem = setitem
del setitem[/code]
I’ll have a look and see whether I can fix it up on the C++side.

Cheers,
Wim

Ric,

in now. Problem was that for most cases Int_t and UInt_t fell in for Short_t and UShort_t (given that python doesn’t have a short type), but those won’t work for references, and so a special case had to be added.

Cheers,
Wim

Many many thanks for your help, Wim!! :slight_smile:

So I’ll download the HEAD version from svn.

Thanks again… and Happy Easter! :slight_smile:

Ric.