// // ******************************************************************** // * License and Disclaimer * // * * // * The Geant4 software is copyright of the Copyright Holders of * // * the Geant4 Collaboration. It is provided under the terms and * // * conditions of the Geant4 Software License, included in the file * // * LICENSE and available at http://cern.ch/geant4/license . These * // * include a list of copyright holders. * // * * // * Neither the authors of this software system, nor their employing * // * institutes,nor the agencies providing financial support for this * // * work make any representation or warranty, express or implied, * // * regarding this software system or assume any liability for its * // * use. Please see the license in the file LICENSE and URL above * // * for the full disclaimer and the limitation of liability. * // * * // * This code implementation is the result of the scientific and * // * technical work of the GEANT4 collaboration. * // * By using, copying, modifying or distributing the software (or * // * any work based on the software) you agree to acknowledge its * // * use in resulting scientific publications, and indicate your * // * acceptance of all terms of the Geant4 Software license. * // ******************************************************************** // // Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr) //_____________________________________________________________________________ template inline G4TFileManager::G4TFileManager(const G4AnalysisManagerState& state) : fAMState(state) {} //_____________________________________________________________________________ template inline G4TFileManager::~G4TFileManager() { for ( auto mapElement : fFileMap ) { delete mapElement.second; } } // // private functions // //_____________________________________________________________________________ template inline void G4TFileManager::FileNotFoundException(const G4String& fileName, const G4String& functionName) const { G4ExceptionDescription description; description << "Failed to get file " << fileName; G4Exception(functionName, "Analysis_W011", JustWarning, description); } //_____________________________________________________________________________ template inline G4TFileInformation* G4TFileManager::GetFileInfoInFunction(const G4String& fileName, G4String functionName, G4bool warn ) const { // Find the file information in the map auto it = fFileMap.find(fileName); if ( it == fFileMap.end() ) { if (warn) { FileNotFoundException(fileName, functionName); } return nullptr; } return it->second; } //_____________________________________________________________________________ template inline std::shared_ptr G4TFileManager::GetFileInFunction(const G4String& fileName, G4String functionName, G4bool warn) const { // Find the file information in the map auto fileInfo = GetFileInfoInFunction(fileName, functionName, warn); if (! fileInfo) return nullptr; // Check if the file is open if ( ! fileInfo->fFile ) { if (warn) { FileNotFoundException(fileName, functionName); } return nullptr; } return fileInfo->fFile; } //_____________________________________________________________________________ template inline #ifdef G4VERBOSE G4bool G4TFileManager::WriteTFile(std::shared_ptr file, const G4String& fileName) #else G4bool G4TFileManager::WriteTFile(std::shared_ptr file, const G4String& /*fileName*/) #endif { #ifdef G4VERBOSE if ( fAMState.GetVerboseL4() ) fAMState.GetVerboseL4()->Message("write", "file", fileName); #endif // Write the found file auto result = WriteFileImpl(file); #ifdef G4VERBOSE if ( fAMState.GetVerboseL1() ) fAMState.GetVerboseL1()->Message("write", "file", fileName); #endif return result; } //_____________________________________________________________________________ template inline #ifdef G4VERBOSE G4bool G4TFileManager::CloseTFile(std::shared_ptr file, const G4String& fileName) #else G4bool G4TFileManager::CloseTFile(std::shared_ptr file, const G4String& /*fileName*/) #endif { #ifdef G4VERBOSE if ( fAMState.GetVerboseL4() ) fAMState.GetVerboseL4()->Message("close", "file", fileName); #endif // Close the found file auto result = CloseFileImpl(file); #ifdef G4VERBOSE if ( fAMState.GetVerboseL1() ) fAMState.GetVerboseL1()->Message("close", "file", fileName); #endif return result; } //_____________________________________________________________________________ template inline G4bool G4TFileManager::DeleteEmptyFile(const G4String& fileName) { #ifdef G4VERBOSE if ( fAMState.GetVerboseL4() ) { fAMState.GetVerboseL4()->Message("delete", "empty file", fileName); } #endif auto result = ! std::remove(fileName); #ifdef G4VERBOSE if ( fAMState.GetVerboseL1() ) { fAMState.GetVerboseL1()->Message("delete", "empty file", fileName, result); } #endif return result; } // // public functions // //_____________________________________________________________________________ template inline std::shared_ptr G4TFileManager::CreateTFile(const G4String& fileName) { // Check if file already exists if ( GetFileInFunction(fileName, "CreateTFile", false) ) { G4ExceptionDescription description; description << "File " << fileName << " already exists."; G4Exception("G4TFileManager::CreateTFile", "Analysis_W001", JustWarning, description); // return it->second->fFile; return nullptr; } auto fileInformation = GetFileInfoInFunction(fileName, "CreateTFile", false); if ( ! fileInformation ) { #ifdef G4VERBOSE if ( fAMState.GetVerboseL4() ) { fAMState.GetVerboseL4()->Message("create", "fileInformation", fileName); } #endif // Create file information and save it in the map fileInformation = new G4TFileInformation(fileName); fFileMap[fileName] = fileInformation; } // Create file and save it in fileInformation #ifdef G4VERBOSE if ( fAMState.GetVerboseL4() ) { fAMState.GetVerboseL4()->Message("create", "file", fileName); } #endif // Let concrete class create a file auto file = CreateFileImpl(fileName); if ( ! file ) { G4ExceptionDescription description; description << "Failed to create file " << fileName; G4Exception("G4TFileManager::CreateTFile", "Analysis_W001", JustWarning, description); return nullptr; } // Save file in fileInformation fileInformation->fFile = file; fileInformation->fIsOpen = true; fileInformation->fIsEmpty = true; fileInformation->fIsDeleted = false; #ifdef G4VERBOSE if ( fAMState.GetVerboseL1() ) { fAMState.GetVerboseL1()->Message("create", "file", fileName); } #endif // Return created file return file; } //_____________________________________________________________________________ template inline G4bool G4TFileManager::WriteTFile(const G4String& fileName) { // Find the file in the map auto file = GetFileInFunction(fileName, "WriteTFile"); // Warning is issued in GetFileInfoFunction if (! file) return false; return WriteTFile(file, fileName); } //_____________________________________________________________________________ template inline G4bool G4TFileManager::CloseTFile(const G4String& fileName) { // Find the file information in the map auto fileInfo = GetFileInfoInFunction(fileName, "CloseTFile"); // Warning is issued in GetFileInfoFunction if ( ! fileInfo ) return false; // Do nothing if file is not open if ( ! fileInfo->fIsOpen ) return false; // Get file from the file information auto file = fileInfo->fFile; if ( ! file ) { FileNotFoundException(fileName, "CloseTFile"); return false; } auto result = CloseTFile(file, fileName); // Update the file information fileInfo->fFile.reset(); fileInfo->fIsOpen = false; return result; } //_____________________________________________________________________________ template inline G4bool G4TFileManager::SetIsEmpty(const G4String& fileName, G4bool isEmpty) { // Find the file information in the map auto fileInfo = GetFileInfoInFunction(fileName, "SetIsEmpty"); // Warning is issued in GetFileFunction if ( ! fileInfo ) return false; #ifdef G4VERBOSE if ( fAMState.GetVerboseL4() ) { fAMState.GetVerboseL4()->Message("notify not empty", "file", fileName); } #endif // Set notification to file information if ( fileInfo->fIsEmpty ) { // do not revert information if file is not empty fileInfo->fIsEmpty = isEmpty; } #ifdef G4VERBOSE if ( fAMState.GetVerboseL2() ) { fAMState.GetVerboseL2()->Message("notify not empty", "file", fileName); } #endif return true; } //_____________________________________________________________________________ template inline std::shared_ptr G4TFileManager::GetTFile( const G4String& fileName, G4bool warn) const { // Find the file information in the map return GetFileInFunction(fileName, "GetTFile", warn); } //_____________________________________________________________________________ template inline G4bool G4TFileManager::OpenFiles() { bool finalResult = true; for ( auto mapElement : fFileMap ) { auto fileInformation = mapElement.second; // Do nothing if file was open by user explicitly if ( fileInformation->fFile ) { G4cout << " ... skipping open file for " << fileInformation->fFileName << G4endl; continue; } auto result = CreateTFile(fileInformation->fFileName); finalResult = result && finalResult; } return finalResult; } //_____________________________________________________________________________ template inline G4bool G4TFileManager::WriteFiles() { bool finalResult = true; for ( auto mapElement : fFileMap ) { auto fileInformation = mapElement.second; if ( ! fileInformation->fIsOpen ) { // G4cout << "skipping write for file " << fileInformation->fFileName << G4endl; continue; } auto result = WriteTFile(fileInformation->fFile, fileInformation->fFileName); finalResult = result && finalResult; } return finalResult; } //_____________________________________________________________________________ template inline G4bool G4TFileManager::CloseFiles() { bool finalResult = true; for ( auto mapElement : fFileMap ) { auto fileInformation = mapElement.second; if ( ! fileInformation->fIsOpen ) { // G4cout << "skipping close for file " << fileInformation->fFileName << G4endl; continue; } auto result = CloseTFile(fileInformation->fFile, fileInformation->fFileName); finalResult = result && finalResult; // Update file information fileInformation->fFile.reset(); fileInformation->fIsOpen = false; } // As the files were set to nullptr, clear should not be needed // fFileMap.clear(); return finalResult; } //_____________________________________________________________________________ template inline G4bool G4TFileManager::DeleteEmptyFiles() { bool finalResult = true; for ( auto mapElement : fFileMap ) { auto fileInformation = mapElement.second; if ( (! fileInformation->fIsEmpty) || fileInformation->fIsDeleted ) { // G4cout << "skipping delete for file " << fileInformation->fFileName << G4endl; continue; } auto result = DeleteEmptyFile(fileInformation->fFileName); finalResult = result && finalResult; // Update file information fileInformation->fIsDeleted = true; } return finalResult; }