Analysis Tools in Geant4 10.2 and 10.3

A new analysis category based on g4tools was added in Geant4 release 9.5 (2011). The aim was to provide users with a lightweight analysis tool available as part of the Geant4 installation without the need to link to an external analysis package. It has progressively been included in all Geant4 examples. Frequent questions in the Geant4 users forum show its increasing popularity in the Geant4 users community. In this presentation, we will give a brief overview of g4tools and the analysis category. We report on new developments since our CHEP 2013 contribution as well as mention upcoming new features.


g4tools
The g4tools [1] code is part of the inlib and exlib [1] packages which were originally developed to build highly portable visualisation and analysis applications running on both laptop/desktop (Linux, MacOS, Windows) and mobile (then iOS and Android) operating systems. The code is mainly pure header and there is no config.h logic, so that it is easy to install and embed. It is strongly object-oriented. There is no implicit global management of instances and no writable statics. Thus, it is very modular and thread safe (see [1] and [2] for more).
For Geant4 [3], g4tools provides histograms, profiles and ntuples, together with code to write them to several supported file formats (ROOT [4], CSV, AIDA XML) and, when needed, also read them back from the files. We supported HBOOK for some time but this format was dropped with 10.3.
Since Geant4 10.2, g4tools includes the graphical core of softinex [2], based on a scene graph logic, that permits plotting (histogram visualisation) within Geant4. Only a default postscript driver, which does not induce to attach to any external libraries, is included right now. PNG or JPEG files could be produced at the cost of a link toward externals, if there are user requests for these formats. There is also no GL-ES, X11, Windows, iOS or Android specific code included in g4tools at present to do this plotting on a screen. Nonetheless, code exists in softinex that would permit to plot for example in an OpenGL window created through the visualization system of Geant4. Text rendering, a key issue in plotting, uses the provided HPLOT fonts, but better rendering is possible using an optional interface to the FreeType [6] library which can be enabled when configuring and building Geant4.
To help support multithreading and multiprocessing, code has been added to pass around histograms in MPI messages. A recent development here uses the passing of baskets/pages of data to parallelise filling of an ntuple in a single ROOT format output file. The data are filled directly by parallel threads and via MPI messages by parallel processes.

Geant4 Analysis
The classes in the analysis category provide a uniform, user-friendly interface to g4tools and hide the differences from the user depending on which output technology was selected [5] . They take care of the higher-level management of the g4tools objects (files, histograms and ntuples), handle allocation and removal of the objects in memory and provide the access methods to them via indexes. They are fully integrated in the Geant4 framework: they follow Geant4's coding style and also implement the built-in Geant4 user interface commands that can be used by users to define or configure their analysis objects. For simplicity of use, all user interface analysis functions are provided within a single class which is seen by the user as G4AnalysisManager. Internally, this type is defined via a typedef and it can point to one of three output type specific manager classes: Each class implements the common interfaces, specific access functions (with a return type specific to the output format) and the singleton access method Instance().

New Features since Geant4 10.0
Geant4 analysis and g4tools presentations at the CHEP 2013 conference ( [5], [1]) covered the functionalities available in Geant4 10.0. In this section, we will present the major new developments since that time. A global view of the Geant4 analysis tools capabilities, including the new features presented in this section, is shown in Fig. 1.

Histograms and Profiles
The set of supported histogram types was extended in Geant4 10.1 with three dimensional histograms and one and two dimensional profile histograms (profiles). Profile histograms (profiles) are used to display the mean value of Y and its error for each bin in X. The displayed error is by default the standard error on the mean (i.e. the standard deviation divided by the sqrt(n).) Since this Geant4 version, histograms and profiles can also be written in the CSV file format, while in previous versions, CSV could only be used with ntuples. Each histogram or profile is written in its own CSV output file, the name of which is generated automatically from the base file name.

Analysis Reader
The analysis reader classes were introduced in Geant4 10.1. They allow reading in g4tools objects from the files generated by the analysis manager(s) during the run of a Geant4 application without the need to link to an external analysis package.
The design of these classes follows the same pattern used for the manager classes: all user interface analysis reader functions are provided within a single class which is seen by the user as G4AnalysisReader. Internally, this type is defined via a typedef and it can point to one of three output type specific manager classes: While the histograms and profiles objects handled by the analysis reader are of the same type as those handled by the analysis manager, the ntuple type is different. The objects loaded with the analysis reader are handled independently from the objects created with analysis manager.

Batch plotting facility
Since Geant4 10.2, it is possible to produce a graphics output file in the Postscript format containing selected histograms and profiles. Users can activate plotting of selected objects using the G4AnalysisManager functions or the Geant4 user interface commands. The Postscript file is then created in addition to the standard output files. At present, only the Postscript format is available. Users can choose from three plotting styles, two of which require the Geant4 library to be built with support for FreeType [6] font rendering. The user can also select the layout of the plots on the page, defined by the number of columns and the number of rows in a page. The page size of the graphics output is fixed to A4 format. Finally, users can also customise the plot dimensions, which represent the plotter window size (width and height) in pixels. Opening