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