Geant4 10.7.0
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
43#include <iostream>
44#include <cstdio>
45
46using namespace G4Analysis;
47
48namespace {
49
50void FileManagerException(const G4String& fileName, const G4String& functionName,
51 const G4String& exceptionClassification, G4bool hdf5Warn = true)
52{
53 if ( GetExtension(fileName) == "hdf5" && ( ! hdf5Warn ) ) return;
54
55 G4String where = "G4GenericFileManager::" + functionName;
56 G4String what = "Analysis_" + exceptionClassification;
57 G4ExceptionDescription description;
58 description << "Cannot get file manager for " << fileName;
59 G4Exception(where, what, JustWarning, description);
60}
61
62}
63
64// static data
65const G4String G4GenericFileManager::fgkDefaultFileType = "root";
66
67//_____________________________________________________________________________
69 : G4VFileManager(state),
70 fDefaultFileType(fgkDefaultFileType),
71 fDefaultFileManager(nullptr),
72 fFileManagers
73 { nullptr, // Csv
74 nullptr, // Hdf5
75 nullptr, // Generic
76 nullptr // Xml
77 },
78 fCsvFileManager(nullptr),
79#ifdef TOOLS_USE_HDF5
80 fHdf5FileManager(nullptr),
81#endif
82 fRootFileManager(nullptr),
83 fXmlFileManager(nullptr),
84 fHdf5Warn(true)
85{}
86
87//_____________________________________________________________________________
89{}
90
91//
92// private methods
93//
94
95//_____________________________________________________________________________
96void G4GenericFileManager::CreateFileManager(G4AnalysisOutput output)
97{
98#ifdef G4VERBOSE
99 if ( fState.GetVerboseL4() ) {
100 fState.GetVerboseL4()->Message("create", "file manager", GetOutputName(output));
101 }
102#endif
103
104 auto outputId = static_cast<size_t>(output);
105 if ( fFileManagers[outputId] ) {
106 G4ExceptionDescription description;
107 description
108 << " "
109 << "The file manager of " << G4Analysis::GetOutputName(output) << " type already exists.";
110 G4Exception("G4GenericFileManager::CreateFileManager",
111 "Analysis_W002", JustWarning, description);
112 return;
113 }
114
115 // Create the manager
116 switch ( output ) {
117 case G4AnalysisOutput::kCsv:
118 fCsvFileManager = std::make_shared<G4CsvFileManager>(fState);
119 fFileManagers[outputId] = fCsvFileManager;
120 break;
121 case G4AnalysisOutput::kHdf5:
122#ifdef TOOLS_USE_HDF5
123 fHdf5FileManager = std::make_shared<G4Hdf5FileManager>(fState);
124 fFileManagers[outputId] = fHdf5FileManager;
125#else
126 if ( fHdf5Warn) {
127 G4ExceptionDescription description;
128 description << "Hdf5 type is not available.";
129 G4Exception("G4GenericFileManager::CreateFileManager",
130 "Analysis_W051", JustWarning, description);
131 fHdf5Warn = false;
132 }
133#endif
134 break;
135 case G4AnalysisOutput::kRoot:
136 fRootFileManager = std::make_shared<G4RootFileManager>(fState);
137 fFileManagers[outputId] = fRootFileManager;
138 break;
139 case G4AnalysisOutput::kXml:
140 fXmlFileManager = std::make_shared<G4XmlFileManager>(fState);
141 fFileManagers[outputId] = fXmlFileManager ;
142 break;
143 case G4AnalysisOutput::kNone:
144 G4ExceptionDescription description;
145 description
146 << G4Analysis::GetOutputName(output) << " type is not supported.";
147 G4Exception("G4GenericFileManager::CreateFileManager",
148 "Analysis_W051", JustWarning, description);
149 break;
150 }
151
152#ifdef G4VERBOSE
153 if ( fState.GetVerboseL3() ) {
154 fState.GetVerboseL3()->Message("create", "file manager", GetOutputName(output));
155 }
156#endif
157}
158
159//_____________________________________________________________________________
160std::shared_ptr<G4VFileManager>
161G4GenericFileManager::GetFileManager(G4AnalysisOutput output) const
162{
163 return fFileManagers[static_cast<size_t>(output)];
164}
165
166//_____________________________________________________________________________
167std::shared_ptr<G4VFileManager>
168G4GenericFileManager::GetFileManager(const G4String& fileName)
169{
170 // Get file extension
171 G4String extension = GetExtension(fileName);
172 if ( ! extension.size() ) {
173 // use the default
174 extension = fDefaultFileType;
175 }
176
177 auto output = G4Analysis::GetOutput(extension);
178 if ( output == G4AnalysisOutput::kNone ) {
179 G4ExceptionDescription description;
180 description
181 << " "
182 << "The file extension " << extension << "is not supported.";
183 G4Exception("G4GenericFileManager::GetFileManager",
184 "Analysis_W051", JustWarning, description);
185 return nullptr;
186 }
187
188 std::shared_ptr<G4VFileManager> fileManager = GetFileManager(output);
189 if ( ! GetFileManager(output) ) {
190 CreateFileManager(output);
191 fileManager = GetFileManager(output);
192 }
193
194 return GetFileManager(output);
195}
196
197//
198// public methods
199//
200
201//_____________________________________________________________________________
203{
204 auto fileManager = GetFileManager(fileName);
205 if ( ! fileManager ) return false;
206
207 if ( fDefaultFileManager && (fDefaultFileManager != fileManager) ) {
208 // Print warning if default output changed
209 // (maybe be not needed?)
210 G4ExceptionDescription description;
211 description
212 << "Default file manager changed (old: "
213 << fDefaultFileManager->GetFileType()
214 << ", new:" << fileManager->GetFileType() << ")";
215 G4Exception("G4GenericFileManager::OpenFile",
216 "Analysis_W001", JustWarning, description);
217 }
218 fDefaultFileManager = fileManager;
219
220#ifdef G4VERBOSE
221 if ( fState.GetVerboseL4() ) {
222 fState.GetVerboseL4()->Message("open", "analysis file", fileName);
223 }
224#endif
225
226 auto finalResult = true;
227 auto result = true;
228
229 // Save the default file name
230 // both in the generic file manager and the output specific one
231 result = SetFileName(fileName);
232 finalResult = finalResult && result;
233 result = fDefaultFileManager->SetFileName(fileName);
234 finalResult = finalResult && result;
235
236 result = fDefaultFileManager->OpenFile(fileName);
237 finalResult = finalResult && result;
238
239#ifdef G4VERBOSE
240 if ( fState.GetVerboseL1() ) {
241 fState.GetVerboseL1()->Message("open", "analysis file", fileName, finalResult);
242 }
243#endif
244
245 fLockDirectoryNames = true;
246 fIsOpenFile = true;
247
248 return finalResult;
249}
250
251//_____________________________________________________________________________
253{
254// Open all files regeistered with objects
255
256#ifdef G4VERBOSE
257 if ( fState.GetVerboseL4() ) {
258 fState.GetVerboseL4()->Message("open", "analysis files", "");
259 }
260#endif
261
262 auto finalResult = true;
263 auto result = true;
264
265 // process names registered in base file manager
266 for ( auto fileName : GetFileNames() ) {
267 auto fileManager = GetFileManager(fileName);
268 if ( ! fileManager ) {
269 FileManagerException(fileName, "OpenFiles", "W001", fHdf5Warn);
270 continue;
271 }
272
273 result = fileManager->CreateFile(fileName);
274 finalResult = result && finalResult;
275 }
276
277#ifdef G4VERBOSE
278 if ( fState.GetVerboseL3() ) {
279 fState.GetVerboseL3()->Message("open", "analysis files", "", finalResult);
280 }
281#endif
282
283 return finalResult;
284}
285
286//_____________________________________________________________________________
288{
289// Finish write for all files regeistered with objects
290
291#ifdef G4VERBOSE
292 if ( fState.GetVerboseL4() ) {
293 fState.GetVerboseL4()->Message("write", "files", "");
294 }
295#endif
296
297 auto finalResult = true;
298 auto result = true;
299
300 for ( auto fileManager : fFileManagers ) {
301 if ( ! fileManager ) continue;
302
303#ifdef G4VERBOSE
304 if ( fState.GetVerboseL4() ) {
305 fState.GetVerboseL4()->Message("write", fileManager->GetFileType(), "files");
306 }
307#endif
308
309 result = fileManager->WriteFiles();
310 finalResult = result && finalResult;
311 }
312
313#ifdef G4VERBOSE
314 if ( fState.GetVerboseL3() ) {
315 fState.GetVerboseL3()->Message("write", "files", "", finalResult);
316 }
317#endif
318
319 return finalResult;
320}
321
322//_____________________________________________________________________________
324{
325// Close all files regeistered with objects
326
327#ifdef G4VERBOSE
328 if ( fState.GetVerboseL4() ) {
329 fState.GetVerboseL4()->Message("close", "files", "");
330 }
331#endif
332
333 auto finalResult = true;
334 auto result = true;
335
336 for ( auto fileManager : fFileManagers ) {
337 if ( ! fileManager ) continue;
338
339#ifdef G4VERBOSE
340 if ( fState.GetVerboseL4() ) {
341 fState.GetVerboseL4()->Message("close", fileManager->GetFileType(), "files");
342 }
343#endif
344
345 result = fileManager->CloseFiles();
346 finalResult = result && finalResult;
347 }
348
349#ifdef G4VERBOSE
350 if ( fState.GetVerboseL3() ) {
351 fState.GetVerboseL3()->Message("close", "files", "", finalResult);
352 }
353#endif
354
355 return finalResult;
356}
357
358//_____________________________________________________________________________
360{
361// Close all files regeistered with objects
362
363#ifdef G4VERBOSE
364 if ( fState.GetVerboseL4() ) {
365 fState.GetVerboseL4()->Message("delete", "empty files", "");
366 }
367#endif
368
369 auto finalResult = true;
370 auto result = true;
371
372 for ( auto fileManager : fFileManagers ) {
373 if ( ! fileManager ) continue;
374
375#ifdef G4VERBOSE
376 if ( fState.GetVerboseL4() ) {
377 fState.GetVerboseL4()->Message("delete", fileManager->GetFileType(), "files");
378 }
379#endif
380
381 result = fileManager->DeleteEmptyFiles();
382 finalResult = result && finalResult;
383 }
384
385#ifdef G4VERBOSE
386 if ( fState.GetVerboseL3() ) {
387 fState.GetVerboseL3()->Message("delete", "empty files", "", finalResult);
388 }
389#endif
390
391 return finalResult;
392}
393
394//_____________________________________________________________________________
396{
397// New prototype, fully implemented in templated base class
398
399 auto fileManager = GetFileManager(fileName);
400 if ( ! fileManager ) {
401 FileManagerException(fileName, "CreateFile", "W001", fHdf5Warn);
402 return false;
403 }
404
405 return fileManager->CreateFile(fileName);
406}
407
408//_____________________________________________________________________________
410{
411// New prototype, fully implemented in templated base class
412
413 auto fileManager = GetFileManager(fileName);
414 if ( ! fileManager ) {
415 FileManagerException(fileName, "WriteFile", "W021", fHdf5Warn);
416 return false;
417 }
418
419 return fileManager->WriteFile(fileName);
420}
421
422//_____________________________________________________________________________
424{
425// New prototype, fully implemented in templated base class
426
427 auto fileManager = GetFileManager(fileName);
428 if ( ! fileManager ) {
429 FileManagerException(fileName, "CloseFile", "W021", fHdf5Warn);
430 return false;
431 }
432
433 return fileManager->CloseFile(fileName);
434}
435
436//_____________________________________________________________________________
438{
439 auto fileManager = GetFileManager(fileName);
440 if ( ! fileManager ) {
441 FileManagerException(fileName, "SetIsEmpty", "W021", fHdf5Warn);
442 return false;
443 }
444
445 return fileManager->SetIsEmpty(fileName, isEmpty);
446}
447
448//_____________________________________________________________________________
450{
451 // Check if value correspond to a valid file type
452 auto output = G4Analysis::GetOutput(value);
453 if ( output == G4AnalysisOutput::kNone ) {
454 G4ExceptionDescription description;
455 description
456 << "The file type " << value << "is not supported." << G4endl
457 << "The default type " << fDefaultFileType << " will be used.";
458 G4Exception("G4GenericFileManager::SetDeafultFileType",
459 "Analysis_W051", JustWarning, description);
460 return;
461 }
462
463 fDefaultFileType = value;
464}
465
466//_____________________________________________________________________________
467std::shared_ptr<G4VNtupleFileManager>
469{
470 if ( ! GetFileManager(output) ) {
471 CreateFileManager(output);
472 }
473
474 std::shared_ptr<G4VNtupleFileManager> vNtupleFileManager = nullptr;
475 G4String failure;
476
477 switch ( output ) {
478 case G4AnalysisOutput::kCsv: {
479 auto ntupleFileManager = std::make_shared<G4CsvNtupleFileManager>(fState);
480 ntupleFileManager->SetFileManager(fCsvFileManager);
481 vNtupleFileManager = ntupleFileManager;
482 break;
483 }
484 case G4AnalysisOutput::kHdf5: {
485#ifdef TOOLS_USE_HDF5
486 auto ntupleFileManager = std::make_shared<G4Hdf5NtupleFileManager>(fState);
487 ntupleFileManager->SetFileManager(fHdf5FileManager);
488 vNtupleFileManager = ntupleFileManager;
489#else
490 failure = " Hdf5 is not available";
491#endif
492 break;
493 }
494 case G4AnalysisOutput::kRoot: {
495 auto ntupleFileManager = std::make_shared<G4RootNtupleFileManager>(fState);
496 ntupleFileManager->SetFileManager(fRootFileManager);
497 vNtupleFileManager = ntupleFileManager;
498 break;
499 }
500 case G4AnalysisOutput::kXml: {
501 auto ntupleFileManager = std::make_shared<G4XmlNtupleFileManager>(fState);
502 ntupleFileManager->SetFileManager(fXmlFileManager);
503 vNtupleFileManager = ntupleFileManager;
504 break;
505 }
506 case G4AnalysisOutput::kNone:
507 break;
508 }
509
510 if ( ! vNtupleFileManager ) {
511 G4ExceptionDescription description;
512 description
513 << " "
514 << "Failed to create ntuple file manager of " << G4Analysis::GetOutputName(output) << " type."
515 << failure;
516 G4Exception("G4GenericFileManager::CreateNtupleFileManager",
517 "Analysis_W002", JustWarning, description);
518 }
519
520 return vNtupleFileManager;
521}
G4AnalysisOutput
@ 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 * GetVerboseL3() const
const G4AnalysisVerbose * GetVerboseL1() const
const G4AnalysisVerbose * GetVerboseL4() const
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
const std::vector< G4String > & GetFileNames() const
const G4AnalysisManagerState & fState
virtual G4bool SetIsEmpty(const G4String &fileName, G4bool isEmpty) final
virtual G4bool WriteFiles() final
virtual G4bool CreateFile(const G4String &fileName) final
std::shared_ptr< G4VNtupleFileManager > CreateNtupleFileManager(G4AnalysisOutput output)
virtual G4bool OpenFiles() final
virtual G4bool CloseFile(const G4String &fileName) final
virtual G4bool OpenFile(const G4String &fileName) final
void SetDefaultFileType(const G4String &value)
virtual G4bool WriteFile(const G4String &fileName) final
G4GenericFileManager(const G4AnalysisManagerState &state)
virtual G4bool CloseFiles() final
virtual G4bool DeleteEmptyFiles() final
G4bool fLockDirectoryNames
virtual G4bool SetFileName(const G4String &fileName) final
G4String GetExtension(const G4String &fileName, const G4String &defaultExtension="")
G4String GetOutputName(G4AnalysisOutput outputType)
G4AnalysisOutput GetOutput(const G4String &outputName, G4bool warn=true)