13 #include "montecarlo/dataobjs/GGSTHitVolInfo.h"
14 #include "montecarlo/dataobjs/GGSTParameters.h"
17 #include "G4LogicalVolume.hh"
18 #include "G4SystemOfUnits.hh"
26 GGSRootFileService::FileInfo::FileInfo()
27 : absBaseName(
""), filePtr(NULL), defaultEventsTree(NULL), nRequests(0), detectorsMap(NULL), detectorsArray(NULL) {}
37 GGSRootFileService::GGSRootFileService() : _suffix(
""), _defaultOutBase(
"GGSRootOutput"), _currVolStorageFile(NULL) {}
42 for (FileInfoContainer::iterator iter = _files.begin(); iter != _files.end(); iter++)
43 if (iter->filePtr != NULL) {
44 iter->filePtr->Close();
57 if (baseName.string() ==
"") {
58 absBaseName = _defaultOutBase;
60 absBaseName = baseName;
63 absBaseName = _GetAbsolutePath(absBaseName);
66 for (FileInfoContainer::iterator iter = _files.begin(); iter != _files.end(); iter++)
67 if (absBaseName == iter->absBaseName) {
75 fInfo.absBaseName = absBaseName;
78 TString absFileName = _AppendSuffixAndExt(absBaseName, run);
81 fInfo.filePtr =
new TFile(absFileName,
"RECREATE");
82 _files.push_back(fInfo);
98 geoParams.Write(
"GGSGeoParams");
107 generatorParams.
GetParam<std::string>(
"generator");
108 generatorParams.Write(
"GGSGenParams");
113 _currVolStorageFile = NULL;
114 _currDetVolName =
"";
116 return fInfo.filePtr;
125 if (baseName.string() ==
"") {
126 absBaseName = _defaultOutBase;
128 absBaseName = baseName;
129 absBaseName = _GetAbsolutePath(absBaseName);
131 for (FileInfoContainer::iterator iter = _files.begin(); iter != _files.end(); iter++)
132 if (iter->absBaseName == absBaseName) {
133 if (iter->nRequests > 1) {
138 if (iter->defaultEventsTree)
139 iter->defaultEventsTree->Write();
140 if (iter->detectorsArray)
141 iter->detectorsArray->Write(
143 TObject::kSingleKey);
144 iter->filePtr->Close();
145 delete iter->filePtr;
146 delete iter->detectorsMap;
147 iter = _files.erase(iter);
155 for (FileInfoContainer::iterator iter = _files.begin(); iter != _files.end(); iter++) {
156 if (file == iter->filePtr) {
157 if (iter->defaultEventsTree == NULL) {
159 iter->defaultEventsTree =
new TTree(
"GGSEventsTree",
"GGS events tree. Info: ");
161 return iter->defaultEventsTree;
168 path GGSRootFileService::_GetAbsolutePath(
const path &baseName) {
170 path absBaseName(baseName);
172 absBaseName = absolute(baseName);
178 TString GGSRootFileService::_AppendSuffixAndExt(
const path &baseName,
const G4Run *run) {
180 int runID = run->GetRunID();
182 path fileName(baseName);
183 fileName.replace_extension();
184 TString newFileName = fileName.string();
188 newFileName +=
"_Run";
189 newFileName.Form(newFileName +
"%i", runID);
194 newFileName = newFileName +
"_" + _suffix;
197 newFileName +=
".root";
203 void GGSRootFileService::_FillDefaultEventsTrees() {
204 for (FileInfoContainer::iterator iter = _files.begin(); iter != _files.end(); iter++) {
205 if (iter->filePtr && iter->defaultEventsTree) {
207 iter->defaultEventsTree->Fill();
214 const G4ThreeVector &position,
int id) {
215 static const std::string routineName(
"GGSRootFileService::StoreVolume");
217 std::string detVolName = detector.substr(0, detector.find_first_of(
'.'));
220 bool fileChanged =
false;
221 if (filePtr != _currVolStorageFile) {
223 _currVolStorageFile = filePtr;
224 for (_currVolStorageFileInfo = _files.begin(); _currVolStorageFileInfo != _files.end(); _currVolStorageFileInfo++) {
225 if (_currVolStorageFileInfo->filePtr == filePtr)
228 if (_currVolStorageFileInfo == _files.end()) {
229 COUT(WARNING) <<
"The requested file is not managed by GGSRootFileService. Volume informations will not be saved."
236 static HitDetMap::iterator currDetector;
238 if (fileChanged || _currDetVolName != detVolName) {
239 if (_currVolStorageFileInfo->detectorsMap == NULL)
240 _currVolStorageFileInfo->detectorsMap =
new HitDetMap;
241 _currDetVolName = detVolName;
242 int currPersDetectorIndex = 0;
243 for (currDetector = _currVolStorageFileInfo->detectorsMap->begin();
244 currDetector != _currVolStorageFileInfo->detectorsMap->end(); currDetector++, currPersDetectorIndex++) {
245 if (currDetector->first == _currDetVolName)
248 if (currDetector == _currVolStorageFileInfo->detectorsMap->end()) {
250 _currVolStorageFileInfo->detectorsMap->push_back(std::pair<std::string, HitVolMap>(_currDetVolName, HitVolMap()));
251 currDetector = _currVolStorageFileInfo->detectorsMap->end();
253 if (_currVolStorageFileInfo->detectorsArray == NULL) {
254 _currVolStorageFileInfo->detectorsArray =
new TClonesArray(
"GGSTHitDetInfo");
256 currPersDetectorIndex = _currVolStorageFileInfo->detectorsArray->GetEntries();
257 currPersDetector =
new ((*(_currVolStorageFileInfo->detectorsArray))[currPersDetectorIndex])
GGSTHitDetInfo;
258 currPersDetector->
detectorName = _currDetVolName.data();
261 currPersDetector = (
GGSTHitDetInfo *)(_currVolStorageFileInfo->detectorsArray->At(currPersDetectorIndex));
268 static std::stringstream ss;
271 std::string volAndId = volume->GetName() + ss.str();
273 std::pair<VolumeKey, G4int> insertValue(VolumeKey(volAndId, volume, position),
274 currPersDetector->
volumes.GetEntries());
275 std::pair<HitVolMap::iterator, bool> insertResult;
276 insertResult = currDetector->second.insert(insertValue);
277 if (insertResult.second) {
280 volInfo->
volumeName = volume->GetName().data();
281 for (
int i = 0; i < 3; i++) {
282 volInfo->
volumePos[i] = position[i] / cm;
286 return currPersDetector->
volumes.GetEntries() - 1;
289 return insertResult.first->second;
TFile * GetFileForThisRun(const path &baseName, const G4Run *run)
Opens a file for a given run and returns a pointer to it.
Float_t volumePos[3]
Position of the touchable in world volume coordinates [cm].
Int_t id
ID of the volume.
const std::map< std::string, std::vector< bool > > & GetVectBoolParameters()
Getter method for vector-of-booleans geometry parameters.
void SetVectBoolGeoParams(const std::map< std::string, std::vector< bool >> &vectBoolGeoParams)
Sets the vector-of-booleans geometry parameters.
Class to store detector informations.
const std::map< std::string, int > & GetIntParameters()
Getter method for integer geometry parameters.
const std::map< std::string, double > & GetRealParameters()
Getter method for real geometry parameters.
Base class for GGS generator actions.
Abstract class needed to load GGS geometry.
static GGSGeoPluginManager & GetInstance()
Get the singleton instance.
T GetParam(const std::string &name) const
Gets a parameter.
void CloseFileForThisRun(const path &baseName)
Closes the ROOT output file.
void SetStringGeoParams(const std::map< std::string, std::string > &stringGeoParams)
Sets the string geometry parameters.
const std::map< std::string, std::vector< std::string > > & GetVectStringParameters()
Getter method for vector-of-strings geometry parameters.
#define COUT(level)
Smart log macro. It writes on stdout only if the specified verbosity level is lesser than the maximum...
int StoreVolume(TFile *filePtr, const std::string &detector, const G4VPhysicalVolume *volume, const G4ThreeVector &position, int id)
Set persistence for the specified volume.
void SetRealGeoParams(const std::map< std::string, double > &realGeoParams)
Sets the real geometry parameters.
const std::map< std::string, std::vector< int > > & GetVectIntParameters()
Getter method for vector-of-integers geometry parameters.
const std::map< std::string, std::string > & GetStringParameters()
Getter method for string geometry parameters.
TClonesArray volumes
Array of GGSTHitVolInfo objects.
Singleton for a centralized ROOT files management.
const std::map< std::string, std::vector< double > > & GetVectRealParameters()
Getter method for vector-of-reals geometry parameters.
void SetVectRealGeoParams(const std::map< std::string, std::vector< double >> &vectRealGeoParams)
Sets the vector-of-reals geometry parameters.
void SetVectStringGeoParams(const std::map< std::string, std::vector< std::string >> &vectStringGeoParams)
Sets the vector-of-strings geometry parameters.
const std::map< std::string, bool > & GetBoolParameters()
Getter method for boolean geometry parameters.
static GGSRunManager * GetRunManager()
Static getter function the run manager.
Class for storing the geometry parameters on Root output file.
TTree * GetDefaultTree(TFile *file)
Gets the default tree for this file.
static GGSRootFileService & GetInstance()
Get reference to GGSRootFileService unique instance.
void SetVectIntGeoParams(const std::map< std::string, std::vector< int >> &vectIntGeoParams)
Sets the vector-of-integers geometry parameters.
TString volumeName
Name of the physical volume.
GGSVGeometryConstruction * GetGeoConstruction()
Returns the geometry construction object.
void SetBoolGeoParams(const std::map< std::string, bool > &boolGeoParams)
Sets the boolean geometry parameters.
~GGSRootFileService()
Destructor.
GGSTHitVolInfo.h GGSTHitVolInfo class declaration.
TString detectorName
Name of detector associated to integrated hits.
Class for writing parameters into the output Root file.
void SetIntGeoParams(const std::map< std::string, int > &intGeoParams)
Sets the integer geometry parameters.