8.2.  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 realized 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.

8.2.1.  Installing Visualization Drivers

Depending on what has been installed on your system, 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 Section 8.3 "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 8.1 in Section 8.3 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.

Unless the environment variable G4VIS_NONE is set to "1" (which causes the makefiles to set a corresponding C-pre-processor macro), visualization drivers that do not depend on external libraries are automatically 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. In order to incorporate them, environment variables must be set (they cause the makefiles to set the corresponding C-pre-processor macros). This is best done by sourcing the scripts generated by ./Configure (without any options).

  ./Configure
  ...
  source env.sh (or env.csh for C shells)

If you wish to "do-it-yourself", use "export" (for Bourne-like shells, including bash) or "setenv" (for C-shells). "G4VIS_BUILD_DRIVERNAME_DRIVER" should be set to "1" before installing the Geant4 libraries:

     setenv G4VIS_BUILD_OPENGLX_DRIVER      1  # OpenGL-Xlib driver
     setenv G4VIS_BUILD_OPENGLXM_DRIVER     1  # OpenGL-Motif driver 
     setenv G4VIS_BUILD_OPENGLQT_DRIVER     1  # Qt driver 
     setenv G4VIS_BUILD_OIX_DRIVER          1  # OpenInventor-Xlib driver
     setenv G4VIS_BUILD_RAYTRACERX_DRIVER   1  # RayTracer-XLib driver
     setenv G4VIS_BUILD_DAWN_DRIVER         1  # DAWN-Network driver
     setenv G4VIS_BUILD_VRML_DRIVER         1  # VRML-Network

Unless the environment variable G4VIS_NONE is set to "1", setting any of the above variables sets a C-pre-processor flag of the same name. Also the C-pre-processor flag G4VIS_BUILD is set (see config/G4VIS_BUILD.gmk), which incorparates the selected driver into the Geant4 libraries.

8.2.2.  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->RegisterGraphicsSytem(new XXX);
     visManager->Initialize();

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 selected with "Configure" or by setting the proper environment variable:

     setenv G4VIS_USE_OPENGLX      1
     setenv G4VIS_USE_OPENGLXM     1
     setenv G4VIS_USE_OPENGLQT     1
     setenv G4VIS_USE_OIX          1
     setenv G4VIS_USE_RAYTRACERX   1
     setenv G4VIS_USE_DAWN         1
     setenv G4VIS_USE_VRML         1

(Of course, this has to be chosen from the set incorporated into the Geant4 libraries during their compilation.) Unless the environment variable G4VIS_NONE is set, these set C-pre-processor flags of the same name.

Also, unless the environment variable G4VIS_NONE is set, the C-pre-processor flag G4VIS_USE is always set by default. This flag is available in describing the main() function.

You may have to set additional environment variables for your selected visualization drivers and graphics systems. For example, the OpenGL driver may require the setting of OGLHOME which points to the location of the OpenGL libraries. For more details, see Section 8.3 "Visualization Drivers" and pages linked from there.

8.2.3.  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.

8.2.4.  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).

Example 8.1 shows the form of the main() function.

Example 8.1.  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
 #ifdef G4VIS_USE
 G4VisManager* visManager = new G4VisExecutive;
 // G4VisExecutive can take a verbosity argument - see /vis/verbose guidance.
 // G4VisManager* visManager = new G4VisExecutive("Quiet");
 visManager->Initialize();
 #endif

 .....
 #ifdef G4VIS_USE
 delete visManager;
 #endif

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


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

Example 8.2.  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.

We recommend that the instantiation, initialization, and deletion of the Visualization Manager be protected by C-pre-processor commands, as in the basic examples. The C-pre-processor macro G4VIS_USE is automatically defined unless the environment variable G4VIS_NONE is set. This assumes that you are compiling your Geant4 executable with the standard version of GNUmakefile found in the config directory.

Example 8.3 shows an example of the main() function available for Geant4 Visualization.

Example 8.3.  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);

 #ifdef G4VIS_USE
      G4VisManager* visManager = new G4VisExecutive;
      visManager->Initialize(argc, argv);
 #endif

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

      delete ui;
      delete runManager;

 #ifdef G4VIS_USE
      delete visManager;
 #endif

      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.)