Geant4  10.03.p01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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  G4ExceptionDescription description;
95  description << " " << "Cannot open file " << GetFullFileName();
96  G4Exception("G4XmlFileManager::CreateHnFile()",
97  "Analysis_W001", JustWarning, description);
98  return false;
99  }
100 
101  tools::waxml::begin(*fHnFile);
102 #ifdef G4VERBOSE
103  if ( fState.GetVerboseL1() )
104  fState.GetVerboseL1()->Message("create", "histo file", GetFullFileName());
105 #endif
106 
107  return true;
108 }
109 
110 //_____________________________________________________________________________
112 {
113  // No file may be open if no master manager is instantiated
114  // and no histograms were booked
115  if ( ! fHnFile.get() ) return true;
116 
117 #ifdef G4VERBOSE
118  if ( fState.GetVerboseL4() )
119  fState.GetVerboseL4()->Message("close", "histo file", GetFullFileName());
120 #endif
121 
122  // close file
123  tools::waxml::end(*fHnFile);
124  fHnFile->close();
125 
126 #ifdef G4VERBOSE
127  if ( fState.GetVerboseL1() )
128  fState.GetVerboseL1()->Message("close", "histo file", GetFullFileName());
129 #endif
130 
131 
132  return true;
133 }
134 
135 //_____________________________________________________________________________
138 {
139  G4String ntupleName = ntupleDescription->fNtupleBooking.name();
140 
141 #ifdef G4VERBOSE
142  if ( fState.GetVerboseL4() )
144  ->Message("create", "ntuple file", GetNtupleFileName(ntupleName));
145 #endif
146 
147  auto ntupleFile = new std::ofstream(GetNtupleFileName(ntupleName));
148  if ( ntupleFile->fail() ) {
149  G4ExceptionDescription description;
150  description << " " << "Cannot open file "
151  << GetNtupleFileName(ntupleName);
152  G4Exception("G4XmlFileManager::CreateNtupleFile()",
153  "Analysis_W001", JustWarning, description);
154  return false;
155  }
156 
157  tools::waxml::begin(*ntupleFile);
158  ntupleDescription->fFile = ntupleFile;
159 
160 #ifdef G4VERBOSE
161  if ( fState.GetVerboseL1() )
163  ->Message("create", "ntuple file", GetNtupleFileName(ntupleName));
164 #endif
165 
166  return true;
167 }
168 
169 //_____________________________________________________________________________
172 {
173  G4String ntupleName = ntupleDescription->fNtupleBooking.name();
174 
175 #ifdef G4VERBOSE
176  if ( fState.GetVerboseL4() )
178  ->Message("close", "ntuple file", GetNtupleFileName(ntupleName));
179 #endif
180 
181  // close file
182  tools::waxml::end(*(ntupleDescription->fFile));
183  ntupleDescription->fFile->close();
184 
185 #ifdef G4VERBOSE
186  if ( fState.GetVerboseL1() )
188  ->Message("close", "ntuple file", GetNtupleFileName(ntupleName));
189 #endif
190 
191  return true;
192 }
193 
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