Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 26 27 // Author: Ivana Hrivnacova, 18/06/2013 (ivan 27 // Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr) 28 28 29 #include "G4AnalysisUtilities.hh" 29 #include "G4AnalysisUtilities.hh" 30 30 31 //____________________________________________ 31 //_____________________________________________________________________________ 32 template <typename FT> 32 template <typename FT> 33 inline 33 inline 34 G4TFileManager<FT>::G4TFileManager(const G4Ana 34 G4TFileManager<FT>::G4TFileManager(const G4AnalysisManagerState& state) 35 : fAMState(state) 35 : fAMState(state) 36 {} 36 {} 37 37 38 //____________________________________________ 38 //_____________________________________________________________________________ 39 template <typename FT> 39 template <typename FT> 40 inline 40 inline 41 G4TFileManager<FT>::~G4TFileManager() 41 G4TFileManager<FT>::~G4TFileManager() 42 { 42 { 43 for ( const auto& mapElement : fFileMap ) { 43 for ( const auto& mapElement : fFileMap ) { 44 delete mapElement.second; 44 delete mapElement.second; 45 } 45 } 46 } 46 } 47 47 48 // 48 // 49 // private functions 49 // private functions 50 // 50 // 51 51 52 //____________________________________________ 52 //_____________________________________________________________________________ 53 template <typename FT> 53 template <typename FT> 54 inline 54 inline 55 void 55 void 56 G4TFileManager<FT>::FileNotFoundWarning(const 56 G4TFileManager<FT>::FileNotFoundWarning(const G4String& fileName, 57 std::string_view functio 57 std::string_view functionName) const 58 { 58 { 59 G4Analysis::Warn("Failed to get file " + fil 59 G4Analysis::Warn("Failed to get file " + fileName, fkClass, functionName); 60 } 60 } 61 61 62 //____________________________________________ 62 //_____________________________________________________________________________ 63 template <typename FT> 63 template <typename FT> 64 inline 64 inline 65 G4TFileInformation<FT>* 65 G4TFileInformation<FT>* 66 G4TFileManager<FT>::GetFileInfoInFunction(cons 66 G4TFileManager<FT>::GetFileInfoInFunction(const G4String& fileName, 67 std::string_view functio 67 std::string_view functionName, G4bool warn ) const 68 { 68 { 69 // Find the file information in the map 69 // Find the file information in the map 70 auto it = fFileMap.find(fileName); 70 auto it = fFileMap.find(fileName); 71 if ( it == fFileMap.end() ) { 71 if ( it == fFileMap.end() ) { 72 if (warn) { 72 if (warn) { 73 FileNotFoundWarning(fileName, functionNa 73 FileNotFoundWarning(fileName, functionName); 74 } 74 } 75 return nullptr; 75 return nullptr; 76 } 76 } 77 77 78 return it->second; 78 return it->second; 79 } 79 } 80 80 81 //____________________________________________ 81 //_____________________________________________________________________________ 82 template <typename FT> 82 template <typename FT> 83 inline 83 inline 84 std::shared_ptr<FT> 84 std::shared_ptr<FT> 85 G4TFileManager<FT>::GetFileInFunction(const G4 85 G4TFileManager<FT>::GetFileInFunction(const G4String& fileName, 86 std::string_view functio 86 std::string_view functionName, G4bool warn) const 87 { 87 { 88 // Find the file information in the map 88 // Find the file information in the map 89 auto fileInfo = GetFileInfoInFunction(fileNa 89 auto fileInfo = GetFileInfoInFunction(fileName, functionName, warn); 90 if (! fileInfo) return nullptr; 90 if (! fileInfo) return nullptr; 91 91 92 // Check if the file is open 92 // Check if the file is open 93 if ( ! fileInfo->fFile ) { 93 if ( ! fileInfo->fFile ) { 94 if (warn) { 94 if (warn) { 95 FileNotFoundWarning(fileName, functionNa 95 FileNotFoundWarning(fileName, functionName); 96 } 96 } 97 return nullptr; 97 return nullptr; 98 } 98 } 99 99 100 return fileInfo->fFile; 100 return fileInfo->fFile; 101 } 101 } 102 102 103 //____________________________________________ 103 //_____________________________________________________________________________ 104 template <typename FT> 104 template <typename FT> 105 inline 105 inline 106 G4bool G4TFileManager<FT>::WriteTFile(std::sha 106 G4bool G4TFileManager<FT>::WriteTFile(std::shared_ptr<FT> file, 107 [[maybe_ 107 [[maybe_unused]] const G4String& fileName) 108 { 108 { 109 fAMState.Message(G4Analysis::kVL4, "write", 109 fAMState.Message(G4Analysis::kVL4, "write", "file", fileName); 110 110 111 // Write the found file 111 // Write the found file 112 auto result = WriteFileImpl(file); 112 auto result = WriteFileImpl(file); 113 113 114 fAMState.Message(G4Analysis::kVL1, "write", 114 fAMState.Message(G4Analysis::kVL1, "write", "file", fileName, result); 115 115 116 return result; 116 return result; 117 } 117 } 118 118 119 //____________________________________________ 119 //_____________________________________________________________________________ 120 template <typename FT> 120 template <typename FT> 121 inline 121 inline 122 G4bool G4TFileManager<FT>::CloseTFile(std::sha 122 G4bool G4TFileManager<FT>::CloseTFile(std::shared_ptr<FT> file, 123 [[maybe_ 123 [[maybe_unused]] const G4String& fileName) 124 { 124 { 125 fAMState.Message(G4Analysis::kVL4, "close", 125 fAMState.Message(G4Analysis::kVL4, "close", "file", fileName); 126 126 127 // Close the found file 127 // Close the found file 128 auto result = CloseFileImpl(file); 128 auto result = CloseFileImpl(file); 129 129 130 fAMState.Message(G4Analysis::kVL1, "close", 130 fAMState.Message(G4Analysis::kVL1, "close", "file", fileName, result); 131 131 132 return result; 132 return result; 133 } 133 } 134 134 135 //____________________________________________ 135 //_____________________________________________________________________________ 136 template <typename FT> 136 template <typename FT> 137 inline 137 inline 138 G4bool G4TFileManager<FT>::DeleteEmptyFile(con 138 G4bool G4TFileManager<FT>::DeleteEmptyFile(const G4String& fileName) 139 { 139 { 140 fAMState.Message(G4Analysis::kVL4, "delete", 140 fAMState.Message(G4Analysis::kVL4, "delete", "empty file", fileName); 141 141 142 auto result = ! std::remove(fileName); 142 auto result = ! std::remove(fileName); 143 143 144 fAMState.Message(G4Analysis::kVL1, "delete", 144 fAMState.Message(G4Analysis::kVL1, "delete", "empty file", fileName, result); 145 145 146 return result; 146 return result; 147 } 147 } 148 148 149 //____________________________________________ 149 //_____________________________________________________________________________ 150 template <typename FT> 150 template <typename FT> 151 inline 151 inline 152 void G4TFileManager<FT>::ClearData() 152 void G4TFileManager<FT>::ClearData() 153 { 153 { 154 for ( const auto& mapElement : fFileMap ) { 154 for ( const auto& mapElement : fFileMap ) { 155 delete mapElement.second; 155 delete mapElement.second; 156 } 156 } 157 fFileMap.clear(); 157 fFileMap.clear(); 158 158 159 fAMState.Message(G4Analysis::kVL2, "clear", 159 fAMState.Message(G4Analysis::kVL2, "clear", "files"); 160 } 160 } 161 161 162 // 162 // 163 // public functions 163 // public functions 164 // 164 // 165 165 166 //____________________________________________ 166 //_____________________________________________________________________________ 167 template <typename FT> 167 template <typename FT> 168 inline 168 inline 169 std::shared_ptr<FT> G4TFileManager<FT>::Create 169 std::shared_ptr<FT> G4TFileManager<FT>::CreateTFile(const G4String& fileName) 170 { 170 { 171 // Just return the file if it already exists << 171 // Check if file already exists 172 if ( auto file = GetFileInFunction(fileName, << 172 if ( GetFileInFunction(fileName, "CreateTFile", false) ) { 173 file != nullptr ) { << 173 // Do not issue the warning if cycle > 0 174 return file; << 174 if (fAMState.GetCycle() == 0) { 175 // should we also update fileInformation ? << 175 G4Analysis::Warn("File " + fileName + " already exists.", >> 176 fkClass, "CreateTFile"); >> 177 return nullptr; >> 178 } >> 179 else { >> 180 return GetFileInFunction(fileName, "CreateTFile", false); >> 181 // should we also update fileInformation ?? >> 182 } >> 183 176 } 184 } 177 185 178 auto fileInformation = GetFileInfoInFunction 186 auto fileInformation = GetFileInfoInFunction(fileName, "CreateTFile", false); 179 if ( ! fileInformation ) { 187 if ( ! fileInformation ) { 180 fAMState.Message(G4Analysis::kVL4, "create 188 fAMState.Message(G4Analysis::kVL4, "create", "fileInformation", fileName); 181 189 182 // Create file information and save it in 190 // Create file information and save it in the map 183 fileInformation = new G4TFileInformation<F 191 fileInformation = new G4TFileInformation<FT>(fileName); 184 fFileMap[fileName] = fileInformation; 192 fFileMap[fileName] = fileInformation; 185 } 193 } 186 194 187 // Create file and save it in fileInformatio 195 // Create file and save it in fileInformation 188 fAMState.Message(G4Analysis::kVL4, "create", 196 fAMState.Message(G4Analysis::kVL4, "create", "file", fileName); 189 197 190 // Let concrete class create a file 198 // Let concrete class create a file 191 auto file = CreateFileImpl(fileName); 199 auto file = CreateFileImpl(fileName); 192 if ( ! file ) { 200 if ( ! file ) { 193 G4Analysis::Warn("Failed to create file " 201 G4Analysis::Warn("Failed to create file " + fileName, fkClass, "CreateTFile"); 194 return nullptr; 202 return nullptr; 195 } 203 } 196 // Save file in fileInformation 204 // Save file in fileInformation 197 fileInformation->fFile = file; 205 fileInformation->fFile = file; 198 fileInformation->fIsOpen = true; 206 fileInformation->fIsOpen = true; 199 fileInformation->fIsEmpty = true; 207 fileInformation->fIsEmpty = true; 200 fileInformation->fIsDeleted = false; 208 fileInformation->fIsDeleted = false; 201 209 202 fAMState.Message(G4Analysis::kVL1, "create", 210 fAMState.Message(G4Analysis::kVL1, "create", "file", fileName); 203 211 204 // Return created file 212 // Return created file 205 return file; 213 return file; 206 } 214 } 207 215 208 //____________________________________________ 216 //_____________________________________________________________________________ 209 template <typename FT> 217 template <typename FT> 210 inline 218 inline 211 G4bool G4TFileManager<FT>::WriteTFile(const G4 219 G4bool G4TFileManager<FT>::WriteTFile(const G4String& fileName) 212 { 220 { 213 // Find the file in the map 221 // Find the file in the map 214 auto file = GetFileInFunction(fileName, "Wri 222 auto file = GetFileInFunction(fileName, "WriteTFile"); 215 // Warning is issued in GetFileInfoFunction 223 // Warning is issued in GetFileInfoFunction 216 if (! file) return false; 224 if (! file) return false; 217 225 218 return WriteTFile(file, fileName); 226 return WriteTFile(file, fileName); 219 } 227 } 220 228 221 //____________________________________________ 229 //_____________________________________________________________________________ 222 template <typename FT> 230 template <typename FT> 223 inline 231 inline 224 G4bool G4TFileManager<FT>::CloseTFile(const G4 232 G4bool G4TFileManager<FT>::CloseTFile(const G4String& fileName) 225 { 233 { 226 // Find the file information in the map 234 // Find the file information in the map 227 auto fileInfo = GetFileInfoInFunction(fileNa 235 auto fileInfo = GetFileInfoInFunction(fileName, "CloseTFile"); 228 // Warning is issued in GetFileInfoFunction 236 // Warning is issued in GetFileInfoFunction 229 if ( ! fileInfo ) return false; 237 if ( ! fileInfo ) return false; 230 238 231 // Do nothing if file is not open 239 // Do nothing if file is not open 232 if ( ! fileInfo->fIsOpen ) return false; 240 if ( ! fileInfo->fIsOpen ) return false; 233 241 234 // Get file from the file information 242 // Get file from the file information 235 auto file = fileInfo->fFile; 243 auto file = fileInfo->fFile; 236 if ( ! file ) { 244 if ( ! file ) { 237 FileNotFoundWarning(fileName, "CloseTFile" 245 FileNotFoundWarning(fileName, "CloseTFile"); 238 return false; 246 return false; 239 } 247 } 240 248 241 auto result = CloseTFile(file, fileName); 249 auto result = CloseTFile(file, fileName); 242 250 243 // Update the file information 251 // Update the file information 244 fileInfo->fFile.reset(); 252 fileInfo->fFile.reset(); 245 fileInfo->fIsOpen = false; 253 fileInfo->fIsOpen = false; 246 254 247 return result; 255 return result; 248 } 256 } 249 257 250 //____________________________________________ 258 //_____________________________________________________________________________ 251 template <typename FT> 259 template <typename FT> 252 inline 260 inline 253 G4bool G4TFileManager<FT>::SetIsEmpty(const G4 261 G4bool G4TFileManager<FT>::SetIsEmpty(const G4String& fileName, G4bool isEmpty) 254 { 262 { 255 // Find the file information in the map 263 // Find the file information in the map 256 auto fileInfo = GetFileInfoInFunction(fileNa 264 auto fileInfo = GetFileInfoInFunction(fileName, "SetIsEmpty"); 257 265 258 // Warning is issued in GetFileFunction 266 // Warning is issued in GetFileFunction 259 if ( ! fileInfo ) return false; 267 if ( ! fileInfo ) return false; 260 268 261 fAMState.Message(G4Analysis::kVL4, "notify n 269 fAMState.Message(G4Analysis::kVL4, "notify not empty", "file", fileName); 262 270 263 // Set notification to file information 271 // Set notification to file information 264 if ( fileInfo->fIsEmpty ) { 272 if ( fileInfo->fIsEmpty ) { 265 // do not revert information if file is no 273 // do not revert information if file is not empty 266 fileInfo->fIsEmpty = isEmpty; 274 fileInfo->fIsEmpty = isEmpty; 267 275 268 if ( ! isEmpty ) { 276 if ( ! isEmpty ) { 269 fAMState.Message(G4Analysis::kVL3, "noti 277 fAMState.Message(G4Analysis::kVL3, "notify not empty", "file", fileName); 270 } 278 } 271 } 279 } 272 280 273 return true; 281 return true; 274 } 282 } 275 283 276 //____________________________________________ 284 //_____________________________________________________________________________ 277 template <typename FT> 285 template <typename FT> 278 inline 286 inline 279 std::shared_ptr<FT> G4TFileManager<FT>::GetTFi 287 std::shared_ptr<FT> G4TFileManager<FT>::GetTFile( 280 const G4String& fileName, G4bool warn) const 288 const G4String& fileName, G4bool warn) const 281 { 289 { 282 // Find the file information in the map 290 // Find the file information in the map 283 return GetFileInFunction(fileName, "GetTFile 291 return GetFileInFunction(fileName, "GetTFile", warn); 284 } 292 } 285 293 286 //____________________________________________ 294 //_____________________________________________________________________________ 287 template <typename FT> 295 template <typename FT> 288 inline 296 inline 289 G4bool G4TFileManager<FT>::OpenFiles() 297 G4bool G4TFileManager<FT>::OpenFiles() 290 { 298 { 291 auto result = true; 299 auto result = true; 292 for ( const auto& mapElement : fFileMap ) { 300 for ( const auto& mapElement : fFileMap ) { 293 auto fileInformation = mapElement.second; 301 auto fileInformation = mapElement.second; 294 // Do nothing if file was open by user ex 302 // Do nothing if file was open by user explicitly 295 if ( fileInformation->fFile ) { 303 if ( fileInformation->fFile ) { 296 // G4cout << "... skipping open file fo 304 // G4cout << "... skipping open file for " << fileInformation->fFileName << G4endl; 297 continue; 305 continue; 298 } 306 } 299 307 300 result &= (CreateTFile(fileInformation->f 308 result &= (CreateTFile(fileInformation->fFileName) != nullptr); 301 } 309 } 302 return result; 310 return result; 303 } 311 } 304 312 305 //____________________________________________ 313 //_____________________________________________________________________________ 306 template <typename FT> 314 template <typename FT> 307 inline 315 inline 308 G4bool G4TFileManager<FT>::WriteFiles() 316 G4bool G4TFileManager<FT>::WriteFiles() 309 { 317 { 310 auto result = true; 318 auto result = true; 311 for ( const auto& mapElement : fFileMap ) { 319 for ( const auto& mapElement : fFileMap ) { 312 auto fileInformation = mapElement.second; 320 auto fileInformation = mapElement.second; 313 if ( ! fileInformation->fIsOpen ) { 321 if ( ! fileInformation->fIsOpen ) { 314 // G4cout << "skipping write for file " 322 // G4cout << "skipping write for file " << fileInformation->fFileName << G4endl; 315 continue; 323 continue; 316 } 324 } 317 result &= WriteTFile(fileInformation->fFi 325 result &= WriteTFile(fileInformation->fFile, fileInformation->fFileName); 318 } 326 } 319 return result; 327 return result; 320 } 328 } 321 329 322 //____________________________________________ 330 //_____________________________________________________________________________ 323 template <typename FT> 331 template <typename FT> 324 inline 332 inline 325 G4bool G4TFileManager<FT>::CloseFiles() 333 G4bool G4TFileManager<FT>::CloseFiles() 326 { 334 { 327 auto result = true; 335 auto result = true; 328 for ( const auto& mapElement : fFileMap ) { 336 for ( const auto& mapElement : fFileMap ) { 329 auto fileInformation = mapElement.second; 337 auto fileInformation = mapElement.second; 330 if ( ! fileInformation->fIsOpen ) { 338 if ( ! fileInformation->fIsOpen ) { 331 // G4cout << "skipping close for file " 339 // G4cout << "skipping close for file " << fileInformation->fFileName << G4endl; 332 continue; 340 continue; 333 } 341 } 334 result &= CloseTFile(fileInformation->fFi 342 result &= CloseTFile(fileInformation->fFile, fileInformation->fFileName); 335 343 336 // Update file information 344 // Update file information 337 fileInformation->fFile.reset(); 345 fileInformation->fFile.reset(); 338 fileInformation->fIsOpen = false; 346 fileInformation->fIsOpen = false; 339 } 347 } 340 348 341 // As the files were set to nullptr, clear s 349 // As the files were set to nullptr, clear should not be needed 342 // fFileMap.clear(); 350 // fFileMap.clear(); 343 351 344 return result; 352 return result; 345 } 353 } 346 354 347 //____________________________________________ 355 //_____________________________________________________________________________ 348 template <typename FT> 356 template <typename FT> 349 inline 357 inline 350 G4bool G4TFileManager<FT>::DeleteEmptyFiles() 358 G4bool G4TFileManager<FT>::DeleteEmptyFiles() 351 { 359 { 352 auto result = true; 360 auto result = true; 353 for ( const auto& mapElement : fFileMap ) { 361 for ( const auto& mapElement : fFileMap ) { 354 auto fileInformation = mapElement.second; 362 auto fileInformation = mapElement.second; 355 if ( (! fileInformation->fIsEmpty) || file 363 if ( (! fileInformation->fIsEmpty) || fileInformation->fIsDeleted ) { 356 // G4cout << "skipping delete for file " 364 // G4cout << "skipping delete for file " << fileInformation->fFileName << G4endl; 357 continue; 365 continue; 358 } 366 } 359 367 360 result &= DeleteEmptyFile(fileInformation- 368 result &= DeleteEmptyFile(fileInformation->fFileName); 361 369 362 // Update file information 370 // Update file information 363 fileInformation->fIsDeleted = true; 371 fileInformation->fIsDeleted = true; 364 } 372 } 365 373 366 return result; 374 return result; 367 } 375 } 368 376