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" << 30 << 31 //____________________________________________ 29 //_____________________________________________________________________________ 32 template <typename FT> 30 template <typename FT> 33 inline 31 inline 34 G4TFileManager<FT>::G4TFileManager(const G4Ana 32 G4TFileManager<FT>::G4TFileManager(const G4AnalysisManagerState& state) 35 : fAMState(state) 33 : fAMState(state) 36 {} 34 {} 37 35 38 //____________________________________________ 36 //_____________________________________________________________________________ 39 template <typename FT> 37 template <typename FT> 40 inline 38 inline 41 G4TFileManager<FT>::~G4TFileManager() 39 G4TFileManager<FT>::~G4TFileManager() 42 { 40 { 43 for ( const auto& mapElement : fFileMap ) { << 41 for ( auto mapElement : fFileMap ) { 44 delete mapElement.second; 42 delete mapElement.second; 45 } 43 } 46 } 44 } 47 45 48 // 46 // 49 // private functions 47 // private functions 50 // 48 // 51 49 52 //____________________________________________ 50 //_____________________________________________________________________________ 53 template <typename FT> 51 template <typename FT> 54 inline 52 inline 55 void << 53 void 56 G4TFileManager<FT>::FileNotFoundWarning(const << 54 G4TFileManager<FT>::FileNotFoundException(const G4String& fileName, 57 std::string_view functio << 55 const G4String& functionName) const 58 { 56 { 59 G4Analysis::Warn("Failed to get file " + fil << 57 G4ExceptionDescription description; >> 58 description >> 59 << "Failed to get file " << fileName; >> 60 G4Exception(functionName, "Analysis_W011", JustWarning, description); 60 } 61 } 61 62 62 //____________________________________________ 63 //_____________________________________________________________________________ 63 template <typename FT> 64 template <typename FT> 64 inline 65 inline 65 G4TFileInformation<FT>* << 66 G4TFileInformation<FT>* 66 G4TFileManager<FT>::GetFileInfoInFunction(cons << 67 G4TFileManager<FT>::GetFileInfoInFunction(const G4String& fileName, 67 std::string_view functio << 68 G4String functionName, G4bool warn ) const 68 { 69 { 69 // Find the file information in the map 70 // Find the file information in the map 70 auto it = fFileMap.find(fileName); 71 auto it = fFileMap.find(fileName); 71 if ( it == fFileMap.end() ) { 72 if ( it == fFileMap.end() ) { 72 if (warn) { 73 if (warn) { 73 FileNotFoundWarning(fileName, functionNa << 74 FileNotFoundException(fileName, functionName); 74 } 75 } 75 return nullptr; 76 return nullptr; 76 } 77 } 77 78 78 return it->second; 79 return it->second; 79 } 80 } 80 81 81 //____________________________________________ 82 //_____________________________________________________________________________ 82 template <typename FT> 83 template <typename FT> 83 inline 84 inline 84 std::shared_ptr<FT> << 85 std::shared_ptr<FT> 85 G4TFileManager<FT>::GetFileInFunction(const G4 86 G4TFileManager<FT>::GetFileInFunction(const G4String& fileName, 86 std::string_view functio << 87 G4String functionName, G4bool warn) const 87 { 88 { 88 // Find the file information in the map 89 // Find the file information in the map 89 auto fileInfo = GetFileInfoInFunction(fileNa 90 auto fileInfo = GetFileInfoInFunction(fileName, functionName, warn); 90 if (! fileInfo) return nullptr; 91 if (! fileInfo) return nullptr; 91 92 92 // Check if the file is open 93 // Check if the file is open 93 if ( ! fileInfo->fFile ) { 94 if ( ! fileInfo->fFile ) { 94 if (warn) { 95 if (warn) { 95 FileNotFoundWarning(fileName, functionNa << 96 FileNotFoundException(fileName, functionName); 96 } 97 } 97 return nullptr; << 98 return nullptr; 98 } 99 } 99 100 100 return fileInfo->fFile; 101 return fileInfo->fFile; 101 } 102 } 102 103 103 //____________________________________________ 104 //_____________________________________________________________________________ 104 template <typename FT> 105 template <typename FT> 105 inline 106 inline 106 G4bool G4TFileManager<FT>::WriteTFile(std::sha << 107 #ifdef G4VERBOSE 107 [[maybe_ << 108 G4bool G4TFileManager<FT>::WriteTFile(std::shared_ptr<FT> file, const G4String& fileName) 108 { << 109 #else 109 fAMState.Message(G4Analysis::kVL4, "write", << 110 G4bool G4TFileManager<FT>::WriteTFile(std::shared_ptr<FT> file, const G4String& /*fileName*/) >> 111 #endif >> 112 { >> 113 #ifdef G4VERBOSE >> 114 if ( fAMState.GetVerboseL4() ) >> 115 fAMState.GetVerboseL4()->Message("write", "file", fileName); >> 116 #endif 110 117 111 // Write the found file 118 // Write the found file 112 auto result = WriteFileImpl(file); 119 auto result = WriteFileImpl(file); 113 120 114 fAMState.Message(G4Analysis::kVL1, "write", << 121 #ifdef G4VERBOSE >> 122 if ( fAMState.GetVerboseL1() ) >> 123 fAMState.GetVerboseL1()->Message("write", "file", fileName); >> 124 #endif 115 125 116 return result; 126 return result; 117 } 127 } 118 128 119 //____________________________________________ 129 //_____________________________________________________________________________ 120 template <typename FT> 130 template <typename FT> 121 inline 131 inline 122 G4bool G4TFileManager<FT>::CloseTFile(std::sha << 132 #ifdef G4VERBOSE 123 [[maybe_ << 133 G4bool G4TFileManager<FT>::CloseTFile(std::shared_ptr<FT> file, const G4String& fileName) 124 { << 134 #else 125 fAMState.Message(G4Analysis::kVL4, "close", << 135 G4bool G4TFileManager<FT>::CloseTFile(std::shared_ptr<FT> file, const G4String& /*fileName*/) >> 136 #endif >> 137 { >> 138 #ifdef G4VERBOSE >> 139 if ( fAMState.GetVerboseL4() ) >> 140 fAMState.GetVerboseL4()->Message("close", "file", fileName); >> 141 #endif 126 142 127 // Close the found file 143 // Close the found file 128 auto result = CloseFileImpl(file); 144 auto result = CloseFileImpl(file); 129 145 130 fAMState.Message(G4Analysis::kVL1, "close", << 146 #ifdef G4VERBOSE >> 147 if ( fAMState.GetVerboseL1() ) >> 148 fAMState.GetVerboseL1()->Message("close", "file", fileName); >> 149 #endif 131 150 132 return result; 151 return result; 133 } 152 } 134 153 135 //____________________________________________ 154 //_____________________________________________________________________________ 136 template <typename FT> 155 template <typename FT> 137 inline 156 inline 138 G4bool G4TFileManager<FT>::DeleteEmptyFile(con 157 G4bool G4TFileManager<FT>::DeleteEmptyFile(const G4String& fileName) 139 { 158 { 140 fAMState.Message(G4Analysis::kVL4, "delete", << 159 #ifdef G4VERBOSE >> 160 if ( fAMState.GetVerboseL4() ) { >> 161 fAMState.GetVerboseL4()->Message("delete", "empty file", fileName); >> 162 } >> 163 #endif 141 164 142 auto result = ! std::remove(fileName); 165 auto result = ! std::remove(fileName); 143 166 144 fAMState.Message(G4Analysis::kVL1, "delete", << 167 #ifdef G4VERBOSE 145 << 168 if ( fAMState.GetVerboseL1() ) { 146 return result; << 169 fAMState.GetVerboseL1()->Message("delete", "empty file", fileName, result); 147 } << 148 << 149 //____________________________________________ << 150 template <typename FT> << 151 inline << 152 void G4TFileManager<FT>::ClearData() << 153 { << 154 for ( const auto& mapElement : fFileMap ) { << 155 delete mapElement.second; << 156 } 170 } 157 fFileMap.clear(); << 171 #endif 158 172 159 fAMState.Message(G4Analysis::kVL2, "clear", << 173 return result; 160 } 174 } 161 175 162 // 176 // 163 // public functions 177 // public functions 164 // 178 // 165 179 166 //____________________________________________ 180 //_____________________________________________________________________________ 167 template <typename FT> 181 template <typename FT> 168 inline 182 inline 169 std::shared_ptr<FT> G4TFileManager<FT>::Create 183 std::shared_ptr<FT> G4TFileManager<FT>::CreateTFile(const G4String& fileName) 170 { 184 { 171 // Just return the file if it already exists << 185 // Check if file already exists 172 if ( auto file = GetFileInFunction(fileName, << 186 if ( GetFileInFunction(fileName, "CreateTFile", false) ) { 173 file != nullptr ) { << 187 G4ExceptionDescription description; 174 return file; << 188 description 175 // should we also update fileInformation ? << 189 << "File " << fileName << " already exists."; >> 190 G4Exception("G4TFileManager<FT>::CreateTFile", >> 191 "Analysis_W001", JustWarning, description); >> 192 // return it->second->fFile; >> 193 return nullptr; 176 } 194 } 177 195 178 auto fileInformation = GetFileInfoInFunction 196 auto fileInformation = GetFileInfoInFunction(fileName, "CreateTFile", false); 179 if ( ! fileInformation ) { 197 if ( ! fileInformation ) { 180 fAMState.Message(G4Analysis::kVL4, "create << 198 #ifdef G4VERBOSE 181 << 199 if ( fAMState.GetVerboseL4() ) { >> 200 fAMState.GetVerboseL4()->Message("create", "fileInformation", fileName); >> 201 } >> 202 #endif 182 // Create file information and save it in 203 // Create file information and save it in the map 183 fileInformation = new G4TFileInformation<F 204 fileInformation = new G4TFileInformation<FT>(fileName); 184 fFileMap[fileName] = fileInformation; 205 fFileMap[fileName] = fileInformation; 185 } 206 } 186 207 187 // Create file and save it in fileInformatio 208 // Create file and save it in fileInformation 188 fAMState.Message(G4Analysis::kVL4, "create", << 209 #ifdef G4VERBOSE >> 210 if ( fAMState.GetVerboseL4() ) { >> 211 fAMState.GetVerboseL4()->Message("create", "file", fileName); >> 212 } >> 213 #endif 189 214 190 // Let concrete class create a file 215 // Let concrete class create a file 191 auto file = CreateFileImpl(fileName); 216 auto file = CreateFileImpl(fileName); 192 if ( ! file ) { 217 if ( ! file ) { 193 G4Analysis::Warn("Failed to create file " << 218 G4ExceptionDescription description; >> 219 description >> 220 << "Failed to create file " << fileName; >> 221 G4Exception("G4TFileManager<FT>::CreateTFile", >> 222 "Analysis_W001", JustWarning, description); 194 return nullptr; 223 return nullptr; 195 } 224 } 196 // Save file in fileInformation 225 // Save file in fileInformation 197 fileInformation->fFile = file; 226 fileInformation->fFile = file; 198 fileInformation->fIsOpen = true; 227 fileInformation->fIsOpen = true; 199 fileInformation->fIsEmpty = true; 228 fileInformation->fIsEmpty = true; 200 fileInformation->fIsDeleted = false; 229 fileInformation->fIsDeleted = false; 201 230 202 fAMState.Message(G4Analysis::kVL1, "create", << 231 #ifdef G4VERBOSE >> 232 if ( fAMState.GetVerboseL1() ) { >> 233 fAMState.GetVerboseL1()->Message("create", "file", fileName); >> 234 } >> 235 #endif 203 236 204 // Return created file 237 // Return created file 205 return file; 238 return file; 206 } 239 } 207 240 208 //____________________________________________ 241 //_____________________________________________________________________________ 209 template <typename FT> 242 template <typename FT> 210 inline 243 inline 211 G4bool G4TFileManager<FT>::WriteTFile(const G4 244 G4bool G4TFileManager<FT>::WriteTFile(const G4String& fileName) 212 { 245 { 213 // Find the file in the map 246 // Find the file in the map 214 auto file = GetFileInFunction(fileName, "Wri 247 auto file = GetFileInFunction(fileName, "WriteTFile"); 215 // Warning is issued in GetFileInfoFunction 248 // Warning is issued in GetFileInfoFunction 216 if (! file) return false; 249 if (! file) return false; 217 250 218 return WriteTFile(file, fileName); 251 return WriteTFile(file, fileName); 219 } 252 } 220 253 221 //____________________________________________ 254 //_____________________________________________________________________________ 222 template <typename FT> 255 template <typename FT> 223 inline 256 inline 224 G4bool G4TFileManager<FT>::CloseTFile(const G4 257 G4bool G4TFileManager<FT>::CloseTFile(const G4String& fileName) 225 { 258 { 226 // Find the file information in the map 259 // Find the file information in the map 227 auto fileInfo = GetFileInfoInFunction(fileNa 260 auto fileInfo = GetFileInfoInFunction(fileName, "CloseTFile"); 228 // Warning is issued in GetFileInfoFunction 261 // Warning is issued in GetFileInfoFunction 229 if ( ! fileInfo ) return false; 262 if ( ! fileInfo ) return false; 230 263 231 // Do nothing if file is not open 264 // Do nothing if file is not open 232 if ( ! fileInfo->fIsOpen ) return false; 265 if ( ! fileInfo->fIsOpen ) return false; 233 266 234 // Get file from the file information 267 // Get file from the file information 235 auto file = fileInfo->fFile; 268 auto file = fileInfo->fFile; 236 if ( ! file ) { 269 if ( ! file ) { 237 FileNotFoundWarning(fileName, "CloseTFile" << 270 FileNotFoundException(fileName, "CloseTFile"); 238 return false; 271 return false; 239 } 272 } 240 273 241 auto result = CloseTFile(file, fileName); 274 auto result = CloseTFile(file, fileName); 242 275 243 // Update the file information 276 // Update the file information 244 fileInfo->fFile.reset(); 277 fileInfo->fFile.reset(); 245 fileInfo->fIsOpen = false; 278 fileInfo->fIsOpen = false; 246 279 247 return result; 280 return result; 248 } 281 } 249 282 250 //____________________________________________ 283 //_____________________________________________________________________________ 251 template <typename FT> 284 template <typename FT> 252 inline 285 inline 253 G4bool G4TFileManager<FT>::SetIsEmpty(const G4 286 G4bool G4TFileManager<FT>::SetIsEmpty(const G4String& fileName, G4bool isEmpty) 254 { 287 { 255 // Find the file information in the map 288 // Find the file information in the map 256 auto fileInfo = GetFileInfoInFunction(fileNa 289 auto fileInfo = GetFileInfoInFunction(fileName, "SetIsEmpty"); 257 290 258 // Warning is issued in GetFileFunction 291 // Warning is issued in GetFileFunction 259 if ( ! fileInfo ) return false; 292 if ( ! fileInfo ) return false; 260 293 261 fAMState.Message(G4Analysis::kVL4, "notify n << 294 #ifdef G4VERBOSE >> 295 if ( fAMState.GetVerboseL4() ) { >> 296 fAMState.GetVerboseL4()->Message("notify not empty", "file", fileName); >> 297 } >> 298 #endif 262 299 263 // Set notification to file information 300 // Set notification to file information 264 if ( fileInfo->fIsEmpty ) { 301 if ( fileInfo->fIsEmpty ) { 265 // do not revert information if file is no 302 // do not revert information if file is not empty 266 fileInfo->fIsEmpty = isEmpty; 303 fileInfo->fIsEmpty = isEmpty; >> 304 } 267 305 268 if ( ! isEmpty ) { << 306 #ifdef G4VERBOSE 269 fAMState.Message(G4Analysis::kVL3, "noti << 307 if ( fAMState.GetVerboseL2() ) { 270 } << 308 fAMState.GetVerboseL2()->Message("notify not empty", "file", fileName); 271 } 309 } >> 310 #endif 272 311 273 return true; 312 return true; 274 } 313 } 275 314 276 //____________________________________________ 315 //_____________________________________________________________________________ 277 template <typename FT> 316 template <typename FT> 278 inline 317 inline 279 std::shared_ptr<FT> G4TFileManager<FT>::GetTFi 318 std::shared_ptr<FT> G4TFileManager<FT>::GetTFile( 280 const G4String& fileName, G4bool warn) const 319 const G4String& fileName, G4bool warn) const 281 { 320 { 282 // Find the file information in the map 321 // Find the file information in the map 283 return GetFileInFunction(fileName, "GetTFile 322 return GetFileInFunction(fileName, "GetTFile", warn); 284 } 323 } 285 324 286 //____________________________________________ 325 //_____________________________________________________________________________ 287 template <typename FT> 326 template <typename FT> 288 inline 327 inline 289 G4bool G4TFileManager<FT>::OpenFiles() 328 G4bool G4TFileManager<FT>::OpenFiles() 290 { 329 { 291 auto result = true; << 330 bool finalResult = true; 292 for ( const auto& mapElement : fFileMap ) { << 331 for ( auto mapElement : fFileMap ) { 293 auto fileInformation = mapElement.second; 332 auto fileInformation = mapElement.second; 294 // Do nothing if file was open by user ex 333 // Do nothing if file was open by user explicitly 295 if ( fileInformation->fFile ) { 334 if ( fileInformation->fFile ) { 296 // G4cout << "... skipping open file fo << 335 G4cout << " ... skipping open file for " << fileInformation->fFileName << G4endl; 297 continue; 336 continue; 298 } 337 } 299 << 338 300 result &= (CreateTFile(fileInformation->f << 339 auto result = CreateTFile(fileInformation->fFileName); >> 340 finalResult = result && finalResult; 301 } 341 } 302 return result; << 342 return finalResult; 303 } 343 } 304 344 305 //____________________________________________ 345 //_____________________________________________________________________________ 306 template <typename FT> 346 template <typename FT> 307 inline 347 inline 308 G4bool G4TFileManager<FT>::WriteFiles() 348 G4bool G4TFileManager<FT>::WriteFiles() 309 { 349 { 310 auto result = true; << 350 bool finalResult = true; 311 for ( const auto& mapElement : fFileMap ) { << 351 for ( auto mapElement : fFileMap ) { 312 auto fileInformation = mapElement.second; 352 auto fileInformation = mapElement.second; 313 if ( ! fileInformation->fIsOpen ) { 353 if ( ! fileInformation->fIsOpen ) { 314 // G4cout << "skipping write for file " 354 // G4cout << "skipping write for file " << fileInformation->fFileName << G4endl; 315 continue; 355 continue; 316 } 356 } 317 result &= WriteTFile(fileInformation->fFi << 357 auto result = WriteTFile(fileInformation->fFile, fileInformation->fFileName); >> 358 finalResult = result && finalResult; 318 } 359 } 319 return result; << 360 return finalResult; 320 } 361 } 321 362 322 //____________________________________________ 363 //_____________________________________________________________________________ 323 template <typename FT> 364 template <typename FT> 324 inline 365 inline 325 G4bool G4TFileManager<FT>::CloseFiles() 366 G4bool G4TFileManager<FT>::CloseFiles() 326 { 367 { 327 auto result = true; << 368 bool finalResult = true; 328 for ( const auto& mapElement : fFileMap ) { << 369 for ( auto mapElement : fFileMap ) { 329 auto fileInformation = mapElement.second; 370 auto fileInformation = mapElement.second; 330 if ( ! fileInformation->fIsOpen ) { 371 if ( ! fileInformation->fIsOpen ) { 331 // G4cout << "skipping close for file " 372 // G4cout << "skipping close for file " << fileInformation->fFileName << G4endl; 332 continue; 373 continue; 333 } 374 } 334 result &= CloseTFile(fileInformation->fFi << 375 auto result = CloseTFile(fileInformation->fFile, fileInformation->fFileName); >> 376 finalResult = result && finalResult; 335 377 336 // Update file information 378 // Update file information 337 fileInformation->fFile.reset(); 379 fileInformation->fFile.reset(); 338 fileInformation->fIsOpen = false; 380 fileInformation->fIsOpen = false; 339 } 381 } 340 382 341 // As the files were set to nullptr, clear s 383 // As the files were set to nullptr, clear should not be needed 342 // fFileMap.clear(); 384 // fFileMap.clear(); 343 385 344 return result; << 386 return finalResult; 345 } 387 } 346 388 347 //____________________________________________ 389 //_____________________________________________________________________________ 348 template <typename FT> 390 template <typename FT> 349 inline 391 inline 350 G4bool G4TFileManager<FT>::DeleteEmptyFiles() 392 G4bool G4TFileManager<FT>::DeleteEmptyFiles() 351 { 393 { 352 auto result = true; << 394 bool finalResult = true; 353 for ( const auto& mapElement : fFileMap ) { << 395 for ( auto mapElement : fFileMap ) { 354 auto fileInformation = mapElement.second; 396 auto fileInformation = mapElement.second; 355 if ( (! fileInformation->fIsEmpty) || file 397 if ( (! fileInformation->fIsEmpty) || fileInformation->fIsDeleted ) { 356 // G4cout << "skipping delete for file " 398 // G4cout << "skipping delete for file " << fileInformation->fFileName << G4endl; 357 continue; 399 continue; 358 } 400 } 359 401 360 result &= DeleteEmptyFile(fileInformation- << 402 auto result = DeleteEmptyFile(fileInformation->fFileName); >> 403 finalResult = result && finalResult; 361 404 362 // Update file information 405 // Update file information 363 fileInformation->fIsDeleted = true; 406 fileInformation->fIsDeleted = true; 364 } 407 } 365 408 366 return result; << 409 return finalResult; 367 } 410 } 368 411