Geant4  10.00.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/gzip_buffer>
35 
36 #include <iostream>
37 #include <cstdio>
38 
39 //_____________________________________________________________________________
41  : G4VFileManager(state),
42  fFile(0),
43  fHistoDirectory(0),
44  fNtupleDirectory(0)
45 {
46 }
47 
48 //_____________________________________________________________________________
50 {
51  delete fFile;
52 }
53 
54 //
55 // public methods
56 //
57 //_____________________________________________________________________________
59 {
60  // Keep file name
61  fFileName = fileName;
63 
64  // delete previous file if exists
65  if ( fFile ) delete fFile;
66 
67  fFile = new tools::wroot::file(std::cout, name);
68  fFile->add_ziper('Z',tools::gzip_buffer);
69  fFile->set_compression(9);
70 
71  if ( ! fFile->is_open() ) {
72  G4ExceptionDescription description;
73  description << " " << "Cannot open file " << fileName;
74  G4Exception("G4RootAnalysisManager::OpenFile()",
75  "Analysis_W001", JustWarning, description);
76  return false;
77  }
78 
79  // Create directories
80  if ( ! CreateHistoDirectory() ) return false;
81  if ( ! CreateNtupleDirectory() ) return false;
82 
83  fLockFileName = true;
86 
87  return true;
88 }
89 
90 //_____________________________________________________________________________
92 {
93 #ifdef G4VERBOSE
94  if ( fState.GetVerboseL4() )
95  fState.GetVerboseL4()->Message("write", "file", GetFullFileName());
96 #endif
97 
98  unsigned int n;
99  G4bool result = fFile->write(n);
100 
101 #ifdef G4VERBOSE
102  if ( fState.GetVerboseL1() )
103  fState.GetVerboseL1()->Message("write", "file", GetFullFileName(), result);
104 #endif
105 
106  return result;
107 }
108 
109 //_____________________________________________________________________________
111 {
112  // close file
113  fFile->close();
114  fLockFileName = false;
115 
116  return true;
117 }
118 
119 //_____________________________________________________________________________
121 {
122  if ( fHistoDirectoryName == "" ) {
123  // Do not create a new directory if its name is not set
124  fHistoDirectory = &(fFile->dir());
125  return true;
126  }
127 
128 #ifdef G4VERBOSE
129  if ( fState.GetVerboseL4() )
131  ->Message("create", "directory for histograms", fHistoDirectoryName);
132 #endif
133 
134  fHistoDirectory = fFile->dir().mkdir(fHistoDirectoryName);
135  if ( ! fHistoDirectory ) {
136  G4ExceptionDescription description;
137  description << " "
138  << "cannot create directory " << fHistoDirectoryName;
139  G4Exception("G4RootFileManager::CreateHistoDirectory()",
140  "Analysis_W002", JustWarning, description);
141  return false;
142  }
143 #ifdef G4VERBOSE
144  else {
145  if ( fState.GetVerboseL2() )
147  ->Message("create", "directory for histograms", fHistoDirectoryName);
148  }
149 #endif
150  return true;
151 }
152 
153 //_____________________________________________________________________________
155 {
156  if ( fNtupleDirectoryName == "" ) {
157  // Do not create a new directory if its name is not set
158  fNtupleDirectory = &(fFile->dir());
159  return true;
160  }
161 
162 #ifdef G4VERBOSE
163  if ( fState.GetVerboseL4() )
165  ->Message("create", "directory for ntuples", fNtupleDirectoryName);
166 #endif
167 
169  if ( ! fNtupleDirectory ) {
170  G4ExceptionDescription description;
171  description << " "
172  << "cannot create directory " << fNtupleDirectoryName;
173  G4Exception("G4RootFileManager::CreateNtupleDirectory()",
174  "Analysis_W002", JustWarning, description);
175  return false;
176  }
177 #ifdef G4VERBOSE
178  else {
179  if ( fState.GetVerboseL2() )
181  ->Message("create", "directory for ntuples", fNtupleDirectoryName);
182  }
183 #endif
184  return true;
185 }
186 
187 
188 
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
virtual G4bool CloseFile()
G4bool fLockHistoDirectoryName
G4String fNtupleDirectoryName
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4String GetFullFileName() const
tools::wroot::directory * fNtupleDirectory
tools::wroot::file * fFile
G4String name
Definition: TRTMaterials.hh:40
G4String fHistoDirectoryName
const G4AnalysisManagerState & fState
tools::wroot::directory * fHistoDirectory
virtual G4bool WriteFile()
const G4AnalysisVerbose * GetVerboseL2() const
G4RootFileManager(const G4AnalysisManagerState &state)
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
G4String fFileName
virtual G4bool OpenFile(const G4String &fileName)
const G4AnalysisVerbose * GetVerboseL1() const