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

#include <G4CsvFileManager.hh>

+ Inheritance diagram for G4CsvFileManager:

Public Member Functions

 G4CsvFileManager (const G4AnalysisManagerState &state)
 
 ~G4CsvFileManager ()
 
virtual G4bool OpenFile (const G4String &fileName) final
 
virtual G4String GetFileType () const final
 
G4bool CreateNtupleFile (CsvNtupleDescription *ntupleDescription)
 
G4bool CloseNtupleFile (CsvNtupleDescription *ntupleDescription)
 
G4String GetNtupleFileName (const G4String &ntupleName) const
 
G4String GetNtupleFileName (G4int ntupleFileNumber) const
 
- Public Member Functions inherited from G4VTFileManager< std::ofstream >
 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< std::ofstream > GetFile () 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< std::ofstream > CreateFileImpl (const G4String &fileName) final
 
virtual G4bool WriteFileImpl (std::shared_ptr< std::ofstream > file) final
 
virtual G4bool CloseFileImpl (std::shared_ptr< std::ofstream > 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< std::ofstream >
std::shared_ptr< std::ofstream > fFile
 
- 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 43 of file G4CsvFileManager.hh.

Constructor & Destructor Documentation

◆ G4CsvFileManager()

G4CsvFileManager::G4CsvFileManager ( const G4AnalysisManagerState state)
explicit

Definition at line 38 of file G4CsvFileManager.cc.

39 : G4VTFileManager(state)
40{
41 // Create helpers defined in the base class
42 fH1FileManager = std::make_shared<G4CsvHnFileManager<histo::h1d>>(this);
43 fH2FileManager = std::make_shared<G4CsvHnFileManager<histo::h2d>>(this);
44 fH3FileManager = std::make_shared<G4CsvHnFileManager<histo::h3d>>(this);
45 fP1FileManager = std::make_shared<G4CsvHnFileManager<histo::p1d>>(this);
46 fP2FileManager = std::make_shared<G4CsvHnFileManager<histo::p2d>>(this);
47}
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

◆ ~G4CsvFileManager()

G4CsvFileManager::~G4CsvFileManager ( )

Definition at line 50 of file G4CsvFileManager.cc.

51{}

Member Function Documentation

◆ CloseFileImpl()

G4bool G4CsvFileManager::CloseFileImpl ( std::shared_ptr< std::ofstream >  file)
finalprotectedvirtual

Definition at line 100 of file G4CsvFileManager.cc.

101{
102 if ( ! file ) return false;
103
104 // close file
105 file->close();
106
107 return true;
108}

◆ CloseNtupleFile()

G4bool G4CsvFileManager::CloseNtupleFile ( CsvNtupleDescription ntupleDescription)

Definition at line 167 of file G4CsvFileManager.cc.

169{
170 // Do nothing if there is no file
171 if ( ! ntupleDescription->fFile ) return true;
172
173 auto finalResult = true;
174
175 auto ntupleFileName = GetNtupleFileName(ntupleDescription);
176
177#ifdef G4VERBOSE
178 if ( fState.GetVerboseL4() ) {
179 fState.GetVerboseL4() ->Message("close", "ntuple file", ntupleFileName);
180 }
181#endif
182
183 // close file
184 auto result = CloseTFile(ntupleFileName);
185 finalResult = result && finalResult;
186
187 // Notify not empty file
188 result = SetIsEmpty(ntupleFileName, ! ntupleDescription->fHasFill);
189 finalResult = result && finalResult;
190
191 // Reset file info in ntuple description
192 ntupleDescription->fFile.reset();
193
194#ifdef G4VERBOSE
195 if ( fState.GetVerboseL2() ) {
196 fState.GetVerboseL2()->Message("close", "ntuple file", ntupleFileName);
197 }
198#endif
199
200 return finalResult;
201}
const G4AnalysisVerbose * GetVerboseL2() const
const G4AnalysisVerbose * GetVerboseL4() const
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
const G4AnalysisManagerState & fState
G4bool CloseTFile(const G4String &fileName)
virtual G4bool SetIsEmpty(const G4String &fileName, G4bool isEmpty) final
std::shared_ptr< TF > fFile

◆ CreateFileImpl()

std::shared_ptr< std::ofstream > G4CsvFileManager::CreateFileImpl ( const G4String fileName)
finalprotectedvirtual

Implements G4TFileManager< FT >.

Definition at line 77 of file G4CsvFileManager.cc.

78{
79 std::shared_ptr<std::ofstream> file = std::make_shared<std::ofstream>(fileName);
80 if ( file->fail() ) {
81 file = nullptr;
82 G4ExceptionDescription description;
83 description << " " << "Cannot create file " << fileName;
84 G4Exception("G4CsvFileManager::CreateFileImpl()",
85 "Analysis_W001", JustWarning, description);
86 return nullptr;
87 }
88
89 return file;
90}
@ 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

◆ CreateNtupleFile()

G4bool G4CsvFileManager::CreateNtupleFile ( CsvNtupleDescription ntupleDescription)

Definition at line 126 of file G4CsvFileManager.cc.

128{
129 // get ntuple file name per object (if defined)
130 auto ntupleFileName = GetNtupleFileName(ntupleDescription);
131
132#ifdef G4VERBOSE
133 if ( fState.GetVerboseL4() ) {
134 fState.GetVerboseL4()->Message("create", "ntuple file", ntupleFileName);
135 }
136#endif
137
138 // update file name if it is already in use
139 while ( GetTFile(ntupleFileName, false) ) {
140 // the file is already in use
141 auto oldName = ntupleDescription->fFileName;
142 auto newName = GetBaseName(oldName) + "_bis." + GetExtension(oldName);
143 ntupleDescription->fFileName = newName;
144
145 G4ExceptionDescription description;
146 description
147 << "Ntuple filename " << oldName << " is already in use." << G4endl
148 << "It will be replaced with : " << newName;
149 G4Exception("G4CsvFileManager::CreateFileImpl()",
150 "Analysis_W001", JustWarning, description);
151
152 ntupleFileName = GetNtupleFileName(ntupleDescription);
153 }
154
155 ntupleDescription->fFile = CreateTFile(ntupleFileName);
156
157#ifdef G4VERBOSE
158 if ( fState.GetVerboseL2() ) {
159 fState.GetVerboseL2()->Message("create", "ntuple file", ntupleFileName);
160 }
161#endif
162
163 return (ntupleDescription->fFile != nullptr);
164}
#define G4endl
Definition: G4ios.hh:57
std::shared_ptr< FT > GetTFile(const G4String &fileName, G4bool warn=true) const
std::shared_ptr< FT > CreateTFile(const G4String &fileName)
G4String GetExtension(const G4String &fileName, const G4String &defaultExtension="")
G4String GetBaseName(const G4String &fileName)

◆ GetFileType()

virtual G4String G4CsvFileManager::GetFileType ( ) const
inlinefinalvirtual

Reimplemented from G4BaseFileManager.

Definition at line 56 of file G4CsvFileManager.hh.

56{ return "csv"; }

◆ 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 G4CsvFileManager::OpenFile ( const G4String fileName)
finalvirtual

Implements G4VFileManager.

Definition at line 115 of file G4CsvFileManager.cc.

116{
117 // Keep file name
118 fFileName = fileName;
119
120 fIsOpenFile = true;
121
122 return true;
123}

◆ WriteFileImpl()

G4bool G4CsvFileManager::WriteFileImpl ( std::shared_ptr< std::ofstream >  file)
finalprotectedvirtual

Definition at line 93 of file G4CsvFileManager.cc.

94{
95 // Nothing to be done here
96 return true;
97}

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