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