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$ 26 27 27 // Author: Ivana Hrivnacova, 25/07/2014 (ivana 28 // Author: Ivana Hrivnacova, 25/07/2014 (ivana@ipno.in2p3.fr) 28 29 29 #include "G4CsvRNtupleManager.hh" 30 #include "G4CsvRNtupleManager.hh" 30 #include "G4CsvRFileManager.hh" << 31 #include "G4CsvRNtupleDescription.hh" 31 #include "G4AnalysisManagerState.hh" 32 #include "G4AnalysisManagerState.hh" 32 #include "G4AnalysisUtilities.hh" << 33 << 34 using namespace G4Analysis; << 35 33 36 //____________________________________________ 34 //_____________________________________________________________________________ 37 G4CsvRNtupleManager::G4CsvRNtupleManager(const 35 G4CsvRNtupleManager::G4CsvRNtupleManager(const G4AnalysisManagerState& state) 38 : G4TRNtupleManager<tools::rcsv::ntuple>(stat << 36 : G4VRNtupleManager(state), 39 {} << 37 fNtupleVector() >> 38 { >> 39 } 40 40 41 // << 41 //_____________________________________________________________________________ >> 42 G4CsvRNtupleManager::~G4CsvRNtupleManager() >> 43 { >> 44 std::vector<G4CsvRNtupleDescription*>::iterator it; >> 45 for (it = fNtupleVector.begin(); it != fNtupleVector.end(); it++ ) { >> 46 delete (*it); >> 47 } >> 48 } >> 49 >> 50 // 42 // private methods 51 // private methods 43 // 52 // 44 53 45 //____________________________________________ 54 //_____________________________________________________________________________ 46 G4int G4CsvRNtupleManager::ReadNtupleImpl(cons << 55 G4CsvRNtupleDescription* G4CsvRNtupleManager::GetNtupleInFunction(G4int id, 47 cons << 56 G4String functionName, G4bool warn) const 48 cons << 57 { 49 G4bo << 58 G4int index = id - fFirstId; 50 { << 59 if ( index < 0 || index >= G4int(fNtupleVector.size()) ) { 51 Message(kVL4, "read", "ntuple", ntupleName); << 60 if ( warn) { 52 << 61 G4String inFunction = "G4CsvRNtupleManager::"; 53 // Ntuples are saved per object and per thre << 62 inFunction += functionName; 54 // but apply the ntuple name and the thread << 63 G4ExceptionDescription description; 55 // only if fileName is not provided explicit << 64 description << " " << "ntuple " << id << " does not exist."; 56 G4String fullFileName = fileName; << 65 G4Exception(inFunction, "Analysis_WR011", JustWarning, description); 57 if ( ! isUserFileName ) { << 66 } 58 fullFileName = fFileManager->GetNtupleFile << 67 return nullptr; 59 } 68 } >> 69 >> 70 return fNtupleVector[index]; >> 71 } >> 72 >> 73 // >> 74 // protected methods >> 75 // 60 76 61 // Update directory path << 77 //_____________________________________________________________________________ 62 if ( ! dirName.empty() ) { << 78 G4bool G4CsvRNtupleManager::IsEmpty() const 63 fullFileName = "./" + dirName + "/" + full << 79 { 64 } << 80 return ! fNtupleVector.size(); >> 81 } >> 82 >> 83 //_____________________________________________________________________________ >> 84 G4bool G4CsvRNtupleManager::Reset() >> 85 { >> 86 // Reset ntuples >> 87 >> 88 std::vector<G4CsvRNtupleDescription*>::iterator it; >> 89 for (it = fNtupleVector.begin(); it != fNtupleVector.end(); it++ ) { >> 90 // ntuple is deleted automatically when file is closed >> 91 // delete (*it)->fNtuple; >> 92 (*it)->fNtuple=0; >> 93 } >> 94 >> 95 return true; >> 96 } >> 97 >> 98 //_____________________________________________________________________________ >> 99 tools::rcsv::ntuple* G4CsvRNtupleManager::GetNtuple() const >> 100 { >> 101 return GetNtuple(fFirstId); >> 102 } >> 103 >> 104 //_____________________________________________________________________________ >> 105 tools::rcsv::ntuple* G4CsvRNtupleManager::GetNtuple(G4int ntupleId) const >> 106 { >> 107 G4CsvRNtupleDescription* rntupleDescription >> 108 = GetNtupleInFunction(ntupleId, "GetNtuple"); 65 109 66 // Open file << 110 if ( ! rntupleDescription ) return nullptr; 67 if ( ! fFileManager->OpenRFile(fullFileName) << 68 auto ntupleFile = fFileManager->GetRFile(ful << 69 << 70 // Create ntuple << 71 auto rntuple = new tools::rcsv::ntuple(*ntup << 72 auto id = SetNtuple(new G4TRNtupleDescriptio << 73 111 74 Message(kVL2, "read", "ntuple", ntupleName, << 112 return rntupleDescription->fNtuple; >> 113 } 75 114 >> 115 //_____________________________________________________________________________ >> 116 G4int G4CsvRNtupleManager::SetNtuple(G4CsvRNtupleDescription* rntupleDescription) >> 117 { >> 118 G4int id = fNtupleVector.size() + fFirstId; >> 119 >> 120 fNtupleVector.push_back(rntupleDescription); >> 121 76 return id; 122 return id; >> 123 } >> 124 >> 125 //_____________________________________________________________________________ >> 126 G4bool G4CsvRNtupleManager::SetNtupleIColumn(const G4String& columnName, >> 127 G4int& value) >> 128 { >> 129 return SetNtupleIColumn(fFirstId, columnName, value); >> 130 } >> 131 >> 132 //_____________________________________________________________________________ >> 133 G4bool G4CsvRNtupleManager::SetNtupleFColumn(const G4String& columnName, >> 134 G4float& value) >> 135 { >> 136 return SetNtupleFColumn(fFirstId, columnName, value); >> 137 } >> 138 >> 139 //_____________________________________________________________________________ >> 140 G4bool G4CsvRNtupleManager::SetNtupleDColumn(const G4String& columnName, >> 141 G4double& value) >> 142 { >> 143 return SetNtupleDColumn(fFirstId, columnName, value); >> 144 } >> 145 >> 146 //_____________________________________________________________________________ >> 147 G4bool G4CsvRNtupleManager::SetNtupleIColumn(const G4String& columnName, >> 148 std::vector<G4int>& vector) >> 149 { >> 150 return SetNtupleIColumn(fFirstId, columnName, vector); >> 151 } >> 152 >> 153 //_____________________________________________________________________________ >> 154 G4bool G4CsvRNtupleManager::SetNtupleFColumn(const G4String& columnName, >> 155 std::vector<G4float>& vector) >> 156 { >> 157 return SetNtupleFColumn(fFirstId, columnName, vector); >> 158 } >> 159 >> 160 //_____________________________________________________________________________ >> 161 G4bool G4CsvRNtupleManager::SetNtupleDColumn(const G4String& columnName, >> 162 std::vector<G4double>& vector) >> 163 { >> 164 return SetNtupleDColumn(fFirstId, columnName, vector); >> 165 } >> 166 >> 167 //_____________________________________________________________________________ >> 168 G4bool G4CsvRNtupleManager::SetNtupleSColumn(const G4String& columnName, >> 169 G4String& value) >> 170 { >> 171 return SetNtupleSColumn(fFirstId, columnName, value); >> 172 } >> 173 >> 174 //_____________________________________________________________________________ >> 175 G4bool G4CsvRNtupleManager::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 G4CsvRNtupleDescription* 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 G4CsvRNtupleManager::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 G4CsvRNtupleDescription* 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 G4CsvRNtupleManager::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 G4CsvRNtupleDescription* 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 G4CsvRNtupleManager::SetNtupleIColumn(G4int ntupleId, >> 269 const G4String& columnName, >> 270 std::vector<G4int>& vector) >> 271 { >> 272 #ifdef G4VERBOSE >> 273 if ( fState.GetVerboseL4() ) { >> 274 G4ExceptionDescription description; >> 275 description << " ntupleId " << ntupleId << " " << columnName; >> 276 fState.GetVerboseL4()->Message("set", "ntuple I column", description); >> 277 } >> 278 #endif >> 279 >> 280 G4CsvRNtupleDescription* ntupleDescription >> 281 = GetNtupleInFunction(ntupleId, "SetNtupleIColumn"); >> 282 if ( ! ntupleDescription ) return false; >> 283 >> 284 tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding; >> 285 ntupleBinding->add_column(columnName, vector); >> 286 >> 287 #ifdef G4VERBOSE >> 288 if ( fState.GetVerboseL2() ) { >> 289 G4ExceptionDescription description; >> 290 description << " ntupleId " << ntupleId << " " << columnName; >> 291 fState.GetVerboseL2()->Message("set", "ntuple I colum", description, true); >> 292 } >> 293 #endif >> 294 >> 295 return true; 77 } 296 } 78 297 79 //____________________________________________ 298 //_____________________________________________________________________________ 80 G4bool G4CsvRNtupleManager::GetTNtupleRow( << 299 G4bool G4CsvRNtupleManager::SetNtupleFColumn(G4int ntupleId, 81 G4TRNtupleDescription<tools::rcsv::ntuple>* << 300 const G4String& columnName, >> 301 std::vector<G4float>& vector) 82 { 302 { 83 auto ntuple = ntupleDescription->fNtuple; << 303 #ifdef G4VERBOSE >> 304 if ( fState.GetVerboseL4() ) { >> 305 G4ExceptionDescription description; >> 306 description << " ntupleId " << ntupleId << " " << columnName; >> 307 fState.GetVerboseL4()->Message("set", "ntuple F column of vector", description); >> 308 } >> 309 #endif >> 310 >> 311 G4CsvRNtupleDescription* ntupleDescription >> 312 = GetNtupleInFunction(ntupleId, "SetNtupleFColumn"); >> 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 F colum", description, true); >> 323 } >> 324 #endif 84 325 85 auto isInitialized = ntupleDescription->fIsI << 326 return true; >> 327 } >> 328 >> 329 //_____________________________________________________________________________ >> 330 G4bool G4CsvRNtupleManager::SetNtupleDColumn(G4int ntupleId, >> 331 const G4String& columnName, >> 332 std::vector<G4double>& vector) >> 333 { >> 334 #ifdef G4VERBOSE >> 335 if ( fState.GetVerboseL4() ) { >> 336 G4ExceptionDescription description; >> 337 description << " ntupleId " << ntupleId << " " << columnName; >> 338 fState.GetVerboseL4()->Message("set", "ntuple D column of vector", description); >> 339 } >> 340 #endif >> 341 >> 342 G4CsvRNtupleDescription* ntupleDescription >> 343 = GetNtupleInFunction(ntupleId, "SetNtupleDColumn"); >> 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 D colum", description, true); >> 354 } >> 355 #endif >> 356 >> 357 return true; >> 358 } >> 359 >> 360 //_____________________________________________________________________________ >> 361 G4bool G4CsvRNtupleManager::SetNtupleSColumn(G4int ntupleId, >> 362 const G4String& columnName, >> 363 G4String& value) >> 364 { >> 365 #ifdef G4VERBOSE >> 366 if ( fState.GetVerboseL4() ) { >> 367 G4ExceptionDescription description; >> 368 description << " ntupleId " << ntupleId << " " << columnName; >> 369 fState.GetVerboseL4()->Message("set", "ntuple S column", description); >> 370 } >> 371 #endif >> 372 >> 373 G4CsvRNtupleDescription* ntupleDescription >> 374 = GetNtupleInFunction(ntupleId, "SetNtupleSColumn"); >> 375 if ( ! ntupleDescription ) return false; >> 376 >> 377 tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding; >> 378 ntupleBinding->add_column(columnName, value); >> 379 >> 380 #ifdef G4VERBOSE >> 381 if ( fState.GetVerboseL2() ) { >> 382 G4ExceptionDescription description; >> 383 description << " ntupleId " << ntupleId << " " << columnName; >> 384 fState.GetVerboseL2()->Message("set", "ntuple S colum", description, true); >> 385 } >> 386 #endif >> 387 >> 388 return true; >> 389 } >> 390 >> 391 //_____________________________________________________________________________ >> 392 G4bool G4CsvRNtupleManager::GetNtupleRow() >> 393 { >> 394 return GetNtupleRow(fFirstId); >> 395 } >> 396 >> 397 //_____________________________________________________________________________ >> 398 G4bool G4CsvRNtupleManager::GetNtupleRow(G4int ntupleId) >> 399 { >> 400 >> 401 #ifdef G4VERBOSE >> 402 if ( fState.GetVerboseL4() ) { >> 403 G4ExceptionDescription description; >> 404 description << " ntupleId " << ntupleId; >> 405 fState.GetVerboseL4()->Message("get", "ntuple row", description); >> 406 } >> 407 #endif >> 408 >> 409 G4CsvRNtupleDescription* ntupleDescription >> 410 = GetNtupleInFunction(ntupleId, "GetNtupleRow"); >> 411 if ( ! ntupleDescription ) return false; >> 412 >> 413 tools::rcsv::ntuple* ntuple = ntupleDescription->fNtuple; >> 414 >> 415 G4bool isInitialized = ntupleDescription->fIsInitialized; 86 if ( ! isInitialized ) { 416 if ( ! isInitialized ) { 87 auto ntupleBinding = ntupleDescription->fN << 417 tools::ntuple_binding* ntupleBinding = ntupleDescription->fNtupleBinding; 88 if ( ! ntuple->initialize(G4cout, *ntupleB 418 if ( ! ntuple->initialize(G4cout, *ntupleBinding) ) { 89 Warn("Ntuple initialization failed !!", << 419 G4ExceptionDescription description; >> 420 description >> 421 << " " >> 422 << "Ntuple initialization failed !!"; >> 423 G4Exception("G4CsvRNtuple::GetNtupleRow()", >> 424 "Analysis_WR021", JustWarning, description); 90 return false; 425 return false; 91 } 426 } 92 ntupleDescription->fIsInitialized = true; 427 ntupleDescription->fIsInitialized = true; 93 ntuple->start(); 428 ntuple->start(); 94 } 429 } 95 430 96 auto next = ntuple->next(); << 431 G4bool next = ntuple->next(); 97 if ( next ) { 432 if ( next ) { 98 if ( ! ntuple->get_row() ) { 433 if ( ! ntuple->get_row() ) { 99 Warn("Ntuple get_row() failed !!", fkCla << 434 G4ExceptionDescription description; >> 435 description >> 436 << " " >> 437 << "Ntuple get_row() failed !!"; >> 438 G4Exception("G4CsvRNtuple::GetNtupleRow()", >> 439 "Analysis_WR021", JustWarning, description); 100 return false; 440 return false; 101 } 441 } 102 } << 442 } >> 443 >> 444 #ifdef G4VERBOSE >> 445 if ( fState.GetVerboseL2() ) { >> 446 G4ExceptionDescription description; >> 447 description << " ntupleId " << ntupleId; >> 448 fState.GetVerboseL2()->Message("get", "ntuple row", description, true); >> 449 } >> 450 #endif 103 451 104 return next; 452 return next; 105 } << 453 } 106 454