Something is wrong when using Root and Geant4 to make an application

When i used Root and Geant4 to make an application, if only some Root header files exist, like:

#include “TCanvas.h”
#include “TROOT.h”
#include “TGraphErrors.h”
#include “TGraph.h”
#include “TF1.h”
#include “TLegend.h”
#include “TArrow.h”
#include “TLatex.h”
#include “TLine.h”

no Root code add, the application worked well,
if one line Root code add, like:

TCanvas* c1=new TCanvas(“Pulse Amplitude Spectrum”,“Pulse Amplitude Spectrum”,1200,1200);

the error occurs, like:
*** Break *** segmentation violation

it means the users can not use Root and Geant4 to make an application now?

the Root and Geant4 code can be compiled, but can not be executed well.

Segmentation violations can indicate a range of issues, so I’d suggest running your application in a debugger like gdb which can trace execution and show where the problem occurs. Which versions of Geant4 and ROOT are you using, and with which OS/compiler version?

Geant4 and ROOT should, and do, work fine together in many applications, and debugging can help to locate any problems that do arise.

The Geant4 version is 10.7_p01, and the Root version is 6.24/00, the OS is ubuntu 20.04, the compiler version is GCC 9.3.0.

==================================================================

Run 0 starts.

G4WT0 > /tracking/storeTrajectory 2
G4WT0 > /tracking/storeTrajectory 0
G4WT0 > ### Run 0 starts on worker thread 0.
G4WT0 > Thread-local run terminated.
G4WT0 > Run Summary
G4WT0 > Number of events processed : 1000000
G4WT0 > User=8.870000s Real=8.903216s Sys=0.020000s [Cpu=99.9%]

*** Break *** segmentation violation

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

Thread 5 (Thread 0x7f572538f700 (LWP 11464)):
#0 0x00007f574d95b3bf in __GI___clock_nanosleep (clock_id=clock_id
entry=0, flags=flags
entry=0, req=0x7f5725213650, rem=0x7f5725213650) at …/sysdeps/unix/sysv/linux/clock_nanosleep.c:78
#1 0x00007f574d961047 in __GI___nanosleep (requested_time=, remaining=) at nanosleep.c:27
#2 0x00007f574f692511 in G4VisManager::G4VisSubThread(void*) () from /usr/local/Geant4_10.7_Qt/lib/libG4vis_management.so
#3 0x00007f574db5ede4 in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007f5749c69609 in start_thread (arg=) at pthread_create.c:477
#5 0x00007f574d99d293 in clone () at …/sysdeps/unix/sysv/linux/x86_64/clone.S:95

Thread 4 (Thread 0x7f573aa25700 (LWP 11461)):
#0 0x00007f574d960dff in __GI___wait4 (pid=11481, stat_loc=stat_loc
entry=0x7f573a8a5d28, options=options
entry=0, usage=usage
entry=0x0) at …/sysdeps/unix/sysv/linux/wait4.c:27
#1 0x00007f574d960d7b in __GI___waitpid (pid=, stat_loc=stat_loc
entry=0x7f573a8a5d28, options=options
entry=0) at waitpid.c:38
#2 0x00007f574d8d00e7 in do_system (line=) at …/sysdeps/posix/system.c:172
#3 0x00007f574f059bfe in TUnixSystem::StackTrace() () from /usr/local/root/lib/libCore.so.6.24
#4 0x00007f574f056a85 in TUnixSystem::DispatchSignals(ESignals) () from /usr/local/root/lib/libCore.so.6.24
#5
#6 0x00007f572ff9084c in ?? () from /lib/x86_64-linux-gnu/libLLVM-11.so.1
#7 0x00007f571b11b388 in std::_Function_handler<llvm::Expected (), llvm::orc::LazyEmittingLayer<llvm::orc::LegacyIRCompileLayer<cling::IncrementalJIT::RemovableObjectLinkingLayer, llvm::orc::SimpleCompiler> >::EmissionDeferredModule::find(llvm::StringRef, bool, llvm::orc::LegacyIRCompileLayer<cling::IncrementalJIT::RemovableObjectLinkingLayer, llvm::orc::SimpleCompiler>&)::{lambda()#1}>::_M_invoke(std::_Any_data const&) () from /usr/local/root/lib/libCling.so
#8 0x00007f571b10be0f in cling::IncrementalExecutor::getPointerToGlobalFromJIT(llvm::StringRef) const () from /usr/local/root/lib/libCling.so
#9 0x00007f571b08664e in cling::Interpreter::Interpreter(int, char const* const*, char const*, std::vector<std::shared_ptrclang::ModuleFileExtension, std::allocator<std::shared_ptrclang::ModuleFileExtension > > const&, bool, cling::Interpreter const*) () from /usr/local/root/lib/libCling.so
#10 0x00007f571afaab74 in TCling::TCling(char const*, char const*, char const* const*) () from /usr/local/root/lib/libCling.so
#11 0x00007f571afac8f5 in CreateInterpreter () from /usr/local/root/lib/libCling.so
#12 0x00007f574eef6f63 in TROOT::InitInterpreter() () from /usr/local/root/lib/libCore.so.6.24
#13 0x00007f574eef745f in ROOT::Internal::GetROOT2() () from /usr/local/root/lib/libCore.so.6.24
#14 0x00007f574ef042bd in TApplication::TApplication(char const*, int*, char**, void*, int) () from /usr/local/root/lib/libCore.so.6.24
#15 0x00007f574ef047ea in TApplication::CreateApplication() () from /usr/local/root/lib/libCore.so.6.24
#16 0x00007f574ecbf525 in TCanvas::Init() () from /usr/local/root/lib/libGpad.so.6.24
#17 0x00007f574ecc14e1 in TCanvas::Constructor(char const*, char const*, int, int) () from /usr/local/root/lib/libGpad.so.6.24
#18 0x00007f574ecc3aa1 in TCanvas::TCanvas(char const*, char const*, int, int) () from /usr/local/root/lib/libGpad.so.6.24
#19 0x000055f3f7547652 in fpRunAction::EndOfRunAction(G4Run const*) ()
#20 0x00007f574e9c90c2 in G4RunManager::RunTermination() () from /usr/local/Geant4_10.7_Qt/lib/libG4run.so
#21 0x00007f574e9db9d5 in G4WorkerRunManager::RunTermination() () from /usr/local/Geant4_10.7_Qt/lib/libG4run.so
#22 0x00007f574e9c702b in G4RunManager::BeamOn(int, char const*, int) () from /usr/local/Geant4_10.7_Qt/lib/libG4run.so
#23 0x00007f574e9d8d9b in G4WorkerRunManager::DoWork() () from /usr/local/Geant4_10.7_Qt/lib/libG4run.so
#24 0x00007f574e9e28be in G4MTRunManagerKernel::StartThread(G4WorkerThread*) () from /usr/local/Geant4_10.7_Qt/lib/libG4run.so
#25 0x00007f574db5ede4 in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
#26 0x00007f5749c69609 in start_thread (arg=) at pthread_create.c:477
#27 0x00007f574d99d293 in clone () at …/sysdeps/unix/sysv/linux/x86_64/clone.S:95

Thread 3 (Thread 0x7f5740920700 (LWP 11459)):
#0 0x00007f574d990aff in __GI___poll (fds=0x7f5734004e60, nfds=3, timeout=-1) at …/sysdeps/unix/sysv/linux/poll.c:29
#1 0x00007f574652336e in ?? () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
#2 0x00007f57465234a3 in g_main_context_iteration () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
#3 0x00007f5749f7c1cc in QEventDispatcherGlib::processEvents(QFlagsQEventLoop::ProcessEventsFlag) () from /usr/local/Qt/5.15.2/gcc_64/lib/libQt5Core.so.5
#4 0x00007f5749f1f21a in QEventLoop::exec(QFlagsQEventLoop::ProcessEventsFlag) () from /usr/local/Qt/5.15.2/gcc_64/lib/libQt5Core.so.5
#5 0x00007f5749d35844 in QThread::exec() () from /usr/local/Qt/5.15.2/gcc_64/lib/libQt5Core.so.5
#6 0x00007f5743280fd5 in QDBusConnectionManager::run() () from /usr/local/Qt/5.15.2/gcc_64/lib/libQt5DBus.so.5
#7 0x00007f5749d36b35 in QThreadPrivate::start(void*) () from /usr/local/Qt/5.15.2/gcc_64/lib/libQt5Core.so.5
#8 0x00007f5749c69609 in start_thread (arg=) at pthread_create.c:477
#9 0x00007f574d99d293 in clone () at …/sysdeps/unix/sysv/linux/x86_64/clone.S:95

Thread 2 (Thread 0x7f5742b01700 (LWP 11458)):
#0 0x00007f574d990aff in __GI___poll (fds=0x7f5742985588, nfds=1, timeout=-1) at …/sysdeps/unix/sysv/linux/poll.c:29
#1 0x00007f5748adac1a in ?? () from /lib/x86_64-linux-gnu/libxcb.so.1
#2 0x00007f5748adc90a in xcb_wait_for_event () from /lib/x86_64-linux-gnu/libxcb.so.1
#3 0x00007f57435a27b0 in QXcbEventQueue::run() () from /usr/local/Qt/5.15.2/gcc_64/lib/libQt5XcbQpa.so.5
#4 0x00007f5749d36b35 in QThreadPrivate::start(void*) () from /usr/local/Qt/5.15.2/gcc_64/lib/libQt5Core.so.5
#5 0x00007f5749c69609 in start_thread (arg=) at pthread_create.c:477
#6 0x00007f574d99d293 in clone () at …/sysdeps/unix/sysv/linux/x86_64/clone.S:95

Thread 1 (Thread 0x7f5744a8ef00 (LWP 11457)):
#0 futex_wait_cancelable (private=, expected=0, futex_word=0x55f3f853bbec) at …/sysdeps/nptl/futex-internal.h:183
#1 __pthread_cond_wait_common (abstime=0x0, clockid=0, mutex=0x55f3f853bb98, cond=0x55f3f853bbc0) at pthread_cond_wait.c:508
#2 __pthread_cond_wait (cond=0x55f3f853bbc0, mutex=0x55f3f853bb98) at pthread_cond_wait.c:638
#3 0x00007f574db58e30 in std::condition_variable::wait(std::unique_lockstd::mutex&) () from /lib/x86_64-linux-gnu/libstdc++.so.6
#4 0x00007f574dd69232 in G4MTBarrier::Wait() () from /usr/local/Geant4_10.7_Qt/lib/libG4global.so
#5 0x00007f574e9d43a0 in G4MTRunManager::RunTermination() () from /usr/local/Geant4_10.7_Qt/lib/libG4run.so
#6 0x00007f574e9c702b in G4RunManager::BeamOn(int, char const*, int) () from /usr/local/Geant4_10.7_Qt/lib/libG4run.so
#7 0x00007f574e9e4877 in G4RunMessenger::SetNewValue(G4UIcommand*, G4String) () from /usr/local/Geant4_10.7_Qt/lib/libG4run.so
#8 0x00007f574ddd84ef in G4UIcommand::DoIt(G4String) () from /usr/local/Geant4_10.7_Qt/lib/libG4intercoms.so
#9 0x00007f574ddf5eea in G4UImanager::ApplyCommand(char const*) () from /usr/local/Geant4_10.7_Qt/lib/libG4intercoms.so
#10 0x00007f574f495cdf in G4VBasicShell::ExecuteCommand(G4String const&) () from /usr/local/Geant4_10.7_Qt/lib/libG4interfaces.so
#11 0x00007f574f49af48 in G4VBasicShell::ApplyShellCommand(G4String const&, bool&, bool&) () from /usr/local/Geant4_10.7_Qt/lib/libG4interfaces.so
#12 0x00007f574f48a9fe in G4UIQt::CommandEnteredCallback() () from /usr/local/Geant4_10.7_Qt/lib/libG4interfaces.so
#13 0x00007f5749f58ddf in void doActivate(QObject*, int, void**) () from /usr/local/Qt/5.15.2/gcc_64/lib/libQt5Core.so.5
#14 0x00007f574b1a5025 in QLineEdit::qt_static_metacall(QObject*, QMetaObject::Call, int, void**) () from /usr/local/Qt/5.15.2/gcc_64/lib/libQt5Widgets.so.5
#15 0x00007f5749f58ddf in void doActivate(QObject*, int, void**) () from /usr/local/Qt/5.15.2/gcc_64/lib/libQt5Core.so.5
#16 0x00007f574b1ab33f in QWidgetLineControl::processKeyEvent(QKeyEvent*) () from /usr/local/Qt/5.15.2/gcc_64/lib/libQt5Widgets.so.5
#17 0x00007f574b19edda in QLineEdit::keyPressEvent(QKeyEvent*) () from /usr/local/Qt/5.15.2/gcc_64/lib/libQt5Widgets.so.5
#18 0x00007f574b0a70a7 in QWidget::event(QEvent*) () from /usr/local/Qt/5.15.2/gcc_64/lib/libQt5Widgets.so.5
#19 0x00007f574b1a490e in QLineEdit::event(QEvent*) () from /usr/local/Qt/5.15.2/gcc_64/lib/libQt5Widgets.so.5
#20 0x00007f574b06843c in QApplicationPrivate::notify_helper(QObject*, QEvent*) () from /usr/local/Qt/5.15.2/gcc_64/lib/libQt5Widgets.so.5
#21 0x00007f574b06fb55 in QApplication::notify(QObject*, QEvent*) () from /usr/local/Qt/5.15.2/gcc_64/lib/libQt5Widgets.so.5
#22 0x00007f5749f20808 in QCoreApplication::notifyInternal2(QObject*, QEvent*) () from /usr/local/Qt/5.15.2/gcc_64/lib/libQt5Core.so.5
#23 0x00007f574b0c2d0b in QWidgetWindow::event(QEvent*) () from /usr/local/Qt/5.15.2/gcc_64/lib/libQt5Widgets.so.5
#24 0x00007f574b06843c in QApplicationPrivate::notify_helper(QObject*, QEvent*) () from /usr/local/Qt/5.15.2/gcc_64/lib/libQt5Widgets.so.5
#25 0x00007f574b06ef20 in QApplication::notify(QObject*, QEvent*) () from /usr/local/Qt/5.15.2/gcc_64/lib/libQt5Widgets.so.5
#26 0x00007f5749f20808 in QCoreApplication::notifyInternal2(QObject*, QEvent*) () from /usr/local/Qt/5.15.2/gcc_64/lib/libQt5Core.so.5
#27 0x00007f574a5e4e1b in QGuiApplicationPrivate::processKeyEvent(QWindowSystemInterfacePrivate::KeyEvent*) () from /usr/local/Qt/5.15.2/gcc_64/lib/libQt5Gui.so.5
#28 0x00007f574a5e9935 in QGuiApplicationPrivate::processWindowSystemEvent(QWindowSystemInterfacePrivate::WindowSystemEvent*) () from /usr/local/Qt/5.15.2/gcc_64/lib/libQt5Gui.so.5
#29 0x00007f574a5c58ab in QWindowSystemInterface::sendWindowSystemEvents(QFlagsQEventLoop::ProcessEventsFlag) () from /usr/local/Qt/5.15.2/gcc_64/lib/libQt5Gui.so.5
#30 0x00007f57435a369a in xcbSourceDispatch(_GSource*, int ()(void), void*) () from /usr/local/Qt/5.15.2/gcc_64/lib/libQt5XcbQpa.so.5
#31 0x00007f574652317d in g_main_context_dispatch () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
#32 0x00007f5746523400 in ?? () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
#33 0x00007f57465234a3 in g_main_context_iteration () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
#34 0x00007f5749f7c1cc in QEventDispatcherGlib::processEvents(QFlagsQEventLoop::ProcessEventsFlag) () from /usr/local/Qt/5.15.2/gcc_64/lib/libQt5Core.so.5
#35 0x00007f5749f1f21a in QEventLoop::exec(QFlagsQEventLoop::ProcessEventsFlag) () from /usr/local/Qt/5.15.2/gcc_64/lib/libQt5Core.so.5
#36 0x00007f5749f281d3 in QCoreApplication::exec() () from /usr/local/Qt/5.15.2/gcc_64/lib/libQt5Core.so.5
#37 0x00007f574f48630d in G4UIQt::SessionStart() () from /usr/local/Geant4_10.7_Qt/lib/libG4interfaces.so
#38 0x000055f3f754533f in main ()

The lines below might hint at the cause of the crash.
You may get help by asking at the ROOT forum ROOT Forum
Only if you are really convinced it is a bug in ROOT then please submit a
report at Sign in to GitHub · GitHub Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.

#6 0x00007f572ff9084c in ?? () from /lib/x86_64-linux-gnu/libLLVM-11.so.1
#7 0x00007f571b11b388 in std::_Function_handler<llvm::Expected (), llvm::orc::LazyEmittingLayer<llvm::orc::LegacyIRCompileLayer<cling::IncrementalJIT::RemovableObjectLinkingLayer, llvm::orc::SimpleCompiler> >::EmissionDeferredModule::find(llvm::StringRef, bool, llvm::orc::LegacyIRCompileLayer<cling::IncrementalJIT::RemovableObjectLinkingLayer, llvm::orc::SimpleCompiler>&)::{lambda()#1}>::_M_invoke(std::_Any_data const&) () from /usr/local/root/lib/libCling.so
#8 0x00007f571b10be0f in cling::IncrementalExecutor::getPointerToGlobalFromJIT(llvm::StringRef) const () from /usr/local/root/lib/libCling.so
#9 0x00007f571b08664e in cling::Interpreter::Interpreter(int, char const* const*, char const*, std::vector<std::shared_ptrclang::ModuleFileExtension, std::allocator<std::shared_ptrclang::ModuleFileExtension > > const&, bool, cling::Interpreter const*) () from /usr/local/root/lib/libCling.so
#10 0x00007f571afaab74 in TCling::TCling(char const*, char const*, char const* const*) () from /usr/local/root/lib/libCling.so
#11 0x00007f571afac8f5 in CreateInterpreter () from /usr/local/root/lib/libCling.so
#12 0x00007f574eef6f63 in TROOT::InitInterpreter() () from /usr/local/root/lib/libCore.so.6.24
#13 0x00007f574eef745f in ROOT::Internal::GetROOT2() () from /usr/local/root/lib/libCore.so.6.24
#14 0x00007f574ef042bd in TApplication::TApplication(char const*, int*, char**, void*, int) () from /usr/local/root/lib/libCore.so.6.24
#15 0x00007f574ef047ea in TApplication::CreateApplication() () from /usr/local/root/lib/libCore.so.6.24
#16 0x00007f574ecbf525 in TCanvas::Init() () from /usr/local/root/lib/libGpad.so.6.24
#17 0x00007f574ecc14e1 in TCanvas::Constructor(char const*, char const*, int, int) () from /usr/local/root/lib/libGpad.so.6.24
#18 0x00007f574ecc3aa1 in TCanvas::TCanvas(char const*, char const*, int, int) () from /usr/local/root/lib/libGpad.so.6.24
#19 0x000055f3f7547652 in fpRunAction::EndOfRunAction(G4Run const*) ()
#20 0x00007f574e9c90c2 in G4RunManager::RunTermination() () from /usr/local/Geant4_10.7_Qt/lib/libG4run.so
#21 0x00007f574e9db9d5 in G4WorkerRunManager::RunTermination() () from /usr/local/Geant4_10.7_Qt/lib/libG4run.so
#22 0x00007f574e9c702b in G4RunManager::BeamOn(int, char const*, int) () from /usr/local/Geant4_10.7_Qt/lib/libG4run.so
#23 0x00007f574e9d8d9b in G4WorkerRunManager::DoWork() () from /usr/local/Geant4_10.7_Qt/lib/libG4run.so
#24 0x00007f574e9e28be in G4MTRunManagerKernel::StartThread(G4WorkerThread*) () from /usr/local/Geant4_10.7_Qt/lib/libG4run.so
#25 0x00007f574db5ede4 in ?? () from /lib/x86_64-linux-gnu/libstdc++.so.6
#26 0x00007f5749c69609 in start_thread (arg=) at pthread_create.c:477
#27 0x00007f574d99d293 in clone () at …/sysdeps/unix/sysv/linux/x86_64/clone.S:95

===========================================================

this is the information produced when executing the application, only one thread is used:
/run/numberOfThreads 1

Sorry, I don’t quite understand - do you mean the output in your previous post is from a single threaded run?

All I can spot is that it seems your code is trying to create a TCanvas in the EndOfRunAction member function of your G4UserRunAction concrete class, so there are two things to check:

  1. I’d guess that you only want the plot for the overall run, so is the creation of the TCanvas only done for the master thread, i.e, protected by a conditional:

    if(IsMaster())
    {
      // code only for master thread
    }
    

    ?

  2. IIRC just creating a TCanvas in an application, e.g.

    int main()
    {
     TCanvas* c1 = new TCanvas(...);
    }
    

    won’t work directly as the object is created then the function exits and the plot may then not be persistent on the screen.

    I’d guess that as ROOT and Qt5 are being mixed, there may be some conflict between (GUI) event loops here. All I can suggest here to try building/running your application without the Qt GUI and visualisation first, e.g.

    $ G4UI_USE_TCSH myapplication
    

    and trace the segfault again. That may give some hints about whether this is a Qt/ROOT clash, or something else.

It might also be worth asking on the ROOT Forum about best/recommended practices for using ROOT graphics in other applications using QT5/etc.

Yes, in my application only one thread is used through the command “/run/numberOfThreads 1”.

when i output in master thread,like:
if(IsMaster())
{
TCanvas* c1=new TCanvas();
}
the error is still exist.

i try to use OPGL as visualization driver, the error is also still exist.

so it seems i can not use the code line " TCanvas* c1=new TCanvas();" or
Geant4 and Root can not be used togather.

Maybe there is a bug in Root 6.24/00, everythings work well in Root 6.24/02.

Hi @siyuan1979,

I see you indicate that your issue has been resolved using ROOT 6.24/02. Something you may also want to note however, is that if you are going to run Geant4 using the multithreaded run manager while using ROOT you will need to enable ROOT thread safety by adding:

if (isMaster)
{
ROOT::EnableThreadSafety();
}

In your beginning of run action. I suspect this may even resolve the issue you were having on ROOT 6.24/00

Joseph