Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 // -*- C++ -*-
0002 //
0003 // Package:    Modules
0004 // Class:      RunLumiEventChecker
0005 //
0006 /**\class RunLumiEventChecker RunLumiEventChecker.cc FWCore/Modules/src/RunLumiEventChecker.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/global/EDAnalyzer.h"
0022 #include "FWCore/Framework/interface/Event.h"
0023 #include "FWCore/Framework/interface/LuminosityBlock.h"
0024 #include "FWCore/Framework/interface/MakerMacros.h"
0025 #include "FWCore/Framework/interface/Run.h"
0026 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0027 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0028 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0029 #include "FWCore/Utilities/interface/UnixSignalHandlers.h"
0030 #include "FWCore/Utilities/interface/propagate_const.h"
0031 
0032 // system include files
0033 #include <algorithm>
0034 #include <map>
0035 #include <memory>
0036 #include <vector>
0037 #include <map>
0038 
0039 //
0040 // class decleration
0041 //
0042 namespace rlec {
0043   struct Cache {};
0044 }  // namespace rlec
0045 
0046 class RunLumiEventChecker
0047     : public edm::global::EDAnalyzer<edm::RunCache<rlec::Cache>, edm::LuminosityBlockCache<rlec::Cache>> {
0048 public:
0049   explicit RunLumiEventChecker(edm::ParameterSet const&);
0050   ~RunLumiEventChecker() override;
0051   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0052 
0053 private:
0054   void beginJob() override;
0055   void analyze(edm::StreamID, edm::Event const&, edm::EventSetup const&) const override;
0056   void endJob() override;
0057 
0058   std::shared_ptr<rlec::Cache> globalBeginRun(edm::Run const& run, edm::EventSetup const& es) const override;
0059   void globalEndRun(edm::Run const& run, edm::EventSetup const& es) const override;
0060 
0061   std::shared_ptr<rlec::Cache> globalBeginLuminosityBlock(edm::LuminosityBlock const& lumi,
0062                                                           edm::EventSetup const& es) const override;
0063   void globalEndLuminosityBlock(edm::LuminosityBlock const& lumi, edm::EventSetup const& es) const override;
0064 
0065   void check(edm::EventID const& iID, bool isEvent) const;
0066 
0067   // ----------member data ---------------------------
0068   std::vector<edm::EventID> ids_;
0069   mutable std::atomic<unsigned int> index_;
0070   bool unorderedEvents_;
0071 };
0072 
0073 //
0074 // constants, enums and typedefs
0075 //
0076 
0077 //
0078 // static data member definitions
0079 //
0080 
0081 //
0082 // constructors and destructor
0083 //
0084 RunLumiEventChecker::RunLumiEventChecker(edm::ParameterSet const& iConfig)
0085     : ids_(iConfig.getUntrackedParameter<std::vector<edm::EventID>>("eventSequence")),
0086       index_(0),
0087       unorderedEvents_(iConfig.getUntrackedParameter<bool>("unorderedEvents")) {
0088   //now do what ever initialization is needed
0089 }
0090 
0091 RunLumiEventChecker::~RunLumiEventChecker() {
0092   // do anything here that needs to be done at desctruction time
0093   // (e.g. close files, deallocate resources etc.)
0094 }
0095 
0096 //
0097 // member functions
0098 //
0099 
0100 void RunLumiEventChecker::check(edm::EventID const& iEventID, bool iIsEvent) const {
0101   if (index_ >= ids_.size()) {
0102     throw cms::Exception("TooManyEvents")
0103         << "Was passes " << ids_.size() << " EventIDs but have processed more events than that\n";
0104   }
0105   if (unorderedEvents_) {
0106     auto itFound = std::find(ids_.begin(), ids_.end(), iEventID);
0107     if (itFound == ids_.end()) {
0108       throw cms::Exception("UnexpecedEvent") << "The event " << iEventID << " was not expected.";
0109     }
0110   } else {
0111     if (iEventID != ids_[index_]) {
0112       throw cms::Exception("WrongEvent") << "Was expecting event " << ids_[index_] << " but was given " << iEventID
0113                                          << "\n";
0114     }
0115   }
0116   ++index_;
0117 }
0118 
0119 // ------------ method called to for each event  ------------
0120 void RunLumiEventChecker::analyze(edm::StreamID, edm::Event const& iEvent, edm::EventSetup const&) const {
0121   check(iEvent.id(), true);
0122 }
0123 
0124 std::shared_ptr<rlec::Cache> RunLumiEventChecker::globalBeginRun(edm::Run const& run, edm::EventSetup const&) const {
0125   check(edm::EventID(run.id().run(), 0, 0), false);
0126   return std::shared_ptr<rlec::Cache>{};
0127 }
0128 void RunLumiEventChecker::globalEndRun(edm::Run const& run, edm::EventSetup const&) const {
0129   check(edm::EventID(run.id().run(), 0, 0), false);
0130 }
0131 
0132 std::shared_ptr<rlec::Cache> RunLumiEventChecker::globalBeginLuminosityBlock(edm::LuminosityBlock const& lumi,
0133                                                                              edm::EventSetup const&) const {
0134   check(edm::EventID(lumi.id().run(), lumi.id().luminosityBlock(), 0), false);
0135   return std::shared_ptr<rlec::Cache>{};
0136 }
0137 
0138 void RunLumiEventChecker::globalEndLuminosityBlock(edm::LuminosityBlock const& lumi, edm::EventSetup const&) const {
0139   check(edm::EventID(lumi.id().run(), lumi.id().luminosityBlock(), 0), false);
0140 }
0141 
0142 // ------------ method called once each job just before starting event loop  ------------
0143 void RunLumiEventChecker::beginJob() {}
0144 
0145 // ------------ method called once each job just after ending the event loop  ------------
0146 void RunLumiEventChecker::endJob() {
0147   if (index_ != ids_.size()) {
0148     throw cms::Exception("WrongNumberOfEvents")
0149         << "Saw " << index_ << " events but was supposed to see " << ids_.size() << "\n";
0150   }
0151 }
0152 
0153 // ------------ method called once each job for validation
0154 void RunLumiEventChecker::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0155   edm::ParameterSetDescription desc;
0156   desc.addUntracked<std::vector<edm::EventID>>("eventSequence");
0157   desc.addUntracked<bool>("unorderedEvents", false);
0158   descriptions.add("runLumiEventIDChecker", desc);
0159 }
0160 
0161 //define this as a plug-in
0162 DEFINE_FWK_MODULE(RunLumiEventChecker);