47void MergingException(
const G4String& functionName,
50 G4String inFunction =
"G4RootNtupleFileManager::" + functionName;
57using std::make_shared;
64 fIsInitialized(false),
66 fNtupleRowWise(false),
69 fNtupleManager(nullptr),
70 fSlaveNtupleManager(nullptr),
77 <<
"G4RootNtupleFileManager already exists."
78 <<
"Cannot create another instance.";
79 G4Exception(
"G4RootNtupleFileManager::G4RootNtupleFileManager()",
87 auto mergeNtuples =
false;
88 SetNtupleMergingMode(mergeNtuples, fNofNtupleFiles);
102void G4RootNtupleFileManager::SetNtupleMergingMode(
G4bool mergeNtuples,
103 G4int nofNtupleFiles)
112 auto canMerge =
true;
118 <<
"Merging ntuples is not applicable in sequential application." <<
G4endl
119 <<
"Setting was ignored.";
120 MergingException(
"SetNtupleMergingMode", description);
126 ( ! fgMasterInstance ) ) {
129 <<
"Merging ntuples requires G4AnalysisManager instance on master." <<
G4endl
130 <<
"Setting was ignored.";
131 MergingException(
"SetNtupleMergingMode", description);
136 if ( ( ! mergeNtuples ) || ( ! canMerge ) ) {
137 fNtupleMergeMode = G4NtupleMergeMode::kNone;
138 mergingMode =
"G4NtupleMergeMode::kNone";
142 fNofNtupleFiles = nofNtupleFiles;
145 if ( fNofNtupleFiles < 0 ) {
148 <<
"Number of reduced files must be [0, nofThreads]." <<
G4endl
149 <<
"Cannot set " << nofNtupleFiles <<
" files" <<
G4endl
150 <<
"Ntuples will be merged in a single file.";
151 MergingException(
"SetNtupleMergingMode", description);
158 fNtupleMergeMode = G4NtupleMergeMode::kMain;
159 mergingMode =
"G4NtupleMergeMode::kMain";
161 fNtupleMergeMode = G4NtupleMergeMode::kSlave;
162 mergingMode =
"G4NtupleMergeMode::kSlave";
174G4int G4RootNtupleFileManager::GetNtupleFileNumber()
176 if ( ! fNofNtupleFiles )
return 0;
178 G4int nofMainManagers = fNofNtupleFiles;
179 if ( ! nofMainManagers ) nofMainManagers = 1;
186G4bool G4RootNtupleFileManager::CloseNtupleFiles()
190 auto finalResult =
true;
191 auto ntupleVector = fNtupleManager->GetNtupleDescriptionVector();
192 for (
auto ntupleDescription : ntupleVector) {
193 auto result = fFileManager->CloseNtupleFile(ntupleDescription);
194 finalResult = finalResult && result;
206 G4int nofNtupleFiles)
209 if ( fIsInitialized ) {
212 <<
"Cannot change merging mode." <<
G4endl
213 <<
"The function must be called before OpenFile().";
214 MergingException(
"SetNtupleMerging", description);
219 SetNtupleMergingMode(mergeNtuples, nofNtupleFiles);
230 rowWiseMode =
"row-wise with extra branch";
232 else if ( rowMode ) {
233 rowWiseMode =
"row-wise";
236 rowWiseMode =
"column-wise";
241 ->
Message(
"set",
"ntuple merging row mode", rowWiseMode);
245 if ( fNtupleRowWise == rowWise && fNtupleRowMode == rowMode )
return;
247 fNtupleRowWise = rowWise;
248 fNtupleRowMode = rowMode;
250 if ( fNtupleManager ) {
251 fNtupleManager->SetNtupleRowWise(rowWise, rowMode);
254 if ( fSlaveNtupleManager ) {
255 fSlaveNtupleManager->SetNtupleRowWise(rowWise, rowMode);
262 fFileManager->SetBasketSize(basketSize);
268 fFileManager->SetBasketEntries(basketEntries);
285 std::shared_ptr<G4VNtupleManager> activeNtupleManager =
nullptr;
286 switch ( fNtupleMergeMode )
288 case G4NtupleMergeMode::kNone:
290 = make_shared<G4RootNtupleManager>(
292 fNtupleManager->SetFileManager(fFileManager);
293 activeNtupleManager = fNtupleManager;
296 case G4NtupleMergeMode::kMain: {
297 G4int nofMainManagers = fNofNtupleFiles;
298 if ( ! nofMainManagers ) nofMainManagers = 1;
301 = make_shared<G4RootNtupleManager>(
303 fNtupleManager->SetFileManager(fFileManager);
304 activeNtupleManager = fNtupleManager;
308 case G4NtupleMergeMode::kSlave:
309 fNtupleManager = fgMasterInstance->fNtupleManager;
311 auto mainNtupleManager
312 = fNtupleManager->GetMainNtupleManager(GetNtupleFileNumber());
314 = make_shared<G4RootPNtupleManager>(
316 activeNtupleManager = fSlaveNtupleManager;
323 switch ( fNtupleMergeMode ) {
324 case G4NtupleMergeMode::kNone:
327 case G4NtupleMergeMode::kMain:
330 case G4NtupleMergeMode::kSlave:
331 mergeMode =
"slave ";
338 fIsInitialized =
true;
340 return activeNtupleManager;
352 auto finalResult =
true;
358 G4String objectType =
"analysis file";
360 objectType =
"main analysis file";
362 if ( fState.GetVerboseL4() )
363 fState.GetVerboseL4()->Message(
"open", objectType, fileName);
367 fNtupleManager->CreateNtuplesFromBooking(
368 fBookingManager->GetNtupleBookingVector());
371 if ( fState.GetVerboseL1() ) {
372 fState.GetVerboseL1()->Message(
"open", objectType, fileName, finalResult);
379 fSlaveNtupleManager->CreateNtuplesFromMain();
388 if ( fNtupleMergeMode == G4NtupleMergeMode::kNone ) {
392 auto finalResult =
true;
395 if ( fNtupleMergeMode == G4NtupleMergeMode::kMain ) ntupleType =
"main ntuples";
396 if ( fNtupleMergeMode == G4NtupleMergeMode::kSlave ) ntupleType =
"slave ntuples";
403 if ( fNtupleMergeMode == G4NtupleMergeMode::kMain ) {
404 auto result = fNtupleManager->Merge();
405 finalResult = result && finalResult;
408 if ( fNtupleMergeMode == G4NtupleMergeMode::kSlave ) {
409 auto result = fSlaveNtupleManager->Merge();
410 finalResult = result && finalResult;
424 auto finalResult =
true;
427 if ( fNtupleMergeMode != G4NtupleMergeMode::kSlave ) {
428 auto result = CloseNtupleFiles();
429 finalResult = finalResult && result;
434 auto result =
Reset();
437 description <<
" " <<
"Resetting data failed";
438 G4Exception(
"G4RootNtupleFileManager::CloseFile()",
441 finalResult = finalResult && result;
454 if ( fNtupleMergeMode == G4NtupleMergeMode::kNone ||
455 fNtupleMergeMode == G4NtupleMergeMode::kMain ) {
456 result = fNtupleManager->Reset(
false);
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
std::ostringstream G4ExceptionDescription
const G4AnalysisVerbose * GetVerboseL3() const
const G4AnalysisVerbose * GetVerboseL2() const
const G4AnalysisVerbose * GetVerboseL1() const
G4bool GetIsMaster() const
const G4AnalysisVerbose * GetVerboseL4() const
void Message(const G4String &action, const G4String &object, const G4String &objectName, G4bool success=true) const
virtual void SetNtupleMerging(G4bool mergeNtuples, G4int nofReducedNtupleFiles=0) override
virtual void SetNtupleRowWise(G4bool rowWise, G4bool rowMode=true) override
virtual void SetBasketSize(unsigned int basketSize) override
G4RootNtupleFileManager(const G4AnalysisManagerState &state)
virtual std::shared_ptr< G4VNtupleManager > CreateNtupleManager() override
virtual G4bool ActionAtCloseFile(G4bool reset) override
virtual G4bool ActionAtWrite() override
virtual void SetBasketEntries(unsigned int basketEntries) override
virtual G4bool Reset() override
virtual G4bool ActionAtOpenFile(const G4String &fileName) override
virtual ~G4RootNtupleFileManager()
std::shared_ptr< G4NtupleBookingManager > fBookingManager
const G4AnalysisManagerState & fState
G4bool IsMultithreadedApplication()