Warning, file /include/Geant4/G4TFileManager.icc was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
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
0032 template <typename FT>
0033 inline
0034 G4TFileManager<FT>::G4TFileManager(const G4AnalysisManagerState& state)
0035 : fAMState(state)
0036 {}
0037
0038
0039 template <typename FT>
0040 inline
0041 G4TFileManager<FT>::~G4TFileManager()
0042 {
0043 for ( const auto& mapElement : fFileMap ) {
0044 delete mapElement.second;
0045 }
0046 }
0047
0048
0049
0050
0051
0052
0053 template <typename FT>
0054 inline
0055 void
0056 G4TFileManager<FT>::FileNotFoundWarning(const G4String& fileName,
0057 std::string_view functionName) const
0058 {
0059 G4Analysis::Warn("Failed to get file " + fileName, fkClass, functionName);
0060 }
0061
0062
0063 template <typename FT>
0064 inline
0065 G4TFileInformation<FT>*
0066 G4TFileManager<FT>::GetFileInfoInFunction(const G4String& fileName,
0067 std::string_view functionName, G4bool warn ) const
0068 {
0069
0070 auto it = fFileMap.find(fileName);
0071 if ( it == fFileMap.end() ) {
0072 if (warn) {
0073 FileNotFoundWarning(fileName, functionName);
0074 }
0075 return nullptr;
0076 }
0077
0078 return it->second;
0079 }
0080
0081
0082 template <typename FT>
0083 inline
0084 std::shared_ptr<FT>
0085 G4TFileManager<FT>::GetFileInFunction(const G4String& fileName,
0086 std::string_view functionName, G4bool warn) const
0087 {
0088
0089 auto fileInfo = GetFileInfoInFunction(fileName, functionName, warn);
0090 if (! fileInfo) return nullptr;
0091
0092
0093 if ( ! fileInfo->fFile ) {
0094 if (warn) {
0095 FileNotFoundWarning(fileName, functionName);
0096 }
0097 return nullptr;
0098 }
0099
0100 return fileInfo->fFile;
0101 }
0102
0103
0104 template <typename FT>
0105 inline
0106 G4bool G4TFileManager<FT>::WriteTFile(std::shared_ptr<FT> file,
0107 [[maybe_unused]] const G4String& fileName)
0108 {
0109 fAMState.Message(G4Analysis::kVL4, "write", "file", fileName);
0110
0111
0112 auto result = WriteFileImpl(file);
0113
0114 fAMState.Message(G4Analysis::kVL1, "write", "file", fileName, result);
0115
0116 return result;
0117 }
0118
0119
0120 template <typename FT>
0121 inline
0122 G4bool G4TFileManager<FT>::CloseTFile(std::shared_ptr<FT> file,
0123 [[maybe_unused]] const G4String& fileName)
0124 {
0125 fAMState.Message(G4Analysis::kVL4, "close", "file", fileName);
0126
0127
0128 auto result = CloseFileImpl(file);
0129
0130 fAMState.Message(G4Analysis::kVL1, "close", "file", fileName, result);
0131
0132 return result;
0133 }
0134
0135
0136 template <typename FT>
0137 inline
0138 G4bool G4TFileManager<FT>::DeleteEmptyFile(const G4String& fileName)
0139 {
0140 fAMState.Message(G4Analysis::kVL4, "delete", "empty file", fileName);
0141
0142 auto result = ! std::remove(fileName);
0143
0144 fAMState.Message(G4Analysis::kVL1, "delete", "empty file", fileName, result);
0145
0146 return result;
0147 }
0148
0149
0150 template <typename FT>
0151 inline
0152 void G4TFileManager<FT>::ClearData()
0153 {
0154 for ( const auto& mapElement : fFileMap ) {
0155 delete mapElement.second;
0156 }
0157 fFileMap.clear();
0158
0159 fAMState.Message(G4Analysis::kVL2, "clear", "files");
0160 }
0161
0162
0163
0164
0165
0166
0167 template <typename FT>
0168 inline
0169 std::shared_ptr<FT> G4TFileManager<FT>::CreateTFile(const G4String& fileName)
0170 {
0171
0172 if ( auto file = GetFileInFunction(fileName, "CreateTFile", false);
0173 file != nullptr ) {
0174 return file;
0175
0176 }
0177
0178 auto fileInformation = GetFileInfoInFunction(fileName, "CreateTFile", false);
0179 if ( ! fileInformation ) {
0180 fAMState.Message(G4Analysis::kVL4, "create", "fileInformation", fileName);
0181
0182
0183 fileInformation = new G4TFileInformation<FT>(fileName);
0184 fFileMap[fileName] = fileInformation;
0185 }
0186
0187
0188 fAMState.Message(G4Analysis::kVL4, "create", "file", fileName);
0189
0190
0191 auto file = CreateFileImpl(fileName);
0192 if ( ! file ) {
0193 G4Analysis::Warn("Failed to create file " + fileName, fkClass, "CreateTFile");
0194 return nullptr;
0195 }
0196
0197 fileInformation->fFile = file;
0198 fileInformation->fIsOpen = true;
0199 fileInformation->fIsEmpty = true;
0200 fileInformation->fIsDeleted = false;
0201
0202 fAMState.Message(G4Analysis::kVL1, "create", "file", fileName);
0203
0204
0205 return file;
0206 }
0207
0208
0209 template <typename FT>
0210 inline
0211 G4bool G4TFileManager<FT>::WriteTFile(const G4String& fileName)
0212 {
0213
0214 auto file = GetFileInFunction(fileName, "WriteTFile");
0215
0216 if (! file) return false;
0217
0218 return WriteTFile(file, fileName);
0219 }
0220
0221
0222 template <typename FT>
0223 inline
0224 G4bool G4TFileManager<FT>::CloseTFile(const G4String& fileName)
0225 {
0226
0227 auto fileInfo = GetFileInfoInFunction(fileName, "CloseTFile");
0228
0229 if ( ! fileInfo ) return false;
0230
0231
0232 if ( ! fileInfo->fIsOpen ) return false;
0233
0234
0235 auto file = fileInfo->fFile;
0236 if ( ! file ) {
0237 FileNotFoundWarning(fileName, "CloseTFile");
0238 return false;
0239 }
0240
0241 auto result = CloseTFile(file, fileName);
0242
0243
0244 fileInfo->fFile.reset();
0245 fileInfo->fIsOpen = false;
0246
0247 return result;
0248 }
0249
0250
0251 template <typename FT>
0252 inline
0253 G4bool G4TFileManager<FT>::SetIsEmpty(const G4String& fileName, G4bool isEmpty)
0254 {
0255
0256 auto fileInfo = GetFileInfoInFunction(fileName, "SetIsEmpty");
0257
0258
0259 if ( ! fileInfo ) return false;
0260
0261 fAMState.Message(G4Analysis::kVL4, "notify not empty", "file", fileName);
0262
0263
0264 if ( fileInfo->fIsEmpty ) {
0265
0266 fileInfo->fIsEmpty = isEmpty;
0267
0268 if ( ! isEmpty ) {
0269 fAMState.Message(G4Analysis::kVL3, "notify not empty", "file", fileName);
0270 }
0271 }
0272
0273 return true;
0274 }
0275
0276
0277 template <typename FT>
0278 inline
0279 std::shared_ptr<FT> G4TFileManager<FT>::GetTFile(
0280 const G4String& fileName, G4bool warn) const
0281 {
0282
0283 return GetFileInFunction(fileName, "GetTFile", warn);
0284 }
0285
0286
0287 template <typename FT>
0288 inline
0289 G4bool G4TFileManager<FT>::OpenFiles()
0290 {
0291 auto result = true;
0292 for ( const auto& mapElement : fFileMap ) {
0293 auto fileInformation = mapElement.second;
0294
0295 if ( fileInformation->fFile ) {
0296
0297 continue;
0298 }
0299
0300 result &= (CreateTFile(fileInformation->fFileName) != nullptr);
0301 }
0302 return result;
0303 }
0304
0305
0306 template <typename FT>
0307 inline
0308 G4bool G4TFileManager<FT>::WriteFiles()
0309 {
0310 auto result = true;
0311 for ( const auto& mapElement : fFileMap ) {
0312 auto fileInformation = mapElement.second;
0313 if ( ! fileInformation->fIsOpen ) {
0314
0315 continue;
0316 }
0317 result &= WriteTFile(fileInformation->fFile, fileInformation->fFileName);
0318 }
0319 return result;
0320 }
0321
0322
0323 template <typename FT>
0324 inline
0325 G4bool G4TFileManager<FT>::CloseFiles()
0326 {
0327 auto result = true;
0328 for ( const auto& mapElement : fFileMap ) {
0329 auto fileInformation = mapElement.second;
0330 if ( ! fileInformation->fIsOpen ) {
0331
0332 continue;
0333 }
0334 result &= CloseTFile(fileInformation->fFile, fileInformation->fFileName);
0335
0336
0337 fileInformation->fFile.reset();
0338 fileInformation->fIsOpen = false;
0339 }
0340
0341
0342
0343
0344 return result;
0345 }
0346
0347
0348 template <typename FT>
0349 inline
0350 G4bool G4TFileManager<FT>::DeleteEmptyFiles()
0351 {
0352 auto result = true;
0353 for ( const auto& mapElement : fFileMap ) {
0354 auto fileInformation = mapElement.second;
0355 if ( (! fileInformation->fIsEmpty) || fileInformation->fIsDeleted ) {
0356
0357 continue;
0358 }
0359
0360 result &= DeleteEmptyFile(fileInformation->fFileName);
0361
0362
0363 fileInformation->fIsDeleted = true;
0364 }
0365
0366 return result;
0367 }