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$ 26 27 27 // Author: Ivana Hrivnacova, 20/07/2017 (ivana 28 // Author: Ivana Hrivnacova, 20/07/2017 (ivana@ipno.in2p3.fr) 28 29 29 #include "G4Hdf5RFileManager.hh" 30 #include "G4Hdf5RFileManager.hh" 30 #include "G4Hdf5HnRFileManager.hh" << 31 #include "G4AnalysisManagerState.hh" 31 #include "G4AnalysisManagerState.hh" 32 #include "G4AnalysisUtilities.hh" 32 #include "G4AnalysisUtilities.hh" 33 33 34 #include "toolx/hdf5/h2file" << 34 #include "tools/hdf5/h2file" 35 #include "toolx/hdf5/group_exists" << 35 #include <tools/zlib> 36 #include "toolx/zlib" << 36 >> 37 #include <iostream> >> 38 #include <cstdio> 37 39 38 using namespace G4Analysis; 40 using namespace G4Analysis; 39 using namespace tools; << 41 >> 42 //_____________________________________________________________________________ >> 43 const G4String G4Hdf5RFileManager::fgkDefaultDirectoryName = "default"; 40 44 41 //____________________________________________ 45 //_____________________________________________________________________________ 42 G4Hdf5RFileManager::G4Hdf5RFileManager(const G 46 G4Hdf5RFileManager::G4Hdf5RFileManager(const G4AnalysisManagerState& state) 43 : G4VRFileManager(state) << 47 : G4BaseFileManager(state), >> 48 fRFiles() 44 { 49 { 45 // Create helpers defined in the base class << 46 fH1RFileManager = std::make_shared<G4Hdf5HnR << 47 fH2RFileManager = std::make_shared<G4Hdf5HnR << 48 fH3RFileManager = std::make_shared<G4Hdf5HnR << 49 fP1RFileManager = std::make_shared<G4Hdf5HnR << 50 fP2RFileManager = std::make_shared<G4Hdf5HnR << 51 } 50 } 52 51 53 // << 52 //_____________________________________________________________________________ >> 53 G4Hdf5RFileManager::~G4Hdf5RFileManager() >> 54 { >> 55 } >> 56 >> 57 // 54 // private methods 58 // private methods 55 // 59 // 56 60 57 //____________________________________________ 61 //_____________________________________________________________________________ 58 hid_t G4Hdf5RFileManager::OpenRFile(const G4St 62 hid_t G4Hdf5RFileManager::OpenRFile(const G4String& fileName, 59 G4bool is 63 G4bool isPerThread) 60 { 64 { 61 // Get full file name 65 // Get full file name 62 G4String name = GetFullFileName(fileName, is 66 G4String name = GetFullFileName(fileName, isPerThread); 63 67 64 Message(kVL4, "open", "read analysis file", << 68 #ifdef G4VERBOSE >> 69 if ( fState.GetVerboseL4() ) >> 70 fState.GetVerboseL4()->Message("open", "read analysis file", name); >> 71 #endif 65 72 66 // create new file 73 // create new file 67 hid_t newFile = H5Fopen(name, H5F_ACC_RDONLY 74 hid_t newFile = H5Fopen(name, H5F_ACC_RDONLY, H5P_DEFAULT); 68 if ( newFile < 0 ) { 75 if ( newFile < 0 ) { 69 Warn("Cannot open file " + name, fkClass, << 76 G4ExceptionDescription description; 70 return kInvalidId; << 77 description << " " << "Cannot open file " << name; >> 78 G4Exception("G4Hdf5RFileManager::OpenFile()", >> 79 "Analysis_WR001", JustWarning, description); >> 80 return false; 71 } 81 } 72 82 73 // newFile->add_unziper('Z',toolx::decompres << 83 // newFile->add_unziper('Z',tools::decompress_buffer); 74 << 84 75 // add file in a map 85 // add file in a map 76 fRFiles[name] = G4Hdf5File(newFile, kInvalid << 86 std::map<G4String, hid_t>::iterator it 77 << 87 = fRFiles.find(name); 78 Message(kVL1, "open", "read analysis file", << 88 if ( it != fRFiles.end() ) { 79 << 89 it->second = newFile; 80 return newFile; << 90 } 81 } << 91 else { 82 << 92 fRFiles[name] = newFile; >> 93 } >> 94 >> 95 #ifdef G4VERBOSE >> 96 if ( fState.GetVerboseL1() ) >> 97 fState.GetVerboseL1() >> 98 ->Message("open", "read analysis file", name); >> 99 #endif >> 100 >> 101 return true; >> 102 } >> 103 83 //____________________________________________ 104 //_____________________________________________________________________________ 84 hid_t G4Hdf5RFileManager::OpenDirectory(hid_t 105 hid_t G4Hdf5RFileManager::OpenDirectory(hid_t file, const G4String& directoryName) 85 { 106 { 86 Message(kVL4, "open", "read directory", dire << 107 #ifdef G4VERBOSE 87 << 108 if ( fState.GetVerboseL4() ) { 88 auto directory = toolx_H5Gopen(file, directo << 109 fState.GetVerboseL4()->Message("open", "read directory", directoryName); >> 110 } >> 111 #endif >> 112 >> 113 auto directory = tools_H5Gopen(file, directoryName); 89 if ( directory < 0 ) { 114 if ( directory < 0 ) { 90 Warn("Cannot open directory " + directoryN << 115 G4ExceptionDescription description; 91 return kInvalidId; << 116 description << " " 92 } << 117 << "cannot open directory " << directoryName; 93 Message(kVL2, "open", "read directory", dire << 118 G4Exception("G4Hdf5RFileManager::OpenDirectory()", 94 return directory; << 119 "Analysis_W001", JustWarning, description); >> 120 return kInvalidId; >> 121 } >> 122 else { >> 123 #ifdef G4VERBOSE >> 124 if ( fState.GetVerboseL2() ) { >> 125 fState.GetVerboseL2()->Message("open", "read directory", directoryName); >> 126 } >> 127 #endif >> 128 return directory; >> 129 } 95 } 130 } 96 131 97 //____________________________________________ 132 //_____________________________________________________________________________ 98 hid_t G4Hdf5RFileManager::GetRDirectory(const 133 hid_t G4Hdf5RFileManager::GetRDirectory(const G4String& directoryType, 99 const << 134 const G4String& fileName, 100 const << 135 const G4String& dirName, 101 G4boo << 136 G4bool isPerThread) 102 { 137 { 103 // Get or open a file 138 // Get or open a file 104 auto rfile = GetRFile(fileName, isPerThread) 139 auto rfile = GetRFile(fileName, isPerThread); 105 if (rfile == nullptr) { << 140 if ( rfile < 0 ) { 106 // Try to open it if not found in the map << 141 // Try to open it if not found in the map 107 if ( OpenRFile(fileName, isPerThread) < 0 << 142 if ( ! OpenRFile(fileName, isPerThread) ) return kInvalidId; 108 rfile = GetRFile(fileName, isPerThread); 143 rfile = GetRFile(fileName, isPerThread); 109 } 144 } 110 145 111 auto isHistograms = (directoryType == "histo << 112 << 113 // Get directory if already open << 114 hid_t directory = kInvalidId; << 115 if ( isHistograms ) { << 116 directory = std::get<1>(*rfile); << 117 } else { << 118 directory = std::get<2>(*rfile); << 119 } << 120 if ( directory != kInvalidId ) { << 121 return directory; << 122 } << 123 << 124 // Use default directory name if not specifi 146 // Use default directory name if not specified 125 auto newDirName = dirName; 147 auto newDirName = dirName; 126 if ( newDirName == "" ) { 148 if ( newDirName == "" ) { >> 149 // if ( fDefaultDirectory > 0 ) { >> 150 // // Return the default directory if the name is not set and the default directory >> 151 // // already exists >> 152 // directory = fDefaultDirectory; >> 153 // return true; >> 154 // } else { 127 // Create the default directory if the n 155 // Create the default directory if the name is not set and the default directory 128 // does not yet exist 156 // does not yet exist 129 newDirName = fgkDefaultDirectoryName; 157 newDirName = fgkDefaultDirectoryName; 130 newDirName += "_"; 158 newDirName += "_"; 131 newDirName += directoryType; 159 newDirName += directoryType; 132 } 160 } 133 161 134 // Open directory 162 // Open directory 135 directory = OpenDirectory(std::get<0>(*rfile << 163 return OpenDirectory(rfile, newDirName); 136 << 137 // Update << 138 if ( isHistograms ) { << 139 std::get<1>(*rfile) = directory; << 140 } else { << 141 std::get<2>(*rfile) = directory; << 142 } << 143 << 144 return directory; << 145 } 164 } 146 165 >> 166 147 // 167 // 148 // public methods 168 // public methods 149 // 169 // 150 170 151 //____________________________________________ 171 //_____________________________________________________________________________ 152 G4Hdf5File* G4Hdf5RFileManager::GetRFile(const << 172 hid_t G4Hdf5RFileManager::GetRFile(const G4String& fileName, 153 G4boo << 173 G4bool isPerThread) const 154 { << 174 { 155 // Get full file name 175 // Get full file name 156 G4String name = GetFullFileName(fileName, is 176 G4String name = GetFullFileName(fileName, isPerThread); 157 177 158 auto it = fRFiles.find(name); << 178 std::map<G4String, hid_t>::const_iterator it 159 if (it != fRFiles.end()) { << 179 = fRFiles.find(name); 160 return &(it->second); << 180 if ( it != fRFiles.end() ) 161 } << 181 return it->second; 162 return nullptr; << 182 else { >> 183 return kInvalidId; >> 184 } 163 } 185 } 164 186 165 //____________________________________________ 187 //_____________________________________________________________________________ 166 hid_t G4Hdf5RFileManager::GetHistoRDirectory(c 188 hid_t G4Hdf5RFileManager::GetHistoRDirectory(const G4String& fileName, const G4String& dirName, 167 G 189 G4bool isPerThread) 168 { 190 { 169 return GetRDirectory("histograms", fileName, 191 return GetRDirectory("histograms", fileName, dirName, isPerThread); 170 } 192 } 171 193 172 //____________________________________________ 194 //_____________________________________________________________________________ 173 hid_t G4Hdf5RFileManager::GetNtupleRDirectory( 195 hid_t G4Hdf5RFileManager::GetNtupleRDirectory(const G4String& fileName, const G4String& dirName, 174 196 G4bool isPerThread) 175 { 197 { 176 return GetRDirectory("ntuples", fileName, di 198 return GetRDirectory("ntuples", fileName, dirName, isPerThread); 177 } 199 } 178 200 179 //____________________________________________ << 201 180 void G4Hdf5RFileManager::CloseFiles() << 181 { << 182 // Close all open directories and file << 183 for ( auto [key, rfile] : fRFiles ) { << 184 if (std::get<1>(rfile) != kInvalidId) { << 185 ::H5Gclose(std::get<1>(rfile)); << 186 } << 187 if (std::get<2>(rfile) != kInvalidId) { << 188 ::H5Gclose(std::get<2>(rfile)); << 189 } << 190 if (std::get<0>(rfile) != kInvalidId) { << 191 ::H5Fclose(std::get<0>(rfile)); << 192 } << 193 } << 194 } << 195 202