File indexing completed on 2024-04-06 12:12:25
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #include "catch.hpp"
0017
0018 #include "FWCore/Framework/interface/EventSelector.h"
0019 #include "DataFormats/Common/interface/TriggerResults.h"
0020 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0021 #include "FWCore/Framework/interface/TriggerNamesService.h"
0022 #include "FWCore/ServiceRegistry/interface/ServiceWrapper.h"
0023 #include "FWCore/ServiceRegistry/interface/ServiceRegistry.h"
0024 #include "FWCore/ServiceRegistry/interface/ServiceToken.h"
0025 #include "FWCore/Utilities/interface/Exception.h"
0026
0027 #include <array>
0028 #include <vector>
0029 #include <string>
0030 #include <iostream>
0031 #include <memory>
0032 #include <cassert>
0033
0034 using namespace edm;
0035
0036 typedef std::vector<std::vector<bool> > Answers;
0037
0038 typedef std::vector<std::string> Strings;
0039 typedef std::vector<Strings> VStrings;
0040 typedef std::vector<bool> Bools;
0041 typedef std::vector<Bools> VBools;
0042
0043
0044
0045 namespace {
0046
0047 size_t const num_trig_paths = 8;
0048 std::array<char const*, num_trig_paths> cpaths = {{
0049 "ap1",
0050 "ap2",
0051 "aq1",
0052 "aq2",
0053 "bp1",
0054 "bp2",
0055 "bq1",
0056 "bq2",
0057 }};
0058 Strings trigger_path_names(cpaths.begin(), cpaths.end());
0059
0060 struct PathSpecifiers {
0061 Strings path;
0062 PathSpecifiers(std::string const& s0,
0063 std::string const& s1 = "",
0064 std::string const& s2 = "",
0065 std::string const& s3 = "",
0066 std::string const& s4 = "",
0067 std::string const& s5 = "",
0068 std::string const& s6 = "",
0069 std::string const& s7 = "",
0070 std::string const& s8 = "",
0071 std::string const& s9 = "")
0072 : path() {
0073 if (s0 != "")
0074 path.push_back(s0);
0075 if (s1 != "")
0076 path.push_back(s1);
0077 if (s2 != "")
0078 path.push_back(s2);
0079 if (s3 != "")
0080 path.push_back(s3);
0081 if (s4 != "")
0082 path.push_back(s4);
0083 if (s5 != "")
0084 path.push_back(s5);
0085 if (s6 != "")
0086 path.push_back(s6);
0087 if (s7 != "")
0088 path.push_back(s7);
0089 if (s8 != "")
0090 path.push_back(s8);
0091 if (s9 != "")
0092 path.push_back(s9);
0093 }
0094 };
0095
0096 const HLTPathStatus pass = HLTPathStatus(edm::hlt::Pass);
0097 const HLTPathStatus fail = HLTPathStatus(edm::hlt::Fail);
0098 const HLTPathStatus excp = HLTPathStatus(edm::hlt::Exception);
0099 const HLTPathStatus redy = HLTPathStatus(edm::hlt::Ready);
0100
0101 struct TrigResults {
0102 std::vector<HLTPathStatus> bit;
0103 TrigResults(HLTPathStatus const& b0,
0104 HLTPathStatus const& b1,
0105 HLTPathStatus const& b2,
0106 HLTPathStatus const& b3,
0107 HLTPathStatus const& b4,
0108 HLTPathStatus const& b5,
0109 HLTPathStatus const& b6,
0110 HLTPathStatus const& b7)
0111 : bit(8) {
0112 bit[0] = b0;
0113 bit[1] = b1;
0114 bit[2] = b2;
0115 bit[3] = b3;
0116 bit[4] = b4;
0117 bit[5] = b5;
0118 bit[6] = b6;
0119 bit[7] = b7;
0120 assert(bit.size() == num_trig_paths);
0121 }
0122 void set(HLTPathStatus const& b0,
0123 HLTPathStatus const& b1,
0124 HLTPathStatus const& b2,
0125 HLTPathStatus const& b3,
0126 HLTPathStatus const& b4,
0127 HLTPathStatus const& b5,
0128 HLTPathStatus const& b6,
0129 HLTPathStatus const& b7) {
0130 bit[0] = b0;
0131 bit[1] = b1;
0132 bit[2] = b2;
0133 bit[3] = b3;
0134 bit[4] = b4;
0135 bit[5] = b5;
0136 bit[6] = b6;
0137 bit[7] = b7;
0138 }
0139 };
0140
0141 std::ostream& operator<<(std::ostream& ost, const Strings& s) {
0142 for (Strings::const_iterator i(s.begin()), e(s.end()); i != e; ++i) {
0143 ost << *i << " ";
0144 }
0145 return ost;
0146 }
0147
0148 std::ostream& operator<<(std::ostream& ost, const TrigResults& tr) {
0149 for (unsigned int i = 0; i < tr.bit.size(); ++i) {
0150 HLTPathStatus b = tr.bit[i];
0151 if (b.state() == edm::hlt::Ready)
0152 ost << "ready ";
0153 if (b.state() == edm::hlt::Pass)
0154 ost << "pass ";
0155 if (b.state() == edm::hlt::Fail)
0156 ost << "fail ";
0157 if (b.state() == edm::hlt::Exception)
0158 ost << "excp ";
0159 }
0160 return ost;
0161 }
0162
0163 template <size_t nb>
0164 Bools toBools(std::array<bool, nb> const& t) {
0165 Bools b;
0166 b.insert(b.end(), t.begin(), t.end());
0167 return b;
0168 }
0169 }
0170 void evSelTest(PathSpecifiers const& ps, TrigResults const& tr, bool ans) {
0171
0172
0173 EventSelector select_based_on_path_specifiers_and_names(ps.path, trigger_path_names);
0174 EventSelector select_based_on_path_specifiers_only(ps.path);
0175
0176 int number_of_trigger_paths = 0;
0177 std::vector<unsigned char> bitArray;
0178
0179 HLTGlobalStatus bm(tr.bit.size());
0180 for (unsigned int b = 0; b < tr.bit.size(); ++b) {
0181 bm[b] = (tr.bit[b]);
0182
0183
0184
0185
0186 if ((number_of_trigger_paths % 4) == 0)
0187 bitArray.push_back(0);
0188 int byteIndex = number_of_trigger_paths / 4;
0189 int subIndex = number_of_trigger_paths % 4;
0190 bitArray[byteIndex] |= (static_cast<unsigned char>(bm[b].state())) << (subIndex * 2);
0191 ++number_of_trigger_paths;
0192 }
0193
0194 TriggerResults results(bm, trigger_path_names);
0195
0196 bool a = select_based_on_path_specifiers_and_names.acceptEvent(results);
0197 bool b = select_based_on_path_specifiers_only.acceptEvent(results);
0198 bool aa = select_based_on_path_specifiers_and_names.acceptEvent(&(bitArray[0]), number_of_trigger_paths);
0199
0200
0201
0202
0203 std::ostringstream s;
0204 if (a != ans || b != ans || aa != ans) {
0205 s << "failed to compare path specifiers with trigger results: "
0206 << "correct=" << ans << " "
0207 << "results=" << a << " " << b << " " << aa << "\n"
0208 << "pathspecs = " << ps.path << "\n"
0209 << "trigger results = " << tr << "\n";
0210 }
0211 REQUIRE_THAT(ans,
0212 Catch::Predicate<bool>([a, b, aa](auto ans) { return a == ans and b == ans and aa == ans; }, s.str()));
0213
0214
0215
0216
0217 ParameterSet trigger_pset;
0218 trigger_pset.addParameter<Strings>("@trigger_paths", trigger_path_names);
0219 trigger_pset.registerIt();
0220
0221 TriggerResults results_id(bm, trigger_pset.id());
0222
0223 bool x = select_based_on_path_specifiers_and_names.acceptEvent(results_id);
0224 bool y = select_based_on_path_specifiers_only.acceptEvent(results_id);
0225
0226 if (x != ans || y != ans) {
0227 s << "failed to compare pathspecs with trigger results using pset ID: "
0228 << "correct=" << ans << " "
0229 << "results=" << x << " " << y << "\n"
0230 << "pathspecs =" << ps.path << "\n"
0231 << "trigger results = " << tr << "\n";
0232 }
0233 REQUIRE_THAT(ans, Catch::Predicate<bool>([x, y](auto ans) { return x == ans and y == ans; }, s.str()));
0234 }
0235
0236 TEST_CASE("test EventSelector test", "[EventSelector]") {
0237
0238
0239
0240
0241 ParameterSet proc_pset;
0242
0243 std::string processName("HLT");
0244 proc_pset.addParameter<std::string>("@process_name", processName);
0245
0246 ParameterSet trigPaths;
0247 trigPaths.addParameter<Strings>("@trigger_paths", trigger_path_names);
0248 proc_pset.addParameter<ParameterSet>("@trigger_paths", trigPaths);
0249
0250 Strings endPaths;
0251 proc_pset.addParameter<Strings>("@end_paths", endPaths);
0252
0253
0254
0255 Strings dummy;
0256 for (size_t i = 0; i < num_trig_paths; ++i) {
0257 proc_pset.addParameter<Strings>(trigger_path_names[i], dummy);
0258 }
0259 proc_pset.registerIt();
0260
0261
0262 typedef edm::service::TriggerNamesService TNS;
0263 typedef serviceregistry::ServiceWrapper<TNS> w_TNS;
0264
0265 auto tnsptr = std::make_shared<w_TNS>(std::make_unique<TNS>(proc_pset));
0266
0267 ServiceToken serviceToken_ = ServiceRegistry::createContaining(tnsptr);
0268
0269
0270 ServiceRegistry::Operate operate(serviceToken_);
0271
0272
0273
0274
0275 PathSpecifiers ps_a("ap2");
0276 TrigResults tr_01(fail, pass, fail, fail, fail, fail, excp, fail);
0277 evSelTest(ps_a, tr_01, true);
0278 tr_01.set(fail, pass, fail, fail, fail, fail, fail, fail);
0279 evSelTest(ps_a, tr_01, true);
0280 tr_01.set(pass, fail, pass, pass, fail, fail, fail, pass);
0281 evSelTest(ps_a, tr_01, false);
0282 tr_01.set(pass, excp, pass, pass, fail, fail, fail, pass);
0283 evSelTest(ps_a, tr_01, false);
0284 tr_01.set(pass, redy, pass, pass, fail, fail, fail, pass);
0285 evSelTest(ps_a, tr_01, false);
0286
0287
0288 PathSpecifiers ps_b("!aq2");
0289 TrigResults tr_02(pass, pass, pass, fail, pass, pass, excp, pass);
0290 evSelTest(ps_b, tr_02, true);
0291 tr_02.set(pass, pass, pass, fail, pass, pass, pass, pass);
0292 evSelTest(ps_b, tr_02, true);
0293 tr_02.set(pass, pass, pass, pass, pass, pass, pass, pass);
0294 evSelTest(ps_b, tr_01, false);
0295 tr_02.set(pass, pass, pass, excp, pass, pass, pass, pass);
0296 evSelTest(ps_b, tr_01, false);
0297 tr_02.set(pass, pass, pass, redy, pass, pass, pass, pass);
0298 evSelTest(ps_b, tr_01, false);
0299
0300
0301 PathSpecifiers ps_c("bp1", "!aq1", "!bq2");
0302 TrigResults tr_03(pass, pass, pass, pass, fail, pass, pass, pass);
0303 evSelTest(ps_c, tr_03, false);
0304 tr_03.set(excp, pass, pass, pass, pass, pass, pass, pass);
0305 evSelTest(ps_c, tr_03, true);
0306 tr_03.set(excp, pass, fail, pass, fail, pass, pass, pass);
0307 evSelTest(ps_c, tr_03, true);
0308 tr_03.set(excp, pass, pass, pass, fail, pass, pass, fail);
0309 evSelTest(ps_c, tr_03, true);
0310 tr_03.set(redy, pass, pass, pass, pass, pass, pass, fail);
0311 evSelTest(ps_c, tr_03, true);
0312
0313
0314 PathSpecifiers ps_d("ap2&noexception");
0315 TrigResults tr_04(fail, pass, fail, fail, fail, fail, excp, fail);
0316 evSelTest(ps_d, tr_04, false);
0317 tr_04.set(fail, pass, fail, fail, fail, fail, fail, fail);
0318 evSelTest(ps_d, tr_04, true);
0319 tr_04.set(pass, fail, pass, pass, fail, fail, fail, pass);
0320 evSelTest(ps_d, tr_04, false);
0321 tr_04.set(pass, excp, pass, pass, fail, fail, fail, pass);
0322 evSelTest(ps_d, tr_04, false);
0323 tr_04.set(pass, pass, pass, pass, fail, fail, redy, pass);
0324 evSelTest(ps_d, tr_04, true);
0325
0326
0327 PathSpecifiers ps_e("bp1", "!aq1&noexception", "!bq2");
0328 TrigResults tr_05(pass, pass, pass, pass, fail, pass, pass, pass);
0329 evSelTest(ps_e, tr_05, false);
0330 tr_05.set(excp, pass, pass, pass, pass, pass, pass, pass);
0331 evSelTest(ps_e, tr_05, true);
0332 tr_05.set(pass, pass, fail, pass, fail, pass, pass, pass);
0333 evSelTest(ps_e, tr_05, true);
0334 tr_05.set(pass, pass, fail, pass, fail, pass, excp, pass);
0335 evSelTest(ps_e, tr_05, false);
0336
0337
0338 PathSpecifiers ps_f("a*2", "?p2");
0339 TrigResults tr_06(fail, pass, fail, fail, fail, fail, excp, fail);
0340 evSelTest(ps_f, tr_06, true);
0341 tr_06.set(fail, fail, fail, pass, fail, fail, fail, fail);
0342 evSelTest(ps_f, tr_06, true);
0343 tr_06.set(pass, fail, pass, fail, fail, pass, excp, excp);
0344 evSelTest(ps_f, tr_06, true);
0345 tr_06.set(pass, fail, pass, fail, pass, fail, pass, pass);
0346 evSelTest(ps_f, tr_06, false);
0347
0348
0349 PathSpecifiers ps_g("!*2", "!ap?");
0350 TrigResults tr_07(fail, pass, fail, fail, fail, fail, fail, fail);
0351 evSelTest(ps_g, tr_07, false);
0352 tr_07.set(pass, fail, pass, fail, excp, fail, pass, fail);
0353 evSelTest(ps_g, tr_07, true);
0354 tr_07.set(fail, fail, pass, pass, fail, pass, excp, excp);
0355 evSelTest(ps_g, tr_07, true);
0356 tr_07.set(pass, fail, fail, fail, fail, fail, fail, redy);
0357 evSelTest(ps_g, tr_07, false);
0358
0359
0360 PathSpecifiers ps_h("a*2&noexception", "?p2");
0361 TrigResults tr_08(fail, pass, fail, fail, fail, fail, excp, fail);
0362 evSelTest(ps_h, tr_08, true);
0363 tr_08.set(fail, fail, fail, pass, fail, fail, excp, fail);
0364 evSelTest(ps_h, tr_08, false);
0365 tr_08.set(pass, fail, pass, fail, fail, pass, excp, excp);
0366 evSelTest(ps_h, tr_08, true);
0367 tr_08.set(pass, fail, pass, fail, pass, fail, pass, pass);
0368 evSelTest(ps_h, tr_08, false);
0369 tr_08.set(excp, fail, pass, pass, pass, fail, pass, pass);
0370 evSelTest(ps_h, tr_08, false);
0371
0372
0373 PathSpecifiers ps_i("!*2&noexception");
0374 TrigResults tr_09(fail, pass, fail, fail, fail, fail, fail, fail);
0375 evSelTest(ps_i, tr_09, false);
0376 tr_09.set(pass, fail, pass, fail, excp, fail, pass, fail);
0377 evSelTest(ps_i, tr_09, false);
0378 tr_09.set(fail, fail, pass, pass, fail, pass, excp, excp);
0379 evSelTest(ps_i, tr_09, false);
0380 tr_09.set(pass, fail, fail, fail, fail, fail, fail, redy);
0381 evSelTest(ps_i, tr_09, false);
0382 tr_09.set(fail, fail, pass, fail, fail, fail, pass, fail);
0383 evSelTest(ps_i, tr_09, true);
0384
0385
0386 PathSpecifiers ps_j("*&noexception", "!*&noexception");
0387 TrigResults tr_10(fail, pass, fail, fail, fail, fail, fail, fail);
0388 evSelTest(ps_j, tr_10, true);
0389 tr_10.set(pass, fail, pass, fail, excp, fail, pass, fail);
0390 evSelTest(ps_j, tr_10, false);
0391 tr_10.set(fail, fail, pass, pass, fail, pass, excp, excp);
0392 evSelTest(ps_j, tr_10, false);
0393 tr_10.set(fail, fail, fail, fail, fail, fail, fail, redy);
0394 evSelTest(ps_j, tr_10, false);
0395 tr_10.set(fail, fail, fail, fail, fail, fail, fail, fail);
0396 evSelTest(ps_j, tr_10, true);
0397 tr_10.set(pass, pass, pass, pass, pass, pass, pass, pass);
0398 evSelTest(ps_j, tr_10, true);
0399 tr_10.set(redy, redy, redy, redy, redy, redy, redy, redy);
0400 evSelTest(ps_j, tr_10, false);
0401
0402
0403 PathSpecifiers ps_k("exception@*");
0404 TrigResults tr_11(fail, pass, fail, fail, fail, fail, fail, fail);
0405 evSelTest(ps_k, tr_11, false);
0406 tr_11.set(pass, fail, pass, fail, excp, fail, pass, fail);
0407 evSelTest(ps_k, tr_11, true);
0408 tr_11.set(redy, redy, redy, redy, redy, redy, redy, excp);
0409 evSelTest(ps_k, tr_11, true);
0410 tr_11.set(pass, pass, pass, pass, pass, pass, pass, excp);
0411 evSelTest(ps_k, tr_11, true);
0412 tr_11.set(redy, redy, redy, redy, redy, redy, redy, redy);
0413 evSelTest(ps_k, tr_11, false);
0414 tr_11.set(pass, fail, fail, fail, fail, fail, fail, excp);
0415 evSelTest(ps_k, tr_11, true);
0416
0417
0418 PathSpecifiers ps_m("exception@a*", "exception@bp1");
0419 TrigResults tr_12(fail, pass, fail, fail, fail, fail, fail, fail);
0420 evSelTest(ps_m, tr_12, false);
0421 tr_12.set(pass, fail, pass, fail, excp, fail, pass, fail);
0422 evSelTest(ps_m, tr_12, true);
0423 tr_12.set(redy, redy, excp, redy, redy, redy, redy, excp);
0424 evSelTest(ps_m, tr_12, true);
0425 tr_12.set(pass, pass, pass, pass, pass, pass, pass, excp);
0426 evSelTest(ps_m, tr_12, false);
0427
0428
0429 PathSpecifiers ps_n("*", "!*", "exception@*");
0430 TrigResults tr_13(fail, pass, fail, fail, fail, fail, fail, fail);
0431 evSelTest(ps_n, tr_13, true);
0432 tr_13.set(pass, pass, pass, pass, pass, pass, pass, pass);
0433 evSelTest(ps_n, tr_13, true);
0434 tr_13.set(redy, redy, redy, redy, redy, redy, redy, excp);
0435 evSelTest(ps_n, tr_13, true);
0436 tr_13.set(redy, redy, redy, redy, redy, redy, redy, redy);
0437 evSelTest(ps_n, tr_13, true);
0438 tr_13.set(pass, pass, pass, pass, pass, pass, pass, excp);
0439 evSelTest(ps_n, tr_13, true);
0440 tr_13.set(excp, excp, excp, excp, excp, excp, excp, excp);
0441 evSelTest(ps_n, tr_13, true);
0442 tr_13.set(fail, redy, redy, redy, redy, redy, redy, redy);
0443 evSelTest(ps_n, tr_13, true);
0444 tr_13.set(fail, fail, fail, fail, fail, fail, fail, fail);
0445 evSelTest(ps_n, tr_13, true);
0446
0447
0448 }