GroupSelector_t.cc
Go to the documentation of this file.
1 #define BOOST_TEST_MODULE (GroupSelector_t)
2 #include "boost/test/unit_test.hpp"
3 
5 
16 #include "fhiclcpp/ParameterSet.h"
17 
18 #include <string>
19 #include <vector>
20 
21 namespace {
22 
23  template <typename PROD>
25  makeBranchDescription(art::BranchType const bt,
26  std::string const& moduleLabel,
27  std::string const& processName,
28  std::string const& instanceName,
29  fhicl::ParameterSet const& pset)
30  {
31  art::TypeID const producedType{typeid(PROD)};
33  bt,
35  producedType, instanceName, art::SupportsView<PROD>::value, false},
36  moduleLabel,
37  pset.id(),
39  processName, fhicl::ParameterSet{}.id(), art::getReleaseVersion()}};
40  }
41 
42  void
43  apply_gs(art::GroupSelector const& gs,
44  art::ProductDescriptionsByID const& descriptions,
45  std::vector<bool>& results)
46  {
47  for (auto const& p : descriptions) {
48  results.push_back(gs.selected(p.second));
49  }
50  }
51 
52  void
53  doTest(fhicl::ParameterSet const& params,
54  std::string const& testname,
55  art::ProductTables const& pTables,
56  std::initializer_list<bool> const& expected)
57  {
58  std::string const parameterName{"outputCommands"};
60  params.get<std::vector<std::string>>(parameterName, {"keep *"}),
61  parameterName,
62  testname);
63  std::vector<bool> results;
64 
65  for (std::size_t i{}; i < art::NumBranchTypes; ++i) {
66  auto const bt = static_cast<art::BranchType>(i);
67  auto const& descriptions = pTables.descriptions(bt);
68  art::GroupSelector const gs{gsr, descriptions};
69  apply_gs(gs, descriptions, results);
70  }
71 
72  BOOST_TEST_REQUIRE(expected == results, boost::test_tools::per_element{});
73  }
74 
75  class GlobalSetup {
76  public:
77  static GlobalSetup&
78  instance()
79  {
80  static GlobalSetup s_gs;
81  return s_gs;
82  }
83 
84  art::ProductTables const&
85  pTables()
86  {
87  return pTables_;
88  }
89 
90  private:
91  art::ProductTables const pTables_{initProductTables()};
92 
94  initProductTables()
95  {
96  // We pretend to have one module, with two products. The products
97  // are of the same and, type differ in instance name.
98  fhicl::ParameterSet modAparams;
99  modAparams.put("i", 2112);
100  modAparams.put("s", "hi");
101 
102  auto b1 = makeBranchDescription<arttest::ProdTypeA<std::string>>(
103  art::InEvent, "modA", "PROD", "i1", modAparams);
104  auto b2 = makeBranchDescription<arttest::ProdTypeA<std::string>>(
105  art::InEvent, "modA", "PROD", "i2", modAparams);
106 
107  // Our second pretend module has only one product, and gives it no
108  // instance name.
109  fhicl::ParameterSet modBparams;
110  modBparams.put("d", 2.5);
111 
112  auto b3 = makeBranchDescription<arttest::ProdTypeB<std::string>>(
113  art::InRun, "modB", "HLT", "", modBparams);
114 
115  // Our third pretend is like modA, except it has a processName_ of
116  // "USER"
117  auto b4 = makeBranchDescription<arttest::ProdTypeA<std::string>>(
118  art::InSubRun, "modA", "USER", "i1", modAparams);
119  auto b5 = makeBranchDescription<arttest::ProdTypeA<std::string>>(
120  art::InResults, "modA", "USER", "i2", modAparams);
121 
122  // Extra tests.
123  auto b6 = makeBranchDescription<art::Ptr<std::string>>(
124  art::InRun, "ptrmvWriter", "PtrmvW", "", modAparams);
125 
126  // Ordered correctly (via BranchType, then by ProductID) for ease of
127  // deducing expected results.
128  art::ProductDescriptions descriptions;
129  descriptions.push_back(
130  std::move(b2)); // ProductID: 1458780065 |
131  // Stringarttest::ProdTypeA_modA_i2_PROD, InEvent.
132  descriptions.push_back(
133  std::move(b1)); // ProductID: 1729834300 |
134  // Stringarttest::ProdTypeA_modA_i1_PROD, InEvent.
135  descriptions.push_back(
136  std::move(b4)); // ProductID: 105444648 |
137  // Stringarttest::ProdTypeA_modA_i1_USER, InSubRun.
138  descriptions.push_back(
139  std::move(b6)); // ProductID: 1113343586 |
140  // Stringart::Ptr_ptrmvWriter__PtrmvW, InRun.
141  descriptions.push_back(
142  std::move(b3)); // ProductID: 1125702662 |
143  // Stringarttest::ProdTypeB_modB__HLT, InRun.
144  descriptions.push_back(
145  std::move(b5)); // ProductID: 933294005 |
146  // Stringarttest::ProdTypeA_modA_i2_USER, InResults.
147  return art::ProductTables{descriptions};
148  }
149  };
150 
151  struct ProductListAccessor {
152  art::ProductTables const& pTables{GlobalSetup::instance().pTables()};
153  };
154 
155 } // namespace
156 
157 BOOST_FIXTURE_TEST_SUITE(Tests, ProductListAccessor)
158 
159 BOOST_AUTO_TEST_CASE(Test_default_parameters)
160 {
161  auto expected = {true, true, true, true, true, true};
162  fhicl::ParameterSet noparams;
163  doTest(noparams, "default parameters", pTables, expected);
164 }
165 
166 BOOST_AUTO_TEST_CASE(Keep_all_branches_with_instance_name_i2)
167 {
168  auto expected = {true, false, false, false, false, true};
169 
170  fhicl::ParameterSet keep_i2;
171  std::string const keep_i2_rule = "keep *_*_i2_*";
172  std::vector<std::string> cmds;
173  cmds.push_back(keep_i2_rule);
174  keep_i2.put("outputCommands", cmds);
175 
176  doTest(keep_i2, "keep i2 parameters", pTables, expected);
177 }
178 
179 BOOST_AUTO_TEST_CASE(Drop_all_branches_with_instance_name_i2)
180 {
181  auto expected = {false, true, true, true, true, false};
182 
183  fhicl::ParameterSet drop_i2;
184  std::string const drop_i2_rule1 = "keep *";
185  std::string const drop_i2_rule2 = "drop *_*_i2_*";
186  std::vector<std::string> cmds;
187  cmds.push_back(drop_i2_rule1);
188  cmds.push_back(drop_i2_rule2);
189  drop_i2.put("outputCommands", cmds);
190 
191  doTest(drop_i2, "drop i2 parameters", pTables, expected);
192 }
193 
194 BOOST_AUTO_TEST_CASE(Drop_all_branches_with_product_type_foo)
195 {
196  auto expected = {true, true, true, true, true, true};
197 
198  fhicl::ParameterSet drop_foo;
199  std::string const drop_foo_rule1 = "keep *_*_*_*"; // same as "keep *"
200  std::string const drop_foo_rule2 = "drop foo_*_*_*";
201  std::vector<std::string> cmds;
202  cmds.push_back(drop_foo_rule1);
203  cmds.push_back(drop_foo_rule2);
204  drop_foo.put("outputCommands", cmds);
205 
206  doTest(drop_foo, "drop_foo parameters", pTables, expected);
207 }
208 
209 BOOST_AUTO_TEST_CASE(Drop_all_branches_with_product_type_ProdTypeA)
210 {
211  auto expected = {false, false, false, true, true, false};
212 
213  fhicl::ParameterSet drop_ProdTypeA;
214  std::string const drop_ProdTypeA_rule1 = "keep *";
215  std::string const drop_ProdTypeA_rule2 = "drop *ProdTypeA_*_*_*";
216  std::vector<std::string> cmds;
217  cmds.push_back(drop_ProdTypeA_rule1);
218  cmds.push_back(drop_ProdTypeA_rule2);
219  drop_ProdTypeA.put("outputCommands", cmds);
220 
221  doTest(drop_ProdTypeA, "drop_ProdTypeA", pTables, expected);
222 }
223 
224 BOOST_AUTO_TEST_CASE(Keep_only_production_branches_with_instance_name_i1)
225 {
226  auto expected = {false, true, false, false, false, false};
227 
228  fhicl::ParameterSet keep_i1prod;
229  std::string const keep_i1prod_rule = "keep *_*_i1_PROD";
230  std::vector<std::string> cmds;
231  cmds.push_back(keep_i1prod_rule);
232  keep_i1prod.put("outputCommands", cmds);
233 
234  doTest(keep_i1prod, "keep_i1prod", pTables, expected);
235 }
236 
237 BOOST_AUTO_TEST_CASE(Keep_drop_keep)
238 {
239  auto expected = {true, true, true, true, true, true};
240 
241  fhicl::ParameterSet indecisive;
242  std::string const indecisive_rule1 = "keep *";
243  std::string const indecisive_rule2 = "drop *";
244  std::string const indecisive_rule3 = "keep *";
245  std::vector<std::string> cmds;
246  cmds.push_back(indecisive_rule1);
247  cmds.push_back(indecisive_rule2);
248  cmds.push_back(indecisive_rule3);
249  indecisive.put("outputCommands", cmds);
250 
251  doTest(indecisive, "indecisive", pTables, expected);
252 }
253 
254 BOOST_AUTO_TEST_CASE(Keep_all_drop_from_modA_keep_USER)
255 {
256  auto expected = {false, false, true, true, true, true};
257 
259  std::string const rule1 = "keep *";
260  std::string const rule2 = "drop *_modA_*_*";
261  std::string const rule3 = "keep *_*_*_USER";
262  std::vector<std::string> cmds;
263  cmds.push_back(rule1);
264  cmds.push_back(rule2);
265  cmds.push_back(rule3);
266  params.put("outputCommands", cmds);
267 
268  doTest(params, "drop_modA_keep_user", pTables, expected);
269 }
270 
271 BOOST_AUTO_TEST_CASE(Exercise_wildcards)
272 {
273  auto expected = {true, true, false, false, true, false};
274 
276  std::string const rule1 = "drop *";
277  std::string const rule2 = "keep *Pr*A_m?dA_??_P?O*";
278  std::string const rule3 = "keep *?*?***??*????*?***_??***?__*?***T";
279  std::vector<std::string> cmds;
280  cmds.push_back(rule1);
281  cmds.push_back(rule2);
282  cmds.push_back(rule3);
283  params.put("outputCommands", cmds);
284 
285  doTest(params, "exercise wildcards1", pTables, expected);
286 }
287 
288 BOOST_AUTO_TEST_CASE(Drop_by_full_spec)
289 {
290  auto expected = {true, true, true, false, true, true};
291 
293  std::string const rule1 = "keep *";
294  std::string const rule2 = "drop Stringart::Ptr_ptrmvWriter__PtrmvW";
295  std::vector<std::string> cmds;
296  cmds.push_back(rule1);
297  cmds.push_back(rule2);
298  params.put("outputCommands", cmds);
299 
300  doTest(params, "drop product by full spec.", pTables, expected);
301 }
302 
303 BOOST_AUTO_TEST_CASE(Illegal_spec)
304 {
306  std::string const bad_rule = "beep *_*_i2_*";
307  std::vector<std::string> cmds;
308  cmds.push_back(bad_rule);
309  std::string const parameterName = "outputCommands";
310  bad.put(parameterName, cmds);
311  BOOST_REQUIRE_EXCEPTION(
312  art::GroupSelectorRules(bad.get<std::vector<std::string>>(parameterName),
313  parameterName,
314  "GroupSelectorTest"),
316  [](auto const& e) {
317  return e.categoryCode() == art::errors::Configuration;
318  });
319 }
320 
321 BOOST_AUTO_TEST_CASE(Drop_by_type_event)
322 {
323  auto expected = {false, false, true, true, true, true};
324 
326  std::string const rule1 = "keep *";
327  std::string const rule2 = "drop * InEvent";
328  std::vector<std::string> cmds;
329  cmds.push_back(rule1);
330  cmds.push_back(rule2);
331  params.put("outputCommands", cmds);
332 
333  doTest(params, "drop product by full spec.", pTables, expected);
334 }
335 
336 BOOST_AUTO_TEST_CASE(Drop_by_type_results)
337 {
338  auto expected = {true, true, true, true, true, false};
339 
341  std::string const rule1 = "keep *";
342  std::string const rule2 = "drop * InResults";
343  std::vector<std::string> cmds;
344  cmds.push_back(rule1);
345  cmds.push_back(rule2);
346  params.put("outputCommands", cmds);
347 
348  doTest(params, "drop product by full spec.", pTables, expected);
349 }
350 
351 BOOST_AUTO_TEST_CASE(Keep_by_type_results)
352 {
353  auto expected = {false, false, false, false, false, true};
354 
356  std::string const rule1 = "drop *";
357  std::string const rule2 = "keep *_*_*_* inResults";
358  std::vector<std::string> cmds;
359  cmds.push_back(rule1);
360  cmds.push_back(rule2);
361  params.put("outputCommands", cmds);
362 
363  doTest(params, "drop product by full spec.", pTables, expected);
364 }
365 
367 {
369  std::string const bad_rule = "keep *_*_i2_* wibble";
370  std::vector<std::string> cmds;
371  cmds.push_back(bad_rule);
372  std::string const parameterName = "outputCommands";
373  bad.put(parameterName, cmds);
374  BOOST_REQUIRE_EXCEPTION(
375  art::GroupSelectorRules(bad.get<std::vector<std::string>>(parameterName),
376  parameterName,
377  "GroupSelectorTest"),
379  [](auto const& e) {
380  return e.categoryCode() == art::errors::Configuration;
381  });
382 }
383 
384 BOOST_AUTO_TEST_SUITE_END()
const char expected[]
Definition: Exception_t.cc:22
std::string string
Definition: nybbler.cc:12
const std::string instance
bool selected(BranchDescription const &desc) const
std::vector< BranchDescription > ProductDescriptions
BOOST_TEST_REQUIRE(static_cast< bool >(inFile))
auto const & descriptions(BranchType const bt) const
Definition: ProductTables.h:43
const double e
bt
Definition: tracks.py:83
def move(depos, offset)
Definition: depos.py:107
T get(std::string const &key) const
Definition: ParameterSet.h:271
std::string const & getReleaseVersion()
p
Definition: test.py:223
BOOST_AUTO_TEST_CASE(Test_default_parameters)
ParameterSetID id() const
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
BranchType
Definition: BranchType.h:20
std::map< ProductID, BranchDescription > ProductDescriptionsByID
unsigned int bad()
void put(std::string const &key)