Geant4-11
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 (ivana@ipno.in2p3.fr)
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
45}
46
47//_____________________________________________________________________________
50{
51 // Create helpers defined in the base class
52 fH1FileManager = std::make_shared<G4Hdf5HnFileManager<histo::h1d>>(this);
53 fH2FileManager = std::make_shared<G4Hdf5HnFileManager<histo::h2d>>(this);
54 fH3FileManager = std::make_shared<G4Hdf5HnFileManager<histo::h3d>>(this);
55 fP1FileManager = std::make_shared<G4Hdf5HnFileManager<histo::p1d>>(this);
56 fP2FileManager = std::make_shared<G4Hdf5HnFileManager<histo::p2d>>(this);
57}
58
59//
60// private methods
61//
62
63//_____________________________________________________________________________
65 const G4String& directoryName, const G4String& objectType)
66{
67// Method for both histograms and ntuples directories.
68
69 // return if no file provided
70 if ( file < 0 ) return false;
71
72 // use default directory name if not provided
73 auto newDirectoryName = directoryName;
74 if ( newDirectoryName == "" ) {
75 newDirectoryName = fgkDefaultDirectoryName;
76 newDirectoryName += "_";
77 newDirectoryName += objectType;
78 }
79
80 Message(kVL4, "create", "directory for " + objectType, newDirectoryName);
81
82 auto success = true;
83
84 // create directory
85 auto directory = tools_H5Gcreate(file, newDirectoryName, 0);
86 // 0 seems to be an optional parameter. The web doc does not say what should
87 // be the default value but 0 is what is found in examples, and in the code, if we pass 0, clearly some
88 // default value is taken.
89 if ( directory < 0 ) {
90 Warn("Cannot create directory " + directoryName,
91 fkClass, "CreateDirectory");
92 success = false;
93 }
94 else {
95 // write atb (header?)
96 auto result = hdf5::write_atb(directory, "type", "directory");
97 if ( !result) {
98 Warn("Write_atb class failed for " + directoryName,
99 fkClass, "CreateDirectory");
100 success = false;
101 }
102 }
103
104 Message(kVL2, "create", "directory for " + objectType, newDirectoryName, success);
105
106 return directory;
107}
108
109//_____________________________________________________________________________
111{
112 // get ntuple file name
113 auto ntupleFileName = ntupleDescription->fFileName;
114 if ( ntupleFileName.size() ) {
115 // update filename per object per thread
116 ntupleFileName = GetTnFileName(ntupleFileName, GetFileType());
117 } else {
118 // get default file name
119 ntupleFileName = GetFullFileName();
120 }
121 return ntupleFileName;
122}
123
124//
125// protected methods
126//
127
128//_____________________________________________________________________________
129std::shared_ptr<G4Hdf5File> G4Hdf5FileManager::CreateFileImpl(const G4String& fileName)
130{
131 // create a new file
132 hid_t file = ::H5Fcreate(fileName, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
133
134 // Do nothing if there is no file
135 // (the error should be handled by caller)
136 if ( file < 0 ) {
137 Warn("::H5Fcreate failed " + fileName, fkClass, "CreateFileImpl");
138 return std::make_shared<G4Hdf5File>(-1, -1, -1);
139 }
140
141 // create a header with general infos
142 if(!tools::hdf5::write_header(file)) {
143 Warn("tools::hdf5::write_header() failed for " + fileName,
144 fkClass, "CreateFileImpl");
145 return std::make_shared<G4Hdf5File>(-1, -1, -1);
146 }
147
148 // create histo directory
149 auto hdirectory
150 = CreateDirectory(file, fHistoDirectoryName, "histograms");
151 if ( hdirectory < 0 ) {
152 // Warning is issued in CreateDirectory
153 return std::make_shared<G4Hdf5File>(-1, -1, -1);
154 }
155
156 // create ntuple directory
157 auto ndirectory
159 if ( ndirectory < 0 ) {
160 // Warnin is issued in CreateDirectory
161 return std::make_shared<G4Hdf5File>(-1, -1, -1);
162 }
163
164 return std::make_shared<G4Hdf5File>(file, hdirectory, ndirectory);
165}
166
167//_____________________________________________________________________________
168G4bool G4Hdf5FileManager::WriteFileImpl(std::shared_ptr<G4Hdf5File> /*file*/)
169{
170 // Nothing to be done here
171 return true;
172}
173
174//_____________________________________________________________________________
175G4bool G4Hdf5FileManager::CloseFileImpl(std::shared_ptr<G4Hdf5File> file)
176{
177 if ( ! file ) return false;
178
180
181 ::H5Gclose(std::get<1>(*file));
182 ::H5Gclose(std::get<2>(*file));
183 ::H5Fclose(std::get<0>(*file));
184
185 lock.unlock();
186
187 return true;
188}
189
190//
191// public methods
192//
193
194//_____________________________________________________________________________
196{
197 // Keep file name
198 fFileName = fileName;
199 auto name = GetFullFileName();
200
201 if ( fFile ) {
202 Warn("File " + fileName + " already exists.", fkClass, "OpenFile");
203 fFile.reset();
204 }
205
206 // create new file
208 if ( ! fFile ) {
209 Warn("Failed to create file " + fileName, fkClass, "OpenFile");
210 return false;
211 }
212
214 fIsOpenFile = true;
215
216 return true;
217}
218
219//_____________________________________________________________________________
221 Hdf5NtupleDescription* ntupleDescription)
222{
223 // get ntuple file name per object
224 auto ntupleFileName = GetNtupleFileName(ntupleDescription);
225
226 auto file = GetTFile(ntupleFileName, false);
227 if (! file) {
228 file = CreateTFile(ntupleFileName);
229 }
230 ntupleDescription->fFile = file;
231
232 return (ntupleDescription->fFile != nullptr);
233}
234
235//_____________________________________________________________________________
237 Hdf5NtupleDescription* ntupleDescription)
238{
239 // Do nothing if there is no file
240 if ( ntupleDescription->fFile == nullptr ) return true;
241
242 // Ntuple files will be closed with CloseFiles() calls
243 ntupleDescription->fFile.reset();
244
245 // Notify not empty file
246 auto ntupleFileName = GetNtupleFileName(ntupleDescription);
247 auto result = SetIsEmpty(ntupleFileName, ! ntupleDescription->fHasFill);
248
249 return result;
250}
251
252//_____________________________________________________________________________
254{
255 if ( ! fFile ) return kInvalidId;
256
257 return std::get<1>(*fFile);
258}
259
260//_____________________________________________________________________________
262{
263 if ( ! fFile ) return kInvalidId;
264
265 return std::get<2>(*fFile);
266}
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
G4String GetFullFileName(const G4String &baseFileName="", G4bool isPerThread=true) const
void Message(G4int level, const G4String &action, const G4String &objectType, const G4String &objectName="", G4bool success=true) const
G4String GetNtupleFileName(Hdf5NtupleDescription *ntupleDescription)
virtual G4bool CloseFileImpl(std::shared_ptr< G4Hdf5File > file) final
hid_t CreateDirectory(hid_t &file, const G4String &directoryName, const G4String &objectType)
static constexpr std::string_view fkClass
virtual G4String GetFileType() const final
hid_t GetNtupleDirectory() const
virtual G4bool OpenFile(const G4String &fileName) final
G4bool CreateNtupleFile(Hdf5NtupleDescription *ntupleDescription)
static const G4String fgkDefaultDirectoryName
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
G4Hdf5FileManager()=delete
std::shared_ptr< G4Hdf5File > GetTFile(const G4String &fileName, G4bool warn=true) const
std::shared_ptr< G4Hdf5File > 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
void LockDirectoryNames()
std::shared_ptr< G4VTHnFileManager< tools::histo::h2d > > fH2FileManager
G4String fHistoDirectoryName
virtual G4bool SetIsEmpty(const G4String &fileName, G4bool isEmpty) final
std::shared_ptr< G4Hdf5File > fFile
constexpr G4int kVL2
G4String GetTnFileName(const G4String &fileName, const G4String &fileType)
constexpr G4int kVL4
constexpr G4int kInvalidId
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)
const char * name(G4int ptype)
std::shared_ptr< FT > fFile