Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4Hdf5FileManager Class Reference

#include <G4Hdf5FileManager.hh>

+ Inheritance diagram for G4Hdf5FileManager:

Public Member Functions

 G4Hdf5FileManager (const G4AnalysisManagerState &state)
 
 ~G4Hdf5FileManager ()
 
virtual G4bool OpenFile (const G4String &fileName) final
 
virtual G4String GetFileType () const final
 
G4bool CreateNtupleFile (Hdf5NtupleDescription *ntupleDescription)
 
G4bool CloseNtupleFile (Hdf5NtupleDescription *ntupleDescription)
 
void SetBasketSize (unsigned int basketSize)
 
hid_t GetHistoDirectory () const
 
hid_t GetNtupleDirectory () const
 
unsigned int GetBasketSize () const
 
G4String GetNtupleFileName (const G4String &ntupleName) const
 
G4String GetNtupleFileName (G4int ntupleFileNumber) const
 
- Public Member Functions inherited from G4VTFileManager< G4Hdf5File >
 G4VTFileManager (const G4AnalysisManagerState &state)
 
 ~G4VTFileManager ()
 
virtual G4bool CreateFile (const G4String &fileName) final
 
virtual G4bool WriteFile (const G4String &fileName) final
 
virtual G4bool WriteFile (const G4String &fileName)=0
 
virtual G4bool CloseFile (const G4String &fileName) final
 
virtual G4bool CloseFile (const G4String &fileName)=0
 
virtual G4bool SetIsEmpty (const G4String &fileName, G4bool isEmpty) final
 
virtual G4bool WriteFiles () final
 
virtual G4bool CloseFiles () final
 
virtual G4bool DeleteEmptyFiles () final
 
std::shared_ptr< G4Hdf5FileGetFile () const
 
- Public Member Functions inherited from G4VFileManager
 G4VFileManager (const G4AnalysisManagerState &state)
 
virtual ~G4VFileManager ()
 
virtual G4bool OpenFile (const G4String &fileName)=0
 
virtual G4bool CreateFile (const G4String &fileName)=0
 
virtual G4bool WriteFile (const G4String &fileName)=0
 
virtual G4bool CloseFile (const G4String &fileName)=0
 
virtual G4bool SetIsEmpty (const G4String &fileName, G4bool isEmpty)=0
 
virtual G4bool WriteFiles ()=0
 
virtual G4bool CloseFiles ()=0
 
virtual G4bool DeleteEmptyFiles ()=0
 
virtual G4bool SetFileName (const G4String &fileName) final
 
G4bool SetHistoDirectoryName (const G4String &dirName)
 
G4bool SetNtupleDirectoryName (const G4String &dirName)
 
void LockDirectoryNames ()
 
G4bool IsOpenFile () const
 
G4String GetHistoDirectoryName () const
 
G4String GetNtupleDirectoryName () const
 
template<typename HT >
std::shared_ptr< G4VTHnFileManager< HT > > GetHnFileManager () const
 
- Public Member Functions inherited from G4BaseFileManager
 G4BaseFileManager (const G4AnalysisManagerState &state)
 
virtual ~G4BaseFileManager ()
 
virtual G4bool SetFileName (const G4String &fileName)
 
virtual G4String GetFileType () const
 
void AddFileName (const G4String &fileName)
 
G4String GetFileName () const
 
G4String GetFullFileName (const G4String &baseFileName="", G4bool isPerThread=true) const
 
const std::vector< G4String > & GetFileNames () const
 
G4String GetHnFileName (const G4String &hnType, const G4String &hnName) const
 
G4String GetNtupleFileName (const G4String &ntupleName) const
 
G4String GetNtupleFileName (G4int ntupleFileNumber) const
 
G4String GetPlotFileName () const
 
- Public Member Functions inherited from G4TFileManager< FT >
 G4TFileManager (const G4AnalysisManagerState &state)
 
virtual ~G4TFileManager ()
 
std::shared_ptr< FT > CreateTFile (const G4String &fileName)
 
G4bool WriteTFile (const G4String &fileName)
 
G4bool CloseTFile (const G4String &fileName)
 
G4bool SetIsEmpty (const G4String &fileName, G4bool isEmpty)
 
std::shared_ptr< FT > GetTFile (const G4String &fileName, G4bool warn=true) const
 
G4bool OpenFiles ()
 
G4bool WriteFiles ()
 
G4bool CloseFiles ()
 
G4bool DeleteEmptyFiles ()
 

Protected Member Functions

virtual std::shared_ptr< G4Hdf5FileCreateFileImpl (const G4String &fileName) final
 
virtual G4bool WriteFileImpl (std::shared_ptr< G4Hdf5File > file) final
 
virtual G4bool CloseFileImpl (std::shared_ptr< G4Hdf5File > file) final
 
- Protected Member Functions inherited from G4TFileManager< FT >
virtual std::shared_ptr< FT > CreateFileImpl (const G4String &fileName)=0
 
virtual G4bool WriteFileImpl (std::shared_ptr< FT > file)=0
 
virtual G4bool CloseFileImpl (std::shared_ptr< FT > file)=0
 

Additional Inherited Members

- Protected Attributes inherited from G4VTFileManager< G4Hdf5File >
std::shared_ptr< G4Hdf5FilefFile
 
- Protected Attributes inherited from G4VFileManager
G4String fHistoDirectoryName
 
G4String fNtupleDirectoryName
 
G4bool fIsOpenFile
 
G4bool fLockDirectoryNames
 
std::shared_ptr< G4VTHnFileManager< tools::histo::h1d > > fH1FileManager
 
std::shared_ptr< G4VTHnFileManager< tools::histo::h2d > > fH2FileManager
 
std::shared_ptr< G4VTHnFileManager< tools::histo::h3d > > fH3FileManager
 
std::shared_ptr< G4VTHnFileManager< tools::histo::p1d > > fP1FileManager
 
std::shared_ptr< G4VTHnFileManager< tools::histo::p2d > > fP2FileManager
 
- Protected Attributes inherited from G4BaseFileManager
const G4AnalysisManagerStatefState
 
G4String fFileName
 
std::vector< G4StringfFileNames
 

Detailed Description

Definition at line 45 of file G4Hdf5FileManager.hh.

Constructor & Destructor Documentation

◆ G4Hdf5FileManager()

G4Hdf5FileManager::G4Hdf5FileManager ( const G4AnalysisManagerState state)
explicit

Definition at line 51 of file G4Hdf5FileManager.cc.

53 fBasketSize(0) // TO DO: check default value !! (433 in test)
54{
55 // Create helpers defined in the base class
56 fH1FileManager = std::make_shared<G4Hdf5HnFileManager<histo::h1d>>(this);
57 fH2FileManager = std::make_shared<G4Hdf5HnFileManager<histo::h2d>>(this);
58 fH3FileManager = std::make_shared<G4Hdf5HnFileManager<histo::h3d>>(this);
59 fP1FileManager = std::make_shared<G4Hdf5HnFileManager<histo::p1d>>(this);
60 fP2FileManager = std::make_shared<G4Hdf5HnFileManager<histo::p2d>>(this);
61}
std::shared_ptr< G4VTHnFileManager< tools::histo::h3d > > fH3FileManager
std::shared_ptr< G4VTHnFileManager< tools::histo::h1d > > fH1FileManager
std::shared_ptr< G4VTHnFileManager< tools::histo::p1d > > fP1FileManager
std::shared_ptr< G4VTHnFileManager< tools::histo::p2d > > fP2FileManager
std::shared_ptr< G4VTHnFileManager< tools::histo::h2d > > fH2FileManager

◆ ~G4Hdf5FileManager()

G4Hdf5FileManager::~G4Hdf5FileManager ( )

Definition at line 64 of file G4Hdf5FileManager.cc.

65{}

Member Function Documentation

◆ CloseFileImpl()

G4bool G4Hdf5FileManager::CloseFileImpl ( std::shared_ptr< G4Hdf5File file)
finalprotectedvirtual

Definition at line 203 of file G4Hdf5FileManager.cc.

204{
205 if ( ! file ) return false;
206
207 G4AutoLock lock(&closeFileMutex);
208
209 ::H5Gclose(std::get<1>(*file));
210 ::H5Gclose(std::get<2>(*file));
211 ::H5Fclose(std::get<0>(*file));
212
213 lock.unlock();
214
215 return true;
216}

◆ CloseNtupleFile()

G4bool G4Hdf5FileManager::CloseNtupleFile ( Hdf5NtupleDescription ntupleDescription)

Definition at line 271 of file G4Hdf5FileManager.cc.

273{
274 // Do nothing if there is no file
275 if ( ! ntupleDescription->fFile ) return true;
276
277 // Ntuple files will be closed with CloseFiles() calls
278 ntupleDescription->fFile.reset();
279
280 // Notify not empty file
281 auto ntupleFileName = GetNtupleFileName(ntupleDescription);
282 auto result = SetIsEmpty(ntupleFileName, ! ntupleDescription->fHasFill);
283
284 return result;
285}
virtual G4bool SetIsEmpty(const G4String &fileName, G4bool isEmpty) final
std::shared_ptr< TF > fFile

◆ CreateFileImpl()

std::shared_ptr< G4Hdf5File > G4Hdf5FileManager::CreateFileImpl ( const G4String fileName)
finalprotectedvirtual

Implements G4TFileManager< FT >.

Definition at line 152 of file G4Hdf5FileManager.cc.

153{
154 // create a new file
155 hid_t file = ::H5Fcreate(fileName, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
156
157 // Do nothing if there is no file
158 // (the error should be handled by caller)
159 if ( file < 0 ) {
160 G4ExceptionDescription description;
161 description << " " << "::H5Fcreate failed " << fileName;
162 G4Exception("G4Hdf5AnalysisManager::CreateFileImpl()",
163 "Analysis_W001", JustWarning, description);
164 return std::make_shared<G4Hdf5File>(-1, -1, -1);
165 }
166
167 // create a header with general infos
168 if(!tools::hdf5::write_header(file)) {
169 G4ExceptionDescription description;
170 description << " " << "tools::hdf5::write_header() failed for " << fileName;
171 G4Exception("G4Hdf5AnalysisManager::CreateFileImpl()",
172 "Analysis_W001", JustWarning, description);
173 return std::make_shared<G4Hdf5File>(-1, -1, -1);
174 }
175
176 // create histo directory
177 auto hdirectory
178 = CreateDirectory(file, fHistoDirectoryName, "histograms");
179 if ( hdirectory < 0 ) {
180 // Warnin is issued in CreateDirectory
181 return std::make_shared<G4Hdf5File>(-1, -1, -1);
182 }
183
184 // create ntuple directory
185 auto ndirectory
186 = CreateDirectory(file, fNtupleDirectoryName, "ntuples");
187 if ( ndirectory < 0 ) {
188 // Warnin is issued in CreateDirectory
189 return std::make_shared<G4Hdf5File>(-1, -1, -1);
190 }
191
192 return std::make_shared<G4Hdf5File>(file, hdirectory, ndirectory);
193}
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:40
G4String fNtupleDirectoryName
G4String fHistoDirectoryName

◆ CreateNtupleFile()

G4bool G4Hdf5FileManager::CreateNtupleFile ( Hdf5NtupleDescription ntupleDescription)

Definition at line 255 of file G4Hdf5FileManager.cc.

257{
258 // get ntuple file name per object
259 auto ntupleFileName = GetNtupleFileName(ntupleDescription);
260
261 auto file = GetTFile(ntupleFileName, false);
262 if (! file) {
263 file = CreateTFile(ntupleFileName);
264 }
265 ntupleDescription->fFile = file;
266
267 return (ntupleDescription->fFile != nullptr);
268}
std::shared_ptr< FT > GetTFile(const G4String &fileName, G4bool warn=true) const
std::shared_ptr< FT > CreateTFile(const G4String &fileName)

◆ GetBasketSize()

unsigned int G4Hdf5FileManager::GetBasketSize ( ) const
inline

Definition at line 97 of file G4Hdf5FileManager.hh.

98{ return fBasketSize; }

◆ GetFileType()

virtual G4String G4Hdf5FileManager::GetFileType ( ) const
inlinefinalvirtual

Reimplemented from G4BaseFileManager.

Definition at line 58 of file G4Hdf5FileManager.hh.

58{ return "hdf5"; }

◆ GetHistoDirectory()

hid_t G4Hdf5FileManager::GetHistoDirectory ( ) const

Definition at line 288 of file G4Hdf5FileManager.cc.

289{
290 if ( ! fFile ) return kInvalidId;
291
292 return std::get<1>(*fFile);
293}
std::shared_ptr< G4Hdf5File > fFile
const G4int kInvalidId

◆ GetNtupleDirectory()

hid_t G4Hdf5FileManager::GetNtupleDirectory ( ) const

Definition at line 296 of file G4Hdf5FileManager.cc.

297{
298 if ( ! fFile ) return kInvalidId;
299
300 return std::get<2>(*fFile);
301}

◆ GetNtupleFileName() [1/2]

G4String G4BaseFileManager::GetNtupleFileName ( const G4String ntupleName) const

Definition at line 70 of file G4BaseFileManager.cc.

109{
111}
virtual G4String GetFileType() const final
G4String GetNtupleFileName(const G4String &fileName, const G4String &fileType, const G4String &ntupleName)

◆ GetNtupleFileName() [2/2]

G4String G4BaseFileManager::GetNtupleFileName ( G4int  ntupleFileNumber) const

Definition at line 76 of file G4BaseFileManager.cc.

115{
116 return G4Analysis::GetNtupleFileName(fFileName, GetFileType(), ntupleFileNumber);
117}

◆ OpenFile()

G4bool G4Hdf5FileManager::OpenFile ( const G4String fileName)
finalvirtual

Implements G4VFileManager.

Definition at line 223 of file G4Hdf5FileManager.cc.

224{
225 // Keep file name
226 fFileName = fileName;
227 auto name = GetFullFileName();
228
229 if ( fFile ) {
230 G4ExceptionDescription description;
231 description
232 << "File " << fileName << " already exists.";
233 G4Exception("G4Hdf5FileManager::OpenFile()",
234 "Analysis_W001", JustWarning, description);
235 fFile.reset();
236 }
237
238 // create new file
239 fFile = CreateTFile(name);
240 if ( ! fFile ) {
241 G4ExceptionDescription description;
242 description << "Failed to create file " << fileName;
243 G4Exception("G4Hdf5FileManager::OpenFile()",
244 "Analysis_W001", JustWarning, description);
245 return false;
246 }
247
248 fLockDirectoryNames = true;
249 fIsOpenFile = true;
250
251 return true;
252}
G4String GetFullFileName(const G4String &baseFileName="", G4bool isPerThread=true) const
G4bool fLockDirectoryNames
const char * name(G4int ptype)

◆ SetBasketSize()

void G4Hdf5FileManager::SetBasketSize ( unsigned int  basketSize)
inline

Definition at line 93 of file G4Hdf5FileManager.hh.

94{ fBasketSize = basketSize; }

◆ WriteFileImpl()

G4bool G4Hdf5FileManager::WriteFileImpl ( std::shared_ptr< G4Hdf5File file)
finalprotectedvirtual

Definition at line 196 of file G4Hdf5FileManager.cc.

197{
198 // Nothing to be done here
199 return true;
200}

The documentation for this class was generated from the following files: