Segmentation violation TLorentzVector

Hello experts,

I have some code which works like this:

    # ==================================================================

    xp4 = TClonesArray('TLorentzVector')
    chain.SetBranchAddress('xP4', AddressOf(xp4))

    kstarp4 = TClonesArray('TLorentzVector')
    chain.SetBranchAddress('KstarP4', AddressOf(kstarp4))

    for i in xrange(100):
        ientry = chain.LoadTree(i)
        if ientry < 0:
            break

        nb = chain.GetEntry(i)
        if nb <= 0:
            continue

        nxcand = chain.nXcand
        if nxcand < 1:
            continue
        
        for nx in range(nxcand):
            b0p4 = chain.xP4[nx]
            kstarp4 = chain.KstarP4[nx]
            print i, b0p4.E(), kstarp4.E()
            #oniap4 = b0p4 - kstarp4
            
    sys.exit()
   # ==================================================================

However, as long as I uncomment the line (oniap4 = b0p4 - kstarp4), it breaks:

5 18.5496659189 3.47647306509

 *** Break *** segmentation violation



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

Thread 2 (Thread 0x40f22940 (LWP 4454)):
#0  0x00002aecdec8fd21 in sem_wait () from /lib64/libpthread.so.0
#1  0x00002aecde9965a8 in PyThread_acquire_lock (lock=0x64871f0, waitflag=128)
    at Python/thread_pthread.h:349
#2  0x00002aecde966774 in PyEval_RestoreThread (tstate=0x6fbfca0)
    at Python/ceval.c:334
#3  0x00002aece504b42f in floatsleep (self=<value optimized out>, 
    args=<value optimized out>)
    at /build/42M/BUILD/slc5_amd64_gcc434/external/python/2.6.4-cms14/Python-2.6.4/Mod
ules/timemodule.c:921                                                                
#4  time_sleep (self=<value optimized out>, args=<value optimized out>)
    at /build/42M/BUILD/slc5_amd64_gcc434/external/python/2.6.4-cms14/Python-2.6.4/Mod
ules/timemodule.c:206                                                                
#5  0x00002aecde96b13d in call_function (f=0x6fc6660, 
    throwflag=<value optimized out>) at Python/ceval.c:3706
#6  PyEval_EvalFrameEx (f=0x6fc6660, throwflag=<value optimized out>)
    at Python/ceval.c:2389
#7  0x00002aecde96cda9 in PyEval_EvalCodeEx (co=0x2aecdf91bf30, 
    globals=<value optimized out>, locals=<value optimized out>, 
    args=0x2aece3408ae8, argcount=1, kws=0x6b7e1a0, kwcount=0, defs=0x0, defcount=0, 
    closure=0x0) at Python/ceval.c:2968
#8  0x00002aecde8f3ee4 in function_call (func=0x2aece33e0938, arg=0x2aece3408ad0, 
    kw=0x6fc4af0) at Objects/funcobject.c:524
#9  0x00002aecde8c64e8 in PyObject_Call (func=0x2aece33e0938, arg=0x2aece3408ad0, 
    kw=0x6fc4af0) at Objects/abstract.c:2492
#10 0x00002aecde968792 in ext_do_call (f=0x6fc6480, throwflag=<value optimized out>)
    at Python/ceval.c:4019
#11 PyEval_EvalFrameEx (f=0x6fc6480, throwflag=<value optimized out>)
    at Python/ceval.c:2429
#12 0x00002aecde96c36a in fast_function (f=0x6fc6220, 
    throwflag=<value optimized out>) at Python/ceval.c:3792
#13 call_function (f=0x6fc6220, throwflag=<value optimized out>)
    at Python/ceval.c:3727
#14 PyEval_EvalFrameEx (f=0x6fc6220, throwflag=<value optimized out>)
    at Python/ceval.c:2389
#15 0x00002aecde96c36a in fast_function (f=0x6fc6050, 
    throwflag=<value optimized out>) at Python/ceval.c:3792
#16 call_function (f=0x6fc6050, throwflag=<value optimized out>)
    at Python/ceval.c:3727
#17 PyEval_EvalFrameEx (f=0x6fc6050, throwflag=<value optimized out>)
    at Python/ceval.c:2389
#18 0x00002aecde96cda9 in PyEval_EvalCodeEx (co=0x2aece34067b0, 
    globals=<value optimized out>, locals=<value optimized out>, 
    args=0x2aece3408aa8, argcount=1, kws=0x0, kwcount=0, defs=0x0, defcount=0, 
    closure=0x0) at Python/ceval.c:2968
#19 0x00002aecde8f3ddd in function_call (func=0x2aece3416d70, arg=0x2aece3408a90, 
    kw=0x0) at Objects/funcobject.c:524
#20 0x00002aecde8c64e8 in PyObject_Call (func=0x2aece3416d70, arg=0x2aece3408a90, 
    kw=0x0) at Objects/abstract.c:2492
#21 0x00002aecde8d69fc in instancemethod_call (func=0x2aece3416d70, 
    arg=0x2aece3408a90, kw=0x0) at Objects/classobject.c:2579
#22 0x00002aecde8c64e8 in PyObject_Call (func=0x2aece33a9cd0, arg=0x2aecdea3b050, 
    kw=0x0) at Objects/abstract.c:2492
#23 0x00002aecde965d66 in PyEval_CallObjectWithKeywords (func=0x2aece33a9cd0, 
    arg=0x2aecdea3b050, kw=0x0) at Python/ceval.c:3575
#24 0x00002aecde99c26d in t_bootstrap (boot_raw=0x6fbf480)
    at ./Modules/threadmodule.c:425
#25 0x00002aecdec8973d in start_thread () from /lib64/libpthread.so.0
#26 0x00002aecdf5fd4bd in clone () from /lib64/libc.so.6
#27 0x0000000000000000 in ?? ()

Thread 1 (Thread 0x2aecdf882230 (LWP 4447)):
#0  0x00002aecdf5c314f in waitpid () from /lib64/libc.so.6
#1  0x00002aecdf565481 in do_system () from /lib64/libc.so.6
#2  0x00002aecdf5657d7 in system () from /lib64/libc.so.6
#3  0x00002aece0139222 in TUnixSystem::StackTrace() ()
   from /afs/cern.ch/cms/slc5_amd64_gcc434/cms/cmssw/CMSSW_4_2_8/external/slc5_amd64_g
cc434/lib/libCore.so                                                                 
#4  0x00002aece0139cdc in TUnixSystem::DispatchSignals(ESignals) ()
   from /afs/cern.ch/cms/slc5_amd64_gcc434/cms/cmssw/CMSSW_4_2_8/external/slc5_amd64_g
cc434/lib/libCore.so                                                                 
#5  <signal handler called>
#6  0x00002aece0386f48 in ROOT::delete_TClonesArray(void*) ()
   from /afs/cern.ch/cms/slc5_amd64_gcc434/cms/cmssw/CMSSW_4_2_8/external/slc5_amd64_g
cc434/lib/libCore.so                                                                 
#7  0x00002aece0104967 in TClass::Destructor(void*, bool) ()
   from /afs/cern.ch/cms/slc5_amd64_gcc434/cms/cmssw/CMSSW_4_2_8/external/slc5_amd64_g
cc434/lib/libCore.so                                                                 
#8  0x00002aece1e0f787 in TLeafObject::ReadBasket(TBuffer&) ()
   from /afs/cern.ch/cms/slc5_amd64_gcc434/cms/cmssw/CMSSW_4_2_8/external/slc5_amd64_g
cc434/lib/libTree.so                                                                 
#9  0x00002aece1dd572c in TBranch::ReadLeavesImpl(TBuffer&) ()
   from /afs/cern.ch/cms/slc5_amd64_gcc434/cms/cmssw/CMSSW_4_2_8/external/slc5_amd64_g
cc434/lib/libTree.so                                                                 
#10 0x00002aece1dd72e5 in TBranch::GetEntry(long long, int) ()
   from /afs/cern.ch/cms/slc5_amd64_gcc434/cms/cmssw/CMSSW_4_2_8/external/slc5_amd64_g
cc434/lib/libTree.so                                                                 
#11 0x00002aece1e1e760 in TTree::GetEntry(long long, int) ()
   from /afs/cern.ch/cms/slc5_amd64_gcc434/cms/cmssw/CMSSW_4_2_8/external/slc5_amd64_g
cc434/lib/libTree.so                                                                 
#12 0x00002aece1ea8a07 in G__G__Tree_110_0_73(G__value*, char const*, G__param*, int)
    ()
   from /afs/cern.ch/cms/slc5_amd64_gcc434/cms/cmssw/CMSSW_4_2_8/external/slc5_amd64_g
cc434/lib/libTree.so                                                                 
#13 0x00002aece06a6bbc in Cint::G__CallFunc::Execute(void*) ()
   from /afs/cern.ch/cms/slc5_amd64_gcc434/cms/cmssw/CMSSW_4_2_8/external/slc5_amd64_g
cc434/lib/libCint.so                                                                 
#14 0x00002aecdfe89951 in PyROOT::TIntExecutor::Execute(Cint::G__CallFunc*, void*) ()
   from /afs/cern.ch/cms/slc5_amd64_gcc434/lcg/root/5.27.06b-cms23/lib/libPyROOT.so
#15 0x00002aecdfe8f70e in PyROOT::TMethodHolder<PyROOT::TScopeAdapter, PyROOT::TMember
Adapter>::CallSafe(void*) ()                                                         
   from /afs/cern.ch/cms/slc5_amd64_gcc434/lcg/root/5.27.06b-cms23/lib/libPyROOT.so
#16 0x00002aecdfe8f8bc in PyROOT::TMethodHolder<PyROOT::TScopeAdapter, PyROOT::TMember
Adapter>::Execute(void*) ()                                                          
   from /afs/cern.ch/cms/slc5_amd64_gcc434/lcg/root/5.27.06b-cms23/lib/libPyROOT.so
#17 0x00002aecdfe8e57c in PyROOT::TMethodHolder<PyROOT::TScopeAdapter, PyROOT::TMember
Adapter>::operator()(PyROOT::ObjectProxy*, _object*, _object*, long) ()              
   from /afs/cern.ch/cms/slc5_amd64_gcc434/lcg/root/5.27.06b-cms23/lib/libPyROOT.so
#18 0x00002aecdfe9522e in PyROOT::(anonymous namespace)::mp_call(PyROOT::MethodProxy*,
 _object*, _object*) ()                                                              
   from /afs/cern.ch/cms/slc5_amd64_gcc434/lcg/root/5.27.06b-cms23/lib/libPyROOT.so
#19 0x00002aecde8c64e8 in PyObject_Call (func=0x2aaaab46d810, arg=0x2aece3414b10, 
    kw=0x0) at Objects/abstract.c:2492
#20 0x00002aecde969522 in do_call (f=0x7619f30, throwflag=<value optimized out>)
    at Python/ceval.c:3924
#21 call_function (f=0x7619f30, throwflag=<value optimized out>)
    at Python/ceval.c:3729
#22 PyEval_EvalFrameEx (f=0x7619f30, throwflag=<value optimized out>)
    at Python/ceval.c:2389
#23 0x00002aecde96cda9 in PyEval_EvalCodeEx (co=0x2aecdf915cd8, 
    globals=<value optimized out>, locals=<value optimized out>, args=0x4, 
    argcount=3, kws=0x61dde50, kwcount=1, defs=0x2aece3414ae8, defcount=1, 
    closure=0x0) at Python/ceval.c:2968
#24 0x00002aecde96b218 in fast_function (f=0x61ddc70, 
    throwflag=<value optimized out>) at Python/ceval.c:3802
#25 call_function (f=0x61ddc70, throwflag=<value optimized out>)
    at Python/ceval.c:3727
#26 PyEval_EvalFrameEx (f=0x61ddc70, throwflag=<value optimized out>)
    at Python/ceval.c:2389
#27 0x00002aecde96c36a in fast_function (f=0x61dd720, 
    throwflag=<value optimized out>) at Python/ceval.c:3792
#28 call_function (f=0x61dd720, throwflag=<value optimized out>)
    at Python/ceval.c:3727
#29 PyEval_EvalFrameEx (f=0x61dd720, throwflag=<value optimized out>)
    at Python/ceval.c:2389
#30 0x00002aecde96c36a in fast_function (f=0x61dd540, 
    throwflag=<value optimized out>) at Python/ceval.c:3792
#31 call_function (f=0x61dd540, throwflag=<value optimized out>)
    at Python/ceval.c:3727
#32 PyEval_EvalFrameEx (f=0x61dd540, throwflag=<value optimized out>)
    at Python/ceval.c:2389
#33 0x00002aecde96c36a in fast_function (f=0x61dd110, 
    throwflag=<value optimized out>) at Python/ceval.c:3792
#34 call_function (f=0x61dd110, throwflag=<value optimized out>)
    at Python/ceval.c:3727
#35 PyEval_EvalFrameEx (f=0x61dd110, throwflag=<value optimized out>)
    at Python/ceval.c:2389
#36 0x00002aecde96cda9 in PyEval_EvalCodeEx (co=0x2aecdf88ad50, 
    globals=<value optimized out>, locals=<value optimized out>, args=0x0, 
    argcount=0, kws=0x0, kwcount=0, defs=0x0, defcount=0, closure=0x0)
    at Python/ceval.c:2968
#37 0x00002aecde96cec2 in PyEval_EvalCode (co=0x7eef340, globals=0x7eef340, 
    locals=0x0) at Python/ceval.c:522
#38 0x00002aecde98ccfa in run_mod (fp=0x61c95a0, 
    filename=0x7fff3acb998c "/afs/cern.ch/user/x/xshi/local/bin/afb", 
    start=<value optimized out>, globals=0x6179ef0, locals=0x6179ef0, closeit=1, 
    flags=0x7fff3acb7060) at Python/pythonrun.c:1335
#39 PyRun_FileExFlags (fp=0x61c95a0, 
    filename=0x7fff3acb998c "/afs/cern.ch/user/x/xshi/local/bin/afb", 
    start=<value optimized out>, globals=0x6179ef0, locals=0x6179ef0, closeit=1, 
    flags=0x7fff3acb7060) at Python/pythonrun.c:1321
#40 0x00002aecde98cfed in PyRun_SimpleFileExFlags (fp=0x61c95a0, 
    filename=0x7fff3acb998c "/afs/cern.ch/user/x/xshi/local/bin/afb", closeit=1, 
    flags=0x7fff3acb7060) at Python/pythonrun.c:931
#41 0x00002aecde99a20b in Py_Main (argc=5, argv=0x7fff3acb7188) at Modules/main.c:599
#42 0x00002aecdf546994 in __libc_start_main () from /lib64/libc.so.6
#43 0x0000000000400619 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  0x00002aece0386f48 in ROOT::delete_TClonesArray(void*) ()
   from /afs/cern.ch/cms/slc5_amd64_gcc434/cms/cmssw/CMSSW_4_2_8/external/slc5_amd64_g
cc434/lib/libCore.so                                                                 
#7  0x00002aece0104967 in TClass::Destructor(void*, bool) ()
   from /afs/cern.ch/cms/slc5_amd64_gcc434/cms/cmssw/CMSSW_4_2_8/external/slc5_amd64_g
cc434/lib/libCore.so                                                                 
#8  0x00002aece1e0f787 in TLeafObject::ReadBasket(TBuffer&) ()
   from /afs/cern.ch/cms/slc5_amd64_gcc434/cms/cmssw/CMSSW_4_2_8/external/slc5_amd64_g
cc434/lib/libTree.so                                                                 
#9  0x00002aece1dd572c in TBranch::ReadLeavesImpl(TBuffer&) ()
   from /afs/cern.ch/cms/slc5_amd64_gcc434/cms/cmssw/CMSSW_4_2_8/external/slc5_amd64_g
cc434/lib/libTree.so                                                                 
#10 0x00002aece1dd72e5 in TBranch::GetEntry(long long, int) ()
   from /afs/cern.ch/cms/slc5_amd64_gcc434/cms/cmssw/CMSSW_4_2_8/external/slc5_amd64_g
cc434/lib/libTree.so                                                                 
#11 0x00002aece1e1e760 in TTree::GetEntry(long long, int) ()
   from /afs/cern.ch/cms/slc5_amd64_gcc434/cms/cmssw/CMSSW_4_2_8/external/slc5_amd64_g
cc434/lib/libTree.so                                                                 
===========================================================

Any suggestions?

Thanks!
Xin

Hi,

you hand the C++ side a pointer to kstarp4 at the beginning of the code, and then later assign to that reference. The consequence is that the C++ side memory will go away with a pointer still outstanding. Recommend to keep it alive (the label of the reference does not matter):chain._kstarp = kstarp
Cheers,
Wim

Hi Wim,

Thank you for your quick response! It worked after your suggestion.

For me, one main reason to use Python instead of C++ is to ignore the reference and pointers … :slight_smile:

Xin