Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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 const size_t numBits = 5;
0020 const int numPatterns = 11;
0021 const int numMasks = 9;
0022 
0023 typedef bool Answers[numPatterns][numMasks];
0024 typedef std::vector<std::string> Strings;
0025 typedef std::vector<Strings> VStrings;
0026 typedef std::vector<bool> Bools;
0027 typedef std::vector<Bools> VBools;
0028 
0029 std::ostream& operator<<(std::ostream& ost, const Strings& s) {
0030   for (Strings::const_iterator i(s.begin()), e(s.end()); i != e; ++i) {
0031     ost << *i << " ";
0032   }
0033   return ost;
0034 }
0035 
0036 std::ostream& operator<<(std::ostream& ost, const Bools& b) {
0037   for (unsigned int i = 0; i < b.size(); ++i) {
0038     ost << b[i] << " ";
0039   }
0040   return ost;
0041 }
0042 
0043 void testone(const Strings& paths, const Strings& pattern, const Bools& mask, bool answer, int jmask) {
0044   ParameterSet pset;  //, parent;
0045   pset.addParameter<Strings>("SelectEvents", pattern);
0046   //parent.addUntrackedParameter<ParameterSet>("SelectEvents",pset);
0047   pset.registerIt();
0048 
0049   // There are 3 different ways to build the EventSelector.  All
0050   // should give the same result.  We exercise all 3 here.
0051   EventSelector select(pset, paths);
0052   EventSelector select1(pattern, paths);
0053   EventSelector select2(pattern);
0054 
0055   int number_of_trigger_paths = 0;
0056   std::vector<unsigned char> bitArray;
0057 
0058   HLTGlobalStatus bm(mask.size());
0059   const HLTPathStatus pass = HLTPathStatus(edm::hlt::Pass);
0060   const HLTPathStatus fail = HLTPathStatus(edm::hlt::Fail);
0061   const HLTPathStatus ex = HLTPathStatus(edm::hlt::Exception);
0062   const HLTPathStatus ready = HLTPathStatus(edm::hlt::Ready);
0063   for (unsigned int b = 0; b < mask.size(); ++b) {
0064     bm[b] = (mask[b] ? pass : fail);
0065 
0066     // There is an alternate version of the function acceptEvent
0067     // that takes an array of characters as an argument instead
0068     // of a TriggerResults object.  These next few lines build
0069     // that array so we can test that also.
0070     if ((number_of_trigger_paths % 4) == 0)
0071       bitArray.push_back(0);
0072     int byteIndex = number_of_trigger_paths / 4;
0073     int subIndex = number_of_trigger_paths % 4;
0074     bitArray[byteIndex] |= (mask[b] ? edm::hlt::Pass : edm::hlt::Fail) << (subIndex * 2);
0075     ++number_of_trigger_paths;
0076   }
0077 
0078   if (jmask == 8 && mask.size() > 4) {
0079     bm[0] = ready;
0080     bm[4] = ex;
0081     bitArray[0] = (bitArray[0] & 0xfc) | edm::hlt::Ready;
0082     bitArray[1] = (bitArray[1] & 0xfc) | edm::hlt::Exception;
0083   }
0084 
0085   TriggerResults results(bm, paths);
0086 
0087   //        std::cerr << "pattern=" << pattern
0088   //          << "mask=" << mask << "\n";  // DBG
0089 
0090   //    std:: cerr << "a \n";
0091   bool a = select.acceptEvent(results);
0092   //    std:: cerr << "a1 \n";
0093   bool a1 = select1.acceptEvent(results);
0094   //    std:: cerr << "a2 \n";
0095   bool a2 = select2.acceptEvent(results);
0096   //    std:: cerr << "b2 \n";
0097   bool b2 = select2.acceptEvent(results);
0098   //    std:: cerr << "c1 \n";
0099   bool c1 = select1.acceptEvent(&(bitArray[0]), number_of_trigger_paths);
0100 
0101   if (a != answer || a1 != answer || a2 != answer || b2 != answer || c1 != answer) {
0102     std::cerr << "failed to compare pattern with mask: "
0103               << "correct=" << answer << " "
0104               << "results=" << a << "  " << a1 << "  " << a2 << "  " << b2 << "  " << c1 << "\n"
0105               << "pattern=" << pattern << "\n"
0106               << "mask=" << mask << "\n"
0107               << "jmask = " << jmask << "\n";
0108     abort();
0109   }
0110 
0111   // Repeat putting the list of trigger names in the pset
0112   // registry
0113 
0114   ParameterSet trigger_pset;
0115   trigger_pset.addParameter<Strings>("@trigger_paths", paths);
0116   trigger_pset.registerIt();
0117 
0118   TriggerResults results_id(bm, trigger_pset.id());
0119 
0120   //    std:: cerr << "a11 \n";
0121   bool a11 = select.acceptEvent(results_id);
0122   //    std:: cerr << "a12 \n";
0123   bool a12 = select1.acceptEvent(results_id);
0124   //    std:: cerr << "a13 \n";
0125   bool a13 = select2.acceptEvent(results_id);
0126   //    std:: cerr << "a14 \n";
0127   bool a14 = select2.acceptEvent(results_id);
0128 
0129   if (a11 != answer || a12 != answer || a13 != answer || a14 != answer) {
0130     std::cerr << "failed to compare pattern with mask using pset ID: "
0131               << "correct=" << answer << " "
0132               << "results=" << a11 << "  " << a12 << "  " << a13 << "  " << a14 << "\n"
0133               << "pattern=" << pattern << "\n"
0134               << "mask=" << mask << "\n"
0135               << "jmask = " << jmask << "\n";
0136     abort();
0137   }
0138 }
0139 
0140 void testall(const Strings& paths, const VStrings& patterns, const VBools& masks, const Answers& answers) {
0141   for (unsigned int i = 0; i < patterns.size(); ++i) {
0142     for (unsigned int j = 0; j < masks.size(); ++j) {
0143       testone(paths, patterns[i], masks[j], answers[i][j], j);
0144     }
0145   }
0146 }
0147 
0148 int main() try {
0149   // Name all our paths. We have as many paths as there are trigger
0150   // bits.
0151   std::array<char const*, numBits> cpaths = {{"a1", "a2", "a3", "a4", "a5"}};
0152   Strings paths(cpaths.begin(), cpaths.end());
0153 
0154   //
0155 
0156   std::array<char const*, 2> cw1 = {{"a1", "a2"}};
0157   std::array<char const*, 2> cw2 = {{"!a1", "!a2"}};
0158   std::array<char const*, 2> cw3 = {{"a1", "!a2"}};
0159   std::array<char const*, 1> cw4 = {{"*"}};
0160   std::array<char const*, 1> cw5 = {{"!*"}};
0161   std::array<char const*, 2> cw6 = {{"*", "!*"}};
0162   std::array<char const*, 2> cw7 = {{"*", "!a2"}};
0163   std::array<char const*, 2> cw8 = {{"!*", "a2"}};
0164   std::array<char const*, 3> cw9 = {{"a1", "a2", "a5"}};
0165   std::array<char const*, 2> cwA = {{"a3", "a4"}};
0166   std::array<char const*, 1> cwB = {{"!a5"}};
0167 
0168   VStrings patterns(numPatterns);
0169   patterns[0].insert(patterns[0].end(), cw1.begin(), cw1.end());
0170   patterns[1].insert(patterns[1].end(), cw2.begin(), cw2.end());
0171   patterns[2].insert(patterns[2].end(), cw3.begin(), cw3.end());
0172   patterns[3].insert(patterns[3].end(), cw4.begin(), cw4.end());
0173   patterns[4].insert(patterns[4].end(), cw5.begin(), cw5.end());
0174   patterns[5].insert(patterns[5].end(), cw6.begin(), cw6.end());
0175   patterns[6].insert(patterns[6].end(), cw7.begin(), cw7.end());
0176   patterns[7].insert(patterns[7].end(), cw8.begin(), cw8.end());
0177   patterns[8].insert(patterns[8].end(), cw9.begin(), cw9.end());
0178   patterns[9].insert(patterns[9].end(), cwA.begin(), cwA.end());
0179   patterns[10].insert(patterns[10].end(), cwB.begin(), cwB.end());
0180 
0181   std::array<bool, numBits> t1 = {{true, false, true, false, true}};
0182   std::array<bool, numBits> t2 = {{false, true, true, false, true}};
0183   std::array<bool, numBits> t3 = {{true, true, true, false, true}};
0184   std::array<bool, numBits> t4 = {{false, false, true, false, true}};
0185   std::array<bool, numBits> t5 = {{false, false, false, false, false}};
0186   std::array<bool, numBits> t6 = {{true, true, true, true, true}};
0187   std::array<bool, numBits> t7 = {{true, true, true, true, false}};
0188   std::array<bool, numBits> t8 = {{false, false, false, false, true}};
0189   std::array<bool, numBits> t9 = {{false, false, false, false, false}};  // for t9 only, above the
0190                                                                          // first is reset to ready
0191                                                                          // last is reset to exception
0192 
0193   VBools testmasks(numMasks);
0194   testmasks[0].insert(testmasks[0].end(), t1.begin(), t1.end());
0195   testmasks[1].insert(testmasks[1].end(), t2.begin(), t2.end());
0196   testmasks[2].insert(testmasks[2].end(), t3.begin(), t3.end());
0197   testmasks[3].insert(testmasks[3].end(), t4.begin(), t4.end());
0198   testmasks[4].insert(testmasks[4].end(), t5.begin(), t5.end());
0199   testmasks[5].insert(testmasks[5].end(), t6.begin(), t6.end());
0200   testmasks[6].insert(testmasks[6].end(), t7.begin(), t7.end());
0201   testmasks[7].insert(testmasks[7].end(), t8.begin(), t8.end());
0202   testmasks[8].insert(testmasks[8].end(), t9.begin(), t9.end());
0203 
0204   Answers ans = {
0205       {true, true, true, false, false, true, true, false, false},
0206       {true, true, false, true, true, false, false, true, true},
0207       {true, false, true, true, true, true, true, true, true},
0208       {true, true, true, true, false, true, true, true, false},  // last column changed due to treatment of excp
0209       {false, false, false, false, true, false, false, false, false},
0210       {true, true, true, true, true, true, true, true, false},  // last column changed due to treatment of excp
0211       {true, true, true, true, true, true, true, true, true},
0212       {false, true, true, false, true, true, true, false, false},
0213       {true, true, true, true, false, true, true, true, false},  // last column changed due to treatment of excp
0214       {true, true, true, true, false, true, true, false, false},
0215       {false, false, false, false, true, false, true, false, false}  // last column changed due to treatment of excp
0216   };
0217 
0218   // We want to create the TriggerNamesService because it is used in
0219   // the tests.  We do that here, but first we need to build a minimal
0220   // parameter set to pass to its constructor.  Then we build the
0221   // service and setup the service system.
0222   ParameterSet proc_pset;
0223 
0224   std::string processName("HLT");
0225   proc_pset.addParameter<std::string>("@process_name", processName);
0226 
0227   ParameterSet trigPaths;
0228   trigPaths.addParameter<Strings>("@trigger_paths", paths);
0229   proc_pset.addParameter<ParameterSet>("@trigger_paths", trigPaths);
0230 
0231   Strings endPaths;
0232   proc_pset.addParameter<Strings>("@end_paths", endPaths);
0233 
0234   // We do not care what is in these parameters for the test, they
0235   // just need to exist.
0236   Strings dummy;
0237   for (size_t i = 0; i < numBits; ++i) {
0238     proc_pset.addParameter<Strings>(paths[i], dummy);
0239   }
0240   proc_pset.registerIt();
0241 
0242   // Now create and setup the service
0243   typedef edm::service::TriggerNamesService TNS;
0244   typedef serviceregistry::ServiceWrapper<TNS> w_TNS;
0245 
0246   auto tnsptr = std::make_shared<w_TNS>(std::make_unique<TNS>(proc_pset));
0247 
0248   ServiceToken serviceToken_ = ServiceRegistry::createContaining(tnsptr);
0249 
0250   //make the services available
0251   ServiceRegistry::Operate operate(serviceToken_);
0252 
0253   // We are ready to run some tests
0254 
0255   testall(paths, patterns, testmasks, ans);
0256   return 0;
0257 } catch (cms::Exception const& e) {
0258   std::cerr << e.explainSelf() << std::endl;
0259   return 1;
0260 } catch (std::exception const& e) {
0261   std::cerr << e.what() << std::endl;
0262   return 1;
0263 }