Geant4  10.03.p01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 #ifdef G4VERBOSE
129  if ( fState.GetVerboseL4() )
130  fState.GetVerboseL4()->Message("write", "file", fileName);
131 #endif
132 
133  unsigned int n;
134  auto result = rfile->write(n);
135 
136 #ifdef G4VERBOSE
137  if ( fState.GetVerboseL1() )
138  fState.GetVerboseL1()->Message("write", "file", fileName, result);
139 #endif
140 
141  return result;
142 }
143 
144 //_____________________________________________________________________________
145 G4bool G4RootFileManager::CloseFile(std::shared_ptr<tools::wroot::file> rfile,
146 #ifdef G4VERBOSE
147  const G4String& fileName)
148 #else
149  const G4String& /*fileName*/)
150 #endif
151 {
152 #ifdef G4VERBOSE
153  if ( fState.GetVerboseL4() )
154  fState.GetVerboseL4()->Message("close", "file", fileName);
155 #endif
156 
157  rfile->close();
158 
159 #ifdef G4VERBOSE
160  if ( fState.GetVerboseL1() )
161  fState.GetVerboseL1()->Message("close", "file", fileName, true);
162 #endif
163 
164  return true;
165 }
166 
167 //
168 // public methods
169 //
170 //_____________________________________________________________________________
172 {
173  // Keep file name
174  fFileName = fileName;
175  auto name = GetFullFileName();
176 
177  // delete previous file if exists
178  //if ( fFile ) delete fFile;
179 
180  // create new file
181  fFile = std::make_shared<tools::wroot::file>(G4cout, name);
182  fFile->add_ziper('Z',tools::compress_buffer);
183  fFile->set_compression(fState.GetCompressionLevel());
184 
185  if ( ! fFile->is_open() ) {
186  G4ExceptionDescription description;
187  description << " " << "Cannot open file " << fileName;
188  G4Exception("G4RootAnalysisManager::OpenFile()",
189  "Analysis_W001", JustWarning, description);
190  return false;
191  }
192 
193  // Create directories
194  if ( ! CreateHistoDirectory() ) return false;
195  if ( ! CreateNtupleDirectory() ) return false;
196 
197  // Open ntuple files
198  OpenNtupleFiles();
199 
200  fLockFileName = true;
203 
204  fIsOpenFile = true;
205 
206  return true;
207 }
208 
209 //_____________________________________________________________________________
211 {
212  auto finalResult = true;
213 
214  auto result = WriteFile(fFile, GetFullFileName());
215  finalResult = finalResult && result;
216 
217  auto counter = 0;
218  for ( auto ntupleFile : fNtupleFiles ) {
219  result = WriteFile(ntupleFile, GetNtupleFileName(counter++));
220  finalResult = finalResult && result;
221  }
222  return finalResult;
223 }
224 
225 //_____________________________________________________________________________
227 {
228  auto finalResult = true;
229 
230  auto result = CloseFile(fFile, GetFullFileName());
231  finalResult = finalResult && result;
232 
233  auto counter = 0;
234  for ( auto ntupleFile : fNtupleFiles ) {
235  result = CloseFile(ntupleFile, GetNtupleFileName(counter++));
236  finalResult = finalResult && result;
237  }
238 
239  fLockFileName = false;
240  fIsOpenFile = false;
241 
242  return finalResult;
243 }
244 
245 //_____________________________________________________________________________
247 {
248  if ( fHistoDirectoryName == "" ) {
249  // Do not create a new directory if its name is not set
250  fHistoDirectory = &(fFile->dir());
251  return true;
252  }
253 
254 #ifdef G4VERBOSE
255  if ( fState.GetVerboseL4() )
257  ->Message("create", "directory for histograms", fHistoDirectoryName);
258 #endif
259 
260  fHistoDirectory = fFile->dir().mkdir(fHistoDirectoryName);
261  if ( ! fHistoDirectory ) {
262  G4ExceptionDescription description;
263  description << " "
264  << "cannot create directory " << fHistoDirectoryName;
265  G4Exception("G4RootFileManager::CreateHistoDirectory()",
266  "Analysis_W001", JustWarning, description);
267  return false;
268  }
269 #ifdef G4VERBOSE
270  else {
271  if ( fState.GetVerboseL2() )
273  ->Message("create", "directory for histograms", fHistoDirectoryName);
274  }
275 #endif
276  return true;
277 }
278 
279 //_____________________________________________________________________________
281 {
282  if ( fNtupleDirectoryName == "" ) {
283  // Do not create a new directory if its name is not set
284  fNtupleDirectory = &(fFile->dir());
285  return true;
286  }
287 
288 #ifdef G4VERBOSE
289  if ( fState.GetVerboseL4() )
291  ->Message("create", "directory for ntuples", fNtupleDirectoryName);
292 #endif
293 
294  fNtupleDirectory = fFile->dir().mkdir(fNtupleDirectoryName);
295  if ( ! fNtupleDirectory ) {
296  G4ExceptionDescription description;
297  description << " "
298  << "cannot create directory " << fNtupleDirectoryName;
299  G4Exception("G4RootFileManager::CreateNtupleDirectory()",
300  "Analysis_W001", JustWarning, description);
301  return false;
302  }
303 #ifdef G4VERBOSE
304  else {
305  if ( fState.GetVerboseL2() )
307  ->Message("create", "directory for ntuples", fNtupleDirectoryName);
308  }
309 #endif
310  return true;
311 }
312 
313 //_____________________________________________________________________________
314 std::shared_ptr<tools::wroot::file>
316 {
317  if ( index==0 && ( ! fNtupleFiles.size() ) ) return fFile;
318 
319  if ( index < 0 || index >= G4int(fNtupleFiles.size()) ) {
320  G4String inFunction = "G4RootFileManager::GetNtupleFile()";
321  G4ExceptionDescription description;
322  description << " " << "ntuple file " << index << " does not exist.";
323  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
324  return nullptr;
325  }
326 
327  return fNtupleFiles[index];
328 }
329 
330 //_____________________________________________________________________________
331 tools::wroot::directory*
333 {
334  if ( index==0 && ( ! fMainNtupleDirectories.size() ) ) return fNtupleDirectory;
335 
336  if ( index < 0 || index >= G4int(fMainNtupleDirectories.size()) ) {
337  G4String inFunction = "G4RootFileManager::GetMainNtupleDirectory()";
338  G4ExceptionDescription description;
339  description << " " << "main ntuple directory " << index << " does not exist.";
340  G4Exception(inFunction, "Analysis_W011", JustWarning, description);
341  return nullptr;
342  }
343 
344  return fMainNtupleDirectories[index];
345 }
346 
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
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