Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4RootFileManager.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 // $Id: G4RootFileManager.cc 70604 2013-06-03 11:27:06Z ihrivnac $
27 
28 // Author: Ivana Hrivnacova, 15/06/2011 (ivana@ipno.in2p3.fr)
29 
30 #include "G4RootFileManager.hh"
32 #include "G4AnalysisUtilities.hh"
33 
34 #include "tools/wroot/file"
35 #include "tools/zlib"
36 
37 #include <iostream>
38 #include <cstdio>
39 
40 using namespace G4Analysis;
41 
42 //_____________________________________________________________________________
44  : G4VFileManager(state),
45  fFile(nullptr),
46  fHistoDirectory(nullptr),
47  fNtupleDirectory(nullptr),
48  fNofNtupleFiles(0),
49  fNtupleFiles(),
50  fMainNtupleDirectories(),
51  fBasketSize(0)
52 {}
53 
54 //_____________________________________________________________________________
56 {}
57 
58 //
59 // private methods
60 //
61 
62 //_____________________________________________________________________________
63 G4bool G4RootFileManager::OpenNtupleFiles()
64 {
65  auto finalResult = true;
66 
67  for ( auto i = 0; i < fNofNtupleFiles; i++ ) {
68 
69  auto name = GetNtupleFileName(i);
70 
71 #ifdef G4VERBOSE
72  if ( fState.GetVerboseL4() )
74  ->Message("create", "main ntuple file", name);
75 #endif
76 
77  // create new file
78  auto rfile = std::make_shared<tools::wroot::file>(G4cout, name);
79  rfile->add_ziper('Z', tools::compress_buffer);
80  rfile->set_compression(fState.GetCompressionLevel());
81 
82  if ( ! rfile->is_open() ) {
83  G4ExceptionDescription description;
84  description << " " << "Cannot open file " << name;
85  G4Exception("G4RootAnalysisManager::OpenFile()",
86  "Analysis_W001", JustWarning, description);
87  finalResult = false;
88  }
89 
90  // Do not create directory if extra ntuple files
91  // auto result = CreateNtupleDirectory(rfile);
92  // finalResult = finalResult && result;
93  tools::wroot::directory* directory = &rfile->dir();
94  if ( fNtupleDirectoryName != "" ) {
95  directory = rfile->dir().mkdir(fNtupleDirectoryName);
96  if ( ! directory ) {
97  G4ExceptionDescription description;
98  description << " "
99  << "cannot create directory " << fNtupleDirectoryName;
100  G4Exception("G4RootFileManager::OpenNtupleFiles()",
101  "Analysis_W001", JustWarning, description);
102  directory = &fFile->dir();
103  }
104  }
105 
106  fNtupleFiles.push_back(rfile);
107  fMainNtupleDirectories.push_back(directory);
108 
109 #ifdef G4VERBOSE
110  if ( fState.GetVerboseL1() )
112  ->Message("create", "main ntuple file", name);
113 #endif
114 
115  }
116 
117  return finalResult;
118 }
119 
120 //_____________________________________________________________________________
121 G4bool G4RootFileManager::WriteFile(std::shared_ptr<tools::wroot::file> rfile,
122 #ifdef G4VERBOSE
123  const G4String& fileName)
124 #else
125  const G4String& /*fileName*/)
126 #endif
127 {
128  // Do nothing if there is no file
129  if ( ! fIsOpenFile ) return true;
130 
131 #ifdef G4VERBOSE
132  if ( fState.GetVerboseL4() )
133  fState.GetVerboseL4()->Message("write", "file", fileName);
134 #endif
135 
136  unsigned int n;
137  auto result = rfile->write(n);
138 
139 #ifdef G4VERBOSE
140  if ( fState.GetVerboseL1() )
141  fState.GetVerboseL1()->Message("write", "file", fileName, result);
142 #endif
143 
144  return result;
145 }
146 
147 //_____________________________________________________________________________
148 G4bool G4RootFileManager::CloseFile(std::shared_ptr<tools::wroot::file> rfile,
149 #ifdef G4VERBOSE
150  const G4String& fileName)
151 #else
152  const G4String& /*fileName*/)
153 #endif
154 {
155  // Do nothing if there is no file
156  if ( ! fIsOpenFile ) return true;
157 
158 #ifdef G4VERBOSE
159  if ( fState.GetVerboseL4() )
160  fState.GetVerboseL4()->Message("close", "file", fileName);
161 #endif
162 
163  rfile->close();
164 
165 #ifdef G4VERBOSE
166  if ( fState.GetVerboseL1() )
167  fState.GetVerboseL1()->Message("close", "file", fileName, true);
168 #endif
169 
170  return true;
171 }
172 
173 //
174 // public methods
175 //
176 //_____________________________________________________________________________
178 {
179  // Keep file name
180  fFileName = fileName;
181  auto name = GetFullFileName();
182 
183  // delete previous file if exists
184  //if ( fFile ) delete fFile;
185 
186  // create new file
187  fFile = std::make_shared<tools::wroot::file>(G4cout, name);
188  fFile->add_ziper('Z',tools::compress_buffer);
189  fFile->set_compression(fState.GetCompressionLevel());
190 
191  if ( ! fFile->is_open() ) {
192  fFile = nullptr;
193  G4ExceptionDescription description;
194  description << " " << "Cannot open file " << fileName;
195  G4Exception("G4RootAnalysisManager::OpenFile()",
196  "Analysis_W001", JustWarning, description);
197  return false;
198  }
199 
200  // Create directories
201  if ( ! CreateHistoDirectory() ) return false;
202  if ( ! CreateNtupleDirectory() ) return false;
203 
204  // Open ntuple files
205  OpenNtupleFiles();
206 
207  fLockFileName = true;
210 
211  fIsOpenFile = true;
212 
213  return true;
214 }
215 
216 //_____________________________________________________________________________
218 {
219  auto finalResult = true;
220 
221  auto result = WriteFile(fFile, GetFullFileName());
222  finalResult = finalResult && result;
223 
224  auto counter = 0;
225  for ( auto ntupleFile : fNtupleFiles ) {
226  result = WriteFile(ntupleFile, GetNtupleFileName(counter++));
227  finalResult = finalResult && result;
228  }
229  return finalResult;
230 }
231 
232 //_____________________________________________________________________________
234 {
235  auto finalResult = true;
236 
237  auto result = CloseFile(fFile, GetFullFileName());
238  finalResult = finalResult && result;
239 
240  auto counter = 0;
241  for ( auto ntupleFile : fNtupleFiles ) {
242  result = CloseFile(ntupleFile, GetNtupleFileName(counter++));
243  finalResult = finalResult && result;
244  }
245 
246  fLockFileName = false;
247  fIsOpenFile = false;
248 
249  return finalResult;
250 }
251 
252 //_____________________________________________________________________________
254 {
255  if ( fHistoDirectoryName == "" ) {
256  // Do not create a new directory if its name is not set
257  fHistoDirectory = &(fFile->dir());
258  return true;
259  }
260 
261 #ifdef G4VERBOSE
262  if ( fState.GetVerboseL4() )
264  ->Message("create", "directory for histograms", fHistoDirectoryName);
265 #endif
266 
267  fHistoDirectory = fFile->dir().mkdir(fHistoDirectoryName);
268  if ( ! fHistoDirectory ) {
269  G4ExceptionDescription description;
270  description << " "
271  << "cannot create directory " << fHistoDirectoryName;
272  G4Exception("G4RootFileManager::CreateHistoDirectory()",
273  "Analysis_W001", JustWarning, description);
274  return false;
275  }
276 #ifdef G4VERBOSE
277  else {
278  if ( fState.GetVerboseL2() )
280  ->Message("create", "directory for histograms", fHistoDirectoryName);
281  }
282 #endif
283  return true;
284 }
285 
286 //_____________________________________________________________________________
288 {
289  if ( fNtupleDirectoryName == "" ) {
290  // Do not create a new directory if its name is not set
291  fNtupleDirectory = &(fFile->dir());
292  return true;
293  }
294 
295 #ifdef G4VERBOSE
296  if ( fState.GetVerboseL4() )
298  ->Message("create", "directory for ntuples", fNtupleDirectoryName);
299 #endif
300 
301  fNtupleDirectory = fFile->dir().mkdir(fNtupleDirectoryName);
302  if ( ! fNtupleDirectory ) {
303  G4ExceptionDescription description;
304  description << " "
305  << "cannot create directory " << fNtupleDirectoryName;
306  G4Exception("G4RootFileManager::CreateNtupleDirectory()",
307  "Analysis_W001", JustWarning, description);
308  return false;
309  }
310 #ifdef G4VERBOSE
311  else {
312  if ( fState.GetVerboseL2() )
314  ->Message("create", "directory for ntuples", fNtupleDirectoryName);
315  }
316 #endif
317  return true;
318 }
319 
320 //_____________________________________________________________________________
321 std::shared_ptr<tools::wroot::file>
323 {
324  if ( index==0 && ( ! fNtupleFiles.size() ) ) return fFile;
325 
326  if ( index < 0 || index >= G4int(fNtupleFiles.size()) ) {
327  G4String inFunction = "G4RootFileManager::GetNtupleFile()";
328  G4ExceptionDescription description;
329  description << " " << "ntuple file " << index << " does not exist.";
330  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
331  return nullptr;
332  }
333 
334  return fNtupleFiles[index];
335 }
336 
337 //_____________________________________________________________________________
338 tools::wroot::directory*
340 {
341  if ( index==0 && ( ! fMainNtupleDirectories.size() ) ) return fNtupleDirectory;
342 
343  if ( index < 0 || index >= G4int(fMainNtupleDirectories.size()) ) {
344  G4String inFunction = "G4RootFileManager::GetMainNtupleDirectory()";
345  G4ExceptionDescription description;
346  description << " " << "main ntuple directory " << index << " does not exist.";
347  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
348  return nullptr;
349  }
350 
351  return fMainNtupleDirectories[index];
352 }
353 
G4double G4ParticleHPJENDLHEData::G4double result
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
const XML_Char * name
Definition: expat.h:151
const G4AnalysisManagerState & fState
G4bool fLockHistoDirectoryName
G4String fNtupleDirectoryName
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4String GetNtupleFileName(const G4String &ntupleName) const
virtual G4bool CloseFile() final
virtual G4bool WriteFile() final
G4String fHistoDirectoryName
int G4int
Definition: G4Types.hh:78
const G4AnalysisVerbose * GetVerboseL2() const
std::shared_ptr< tools::wroot::file > GetNtupleFile(G4int index) const
G4RootFileManager(const G4AnalysisManagerState &state)
G4GLOB_DLL std::ostream G4cout
virtual G4bool OpenFile(const G4String &fileName) final
const G4AnalysisVerbose * GetVerboseL4() const
virtual ~G4RootFileManager()
bool G4bool
Definition: G4Types.hh:79
const G4int n
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4bool fLockNtupleDirectoryName
tools::wroot::directory * GetMainNtupleDirectory(G4int index) const
G4String GetFullFileName(const G4String &baseFileName="", G4bool isPerThread=true) const
const G4AnalysisVerbose * GetVerboseL1() const