Geant4  10.01.p02
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 
33 #include "tools/wroot/file"
34 #include "tools/rroot/file"
35 #include <tools/gzip_buffer>
36 
37 #include <iostream>
38 #include <cstdio>
39 
40 //_____________________________________________________________________________
42  : G4VFileManager(state),
43  fFile(0),
44  fHistoDirectory(0),
45  fProfileDirectory(0),
46  fNtupleDirectory(0)
47 {
48 }
49 
50 //_____________________________________________________________________________
52 {
53  delete fFile;
54 }
55 
56 //
57 // public methods
58 //
59 //_____________________________________________________________________________
61 {
62  // Keep file name
63  fFileName = fileName;
65 
66  // delete previous file if exists
67  if ( fFile ) delete fFile;
68 
69  // create new file
70  fFile = new tools::wroot::file(G4cout, name);
71  fFile->add_ziper('Z',tools::gzip_buffer);
72  fFile->set_compression(9);
73 
74  if ( ! fFile->is_open() ) {
75  G4ExceptionDescription description;
76  description << " " << "Cannot open file " << fileName;
77  G4Exception("G4RootAnalysisManager::OpenFile()",
78  "Analysis_W001", JustWarning, description);
79  return false;
80  }
81 
82  // Create directories
83  if ( ! CreateHistoDirectory() ) return false;
84  if ( ! CreateProfileDirectory() ) return false;
85  if ( ! CreateNtupleDirectory() ) return false;
86 
87  fLockFileName = true;
91 
92  return true;
93 }
94 
95 //_____________________________________________________________________________
97 {
98 #ifdef G4VERBOSE
99  if ( fState.GetVerboseL4() )
100  fState.GetVerboseL4()->Message("write", "file", GetFullFileName());
101 #endif
102 
103  unsigned int n;
104  G4bool result = fFile->write(n);
105 
106 #ifdef G4VERBOSE
107  if ( fState.GetVerboseL1() )
108  fState.GetVerboseL1()->Message("write", "file", GetFullFileName(), result);
109 #endif
110 
111  return result;
112 }
113 
114 //_____________________________________________________________________________
116 {
117  // close file
118  fFile->close();
119  fLockFileName = false;
120 
121  return true;
122 }
123 
124 //_____________________________________________________________________________
126 {
127  if ( fHistoDirectoryName == "" ) {
128  // Do not create a new directory if its name is not set
129  fHistoDirectory = &(fFile->dir());
130  return true;
131  }
132 
133 #ifdef G4VERBOSE
134  if ( fState.GetVerboseL4() )
136  ->Message("create", "directory for histograms", fHistoDirectoryName);
137 #endif
138 
139  fHistoDirectory = fFile->dir().mkdir(fHistoDirectoryName);
140  if ( ! fHistoDirectory ) {
141  G4ExceptionDescription description;
142  description << " "
143  << "cannot create directory " << fHistoDirectoryName;
144  G4Exception("G4RootFileManager::CreateHistoDirectory()",
145  "Analysis_W001", JustWarning, description);
146  return false;
147  }
148 #ifdef G4VERBOSE
149  else {
150  if ( fState.GetVerboseL2() )
152  ->Message("create", "directory for histograms", fHistoDirectoryName);
153  }
154 #endif
155  return true;
156 }
157 
158 //_____________________________________________________________________________
160 {
161  if ( fProfileDirectoryName == "" ) {
162  // Do not create a new directory if its name is not set
163  fProfileDirectory = &(fFile->dir());
164  return true;
165  }
166 
167 #ifdef G4VERBOSE
168  if ( fState.GetVerboseL4() )
170  ->Message("create", "directory for profiles", fProfileDirectoryName);
171 #endif
172 
174  if ( ! fProfileDirectory ) {
175  G4ExceptionDescription description;
176  description << " "
177  << "cannot create directory " << fProfileDirectoryName;
178  G4Exception("G4RootFileManager::CreateProfileDirectory()",
179  "Analysis_W001", JustWarning, description);
180  return false;
181  }
182 #ifdef G4VERBOSE
183  else {
184  if ( fState.GetVerboseL2() )
186  ->Message("create", "directory for profiles", fProfileDirectoryName);
187  }
188 #endif
189  return true;
190 }
191 
192 //_____________________________________________________________________________
194 {
195  if ( fNtupleDirectoryName == "" ) {
196  // Do not create a new directory if its name is not set
197  fNtupleDirectory = &(fFile->dir());
198  return true;
199  }
200 
201 #ifdef G4VERBOSE
202  if ( fState.GetVerboseL4() )
204  ->Message("create", "directory for ntuples", fNtupleDirectoryName);
205 #endif
206 
208  if ( ! fNtupleDirectory ) {
209  G4ExceptionDescription description;
210  description << " "
211  << "cannot create directory " << fNtupleDirectoryName;
212  G4Exception("G4RootFileManager::CreateNtupleDirectory()",
213  "Analysis_W001", JustWarning, description);
214  return false;
215  }
216 #ifdef G4VERBOSE
217  else {
218  if ( fState.GetVerboseL2() )
220  ->Message("create", "directory for ntuples", fNtupleDirectoryName);
221  }
222 #endif
223  return true;
224 }
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
virtual G4bool CloseFile()
const G4AnalysisManagerState & fState
G4bool fLockHistoDirectoryName
G4String fNtupleDirectoryName
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
tools::wroot::directory * fNtupleDirectory
tools::wroot::file * fFile
G4String name
Definition: TRTMaterials.hh:40
G4String fHistoDirectoryName
tools::wroot::directory * fHistoDirectory
virtual G4bool WriteFile()
const G4AnalysisVerbose * GetVerboseL2() const
G4RootFileManager(const G4AnalysisManagerState &state)
G4GLOB_DLL std::ostream G4cout
const G4AnalysisVerbose * GetVerboseL4() const
virtual ~G4RootFileManager()
bool G4bool
Definition: G4Types.hh:79
const G4int n
tools::wroot::directory * fProfileDirectory
G4String fProfileDirectoryName
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4bool fLockNtupleDirectoryName
virtual G4bool OpenFile(const G4String &fileName)
G4bool fLockProfileDirectoryName
G4String GetFullFileName(const G4String &baseFileName="", G4bool isPerThread=true) const
const G4AnalysisVerbose * GetVerboseL1() const