65 if ( fgInstance ==
nullptr ) {
76 return ( fgInstance != 0 );
84 fNtupleManager(nullptr),
85 fSlaveNtupleManager(nullptr),
88 if ( ( isMaster && fgMasterInstance ) || ( fgInstance ) ) {
92 <<
"G4RootAnalysisManager already exists."
93 <<
"Cannot create another instance.";
94 G4Exception(
"G4RootAnalysisManager::G4RootAnalysisManager()",
97 if ( isMaster ) fgMasterInstance =
this;
101 fFileManager = std::make_shared<G4RootFileManager>(
fState);
103 fFileManager->SetBasketSize(fgkDefaultBasketSize);
108 auto mergeNtuples =
false;
109 SetNtupleMergingMode(mergeNtuples, fNofNtupleFiles);
112 CreateNtupleManagers();
119 fgInstance =
nullptr;
127 void G4RootAnalysisManager::SetNtupleMergingMode(
G4bool mergeNtuples,
128 G4int nofNtupleFiles)
134 ->
Message(
"set",
"ntuple merging mode",
"");
137 auto canMerge =
true;
141 if ( nofNtupleFiles > 0 ) {
144 <<
" " <<
"Merging ntuples is not applicable in sequential application."
146 <<
" " <<
"Setting was ignored.";
147 G4Exception(
"G4RootAnalysisManager::SetNtupleMergingMode()",
155 ( ! fgMasterInstance ) ) {
158 <<
" " <<
"Merging ntuples requires G4AnalysisManager instance on master."
160 <<
" " <<
"Setting was ignored.";
161 G4Exception(
"G4RootAnalysisManager::SetNtupleMergingMode()",
167 if ( ( ! mergeNtuples ) || ( ! canMerge ) ) {
169 mergingMode =
"G4NtupleMergeMode::kNone";
174 fNofNtupleFiles = nofNtupleFiles;
178 if ( fNofNtupleFiles < 0 ) {
181 <<
" " <<
"Number of reduced files must be [0, nofThreads]."
183 <<
" " <<
"Cannot set " << nofNtupleFiles
186 <<
" " <<
"Ntuples will be merged in a single file.";
187 G4Exception(
"G4RootAnalysisManager::SetNtupleMergingMode()",
213 mergingMode =
"G4NtupleMergeMode::kMain";
216 mergingMode =
"G4NtupleMergeMode::kSlave";
223 ->
Message(
"set",
"ntuple merging mode", mergingMode);
228 void G4RootAnalysisManager::ClearNtupleManagers()
237 delete fNtupleManager;
238 fNtupleManager =
nullptr;
242 delete fSlaveNtupleManager;
243 fSlaveNtupleManager =
nullptr;
252 void G4RootAnalysisManager::CreateNtupleManagers()
259 switch ( fNtupleMergeMode )
263 fNtupleManager->SetFileManager(fFileManager);
268 G4int nofMainManagers = fNofNtupleFiles;
269 if ( ! nofMainManagers ) nofMainManagers = 1;
272 fNtupleManager->SetFileManager(fFileManager);
278 fNtupleManager = fgMasterInstance->fNtupleManager;
280 auto mainNtupleManager
281 = fNtupleManager->GetMainNtupleManager(GetNtupleFileNumber());
294 G4int G4RootAnalysisManager::GetNtupleFileNumber()
296 if ( ! fNofNtupleFiles )
return 0;
298 G4int nofMainManagers = fNofNtupleFiles;
299 if ( ! nofMainManagers ) nofMainManagers = 1;
302 G4cout <<
"In GetNtupleFileNumber: "
310 G4bool G4RootAnalysisManager::WriteH1()
315 if ( ! h1Vector.size() )
return true;
320 auto directory = fFileManager->GetHistoDirectory();
321 result = WriteT(h1Vector, hnVector, directory,
"h1");
335 G4bool G4RootAnalysisManager::WriteH2()
340 if ( ! h2Vector.size() )
return true;
345 auto directory = fFileManager->GetHistoDirectory();
346 result = WriteT(h2Vector, hnVector, directory,
"h2");
360 G4bool G4RootAnalysisManager::WriteH3()
365 if ( ! h3Vector.size() )
return true;
370 auto directory = fFileManager->GetHistoDirectory();
371 result = WriteT(h3Vector, hnVector, directory,
"h3");
385 G4bool G4RootAnalysisManager::WriteP1()
390 if ( ! p1Vector.size() )
return true;
395 auto directory = fFileManager->GetHistoDirectory();
396 result = WriteT(p1Vector, hnVector, directory,
"p1");
410 G4bool G4RootAnalysisManager::WriteP2()
415 if ( ! p2Vector.size() )
return true;
420 auto directory = fFileManager->GetHistoDirectory();
421 result = WriteT(p2Vector, hnVector, directory,
"p2");
435 G4bool G4RootAnalysisManager::WriteNtuple()
439 auto finalResult =
true;
451 auto result = fNtupleManager->Merge();
452 finalResult =
result && finalResult;
456 auto result = fSlaveNtupleManager->Merge();
457 finalResult =
result && finalResult;
469 G4bool G4RootAnalysisManager::Reset()
473 auto finalResult =
true;
476 finalResult = finalResult &&
result;
480 result = fNtupleManager->Reset(
false);
481 finalResult = result && finalResult;
484 finalResult = finalResult &&
result;
496 auto finalResult =
true;
497 auto result = fFileManager->SetFileName(fileName);
498 finalResult = finalResult &&
result;
508 result = fFileManager->OpenFile(fileName);
509 finalResult = finalResult &&
result;
511 fNtupleManager->SetNtupleDirectory(fFileManager->GetNtupleDirectory());
529 fFileManager->SetNofNtupleFiles(fNofNtupleFiles);
530 result = fFileManager->OpenFile(fileName);
531 finalResult = finalResult &&
result;
543 fSlaveNtupleManager->CreateNtuplesFromMain();
553 auto finalResult =
true;
555 if ( ! fgMasterInstance &&
561 <<
" " <<
"No master G4RootAnalysisManager instance exists."
563 <<
" " <<
"Histogram/profile data will not be merged.";
569 auto result = WriteH1();
570 finalResult = finalResult &&
result;
574 finalResult = finalResult &&
result;
578 finalResult = finalResult &&
result;
582 finalResult = finalResult &&
result;
586 finalResult = finalResult &&
result;
589 result = WriteNtuple();
590 finalResult = finalResult &&
result;
594 result = fFileManager->WriteFile();
595 finalResult = finalResult &&
result;
600 result =
WriteAscii(fFileManager->GetFileName());
601 finalResult = finalResult &&
result;
610 auto finalResult =
true;
613 auto result = Reset();
616 description <<
" " <<
"Resetting data failed";
620 finalResult = finalResult &&
result;
624 fFileManager->CloseFile();
636 result = ! std::remove(fFileManager->GetFullFileName());
640 description <<
" " <<
"Removing file "
641 << fFileManager->GetFullFileName() <<
" failed";
645 finalResult = finalResult &&
result;
649 ->
Message(
"delete",
"empty file", fFileManager->GetFullFileName());
662 G4int nofNtupleFiles,
663 unsigned int basketSize)
667 fFileManager->SetBasketSize(basketSize);
670 SetNtupleMergingMode(mergeNtuples, nofNtupleFiles);
673 ClearNtupleManagers();
676 CreateNtupleManagers();
G4double G4ParticleHPJENDLHEData::G4double result
virtual G4bool OpenFileImpl(const G4String &fileName) final
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
std::ostringstream G4ExceptionDescription
G4int GetNumberOfRunningWorkerThreads()
virtual G4bool CloseFileImpl() final
void CreateNtuplesFromBooking()
static G4bool IsInstance()
#define G4MUTEX_INITIALIZER
const G4AnalysisVerbose * GetVerboseL2() const
const G4AnalysisVerbose * GetVerboseL3() const
G4GLOB_DLL std::ostream G4cout
const G4AnalysisVerbose * GetVerboseL4() const
G4RootAnalysisManager(G4bool isMaster=true)
virtual ~G4RootAnalysisManager()
G4bool IsMultithreadedApplication()
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
void SetFileManager(std::shared_ptr< G4VFileManager > fileManager)
virtual G4bool WriteImpl() final
void SetNtupleMerging(G4bool mergeNtuples, G4int nofReducedNtupleFiles=0, unsigned int basketSize=fgkDefaultBasketSize)
static G4RootAnalysisManager * Instance()
G4bool GetIsMaster() const
G4AnalysisManagerState fState
const G4AnalysisVerbose * GetVerboseL1() const
void SetNtupleManager(G4VNtupleManager *ntupleManager)
G4bool WriteAscii(const G4String &fileName)