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

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.

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 proceedure is described in detail in the Installation Guide, to which you should refer.

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 set at "Cmake" or "GNUMakefile step", see Section 2. (Of course, this has to be chosen from the set incorporated into the Geant4 libraries during their compilation.)

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. To see the behaviour of C-pre-processor macro G4VIS_USE and G4UI_USE, see Section 2.

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
 #ifdef G4UI_USE
      G4UIExecutive * ui = new G4UIExecutive;
      ui->SessionStart();

      delete ui;
#endif
      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.)