File indexing completed on 2025-01-18 09:58:05
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 #include "G4AnalysisUtilities.hh"
0030
0031 #include "tools/wcsv_histo"
0032 #include "tools/histo/h1d"
0033 #include "tools/histo/h2d"
0034 #include "tools/histo/h3d"
0035 #include "tools/histo/p1d"
0036 #include "tools/histo/p2d"
0037
0038
0039 template <>
0040 inline
0041 G4bool G4CsvHnFileManager<tools::histo::p1d>::Write(
0042 std::ofstream& hnfile, tools::histo::p1d* ht)
0043 {
0044 return tools::wcsv::pto(hnfile, ht->s_cls(), *ht);
0045 }
0046
0047
0048 template <>
0049 inline
0050 G4bool G4CsvHnFileManager<tools::histo::p2d>::Write(
0051 std::ofstream& hnfile, tools::histo::p2d* ht)
0052 {
0053 return tools::wcsv::pto(hnfile, ht->s_cls(), *ht);
0054 }
0055
0056
0057 template <typename HT>
0058 inline
0059 G4bool G4CsvHnFileManager<HT>::Write(
0060 std::ofstream& hnfile, HT* ht)
0061 {
0062 return tools::wcsv::hto(hnfile, ht->s_cls(), *ht);
0063 }
0064
0065
0066 template <typename HT>
0067 inline
0068 G4bool G4CsvHnFileManager<HT>::WriteExtra(
0069 HT* ht, const G4String& htName, const G4String& fileName)
0070 {
0071
0072 std::ofstream hnFile(fileName);
0073
0074
0075 if ( ! hnFile.is_open() ) return false;
0076
0077 auto result = Write(hnFile, ht);
0078
0079 if ( ! result ) {
0080 G4Analysis::Warn(
0081 "Saving " + G4Analysis::GetHnType<HT>() + " " + htName + " failed",
0082 fkClass, "WriteExtra");
0083 return false;
0084 }
0085 hnFile.close();
0086 return true;
0087 }
0088
0089
0090 template <typename HT>
0091 inline
0092 G4bool G4CsvHnFileManager<HT>::Write(
0093 HT* ht, const G4String& htName, G4String& fileName)
0094 {
0095 if ( fileName.empty() ) {
0096
0097 G4cerr << "!!! Csv file name not defined." << G4endl;
0098 G4cerr << "!!! Write " << htName << " failed." << G4endl;
0099 return false;
0100 }
0101
0102
0103 fileName = fFileManager->GetHnFileName(fileName, fFileManager->GetCycle());
0104
0105 auto hnFile = fFileManager->GetTFile(fileName, false);
0106 if ( ! hnFile ) {
0107
0108
0109 auto hnFileName = fFileManager->GetHnFileName(G4Analysis::GetHnType<HT>(), htName);
0110
0111
0112
0113 if ( fFileManager->IsHistoDirectory() ) {
0114 hnFileName = "./" + fFileManager->GetHistoDirectoryName() + "/" + hnFileName;
0115 }
0116
0117 if ( ! hnFileName.empty() ) {
0118 hnFile = fFileManager->CreateTFile(hnFileName);
0119 }
0120
0121 if ( ! hnFile ) {
0122 G4Analysis::Warn("Failed to get Csv file " + fileName, fkClass, "Write");
0123 return false;
0124 }
0125 fileName = hnFileName;
0126 }
0127
0128 return Write(*hnFile, ht);
0129 }