Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4Hdf5FileManager.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, 20/07/2017 ([email protected])
28
29#include "G4Hdf5FileManager.hh"
33#include "G4AutoLock.hh"
34
35#include "tools/hdf5/h2file"
36
37using namespace G4Analysis;
38using namespace tools;
39
40//using namespace G4Analysis;
41
42namespace {
43 //Mutex to lock master manager when closing a file
44 G4Mutex closeFileMutex = G4MUTEX_INITIALIZER;
45}
46
47//_____________________________________________________________________________
48const G4String G4Hdf5FileManager::fgkDefaultDirectoryName = "default";
49
50//_____________________________________________________________________________
53 fBasketSize(0) // TO DO: check default value !! (433 in test)
54{
55 // Create helpers defined in the base class
56 fH1FileManager = std::make_shared<G4Hdf5HnFileManager<histo::h1d>>(this);
57 fH2FileManager = std::make_shared<G4Hdf5HnFileManager<histo::h2d>>(this);
58 fH3FileManager = std::make_shared<G4Hdf5HnFileManager<histo::h3d>>(this);
59 fP1FileManager = std::make_shared<G4Hdf5HnFileManager<histo::p1d>>(this);
60 fP2FileManager = std::make_shared<G4Hdf5HnFileManager<histo::p2d>>(this);
61}
62
63//_____________________________________________________________________________
65{}
66
67//
68// private methods
69//
70
71//_____________________________________________________________________________
72hid_t G4Hdf5FileManager::CreateDirectory(hid_t& file,
73 const G4String& directoryName, const G4String& objectType)
74{
75// Method for both histograms and ntuples directories.
76
77 // return if no file provided
78 if ( file < 0 ) return false;
79
80 // use default directory name if not provided
81 auto newDirectoryName = directoryName;
82 if ( newDirectoryName == "" ) {
83 newDirectoryName = fgkDefaultDirectoryName;
84 newDirectoryName += "_";
85 newDirectoryName += objectType;
86 }
87
88#ifdef G4VERBOSE
89 if ( fState.GetVerboseL4() ) {
91 ->Message("create", "directory for " + objectType, newDirectoryName);
92 }
93#endif
94
95 auto success = true;
96
97 // create directory
98 auto directory = tools_H5Gcreate(file, newDirectoryName, 0);
99 // 0 seems to be an optional parameter. The web doc does not say what should
100 // be the default value but 0 is what is found in examples, and in the code, if we pass 0, clearly some
101 // default value is taken.
102 if ( directory < 0 ) {
103 G4ExceptionDescription description;
104 description << " "
105 << "cannot create directory " << directoryName;
106 G4Exception("G4Hdf5FileManager::CreateDirectory()",
107 "Analysis_W001", JustWarning, description);
108 success = false;
109 }
110 else {
111 // write atb (header?)
112 auto result = hdf5::write_atb(directory, "type", "directory");
113 if ( !result) {
114 G4ExceptionDescription description;
115 description << " "
116 << "write_atb class failed for " << directoryName;
117 G4Exception("G4Hdf5FileManager::CreateDirectory()",
118 "Analysis_W001", JustWarning, description);
119 success = false;
120 }
121 }
122
123#ifdef G4VERBOSE
124 if ( fState.GetVerboseL2() ) {
126 ->Message("create", "directory for " + objectType, newDirectoryName, success);
127 }
128#endif
129 return directory;
130}
131
132//_____________________________________________________________________________
133G4String G4Hdf5FileManager::GetNtupleFileName(Hdf5NtupleDescription* ntupleDescription)
134{
135 // get ntuple file name
136 auto ntupleFileName = ntupleDescription->fFileName;
137 if ( ntupleFileName.size() ) {
138 // update filename per object per thread
139 ntupleFileName = GetTnFileName(ntupleFileName, GetFileType());
140 } else {
141 // get default file name
142 ntupleFileName = GetFullFileName();
143 }
144 return ntupleFileName;
145}
146
147//
148// protected methods
149//
150
151//_____________________________________________________________________________
152std::shared_ptr<G4Hdf5File> G4Hdf5FileManager::CreateFileImpl(const G4String& fileName)
153{
154 // create a new file
155 hid_t file = ::H5Fcreate(fileName, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
156
157 // Do nothing if there is no file
158 // (the error should be handled by caller)
159 if ( file < 0 ) {
160 G4ExceptionDescription description;
161 description << " " << "::H5Fcreate failed " << fileName;
162 G4Exception("G4Hdf5AnalysisManager::CreateFileImpl()",
163 "Analysis_W001", JustWarning, description);
164 return std::make_shared<G4Hdf5File>(-1, -1, -1);
165 }
166
167 // create a header with general infos
168 if(!tools::hdf5::write_header(file)) {
169 G4ExceptionDescription description;
170 description << " " << "tools::hdf5::write_header() failed for " << fileName;
171 G4Exception("G4Hdf5AnalysisManager::CreateFileImpl()",
172 "Analysis_W001", JustWarning, description);
173 return std::make_shared<G4Hdf5File>(-1, -1, -1);
174 }
175
176 // create histo directory
177 auto hdirectory
178 = CreateDirectory(file, fHistoDirectoryName, "histograms");
179 if ( hdirectory < 0 ) {
180 // Warnin is issued in CreateDirectory
181 return std::make_shared<G4Hdf5File>(-1, -1, -1);
182 }
183
184 // create ntuple directory
185 auto ndirectory
186 = CreateDirectory(file, fNtupleDirectoryName, "ntuples");
187 if ( ndirectory < 0 ) {
188 // Warnin is issued in CreateDirectory
189 return std::make_shared<G4Hdf5File>(-1, -1, -1);
190 }
191
192 return std::make_shared<G4Hdf5File>(file, hdirectory, ndirectory);
193}
194
195//_____________________________________________________________________________
196G4bool G4Hdf5FileManager::WriteFileImpl(std::shared_ptr<G4Hdf5File> /*file*/)
197{
198 // Nothing to be done here
199 return true;
200}
201
202//_____________________________________________________________________________
203G4bool G4Hdf5FileManager::CloseFileImpl(std::shared_ptr<G4Hdf5File> file)
204{
205 if ( ! file ) return false;
206
207 G4AutoLock lock(&closeFileMutex);
208
209 ::H5Gclose(std::get<1>(*file));
210 ::H5Gclose(std::get<2>(*file));
211 ::H5Fclose(std::get<0>(*file));
212
213 lock.unlock();
214
215 return true;
216}
217
218//
219// public methods
220//
221
222//_____________________________________________________________________________
224{
225 // Keep file name
226 fFileName = fileName;
227 auto name = GetFullFileName();
228
229 if ( fFile ) {
230 G4ExceptionDescription description;
231 description
232 << "File " << fileName << " already exists.";
233 G4Exception("G4Hdf5FileManager::OpenFile()",
234 "Analysis_W001", JustWarning, description);
235 fFile.reset();
236 }
237
238 // create new file
239 fFile = CreateTFile(name);
240 if ( ! fFile ) {
241 G4ExceptionDescription description;
242 description << "Failed to create file " << fileName;
243 G4Exception("G4Hdf5FileManager::OpenFile()",
244 "Analysis_W001", JustWarning, description);
245 return false;
246 }
247
248 fLockDirectoryNames = true;
249 fIsOpenFile = true;
250
251 return true;
252}
253
254//_____________________________________________________________________________
256 Hdf5NtupleDescription* ntupleDescription)
257{
258 // get ntuple file name per object
259 auto ntupleFileName = GetNtupleFileName(ntupleDescription);
260
261 auto file = GetTFile(ntupleFileName, false);
262 if (! file) {
263 file = CreateTFile(ntupleFileName);
264 }
265 ntupleDescription->fFile = file;
266
267 return (ntupleDescription->fFile != nullptr);
268}
269
270//_____________________________________________________________________________
272 Hdf5NtupleDescription* ntupleDescription)
273{
274 // Do nothing if there is no file
275 if ( ! ntupleDescription->fFile ) return true;
276
277 // Ntuple files will be closed with CloseFiles() calls
278 ntupleDescription->fFile.reset();
279
280 // Notify not empty file
281 auto ntupleFileName = GetNtupleFileName(ntupleDescription);
282 auto result = SetIsEmpty(ntupleFileName, ! ntupleDescription->fHasFill);
283
284 return result;
285}
286
287//_____________________________________________________________________________
289{
290 if ( ! fFile ) return kInvalidId;
291
292 return std::get<1>(*fFile);
293}
294
295//_____________________________________________________________________________
297{
298 if ( ! fFile ) return kInvalidId;
299
300 return std::get<2>(*fFile);
301}
@ 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
std::tuple< hid_t, hid_t, hid_t > G4Hdf5File
#define G4MUTEX_INITIALIZER
Definition: G4Threading.hh:85
std::mutex G4Mutex
Definition: G4Threading.hh:81
bool G4bool
Definition: G4Types.hh:86
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
virtual G4bool CloseFileImpl(std::shared_ptr< G4Hdf5File > file) final
virtual G4String GetFileType() const final
hid_t GetNtupleDirectory() const
virtual G4bool OpenFile(const G4String &fileName) final
G4bool CreateNtupleFile(Hdf5NtupleDescription *ntupleDescription)
G4Hdf5FileManager(const G4AnalysisManagerState &state)
virtual std::shared_ptr< G4Hdf5File > CreateFileImpl(const G4String &fileName) final
hid_t GetHistoDirectory() const
G4bool CloseNtupleFile(Hdf5NtupleDescription *ntupleDescription)
virtual G4bool WriteFileImpl(std::shared_ptr< G4Hdf5File > file) final
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
G4String fNtupleDirectoryName
std::shared_ptr< G4VTHnFileManager< tools::histo::p2d > > fP2FileManager
G4bool fLockDirectoryNames
std::shared_ptr< G4VTHnFileManager< tools::histo::h2d > > fH2FileManager
G4String fHistoDirectoryName
virtual G4bool SetIsEmpty(const G4String &fileName, G4bool isEmpty) final
std::shared_ptr< G4Hdf5File > fFile
G4String GetTnFileName(const G4String &fileName, const G4String &fileType)
const G4int kInvalidId
std::shared_ptr< TF > fFile