Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
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 ([email protected])
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//_____________________________________________________________________________
56G4String G4XmlFileManager::GetNtupleFileName(XmlNtupleDescription* ntupleDescription)
57{
58 // get ntuple file name
59 auto ntupleFileName = ntupleDescription->GetFileName();
60 if (ntupleFileName.size() != 0u) {
61 // update filename per object per thread
62 ntupleFileName =
63 GetTnFileName(ntupleFileName, GetFileType()/*, ntupleDescription->GetCycle()*/);
64 }
65 else {
66 // compose ntuple file name from the default file name
67 ntupleFileName = GetNtupleFileName(
68 ntupleDescription->GetNtupleBooking().name()/*, ntupleDescription->GetCycle()*/);
69 }
70
71 return ntupleFileName;
72}
73
74//
75// protected methods
76//
77
78//_____________________________________________________________________________
79std::shared_ptr<std::ofstream> G4XmlFileManager::CreateFileImpl(const G4String& fileName)
80{
81 std::shared_ptr<std::ofstream> file = std::make_shared<std::ofstream>(fileName);
82 if ( file->fail() ) {
83 Warn(G4String("Cannot create file ") + fileName, fkClass, "CreateFileImpl");
84 return nullptr;
85 }
86
87 waxml::begin(*file);
88 return file;
89}
90
91//_____________________________________________________________________________
92G4bool G4XmlFileManager::WriteFileImpl(std::shared_ptr<std::ofstream> /*file*/)
93{
94 // Nothing to be done here
95 return true;
96}
97
98//_____________________________________________________________________________
99G4bool G4XmlFileManager::CloseFileImpl(std::shared_ptr<std::ofstream> file)
100{
101 if ( ! file ) return false;
102
103 // close file
104 waxml::end(*file);
105 file->close();
106
107 return true;
108}
109
110//
111// public methods
112//
113
114//_____________________________________________________________________________
116{
117 // Keep and locks file name
118 fFileName = fileName;
119 auto name = GetFullFileName(fFileName);
120
121 if ( fFile ) {
122 Warn(G4String("File ") + fileName + " already exists.", fkClass, "OpenFile");
123 fFile.reset();
124 }
125
126 // Create histograms file (on master)
127 if ( fState.GetIsMaster() ) {
128 // Create file (and save in in the file map (on master only)
129 fFile = CreateTFile(name);
130 if ( ! fFile) {
131 Warn(G4String("Failed to create file") + fileName, fkClass, "OpenFile");
132 return false;
133 }
134 }
135
136 fIsOpenFile = true;
137
138 return true;
139}
140
141//_____________________________________________________________________________
143 XmlNtupleDescription* ntupleDescription)
144{
145 // Get ntuple file name per object (if defined)
146 auto ntupleFileName = GetNtupleFileName(ntupleDescription);
147
148 // Create ntuple file name if it does not yet exist
149 auto ntupleFile = GetTFile(ntupleFileName, false);
150 if ( ntupleFile == nullptr) {
151 ntupleFile = CreateTFile(ntupleFileName);
152 }
153
154 ntupleDescription->SetFile(ntupleFile);
155
156 return (ntupleDescription->GetFile() != nullptr);
157}
158
159//_____________________________________________________________________________
161 XmlNtupleDescription* ntupleDescription)
162{
163 // Notify not empty file
164 auto ntupleFileName = GetNtupleFileName(ntupleDescription);
165 auto result = SetIsEmpty(ntupleFileName, ! ntupleDescription->GetHasFill());
166
167 // Ntuple files are registered in file manager map.
168 // they will be closed with CloseFiles() calls
169 ntupleDescription->GetFile().reset();
170
171 return result;
172}
bool G4bool
Definition G4Types.hh:86
G4String GetFullFileName(const G4String &baseFileName="", G4bool isPerThread=true) const
const G4AnalysisManagerState & fState
std::shared_ptr< std::ofstream > GetTFile(const G4String &fileName, G4bool warn=true) const
std::shared_ptr< std::ofstream > CreateTFile(const G4String &fileName)
void SetFile(std::shared_ptr< FT > file)
const tools::ntuple_booking & GetNtupleBooking() const
std::shared_ptr< FT > GetFile() const
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
G4bool SetIsEmpty(const G4String &fileName, G4bool isEmpty) final
std::shared_ptr< std::ofstream > fFile
G4bool OpenFile(const G4String &fileName) final
G4String GetFileType() const final
G4bool CloseFileImpl(std::shared_ptr< std::ofstream > file) final
G4XmlFileManager()=delete
std::shared_ptr< std::ofstream > CreateFileImpl(const G4String &fileName) final
G4bool WriteFileImpl(std::shared_ptr< std::ofstream > file) final
G4bool CloseNtupleFile(XmlNtupleDescription *ntupleDescription)
G4bool CreateNtupleFile(XmlNtupleDescription *ntupleDescription)
G4String GetTnFileName(const G4String &fileName, const G4String &fileType, G4int cycle=0)
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)