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, 26/08/2021 (ivana 27 // Author: Ivana Hrivnacova, 26/08/2021 (ivana@ipno.in2p3.fr) 28 28 29 #include "G4AnalysisUtilities.hh" 29 #include "G4AnalysisUtilities.hh" 30 #include "G4RootRFileDef.hh" << 31 30 32 #include "tools/rroot/file" 31 #include "tools/rroot/file" 33 #include "tools/rroot/rall" 32 #include "tools/rroot/rall" 34 #include "tools/rroot/streamers" 33 #include "tools/rroot/streamers" 35 34 36 //____________________________________________ 35 //_____________________________________________________________________________ 37 template <typename HT> 36 template <typename HT> 38 inline 37 inline 39 tools::rroot::buffer* G4RootHnRFileManager<HT> << 38 std::tuple<tools::rroot::buffer*, tools::rroot::TDirectory*> >> 39 G4RootHnRFileManager<HT>::GetBuffer( 40 const G4String& fileName, const G4String& di 40 const G4String& fileName, const G4String& dirName, const G4String& objectName) 41 { 41 { 42 // Get buffer for reading histogram or profile 42 // Get buffer for reading histogram or profile specified by objectNmae 43 // for a file specified by fileName; 43 // for a file specified by fileName; 44 // Open the file if it was not yet open 44 // Open the file if it was not yet open 45 45 46 // Histograms and profiles are not saved per 46 // Histograms and profiles are not saved per thread 47 G4bool isPerThread = false; 47 G4bool isPerThread = false; 48 48 49 // Get or open a file 49 // Get or open a file 50 auto rfileTuple = fRFileManager->GetRFile(fi << 50 auto rfile = fRFileManager->GetRFile(fileName, isPerThread); 51 if (rfileTuple == nullptr) { << 51 if ( ! rfile ) { 52 if ( ! fRFileManager->OpenRFile(fileName, 52 if ( ! fRFileManager->OpenRFile(fileName, isPerThread) ) { 53 return nullptr; << 53 return std::tuple(nullptr, nullptr); 54 } << 55 rfileTuple = fRFileManager->GetRFile(fileN << 56 } << 57 auto rfile = std::get<0>(*rfileTuple); << 58 << 59 // Get or open a directory if dirName is def << 60 tools::rroot::TDirectory* histoDirectory = n << 61 if ( histoDirectory == nullptr ) { << 62 // Retrieve directory only once as analysi << 63 // 1 histo directory par file) << 64 if ( ! dirName.empty() ) { << 65 histoDirectory = tools::rroot::find_dir( << 66 if ( histoDirectory != nullptr ) { << 67 std::get<1>(*rfileTuple) = histoDirect << 68 } << 69 else { << 70 G4Analysis::Warn( << 71 "Directory " + dirName + " not found << 72 fkClass, "ReadNtupleImpl"); << 73 return nullptr; << 74 } << 75 } 54 } >> 55 rfile = fRFileManager->GetRFile(fileName, isPerThread); 76 } 56 } 77 57 78 // Get key 58 // Get key 79 tools::rroot::key* key = nullptr; 59 tools::rroot::key* key = nullptr; 80 if ( histoDirectory != nullptr ) { << 60 tools::rroot::TDirectory* newDir = nullptr; 81 key = histoDirectory->find_key(objectName) << 61 if ( ! dirName.empty() ) { >> 62 newDir = tools::rroot::find_dir(rfile->dir(), dirName); >> 63 if ( newDir ) { >> 64 key = newDir->find_key(objectName); >> 65 } >> 66 else { >> 67 G4Analysis::Warn( >> 68 "Directory " + dirName + " not found in file " + fileName + ".", >> 69 fkClass, "GetBuffer"); >> 70 return std::tuple(nullptr, nullptr); >> 71 } 82 } 72 } 83 else { 73 else { 84 key = rfile->dir().find_key(objectName); 74 key = rfile->dir().find_key(objectName); 85 } 75 } 86 if (key == nullptr) { << 76 >> 77 if ( ! key ) { 87 G4Analysis::Warn( 78 G4Analysis::Warn( 88 "Key " + objectName + " for Histogram/Pr 79 "Key " + objectName + " for Histogram/Profile not found in file " + 89 fileName + ", directory " + dirName, fkC 80 fileName + ", directory " + dirName, fkClass, "GetBuffer"); 90 return nullptr; << 81 return std::tuple(nullptr, newDir); 91 } 82 } 92 83 93 unsigned int size; 84 unsigned int size; 94 char* charBuffer = key->get_object_buffer(*r 85 char* charBuffer = key->get_object_buffer(*rfile, size); 95 86 96 if (charBuffer == nullptr) { << 87 if ( ! charBuffer ) { 97 G4Analysis::Warn( 88 G4Analysis::Warn( 98 "Cannot get " + objectName + " in file " 89 "Cannot get " + objectName + " in file " + fileName, 99 fkClass, "GetBuffer"); 90 fkClass, "GetBuffer"); 100 return nullptr; << 91 return std::tuple(nullptr, newDir); 101 } 92 } 102 93 103 auto verbose = false; 94 auto verbose = false; 104 return new tools::rroot::buffer(G4cout, rfil << 95 return std::tuple(new tools::rroot::buffer(G4cout, rfile->byte_swap(), size, charBuffer, 105 key->key_len << 96 key->key_length(), verbose), newDir); 106 } 97 } 107 98 108 //____________________________________________ 99 //_____________________________________________________________________________ 109 template <> 100 template <> 110 inline 101 inline 111 tools::histo::h1d* G4RootHnRFileManager<tools: 102 tools::histo::h1d* G4RootHnRFileManager<tools::histo::h1d>::ReadT( 112 tools::rroot::buffer* buffer) 103 tools::rroot::buffer* buffer) 113 { 104 { 114 return tools::rroot::TH1D_stream(*buffer); 105 return tools::rroot::TH1D_stream(*buffer); 115 } 106 } 116 107 117 //____________________________________________ 108 //_____________________________________________________________________________ 118 template <> 109 template <> 119 inline 110 inline 120 tools::histo::h2d* G4RootHnRFileManager<tools: 111 tools::histo::h2d* G4RootHnRFileManager<tools::histo::h2d>::ReadT( 121 tools::rroot::buffer* buffer) 112 tools::rroot::buffer* buffer) 122 { 113 { 123 return tools::rroot::TH2D_stream(*buffer); 114 return tools::rroot::TH2D_stream(*buffer); 124 } 115 } 125 116 126 //____________________________________________ 117 //_____________________________________________________________________________ 127 template <> 118 template <> 128 inline 119 inline 129 tools::histo::h3d* G4RootHnRFileManager<tools: 120 tools::histo::h3d* G4RootHnRFileManager<tools::histo::h3d>::ReadT( 130 tools::rroot::buffer* buffer) 121 tools::rroot::buffer* buffer) 131 { 122 { 132 return tools::rroot::TH3D_stream(*buffer); 123 return tools::rroot::TH3D_stream(*buffer); 133 } 124 } 134 125 135 //____________________________________________ 126 //_____________________________________________________________________________ 136 template <> 127 template <> 137 inline 128 inline 138 tools::histo::p1d* G4RootHnRFileManager<tools: 129 tools::histo::p1d* G4RootHnRFileManager<tools::histo::p1d>::ReadT( 139 tools::rroot::buffer* buffer) 130 tools::rroot::buffer* buffer) 140 { 131 { 141 return tools::rroot::TProfile_stream(*buffer 132 return tools::rroot::TProfile_stream(*buffer); 142 } 133 } 143 134 144 //____________________________________________ 135 //_____________________________________________________________________________ 145 template <> 136 template <> 146 inline 137 inline 147 tools::histo::p2d* G4RootHnRFileManager<tools: 138 tools::histo::p2d* G4RootHnRFileManager<tools::histo::p2d>::ReadT( 148 tools::rroot::buffer* buffer) 139 tools::rroot::buffer* buffer) 149 { 140 { 150 return tools::rroot::TProfile2D_stream(*buff 141 return tools::rroot::TProfile2D_stream(*buffer); 151 } 142 } 152 143 153 //____________________________________________ 144 //_____________________________________________________________________________ 154 template <typename HT> 145 template <typename HT> 155 inline 146 inline 156 HT* G4RootHnRFileManager<HT>::Read( 147 HT* G4RootHnRFileManager<HT>::Read( 157 const G4String& htName, const G4String& file 148 const G4String& htName, const G4String& fileName, const G4String& dirName, 158 G4bool /*isUserFileName*/) 149 G4bool /*isUserFileName*/) 159 { 150 { 160 auto buffer = GetBuffer(fileName, dirName, h << 151 auto [buffer, newDir] = GetBuffer(fileName, dirName, htName); 161 if ( ! buffer ) { 152 if ( ! buffer ) { >> 153 delete newDir; 162 return nullptr; 154 return nullptr; 163 } 155 } 164 156 165 auto ht = ReadT(buffer); 157 auto ht = ReadT(buffer); 166 delete buffer; 158 delete buffer; >> 159 delete newDir; 167 160 168 if ( ! ht ) { 161 if ( ! ht ) { 169 G4Analysis::Warn( 162 G4Analysis::Warn( 170 "Streaming " + htName + " in file " + fi 163 "Streaming " + htName + " in file " + fileName + " failed.", 171 fkClass, "Read"); 164 fkClass, "Read"); 172 return nullptr; 165 return nullptr; 173 } 166 } 174 167 175 return ht; 168 return ht; 176 } 169 } 177 170