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->GetFileName(); 59 auto cycle = GetCycle(); 59 auto cycle = GetCycle(); 60 if (ntupleFileName.size() != 0u) { 60 if (ntupleFileName.size() != 0u) { 61 // update filename per object per thread 61 // update filename per object per thread 62 ntupleFileName = GetTnFileName(ntupleFileN 62 ntupleFileName = GetTnFileName(ntupleFileName, GetFileType(), cycle); 63 } 63 } 64 else { 64 else { 65 // compose ntuple file name from the defau 65 // compose ntuple file name from the default file name 66 ntupleFileName = GetNtupleFileName(ntupleD 66 ntupleFileName = GetNtupleFileName(ntupleDescription->GetNtupleBooking().name(), cycle); 67 } 67 } 68 68 69 if ( IsNtupleDirectory() ) { 69 if ( IsNtupleDirectory() ) { 70 ntupleFileName = "./" + GetNtupleDirectory 70 ntupleFileName = "./" + GetNtupleDirectoryName() + "/" + ntupleFileName; 71 } 71 } 72 72 73 return ntupleFileName; 73 return ntupleFileName; 74 } 74 } 75 75 76 // 76 // 77 // protected methods 77 // protected methods 78 // 78 // 79 79 80 //____________________________________________ 80 //_____________________________________________________________________________ 81 std::shared_ptr<std::ofstream> G4CsvFileManage 81 std::shared_ptr<std::ofstream> G4CsvFileManager::CreateFileImpl(const G4String& fileName) 82 { 82 { 83 std::shared_ptr<std::ofstream> file = std::m 83 std::shared_ptr<std::ofstream> file = std::make_shared<std::ofstream>(fileName); 84 if ( file->fail() ) { 84 if ( file->fail() ) { 85 Warn("Cannot create file " + fileName, fkC 85 Warn("Cannot create file " + fileName, fkClass, "CreateFileImpl"); 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 //____________________________________________ 125 //_____________________________________________________________________________ 126 G4bool G4CsvFileManager::SetHistoDirectoryName 126 G4bool G4CsvFileManager::SetHistoDirectoryName(const G4String& dirName) 127 { 127 { 128 // A directory is taken into account only if 128 // A directory is taken into account only if it exists in file system 129 if ( G4fs::is_directory(dirName.data()) ) { 129 if ( G4fs::is_directory(dirName.data()) ) { 130 fIsHistoDirectory = G4VFileManager::SetHi 130 fIsHistoDirectory = G4VFileManager::SetHistoDirectoryName(dirName); 131 return fIsHistoDirectory; 131 return fIsHistoDirectory; 132 } 132 } 133 133 134 G4Analysis::Warn("Directory " + dirName + " 134 G4Analysis::Warn("Directory " + dirName + " does not exists.\n" 135 "Histograms will be written in the current 135 "Histograms will be written in the current directory.", 136 fkClass, "SetHistoDirectoryName"); 136 fkClass, "SetHistoDirectoryName"); 137 return false; 137 return false; 138 } 138 } 139 139 140 //____________________________________________ 140 //_____________________________________________________________________________ 141 G4bool G4CsvFileManager::SetNtupleDirectoryNam 141 G4bool G4CsvFileManager::SetNtupleDirectoryName(const G4String& dirName) 142 { 142 { 143 // A directory is taken into account only if 143 // A directory is taken into account only if it exists in file system 144 if ( G4fs::is_directory(dirName.data()) ) { 144 if ( G4fs::is_directory(dirName.data()) ) { 145 fIsNtupleDirectory = G4VFileManager::SetN 145 fIsNtupleDirectory = G4VFileManager::SetNtupleDirectoryName(dirName); 146 return fIsNtupleDirectory; 146 return fIsNtupleDirectory; 147 } 147 } 148 148 149 G4Analysis::Warn("Directory " + dirName + " 149 G4Analysis::Warn("Directory " + dirName + " does not exists.\n" 150 "Ntuples will be written in the current di 150 "Ntuples will be written in the current directory.", 151 fkClass, "SetNtupleDirectoryName"); 151 fkClass, "SetNtupleDirectoryName"); 152 return false; 152 return false; 153 } 153 } 154 154 155 //____________________________________________ 155 //_____________________________________________________________________________ 156 G4bool G4CsvFileManager::NotifyNtupleFile(CsvN 156 G4bool G4CsvFileManager::NotifyNtupleFile(CsvNtupleDescription* ntupleDescription) 157 { 157 { 158 // Notify not empty file 158 // Notify not empty file 159 auto ntupleFileName = GetNtupleFileName(ntup 159 auto ntupleFileName = GetNtupleFileName(ntupleDescription); 160 160 161 return SetIsEmpty(ntupleFileName, ! ntupleDe 161 return SetIsEmpty(ntupleFileName, ! ntupleDescription->GetHasFill()); 162 } 162 } 163 163 164 //____________________________________________ 164 //_____________________________________________________________________________ 165 G4bool G4CsvFileManager::CreateNtupleFile( 165 G4bool G4CsvFileManager::CreateNtupleFile( 166 CsvNtupleDescription* ntupleDescription) 166 CsvNtupleDescription* ntupleDescription) 167 { 167 { 168 // Get ntuple file name per object (if defin 168 // Get ntuple file name per object (if defined) 169 auto ntupleFileName = GetNtupleFileName(ntup 169 auto ntupleFileName = GetNtupleFileName(ntupleDescription); 170 170 171 // Update file name if it is already in use 171 // Update file name if it is already in use 172 while ( GetTFile(ntupleFileName, false) != n 172 while ( GetTFile(ntupleFileName, false) != nullptr ) { 173 // the file is already in use 173 // the file is already in use 174 auto oldName = ntupleFileName; 174 auto oldName = ntupleFileName; 175 auto newName = GetBaseName(oldName) + "_bi 175 auto newName = GetBaseName(oldName) + "_bis." + GetExtension(oldName); 176 ntupleDescription->SetFileName(newName); 176 ntupleDescription->SetFileName(newName); 177 177 178 Warn("Ntuple filename " + oldName + " is a 178 Warn("Ntuple filename " + oldName + " is already in use.\n" + 179 "It will be replaced with : " + newNa 179 "It will be replaced with : " + newName, 180 fkClass, "CreateNtupleFile"); 180 fkClass, "CreateNtupleFile"); 181 181 182 ntupleFileName = GetNtupleFileName(ntupleD 182 ntupleFileName = GetNtupleFileName(ntupleDescription); 183 } 183 } 184 184 185 // Create new ntuple file 185 // Create new ntuple file 186 ntupleDescription->SetFile(CreateTFile(ntupl 186 ntupleDescription->SetFile(CreateTFile(ntupleFileName)); 187 187 188 return (ntupleDescription->GetFile() != null 188 return (ntupleDescription->GetFile() != nullptr); 189 } 189 } 190 190 191 //____________________________________________ 191 //_____________________________________________________________________________ 192 G4bool G4CsvFileManager::CloseNtupleFile( 192 G4bool G4CsvFileManager::CloseNtupleFile( 193 CsvNtupleDescription* ntupleDescription) 193 CsvNtupleDescription* ntupleDescription) 194 { 194 { 195 // Notifying not empty file is done in G4Csv 195 // Notifying not empty file is done in G4CsvNtupleFileManager::ActionAtWrite, 196 // as here we increment the cycle number and 196 // as here we increment the cycle number and GetNtupleFileName returns a file name 197 // for the next cycle version. 197 // for the next cycle version. 198 198 199 // Ntuple files are registered in file manag 199 // Ntuple files are registered in file manager map. 200 // they will be closed with CloseFiles() cal 200 // they will be closed with CloseFiles() calls 201 201 202 ntupleDescription->GetFile().reset(); 202 ntupleDescription->GetFile().reset(); 203 203 204 return true; 204 return true; 205 } 205 } 206 206