GGS(GenericGEANT4Simulation)Software  2.6.0
 All Data Structures Namespaces Files Functions Variables Typedefs 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 <vector>
14 #include <list>
15 
16 #include "TFile.h"
17 #include "TString.h"
18 #include "TObjString.h"
19 #include "TTree.h"
20 
21 #include "G4Run.hh"
22 #include "G4VPhysicalVolume.hh"
23 
24 #include "boost/unordered_map.hpp"
25 #include <boost/functional/hash.hpp>
26 #include "boost/filesystem.hpp"
27 using namespace boost::filesystem;
28 
31 
47 
48 public:
53  static GGSRootFileService& GetInstance();
54 
57 
71  TFile* GetFileForThisRun(const path &baseName, const G4Run *run);
72 
88  void CloseFileForThisRun(const path &baseName);
89 
106  TTree *GetDefaultTree(TFile *file);
107 
114  void SetSuffix(const std::string &suffix) {
115  _suffix = suffix;
116  }
117 
124  void SetDefaultFileBase(const path &newFileBase) {
125  _defaultOutBase = newFileBase;
126  }
127 
144  int StoreVolume(TFile *filePtr, const std::string &detector, const G4VPhysicalVolume *volume,
145  const G4ThreeVector &position, int id);
146 
151  void SetSimInfo(const GGSTSimInfo &simInfo) {
152  _simInfo = simInfo;
153  }
154 
155 private:
156 
158 
159  path _GetAbsolutePath(const path &baseName);
160  TString _AppendSuffixAndExt(const path &baseName, const G4Run *run);
161 
162  friend class GGSUserActionsManager;
163  void _FillDefaultEventsTrees(); // Called by GGSUserActionsManager at end of event
164 
165  // Define the key class for indexing the transient volume index...
166  struct VolumeKey {
167  std::string name;
168  const G4VPhysicalVolume *physVol;
169  G4ThreeVector position;
170 
171  VolumeKey(const std::string &volName, const G4VPhysicalVolume *physVolPtr, const G4ThreeVector &pos) :
172  name(volName), physVol(physVolPtr), position(pos) {
173 
174  }
175  bool operator==(const VolumeKey &rhs) const {
176  return (name == rhs.name && physVol == rhs.physVol && position == rhs.position);
177  }
178  };
179  // ... its hash function...
180  friend std::size_t hash_value(const VolumeKey &key) {
181  std::size_t seed = 0;
182  boost::hash_combine(seed, key.name);
183  boost::hash_combine(seed, key.physVol);
184  boost::hash_combine(seed, key.position[0]);
185  boost::hash_combine(seed, key.position[1]);
186  boost::hash_combine(seed, key.position[2]);
187  return seed;
188  }
189  // ... and the transient index itself.
190  typedef boost::unordered_map<VolumeKey, G4int, boost::hash<VolumeKey> > HitVolMap; // <volume name, position in the GGSTHitDetInfo.volumes array>
191  typedef std::list<std::pair<std::string, HitVolMap> > HitDetMap; // <detector name, index of volumes>
192 
193  // Define the class to store the info about the output file...
194  struct FileInfo {
195  path absBaseName;
196  TFile *filePtr;
197  TTree *defaultEventsTree;
198  int nRequests;
199  HitDetMap *detectorsMap; // Transient index structure for fast searching during event processing.
200  TClonesArray *detectorsArray; // Persistent detectors objects to be saved in this file
201 
202  FileInfo();
203  };
204  // ... the file info container class...
205  typedef std::list<FileInfo> FileInfoContainer;
206  // ... and the container itself.
207  FileInfoContainer _files;
208 
209  TString _suffix;
210  path _defaultOutBase;
211 
212  GGSTSimInfo _simInfo;
213 
214  // Variables used for performance in StoreVolume
215  TFile *_currVolStorageFile;
216  std::string _currDetVolName;
217  FileInfoContainer::iterator _currVolStorageFileInfo;
218 };
219 
220 #endif /* GGSROOTFILESERVICE_H_ */
void SetDefaultFileBase(const path &newFileBase)
Sets the default file name.
Singleton for a centralized ROOT files management.
The GGS ser actions manager.
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