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 // $Id: G4RootRNtupleManager.cc 70604 2013-06-03 11:27:06Z ihrivnac $ 26 27 27 // Author: Ivana Hrivnacova, 09/04/2014 (ivana 28 // Author: Ivana Hrivnacova, 09/04/2014 (ivana@ipno.in2p3.fr) 28 29 29 #include "G4RootRNtupleManager.hh" 30 #include "G4RootRNtupleManager.hh" 30 #include "G4RootRFileManager.hh" << 31 #include "G4RootRNtupleDescription.hh" 31 #include "G4AnalysisManagerState.hh" 32 #include "G4AnalysisManagerState.hh" 32 #include "G4AnalysisUtilities.hh" << 33 << 34 #include "tools/rroot/file" << 35 #include "tools/rroot/rall" << 36 #include "tools/rroot/streamers" << 37 #include "tools/rroot/fac" << 38 #include "tools/rroot/tree" << 39 << 40 using namespace G4Analysis; << 41 33 42 //____________________________________________ 34 //_____________________________________________________________________________ 43 G4RootRNtupleManager::G4RootRNtupleManager(con 35 G4RootRNtupleManager::G4RootRNtupleManager(const G4AnalysisManagerState& state) 44 : G4TRNtupleManager<tools::rroot::ntuple>(sta << 36 : G4VRNtupleManager(state), 45 {} << 37 fNtupleVector() >> 38 { >> 39 } 46 40 47 // << 41 //_____________________________________________________________________________ >> 42 G4RootRNtupleManager::~G4RootRNtupleManager() >> 43 { >> 44 std::vector<G4RootRNtupleDescription*>::iterator it; >> 45 for (it = fNtupleVector.begin(); it != fNtupleVector.end(); it++ ) { >> 46 delete (*it); >> 47 } >> 48 } >> 49 >> 50 // 48 // private methods 51 // private methods 49 // 52 // 50 53 51 //____________________________________________ 54 //_____________________________________________________________________________ 52 G4int G4RootRNtupleManager::ReadNtupleImpl(con << 55 G4RootRNtupleDescription* G4RootRNtupleManager::GetNtupleInFunction(G4int id, 53 con << 56 G4String functionName, G4bool warn) const 54 con << 57 { 55 G4b << 58 G4int index = id - fFirstId; 56 { << 59 if ( index < 0 || index >= G4int(fNtupleVector.size()) ) { 57 Message(kVL4, "read", "ntuple", ntupleName); << 60 if ( warn) { 58 << 61 G4String inFunction = "G4RootRNtupleManager::"; 59 // Ntuples are saved per thread << 62 inFunction += functionName; 60 // but do not apply the thread suffix if fil << 63 G4ExceptionDescription description; 61 auto isPerThread = true; << 64 description << " " << "ntuple " << id << " does not exist."; 62 if ( isUserFileName ) isPerThread = false; << 65 G4Exception(inFunction, "Analysis_WR011", JustWarning, description); 63 << 64 // Get or open a file << 65 auto rfileTuple = fFileManager->GetRFile(fil << 66 if (rfileTuple == nullptr) { << 67 if ( ! fFileManager->OpenRFile(fileName, i << 68 rfileTuple = fFileManager->GetRFile(fileNa << 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 } 66 } >> 67 return nullptr; 89 } 68 } >> 69 >> 70 return fNtupleVector[index]; >> 71 } >> 72 >> 73 // >> 74 // protected methods >> 75 // 90 76 91 // Get key << 77 //_____________________________________________________________________________ 92 tools::rroot::key* key = nullptr; << 78 G4bool G4RootRNtupleManager::IsEmpty() const 93 if ( ntupleDirectory != nullptr ) { << 79 { 94 key = ntupleDirectory->find_key(ntupleName << 80 return ! fNtupleVector.size(); 95 } << 81 } 96 else { << 82 97 key = rfile->dir().find_key(ntupleName); << 83 //_____________________________________________________________________________ 98 } << 84 G4bool G4RootRNtupleManager::Reset() 99 if (key == nullptr) { << 85 { 100 Warn("Key " + ntupleName + " for Ntuple no << 86 // Reset ntuples 101 ", directory " + dirName, fkClass, "Read << 102 return kInvalidId; << 103 } << 104 << 105 unsigned int size; << 106 char* charBuffer = key->get_object_buffer(*r << 107 if (charBuffer == nullptr) { << 108 Warn("Cannot get data buffer for Ntuple " << 109 " in file " + fileName, fkClass, "Rea << 110 return kInvalidId; << 111 } << 112 87 113 auto verbose = false; << 88 std::vector<G4RootRNtupleDescription*>::iterator it; 114 auto buffer << 89 for (it = fNtupleVector.begin(); it != fNtupleVector.end(); it++ ) { 115 = new tools::rroot::buffer(G4cout, rfile-> << 90 // ntuple is deleted automatically when file is closed 116 key->key_length << 91 // delete (*it)->fNtuple; 117 buffer->set_map_objs(true); << 92 (*it)->fNtuple=0; 118 << 93 } 119 auto fac = new tools::rroot::fac(G4cout); << 94 120 << 95 return true; 121 auto tree = new tools::rroot::tree(*rfile, * << 96 } 122 if ( ! tree->stream(*buffer) ) { << 123 Warn("TTree streaming failed for Ntuple " << 124 " in file " + fileName, << 125 fkClass, "ReadNtupleImpl"); << 126 << 127 delete buffer; << 128 delete tree; << 129 return kInvalidId; << 130 } << 131 97 132 auto rntuple = new tools::rroot::ntuple(*tr << 98 //_____________________________________________________________________________ 133 auto rntupleDescription = new G4TRNtupleDesc << 99 tools::rroot::ntuple* G4RootRNtupleManager::GetNtuple() const >> 100 { >> 101 return GetNtuple(fFirstId); >> 102 } 134 103 135 auto id = SetNtuple(rntupleDescription); << 104 //_____________________________________________________________________________ >> 105 tools::rroot::ntuple* G4RootRNtupleManager::GetNtuple(G4int ntupleId) const >> 106 { >> 107 G4RootRNtupleDescription* rntupleDescription >> 108 = GetNtupleInFunction(ntupleId, "GetRNtuple"); 136 109 >> 110 if ( ! rntupleDescription ) return nullptr; >> 111 >> 112 return rntupleDescription->fNtuple; >> 113 } 137 114 138 Message(kVL2, "read", "ntuple", ntupleName, << 115 //_____________________________________________________________________________ >> 116 G4int G4RootRNtupleManager::SetNtuple(G4RootRNtupleDescription* rntupleDescription) >> 117 { >> 118 G4int id = fNtupleVector.size() + fFirstId; 139 119 >> 120 fNtupleVector.push_back(rntupleDescription); >> 121 140 return id; 122 return id; >> 123 } >> 124 >> 125 //_____________________________________________________________________________ >> 126 G4bool G4RootRNtupleManager::SetNtupleIColumn(const G4String& columnName, >> 127 G4int& value) >> 128 { >> 129 return SetNtupleIColumn(fFirstId, columnName, value); >> 130 } >> 131 >> 132 //_____________________________________________________________________________ >> 133 G4bool G4RootRNtupleManager::SetNtupleFColumn(const G4String& columnName, >> 134 G4float& value) >> 135 { >> 136 return SetNtupleFColumn(fFirstId, columnName, value); >> 137 } >> 138 >> 139 //_____________________________________________________________________________ >> 140 G4bool G4RootRNtupleManager::SetNtupleDColumn(const G4String& columnName, >> 141 G4double& value) >> 142 { >> 143 return SetNtupleDColumn(fFirstId, columnName, value); >> 144 } >> 145 >> 146 //_____________________________________________________________________________ >> 147 G4bool G4RootRNtupleManager::SetNtupleSColumn(const G4String& columnName, >> 148 G4String& value) >> 149 { >> 150 return SetNtupleSColumn(fFirstId, columnName, value); >> 151 } >> 152 >> 153 //_____________________________________________________________________________ >> 154 G4bool G4RootRNtupleManager::SetNtupleIColumn(const G4String& columnName, >> 155 std::vector<G4int>& vector) >> 156 { >> 157 return SetNtupleIColumn(fFirstId, columnName, vector); 141 } 158 } 142 159 143 //____________________________________________ 160 //_____________________________________________________________________________ 144 G4bool G4RootRNtupleManager::GetTNtupleRow( << 161 G4bool G4RootRNtupleManager::SetNtupleFColumn(const G4String& columnName, 145 G4TRNtupleDescription<tools::rroot::ntuple>* << 162 std::vector<G4float>& vector) >> 163 { >> 164 return SetNtupleFColumn(fFirstId, columnName, vector); >> 165 } >> 166 >> 167 //_____________________________________________________________________________ >> 168 G4bool G4RootRNtupleManager::SetNtupleDColumn(const G4String& columnName, >> 169 std::vector<G4double>& vector) >> 170 { >> 171 return SetNtupleDColumn(fFirstId, columnName, vector); >> 172 } >> 173 >> 174 //_____________________________________________________________________________ >> 175 G4bool G4RootRNtupleManager::SetNtupleIColumn(G4int ntupleId, >> 176 const G4String& columnName, >> 177 G4int& value) >> 178 { >> 179 #ifdef G4VERBOSE >> 180 if ( fState.GetVerboseL4() ) { >> 181 G4ExceptionDescription description; >> 182 description << " ntupleId " << ntupleId << " " << columnName; >> 183 fState.GetVerboseL4()->Message("set", "ntuple I column", description); >> 184 } >> 185 #endif >> 186 >> 187 G4RootRNtupleDescription* ntupleDescription >> 188 = GetNtupleInFunction(ntupleId, "SetNtupleIColumn"); >> 189 if ( ! ntupleDescription ) return false; >> 190 >> 191 tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding; >> 192 ntupleBinding->add_column(columnName, value); >> 193 >> 194 #ifdef G4VERBOSE >> 195 if ( fState.GetVerboseL2() ) { >> 196 G4ExceptionDescription description; >> 197 description << " ntupleId " << ntupleId << " " << columnName; >> 198 fState.GetVerboseL2()->Message("set", "ntuple I colum", description, true); >> 199 } >> 200 #endif >> 201 >> 202 return true; >> 203 } >> 204 >> 205 //_____________________________________________________________________________ >> 206 G4bool G4RootRNtupleManager::SetNtupleFColumn(G4int ntupleId, >> 207 const G4String& columnName, >> 208 G4float& value) >> 209 { >> 210 #ifdef G4VERBOSE >> 211 if ( fState.GetVerboseL4() ) { >> 212 G4ExceptionDescription description; >> 213 description << " ntupleId " << ntupleId << " " << columnName; >> 214 fState.GetVerboseL4()->Message("set", "ntuple F column", description); >> 215 } >> 216 #endif >> 217 >> 218 G4RootRNtupleDescription* ntupleDescription >> 219 = GetNtupleInFunction(ntupleId, "SetNtupleFColumn"); >> 220 if ( ! ntupleDescription ) return false; >> 221 >> 222 tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding; >> 223 ntupleBinding->add_column(columnName, value); >> 224 >> 225 #ifdef G4VERBOSE >> 226 if ( fState.GetVerboseL2() ) { >> 227 G4ExceptionDescription description; >> 228 description << " ntupleId " << ntupleId << " " << columnName; >> 229 fState.GetVerboseL2()->Message("set", "ntuple F colum", description, true); >> 230 } >> 231 #endif >> 232 >> 233 return true; >> 234 } >> 235 >> 236 //_____________________________________________________________________________ >> 237 G4bool G4RootRNtupleManager::SetNtupleDColumn(G4int ntupleId, >> 238 const G4String& columnName, >> 239 G4double& value) >> 240 { >> 241 #ifdef G4VERBOSE >> 242 if ( fState.GetVerboseL4() ) { >> 243 G4ExceptionDescription description; >> 244 description << " ntupleId " << ntupleId << " " << columnName; >> 245 fState.GetVerboseL4()->Message("set", "ntuple D column", description); >> 246 } >> 247 #endif >> 248 >> 249 G4RootRNtupleDescription* ntupleDescription >> 250 = GetNtupleInFunction(ntupleId, "SetNtupleDColumn"); >> 251 if ( ! ntupleDescription ) return false; >> 252 >> 253 tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding; >> 254 ntupleBinding->add_column(columnName, value); >> 255 >> 256 #ifdef G4VERBOSE >> 257 if ( fState.GetVerboseL2() ) { >> 258 G4ExceptionDescription description; >> 259 description << " ntupleId " << ntupleId << " " << columnName; >> 260 fState.GetVerboseL2()->Message("set", "ntuple D colum", description, true); >> 261 } >> 262 #endif >> 263 >> 264 return true; >> 265 } >> 266 >> 267 //_____________________________________________________________________________ >> 268 G4bool G4RootRNtupleManager::SetNtupleSColumn(G4int ntupleId, >> 269 const G4String& columnName, >> 270 G4String& value) >> 271 { >> 272 #ifdef G4VERBOSE >> 273 if ( fState.GetVerboseL4() ) { >> 274 G4ExceptionDescription description; >> 275 description << " ntupleId " << ntupleId << " " << columnName; >> 276 fState.GetVerboseL4()->Message("set", "ntuple S column", description); >> 277 } >> 278 #endif >> 279 >> 280 G4RootRNtupleDescription* ntupleDescription >> 281 = GetNtupleInFunction(ntupleId, "SetNtupleSColumn"); >> 282 if ( ! ntupleDescription ) return false; >> 283 >> 284 tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding; >> 285 ntupleBinding->add_column(columnName, value); >> 286 >> 287 #ifdef G4VERBOSE >> 288 if ( fState.GetVerboseL2() ) { >> 289 G4ExceptionDescription description; >> 290 description << " ntupleId " << ntupleId << " " << columnName; >> 291 fState.GetVerboseL2()->Message("set", "ntuple S colum", description, true); >> 292 } >> 293 #endif >> 294 >> 295 return true; >> 296 } >> 297 >> 298 //_____________________________________________________________________________ >> 299 G4bool G4RootRNtupleManager::SetNtupleIColumn(G4int ntupleId, >> 300 const G4String& columnName, >> 301 std::vector<G4int>& vector) >> 302 { >> 303 #ifdef G4VERBOSE >> 304 if ( fState.GetVerboseL4() ) { >> 305 G4ExceptionDescription description; >> 306 description << " ntupleId " << ntupleId << " " << columnName; >> 307 fState.GetVerboseL4()->Message("set", "ntuple I column", description); >> 308 } >> 309 #endif >> 310 >> 311 G4RootRNtupleDescription* ntupleDescription >> 312 = GetNtupleInFunction(ntupleId, "SetNtupleIColumn"); >> 313 if ( ! ntupleDescription ) return false; >> 314 >> 315 tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding; >> 316 ntupleBinding->add_column(columnName, vector); >> 317 >> 318 #ifdef G4VERBOSE >> 319 if ( fState.GetVerboseL2() ) { >> 320 G4ExceptionDescription description; >> 321 description << " ntupleId " << ntupleId << " " << columnName; >> 322 fState.GetVerboseL2()->Message("set", "ntuple I colum", description, true); >> 323 } >> 324 #endif >> 325 >> 326 return true; >> 327 } >> 328 >> 329 //_____________________________________________________________________________ >> 330 G4bool G4RootRNtupleManager::SetNtupleFColumn(G4int ntupleId, >> 331 const G4String& columnName, >> 332 std::vector<G4float>& vector) >> 333 { >> 334 #ifdef G4VERBOSE >> 335 if ( fState.GetVerboseL4() ) { >> 336 G4ExceptionDescription description; >> 337 description << " ntupleId " << ntupleId << " " << columnName; >> 338 fState.GetVerboseL4()->Message("set", "ntuple F column of vector", description); >> 339 } >> 340 #endif >> 341 >> 342 G4RootRNtupleDescription* ntupleDescription >> 343 = GetNtupleInFunction(ntupleId, "SetNtupleFColumn"); >> 344 if ( ! ntupleDescription ) return false; >> 345 >> 346 tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding; >> 347 ntupleBinding->add_column(columnName, vector); >> 348 >> 349 #ifdef G4VERBOSE >> 350 if ( fState.GetVerboseL2() ) { >> 351 G4ExceptionDescription description; >> 352 description << " ntupleId " << ntupleId << " " << columnName; >> 353 fState.GetVerboseL2()->Message("set", "ntuple F colum", description, true); >> 354 } >> 355 #endif >> 356 >> 357 return true; >> 358 } >> 359 >> 360 //_____________________________________________________________________________ >> 361 G4bool G4RootRNtupleManager::SetNtupleDColumn(G4int ntupleId, >> 362 const G4String& columnName, >> 363 std::vector<G4double>& vector) 146 { 364 { 147 auto ntuple = ntupleDescription->fNtuple; << 365 #ifdef G4VERBOSE 148 auto ntupleBinding = ntupleDescription->fNtu << 366 if ( fState.GetVerboseL4() ) { >> 367 G4ExceptionDescription description; >> 368 description << " ntupleId " << ntupleId << " " << columnName; >> 369 fState.GetVerboseL4()->Message("set", "ntuple D column of vector", description); >> 370 } >> 371 #endif >> 372 >> 373 G4RootRNtupleDescription* ntupleDescription >> 374 = GetNtupleInFunction(ntupleId, "SetNtupleDColumn"); >> 375 if ( ! ntupleDescription ) return false; >> 376 >> 377 tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding; >> 378 ntupleBinding->add_column(columnName, vector); >> 379 >> 380 #ifdef G4VERBOSE >> 381 if ( fState.GetVerboseL2() ) { >> 382 G4ExceptionDescription description; >> 383 description << " ntupleId " << ntupleId << " " << columnName; >> 384 fState.GetVerboseL2()->Message("set", "ntuple D colum", description, true); >> 385 } >> 386 #endif >> 387 >> 388 return true; >> 389 } >> 390 >> 391 //_____________________________________________________________________________ >> 392 G4bool G4RootRNtupleManager::GetNtupleRow() >> 393 { >> 394 return GetNtupleRow(fFirstId); >> 395 } >> 396 >> 397 //_____________________________________________________________________________ >> 398 G4bool G4RootRNtupleManager::GetNtupleRow(G4int ntupleId) >> 399 { >> 400 #ifdef G4VERBOSE >> 401 if ( fState.GetVerboseL4() ) { >> 402 G4ExceptionDescription description; >> 403 description << " ntupleId " << ntupleId; >> 404 fState.GetVerboseL4()->Message("get", "ntuple row", description); >> 405 } >> 406 #endif >> 407 >> 408 G4RootRNtupleDescription* ntupleDescription >> 409 = GetNtupleInFunction(ntupleId, "GetNtupleRow"); >> 410 if ( ! ntupleDescription ) return false; >> 411 >> 412 tools::rroot::ntuple* ntuple = ntupleDescription->fNtuple; >> 413 tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding; 149 414 150 G4bool isInitialized = ntupleDescription->fI 415 G4bool isInitialized = ntupleDescription->fIsInitialized; 151 if ( ! isInitialized ) { 416 if ( ! isInitialized ) { 152 417 >> 418 #ifdef G4VERBOSE >> 419 if ( fState.GetVerboseL4() ) { >> 420 G4ExceptionDescription description; >> 421 description << " ntupleId " << ntupleId; >> 422 fState.GetVerboseL4()->Message("initialize", "ntuple", description); >> 423 } >> 424 #endif 153 if ( ! ntuple->initialize(G4cout, *ntupleB 425 if ( ! ntuple->initialize(G4cout, *ntupleBinding) ) { 154 Warn("Ntuple initialization failed !!", << 426 G4ExceptionDescription description; >> 427 description >> 428 << " " >> 429 << "Ntuple initialization failed !!"; >> 430 G4Exception("G4RootRNtuple::GetNtupleRow()", >> 431 "Analysis_WR021", JustWarning, description); 155 return false; 432 return false; 156 } 433 } 157 ntupleDescription->fIsInitialized = true; 434 ntupleDescription->fIsInitialized = true; 158 ntuple->start(); 435 ntuple->start(); 159 } 436 } 160 437 161 auto next = ntuple->next(); << 438 G4bool next = ntuple->next(); 162 if ( next ) { 439 if ( next ) { 163 if ( ! ntuple->get_row() ) { 440 if ( ! ntuple->get_row() ) { 164 Warn("Ntuple get_row() failed !!", fkCla << 441 G4ExceptionDescription description; >> 442 description >> 443 << " " >> 444 << "Ntuple get_row() failed !!"; >> 445 G4Exception("G4RootRNtuple::GetNtupleRow()", >> 446 "Analysis_WR021", JustWarning, description); 165 return false; 447 return false; 166 } 448 } 167 } << 449 } >> 450 >> 451 #ifdef G4VERBOSE >> 452 if ( fState.GetVerboseL2() ) { >> 453 G4ExceptionDescription description; >> 454 description << " ntupleId " << ntupleId; >> 455 fState.GetVerboseL2()->Message("get", "ntuple row", description, true); >> 456 } >> 457 #endif 168 458 169 return next; 459 return next; 170 } << 460 } 171 461