MultiChannelNoiseDB.cxx
Go to the documentation of this file.
1 #include "MultiChannelNoiseDB.h"
2 
4 
5 #include "WireCellUtil/NamedFactory.h"
6 
7 WIRECELL_FACTORY(wclsMultiChannelNoiseDB, wcls::MultiChannelNoiseDB,
8  wcls::IArtEventVisitor, WireCell::IChannelNoiseDatabase, WireCell::IConfigurable)
9 
10 
11 using namespace WireCell;
12 
14 {
15 }
16 
18 {
19 }
20 
21 void wcls::MultiChannelNoiseDB::MultiChannelNoiseDB::visit(art::Event & event)
22 {
23  for (auto one : m_rules) {
24  if (one.check(event)) {
25  m_pimpl = one.chndb;
26  // std::cerr << "wclsMultiChannelNoiseDB: using: " << type(*one.chndb)
27  // << " @" << (void*)one.chndb.get()
28  // << " from MultiChannelNoiseDB @" << (void*)this
29  // <<"\n";
30  m_pimpl_visitor = one.visitor;
31  if (m_pimpl_visitor) { // okay to be nullptr if not a wclsChannelNoiseDB
32  m_pimpl_visitor->visit(event);
33  }
34  return;
35  }
36  }
37  THROW(KeyError() << errmsg{"MultiChannelNoiseDB: no matching rule for event, consider 'bool' catch all in config"});
38 }
39 
40 struct ReturnBool {
41  bool ok;
42  ReturnBool(Json::Value jargs) // simple value, true or false
43  : ok(jargs.asBool()) {}
44  bool operator()(art::Event& /*event*/) {
45  return ok;
46  }
47 };
48 
49 struct RunRange {
50  int first, last;
51  RunRange(Json::Value jargs) { // {first: NNN, last: MMM}
52  first = jargs["first"].asInt();
53  last = jargs["last"].asInt();
54  }
55  bool operator()(art::Event& event) {
56  int run = event.run();
57  return first <= run and run <= last;
58  }
59 };
60 
61 struct RunList {
62  std::unordered_set<int> runs;
63  RunList(Json::Value jargs) { // [1001, 1002, 1003]
64  for (auto j : jargs) {
65  runs.insert(j.asInt());
66  }
67  }
68  bool operator()(art::Event& event) {
69  int run = event.run();
70  return runs.find(run) != runs.end();
71  }
72 };
73 
74 struct RunStarting {
75  int run;
76  RunStarting(Json::Value jargs) { // 1001
77  run = jargs.asInt();
78  }
79  bool operator()(art::Event& event) {
80  int thisrun = event.run();
81  return thisrun >= run;
82  }
83 };
84 struct RunBefore {
85  int run;
86  RunBefore(Json::Value jargs) { // 1001
87  run = jargs.asInt();
88  }
89  bool operator()(art::Event& event) {
90  int thisrun = event.run();
91  return thisrun < run;
92  }
93 };
94 
95 void wcls::MultiChannelNoiseDB::MultiChannelNoiseDB::configure(const WireCell::Configuration& cfg)
96 {
97  for (auto jrule : cfg["rules"]) {
98  auto rule = jrule["rule"].asString();
99  auto tn = jrule["chndb"].asString();
100  std::cerr << "\tMultiChannelNoiseDB: " << tn << " using rule: " << rule << std::endl;
101  auto chndb = Factory::find_tn<IChannelNoiseDatabase>(tn);
102  if (!chndb) {
103  THROW(KeyError() << errmsg{"Failed to find (sub) channel noise DB object: " + tn});
104  }
105  IArtEventVisitor::pointer visitor = Factory::find_maybe_tn<IArtEventVisitor>(tn);
106  auto jargs = jrule["args"];
107  if (rule == "runrange") {
108  m_rules.push_back(SubDB(RunRange(jargs), chndb, visitor));
109  continue;
110  }
111  if (rule == "runlist") {
112  m_rules.push_back(SubDB(RunList(jargs), chndb, visitor));
113  continue;
114  }
115  if (rule == "runstarting") {
116  m_rules.push_back(SubDB(RunStarting(jargs), chndb, visitor));
117  continue;
118  }
119  if (rule == "runbefore") {
120  m_rules.push_back(SubDB(RunBefore(jargs), chndb, visitor));
121  continue;
122  }
123 
124  if (rule == "bool") {
125  m_rules.push_back(SubDB(ReturnBool(jargs), chndb, visitor));
126  continue;
127  }
128  THROW(KeyError() << errmsg{"Unknown multi channel noise DB rule: " + rule});
129  }
130 }
131 
132 /* example:
133  rules: [
134  {
135  rule: "runlist",
136  chndb: "wclsChannelNoiseDB:prehwfix",
137  args: [1001, 1003, 1005],
138  },
139  {
140  rule: "runrange",
141  chndb: "wclsChannelNoiseDB:",
142  args: { first: 1001, second: 1003},
143  },
144  { // provide a catch all or risk an runtime exception
145  rule: "bool",
146  chndb: "OmniChannelNoiseDB:",
147  args: true
148  },
149  // also "runstarting" and "runbefore"
150  ]
151 */
152 
153 WireCell::Configuration wcls::MultiChannelNoiseDB::MultiChannelNoiseDB::default_configuration() const
154 {
155  Configuration cfg;
156  cfg["rules"] = Json::arrayValue;
157  return cfg;
158 }
WireCell::IChannelNoiseDatabase::pointer m_pimpl
WIRECELL_FACTORY(wclsMultiChannelNoiseDB, wcls::MultiChannelNoiseDB, wcls::IArtEventVisitor, WireCell::IChannelNoiseDatabase, WireCell::IConfigurable) using namespace WireCell
ReturnBool(Json::Value jargs)
IArtEventVisitor::pointer m_pimpl_visitor
RunStarting(Json::Value jargs)
std::unordered_set< int > runs
bool operator()(art::Event &event)
RunList(Json::Value jargs)
bool operator()(art::Event &event)
bool operator()(art::Event &event)
RunBefore(Json::Value jargs)
bool operator()(art::Event &event)
RunRange(Json::Value jargs)
bool operator()(art::Event &)
QTextStream & endl(QTextStream &s)
Event finding and building.