TMap in PyROOT

Dear experts,

I try to fill a TMap in ROOT using int’s as key and TVector as values. However, I’m unable to do so, since the arguments to TMap::Add cannot be converted (I guess due to TObject* parameters). What I do is something like this:

mEvents = ROOT.TMap()
someArray = array('f',someList)
myInt = some int
vec = ROOT.TVector(len(someArrary),someArray)
mEvents.Add(myInt, vec)

I already tried switching to ROOT primitive types, i.e. ROOT.Int/Double/TString and also tried ctypes, but non of them worked. Is there any way to do this within python?

Many thanks in advance!

Cheers,

Marcus

Marcus,

nothing to do with python: the interface requires a TObject*, so nothing else will pass. Think about it: if an integer is passed where a TObject* is expected, then the next step will most likely be a crash on the C++ side, as the int will not point to a valid address.

The list of alternatives that you put there has TString, but as you found, that, too, does not derive from TObject. Use TObjString instead.

You can also simply bind an std::map<int, TVector>, which will work. Or, if the integers are consecutive, use an std::vector or some TCollection. If the integers have no meaning other than bookkeeping or ordering, then a THashList may work for you.

Cheers,
Wim

Hi Wim,

thanks a lot for your reply. I tried to switch to std::map<int, std::vector >, by doing:

mEvents = ROOT.std.map(int, ROOT.std.vector(int))

For this I need to generate the corresponding dictionary (right?), so I tried:

ROOT.gInterpreter.GenerateDictionary(“map<int,vector >”,“vector;map”)

Unfortunately the dictionart generation crashes due to errors which I don’t understand:

/amnt/remote/pkthafc.home/morgenst/workarea/2011/HiggsAnalysis/HadHad/macros/AutoDict_map_int_vector_int____cxx_ACLiC_dict.o: In function G__cpp_setup_tagtableAutoDict_map_int_vector_int____cxx_ACLiC_dict': /amnt/remote/pkthafc.home/morgenst/workarea/2011/HiggsAnalysis/HadHad/macros/AutoDict_map_int_vector_int____cxx_ACLiC_dict.cxx:1294: undefined reference toG__get_linked_tagnum_fwd’
**this goes on for a while

I’m even unable to generate the dictionary for simpler object, such as std::vector<double*>. Do you know what I’m doing wrong?

Thanks,

Marcus

Hi,

that linking problem is an open bug in gSystem: https://savannah.cern.ch/bugs/?95188. It can be worked around using: gSystem.SetMakeSharedLib(gSystem.GetMakeSharedLib().replace('-Wl,--as-needed', ''))
Cheers,
Wim

Hi Wim,

thanks a lot. I unfortunately wasn’t able to get this fixed with the proposing in root 5.34.01. So I switched back to 5.32.02 and there it works just fine.

Thanks,

Marcus