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