How to Visualize the Detector and Events

Introduction

This section briefly explains how to perform Geant4 Visualization. The description here is based on the sample program examples/basic/B1. More details are given in Visualization.

Visualization Drivers

The Geant4 visualization system was developed in response to a diverse set of requirements:

  1. Quick response to study geometries, trajectories and hits

  2. High-quality output for publications

  3. Flexible camera control to debug complex geometries

  4. Tools to show volume overlap errors in detector geometries

  5. Interactive picking to get more information on visualized objects

No one graphics system is ideal for all of these requirements, and many of the large software frameworks into which Geant4 has been incorporated already have their own visualization systems, so Geant4 visualization was designed around an abstract interface that supports a diverse family of graphics systems. Some of these graphics systems use a graphics library compiled with Geant4, such as OpenGL, Qt or OpenInventor, while others involve a separate application, such as HepRApp or DAWN.

You need not use all visualization drivers. You can select those suitable to your purposes. In the following, for simplicity, we assume that the Geant4 libraries are built with the Qt driver.

If you build Geant4 using the standard CMake procedure, you include Qt by setting GEANT4_USE_QT to ON.

In order to use the the Qt driver, you need the OpenGL library, which is installed in many platforms by default and CMake will find it. (If you wish to “do-it-yourself”, see Installing Visualization Drivers.) The makefiles then set appropriate C-pre-processor flags to select appropriate code at compilation time.

If you are using multithreaded mode, from Geant4 version 10.2 event drawing is performed by a separate thread and you may need to optimise this with special /vis/multithreading commands - see Multithreading commands.

How to Incorporate Visualization Drivers into an Executable

Most Geant4 examples already incorporate visualization drivers. If you want to include visualization in your own Geant4 application, you need to instantiate and initialize a subclass of G4VisManager that implements the pure virtual function RegisterGraphicsSystems().

The provided class G4VisExecutive can handle all of this work for you. G4VisExecutive is sensitive to the G4VIS_... variables (that you either set by hand or that are set for you by GNUMake or CMake configuration):

auto visManager = new G4VisExecutive(argc, argv);

See below for how to use in your main program. Basic example B1 is a good place to look..

If you really want to write your own subclass, rather than use G4VisExecutive, you may do so. You will see how to do this by looking at G4VisExecutive.icc. This subclass must be compiled in the user’s domain to force the loading of appropriate libraries in the right order. A typical extract is:

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

The G4VisExecutive takes ownership of all registered graphics systems, and will delete them when it is deleted at the end of the user’s job (see below).

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

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

An example of a typical main() function is given below.

Writing the main() Method to Include Visualization

Now we explain how to write a visualization manager and the main() function for Geant4 visualization. In order that your Geant4 executable is able to perform visualization, you must instantiate and initialize your Visualization Manager in the main() function. The typical main() function available for visualization is written in the following style:

Listing 25 The typical main() routine available for visualization.
.....
#include "G4VisExecutive.hh"
.....
int main(int argc,char** argv) {
  .....
  // Initialize visualization with the default graphics system
  auto visManager = new G4VisExecutive(argc, argv);
  // Constructors can also take optional arguments:
  // - a graphics system of choice, eg. "OGL"
  // - and a verbosity argument - see /vis/verbose guidance.
  // auto visManager = new G4VisExecutive(argc, argv, "OGL", "Quiet");
  // auto visManager = new G4VisExecutive("Quiet");
  visManager->Initialize();
  .....
  // Job termination
  delete visManager;
  .....
  return 0;
}

We recommend you choose the graphics driver at run time - see Controlling Visualization from Commands. This gives you flexibility to switch drivers easily.

Note that we are here recommending that all jobs instantiate a Visualization Manager. Even in batch mode you may generate an image using one of the file-writing drivers - TSG_OFFSCREEN, VTK_OFFSCREEN, DAWNFILE, VRML2FILE, HepRepFile, RayTracer.

Note also that it is your responsibility to delete the Visualization Manager. A good example of a main() function is examples/basic/B1/exampleB1.cc.

Sample Visualization Sessions

Most Geant4 examples include a vis.mac. Run that macro to see a typical visualization. Read the comments in the macro to learn a little bit about some visualization commands. The vis.mac also includes commented-out optional visualization commands. By uncommenting some of these you can see additional visualization features.

For More Information on Geant4 Visualization

See the Visualization part of this user guide.