Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:28:16

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