Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 
0002 #include "FWCore/Framework/interface/EventSelector.h"
0003 #include "DataFormats/Common/interface/TriggerResults.h"
0004 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0005 #include "FWCore/Framework/interface/TriggerNamesService.h"
0006 #include "FWCore/ServiceRegistry/interface/ServiceWrapper.h"
0007 #include "FWCore/ServiceRegistry/interface/ServiceRegistry.h"
0008 #include "FWCore/ServiceRegistry/interface/ServiceToken.h"
0009 #include "FWCore/Utilities/interface/Exception.h"
0010 
0011 #include <array>
0012 #include <vector>
0013 #include <string>
0014 #include <iostream>
0015 #include <memory>
0016 
0017 using namespace edm;
0018 
0019 size_t const numBits = 12;  // There must be a better way than this but I choose to
0020                             // avoid modifying a whole slew of code using the array
0021                             // instead of push_back()s.
0022 
0023 typedef std::vector<std::vector<bool> > Answers;
0024 
0025 typedef std::vector<std::string> Strings;
0026 typedef std::vector<Strings> VStrings;
0027 typedef std::vector<bool> Bools;
0028 typedef std::vector<Bools> VBools;
0029 
0030 std::ostream& operator<<(std::ostream& ost, const Strings& s) {
0031   for (Strings::const_iterator i(s.begin()), e(s.end()); i != e; ++i) {
0032     ost << *i << " ";
0033   }
0034   return ost;
0035 }
0036 
0037 std::ostream& operator<<(std::ostream& ost, const Bools& b) {
0038   for (unsigned int i = 0; i < b.size(); ++i) {
0039     ost << b[i] << " ";
0040   }
0041   return ost;
0042 }
0043 
0044 template <size_t nb>
0045 Bools toBools(std::array<bool, nb> const& t) {
0046   Bools b;
0047   b.insert(b.end(), t.begin(), t.end());
0048   return b;
0049 }
0050 
0051 void testone(const Strings& paths, const Strings& pattern, const Bools& mask, bool answer, int jmask) {
0052   ParameterSet pset;  //, parent;
0053   pset.addParameter<Strings>("SelectEvents", pattern);
0054   pset.registerIt();
0055   //parent.addUntrackedParameter<ParameterSet>("SelectEvents",pset);
0056   //parent.registerIt();
0057 
0058   // There are 3 different ways to build the EventSelector.  All
0059   // should give the same result.  We exercise all 3 here.
0060   EventSelector select_based_on_pset(pset, paths);
0061   EventSelector select_based_on_pattern_paths(pattern, paths);
0062   EventSelector select_based_on_pattern(pattern);
0063 
0064   int number_of_trigger_paths = 0;
0065   std::vector<unsigned char> bitArray;
0066 
0067   HLTGlobalStatus bm(mask.size());
0068   const HLTPathStatus pass = HLTPathStatus(edm::hlt::Pass);
0069   const HLTPathStatus fail = HLTPathStatus(edm::hlt::Fail);
0070   const HLTPathStatus ex = HLTPathStatus(edm::hlt::Exception);
0071   const HLTPathStatus ready = HLTPathStatus(edm::hlt::Ready);
0072   for (unsigned int b = 0; b < mask.size(); ++b) {
0073     bm[b] = (mask[b] ? pass : fail);
0074 
0075     // There is an alternate version of the function acceptEvent
0076     // that takes an array of characters as an argument instead
0077     // of a TriggerResults object.  These next few lines build
0078     // that array so we can test that also.
0079     if ((number_of_trigger_paths % 4) == 0)
0080       bitArray.push_back(0);
0081     int byteIndex = number_of_trigger_paths / 4;
0082     int subIndex = number_of_trigger_paths % 4;
0083     bitArray[byteIndex] |= (mask[b] ? edm::hlt::Pass : edm::hlt::Fail) << (subIndex * 2);
0084     ++number_of_trigger_paths;
0085   }
0086 
0087   if (jmask == 8 && mask.size() > 4) {
0088     bm[0] = ready;
0089     bm[4] = ex;
0090     bitArray[0] = (bitArray[0] & 0xfc) | edm::hlt::Ready;
0091     bitArray[1] = (bitArray[1] & 0xfc) | edm::hlt::Exception;
0092   }
0093 
0094   TriggerResults results(bm, paths);
0095 
0096   bool a = select_based_on_pset.acceptEvent(results);
0097   bool b = select_based_on_pattern_paths.acceptEvent(results);
0098   bool c = select_based_on_pattern.acceptEvent(results);
0099   bool ab = select_based_on_pset.acceptEvent(&(bitArray[0]), number_of_trigger_paths);
0100   bool bb = select_based_on_pattern_paths.acceptEvent(&(bitArray[0]), number_of_trigger_paths);
0101   // select_based_on_pattern.acceptEvent(&(bitArray[0]),
0102   //                                     number_of_trigger_paths);
0103   // is not a valid way to use acceptEvent.
0104 
0105   if (a != answer || b != answer || c != answer || ab != answer || bb != answer) {
0106     std::cerr << "failed to compare pattern with mask: "
0107               << "correct=" << answer << " "
0108               << "results=" << a << "  " << b << "  " << c << "  " << ab << "  " << bb << "\n"
0109               << "pattern=" << pattern << "\n"
0110               << "mask=" << mask << "\n"
0111               << "jmask = " << jmask << "\n";
0112     abort();
0113   }
0114 
0115   // Repeat putting the list of trigger names in the pset
0116   // registry
0117 
0118   ParameterSet trigger_pset;
0119   trigger_pset.addParameter<Strings>("@trigger_paths", paths);
0120   trigger_pset.registerIt();
0121 
0122   TriggerResults results_id(bm, trigger_pset.id());
0123 
0124   bool x = select_based_on_pset.acceptEvent(results_id);
0125   bool y = select_based_on_pattern_paths.acceptEvent(results_id);
0126   bool z = select_based_on_pattern.acceptEvent(results_id);
0127 
0128   if (x != answer || y != answer || z != answer) {
0129     std::cerr << "failed to compare pattern with mask using pset ID: "
0130               << "correct=" << answer << " "
0131               << "results=" << x << "  " << y << "  " << z << "\n"
0132               << "pattern=" << pattern << "\n"
0133               << "mask=" << mask << "\n"
0134               << "jmask = " << jmask << "\n";
0135     abort();
0136   }
0137 }
0138 
0139 void testall(const Strings& paths, const VStrings& patterns, const VBools& masks, const Answers& answers) {
0140   for (unsigned int i = 0; i < patterns.size(); ++i) {
0141     for (unsigned int j = 0; j < masks.size(); ++j) {
0142       testone(paths, patterns[i], masks[j], answers[i][j], j);
0143     }
0144   }
0145 }
0146 
0147 int main() try {
0148   // Name all our paths. We have as many paths as there are trigger
0149   // bits.
0150 
0151   std::array<char const*, numBits> cpaths = {{
0152       "HLTx1",
0153       "HLTx2",
0154       "HLTy1",
0155       "HLTy2",
0156       "CALIBx1",
0157       "CALIBx2",
0158       "CALIBy1",
0159       "CALIBy2",
0160       "DEBUGx1",
0161       "DEBUGx2",
0162       "DEBUGy1",
0163       "DEBUGy2",
0164   }};
0165   Strings paths(cpaths.begin(), cpaths.end());
0166 
0167   // Create our test patterns.  Each of these will be tested against each mask.
0168 
0169   VStrings patterns;
0170 
0171   Strings criteria_star;
0172   criteria_star.push_back("*");
0173   patterns.push_back(criteria_star);
0174   Strings criteria_notstar;
0175   criteria_notstar.push_back("!*");
0176   patterns.push_back(criteria_notstar);
0177   Strings criteria0;
0178   criteria0.push_back("HLTx1");
0179   criteria0.push_back("HLTy1");
0180   patterns.push_back(criteria0);
0181   Strings criteria1;
0182   criteria1.push_back("CALIBx2");
0183   criteria1.push_back("!HLTx2");
0184   patterns.push_back(criteria1);
0185   Strings criteria2;
0186   criteria2.push_back("HLT*");
0187   patterns.push_back(criteria2);
0188   Strings criteria3;
0189   criteria3.push_back("!HLT*");
0190   patterns.push_back(criteria3);
0191   Strings criteria4;
0192   criteria4.push_back("DEBUG*1");
0193   criteria4.push_back("HLT?2");
0194   patterns.push_back(criteria4);
0195   Strings criteria5;
0196   criteria5.push_back("D*x1");
0197   criteria5.push_back("CALIBx*");
0198   patterns.push_back(criteria5);
0199   Strings criteria6;
0200   criteria6.push_back("HL*1");
0201   criteria6.push_back("C?LIB*2");
0202   patterns.push_back(criteria6);
0203   Strings criteria7;
0204   criteria7.push_back("H*x1");
0205   patterns.push_back(criteria7);
0206   Strings criteria8;
0207   criteria8.push_back("!H*x1");
0208   patterns.push_back(criteria8);
0209   Strings criteria9;
0210   criteria9.push_back("C?LIB*2");
0211   patterns.push_back(criteria9);
0212 
0213   // Create our test trigger masks.
0214 
0215   VBools testmasks;
0216 
0217   std::array<bool, numBits> t0 = {{false, false, false, false, false, false, false, false, false, false, false, false}};
0218   testmasks.push_back(toBools(t0));
0219   std::array<bool, numBits> t1 = {{true, true, true, true, true, true, true, true, true, true, true, true}};
0220   testmasks.push_back(toBools(t1));
0221   std::array<bool, numBits> t2 = {{true, false, false, false, false, false, false, false, false, false, false, false}};
0222   testmasks.push_back(toBools(t2));
0223   std::array<bool, numBits> t3 = {{false, true, false, false, false, false, false, false, false, false, false, false}};
0224   testmasks.push_back(toBools(t3));
0225 
0226   std::array<bool, numBits> t4 = {{false, false, false, false, false, false, false, false, true, false, false, false}};
0227   testmasks.push_back(toBools(t4));
0228   std::array<bool, numBits> t5 = {{true, true, true, true, false, false, true, false, false, false, false, false}};
0229   testmasks.push_back(toBools(t5));
0230   std::array<bool, numBits> t6 = {{false, false, false, false, false, true, false, false, false, false, true, false}};
0231   testmasks.push_back(toBools(t6));
0232   std::array<bool, numBits> t7 = {{true, false, true, false, false, true, true, false, false, true, false, true}};
0233   testmasks.push_back(toBools(t7));
0234   std::array<bool, numBits> t8 = {{false, false, false, false, false, true, false, false, true, true, true, true}};
0235   testmasks.push_back(toBools(t8));  // For j=8 only, the first HLTx1 (false) is
0236                                      // reset to ready and the fifth CALIBx2 (true)
0237                                      // is reset to exception.
0238 
0239   // Create the answers
0240 
0241   Answers ans;
0242 
0243   std::vector<bool> ansstar;  // Answers for criteria star: {{ "*" }};
0244   ansstar.push_back(false);   // f f f f f f f f f f f f
0245   ansstar.push_back(true);    // t t t t t t t t t t t t
0246   ansstar.push_back(true);    // t f f f f f f f f f f f
0247   ansstar.push_back(true);    // f t f f f f f f f f f f
0248   ansstar.push_back(true);    // f f f f f f f f t f f f
0249   ansstar.push_back(true);    // t t t t f f t f f f f f
0250   ansstar.push_back(true);    // f f f f f t f f f f t f
0251   ansstar.push_back(true);    // t f f f f t t f f t f t
0252   ansstar.push_back(true);    // r f f f e t f f t t t t
0253 
0254   ans.push_back(ansstar);
0255 
0256   std::vector<bool> ansnotstar;  // Answers for criteria notstar: {{ "!*" }};
0257   ansnotstar.push_back(true);    // f f f f f f f f f f f f
0258   ansnotstar.push_back(false);   // t t t t t t t t t t t t
0259   ansnotstar.push_back(false);   // t f f f f f f f f f f f
0260   ansnotstar.push_back(false);   // f t f f f f f f f f f f
0261   ansnotstar.push_back(false);   // f f f f f f f f t f f f
0262   ansnotstar.push_back(false);   // t t t t f f t f f f f f
0263   ansnotstar.push_back(false);   // f f f f f t f f f f t f
0264   ansnotstar.push_back(false);   // t f f f f t t f f t f t
0265   ansnotstar.push_back(false);   // r f f f e t f f t t t t
0266 
0267   ans.push_back(ansnotstar);
0268 
0269   std::vector<bool> ans0;  // Answers for criteria 0:{{ "HLTx1", "HLTy1" }};
0270   ans0.push_back(false);   // f f f f f f f f f f f f
0271   ans0.push_back(true);    // t t t t t t t t t t t t
0272   ans0.push_back(true);    // t f f f f f f f f f f f
0273   ans0.push_back(false);   // f t f f f f f f f f f f
0274   ans0.push_back(false);   // f f f f f f f f t f f f
0275   ans0.push_back(true);    // t t t t f f t f f f f f
0276   ans0.push_back(false);   // f f f f f t f f f f t f
0277   ans0.push_back(true);    // t f f f f t t f f t f t
0278   ans0.push_back(false);   // r f f f e t f f t t t t
0279 
0280   ans.push_back(ans0);
0281 
0282   std::vector<bool> ans1;  // Answers for criteria 1:{{"CALIBx2","!HLTx2"}};
0283   ans1.push_back(true);    // f f f f f f f f f f f f
0284   ans1.push_back(true);    // t t t t t t t t t t t t
0285   ans1.push_back(true);    // t f f f f f f f f f f f
0286   ans1.push_back(false);   // f t f f f f f f f f f f
0287   ans1.push_back(true);    // f f f f f f f f t f f f
0288   ans1.push_back(false);   // t t t t f f t f f f f f
0289   ans1.push_back(true);    // f f f f f t f f f f t f
0290   ans1.push_back(true);    // t f f f f t t f f t f t
0291   ans1.push_back(true);    // r f f f e t f f t t t t
0292 
0293   ans.push_back(ans1);
0294 
0295   std::vector<bool> ans2;  // Answers for criteria 2:{{ "HLT*" }};
0296   ans2.push_back(false);   // f f f f f f f f f f f f
0297   ans2.push_back(true);    // t t t t t t t t t t t t
0298   ans2.push_back(true);    // t f f f f f f f f f f f
0299   ans2.push_back(true);    // f t f f f f f f f f f f
0300   ans2.push_back(false);   // f f f f f f f f t f f f
0301   ans2.push_back(true);    // t t t t f f t f f f f f
0302   ans2.push_back(false);   // f f f f f t f f f f t f
0303   ans2.push_back(true);    // t f f f f t t f f t f t
0304   ans2.push_back(false);   // r f f f e t f f t t t t
0305 
0306   ans.push_back(ans2);
0307 
0308   std::vector<bool> ans3;  // Answers for criteria 3:{{ "!HLT*" }};
0309   ans3.push_back(true);    // f f f f f f f f f f f f
0310   ans3.push_back(false);   // t t t t t t t t t t t t
0311   ans3.push_back(false);   // t f f f f f f f f f f f
0312   ans3.push_back(false);   // f t f f f f f f f f f f
0313   ans3.push_back(true);    // f f f f f f f f t f f f
0314   ans3.push_back(false);   // t t t t f f t f f f f f
0315   ans3.push_back(true);    // f f f f f t f f f f t f
0316   ans3.push_back(false);   // t f f f f t t f f t f t
0317   ans3.push_back(false);   // r f f f e t f f t t t t // ready is not fail
0318 
0319   ans.push_back(ans3);
0320 
0321   std::vector<bool> ans4;  // Answers for criteria 4:{{"DEBUG*1","HLT?2"}};
0322   ans4.push_back(false);   // f f f f f f f f f f f f
0323   ans4.push_back(true);    // t t t t t t t t t t t t
0324   ans4.push_back(false);   // t f f f f f f f f f f f
0325   ans4.push_back(true);    // f t f f f f f f f f f f
0326   ans4.push_back(true);    // f f f f f f f f t f f f
0327   ans4.push_back(true);    // t t t t f f t f f f f f
0328   ans4.push_back(true);    // f f f f f t f f f f t f
0329   ans4.push_back(false);   // t f f f f t t f f t f t
0330   ans4.push_back(true);    // r f f f e t f f t t t t
0331 
0332   ans.push_back(ans4);
0333 
0334   std::vector<bool> ans5;  // Answers for criteria 5:{{ "D*x1", "CALIBx*" }};
0335   ans5.push_back(false);   // f f f f f f f f f f f f
0336   ans5.push_back(true);    // t t t t t t t t t t t t
0337   ans5.push_back(false);   // t f f f f f f f f f f f
0338   ans5.push_back(false);   // f t f f f f f f f f f f
0339   ans5.push_back(true);    // f f f f f f f f t f f f
0340   ans5.push_back(false);   // t t t t f f t f f f f f
0341   ans5.push_back(true);    // f f f f f t f f f f t f
0342   ans5.push_back(true);    // t f f f f t t f f t f t
0343   ans5.push_back(true);    // r f f f e t f f t t t t
0344 
0345   ans.push_back(ans5);
0346 
0347   std::vector<bool> ans6;  // Answers for criteria 6:{{ "HL*1", "C?LIB*2" }};
0348   ans6.push_back(false);   // f f f f f f f f f f f f
0349   ans6.push_back(true);    // t t t t t t t t t t t t
0350   ans6.push_back(true);    // t f f f f f f f f f f f
0351   ans6.push_back(false);   // f t f f f f f f f f f f
0352   ans6.push_back(false);   // f f f f f f f f t f f f
0353   ans6.push_back(true);    // t t t t f f t f f f f f
0354   ans6.push_back(true);    // f f f f f t f f f f t f
0355   ans6.push_back(true);    // t f f f f t t f f t f t
0356   ans6.push_back(true);    // r f f f e t f f t t t t
0357 
0358   ans.push_back(ans6);
0359 
0360   std::vector<bool> ans7;  // Answers for criteria7:{{ "H*x1" }};
0361   ans7.push_back(false);   // f f f f f f f f f f f f
0362   ans7.push_back(true);    // t t t t t t t t t t t t
0363   ans7.push_back(true);    // t f f f f f f f f f f f
0364   ans7.push_back(false);   // f t f f f f f f f f f f
0365   ans7.push_back(false);   // f f f f f f f f t f f f
0366   ans7.push_back(true);    // t t t t f f t f f f f f
0367   ans7.push_back(false);   // f f f f f t f f f f t f
0368   ans7.push_back(true);    // t f f f f t t f f t f t
0369   ans7.push_back(false);   // r f f f e t f f t t t t
0370 
0371   ans.push_back(ans7);
0372 
0373   std::vector<bool> ans8;  // Answers for criteria8:{{ "!H*x1" }};
0374   ans8.push_back(true);    // f f f f f f f f f f f f
0375   ans8.push_back(false);   // t t t t t t t t t t t t
0376   ans8.push_back(false);   // t f f f f f f f f f f f
0377   ans8.push_back(true);    // f t f f f f f f f f f f
0378   ans8.push_back(true);    // f f f f f f f f t f f f
0379   ans8.push_back(false);   // t t t t f f t f f f f f
0380   ans8.push_back(true);    // f f f f f t f f f f t f
0381   ans8.push_back(false);   // t f f f f t t f f t f t
0382   ans8.push_back(false);   // r f f f e t f f t t t t -- false because ready does not
0383                            //                 itself cause an accept
0384 
0385   ans.push_back(ans8);
0386 
0387   std::vector<bool> ans9;  // Answers for criteria 9:{{ "C?LIB*2" }};
0388   ans9.push_back(false);   // f f f f f f f f f f f f
0389   ans9.push_back(true);    // t t t t t t t t t t t t
0390   ans9.push_back(false);   // t f f f f f f f f f f f
0391   ans9.push_back(false);   // f t f f f f f f f f f f
0392   ans9.push_back(false);   // f f f f f f f f t f f f
0393   ans9.push_back(false);   // t t t t f f t f f f f f
0394   ans9.push_back(true);    // f f f f f t f f f f t f
0395   ans9.push_back(true);    // t f f f f t t f f t f t
0396   ans9.push_back(true);    // r f f f e t f f t t t t
0397 
0398   ans.push_back(ans9);
0399 
0400   // The following code is identical to that in EventSelector_t.cpp:
0401 
0402   // We want to create the TriggerNamesService because it is used in
0403   // the tests.  We do that here, but first we need to build a minimal
0404   // parameter set to pass to its constructor.  Then we build the
0405   // service and setup the service system.
0406   ParameterSet proc_pset;
0407 
0408   std::string processName("HLT");
0409   proc_pset.addParameter<std::string>("@process_name", processName);
0410 
0411   ParameterSet trigPaths;
0412   trigPaths.addParameter<Strings>("@trigger_paths", paths);
0413   proc_pset.addParameter<ParameterSet>("@trigger_paths", trigPaths);
0414 
0415   Strings endPaths;
0416   proc_pset.addParameter<Strings>("@end_paths", endPaths);
0417 
0418   // We do not care what is in these parameters for the test, they
0419   // just need to exist.
0420   Strings dummy;
0421   for (size_t i = 0; i < numBits; ++i) {
0422     proc_pset.addParameter<Strings>(paths[i], dummy);
0423   }
0424   proc_pset.registerIt();
0425 
0426   // Now create and setup the service
0427   typedef edm::service::TriggerNamesService TNS;
0428   typedef serviceregistry::ServiceWrapper<TNS> w_TNS;
0429 
0430   auto tnsptr = std::make_shared<w_TNS>(std::make_unique<TNS>(proc_pset));
0431 
0432   ServiceToken serviceToken_ = ServiceRegistry::createContaining(tnsptr);
0433 
0434   //make the services available
0435   ServiceRegistry::Operate operate(serviceToken_);
0436 
0437   // We are ready to run some tests
0438 
0439   testall(paths, patterns, testmasks, ans);
0440   return 0;
0441 } catch (cms::Exception const& e) {
0442   std::cerr << e.explainSelf() << std::endl;
0443   return 1;
0444 } catch (std::exception const& e) {
0445   std::cerr << e.what() << std::endl;
0446   return 1;
0447 }