ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4XmlFileManager.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4XmlFileManager.cc
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 
27 // Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr)
28 
29 #include "G4XmlFileManager.hh"
31 #include "G4AnalysisUtilities.hh"
32 
33 #include "tools/waxml/begend"
34 
35 using namespace G4Analysis;
36 
37 //_____________________________________________________________________________
39  : G4VFileManager(state),
40  fHnFile(nullptr)
41 {
42 }
43 
44 //_____________________________________________________________________________
46 {}
47 
48 //
49 // public methods
50 //
51 
52 //_____________________________________________________________________________
54 {
55  // Keep and locks file name
56  fFileName = fileName;
57  fLockFileName = true;
58  fIsOpenFile = true;
59 
60  return true;
61 }
62 
63 //_____________________________________________________________________________
65 {
66  // Nothing to be done here
67  return true;
68 }
69 
70 //_____________________________________________________________________________
72 {
73  // Unlock file name
74 
75  fLockFileName = false;
76  fIsOpenFile = false;
77  return true;
78 }
79 
80 //_____________________________________________________________________________
82 {
83 #ifdef G4VERBOSE
84  if ( fState.GetVerboseL4() )
85  fState.GetVerboseL4()->Message("create", "histo file", GetFullFileName());
86 #endif
87 
88  // delete a previous file if it exists
89  //if ( fHnFile ) delete fHnFile;
90 
91  fHnFile = std::make_shared<std::ofstream>(GetFullFileName());
92  if ( fHnFile->fail() ) {
93  fHnFile = nullptr;
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  delete ntupleFile;
150  G4ExceptionDescription description;
151  description << " " << "Cannot open file "
152  << GetNtupleFileName(ntupleName);
153  G4Exception("G4XmlFileManager::CreateNtupleFile()",
154  "Analysis_W001", JustWarning, description);
155  return false;
156  }
157 
158  tools::waxml::begin(*ntupleFile);
159  ntupleDescription->fFile = ntupleFile;
160 
161 #ifdef G4VERBOSE
162  if ( fState.GetVerboseL1() )
164  ->Message("create", "ntuple file", GetNtupleFileName(ntupleName));
165 #endif
166 
167  return true;
168 }
169 
170 //_____________________________________________________________________________
173 {
174  // Do nothing if there is no file
175  if ( ! ntupleDescription->fFile ) return true;
176 
177  G4String ntupleName = ntupleDescription->fNtupleBooking.name();
178 
179 #ifdef G4VERBOSE
180  if ( fState.GetVerboseL4() )
182  ->Message("close", "ntuple file", GetNtupleFileName(ntupleName));
183 #endif
184 
185  // close file
186  tools::waxml::end(*(ntupleDescription->fFile));
187  ntupleDescription->fFile->close();
188 
189 #ifdef G4VERBOSE
190  if ( fState.GetVerboseL1() )
192  ->Message("close", "ntuple file", GetNtupleFileName(ntupleName));
193 #endif
194 
195  return true;
196 }
197