WCLS_tool.cc
Go to the documentation of this file.
3 
6 
11 #include "fhiclcpp/types/Comment.h"
12 #include "fhiclcpp/types/Table.h"
13 
14 #include "WireCellApps/Main.h"
15 #include "WireCellUtil/String.h"
16 #include "WireCellUtil/Logging.h"
17 
18 #include "WireCellUtil/NamedFactory.h"
19 
20 #include <string>
21 
22 
23 namespace wcls {
24 
25 
26  // https://cdcvs.fnal.gov/redmine/projects/fhicl-cpp/wiki/Fhiclcpp_types_in_detail#TableltT-KeysToIgnoregt
28  std::set<std::string> operator()() {
29  // Ignore these for validation.
30  return {"params"};
31  }
32  };
33 
34  // https://cdcvs.fnal.gov/redmine/projects/art/wiki/Configuration_validation_and_description
35  struct WCLSConfig {
38  //typedef fhicl::OptionalTable<fhicl::ParameterSet> generic_pset_t;
40 
41  // These are WCT config items to pass through
42  string_list_t configs { fhicl::Name("configs"),
43  fhicl::Comment("List of one or more WCT configuration files."
44  "\nThey are located w.r.t. the WCT load path.") };
45  string_list_t apps { fhicl::Name("apps"),
46  fhicl::Comment("List of one or more WCT application objects to execute.")};
47  string_list_t plugins { fhicl::Name("plugins"),
48  fhicl::Comment("List of WCT component plugin libraries to load.\n"
49  "They are located w.r.t. the OS library load path")};
50 
51  optional_string_list_t paths { fhicl::Name("paths"),
52  fhicl::Comment("Optional list of file system paths to add to the WCT "
53  "configuration file load path."
54  "\nThis augments the WIRECELL_PATH environment variable.")};
55  generic_pset_t params{ fhicl::Name("params"),
56  fhicl::Comment("Optional table giving external variables to inject into WCT configuration.")};
57  generic_pset_t structs{ fhicl::Name("structs"),
58  fhicl::Comment("Optional table giving external Jsonnet code to inject into WCT configuration.")};
59 
60  // These are items needed by the tool
61  optional_string_list_t inputers { fhicl::Name("inputers"),
62  fhicl::Comment("List of WCT components which act as WCT sources.\n"
63  "They are called before WCT executes on each Art Event object") };
64  optional_string_list_t outputers { fhicl::Name("outputers"),
65  fhicl::Comment("List of WCT components which act as WCT sinks.\n"
66  "They are called after WCT executes on each Art Event object.") };
67 
68  optional_string_list_t logsinks { fhicl::Name("logsinks"),
69  fhicl::Comment("List of WCT log sinks.\n"
70  "Eg the strings 'stdout', 'stderr' or a file name.\n"
71  "An optional log level may be appended with ':<level>'.") };
72  optional_string_list_t loglevels { fhicl::Name("loglevels"),
73  fhicl::Comment("List of minimum WCT logger levels.\n"
74  "Specify as '<logger>:<level>' or as just '<level>' for default.") };
75 
76 
77  };
78 
79  class WCLS : public MainTool {
80  public:
82 
83  explicit WCLS(Parameters const& ps);
84  virtual ~WCLS() { }
85 
86  void produces(art::ProducesCollector& collector) {
87  for (auto iaev : m_outputers) {
88  iaev->produces(collector);
89  }
90  }
91  void process(art::Event& event);
92 
93  private:
94  WireCell::Main m_wcmain;
96  // for c2: m_prod is not used
97  // art::EDProducer* m_prod;
98  };
99 }
100 
101 
102 
104  : m_wcmain()
105 {
106  const auto& wclscfg = params();
108 
109  if (wclscfg.logsinks(slist)) {
110  for (auto logsink : slist) {
111  //std::cerr << "Log sink: \"" << logsink << "\"\n";
112  auto ls = WireCell::String::split(logsink, ":");
113  if (ls.size() == 2) {
114  m_wcmain.add_logsink(ls[0], ls[1]);
115  }
116  else {
117  m_wcmain.add_logsink(ls[0]);
118  }
119  }
120  }
121  slist.clear();
122  if (wclscfg.loglevels(slist)) {
123  for (auto loglevel : slist) {
124  //std::cerr << "Log level: \"" << loglevel << "\"\n";
125  auto ll = WireCell::String::split(loglevel, ":");
126  if (ll.size() == 2) {
127  m_wcmain.set_loglevel(ll[0], ll[1]);
128  }
129  else{
130  m_wcmain.set_loglevel("", ll[0]);
131  }
132  }
133  }
134  slist.clear();
135  WireCell::Log::set_pattern("[%H:%M:%S.%03e] %L [%^%=8n%$] %v");
136 
137  // transfer configuration
138 
139  // required
140 
141  for (auto cfg : wclscfg.configs()) {
142  m_wcmain.add_config(cfg);
143  }
144 
145  for (auto app : wclscfg.apps()) {
146  m_wcmain.add_app(app);
147  }
148 
149  for (auto plugin : wclscfg.plugins()) {
150  m_wcmain.add_plugin(plugin);
151  }
152 
153  // optional
154 
155  if (wclscfg.paths(slist)) {
156  for (auto path : slist) {
157  m_wcmain.add_path(path);
158  }
159  }
160  slist.clear();
161 
162 
163  {
164  fhicl::ParameterSet wcps;
165  if (wclscfg.params.get_if_present(wcps)) {
166  for (auto key : wcps.get_names()) {
167  auto value = wcps.get<std::string>(key);
168  m_wcmain.add_var(key, value);
169  }
170  }
171  }
172  {
173  fhicl::ParameterSet wcps;
174  if (wclscfg.structs.get_if_present(wcps)) {
175  for (auto key : wcps.get_names()) {
176  auto value = wcps.get<std::string>(key);
177  m_wcmain.add_code(key, value);
178  }
179  }
180  }
181 
182  //std::cerr << "Initialize Wire Cell\n";
183  try {
184  m_wcmain.initialize();
185  }
186  catch (WireCell::Exception& e) {
187  std::cerr << "Wire Cell Toolkit threw an exception\n";
188  auto msg = errstr(e);
189  std::cerr << msg << std::endl;
190  throw cet::exception("WireCellLArSoft") << msg;
191  }
192 
193 
194 
195  if (wclscfg.inputers(slist)) {
196  for (auto inputer : slist) {
197  auto iaev = WireCell::Factory::find_tn<IArtEventVisitor>(inputer);
198  m_inputers.push_back(iaev);
199  std::cerr << "Inputer: \"" << inputer << "\"\n";
200  }
201  }
202  slist.clear();
203  if (wclscfg.outputers(slist)) {
204  for (auto outputer : slist) {
205  auto iaev = WireCell::Factory::find_tn<IArtEventVisitor>(outputer);
206  m_outputers.push_back(iaev);
207  std::cerr << "Outputer: \"" << outputer << "\"\n";
208  }
209  }
210  slist.clear();
211 }
212 
214 {
215  for (auto iaev : m_inputers) {
216  //std::cerr << "pre visit\n";
217  iaev->visit(event);
218  }
219 
220  //std::cerr << "Running Wire Cell Toolkit...\n";
221  m_wcmain();
222  //std::cerr << "... Wire Cell Toolkit done\n";
223 
224  for (auto iaev : m_outputers) {
225  //std::cerr << "post visit\n";
226  iaev->visit(event);
227  }
228 }
229 
230 
232 
233 // Local Variables:
234 // mode: c++
235 // c-basic-offset: 4
236 // End:
virtual ~WCLS()
Definition: WCLS_tool.cc:84
wcls::IArtEventVisitor::vector m_outputers
Definition: WCLS_tool.cc:95
fhicl::OptionalSequence< std::string > optional_string_list_t
Definition: WCLS_tool.cc:37
#define DEFINE_ART_CLASS_TOOL(tool)
Definition: ToolMacros.h:42
void msg(const char *fmt,...)
Definition: message.cpp:107
std::array< tt::return_type< std::string >,-1ull > value_type
std::string string
Definition: nybbler.cc:12
std::set< std::string > operator()()
Definition: WCLS_tool.cc:28
fhicl::Sequence< std::string > string_list_t
Definition: WCLS_tool.cc:36
struct vector vector
ChannelGroupService::Name Name
void produces(art::ProducesCollector &collector)
Definition: WCLS_tool.cc:86
def process(f, kind)
Definition: search.py:254
const double e
def key(type, name=None)
Definition: graph.py:13
WireCell::Main m_wcmain
Definition: WCLS_tool.cc:94
T get(std::string const &key) const
Definition: ParameterSet.h:271
fhicl::OptionalDelegatedParameter generic_pset_t
Definition: WCLS_tool.cc:39
void process(art::Event &event)
Accept an event to process.
Definition: WCLS_tool.cc:213
static constexpr double ps
Definition: Units.h:99
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
#define Comment
wcls::IArtEventVisitor::vector m_inputers
Definition: WCLS_tool.cc:95
std::vector< std::string > get_names() const
WCLS(Parameters const &ps)
Definition: WCLS_tool.cc:103
void split(std::string const &s, char c, OutIter dest)
Definition: split.h:35
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
QTextStream & endl(QTextStream &s)
Event finding and building.