Vector<double> dictionnary?

Hi Rooters

I am trying to use std::vector in a python script and I don’t know how to do it.
Namely:

[code]python

import ROOT
from ROOT import std
a = std.vector(int)(10) # ok, vector of 10 int
b = std.vector(float)(10) # ok, vector of 10 float
c = std.vector(double)(10) # !! gloups !!

Traceback (most recent call last):
File “”, line 1, in
NameError: name ‘double’ is not defined

[/code]
It looks like that the dictionnary for std::vector is not built. But if I try to do the same thing using the CINT interpreter:

root
root[0] #include <vector>
root[1] std::vector<double> c(10)

It works, I can use “c” as a vector of double. Using CINT, It seems that the dictionnary for std::vector has been built.
On another hand, I tried something scary with Cint
std::vector with “MyClass” been my own class I added with a shared library and a dictionnary. I have never created a dicitionnary of vector, just a dictionnay of MyClass. It looks like Cint is able to create a vector of MyClass on the fly, meaning template instantiation on the fly???
This could explain why it works using Cint and not Python.

So here are my questions:

  1. why do we have different beahvior using Cint and Python? Am I right thinking Cint is now able to instantiate templates on the fly as previewed in ROOT Workshop 2007?

  2. How can I create a dictionnary (not on the fly) for vector (or vector) so I can use them from the Python interpreter? I tried to add the line:

#pragma link C++ class std::vector<double>+;

in my LinDef.h file but it doesn’t work, it says the dictionnary has already been built?

  1. Python hasn’t got a “double” type, just a float. This may be the reason why vector doesn’t work even if I have a dictionnary for it. So how can I do the mapping when I call a method which expects a vector (in C++) as an argument and I feed it with a vector(float) (in Python?)

Thank for your help

I am using ROOT 5.18.00 and python-2.5.1 on a Linux box (gcc is 3.3.5)

Matthieu,

it’s a simple name error: there is no such thing as type double in python (type float is of C-type double). In general, template arguments need to be given as strings, but for convenience, types are parsed for their name (which is why float as a template argument yields C-type float, even as it is double :slight_smile: ).

Anyway, just do:

or, if you prefer:

double = 'double' c = std.vector(double)()
or even:

[code]class double:
pass

c = std.vector(double)(10)[/code]
HTH,
WIm

Hi Wim

Thanks for your answer. It works!