Geant4手册目录
Geant4: 2.11.  How to Visualize the Detector and Events

2.11.  How to Visualize the Detector and Events

2.11.1.  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 Chapter 8 "Visualization".

2.11.2.  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 Section 8.2.1.) The makefiles then set appropriate C-pre-processor flags to select appropriate code at compilation time.

2.11.3.  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). See any of the Geant4 examples for how to use G4VisExecutive.

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.

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

Example 2.25.  The typical main() routine available for visualization.

 //----- C++ source codes: main() function for visualization
 #ifdef G4VIS_USE
 #include "G4VisExecutive.hh"
 #endif

 .....

 int main(int argc,char** argv) {

 .....

   // Instantiation and initialization of the Visualization Manager
 #ifdef G4VIS_USE
   // visualization manager
   G4VisManager* visManager = new G4VisExecutive;
   // G4VisExecutive can take a verbosity argument - see /vis/verbose guidance.
   // G4VisManager* visManager = new G4VisExecutive("Quiet");
   visManager->Initialize();
 #endif

 .....

   // Job termination
 #ifdef G4VIS_USE
   delete visManager;
 #endif

 .....

   return 0;
 }

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



In the instantiation, initialization, and deletion of the Visualization Manager, the use of the macro G4VIS_USE is recommended as it is set automatically by the CMake and GNUmake support scripts. This allows one easily to build an executable without visualization, if required, without changing the code (but remember you have to force recompilation whenever you change the environment). Note that it is your responsibility to delete the instantiated Visualization Manager by yourself. A complete description of a sample main() function is described in examples/basic/B1/exampleB1.cc.

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

2.11.6.  For More Information on Geant4 Visualization

See the Chapter 8 "Visualization" part of this user guide.

<-- 2.10. How to Execute a Program  Chapter 3. Toolkit Fundamentals --> "