GGS(GenericGEANT4Simulation)Software  2.99.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Macros
GGSRootFileService.h
Go to the documentation of this file.
1 /*
2  * GGSRootFileService.h
3  *
4  * Created on: 03 Jun 2011
5  * Author: Nicola Mori
6  */
7 
10 #ifndef GGSROOTFILESERVICE_H_
11 #define GGSROOTFILESERVICE_H_
12 
13 #include <filesystem>
14 #include <functional>
15 #include <list>
16 #include <unordered_map>
17 #include <vector>
18 
19 #include "ROOT/TBufferMerger.hxx"
20 #include "TFile.h"
21 #include "TObjString.h"
22 #include "TString.h"
23 #include "TTree.h"
24 
25 #include "G4GenericMessenger.hh"
26 #include "G4Run.hh"
27 #include "G4VPhysicalVolume.hh"
28 
30 class GGSTHitDetInfo;
31 
47 
48 public:
54 
69  TFile *GetFileForThisRun(const std::filesystem::path &baseName, const G4Run *run);
70 
87  void CloseFileForThisRun(const std::filesystem::path &baseName, const G4Run *run);
88 
106  TTree *GetDefaultTree(TFile *file);
107 
114  void SetSuffix(const std::string &suffix) { _suffix = suffix; }
115 
122  void SetDefaultFileBase(const std::filesystem::path &newFileBase) { _defaultOutBase = newFileBase; }
123 
140  int StoreVolume(TFile *tFilePtr, const std::string &detector, const G4VPhysicalVolume *volume,
141  const G4ThreeVector &position, int id);
142 
147  void SetSimInfo(const GGSTSimInfo &simInfo) { _simInfo = simInfo; }
148 
154  enum class OutputMode { SINGLEFILE, MULTIFILE };
155 
164  OutputMode GetOutputMode() { return (_singleFileForMT ? OutputMode::SINGLEFILE : OutputMode::MULTIFILE); }
165 
166 private:
168 
169  std::filesystem::path _GetAbsolutePath(const std::filesystem::path &baseName);
170  std::filesystem::path _AppendSuffixAndExt(const std::filesystem::path &baseName, const G4Run *run);
171 
172  void _SetMultiFileMode();
173 
174  friend class GGSMultiUserAction;
175  void _FillDefaultEventsTrees(); // Called by GGSMultiUserAction at end of event
176 
177  // Define the key class for indexing the transient volume index...
178  struct VolumeKey {
179  std::string name;
180  const G4VPhysicalVolume *physVol;
181  G4ThreeVector position;
182 
183  VolumeKey(const std::string &volName, const G4VPhysicalVolume *physVolPtr, const G4ThreeVector &pos)
184  : name(volName), physVol(physVolPtr), position(pos) {}
185  bool operator==(const VolumeKey &rhs) const {
186  return (name == rhs.name && physVol == rhs.physVol && position == rhs.position);
187  }
188 
189  friend struct std::hash<VolumeKey>;
190  };
191  // ... its hash struct (defined in the source file for GGSRootFileService) ...
192  friend struct std::hash<VolumeKey>;
193 
194  // ... and the transient index itself.
195  typedef std::unordered_map<VolumeKey, G4int, std::hash<VolumeKey>>
196  HitVolMap; // <volume name, position in the GGSTHitDetInfo.volumes array>
197  typedef std::list<std::pair<std::string, HitVolMap>> HitDetMap; // <detector name, index of volumes>
198 
199  // Define the class to store the info about the output file...
200  struct FileInfo {
201  std::filesystem::path absPath;
202  std::unique_ptr<ROOT::TBufferMerger> bufferMerger; // For MT single-file mode
203  HitDetMap detectorsMap; // Transient index structure for fast searching during event processing.
204  TClonesArray *detectorsArray; // Persistent detectors objects to be saved in this output file
205  // Each file can have many associated TFiles in MT single-file mode, since bufferMerger will produce one TFile per
206  // thread. So define a housekeeping struct for the TFiles (will be used also for the single TFile in ST mode or in
207  // MT multi-file mode).
208  struct TFileInfo {
209  std::shared_ptr<TFile> tFile; // Must be destroyed after the buffer manager
210  TTree *defaultEventsTree;
211  int threadNo;
212  int nRequests;
213  };
214  std::vector<TFileInfo> tFilesInfo; // Will have only 1 element in ST mode or in MT multi-file mode
215  };
216  // ... the file info container class...
217  typedef std::list<FileInfo> FileInfoContainer;
218  // ... and the container itself.
219  FileInfoContainer _filesInfo;
220 
221  std::string _suffix;
222  std::filesystem::path _defaultOutBase;
223  bool _singleFileForMT;
224 
225  GGSTSimInfo _simInfo;
226 
227  G4GenericMessenger _messenger;
228 
229  // Variables used for performance in StoreVolume
230  G4ThreadLocalStatic TFile *_currVolStorageTFile;
231  G4ThreadLocalStatic std::string _currDetVolName;
232  G4ThreadLocalStatic FileInfoContainer::iterator _currVolStorageFileInfo;
233  G4ThreadLocalStatic HitDetMap::iterator _currDetector;
234  G4ThreadLocalStatic GGSTHitDetInfo *_currPersDetector;
235 };
236 
237 #endif /* GGSROOTFILESERVICE_H_ */
void SetDefaultFileBase(const std::filesystem::path &newFileBase)
Sets the default file name.
OutputMode
Aliases for the different output modes.
OutputMode GetOutputMode()
Gets the ouptut mode.
Class to store detector informations.
int StoreVolume(TFile *tFilePtr, const std::string &detector, const G4VPhysicalVolume *volume, const G4ThreeVector &position, int id)
Set persistence for the specified volume.
TFile * GetFileForThisRun(const std::filesystem::path &baseName, const G4Run *run)
Opens a file for a given run and returns a pointer to it.
void CloseFileForThisRun(const std::filesystem::path &baseName, const G4Run *run)
Closes the ROOT output file.
A multiplexer container for user actions.
Singleton for a centralized ROOT files management.
TTree * GetDefaultTree(TFile *file)
Gets the default tree for this file.
static GGSRootFileService & GetInstance()
Get reference to GGSRootFileService unique instance.
void SetSuffix(const std::string &suffix)
Sets the suffix for file names.
void SetSimInfo(const GGSTSimInfo &simInfo)
Set the simulation info object to be saved on output files.
A class to store simulation informations.
Definition: GGSTSimInfo.h:21