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