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: G4CsvFileManager.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 "G4CsvFileManager.hh" 30 #include "G4CsvFileManager.hh" 30 #include "G4CsvHnFileManager.hh" << 31 #include "G4AnalysisManagerState.hh" 31 #include "G4AnalysisManagerState.hh" 32 #include "G4AnalysisUtilities.hh" << 33 #include "G4Filesystem.hh" << 34 << 35 using namespace G4Analysis; << 36 using namespace tools; << 37 32 38 //____________________________________________ 33 //_____________________________________________________________________________ 39 G4CsvFileManager::G4CsvFileManager(const G4Ana 34 G4CsvFileManager::G4CsvFileManager(const G4AnalysisManagerState& state) 40 : G4VTFileManager(state) << 35 : G4VFileManager(state) 41 { << 36 {} 42 // Create helpers defined in the base class << 43 fH1FileManager = std::make_shared<G4CsvHnFil << 44 fH2FileManager = std::make_shared<G4CsvHnFil << 45 fH3FileManager = std::make_shared<G4CsvHnFil << 46 fP1FileManager = std::make_shared<G4CsvHnFil << 47 fP2FileManager = std::make_shared<G4CsvHnFil << 48 } << 49 << 50 // << 51 // private methods << 52 // << 53 << 54 //____________________________________________ << 55 G4String G4CsvFileManager::GetNtupleFileName(C << 56 { << 57 // get ntuple file name << 58 auto ntupleFileName = ntupleDescription->Get << 59 auto cycle = GetCycle(); << 60 if (ntupleFileName.size() != 0u) { << 61 // update filename per object per thread << 62 ntupleFileName = GetTnFileName(ntupleFileN << 63 } << 64 else { << 65 // compose ntuple file name from the defau << 66 ntupleFileName = GetNtupleFileName(ntupleD << 67 } << 68 << 69 if ( IsNtupleDirectory() ) { << 70 ntupleFileName = "./" + GetNtupleDirectory << 71 } << 72 << 73 return ntupleFileName; << 74 } << 75 << 76 // << 77 // protected methods << 78 // << 79 37 80 //____________________________________________ 38 //_____________________________________________________________________________ 81 std::shared_ptr<std::ofstream> G4CsvFileManage << 39 G4CsvFileManager::~G4CsvFileManager() 82 { << 40 {} 83 std::shared_ptr<std::ofstream> file = std::m << 84 if ( file->fail() ) { << 85 Warn("Cannot create file " + fileName, fkC << 86 return nullptr; << 87 } << 88 << 89 return file; << 90 } << 91 << 92 //____________________________________________ << 93 G4bool G4CsvFileManager::WriteFileImpl(std::sh << 94 { << 95 // Nothing to be done here << 96 return true; << 97 } << 98 << 99 //____________________________________________ << 100 G4bool G4CsvFileManager::CloseFileImpl(std::sh << 101 { << 102 if ( ! file ) return false; << 103 41 104 // close file << 42 // 105 file->close(); << 106 << 107 return true; << 108 } << 109 << 110 // << 111 // public methods 43 // public methods 112 // 44 // 113 45 114 //____________________________________________ 46 //_____________________________________________________________________________ 115 G4bool G4CsvFileManager::OpenFile(const G4Stri 47 G4bool G4CsvFileManager::OpenFile(const G4String& fileName) 116 { 48 { 117 // Keep file name 49 // Keep file name 118 fFileName = fileName; 50 fFileName = fileName; 119 51 >> 52 fLockFileName = true; >> 53 fLockNtupleDirectoryName = true; 120 fIsOpenFile = true; 54 fIsOpenFile = true; 121 << 55 122 return true; 56 return true; 123 } << 57 } 124 << 58 125 //____________________________________________ << 126 G4bool G4CsvFileManager::SetHistoDirectoryName << 127 { << 128 // A directory is taken into account only if << 129 if ( G4fs::is_directory(dirName.data()) ) { << 130 fIsHistoDirectory = G4VFileManager::SetHi << 131 return fIsHistoDirectory; << 132 } << 133 << 134 G4Analysis::Warn("Directory " + dirName + " << 135 "Histograms will be written in the current << 136 fkClass, "SetHistoDirectoryName"); << 137 return false; << 138 } << 139 << 140 //____________________________________________ 59 //_____________________________________________________________________________ 141 G4bool G4CsvFileManager::SetNtupleDirectoryNam << 60 G4bool G4CsvFileManager::WriteFile() 142 { 61 { 143 // A directory is taken into account only if << 62 // nothing to be done for Csv file 144 if ( G4fs::is_directory(dirName.data()) ) { << 63 return true; 145 fIsNtupleDirectory = G4VFileManager::SetN << 146 return fIsNtupleDirectory; << 147 } << 148 << 149 G4Analysis::Warn("Directory " + dirName + " << 150 "Ntuples will be written in the current di << 151 fkClass, "SetNtupleDirectoryName"); << 152 return false; << 153 } 64 } 154 65 155 //____________________________________________ 66 //_____________________________________________________________________________ 156 G4bool G4CsvFileManager::NotifyNtupleFile(CsvN << 67 G4bool G4CsvFileManager::CloseFile() 157 { 68 { 158 // Notify not empty file << 69 fLockFileName = false; 159 auto ntupleFileName = GetNtupleFileName(ntup << 70 fIsOpenFile = false; 160 << 71 return true; 161 return SetIsEmpty(ntupleFileName, ! ntupleDe << 72 } 162 } << 73 163 << 164 //____________________________________________ 74 //_____________________________________________________________________________ 165 G4bool G4CsvFileManager::CreateNtupleFile( 75 G4bool G4CsvFileManager::CreateNtupleFile( 166 CsvNtupleDescription* ntupleDescription) << 76 G4TNtupleDescription<tools::wcsv::ntuple>* ntupleDescription) 167 { 77 { 168 // Get ntuple file name per object (if defin << 78 G4String ntupleName = ntupleDescription->fNtupleBooking.name(); 169 auto ntupleFileName = GetNtupleFileName(ntup << 170 << 171 // Update file name if it is already in use << 172 while ( GetTFile(ntupleFileName, false) != n << 173 // the file is already in use << 174 auto oldName = ntupleFileName; << 175 auto newName = GetBaseName(oldName) + "_bi << 176 ntupleDescription->SetFileName(newName); << 177 << 178 Warn("Ntuple filename " + oldName + " is a << 179 "It will be replaced with : " + newNa << 180 fkClass, "CreateNtupleFile"); << 181 79 182 ntupleFileName = GetNtupleFileName(ntupleD << 80 #ifdef G4VERBOSE 183 } << 81 if ( fState.GetVerboseL4() ) 184 << 82 fState.GetVerboseL4() 185 // Create new ntuple file << 83 ->Message("create", "file", GetNtupleFileName(ntupleName)); 186 ntupleDescription->SetFile(CreateTFile(ntupl << 84 #endif >> 85 >> 86 auto ntupleFile = new std::ofstream(GetNtupleFileName(ntupleName)); >> 87 if ( ntupleFile->fail() ) { >> 88 G4ExceptionDescription description; >> 89 description << " " << "Cannot open file " >> 90 << GetNtupleFileName(ntupleName); >> 91 G4Exception("G4CsvFileManager::CreateNtupleFile()", >> 92 "Analysis_W001", JustWarning, description); >> 93 return false; >> 94 } >> 95 >> 96 #ifdef G4VERBOSE >> 97 if ( fState.GetVerboseL1() ) >> 98 fState.GetVerboseL1() >> 99 ->Message("create", "file", GetNtupleFileName(ntupleName)); >> 100 #endif 187 101 188 return (ntupleDescription->GetFile() != null << 102 ntupleDescription->fFile = ntupleFile; 189 } << 103 return true; >> 104 } 190 105 191 //____________________________________________ 106 //_____________________________________________________________________________ 192 G4bool G4CsvFileManager::CloseNtupleFile( 107 G4bool G4CsvFileManager::CloseNtupleFile( 193 CsvNtupleDescription* ntupleDescription) << 108 G4TNtupleDescription<tools::wcsv::ntuple>* ntupleDescription) 194 { 109 { 195 // Notifying not empty file is done in G4Csv << 110 G4String ntupleName = ntupleDescription->fNtupleBooking.name(); 196 // as here we increment the cycle number and << 197 // for the next cycle version. << 198 111 199 // Ntuple files are registered in file manag << 112 #ifdef G4VERBOSE 200 // they will be closed with CloseFiles() cal << 113 if ( fState.GetVerboseL4() ) >> 114 fState.GetVerboseL4() >> 115 ->Message("close", "file", GetNtupleFileName(ntupleName)); >> 116 #endif 201 117 202 ntupleDescription->GetFile().reset(); << 118 // close file >> 119 ntupleDescription->fFile->close(); 203 120 204 return true; << 121 #ifdef G4VERBOSE 205 } << 122 if ( fState.GetVerboseL1() ) >> 123 fState.GetVerboseL1() >> 124 ->Message("close", "file", GetNtupleFileName(ntupleName)); >> 125 #endif >> 126 >> 127 return true; >> 128 } >> 129 206 130