LDMX Software
StorageControl.cxx
2
3#include "Framework/Exception/Exception.h"
4
5namespace framework {
6
8
9void StorageControl::addHint(const std::string& processor_name, Hint hint,
10 const std::string& purposeString) {
11 for (const auto& [processor_rule, purpose_rule] : rules_) {
12 if (std::regex_match(processor_name, processor_rule) and
13 std::regex_match(purposeString, purpose_rule)) {
14 // cache hints that matched a rule for later tallying
15 hints_.push_back(hint);
16 // leave after first match to avoid double-counting
17 break;
18 }
19 }
20}
21
22void StorageControl::addRule(const std::string& processor_pat,
23 const std::string& purpose_pat) {
27 if (processor_pat.empty()) return;
28
29 try {
30 rules_.emplace_back(
31 std::piecewise_construct,
32 std::forward_as_tuple(processor_pat,
33 std::regex::extended | std::regex::nosubs),
34 std::forward_as_tuple(purpose_pat.empty() ? ".*" : purpose_pat,
35 std::regex::extended | std::regex::nosubs));
36 } catch (const std::regex_error& e) {
37 // re-throw the regex error with our error
38 std::string msg{
39 "Invalid regex configured for the storage control listening rules: "};
40 msg += e.what();
41 EXCEPTION_RAISE("ConfigureError", msg);
42 }
43}
44
45bool StorageControl::keepEvent(bool event_completed) const {
50 if (not event_completed) return false;
51
55 int votesKeep(0), votesDrop(0);
56 bool mustDrop{false}, mustKeep{false};
57 for (auto hint : hints_) {
58 switch (hint) {
59 case Hint::MustDrop:
60 mustDrop = true;
61 break;
62 case Hint::MustKeep:
63 mustKeep = true;
64 break;
65 case Hint::ShouldDrop:
66 votesDrop++;
67 break;
68 case Hint::ShouldKeep:
69 votesKeep++;
70 break;
71 case Hint::Undefined:
72 case Hint::NoOpinion:
73 break;
74 default:
75 // how did I get here?
76 EXCEPTION_RAISE(
77 "SupaBad",
78 "This error comes from StorageControl and should never happen. "
79 "A storage hint should always be one of the members of the "
80 "StorageControlHint enum.");
81 }
82 }
83
87 if (mustDrop) return false;
88
93 if (mustKeep) return true;
94
99 if (votesKeep > votesDrop) return true;
100 if (votesDrop > votesKeep) return false;
101
107 return defaultIsKeep_;
108}
109} // namespace framework
Definitions related to event storage control from an EventProcessor.
void addHint(const std::string &processor_name, Hint hint, const std::string &purposeString)
Add a storage hint for a given processor.
std::vector< std::pair< std::regex, std::regex > > rules_
Collection of rules allowing certain processors or purposes to be considered ("listened to") during t...
bool keepEvent(bool event_completed) const
Determine if the current event should be kept, based on the defined rules.
std::vector< Hint > hints_
Collection of hints from the event processors.
void addRule(const std::string &processor_pat, const std::string &purpose_pat)
Add a listening rule.
bool defaultIsKeep_
Default state for storage control.
void resetEventState()
Reset the event-by-event state.
Hint
Hints that can be provided by processors to the storage controller.
All classes in the ldmx-sw project use this namespace.
Definition PerfDict.cxx:45