Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 // -*- C++ -*-
0002 //
0003 // Package:    Modules
0004 // Class:      EventIDChecker
0005 //
0006 /**\class EventIDChecker EventIDChecker.cc FWCore/Modules/src/EventIDChecker.cc
0007 
0008  Description: Checks that the events passed to it come in the order specified in its configuration
0009 
0010  Implementation:
0011      <Notes on implementation>
0012 */
0013 //
0014 // Original Author:  Chris Jones
0015 //         Created:  Tue Jun 16 15:42:17 CDT 2009
0016 //
0017 //
0018 
0019 // user include files
0020 #include "DataFormats/Provenance/interface/EventID.h"
0021 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0022 #include "FWCore/Framework/interface/Event.h"
0023 #include "FWCore/Framework/interface/MakerMacros.h"
0024 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0025 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0026 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0027 
0028 // system include files
0029 #include <algorithm>
0030 #include <memory>
0031 #include <vector>
0032 
0033 //
0034 // class decleration
0035 //
0036 
0037 class EventIDChecker : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm::one::WatchLuminosityBlocks> {
0038 public:
0039   explicit EventIDChecker(edm::ParameterSet const&);
0040   ~EventIDChecker() override;
0041   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0042 
0043 private:
0044   void beginJob() override;
0045   void beginRun(edm::Run const&, edm::EventSetup const&) override;
0046   void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
0047   void analyze(edm::Event const&, edm::EventSetup const&) override;
0048   void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
0049   void endRun(edm::Run const&, edm::EventSetup const&) override;
0050   void endJob() override;
0051 
0052   // ----------member data ---------------------------
0053   std::vector<edm::EventID> ids_;
0054   unsigned int index_;
0055   edm::RunNumber_t presentRun_ = 0;
0056   edm::LuminosityBlockNumber_t presentLumi_ = 0;
0057 
0058   unsigned int multiProcessSequentialEvents_;
0059   unsigned int numberOfEventsLeftBeforeSearch_;
0060   bool mustSearch_;
0061 };
0062 
0063 //
0064 // constants, enums and typedefs
0065 //
0066 
0067 //
0068 // static data member definitions
0069 //
0070 
0071 //
0072 // constructors and destructor
0073 //
0074 EventIDChecker::EventIDChecker(edm::ParameterSet const& iConfig)
0075     : ids_(iConfig.getUntrackedParameter<std::vector<edm::EventID> >("eventSequence")),
0076       index_(0),
0077       multiProcessSequentialEvents_(iConfig.getUntrackedParameter<unsigned int>("multiProcessSequentialEvents")),
0078       numberOfEventsLeftBeforeSearch_(0),
0079       mustSearch_(false) {
0080   //now do what ever initialization is needed
0081 }
0082 
0083 EventIDChecker::~EventIDChecker() {
0084   // do anything here that needs to be done at desctruction time
0085   // (e.g. close files, deallocate resources etc.)
0086 }
0087 
0088 //
0089 // member functions
0090 //
0091 
0092 namespace {
0093   struct CompareWithoutLumi {
0094     CompareWithoutLumi(edm::EventID const& iThis) : m_this(iThis) {}
0095     bool operator()(edm::EventID const& iOther) {
0096       return m_this.run() == iOther.run() && m_this.event() == iOther.event();
0097     }
0098     edm::EventID m_this;
0099   };
0100 }  // namespace
0101 
0102 // ------------ method called to for each event  ------------
0103 void EventIDChecker::analyze(edm::Event const& iEvent, edm::EventSetup const&) {
0104   if (mustSearch_) {
0105     if (0 == numberOfEventsLeftBeforeSearch_) {
0106       numberOfEventsLeftBeforeSearch_ = multiProcessSequentialEvents_;
0107       //the event must be after the last event in our list since multicore doesn't go backwards
0108       std::vector<edm::EventID>::iterator itFind =
0109           std::find_if(ids_.begin() + index_, ids_.end(), CompareWithoutLumi(iEvent.id()));
0110       if (itFind == ids_.end()) {
0111         throw cms::Exception("MissedEvent") << "The event " << iEvent.id() << "is not in the list.\n";
0112       }
0113       index_ = itFind - ids_.begin();
0114     }
0115     --numberOfEventsLeftBeforeSearch_;
0116   }
0117 
0118   if (index_ >= ids_.size()) {
0119     throw cms::Exception("TooManyEvents")
0120         << "Was passes " << ids_.size() << " EventIDs but have processed more events than that\n";
0121   }
0122   if (iEvent.id().run() != ids_[index_].run() || iEvent.id().event() != ids_[index_].event()) {
0123     throw cms::Exception("WrongEvent") << "Was expecting event " << ids_[index_] << " but was given " << iEvent.id()
0124                                        << "\n";
0125   }
0126 
0127   if (presentRun_ != iEvent.run()) {
0128     throw cms::Exception("MissingRunTransitionForEvent")
0129         << "at event expected Run " << presentRun_ << " but got " << iEvent.run();
0130   }
0131   if (presentLumi_ != iEvent.luminosityBlock()) {
0132     throw cms::Exception("MissingLuminosityBlockTransitionForEvent")
0133         << "expected LuminosityBlock " << presentLumi_ << " but got " << iEvent.luminosityBlock();
0134   }
0135 
0136   ++index_;
0137 }
0138 
0139 void EventIDChecker::beginRun(edm::Run const& iRun, edm::EventSetup const&) { presentRun_ = iRun.run(); }
0140 void EventIDChecker::beginLuminosityBlock(edm::LuminosityBlock const& iLumi, edm::EventSetup const&) {
0141   if (presentRun_ != iLumi.run()) {
0142     throw cms::Exception("WrongRunForLuminosityBlock")
0143         << "at beginLuminosityBlock expected Run " << presentRun_ << " but got " << iLumi.run();
0144   }
0145   presentLumi_ = iLumi.luminosityBlock();
0146 }
0147 
0148 void EventIDChecker::endLuminosityBlock(edm::LuminosityBlock const& iLumi, edm::EventSetup const&) {
0149   if (presentRun_ != iLumi.run()) {
0150     throw cms::Exception("WrongRunForLuminosityBlock")
0151         << "at endLuminosityBlock expected Run " << presentRun_ << " but got " << iLumi.run();
0152   }
0153   if (presentLumi_ != iLumi.luminosityBlock()) {
0154     throw cms::Exception("WrongEndLuminosityBlock")
0155         << "expected LuminosityBlock " << presentLumi_ << " but got " << iLumi.luminosityBlock();
0156   }
0157 }
0158 void EventIDChecker::endRun(edm::Run const& iRun, edm::EventSetup const&) {
0159   if (presentRun_ != iRun.run()) {
0160     throw cms::Exception("WrongEndRun") << "expected Run " << presentRun_ << " but got " << iRun.run();
0161   }
0162 }
0163 
0164 // ------------ method called once each job just before starting event loop  ------------
0165 void EventIDChecker::beginJob() {}
0166 
0167 // ------------ method called once each job just after ending the event loop  ------------
0168 void EventIDChecker::endJob() {}
0169 
0170 // ------------ method called once each job for validation
0171 void EventIDChecker::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0172   edm::ParameterSetDescription desc;
0173   desc.addUntracked<std::vector<edm::EventID> >("eventSequence");
0174   desc.addUntracked<unsigned int>("multiProcessSequentialEvents", 0U);
0175   descriptions.add("eventIDChecker", desc);
0176 }
0177 
0178 //define this as a plug-in
0179 DEFINE_FWK_MODULE(EventIDChecker);