Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4XmlFileManager.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26
27// Author: Ivana Hrivnacova, 18/06/2013 ([email protected])
28
29#include "G4XmlFileManager.hh"
30#include "G4XmlHnFileManager.hh"
33
34#include "tools/waxml/begend"
35
36using namespace G4Analysis;
37using namespace tools;
38
39//_____________________________________________________________________________
41 : G4VTFileManager<std::ofstream>(state)
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}
50
51//_____________________________________________________________________________
53{}
54
55//
56// private methods
57//
58
59//_____________________________________________________________________________
60G4String G4XmlFileManager::GetNtupleFileName(XmlNtupleDescription* ntupleDescription)
61{
62 // get ntuple file name
63 auto ntupleFileName = ntupleDescription->fFileName;
64 if ( ntupleFileName.size() ) {
65 // update filename per object per thread
66 ntupleFileName = GetTnFileName(ntupleFileName, GetFileType());
67 } else {
68 // compose ntuple file name from the default file name
69 ntupleFileName = GetNtupleFileName(ntupleDescription->fNtupleBooking.name());
70 }
71 return ntupleFileName;
72}
73
74//
75// protected methods
76//
77
78//_____________________________________________________________________________
79std::shared_ptr<std::ofstream> G4XmlFileManager::CreateFileImpl(const G4String& fileName)
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}
94
95//_____________________________________________________________________________
96G4bool G4XmlFileManager::WriteFileImpl(std::shared_ptr<std::ofstream> /*file*/)
97{
98 // Nothing to be done here
99 return true;
100}
101
102//_____________________________________________________________________________
103G4bool G4XmlFileManager::CloseFileImpl(std::shared_ptr<std::ofstream> file)
104{
105 if ( ! file ) return false;
106
107 // close file
108 waxml::end(*file);
109 file->close();
110
111 return true;
112}
113
114//
115// public methods
116//
117
118//_____________________________________________________________________________
120{
121 // Keep and locks file name
122 fFileName = fileName;
123 auto name = GetFullFileName(fFileName);
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}
150
151//_____________________________________________________________________________
153 XmlNtupleDescription* ntupleDescription)
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}
191
192//_____________________________________________________________________________
194 XmlNtupleDescription* ntupleDescription)
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}
@ 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
bool G4bool
Definition: G4Types.hh:86
#define G4endl
Definition: G4ios.hh:57
const G4AnalysisVerbose * GetVerboseL2() const
const G4AnalysisVerbose * GetVerboseL4() const
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
G4String GetFullFileName(const G4String &baseFileName="", G4bool isPerThread=true) const
const G4AnalysisManagerState & fState
G4bool CloseTFile(const G4String &fileName)
std::shared_ptr< FT > GetTFile(const G4String &fileName, G4bool warn=true) const
std::shared_ptr< FT > CreateTFile(const G4String &fileName)
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
virtual G4bool SetIsEmpty(const G4String &fileName, G4bool isEmpty) final
std::shared_ptr< std::ofstream > fFile
virtual G4bool OpenFile(const G4String &fileName) final
virtual G4bool CloseFileImpl(std::shared_ptr< std::ofstream > file) final
G4XmlFileManager(const G4AnalysisManagerState &state)
virtual std::shared_ptr< std::ofstream > CreateFileImpl(const G4String &fileName) final
virtual G4bool WriteFileImpl(std::shared_ptr< std::ofstream > file) final
G4bool CloseNtupleFile(XmlNtupleDescription *ntupleDescription)
virtual G4String GetFileType() const final
G4bool CreateNtupleFile(XmlNtupleDescription *ntupleDescription)
G4String GetExtension(const G4String &fileName, const G4String &defaultExtension="")
G4String GetTnFileName(const G4String &fileName, const G4String &fileType)
G4String GetBaseName(const G4String &fileName)
std::shared_ptr< TF > fFile
tools::ntuple_booking fNtupleBooking