Geant3 to Geant4 Geometry Conversion Tools

Torre Wenaus, LLNL 6/95

Summary

Approach

There are two basic ways of approaching conversion of Geant3 geometries to Geant4: generation of a Geant4 geometry specification 'on-the-fly' as Geant3 geometry-building code is executed, or analysing a Geant3 RZ file to generate an equivalent Geant4 geometry ('geometry' here is shorthand for all the data structures set up during initialization: the geometry itself, materials, tracking media, tracking parameters, particles, sensitive detectors and hit definitions).

Both approaches are offered in the present package. The RZ conversion is implemented as a standalone program which analyzes the RZ file to determine the G3 calls required to build it, and makes the appropriate calls to the G3->G4 conversion package. The conversion code consists of replacements for Geant3 geometry and other data structure definition routines. These serve as wrappers for the real routines; they perform the G3->G4 conversion and also call the original G3 routine (except in the case of RZ file conversion). For many Geant3 applications it is necessary that the G3 code be executed so that the geometry is actually built during the conversion process, because user code often makes reference during geometry building to data structures generated earlier via GFxxxx calls (eg. very often tracking medium structures are accessed during geometry building).

Of course, a wrapper routine must have a name different from the routine it is wrapping. Modifying the G3 libraries to rename the Gxxxxx routines would be a major hassle for the general user, so the approach taken is to change the names of the conversion routines (and therefore the routines referenced in user code to do the conversion) instead. The conversion routines are named Kxxxxx rather than Gxxxxx. A Perl script is provided to convert user routines to call Kxxxxx rather than Gxxxxx (and back again).

The conversion routines offer two approaches to on-the-fly conversion to Geant4:

The former approach generates a large body of C++ code which must be compiled into the G4 program, but then will execute quickly as integral code. C++ compiler limitations require that the C++ code generated be divided into chunks of not too great a size for the compiler to digest; 1000 G3 routines per function is found to work on the compilers tested (the number can easily be adjusted). The converter automatically generates a wrapper routine for the N files of G3 routines generated; the user calls the wrapper and can ignore the subdivision of the code.

In the latter approach no compile/rebuild of the G4 program is necessary at all to incorporate a new or revised G3 geometry specification; the call list interpreter will just interpret the new file. Thus the former may be preferable where the G3-converted geometry is stable, the latter where updates to the G3 geometry need to be quickly and easily available to the G4 program.

Usage

Source code conversion

The G3->G4 conversion package is used as follows:
  • Run the Perl script g3tog4.pl on the application source code to convert Gxxxxx calls to Kxxxxx calls. It is used as follows:
           g3tog4.pl uginit.f
    
    performs Gxxxxx->Kxxxxx conversion on uginit.f and saves an unmodified copy in uginit.f.old. Thus an easy way to incorporate this conversion step is to add it to the script that does Fortran compilation for your application's build procedure.
  • Introduce a call
             call g3tog4(lunl,lunc,' ')
    
    early in your program, before any Geant routines are called. Parameters:
          LUNL  =0: no call list output generated
                >0: call list written to unit LUNL; filename g3calls.dat
                    is opened internally
                <0: call list written to unit -LUNL; file assumed already
                    open
          LUNC  =0: no C++ code is generated
                >0: C++ code wrapper written to g3tog4code.cc. Function
                         void g3tog4code();
                    should be called to run the code. Actual g3tog4 C++
                    sources are written to g3tog4code_nn.cc where nn
                    goes from 01 to the number of files required.
                <0: not supported.
          OPT   ' ' no options supported at present
    
  • Build and run your program. The conversion output is closed out when GGCLOS (or rather, KGCLOS) is encountered. In the directory from which you ran the program look for
            
          g3calls.dat
          g3tog4code.cc
          g3tog4code_nn.cc
    
    depending on the parameters you passed to subroutine g3tog4.

    RZ file conversion

    A standalone program, rztog4, performs the conversion of a Geant RZ file. It is used as follows...
         rztog4  rzfile.name
    
    and generates a call list file (g3calls.dat) and C++ source code (g3tog4code*.cc).

    Importing converted geometries into Geant4

    The cltog4 program reads the call list generated eg. by rztog4 and generates G4's RZ-equivalent initialization file as output, for use in a normal G4 run. [At the present time, the only output is a log file 'clparse.out' that logs the constructors and methods called in the generation of the geometry.]

    It is trivial to use the directly-generated C++ in G4. g4geom.cc does this and is a few lines long:

    void g3tog4code();
    main()
    {
        g3tog4code();
    }
    
    g4geom.com is the needed linker. The G3->G4 tools haven't yet been organized with a makefile and link library, but soon will be.

    The Geant4 Code

    The Geant4 calling interface as implemented in the G4.h, G4.cc files is based on the definition of a preliminary interface in discussions with the Geant4 team.

    The C++ code generated directly by the converter looks very much like Fortran code; it uses (for compactness) a set of C++ routines which are a direct mapping to the Geant3 routines. The real content of the Geant3 to Geant4 conversion is in the G4gxxxx.cc routines, which implement the Geant3 routines in terms of Geant4.

    Here are examples of the implementation of Geant3 routines in Geant4:

    Status

    Conversion of both G3 code on-the-fly and G3 RZ files (the rztog4 program) are implemented. Conversion options are directly generated G4 C++, and/or generation of an intermediate 'call list' file. The cltog4 program converts to the call list, presently to 'empty' G4 class instantiations; eventually, to G4's equivalent of an RZ file. The g4code.cc program is an example of the use of the automatically generated G4 C++ to build the C++ geometry.

    The package has been tested with the geometries of BaBar, CMS, Atlas, Zeus, L3, and Opal. The only problem was with Atlas, and the problem is due to a bug in the Atlas geometry.