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, 09/04/2014 (ivana 27 // Author: Ivana Hrivnacova, 09/04/2014 (ivana@ipno.in2p3.fr) 28 28 29 #include "G4RootRNtupleManager.hh" 29 #include "G4RootRNtupleManager.hh" 30 #include "G4RootRFileManager.hh" 30 #include "G4RootRFileManager.hh" 31 #include "G4AnalysisManagerState.hh" 31 #include "G4AnalysisManagerState.hh" 32 #include "G4AnalysisUtilities.hh" 32 #include "G4AnalysisUtilities.hh" 33 33 34 #include "tools/rroot/file" 34 #include "tools/rroot/file" 35 #include "tools/rroot/rall" 35 #include "tools/rroot/rall" 36 #include "tools/rroot/streamers" 36 #include "tools/rroot/streamers" 37 #include "tools/rroot/fac" 37 #include "tools/rroot/fac" 38 #include "tools/rroot/tree" 38 #include "tools/rroot/tree" 39 39 40 using namespace G4Analysis; 40 using namespace G4Analysis; 41 41 42 //____________________________________________ 42 //_____________________________________________________________________________ 43 G4RootRNtupleManager::G4RootRNtupleManager(con 43 G4RootRNtupleManager::G4RootRNtupleManager(const G4AnalysisManagerState& state) 44 : G4TRNtupleManager<tools::rroot::ntuple>(sta 44 : G4TRNtupleManager<tools::rroot::ntuple>(state) 45 {} 45 {} 46 46 47 // 47 // 48 // private methods 48 // private methods 49 // 49 // 50 50 51 //____________________________________________ 51 //_____________________________________________________________________________ 52 G4int G4RootRNtupleManager::ReadNtupleImpl(con 52 G4int G4RootRNtupleManager::ReadNtupleImpl(const G4String& ntupleName, 53 con 53 const G4String& fileName, 54 con 54 const G4String& dirName, 55 G4b 55 G4bool isUserFileName) 56 { 56 { 57 Message(kVL4, "read", "ntuple", ntupleName); 57 Message(kVL4, "read", "ntuple", ntupleName); 58 58 59 // Ntuples are saved per thread 59 // Ntuples are saved per thread 60 // but do not apply the thread suffix if fil 60 // but do not apply the thread suffix if fileName is provided explicitly 61 auto isPerThread = true; 61 auto isPerThread = true; 62 if ( isUserFileName ) isPerThread = false; 62 if ( isUserFileName ) isPerThread = false; 63 63 64 // Get or open a file 64 // Get or open a file 65 auto rfileTuple = fFileManager->GetRFile(fil << 65 auto rfile = fFileManager->GetRFile(fileName, isPerThread); 66 if (rfileTuple == nullptr) { << 66 if ( ! rfile ) { 67 if ( ! fFileManager->OpenRFile(fileName, i 67 if ( ! fFileManager->OpenRFile(fileName, isPerThread) ) return kInvalidId; 68 rfileTuple = fFileManager->GetRFile(fileNa << 68 rfile = fFileManager->GetRFile(fileName, isPerThread); 69 } << 70 auto rfile = std::get<0>(*rfileTuple); << 71 << 72 // Get or open a directory if dirName is def << 73 tools::rroot::TDirectory* ntupleDirectory = << 74 if ( ntupleDirectory == nullptr ) { << 75 // Retrieve directory only once as analysi << 76 // 1 ntuple directory par file) << 77 if ( ! dirName.empty() ) { << 78 ntupleDirectory = tools::rroot::find_dir << 79 if ( ntupleDirectory != nullptr ) { << 80 std::get<2>(*rfileTuple) = ntupleDirec << 81 } << 82 else { << 83 G4Analysis::Warn( << 84 "Directory " + dirName + " not found << 85 fkClass, "ReadNtupleImpl"); << 86 return kInvalidId; << 87 } << 88 } << 89 } 69 } 90 70 91 // Get key 71 // Get key 92 tools::rroot::key* key = nullptr; 72 tools::rroot::key* key = nullptr; 93 if ( ntupleDirectory != nullptr ) { << 73 tools::rroot::TDirectory* newDir = nullptr; 94 key = ntupleDirectory->find_key(ntupleName << 74 if ( ! dirName.empty() ) { >> 75 newDir = tools::rroot::find_dir(rfile->dir(), dirName); >> 76 if ( newDir ) { >> 77 key = newDir->find_key(ntupleName); >> 78 } >> 79 else { >> 80 G4Analysis::Warn( >> 81 "Directory " + dirName + " not found in file " + fileName + ".", >> 82 fkClass, "ReadNtupleImpl"); >> 83 return kInvalidId; >> 84 } 95 } 85 } 96 else { 86 else { 97 key = rfile->dir().find_key(ntupleName); 87 key = rfile->dir().find_key(ntupleName); 98 } 88 } 99 if (key == nullptr) { << 89 >> 90 if ( ! key ) { 100 Warn("Key " + ntupleName + " for Ntuple no 91 Warn("Key " + ntupleName + " for Ntuple not found in file " + fileName + 101 ", directory " + dirName, fkClass, "Read 92 ", directory " + dirName, fkClass, "ReadNtupleImpl"); >> 93 delete newDir; 102 return kInvalidId; 94 return kInvalidId; 103 } 95 } 104 96 105 unsigned int size; 97 unsigned int size; 106 char* charBuffer = key->get_object_buffer(*r 98 char* charBuffer = key->get_object_buffer(*rfile, size); 107 if (charBuffer == nullptr) { << 99 if ( ! charBuffer ) { 108 Warn("Cannot get data buffer for Ntuple " 100 Warn("Cannot get data buffer for Ntuple " + ntupleName + 109 " in file " + fileName, fkClass, "Rea 101 " in file " + fileName, fkClass, "ReadNtupleImpl"); >> 102 delete newDir; 110 return kInvalidId; 103 return kInvalidId; 111 } 104 } 112 105 113 auto verbose = false; 106 auto verbose = false; 114 auto buffer 107 auto buffer 115 = new tools::rroot::buffer(G4cout, rfile-> 108 = new tools::rroot::buffer(G4cout, rfile->byte_swap(), size, charBuffer, 116 key->key_length 109 key->key_length(), verbose); 117 buffer->set_map_objs(true); 110 buffer->set_map_objs(true); 118 111 119 auto fac = new tools::rroot::fac(G4cout); 112 auto fac = new tools::rroot::fac(G4cout); 120 113 121 auto tree = new tools::rroot::tree(*rfile, * 114 auto tree = new tools::rroot::tree(*rfile, *fac); 122 if ( ! tree->stream(*buffer) ) { 115 if ( ! tree->stream(*buffer) ) { 123 Warn("TTree streaming failed for Ntuple " 116 Warn("TTree streaming failed for Ntuple " + ntupleName + 124 " in file " + fileName, 117 " in file " + fileName, 125 fkClass, "ReadNtupleImpl"); 118 fkClass, "ReadNtupleImpl"); 126 119 127 delete buffer; 120 delete buffer; 128 delete tree; 121 delete tree; >> 122 delete newDir; 129 return kInvalidId; 123 return kInvalidId; 130 } 124 } 131 125 132 auto rntuple = new tools::rroot::ntuple(*tr 126 auto rntuple = new tools::rroot::ntuple(*tree); //use the flat ntuple API. 133 auto rntupleDescription = new G4TRNtupleDesc 127 auto rntupleDescription = new G4TRNtupleDescription<tools::rroot::ntuple>(rntuple); 134 128 135 auto id = SetNtuple(rntupleDescription); 129 auto id = SetNtuple(rntupleDescription); 136 << 137 130 138 Message(kVL2, "read", "ntuple", ntupleName, 131 Message(kVL2, "read", "ntuple", ntupleName, id > kInvalidId); 139 132 140 return id; 133 return id; 141 } 134 } 142 135 143 //____________________________________________ 136 //_____________________________________________________________________________ 144 G4bool G4RootRNtupleManager::GetTNtupleRow( 137 G4bool G4RootRNtupleManager::GetTNtupleRow( 145 G4TRNtupleDescription<tools::rroot::ntuple>* 138 G4TRNtupleDescription<tools::rroot::ntuple>* ntupleDescription) 146 { 139 { 147 auto ntuple = ntupleDescription->fNtuple; 140 auto ntuple = ntupleDescription->fNtuple; 148 auto ntupleBinding = ntupleDescription->fNtu 141 auto ntupleBinding = ntupleDescription->fNtupleBinding; 149 142 150 G4bool isInitialized = ntupleDescription->fI 143 G4bool isInitialized = ntupleDescription->fIsInitialized; 151 if ( ! isInitialized ) { 144 if ( ! isInitialized ) { 152 145 153 if ( ! ntuple->initialize(G4cout, *ntupleB 146 if ( ! ntuple->initialize(G4cout, *ntupleBinding) ) { 154 Warn("Ntuple initialization failed !!", 147 Warn("Ntuple initialization failed !!", fkClass, "GetTNtupleRow"); 155 return false; 148 return false; 156 } 149 } 157 ntupleDescription->fIsInitialized = true; 150 ntupleDescription->fIsInitialized = true; 158 ntuple->start(); 151 ntuple->start(); 159 } 152 } 160 153 161 auto next = ntuple->next(); 154 auto next = ntuple->next(); 162 if ( next ) { 155 if ( next ) { 163 if ( ! ntuple->get_row() ) { 156 if ( ! ntuple->get_row() ) { 164 Warn("Ntuple get_row() failed !!", fkCla 157 Warn("Ntuple get_row() failed !!", fkClass, "GetTNtupleRow"); 165 return false; 158 return false; 166 } 159 } 167 } 160 } 168 161 169 return next; 162 return next; 170 } 163 } 171 164