9.2.  Analysis Manager Classes

The analysis manager classes provide uniform interfaces to the g4tools package and hide the differences between use of g4tools classes for the supported output formats (ROOT, AIDA XML, CSV and HBOOK).

An analysis manager class is available for each supported output format:

All these manager classes implement:

In order to avoid a dependence of the Geant4 kernel libraries on CERNLIB the G4HbookAnalysisManger class is not included in the Geant4 analysis class category but in examples/extended/common/analysis together with all necessary configuration files for its build with the CERNLIB libraries.

9.2.1.  Histograms

A complete example of use of analysis manager classes is provided in basic example B4, in the B4RunAction and B4EventAction classes. The code for handling histograms given in the following example is extracted from these classes.

Example 9.1.  Example with Histograms

#include "B4Analysis.hh"

void B4RunAction::BeginOfRunAction(const G4Run* run) 
{ 
  // Get/create analysis manager
  G4AnalysisManager* man = G4AnalysisManager::Instance();
  
  // Open an output file
  man->OpenFile("B4");

  // Create histogram(s)
  man->CreateH1("0","Edep in absorber", 100, 0., 800*MeV);
  man->CreateH1("1","Edep in gap", 100, 0., 100*MeV);
}

void N4EventAction::EndOfEventAction(const G4Run* aRun)
{
  // Fill histograms
  G4AnalysisManager* man = G4AnalysisManager::Instance();
  man->FillH1(0, fEnergyAbs);
  man->FillH1(1, fEnergyGap);
}

void N4RunAction::EndOfRunAction(const G4Run* aRun)
{
  // Save histograms
  G4AnalysisManager* man = G4AnalysisManager::Instance();
  man->Write();
  man->CloseFile();
  // Complete clean-up
  delete G4AnalysisManager::Instance();
}

The code specific to the output format is hidden in B4Analysis.hh where the selection of the output format takes place.

#ifndef B4Analysis_h
#define B4Analysis_h 1

#include "g4analysis_defs.hh"

using namespace G4Root;
//using namespace G4Xml;
//using namespace G4Csv;

#endif


If a file extension is not specified in the call to G4AnalysisManager::OpenFile(), it is automatically completed according to a selected output format.

The histogram names "0", "1" were kept as they are defined in the analogous example in extended/analysis/AnaEx01 (with use of AIDA analysis). They have no relation to the histogram ID which is used afterwords in histograms fill. The histogram ID is automatically generated when a histogram is created by G4AnalysisManager::CreateH1(), and its value is returned from this function. The default start value 0 can be changed with the G4AnalysisManager->SetFirstHistoId(G4int) method.

It is also possible to access directly the histogram objects by the manager class. The concrete histogram type is hidden behind a selected namespace. In example B4, the histogram functions mean() and rms() are called:

  G4AnalysisManager* analysisManager = G4AnalysisManager::Instance();
  if ( analysisManager->GetH1(1) ) {
    G4cout << "\n ----> print histograms statistic \n" << G4endl;
    G4cout << " EAbs : mean = " << analysisManager->GetH1(1)->mean() 
           << " rms = " << analysisManager->GetH1(1)->rms(), 
           << G4endl;
    // ...           
  }             

9.2.2.  Ntuples

In the following example the code for handling ntuples extracted from basic example B4, from the B4RunAction and B4EventAction classes, is presented.

Example 9.2.  Example with Ntuple

#include "B4Analysis.hh"

void B4RunAction::BeginOfRunAction(const G4Run* run) 
{ 
  // Get/create analysis manager
  G4AnalysisManager* man = G4AnalysisManager::Instance();
  
  // Open an output file
  man->OpenFile("B4");

  // Create ntuple
  man->CreateNtuple("B4", "Edep and TrackL");
  man->CreateNtupleDColumn("Eabs");
  man->CreateNtupleDColumn("Egap");
  man->FinishNtuple();
}

void N4EventAction::EndOfEventAction(const G4Run* aRun)
{
  G4AnalysisManager* man = G4AnalysisManager::Instance();
  man->FillNtupleDColumn(0, fEnergyAbs);
  man->FillNtupleDColumn(1, fEnergyGap);
  man->AddNtupleRow();  
}


In a similar way as for histogram ID, the ntuple column ID is automatically generated when the ntuple column is created by G4AnalysisManager::CreateNtupleTColumn(), and its value is returned from this function. The default start value 0 can be again changed with the G4AnalysisManager::SetFirstNtupleId(G4int) method. The ntuple column ID is specific to each ntuple column type. If the third ntuple column of a different type than double (int or float) is created, its ID will have the value equal 0.

9.2.3.  Coexistence of Several Managers

The specific manager classes are singletons and so it is not possible to create more than one instance of an analysis manager of one type, eg. G4RootAnalysisManager. However two analysis manager objects of different types can coexist. Then instead of G4AnalysisManager typedef from g4analysis_defs.hh the concrete type of each manager has to be given explicitly.

Example 9.3.  Example with two analysis manager instances

#include "G4CsvManager.hh"
#include "G4XmlManager.hh"

G4CsvManager* csvManager = G4CsvManager::Instance();
G4XmlManager* xmlManager = G4XmlManager::Instance();
Or:
#include "g4analysis_defs.hh"

G4Csv::G4AnalysisManager* csvManager = G4Csv::G4AnalysisManager::Instance();
G4Xml::G4AnalysisManager* xmlManager = G4Xml::G4AnalysisManager::Instance();


9.2.4.  Supported Features and Limitations

The first version of the analysis category based on g4tools is provided with certain limitations that can be reduced according to the feedback from Geant4 users and developers.

Below is a summary of currently supported features and limitations in manager classes:

  • Histogram types: H1, H2 of double
  • Ntuple column types: int, float, double
  • Only one ntuple can be created/handled.
  • No limitation for the number of histograms
  • Direct access to the ntuple object is not provided.
  • The directory structure in the output file is fixed to one directory for histograms and one for an ntuple, users can change the names of these directories.
  • G4CsvAnalysisManager can be used only with ntuples.

Supported features and limitations in g4tools:

  • Histogram types: H1, H2, H3 of double
  • Profile types: P1, P2 of double
  • Ntuple column types: int, float, double
  • Csv classes can be used only with ntuples.