Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 #include "G4AnalysisUtilities.hh"
33 
34 #include "tools/waxml/begend"
35 
36 using namespace G4Analysis;
37 
38 //_____________________________________________________________________________
40  : G4VFileManager(state),
41  fHnFile(nullptr)
42 {
43 }
44 
45 //_____________________________________________________________________________
47 {}
48 
49 //
50 // public methods
51 //
52 
53 //_____________________________________________________________________________
55 {
56  // Keep and locks file name
57  fFileName = fileName;
58  fLockFileName = true;
59  fIsOpenFile = true;
60 
61  return true;
62 }
63 
64 //_____________________________________________________________________________
66 {
67  // Nothing to be done here
68  return true;
69 }
70 
71 //_____________________________________________________________________________
73 {
74  // Unlock file name
75 
76  fLockFileName = false;
77  fIsOpenFile = false;
78  return true;
79 }
80 
81 //_____________________________________________________________________________
83 {
84 #ifdef G4VERBOSE
85  if ( fState.GetVerboseL4() )
86  fState.GetVerboseL4()->Message("create", "histo file", GetFullFileName());
87 #endif
88 
89  // delete a previous file if it exists
90  //if ( fHnFile ) delete fHnFile;
91 
92  fHnFile = std::make_shared<std::ofstream>(GetFullFileName());
93  if ( fHnFile->fail() ) {
94  fHnFile = nullptr;
95  G4ExceptionDescription description;
96  description << " " << "Cannot open file " << GetFullFileName();
97  G4Exception("G4XmlFileManager::CreateHnFile()",
98  "Analysis_W001", JustWarning, description);
99  return false;
100  }
101 
102  tools::waxml::begin(*fHnFile);
103 #ifdef G4VERBOSE
104  if ( fState.GetVerboseL1() )
105  fState.GetVerboseL1()->Message("create", "histo file", GetFullFileName());
106 #endif
107 
108  return true;
109 }
110 
111 //_____________________________________________________________________________
113 {
114  // No file may be open if no master manager is instantiated
115  // and no histograms were booked
116  if ( ! fHnFile.get() ) return true;
117 
118 #ifdef G4VERBOSE
119  if ( fState.GetVerboseL4() )
120  fState.GetVerboseL4()->Message("close", "histo file", GetFullFileName());
121 #endif
122 
123  // close file
124  tools::waxml::end(*fHnFile);
125  fHnFile->close();
126 
127 #ifdef G4VERBOSE
128  if ( fState.GetVerboseL1() )
129  fState.GetVerboseL1()->Message("close", "histo file", GetFullFileName());
130 #endif
131 
132 
133  return true;
134 }
135 
136 //_____________________________________________________________________________
139 {
140  G4String ntupleName = ntupleDescription->fNtupleBooking.name();
141 
142 #ifdef G4VERBOSE
143  if ( fState.GetVerboseL4() )
145  ->Message("create", "ntuple file", GetNtupleFileName(ntupleName));
146 #endif
147 
148  auto ntupleFile = new std::ofstream(GetNtupleFileName(ntupleName));
149  if ( ntupleFile->fail() ) {
150  delete ntupleFile;
151  G4ExceptionDescription description;
152  description << " " << "Cannot open file "
153  << GetNtupleFileName(ntupleName);
154  G4Exception("G4XmlFileManager::CreateNtupleFile()",
155  "Analysis_W001", JustWarning, description);
156  return false;
157  }
158 
159  tools::waxml::begin(*ntupleFile);
160  ntupleDescription->fFile = ntupleFile;
161 
162 #ifdef G4VERBOSE
163  if ( fState.GetVerboseL1() )
165  ->Message("create", "ntuple file", GetNtupleFileName(ntupleName));
166 #endif
167 
168  return true;
169 }
170 
171 //_____________________________________________________________________________
174 {
175  // Do nothing if there is no file
176  if ( ! ntupleDescription->fFile ) return true;
177 
178  G4String ntupleName = ntupleDescription->fNtupleBooking.name();
179 
180 #ifdef G4VERBOSE
181  if ( fState.GetVerboseL4() )
183  ->Message("close", "ntuple file", GetNtupleFileName(ntupleName));
184 #endif
185 
186  // close file
187  tools::waxml::end(*(ntupleDescription->fFile));
188  ntupleDescription->fFile->close();
189 
190 #ifdef G4VERBOSE
191  if ( fState.GetVerboseL1() )
193  ->Message("close", "ntuple file", GetNtupleFileName(ntupleName));
194 #endif
195 
196  return true;
197 }
198 
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
const G4AnalysisManagerState & fState
std::ostringstream G4ExceptionDescription
Definition: globals.hh:76
G4String GetNtupleFileName(const G4String &ntupleName) const
G4XmlFileManager(const G4AnalysisManagerState &state)
virtual G4bool OpenFile(const G4String &fileName) final
virtual G4bool CloseFile() final
virtual G4bool WriteFile() final
const G4AnalysisVerbose * GetVerboseL4() const
bool G4bool
Definition: G4Types.hh:79
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
G4bool CloseNtupleFile(NtupleDescriptionType *ntupleDescription)
G4bool CreateNtupleFile(NtupleDescriptionType *ntupleDescription)
tools::ntuple_booking fNtupleBooking
G4String GetFullFileName(const G4String &baseFileName="", G4bool isPerThread=true) const
const G4AnalysisVerbose * GetVerboseL1() const