GGS(GenericGEANT4Simulation)Software
2.7.0
|
Singleton for a centralized ROOT files management. More...
#include <GGSRootFileService.h>
Public Member Functions | |
~GGSRootFileService () | |
Destructor. | |
TFile * | GetFileForThisRun (const path &baseName, const G4Run *run) |
Opens a file for a given run and returns a pointer to it. More... | |
void | CloseFileForThisRun (const path &baseName) |
Closes the ROOT output file. More... | |
TTree * | GetDefaultTree (TFile *file) |
Gets the default tree for this file. More... | |
void | SetSuffix (const std::string &suffix) |
Sets the suffix for file names. More... | |
void | SetDefaultFileBase (const path &newFileBase) |
Sets the default file name. More... | |
int | StoreVolume (TFile *filePtr, const std::string &detector, const G4VPhysicalVolume *volume, const G4ThreeVector &position, int id) |
Set persistence for the specified volume. More... | |
void | SetSimInfo (const GGSTSimInfo &simInfo) |
Set the simulation info object to be saved on output files. More... | |
Static Public Member Functions | |
static GGSRootFileService & | GetInstance () |
Get reference to GGSRootFileService unique instance. More... | |
Friends | |
class | GGSUserActionsManager |
std::size_t | hash_value (const VolumeKey &key) |
Singleton for a centralized ROOT files management.
The aim of this class is to provide a centralized management for ROOT output files. For each run, the singleton will provide pointers to output file to the action which may require them with the GetFileForThisRun method. The purpose of this is to allow multiple actions to write on the same ROOT file, avoiding the proliferation of output files when many actions are in use. Typically, the singleton will create a file the first time that file is requested; if subsequently other actions require the same file, the singleton will not open it again, but it will simply return a pointer to the already opened file. Consistently, file closure requests issued by calling CloseFileForThisRun will be ignored until no more actions require that file. Users wishing to use this service in their actions are demanded to call GetFileForThisRun in BeginOfRunAction to get the file and to close it with CloseFileForThisRun in EndOfRunAction. The service also provides a standard TTree called GGSEventsTree to let actions save their event data in different branches of the same tree rather than in different trees. This helps I/O performance. See GetDefaultTree.
Definition at line 46 of file GGSRootFileService.h.
void GGSRootFileService::CloseFileForThisRun | ( | const path & | baseName | ) |
Closes the ROOT output file.
The closure request is handled this way: if a file has been requested N times, it will be closed when this method is called for the N-th time. This will avoid to close the file when other actions may still need it.
WARNING: when a ROOT file is closed, all the TTree objects associated to it will be automatically deleted. User must NOT delete its trees before calling this method. Eg.:
outRootFile->cd(); outTree->Write(); GGSRootFileService::GetInstance().CloseFileForThisRun(outBase); //delete outTree; // Uncommenting this will generate a segfault
baseName | The file base name. |
Definition at line 120 of file GGSRootFileService.cpp.
TTree * GGSRootFileService::GetDefaultTree | ( | TFile * | file | ) |
Gets the default tree for this file.
This method returns the default events tree for the specified file. This is intended to allow user actions to write their output as branches of the same tree instead of using a tree for each action. In this way the best performance of the I/O system of ROOT can be exploited (see https://twiki.cern.ch/twiki/bin/view/LHCb/PersistencyMigration#POOL). The Fill() and Write() operations for the default tree are managed by GGSRootFileService. The only operation that can be done with the tree is the creation of a branch. Explicitly calling Fill() or Write() for the default tree in user actions will result in a wrong number of events and in multiple keys for the tree in the output file. Fill() will be automatically called at the end of each event after calling EndOfEventAction() for every user action.
Different default trees can be requested for different files.
file | The file on which the default tree will be saved. This file pointer must have been previously retrieved by calling GetFileForThisRun; DON'T use a file created in any other way. |
Definition at line 153 of file GGSRootFileService.cpp.
TFile * GGSRootFileService::GetFileForThisRun | ( | const path & | baseName, |
const G4Run * | run | ||
) |
Opens a file for a given run and returns a pointer to it.
This method will open a file for a given run and return a pointer to it; if the file is already opened, it will simply return the pointer without attempting to open it again. The created file will be named baseName.root for run 0 (eventually baseName_suffix.root if a suffix is specified calling SetSuffix); for subsequent runs, it will be named baseName_RunID.root (or baseName_RunID_suffix.root).
baseName | The file base name (.root extension, run ID and suffix will be (eventually) automatically appended). If no name is provided, the default one ("GGSRootOutput") will be used (it can also be set with SetDefaultFileBase). |
run | The current run. |
Definition at line 52 of file GGSRootFileService.cpp.
|
static |
Get reference to GGSRootFileService unique instance.
Definition at line 30 of file GGSRootFileService.cpp.
|
inline |
Sets the default file name.
The default file name is used when a file is requested by calling GetFileForThisRun but no file name is provided.
newFileBase | The new default file base name (optionally with path). |
Definition at line 123 of file GGSRootFileService.h.
|
inline |
Set the simulation info object to be saved on output files.
simInfo | The object containing informations about the simulation. |
Definition at line 148 of file GGSRootFileService.h.
|
inline |
Sets the suffix for file names.
The suffix will be appended after just before the .root extension.
suffix | The suffix. |
Definition at line 115 of file GGSRootFileService.h.
int GGSRootFileService::StoreVolume | ( | TFile * | filePtr, |
const std::string & | detector, | ||
const G4VPhysicalVolume * | volume, | ||
const G4ThreeVector & | position, | ||
int | id | ||
) |
Set persistence for the specified volume.
The specified volume (volume name, position and id) will be saved as a GGSTHitVolInfo object inside the GGSTHitDetInfo object corresponding to the specified detector. The GGSTHitDetInfo is saved as an element of a TClonesArray called GGSHitDetArray inside the specified file.
The (volume name, id) pair must be unique for each volume to be stored.
filePtr | The file where to save the informations about detectors and volumes. |
detector | The name of the detector. |
volume | The pointer to the physical volume. |
position | The position of the volume (in internal G4 units; will be saved in [cm]). |
id | The ID of the volume (see GGSTHitVolInfo). |
Definition at line 213 of file GGSRootFileService.cpp.