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, 15/06/2011 (ivan 27 // Author: Ivana Hrivnacova, 15/06/2011 (ivana@ipno.in2p3.fr) 28 28 29 #include "G4RootFileManager.hh" 29 #include "G4RootFileManager.hh" 30 #include "G4RootHnFileManager.hh" 30 #include "G4RootHnFileManager.hh" 31 #include "G4AnalysisManagerState.hh" 31 #include "G4AnalysisManagerState.hh" 32 #include "G4AnalysisUtilities.hh" 32 #include "G4AnalysisUtilities.hh" 33 33 34 #include "tools/wroot/file" 34 #include "tools/wroot/file" 35 #include "tools/wroot/to" 35 #include "tools/wroot/to" 36 #include "toolx/zlib" 36 #include "toolx/zlib" 37 37 38 using namespace tools; 38 using namespace tools; 39 using namespace G4Analysis; 39 using namespace G4Analysis; 40 40 41 //____________________________________________ 41 //_____________________________________________________________________________ 42 G4RootFileManager::G4RootFileManager(const G4A 42 G4RootFileManager::G4RootFileManager(const G4AnalysisManagerState& state) 43 : G4VTFileManager<G4RootFile>(state) 43 : G4VTFileManager<G4RootFile>(state) 44 { 44 { 45 // Create helpers defined in the base class 45 // Create helpers defined in the base class 46 fH1FileManager = std::make_shared<G4RootHnFi 46 fH1FileManager = std::make_shared<G4RootHnFileManager<histo::h1d>>(this); 47 fH2FileManager = std::make_shared<G4RootHnFi 47 fH2FileManager = std::make_shared<G4RootHnFileManager<histo::h2d>>(this); 48 fH3FileManager = std::make_shared<G4RootHnFi 48 fH3FileManager = std::make_shared<G4RootHnFileManager<histo::h3d>>(this); 49 fP1FileManager = std::make_shared<G4RootHnFi 49 fP1FileManager = std::make_shared<G4RootHnFileManager<histo::p1d>>(this); 50 fP2FileManager = std::make_shared<G4RootHnFi 50 fP2FileManager = std::make_shared<G4RootHnFileManager<histo::p2d>>(this); 51 } 51 } 52 52 53 // 53 // 54 // private methods 54 // private methods 55 // 55 // 56 56 57 //____________________________________________ 57 //_____________________________________________________________________________ 58 tools::wroot::directory* G4RootFileManager::C 58 tools::wroot::directory* G4RootFileManager::CreateDirectory( 59 tools::wroot::file* rfile, 59 tools::wroot::file* rfile, 60 const G4String& directoryName, [[maybe_unus 60 const G4String& directoryName, [[maybe_unused]] const G4String& objectType) const 61 { 61 { 62 if (rfile == nullptr) return nullptr; 62 if (rfile == nullptr) return nullptr; 63 63 64 if ( directoryName == "" ) { 64 if ( directoryName == "" ) { 65 // Do not create a new directory if its na 65 // Do not create a new directory if its name is not set 66 return &(rfile->dir()); 66 return &(rfile->dir()); 67 } 67 } 68 68 69 Message(kVL4, "create", "directory for " + o 69 Message(kVL4, "create", "directory for " + objectType, directoryName); 70 70 71 auto directory = rfile->dir().mkdir(director 71 auto directory = rfile->dir().mkdir(directoryName); 72 if (directory == nullptr) { 72 if (directory == nullptr) { 73 Warn("Cannot create directory " + director 73 Warn("Cannot create directory " + directoryName, fkClass, "CreateDirectory"); 74 return nullptr; 74 return nullptr; 75 } 75 } 76 Message(kVL2, "create", "directory for " + o 76 Message(kVL2, "create", "directory for " + objectType, directoryName); 77 77 78 return directory; 78 return directory; 79 } 79 } 80 80 81 //____________________________________________ 81 //_____________________________________________________________________________ 82 G4String G4RootFileManager::GetNtupleFileName( 82 G4String G4RootFileManager::GetNtupleFileName( 83 RootNtupleDescri 83 RootNtupleDescription* ntupleDescription, 84 G4bool perThread 84 G4bool perThread, 85 G4int mainNumber 85 G4int mainNumber) const 86 { 86 { 87 // get ntuple file name 87 // get ntuple file name 88 88 89 auto ntupleFileName = ntupleDescription->Get 89 auto ntupleFileName = ntupleDescription->GetFileName(); 90 if (ntupleFileName.size() != 0u) { 90 if (ntupleFileName.size() != 0u) { 91 if ( perThread ) { 91 if ( perThread ) { 92 ntupleFileName = GetTnFileName(ntupleFil 92 ntupleFileName = GetTnFileName(ntupleFileName, GetFileType()); 93 } 93 } 94 } 94 } 95 else { 95 else { 96 // get default file name 96 // get default file name 97 ntupleFileName = GetFullFileName(fFileName 97 ntupleFileName = GetFullFileName(fFileName, perThread); 98 } 98 } 99 99 100 // update filename per mainNumber 100 // update filename per mainNumber 101 if ( mainNumber > -1 ) { 101 if ( mainNumber > -1 ) { 102 // update filename per mainNumber 102 // update filename per mainNumber 103 ntupleFileName 103 ntupleFileName 104 = G4Analysis::GetNtupleFileName(ntupleFi 104 = G4Analysis::GetNtupleFileName(ntupleFileName, GetFileType(), mainNumber); 105 } 105 } 106 106 107 return ntupleFileName; 107 return ntupleFileName; 108 } 108 } 109 109 110 // 110 // 111 // protected methods 111 // protected methods 112 // 112 // 113 113 114 //____________________________________________ 114 //_____________________________________________________________________________ 115 std::shared_ptr<G4RootFile> 115 std::shared_ptr<G4RootFile> 116 G4RootFileManager::CreateFileImpl(const G4Stri 116 G4RootFileManager::CreateFileImpl(const G4String& fileName) 117 { 117 { 118 // create file 118 // create file 119 std::shared_ptr<wroot::file> file = std::mak 119 std::shared_ptr<wroot::file> file = std::make_shared<wroot::file>(G4cout, fileName); 120 file->add_ziper('Z',toolx::compress_buffer); 120 file->add_ziper('Z',toolx::compress_buffer); 121 file->set_compression(GetCompressionLevel()) << 121 file->set_compression(fState.GetCompressionLevel()); 122 122 123 if ( ! file->is_open() ) { 123 if ( ! file->is_open() ) { 124 Warn("Cannot create file " + fileName, fkC 124 Warn("Cannot create file " + fileName, fkClass, "CreateFileImpl"); 125 return std::make_shared<G4RootFile>(nullpt 125 return std::make_shared<G4RootFile>(nullptr, nullptr, nullptr); 126 } 126 } 127 127 128 // create histo directory 128 // create histo directory 129 tools::wroot::directory* hdirectory 129 tools::wroot::directory* hdirectory 130 = CreateDirectory(file.get(), fHistoDirect 130 = CreateDirectory(file.get(), fHistoDirectoryName, "histograms"); 131 if (hdirectory == nullptr) { 131 if (hdirectory == nullptr) { 132 // Warning is issued in CreateDirectory 132 // Warning is issued in CreateDirectory 133 return std::make_shared<G4RootFile>(nullpt 133 return std::make_shared<G4RootFile>(nullptr, nullptr, nullptr); 134 } 134 } 135 135 136 // create ntuple directory 136 // create ntuple directory 137 tools::wroot::directory* ndirectory 137 tools::wroot::directory* ndirectory 138 = CreateDirectory(file.get(), fNtupleDirec 138 = CreateDirectory(file.get(), fNtupleDirectoryName, "ntuples"); 139 if (ndirectory == nullptr) { 139 if (ndirectory == nullptr) { 140 // Warning is issued in CreateDirectory 140 // Warning is issued in CreateDirectory 141 return std::make_shared<G4RootFile>(nullpt 141 return std::make_shared<G4RootFile>(nullptr, nullptr, nullptr); 142 } 142 } 143 143 144 return std::make_shared<G4RootFile>(file, hd 144 return std::make_shared<G4RootFile>(file, hdirectory, ndirectory); 145 } 145 } 146 146 147 //____________________________________________ 147 //_____________________________________________________________________________ 148 G4bool G4RootFileManager::WriteFileImpl(std::s 148 G4bool G4RootFileManager::WriteFileImpl(std::shared_ptr<G4RootFile> file) 149 { 149 { 150 // New prototype: called by G4TFileManager bas 150 // New prototype: called by G4TFileManager base classe 151 151 152 // nothing to be done, but file should exist 152 // nothing to be done, but file should exist 153 return (file == nullptr) ? false : true; 153 return (file == nullptr) ? false : true; 154 154 155 } 155 } 156 156 157 //____________________________________________ 157 //_____________________________________________________________________________ 158 G4bool G4RootFileManager::CloseFileImpl(std::s 158 G4bool G4RootFileManager::CloseFileImpl(std::shared_ptr<G4RootFile> file) 159 { 159 { 160 // New prototype: called by G4TFileManager bas 160 // New prototype: called by G4TFileManager base classe 161 161 162 if (file == nullptr) return false; 162 if (file == nullptr) return false; 163 163 164 // write file (only once) 164 // write file (only once) 165 unsigned int n; 165 unsigned int n; 166 std::get<0>(*file)->write(n); 166 std::get<0>(*file)->write(n); 167 167 168 // close file 168 // close file 169 std::get<0>(*file)->close(); 169 std::get<0>(*file)->close(); 170 170 171 return true; 171 return true; 172 } 172 } 173 173 174 // 174 // 175 // public methods 175 // public methods 176 // 176 // 177 //____________________________________________ 177 //_____________________________________________________________________________ 178 G4bool G4RootFileManager::OpenFile(const G4Str 178 G4bool G4RootFileManager::OpenFile(const G4String& fileName) 179 { 179 { 180 // Open default file 180 // Open default file 181 181 182 // Keep file name 182 // Keep file name 183 fFileName = fileName; 183 fFileName = fileName; 184 auto name = GetFullFileName(); 184 auto name = GetFullFileName(); 185 185 186 if ( fFile ) { 186 if ( fFile ) { 187 Warn("File " + fileName + " already exists 187 Warn("File " + fileName + " already exists.", fkClass, "OpenFile"); 188 fFile.reset(); 188 fFile.reset(); 189 } 189 } 190 190 191 // Create file (and save in in the file map) 191 // Create file (and save in in the file map) 192 fFile = CreateTFile(name); 192 fFile = CreateTFile(name); 193 if ( ! fFile ) { 193 if ( ! fFile ) { 194 Warn("Failed to create file " + fileName, 194 Warn("Failed to create file " + fileName, fkClass, "OpenFile"); 195 return false; 195 return false; 196 } 196 } 197 197 198 LockDirectoryNames(); 198 LockDirectoryNames(); 199 fIsOpenFile = true; 199 fIsOpenFile = true; 200 200 201 return true; 201 return true; 202 } 202 } 203 203 204 //____________________________________________ 204 //_____________________________________________________________________________ 205 std::shared_ptr<G4RootFile> G4RootFileManager: 205 std::shared_ptr<G4RootFile> G4RootFileManager::CreateNtupleFile( 206 RootNtupleDescription* ntupleDescription, G4 206 RootNtupleDescription* ntupleDescription, G4int mainNumber) 207 { 207 { 208 // get ntuple file name per object 208 // get ntuple file name per object 209 auto perThread = true; 209 auto perThread = true; 210 auto ntupleFileName = GetNtupleFileName(ntup 210 auto ntupleFileName = GetNtupleFileName(ntupleDescription, perThread, mainNumber); 211 211 212 auto file = GetTFile(ntupleFileName, false); 212 auto file = GetTFile(ntupleFileName, false); 213 if (! file) { 213 if (! file) { 214 file = CreateTFile(ntupleFileName); 214 file = CreateTFile(ntupleFileName); 215 } 215 } 216 216 217 // register file in ntuple description only 217 // register file in ntuple description only if it is not main ntuple file 218 // (main ntuple files are managed in main nt 218 // (main ntuple files are managed in main ntuple manager) 219 if ( mainNumber == -1 ) { 219 if ( mainNumber == -1 ) { 220 ntupleDescription->SetFile(file); 220 ntupleDescription->SetFile(file); 221 } 221 } 222 222 223 return file; 223 return file; 224 } 224 } 225 225 226 //____________________________________________ 226 //_____________________________________________________________________________ 227 std::shared_ptr<G4RootFile> G4RootFileManager: 227 std::shared_ptr<G4RootFile> G4RootFileManager::GetNtupleFile( 228 RootNtupleDescription* ntupleDescription, G 228 RootNtupleDescription* ntupleDescription, G4bool perThread, G4int mainNumber) const 229 { 229 { 230 // get ntuple file name per object 230 // get ntuple file name per object 231 auto ntupleFileName = GetNtupleFileName(ntup 231 auto ntupleFileName = GetNtupleFileName(ntupleDescription, perThread, mainNumber); 232 232 233 return GetTFile(ntupleFileName, false); 233 return GetTFile(ntupleFileName, false); 234 } 234 } 235 235 236 //____________________________________________ 236 //_____________________________________________________________________________ 237 G4bool G4RootFileManager::CloseNtupleFile( 237 G4bool G4RootFileManager::CloseNtupleFile( 238 RootNtupleDescription* ntupleDescription, G 238 RootNtupleDescription* ntupleDescription, G4int mainNumber) 239 { 239 { 240 // auto result = true; 240 // auto result = true; 241 241 242 // Notify not empty file 242 // Notify not empty file 243 auto ntupleFileName = GetNtupleFileName(ntup 243 auto ntupleFileName = GetNtupleFileName(ntupleDescription, true, mainNumber); 244 auto result = SetIsEmpty(ntupleFileName, ! n 244 auto result = SetIsEmpty(ntupleFileName, ! ntupleDescription->GetHasFill()); 245 245 246 // Ntuple files will be closed with CloseFil 246 // Ntuple files will be closed with CloseFiles() calls 247 ntupleDescription->GetFile().reset(); 247 ntupleDescription->GetFile().reset(); 248 248 249 return result; 249 return result; 250 } 250 } 251 251