Compilation Issue: G4VisExecutive.icc

Hello everyone,

I’m facing an issue while compiling my Geant4 code. When trying to compile, I get the following error:

In file included from /home/ranulfo/Programas/Geant4/geant4-v11.2.1/geant4-v11.2.0-install/include/Geant4/G4VisExecutive.hh:141,
                 from tutorial003.cpp:79:
/home/ranulfo/Programas/Geant4/geant4-v11.2.1/geant4-v11.2.0-install/include/Geant4/G4VisExecutive.icc:142:10: fatal error: QtGlobal: No such file or directory
  142 | #include <QtGlobal>
      |          ^~~~~~~~~~
compilation terminated.

It seems that the problem is related to the G4VisExecutive.icc file. The code I’m trying to compile is as follows:

#include <iostream>

#include "G4RunManager.hh" 

// The Detector
#include "G4VUserDetectorConstruction.hh"
#include "G4Box.hh"
#include "G4Material.hh"
#include "G4NistManager.hh"
#include "G4LogicalVolume.hh"
#include "G4PVPlacement.hh"

class MyDetector : public G4VUserDetectorConstruction {
public:
    virtual G4VPhysicalVolume* Construct() override;
};

G4VPhysicalVolume* MyDetector::Construct() {
    auto *lAr = G4NistManager::Instance()->FindOrBuildMaterial("G4_lAr");
    auto *worldBox = new G4Box("worldBox", 10000, 10000, 10000);
    auto *logicalWorld = new G4LogicalVolume(worldBox, lAr, "Logical World");
    auto *physicalWorld = new G4PVPlacement(0,{0,0,0}, logicalWorld, "Physical World", 0, false, 0);
    return physicalWorld;
}

//The Physics List
#include "G4VUserPhysicsList.hh"
#include "G4PhysListFactory.hh"

//The Action  Initialization (particle source)
#include "G4VUserActionInitialization.hh"
#include "G4VUserPrimaryGeneratorAction.hh"
#include "G4ParticleGun.hh"
#include "G4Geantino.hh"

class MyPrimaryGenerator : public G4VUserPrimaryGeneratorAction {
private:
    G4ParticleGun* m_ParticleGun;
public:
    MyPrimaryGenerator();
    ~MyPrimaryGenerator();
    virtual void GeneratePrimaries(G4Event* anEvent) override;
};

MyPrimaryGenerator::MyPrimaryGenerator() : 
    G4VUserPrimaryGeneratorAction(), 
    m_ParticleGun( new G4ParticleGun( G4Geantino::Definition() ) )
{
}

MyPrimaryGenerator::~MyPrimaryGenerator() {
    delete m_ParticleGun;
}

void MyPrimaryGenerator::GeneratePrimaries(G4Event* anEvent) {
    m_ParticleGun->GeneratePrimaryVertex(anEvent);
    std::cout << "Run ID       " << anEvent->GetEventID() << std::endl;
    std::cout << "Primary      " << m_ParticleGun->GetParticleDefinition()->GetParticleName() << std::endl;
    std::cout << "Energy (MeV) " << m_ParticleGun->GetParticleEnergy() << std::endl;
}

class MyActionInitialization : public G4VUserActionInitialization {
public:
    virtual void Build() const override;
};

void MyActionInitialization::Build() const {
    SetUserAction( new MyPrimaryGenerator );
}

// This is the Main code.
#include "G4UIExecutive.hh"
#include "G4UImanager.hh"

#define G4VIS_USE_OPENGLX
#define G4VIS_USE_OPENGLQT  

// This is the Visualization Engine.
#include "G4VisExecutive.hh"
#include "G4VisManager.hh"

int main(int argc, char** argv) {

    auto *runManager = new G4RunManager();  

    auto *factory = new G4PhysListFactory();
    auto *physicsList = factory->GetReferencePhysList("Shielding");

    runManager->SetUserInitialization( new MyDetector() );
    runManager->SetUserInitialization( physicsList );
    runManager->SetUserInitialization( new MyActionInitialization() );
    runManager->Initialize();

    auto *visManager = new G4VisExecutive();
    visManager->Initialise();

    auto *uiExecutive = new G4UIExecutive(argc,argv, "Qt"); 
    

    if (argc == 1){
        uiExecutive->SessionStart();
    } else {
        auto *uiManager = G4UImanager::GetUIpointer();
        uiManager->ApplyCommand("/control/execute " + G4String(argv[1]) );
        uiExecutive->SessionStart();
    }

    delete runManager; // The runManager will delete all other pointers owned by it.
    delete uiExecutive;
    delete visManager; 
};

I’m using Geant4 version 11.2.1, and I’m trying to compile a simple code that includes graphical visualization. I’ve tried adding the following lines to my code, as suggested in some solutions I found:

#define G4VIS_USE_OPENGLX
#define G4VIS_USE_OPENGLQT

But the error still persists

I’m use:

No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.4 LTS
Release: 22.04
Codename: jammy

Has anyone faced this issue before or knows how I can resolve it?

I appreciate any help you can provide!

With what options did you configure/build Geant4 with, and how are you configuring/building your application?

Those defines should never be set manually - is the error you’re seeing with or without these?

I configured and built Geant4 using the following options:

cmake -DCMAKE_INSTALL_PREFIX=../geant4-v11.2.0-install\
       -DGEANT4_INSTALL_DATADIR=../geant4-v11.2.0-install/share/data \
       -DGEANT4_USE_GDML=ON\
       -DGEANT4_USE_QT=ON\
       -DGEANT4_USE_RAYTRACER_X11=ON\
       -DGEANT4_USE_OPENGL_X11=ON \
       -DGEANT4_BUILD_MULTITHREADED=ON\
       ..

I followed the steps in the videos:

(000 - Installation on Ubuntu/Instalação no Ubuntu);
(001 - The Smallest Simulation/A menor simulação),;
(002 - User Interface/Interface com o usuário)

From this YouTube playlist:
[Tutoriais de Geant4 do Gustavo Valdiviesso] (https://www.youtube.com/watch?v=tjc-ARiawEs&list=PLZdgmLlVnGNXCmRAyC1GjzgFVUm-Hh4Jo)

GEANT4 ran the correct code and I was able to replicate the same!

However, in this video, starting at minute 35:00, I can’t open the graphical interface and there is this QtGlobal bug:

(003 - Introduction to Visualization/Introdução à Visualização)

Before this bug appeared, I had an error that I managed to solve as follows:

When you are about to execute the code with " #define G4VIS_USE_OPENGLX " a message like this may appear:


"error: operator '||' has no right operand
   292 | G4VIS_USE_OPENGLX || G4VIS_USE_OPENGLWIN32"

It’s a problem that persists even after enabling OpenGL X11. The error appears to be related to a comparison in the “G4VisExecutive.icc” file that is in the GEANT4 installation folder. I accessed the folder where I installed GEANT4 and looked for the file “G4VisExecutive.icc” (I opened the file with a text program). I searched the file for “G4VIS_USE_OPENGLX || G4VIS_USE_OPENGLWIN32”. You’ll probably come across this or something similar:

#if defined G4VIS_USE_OPENGLQT || G4VIS_USE_OPENGLXM ||\
             G4VIS_USE_OPENGLX || G4VIS_USE_OPENGLWIN32
   fDefaultGraphicsSystemName = "OGL";

I changed this line in the file to:

#ifdef USE_OPENGL
   fDefaultGraphicsSystemName = "OGL";
#endif

I saved the file, compiled the code and ran it. For me using UBUNTU 22.04 LTS it worked and opened a GUI window.

Perhaps this QtGlobal bug is because in Ubuntu 22.05 LTS Qt5 is no longer used and it does not know how to find this library, as reported here:

Package ‘qt5-default’ and Ubuntu update

This is the G4VisExecutive.icc file:

//
// ********************************************************************
// * License and Disclaimer                                           *
// *                                                                  *
// * The  Geant4 software  is  copyright of the Copyright Holders  of *
// * the Geant4 Collaboration.  It is provided  under  the terms  and *
// * conditions of the Geant4 Software License,  included in the file *
// * LICENSE and available at  http://cern.ch/geant4/license .  These *
// * include a list of copyright holders.                             *
// *                                                                  *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work  make  any representation or  warranty, express or implied, *
// * regarding  this  software system or assume any liability for its *
// * use.  Please see the license in the file  LICENSE  and URL above *
// * for the full disclaimer and the limitation of liability.         *
// *                                                                  *
// * This  code  implementation is the result of  the  scientific and *
// * technical work of the GEANT4 collaboration.                      *
// * By using,  copying,  modifying or  distributing the software (or *
// * any work based  on the software)  you  agree  to acknowledge its *
// * use  in  resulting  scientific  publications,  and indicate your *
// * acceptance of all terms of the Geant4 Software license.          *
// ********************************************************************
//
//
//
//
// John Allison 24th January 1998.

#ifndef G4VISEXECUTIVE_ICC
#define G4VISEXECUTIVE_ICC

// Filter/Model Factories
#include "G4HitFilterFactories.hh"
#include "G4DigiFilterFactories.hh"
#include "G4TrajectoryFilterFactories.hh"
#include "G4TrajectoryModelFactories.hh"

// Supported drivers...
// Not needing external packages or libraries...
#include "G4ASCIITree.hh"        // no_geant4_module_check
#include "G4DAWNFILE.hh"         // no_geant4_module_check
#include "G4HepRepFile.hh"       // no_geant4_module_check
#include "G4RayTracer.hh"        // no_geant4_module_check
#include "G4VRML2File.hh"        // no_geant4_module_check
#include "G4GMocrenFile.hh"      // no_geant4_module_check
#include "G4ToolsSGOffscreen.hh" // no_geant4_module_check

// Needing external packages or libraries...
#ifdef G4VIS_USE_OPENGLX
#include "G4OpenGLImmediateX.hh" // no_geant4_module_check
#include "G4OpenGLStoredX.hh"    // no_geant4_module_check
#endif

#ifdef G4VIS_USE_OPENGLWIN32
#include "G4OpenGLImmediateWin32.hh" // no_geant4_module_check
#include "G4OpenGLStoredWin32.hh"    // no_geant4_module_check
#endif

#ifdef G4VIS_USE_OPENGLXM
#include "G4OpenGLImmediateXm.hh" // no_geant4_module_check
#include "G4OpenGLStoredXm.hh"    // no_geant4_module_check
#endif

#ifdef G4VIS_USE_OPENGLQT
#include "G4OpenGLImmediateQt.hh" // no_geant4_module_check
#include "G4OpenGLStoredQt.hh"    // no_geant4_module_check
#endif

#ifdef G4VIS_USE_OIX
#include "G4OpenInventorX.hh"          // no_geant4_module_check
#include "G4OpenInventorXtExtended.hh" // no_geant4_module_check
#endif

#ifdef G4VIS_USE_OIQT
#include "G4OpenInventorQt.hh" // no_geant4_module_check
#endif

#ifdef G4VIS_USE_OIWIN32
#include "G4OpenInventorWin32.hh" // no_geant4_module_check
#endif

#ifdef G4VIS_USE_RAYTRACERX
#include "G4RayTracerX.hh" // no_geant4_module_check
#endif

#ifdef G4VIS_USE_QT3D
#include "G4Qt3D.hh" // no_geant4_module_check
#endif

#ifdef G4VIS_USE_TOOLSSG_X11_GLES
#include "G4ToolsSGX11GLES.hh" // no_geant4_module_check
#endif

#ifdef G4VIS_USE_TOOLSSG_X11_ZB
#include "G4ToolsSGX11ZB.hh" // no_geant4_module_check
#endif

#ifdef G4VIS_USE_TOOLSSG_WINDOWS_GLES
#include "G4ToolsSGWindowsGLES.hh" // no_geant4_module_check
#endif

#ifdef G4VIS_USE_TOOLSSG_WINDOWS_ZB
#include "G4ToolsSGWindowsZB.hh" // no_geant4_module_check
#endif

#ifdef G4VIS_USE_TOOLSSG_XT_GLES
#include "G4ToolsSGXtGLES.hh" // no_geant4_module_check
#endif

#ifdef G4VIS_USE_TOOLSSG_XT_ZB
#include "G4ToolsSGXtZB.hh" // no_geant4_module_check
#endif

#ifdef G4VIS_USE_TOOLSSG_QT_GLES
#include "G4ToolsSGQtGLES.hh" // no_geant4_module_check
#endif

#ifdef G4VIS_USE_TOOLSSG_QT_ZB
#include "G4ToolsSGQtZB.hh" // no_geant4_module_check
#endif

#ifdef G4VIS_USE_VTK
#include "G4Vtk.hh" // no_geant4_module_check
#include "G4VtkOffscreen.hh" // no_geant4_module_check
#undef G4VIS_USE_TOOLSSG_X11_GLES
#undef G4VIS_USE_TOOLSSG_XT_GLES
#undef G4VIS_USE_TOOLSSG_QT_GLES
#undef G4VIS_USE_TOOLSSG_WINDOWS_GLES
#undef G4VIS_USE_OPENGLQT
#undef G4VIS_USE_OPENGLXM
#undef G4VIS_USE_OPENGLX
#undef G4VIS_USE_OPENGLWIN32
#endif

#ifdef G4VIS_USE_VTK_QT
#include "G4VtkQt.hh" // no_geant4_module_check
#endif

#if defined (G4VIS_USE_OPENGLQT) || (G4VIS_USE_TOOLSSG_QT_GLES)
#include <QtGlobal>
#endif

#include "G4UImanager.hh"
#include "G4UIsession.hh"
#include "G4UIbatch.hh"

#include <cstdlib>
#include <sstream>

// The inline keyword prevents the compiler making an external
// reference even though they cannot actually be inlined since they
// are virtual functions.  This prevents a "multiple definition" error
// if it is included in more than one file.  However, as explained in
// the class description in G4VisExecutive.hh, it should never be
// necessary to #include "G4VisExecutive.hh" in more than one file
// since after instantiation the object can be treated as a
// G4VisManager.

inline
G4VisExecutive::G4VisExecutive(int argc, char** argv, const G4String& system,
                               const G4String& verbosityString):
G4VisManager(verbosityString),
fSelected(false)
{
  if (!fSelected) SetDefaultsByArgument(system);
  if (!fSelected) SetDefaultsByEnvironment();
  if (!fSelected) SetDefaultsByFile(argc, argv);
  if (!fSelected) SetDefaultsByBatch();
  if (!fSelected) SetDefaultsByBuildFlags();
}

inline
G4VisExecutive::G4VisExecutive (const G4String& verbosityString):
G4VisManager(verbosityString),
fSelected(false)
{
  if (!fSelected) SetDefaultsByEnvironment();
  if (!fSelected) SetDefaultsByBatch();
  if (!fSelected) SetDefaultsByBuildFlags();
}

inline
void G4VisExecutive::SetDefaultsByArgument(const G4String& system)
{
  // 1st priority: selection by G4VisExecuitive argument
  if (!system.empty()) {
    fDefaultGraphicsSystemName = system;
    fDefaultGraphicsSystemBasis = "G4VisExecuitive argument";
    fSelected = true;
  }
}

inline
void G4VisExecutive::SetDefaultsByEnvironment()
{
  // 2nd priority: by environment variable
  if (auto g4env = std::getenv("G4VIS_DEFAULT_DRIVER")) {
    G4String graphicsSystem, windowSizeHint;
    std::istringstream iss(g4env);
    iss >> graphicsSystem >> windowSizeHint;
    if (!graphicsSystem.empty()) {
      fDefaultGraphicsSystemName = graphicsSystem;
      fDefaultGraphicsSystemBasis = "environment G4VIS_DEFAULT_DRIVER";
      fSelected = true;
    }
    if (!windowSizeHint.empty()) {
      fDefaultXGeometryString = windowSizeHint;
      fDefaultXGeometryStringBasis  = "environment G4VIS_DEFAULT_DRIVER";
    }
  }
}

inline
void G4VisExecutive::SetDefaultsByFile(int argc, char** argv)
{
  // 3rd priority: in $HOME/.g4session

  // Find ~/.g4session - simply return if it does not exist
  const char* home = std::getenv("HOME");
  if (home == nullptr) return;
  G4String homedir = home;
#ifndef WIN32
  G4String filename = homedir + "/.g4session";
#else
  G4String filename = homedir + "\\.g4session";
#endif
  std::ifstream g4session(filename);
  if (g4session.fail()) return;

  // Find app name
  if (argc < 1) return;
  G4String appInput = argv[0];
  G4String appName = "";
  size_t islash = appInput.find_last_of("/\\");
  if (islash == G4String::npos) appName = appInput;
  else appName = appInput.substr(islash + 1, appInput.size() - islash - 1);

  // Scan ~/.g4session
  G4String line;
  // First line is the user-chosen default: session, graphics system, windo wize hint
  G4String applicableSession, applicableGraphicsSystem, applicableWindowSizeHint;
  std::getline(g4session,line);
  auto hash = line.find_first_of('#'); line = line.substr(0,hash);
  std::istringstream iss(line);
  iss >> applicableSession >> applicableGraphicsSystem >> applicableWindowSizeHint;
  // Read subsequent lines
  while (g4session.good()) {
    G4String app, session, graphicsSystem, windowSizeHint;
    std::getline(g4session,line);
    hash = line.find_first_of('#'); line = line.substr(0,hash);
    std::istringstream iss1(line);
    iss1 >> app >> session >> graphicsSystem >> windowSizeHint;
    if (app == appName) {
      if (!session.empty())        applicableSession        = session;  // See G4UIExecutive
      if (!graphicsSystem.empty()) applicableGraphicsSystem = graphicsSystem;
      if (!windowSizeHint.empty()) applicableWindowSizeHint = windowSizeHint;
    }
  }

  if (!applicableGraphicsSystem.empty()) {
    fDefaultGraphicsSystemName = applicableGraphicsSystem;
    fDefaultGraphicsSystemBasis = "~/.g4session";
    fSelected = true;
  }
  if (!applicableWindowSizeHint.empty()) {
    fDefaultXGeometryString = applicableWindowSizeHint;
    fDefaultXGeometryStringBasis = "~/.g4session";
  }
}

inline
void G4VisExecutive::SetDefaultsByBatch()
{
  // 4th, special case for batch session
  G4UIsession* session = G4UImanager::GetUIpointer()->GetBaseSession();
  if (session == nullptr                   // Usual case - pure batch
  || dynamic_cast<G4UIbatch*>(session)) {  // From a macro from batch
    // Choose an offscreen or file-writing system
    fDefaultGraphicsSystemName = "TSG_OFFSCREEN";
    fDefaultGraphicsSystemBasis = "batch session";
    fSelected = true;
  }
}

inline
void G4VisExecutive::SetDefaultsByBuildFlags()
{
  // 5th, by cpp flags
#if defined G4VIS_USE_OPENGLQT || G4VIS_USE_OPENGLXM ||\
            G4VIS_USE_OPENGLX  || G4VIS_USE_OPENGLWIN32
  fDefaultGraphicsSystemName = "OGL";
#elif defined G4VIS_USE_OI || G4VIS_USE_OIX
  fDefaultGraphicsSystemName = "OI";
#elif defined G4VIS_USE_TOOLSSG_QT_GLES || G4VIS_USE_TOOLSSG_X11_GLES ||\
              G4VIS_USE_TOOLSSG_XT_GLES || G4VIS_USE_TOOLSSG_WINDOWS_GLES
  fDefaultGraphicsSystemName = "TSG";
#elif defined G4VIS_USE_VTK || G4VIS_USE_VTK_QT
  fDefaultGraphicsSystemName = "Vtk";
#elif defined G4VIS_USE_TOOLSSG_QT_ZB || G4VIS_USE_TOOLSSG_X11_ZB ||\
              G4VIS_USE_TOOLSSG_XT_ZB || G4VIS_USE_TOOLSSG_WINDOWS_ZB
  fDefaultGraphicsSystemName = "TSG";
#elif defined G4VIS_USE_QT3D
  fDefaultGraphicsSystemName = "Qt3D";
#else
  // Choose a graphics system not needing external packages or libraries
  fDefaultGraphicsSystemName = "TSG_OFFSCREEN";
#endif
  fDefaultGraphicsSystemBasis = "build flags";
  fSelected = true;
}

inline void
G4VisExecutive::RegisterGraphicsSystems () {

 // Graphics Systems not needing external packages or libraries...
  RegisterGraphicsSystem (new G4ASCIITree);
  RegisterGraphicsSystem (new G4DAWNFILE);
  RegisterGraphicsSystem (new G4HepRepFile);
  RegisterGraphicsSystem (new G4RayTracer);
  RegisterGraphicsSystem (new G4VRML2File);
  RegisterGraphicsSystem (new G4GMocrenFile);
  RegisterGraphicsSystem (new G4ToolsSGOffscreen);
  G4VGraphicsSystem* tsg_offscreen = new G4ToolsSGOffscreen;
  RegisterGraphicsSystem(tsg_offscreen);
  tsg_offscreen->AddNickname("TSG_FILE");

// Graphics systems needing external packages or libraries...
// Register OGL family of drivers with their normal names,
// super-abbreviated names and fallback names where approproiate.

#ifdef G4VIS_USE_OPENGLQT
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
  G4VGraphicsSystem* ogliqt = new G4OpenGLImmediateQt;
  G4VGraphicsSystem* oglsqt = new G4OpenGLStoredQt;
  RegisterGraphicsSystem(ogliqt);
  RegisterGraphicsSystem(oglsqt);
  ogliqt->AddNickname("OGLI");
  oglsqt->AddNickname("OGL");
  oglsqt->AddNickname("OGLS");
#endif
#endif

#ifdef G4VIS_USE_OPENGLXM
  G4VGraphicsSystem* oglixm = new G4OpenGLImmediateXm;
  G4VGraphicsSystem* oglsxm = new G4OpenGLStoredXm;
  RegisterGraphicsSystem(oglixm);
  RegisterGraphicsSystem(oglsxm);
#  ifdef G4VIS_USE_OPENGLQT
  oglixm->AddNickname("OGLIQt_FALLBACK");
  oglsxm->AddNickname("OGLSQt_FALLBACK");
#  else
  oglixm->AddNickname("OGLI");
  oglsxm->AddNickname("OGL");
  oglsxm->AddNickname("OGLS");
#  endif
#endif

#ifdef G4VIS_USE_OPENGLX
  G4VGraphicsSystem* oglix = new G4OpenGLImmediateX;
  G4VGraphicsSystem* oglsx = new G4OpenGLStoredX;
  RegisterGraphicsSystem(oglix);
  RegisterGraphicsSystem(oglsx);
#  ifdef G4VIS_USE_OPENGLQT
  oglix->AddNickname("OGLIQt_FALLBACK");
  oglsx->AddNickname("OGLSQt_FALLBACK");
#  endif
#  ifdef G4VIS_USE_OPENGLXM
  oglix->AddNickname("OGLIXm_FALLBACK");
  oglsx->AddNickname("OGLSXm_FALLBACK");
#  endif
#  if defined(G4VIS_USE_OPENGLQT) || (G4VIS_USE_OPENGLXM)
#  else
  oglix->AddNickname("OGLI");
  oglsx->AddNickname("OGL");
  oglsx->AddNickname("OGLS");
#  endif
#endif

#ifdef G4VIS_USE_OPENGLWIN32
  G4VGraphicsSystem* ogliwin32 = new G4OpenGLImmediateWin32;
  G4VGraphicsSystem* oglswin32 = new G4OpenGLStoredWin32;
  RegisterGraphicsSystem(ogliwin32);
  RegisterGraphicsSystem(oglswin32);
#  ifdef G4VIS_USE_OPENGLQT
  ogliwin32->AddNickname("OGLIQt_FALLBACK");
  oglswin32->AddNickname("OGLSQt_FALLBACK");
#  else
  ogliwin32->AddNickname("OGLI");
  oglswin32->AddNickname("OGL");
  oglswin32->AddNickname("OGLS");
#  endif
#endif

// Register OI graphics system with super-abbreviated nickname
#ifdef G4VIS_USE_OI
  G4VGraphicsSystem* oi = 0;
#  ifdef G4VIS_USE_OIX
  oi = new G4OpenInventorX;
#  elif defined(G4VIS_USE_OIQT)
  oi = new G4OpenInventorQt;
#  elif defined(G4VIS_USE_OIWIN32)
  oi = new G4OpenInventorWin32;
#  endif
  if(oi)
  {
    RegisterGraphicsSystem(oi);
    oi->AddNickname("OI");
  }
#endif

// The new, extended OI is registered separately for now.
#ifdef G4VIS_USE_OIX
  RegisterGraphicsSystem(new G4OpenInventorXtExtended);
#endif

#ifdef G4VIS_USE_RAYTRACERX
  RegisterGraphicsSystem(new G4RayTracerX);
#endif

#ifdef G4VIS_USE_QT3D
  RegisterGraphicsSystem(new G4Qt3D);
#endif

#ifdef G4VIS_USE_TOOLSSG_X11_GLES
  G4VGraphicsSystem* tsg_x11_gles = new G4ToolsSGX11GLES;
  RegisterGraphicsSystem(tsg_x11_gles);
  tsg_x11_gles->AddNickname("TSGX11");
#ifdef G4VIS_USE_TOOLSSG_XT_GLES
  tsg_x11_gles->AddNickname("TSG_XT_GLES_FALLBACK");
#endif
#ifdef G4VIS_USE_TOOLSSG_QT_GLES
#ifndef G4VIS_USE_TOOLSSG_XT_GLES
  tsg_x11_gles->AddNickname("TSG_QT_GLES_FALLBACK");
#endif
#endif
#endif

#ifdef G4VIS_USE_TOOLSSG_X11_ZB
  G4VGraphicsSystem* tsg_x11_zb = new G4ToolsSGX11ZB;
  RegisterGraphicsSystem(tsg_x11_zb);
  tsg_x11_zb->AddNickname("TSGX11ZB");
#endif

#ifdef G4VIS_USE_TOOLSSG_XT_GLES
  G4VGraphicsSystem* tsg_xt_gles = new G4ToolsSGXtGLES;
  RegisterGraphicsSystem(tsg_xt_gles);
  tsg_xt_gles->AddNickname("TSGXt");
#ifdef G4VIS_USE_TOOLSSG_QT_GLES
  tsg_xt_gles->AddNickname("TSG_QT_GLES_FALLBACK");
#endif
#endif

#ifdef G4VIS_USE_TOOLSSG_XT_ZB
  G4VGraphicsSystem* tsg_xt_zb = new G4ToolsSGXtZB;
  RegisterGraphicsSystem(tsg_xt_zb);
  tsg_xt_zb->AddNickname("TSGXtZB");
#endif

#ifdef G4VIS_USE_TOOLSSG_QT_GLES
  G4VGraphicsSystem* tsg_qt_gles = new G4ToolsSGQtGLES;
  RegisterGraphicsSystem(tsg_qt_gles);
  tsg_qt_gles->AddNickname("TSGQt");
#endif

#ifdef G4VIS_USE_TOOLSSG_QT_ZB
  G4VGraphicsSystem* tsg_qt_zb = new G4ToolsSGQtZB;
  RegisterGraphicsSystem(tsg_qt_zb);
  tsg_qt_zb->AddNickname("TSGQtZB");
#endif

#ifdef G4VIS_USE_TOOLSSG_WINDOWS_GLES
  G4VGraphicsSystem* tsg_windows_gles = new G4ToolsSGWindowsGLES;
  RegisterGraphicsSystem(tsg_windows_gles);
#endif

#ifdef G4VIS_USE_TOOLSSG_WINDOWS_ZB
  G4VGraphicsSystem* tsg_windows_zb = new G4ToolsSGWindowsZB;
  RegisterGraphicsSystem(tsg_windows_zb);
#endif

#ifdef G4VIS_USE_TOOLSSG_QT_GLES
  tsg_qt_gles->AddNickname("TSG");
#  if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
  if (fVerbosity >= startup) {
    G4cout << "  Qt6: Asking ToolsSG to stand in for OpenGL" << G4endl;
    tsg_qt_gles->AddNickname("OGL");
  }
#  endif
#elif defined(G4VIS_USE_TOOLSSG_XT_GLES)
  tsg_xt_gles->AddNickname("TSG");
#elif defined(G4VIS_USE_TOOLSSG_X11_GLES)
  tsg_x11_gles->AddNickname("TSG");
#elif defined(G4VIS_USE_TOOLSSG_WINDOWS_GLES)
  tsg_windows_gles->AddNickname("TSG");
#else
#ifdef G4VIS_USE_TOOLSSG_QT_ZB
  tsg_qt_zb->AddNickname("TSG");
#elif G4VIS_USE_TOOLSSG_WINDOWS_ZB
  tsg_windows_zb->AddNickname("TSG");
#else
  tsg_offscreen->AddNickname("TSG");
#endif
#endif

#ifdef G4VIS_USE_VTK
  if (fVerbosity >= startup) {
    G4cout
    << "  VTK: OpenGL-based drivers suppressed because of compatibility issues."
    << G4endl;
  }
  G4Vtk* vtkN = new G4Vtk();
  G4VtkOffscreen* vtkOS = new G4VtkOffscreen();
  RegisterGraphicsSystem(vtkN);
  RegisterGraphicsSystem(vtkOS);
#  ifdef G4VIS_USE_VTK_QT
  vtkN->AddNickname("VTKQt_FALLBACK");
#  endif
#endif

#ifdef G4VIS_USE_VTK_QT
  G4VtkQt* vtkQt = new G4VtkQt();
  RegisterGraphicsSystem(vtkQt);
  vtkQt->AddNickname("VTKQt");
#endif

#ifdef G4VIS_USE_VTK_QT
  vtkQt->AddNickname("Vtk");
  vtkQt->AddNickname("VTK");
#elif defined(G4VIS_USE_VTK)
  vtkN->AddNickname("Vtk");
  vtkN->AddNickname("VTK");
#endif
}

// See comments about inlining above.
inline void
G4VisExecutive::RegisterModelFactories()
{
   // Trajectory draw models
   RegisterModelFactory(new G4TrajectoryGenericDrawerFactory());
   RegisterModelFactory(new G4TrajectoryDrawByAttributeFactory());
   RegisterModelFactory(new G4TrajectoryDrawByChargeFactory());
   RegisterModelFactory(new G4TrajectoryDrawByOriginVolumeFactory());
   RegisterModelFactory(new G4TrajectoryDrawByParticleIDFactory());
   RegisterModelFactory(new G4TrajectoryDrawByEncounteredVolumeFactory());

   // Trajectory filter models
   RegisterModelFactory(new G4TrajectoryAttributeFilterFactory());
   RegisterModelFactory(new G4TrajectoryChargeFilterFactory());
   RegisterModelFactory(new G4TrajectoryOriginVolumeFilterFactory());
   RegisterModelFactory(new G4TrajectoryParticleFilterFactory());
   RegisterModelFactory(new G4TrajectoryEncounteredVolumeFilterFactory());

   // Hit filter models
   RegisterModelFactory(new G4HitAttributeFilterFactory());

   // Digi filter models
   RegisterModelFactory(new G4DigiAttributeFilterFactory());
}

#endif

I can’t watch all of the tutorials, but are you following the steps in the second video on compiling your application using g++ and geant4-config? If so, the tutorial doesn’t use geant4-config quite correctly. Nominally, the correct usage is:

$ g++ $(geant4-config --cflags --libs) filewithmain.cpp

The --cflags give the correct includes and -D flags needed to setup the vis system. I’d suggest

  1. Reset any changes G4VisExecutive.icc to the original.
  2. Remove all #define G4VIS_... in your program
  3. Recompile as above with the correct invocation of geant4-config

I’d also recommend looking at How to make an executable program in the Application Developer’s Guide. The CMake system provides a much more robust build system generally, especially once you start to organise code with multiple files.

I kept the standard code and left the G4VisExecutive.icc file unchanged.

The issue itself is that the code cannot execute the “/vis/OPEN OGL” command, as shown in the response below:

I can even make some changes to the code by removing the “G4Vis…” and the “QtGlobal” error message disappears. The code compiles correctly, but it doesn’t execute the “/vis/OPEN OGL” command. The message below is from the main code as I had sent before.


ranulfo@ranulfo:~/Documentos/GEANT4_Tutorial/Tutorial/Aula 3$ source compile.sh
In file included from /home/ranulfo/Programas/Geant4/geant4-v11.2.1/geant4-v11.2.0-install/include/Geant4/G4VisExecutive.hh:141,
                 from tutorial003.cpp:79:
/home/ranulfo/Programas/Geant4/geant4-v11.2.1/geant4-v11.2.0-install/include/Geant4/G4VisExecutive.icc:142:10: fatal error: QtGlobal: Arquivo ou diretório inexistente
  142 | #include <QtGlobal>
      |          ^~~~~~~~~~
compilation terminated.

Even with the “QtGlobal” error, I went ahead and compiled to see the result.


ranulfo@ranulfo:~/Documentos/GEANT4_Tutorial/Tutorial/Aula 3$ ./tutorial003 muons.mac 

**************************************************************
 Geant4 version Name: geant4-11-02-patch-01 [MT]   (16-February-2024)
                       Copyright : Geant4 Collaboration
                      References : NIM A 506 (2003), 250-303
                                 : IEEE-TNS 53 (2006), 270-278
                                 : NIM A 835 (2016), 186-225
                             WWW : http://geant4.org/
**************************************************************

G4PhysListFactory::GetReferencePhysList <Shielding>  EMoption= 0
<<< Geant4 Physics List simulation engine: Shielding
<<< (Note that Shielding and Shielding_HP are equivalent!)


 hInelastic Shielding : threshold between BERT and FTFP is over the interval 
 for pions :   3 to 6 GeV
 for kaons :   3 to 6 GeV
 for proton :  3 to 6 GeV
 for neutron : 3 to 6 GeV

@@@ G4ParticleHPInelasticData instantiated for particle neutron data directory variable is G4NEUTRONHPDATA pointing to /home/ranulfo/Programas/Geant4/geant4-v11.2.1/geant4-v11.2.0-install/share/data/G4NDL4.7
Visualization Manager instantiating with verbosity "warnings (3)"...
Visualization Manager initialising...
Registering graphics systems...

You have successfully registered the following graphics systems.
Registered graphics systems are:
  ASCIITree (ATree)
  DAWNFILE (DAWNFILE)
  G4HepRepFile (HepRepFile)
  RayTracer (RayTracer)
  VRML2FILE (VRML2FILE)
  gMocrenFile (gMocrenFile)
  TOOLSSG_OFFSCREEN (TSG_OFFSCREEN)
  TOOLSSG_OFFSCREEN (TSG_OFFSCREEN, TSG_FILE, TSG)
Default graphics system is: TSG_OFFSCREEN (based on batch session).
Default window size hint is: 600x600-0+0 (based on G4VisManager initialisation).
Note: Parameters specified on the command line will override these defaults.
      Use "vis/open" without parameters to get these defaults.

Registering model factories...

You have successfully registered the following model factories.
Registered model factories:
  generic
  drawByAttribute
  drawByCharge
  drawByOriginVolume
  drawByParticleID
  drawByEncounteredVolume

Registered models:
  None

Registered filter factories:
  attributeFilter
  chargeFilter
  originVolumeFilter
  particleFilter
  encounteredVolumeFilter

Registered filters:
  None

You have successfully registered the following user vis actions.
Run Duration User Vis Actions: none
End of Event User Vis Actions: none
End of Run User Vis Actions: none

Some /vis commands (optionally) take a string to specify colour.
"/vis/list" to see available colours.
Available UI session types: [ Qt, tcsh, csh ]
QSocketNotifier: Can only be used with threads started with QThread
parameter value (OGL) is not listed in the candidate List.
  Candidates are: ASCIITree ATree DAWNFILE G4HepRepFile HepRepFile RayTracer VRML2FILE gMocrenFile TOOLSSG_OFFSCREEN TSG_OFFSCREEN TOOLSSG_OFFSCREEN TSG_OFFSCREEN TSG_FILE TSG
sub-command "/vis/sceneHandler/create" failed.
Registered graphics systems are:
  ASCIITree (ATree)
  DAWNFILE (DAWNFILE)
  G4HepRepFile (HepRepFile)
  RayTracer (RayTracer)
  VRML2FILE (VRML2FILE)
  gMocrenFile (gMocrenFile)
  TOOLSSG_OFFSCREEN (TSG_OFFSCREEN)
  TOOLSSG_OFFSCREEN (TSG_OFFSCREEN, TSG_FILE, TSG)
Default graphics system is: TSG_OFFSCREEN (based on batch session).
Default window size hint is: 600x600-0+0 (based on G4VisManager initialisation).
Note: Parameters specified on the command line will override these defaults.
      Use "vis/open" without parameters to get these defaults.

-------- WWWW ------- G4Exception-START -------- WWWW -------
*** G4Exception : UIMAN0123
      issued by : G4UImanager::ApplyCommand
Invoked command has failed - see above.
Error code : 500
*** This is just a warning message. ***
-------- WWWW -------- G4Exception-END --------- WWWW -------

It’s noticed that the graphical interface opens, but the command “/vis/open OGL” is not executed. Therefore, the simulation is not carried out and I cannot observe the interaction through the interface.

The code runs correctly, has a good structure, and the issue is recognizing “/vis/open OGL”. Additionally, I used the following command to compile, which is what you are using:

g++ tutorial003.cpp `geant4-config --libs` -I${G4INCLUDES} -o tutorial003

What you sent didn’t work:

#$ g++ $(geant4-config --cflags --libs) tutorial003.cpp

What error messages did you see? Does (note the “$” means “whatever your command prompt is”, not an actual command)

$ g++ tutorial003.cpp `geant4-config --cflags --libs` -o tutorial003

work (and if not, with what errors)? What does

$ geant4-config --cflags --libs

print?

I ran some tests here, but before reaching the final result, I’ll show the reasoning and the process. As I mentioned, when I run ‘source compile.sh’, I received this message:

ranulfo@ranulfo:~/Documentos/GEANT4_Tutorial/Tutorial/Aula 3$ source compile.sh
In file included from /home/ranulfo/Programas/Geant4/geant4-v11.2.1/geant4-v11.2.0-install/include/Geant4/G4VisExecutive.hh:141,
                 from tutorial003.cpp:79:
/home/ranulfo/Programas/Geant4/geant4-v11.2.1/geant4-v11.2.0-install/include/Geant4/G4VisExecutive.icc:292:32: error: operator '||' has no right operand
  292 |             G4VIS_USE_OPENGLX  || G4VIS_USE_OPENGLWIN32

I changed the line in the file G4VisExecutive.icc:

#if defined G4VIS_USE_OPENGLQT || G4VIS_USE_OPENGLXM ||\
            G4VIS_USE_OPENGLX  || G4VIS_USE_OPENGLWIN32
  fDefaultGraphicsSystemName = "OGL";

I changed the line to this:

#ifdef USE_OPENGL
  fDefaultGraphicsSystemName = "OGL";

The response is this after compiling:

ranulfo@ranulfo:~/Documentos/GEANT4_Tutorial/Tutorial/Aula 3$ source compile.sh
ranulfo@ranulfo:~/Documentos/GEANT4_Tutorial/Tutorial/Aula 3$ ./tutorial003

**************************************************************
 Geant4 version Name: geant4-11-02-patch-01 [MT]   (16-February-2024)
                       Copyright : Geant4 Collaboration
                      References : NIM A 506 (2003), 250-303
                                 : IEEE-TNS 53 (2006), 270-278
                                 : NIM A 835 (2016), 186-225
                             WWW : http://geant4.org/
**************************************************************

G4PhysListFactory::GetReferencePhysList <Shielding>  EMoption= 0
<<< Geant4 Physics List simulation engine: Shielding
<<< (Note that Shielding and Shielding_HP are equivalent!)


 hInelastic Shielding : threshold between BERT and FTFP is over the interval 
 for pions :   3 to 6 GeV
 for kaons :   3 to 6 GeV
 for proton :  3 to 6 GeV
 for neutron : 3 to 6 GeV

@@@ G4ParticleHPInelasticData instantiated for particle neutron data directory variable is G4NEUTRONHPDATA pointing to /home/ranulfo/Programas/Geant4/geant4-v11.2.1/geant4-v11.2.0-install/share/data/G4NDL4.7
Visualization Manager instantiating with verbosity "warnings (3)"...
Visualization Manager initialising...
Registering graphics systems...

You have successfully registered the following graphics systems.
Registered graphics systems are:
  ASCIITree (ATree)
  DAWNFILE (DAWNFILE)
  G4HepRepFile (HepRepFile)
  RayTracer (RayTracer)
  VRML2FILE (VRML2FILE)
  gMocrenFile (gMocrenFile)
  TOOLSSG_OFFSCREEN (TSG_OFFSCREEN)
  TOOLSSG_OFFSCREEN (TSG_OFFSCREEN, TSG_FILE, TSG)
  OpenGLImmediateX (OGLIX, OGLI)
  OpenGLStoredX (OGLSX, OGL, OGLS)
Default graphics system is: TSG_OFFSCREEN (based on batch session).
Default window size hint is: 600x600-0+0 (based on G4VisManager initialisation).
Note: Parameters specified on the command line will override these defaults.
      Use "vis/open" without parameters to get these defaults.

Registering model factories...

You have successfully registered the following model factories.
Registered model factories:
  generic
  drawByAttribute
  drawByCharge
  drawByOriginVolume
  drawByParticleID
  drawByEncounteredVolume

Registered models:
  None

Registered filter factories:
  attributeFilter
  chargeFilter
  originVolumeFilter
  particleFilter
  encounteredVolumeFilter

Registered filters:
  None

You have successfully registered the following user vis actions.
Run Duration User Vis Actions: none
End of Event User Vis Actions: none
End of Run User Vis Actions: none

Some /vis commands (optionally) take a string to specify colour.
"/vis/list" to see available colours.
Available UI session types: [ Qt, tcsh, csh ]
Idle>   

Note that the commands are enabled:

OpenGLImmediateX (OGLIX, OGLI)
OpenGLStoredX (OGLSX, OGL, OGLS)

Which was not happening before!

When I add the command “#define G4VIS_USE_OPENGLQT”, change “csh” to “Qt” in the code, and request compilation, the response is:


ranulfo@ranulfo:~/Documentos/GEANT4_Tutorial/Tutorial/Aula 3$ source compile.sh
In file included from /home/ranulfo/Programas/Geant4/geant4-v11.2.1/geant4-v11.2.0-install/include/Geant4/G4VisExecutive.hh:141,
                 from tutorial003.cpp:79:
/home/ranulfo/Programas/Geant4/geant4-v11.2.1/geant4-v11.2.0-install/include/Geant4/G4VisExecutive.icc:142:10: fatal error: QtGlobal: Arquivo ou diretório inexistente
  142 | #include <QtGlobal>
      |          ^~~~~~~~~~
compilation terminated.



ranulfo@ranulfo:~/Documentos/GEANT4_Tutorial/Tutorial/Aula 3$ ./tutorial003

**************************************************************
 Geant4 version Name: geant4-11-02-patch-01 [MT]   (16-February-2024)
                       Copyright : Geant4 Collaboration
                      References : NIM A 506 (2003), 250-303
                                 : IEEE-TNS 53 (2006), 270-278
                                 : NIM A 835 (2016), 186-225
                             WWW : http://geant4.org/
**************************************************************

G4PhysListFactory::GetReferencePhysList <Shielding>  EMoption= 0
<<< Geant4 Physics List simulation engine: Shielding
<<< (Note that Shielding and Shielding_HP are equivalent!)


 hInelastic Shielding : threshold between BERT and FTFP is over the interval 
 for pions :   3 to 6 GeV
 for kaons :   3 to 6 GeV
 for proton :  3 to 6 GeV
 for neutron : 3 to 6 GeV

@@@ G4ParticleHPInelasticData instantiated for particle neutron data directory variable is G4NEUTRONHPDATA pointing to /home/ranulfo/Programas/Geant4/geant4-v11.2.1/geant4-v11.2.0-install/share/data/G4NDL4.7
Visualization Manager instantiating with verbosity "warnings (3)"...
Visualization Manager initialising...
Registering graphics systems...

You have successfully registered the following graphics systems.
Registered graphics systems are:
  ASCIITree (ATree)
  DAWNFILE (DAWNFILE)
  G4HepRepFile (HepRepFile)
  RayTracer (RayTracer)
  VRML2FILE (VRML2FILE)
  gMocrenFile (gMocrenFile)
  TOOLSSG_OFFSCREEN (TSG_OFFSCREEN)
  TOOLSSG_OFFSCREEN (TSG_OFFSCREEN, TSG_FILE, TSG)
  OpenGLImmediateX (OGLIX, OGLI)
  OpenGLStoredX (OGLSX, OGL, OGLS)
Default graphics system is: TSG_OFFSCREEN (based on batch session).
Default window size hint is: 600x600-0+0 (based on G4VisManager initialisation).
Note: Parameters specified on the command line will override these defaults.
      Use "vis/open" without parameters to get these defaults.

Registering model factories...

You have successfully registered the following model factories.
Registered model factories:
  generic
  drawByAttribute
  drawByCharge
  drawByOriginVolume
  drawByParticleID
  drawByEncounteredVolume

Registered models:
  None

Registered filter factories:
  attributeFilter
  chargeFilter
  originVolumeFilter
  particleFilter
  encounteredVolumeFilter

Registered filters:
  None

You have successfully registered the following user vis actions.
Run Duration User Vis Actions: none
End of Event User Vis Actions: none
End of Run User Vis Actions: none

Some /vis commands (optionally) take a string to specify colour.
"/vis/list" to see available colours.
Available UI session types: [ Qt, tcsh, csh ]
Idle> 

Unfortunately, the graphical interface doesn’t open for me to visualize the interaction with more details.

ranulfo@ranulfo:~/Documentos/GEANT4_Tutorial/Tutorial/Aula 3$ source compile.sh
In file included from /home/ranulfo/Programas/Geant4/geant4-v11.2.1/geant4-v11.2.0-install/include/Geant4/G4VisExecutive.hh:141,
                 from tutorial003.cpp:79:
/home/ranulfo/Programas/Geant4/geant4-v11.2.1/geant4-v11.2.0-install/include/Geant4/G4VisExecutive.icc:142:10: fatal error: QtGlobal: Arquivo ou diretório inexistente
  142 | #include <QtGlobal>
      |          ^~~~~~~~~~
compilation terminated.

ranulfo@ranulfo:~/Documentos/GEANT4_Tutorial/Tutorial/Aula 3$ 

Below is the muons.mac file I use to perform the interaction. I call it in the terminal as: ./tutorial003 muons.mac. The interaction occurs, but the graphical interface, where I can see the interaction between particles and matter in more detail, does not open.

# This is a comment and will be ignored by geant4

# Use this open statement to create an OpenGL view:
/vis/open OGLIX

# Draw geometry:
/vis/drawVolume

# Specify view angle:
/vis/viewer/set/viewpointThetaPhi 45. 45.

# Specify zoom value:
/vis/viewer/zoom 1.0

# Draw smooth trajectories at end of event, showing trajectory points
# as markers 2 pixels wide:
/vis/scene/add/trajectories smooth
/vis/modeling/trajectories/create/drawByCharge
/vis/modeling/trajectories/drawByCharge-0/default/setDrawStepPts true
/vis/modeling/trajectories/drawByCharge-0/default/setStepPtsSize 2

# To superimpose all of the events from a given run:
#/vis/scene/endOfEventAction accumulate

# Re-establish auto refreshing and verbosity:
/vis/viewer/set/autoRefresh true

# Let's ask geant4 to report more details about the simulation
# by ajusting the verbosity level for each subsystem:

/run/verbose 1
/event/verbose 1
/tracking/verbose 1

# Now we define the source. Right now it is a mono-energetic beam of muons+
# We can set either energy or momentum, 
# but not both (since the mass is already defined).
# Between energy and momentum, geant4 will follow the last instruction before beamOn.

/gun/particle mu+
/gun/energy 10 MeV
/gun/position -10 0 0 m
/run/beamOn 1

Well, after all this puzzle, I believe the problem seems to be with the compiler. I tried various ways to open it. I was using the following command in the file:

#  Define an environment variable G4INCLUDES that points to 
# your installation path, where the includes are. It is usually
# something like: /path/to/geant4-install/include/Geant4

g++ tutorial003.cpp `geant4-config --libs` -I${G4INCLUDES} -o tutorial003

This error occurs because the compiler can’t find the QtGlobal header file, which is part of the Qt framework. This will compile the code but won’t include the necessary Qt libraries, hence the error when running ./tutorial003.

Another option was to use the following command:

g++ tutorial003.cpp `geant4-config --cflags --libs` -o tutorial003 -I/usr/include/qt5 -I/usr/include/qt -lQt5Core -lQt5Gui -lQt5Widgets

However, the response was this:

ranulfo@ranulfo:~/Documentos/GEANT4_Tutorial/Tutorial/Aula 3$ source compile.sh
tutorial003.cpp:75: warning: "G4VIS_USE_OPENGLX" redefined
   75 | #define G4VIS_USE_OPENGLX
      | 
<command-line>: note: this is the location of the previous definition
tutorial003.cpp:76: warning: "G4VIS_USE_OPENGLQT" redefined
   76 | #define G4VIS_USE_OPENGLQT
      | 
<command-line>: note: this is the location of the previous definition
In file included from /usr/include/x86_64-linux-gnu/qt5/QtCore/QtGlobal:1,
                 from /home/ranulfo/Programas/Geant4/geant4-v11.2.1/geant4-v11.2.0-install/include/Geant4/G4VisExecutive.icc:142,
                 from /home/ranulfo/Programas/Geant4/geant4-v11.2.1/geant4-v11.2.0-install/include/Geant4/G4VisExecutive.hh:141,
                 from tutorial003.cpp:79:
/usr/include/x86_64-linux-gnu/qt5/QtCore/qglobal.h:1281:4: error: #error "You must build your code with position independent code if Qt was built with -reduce-relocations. " "Compile your code with -fPIC (and not with -fPIE)."
 1281 | #  error "You must build your code with position independent code if Qt was built with -reduce-relocations. "\
      |    ^~~~~

The option you suggested:

#  Define an environment variable G4INCLUDES that points to 
# your installation path, where the includes are. It is usually
# something like: /path/to/geant4-install/include/Geant4

#g++ tutorial003.cpp `geant4-config --libs` -I${G4INCLUDES} -o tutorial003

#g++ tutorial003.cpp `geant4-config --cflags --libs` -o tutorial003 -I/usr/include/qt5 -I/usr/include/qt -lQt5Core -lQt5Gui -lQt5Widgets

g++ geant4-config --cflags --libs

The response was this:

ranulfo@ranulfo:~/Documentos/GEANT4_Tutorial/Tutorial/Aula 3$ source compile.sh
g++: error: unrecognized command-line option ‘--cflags’
g++: error: unrecognized command-line option ‘--libs’; did you mean ‘--libs=’?

The code opens a graphical interface, but it’s not interactive. I’d like to open that white window where we can zoom and do other operations. The problem seems to be related to GEANT4 not loading these libraries. I think it might be an issue with the operating system or packages. I’ve tried uninstalling and reinstalling packages, installing GEANT4, compiling in the ‘build’ folder, and tried many other things, but I keep facing the same errors. It seems to be a problem with the Qt5 packages that were disabled, among other things.

Also, I managed to open the white interaction window using a new approach. However, when I add the command “/vis/open OGL”, errors occur and the simulation doesn’t run. It’s a problem I haven’t been able to solve, even after reading documentation, watching videos, and consulting GitHub.

Another point is that when updating the operating system, packages become outdated, but GEANT4 doesn’t replace them automatically. Well, I don’t know… I guess I’m stuck in this deadlock.

Thanks for the detailed info - I’m going to suggest taking a few steps back as I think we need to simplify and clarify things to identify and resolve the issue.

  1. DO NOT modify G4VisExecutive.icc, and please, please, return it to the content it had when installed. If there is a bug here, then we don’t want to confuse matters.
  2. Please attach, or post here:
    • Your compile.sh script
    • The full tutorial003.cpp file as you have it now

With those we can try locally and see if we can resolve things.

My file compile.sh:

#  Define an environment variable G4INCLUDES that points to 
# your installation path, where the includes are. It is usually
# something like: /path/to/geant4-install/include/Geant4

g++ tutorial003.cpp `geant4-config --libs` -I${G4INCLUDES} -o tutorial003

My code:

#include <iostream>

#include "G4RunManager.hh" 

// The Detector
#include "G4VUserDetectorConstruction.hh"
#include "G4Box.hh"
#include "G4Material.hh"
#include "G4NistManager.hh"
#include "G4LogicalVolume.hh"
#include "G4PVPlacement.hh"

class MyDetector : public G4VUserDetectorConstruction {
public:
    virtual G4VPhysicalVolume* Construct() override;
};

G4VPhysicalVolume* MyDetector::Construct() {
    auto *lAr = G4NistManager::Instance()->FindOrBuildMaterial("G4_lAr");
    auto *worldBox = new G4Box("worldBox", 10000, 10000, 10000);
    auto *logicalWorld = new G4LogicalVolume(worldBox, lAr, "Logical World");
    auto *physicalWorld = new G4PVPlacement(0,{0,0,0}, logicalWorld, "Physical World", 0, false, 0);
    return physicalWorld;
}

//The Physics List
#include "G4VUserPhysicsList.hh"
#include "G4PhysListFactory.hh"

//The Action  Initialization (particle source)
#include "G4VUserActionInitialization.hh"
#include "G4VUserPrimaryGeneratorAction.hh"
#include "G4ParticleGun.hh"
#include "G4Geantino.hh"

class MyPrimaryGenerator : public G4VUserPrimaryGeneratorAction {
private:
    G4ParticleGun* m_ParticleGun;
public:
    MyPrimaryGenerator();
    ~MyPrimaryGenerator();
    virtual void GeneratePrimaries(G4Event* anEvent) override;
};

MyPrimaryGenerator::MyPrimaryGenerator() : 
    G4VUserPrimaryGeneratorAction(), 
    m_ParticleGun( new G4ParticleGun( G4Geantino::Definition() ) )
{
}

MyPrimaryGenerator::~MyPrimaryGenerator() {
    delete m_ParticleGun;
}

void MyPrimaryGenerator::GeneratePrimaries(G4Event* anEvent) {
    m_ParticleGun->GeneratePrimaryVertex(anEvent);
    std::cout << "Run ID       " << anEvent->GetEventID() << std::endl;
    std::cout << "Primary      " << m_ParticleGun->GetParticleDefinition()->GetParticleName() << std::endl;
    std::cout << "Energy (MeV) " << m_ParticleGun->GetParticleEnergy() << std::endl;
}

class MyActionInitialization : public G4VUserActionInitialization {
public:
    virtual void Build() const override;
};

void MyActionInitialization::Build() const {
    SetUserAction( new MyPrimaryGenerator );
}

// This is the Main code.
#include "G4UIExecutive.hh"
#include "G4UImanager.hh"

#define G4VIS_USE_OPENGLX
#define G4VIS_USE_OPENGLQT  

// This is the Visualization Engine.
#include "G4VisExecutive.hh"
#include "G4VisManager.hh"

int main(int argc, char** argv) {

    auto *runManager = new G4RunManager();  

    auto *factory = new G4PhysListFactory();
    auto *physicsList = factory->GetReferencePhysList("Shielding");

    runManager->SetUserInitialization( new MyDetector() );
    runManager->SetUserInitialization( physicsList );
    runManager->SetUserInitialization( new MyActionInitialization() );
    runManager->Initialize();

    auto *visManager = new G4VisExecutive();
    visManager->Initialise();

    auto *uiExecutive = new G4UIExecutive(argc,argv, "Qt"); 
    

    if (argc == 1){
        uiExecutive->SessionStart();
    } else {
        auto *uiManager = G4UImanager::GetUIpointer();
        uiManager->ApplyCommand("/control/execute " + G4String(argv[1]) );
        uiExecutive->SessionStart();
    }

    delete runManager; // The runManager will delete all other pointers owned by it.
    delete uiExecutive;
    delete visManager; 
};

My muons.mac file:

# This is a comment and will be ignored by geant4

# Use this open statement to create an OpenGL view:
/vis/open OGLIX

# Draw geometry:
/vis/drawVolume

# Specify view angle:
/vis/viewer/set/viewpointThetaPhi 45. 45.

# Specify zoom value:
/vis/viewer/zoom 1.0

# Draw smooth trajectories at end of event, showing trajectory points
# as markers 2 pixels wide:
/vis/scene/add/trajectories smooth
/vis/modeling/trajectories/create/drawByCharge
/vis/modeling/trajectories/drawByCharge-0/default/setDrawStepPts true
/vis/modeling/trajectories/drawByCharge-0/default/setStepPtsSize 2

# To superimpose all of the events from a given run:
#/vis/scene/endOfEventAction accumulate

# Re-establish auto refreshing and verbosity:
/vis/viewer/set/autoRefresh true

# Let's ask geant4 to report more details about the simulation
# by ajusting the verbosity level for each subsystem:

/run/verbose 1
/event/verbose 1
/tracking/verbose 1

# Now we define the source. Right now it is a mono-energetic beam of muons+
# We can set either energy or momentum, 
# but not both (since the mass is already defined).
# Between energy and momentum, geant4 will follow the last instruction before beamOn.

/gun/particle mu+
/gun/energy 10 MeV
/gun/position -10 0 0 m
/run/beamOn 1

I returned the original settings of the G4VisExecutive.icc file:


//
// ********************************************************************
// * License and Disclaimer                                           *
// *                                                                  *
// * The  Geant4 software  is  copyright of the Copyright Holders  of *
// * the Geant4 Collaboration.  It is provided  under  the terms  and *
// * conditions of the Geant4 Software License,  included in the file *
// * LICENSE and available at  http://cern.ch/geant4/license .  These *
// * include a list of copyright holders.                             *
// *                                                                  *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work  make  any representation or  warranty, express or implied, *
// * regarding  this  software system or assume any liability for its *
// * use.  Please see the license in the file  LICENSE  and URL above *
// * for the full disclaimer and the limitation of liability.         *
// *                                                                  *
// * This  code  implementation is the result of  the  scientific and *
// * technical work of the GEANT4 collaboration.                      *
// * By using,  copying,  modifying or  distributing the software (or *
// * any work based  on the software)  you  agree  to acknowledge its *
// * use  in  resulting  scientific  publications,  and indicate your *
// * acceptance of all terms of the Geant4 Software license.          *
// ********************************************************************
//
//
//
//
// John Allison 24th January 1998.

#ifndef G4VISEXECUTIVE_ICC
#define G4VISEXECUTIVE_ICC

// Filter/Model Factories
#include "G4HitFilterFactories.hh"
#include "G4DigiFilterFactories.hh"
#include "G4TrajectoryFilterFactories.hh"
#include "G4TrajectoryModelFactories.hh"

// Supported drivers...
// Not needing external packages or libraries...
#include "G4ASCIITree.hh"        // no_geant4_module_check
#include "G4DAWNFILE.hh"         // no_geant4_module_check
#include "G4HepRepFile.hh"       // no_geant4_module_check
#include "G4RayTracer.hh"        // no_geant4_module_check
#include "G4VRML2File.hh"        // no_geant4_module_check
#include "G4GMocrenFile.hh"      // no_geant4_module_check
#include "G4ToolsSGOffscreen.hh" // no_geant4_module_check

// Needing external packages or libraries...
#ifdef G4VIS_USE_OPENGLX
#include "G4OpenGLImmediateX.hh" // no_geant4_module_check
#include "G4OpenGLStoredX.hh"    // no_geant4_module_check
#endif

#ifdef G4VIS_USE_OPENGLWIN32
#include "G4OpenGLImmediateWin32.hh" // no_geant4_module_check
#include "G4OpenGLStoredWin32.hh"    // no_geant4_module_check
#endif

#ifdef G4VIS_USE_OPENGLXM
#include "G4OpenGLImmediateXm.hh" // no_geant4_module_check
#include "G4OpenGLStoredXm.hh"    // no_geant4_module_check
#endif

#ifdef G4VIS_USE_OPENGLQT
#include "G4OpenGLImmediateQt.hh" // no_geant4_module_check
#include "G4OpenGLStoredQt.hh"    // no_geant4_module_check
#endif

#ifdef G4VIS_USE_OIX
#include "G4OpenInventorX.hh"          // no_geant4_module_check
#include "G4OpenInventorXtExtended.hh" // no_geant4_module_check
#endif

#ifdef G4VIS_USE_OIQT
#include "G4OpenInventorQt.hh" // no_geant4_module_check
#endif

#ifdef G4VIS_USE_OIWIN32
#include "G4OpenInventorWin32.hh" // no_geant4_module_check
#endif

#ifdef G4VIS_USE_RAYTRACERX
#include "G4RayTracerX.hh" // no_geant4_module_check
#endif

#ifdef G4VIS_USE_QT3D
#include "G4Qt3D.hh" // no_geant4_module_check
#endif

#ifdef G4VIS_USE_TOOLSSG_X11_GLES
#include "G4ToolsSGX11GLES.hh" // no_geant4_module_check
#endif

#ifdef G4VIS_USE_TOOLSSG_X11_ZB
#include "G4ToolsSGX11ZB.hh" // no_geant4_module_check
#endif

#ifdef G4VIS_USE_TOOLSSG_WINDOWS_GLES
#include "G4ToolsSGWindowsGLES.hh" // no_geant4_module_check
#endif

#ifdef G4VIS_USE_TOOLSSG_WINDOWS_ZB
#include "G4ToolsSGWindowsZB.hh" // no_geant4_module_check
#endif

#ifdef G4VIS_USE_TOOLSSG_XT_GLES
#include "G4ToolsSGXtGLES.hh" // no_geant4_module_check
#endif

#ifdef G4VIS_USE_TOOLSSG_XT_ZB
#include "G4ToolsSGXtZB.hh" // no_geant4_module_check
#endif

#ifdef G4VIS_USE_TOOLSSG_QT_GLES
#include "G4ToolsSGQtGLES.hh" // no_geant4_module_check
#endif

#ifdef G4VIS_USE_TOOLSSG_QT_ZB
#include "G4ToolsSGQtZB.hh" // no_geant4_module_check
#endif

#ifdef G4VIS_USE_VTK
#include "G4Vtk.hh" // no_geant4_module_check
#include "G4VtkOffscreen.hh" // no_geant4_module_check
#undef G4VIS_USE_TOOLSSG_X11_GLES
#undef G4VIS_USE_TOOLSSG_XT_GLES
#undef G4VIS_USE_TOOLSSG_QT_GLES
#undef G4VIS_USE_TOOLSSG_WINDOWS_GLES
#undef G4VIS_USE_OPENGLQT
#undef G4VIS_USE_OPENGLXM
#undef G4VIS_USE_OPENGLX
#undef G4VIS_USE_OPENGLWIN32
#endif

#ifdef G4VIS_USE_VTK_QT
#include "G4VtkQt.hh" // no_geant4_module_check
#endif

#if defined (G4VIS_USE_OPENGLQT) || (G4VIS_USE_TOOLSSG_QT_GLES)
#include <QtGlobal>
#endif

#include "G4UImanager.hh"
#include "G4UIsession.hh"
#include "G4UIbatch.hh"

#include <cstdlib>
#include <sstream>

// The inline keyword prevents the compiler making an external
// reference even though they cannot actually be inlined since they
// are virtual functions.  This prevents a "multiple definition" error
// if it is included in more than one file.  However, as explained in
// the class description in G4VisExecutive.hh, it should never be
// necessary to #include "G4VisExecutive.hh" in more than one file
// since after instantiation the object can be treated as a
// G4VisManager.

inline
G4VisExecutive::G4VisExecutive(int argc, char** argv, const G4String& system,
                               const G4String& verbosityString):
G4VisManager(verbosityString),
fSelected(false)
{
  if (!fSelected) SetDefaultsByArgument(system);
  if (!fSelected) SetDefaultsByEnvironment();
  if (!fSelected) SetDefaultsByFile(argc, argv);
  if (!fSelected) SetDefaultsByBatch();
  if (!fSelected) SetDefaultsByBuildFlags();
}

inline
G4VisExecutive::G4VisExecutive (const G4String& verbosityString):
G4VisManager(verbosityString),
fSelected(false)
{
  if (!fSelected) SetDefaultsByEnvironment();
  if (!fSelected) SetDefaultsByBatch();
  if (!fSelected) SetDefaultsByBuildFlags();
}

inline
void G4VisExecutive::SetDefaultsByArgument(const G4String& system)
{
  // 1st priority: selection by G4VisExecuitive argument
  if (!system.empty()) {
    fDefaultGraphicsSystemName = system;
    fDefaultGraphicsSystemBasis = "G4VisExecuitive argument";
    fSelected = true;
  }
}

inline
void G4VisExecutive::SetDefaultsByEnvironment()
{
  // 2nd priority: by environment variable
  if (auto g4env = std::getenv("G4VIS_DEFAULT_DRIVER")) {
    G4String graphicsSystem, windowSizeHint;
    std::istringstream iss(g4env);
    iss >> graphicsSystem >> windowSizeHint;
    if (!graphicsSystem.empty()) {
      fDefaultGraphicsSystemName = graphicsSystem;
      fDefaultGraphicsSystemBasis = "environment G4VIS_DEFAULT_DRIVER";
      fSelected = true;
    }
    if (!windowSizeHint.empty()) {
      fDefaultXGeometryString = windowSizeHint;
      fDefaultXGeometryStringBasis  = "environment G4VIS_DEFAULT_DRIVER";
    }
  }
}

inline
void G4VisExecutive::SetDefaultsByFile(int argc, char** argv)
{
  // 3rd priority: in $HOME/.g4session

  // Find ~/.g4session - simply return if it does not exist
  const char* home = std::getenv("HOME");
  if (home == nullptr) return;
  G4String homedir = home;
#ifndef WIN32
  G4String filename = homedir + "/.g4session";
#else
  G4String filename = homedir + "\\.g4session";
#endif
  std::ifstream g4session(filename);
  if (g4session.fail()) return;

  // Find app name
  if (argc < 1) return;
  G4String appInput = argv[0];
  G4String appName = "";
  size_t islash = appInput.find_last_of("/\\");
  if (islash == G4String::npos) appName = appInput;
  else appName = appInput.substr(islash + 1, appInput.size() - islash - 1);

  // Scan ~/.g4session
  G4String line;
  // First line is the user-chosen default: session, graphics system, windo wize hint
  G4String applicableSession, applicableGraphicsSystem, applicableWindowSizeHint;
  std::getline(g4session,line);
  auto hash = line.find_first_of('#'); line = line.substr(0,hash);
  std::istringstream iss(line);
  iss >> applicableSession >> applicableGraphicsSystem >> applicableWindowSizeHint;
  // Read subsequent lines
  while (g4session.good()) {
    G4String app, session, graphicsSystem, windowSizeHint;
    std::getline(g4session,line);
    hash = line.find_first_of('#'); line = line.substr(0,hash);
    std::istringstream iss1(line);
    iss1 >> app >> session >> graphicsSystem >> windowSizeHint;
    if (app == appName) {
      if (!session.empty())        applicableSession        = session;  // See G4UIExecutive
      if (!graphicsSystem.empty()) applicableGraphicsSystem = graphicsSystem;
      if (!windowSizeHint.empty()) applicableWindowSizeHint = windowSizeHint;
    }
  }

  if (!applicableGraphicsSystem.empty()) {
    fDefaultGraphicsSystemName = applicableGraphicsSystem;
    fDefaultGraphicsSystemBasis = "~/.g4session";
    fSelected = true;
  }
  if (!applicableWindowSizeHint.empty()) {
    fDefaultXGeometryString = applicableWindowSizeHint;
    fDefaultXGeometryStringBasis = "~/.g4session";
  }
}

inline
void G4VisExecutive::SetDefaultsByBatch()
{
  // 4th, special case for batch session
  G4UIsession* session = G4UImanager::GetUIpointer()->GetBaseSession();
  if (session == nullptr                   // Usual case - pure batch
  || dynamic_cast<G4UIbatch*>(session)) {  // From a macro from batch
    // Choose an offscreen or file-writing system
    fDefaultGraphicsSystemName = "TSG_OFFSCREEN";
    fDefaultGraphicsSystemBasis = "batch session";
    fSelected = true;
  }
}

inline
void G4VisExecutive::SetDefaultsByBuildFlags()
{
  // 5th, by cpp flags
#if defined G4VIS_USE_OPENGLQT || G4VIS_USE_OPENGLXM ||\
            G4VIS_USE_OPENGLX  || G4VIS_USE_OPENGLWIN32
  fDefaultGraphicsSystemName = "OGL";
#elif defined G4VIS_USE_OI || G4VIS_USE_OIX
  fDefaultGraphicsSystemName = "OI";
#elif defined G4VIS_USE_TOOLSSG_QT_GLES || G4VIS_USE_TOOLSSG_X11_GLES ||\
              G4VIS_USE_TOOLSSG_XT_GLES || G4VIS_USE_TOOLSSG_WINDOWS_GLES
  fDefaultGraphicsSystemName = "TSG";
#elif defined G4VIS_USE_VTK || G4VIS_USE_VTK_QT
  fDefaultGraphicsSystemName = "Vtk";
#elif defined G4VIS_USE_TOOLSSG_QT_ZB || G4VIS_USE_TOOLSSG_X11_ZB ||\
              G4VIS_USE_TOOLSSG_XT_ZB || G4VIS_USE_TOOLSSG_WINDOWS_ZB
  fDefaultGraphicsSystemName = "TSG";
#elif defined G4VIS_USE_QT3D
  fDefaultGraphicsSystemName = "Qt3D";
#else
  // Choose a graphics system not needing external packages or libraries
  fDefaultGraphicsSystemName = "TSG_OFFSCREEN";
#endif
  fDefaultGraphicsSystemBasis = "build flags";
  fSelected = true;
}

inline void
G4VisExecutive::RegisterGraphicsSystems () {

 // Graphics Systems not needing external packages or libraries...
  RegisterGraphicsSystem (new G4ASCIITree);
  RegisterGraphicsSystem (new G4DAWNFILE);
  RegisterGraphicsSystem (new G4HepRepFile);
  RegisterGraphicsSystem (new G4RayTracer);
  RegisterGraphicsSystem (new G4VRML2File);
  RegisterGraphicsSystem (new G4GMocrenFile);
  RegisterGraphicsSystem (new G4ToolsSGOffscreen);
  G4VGraphicsSystem* tsg_offscreen = new G4ToolsSGOffscreen;
  RegisterGraphicsSystem(tsg_offscreen);
  tsg_offscreen->AddNickname("TSG_FILE");

// Graphics systems needing external packages or libraries...
// Register OGL family of drivers with their normal names,
// super-abbreviated names and fallback names where approproiate.

#ifdef G4VIS_USE_OPENGLQT
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
  G4VGraphicsSystem* ogliqt = new G4OpenGLImmediateQt;
  G4VGraphicsSystem* oglsqt = new G4OpenGLStoredQt;
  RegisterGraphicsSystem(ogliqt);
  RegisterGraphicsSystem(oglsqt);
  ogliqt->AddNickname("OGLI");
  oglsqt->AddNickname("OGL");
  oglsqt->AddNickname("OGLS");
#endif
#endif

#ifdef G4VIS_USE_OPENGLXM
  G4VGraphicsSystem* oglixm = new G4OpenGLImmediateXm;
  G4VGraphicsSystem* oglsxm = new G4OpenGLStoredXm;
  RegisterGraphicsSystem(oglixm);
  RegisterGraphicsSystem(oglsxm);
#  ifdef G4VIS_USE_OPENGLQT
  oglixm->AddNickname("OGLIQt_FALLBACK");
  oglsxm->AddNickname("OGLSQt_FALLBACK");
#  else
  oglixm->AddNickname("OGLI");
  oglsxm->AddNickname("OGL");
  oglsxm->AddNickname("OGLS");
#  endif
#endif

#ifdef G4VIS_USE_OPENGLX
  G4VGraphicsSystem* oglix = new G4OpenGLImmediateX;
  G4VGraphicsSystem* oglsx = new G4OpenGLStoredX;
  RegisterGraphicsSystem(oglix);
  RegisterGraphicsSystem(oglsx);
#  ifdef G4VIS_USE_OPENGLQT
  oglix->AddNickname("OGLIQt_FALLBACK");
  oglsx->AddNickname("OGLSQt_FALLBACK");
#  endif
#  ifdef G4VIS_USE_OPENGLXM
  oglix->AddNickname("OGLIXm_FALLBACK");
  oglsx->AddNickname("OGLSXm_FALLBACK");
#  endif
#  if defined(G4VIS_USE_OPENGLQT) || (G4VIS_USE_OPENGLXM)
#  else
  oglix->AddNickname("OGLI");
  oglsx->AddNickname("OGL");
  oglsx->AddNickname("OGLS");
#  endif
#endif

#ifdef G4VIS_USE_OPENGLWIN32
  G4VGraphicsSystem* ogliwin32 = new G4OpenGLImmediateWin32;
  G4VGraphicsSystem* oglswin32 = new G4OpenGLStoredWin32;
  RegisterGraphicsSystem(ogliwin32);
  RegisterGraphicsSystem(oglswin32);
#  ifdef G4VIS_USE_OPENGLQT
  ogliwin32->AddNickname("OGLIQt_FALLBACK");
  oglswin32->AddNickname("OGLSQt_FALLBACK");
#  else
  ogliwin32->AddNickname("OGLI");
  oglswin32->AddNickname("OGL");
  oglswin32->AddNickname("OGLS");
#  endif
#endif

// Register OI graphics system with super-abbreviated nickname
#ifdef G4VIS_USE_OI
  G4VGraphicsSystem* oi = 0;
#  ifdef G4VIS_USE_OIX
  oi = new G4OpenInventorX;
#  elif defined(G4VIS_USE_OIQT)
  oi = new G4OpenInventorQt;
#  elif defined(G4VIS_USE_OIWIN32)
  oi = new G4OpenInventorWin32;
#  endif
  if(oi)
  {
    RegisterGraphicsSystem(oi);
    oi->AddNickname("OI");
  }
#endif

// The new, extended OI is registered separately for now.
#ifdef G4VIS_USE_OIX
  RegisterGraphicsSystem(new G4OpenInventorXtExtended);
#endif

#ifdef G4VIS_USE_RAYTRACERX
  RegisterGraphicsSystem(new G4RayTracerX);
#endif

#ifdef G4VIS_USE_QT3D
  RegisterGraphicsSystem(new G4Qt3D);
#endif

#ifdef G4VIS_USE_TOOLSSG_X11_GLES
  G4VGraphicsSystem* tsg_x11_gles = new G4ToolsSGX11GLES;
  RegisterGraphicsSystem(tsg_x11_gles);
  tsg_x11_gles->AddNickname("TSGX11");
#ifdef G4VIS_USE_TOOLSSG_XT_GLES
  tsg_x11_gles->AddNickname("TSG_XT_GLES_FALLBACK");
#endif
#ifdef G4VIS_USE_TOOLSSG_QT_GLES
#ifndef G4VIS_USE_TOOLSSG_XT_GLES
  tsg_x11_gles->AddNickname("TSG_QT_GLES_FALLBACK");
#endif
#endif
#endif

#ifdef G4VIS_USE_TOOLSSG_X11_ZB
  G4VGraphicsSystem* tsg_x11_zb = new G4ToolsSGX11ZB;
  RegisterGraphicsSystem(tsg_x11_zb);
  tsg_x11_zb->AddNickname("TSGX11ZB");
#endif

#ifdef G4VIS_USE_TOOLSSG_XT_GLES
  G4VGraphicsSystem* tsg_xt_gles = new G4ToolsSGXtGLES;
  RegisterGraphicsSystem(tsg_xt_gles);
  tsg_xt_gles->AddNickname("TSGXt");
#ifdef G4VIS_USE_TOOLSSG_QT_GLES
  tsg_xt_gles->AddNickname("TSG_QT_GLES_FALLBACK");
#endif
#endif

#ifdef G4VIS_USE_TOOLSSG_XT_ZB
  G4VGraphicsSystem* tsg_xt_zb = new G4ToolsSGXtZB;
  RegisterGraphicsSystem(tsg_xt_zb);
  tsg_xt_zb->AddNickname("TSGXtZB");
#endif

#ifdef G4VIS_USE_TOOLSSG_QT_GLES
  G4VGraphicsSystem* tsg_qt_gles = new G4ToolsSGQtGLES;
  RegisterGraphicsSystem(tsg_qt_gles);
  tsg_qt_gles->AddNickname("TSGQt");
#endif

#ifdef G4VIS_USE_TOOLSSG_QT_ZB
  G4VGraphicsSystem* tsg_qt_zb = new G4ToolsSGQtZB;
  RegisterGraphicsSystem(tsg_qt_zb);
  tsg_qt_zb->AddNickname("TSGQtZB");
#endif

#ifdef G4VIS_USE_TOOLSSG_WINDOWS_GLES
  G4VGraphicsSystem* tsg_windows_gles = new G4ToolsSGWindowsGLES;
  RegisterGraphicsSystem(tsg_windows_gles);
#endif

#ifdef G4VIS_USE_TOOLSSG_WINDOWS_ZB
  G4VGraphicsSystem* tsg_windows_zb = new G4ToolsSGWindowsZB;
  RegisterGraphicsSystem(tsg_windows_zb);
#endif

#ifdef G4VIS_USE_TOOLSSG_QT_GLES
  tsg_qt_gles->AddNickname("TSG");
#  if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
  if (fVerbosity >= startup) {
    G4cout << "  Qt6: Asking ToolsSG to stand in for OpenGL" << G4endl;
    tsg_qt_gles->AddNickname("OGL");
  }
#  endif
#elif defined(G4VIS_USE_TOOLSSG_XT_GLES)
  tsg_xt_gles->AddNickname("TSG");
#elif defined(G4VIS_USE_TOOLSSG_X11_GLES)
  tsg_x11_gles->AddNickname("TSG");
#elif defined(G4VIS_USE_TOOLSSG_WINDOWS_GLES)
  tsg_windows_gles->AddNickname("TSG");
#else
#ifdef G4VIS_USE_TOOLSSG_QT_ZB
  tsg_qt_zb->AddNickname("TSG");
#elif G4VIS_USE_TOOLSSG_WINDOWS_ZB
  tsg_windows_zb->AddNickname("TSG");
#else
  tsg_offscreen->AddNickname("TSG");
#endif
#endif

#ifdef G4VIS_USE_VTK
  if (fVerbosity >= startup) {
    G4cout
    << "  VTK: OpenGL-based drivers suppressed because of compatibility issues."
    << G4endl;
  }
  G4Vtk* vtkN = new G4Vtk();
  G4VtkOffscreen* vtkOS = new G4VtkOffscreen();
  RegisterGraphicsSystem(vtkN);
  RegisterGraphicsSystem(vtkOS);
#  ifdef G4VIS_USE_VTK_QT
  vtkN->AddNickname("VTKQt_FALLBACK");
#  endif
#endif

#ifdef G4VIS_USE_VTK_QT
  G4VtkQt* vtkQt = new G4VtkQt();
  RegisterGraphicsSystem(vtkQt);
  vtkQt->AddNickname("VTKQt");
#endif

#ifdef G4VIS_USE_VTK_QT
  vtkQt->AddNickname("Vtk");
  vtkQt->AddNickname("VTK");
#elif defined(G4VIS_USE_VTK)
  vtkN->AddNickname("Vtk");
  vtkN->AddNickname("VTK");
#endif
}

// See comments about inlining above.
inline void
G4VisExecutive::RegisterModelFactories()
{
   // Trajectory draw models
   RegisterModelFactory(new G4TrajectoryGenericDrawerFactory());
   RegisterModelFactory(new G4TrajectoryDrawByAttributeFactory());
   RegisterModelFactory(new G4TrajectoryDrawByChargeFactory());
   RegisterModelFactory(new G4TrajectoryDrawByOriginVolumeFactory());
   RegisterModelFactory(new G4TrajectoryDrawByParticleIDFactory());
   RegisterModelFactory(new G4TrajectoryDrawByEncounteredVolumeFactory());

   // Trajectory filter models
   RegisterModelFactory(new G4TrajectoryAttributeFilterFactory());
   RegisterModelFactory(new G4TrajectoryChargeFilterFactory());
   RegisterModelFactory(new G4TrajectoryOriginVolumeFilterFactory());
   RegisterModelFactory(new G4TrajectoryParticleFilterFactory());
   RegisterModelFactory(new G4TrajectoryEncounteredVolumeFilterFactory());

   // Hit filter models
   RegisterModelFactory(new G4HitAttributeFilterFactory());

   // Digi filter models
   RegisterModelFactory(new G4DigiAttributeFilterFactory());
}

#endif

Message when I request compilation. Note that I used “./tutorial003” to compile.

ranulfo@ranulfo:~/Documentos/GEANT4_Tutorial/Tutorial/Aula 3$ source compile.sh
In file included from /home/ranulfo/Programas/Geant4/geant4-v11.2.1/geant4-v11.2.0-install/include/Geant4/G4VisExecutive.hh:141,
                 from tutorial003.cpp:79:
/home/ranulfo/Programas/Geant4/geant4-v11.2.1/geant4-v11.2.0-install/include/Geant4/G4VisExecutive.icc:142:10: fatal error: QtGlobal: Arquivo ou diretório inexistente
  142 | #include <QtGlobal>
      |          ^~~~~~~~~~
compilation terminated.
ranulfo@ranulfo:~/Documentos/GEANT4_Tutorial/Tutorial/Aula 3$ ./tutorial003 

**************************************************************
 Geant4 version Name: geant4-11-02-patch-01 [MT]   (16-February-2024)
                       Copyright : Geant4 Collaboration
                      References : NIM A 506 (2003), 250-303
                                 : IEEE-TNS 53 (2006), 270-278
                                 : NIM A 835 (2016), 186-225
                             WWW : http://geant4.org/
**************************************************************

G4PhysListFactory::GetReferencePhysList <Shielding>  EMoption= 0
<<< Geant4 Physics List simulation engine: Shielding
<<< (Note that Shielding and Shielding_HP are equivalent!)


 hInelastic Shielding : threshold between BERT and FTFP is over the interval 
 for pions :   3 to 6 GeV
 for kaons :   3 to 6 GeV
 for proton :  3 to 6 GeV
 for neutron : 3 to 6 GeV

@@@ G4ParticleHPInelasticData instantiated for particle neutron data directory variable is G4NEUTRONHPDATA pointing to /home/ranulfo/Programas/Geant4/geant4-v11.2.1/geant4-v11.2.0-install/share/data/G4NDL4.7
Visualization Manager instantiating with verbosity "warnings (3)"...
Visualization Manager initialising...
Registering graphics systems...

You have successfully registered the following graphics systems.
Registered graphics systems are:
  ASCIITree (ATree)
  DAWNFILE (DAWNFILE)
  G4HepRepFile (HepRepFile)
  RayTracer (RayTracer)
  VRML2FILE (VRML2FILE)
  gMocrenFile (gMocrenFile)
  TOOLSSG_OFFSCREEN (TSG_OFFSCREEN)
  TOOLSSG_OFFSCREEN (TSG_OFFSCREEN, TSG_FILE, TSG)
  OpenGLImmediateX (OGLIX, OGLI)
  OpenGLStoredX (OGLSX, OGL, OGLS)
Default graphics system is: TSG_OFFSCREEN (based on batch session).
Default window size hint is: 600x600-0+0 (based on G4VisManager initialisation).
Note: Parameters specified on the command line will override these defaults.
      Use "vis/open" without parameters to get these defaults.

Registering model factories...

You have successfully registered the following model factories.
Registered model factories:
  generic
  drawByAttribute
  drawByCharge
  drawByOriginVolume
  drawByParticleID
  drawByEncounteredVolume

Registered models:
  None

Registered filter factories:
  attributeFilter
  chargeFilter
  originVolumeFilter
  particleFilter
  encounteredVolumeFilter

Registered filters:
  None

You have successfully registered the following user vis actions.
Run Duration User Vis Actions: none
End of Event User Vis Actions: none
End of Run User Vis Actions: none

Some /vis commands (optionally) take a string to specify colour.
"/vis/list" to see available colours.
Available UI session types: [ Qt, tcsh, csh ]
Idle> 

When I ask to compile “./tutorial003 muons.mac”. Compiles, opens a window, but is not a graphical interface.

In short, the code generates the simulation correctly, has this problem and does not open the graphical interface to see the interaction in more detail. I can even save the results in a txt format file.

Remove these lines, as setting these defines is the job of the CMake/geant4-config scripts and they shouldn’t be defined manually. Assuming these are removed, and that G4VisExecutive.icc is restored to its default state, please try

g++ tutorial003.cpp `geant4-config --libs --cflags` -o tutorial003

If that still causes problems, please post the error messages, and also what

geant4-config --libs --cflags

prints.

I compiled the code, left it in a comment:

//#define G4VIS_USE_OPENGLX
//#define G4VIS_USE_OPENGLQT

I used the following command:

g++ tutorial003.cpp `geant4-config --libs --cflags` -o tutorial003

Answer I got:

ranulfo@ranulfo:~/Documentos/GEANT4_Tutorial/Tutorial/Class 3$ source compile.sh
In file included from /usr/include/x86_64-linux-gnu/qt5/QtCore/QtGlobal:1,
                  from /home/ranulfo/Programas/Geant4/geant4-v11.2.1/geant4-v11.2.0-install/include/Geant4/G4VisExecutive.icc:142,
                  from /home/ranulfo/Programas/Geant4/geant4-v11.2.1/geant4-v11.2.0-install/include/Geant4/G4VisExecutive.hh:141,
                  from tutorial003.cpp:79:
/usr/include/x86_64-linux-gnu/qt5/QtCore/qglobal.h:1281:4: error: #error "You must build your code with position independent code if Qt was built with -reduce-relocations. " "Compile your code with -fPIC (and not with -fPIE)."
  1281 | # error "You must build your code with position independent code if Qt was built with -reduce-relocations."\
       | ^~~~~
ranulfo@ranulfo:~/Documentos/GEANT4_Tutorial/Tutorial/Class 3$

So the message that is being printed (with the #error directive) is:

You must build your code with position independent code if Qt was built with -reduce-relocations.
Compile your code with -fPIC (and not with -fPIE).

That message is coming directly from qtglobal.h Have you tried adding the -fPIC flag to your build flags, with a command like

g++ tutorial003.cpp `geant4-config --libs --cflags` -fPIC -o tutorial003
1 Like

I got this answer:

ranulfo@ranulfo:~/Documentos/GEANT4_Tutorial/Tutorial/Class 3$ source compile.sh
tutorial003.cpp:75: warning: "G4VIS_USE_OPENGLX" redefined
    75 | #define G4VIS_USE_OPENGLX
       |
<command-line>: note: this is the location of the previous definition
tutorial003.cpp:76: warning: "G4VIS_USE_OPENGLQT" redefined
    76 | #define G4VIS_USE_OPENGLQT
       |
<command-line>: note: this is the location of the previous definition
In file included from /home/ranulfo/Programas/Geant4/geant4-v11.2.1/geant4-v11.2.0-install/include/Geant4/G4VisExecutive.hh:141,
                  from tutorial003.cpp:79:
/home/ranulfo/Programas/Geant4/geant4-v11.2.1/geant4-v11.2.0-install/include/Geant4/G4VisExecutive.icc:292:32: error: operator '||' has no right operand
   292 | G4VIS_USE_OPENGLX || G4VIS_USE_OPENGLWIN32
       | ^~
ranulfo@ranulfo:~/Documentos/GEANT4_Tutorial/Tutorial/Class 3$

To solve this, you need to change this line in the G4VisExecutive.icc file, but I restored it to the original format.

You haven’t removed/commented out those #define lines properly in tutorial003.cpp

Sorry for the error, now I removed the lines and this was the answer

ranulfo@ranulfo:~/Documentos/GEANT4_Tutorial/Tutorial/Class 3$ source compile.sh
/usr/bin/ld: /tmp/ccIlDVPW.o: undefined reference to symbol "qt_version_tag@@Qt_5.15"
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libQt5Core.so.5: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
ranulfo@ranulfo:~/Documentos/GEANT4_Tutorial/Tutorial/Class 3$ 

Please try adding the -Wl,--no-as-needed flag as follows:

g++ tutorial003.cpp -Wl,--no-as-needed `geant4-config --libs --cflags` -fPIC -o tutorial003

and also the output of

geant4-config --libs --cflags

Response:

ranulfo@ranulfo:~/Documentos/GEANT4_Tutorial/Tutorial/Class 3$ source compile.sh
/usr/bin/ld: /tmp/ccAyOY9j.o: undefined reference to symbol "qt_version_tag@@Qt_5.15"
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libQt5Core.so.5: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
ranulfo@ranulfo:~/Documentos/GEANT4_Tutorial/Tutorial/Class 3$

I need to see this info as well…

The compile used is:

#  Define an environment variable G4INCLUDES that points to 
# your installation path, where the includes are. It is usually
# something like: /path/to/geant4-install/include/Geant4

g++ tutorial003.cpp -Wl,--no-as-needed `geant4-config --libs --cflags` -fPIC -o tutorial003

Sorry, I meant run the command

geant4-config --libs --cflags

in the terminal and post what it prints please.