Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:12:20

0001 
0002 #include "FWCore/Framework/interface/one/OutputModule.h"
0003 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0004 
0005 #include "DataFormats/Common/interface/Handle.h"
0006 #include "DataFormats/Common/interface/TriggerResults.h"
0007 #include "DataFormats/Provenance/interface/ModuleDescription.h"
0008 #include "FWCore/Framework/interface/EventForOutput.h"
0009 #include "FWCore/Framework/interface/LuminosityBlockForOutput.h"
0010 #include "FWCore/Framework/interface/MakerMacros.h"
0011 #include "FWCore/Framework/interface/RunForOutput.h"
0012 #include "FWCore/ServiceRegistry/interface/ModuleCallingContext.h"
0013 
0014 #include <cassert>
0015 #include <string>
0016 #include <iostream>
0017 #include <algorithm>
0018 #include <numeric>
0019 #include <iterator>
0020 
0021 using namespace edm;
0022 
0023 using Trig = detail::TriggerResultsBasedEventSelector::handle_t;
0024 
0025 namespace {
0026 
0027   void printBits(unsigned char c) {
0028     //cout << "HEX: "<< "0123456789ABCDEF"[((c >> 4) & 0xF)] << std::endl;
0029 
0030     for (int i = 7; i >= 0; --i) {
0031       int bit = ((c >> i) & 1);
0032       std::cout << " " << bit;
0033     }
0034   }
0035 
0036   void packIntoString(std::vector<unsigned char> const& source, std::vector<unsigned char>& package) {
0037     unsigned int packInOneByte = 4;
0038     unsigned int sizeOfPackage = 1 + ((source.size() - 1) / packInOneByte);  //Two bits per HLT
0039     if (source.size() == 0)
0040       sizeOfPackage = 0;
0041 
0042     package.resize(sizeOfPackage);
0043     memset(&package[0], 0x00, sizeOfPackage);
0044 
0045     for (unsigned int i = 0; i != source.size(); ++i) {
0046       unsigned int whichByte = i / packInOneByte;
0047       unsigned int indxWithinByte = i % packInOneByte;
0048       package[whichByte] = package[whichByte] | (source[i] << (indxWithinByte * 2));
0049     }
0050     //for (unsigned int i=0; i !=package.size() ; ++i)
0051     //   printBits(package[i]);
0052     std::cout << std::endl;
0053   }
0054 
0055 }  // namespace
0056 namespace edmtest {
0057 
0058   class TestOutputModule : public edm::one::OutputModule<> {
0059   public:
0060     explicit TestOutputModule(edm::ParameterSet const&);
0061     virtual ~TestOutputModule();
0062 
0063   private:
0064     Trig getTriggerResults(EDGetTokenT<TriggerResults> const& token, EventForOutput const& e) const;
0065 
0066     virtual void write(edm::EventForOutput const& e) override;
0067     virtual void writeLuminosityBlock(edm::LuminosityBlockForOutput const&) override;
0068     virtual void writeRun(edm::RunForOutput const&) override;
0069     virtual void endJob() override;
0070 
0071     std::string name_;
0072     int bitMask_;
0073     std::vector<unsigned char> hltbits_;
0074     bool expectTriggerResults_;
0075     edm::EDGetTokenT<edm::TriggerResults> resultsToken_;
0076   };
0077 
0078   // -----------------------------------------------------------------
0079 
0080   TestOutputModule::TestOutputModule(edm::ParameterSet const& ps)
0081       : edm::one::OutputModuleBase(ps),
0082         edm::one::OutputModule<>(ps),
0083         name_(ps.getParameter<std::string>("name")),
0084         bitMask_(ps.getParameter<int>("bitMask")),
0085         hltbits_(0),
0086         expectTriggerResults_(ps.getUntrackedParameter<bool>("expectTriggerResults", true)),
0087         resultsToken_(consumes(edm::InputTag("TriggerResults"))) {}
0088 
0089   TestOutputModule::~TestOutputModule() {}
0090 
0091   Trig TestOutputModule::getTriggerResults(EDGetTokenT<TriggerResults> const& token,
0092                                            EventForOutput const& event) const {
0093     return event.getHandle(token);
0094   }
0095 
0096   void TestOutputModule::write(edm::EventForOutput const& e) {
0097     assert(e.moduleCallingContext()->moduleDescription()->moduleLabel() == description().moduleLabel());
0098 
0099     Trig prod;
0100 
0101     // There should not be a TriggerResults object in the event
0102     // if all three of the following requirements are met:
0103     //
0104     //     1.  MakeTriggerResults has not been explicitly set true
0105     //     2.  There are no filter modules in any path
0106     //     3.  The input file of the job does not have a TriggerResults object
0107     //
0108     // The user of this test module is expected to know
0109     // whether these conditions are met and let the module know
0110     // if no TriggerResults object is expected using the configuration
0111     // file.  In this case, the next few lines of code will abort
0112     // if a TriggerResults object is found.
0113 
0114     if (!expectTriggerResults_) {
0115       try {
0116         prod = getTriggerResults(resultsToken_, e);
0117         //throw doesn't happen until we dereference
0118         *prod;
0119       } catch (const cms::Exception&) {
0120         // We did not find one as expected, nothing else to test.
0121         return;
0122       }
0123       std::cerr << "\nTestOutputModule::write\n"
0124                 << "Expected there to be no TriggerResults object but we found one" << std::endl;
0125       abort();
0126     }
0127 
0128     // Now deal with the other case where we expect the object
0129     // to be present.
0130 
0131     prod = getTriggerResults(resultsToken_, e);
0132 
0133     std::vector<unsigned char> vHltState;
0134 
0135     std::vector<std::string> hlts = getAllTriggerNames();
0136     unsigned int hltSize = hlts.size();
0137 
0138     for (unsigned int i = 0; i != hltSize; ++i) {
0139       vHltState.push_back(((prod->at(i)).state()));
0140     }
0141 
0142     //Pack into member hltbits_
0143     packIntoString(vHltState, hltbits_);
0144 
0145     //This is Just a printing code.
0146     std::cout << "Size of hltbits:" << hltbits_.size() << std::endl;
0147 
0148     char* intp = (char*)&bitMask_;
0149     bool matched = false;
0150 
0151     for (int i = hltbits_.size() - 1; i != -1; --i) {
0152       std::cout << std::endl << "Current Bits Mask byte:";
0153       printBits(hltbits_[i]);
0154       unsigned char tmp = static_cast<unsigned char>(*(intp + i));
0155       std::cout << std::endl << "Original Byte:";
0156       printBits(tmp);
0157       std::cout << std::endl;
0158 
0159       if (tmp == hltbits_[i])
0160         matched = true;
0161     }
0162     std::cout << "\n";
0163 
0164     if (!matched && hltSize > 0) {
0165       std::cerr << "\ncfg bitMask is different from event..aborting." << std::endl;
0166 
0167       abort();
0168     } else
0169       std::cout << "\nSUCCESS: Found Matching Bits" << std::endl;
0170   }
0171 
0172   void TestOutputModule::writeLuminosityBlock(edm::LuminosityBlockForOutput const& lb) {
0173     assert(lb.moduleCallingContext()->moduleDescription()->moduleLabel() == description().moduleLabel());
0174   }
0175 
0176   void TestOutputModule::writeRun(edm::RunForOutput const& r) {
0177     assert(r.moduleCallingContext()->moduleDescription()->moduleLabel() == description().moduleLabel());
0178   }
0179 
0180   void TestOutputModule::endJob() {}
0181 }  // namespace edmtest
0182 using edmtest::TestOutputModule;
0183 
0184 DEFINE_FWK_MODULE(TestOutputModule);