Geant4 Cross Reference |
1 // 2 // ******************************************************************** 3 // * License and Disclaimer * 4 // * * 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. * 10 // * * 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitation of liability. * 17 // * * 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************************************** 25 // 26 // 27 // Author: Ivana Hrivnacova, 26/08/2021 (ivana@ipno.in2p3.fr) 28 29 #include "G4AnalysisUtilities.hh" 30 #include "G4RootRFileDef.hh" 31 32 #include "tools/rroot/file" 33 #include "tools/rroot/rall" 34 #include "tools/rroot/streamers" 35 36 //_____________________________________________________________________________ 37 template <typename HT> 38 inline 39 tools::rroot::buffer* G4RootHnRFileManager<HT>::GetBuffer( 40 const G4String& fileName, const G4String& dirName, const G4String& objectName) 41 { 42 // Get buffer for reading histogram or profile specified by objectNmae 43 // for a file specified by fileName; 44 // Open the file if it was not yet open 45 46 // Histograms and profiles are not saved per thread 47 G4bool isPerThread = false; 48 49 // Get or open a file 50 auto rfileTuple = fRFileManager->GetRFile(fileName, isPerThread); 51 if (rfileTuple == nullptr) { 52 if ( ! fRFileManager->OpenRFile(fileName, isPerThread) ) { 53 return nullptr; 54 } 55 rfileTuple = fRFileManager->GetRFile(fileName, isPerThread); 56 } 57 auto rfile = std::get<0>(*rfileTuple); 58 59 // Get or open a directory if dirName is defined 60 tools::rroot::TDirectory* histoDirectory = nullptr; 61 if ( histoDirectory == nullptr ) { 62 // Retrieve directory only once as analysis manager supports only 63 // 1 histo directory par file) 64 if ( ! dirName.empty() ) { 65 histoDirectory = tools::rroot::find_dir(rfile->dir(), dirName); 66 if ( histoDirectory != nullptr ) { 67 std::get<1>(*rfileTuple) = histoDirectory; 68 } 69 else { 70 G4Analysis::Warn( 71 "Directory " + dirName + " not found in file " + fileName + ".", 72 fkClass, "ReadNtupleImpl"); 73 return nullptr; 74 } 75 } 76 } 77 78 // Get key 79 tools::rroot::key* key = nullptr; 80 if ( histoDirectory != nullptr ) { 81 key = histoDirectory->find_key(objectName); 82 } 83 else { 84 key = rfile->dir().find_key(objectName); 85 } 86 if (key == nullptr) { 87 G4Analysis::Warn( 88 "Key " + objectName + " for Histogram/Profile not found in file " + 89 fileName + ", directory " + dirName, fkClass, "GetBuffer"); 90 return nullptr; 91 } 92 93 unsigned int size; 94 char* charBuffer = key->get_object_buffer(*rfile, size); 95 96 if (charBuffer == nullptr) { 97 G4Analysis::Warn( 98 "Cannot get " + objectName + " in file " + fileName, 99 fkClass, "GetBuffer"); 100 return nullptr; 101 } 102 103 auto verbose = false; 104 return new tools::rroot::buffer(G4cout, rfile->byte_swap(), size, charBuffer, 105 key->key_length(), verbose); 106 } 107 108 //_____________________________________________________________________________ 109 template <> 110 inline 111 tools::histo::h1d* G4RootHnRFileManager<tools::histo::h1d>::ReadT( 112 tools::rroot::buffer* buffer) 113 { 114 return tools::rroot::TH1D_stream(*buffer); 115 } 116 117 //_____________________________________________________________________________ 118 template <> 119 inline 120 tools::histo::h2d* G4RootHnRFileManager<tools::histo::h2d>::ReadT( 121 tools::rroot::buffer* buffer) 122 { 123 return tools::rroot::TH2D_stream(*buffer); 124 } 125 126 //_____________________________________________________________________________ 127 template <> 128 inline 129 tools::histo::h3d* G4RootHnRFileManager<tools::histo::h3d>::ReadT( 130 tools::rroot::buffer* buffer) 131 { 132 return tools::rroot::TH3D_stream(*buffer); 133 } 134 135 //_____________________________________________________________________________ 136 template <> 137 inline 138 tools::histo::p1d* G4RootHnRFileManager<tools::histo::p1d>::ReadT( 139 tools::rroot::buffer* buffer) 140 { 141 return tools::rroot::TProfile_stream(*buffer); 142 } 143 144 //_____________________________________________________________________________ 145 template <> 146 inline 147 tools::histo::p2d* G4RootHnRFileManager<tools::histo::p2d>::ReadT( 148 tools::rroot::buffer* buffer) 149 { 150 return tools::rroot::TProfile2D_stream(*buffer); 151 } 152 153 //_____________________________________________________________________________ 154 template <typename HT> 155 inline 156 HT* G4RootHnRFileManager<HT>::Read( 157 const G4String& htName, const G4String& fileName, const G4String& dirName, 158 G4bool /*isUserFileName*/) 159 { 160 auto buffer = GetBuffer(fileName, dirName, htName); 161 if ( ! buffer ) { 162 return nullptr; 163 } 164 165 auto ht = ReadT(buffer); 166 delete buffer; 167 168 if ( ! ht ) { 169 G4Analysis::Warn( 170 "Streaming " + htName + " in file " + fileName + " failed.", 171 fkClass, "Read"); 172 return nullptr; 173 } 174 175 return ht; 176 } 177