Skip to content

Commit 1ed83ad

Browse files
committed
AutoSave option for RootTreeWriter
--autosave <n> option allows to save the tree after every n events. The default is -1 (autosave off), can be changed in the MakeRootTreeWriterSpec constructor by providing an integer argument coming after (not necessarilly immidiately) the number or events.
1 parent c6199d0 commit 1ed83ad

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

Framework/Utils/include/DPLUtils/MakeRootTreeWriterSpec.h

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ namespace framework
6464
/// --outfile
6565
/// --treename
6666
/// --nevents
67+
/// --autosave
6768
/// --terminate
6869
///
6970
/// \par
@@ -73,8 +74,10 @@ namespace framework
7374
/// \par Constructor arguments:
7475
/// Default file name can be configured alone, tree name can only be specified after
7576
/// file name. The default number of events can be specified at arbitrary place between
76-
/// process name and branch configuration. The process will signal to the DPL that it
77-
/// is ready for termination.
77+
/// process name and branch configuration. The number of events triggering autosaving
78+
/// (by default - off) can be also specified in the constructor as an integer argument
79+
/// coming after (not necessarilly immidiately) the number or events. The process will
80+
/// signal to the DPL that it is ready for termination.
7881
///
7982
/// \par Termination policy:
8083
/// The configurable termination policy specifies what to signal to the DPL when the event
@@ -334,6 +337,8 @@ class MakeRootTreeWriterSpec
334337
std::vector<std::pair<std::string, std::string>> branchNameOptions;
335338
// number of events to be processed
336339
int nEvents = -1;
340+
// autosave every nEventsAutoSave events
341+
int nEventsAutoSave = -1;
337342
// starting with all inputs, every input which has been indicated 'ready' is removed
338343
std::unordered_set<std::string> activeInputs;
339344
// event counter
@@ -372,6 +377,7 @@ class MakeRootTreeWriterSpec
372377
<< "different branches on the same input. Be aware that the --nevents option might lead to incomplete\n"
373378
<< "data in the output file as the number of processed input sets is counted";
374379
}
380+
processAttributes->nEventsAutoSave = ic.options().get<int>("autosave");
375381
try {
376382
processAttributes->terminationPolicy = TerminationPolicyMap.at(ic.options().get<std::string>("terminate"));
377383
} catch (std::out_of_range&) {
@@ -404,6 +410,7 @@ class MakeRootTreeWriterSpec
404410
auto& activeInputs = processAttributes->activeInputs;
405411
auto& counter = processAttributes->counter;
406412
auto& nEvents = processAttributes->nEvents;
413+
auto& nEventsAutoSave = processAttributes->nEventsAutoSave;
407414
if (writer->isClosed()) {
408415
return;
409416
}
@@ -459,6 +466,8 @@ class MakeRootTreeWriterSpec
459466
if ((nEvents >= 0 && counter == nEvents) || checkReady(pc.inputs())) {
460467
writer->close();
461468
pc.services().get<ControlService>().readyToQuit(terminationPolicy == TerminationPolicy::Workflow ? QuitRequest::All : QuitRequest::Me);
469+
} else if (nEventsAutoSave > 0 && counter && (counter % nEventsAutoSave) == 0) {
470+
writer->autoSave();
462471
}
463472
};
464473

@@ -471,6 +480,7 @@ class MakeRootTreeWriterSpec
471480
{"treename", VariantType::String, mDefaultTreeName.c_str(), {"Name of tree"}},
472481
{"treetitle", VariantType::String, mDefaultTreeTitle.c_str(), {"Title of tree"}},
473482
{"nevents", VariantType::Int, mDefaultNofEvents, {"Number of events to execute"}},
483+
{"autosave", VariantType::Int, mDefaultAutoSave, {"Autosave after number of events"}},
474484
{"terminate", VariantType::String, mDefaultTerminationPolicy.c_str(), {"Terminate the 'process' or 'workflow'"}},
475485
};
476486
for (size_t branchIndex = 0; branchIndex < mBranchNameOptions.size(); branchIndex++) {
@@ -521,8 +531,14 @@ class MakeRootTreeWriterSpec
521531
void parseConstructorArgs(int arg, Args&&... args)
522532
{
523533
static_assert(N == 0, "wrong argument order, default file and tree options must come before branch specs");
524-
mDefaultNofEvents = arg;
525-
534+
if (mNIntArgCounter == 0) {
535+
mDefaultNofEvents = arg;
536+
} else if (mNIntArgCounter == 1) {
537+
mDefaultAutoSave = arg;
538+
} else {
539+
throw std::logic_error("Too many integer arguments in the constructor");
540+
}
541+
mNIntArgCounter++;
526542
parseConstructorArgs<N>(std::forward<Args>(args)...);
527543
}
528544

@@ -620,10 +636,12 @@ class MakeRootTreeWriterSpec
620636
std::string mDefaultTreeName;
621637
std::string mDefaultTreeTitle;
622638
int mDefaultNofEvents = -1;
639+
int mDefaultAutoSave = -1;
623640
std::string mDefaultTerminationPolicy = "process";
624641
TerminationCondition mTerminationCondition;
625642
Preprocessor mPreprocessor;
626643
size_t mNofBranches = 0;
644+
int mNIntArgCounter = 0;
627645
CustomClose mCustomClose;
628646
};
629647
} // namespace framework

Framework/Utils/include/DPLUtils/RootTreeWriter.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,17 @@ class RootTreeWriter
363363
mFile.reset(nullptr);
364364
}
365365

366+
/// autosave the tree
367+
void autoSave()
368+
{
369+
if (mIsClosed || !mFile) {
370+
return;
371+
}
372+
mTree->SetEntries();
373+
LOG(INFO) << "Autosaving " << mTree->GetName() << " at entry " << mTree->GetEntries();
374+
mTree->AutoSave("overwrite");
375+
}
376+
366377
bool isClosed() const
367378
{
368379
return mIsClosed;

0 commit comments

Comments
 (0)