LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
RPManager.cc
Go to the documentation of this file.
2 // vim: set sw=2 expandtab :
3 
6 #include "cetlib/BasicPluginFactory.h"
8 
9 #include <functional>
10 #include <map>
11 #include <memory>
12 #include <string>
13 #include <vector>
14 
15 using namespace std;
16 using namespace literals::string_literals;
18 
19 namespace art {
20 
21  RPManager::RPManager(fhicl::ParameterSet const& ps) : rpmap_{}
22  {
23  string const omLabel(ps.get<string>("module_label"));
24  auto results = ps.get<ParameterSet>("results", {});
25  auto producers = results.get<ParameterSet>("producers", {});
26  auto results_keys = results.get_names();
27  auto producer_keys = producers.get_names();
28  map<string, vector<string>> paths;
29  string errMsg;
30  if (producer_keys.empty() && !results.is_empty()) {
32  << "Parameter set " << omLabel
33  << ".results is non-empty, but does not contain\n"
34  << "a non-empty producers parameter set.\n";
35  }
36  auto const path_keys_end =
37  remove(results_keys.begin(), results_keys.end(), "producers");
38  auto const& all_labels = producers.get_names();
39  set<string> all_labels_set(all_labels.cbegin(), all_labels.cend());
40  map<string, string> used_labels;
41  for (auto path_key = results_keys.begin(); path_key != path_keys_end;
42  ++path_key) {
43  if (!results.is_key_to_sequence(*path_key)) {
44  errMsg += "Parameter " + omLabel + ".results." + *path_key +
45  " does not describe a sequence (i.e. a path).\n";
46  continue;
47  }
48  auto path = results.get<vector<string>>(*path_key);
49  int idx = 0;
50  for (auto const& l : path) {
51  if (all_labels_set.find(l) == all_labels_set.end()) {
52  // Bad label
53  errMsg += omLabel + ".results."s + *path_key + '[' +
54  std::to_string(idx) + "] ("s + l + ')' +
55  " is not defined in "s + omLabel + ".results.producers.\n"s;
56  ++idx;
57  continue;
58  }
59  auto const ans = used_labels.emplace(l, *path_key);
60  if (!ans.second) {
61  // Duplicate
62  errMsg += omLabel + ".results." + *path_key + '[' +
63  std::to_string(idx) + "] (" + l + ')' +
64  " is a duplicate: previously used in path " +
65  ans.first->second + ".\n";
66  }
67  ++idx;
68  }
69  if (errMsg.empty()) {
70  paths.emplace(*path_key, std::move(path));
71  }
72  }
73  if (paths.empty() && (errMsg.empty())) {
74  paths.emplace("default_path", all_labels);
75  }
76  if (!errMsg.empty()) {
78  << "Errors encountered while configuring ResultsProducers:\n"
79  << errMsg;
80  }
81  cet::BasicPluginFactory pf{Suffixes::plugin(), "makeRP"};
82  for (auto const& [path_name, module_names] : paths) {
83  auto ins_res = rpmap_.emplace(path_name, vector<unique_ptr<RPWorker>>{});
84  transform(module_names.cbegin(),
85  module_names.cend(),
86  back_inserter(ins_res.first->second),
87  [&pf, &producers, &omLabel, &errMsg](string const& pkey) {
88  unique_ptr<RPWorker> result;
89  auto const& pconfig = producers.get<ParameterSet>(pkey);
90  auto libspec = pconfig.get<string>("plugin_type");
91  auto const& ptype = pf.pluginType(libspec);
93  result = pf.makePlugin<unique_ptr<RPWorker>,
94  RPParams const&,
95  ParameterSet const&>(
96  libspec, {pconfig.id(), libspec, pkey}, pconfig);
97  } else {
98  errMsg +=
99  "Parameter set " + omLabel + ".results." + pkey +
100  " specifies a plugin " + libspec +
101  "\n which is not of required type ResultsProducer.\n";
102  }
103  return result;
104  });
105  }
106  if (!errMsg.empty()) {
108  << "Errors encountered while instantiating ResultsProducers:\n"
109  << errMsg;
110  }
111  }
112 
113  void
115  {
116  for (auto& path : rpmap_) {
117  for (auto& w : path.second) {
118  wfunc(*w);
119  }
120  }
121  }
122 
123 } // namespace art
std::map< std::string, std::vector< std::unique_ptr< RPWorker > > > rpmap_
Definition: RPManager.h:35
std::function< void(RPWorker &)> on_rpworker_t
Definition: RPManager.h:24
STL namespace.
void for_each_RPWorker(on_rpworker_t wfunc)
Definition: RPManager.cc:114
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:289
decltype(auto) constexpr to_string(T &&obj)
ADL-aware version of std::to_string.
T get(std::string const &key) const
Definition: ParameterSet.h:314
static std::string const & plugin()
double value
Definition: spectrum.C:18
ParameterSetID id() const
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
std::vector< std::string > get_names() const
Definition: MVAAlg.h:12
Float_t w
Definition: plot.C:20