Geant4 10.7.0
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)
 
virtual ~G4GenericFileManager ()
 
virtual G4bool OpenFile (const G4String &fileName) final
 
virtual G4bool OpenFiles () final
 
virtual G4bool WriteFiles () final
 
virtual G4bool CloseFiles () final
 
virtual G4bool DeleteEmptyFiles () final
 
virtual G4bool CreateFile (const G4String &fileName) final
 
virtual G4bool WriteFile (const G4String &fileName) final
 
virtual G4bool CloseFile (const G4String &fileName) final
 
virtual G4bool SetIsEmpty (const G4String &fileName, G4bool isEmpty) final
 
virtual G4String GetFileType () const final
 
void SetDefaultFileType (const G4String &value)
 
G4String GetDefaultFileType () const
 
template<typename T >
G4bool WriteT (const std::vector< T * > &htVector, const std::vector< G4HnInformation * > &hnVector)
 
template<typename T >
G4bool WriteTExtra (const G4String &fileName, T *ht, const G4String &htName)
 
std::shared_ptr< G4VNtupleFileManagerCreateNtupleFileManager (G4AnalysisOutput output)
 
- 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
 

Additional Inherited Members

- 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 51 of file G4GenericFileManager.hh.

Constructor & Destructor Documentation

◆ G4GenericFileManager()

G4GenericFileManager::G4GenericFileManager ( const G4AnalysisManagerState state)
explicit

Definition at line 68 of file G4GenericFileManager.cc.

69 : G4VFileManager(state),
70 fDefaultFileType(fgkDefaultFileType),
71 fDefaultFileManager(nullptr),
72 fFileManagers
73 { nullptr, // Csv
74 nullptr, // Hdf5
75 nullptr, // Generic
76 nullptr // Xml
77 },
78 fCsvFileManager(nullptr),
79#ifdef TOOLS_USE_HDF5
80 fHdf5FileManager(nullptr),
81#endif
82 fRootFileManager(nullptr),
83 fXmlFileManager(nullptr),
84 fHdf5Warn(true)
85{}

◆ ~G4GenericFileManager()

G4GenericFileManager::~G4GenericFileManager ( )
virtual

Definition at line 88 of file G4GenericFileManager.cc.

89{}

Member Function Documentation

◆ CloseFile()

G4bool G4GenericFileManager::CloseFile ( const G4String fileName)
finalvirtual

Implements G4VFileManager.

Definition at line 423 of file G4GenericFileManager.cc.

424{
425// New prototype, fully implemented in templated base class
426
427 auto fileManager = GetFileManager(fileName);
428 if ( ! fileManager ) {
429 FileManagerException(fileName, "CloseFile", "W021", fHdf5Warn);
430 return false;
431 }
432
433 return fileManager->CloseFile(fileName);
434}

◆ CloseFiles()

G4bool G4GenericFileManager::CloseFiles ( )
finalvirtual

Implements G4VFileManager.

Definition at line 323 of file G4GenericFileManager.cc.

324{
325// Close all files regeistered with objects
326
327#ifdef G4VERBOSE
328 if ( fState.GetVerboseL4() ) {
329 fState.GetVerboseL4()->Message("close", "files", "");
330 }
331#endif
332
333 auto finalResult = true;
334 auto result = true;
335
336 for ( auto fileManager : fFileManagers ) {
337 if ( ! fileManager ) continue;
338
339#ifdef G4VERBOSE
340 if ( fState.GetVerboseL4() ) {
341 fState.GetVerboseL4()->Message("close", fileManager->GetFileType(), "files");
342 }
343#endif
344
345 result = fileManager->CloseFiles();
346 finalResult = result && finalResult;
347 }
348
349#ifdef G4VERBOSE
350 if ( fState.GetVerboseL3() ) {
351 fState.GetVerboseL3()->Message("close", "files", "", finalResult);
352 }
353#endif
354
355 return finalResult;
356}
const G4AnalysisVerbose * GetVerboseL3() const
const G4AnalysisVerbose * GetVerboseL4() const
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
const G4AnalysisManagerState & fState

◆ CreateFile()

G4bool G4GenericFileManager::CreateFile ( const G4String fileName)
finalvirtual

Implements G4VFileManager.

Definition at line 395 of file G4GenericFileManager.cc.

396{
397// New prototype, fully implemented in templated base class
398
399 auto fileManager = GetFileManager(fileName);
400 if ( ! fileManager ) {
401 FileManagerException(fileName, "CreateFile", "W001", fHdf5Warn);
402 return false;
403 }
404
405 return fileManager->CreateFile(fileName);
406}

◆ CreateNtupleFileManager()

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

Definition at line 468 of file G4GenericFileManager.cc.

469{
470 if ( ! GetFileManager(output) ) {
471 CreateFileManager(output);
472 }
473
474 std::shared_ptr<G4VNtupleFileManager> vNtupleFileManager = nullptr;
475 G4String failure;
476
477 switch ( output ) {
478 case G4AnalysisOutput::kCsv: {
479 auto ntupleFileManager = std::make_shared<G4CsvNtupleFileManager>(fState);
480 ntupleFileManager->SetFileManager(fCsvFileManager);
481 vNtupleFileManager = ntupleFileManager;
482 break;
483 }
484 case G4AnalysisOutput::kHdf5: {
485#ifdef TOOLS_USE_HDF5
486 auto ntupleFileManager = std::make_shared<G4Hdf5NtupleFileManager>(fState);
487 ntupleFileManager->SetFileManager(fHdf5FileManager);
488 vNtupleFileManager = ntupleFileManager;
489#else
490 failure = " Hdf5 is not available";
491#endif
492 break;
493 }
494 case G4AnalysisOutput::kRoot: {
495 auto ntupleFileManager = std::make_shared<G4RootNtupleFileManager>(fState);
496 ntupleFileManager->SetFileManager(fRootFileManager);
497 vNtupleFileManager = ntupleFileManager;
498 break;
499 }
500 case G4AnalysisOutput::kXml: {
501 auto ntupleFileManager = std::make_shared<G4XmlNtupleFileManager>(fState);
502 ntupleFileManager->SetFileManager(fXmlFileManager);
503 vNtupleFileManager = ntupleFileManager;
504 break;
505 }
506 case G4AnalysisOutput::kNone:
507 break;
508 }
509
510 if ( ! vNtupleFileManager ) {
511 G4ExceptionDescription description;
512 description
513 << " "
514 << "Failed to create ntuple file manager of " << G4Analysis::GetOutputName(output) << " type."
515 << failure;
516 G4Exception("G4GenericFileManager::CreateNtupleFileManager",
517 "Analysis_W002", JustWarning, description);
518 }
519
520 return vNtupleFileManager;
521}
@ 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 GetOutputName(G4AnalysisOutput outputType)

◆ DeleteEmptyFiles()

G4bool G4GenericFileManager::DeleteEmptyFiles ( )
finalvirtual

Implements G4VFileManager.

Definition at line 359 of file G4GenericFileManager.cc.

360{
361// Close all files regeistered with objects
362
363#ifdef G4VERBOSE
364 if ( fState.GetVerboseL4() ) {
365 fState.GetVerboseL4()->Message("delete", "empty files", "");
366 }
367#endif
368
369 auto finalResult = true;
370 auto result = true;
371
372 for ( auto fileManager : fFileManagers ) {
373 if ( ! fileManager ) continue;
374
375#ifdef G4VERBOSE
376 if ( fState.GetVerboseL4() ) {
377 fState.GetVerboseL4()->Message("delete", fileManager->GetFileType(), "files");
378 }
379#endif
380
381 result = fileManager->DeleteEmptyFiles();
382 finalResult = result && finalResult;
383 }
384
385#ifdef G4VERBOSE
386 if ( fState.GetVerboseL3() ) {
387 fState.GetVerboseL3()->Message("delete", "empty files", "", finalResult);
388 }
389#endif
390
391 return finalResult;
392}

◆ GetDefaultFileType()

G4String G4GenericFileManager::GetDefaultFileType ( ) const

◆ GetFileType()

virtual G4String G4GenericFileManager::GetFileType ( ) const
inlinefinalvirtual

Reimplemented from G4BaseFileManager.

Definition at line 72 of file G4GenericFileManager.hh.

72{ return ""; }

◆ OpenFile()

G4bool G4GenericFileManager::OpenFile ( const G4String fileName)
finalvirtual

Implements G4VFileManager.

Definition at line 202 of file G4GenericFileManager.cc.

203{
204 auto fileManager = GetFileManager(fileName);
205 if ( ! fileManager ) return false;
206
207 if ( fDefaultFileManager && (fDefaultFileManager != fileManager) ) {
208 // Print warning if default output changed
209 // (maybe be not needed?)
210 G4ExceptionDescription description;
211 description
212 << "Default file manager changed (old: "
213 << fDefaultFileManager->GetFileType()
214 << ", new:" << fileManager->GetFileType() << ")";
215 G4Exception("G4GenericFileManager::OpenFile",
216 "Analysis_W001", JustWarning, description);
217 }
218 fDefaultFileManager = fileManager;
219
220#ifdef G4VERBOSE
221 if ( fState.GetVerboseL4() ) {
222 fState.GetVerboseL4()->Message("open", "analysis file", fileName);
223 }
224#endif
225
226 auto finalResult = true;
227 auto result = true;
228
229 // Save the default file name
230 // both in the generic file manager and the output specific one
231 result = SetFileName(fileName);
232 finalResult = finalResult && result;
233 result = fDefaultFileManager->SetFileName(fileName);
234 finalResult = finalResult && result;
235
236 result = fDefaultFileManager->OpenFile(fileName);
237 finalResult = finalResult && result;
238
239#ifdef G4VERBOSE
240 if ( fState.GetVerboseL1() ) {
241 fState.GetVerboseL1()->Message("open", "analysis file", fileName, finalResult);
242 }
243#endif
244
245 fLockDirectoryNames = true;
246 fIsOpenFile = true;
247
248 return finalResult;
249}
const G4AnalysisVerbose * GetVerboseL1() const
G4bool fLockDirectoryNames
virtual G4bool SetFileName(const G4String &fileName) final

◆ OpenFiles()

G4bool G4GenericFileManager::OpenFiles ( )
finalvirtual

Definition at line 252 of file G4GenericFileManager.cc.

253{
254// Open all files regeistered with objects
255
256#ifdef G4VERBOSE
257 if ( fState.GetVerboseL4() ) {
258 fState.GetVerboseL4()->Message("open", "analysis files", "");
259 }
260#endif
261
262 auto finalResult = true;
263 auto result = true;
264
265 // process names registered in base file manager
266 for ( auto fileName : GetFileNames() ) {
267 auto fileManager = GetFileManager(fileName);
268 if ( ! fileManager ) {
269 FileManagerException(fileName, "OpenFiles", "W001", fHdf5Warn);
270 continue;
271 }
272
273 result = fileManager->CreateFile(fileName);
274 finalResult = result && finalResult;
275 }
276
277#ifdef G4VERBOSE
278 if ( fState.GetVerboseL3() ) {
279 fState.GetVerboseL3()->Message("open", "analysis files", "", finalResult);
280 }
281#endif
282
283 return finalResult;
284}
const std::vector< G4String > & GetFileNames() const

◆ SetDefaultFileType()

void G4GenericFileManager::SetDefaultFileType ( const G4String value)

Definition at line 449 of file G4GenericFileManager.cc.

450{
451 // Check if value correspond to a valid file type
452 auto output = G4Analysis::GetOutput(value);
453 if ( output == G4AnalysisOutput::kNone ) {
454 G4ExceptionDescription description;
455 description
456 << "The file type " << value << "is not supported." << G4endl
457 << "The default type " << fDefaultFileType << " will be used.";
458 G4Exception("G4GenericFileManager::SetDeafultFileType",
459 "Analysis_W051", JustWarning, description);
460 return;
461 }
462
463 fDefaultFileType = value;
464}
#define G4endl
Definition: G4ios.hh:57
G4AnalysisOutput GetOutput(const G4String &outputName, G4bool warn=true)

◆ SetIsEmpty()

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

Implements G4VFileManager.

Definition at line 437 of file G4GenericFileManager.cc.

438{
439 auto fileManager = GetFileManager(fileName);
440 if ( ! fileManager ) {
441 FileManagerException(fileName, "SetIsEmpty", "W021", fHdf5Warn);
442 return false;
443 }
444
445 return fileManager->SetIsEmpty(fileName, isEmpty);
446}

◆ WriteFile()

G4bool G4GenericFileManager::WriteFile ( const G4String fileName)
finalvirtual

Implements G4VFileManager.

Definition at line 409 of file G4GenericFileManager.cc.

410{
411// New prototype, fully implemented in templated base class
412
413 auto fileManager = GetFileManager(fileName);
414 if ( ! fileManager ) {
415 FileManagerException(fileName, "WriteFile", "W021", fHdf5Warn);
416 return false;
417 }
418
419 return fileManager->WriteFile(fileName);
420}

◆ WriteFiles()

G4bool G4GenericFileManager::WriteFiles ( )
finalvirtual

Implements G4VFileManager.

Definition at line 287 of file G4GenericFileManager.cc.

288{
289// Finish write for all files regeistered with objects
290
291#ifdef G4VERBOSE
292 if ( fState.GetVerboseL4() ) {
293 fState.GetVerboseL4()->Message("write", "files", "");
294 }
295#endif
296
297 auto finalResult = true;
298 auto result = true;
299
300 for ( auto fileManager : fFileManagers ) {
301 if ( ! fileManager ) continue;
302
303#ifdef G4VERBOSE
304 if ( fState.GetVerboseL4() ) {
305 fState.GetVerboseL4()->Message("write", fileManager->GetFileType(), "files");
306 }
307#endif
308
309 result = fileManager->WriteFiles();
310 finalResult = result && finalResult;
311 }
312
313#ifdef G4VERBOSE
314 if ( fState.GetVerboseL3() ) {
315 fState.GetVerboseL3()->Message("write", "files", "", finalResult);
316 }
317#endif
318
319 return finalResult;
320}

◆ WriteT()

template<typename T >
G4bool G4GenericFileManager::WriteT ( const std::vector< T * > &  htVector,
const std::vector< G4HnInformation * > &  hnVector 
)

◆ WriteTExtra()

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

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