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, 25/07/2014 (ivana 27 // Author: Ivana Hrivnacova, 25/07/2014 (ivana@ipno.in2p3.fr) 28 28 29 #include "G4XmlRNtupleManager.hh" 29 #include "G4XmlRNtupleManager.hh" 30 #include "G4XmlRFileManager.hh" 30 #include "G4XmlRFileManager.hh" 31 #include "G4AnalysisManagerState.hh" 31 #include "G4AnalysisManagerState.hh" 32 #include "G4AnalysisUtilities.hh" 32 #include "G4AnalysisUtilities.hh" 33 33 34 using namespace G4Analysis; 34 using namespace G4Analysis; 35 using std::to_string; 35 using std::to_string; 36 36 37 // 37 // 38 // utility function (to be provided in tools) 38 // utility function (to be provided in tools) 39 // 39 // 40 40 41 namespace tools { 41 namespace tools { 42 namespace aida { 42 namespace aida { 43 template <class T> 43 template <class T> 44 bool to_vector(base_ntu& a_ntu,std::vector<T>& 44 bool to_vector(base_ntu& a_ntu,std::vector<T>& a_vec) { 45 a_vec.clear(); 45 a_vec.clear(); 46 const std::vector<base_col*>& cols = a_ntu.c 46 const std::vector<base_col*>& cols = a_ntu.cols(); 47 if(cols.empty()) return false; 47 if(cols.empty()) return false; 48 base_col* _base_col = cols.front(); 48 base_col* _base_col = cols.front(); 49 aida_col<T>* _col = safe_cast<base_col, aida 49 aida_col<T>* _col = safe_cast<base_col, aida_col<T> >(*_base_col); 50 if(!_col) return false; 50 if(!_col) return false; 51 a_ntu.start(); 51 a_ntu.start(); 52 uint64 _rows = a_ntu.rows(); 52 uint64 _rows = a_ntu.rows(); 53 a_vec.resize(_rows); 53 a_vec.resize(_rows); 54 T v; 54 T v; 55 {for(uint64 row=0;row<_rows;row++) { 55 {for(uint64 row=0;row<_rows;row++) { 56 if(!a_ntu.next()) {a_vec.clear();return fa 56 if(!a_ntu.next()) {a_vec.clear();return false;} 57 if(!_col->get_entry(v)) {a_vec.clear();ret 57 if(!_col->get_entry(v)) {a_vec.clear();return false;} 58 a_vec[row] = v; 58 a_vec[row] = v; 59 }} 59 }} 60 return true; 60 return true; 61 } 61 } 62 }} 62 }} 63 63 64 //____________________________________________ 64 //_____________________________________________________________________________ 65 G4XmlRNtupleManager::G4XmlRNtupleManager(const 65 G4XmlRNtupleManager::G4XmlRNtupleManager(const G4AnalysisManagerState& state) 66 : G4TRNtupleManager<tools::aida::ntuple>(stat 66 : G4TRNtupleManager<tools::aida::ntuple>(state) 67 {} 67 {} 68 68 69 // 69 // 70 // private methods 70 // private methods 71 // 71 // 72 72 73 //____________________________________________ 73 //_____________________________________________________________________________ 74 G4int G4XmlRNtupleManager::ReadNtupleImpl(cons 74 G4int G4XmlRNtupleManager::ReadNtupleImpl(const G4String& ntupleName, 75 cons 75 const G4String& fileName, 76 cons 76 const G4String& /*dirName*/, 77 G4bo 77 G4bool isUserFileName) 78 { 78 { 79 Message(kVL4, "read", "ntuple", ntupleName); 79 Message(kVL4, "read", "ntuple", ntupleName); 80 80 81 // Ntuples are saved per object and per thre 81 // Ntuples are saved per object and per thread 82 // but apply the ntuple name and the thread 82 // but apply the ntuple name and the thread suffixes 83 // only if fileName is not provided explicit 83 // only if fileName is not provided explicitly 84 auto fullFileName = fileName; 84 auto fullFileName = fileName; 85 if ( ! isUserFileName ) { 85 if ( ! isUserFileName ) { 86 fullFileName = fFileManager->GetNtupleFile 86 fullFileName = fFileManager->GetNtupleFileName(ntupleName); 87 } 87 } 88 88 89 auto handler = fFileManager->GetHandler<tool 89 auto handler = fFileManager->GetHandler<tools::aida::ntuple>( 90 fullFileName, 90 fullFileName, ntupleName, "ReadNtupleImpl"); 91 if (handler == nullptr) return kInvalidId; << 91 if ( ! handler ) return kInvalidId; 92 92 93 auto rntuple = static_cast<tools::aida::ntup 93 auto rntuple = static_cast<tools::aida::ntuple*>(handler->object()); 94 auto id = SetNtuple(new G4TRNtupleDescriptio 94 auto id = SetNtuple(new G4TRNtupleDescription<tools::aida::ntuple>(rntuple)); 95 95 96 Message(kVL2, "read", "ntuple", ntupleName, 96 Message(kVL2, "read", "ntuple", ntupleName, id > kInvalidId); 97 97 98 return id; 98 return id; 99 } 99 } 100 100 101 //____________________________________________ 101 //_____________________________________________________________________________ 102 G4bool G4XmlRNtupleManager::SetNtupleIColumn(G 102 G4bool G4XmlRNtupleManager::SetNtupleIColumn(G4int ntupleId, 103 c 103 const G4String& columnName, 104 s 104 std::vector<G4int>& vector) 105 { 105 { 106 // Override base class default implementation 106 // Override base class default implementation 107 107 108 Message(kVL4, "set", "ntuple I column", 108 Message(kVL4, "set", "ntuple I column", 109 " ntupleId " + to_string(ntupleId) + " " + 109 " ntupleId " + to_string(ntupleId) + " " + columnName); 110 110 111 auto ntupleDescription = GetNtupleDescriptio 111 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetNtupleIColumn"); 112 if (ntupleDescription == nullptr) return fal << 112 if ( ! ntupleDescription ) return false; 113 113 114 // not supported 114 // not supported 115 //tools::ntuple_binding* ntupleBinding = ntu 115 //tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding; 116 //ntupleBinding->add_column(columnName, vect 116 //ntupleBinding->add_column(columnName, vector); 117 117 118 auto subNtuple = new tools::aida::ntuple(G4c 118 auto subNtuple = new tools::aida::ntuple(G4cout, columnName); 119 ntupleDescription->fIVectorBindingMap[subNtu 119 ntupleDescription->fIVectorBindingMap[subNtuple] = &vector; 120 tools::ntuple_binding* ntupleBinding = ntupl 120 tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding; 121 ntupleBinding->add_column_cid(columnName, *s 121 ntupleBinding->add_column_cid(columnName, *subNtuple); 122 122 123 Message(kVL2, "set", "ntuple I column", 123 Message(kVL2, "set", "ntuple I column", 124 " ntupleId " + to_string(ntupleId) + " " + 124 " ntupleId " + to_string(ntupleId) + " " + columnName); 125 125 126 return true; 126 return true; 127 } 127 } 128 128 129 //____________________________________________ 129 //_____________________________________________________________________________ 130 G4bool G4XmlRNtupleManager::SetNtupleFColumn(G 130 G4bool G4XmlRNtupleManager::SetNtupleFColumn(G4int ntupleId, 131 c 131 const G4String& columnName, 132 s 132 std::vector<G4float>& vector) 133 { 133 { 134 // Override base class default implementation 134 // Override base class default implementation 135 135 136 Message(kVL4, "set", "ntuple F column", 136 Message(kVL4, "set", "ntuple F column", 137 " ntupleId " + to_string(ntupleId) + " " + 137 " ntupleId " + to_string(ntupleId) + " " + columnName); 138 138 139 139 140 auto ntupleDescription = GetNtupleDescriptio 140 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetNtupleFColumn"); 141 if (ntupleDescription == nullptr) return fal << 141 if ( ! ntupleDescription ) return false; 142 142 143 // not supported 143 // not supported 144 //tools::ntuple_binding* ntupleBinding = ntu 144 //tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding; 145 //ntupleBinding->add_column(columnName, vect 145 //ntupleBinding->add_column(columnName, vector); 146 146 147 auto subNtuple = new tools::aida::ntuple(G4c 147 auto subNtuple = new tools::aida::ntuple(G4cout, columnName); 148 ntupleDescription->fFVectorBindingMap[subNtu 148 ntupleDescription->fFVectorBindingMap[subNtuple] = &vector; 149 tools::ntuple_binding* ntupleBinding = ntupl 149 tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding; 150 ntupleBinding->add_column_cid(columnName, *s 150 ntupleBinding->add_column_cid(columnName, *subNtuple); 151 151 152 Message(kVL4, "set", "ntuple F column", 152 Message(kVL4, "set", "ntuple F column", 153 " ntupleId " + to_string(ntupleId) + " " + 153 " ntupleId " + to_string(ntupleId) + " " + columnName); 154 154 155 return true; 155 return true; 156 } 156 } 157 157 158 //____________________________________________ 158 //_____________________________________________________________________________ 159 G4bool G4XmlRNtupleManager::SetNtupleDColumn(G 159 G4bool G4XmlRNtupleManager::SetNtupleDColumn(G4int ntupleId, 160 c 160 const G4String& columnName, 161 s 161 std::vector<G4double>& vector) 162 { 162 { 163 // Override base class default implementation 163 // Override base class default implementation 164 164 165 Message(kVL4, "set", "ntuple D column", 165 Message(kVL4, "set", "ntuple D column", 166 " ntupleId " + to_string(ntupleId) + " " + 166 " ntupleId " + to_string(ntupleId) + " " + columnName); 167 167 168 auto ntupleDescription = GetNtupleDescriptio 168 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetNtupleDColumn"); 169 if (ntupleDescription == nullptr) return fal << 169 if ( ! ntupleDescription ) return false; 170 170 171 // not supported 171 // not supported 172 //tools::ntuple_binding* ntupleBinding = ntu 172 //tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding; 173 //ntupleBinding->add_column(columnName, vect 173 //ntupleBinding->add_column(columnName, vector); 174 174 175 auto subNtuple = new tools::aida::ntuple(G4c 175 auto subNtuple = new tools::aida::ntuple(G4cout, columnName); 176 ntupleDescription->fDVectorBindingMap[subNtu 176 ntupleDescription->fDVectorBindingMap[subNtuple] = &vector; 177 tools::ntuple_binding* ntupleBinding = ntupl 177 tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding; 178 ntupleBinding->add_column_cid(columnName, *s 178 ntupleBinding->add_column_cid(columnName, *subNtuple); 179 179 180 Message(kVL2, "set", "ntuple D column", 180 Message(kVL2, "set", "ntuple D column", 181 " ntupleId " + to_string(ntupleId) + " " + 181 " ntupleId " + to_string(ntupleId) + " " + columnName); 182 182 183 return true; 183 return true; 184 } 184 } 185 185 186 //____________________________________________ 186 //_____________________________________________________________________________ 187 G4bool G4XmlRNtupleManager::SetNtupleSColumn(G 187 G4bool G4XmlRNtupleManager::SetNtupleSColumn(G4int ntupleId, 188 c 188 const G4String& columnName, 189 s 189 std::vector<std::string>& vector) 190 { 190 { 191 // Override base class default implementation 191 // Override base class default implementation 192 192 193 Message(kVL4, "set", "ntuple S column", 193 Message(kVL4, "set", "ntuple S column", 194 " ntupleId " + to_string(ntupleId) + " " + 194 " ntupleId " + to_string(ntupleId) + " " + columnName); 195 195 196 auto ntupleDescription = GetNtupleDescriptio 196 auto ntupleDescription = GetNtupleDescriptionInFunction(ntupleId, "SetNtupleSColumn"); 197 if (ntupleDescription == nullptr) return fal << 197 if ( ! ntupleDescription ) return false; 198 198 199 // not supported 199 // not supported 200 //tools::ntuple_binding* ntupleBinding = ntu 200 //tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding; 201 //ntupleBinding->add_column(columnName, vect 201 //ntupleBinding->add_column(columnName, vector); 202 202 203 auto subNtuple = new tools::aida::ntuple(G4c 203 auto subNtuple = new tools::aida::ntuple(G4cout, columnName); 204 ntupleDescription->fSVectorBindingMap[subNtu 204 ntupleDescription->fSVectorBindingMap[subNtuple] = &vector; 205 tools::ntuple_binding* ntupleBinding = ntupl 205 tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding; 206 ntupleBinding->add_column_cid(columnName, *s 206 ntupleBinding->add_column_cid(columnName, *subNtuple); 207 207 208 Message(kVL2, "set", "ntuple S column", 208 Message(kVL2, "set", "ntuple S column", 209 " ntupleId " + to_string(ntupleId) + " " + 209 " ntupleId " + to_string(ntupleId) + " " + columnName); 210 210 211 return true; 211 return true; 212 } 212 } 213 213 214 //____________________________________________ 214 //_____________________________________________________________________________ 215 G4bool G4XmlRNtupleManager::GetTNtupleRow( 215 G4bool G4XmlRNtupleManager::GetTNtupleRow( 216 G4TRNtupleDescription<tools::aida::ntuple>* 216 G4TRNtupleDescription<tools::aida::ntuple>* ntupleDescription) 217 { 217 { 218 auto ntuple = ntupleDescription->fNtuple; 218 auto ntuple = ntupleDescription->fNtuple; 219 219 220 G4bool isInitialized = ntupleDescription->fI 220 G4bool isInitialized = ntupleDescription->fIsInitialized; 221 if ( ! isInitialized ) { 221 if ( ! isInitialized ) { 222 tools::ntuple_binding* ntupleBinding = ntu 222 tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding; 223 if ( ! ntuple->set_binding(std::cout, *ntu 223 if ( ! ntuple->set_binding(std::cout, *ntupleBinding) ) { 224 Warn("Ntuple initialization failed !!", 224 Warn("Ntuple initialization failed !!", fkClass, "GetTNtupleRow"); 225 return false; 225 return false; 226 } 226 } 227 ntupleDescription->fIsInitialized = true; 227 ntupleDescription->fIsInitialized = true; 228 ntuple->start(); 228 ntuple->start(); 229 } 229 } 230 230 231 G4bool next = ntuple->next(); 231 G4bool next = ntuple->next(); 232 if ( next ) { 232 if ( next ) { 233 if ( ! ntuple->get_row() ) { 233 if ( ! ntuple->get_row() ) { 234 Warn("Ntuple get_row() failed !!", fkCla 234 Warn("Ntuple get_row() failed !!", fkClass, "GetTNtupleRow"); 235 return false; 235 return false; 236 } 236 } 237 237 238 // fill vector from sub ntuples 238 // fill vector from sub ntuples 239 for ( auto [key, value] : ntupleDescriptio 239 for ( auto [key, value] : ntupleDescription->fIVectorBindingMap) { 240 tools::aida::to_vector<int>(*key, *value 240 tools::aida::to_vector<int>(*key, *value); 241 } 241 } 242 for ( auto [key, value] : ntupleDescriptio 242 for ( auto [key, value] : ntupleDescription->fFVectorBindingMap) { 243 tools::aida::to_vector<float>(*key, *val 243 tools::aida::to_vector<float>(*key, *value); 244 } 244 } 245 for ( auto [key, value] : ntupleDescriptio 245 for ( auto [key, value] : ntupleDescription->fDVectorBindingMap) { 246 tools::aida::to_vector<double>(*key, *va 246 tools::aida::to_vector<double>(*key, *value); 247 } 247 } 248 for ( auto [key, value] : ntupleDescriptio 248 for ( auto [key, value] : ntupleDescription->fSVectorBindingMap) { 249 tools::aida::to_vector<std::string>(*key 249 tools::aida::to_vector<std::string>(*key, *value); 250 } 250 } 251 } 251 } 252 252 253 return next; 253 return next; 254 } 254 } 255 255