Geant4 Cross Reference |
1 // 1 2 // ******************************************* 3 // * License and Disclaimer 4 // * 5 // * The Geant4 software is copyright of th 6 // * the Geant4 Collaboration. It is provided 7 // * conditions of the Geant4 Software License 8 // * LICENSE and available at http://cern.ch/ 9 // * include a list of copyright holders. 10 // * 11 // * Neither the authors of this software syst 12 // * institutes,nor the agencies providing fin 13 // * work make any representation or warran 14 // * regarding this software system or assum 15 // * use. Please see the license in the file 16 // * for the full disclaimer and the limitatio 17 // * 18 // * This code implementation is the result 19 // * technical work of the GEANT4 collaboratio 20 // * By using, copying, modifying or distri 21 // * any work based on the software) you ag 22 // * use in resulting scientific publicati 23 // * acceptance of all terms of the Geant4 Sof 24 // ******************************************* 25 // 26 27 // Author: Ivana Hrivnacova, 15/06/2011 (ivan 28 29 #include "G4RootFileManager.hh" 30 #include "G4RootHnFileManager.hh" 31 #include "G4AnalysisManagerState.hh" 32 #include "G4AnalysisUtilities.hh" 33 34 #include "tools/wroot/file" 35 #include "tools/wroot/to" 36 #include "toolx/zlib" 37 38 using namespace tools; 39 using namespace G4Analysis; 40 41 //____________________________________________ 42 G4RootFileManager::G4RootFileManager(const G4A 43 : G4VTFileManager<G4RootFile>(state) 44 { 45 // Create helpers defined in the base class 46 fH1FileManager = std::make_shared<G4RootHnFi 47 fH2FileManager = std::make_shared<G4RootHnFi 48 fH3FileManager = std::make_shared<G4RootHnFi 49 fP1FileManager = std::make_shared<G4RootHnFi 50 fP2FileManager = std::make_shared<G4RootHnFi 51 } 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 81 //____________________________________________ 82 G4String G4RootFileManager::GetNtupleFileName( 83 RootNtupleDescri 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 110 // 111 // protected methods 112 // 113 114 //____________________________________________ 115 std::shared_ptr<G4RootFile> 116 G4RootFileManager::CreateFileImpl(const G4Stri 117 { 118 // create file 119 std::shared_ptr<wroot::file> file = std::mak 120 file->add_ziper('Z',toolx::compress_buffer); 121 file->set_compression(GetCompressionLevel()) 122 123 if ( ! file->is_open() ) { 124 Warn("Cannot create file " + fileName, fkC 125 return std::make_shared<G4RootFile>(nullpt 126 } 127 128 // create histo directory 129 tools::wroot::directory* hdirectory 130 = CreateDirectory(file.get(), fHistoDirect 131 if (hdirectory == nullptr) { 132 // Warning is issued in CreateDirectory 133 return std::make_shared<G4RootFile>(nullpt 134 } 135 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 } 143 144 return std::make_shared<G4RootFile>(file, hd 145 } 146 147 //____________________________________________ 148 G4bool G4RootFileManager::WriteFileImpl(std::s 149 { 150 // New prototype: called by G4TFileManager bas 151 152 // nothing to be done, but file should exist 153 return (file == nullptr) ? false : true; 154 155 } 156 157 //____________________________________________ 158 G4bool G4RootFileManager::CloseFileImpl(std::s 159 { 160 // New prototype: called by G4TFileManager bas 161 162 if (file == nullptr) return false; 163 164 // write file (only once) 165 unsigned int n; 166 std::get<0>(*file)->write(n); 167 168 // close file 169 std::get<0>(*file)->close(); 170 171 return true; 172 } 173 174 // 175 // public methods 176 // 177 //____________________________________________ 178 G4bool G4RootFileManager::OpenFile(const G4Str 179 { 180 // Open default file 181 182 // Keep file name 183 fFileName = fileName; 184 auto name = GetFullFileName(); 185 186 if ( fFile ) { 187 Warn("File " + fileName + " already exists 188 fFile.reset(); 189 } 190 191 // Create file (and save in in the file map) 192 fFile = CreateTFile(name); 193 if ( ! fFile ) { 194 Warn("Failed to create file " + fileName, 195 return false; 196 } 197 198 LockDirectoryNames(); 199 fIsOpenFile = true; 200 201 return true; 202 } 203 204 //____________________________________________ 205 std::shared_ptr<G4RootFile> G4RootFileManager: 206 RootNtupleDescription* ntupleDescription, G4 207 { 208 // get ntuple file name per object 209 auto perThread = true; 210 auto ntupleFileName = GetNtupleFileName(ntup 211 212 auto file = GetTFile(ntupleFileName, false); 213 if (! file) { 214 file = CreateTFile(ntupleFileName); 215 } 216 217 // register file in ntuple description only 218 // (main ntuple files are managed in main nt 219 if ( mainNumber == -1 ) { 220 ntupleDescription->SetFile(file); 221 } 222 223 return file; 224 } 225 226 //____________________________________________ 227 std::shared_ptr<G4RootFile> G4RootFileManager: 228 RootNtupleDescription* ntupleDescription, G 229 { 230 // get ntuple file name per object 231 auto ntupleFileName = GetNtupleFileName(ntup 232 233 return GetTFile(ntupleFileName, false); 234 } 235 236 //____________________________________________ 237 G4bool G4RootFileManager::CloseNtupleFile( 238 RootNtupleDescription* ntupleDescription, G 239 { 240 // auto result = true; 241 242 // Notify not empty file 243 auto ntupleFileName = GetNtupleFileName(ntup 244 auto result = SetIsEmpty(ntupleFileName, ! n 245 246 // Ntuple files will be closed with CloseFil 247 ntupleDescription->GetFile().reset(); 248 249 return result; 250 } 251