Geant4 11.2.2
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4GenericFileManager.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, 15/06/2011 ([email protected])
28
32#include "G4CsvFileManager.hh"
34#ifdef TOOLS_USE_HDF5
35#include "G4Hdf5FileManager.hh"
37#endif
38#include "G4RootFileManager.hh"
40#include "G4XmlFileManager.hh"
42
43using namespace G4Analysis;
44
45namespace {
46
47//_____________________________________________________________________________
48void FileManagerWarning(const G4String& fileName,
49 std::string_view className,
50 std::string_view functionName,
51 G4bool hdf5Warn = true)
52{
53 if ( GetExtension(fileName) == "hdf5" && ( ! hdf5Warn ) ) return;
54
55 Warn("Cannot get file manager for " + fileName,
56 className, functionName);
57}
58
59}
60
61//_____________________________________________________________________________
65
66//
67// private methods
68//
69
70//_____________________________________________________________________________
71void G4GenericFileManager::CreateFileManager(G4AnalysisOutput output)
72{
73 Message(kVL4, "create", "file manager", GetOutputName(output));
74
75 auto outputId = static_cast<size_t>(output);
76 if ( fFileManagers[outputId] ) {
77 Warn("The file manager of " + G4Analysis::GetOutputName(output) +
78 " type already exists.",
79 fkClass, "CreateFileManager");
80 return;
81 }
82
83 // Create the manager
84 switch ( output ) {
86 fCsvFileManager = std::make_shared<G4CsvFileManager>(fState);
87 fFileManagers[outputId] = fCsvFileManager;
88 break;
90#ifdef TOOLS_USE_HDF5
91 fHdf5FileManager = std::make_shared<G4Hdf5FileManager>(fState);
92 fFileManagers[outputId] = fHdf5FileManager;
93#else
94 if ( fHdf5Warn) {
95 Warn("Hdf5 type is not available.", fkClass, "CreateFileManager");
96 fHdf5Warn = false;
97 }
98#endif
99 break;
101 fRootFileManager = std::make_shared<G4RootFileManager>(fState);
102 fFileManagers[outputId] = fRootFileManager;
103 break;
105 fXmlFileManager = std::make_shared<G4XmlFileManager>(fState);
106 fFileManagers[outputId] = fXmlFileManager ;
107 break;
109 Warn(G4Analysis::GetOutputName(output) + " type is not supported.",
110 fkClass, "CreateFileManager");
111 return;
112 }
113
114 // Pass directory names (only if set)
115 if ( ! GetHistoDirectoryName().empty() ) {
116 fFileManagers[outputId]->SetHistoDirectoryName(GetHistoDirectoryName());
117 }
118 if ( ! GetNtupleDirectoryName().empty() ) {
119 fFileManagers[outputId]->SetNtupleDirectoryName(GetNtupleDirectoryName());
120 }
121
122 Message(kVL3, "create", "file manager", GetOutputName(output));
123}
124
125//_____________________________________________________________________________
126std::shared_ptr<G4VFileManager>
128{
129 return fFileManagers[static_cast<size_t>(output)];
130}
131
132//_____________________________________________________________________________
133std::shared_ptr<G4VFileManager>
135{
136 // Get file extension
137 G4String extension = GetExtension(fileName);
138 if (extension.size() == 0u) {
139 // use the default
140 extension = fDefaultFileType;
141 }
142
143 auto output = G4Analysis::GetOutput(extension);
144 if ( output == G4AnalysisOutput::kNone ) {
145 Warn("The file extension " + extension + "is not supported.",
146 fkClass, "GetFileManager");
147 return nullptr;
148 }
149
150 std::shared_ptr<G4VFileManager> fileManager = GetFileManager(output);
151 if ( ! GetFileManager(output) ) {
152 CreateFileManager(output);
153 fileManager = GetFileManager(output);
154 }
155
156 return GetFileManager(output);
157}
158
159//
160// public methods
161//
162
163//_____________________________________________________________________________
165{
166 auto fileManager = GetFileManager(fileName);
167 if ( ! fileManager ) return false;
168
169 if ( fDefaultFileManager && (fDefaultFileManager != fileManager) ) {
170 // Print warning if default output changed
171 // (maybe be not needed?)
172 Warn("Default file manager changed "
173 "(old: " +fDefaultFileManager->GetFileType() +
174 ", new:" + fileManager->GetFileType() + ")",
175 fkClass, "OpenFile");
176 }
177 fDefaultFileManager = fileManager;
178 fDefaultFileType = fileManager->GetFileType();
179
180 Message(kVL4, "open", "analysis file", fileName);
181
182 auto result = true;
183
184 // Save the default file name
185 // both in the generic file manager and the output specific one
186 result &= SetFileName(fileName);
187 result &= fDefaultFileManager->SetFileName(fileName);
188 result &= fDefaultFileManager->OpenFile(fileName);
189
191 fIsOpenFile = true;
192
193 Message(kVL1, "open", "analysis file", fileName, result);
194
195 return result;
196}
197
198//_____________________________________________________________________________
200{
201// Open all files regeistered with objects
202
203 Message(kVL4, "open", "analysis files");
204
205 auto result = true;
206
207 // process names registered in base file manager
208 for ( const auto& fileName : GetFileNames() ) {
209 auto fileManager = GetFileManager(fileName);
210 if ( ! fileManager ) {
211 FileManagerWarning(fileName, fkClass, "OpenFiles", fHdf5Warn);
212 continue;
213 }
214
215 // filenames for csv need to be updated
216 auto newFileName = fileName;
217 if (fileManager == fCsvFileManager) {
218 newFileName = fileManager->GetHnFileName(fileName, GetCycle());
219 }
220
221 result &= fileManager->CreateFile(newFileName);
222 }
223
224 Message(kVL3, "open", "analysis files", "", result);
225
226 return result;
227}
228
229//_____________________________________________________________________________
231{
232// Finish write for all files registered with objects
233
234 Message(kVL4, "write", "analysis files");
235
236 auto result = true;
237
238 for ( const auto& fileManager : fFileManagers ) {
239 if ( ! fileManager ) continue;
240
241 Message(kVL4, "write", fileManager->GetFileType(), "files");
242
243 result &= fileManager->WriteFiles();
244 }
245
246 Message(kVL3, "write", "analysis files", "", result);
247
248 return result;
249}
250
251//_____________________________________________________________________________
253{
254// Close all files regeistered with objects
255
256 Message(kVL4, "close", "analysis files");
257
258 auto result = true;
259
260 for ( const auto& fileManager : fFileManagers ) {
261 if ( ! fileManager ) continue;
262
263 Message(kVL4, "close", fileManager->GetFileType(), "files");
264
265 result &= fileManager->CloseFiles();
266 }
267
268 fIsOpenFile = false;
269
270 Message(kVL3, "close", "analysis files", "", result);
271
272 return result;
273}
274
275//_____________________________________________________________________________
277{
278// Close all files regeistered with objects
279
280 Message(kVL4, "delete", "empty files");
281
282 auto result = true;
283
284 for ( const auto& fileManager : fFileManagers ) {
285 if ( ! fileManager ) continue;
286
287 Message(kVL4, "delete", fileManager->GetFileType(), "empty files");
288
289 result &= fileManager->DeleteEmptyFiles();
290 }
291
292 // Clear all saved file names in base file manager
294
295 Message(kVL3, "delete", "empty files", "", result);
296
297 return result;
298}
299
300//_____________________________________________________________________________
302{
303// Clear files data
304
305 for ( const auto& fileManager : fFileManagers ) {
306 if ( ! fileManager ) continue;
307
308 fileManager->Clear();
309 }
311}
312
313//_____________________________________________________________________________
315{
316// New prototype, fully implemented in templated base class
317
318 auto fileManager = GetFileManager(fileName);
319 if ( ! fileManager ) {
320 FileManagerWarning(fileName, fkClass, "CreateFile", fHdf5Warn);
321 return false;
322 }
323
324 return fileManager->CreateFile(fileName);
325}
326
327//_____________________________________________________________________________
329{
330// New prototype, fully implemented in templated base class
331
332 auto fileManager = GetFileManager(fileName);
333 if ( ! fileManager ) {
334 FileManagerWarning(fileName, fkClass, "WriteFile", fHdf5Warn);
335 return false;
336 }
337
338 return fileManager->WriteFile(fileName);
339}
340
341//_____________________________________________________________________________
343{
344// New prototype, fully implemented in templated base class
345
346 auto fileManager = GetFileManager(fileName);
347 if ( ! fileManager ) {
348 FileManagerWarning(fileName, fkClass, "CloseFile", fHdf5Warn);
349 return false;
350 }
351
352 return fileManager->CloseFile(fileName);
353}
354
355//_____________________________________________________________________________
357{
358 auto fileManager = GetFileManager(fileName);
359 if ( ! fileManager ) {
360 FileManagerWarning(fileName, fkClass, "SetIsEmpty", fHdf5Warn);
361 return false;
362 }
363
364 return fileManager->SetIsEmpty(fileName, isEmpty);
365}
366
367//_____________________________________________________________________________
369{
370 auto result = G4VFileManager::SetHistoDirectoryName(dirName);
371
372 for (auto& fileManager : fFileManagers ) {
373 if ( fileManager != nullptr ) {
374 result &= fileManager->SetHistoDirectoryName(dirName);
375 }
376 }
377 return result;
378}
379
380//_____________________________________________________________________________
382{
383 auto result = G4VFileManager::SetNtupleDirectoryName(dirName);
384
385 for (auto& fileManager : fFileManagers ) {
386 if ( fileManager != nullptr ) {
387 result &= fileManager->SetNtupleDirectoryName(dirName);
388 }
389 }
390 return result;
391}
392
393//_____________________________________________________________________________
395{
396 // Check if value correspond to a valid file type
397 auto output = G4Analysis::GetOutput(value);
398 if ( output == G4AnalysisOutput::kNone ) {
399 Warn("The file type " + value + "is not supported.\n" +
400 "The default type " + fDefaultFileType + " will be used.",
401 fkClass, "SetDeafultFileType");
402 return;
403 }
404
405 fDefaultFileType = value;
406}
407
408//_____________________________________________________________________________
409std::shared_ptr<G4VNtupleFileManager>
411{
412 if ( ! GetFileManager(output) ) {
413 CreateFileManager(output);
414 }
415
416 std::shared_ptr<G4VNtupleFileManager> vNtupleFileManager = nullptr;
417 G4String failure;
418
419 switch ( output ) {
421 auto ntupleFileManager = std::make_shared<G4CsvNtupleFileManager>(fState);
422 ntupleFileManager->SetFileManager(fCsvFileManager);
423 vNtupleFileManager = ntupleFileManager;
424 break;
425 }
427#ifdef TOOLS_USE_HDF5
428 auto ntupleFileManager = std::make_shared<G4Hdf5NtupleFileManager>(fState);
429 ntupleFileManager->SetFileManager(fHdf5FileManager);
430 vNtupleFileManager = ntupleFileManager;
431#else
432 failure = " Hdf5 is not available";
433#endif
434 break;
435 }
437 auto ntupleFileManager = std::make_shared<G4RootNtupleFileManager>(fState);
438 ntupleFileManager->SetFileManager(fRootFileManager);
439 vNtupleFileManager = ntupleFileManager;
440 break;
441 }
443 auto ntupleFileManager = std::make_shared<G4XmlNtupleFileManager>(fState);
444 ntupleFileManager->SetFileManager(fXmlFileManager);
445 vNtupleFileManager = ntupleFileManager;
446 break;
447 }
449 break;
450 }
451
452 if ( ! vNtupleFileManager ) {
453 Warn("Failed to create ntuple file manager of " +
454 G4Analysis::GetOutputName(output) + " type.\n" + failure,
455 fkClass, "CreateNtupleFileManager");
456 }
457
458 return vNtupleFileManager;
459}
bool G4bool
Definition G4Types.hh:86
const std::vector< G4String > & GetFileNames() const
void Message(G4int level, const G4String &action, const G4String &objectType, const G4String &objectName="", G4bool success=true) const
const G4AnalysisManagerState & fState
G4bool SetIsEmpty(const G4String &fileName, G4bool isEmpty) final
G4bool CreateFile(const G4String &fileName) final
std::shared_ptr< G4VNtupleFileManager > CreateNtupleFileManager(G4AnalysisOutput output)
virtual G4bool OpenFiles() final
G4bool CloseFile(const G4String &fileName) final
std::shared_ptr< G4VFileManager > GetFileManager(const G4String &fileName)
G4bool OpenFile(const G4String &fileName) final
G4bool SetNtupleDirectoryName(const G4String &dirName) override
void SetDefaultFileType(const G4String &value)
G4bool SetHistoDirectoryName(const G4String &dirName) override
G4bool WriteFile(const G4String &fileName) final
G4GenericFileManager(const G4AnalysisManagerState &state)
G4String GetNtupleDirectoryName() const
G4int GetCycle() const
G4String GetHistoDirectoryName() const
virtual G4bool SetHistoDirectoryName(const G4String &dirName)
G4bool SetFileName(const G4String &fileName) final
virtual G4bool SetNtupleDirectoryName(const G4String &dirName)
void UnlockDirectoryNames()
G4String GetExtension(const G4String &fileName, const G4String &defaultExtension="")
constexpr G4int kVL1
G4String GetOutputName(G4AnalysisOutput outputType)
constexpr G4int kVL3
G4AnalysisOutput GetOutput(const G4String &outputName, G4bool warn=true)
constexpr G4int kVL4
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)