Geant4-11
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 (ivana@ipno.in2p3.fr)
28
29#include "G4CsvFileManager.hh"
30#include "G4CsvHnFileManager.hh"
33#include "G4Filesystem.hh"
34
35using namespace G4Analysis;
36using namespace tools;
37
38//_____________________________________________________________________________
40 : G4VTFileManager(state)
41{
42 // Create helpers defined in the base class
43 fH1FileManager = std::make_shared<G4CsvHnFileManager<histo::h1d>>(this);
44 fH2FileManager = std::make_shared<G4CsvHnFileManager<histo::h2d>>(this);
45 fH3FileManager = std::make_shared<G4CsvHnFileManager<histo::h3d>>(this);
46 fP1FileManager = std::make_shared<G4CsvHnFileManager<histo::p1d>>(this);
47 fP2FileManager = std::make_shared<G4CsvHnFileManager<histo::p2d>>(this);
48}
49
50//
51// private methods
52//
53
54//_____________________________________________________________________________
56{
57 // get ntuple file name
58 auto ntupleFileName = ntupleDescription->fFileName;
59 if ( ntupleFileName.size() ) {
60 // update filename per object per thread
61 ntupleFileName = GetTnFileName(ntupleFileName, GetFileType());
62 } else {
63 // compose ntuple file name from the default file name
64 ntupleFileName = GetNtupleFileName(ntupleDescription->fNtupleBooking.name());
65 }
66
67 if ( IsNtupleDirectory() ) {
68 ntupleFileName = "./" + GetNtupleDirectoryName() + "/" + ntupleFileName;
69 }
70
71 return ntupleFileName;
72}
73
74//
75// protected methods
76//
77
78//_____________________________________________________________________________
79std::shared_ptr<std::ofstream> G4CsvFileManager::CreateFileImpl(const G4String& fileName)
80{
81 std::shared_ptr<std::ofstream> file = std::make_shared<std::ofstream>(fileName);
82 if ( file->fail() ) {
83 Warn("Cannot create file " + fileName, fkClass, "CreateFileImpl");
84 return nullptr;
85 }
86
87 return file;
88}
89
90//_____________________________________________________________________________
91G4bool G4CsvFileManager::WriteFileImpl(std::shared_ptr<std::ofstream> /*file*/)
92{
93 // Nothing to be done here
94 return true;
95}
96
97//_____________________________________________________________________________
98G4bool G4CsvFileManager::CloseFileImpl(std::shared_ptr<std::ofstream> file)
99{
100 if ( ! file ) return false;
101
102 // close file
103 file->close();
104
105 return true;
106}
107
108//
109// public methods
110//
111
112//_____________________________________________________________________________
114{
115 // Keep file name
116 fFileName = fileName;
117
118 fIsOpenFile = true;
119
120 return true;
121}
122
123//_____________________________________________________________________________
125{
126 // A directory is taken into account only if it exists in file system
127 if ( G4fs::is_directory(dirName.data()) ) {
129 return fIsHistoDirectory;
130 }
131
132 G4Analysis::Warn("Directory " + dirName + " does not exists.\n"
133 "Histograms will be written in the current directory.",
134 fkClass, "SetHistoDirectoryName");
135 return false;
136}
137
138//_____________________________________________________________________________
140{
141 // A directory is taken into account only if it exists in file system
142 if ( G4fs::is_directory(dirName.data()) ) {
144 return fIsNtupleDirectory;
145 }
146
147 G4Analysis::Warn("Directory " + dirName + " does not exists.\n"
148 "Ntuples will be written in the current directory.",
149 fkClass, "SetNtupleDirectoryName");
150 return false;
151}
152
153//_____________________________________________________________________________
155 CsvNtupleDescription* ntupleDescription)
156{
157 // get ntuple file name per object (if defined)
158 auto ntupleFileName = GetNtupleFileName(ntupleDescription);
159
160 Message(kVL4, "create", "ntuple file", ntupleFileName);
161
162 // update file name if it is already in use
163 while ( GetTFile(ntupleFileName, false) ) {
164 // the file is already in use
165 auto oldName = ntupleDescription->fFileName;
166 auto newName = GetBaseName(oldName) + "_bis." + GetExtension(oldName);
167 ntupleDescription->fFileName = newName;
168
169 Warn("Ntuple filename " + oldName + " is already in use.\n" +
170 "It will be replaced with : " + newName,
171 fkClass, "CreateNtupleFile");
172
173 ntupleFileName = GetNtupleFileName(ntupleDescription);
174 }
175
176 ntupleDescription->fFile = CreateTFile(ntupleFileName);
177
178 Message(kVL2, "create", "ntuple file", ntupleFileName);
179
180 return (ntupleDescription->fFile != nullptr);
181}
182
183//_____________________________________________________________________________
185 CsvNtupleDescription* ntupleDescription)
186{
187 // Do nothing if there is no file
188 if ( ! ntupleDescription->fFile ) return true;
189
190 auto result = true;
191
192 auto ntupleFileName = GetNtupleFileName(ntupleDescription);
193
194 Message(kVL4, "close", "ntuple file", ntupleFileName);
195
196 // close file
197 result &= CloseTFile(ntupleFileName);
198 // Notify not empty file
199 result &= SetIsEmpty(ntupleFileName, ! ntupleDescription->fHasFill);
200
201 // Reset file info in ntuple description
202 ntupleDescription->fFile.reset();
203
204 Message(kVL2, "close", "ntuple file", ntupleFileName);
205
206 return result;
207}
bool G4bool
Definition: G4Types.hh:86
void Message(G4int level, const G4String &action, const G4String &objectType, const G4String &objectName="", G4bool success=true) const
G4bool CreateNtupleFile(CsvNtupleDescription *ntupleDescription)
virtual std::shared_ptr< std::ofstream > CreateFileImpl(const G4String &fileName) final
virtual G4bool OpenFile(const G4String &fileName) final
virtual G4bool SetHistoDirectoryName(const G4String &dirName) final
G4CsvFileManager()=delete
virtual G4bool SetNtupleDirectoryName(const G4String &dirName) final
static constexpr std::string_view fkClass
virtual G4bool CloseFileImpl(std::shared_ptr< std::ofstream > file) final
G4bool IsNtupleDirectory() const
virtual G4bool WriteFileImpl(std::shared_ptr< std::ofstream > file) final
G4bool CloseNtupleFile(CsvNtupleDescription *ntupleDescription)
virtual G4String GetFileType() const final
G4String GetNtupleFileName(CsvNtupleDescription *ntupleDescription)
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)
G4String GetNtupleDirectoryName() const
std::shared_ptr< G4VTHnFileManager< tools::histo::h3d > > fH3FileManager
virtual G4bool SetHistoDirectoryName(const G4String &dirName)
std::shared_ptr< G4VTHnFileManager< tools::histo::h1d > > fH1FileManager
std::shared_ptr< G4VTHnFileManager< tools::histo::p1d > > fP1FileManager
std::shared_ptr< G4VTHnFileManager< tools::histo::p2d > > fP2FileManager
virtual G4bool SetNtupleDirectoryName(const G4String &dirName)
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="")
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)
tools::ntuple_booking fNtupleBooking
std::shared_ptr< FT > fFile