48void FileManagerWarning(
const G4String& fileName,
49 std::string_view className,
50 std::string_view functionName,
53 if (
GetExtension(fileName) ==
"hdf5" && ( ! hdf5Warn ) )
return;
55 Warn(
"Cannot get file manager for " + fileName,
56 className, functionName);
75 auto outputId =
static_cast<size_t>(output);
76 if ( fFileManagers[outputId] ) {
78 " type already exists.",
79 fkClass,
"CreateFileManager");
85 case G4AnalysisOutput::kCsv:
86 fCsvFileManager = std::make_shared<G4CsvFileManager>(
fState);
87 fFileManagers[outputId] = fCsvFileManager;
89 case G4AnalysisOutput::kHdf5:
91 fHdf5FileManager = std::make_shared<G4Hdf5FileManager>(
fState);
92 fFileManagers[outputId] = fHdf5FileManager;
95 Warn(
"Hdf5 type is not available.", fkClass,
"CreateFileManager");
100 case G4AnalysisOutput::kRoot:
101 fRootFileManager = std::make_shared<G4RootFileManager>(
fState);
102 fFileManagers[outputId] = fRootFileManager;
104 case G4AnalysisOutput::kXml:
105 fXmlFileManager = std::make_shared<G4XmlFileManager>(
fState);
106 fFileManagers[outputId] = fXmlFileManager ;
108 case G4AnalysisOutput::kNone:
110 fkClass,
"CreateFileManager");
126std::shared_ptr<G4VFileManager>
129 return fFileManagers[
static_cast<size_t>(output)];
133std::shared_ptr<G4VFileManager>
138 if (extension.size() == 0u) {
140 extension = fDefaultFileType;
144 if ( output == G4AnalysisOutput::kNone ) {
145 Warn(
"The file extension " + extension +
"is not supported.",
146 fkClass,
"GetFileManager");
150 std::shared_ptr<G4VFileManager> fileManager =
GetFileManager(output);
152 CreateFileManager(output);
167 if ( ! fileManager )
return false;
169 if ( fDefaultFileManager && (fDefaultFileManager != fileManager) ) {
172 Warn(
"Default file manager changed "
173 "(old: " +fDefaultFileManager->GetFileType() +
174 ", new:" + fileManager->GetFileType() +
")",
175 fkClass,
"OpenFile");
177 fDefaultFileManager = fileManager;
178 fDefaultFileType = fileManager->GetFileType();
187 result &= fDefaultFileManager->SetFileName(fileName);
188 result &= fDefaultFileManager->OpenFile(fileName);
193 Message(
kVL1,
"open",
"analysis file", fileName, result);
210 if ( ! fileManager ) {
211 FileManagerWarning(fileName, fkClass,
"OpenFiles", fHdf5Warn);
216 auto newFileName = fileName;
217 if (fileManager == fCsvFileManager) {
218 newFileName = fileManager->GetHnFileName(fileName,
GetCycle());
221 result &= fileManager->CreateFile(newFileName);
224 Message(
kVL3,
"open",
"analysis files",
"", result);
238 for (
const auto& fileManager : fFileManagers ) {
239 if ( ! fileManager )
continue;
241 Message(
kVL4,
"write", fileManager->GetFileType(),
"files");
243 result &= fileManager->WriteFiles();
246 Message(
kVL3,
"write",
"analysis files",
"", result);
260 for (
const auto& fileManager : fFileManagers ) {
261 if ( ! fileManager )
continue;
263 Message(
kVL4,
"close", fileManager->GetFileType(),
"files");
265 result &= fileManager->CloseFiles();
270 Message(
kVL3,
"close",
"analysis files",
"", result);
284 for (
const auto& fileManager : fFileManagers ) {
285 if ( ! fileManager )
continue;
287 Message(
kVL4,
"delete", fileManager->GetFileType(),
"empty files");
289 result &= fileManager->DeleteEmptyFiles();
292 Message(
kVL3,
"delete",
"empty files",
"", result);
302 for (
const auto& fileManager : fFileManagers ) {
303 if ( ! fileManager )
continue;
305 fileManager->Clear();
316 if ( ! fileManager ) {
317 FileManagerWarning(fileName, fkClass,
"CreateFile", fHdf5Warn);
321 return fileManager->CreateFile(fileName);
330 if ( ! fileManager ) {
331 FileManagerWarning(fileName, fkClass,
"WriteFile", fHdf5Warn);
335 return fileManager->WriteFile(fileName);
344 if ( ! fileManager ) {
345 FileManagerWarning(fileName, fkClass,
"CloseFile", fHdf5Warn);
349 return fileManager->CloseFile(fileName);
356 if ( ! fileManager ) {
357 FileManagerWarning(fileName, fkClass,
"SetIsEmpty", fHdf5Warn);
361 return fileManager->SetIsEmpty(fileName, isEmpty);
369 for (
auto& fileManager : fFileManagers ) {
370 if ( fileManager !=
nullptr ) {
371 result &= fileManager->SetHistoDirectoryName(dirName);
382 for (
auto& fileManager : fFileManagers ) {
383 if ( fileManager !=
nullptr ) {
384 result &= fileManager->SetNtupleDirectoryName(dirName);
395 if ( output == G4AnalysisOutput::kNone ) {
396 Warn(
"The file type " + value +
"is not supported.\n" +
397 "The default type " + fDefaultFileType +
" will be used.",
398 fkClass,
"SetDeafultFileType");
402 fDefaultFileType = value;
406std::shared_ptr<G4VNtupleFileManager>
410 CreateFileManager(output);
413 std::shared_ptr<G4VNtupleFileManager> vNtupleFileManager =
nullptr;
417 case G4AnalysisOutput::kCsv: {
418 auto ntupleFileManager = std::make_shared<G4CsvNtupleFileManager>(
fState);
419 ntupleFileManager->SetFileManager(fCsvFileManager);
420 vNtupleFileManager = ntupleFileManager;
423 case G4AnalysisOutput::kHdf5: {
425 auto ntupleFileManager = std::make_shared<G4Hdf5NtupleFileManager>(
fState);
426 ntupleFileManager->SetFileManager(fHdf5FileManager);
427 vNtupleFileManager = ntupleFileManager;
429 failure =
" Hdf5 is not available";
433 case G4AnalysisOutput::kRoot: {
434 auto ntupleFileManager = std::make_shared<G4RootNtupleFileManager>(
fState);
435 ntupleFileManager->SetFileManager(fRootFileManager);
436 vNtupleFileManager = ntupleFileManager;
439 case G4AnalysisOutput::kXml: {
440 auto ntupleFileManager = std::make_shared<G4XmlNtupleFileManager>(
fState);
441 ntupleFileManager->SetFileManager(fXmlFileManager);
442 vNtupleFileManager = ntupleFileManager;
445 case G4AnalysisOutput::kNone:
449 if ( ! vNtupleFileManager ) {
450 Warn(
"Failed to create ntuple file manager of " +
452 fkClass,
"CreateNtupleFileManager");
455 return vNtupleFileManager;
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 WriteFiles() 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)
G4bool CloseFiles() final
G4bool DeleteEmptyFiles() final
G4String GetNtupleDirectoryName() const
G4String GetHistoDirectoryName() const
virtual G4bool SetHistoDirectoryName(const G4String &dirName)
G4bool SetFileName(const G4String &fileName) final
virtual G4bool SetNtupleDirectoryName(const G4String &dirName)
void LockDirectoryNames()
void UnlockDirectoryNames()
G4String GetExtension(const G4String &fileName, const G4String &defaultExtension="")
G4String GetOutputName(G4AnalysisOutput outputType)
G4AnalysisOutput GetOutput(const G4String &outputName, G4bool warn=true)
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)