Std maps in PyROOT

Dear experts,

I want to use in PyROOT a C++ function that takes as input a map of the kind

How can I create a PyROOT object that could be given as input to the aforementioned function?
What is the simplest way to fill this map?

Many thanks,

Maurizio

Hi,

the following should have worked:[code]import ROOT
ROOT.gInterpreter.GenerateDictionary(“std::map<std::string, RooRealVar>”, “map;string;RooRealVar.h”)

m = ROOT.std.map(‘string, RooRealVar’)()
m[“aap”] = ROOT.RooRealVar(‘noot’, ‘mies’, 0)[/code]but I’m seeing unexplained crashes in the RooRealVar copy constructor (which has an odd-ball signature). Code like the above works if e.g. a TObject is used instead of RooRealVar.

So, I tried the same in C++ and see a crash in the copy constructor there as well. Is it known how the std::map<std::string, RooRealVar> is to be used at all?

Cheers,
Wim

Was a solution to this problem ever found?

I’m trying to do the same thing with an std::map from std::string to RooDataSet. I can construct the map, but the code seg faults when I try to fill it:

from ROOT import *
gInterpreter.GenerateDictionary("std::map<std::string, RooDataSet>", "map;string;RooDataSet.h")
tmp_map = std.map ('string, RooDataSet')()
tmp_dataset = RooDataSet ()
tmp_map["tmp"] = tmp_dataset # <-- Seg fault when processing this line

The stack trace is as follows:

===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================

Thread 2 (Thread 0x7f5b13646700 (LWP 11093)):
#0  0x00007f5b216f5ee0 in sem_wait () from /lib64/libpthread.so.0
#1  0x00007f5b21a0c875 in PyThread_acquire_lock () from /usr/lib64/libpython2.7.so.1.0
#2  0x00007f5b219dc654 in PyEval_RestoreThread () from /usr/lib64/libpython2.7.so.1.0
#3  0x00007f5b136492e4 in ?? () from /usr/lib64/python2.7/lib-dynload/time.so
#4  0x00007f5b219e277b in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#5  0x00007f5b219e34d2 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.7.so.1.0
#6  0x00007f5b21974aae in ?? () from /usr/lib64/libpython2.7.so.1.0
#7  0x00007f5b219502de in PyObject_Call () from /usr/lib64/libpython2.7.so.1.0
#8  0x00007f5b219e00e7 in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#9  0x00007f5b219e233e in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#10 0x00007f5b219e233e in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#11 0x00007f5b219e34d2 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.7.so.1.0
#12 0x00007f5b2197499e in ?? () from /usr/lib64/libpython2.7.so.1.0
#13 0x00007f5b219502de in PyObject_Call () from /usr/lib64/libpython2.7.so.1.0
#14 0x00007f5b2195e7a0 in ?? () from /usr/lib64/libpython2.7.so.1.0
#15 0x00007f5b219502de in PyObject_Call () from /usr/lib64/libpython2.7.so.1.0
#16 0x00007f5b219dcce7 in PyEval_CallObjectWithKeywords () from /usr/lib64/libpython2.7.so.1.0
#17 0x00007f5b21a107a2 in ?? () from /usr/lib64/libpython2.7.so.1.0
#18 0x00007f5b216eff3b in start_thread () from /lib64/libpthread.so.0
#19 0x00007f5b2142b26d in clone () from /lib64/libc.so.6

Thread 1 (Thread 0x7f5b21ea6700 (LWP 11086)):
#0  0x00007f5b213fa4f7 in waitpid () from /lib64/libc.so.6
#1  0x00007f5b21381c80 in ?? () from /lib64/libc.so.6
#2  0x00007f5b21382040 in system () from /lib64/libc.so.6
#3  0x00007f5b18b6d845 in TUnixSystem::StackTrace() () from /usr/lib64/root/libCore.so.5.34
#4  0x00007f5b18b70273 in TUnixSystem::DispatchSignals(ESignals) () from /usr/lib64/root/libCore.so.5.34
#5  <signal handler called>
#6  0x00007f5b10e13b9b in RooAbsData::RooAbsData(RooAbsData const&, char const*) () from /usr/lib64/root/libRooFitCore.so
#7  0x00007f5b10e83aa3 in RooDataSet::RooDataSet(RooDataSet const&, char const*) () from /usr/lib64/root/libRooFitCore.so
#8  0x00007f5b0b634ca6 in std::map<std::string, RooDataSet, std::less<std::string>, std::allocator<std::pair<std::string const, RooDataSet> > >::operator[](std::string const&) () from /home/eberry/Desktop/AutoDict_std__map_std__string__RooDataSet__cxx.so
#9  0x00007f5b0b633ba5 in G__AutoDict_std__map_std__string__RooDataSet__cxx_ACLiC_dict_3576_0_13(G__value*, char const*, G__param*, int) () from /home/eberry/Desktop/AutoDict_std__map_std__string__RooDataSet__cxx.so
#10 0x00007f5b180cecff in Cint::G__CallFunc::Execute(void*) () from /usr/lib64/root/libCint.so.5.34
#11 0x00007f5b19bbd756 in PyROOT::TRootObjectRefExecutor::Execute(Cint::G__CallFunc*, void*, bool) () from /usr/lib64/root/libPyROOT.so
#12 0x00007f5b19bd72f7 in PyROOT::TMethodHolder<PyROOT::TScopeAdapter, PyROOT::TMemberAdapter>::CallSafe(void*, bool) () from /usr/lib64/root/libPyROOT.so
#13 0x00007f5b19bd74b9 in PyROOT::TMethodHolder<PyROOT::TScopeAdapter, PyROOT::TMemberAdapter>::Execute(void*, bool) () from /usr/lib64/root/libPyROOT.so
#14 0x00007f5b19bd6934 in PyROOT::TMethodHolder<PyROOT::TScopeAdapter, PyROOT::TMemberAdapter>::operator()(PyROOT::ObjectProxy*, _object*, _object*, long, bool) () from /usr/lib64/root/libPyROOT.so
#15 0x00007f5b19bb9811 in ?? () from /usr/lib64/root/libPyROOT.so
#16 0x00007f5b219502de in PyObject_Call () from /usr/lib64/libpython2.7.so.1.0
#17 0x00007f5b219a4427 in ?? () from /usr/lib64/libpython2.7.so.1.0
#18 0x00007f5b219a4748 in ?? () from /usr/lib64/libpython2.7.so.1.0
#19 0x00007f5b219dea5b in PyEval_EvalFrameEx () from /usr/lib64/libpython2.7.so.1.0
#20 0x00007f5b219e34d2 in PyEval_EvalCodeEx () from /usr/lib64/libpython2.7.so.1.0
#21 0x00007f5b219e35e2 in PyEval_EvalCode () from /usr/lib64/libpython2.7.so.1.0
#22 0x00007f5b219fc25a in ?? () from /usr/lib64/libpython2.7.so.1.0
#23 0x00007f5b219fd072 in PyRun_FileExFlags () from /usr/lib64/libpython2.7.so.1.0
#24 0x00007f5b219fda9d in PyRun_SimpleFileExFlags () from /usr/lib64/libpython2.7.so.1.0
#25 0x00007f5b21a0eba5 in Py_Main () from /usr/lib64/libpython2.7.so.1.0
#26 0x00007f5b21361bf5 in __libc_start_main () from /lib64/libc.so.6
#27 0x00000000004009fd in _start ()
===========================================================


The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug report at
http://root.cern.ch/bugs. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#6  0x00007f5b10e13b9b in RooAbsData::RooAbsData(RooAbsData const&, char const*) () from /usr/lib64/root/libRooFitCore.so
#7  0x00007f5b10e83aa3 in RooDataSet::RooDataSet(RooDataSet const&, char const*) () from /usr/lib64/root/libRooFitCore.so
#8  0x00007f5b0b634ca6 in std::map<std::string, RooDataSet, std::less<std::string>, std::allocator<std::pair<std::string const, RooDataSet> > >::operator[](std::string const&) () from /home/eberry/Desktop/AutoDict_std__map_std__string__RooDataSet__cxx.so
===========================================================

Edmund

Edmund,

sorry for the late reply, but I’m traveling (and will be for a couple more weeks).

No the problem has not been solved, AFAIK. However, I do not believe you have the same issue as in the other topic you indicate using RooRealVar*, whereas the problem here is RooRealVar. Only the latter will have its copy constructor called by the std::map.

Cheers,
Wim

I’m trying to do something identical to eberry.

It seems to work on ROOT version 5.34.12-x86_64-slc5-gcc4.3 when I use the code:

import ROOT
from ROOT import *

somevariable = ROOT.std.map("string,RooDataSet")()

somevariable["test"] = ....

but it doesn’t work on the version of ROOT associated with Athena AtlasProduction,17.2.3.5,slc5.

Is there a way to make this work on this version of PyROOT?

Cheers,
Laurence

Laurence,

my guess is that in more recent versions, the auto-dict generator will kick in; whereas in older versions, you’ll have to supply the dictionary yourself.

Can you check whether “somevariable” has (std::map) methods in the older release?

Cheers,
Wim

[quote=“wlav”]Laurence,

my guess is that in more recent versions, the auto-dict generator will kick in; whereas in older versions, you’ll have to supply the dictionary yourself.

Can you check whether “somevariable” has (std::map) methods in the older release?

Cheers,
Wim[/quote]
Hi Wim,

It does have access to the std::map because the following code works:

import ROOT
from ROOT import *
somevariable = ROOT.std.map("int,int")()
somevariable[1] = 2
print somevariable[1]

It only seems to have trouble when I try and make a map which includes ROOT specific objects, eg:

import ROOT
from ROOT import *
somevariable = ROOT.std.map("TString,int")()

When I run this code I get the following error:

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/cvmfs/atlas.cern.ch/repo/sw/software/i686-slc5-gcc43-opt/17.2.3/sw/lcg/app/releases/ROOT/5.30.05/i686-slc5-gcc43-opt/root/lib/ROOT.py", line 179, in __call__ result = _root.MakeRootTemplateClass( *newargs ) AttributeError: type object 'ROOT' has no attribute 'map<TString,int>'

I’ve tried creating a new dictionary using the command:

but this fails to compile and gives the error message:

In file included from /tmp/rootcint_kaUQ1L.h:3, from /tmp/IQol24_cint.cxx:1: /data/lspiller/PhysicsNtuple/PhysicsLight/AutoDict_map_TString_int.cxx:1:25: error: map,TString.h: No such file or directory Error: external preprocessing failed. :0: !!!Removing /data/lspiller/PhysicsNtuple/PhysicsLight/AutoDict_map_TString_int_cxx_ACLiC_dict.cxx /data/lspiller/PhysicsNtuple/PhysicsLight/AutoDict_map_TString_int_cxx_ACLiC_dict.h !!! Error: /cvmfs/atlas.cern.ch/repo/sw/software/i686-slc5-gcc43-opt/17.2.3/sw/lcg/app/releases/ROOT/5.30.05/i686-slc5-gcc43-opt/root/bin/rootcint: error loading headers... Error in <ACLiC>: Dictionary generation failed! AutoDict_map_TString_int.cxx:1:25: error: map,TString.h: No such file or directory 2

Cheers,
Laurence

Laurence,

[quote=“lspiller”]It does have access to the std::map because the following code works:[/quote]no; each std::map instantiation is its own class and should thus carry its own dictionary. The fact that int, int works says nothing about other instantiations.

As for the error that you see, did you first try:ROOT.gInterpreter.GenerateDictionary("map<TString,int>","map,TString.h")
(note the comma instead of semi-colon) before the code that you posted? If so, wipe out all AutoDict* files in your local directory and try again (but of course with ‘;’ instead of ‘,’).

HTH,
Wim

[quote=“wlav”]Laurence,

[quote=“lspiller”]It does have access to the std::map because the following code works:[/quote]no; each std::map instantiation is its own class and should thus carry its own dictionary. The fact that int, int works says nothing about other instantiations.

As for the error that you see, did you first try:ROOT.gInterpreter.GenerateDictionary("map<TString,int>","map,TString.h")
(note the comma instead of semi-colon) before the code that you posted? If so, wipe out all AutoDict* files in your local directory and try again (but of course with ‘;’ instead of ‘,’).

HTH,
Wim[/quote]
I tried running the the command you suggested but got the following error message:

>>> ROOT.gInterpreter.GenerateDictionary("map<TString,int>","map,TString.h") In file included from /tmp/rootcint_6ZobIs.h:3, from /tmp/tmLmLu_cint.cxx:1: /data/lspiller/AutoDict_map_TString_int_.cxx:1:25: error: map,TString.h: No such file or directory Error: external preprocessing failed. :0: !!!Removing /data/lspiller/AutoDict_map_TString_int__cxx_ACLiC_dict.cxx /data/lspiller/AutoDict_map_TString_int__cxx_ACLiC_dict.h !!! Error: /cvmfs/atlas.cern.ch/repo/sw/software/i686-slc5-gcc43-opt/17.2.3/sw/lcg/app/releases/ROOT/5.30.05/i686-slc5-gcc43-opt/root/bin/rootcint: error loading headers... Error in <ACLiC>: Dictionary generation failed! AutoDict_map_TString_int_.cxx:1:25: error: map,TString.h: No such file or directory 2

Laurence,

please (re)read what I wrote. I did not suggest you to run that command. I asked if you had run that command before running with ‘;’. If you did, you will have left broken AutoDict* files in your directory that explain the error message. (If you did not, than I can not explain the error message.)

What I did suggest, is that you remove all AutoDict* files and the re-run the command, making sure to use ‘;’ and NOT ‘,’.

Cheers,
Wim