Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-01-17 23:57:40

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   // There are 2 different ways to build the EventSelector.
0177   // Both should give the same result.  We exercise both here.
0178   EventSelector select_based_on_path_specifiers_and_names(ps.path, trigger_path_names);
0179   EventSelector select_based_on_path_specifiers_only(ps.path);
0180 
0181   int number_of_trigger_paths = 0;
0182   std::vector<unsigned char> bitArray;
0183 
0184   HLTGlobalStatus bm(tr.bit.size());
0185   for (unsigned int b = 0; b < tr.bit.size(); ++b) {
0186     bm[b] = (tr.bit[b]);
0187     // There is an alternate version of the function acceptEvent
0188     // that takes an array of characters as an argument instead
0189     // of a TriggerResults object.  These next few lines build
0190     // that array so we can test that also.
0191     if ((number_of_trigger_paths % 4) == 0)
0192       bitArray.push_back(0);
0193     int byteIndex = number_of_trigger_paths / 4;
0194     int subIndex = number_of_trigger_paths % 4;
0195     bitArray[byteIndex] |= (static_cast<unsigned char>(bm[b].state())) << (subIndex * 2);
0196     ++number_of_trigger_paths;
0197   }
0198 
0199   TriggerResults results(bm, trigger_path_names);
0200 
0201   bool a = select_based_on_path_specifiers_and_names.acceptEvent(results);
0202   bool b = select_based_on_path_specifiers_only.acceptEvent(results);
0203   bool aa = select_based_on_path_specifiers_and_names.acceptEvent(&(bitArray[0]), number_of_trigger_paths);
0204   // select_based_on_path_specifiers_only.acceptEvent(&(bitArray[0]),
0205   //                                     number_of_trigger_paths);
0206   // is not a valid way to use acceptEvent.
0207 
0208   if (a != ans || b != ans || aa != ans) {
0209     std::cerr << "failed to compare path specifiers with trigger results: "
0210               << "correct=" << ans << " "
0211               << "results=" << a << "  " << b << "  " << aa << "\n"
0212               << "pathspecs = " << ps.path << "\n"
0213               << "trigger results = " << tr << "\n";
0214     abort();
0215   }
0216 
0217   // Repeat putting the list of trigger names in the pset
0218   // registry
0219 
0220   ParameterSet trigger_pset;
0221   trigger_pset.addParameter<Strings>("@trigger_paths", trigger_path_names);
0222   trigger_pset.registerIt();
0223 
0224   TriggerResults results_id(bm, trigger_pset.id());
0225 
0226   bool x = select_based_on_path_specifiers_and_names.acceptEvent(results_id);
0227   bool y = select_based_on_path_specifiers_only.acceptEvent(results_id);
0228 
0229   if (x != ans || y != ans) {
0230     std::cerr << "failed to compare pathspecs with trigger results using pset ID: "
0231               << "correct=" << ans << " "
0232               << "results=" << x << "  " << y << "\n"
0233               << "pathspecs =" << ps.path << "\n"
0234               << "trigger results = " << tr << "\n";
0235     abort();
0236   }
0237 }
0238 
0239 int main() try {
0240   // We want to create the TriggerNamesService because it is used in
0241   // the tests.  We do that here, but first we need to build a minimal
0242   // parameter set to pass to its constructor.  Then we build the
0243   // service and setup the service system.
0244   ParameterSet proc_pset;
0245 
0246   std::string processName("HLT");
0247   proc_pset.addParameter<std::string>("@process_name", processName);
0248 
0249   ParameterSet trigPaths;
0250   trigPaths.addParameter<Strings>("@trigger_paths", trigger_path_names);
0251   proc_pset.addParameter<ParameterSet>("@trigger_paths", trigPaths);
0252 
0253   Strings endPaths;
0254   proc_pset.addParameter<Strings>("@end_paths", endPaths);
0255 
0256   // We do not care what is in these parameters for the test, they
0257   // just need to exist.
0258   Strings dummy;
0259   for (size_t i = 0; i < num_trig_paths; ++i) {
0260     proc_pset.addParameter<Strings>(trigger_path_names[i], dummy);
0261   }
0262   proc_pset.registerIt();
0263 
0264   // Now create and setup the service
0265   typedef edm::service::TriggerNamesService TNS;
0266   typedef serviceregistry::ServiceWrapper<TNS> w_TNS;
0267 
0268   auto tnsptr = std::make_shared<w_TNS>(std::make_unique<TNS>(proc_pset));
0269 
0270   ServiceToken serviceToken_ = ServiceRegistry::createContaining(tnsptr);
0271 
0272   //make the services available
0273   ServiceRegistry::Operate operate(serviceToken_);
0274 
0275   // We are ready to run some tests
0276 
0277   //    01 - Non-wildcard positives, exception accepted
0278   PathSpecifiers ps_a("ap2");
0279   TrigResults tr_01(fail, pass, fail, fail, fail, fail, excp, fail);
0280   evSelTest(ps_a, tr_01, true);
0281   tr_01.set(fail, pass, fail, fail, fail, fail, fail, fail);
0282   evSelTest(ps_a, tr_01, true);
0283   tr_01.set(pass, fail, pass, pass, fail, fail, fail, pass);
0284   evSelTest(ps_a, tr_01, false);
0285   tr_01.set(pass, excp, pass, pass, fail, fail, fail, pass);
0286   evSelTest(ps_a, tr_01, false);
0287   tr_01.set(pass, redy, pass, pass, fail, fail, fail, pass);
0288   evSelTest(ps_a, tr_01, false);
0289 
0290   //    02 - Non-wildcard negatives, exception accepted
0291   PathSpecifiers ps_b("!aq2");
0292   TrigResults tr_02(pass, pass, pass, fail, pass, pass, excp, pass);
0293   evSelTest(ps_b, tr_02, true);
0294   tr_02.set(pass, pass, pass, fail, pass, pass, pass, pass);
0295   evSelTest(ps_b, tr_02, true);
0296   tr_02.set(pass, pass, pass, pass, pass, pass, pass, pass);
0297   evSelTest(ps_b, tr_01, false);
0298   tr_02.set(pass, pass, pass, excp, pass, pass, pass, pass);
0299   evSelTest(ps_b, tr_01, false);
0300   tr_02.set(pass, pass, pass, redy, pass, pass, pass, pass);
0301   evSelTest(ps_b, tr_01, false);
0302 
0303   //    03 - Non-wildcard positives and negatives mixed, exception accepted
0304   PathSpecifiers ps_c("bp1", "!aq1", "!bq2");
0305   TrigResults tr_03(pass, pass, pass, pass, fail, pass, pass, pass);
0306   evSelTest(ps_c, tr_03, false);
0307   tr_03.set(excp, pass, pass, pass, pass, pass, pass, pass);
0308   evSelTest(ps_c, tr_03, true);
0309   tr_03.set(excp, pass, fail, pass, fail, pass, pass, pass);
0310   evSelTest(ps_c, tr_03, true);
0311   tr_03.set(excp, pass, pass, pass, fail, pass, pass, fail);
0312   evSelTest(ps_c, tr_03, true);
0313   tr_03.set(redy, pass, pass, pass, pass, pass, pass, fail);
0314   evSelTest(ps_c, tr_03, true);
0315 
0316   //    04 - Non-wildcard positives, exception not accepted
0317   PathSpecifiers ps_d("ap2&noexception");
0318   TrigResults tr_04(fail, pass, fail, fail, fail, fail, excp, fail);
0319   evSelTest(ps_d, tr_04, false);
0320   tr_04.set(fail, pass, fail, fail, fail, fail, fail, fail);
0321   evSelTest(ps_d, tr_04, true);
0322   tr_04.set(pass, fail, pass, pass, fail, fail, fail, pass);
0323   evSelTest(ps_d, tr_04, false);
0324   tr_04.set(pass, excp, pass, pass, fail, fail, fail, pass);
0325   evSelTest(ps_d, tr_04, false);
0326   tr_04.set(pass, pass, pass, pass, fail, fail, redy, pass);
0327   evSelTest(ps_d, tr_04, true);
0328 
0329   //    05 - Non-wildcard negatives, exception not accepted, mixed with 01 case
0330   PathSpecifiers ps_e("bp1", "!aq1&noexception", "!bq2");
0331   TrigResults tr_05(pass, pass, pass, pass, fail, pass, pass, pass);
0332   evSelTest(ps_e, tr_05, false);
0333   tr_05.set(excp, pass, pass, pass, pass, pass, pass, pass);
0334   evSelTest(ps_e, tr_05, true);
0335   tr_05.set(pass, pass, fail, pass, fail, pass, pass, pass);
0336   evSelTest(ps_e, tr_05, true);
0337   tr_05.set(pass, pass, fail, pass, fail, pass, excp, pass);
0338   evSelTest(ps_e, tr_05, false);
0339 
0340   //    06 - Wildcard positives, exception accepted
0341   PathSpecifiers ps_f("a*2", "?p2");
0342   TrigResults tr_06(fail, pass, fail, fail, fail, fail, excp, fail);
0343   evSelTest(ps_f, tr_06, true);
0344   tr_06.set(fail, fail, fail, pass, fail, fail, fail, fail);
0345   evSelTest(ps_f, tr_06, true);
0346   tr_06.set(pass, fail, pass, fail, fail, pass, excp, excp);
0347   evSelTest(ps_f, tr_06, true);
0348   tr_06.set(pass, fail, pass, fail, pass, fail, pass, pass);
0349   evSelTest(ps_f, tr_06, false);
0350 
0351   //    07 - Wildcard negatives, exception accepted
0352   PathSpecifiers ps_g("!*2", "!ap?");
0353   TrigResults tr_07(fail, pass, fail, fail, fail, fail, fail, fail);
0354   evSelTest(ps_g, tr_07, false);
0355   tr_07.set(pass, fail, pass, fail, excp, fail, pass, fail);
0356   evSelTest(ps_g, tr_07, true);
0357   tr_07.set(fail, fail, pass, pass, fail, pass, excp, excp);
0358   evSelTest(ps_g, tr_07, true);
0359   tr_07.set(pass, fail, fail, fail, fail, fail, fail, redy);
0360   evSelTest(ps_g, tr_07, false);
0361 
0362   //    08 - Wildcard positives, exception not accepted
0363   PathSpecifiers ps_h("a*2&noexception", "?p2");
0364   TrigResults tr_08(fail, pass, fail, fail, fail, fail, excp, fail);
0365   evSelTest(ps_h, tr_08, true);
0366   tr_08.set(fail, fail, fail, pass, fail, fail, excp, fail);
0367   evSelTest(ps_h, tr_08, false);
0368   tr_08.set(pass, fail, pass, fail, fail, pass, excp, excp);
0369   evSelTest(ps_h, tr_08, true);
0370   tr_08.set(pass, fail, pass, fail, pass, fail, pass, pass);
0371   evSelTest(ps_h, tr_08, false);
0372   tr_08.set(excp, fail, pass, pass, pass, fail, pass, pass);
0373   evSelTest(ps_h, tr_08, false);
0374 
0375   //    09 - Wildcard negatives, exception not accepted
0376   PathSpecifiers ps_i("!*2&noexception");
0377   TrigResults tr_09(fail, pass, fail, fail, fail, fail, fail, fail);
0378   evSelTest(ps_i, tr_09, false);
0379   tr_09.set(pass, fail, pass, fail, excp, fail, pass, fail);
0380   evSelTest(ps_i, tr_09, false);
0381   tr_09.set(fail, fail, pass, pass, fail, pass, excp, excp);
0382   evSelTest(ps_i, tr_09, false);
0383   tr_09.set(pass, fail, fail, fail, fail, fail, fail, redy);
0384   evSelTest(ps_i, tr_09, false);
0385   tr_09.set(fail, fail, pass, fail, fail, fail, pass, fail);
0386   evSelTest(ps_i, tr_09, true);
0387 
0388   //    10 - Everything except exceptions
0389   PathSpecifiers ps_j("*&noexception", "!*&noexception");
0390   TrigResults tr_10(fail, pass, fail, fail, fail, fail, fail, fail);
0391   evSelTest(ps_j, tr_10, true);
0392   tr_10.set(pass, fail, pass, fail, excp, fail, pass, fail);
0393   evSelTest(ps_j, tr_10, false);
0394   tr_10.set(fail, fail, pass, pass, fail, pass, excp, excp);
0395   evSelTest(ps_j, tr_10, false);
0396   tr_10.set(fail, fail, fail, fail, fail, fail, fail, redy);
0397   evSelTest(ps_j, tr_10, false);
0398   tr_10.set(fail, fail, fail, fail, fail, fail, fail, fail);
0399   evSelTest(ps_j, tr_10, true);
0400   tr_10.set(pass, pass, pass, pass, pass, pass, pass, pass);
0401   evSelTest(ps_j, tr_10, true);
0402   tr_10.set(redy, redy, redy, redy, redy, redy, redy, redy);
0403   evSelTest(ps_j, tr_10, false);  // rejected because all Ready fails !*
0404 
0405   //    11 - Exception demanded
0406   PathSpecifiers ps_k("exception@*");
0407   TrigResults tr_11(fail, pass, fail, fail, fail, fail, fail, fail);
0408   evSelTest(ps_k, tr_11, false);
0409   tr_11.set(pass, fail, pass, fail, excp, fail, pass, fail);
0410   evSelTest(ps_k, tr_11, true);
0411   tr_11.set(redy, redy, redy, redy, redy, redy, redy, excp);
0412   evSelTest(ps_k, tr_11, true);
0413   tr_11.set(pass, pass, pass, pass, pass, pass, pass, excp);
0414   evSelTest(ps_k, tr_11, true);
0415   tr_11.set(redy, redy, redy, redy, redy, redy, redy, redy);
0416   evSelTest(ps_k, tr_11, false);
0417   tr_11.set(pass, fail, fail, fail, fail, fail, fail, excp);
0418   evSelTest(ps_k, tr_11, true);
0419 
0420   //    12 - Specific and wildcarded exceptions
0421   PathSpecifiers ps_m("exception@a*", "exception@bp1");
0422   TrigResults tr_12(fail, pass, fail, fail, fail, fail, fail, fail);
0423   evSelTest(ps_m, tr_12, false);
0424   tr_12.set(pass, fail, pass, fail, excp, fail, pass, fail);
0425   evSelTest(ps_m, tr_12, true);
0426   tr_12.set(redy, redy, excp, redy, redy, redy, redy, excp);
0427   evSelTest(ps_m, tr_12, true);
0428   tr_12.set(pass, pass, pass, pass, pass, pass, pass, excp);
0429   evSelTest(ps_m, tr_12, false);
0430 
0431   //    13 - Everything - also tests that it accepts all Ready
0432   PathSpecifiers ps_n("*", "!*", "exception@*");
0433   TrigResults tr_13(fail, pass, fail, fail, fail, fail, fail, fail);
0434   evSelTest(ps_n, tr_13, true);
0435   tr_13.set(pass, pass, pass, pass, pass, pass, pass, pass);
0436   evSelTest(ps_n, tr_13, true);
0437   tr_13.set(redy, redy, redy, redy, redy, redy, redy, excp);
0438   evSelTest(ps_n, tr_13, true);
0439   tr_13.set(redy, redy, redy, redy, redy, redy, redy, redy);
0440   evSelTest(ps_n, tr_13, true);
0441   tr_13.set(pass, pass, pass, pass, pass, pass, pass, excp);
0442   evSelTest(ps_n, tr_13, true);
0443   tr_13.set(excp, excp, excp, excp, excp, excp, excp, excp);
0444   evSelTest(ps_n, tr_13, true);
0445   tr_13.set(fail, redy, redy, redy, redy, redy, redy, redy);
0446   evSelTest(ps_n, tr_13, true);
0447   tr_13.set(fail, fail, fail, fail, fail, fail, fail, fail);
0448   evSelTest(ps_n, tr_13, true);
0449 
0450   // Now test testSelectionOverlap
0451 
0452   return 0;
0453 } catch (cms::Exception const& e) {
0454   std::cerr << e.explainSelf() << std::endl;
0455   return 1;
0456 } catch (std::exception const& e) {
0457   std::cerr << e.what() << std::endl;
0458   return 1;
0459 }