Std::vector containing custom objects

Dear all,

I’m trying to use a pyROOT script with a class that returns a vector of a struct:

#the struct (note: outside any namespace)

struct TripRegion{
    double eta1;
    double eta2;
    double phi1;
    double phi2;

#the function

std::vector<TripRegion> getEtaPhi(int run, int lbn);

I’m loading the class as a library in my pyROOT macro:


however, I suspect I need to do something special to access this function:

    TripRegions = ttr.getEtaPhi(runNumber, thelb) 
    print TripRegions
    print len(TripRegions)

leads to:

cate@catelenovolinux:~/Work/DijetResonances2012Full/BCH/TileTripReaderMapping/MakeMapOfTrips$ python 
TClass::TClass:0: RuntimeWarning: no dictionary for class TripRegion is available
Note: (file "(tmpfile)", line 2) File "vector" already loaded
<ROOT.vector<TripRegion> object at 0x323ee10>
Traceback (most recent call last):
  File "", line 45, in <module>
    print len(TripRegions)
TypeError: object of type 'vector<TripRegion,allocator<TripRegion> >' has no len()

My first guess would be that I have to generate a dictionary for the struct, but even in that case I’d appreciate guidance as I’ve never done it before and my basic attempts following … dictionary have not succeeded.



yes, a dictionary is needed. If the header for the class (which I presume to be TripRegion.h, change as appropriate) is accessible from a standard include path or local directory, then this should do:ROOT.gInterpreter.GenerateDictionary("vector<TripRegion>","TripRegion.h;vector")
If you need more details, then let us know what “not succeeded” means in your case.


Dear Wim,

thanks for your quick reply. This time things worked out with


and I can use the standard attributes of a stl::vector. I suspect my previous attempt failed because the full header was too complicated to process (the struct was inside another class and CINT tried to generate a dictionary for the entire thing), and now that the struct is in its separate header it works.

I now have a followup question on how to access the attributes of my struct:

    print TripRegions
    print len(TripRegions)
    print TripRegions[0]
    print TripRegions[0].eta1


<ROOT.vector<TripRegion> object at 0x2fb02c0>
<ROOT.TripRegion object at 0x2fb0410>
Traceback (most recent call last):
  File "", line 47, in <module>
    print TripRegions[0].eta1
AttributeError: 'TripRegion' object has no attribute 'eta1'

where the problem is in trying to access eta1.


Actually I just realised I had to generate a separate dictionary for the class:

as the dictionary for the vector won’t take care of that.

Sorry for the noise and thanks again!