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, VRML1FILE, VRML2FILE and ATree and GAGTree.

The OpenGL, Qt, OpenInventor and RayTracerX drivers are not incorporated by default. Nor are the DAWN-Network and VRML-Network drivers, because they require the network setting of the installed machine. 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, a provided class with included implementation. G4VisExecutive is sensitive to the G4VIS_USE... variables mentioned below.

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, VRML1FILE, VRML2FILE, ATree and GAGTree drivers. Additionally, you may choose from the OpenGL-Xlib, OpenGL-Motif, Qt, OpenInventor, RayTracerX, DAWN-Network and VRML-Network drivers, each of which can be enabled when configuring your application through CMake using “component” arguments to find_package as described in Use of Geant4Config.cmake with find_package in CMake. (Of course, this has to be chosen from the set incorporated into the Geant4 libraries during their compilation.)

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 above (but you may write your own class - see above).

Listing 94 shows the form of the main() function.

Listing 94 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++

Alternatively, you can implement an empty RegisterGraphicsSystems() function, and register visualization drivers you want directly in your main() function. See Listing 95.

Listing 95 An alternative style for the main() function.
//----- C++ source codes: How to register a visualization driver directly
//                        in main() function

.....
G4VisManager* visManager = new G4VisExecutive;
visManager -> RegisterGraphicsSystem (new MyGraphicsSystem);
.....
delete visManager

//----- end of C++

Do not forget to delete the instantiated Visualization Manager by yourself. Note that a graphics system for Geant4 Visualization may run as a different process. In that case, the destructor of G4VisManager might have to terminate the graphics system and/or close the connection.

Listing 96 shows an example of the main() function available for Geant4 Visualization.

Listing 96 An example of the main() function available for Geant4 Visualization.
//----- C++ source codes: An example of main() for visualization
.....
#include "G4VisExecutive.hh"
#include "G4UIExecutive.hh"
.....

int main(int argc, char *argv[])
{
     // Run Manager
     G4RunManager * runManager = new G4RunManager;

     // Detector components
     runManager->set_userInitialization(new MyDetectorConstruction);
     runManager->set_userInitialization(new MyPhysicsList);

     // UserAction classes.
     runManager->set_userAction(new MyRunAction);
     runManager->set_userAction(new MyPrimaryGeneratorAction);
     runManager->set_userAction(new MyEventAction);
     runManager->set_userAction(new MySteppingAction);

     G4VisManager* visManager = new G4VisExecutive;
     visManager->Initialize(argc, argv);

     // Define (G)UI
     G4UIExecutive * ui = new G4UIExecutive;
     ui->SessionStart();

     delete ui;
     delete runManager;
     delete visManager;

     return 0;
}

//----- end of C++

Useful information on incorporated visualization drivers can be displayed in initializing the Visualization Manager. This is done by setting the verbosity flag to an appropriate number or string:

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 the following code:

...
G4VisManager* visManager = new G4VisExecutive("Quiet");
visManager->Initialize();
...

(This can also be set with the /vis/verbose command.)