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 // $Id: G4RootFileManager.cc 70604 2013-06-03 11:27:06Z ihrivnac $ 26 27 27 // Author: Ivana Hrivnacova, 15/06/2011 (ivan 28 // Author: Ivana Hrivnacova, 15/06/2011 (ivana@ipno.in2p3.fr) 28 29 29 #include "G4RootFileManager.hh" 30 #include "G4RootFileManager.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/zlib" 36 #include "toolx/zlib" << 36 >> 37 #include <iostream> >> 38 #include <cstdio> 37 39 38 using namespace tools; << 39 using namespace G4Analysis; 40 using namespace G4Analysis; 40 41 41 //____________________________________________ 42 //_____________________________________________________________________________ 42 G4RootFileManager::G4RootFileManager(const G4A 43 G4RootFileManager::G4RootFileManager(const G4AnalysisManagerState& state) 43 : G4VTFileManager<G4RootFile>(state) << 44 : G4VFileManager(state), 44 { << 45 fFile(nullptr), 45 // Create helpers defined in the base class << 46 fHistoDirectory(nullptr), 46 fH1FileManager = std::make_shared<G4RootHnFi << 47 fNtupleDirectory(nullptr), 47 fH2FileManager = std::make_shared<G4RootHnFi << 48 fNofNtupleFiles(0), 48 fH3FileManager = std::make_shared<G4RootHnFi << 49 fNtupleFiles(), 49 fP1FileManager = std::make_shared<G4RootHnFi << 50 fMainNtupleDirectories(), 50 fP2FileManager = std::make_shared<G4RootHnFi << 51 fBasketSize(0) 51 } << 52 {} 52 << 53 // << 54 // private methods << 55 // << 56 << 57 //____________________________________________ << 58 tools::wroot::directory* G4RootFileManager::C << 59 tools::wroot::file* rfile, << 60 const G4String& directoryName, [[maybe_unus << 61 { << 62 if (rfile == nullptr) return nullptr; << 63 << 64 if ( directoryName == "" ) { << 65 // Do not create a new directory if its na << 66 return &(rfile->dir()); << 67 } << 68 << 69 Message(kVL4, "create", "directory for " + o << 70 << 71 auto directory = rfile->dir().mkdir(director << 72 if (directory == nullptr) { << 73 Warn("Cannot create directory " + director << 74 return nullptr; << 75 } << 76 Message(kVL2, "create", "directory for " + o << 77 << 78 return directory; << 79 } << 80 53 81 //____________________________________________ 54 //_____________________________________________________________________________ 82 G4String G4RootFileManager::GetNtupleFileName( << 55 G4RootFileManager::~G4RootFileManager() 83 RootNtupleDescri << 56 {} 84 G4bool perThread << 85 G4int mainNumber << 86 { << 87 // get ntuple file name << 88 << 89 auto ntupleFileName = ntupleDescription->Get << 90 if (ntupleFileName.size() != 0u) { << 91 if ( perThread ) { << 92 ntupleFileName = GetTnFileName(ntupleFil << 93 } << 94 } << 95 else { << 96 // get default file name << 97 ntupleFileName = GetFullFileName(fFileName << 98 } << 99 << 100 // update filename per mainNumber << 101 if ( mainNumber > -1 ) { << 102 // update filename per mainNumber << 103 ntupleFileName << 104 = G4Analysis::GetNtupleFileName(ntupleFi << 105 } << 106 << 107 return ntupleFileName; << 108 } << 109 57 110 // 58 // 111 // protected methods << 59 // private methods 112 // 60 // 113 61 114 //____________________________________________ 62 //_____________________________________________________________________________ 115 std::shared_ptr<G4RootFile> << 63 G4bool G4RootFileManager::OpenNtupleFiles() 116 G4RootFileManager::CreateFileImpl(const G4Stri << 117 { 64 { 118 // create file << 65 auto finalResult = true; 119 std::shared_ptr<wroot::file> file = std::mak << 120 file->add_ziper('Z',toolx::compress_buffer); << 121 file->set_compression(GetCompressionLevel()) << 122 66 123 if ( ! file->is_open() ) { << 67 for ( auto i = 0; i < fNofNtupleFiles; i++ ) { 124 Warn("Cannot create file " + fileName, fkC << 68 125 return std::make_shared<G4RootFile>(nullpt << 69 auto name = GetNtupleFileName(i); 126 } << 70 >> 71 #ifdef G4VERBOSE >> 72 if ( fState.GetVerboseL4() ) >> 73 fState.GetVerboseL4() >> 74 ->Message("create", "main ntuple file", name); >> 75 #endif >> 76 >> 77 // create new file >> 78 auto rfile = std::make_shared<tools::wroot::file>(G4cout, name); >> 79 rfile->add_ziper('Z', tools::compress_buffer); >> 80 rfile->set_compression(fState.GetCompressionLevel()); >> 81 >> 82 if ( ! rfile->is_open() ) { >> 83 G4ExceptionDescription description; >> 84 description << " " << "Cannot open file " << name; >> 85 G4Exception("G4RootAnalysisManager::OpenFile()", >> 86 "Analysis_W001", JustWarning, description); >> 87 finalResult = false; >> 88 } 127 89 128 // create histo directory << 90 // Do not create directory if extra ntuple files 129 tools::wroot::directory* hdirectory << 91 // auto result = CreateNtupleDirectory(rfile); 130 = CreateDirectory(file.get(), fHistoDirect << 92 // finalResult = finalResult && result; 131 if (hdirectory == nullptr) { << 93 tools::wroot::directory* directory = &rfile->dir(); 132 // Warning is issued in CreateDirectory << 94 if ( fNtupleDirectoryName != "" ) { 133 return std::make_shared<G4RootFile>(nullpt << 95 directory = rfile->dir().mkdir(fNtupleDirectoryName); 134 } << 96 if ( ! directory ) { >> 97 G4ExceptionDescription description; >> 98 description << " " >> 99 << "cannot create directory " << fNtupleDirectoryName; >> 100 G4Exception("G4RootFileManager::OpenNtupleFiles()", >> 101 "Analysis_W001", JustWarning, description); >> 102 directory = &fFile->dir(); >> 103 } >> 104 } >> 105 >> 106 fNtupleFiles.push_back(rfile); >> 107 fMainNtupleDirectories.push_back(directory); >> 108 >> 109 #ifdef G4VERBOSE >> 110 if ( fState.GetVerboseL1() ) >> 111 fState.GetVerboseL1() >> 112 ->Message("create", "main ntuple file", name); >> 113 #endif 135 114 136 // create ntuple directory << 137 tools::wroot::directory* ndirectory << 138 = CreateDirectory(file.get(), fNtupleDirec << 139 if (ndirectory == nullptr) { << 140 // Warning is issued in CreateDirectory << 141 return std::make_shared<G4RootFile>(nullpt << 142 } 115 } 143 116 144 return std::make_shared<G4RootFile>(file, hd << 117 return finalResult; 145 } << 118 } 146 119 147 //____________________________________________ 120 //_____________________________________________________________________________ 148 G4bool G4RootFileManager::WriteFileImpl(std::s << 121 G4bool G4RootFileManager::WriteFile(std::shared_ptr<tools::wroot::file> rfile, >> 122 #ifdef G4VERBOSE >> 123 const G4String& fileName) >> 124 #else >> 125 const G4String& /*fileName*/) >> 126 #endif 149 { 127 { 150 // New prototype: called by G4TFileManager bas << 128 #ifdef G4VERBOSE >> 129 if ( fState.GetVerboseL4() ) >> 130 fState.GetVerboseL4()->Message("write", "file", fileName); >> 131 #endif 151 132 152 // nothing to be done, but file should exist << 133 unsigned int n; 153 return (file == nullptr) ? false : true; << 134 auto result = rfile->write(n); 154 135 155 } << 136 #ifdef G4VERBOSE >> 137 if ( fState.GetVerboseL1() ) >> 138 fState.GetVerboseL1()->Message("write", "file", fileName, result); >> 139 #endif 156 140 >> 141 return result; >> 142 } >> 143 157 //____________________________________________ 144 //_____________________________________________________________________________ 158 G4bool G4RootFileManager::CloseFileImpl(std::s << 145 G4bool G4RootFileManager::CloseFile(std::shared_ptr<tools::wroot::file> rfile, >> 146 #ifdef G4VERBOSE >> 147 const G4String& fileName) >> 148 #else >> 149 const G4String& /*fileName*/) >> 150 #endif 159 { 151 { 160 // New prototype: called by G4TFileManager bas << 152 #ifdef G4VERBOSE 161 << 153 if ( fState.GetVerboseL4() ) 162 if (file == nullptr) return false; << 154 fState.GetVerboseL4()->Message("close", "file", fileName); 163 << 155 #endif 164 // write file (only once) << 156 165 unsigned int n; << 157 rfile->close(); 166 std::get<0>(*file)->write(n); << 158 167 << 159 #ifdef G4VERBOSE 168 // close file << 160 if ( fState.GetVerboseL1() ) 169 std::get<0>(*file)->close(); << 161 fState.GetVerboseL1()->Message("close", "file", fileName, true); >> 162 #endif 170 163 171 return true; 164 return true; 172 } 165 } 173 << 166 174 // 167 // 175 // public methods 168 // public methods 176 // 169 // 177 //____________________________________________ 170 //_____________________________________________________________________________ 178 G4bool G4RootFileManager::OpenFile(const G4Str 171 G4bool G4RootFileManager::OpenFile(const G4String& fileName) 179 { 172 { 180 // Open default file << 181 << 182 // Keep file name 173 // Keep file name 183 fFileName = fileName; 174 fFileName = fileName; 184 auto name = GetFullFileName(); 175 auto name = GetFullFileName(); 185 << 176 186 if ( fFile ) { << 177 // delete previous file if exists 187 Warn("File " + fileName + " already exists << 178 //if ( fFile ) delete fFile; 188 fFile.reset(); << 179 189 } << 180 // create new file 190 << 181 fFile = std::make_shared<tools::wroot::file>(G4cout, name); 191 // Create file (and save in in the file map) << 182 fFile->add_ziper('Z',tools::compress_buffer); 192 fFile = CreateTFile(name); << 183 fFile->set_compression(fState.GetCompressionLevel()); 193 if ( ! fFile ) { << 184 194 Warn("Failed to create file " + fileName, << 185 if ( ! fFile->is_open() ) { >> 186 G4ExceptionDescription description; >> 187 description << " " << "Cannot open file " << fileName; >> 188 G4Exception("G4RootAnalysisManager::OpenFile()", >> 189 "Analysis_W001", JustWarning, description); 195 return false; 190 return false; 196 } 191 } 197 192 198 LockDirectoryNames(); << 193 // Create directories >> 194 if ( ! CreateHistoDirectory() ) return false; >> 195 if ( ! CreateNtupleDirectory() ) return false; >> 196 >> 197 // Open ntuple files >> 198 OpenNtupleFiles(); >> 199 >> 200 fLockFileName = true; >> 201 fLockHistoDirectoryName = true; >> 202 fLockNtupleDirectoryName = true; >> 203 199 fIsOpenFile = true; 204 fIsOpenFile = true; 200 205 201 return true; 206 return true; 202 } << 207 } 203 208 204 //____________________________________________ 209 //_____________________________________________________________________________ 205 std::shared_ptr<G4RootFile> G4RootFileManager: << 210 G4bool G4RootFileManager:: WriteFile() 206 RootNtupleDescription* ntupleDescription, G4 << 207 { 211 { 208 // get ntuple file name per object << 212 auto finalResult = true; 209 auto perThread = true; << 210 auto ntupleFileName = GetNtupleFileName(ntup << 211 213 212 auto file = GetTFile(ntupleFileName, false); << 214 auto result = WriteFile(fFile, GetFullFileName()); 213 if (! file) { << 215 finalResult = finalResult && result; 214 file = CreateTFile(ntupleFileName); << 215 } << 216 216 217 // register file in ntuple description only << 217 auto counter = 0; 218 // (main ntuple files are managed in main nt << 218 for ( auto ntupleFile : fNtupleFiles ) { 219 if ( mainNumber == -1 ) { << 219 result = WriteFile(ntupleFile, GetNtupleFileName(counter++)); 220 ntupleDescription->SetFile(file); << 220 finalResult = finalResult && result; 221 } 221 } 222 << 222 return finalResult; 223 return file; << 224 } 223 } 225 224 226 //____________________________________________ 225 //_____________________________________________________________________________ 227 std::shared_ptr<G4RootFile> G4RootFileManager: << 226 G4bool G4RootFileManager::CloseFile() 228 RootNtupleDescription* ntupleDescription, G << 229 { 227 { 230 // get ntuple file name per object << 228 auto finalResult = true; 231 auto ntupleFileName = GetNtupleFileName(ntup << 229 >> 230 auto result = CloseFile(fFile, GetFullFileName()); >> 231 finalResult = finalResult && result; >> 232 >> 233 auto counter = 0; >> 234 for ( auto ntupleFile : fNtupleFiles ) { >> 235 result = CloseFile(ntupleFile, GetNtupleFileName(counter++)); >> 236 finalResult = finalResult && result; >> 237 } >> 238 >> 239 fLockFileName = false; >> 240 fIsOpenFile = false; 232 241 233 return GetTFile(ntupleFileName, false); << 242 return finalResult; >> 243 } >> 244 >> 245 //_____________________________________________________________________________ >> 246 G4bool G4RootFileManager::CreateHistoDirectory() >> 247 { >> 248 if ( fHistoDirectoryName == "" ) { >> 249 // Do not create a new directory if its name is not set >> 250 fHistoDirectory = &(fFile->dir()); >> 251 return true; >> 252 } >> 253 >> 254 #ifdef G4VERBOSE >> 255 if ( fState.GetVerboseL4() ) >> 256 fState.GetVerboseL4() >> 257 ->Message("create", "directory for histograms", fHistoDirectoryName); >> 258 #endif >> 259 >> 260 fHistoDirectory = fFile->dir().mkdir(fHistoDirectoryName); >> 261 if ( ! fHistoDirectory ) { >> 262 G4ExceptionDescription description; >> 263 description << " " >> 264 << "cannot create directory " << fHistoDirectoryName; >> 265 G4Exception("G4RootFileManager::CreateHistoDirectory()", >> 266 "Analysis_W001", JustWarning, description); >> 267 return false; >> 268 } >> 269 #ifdef G4VERBOSE >> 270 else { >> 271 if ( fState.GetVerboseL2() ) >> 272 fState.GetVerboseL2() >> 273 ->Message("create", "directory for histograms", fHistoDirectoryName); >> 274 } >> 275 #endif >> 276 return true; 234 } 277 } 235 278 236 //____________________________________________ 279 //_____________________________________________________________________________ 237 G4bool G4RootFileManager::CloseNtupleFile( << 280 G4bool G4RootFileManager::CreateNtupleDirectory() 238 RootNtupleDescription* ntupleDescription, G << 239 { 281 { 240 // auto result = true; << 282 if ( fNtupleDirectoryName == "" ) { >> 283 // Do not create a new directory if its name is not set >> 284 fNtupleDirectory = &(fFile->dir()); >> 285 return true; >> 286 } >> 287 >> 288 #ifdef G4VERBOSE >> 289 if ( fState.GetVerboseL4() ) >> 290 fState.GetVerboseL4() >> 291 ->Message("create", "directory for ntuples", fNtupleDirectoryName); >> 292 #endif >> 293 >> 294 fNtupleDirectory = fFile->dir().mkdir(fNtupleDirectoryName); >> 295 if ( ! fNtupleDirectory ) { >> 296 G4ExceptionDescription description; >> 297 description << " " >> 298 << "cannot create directory " << fNtupleDirectoryName; >> 299 G4Exception("G4RootFileManager::CreateNtupleDirectory()", >> 300 "Analysis_W001", JustWarning, description); >> 301 return false; >> 302 } >> 303 #ifdef G4VERBOSE >> 304 else { >> 305 if ( fState.GetVerboseL2() ) >> 306 fState.GetVerboseL2() >> 307 ->Message("create", "directory for ntuples", fNtupleDirectoryName); >> 308 } >> 309 #endif >> 310 return true; >> 311 } 241 312 242 // Notify not empty file << 313 //_____________________________________________________________________________ 243 auto ntupleFileName = GetNtupleFileName(ntup << 314 std::shared_ptr<tools::wroot::file> 244 auto result = SetIsEmpty(ntupleFileName, ! n << 315 G4RootFileManager::GetNtupleFile(G4int index) const >> 316 { >> 317 if ( index==0 && ( ! fNtupleFiles.size() ) ) return fFile; >> 318 >> 319 if ( index < 0 || index >= G4int(fNtupleFiles.size()) ) { >> 320 G4String inFunction = "G4RootFileManager::GetNtupleFile()"; >> 321 G4ExceptionDescription description; >> 322 description << " " << "ntuple file " << index << " does not exist."; >> 323 G4Exception(inFunction, "Analysis_W011", JustWarning, description); >> 324 return nullptr; >> 325 } 245 326 246 // Ntuple files will be closed with CloseFil << 327 return fNtupleFiles[index]; 247 ntupleDescription->GetFile().reset(); << 328 } 248 329 249 return result; << 330 //_____________________________________________________________________________ >> 331 tools::wroot::directory* >> 332 G4RootFileManager::GetMainNtupleDirectory(G4int index) const >> 333 { >> 334 if ( index==0 && ( ! fMainNtupleDirectories.size() ) ) return fNtupleDirectory; >> 335 >> 336 if ( index < 0 || index >= G4int(fMainNtupleDirectories.size()) ) { >> 337 G4String inFunction = "G4RootFileManager::GetMainNtupleDirectory()"; >> 338 G4ExceptionDescription description; >> 339 description << " " << "main ntuple directory " << index << " does not exist."; >> 340 G4Exception(inFunction, "Analysis_W011", JustWarning, description); >> 341 return nullptr; >> 342 } >> 343 >> 344 return fMainNtupleDirectories[index]; 250 } 345 } >> 346 251 347