Geant4  10.01.p03
G4XmlFileManager.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: G4XmlFileManager.cc 70604 2013-06-03 11:27:06Z ihrivnac $
27 
28 // Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr)
29 
30 #include "G4XmlFileManager.hh"
32 
33 #include "tools/waxml/begend"
34 
35 //_____________________________________________________________________________
37  : G4VFileManager(state),
38  fHnFile(0)
39 {
40 }
41 
42 //_____________________________________________________________________________
44 {
45  delete fHnFile;
46 }
47 
48 //
49 // public methods
50 //
51 
52 //_____________________________________________________________________________
54 {
55  // Keep and locks file name
56  fFileName = fileName;
57  fLockFileName = true;
58 
59  return true;
60 }
61 
62 //_____________________________________________________________________________
64 {
65  // Nothing to be done here
66  return true;
67 }
68 
69 //_____________________________________________________________________________
71 {
72  // Unlock file name
73 
74  fLockFileName = false;
75  return true;
76 }
77 
78 //_____________________________________________________________________________
80 {
81 #ifdef G4VERBOSE
82  if ( fState.GetVerboseL4() )
83  fState.GetVerboseL4()->Message("create", "histo file", GetFullFileName());
84 #endif
85 
86  // delete a previous file if it exists
87  if ( fHnFile ) delete fHnFile;
88 
89  fHnFile = new std::ofstream(GetFullFileName());
90  if ( fHnFile->fail() ) {
91  G4ExceptionDescription description;
92  description << " " << "Cannot open file " << GetFullFileName();
93  G4Exception("G4XmlFileManager::CreateHnFile()",
94  "Analysis_W001", JustWarning, description);
95  return false;
96  }
97 
98  tools::waxml::begin(*fHnFile);
99 #ifdef G4VERBOSE
100  if ( fState.GetVerboseL1() )
101  fState.GetVerboseL1()->Message("create", "histo file", GetFullFileName());
102 #endif
103 
104  return true;
105 }
106 
107 //_____________________________________________________________________________
109 {
110  // No file may be open if no master manager is instantiated
111  // and no histograms were booked
112  if ( ! fHnFile ) return true;
113 
114 #ifdef G4VERBOSE
115  if ( fState.GetVerboseL4() )
116  fState.GetVerboseL4()->Message("close", "histo file", GetFullFileName());
117 #endif
118 
119  // close file
120  tools::waxml::end(*fHnFile);
121  fHnFile->close();
122 
123 #ifdef G4VERBOSE
124  if ( fState.GetVerboseL1() )
125  fState.GetVerboseL1()->Message("close", "histo file", GetFullFileName());
126 #endif
127 
128 
129  return true;
130 }
131 
132 //_____________________________________________________________________________
134  G4XmlNtupleDescription* ntupleDescription)
135 {
136  G4String ntupleName = ntupleDescription->fNtupleBooking->name();
137 
138 #ifdef G4VERBOSE
139  if ( fState.GetVerboseL4() )
141  ->Message("create", "ntuple file", GetNtupleFileName(ntupleName));
142 #endif
143 
144  std::ofstream* ntupleFile
145  = new std::ofstream(GetNtupleFileName(ntupleName));
146  if ( ntupleFile->fail() ) {
147  G4ExceptionDescription description;
148  description << " " << "Cannot open file "
149  << GetNtupleFileName(ntupleName);
150  G4Exception("G4XmlFileManager::CreateNtupleFile()",
151  "Analysis_W001", JustWarning, description);
152  return false;
153  }
154 
155  tools::waxml::begin(*ntupleFile);
156  ntupleDescription->fFile = ntupleFile;
157 
158 #ifdef G4VERBOSE
159  if ( fState.GetVerboseL1() )
161  ->Message("create", "ntuple file", GetNtupleFileName(ntupleName));
162 #endif
163 
164  return true;
165 }
166 
167 //_____________________________________________________________________________
169  G4XmlNtupleDescription* ntupleDescription)
170 {
171  G4String ntupleName = ntupleDescription->fNtupleBooking->name();
172 
173 #ifdef G4VERBOSE
174  if ( fState.GetVerboseL4() )
176  ->Message("close", "ntuple file", GetNtupleFileName(ntupleName));
177 #endif
178 
179  // close file
180  tools::waxml::end(*(ntupleDescription->fFile));
181  ntupleDescription->fFile->close();
182 
183 #ifdef G4VERBOSE
184  if ( fState.GetVerboseL1() )
186  ->Message("close", "ntuple file", GetNtupleFileName(ntupleName));
187 #endif
188 
189  return true;
190 }
191 
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
std::ofstream * fHnFile
const G4AnalysisManagerState & fState
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4String GetNtupleFileName(const G4String &ntupleName) const
G4XmlFileManager(const G4AnalysisManagerState &state)
tools::ntuple_booking * fNtupleBooking
G4bool CreateNtupleFile(G4XmlNtupleDescription *ntupleDescription)
virtual G4bool WriteFile()
const G4AnalysisVerbose * GetVerboseL4() const
bool G4bool
Definition: G4Types.hh:79
virtual G4bool CloseFile()
virtual G4bool OpenFile(const G4String &fileName)
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4bool CloseNtupleFile(G4XmlNtupleDescription *ntupleDescription)
G4String GetFullFileName(const G4String &baseFileName="", G4bool isPerThread=true) const
const G4AnalysisVerbose * GetVerboseL1() const