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