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

#include <G4GenericFileManager.hh>

+ Inheritance diagram for G4GenericFileManager:

Public Member Functions

 G4GenericFileManager (const G4AnalysisManagerState &state)
 
 ~G4GenericFileManager () override=default
 
G4bool OpenFile (const G4String &fileName) final
 
virtual G4bool OpenFiles () final
 
G4bool WriteFiles () final
 
G4bool CloseFiles () final
 
G4bool DeleteEmptyFiles () final
 
void Clear () final
 
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 SetHistoDirectoryName (const G4String &dirName) override
 
G4bool SetNtupleDirectoryName (const G4String &dirName) override
 
G4String GetFileType () const final
 
void SetDefaultFileType (const G4String &value)
 
G4String GetDefaultFileType () const
 
std::shared_ptr< G4VFileManagerGetFileManager (const G4String &fileName)
 
template<typename HT >
G4bool WriteTExtra (const G4String &fileName, HT *ht, const G4String &htName)
 
std::shared_ptr< G4VNtupleFileManagerCreateNtupleFileManager (G4AnalysisOutput output)
 
- Public Member Functions inherited from G4VFileManager
 G4VFileManager (const G4AnalysisManagerState &state)
 
 G4VFileManager ()=delete
 
 ~G4VFileManager () override=default
 
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 OpenFiles ()=0
 
virtual G4bool WriteFiles ()=0
 
virtual G4bool CloseFiles ()=0
 
virtual G4bool DeleteEmptyFiles ()=0
 
virtual void Clear ()=0
 
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
 
virtual G4bool SetFileName (const G4String &fileName)
 
virtual G4String GetFileType () const
 
virtual G4bool HasCycles () 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 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
 

Additional Inherited Members

- Protected Member Functions inherited from G4BaseFileManager
void Message (G4int level, const G4String &action, const G4String &objectType, const G4String &objectName="", G4bool success=true) const
 
- 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
 
G4String fFileName
 
std::vector< G4StringfFileNames
 
- Static Protected Attributes inherited from G4VFileManager
static constexpr std::string_view fkClass { "G4VFileManager" }
 

Detailed Description

Definition at line 52 of file G4GenericFileManager.hh.

Constructor & Destructor Documentation

◆ G4GenericFileManager()

G4GenericFileManager::G4GenericFileManager ( const G4AnalysisManagerState state)
explicit

Definition at line 62 of file G4GenericFileManager.cc.

63 : G4VFileManager(state)
64{}
G4VFileManager()=delete

◆ ~G4GenericFileManager()

G4GenericFileManager::~G4GenericFileManager ( )
overridedefault

Member Function Documentation

◆ Clear()

void G4GenericFileManager::Clear ( )
finalvirtual

Implements G4VFileManager.

Definition at line 298 of file G4GenericFileManager.cc.

299{
300// Clear files data
301
302 for ( const auto& fileManager : fFileManagers ) {
303 if ( ! fileManager ) continue;
304
305 fileManager->Clear();
306 }
308}
void UnlockDirectoryNames()

◆ CloseFile()

G4bool G4GenericFileManager::CloseFile ( const G4String fileName)
finalvirtual

Implements G4VFileManager.

Definition at line 339 of file G4GenericFileManager.cc.

340{
341// New prototype, fully implemented in templated base class
342
343 auto fileManager = GetFileManager(fileName);
344 if ( ! fileManager ) {
345 FileManagerWarning(fileName, fkClass, "CloseFile", fHdf5Warn);
346 return false;
347 }
348
349 return fileManager->CloseFile(fileName);
350}
std::shared_ptr< G4VFileManager > GetFileManager(const G4String &fileName)

◆ CloseFiles()

G4bool G4GenericFileManager::CloseFiles ( )
finalvirtual

Implements G4VFileManager.

Definition at line 252 of file G4GenericFileManager.cc.

253{
254// Close all files regeistered with objects
255
256 Message(kVL4, "close", "analysis files");
257
258 auto result = true;
259
260 for ( const auto& fileManager : fFileManagers ) {
261 if ( ! fileManager ) continue;
262
263 Message(kVL4, "close", fileManager->GetFileType(), "files");
264
265 result &= fileManager->CloseFiles();
266 }
267
268 fIsOpenFile = false;
269
270 Message(kVL3, "close", "analysis files", "", result);
271
272 return result;
273}
void Message(G4int level, const G4String &action, const G4String &objectType, const G4String &objectName="", G4bool success=true) const
constexpr G4int kVL3
constexpr G4int kVL4

◆ CreateFile()

G4bool G4GenericFileManager::CreateFile ( const G4String fileName)
finalvirtual

Implements G4VFileManager.

Definition at line 311 of file G4GenericFileManager.cc.

312{
313// New prototype, fully implemented in templated base class
314
315 auto fileManager = GetFileManager(fileName);
316 if ( ! fileManager ) {
317 FileManagerWarning(fileName, fkClass, "CreateFile", fHdf5Warn);
318 return false;
319 }
320
321 return fileManager->CreateFile(fileName);
322}

◆ CreateNtupleFileManager()

std::shared_ptr< G4VNtupleFileManager > G4GenericFileManager::CreateNtupleFileManager ( G4AnalysisOutput  output)

Definition at line 407 of file G4GenericFileManager.cc.

408{
409 if ( ! GetFileManager(output) ) {
410 CreateFileManager(output);
411 }
412
413 std::shared_ptr<G4VNtupleFileManager> vNtupleFileManager = nullptr;
414 G4String failure;
415
416 switch ( output ) {
417 case G4AnalysisOutput::kCsv: {
418 auto ntupleFileManager = std::make_shared<G4CsvNtupleFileManager>(fState);
419 ntupleFileManager->SetFileManager(fCsvFileManager);
420 vNtupleFileManager = ntupleFileManager;
421 break;
422 }
423 case G4AnalysisOutput::kHdf5: {
424#ifdef TOOLS_USE_HDF5
425 auto ntupleFileManager = std::make_shared<G4Hdf5NtupleFileManager>(fState);
426 ntupleFileManager->SetFileManager(fHdf5FileManager);
427 vNtupleFileManager = ntupleFileManager;
428#else
429 failure = " Hdf5 is not available";
430#endif
431 break;
432 }
433 case G4AnalysisOutput::kRoot: {
434 auto ntupleFileManager = std::make_shared<G4RootNtupleFileManager>(fState);
435 ntupleFileManager->SetFileManager(fRootFileManager);
436 vNtupleFileManager = ntupleFileManager;
437 break;
438 }
439 case G4AnalysisOutput::kXml: {
440 auto ntupleFileManager = std::make_shared<G4XmlNtupleFileManager>(fState);
441 ntupleFileManager->SetFileManager(fXmlFileManager);
442 vNtupleFileManager = ntupleFileManager;
443 break;
444 }
445 case G4AnalysisOutput::kNone:
446 break;
447 }
448
449 if ( ! vNtupleFileManager ) {
450 Warn("Failed to create ntuple file manager of " +
451 G4Analysis::GetOutputName(output) + " type.\n" + failure,
452 fkClass, "CreateNtupleFileManager");
453 }
454
455 return vNtupleFileManager;
456}
const G4AnalysisManagerState & fState
G4String GetOutputName(G4AnalysisOutput outputType)
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)

◆ DeleteEmptyFiles()

G4bool G4GenericFileManager::DeleteEmptyFiles ( )
finalvirtual

Implements G4VFileManager.

Definition at line 276 of file G4GenericFileManager.cc.

277{
278// Close all files regeistered with objects
279
280 Message(kVL4, "delete", "empty files");
281
282 auto result = true;
283
284 for ( const auto& fileManager : fFileManagers ) {
285 if ( ! fileManager ) continue;
286
287 Message(kVL4, "delete", fileManager->GetFileType(), "empty files");
288
289 result &= fileManager->DeleteEmptyFiles();
290 }
291
292 Message(kVL3, "delete", "empty files", "", result);
293
294 return result;
295}

◆ GetDefaultFileType()

G4String G4GenericFileManager::GetDefaultFileType ( ) const

◆ GetFileManager()

std::shared_ptr< G4VFileManager > G4GenericFileManager::GetFileManager ( const G4String fileName)

Definition at line 134 of file G4GenericFileManager.cc.

135{
136 // Get file extension
137 G4String extension = GetExtension(fileName);
138 if (extension.size() == 0u) {
139 // use the default
140 extension = fDefaultFileType;
141 }
142
143 auto output = G4Analysis::GetOutput(extension);
144 if ( output == G4AnalysisOutput::kNone ) {
145 Warn("The file extension " + extension + "is not supported.",
146 fkClass, "GetFileManager");
147 return nullptr;
148 }
149
150 std::shared_ptr<G4VFileManager> fileManager = GetFileManager(output);
151 if ( ! GetFileManager(output) ) {
152 CreateFileManager(output);
153 fileManager = GetFileManager(output);
154 }
155
156 return GetFileManager(output);
157}
G4String GetExtension(const G4String &fileName, const G4String &defaultExtension="")
G4AnalysisOutput GetOutput(const G4String &outputName, G4bool warn=true)

Referenced by CloseFile(), CreateFile(), CreateNtupleFileManager(), GetFileManager(), OpenFile(), OpenFiles(), SetIsEmpty(), and WriteFile().

◆ GetFileType()

G4String G4GenericFileManager::GetFileType ( ) const
inlinefinalvirtual

Reimplemented from G4BaseFileManager.

Definition at line 79 of file G4GenericFileManager.hh.

79{ return ""; }

◆ OpenFile()

G4bool G4GenericFileManager::OpenFile ( const G4String fileName)
finalvirtual

Implements G4VFileManager.

Definition at line 164 of file G4GenericFileManager.cc.

165{
166 auto fileManager = GetFileManager(fileName);
167 if ( ! fileManager ) return false;
168
169 if ( fDefaultFileManager && (fDefaultFileManager != fileManager) ) {
170 // Print warning if default output changed
171 // (maybe be not needed?)
172 Warn("Default file manager changed "
173 "(old: " +fDefaultFileManager->GetFileType() +
174 ", new:" + fileManager->GetFileType() + ")",
175 fkClass, "OpenFile");
176 }
177 fDefaultFileManager = fileManager;
178 fDefaultFileType = fileManager->GetFileType();
179
180 Message(kVL4, "open", "analysis file", fileName);
181
182 auto result = true;
183
184 // Save the default file name
185 // both in the generic file manager and the output specific one
186 result &= SetFileName(fileName);
187 result &= fDefaultFileManager->SetFileName(fileName);
188 result &= fDefaultFileManager->OpenFile(fileName);
189
191 fIsOpenFile = true;
192
193 Message(kVL1, "open", "analysis file", fileName, result);
194
195 return result;
196}
G4bool SetFileName(const G4String &fileName) final
void LockDirectoryNames()
constexpr G4int kVL1

◆ OpenFiles()

G4bool G4GenericFileManager::OpenFiles ( )
finalvirtual

Implements G4VFileManager.

Definition at line 199 of file G4GenericFileManager.cc.

200{
201// Open all files regeistered with objects
202
203 Message(kVL4, "open", "analysis files");
204
205 auto result = true;
206
207 // process names registered in base file manager
208 for ( const auto& fileName : GetFileNames() ) {
209 auto fileManager = GetFileManager(fileName);
210 if ( ! fileManager ) {
211 FileManagerWarning(fileName, fkClass, "OpenFiles", fHdf5Warn);
212 continue;
213 }
214
215 // filenames for csv need to be updated
216 auto newFileName = fileName;
217 if (fileManager == fCsvFileManager) {
218 newFileName = fileManager->GetHnFileName(fileName, GetCycle());
219 }
220
221 result &= fileManager->CreateFile(newFileName);
222 }
223
224 Message(kVL3, "open", "analysis files", "", result);
225
226 return result;
227}
const std::vector< G4String > & GetFileNames() const
G4int GetCycle() const

◆ SetDefaultFileType()

void G4GenericFileManager::SetDefaultFileType ( const G4String value)

Definition at line 391 of file G4GenericFileManager.cc.

392{
393 // Check if value correspond to a valid file type
394 auto output = G4Analysis::GetOutput(value);
395 if ( output == G4AnalysisOutput::kNone ) {
396 Warn("The file type " + value + "is not supported.\n" +
397 "The default type " + fDefaultFileType + " will be used.",
398 fkClass, "SetDeafultFileType");
399 return;
400 }
401
402 fDefaultFileType = value;
403}

◆ SetHistoDirectoryName()

G4bool G4GenericFileManager::SetHistoDirectoryName ( const G4String dirName)
overridevirtual

Reimplemented from G4VFileManager.

Definition at line 365 of file G4GenericFileManager.cc.

366{
367 auto result = G4VFileManager::SetHistoDirectoryName(dirName);
368
369 for (auto& fileManager : fFileManagers ) {
370 if ( fileManager != nullptr ) {
371 result &= fileManager->SetHistoDirectoryName(dirName);
372 }
373 }
374 return result;
375}
virtual G4bool SetHistoDirectoryName(const G4String &dirName)

◆ SetIsEmpty()

G4bool G4GenericFileManager::SetIsEmpty ( const G4String fileName,
G4bool  isEmpty 
)
finalvirtual

Implements G4VFileManager.

Definition at line 353 of file G4GenericFileManager.cc.

354{
355 auto fileManager = GetFileManager(fileName);
356 if ( ! fileManager ) {
357 FileManagerWarning(fileName, fkClass, "SetIsEmpty", fHdf5Warn);
358 return false;
359 }
360
361 return fileManager->SetIsEmpty(fileName, isEmpty);
362}

◆ SetNtupleDirectoryName()

G4bool G4GenericFileManager::SetNtupleDirectoryName ( const G4String dirName)
overridevirtual

Reimplemented from G4VFileManager.

Definition at line 378 of file G4GenericFileManager.cc.

379{
380 auto result = G4VFileManager::SetNtupleDirectoryName(dirName);
381
382 for (auto& fileManager : fFileManagers ) {
383 if ( fileManager != nullptr ) {
384 result &= fileManager->SetNtupleDirectoryName(dirName);
385 }
386 }
387 return result;
388}
virtual G4bool SetNtupleDirectoryName(const G4String &dirName)

◆ WriteFile()

G4bool G4GenericFileManager::WriteFile ( const G4String fileName)
finalvirtual

Implements G4VFileManager.

Definition at line 325 of file G4GenericFileManager.cc.

326{
327// New prototype, fully implemented in templated base class
328
329 auto fileManager = GetFileManager(fileName);
330 if ( ! fileManager ) {
331 FileManagerWarning(fileName, fkClass, "WriteFile", fHdf5Warn);
332 return false;
333 }
334
335 return fileManager->WriteFile(fileName);
336}

◆ WriteFiles()

G4bool G4GenericFileManager::WriteFiles ( )
finalvirtual

Implements G4VFileManager.

Definition at line 230 of file G4GenericFileManager.cc.

231{
232// Finish write for all files registered with objects
233
234 Message(kVL4, "write", "analysis files");
235
236 auto result = true;
237
238 for ( const auto& fileManager : fFileManagers ) {
239 if ( ! fileManager ) continue;
240
241 Message(kVL4, "write", fileManager->GetFileType(), "files");
242
243 result &= fileManager->WriteFiles();
244 }
245
246 Message(kVL3, "write", "analysis files", "", result);
247
248 return result;
249}

◆ WriteTExtra()

template<typename HT >
G4bool G4GenericFileManager::WriteTExtra ( const G4String fileName,
HT *  ht,
const G4String htName 
)

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