42using std::make_shared;
58 auto mergeNtuples =
false;
59 SetNtupleMergingMode(mergeNtuples, fNofNtupleFiles);
73void G4RootNtupleFileManager::SetNtupleMergingMode(
G4bool mergeNtuples,
83 Warn(
"Merging ntuples is not applicable in sequential application.\n"
84 "Setting was ignored.",
85 fkClass,
"SetNtupleMergingMode");
91 Warn(
"Merging ntuples requires G4AnalysisManager instance on master.\n"
92 "Setting was ignored.",
93 fkClass,
"SetNtupleMergingMode");
98 if ( ( ! mergeNtuples ) || ( ! canMerge ) ) {
99 fNtupleMergeMode = G4NtupleMergeMode::kNone;
100 mergingMode =
"G4NtupleMergeMode::kNone";
104 fNofNtupleFiles = nofNtupleFiles;
107 if ( fNofNtupleFiles < 0 ) {
108 Warn(
"Number of reduced files must be [0, nofThreads].\n"
109 "Cannot set " + to_string(nofNtupleFiles) +
" files.\n" +
110 "Setting was ignored.",
111 fkClass,
"SetNtupleMergingMode");
119 fNtupleMergeMode = G4NtupleMergeMode::kMain;
120 mergingMode =
"G4NtupleMergeMode::kMain";
122 fNtupleMergeMode = G4NtupleMergeMode::kSlave;
123 mergingMode =
"G4NtupleMergeMode::kSlave";
127 Message(
kVL2,
"set",
"ntuple merging mode", mergingMode);
131G4int G4RootNtupleFileManager::GetNtupleFileNumber()
133 if (fNofNtupleFiles == 0)
return 0;
135 G4int nofMainManagers = fNofNtupleFiles;
136 if (nofMainManagers == 0) nofMainManagers = 1;
143G4bool G4RootNtupleFileManager::CloseNtupleFiles()
146 auto mainNumber = ( fNofNtupleFiles > 0 ) ? 0 : -1;
149 auto ntupleVector = fNtupleManager->GetNtupleDescriptionVector();
150 for (
auto ntupleDescription : ntupleVector) {
151 for (
G4int i = mainNumber; i < fNofNtupleFiles; ++i ) {
152 result &= fFileManager->CloseNtupleFile(ntupleDescription, i);
164 G4int nofNtupleFiles)
167 if ( fIsInitialized ) {
168 Warn(
"Cannot change merging mode.\n"
169 "The function must be called before OpenFile().",
170 fkClass,
"SetNtupleMerging");
175 SetNtupleMergingMode(mergeNtuples, nofNtupleFiles);
186 rowWiseMode =
"row-wise with extra branch";
188 else if ( rowMode ) {
189 rowWiseMode =
"row-wise";
192 rowWiseMode =
"column-wise";
195 Message(
kVL1,
"set",
"ntuple merging row mode", rowWiseMode);
198 if ( fNtupleRowWise == rowWise && fNtupleRowMode == rowMode )
return;
200 fNtupleRowWise = rowWise;
201 fNtupleRowMode = rowMode;
203 if ( fNtupleManager ) {
204 fNtupleManager->SetNtupleRowWise(rowWise, rowMode);
207 if ( fSlaveNtupleManager ) {
208 fSlaveNtupleManager->SetNtupleRowWise(rowWise, rowMode);
215 fFileManager->SetBasketSize(basketSize);
221 fFileManager->SetBasketEntries(basketEntries);
235 std::shared_ptr<G4VNtupleManager> activeNtupleManager =
nullptr;
236 switch ( fNtupleMergeMode )
238 case G4NtupleMergeMode::kNone:
240 = make_shared<G4RootNtupleManager>(
242 fNtupleManager->SetFileManager(fFileManager);
243 activeNtupleManager = fNtupleManager;
246 case G4NtupleMergeMode::kMain: {
247 G4int nofMainManagers = fNofNtupleFiles;
248 if (nofMainManagers == 0) {
253 = make_shared<G4RootNtupleManager>(
255 fNtupleManager->SetFileManager(fFileManager);
256 activeNtupleManager = fNtupleManager;
260 case G4NtupleMergeMode::kSlave:
261 fNtupleManager = fgMasterInstance->fNtupleManager;
263 auto mainNtupleManager
264 = fNtupleManager->GetMainNtupleManager(GetNtupleFileNumber());
266 = make_shared<G4RootPNtupleManager>(
268 activeNtupleManager = fSlaveNtupleManager;
273 switch ( fNtupleMergeMode ) {
274 case G4NtupleMergeMode::kNone:
277 case G4NtupleMergeMode::kMain:
280 case G4NtupleMergeMode::kSlave:
281 mergeMode =
"slave ";
284 Message(
kVL3,
"create", mergeMode +
"ntuple manager");
286 fIsInitialized =
true;
288 return activeNtupleManager;
296 if ( fNtupleMergeMode == G4NtupleMergeMode::kNone ||
297 fNtupleMergeMode == G4NtupleMergeMode::kMain ) {
299 G4String objectType =
"analysis file";
300 if ( fNtupleMergeMode == G4NtupleMergeMode::kMain ) {
301 objectType =
"main analysis file";
306 fNtupleManager->CreateNtuplesFromBooking(
324 if ( fNtupleMergeMode == G4NtupleMergeMode::kNone ) {
331 if ( fNtupleMergeMode == G4NtupleMergeMode::kMain ) ntupleType =
"main ntuples";
332 if ( fNtupleMergeMode == G4NtupleMergeMode::kSlave ) ntupleType =
"slave ntuples";
336 if ( fNtupleMergeMode == G4NtupleMergeMode::kMain ) {
337 result &= fNtupleManager->Merge();
340 if ( fNtupleMergeMode == G4NtupleMergeMode::kSlave ) {
341 result &= fSlaveNtupleManager->Merge();
352 if ( fNtupleMergeMode == G4NtupleMergeMode::kSlave) {
353 fSlaveNtupleManager->SetNewCycle(
false);
357 return CloseNtupleFiles();
367 if ( fNtupleMergeMode == G4NtupleMergeMode::kNone ||
368 fNtupleMergeMode == G4NtupleMergeMode::kMain ) {
369 result &= fNtupleManager->Reset();
372 if ( fNtupleMergeMode == G4NtupleMergeMode::kSlave ) {
373 fSlaveNtupleManager->Reset();
G4bool GetIsMaster() const
void SetNtupleMerging(G4bool mergeNtuples, G4int nofReducedNtupleFiles=0) override
void SetNtupleRowWise(G4bool rowWise, G4bool rowMode=true) override
void SetBasketSize(unsigned int basketSize) override
G4bool ActionAtCloseFile() override
std::shared_ptr< G4VNtupleManager > CreateNtupleManager() override
~G4RootNtupleFileManager() override
G4bool ActionAtWrite() override
void SetBasketEntries(unsigned int basketEntries) override
G4bool ActionAtOpenFile(const G4String &fileName) override
G4RootNtupleFileManager()=delete
std::shared_ptr< G4NtupleBookingManager > fBookingManager
void Message(G4int level, const G4String &action, const G4String &objectType, const G4String &objectName="", G4bool success=true) const
const G4AnalysisManagerState & fState
void Warn(const G4String &message, const std::string_view inClass, const std::string_view inFunction)
G4bool IsMultithreadedApplication()