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 // $Id: G4XmlFileManager.cc 70604 2013-06-03 11:27:06Z ihrivnac $ 26 27 27 // Author: Ivana Hrivnacova, 18/06/2013 (ivan 28 // Author: Ivana Hrivnacova, 18/06/2013 (ivana@ipno.in2p3.fr) 28 29 29 #include "G4XmlFileManager.hh" 30 #include "G4XmlFileManager.hh" 30 #include "G4XmlHnFileManager.hh" << 31 #include "G4AnalysisManagerState.hh" 31 #include "G4AnalysisManagerState.hh" 32 #include "G4AnalysisUtilities.hh" << 33 32 34 #include "tools/waxml/begend" 33 #include "tools/waxml/begend" 35 34 36 using namespace G4Analysis; << 37 using namespace tools; << 38 << 39 //____________________________________________ 35 //_____________________________________________________________________________ 40 G4XmlFileManager::G4XmlFileManager(const G4Ana 36 G4XmlFileManager::G4XmlFileManager(const G4AnalysisManagerState& state) 41 : G4VTFileManager<std::ofstream>(state) << 37 : G4VFileManager(state), >> 38 fHnFile(0) 42 { 39 { 43 // Create helpers defined in the base class << 44 fH1FileManager = std::make_shared<G4XmlHnFil << 45 fH2FileManager = std::make_shared<G4XmlHnFil << 46 fH3FileManager = std::make_shared<G4XmlHnFil << 47 fP1FileManager = std::make_shared<G4XmlHnFil << 48 fP2FileManager = std::make_shared<G4XmlHnFil << 49 } 40 } 50 41 51 // << 52 // private methods << 53 // << 54 << 55 //____________________________________________ 42 //_____________________________________________________________________________ 56 G4String G4XmlFileManager::GetNtupleFileName(X << 43 G4XmlFileManager::~G4XmlFileManager() 57 { << 44 { 58 // get ntuple file name << 45 delete fHnFile; 59 auto ntupleFileName = ntupleDescription->Get << 60 if (ntupleFileName.size() != 0u) { << 61 // update filename per object per thread << 62 ntupleFileName = << 63 GetTnFileName(ntupleFileName, GetFileTyp << 64 } << 65 else { << 66 // compose ntuple file name from the defau << 67 ntupleFileName = GetNtupleFileName( << 68 ntupleDescription->GetNtupleBooking().na << 69 } << 70 << 71 return ntupleFileName; << 72 } 46 } 73 47 74 // << 48 // 75 // protected methods << 49 // public methods 76 // 50 // 77 51 78 //____________________________________________ 52 //_____________________________________________________________________________ 79 std::shared_ptr<std::ofstream> G4XmlFileManage << 53 G4bool G4XmlFileManager::OpenFile(const G4String& fileName) 80 { 54 { 81 std::shared_ptr<std::ofstream> file = std::m << 55 // Keep and locks file name 82 if ( file->fail() ) { << 56 fFileName = fileName; 83 Warn(G4String("Cannot create file ") + fil << 57 fLockFileName = true; 84 return nullptr; << 85 } << 86 << 87 waxml::begin(*file); << 88 return file; << 89 } << 90 58 >> 59 return true; >> 60 } >> 61 91 //____________________________________________ 62 //_____________________________________________________________________________ 92 G4bool G4XmlFileManager::WriteFileImpl(std::sh << 63 G4bool G4XmlFileManager::WriteFile() 93 { 64 { 94 // Nothing to be done here 65 // Nothing to be done here 95 return true; 66 return true; 96 } 67 } 97 68 98 //____________________________________________ 69 //_____________________________________________________________________________ 99 G4bool G4XmlFileManager::CloseFileImpl(std::sh << 70 G4bool G4XmlFileManager::CloseFile() 100 { 71 { 101 if ( ! file ) return false; << 72 // Unlock file name 102 << 73 103 // close file << 74 fLockFileName = false; 104 waxml::end(*file); << 75 return true; 105 file->close(); << 76 } >> 77 >> 78 //_____________________________________________________________________________ >> 79 G4bool G4XmlFileManager::CreateHnFile() >> 80 { >> 81 #ifdef G4VERBOSE >> 82 if ( fState.GetVerboseL4() ) >> 83 fState.GetVerboseL4()->Message("create", "histo file", GetFullFileName()); >> 84 #endif >> 85 >> 86 // delete a previous file if it exists >> 87 if ( fHnFile ) delete fHnFile; >> 88 >> 89 fHnFile = new std::ofstream(GetFullFileName()); >> 90 if ( fHnFile->fail() ) { >> 91 G4ExceptionDescription description; >> 92 description << " " << "Cannot open file " << GetFullFileName(); >> 93 G4Exception("G4XmlFileManager::CreateHnFile()", >> 94 "Analysis_W001", JustWarning, description); >> 95 return false; >> 96 } >> 97 >> 98 tools::waxml::begin(*fHnFile); >> 99 #ifdef G4VERBOSE >> 100 if ( fState.GetVerboseL1() ) >> 101 fState.GetVerboseL1()->Message("create", "histo file", GetFullFileName()); >> 102 #endif 106 103 107 return true; 104 return true; 108 } << 105 } 109 << 110 // << 111 // public methods << 112 // << 113 106 114 //____________________________________________ 107 //_____________________________________________________________________________ 115 G4bool G4XmlFileManager::OpenFile(const G4Stri << 108 G4bool G4XmlFileManager::CloseHnFile() 116 { 109 { 117 // Keep and locks file name << 110 // No file may be open if no master manager is instantiated 118 fFileName = fileName; << 111 // and no histograms were booked 119 auto name = GetFullFileName(fFileName); << 112 if ( ! fHnFile ) return true; 120 113 121 if ( fFile ) { << 114 #ifdef G4VERBOSE 122 Warn(G4String("File ") + fileName + " alre << 115 if ( fState.GetVerboseL4() ) 123 fFile.reset(); << 116 fState.GetVerboseL4()->Message("close", "histo file", GetFullFileName()); 124 } << 117 #endif 125 118 126 // Create histograms file (on master) << 119 // close file 127 if ( fState.GetIsMaster() ) { << 120 tools::waxml::end(*fHnFile); 128 // Create file (and save in in the file ma << 121 fHnFile->close(); 129 fFile = CreateTFile(name); << 130 if ( ! fFile) { << 131 Warn(G4String("Failed to create file") + << 132 return false; << 133 } << 134 } << 135 122 136 fIsOpenFile = true; << 123 #ifdef G4VERBOSE >> 124 if ( fState.GetVerboseL1() ) >> 125 fState.GetVerboseL1()->Message("close", "histo file", GetFullFileName()); >> 126 #endif 137 127 138 return true; << 139 } << 140 128 >> 129 return true; >> 130 } >> 131 141 //____________________________________________ 132 //_____________________________________________________________________________ 142 G4bool G4XmlFileManager::CreateNtupleFile( 133 G4bool G4XmlFileManager::CreateNtupleFile( 143 XmlNtupleDescription* ntupleDescription) << 134 G4XmlNtupleDescription* ntupleDescription) 144 { 135 { 145 // Get ntuple file name per object (if defin << 136 G4String ntupleName = ntupleDescription->fNtupleBooking->name(); 146 auto ntupleFileName = GetNtupleFileName(ntup << 147 137 148 // Create ntuple file name if it does not ye << 138 #ifdef G4VERBOSE 149 auto ntupleFile = GetTFile(ntupleFileName, f << 139 if ( fState.GetVerboseL4() ) 150 if ( ntupleFile == nullptr) { << 140 fState.GetVerboseL4() 151 ntupleFile = CreateTFile(ntupleFileName); << 141 ->Message("create", "ntuple file", GetNtupleFileName(ntupleName)); 152 } << 142 #endif >> 143 >> 144 std::ofstream* ntupleFile >> 145 = new std::ofstream(GetNtupleFileName(ntupleName)); >> 146 if ( ntupleFile->fail() ) { >> 147 G4ExceptionDescription description; >> 148 description << " " << "Cannot open file " >> 149 << GetNtupleFileName(ntupleName); >> 150 G4Exception("G4XmlFileManager::CreateNtupleFile()", >> 151 "Analysis_W001", JustWarning, description); >> 152 return false; >> 153 } >> 154 >> 155 tools::waxml::begin(*ntupleFile); >> 156 ntupleDescription->fFile = ntupleFile; >> 157 >> 158 #ifdef G4VERBOSE >> 159 if ( fState.GetVerboseL1() ) >> 160 fState.GetVerboseL1() >> 161 ->Message("create", "ntuple file", GetNtupleFileName(ntupleName)); >> 162 #endif 153 163 154 ntupleDescription->SetFile(std::move(ntupleF << 164 return true; 155 << 165 } 156 return (ntupleDescription->GetFile() != null << 157 } << 158 166 159 //____________________________________________ 167 //_____________________________________________________________________________ 160 G4bool G4XmlFileManager::CloseNtupleFile( 168 G4bool G4XmlFileManager::CloseNtupleFile( 161 XmlNtupleDescription* ntupleDescription) << 169 G4XmlNtupleDescription* ntupleDescription) 162 { 170 { 163 // Notify not empty file << 171 G4String ntupleName = ntupleDescription->fNtupleBooking->name(); 164 auto ntupleFileName = GetNtupleFileName(ntup << 165 auto result = SetIsEmpty(ntupleFileName, ! n << 166 172 167 // Ntuple files are registered in file manag << 173 #ifdef G4VERBOSE 168 // they will be closed with CloseFiles() cal << 174 if ( fState.GetVerboseL4() ) 169 ntupleDescription->GetFile().reset(); << 175 fState.GetVerboseL4() >> 176 ->Message("close", "ntuple file", GetNtupleFileName(ntupleName)); >> 177 #endif 170 178 171 return result; << 179 // close file 172 } << 180 tools::waxml::end(*(ntupleDescription->fFile)); >> 181 ntupleDescription->fFile->close(); >> 182 >> 183 #ifdef G4VERBOSE >> 184 if ( fState.GetVerboseL1() ) >> 185 fState.GetVerboseL1() >> 186 ->Message("close", "ntuple file", GetNtupleFileName(ntupleName)); >> 187 #endif >> 188 >> 189 return true; >> 190 } >> 191 173 192