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: G4RootAnalysisReader.cc 74257 2013-10-02 14:24:55Z gcosmo $ 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 "G4RootAnalysisReader.hh" 30 #include "G4RootAnalysisReader.hh" 30 #include "G4RootRFileManager.hh" 31 #include "G4RootRFileManager.hh" 31 #include "G4RootRNtupleManager.hh" 32 #include "G4RootRNtupleManager.hh" >> 33 #include "G4RootRNtupleDescription.hh" >> 34 #include "G4AnalysisVerbose.hh" 32 #include "G4AnalysisUtilities.hh" 35 #include "G4AnalysisUtilities.hh" 33 #include "G4ThreadLocalSingleton.hh" << 34 #include "G4Threading.hh" 36 #include "G4Threading.hh" 35 37 >> 38 #include <tools/rroot/file> >> 39 #include <tools/rroot/streamers> >> 40 #include <tools/rroot/fac> >> 41 #include <tools/rroot/tree> >> 42 #include <tools/rroot/ntuple> >> 43 >> 44 #include <iostream> >> 45 #include <cstdio> >> 46 36 using namespace G4Analysis; 47 using namespace G4Analysis; 37 48 >> 49 G4RootAnalysisReader* G4RootAnalysisReader::fgMasterInstance = nullptr; >> 50 G4ThreadLocal G4RootAnalysisReader* G4RootAnalysisReader::fgInstance = nullptr; >> 51 38 //____________________________________________ 52 //_____________________________________________________________________________ 39 G4RootAnalysisReader* G4RootAnalysisReader::In 53 G4RootAnalysisReader* G4RootAnalysisReader::Instance() 40 { 54 { 41 static G4ThreadLocalSingleton<G4RootAnalysis << 55 if ( fgInstance == nullptr ) { 42 return instance.Instance(); << 56 G4bool isMaster = ! G4Threading::IsWorkerThread(); 43 } << 57 fgInstance = new G4RootAnalysisReader(isMaster); >> 58 } >> 59 >> 60 return fgInstance; >> 61 } 44 62 45 //____________________________________________ 63 //_____________________________________________________________________________ 46 G4RootAnalysisReader::G4RootAnalysisReader() << 64 G4RootAnalysisReader::G4RootAnalysisReader(G4bool isMaster) 47 : G4ToolsAnalysisReader("Root") << 65 : G4ToolsAnalysisReader("Root", isMaster), >> 66 fNtupleManager(nullptr), >> 67 fFileManager(nullptr) 48 { 68 { 49 if ( ! G4Threading::IsWorkerThread() ) fgMas << 69 if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) { >> 70 G4ExceptionDescription description; >> 71 description >> 72 << " " >> 73 << "G4RootAnalysisReader already exists." >> 74 << "Cannot create another instance."; >> 75 G4Exception("G4RootAnalysisReader::G4RootAnalysisReader()", >> 76 "Analysis_F001", FatalException, description); >> 77 } >> 78 if ( isMaster ) fgMasterInstance = this; >> 79 fgInstance = this; 50 80 51 // Create managers 81 // Create managers 52 fNtupleManager = std::make_shared<G4RootRNtu << 82 fNtupleManager = new G4RootRNtupleManager(fState); 53 fFileManager = std::make_shared<G4RootRFileM << 83 fFileManager = new G4RootRFileManager(fState); 54 fNtupleManager->SetFileManager(fFileManager) << 84 // The managers will be deleted by the base class 55 << 85 56 // Set managers to base class 86 // Set managers to base class 57 SetNtupleManager(fNtupleManager); 87 SetNtupleManager(fNtupleManager); 58 SetFileManager(fFileManager); 88 SetFileManager(fFileManager); 59 } 89 } 60 90 61 //____________________________________________ 91 //_____________________________________________________________________________ 62 G4RootAnalysisReader::~G4RootAnalysisReader() 92 G4RootAnalysisReader::~G4RootAnalysisReader() 63 { 93 { 64 if ( fState.GetIsMaster() ) fgMasterInstance 94 if ( fState.GetIsMaster() ) fgMasterInstance = nullptr; >> 95 fgInstance = nullptr; 65 } 96 } 66 97 67 // << 98 // 68 // private methods 99 // private methods 69 // 100 // 70 101 71 //____________________________________________ 102 //_____________________________________________________________________________ >> 103 tools::rroot::buffer* G4RootAnalysisReader::GetBuffer( >> 104 const G4String& fileName, >> 105 const G4String& objectName, >> 106 const G4String& inFunction) >> 107 { >> 108 // Get buffer for reading histogram or profile specified by objectNmae >> 109 // for a file specified by fileName; >> 110 // open the file if it was not yet open >> 111 >> 112 // Histograms and profiles are not saved per thread >> 113 G4bool isPerThread = false; >> 114 >> 115 // Get or open a file >> 116 auto rfile = fFileManager->GetRFile(fileName, isPerThread); >> 117 if ( ! rfile ) { >> 118 if ( ! fFileManager->OpenRFile(fileName, isPerThread) ) return nullptr; >> 119 rfile = fFileManager->GetRFile(fileName, isPerThread); >> 120 } >> 121 >> 122 auto key >> 123 = ( ! rfile ) ? nullptr : rfile->dir().find_key(objectName); >> 124 >> 125 unsigned int size; >> 126 //char* charBuffer >> 127 // = ( ! key ) ? 0 : key->get_object_buffer(size); >> 128 char* charBuffer = 0; >> 129 if ( key ) charBuffer = key->get_object_buffer(size); >> 130 >> 131 if ( ! charBuffer ) { >> 132 G4ExceptionDescription description; >> 133 description >> 134 << " " >> 135 << "Cannot get " << objectName << " in file " << fileName; >> 136 G4Exception(inFunction, "Analysis_WR011", JustWarning, description); >> 137 return nullptr; >> 138 } >> 139 >> 140 auto verbose = false; >> 141 return new tools::rroot::buffer(G4cout, rfile->byte_swap(), size, charBuffer, >> 142 key->key_length(), verbose); >> 143 } >> 144 >> 145 //_____________________________________________________________________________ 72 G4bool G4RootAnalysisReader::Reset() 146 G4bool G4RootAnalysisReader::Reset() 73 { 147 { 74 // Reset histograms and ntuple 148 // Reset histograms and ntuple 75 149 76 auto result = true; << 150 auto finalResult = true; >> 151 >> 152 auto result = G4ToolsAnalysisReader::Reset(); >> 153 finalResult = finalResult && result; >> 154 >> 155 result = fNtupleManager->Reset(); >> 156 finalResult = finalResult && result; >> 157 >> 158 return finalResult; >> 159 } >> 160 >> 161 // >> 162 // protected methods >> 163 // 77 164 78 result &= G4ToolsAnalysisReader::Reset(); << 165 //_____________________________________________________________________________ 79 result &= fNtupleManager->Reset(); << 166 G4int G4RootAnalysisReader::ReadH1Impl(const G4String& h1Name, >> 167 const G4String& fileName, >> 168 G4bool /*isUserFileName*/) >> 169 { >> 170 #ifdef G4VERBOSE >> 171 if ( fState.GetVerboseL4() ) >> 172 fState.GetVerboseL4()->Message("read", "h1", h1Name); >> 173 #endif >> 174 >> 175 auto buffer = GetBuffer(fileName, h1Name, "ReadH1Impl"); >> 176 if ( ! buffer ) return kInvalidId; >> 177 >> 178 auto h1 = tools::rroot::TH1D_stream(*buffer); >> 179 delete buffer; >> 180 >> 181 if ( ! h1 ) { >> 182 G4ExceptionDescription description; >> 183 description >> 184 << " " >> 185 << "Streaming " << h1Name << " in file " << fileName << " failed."; >> 186 G4Exception("G4RootAnalysisReader::ReadH1Impl", >> 187 "Analysis_WR011", JustWarning, description); >> 188 return kInvalidId; >> 189 } >> 190 >> 191 auto id = fH1Manager->AddH1(h1Name, h1); >> 192 >> 193 #ifdef G4VERBOSE >> 194 if ( fState.GetVerboseL2() ) >> 195 fState.GetVerboseL2()->Message("read", "h1", h1Name, id > kInvalidId); >> 196 #endif >> 197 >> 198 return id; >> 199 } 80 200 81 return result; << 201 //_____________________________________________________________________________ 82 } << 202 G4int G4RootAnalysisReader::ReadH2Impl(const G4String& h2Name, >> 203 const G4String& fileName, >> 204 G4bool /*isUserFileName*/) >> 205 { >> 206 #ifdef G4VERBOSE >> 207 if ( fState.GetVerboseL4() ) >> 208 fState.GetVerboseL4()->Message("read", "h2", h2Name); >> 209 #endif >> 210 >> 211 auto buffer = GetBuffer(fileName, h2Name, "ReadH2Impl"); >> 212 if ( ! buffer ) return kInvalidId; >> 213 >> 214 // if h2Name represents H1, then we get !! >> 215 // tools::rroot::buffer::check_byte_count : object of class "TNamed" read too few bytes (603979762 missing). >> 216 // tools::rroot::buffer::check_byte_count : "TNamed" streamer not in sync with data on file, fix streamer. >> 217 // Segmentation fault (core dumped) >> 218 >> 219 auto h2 = tools::rroot::TH2D_stream(*buffer); >> 220 delete buffer; >> 221 >> 222 if ( ! h2 ) { >> 223 G4ExceptionDescription description; >> 224 description >> 225 << " " >> 226 << "Streaming " << h2Name << " in file " << fileName << " failed."; >> 227 G4Exception("G4RootAnalysisReader::ReadH2Impl", >> 228 "Analysis_WR011", JustWarning, description); >> 229 return kInvalidId; >> 230 } >> 231 >> 232 auto id = fH2Manager->AddH2(h2Name, h2); >> 233 >> 234 #ifdef G4VERBOSE >> 235 if ( fState.GetVerboseL2() ) >> 236 fState.GetVerboseL2()->Message("read", "h2", h2Name, id > kInvalidId); >> 237 #endif >> 238 >> 239 return id; >> 240 } 83 241 84 // << 242 //_____________________________________________________________________________ 85 // protected methods << 243 G4int G4RootAnalysisReader::ReadH3Impl(const G4String& h3Name, 86 // << 244 const G4String& fileName, >> 245 G4bool /*isUserFileName*/) >> 246 { >> 247 >> 248 #ifdef G4VERBOSE >> 249 if ( fState.GetVerboseL4() ) >> 250 fState.GetVerboseL4()->Message("read", "h3", h3Name); >> 251 #endif >> 252 >> 253 auto buffer = GetBuffer(fileName, h3Name, "ReadH3Impl"); >> 254 if ( ! buffer ) return kInvalidId; >> 255 >> 256 auto h3 = tools::rroot::TH3D_stream(*buffer); >> 257 delete buffer; >> 258 >> 259 if ( ! h3 ) { >> 260 G4ExceptionDescription description; >> 261 description >> 262 << " " >> 263 << "Streaming " << h3Name << " in file " << fileName << " failed."; >> 264 G4Exception("G4RootAnalysisReader::ReadH3Impl", >> 265 "Analysis_WR011", JustWarning, description); >> 266 return kInvalidId; >> 267 } >> 268 >> 269 auto id = fH3Manager->AddH3(h3Name, h3); >> 270 >> 271 #ifdef G4VERBOSE >> 272 if ( fState.GetVerboseL2() ) >> 273 fState.GetVerboseL2()->Message("read", "h3", h3Name, id > kInvalidId); >> 274 #endif >> 275 >> 276 return id; >> 277 /* >> 278 // not yet available >> 279 return kInvalidId; >> 280 */ >> 281 } 87 282 88 //____________________________________________ 283 //_____________________________________________________________________________ 89 G4bool G4RootAnalysisReader::CloseFilesImpl(G << 284 G4int G4RootAnalysisReader::ReadP1Impl(const G4String& p1Name, >> 285 const G4String& fileName, >> 286 G4bool /*isUserFileName*/) 90 { 287 { 91 Message(kVL4, "close", "files"); << 288 #ifdef G4VERBOSE >> 289 if ( fState.GetVerboseL4() ) >> 290 fState.GetVerboseL4()->Message("read", "p1", p1Name); >> 291 #endif >> 292 >> 293 auto buffer = GetBuffer(fileName, p1Name, "ReadP1Impl"); >> 294 if ( ! buffer ) return kInvalidId; >> 295 >> 296 auto p1 = tools::rroot::TProfile_stream(*buffer); >> 297 delete buffer; >> 298 >> 299 if ( ! p1 ) { >> 300 G4ExceptionDescription description; >> 301 description >> 302 << " " >> 303 << "Streaming " << p1Name << " in file " << fileName << " failed."; >> 304 G4Exception("G4RootAnalysisReader::ReadP1Impl", >> 305 "Analysis_WR011", JustWarning, description); >> 306 return kInvalidId; >> 307 } >> 308 >> 309 auto id = fP1Manager->AddP1(p1Name, p1); >> 310 >> 311 #ifdef G4VERBOSE >> 312 if ( fState.GetVerboseL2() ) >> 313 fState.GetVerboseL2()->Message("read", "p1", p1Name, id > kInvalidId); >> 314 #endif >> 315 >> 316 return id; >> 317 } 92 318 93 auto result = true; << 319 //_____________________________________________________________________________ >> 320 G4int G4RootAnalysisReader::ReadP2Impl(const G4String& p2Name, >> 321 const G4String& fileName, >> 322 G4bool /*isUserFileName*/) >> 323 { 94 324 95 if (reset) { << 325 #ifdef G4VERBOSE 96 result &= Reset(); << 326 if ( fState.GetVerboseL4() ) 97 } << 327 fState.GetVerboseL4()->Message("read", "p2", p2Name); >> 328 #endif >> 329 >> 330 auto buffer = GetBuffer(fileName, p2Name, "ReadP2Impl"); >> 331 if ( ! buffer ) return kInvalidId; >> 332 >> 333 auto p2 = tools::rroot::TProfile2D_stream(*buffer); >> 334 delete buffer; >> 335 >> 336 if ( ! p2 ) { >> 337 G4ExceptionDescription description; >> 338 description >> 339 << " " >> 340 << "Streaming " << p2Name << " in file " << fileName << " failed."; >> 341 G4Exception("G4RootAnalysisReader::ReadP2Impl", >> 342 "Analysis_WR011", JustWarning, description); >> 343 return kInvalidId; >> 344 } >> 345 >> 346 auto id = fP2Manager->AddP2(p2Name, p2); >> 347 >> 348 #ifdef G4VERBOSE >> 349 if ( fState.GetVerboseL2() ) >> 350 fState.GetVerboseL2()->Message("read", "p2", p2Name, id > kInvalidId); >> 351 #endif >> 352 >> 353 return id; >> 354 } 98 355 99 fFileManager->CloseFiles(); << 356 //_____________________________________________________________________________ >> 357 G4int G4RootAnalysisReader::ReadNtupleImpl(const G4String& ntupleName, >> 358 const G4String& fileName, >> 359 G4bool isUserFileName) >> 360 { >> 361 #ifdef G4VERBOSE >> 362 if ( fState.GetVerboseL4() ) >> 363 fState.GetVerboseL4()->Message("read", "ntuple", ntupleName); >> 364 #endif >> 365 >> 366 // Ntuples are saved per thread >> 367 // but do not apply the thread suffix if fileName is provided explicitly >> 368 auto isPerThread = true; >> 369 if ( isUserFileName ) isPerThread = false; >> 370 >> 371 // Get or open a file >> 372 auto rfile = fFileManager->GetRFile(fileName, isPerThread); >> 373 if ( ! rfile ) { >> 374 if ( ! fFileManager->OpenRFile(fileName, isPerThread) ) return kInvalidId; >> 375 rfile = fFileManager->GetRFile(fileName, isPerThread); >> 376 } >> 377 >> 378 auto key = rfile->dir().find_key(ntupleName); >> 379 if ( ! key ) { >> 380 G4ExceptionDescription description; >> 381 description >> 382 << " " >> 383 << "Key " << ntupleName << " for Ntuple not found in file " << fileName; >> 384 G4Exception("G4RootAnalysisReader::ReadNtupleImpl()", >> 385 "Analysis_WR011", JustWarning, description); >> 386 return kInvalidId; >> 387 } 100 388 101 Message(kVL2, "close", "files", "", result); << 389 unsigned int size; >> 390 char* charBuffer = key->get_object_buffer(size); >> 391 if ( ! charBuffer ) { >> 392 G4ExceptionDescription description; >> 393 description >> 394 << " " >> 395 << "Cannot get data buffer for Ntuple " << ntupleName << " in file " << fileName; >> 396 G4Exception("G4RootAnalysisReader::ReadNtupleImpl()", >> 397 "Analysis_WR021", JustWarning, description); >> 398 return kInvalidId; >> 399 } >> 400 >> 401 auto verbose = false; >> 402 auto buffer >> 403 = new tools::rroot::buffer(G4cout, rfile->byte_swap(), size, charBuffer, >> 404 key->key_length(), verbose); >> 405 auto fac = new tools::rroot::fac(*rfile); >> 406 >> 407 auto tree = new tools::rroot::tree(*rfile, *fac); >> 408 if ( ! tree->stream(*buffer) ) { >> 409 G4ExceptionDescription description; >> 410 description >> 411 << " " >> 412 << "TTree streaming failed for Ntuple " << ntupleName << " in file " << fileName; >> 413 G4Exception("G4RootAnalysisReader::ReadNtupleImpl()", >> 414 "Analysis_WR021", JustWarning, description); >> 415 >> 416 delete buffer; >> 417 delete tree; >> 418 return kInvalidId; >> 419 } >> 420 >> 421 auto rntuple >> 422 = new tools::rroot::ntuple(*tree); //use the flat ntuple API. >> 423 auto rntupleDescription >> 424 = new G4RootRNtupleDescription(rntuple, buffer, fac, tree); >> 425 >> 426 auto id = fNtupleManager->SetNtuple(rntupleDescription); >> 427 >> 428 #ifdef G4VERBOSE >> 429 if ( fState.GetVerboseL2() ) >> 430 fState.GetVerboseL2()->Message("read", "ntuple", ntupleName, id > kInvalidId); >> 431 #endif 102 432 103 return result; << 433 return id; 104 } << 434 } 105 435