Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
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 // $Id$
27 
28 // Author: Ivana Hrivnacova, 20/07/2017 (ivana@ipno.in2p3.fr)
29 
30 #include "G4Hdf5FileManager.hh"
32 #include "G4AnalysisUtilities.hh"
33 
34 #include "tools/hdf5/h2file"
35 
36 using namespace G4Analysis;
37 
38 //_____________________________________________________________________________
40 
41 //_____________________________________________________________________________
43  : G4VFileManager(state),
44  fFile(kInvalidId),
45  fHistoDirectory(kInvalidId),
46  fNtupleDirectory(kInvalidId),
47  fBasketSize(0) // TO DO: check default value !! (433 in test)
48 {}
49 
50 //_____________________________________________________________________________
52 {}
53 
54 //
55 // private methods
56 //
57 
58 //_____________________________________________________________________________
60  const G4String& directoryName, hid_t& directory)
61 {
62 // Method for both histograms and ntuples directories.
63 // For histos: CreateDirectory("histograms", fHistoDirectoryName, fHistoDirectory)
64 // For ntples: CreateDirectory("ntuples", fNtupleDirectoryName, fNtupleDirectory)
65 
66  auto newDirectoryName = directoryName;
67 
68  if ( newDirectoryName == "" ) {
69  // if ( fDefaultDirectory > 0 ) {
70  // // Return the default directory if the name is not set and the default directory
71  // // already exists
72  // directory = fDefaultDirectory;
73  // return true;
74  // } else {
75  // Create the default directory if the name is not set and the default directory
76  // does not yet exist
77  newDirectoryName = fgkDefaultDirectoryName;
78  newDirectoryName += "_";
79  newDirectoryName += directoryType;
80  }
81 
82 #ifdef G4VERBOSE
83  if ( fState.GetVerboseL4() ) {
84  G4String message = "directory for ";
85  message += directoryType;
86  fState.GetVerboseL4()->Message("create", message, newDirectoryName);
87  }
88 #endif
89 
90  directory = tools_H5Gcreate(fFile, newDirectoryName, 0);
91  // 0 seems to be an optional parameter. The web doc does not say what should
92  // be the default value but 0 is what is found in examples, and in the code, if we pass 0, clearly some
93  // default value is taken.
94 
95  if ( directory < 0 ) {
96  G4ExceptionDescription description;
97  description << " "
98  << "cannot create directory " << directoryName;
99  G4Exception("G4Hdf5FileManager::CreateDirectory()",
100  "Analysis_W001", JustWarning, description);
101  return false;
102  }
103 #ifdef G4VERBOSE
104  else {
105  if ( fState.GetVerboseL2() ) {
106  G4String message = "directory for ";
107  message += directoryType;
108  fState.GetVerboseL2()->Message("create", message, newDirectoryName);
109  }
110  }
111 #endif
112  return true;
113 }
114 
115 //_____________________________________________________________________________
116 #ifdef G4VERBOSE
118  const G4String& directoryName, hid_t& directory)
119 #else
121  const G4String& directoryName, hid_t& directory)
122 #endif
123 {
124 #ifdef G4VERBOSE
125  if ( fState.GetVerboseL4() ) {
126  G4String message = "directory for ";
127  message += directoryType;
128  fState.GetVerboseL4()
129  ->Message("write", message, directoryName);
130  }
131 #endif
132 
133  auto result
134  = tools::hdf5::write_atb(directory, "type", "directory");
135 
136  if ( ! result ) {
137  G4ExceptionDescription description;
138  description << " "
139  << "cannot write directory " << directoryName;
140  G4Exception("G4Hdf5FileManager::WriteDirectory()",
141  "Analysis_W001", JustWarning, description);
142  // close
143  // ::H5Gclose(histos);
144  // ::H5Fclose(file);
145 
146  return false;
147  }
148 #ifdef G4VERBOSE
149  else {
150  if ( fState.GetVerboseL2() ) {
151  G4String message = "directory for ";
152  message += directoryType;
153  fState.GetVerboseL2()->Message("write", message, fHistoDirectoryName);
154  }
155  }
156 #endif
157 
158  return result;
159 }
160 
161 //
162 // public methods
163 //
164 
165 //_____________________________________________________________________________
167 {
168  // Keep file name
169  fFileName = fileName;
170  auto name = GetFullFileName();
171 
172  // delete previous file if exists
173  //if ( fFile ) delete fFile;
174 
175  // create new file
176  fFile = ::H5Fcreate(name, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
177  if ( fFile < 0 ) {
178  G4ExceptionDescription description;
179  description << " " << "Cannot open file " << fileName;
180  G4Exception("G4Hdf5AnalysisManager::OpenFile()",
181  "Analysis_W001", JustWarning, description);
182  return false;
183  }
184 
185  // Create directories
186  if ( ! CreateDirectory("histograms", fHistoDirectoryName, fHistoDirectory) ) return false;
187  if ( ! CreateDirectory("ntuples", fNtupleDirectoryName, fNtupleDirectory) ) return false;
188 
189  // // Open ntuple files
190  // OpenNtupleFiles();
191 
192  // Write directories
193  if ( ! WriteHistoDirectory() ) return false;
194  if ( ! WriteNtupleDirectory() ) return false;
195 
196  fLockFileName = true;
199 
200  fIsOpenFile = true;
201 
202  return true;
203 }
204 
205 //_____________________________________________________________________________
207 {
208  // Nothing to be done here
209  return true;
210 }
211 
212 //_____________________________________________________________________________
214 {
215  // Do nothing if there is no file
216  if ( fFile < 0 ) return true;
217 
218 #ifdef G4VERBOSE
219  if ( fState.GetVerboseL4() )
220  fState.GetVerboseL4()->Message("close", "file", GetFullFileName());
221 #endif
222 
223  if ( fHistoDirectory >= 0 ) {
224  ::H5Gclose(fHistoDirectory);
225  }
226  if ( fNtupleDirectory >= 0 ) {
227  ::H5Gclose(fNtupleDirectory);
228  }
229  ::H5Fclose(fFile);
230 
231  fLockFileName = false;
232  fIsOpenFile = false;
233 
234 #ifdef G4VERBOSE
235  if ( fState.GetVerboseL1() )
236  fState.GetVerboseL1()->Message("close", "file", GetFullFileName(), true);
237 #endif
238 
239  // CHECK
240  // the result not returned from hdf5
241 
242  return true;
243 }
244 
245 //_____________________________________________________________________________
247 {
248  return WriteDirectory("histograms", fHistoDirectoryName, fHistoDirectory);
249 }
250 
251 //_____________________________________________________________________________
253 {
255 }
256 
257 //_____________________________________________________________________________
259 {
260  if ( fHistoDirectory >= 0 ) {
261  ::H5Gclose(fHistoDirectory);
262  }
263  ::H5Fclose(fFile);
264 }
265 
266 
267 
const XML_Char * name
Definition: expat.h:151
const G4AnalysisVerbose * GetVerboseL2() const
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:45
void message(RunManager *runmanager)
Definition: ts_scorers.cc:72
G4bool fLockHistoDirectoryName
static const G4String fgkDefaultDirectoryName
G4String GetFullFileName(const G4String &baseFileName="", G4bool isPerThread=true) const
G4bool fLockNtupleDirectoryName
G4bool WriteDirectory(const G4String &directoryType, const G4String &directoryName, hid_t &directory)
G4bool CreateDirectory(const G4String &directoryType, const G4String &directoryName, hid_t &directory)
bool G4bool
Definition: G4Types.hh:79
G4String fHistoDirectoryName
const G4AnalysisVerbose * GetVerboseL4() const
const G4int kInvalidId
const G4AnalysisManagerState & fState
G4double G4ParticleHPJENDLHEData::G4double result
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.hh:65
G4Hdf5FileManager(const G4AnalysisManagerState &state)
G4String fNtupleDirectoryName
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
virtual G4bool OpenFile(const G4String &fileName) final
virtual G4bool CloseFile() final
virtual G4bool WriteFile() final
const G4AnalysisVerbose * GetVerboseL1() const