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 rfileTuple = fFileManager->GetRFile(fileName, isPerThread); 66 if (rfileTuple == nullptr) { 66 if (rfileTuple == nullptr) { 67 if ( ! fFileManager->OpenRFile(fileName, i 67 if ( ! fFileManager->OpenRFile(fileName, isPerThread) ) return kInvalidId; 68 rfileTuple = fFileManager->GetRFile(fileNa 68 rfileTuple = fFileManager->GetRFile(fileName, isPerThread); 69 } 69 } 70 auto rfile = std::get<0>(*rfileTuple); 70 auto rfile = std::get<0>(*rfileTuple); 71 71 72 // Get or open a directory if dirName is def 72 // Get or open a directory if dirName is defined 73 tools::rroot::TDirectory* ntupleDirectory = 73 tools::rroot::TDirectory* ntupleDirectory = nullptr; 74 if ( ntupleDirectory == nullptr ) { 74 if ( ntupleDirectory == nullptr ) { 75 // Retrieve directory only once as analysi 75 // Retrieve directory only once as analysis manager supports only 76 // 1 ntuple directory par file) 76 // 1 ntuple directory par file) 77 if ( ! dirName.empty() ) { 77 if ( ! dirName.empty() ) { 78 ntupleDirectory = tools::rroot::find_dir 78 ntupleDirectory = tools::rroot::find_dir(rfile->dir(), dirName); 79 if ( ntupleDirectory != nullptr ) { 79 if ( ntupleDirectory != nullptr ) { 80 std::get<2>(*rfileTuple) = ntupleDirec 80 std::get<2>(*rfileTuple) = ntupleDirectory; 81 } 81 } 82 else { 82 else { 83 G4Analysis::Warn( 83 G4Analysis::Warn( 84 "Directory " + dirName + " not found 84 "Directory " + dirName + " not found in file " + fileName + ".", 85 fkClass, "ReadNtupleImpl"); 85 fkClass, "ReadNtupleImpl"); 86 return kInvalidId; 86 return kInvalidId; 87 } 87 } 88 } 88 } 89 } 89 } 90 90 91 // Get key 91 // Get key 92 tools::rroot::key* key = nullptr; 92 tools::rroot::key* key = nullptr; 93 if ( ntupleDirectory != nullptr ) { 93 if ( ntupleDirectory != nullptr ) { 94 key = ntupleDirectory->find_key(ntupleName 94 key = ntupleDirectory->find_key(ntupleName); 95 } 95 } 96 else { 96 else { 97 key = rfile->dir().find_key(ntupleName); 97 key = rfile->dir().find_key(ntupleName); 98 } 98 } 99 if (key == nullptr) { 99 if (key == nullptr) { 100 Warn("Key " + ntupleName + " for Ntuple no 100 Warn("Key " + ntupleName + " for Ntuple not found in file " + fileName + 101 ", directory " + dirName, fkClass, "Read 101 ", directory " + dirName, fkClass, "ReadNtupleImpl"); 102 return kInvalidId; 102 return kInvalidId; 103 } 103 } 104 104 105 unsigned int size; 105 unsigned int size; 106 char* charBuffer = key->get_object_buffer(*r 106 char* charBuffer = key->get_object_buffer(*rfile, size); 107 if (charBuffer == nullptr) { 107 if (charBuffer == nullptr) { 108 Warn("Cannot get data buffer for Ntuple " 108 Warn("Cannot get data buffer for Ntuple " + ntupleName + 109 " in file " + fileName, fkClass, "Rea 109 " in file " + fileName, fkClass, "ReadNtupleImpl"); 110 return kInvalidId; 110 return kInvalidId; 111 } 111 } 112 112 113 auto verbose = false; 113 auto verbose = false; 114 auto buffer 114 auto buffer 115 = new tools::rroot::buffer(G4cout, rfile-> 115 = new tools::rroot::buffer(G4cout, rfile->byte_swap(), size, charBuffer, 116 key->key_length 116 key->key_length(), verbose); 117 buffer->set_map_objs(true); 117 buffer->set_map_objs(true); 118 118 119 auto fac = new tools::rroot::fac(G4cout); 119 auto fac = new tools::rroot::fac(G4cout); 120 120 121 auto tree = new tools::rroot::tree(*rfile, * 121 auto tree = new tools::rroot::tree(*rfile, *fac); 122 if ( ! tree->stream(*buffer) ) { 122 if ( ! tree->stream(*buffer) ) { 123 Warn("TTree streaming failed for Ntuple " 123 Warn("TTree streaming failed for Ntuple " + ntupleName + 124 " in file " + fileName, 124 " in file " + fileName, 125 fkClass, "ReadNtupleImpl"); 125 fkClass, "ReadNtupleImpl"); 126 126 127 delete buffer; 127 delete buffer; 128 delete tree; 128 delete tree; 129 return kInvalidId; 129 return kInvalidId; 130 } 130 } 131 131 132 auto rntuple = new tools::rroot::ntuple(*tr 132 auto rntuple = new tools::rroot::ntuple(*tree); //use the flat ntuple API. 133 auto rntupleDescription = new G4TRNtupleDesc 133 auto rntupleDescription = new G4TRNtupleDescription<tools::rroot::ntuple>(rntuple); 134 134 135 auto id = SetNtuple(rntupleDescription); 135 auto id = SetNtuple(rntupleDescription); 136 136 137 137 138 Message(kVL2, "read", "ntuple", ntupleName, 138 Message(kVL2, "read", "ntuple", ntupleName, id > kInvalidId); 139 139 140 return id; 140 return id; 141 } 141 } 142 142 143 //____________________________________________ 143 //_____________________________________________________________________________ 144 G4bool G4RootRNtupleManager::GetTNtupleRow( 144 G4bool G4RootRNtupleManager::GetTNtupleRow( 145 G4TRNtupleDescription<tools::rroot::ntuple>* 145 G4TRNtupleDescription<tools::rroot::ntuple>* ntupleDescription) 146 { 146 { 147 auto ntuple = ntupleDescription->fNtuple; 147 auto ntuple = ntupleDescription->fNtuple; 148 auto ntupleBinding = ntupleDescription->fNtu 148 auto ntupleBinding = ntupleDescription->fNtupleBinding; 149 149 150 G4bool isInitialized = ntupleDescription->fI 150 G4bool isInitialized = ntupleDescription->fIsInitialized; 151 if ( ! isInitialized ) { 151 if ( ! isInitialized ) { 152 152 153 if ( ! ntuple->initialize(G4cout, *ntupleB 153 if ( ! ntuple->initialize(G4cout, *ntupleBinding) ) { 154 Warn("Ntuple initialization failed !!", 154 Warn("Ntuple initialization failed !!", fkClass, "GetTNtupleRow"); 155 return false; 155 return false; 156 } 156 } 157 ntupleDescription->fIsInitialized = true; 157 ntupleDescription->fIsInitialized = true; 158 ntuple->start(); 158 ntuple->start(); 159 } 159 } 160 160 161 auto next = ntuple->next(); 161 auto next = ntuple->next(); 162 if ( next ) { 162 if ( next ) { 163 if ( ! ntuple->get_row() ) { 163 if ( ! ntuple->get_row() ) { 164 Warn("Ntuple get_row() failed !!", fkCla 164 Warn("Ntuple get_row() failed !!", fkClass, "GetTNtupleRow"); 165 return false; 165 return false; 166 } 166 } 167 } 167 } 168 168 169 return next; 169 return next; 170 } 170 } 171 171