Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4CsvFileManager.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 "G4CsvFileManager.hh"
30#include "G4CsvHnFileManager.hh"
33
34using namespace G4Analysis;
35using namespace tools;
36
37//_____________________________________________________________________________
39 : G4VTFileManager(state)
40{
41 // Create helpers defined in the base class
42 fH1FileManager = std::make_shared<G4CsvHnFileManager<histo::h1d>>(this);
43 fH2FileManager = std::make_shared<G4CsvHnFileManager<histo::h2d>>(this);
44 fH3FileManager = std::make_shared<G4CsvHnFileManager<histo::h3d>>(this);
45 fP1FileManager = std::make_shared<G4CsvHnFileManager<histo::p1d>>(this);
46 fP2FileManager = std::make_shared<G4CsvHnFileManager<histo::p2d>>(this);
47}
48
49//_____________________________________________________________________________
51{}
52
53//
54// private methods
55//
56
57//_____________________________________________________________________________
58G4String G4CsvFileManager::GetNtupleFileName(CsvNtupleDescription* ntupleDescription)
59{
60 // get ntuple file name
61 auto ntupleFileName = ntupleDescription->fFileName;
62 if ( ntupleFileName.size() ) {
63 // update filename per object per thread
64 ntupleFileName = GetTnFileName(ntupleFileName, GetFileType());
65 } else {
66 // compose ntuple file name from the default file name
67 ntupleFileName = GetNtupleFileName(ntupleDescription->fNtupleBooking.name());
68 }
69 return ntupleFileName;
70}
71
72//
73// protected methods
74//
75
76//_____________________________________________________________________________
77std::shared_ptr<std::ofstream> G4CsvFileManager::CreateFileImpl(const G4String& fileName)
78{
79 std::shared_ptr<std::ofstream> file = std::make_shared<std::ofstream>(fileName);
80 if ( file->fail() ) {
81 file = nullptr;
82 G4ExceptionDescription description;
83 description << " " << "Cannot create file " << fileName;
84 G4Exception("G4CsvFileManager::CreateFileImpl()",
85 "Analysis_W001", JustWarning, description);
86 return nullptr;
87 }
88
89 return file;
90}
91
92//_____________________________________________________________________________
93G4bool G4CsvFileManager::WriteFileImpl(std::shared_ptr<std::ofstream> /*file*/)
94{
95 // Nothing to be done here
96 return true;
97}
98
99//_____________________________________________________________________________
100G4bool G4CsvFileManager::CloseFileImpl(std::shared_ptr<std::ofstream> file)
101{
102 if ( ! file ) return false;
103
104 // close file
105 file->close();
106
107 return true;
108}
109
110//
111// public methods
112//
113
114//_____________________________________________________________________________
116{
117 // Keep file name
118 fFileName = fileName;
119
120 fIsOpenFile = true;
121
122 return true;
123}
124
125//_____________________________________________________________________________
127 CsvNtupleDescription* ntupleDescription)
128{
129 // get ntuple file name per object (if defined)
130 auto ntupleFileName = GetNtupleFileName(ntupleDescription);
131
132#ifdef G4VERBOSE
133 if ( fState.GetVerboseL4() ) {
134 fState.GetVerboseL4()->Message("create", "ntuple file", ntupleFileName);
135 }
136#endif
137
138 // update file name if it is already in use
139 while ( GetTFile(ntupleFileName, false) ) {
140 // the file is already in use
141 auto oldName = ntupleDescription->fFileName;
142 auto newName = GetBaseName(oldName) + "_bis." + GetExtension(oldName);
143 ntupleDescription->fFileName = newName;
144
145 G4ExceptionDescription description;
146 description
147 << "Ntuple filename " << oldName << " is already in use." << G4endl
148 << "It will be replaced with : " << newName;
149 G4Exception("G4CsvFileManager::CreateFileImpl()",
150 "Analysis_W001", JustWarning, description);
151
152 ntupleFileName = GetNtupleFileName(ntupleDescription);
153 }
154
155 ntupleDescription->fFile = CreateTFile(ntupleFileName);
156
157#ifdef G4VERBOSE
158 if ( fState.GetVerboseL2() ) {
159 fState.GetVerboseL2()->Message("create", "ntuple file", ntupleFileName);
160 }
161#endif
162
163 return (ntupleDescription->fFile != nullptr);
164}
165
166//_____________________________________________________________________________
168 CsvNtupleDescription* ntupleDescription)
169{
170 // Do nothing if there is no file
171 if ( ! ntupleDescription->fFile ) return true;
172
173 auto finalResult = true;
174
175 auto ntupleFileName = GetNtupleFileName(ntupleDescription);
176
177#ifdef G4VERBOSE
178 if ( fState.GetVerboseL4() ) {
179 fState.GetVerboseL4() ->Message("close", "ntuple file", ntupleFileName);
180 }
181#endif
182
183 // close file
184 auto result = CloseTFile(ntupleFileName);
185 finalResult = result && finalResult;
186
187 // Notify not empty file
188 result = SetIsEmpty(ntupleFileName, ! ntupleDescription->fHasFill);
189 finalResult = result && finalResult;
190
191 // Reset file info in ntuple description
192 ntupleDescription->fFile.reset();
193
194#ifdef G4VERBOSE
195 if ( fState.GetVerboseL2() ) {
196 fState.GetVerboseL2()->Message("close", "ntuple file", ntupleFileName);
197 }
198#endif
199
200 return finalResult;
201}
@ JustWarning
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
Definition: G4Exception.cc:35
std::ostringstream G4ExceptionDescription
Definition: G4Exception.hh:40
bool G4bool
Definition: G4Types.hh:86
#define G4endl
Definition: G4ios.hh:57
const G4AnalysisVerbose * GetVerboseL2() const
const G4AnalysisVerbose * GetVerboseL4() const
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
const G4AnalysisManagerState & fState
G4bool CreateNtupleFile(CsvNtupleDescription *ntupleDescription)
virtual std::shared_ptr< std::ofstream > CreateFileImpl(const G4String &fileName) final
virtual G4bool OpenFile(const G4String &fileName) final
virtual G4bool CloseFileImpl(std::shared_ptr< std::ofstream > file) final
G4CsvFileManager(const G4AnalysisManagerState &state)
virtual G4bool WriteFileImpl(std::shared_ptr< std::ofstream > file) final
G4bool CloseNtupleFile(CsvNtupleDescription *ntupleDescription)
virtual G4String GetFileType() const final
G4bool CloseTFile(const G4String &fileName)
std::shared_ptr< FT > GetTFile(const G4String &fileName, G4bool warn=true) const
std::shared_ptr< FT > 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
G4String GetExtension(const G4String &fileName, const G4String &defaultExtension="")
G4String GetTnFileName(const G4String &fileName, const G4String &fileType)
G4String GetBaseName(const G4String &fileName)
std::shared_ptr< TF > fFile
tools::ntuple_booking fNtupleBooking