Geant4 11.2.2
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 ()=delete
 
 ~G4Hdf5FileManager () override=default
 
G4bool OpenFile (const G4String &fileName) final
 
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, G4int cycle=0) const
 
G4String GetNtupleFileName (G4int ntupleFileNumber, G4int cycle=0) const
 
- Public Member Functions inherited from G4VTFileManager< G4Hdf5File >
 G4VTFileManager (const G4AnalysisManagerState &state)
 
 ~G4VTFileManager () override=default
 
G4bool CreateFile (const G4String &fileName) final
 
G4bool WriteFile (const G4String &fileName) final
 
G4bool CloseFile (const G4String &fileName) final
 
G4bool SetIsEmpty (const G4String &fileName, G4bool isEmpty) final
 
G4bool OpenFiles () final
 
G4bool WriteFiles () final
 
G4bool CloseFiles () final
 
G4bool DeleteEmptyFiles () final
 
void Clear () final
 
std::shared_ptr< G4Hdf5FileGetFile () const
 
- Public Member Functions inherited from G4VFileManager
 G4VFileManager (const G4AnalysisManagerState &state)
 
 G4VFileManager ()=delete
 
 ~G4VFileManager () override=default
 
G4bool SetFileName (const G4String &fileName) final
 
virtual G4bool SetHistoDirectoryName (const G4String &dirName)
 
virtual G4bool SetNtupleDirectoryName (const G4String &dirName)
 
void LockDirectoryNames ()
 
void UnlockDirectoryNames ()
 
G4bool IsOpenFile () const
 
G4String GetHistoDirectoryName () const
 
G4String GetNtupleDirectoryName () const
 
G4int GetCycle () const
 
template<typename HT >
std::shared_ptr< G4VTHnFileManager< HT > > GetHnFileManager () const
 
- Public Member Functions inherited from G4BaseFileManager
 G4BaseFileManager (const G4AnalysisManagerState &state)
 
 G4BaseFileManager ()=delete
 
virtual ~G4BaseFileManager ()=default
 
void SetCompressionLevel (G4int level)
 
virtual G4bool HasCycles () const
 
void AddFileName (const G4String &fileName)
 
G4int GetCompressionLevel () const
 
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 GetHnFileName (const G4String &fileName, G4int cycle=0) const
 
G4String GetNtupleFileName (const G4String &ntupleName, G4int cycle=0) const
 
G4String GetNtupleFileName (G4int ntupleFileNumber, G4int cycle=0) const
 
G4String GetPlotFileName () const
 
- Public Member Functions inherited from G4TFileManager< G4Hdf5File >
 G4TFileManager (const G4AnalysisManagerState &state)
 
 G4TFileManager ()=delete
 
virtual ~G4TFileManager ()
 
std::shared_ptr< G4Hdf5FileCreateTFile (const G4String &fileName)
 
G4bool WriteTFile (const G4String &fileName)
 
G4bool CloseTFile (const G4String &fileName)
 
G4bool SetIsEmpty (const G4String &fileName, G4bool isEmpty)
 
std::shared_ptr< G4Hdf5FileGetTFile (const G4String &fileName, G4bool warn=true) const
 
G4bool OpenFiles ()
 
G4bool WriteFiles ()
 
G4bool CloseFiles ()
 
G4bool DeleteEmptyFiles ()
 
void ClearData ()
 

Protected Member Functions

std::shared_ptr< G4Hdf5FileCreateFileImpl (const G4String &fileName) final
 
G4bool WriteFileImpl (std::shared_ptr< G4Hdf5File > file) final
 
G4bool CloseFileImpl (std::shared_ptr< G4Hdf5File > file) final
 
- Protected Member Functions inherited from G4BaseFileManager
void Message (G4int level, const G4String &action, const G4String &objectType, const G4String &objectName="", G4bool success=true) const
 
void ClearData ()
 
- Protected Member Functions inherited from G4TFileManager< G4Hdf5File >

Additional Inherited Members

- Protected Attributes inherited from G4VTFileManager< G4Hdf5File >
std::shared_ptr< G4Hdf5FilefFile
 
- Protected Attributes inherited from G4VFileManager
G4String fHistoDirectoryName
 
G4String fNtupleDirectoryName
 
G4bool fIsOpenFile { false }
 
G4bool fLockDirectoryNames { false }
 
std::shared_ptr< G4VTHnFileManager< tools::histo::h1d > > fH1FileManager { nullptr }
 
std::shared_ptr< G4VTHnFileManager< tools::histo::h2d > > fH2FileManager { nullptr }
 
std::shared_ptr< G4VTHnFileManager< tools::histo::h3d > > fH3FileManager { nullptr }
 
std::shared_ptr< G4VTHnFileManager< tools::histo::p1d > > fP1FileManager { nullptr }
 
std::shared_ptr< G4VTHnFileManager< tools::histo::p2d > > fP2FileManager { nullptr }
 
- Protected Attributes inherited from G4BaseFileManager
const G4AnalysisManagerStatefState
 
G4int fCompressionLevel { 1 }
 
G4String fFileName
 
std::vector< G4StringfFileNames
 
- Static Protected Attributes inherited from G4VFileManager
static constexpr std::string_view fkClass { "G4VFileManager" }
 

Detailed Description

Definition at line 47 of file G4Hdf5FileManager.hh.

Constructor & Destructor Documentation

◆ G4Hdf5FileManager() [1/2]

G4Hdf5FileManager::G4Hdf5FileManager ( const G4AnalysisManagerState & state)
explicit

Definition at line 48 of file G4Hdf5FileManager.cc.

50{
51 // Create helpers defined in the base class
52 fH1FileManager = std::make_shared<G4Hdf5HnFileManager<histo::h1d>>(this);
53 fH2FileManager = std::make_shared<G4Hdf5HnFileManager<histo::h2d>>(this);
54 fH3FileManager = std::make_shared<G4Hdf5HnFileManager<histo::h3d>>(this);
55 fP1FileManager = std::make_shared<G4Hdf5HnFileManager<histo::p1d>>(this);
56 fP2FileManager = std::make_shared<G4Hdf5HnFileManager<histo::p2d>>(this);
57}
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() [2/2]

G4Hdf5FileManager::G4Hdf5FileManager ( )
delete

◆ ~G4Hdf5FileManager()

G4Hdf5FileManager::~G4Hdf5FileManager ( )
overridedefault

Member Function Documentation

◆ CloseFileImpl()

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

Implements G4TFileManager< G4Hdf5File >.

Definition at line 176 of file G4Hdf5FileManager.cc.

177{
178 if ( ! file ) return false;
179
180 G4AutoLock lock(&closeFileMutex);
181
182 ::H5Gclose(std::get<1>(*file));
183 ::H5Gclose(std::get<2>(*file));
184 ::H5Fclose(std::get<0>(*file));
185
186 lock.unlock();
187
188 return true;
189}

◆ CloseNtupleFile()

G4bool G4Hdf5FileManager::CloseNtupleFile ( Hdf5NtupleDescription * ntupleDescription)

Definition at line 237 of file G4Hdf5FileManager.cc.

239{
240 // Notify not empty file
241 auto ntupleFileName = GetNtupleFileName(ntupleDescription);
242 auto result = SetIsEmpty(ntupleFileName, ! ntupleDescription->GetHasFill());
243
244 // Ntuple files are registered in file manager map.
245 // they will be closed with CloseFiles() calls
246 ntupleDescription->GetFile().reset();
247
248 return result;
249}
std::shared_ptr< FT > GetFile() const
G4bool SetIsEmpty(const G4String &fileName, G4bool isEmpty) final

◆ CreateFileImpl()

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

Implements G4TFileManager< G4Hdf5File >.

Definition at line 130 of file G4Hdf5FileManager.cc.

131{
132 // create a new file
133 hid_t file = ::H5Fcreate(fileName, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
134
135 // Do nothing if there is no file
136 // (the error should be handled by caller)
137 if ( file < 0 ) {
138 Warn("::H5Fcreate failed " + fileName, fkClass, "CreateFileImpl");
139 return std::make_shared<G4Hdf5File>(-1, -1, -1);
140 }
141
142 // create a header with general infos
143 if(!toolx::hdf5::write_header(file)) {
144 Warn("toolx::hdf5::write_header() failed for " + fileName,
145 fkClass, "CreateFileImpl");
146 return std::make_shared<G4Hdf5File>(-1, -1, -1);
147 }
148
149 // create histo directory
150 auto hdirectory
151 = CreateDirectory(file, fHistoDirectoryName, "histograms");
152 if ( hdirectory < 0 ) {
153 // Warning is issued in CreateDirectory
154 return std::make_shared<G4Hdf5File>(-1, -1, -1);
155 }
156
157 // create ntuple directory
158 auto ndirectory
159 = CreateDirectory(file, fNtupleDirectoryName, "ntuples");
160 if ( ndirectory < 0 ) {
161 // Warnin is issued in CreateDirectory
162 return std::make_shared<G4Hdf5File>(-1, -1, -1);
163 }
164
165 return std::make_shared<G4Hdf5File>(file, hdirectory, ndirectory);
166}
G4String fNtupleDirectoryName
G4String fHistoDirectoryName
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)

◆ CreateNtupleFile()

G4bool G4Hdf5FileManager::CreateNtupleFile ( Hdf5NtupleDescription * ntupleDescription)

Definition at line 221 of file G4Hdf5FileManager.cc.

223{
224 // get ntuple file name per object
225 auto ntupleFileName = GetNtupleFileName(ntupleDescription);
226
227 auto file = GetTFile(ntupleFileName, false);
228 if (! file) {
229 file = CreateTFile(ntupleFileName);
230 }
231 ntupleDescription->SetFile(file);
232
233 return (ntupleDescription->GetFile() != nullptr);
234}
std::shared_ptr< G4Hdf5File > GetTFile(const G4String &fileName, G4bool warn=true) const
std::shared_ptr< G4Hdf5File > CreateTFile(const G4String &fileName)
void SetFile(std::shared_ptr< FT > file)

◆ GetBasketSize()

unsigned int G4Hdf5FileManager::GetBasketSize ( ) const
inline

Definition at line 101 of file G4Hdf5FileManager.hh.

102{ return fBasketSize; }

◆ GetFileType()

G4String G4Hdf5FileManager::GetFileType ( ) const
inlinefinalvirtual

Reimplemented from G4BaseFileManager.

Definition at line 61 of file G4Hdf5FileManager.hh.

61{ return "hdf5"; }

◆ GetHistoDirectory()

hid_t G4Hdf5FileManager::GetHistoDirectory ( ) const

Definition at line 252 of file G4Hdf5FileManager.cc.

253{
254 if ( ! fFile ) return kInvalidId;
255
256 return std::get<1>(*fFile);
257}
std::shared_ptr< G4Hdf5File > fFile
constexpr G4int kInvalidId

◆ GetNtupleDirectory()

hid_t G4Hdf5FileManager::GetNtupleDirectory ( ) const

Definition at line 260 of file G4Hdf5FileManager.cc.

261{
262 if ( ! fFile ) return kInvalidId;
263
264 return std::get<2>(*fFile);
265}

◆ GetNtupleFileName() [1/2]

G4String G4BaseFileManager::GetNtupleFileName ( const G4String & ntupleName,
G4int cycle = 0 ) const

Definition at line 84 of file G4BaseFileManager.cc.

110{
111 // Do not pass cycle if supported by the output type
112 auto cycleToPass = (HasCycles()) ? 0 : cycle;
113
114 return G4Analysis::GetNtupleFileName(fFileName, GetFileType(), ntupleName, cycleToPass);
115}
virtual G4bool HasCycles() const
G4String GetFileType() const final
G4String GetNtupleFileName(const G4String &fileName, const G4String &fileType, const G4String &ntupleName, G4int cycle=0)

◆ GetNtupleFileName() [2/2]

G4String G4BaseFileManager::GetNtupleFileName ( G4int ntupleFileNumber,
G4int cycle = 0 ) const

Definition at line 92 of file G4BaseFileManager.cc.

120{
121 // Do not pass cycle if supported by the output type
122 auto cycleToPass = (HasCycles()) ? 0 : cycle;
123
124 return G4Analysis::GetNtupleFileName(fFileName, GetFileType(), ntupleFileNumber, cycleToPass);
125}

◆ OpenFile()

G4bool G4Hdf5FileManager::OpenFile ( const G4String & fileName)
finalvirtual

Implements G4VFileManager.

Definition at line 196 of file G4Hdf5FileManager.cc.

197{
198 // Keep file name
199 fFileName = fileName;
200 auto name = GetFullFileName();
201
202 if ( fFile ) {
203 Warn("File " + fileName + " already exists.", fkClass, "OpenFile");
204 fFile.reset();
205 }
206
207 // create new file
208 fFile = CreateTFile(name);
209 if ( ! fFile ) {
210 Warn("Failed to create file " + fileName, fkClass, "OpenFile");
211 return false;
212 }
213
215 fIsOpenFile = true;
216
217 return true;
218}
G4String GetFullFileName(const G4String &baseFileName="", G4bool isPerThread=true) const
const char * name(G4int ptype)

◆ SetBasketSize()

void G4Hdf5FileManager::SetBasketSize ( unsigned int basketSize)
inline

Definition at line 97 of file G4Hdf5FileManager.hh.

98{ fBasketSize = basketSize; }

◆ WriteFileImpl()

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

Implements G4TFileManager< G4Hdf5File >.

Definition at line 169 of file G4Hdf5FileManager.cc.

170{
171 // Nothing to be done here
172 return true;
173}

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