ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4Hdf5FileManager.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4Hdf5FileManager.cc
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"
31 #include "G4AnalysisUtilities.hh"
32 
33 #include "tools/hdf5/h2file"
34 
35 using namespace G4Analysis;
36 
37 //_____________________________________________________________________________
39 
40 //_____________________________________________________________________________
42  : G4VFileManager(state),
43  fFile(kInvalidId),
44  fHistoDirectory(kInvalidId),
45  fNtupleDirectory(kInvalidId),
46  fBasketSize(0) // TO DO: check default value !! (433 in test)
47 {}
48 
49 //_____________________________________________________________________________
51 {}
52 
53 //
54 // private methods
55 //
56 
57 //_____________________________________________________________________________
59  const G4String& directoryName, hid_t& directory)
60 {
61 // Method for both histograms and ntuples directories.
62 // For histos: CreateDirectory("histograms", fHistoDirectoryName, fHistoDirectory)
63 // For ntples: CreateDirectory("ntuples", fNtupleDirectoryName, fNtupleDirectory)
64 
65  auto newDirectoryName = directoryName;
66 
67  if ( newDirectoryName == "" ) {
68  // if ( fDefaultDirectory > 0 ) {
69  // // Return the default directory if the name is not set and the default directory
70  // // already exists
71  // directory = fDefaultDirectory;
72  // return true;
73  // } else {
74  // Create the default directory if the name is not set and the default directory
75  // does not yet exist
76  newDirectoryName = fgkDefaultDirectoryName;
77  newDirectoryName += "_";
78  newDirectoryName += directoryType;
79  }
80 
81 #ifdef G4VERBOSE
82  if ( fState.GetVerboseL4() ) {
83  G4String message = "directory for ";
84  message += directoryType;
85  fState.GetVerboseL4()->Message("create", message, newDirectoryName);
86  }
87 #endif
88 
89  directory = tools_H5Gcreate(fFile, newDirectoryName, 0);
90  // 0 seems to be an optional parameter. The web doc does not say what should
91  // be the default value but 0 is what is found in examples, and in the code, if we pass 0, clearly some
92  // default value is taken.
93 
94  if ( directory < 0 ) {
95  G4ExceptionDescription description;
96  description << " "
97  << "cannot create directory " << directoryName;
98  G4Exception("G4Hdf5FileManager::CreateDirectory()",
99  "Analysis_W001", JustWarning, description);
100  return false;
101  }
102 #ifdef G4VERBOSE
103  else {
104  if ( fState.GetVerboseL2() ) {
105  G4String message = "directory for ";
106  message += directoryType;
107  fState.GetVerboseL2()->Message("create", message, newDirectoryName);
108  }
109  }
110 #endif
111  return true;
112 }
113 
114 //_____________________________________________________________________________
115 #ifdef G4VERBOSE
117  const G4String& directoryName, hid_t& directory)
118 #else
120  const G4String& directoryName, hid_t& directory)
121 #endif
122 {
123 #ifdef G4VERBOSE
124  if ( fState.GetVerboseL4() ) {
125  G4String message = "directory for ";
126  message += directoryType;
127  fState.GetVerboseL4()
128  ->Message("write", message, directoryName);
129  }
130 #endif
131 
132  auto result
133  = tools::hdf5::write_atb(directory, "type", "directory");
134 
135  if ( ! result ) {
136  G4ExceptionDescription description;
137  description << " "
138  << "cannot write directory " << directoryName;
139  G4Exception("G4Hdf5FileManager::WriteDirectory()",
140  "Analysis_W001", JustWarning, description);
141  // close
142  // ::H5Gclose(histos);
143  // ::H5Fclose(file);
144 
145  return false;
146  }
147 #ifdef G4VERBOSE
148  else {
149  if ( fState.GetVerboseL2() ) {
150  G4String message = "directory for ";
151  message += directoryType;
152  fState.GetVerboseL2()->Message("write", message, fHistoDirectoryName);
153  }
154  }
155 #endif
156 
157  return result;
158 }
159 
160 //
161 // public methods
162 //
163 
164 //_____________________________________________________________________________
166 {
167  // Keep file name
168  fFileName = fileName;
169  auto name = GetFullFileName();
170 
171  // delete previous file if exists
172  //if ( fFile ) delete fFile;
173 
174  // create new file
175  fFile = ::H5Fcreate(name, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
176  if ( fFile < 0 ) {
177  G4ExceptionDescription description;
178  description << " " << "Cannot open file " << fileName;
179  G4Exception("G4Hdf5AnalysisManager::OpenFile()",
180  "Analysis_W001", JustWarning, description);
181  return false;
182  }
183 
184  // Create directories
185  if ( ! CreateDirectory("histograms", fHistoDirectoryName, fHistoDirectory) ) return false;
186  if ( ! CreateDirectory("ntuples", fNtupleDirectoryName, fNtupleDirectory) ) return false;
187 
188  // // Open ntuple files
189  // OpenNtupleFiles();
190 
191  // Write directories
192  if ( ! WriteHistoDirectory() ) return false;
193  if ( ! WriteNtupleDirectory() ) return false;
194 
195  fLockFileName = true;
198 
199  fIsOpenFile = true;
200 
201  return true;
202 }
203 
204 //_____________________________________________________________________________
206 {
207  // Nothing to be done here
208  return true;
209 }
210 
211 //_____________________________________________________________________________
213 {
214  // Do nothing if there is no file
215  if ( fFile < 0 ) return true;
216 
217 #ifdef G4VERBOSE
218  if ( fState.GetVerboseL4() )
219  fState.GetVerboseL4()->Message("close", "file", GetFullFileName());
220 #endif
221 
222  if ( fHistoDirectory >= 0 ) {
223  ::H5Gclose(fHistoDirectory);
224  }
225  if ( fNtupleDirectory >= 0 ) {
226  ::H5Gclose(fNtupleDirectory);
227  }
228  ::H5Fclose(fFile);
229 
230  fLockFileName = false;
231  fIsOpenFile = false;
232 
233 #ifdef G4VERBOSE
234  if ( fState.GetVerboseL1() )
235  fState.GetVerboseL1()->Message("close", "file", GetFullFileName(), true);
236 #endif
237 
238  // CHECK
239  // the result not returned from hdf5
240 
241  return true;
242 }
243 
244 //_____________________________________________________________________________
246 {
247  return WriteDirectory("histograms", fHistoDirectoryName, fHistoDirectory);
248 }
249 
250 //_____________________________________________________________________________
252 {
254 }
255 
256 //_____________________________________________________________________________
258 {
259  if ( fHistoDirectory >= 0 ) {
260  ::H5Gclose(fHistoDirectory);
261  }
262  ::H5Fclose(fFile);
263 }
264 
265 
266