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

#include <G4XmlFileManager.hh>

+ Inheritance diagram for G4XmlFileManager:

Public Member Functions

 G4XmlFileManager (const G4AnalysisManagerState &state)
 
 ~G4XmlFileManager ()
 
virtual G4bool OpenFile (const G4String &fileName) final
 
virtual G4String GetFileType () const final
 
G4bool CreateNtupleFile (XmlNtupleDescription *ntupleDescription)
 
G4bool CloseNtupleFile (XmlNtupleDescription *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 49 of file G4XmlFileManager.hh.

Constructor & Destructor Documentation

◆ G4XmlFileManager()

G4XmlFileManager::G4XmlFileManager ( const G4AnalysisManagerState state)
explicit

Definition at line 40 of file G4XmlFileManager.cc.

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

◆ ~G4XmlFileManager()

G4XmlFileManager::~G4XmlFileManager ( )

Definition at line 52 of file G4XmlFileManager.cc.

53{}

Member Function Documentation

◆ CloseFileImpl()

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

Definition at line 103 of file G4XmlFileManager.cc.

104{
105 if ( ! file ) return false;
106
107 // close file
108 waxml::end(*file);
109 file->close();
110
111 return true;
112}

◆ CloseNtupleFile()

G4bool G4XmlFileManager::CloseNtupleFile ( XmlNtupleDescription ntupleDescription)

Definition at line 193 of file G4XmlFileManager.cc.

195{
196 // Do nothing if there is no file
197 if ( ! ntupleDescription->fFile ) return true;
198
199 auto finalResult = true;
200
201 auto ntupleFileName = GetNtupleFileName(ntupleDescription);
202
203#ifdef G4VERBOSE
204 if ( fState.GetVerboseL4() ) {
205 fState.GetVerboseL4()->Message("close", "ntuple file", ntupleFileName);
206 }
207#endif
208
209 // close file
210 auto result = CloseTFile(ntupleFileName);
211 finalResult = result && finalResult;
212
213 // Notify not empty file
214 result = SetIsEmpty(ntupleFileName, ! ntupleDescription->fHasFill);
215 finalResult = result && finalResult;
216
217 // Reset file info in ntuple description
218 ntupleDescription->fFile.reset();
219
220#ifdef G4VERBOSE
221 if ( fState.GetVerboseL2() ) {
222 fState.GetVerboseL2()->Message("close", "ntuple file", ntupleFileName);
223 }
224#endif
225
226 return result;
227}
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 > G4XmlFileManager::CreateFileImpl ( const G4String fileName)
finalprotectedvirtual

Implements G4TFileManager< FT >.

Definition at line 79 of file G4XmlFileManager.cc.

80{
81 std::shared_ptr<std::ofstream> file = std::make_shared<std::ofstream>(fileName);
82 if ( file->fail() ) {
83 file = nullptr;
84 G4ExceptionDescription description;
85 description << " " << "Cannot create file " << fileName;
86 G4Exception("G4XmlFileManager::CreateFileImpl()",
87 "Analysis_W001", JustWarning, description);
88 return nullptr;
89 }
90
91 waxml::begin(*file);
92 return file;
93}
@ 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 G4XmlFileManager::CreateNtupleFile ( XmlNtupleDescription ntupleDescription)

Definition at line 152 of file G4XmlFileManager.cc.

154{
155 // get ntuple file name per object (if defined)
156 auto ntupleFileName = GetNtupleFileName(ntupleDescription);
157
158#ifdef G4VERBOSE
159 if ( fState.GetVerboseL4() ) {
160 fState.GetVerboseL4()->Message("create", "ntuple file", ntupleFileName);
161 }
162#endif
163
164 // update file name if it is already in use
165 while ( GetTFile(ntupleFileName, false) ) {
166 // the file is already in use
167 auto oldName = ntupleDescription->fFileName;
168 auto newName = GetBaseName(oldName) + "_bis." + GetExtension(oldName);
169 ntupleDescription->fFileName = newName;
170
171 G4ExceptionDescription description;
172 description
173 << "Ntuple filename " << oldName << " is already in use." << G4endl
174 << "It will be replaced with : " << newName;
175 G4Exception("G4XmlFileManager::CreateFileImpl()",
176 "Analysis_W001", JustWarning, description);
177
178 ntupleFileName = GetNtupleFileName(ntupleDescription);
179 }
180
181 ntupleDescription->fFile = CreateTFile(ntupleFileName);
182
183#ifdef G4VERBOSE
184 if ( fState.GetVerboseL2() ) {
185 fState.GetVerboseL2()->Message("create", "ntuple file", ntupleFileName);
186 }
187#endif
188
189 return (ntupleDescription->fFile != nullptr);
190}
#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 G4XmlFileManager::GetFileType ( ) const
inlinefinalvirtual

Reimplemented from G4BaseFileManager.

Definition at line 62 of file G4XmlFileManager.hh.

62{ return "xml"; }

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

Implements G4VFileManager.

Definition at line 119 of file G4XmlFileManager.cc.

120{
121 // Keep and locks file name
122 fFileName = fileName;
124
125 if ( fFile ) {
126 G4ExceptionDescription description;
127 description << "File " << fileName << " already exists.";
128 G4Exception("G4XmlFileManager::OpenFile()",
129 "Analysis_W001", JustWarning, description);
130 fFile.reset();
131 }
132
133 // Create histograms file (on master)
134 if ( fState.GetIsMaster() ) {
135 // Create file (and save in in the file map (on master only)
136 fFile = CreateTFile(name);
137 if ( ! fFile) {
138 G4ExceptionDescription description;
139 description << "Failed to create file " << fileName;
140 G4Exception("G4XmlFileManager::OpenFile()",
141 "Analysis_W001", JustWarning, description);
142 return false;
143 }
144 }
145
146 fIsOpenFile = true;
147
148 return true;
149}
G4String GetFullFileName(const G4String &baseFileName="", G4bool isPerThread=true) const
std::shared_ptr< std::ofstream > fFile
const char * name(G4int ptype)

◆ WriteFileImpl()

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

Definition at line 96 of file G4XmlFileManager.cc.

97{
98 // Nothing to be done here
99 return true;
100}

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