Geant4-11
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
27// Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr)
28
29#include "G4XmlFileManager.hh"
30#include "G4XmlHnFileManager.hh"
33
34#include "tools/waxml/begend"
35
36using namespace G4Analysis;
37using namespace tools;
38
39//_____________________________________________________________________________
41 : G4VTFileManager<std::ofstream>(state)
42{
43 // Create helpers defined in the base class
44 fH1FileManager = std::make_shared<G4XmlHnFileManager<histo::h1d>>(this);
45 fH2FileManager = std::make_shared<G4XmlHnFileManager<histo::h2d>>(this);
46 fH3FileManager = std::make_shared<G4XmlHnFileManager<histo::h3d>>(this);
47 fP1FileManager = std::make_shared<G4XmlHnFileManager<histo::p1d>>(this);
48 fP2FileManager = std::make_shared<G4XmlHnFileManager<histo::p2d>>(this);
49}
50
51//
52// private methods
53//
54
55//_____________________________________________________________________________
57{
58 // get ntuple file name
59 auto ntupleFileName = ntupleDescription->fFileName;
60 if ( ntupleFileName.size() ) {
61 // update filename per object per thread
62 ntupleFileName = GetTnFileName(ntupleFileName, GetFileType());
63 } else {
64 // compose ntuple file name from the default file name
65 ntupleFileName = GetNtupleFileName(ntupleDescription->fNtupleBooking.name());
66 }
67 return ntupleFileName;
68}
69
70//
71// protected methods
72//
73
74//_____________________________________________________________________________
75std::shared_ptr<std::ofstream> G4XmlFileManager::CreateFileImpl(const G4String& fileName)
76{
77 std::shared_ptr<std::ofstream> file = std::make_shared<std::ofstream>(fileName);
78 if ( file->fail() ) {
79 Warn(G4String("Cannot create file ") + fileName, fkClass, "CreateFileImpl");
80 return nullptr;
81 }
82
83 waxml::begin(*file);
84 return file;
85}
86
87//_____________________________________________________________________________
88G4bool G4XmlFileManager::WriteFileImpl(std::shared_ptr<std::ofstream> /*file*/)
89{
90 // Nothing to be done here
91 return true;
92}
93
94//_____________________________________________________________________________
95G4bool G4XmlFileManager::CloseFileImpl(std::shared_ptr<std::ofstream> file)
96{
97 if ( ! file ) return false;
98
99 // close file
100 waxml::end(*file);
101 file->close();
102
103 return true;
104}
105
106//
107// public methods
108//
109
110//_____________________________________________________________________________
112{
113 // Keep and locks file name
114 fFileName = fileName;
116
117 if ( fFile ) {
118 Warn(G4String("File ") + fileName + " already exists.", fkClass, "OpenFile");
119 fFile.reset();
120 }
121
122 // Create histograms file (on master)
123 if ( fState.GetIsMaster() ) {
124 // Create file (and save in in the file map (on master only)
126 if ( ! fFile) {
127 Warn(G4String("Failed to create file") + fileName, fkClass, "OpenFile");
128 return false;
129 }
130 }
131
132 fIsOpenFile = true;
133
134 return true;
135}
136
137//_____________________________________________________________________________
139 XmlNtupleDescription* ntupleDescription)
140{
141 // get ntuple file name per object (if defined)
142 auto ntupleFileName = GetNtupleFileName(ntupleDescription);
143
144 Message(kVL4, "create", "ntuple file", ntupleFileName);
145
146 // update file name if it is already in use
147 while ( GetTFile(ntupleFileName, false) ) {
148 // the file is already in use
149 auto oldName = ntupleDescription->fFileName;
150 auto newName = GetBaseName(oldName) + "_bis." + GetExtension(oldName);
151 ntupleDescription->fFileName = newName;
152
153 Warn(G4String( "Ntuple filename ") + oldName + " is already in use.\n" +
154 "It will be replaced with : " + newName,
155 fkClass, "CreateNtupleFile");
156
157 ntupleFileName = GetNtupleFileName(ntupleDescription);
158 }
159
160 ntupleDescription->fFile = CreateTFile(ntupleFileName);
161
162 Message(kVL2, "create", "ntuple file", ntupleFileName);
163
164 return (ntupleDescription->fFile != nullptr);
165}
166
167//_____________________________________________________________________________
169 XmlNtupleDescription* ntupleDescription)
170{
171 // Do nothing if there is no file
172 if ( ! ntupleDescription->fFile ) return true;
173
174 auto result = true;
175
176 auto ntupleFileName = GetNtupleFileName(ntupleDescription);
177
178 Message(kVL4, "close", "ntuple file", ntupleFileName);
179
180 // Close file
181 result &= CloseTFile(ntupleFileName);
182 // Notify not empty file
183 result &=SetIsEmpty(ntupleFileName, ! ntupleDescription->fHasFill);
184
185 // Reset file info in ntuple description
186 ntupleDescription->fFile.reset();
187
188 Message(kVL2, "close", "ntuple file", ntupleFileName);
189
190 return result;
191}
bool G4bool
Definition: G4Types.hh:86
G4String GetFullFileName(const G4String &baseFileName="", G4bool isPerThread=true) const
void Message(G4int level, const G4String &action, const G4String &objectType, const G4String &objectName="", G4bool success=true) const
const G4AnalysisManagerState & fState
G4bool CloseTFile(const G4String &fileName)
std::shared_ptr< std::ofstream > GetTFile(const G4String &fileName, G4bool warn=true) const
std::shared_ptr< std::ofstream > CreateTFile(const G4String &fileName)
std::shared_ptr< G4VTHnFileManager< tools::histo::h3d > > fH3FileManager
std::shared_ptr< G4VTHnFileManager< tools::histo::h1d > > fH1FileManager
std::shared_ptr< G4VTHnFileManager< tools::histo::p1d > > fP1FileManager
std::shared_ptr< G4VTHnFileManager< tools::histo::p2d > > fP2FileManager
std::shared_ptr< G4VTHnFileManager< tools::histo::h2d > > fH2FileManager
virtual G4bool SetIsEmpty(const G4String &fileName, G4bool isEmpty) final
std::shared_ptr< std::ofstream > fFile
static constexpr std::string_view fkClass
virtual G4bool OpenFile(const G4String &fileName) final
G4String GetNtupleFileName(XmlNtupleDescription *ntupleDescription)
virtual G4bool CloseFileImpl(std::shared_ptr< std::ofstream > file) final
G4XmlFileManager()=delete
virtual std::shared_ptr< std::ofstream > CreateFileImpl(const G4String &fileName) final
virtual G4bool WriteFileImpl(std::shared_ptr< std::ofstream > file) final
G4bool CloseNtupleFile(XmlNtupleDescription *ntupleDescription)
virtual G4String GetFileType() const final
G4bool CreateNtupleFile(XmlNtupleDescription *ntupleDescription)
G4String GetExtension(const G4String &fileName, const G4String &defaultExtension="")
constexpr G4int kVL2
G4String GetTnFileName(const G4String &fileName, const G4String &fileType)
constexpr G4int kVL4
G4String GetBaseName(const G4String &fileName)
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)
const char * name(G4int ptype)
tools::ntuple_booking fNtupleBooking
std::shared_ptr< FT > fFile