11 #include "montecarlo/dataobjs/GGSTHitVolInfo.h"
15 #include "G4SystemOfUnits.hh"
16 #include "G4LogicalVolume.hh"
24 GGSRootFileService::FileInfo::FileInfo() :
25 absBaseName(
""), filePtr(NULL), defaultEventsTree(NULL), nRequests(0), detectorsMap(NULL), detectorsArray(NULL) {
37 GGSRootFileService::GGSRootFileService() :
38 _suffix(
""), _defaultOutBase(
"GGSRootOutput"), _currVolStorageFile(
45 for (FileInfoContainer::iterator iter = _files.begin(); iter != _files.end(); iter++)
46 if (iter->filePtr != NULL) {
47 iter->filePtr->Close();
60 if (baseName.string() ==
"") {
61 absBaseName = _defaultOutBase;
64 absBaseName = baseName;
67 absBaseName = _GetAbsolutePath(absBaseName);
70 for (FileInfoContainer::iterator iter = _files.begin(); iter != _files.end(); iter++)
71 if (absBaseName == iter->absBaseName) {
79 fInfo.absBaseName = absBaseName;
82 TString absFileName = _AppendSuffixAndExt(absBaseName, run);
85 fInfo.filePtr =
new TFile(absFileName,
"RECREATE");
86 _files.push_back(fInfo);
98 geoParams.Write(
"GGSGeoParams");
101 _currVolStorageFile = NULL;
102 _currDetVolName =
"";
104 return fInfo.filePtr;
114 if (baseName.string() ==
"") {
115 absBaseName = _defaultOutBase;
118 absBaseName = baseName;
119 absBaseName = _GetAbsolutePath(absBaseName);
121 for (FileInfoContainer::iterator iter = _files.begin(); iter != _files.end(); iter++)
122 if (iter->absBaseName == absBaseName) {
123 if (iter->nRequests > 1) {
129 if (iter->defaultEventsTree)
130 iter->defaultEventsTree->Write();
131 if (iter->detectorsArray)
132 iter->detectorsArray->Write(
"GGSHitDetInfo", TObject::kSingleKey);
133 iter->filePtr->Close();
134 delete iter->filePtr;
135 delete iter->detectorsMap;
136 iter = _files.erase(iter);
144 for (FileInfoContainer::iterator iter = _files.begin(); iter != _files.end(); iter++) {
145 if (file == iter->filePtr) {
146 if (iter->defaultEventsTree == NULL) {
148 iter->defaultEventsTree =
new TTree(
"GGSEventsTree",
"GGS events tree. Info: ");
150 return iter->defaultEventsTree;
157 path GGSRootFileService::_GetAbsolutePath(
const path &baseName) {
159 path absBaseName(baseName);
161 absBaseName = absolute(baseName);
167 TString GGSRootFileService::_AppendSuffixAndExt(
const path &baseName,
const G4Run *run) {
169 int runID = run->GetRunID();
171 path fileName(baseName);
172 fileName.replace_extension();
173 TString newFileName = fileName.string();
177 newFileName +=
"_Run";
178 newFileName.Form(newFileName +
"%i", runID);
183 newFileName = newFileName +
"_" + _suffix;
186 newFileName +=
".root";
192 void GGSRootFileService::_FillDefaultEventsTrees() {
193 for (FileInfoContainer::iterator iter = _files.begin(); iter != _files.end(); iter++) {
194 if (iter->filePtr && iter->defaultEventsTree) {
196 iter->defaultEventsTree->Fill();
203 const G4ThreeVector &position,
int id) {
204 static const std::string routineName(
"GGSRootFileService::StoreVolume");
206 std::string detVolName = detector.substr(0, detector.find_first_of(
'.'));
209 bool fileChanged =
false;
210 if (filePtr != _currVolStorageFile) {
212 _currVolStorageFile = filePtr;
213 for (_currVolStorageFileInfo = _files.begin(); _currVolStorageFileInfo != _files.end(); _currVolStorageFileInfo++) {
214 if (_currVolStorageFileInfo->filePtr == filePtr)
217 if (_currVolStorageFileInfo == _files.end()) {
218 COUT(WARNING) <<
"The requested file is not managed by GGSRootFileService. Volume informations will not be saved."
225 static HitDetMap::iterator currDetector;
227 if (fileChanged || _currDetVolName != detVolName) {
228 if (_currVolStorageFileInfo->detectorsMap == NULL)
229 _currVolStorageFileInfo->detectorsMap =
new HitDetMap;
230 _currDetVolName = detVolName;
231 int currPersDetectorIndex = 0;
232 for (currDetector = _currVolStorageFileInfo->detectorsMap->begin();
233 currDetector != _currVolStorageFileInfo->detectorsMap->end(); currDetector++, currPersDetectorIndex++) {
234 if (currDetector->first == _currDetVolName)
237 if (currDetector == _currVolStorageFileInfo->detectorsMap->end()) {
239 _currVolStorageFileInfo->detectorsMap->push_back(std::pair<std::string, HitVolMap>(_currDetVolName, HitVolMap()));
240 currDetector = _currVolStorageFileInfo->detectorsMap->end();
242 if (_currVolStorageFileInfo->detectorsArray == NULL) {
243 _currVolStorageFileInfo->detectorsArray =
new TClonesArray(
"GGSTHitDetInfo");
245 currPersDetectorIndex = _currVolStorageFileInfo->detectorsArray->GetEntries();
246 currPersDetector =
new ((*(_currVolStorageFileInfo->detectorsArray))[currPersDetectorIndex])
GGSTHitDetInfo;
247 currPersDetector->
detectorName = _currDetVolName.data();
251 currPersDetector = (
GGSTHitDetInfo*) (_currVolStorageFileInfo->detectorsArray->At(currPersDetectorIndex));
258 static std::stringstream ss;
261 std::string volAndId = volume->GetName() + ss.str();
263 std::pair<VolumeKey, G4int> insertValue(VolumeKey(volAndId, volume, position),
264 currPersDetector->
volumes.GetEntries());
265 std::pair<HitVolMap::iterator, bool> insertResult;
266 insertResult = currDetector->second.insert(insertValue);
267 if (insertResult.second) {
270 volInfo->
volumeName = volume->GetName().data();
271 for (
int i = 0; i < 3; i++) {
272 volInfo->
volumePos[i] = position[i] / cm;
276 return currPersDetector->
volumes.GetEntries() - 1;
280 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.
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.
Abstract class needed to load GGS geometry.
static GGSGeoPluginManager & GetInstance()
Get the singleton instance.
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.
#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::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, bool > & GetBoolParameters()
Getter method for boolean geometry parameters.
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.
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.
void SetIntGeoParams(const std::map< std::string, int > &intGeoParams)
Sets the integer geometry parameters.