Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:02:38

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