How to STL vector<custom_object>, object C++ defined (S

Hi All,

Sorry for bothering, but I could not find an answer to my question while googling around.

My question relates to the following. I have defined a class in c++

class pcalc
	Double_t pval;
	Int_t data;
	Double_t mc_sm;
	Double_t error;
	Double_t low_range;
	Double_t up_range;

In the c++ code, a vector is filled with these. So a vector result. This is in the end all stored in a tree.
Afterwards, I would like to be open to examine such an object in python. I would like to do this for example:

gSystem.Load("") //Defines object pcalc
	for entry in tree:
            print entry.pcalc[0].pval

Giving the error
TypeError: ‘vector’ object does not support indexing

However, since this object pcalc is now known to python, it does not know how to acces the vector or loop over these indices. This might be the wrong forum, but how do I teach python how to handle this?

Kind regards,

Ok, I figured out how to make object pcalc known to python. However, even still I can not acces this vector. It does not know anything about a vector for example vector.Size(), even though the code is perfectly able to do such operations over vectors of floats for example.


this:TypeError: 'vector<pcalc>' object does not support indexinghappens if the dictionary is not loaded. As for ‘vector.Size()’, the member function is with a lower case ‘s’ (i.e. size()). You can use dir() to check whether methods exist or not.

(The reason that you get this half-working std::vector is that the I/O layer knows its structure, so it can actually read it back without a dictionary, but you need a dictionary for interactivity. This is all fixed in ROOT6, as Cling can automatically instantiate templates.)



Thanks for the response. I know that it should be .size(), this was a quick example. However, the problem is not that the object pcalc is defined, I want python to be able to handle vector objects. How do I make/generate(?) a dictionary so such an vector of this object can be handled using root 5? Using root 6 is not an option (at the moment).



For ROOT 5 you simply add a dictionary for vector to wherever you generate the dictionary for pcalc, i.e. by adding it to the linkdef file, or by adding it to the selection.xml file and creating a dummy vector in the header that gets passed to genreflex.

Or you call gInterpreter.GenerateDictionary(“vector”, “vector;TheHeaderThatDefinesPCalc.h”) after loading

Cheers, Axel.


Solved. Thanks both.

Kind regards,