Geant4 11.1.1
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 ()=delete
 
 ~G4CsvFileManager () override=default
 
G4bool OpenFile (const G4String &fileName) final
 
G4bool SetHistoDirectoryName (const G4String &dirName) final
 
G4bool SetNtupleDirectoryName (const G4String &dirName) final
 
G4String GetFileType () const final
 
G4bool NotifyNtupleFile (CsvNtupleDescription *ntupleDescription)
 
G4bool CreateNtupleFile (CsvNtupleDescription *ntupleDescription)
 
G4bool CloseNtupleFile (CsvNtupleDescription *ntupleDescription)
 
G4bool IsHistoDirectory () const
 
G4bool IsNtupleDirectory () const
 
G4String GetNtupleFileName (const G4String &ntupleName, G4int cycle=0) const
 
G4String GetNtupleFileName (G4int ntupleFileNumber, G4int cycle=0) const
 
- Public Member Functions inherited from G4VTFileManager< std::ofstream >
 G4VTFileManager (const G4AnalysisManagerState &state)
 
 ~G4VTFileManager () override=default
 
G4bool CreateFile (const G4String &fileName) final
 
G4bool WriteFile (const G4String &fileName) final
 
virtual G4bool WriteFile (const G4String &fileName)=0
 
G4bool CloseFile (const G4String &fileName) final
 
virtual G4bool CloseFile (const G4String &fileName)=0
 
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< std::ofstream > GetFile () const
 
- 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
 
- Public Member Functions inherited from G4TFileManager< FT >
 G4TFileManager (const G4AnalysisManagerState &state)
 
 G4TFileManager ()=delete
 
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 ()
 
void ClearData ()
 

Protected Member Functions

std::shared_ptr< std::ofstream > CreateFileImpl (const G4String &fileName) final
 
G4bool WriteFileImpl (std::shared_ptr< std::ofstream > file) final
 
G4bool CloseFileImpl (std::shared_ptr< std::ofstream > 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
 
- 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 { 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 45 of file G4CsvFileManager.hh.

Constructor & Destructor Documentation

◆ G4CsvFileManager() [1/2]

G4CsvFileManager::G4CsvFileManager ( const G4AnalysisManagerState state)
explicit

Definition at line 39 of file G4CsvFileManager.cc.

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

G4CsvFileManager::G4CsvFileManager ( )
delete

◆ ~G4CsvFileManager()

G4CsvFileManager::~G4CsvFileManager ( )
overridedefault

Member Function Documentation

◆ CloseFileImpl()

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

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 192 of file G4CsvFileManager.cc.

194{
195 // Notifying not empty file is done in G4CsvNtupleFileManager::ActionAtWrite,
196 // as here we increment the cycle number and GetNtupleFileName returns a file name
197 // for the next cycle version.
198
199 // Ntuple files are registered in file manager map.
200 // they will be closed with CloseFiles() calls
201
202 ntupleDescription->GetFile().reset();
203
204 return true;
205}
std::shared_ptr< FT > GetFile() const

◆ CreateFileImpl()

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

Implements G4TFileManager< FT >.

Definition at line 81 of file G4CsvFileManager.cc.

82{
83 std::shared_ptr<std::ofstream> file = std::make_shared<std::ofstream>(fileName);
84 if ( file->fail() ) {
85 Warn("Cannot create file " + fileName, fkClass, "CreateFileImpl");
86 return nullptr;
87 }
88
89 return file;
90}
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)

◆ CreateNtupleFile()

G4bool G4CsvFileManager::CreateNtupleFile ( CsvNtupleDescription ntupleDescription)

Definition at line 165 of file G4CsvFileManager.cc.

167{
168 // Get ntuple file name per object (if defined)
169 auto ntupleFileName = GetNtupleFileName(ntupleDescription);
170
171 // Update file name if it is already in use
172 while ( GetTFile(ntupleFileName, false) != nullptr ) {
173 // the file is already in use
174 auto oldName = ntupleFileName;
175 auto newName = GetBaseName(oldName) + "_bis." + GetExtension(oldName);
176 ntupleDescription->SetFileName(newName);
177
178 Warn("Ntuple filename " + oldName + " is already in use.\n" +
179 "It will be replaced with : " + newName,
180 fkClass, "CreateNtupleFile");
181
182 ntupleFileName = GetNtupleFileName(ntupleDescription);
183 }
184
185 // Create new ntuple file
186 ntupleDescription->SetFile(CreateTFile(ntupleFileName));
187
188 return (ntupleDescription->GetFile() != nullptr);
189}
std::shared_ptr< FT > GetTFile(const G4String &fileName, G4bool warn=true) const
std::shared_ptr< FT > CreateTFile(const G4String &fileName)
void SetFile(std::shared_ptr< FT > file)
void SetFileName(const G4String &fileName)
G4String GetExtension(const G4String &fileName, const G4String &defaultExtension="")
G4String GetBaseName(const G4String &fileName)

◆ GetFileType()

G4String G4CsvFileManager::GetFileType ( ) const
inlinefinalvirtual

Reimplemented from G4BaseFileManager.

Definition at line 62 of file G4CsvFileManager.hh.

62{ return "csv"; }

◆ GetNtupleFileName() [1/2]

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

Definition at line 80 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 88 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}

◆ IsHistoDirectory()

G4bool G4CsvFileManager::IsHistoDirectory ( ) const
inline

Definition at line 92 of file G4CsvFileManager.hh.

93{ return fIsHistoDirectory; }

◆ IsNtupleDirectory()

G4bool G4CsvFileManager::IsNtupleDirectory ( ) const
inline

Definition at line 95 of file G4CsvFileManager.hh.

96{ return fIsNtupleDirectory; }

◆ NotifyNtupleFile()

G4bool G4CsvFileManager::NotifyNtupleFile ( CsvNtupleDescription ntupleDescription)

Definition at line 156 of file G4CsvFileManager.cc.

157{
158 // Notify not empty file
159 auto ntupleFileName = GetNtupleFileName(ntupleDescription);
160
161 return SetIsEmpty(ntupleFileName, ! ntupleDescription->GetHasFill());
162}
G4bool SetIsEmpty(const G4String &fileName, G4bool isEmpty) final

◆ 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}

◆ SetHistoDirectoryName()

G4bool G4CsvFileManager::SetHistoDirectoryName ( const G4String dirName)
finalvirtual

Reimplemented from G4VFileManager.

Definition at line 126 of file G4CsvFileManager.cc.

127{
128 // A directory is taken into account only if it exists in file system
129 if ( G4fs::is_directory(dirName.data()) ) {
130 fIsHistoDirectory = G4VFileManager::SetHistoDirectoryName(dirName);
131 return fIsHistoDirectory;
132 }
133
134 G4Analysis::Warn("Directory " + dirName + " does not exists.\n"
135 "Histograms will be written in the current directory.",
136 fkClass, "SetHistoDirectoryName");
137 return false;
138}
virtual G4bool SetHistoDirectoryName(const G4String &dirName)

◆ SetNtupleDirectoryName()

G4bool G4CsvFileManager::SetNtupleDirectoryName ( const G4String dirName)
finalvirtual

Reimplemented from G4VFileManager.

Definition at line 141 of file G4CsvFileManager.cc.

142{
143 // A directory is taken into account only if it exists in file system
144 if ( G4fs::is_directory(dirName.data()) ) {
145 fIsNtupleDirectory = G4VFileManager::SetNtupleDirectoryName(dirName);
146 return fIsNtupleDirectory;
147 }
148
149 G4Analysis::Warn("Directory " + dirName + " does not exists.\n"
150 "Ntuples will be written in the current directory.",
151 fkClass, "SetNtupleDirectoryName");
152 return false;
153}
virtual G4bool SetNtupleDirectoryName(const G4String &dirName)

◆ WriteFileImpl()

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

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: