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: G4CsvAnalysisReader.cc 74257 2013-10-02 14:24:55Z gcosmo $ 26 27 27 // Author: Ivana Hrivnacova, 05/09/2014 (ivana 28 // Author: Ivana Hrivnacova, 05/09/2014 (ivana@ipno.in2p3.fr) 28 29 29 #include "G4CsvAnalysisReader.hh" 30 #include "G4CsvAnalysisReader.hh" 30 #include "G4CsvRFileManager.hh" 31 #include "G4CsvRFileManager.hh" >> 32 #include "G4H1ToolsManager.hh" >> 33 #include "G4H2ToolsManager.hh" >> 34 #include "G4H3ToolsManager.hh" >> 35 #include "G4P1ToolsManager.hh" >> 36 #include "G4P2ToolsManager.hh" 31 #include "G4CsvRNtupleManager.hh" 37 #include "G4CsvRNtupleManager.hh" 32 #include "G4ThreadLocalSingleton.hh" << 38 #include "G4CsvRNtupleDescription.hh" >> 39 #include "G4AnalysisVerbose.hh" >> 40 #include "G4AnalysisUtilities.hh" 33 #include "G4Threading.hh" 41 #include "G4Threading.hh" 34 42 >> 43 #include <tools/aida_ntuple> >> 44 #include <tools/rcsv_histo> >> 45 >> 46 #include <iostream> >> 47 #include <cstdio> >> 48 35 using namespace G4Analysis; 49 using namespace G4Analysis; 36 50 >> 51 G4CsvAnalysisReader* G4CsvAnalysisReader::fgMasterInstance = 0; >> 52 G4ThreadLocal G4CsvAnalysisReader* G4CsvAnalysisReader::fgInstance = 0; >> 53 >> 54 // >> 55 // utility functions >> 56 // >> 57 >> 58 namespace { >> 59 37 //____________________________________________ 60 //_____________________________________________________________________________ 38 G4CsvAnalysisReader* G4CsvAnalysisReader::Inst << 61 void* ReadObject(std::istream& hnFile, >> 62 const G4String& objectType, >> 63 const G4String& fileName, >> 64 const G4String& inFunction) 39 { 65 { 40 static G4ThreadLocalSingleton<G4CsvAnalysisR << 66 tools::rcsv::histo handler(hnFile); 41 return instance.Instance(); << 67 std::string objectTypeInFile; >> 68 void* object; >> 69 G4bool verbose = false; >> 70 if ( ! handler.read(G4cout, objectTypeInFile, object, verbose) ) { >> 71 G4ExceptionDescription description; >> 72 description >> 73 << " " >> 74 << "Cannot get "<< objectType << " in file " << fileName; >> 75 G4String inFunctionFull = "G4CsvAnalysisReader::"; >> 76 inFunctionFull.append(inFunction); >> 77 G4Exception(inFunctionFull, "Analysis_WR011", JustWarning, description); >> 78 return 0; >> 79 } >> 80 if ( objectTypeInFile != objectType ) { >> 81 G4ExceptionDescription description; >> 82 description >> 83 << " " >> 84 << "Object type read in "<< fileName >> 85 << " does not match" << G4endl; >> 86 G4String inFunctionFull = "G4CsvAnalysisReader::"; >> 87 inFunctionFull.append(inFunction); >> 88 G4Exception(inFunctionFull, "Analysis_WR011", JustWarning, description); >> 89 return 0; >> 90 } >> 91 >> 92 return object; >> 93 } >> 94 42 } 95 } 43 96 44 //____________________________________________ 97 //_____________________________________________________________________________ 45 G4CsvAnalysisReader::G4CsvAnalysisReader() << 98 G4CsvAnalysisReader* G4CsvAnalysisReader::Instance() 46 : G4ToolsAnalysisReader("Csv") << 47 { 99 { 48 if ( ! G4Threading::IsWorkerThread() ) fgMas << 100 if ( fgInstance == 0 ) { >> 101 G4bool isMaster = ! G4Threading::IsWorkerThread(); >> 102 fgInstance = new G4CsvAnalysisReader(isMaster); >> 103 } >> 104 >> 105 return fgInstance; >> 106 } 49 107 50 // Create managers << 108 //_____________________________________________________________________________ 51 fNtupleManager = std::make_shared<G4CsvRNtup << 109 G4CsvAnalysisReader::G4CsvAnalysisReader(G4bool isMaster) 52 fFileManager = std::make_shared<G4CsvRFileMa << 110 : G4VAnalysisReader("Csv", isMaster), 53 fNtupleManager->SetFileManager(fFileManager) << 111 fH1Manager(0), >> 112 fH2Manager(0), >> 113 fH3Manager(0), >> 114 fP1Manager(0), >> 115 fP2Manager(0), >> 116 fNtupleManager(0), >> 117 fFileManager(0) >> 118 { >> 119 if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) { >> 120 G4ExceptionDescription description; >> 121 description >> 122 << " " >> 123 << "G4CsvAnalysisReader already exists." >> 124 << "Cannot create another instance."; >> 125 G4Exception("G4CsvAnalysisReader::G4CsvAnalysisReader()", >> 126 "Analysis_F001", FatalException, description); >> 127 } >> 128 if ( isMaster ) fgMasterInstance = this; >> 129 fgInstance = this; 54 130 >> 131 // Create managers >> 132 fH1Manager = new G4H1ToolsManager(fState); >> 133 fH2Manager = new G4H2ToolsManager(fState); >> 134 fH3Manager = new G4H3ToolsManager(fState); >> 135 fP1Manager = new G4P1ToolsManager(fState); >> 136 fP2Manager = new G4P2ToolsManager(fState); >> 137 fNtupleManager = new G4CsvRNtupleManager(fState); >> 138 fFileManager = new G4CsvRFileManager(fState); >> 139 // The managers will be deleted by the base class >> 140 55 // Set managers to base class 141 // Set managers to base class >> 142 SetH1Manager(fH1Manager); >> 143 SetH2Manager(fH2Manager); >> 144 SetH3Manager(fH3Manager); >> 145 SetP1Manager(fP1Manager); >> 146 SetP2Manager(fP2Manager); 56 SetNtupleManager(fNtupleManager); 147 SetNtupleManager(fNtupleManager); 57 SetFileManager(fFileManager); 148 SetFileManager(fFileManager); 58 } 149 } 59 150 60 //____________________________________________ 151 //_____________________________________________________________________________ 61 G4CsvAnalysisReader::~G4CsvAnalysisReader() 152 G4CsvAnalysisReader::~G4CsvAnalysisReader() 62 { 153 { 63 if ( fState.GetIsMaster() ) fgMasterInstance << 154 if ( fState.GetIsMaster() ) fgMasterInstance = 0; >> 155 fgInstance = 0; 64 } 156 } 65 157 66 // << 158 // 67 // private methods 159 // private methods 68 // 160 // 69 161 70 //____________________________________________ 162 //_____________________________________________________________________________ >> 163 G4String G4CsvAnalysisReader::GetHnFileName( >> 164 const G4String& hnType, >> 165 const G4String& hnName, >> 166 const G4String& fileName, >> 167 G4bool isUserFileName) const >> 168 { >> 169 if ( isUserFileName ) { >> 170 return fFileManager->GetFullFileName(fileName); >> 171 } >> 172 else { >> 173 return fFileManager->GetHnFileName(hnType, hnName); >> 174 } >> 175 } >> 176 >> 177 //_____________________________________________________________________________ 71 G4bool G4CsvAnalysisReader::Reset() 178 G4bool G4CsvAnalysisReader::Reset() 72 { 179 { 73 // Reset histograms and ntuple 180 // Reset histograms and ntuple 74 181 75 auto result = true; << 182 G4bool finalResult = true; 76 << 183 77 result &= G4ToolsAnalysisReader::Reset(); << 184 G4bool result = fH1Manager->Reset(); 78 result &= fNtupleManager->Reset(); << 185 finalResult = finalResult && result; 79 << 186 80 return result; << 187 result = fH2Manager->Reset(); 81 } << 188 finalResult = finalResult && result; 82 << 189 83 // << 190 result = fNtupleManager->Reset(); >> 191 finalResult = finalResult && result; >> 192 >> 193 return finalResult; >> 194 } >> 195 >> 196 // 84 // protected methods 197 // protected methods 85 // 198 // 86 199 87 //____________________________________________ 200 //_____________________________________________________________________________ 88 G4bool G4CsvAnalysisReader::CloseFilesImpl(G4 << 201 G4int G4CsvAnalysisReader::ReadH1Impl(const G4String& h1Name, >> 202 const G4String& fileName, >> 203 G4bool isUserFileName) 89 { 204 { 90 Message(kVL4, "close", "files"); << 205 #ifdef G4VERBOSE >> 206 if ( fState.GetVerboseL4() ) >> 207 fState.GetVerboseL4()->Message("get", "h1", h1Name); >> 208 #endif >> 209 >> 210 // open file >> 211 G4String h1FileName = GetHnFileName("h1", h1Name, fileName, isUserFileName); >> 212 std::ifstream hnFile(h1FileName); >> 213 if ( ! hnFile.is_open() ) { >> 214 G4ExceptionDescription description; >> 215 description << " " << "Cannot open file " << h1FileName; >> 216 G4Exception("G4CsvAnalysisReader::ReadH1Impl()", >> 217 "Analysis_WR001", JustWarning, description); >> 218 return kInvalidId; >> 219 } >> 220 #ifdef G4VERBOSE >> 221 if ( fState.GetVerboseL1() ) >> 222 fState.GetVerboseL1() >> 223 ->Message("open", "read file", h1FileName); >> 224 #endif >> 225 >> 226 void* object >> 227 = ReadObject(hnFile, tools::histo::h1d::s_class(), h1FileName, "ReadH1Impl"); >> 228 if ( ! object ) return kInvalidId; >> 229 >> 230 tools::histo::h1d* h1 >> 231 = static_cast<tools::histo::h1d*>(object); >> 232 G4int id = fH1Manager->AddH1(h1Name, h1); >> 233 >> 234 #ifdef G4VERBOSE >> 235 if ( fState.GetVerboseL2() ) >> 236 fState.GetVerboseL2()->Message("read", "h1", h1Name, id > kInvalidId); >> 237 #endif >> 238 >> 239 return id; >> 240 } 91 241 92 auto result = true; << 242 //_____________________________________________________________________________ >> 243 G4int G4CsvAnalysisReader::ReadH2Impl(const G4String& h2Name, >> 244 const G4String& fileName, >> 245 G4bool isUserFileName) >> 246 { >> 247 #ifdef G4VERBOSE >> 248 if ( fState.GetVerboseL4() ) >> 249 fState.GetVerboseL4()->Message("read", "h2", h2Name); >> 250 #endif >> 251 >> 252 // open file >> 253 G4String h2FileName = GetHnFileName("h2", h2Name, fileName, isUserFileName); >> 254 std::ifstream hnFile(h2FileName); >> 255 if ( ! hnFile.is_open() ) { >> 256 G4ExceptionDescription description; >> 257 description << " " << "Cannot open file " << h2FileName; >> 258 G4Exception("G4CsvAnalysisReader::ReadH2Impl()", >> 259 "Analysis_WR001", JustWarning, description); >> 260 return kInvalidId; >> 261 } >> 262 #ifdef G4VERBOSE >> 263 if ( fState.GetVerboseL1() ) >> 264 fState.GetVerboseL1() >> 265 ->Message("open", "read file", h2FileName); >> 266 #endif >> 267 >> 268 void* object >> 269 = ReadObject(hnFile, tools::histo::h2d::s_class(), h2FileName, "ReadH2Impl"); >> 270 if ( ! object ) return kInvalidId; >> 271 >> 272 tools::histo::h2d* h2 >> 273 = static_cast<tools::histo::h2d*>(object); >> 274 G4int id = fH2Manager->AddH2(h2Name, h2); >> 275 >> 276 #ifdef G4VERBOSE >> 277 if ( fState.GetVerboseL2() ) >> 278 fState.GetVerboseL2()->Message("read", "h2", h2Name, id > kInvalidId); >> 279 #endif >> 280 >> 281 return id; >> 282 } 93 283 94 if (reset) { << 284 //_____________________________________________________________________________ 95 result &= Reset(); << 285 G4int G4CsvAnalysisReader::ReadH3Impl(const G4String& h3Name, >> 286 const G4String& fileName, >> 287 G4bool isUserFileName) >> 288 { >> 289 #ifdef G4VERBOSE >> 290 if ( fState.GetVerboseL4() ) >> 291 fState.GetVerboseL4()->Message("read", "h3", h3Name); >> 292 #endif >> 293 >> 294 // open file >> 295 G4String h3FileName = GetHnFileName("h3", h3Name, fileName, isUserFileName); >> 296 std::ifstream hnFile(h3FileName); >> 297 if ( ! hnFile.is_open() ) { >> 298 G4ExceptionDescription description; >> 299 description << " " << "Cannot open file " << h3FileName; >> 300 G4Exception("G4CsvAnalysisReader::ReadH3Impl()", >> 301 "Analysis_WR001", JustWarning, description); >> 302 return kInvalidId; 96 } 303 } >> 304 #ifdef G4VERBOSE >> 305 if ( fState.GetVerboseL1() ) >> 306 fState.GetVerboseL1() >> 307 ->Message("open", "read file", h3FileName); >> 308 #endif >> 309 >> 310 void* object >> 311 = ReadObject(hnFile, tools::histo::h3d::s_class(), h3FileName, "ReadH3Impl"); >> 312 if ( ! object ) return kInvalidId; >> 313 >> 314 tools::histo::h3d* h3 >> 315 = static_cast<tools::histo::h3d*>(object); >> 316 G4int id = fH3Manager->AddH3(h3Name, h3); >> 317 >> 318 #ifdef G4VERBOSE >> 319 if ( fState.GetVerboseL2() ) >> 320 fState.GetVerboseL2()->Message("read", "h3", h3Name, id > kInvalidId); >> 321 #endif >> 322 >> 323 return id; >> 324 } 97 325 98 fFileManager->CloseFiles(); << 326 //_____________________________________________________________________________ >> 327 G4int G4CsvAnalysisReader::ReadP1Impl(const G4String& p1Name, >> 328 const G4String& fileName, >> 329 G4bool isUserFileName) >> 330 { >> 331 #ifdef G4VERBOSE >> 332 if ( fState.GetVerboseL4() ) >> 333 fState.GetVerboseL4()->Message("read", "p1", p1Name); >> 334 #endif >> 335 >> 336 // open file >> 337 G4String p1FileName = GetHnFileName("p1", p1Name, fileName, isUserFileName); >> 338 std::ifstream hnFile(p1FileName); >> 339 if ( ! hnFile.is_open() ) { >> 340 G4ExceptionDescription description; >> 341 description << " " << "Cannot open file " << p1FileName; >> 342 G4Exception("G4CsvAnalysisReader::ReadP1Impl()", >> 343 "Analysis_WR001", JustWarning, description); >> 344 return kInvalidId; >> 345 } >> 346 #ifdef G4VERBOSE >> 347 if ( fState.GetVerboseL1() ) >> 348 fState.GetVerboseL1() >> 349 ->Message("open", "read file", p1FileName); >> 350 #endif >> 351 >> 352 void* object >> 353 = ReadObject(hnFile, tools::histo::p1d::s_class(), fileName, "ReadP1Impl"); >> 354 if ( ! object ) return kInvalidId; >> 355 >> 356 tools::histo::p1d* p1 >> 357 = static_cast<tools::histo::p1d*>(object); >> 358 G4int id = fP1Manager->AddP1(p1Name, p1); >> 359 >> 360 #ifdef G4VERBOSE >> 361 if ( fState.GetVerboseL2() ) >> 362 fState.GetVerboseL2()->Message("read", "p1", p1Name, id > kInvalidId); >> 363 #endif >> 364 >> 365 return id; >> 366 } 99 367 100 Message(kVL2, "close", "files", "", result); << 368 //_____________________________________________________________________________ >> 369 G4int G4CsvAnalysisReader::ReadP2Impl(const G4String& p2Name, >> 370 const G4String& fileName, >> 371 G4bool isUserFileName) >> 372 { >> 373 #ifdef G4VERBOSE >> 374 if ( fState.GetVerboseL4() ) >> 375 fState.GetVerboseL4()->Message("read", "p2", p2Name); >> 376 #endif >> 377 >> 378 // open file >> 379 G4String p2FileName = GetHnFileName("p2", p2Name, fileName, isUserFileName); >> 380 std::ifstream hnFile(p2FileName); >> 381 if ( ! hnFile.is_open() ) { >> 382 G4ExceptionDescription description; >> 383 description << " " << "Cannot open file " << p2FileName; >> 384 G4Exception("G4CsvAnalysisReader::ReadP2Impl()", >> 385 "Analysis_WR001", JustWarning, description); >> 386 return kInvalidId; >> 387 } >> 388 #ifdef G4VERBOSE >> 389 if ( fState.GetVerboseL1() ) >> 390 fState.GetVerboseL1() >> 391 ->Message("open", "read file", p2FileName); >> 392 #endif >> 393 >> 394 void* object >> 395 = ReadObject(hnFile, tools::histo::p2d::s_class(), p2FileName, "ReadP2Impl"); >> 396 if ( ! object ) return kInvalidId; >> 397 >> 398 tools::histo::p2d* p2 >> 399 = static_cast<tools::histo::p2d*>(object); >> 400 G4int id = fP2Manager->AddP2(p2Name, p2); >> 401 >> 402 >> 403 #ifdef G4VERBOSE >> 404 if ( fState.GetVerboseL2() ) >> 405 fState.GetVerboseL2()->Message("read", "p2", p2Name, id > kInvalidId); >> 406 #endif >> 407 >> 408 return id; >> 409 } 101 410 102 return result; << 411 //_____________________________________________________________________________ 103 } << 412 G4int G4CsvAnalysisReader::ReadNtupleImpl(const G4String& ntupleName, >> 413 const G4String& fileName, >> 414 G4bool isUserFileName) >> 415 { >> 416 #ifdef G4VERBOSE >> 417 if ( fState.GetVerboseL4() ) >> 418 fState.GetVerboseL4()->Message("read", "ntuple", ntupleName); >> 419 #endif >> 420 >> 421 // Ntuples are saved per object and per thread >> 422 // but apply the ntuple name and the thread suffixes >> 423 // only if fileName is not provided explicitly >> 424 G4String fullFileName = fileName; >> 425 if ( ! isUserFileName ) { >> 426 fullFileName = fFileManager->GetNtupleFileName(ntupleName); >> 427 } >> 428 >> 429 // Open file >> 430 if ( ! fFileManager->OpenRFile(fullFileName) ) return kInvalidId; >> 431 std::ifstream* ntupleFile >> 432 = fFileManager->GetRFile(fullFileName); >> 433 >> 434 // Create ntuple >> 435 tools::rcsv::ntuple* rntuple = new tools::rcsv::ntuple(*ntupleFile); >> 436 G4int id >> 437 = fNtupleManager->SetNtuple(new G4CsvRNtupleDescription(rntuple)); >> 438 >> 439 #ifdef G4VERBOSE >> 440 if ( fState.GetVerboseL2() ) >> 441 fState.GetVerboseL2()->Message("read", "ntuple", ntupleName, id > kInvalidId); >> 442 #endif >> 443 >> 444 return id; >> 445 } 104 446