GGS(GenericGEANT4Simulation)Software  2.7.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 <list>
14 #include <vector>
15 
16 #include "TFile.h"
17 #include "TObjString.h"
18 #include "TString.h"
19 #include "TTree.h"
20 
21 #include "G4Run.hh"
22 #include "G4VPhysicalVolume.hh"
23 
24 #include "boost/filesystem.hpp"
25 #include "boost/unordered_map.hpp"
26 #include <boost/functional/hash.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 
107  TTree *GetDefaultTree(TFile *file);
108 
115  void SetSuffix(const std::string &suffix) { _suffix = suffix; }
116 
123  void SetDefaultFileBase(const path &newFileBase) { _defaultOutBase = newFileBase; }
124 
141  int StoreVolume(TFile *filePtr, const std::string &detector, const G4VPhysicalVolume *volume,
142  const G4ThreeVector &position, int id);
143 
148  void SetSimInfo(const GGSTSimInfo &simInfo) { _simInfo = simInfo; }
149 
150 private:
152 
153  path _GetAbsolutePath(const path &baseName);
154  TString _AppendSuffixAndExt(const path &baseName, const G4Run *run);
155 
156  friend class GGSUserActionsManager;
157  void _FillDefaultEventsTrees(); // Called by GGSUserActionsManager at end of event
158 
159  // Define the key class for indexing the transient volume index...
160  struct VolumeKey {
161  std::string name;
162  const G4VPhysicalVolume *physVol;
163  G4ThreeVector position;
164 
165  VolumeKey(const std::string &volName, const G4VPhysicalVolume *physVolPtr, const G4ThreeVector &pos)
166  : name(volName), physVol(physVolPtr), position(pos) {}
167  bool operator==(const VolumeKey &rhs) const {
168  return (name == rhs.name && physVol == rhs.physVol && position == rhs.position);
169  }
170  };
171  // ... its hash function...
172  friend std::size_t hash_value(const VolumeKey &key) {
173  std::size_t seed = 0;
174  boost::hash_combine(seed, key.name);
175  boost::hash_combine(seed, key.physVol);
176  boost::hash_combine(seed, key.position[0]);
177  boost::hash_combine(seed, key.position[1]);
178  boost::hash_combine(seed, key.position[2]);
179  return seed;
180  }
181  // ... and the transient index itself.
182  typedef boost::unordered_map<VolumeKey, G4int, boost::hash<VolumeKey>>
183  HitVolMap; // <volume name, position in the GGSTHitDetInfo.volumes array>
184  typedef std::list<std::pair<std::string, HitVolMap>> HitDetMap; // <detector name, index of volumes>
185 
186  // Define the class to store the info about the output file...
187  struct FileInfo {
188  path absBaseName;
189  TFile *filePtr;
190  TTree *defaultEventsTree;
191  int nRequests;
192  HitDetMap *detectorsMap; // Transient index structure for fast searching during event processing.
193  TClonesArray *detectorsArray; // Persistent detectors objects to be saved in this file
194 
195  FileInfo();
196  };
197  // ... the file info container class...
198  typedef std::list<FileInfo> FileInfoContainer;
199  // ... and the container itself.
200  FileInfoContainer _files;
201 
202  TString _suffix;
203  path _defaultOutBase;
204 
205  GGSTSimInfo _simInfo;
206 
207  // Variables used for performance in StoreVolume
208  TFile *_currVolStorageFile;
209  std::string _currDetVolName;
210  FileInfoContainer::iterator _currVolStorageFileInfo;
211 };
212 
213 #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