Adding Visualization to Your Executable

This section explains how to incorporate your selected visualization drivers into the main() function and create an executable for it. In order to perform visualization with your Geant4 executable, you must compile it with support for the required visualization driver(s). You may be dazzled by the number of choices of visualization driver, but you need not use all of them at one time.

Installing Visualization Drivers

Depending on what has been installed on your system and how the Geant4 install you are using was configured, several kinds of visualization driver are available. One or many drivers may be chosen for realization in compilation, depending on your visualization requirements. Features and notes on each driver are briefly described in The Visualization Drivers, along with links to detailed web pages for the various drivers.

Note that not all drivers can be installed on all systems; Table 21 in The Visualization Drivers lists all the available drivers and the platforms on which they can be installed. For any of the visualization drivers to work, the corresponding graphics system must be installed beforehand.

Visualization drivers that do not depend on external libraries are by default incorporated into Geant4 libraries during their installation. Here “installation of Geant4 libraries” means the generation of Geant4 libraries by compilation. The automatically incorporated visualization drivers are: DAWNFILE, HepRepFile, HepRepXML, RayTracer, VRML2FILE and ATree and GAGTree.

The OpenGL, Qt, OpenInventor, Qt3D, TSG, Vtk and RayTracerX drivers are not incorporated by default. These drivers must be selected when you build the Geant4 Toolkit itself. This procedure is described in detail in the Installation Guide, to which you should refer.

How to Realize Visualization Drivers in an Executable

You can realize and use any of the visualization driver(s) you want in your Geant4 executable, provided they are among the set installed beforehand into the Geant4 libraries. A warning will appear if this is not the case.

In order to realize visualization drivers, you must instantiate and initialize a subclass of G4VisManager that implements the pure virtual function RegisterGraphicsSystems(). This subclass must be compiled in the user’s domain to force the loading of appropriate libraries in the right order. The easiest way to do this is to use G4VisExecutive:

auto visManager = new G4VisExecutive(argc, argv);

If you do wish to write your own subclass…

…you may do so. You will see how to do this by looking at G4VisExecutive.icc. A typical extract is:

...
  RegisterGraphicsSystem (new G4DAWNFILE);
...
#ifdef G4VIS_USE_OPENGLX
  RegisterGraphicsSystem (new G4OpenGLImmediateX);
  RegisterGraphicsSystem (new G4OpenGLStoredX);
#endif
...

If you wish to use G4VisExecutive but register an additional graphics system, XXX say, you may do so either before or after initializing:

visManager->RegisterGraphicsSystem(new XXX);
visManager->Initialize();

and add the library name to your CMakeLists.txt file:

target_link_libraries(exampleB1 G4visXXX ${Geant4_LIBRARIES})

By default…

…you get the DAWNFILE, HepRepFile, RayTracer, VRML2FILE and ATree drivers.

Optionally…

…you may install the OpenGL-Xlib, OpenGL-Motif, Qt, OpenInventor, Qt3D, TSG, Vtk and RayTracerX drivers, each of which can be enabled when configuring your application through CMake options - see Installation Guide.

For more details, see The Visualization Drivers and pages linked from there.

Visualization Manager

Visualization procedures are controlled by the “Visualization Manager”, a class which must inherit from G4VisManager defined in the visualization category. Most users will find that they can just use the default visualization manager, G4VisExecutive. The Visualization Manager accepts users’ requests for visualization, processes them, and passes the processed requirements to the abstract interface, i.e., to the currently selected visualization driver.

How to Write the main() Function

In order for your Geant4 executable to perform visualization, you must instantiate and initialize “your” Visualization Manager in the main() function. The core of the Visualization Manager is the class G4VisManager, defined in the visualization category. This class requires that one pure virtual function be implemented, namely, void RegisterGraphicsSystems(). The easiest way to do this is to use G4VisExecutive, as described in Listing 98.

Listing 98 The form of the main() function.
//----- C++ source codes: Instantiation and initialization of G4VisManager
.....
// Your Visualization Manager
#include "G4VisExecutive.hh"
.....
// Instantiation and initialization of the Visualization Manager
G4VisManager* visManager = new G4VisExecutive;
// G4VisExecutive can take a verbosity argument - see /vis/verbose guidance.
// G4VisManager* visManager = new G4VisExecutive("Quiet");
visManager->Initialize();
.....
delete visManager;
//----- end of C++

Do not forget to delete the instantiated Visualization Manager.

Listing 99 shows a complete main() function (but see the examples distributed with Geant4 for more ideas).

Listing 99 A main() function with interaction and visualization.
.....
#include "G4VisExecutive.hh"
#include "G4UIExecutive.hh"
.....
int main(int argc,char** argv)
{
  auto ui = new G4UIExecutive(argc, argv);

  auto* runManager = G4RunManagerFactory::CreateRunManager();

  runManager->SetUserInitialization(new DetectorConstruction);
  auto physicsList = new QBBC;
  runManager->SetUserInitialization(physicsList);
  runManager->SetUserInitialization(new ActionInitialization);

  auto visManager = new G4VisExecutive;
  visManager->Initialize();

  ui->SessionStart();

  delete visManager;
  delete runManager;
  delete ui;
}

The visualization manager prints useful information depending on the verbosity level:

Simple graded message scheme - give first letter or a digit:
 0) quiet,         // Nothing is printed.
 1) startup,       // Startup and endup messages are printed...
 2) errors,        // ...and errors...
 3) warnings,      // ...and warnings...
 4) confirmations, // ...and confirming messages...
 5) parameters,    // ...and parameters of scenes and views...
 6) all            // ...and everything available.

For example, in your main() function, write:

G4VisManager* visManager = new G4VisExecutive("Quiet");

or change with the /vis/verbose command.