Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-05-31 04:19:34

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   unsigned int minNEvents_ = 0;
0071   unsigned int maxNEvents_ = 0;
0072   bool unorderedEvents_;
0073 };
0074 
0075 //
0076 // constants, enums and typedefs
0077 //
0078 
0079 //
0080 // static data member definitions
0081 //
0082 
0083 //
0084 // constructors and destructor
0085 //
0086 RunLumiEventChecker::RunLumiEventChecker(edm::ParameterSet const& iConfig)
0087     : ids_(iConfig.getUntrackedParameter<std::vector<edm::EventID>>("eventSequence")),
0088       index_(0),
0089       minNEvents_(iConfig.getUntrackedParameter<unsigned int>("minNumberOfEvents")),
0090       maxNEvents_(iConfig.getUntrackedParameter<unsigned int>("maxNumberOfEvents")),
0091       unorderedEvents_(iConfig.getUntrackedParameter<bool>("unorderedEvents")) {
0092   //now do what ever initialization is needed
0093 }
0094 
0095 RunLumiEventChecker::~RunLumiEventChecker() {
0096   // do anything here that needs to be done at desctruction time
0097   // (e.g. close files, deallocate resources etc.)
0098 }
0099 
0100 //
0101 // member functions
0102 //
0103 
0104 void RunLumiEventChecker::check(edm::EventID const& iEventID, bool iIsEvent) const {
0105   if (index_ >= ids_.size()) {
0106     throw cms::Exception("TooManyEvents")
0107         << "Was passes " << ids_.size() << " EventIDs but have processed more events than that\n";
0108   }
0109   if (unorderedEvents_) {
0110     auto itFound = std::find(ids_.begin(), ids_.end(), iEventID);
0111     if (itFound == ids_.end()) {
0112       throw cms::Exception("UnexpecedEvent") << "The event " << iEventID << " was not expected.";
0113     }
0114   } else {
0115     if (iEventID != ids_[index_]) {
0116       throw cms::Exception("WrongEvent") << "Was expecting event " << ids_[index_] << " but was given " << iEventID
0117                                          << "\n";
0118     }
0119   }
0120   ++index_;
0121 }
0122 
0123 // ------------ method called to for each event  ------------
0124 void RunLumiEventChecker::analyze(edm::StreamID, edm::Event const& iEvent, edm::EventSetup const&) const {
0125   check(iEvent.id(), true);
0126 }
0127 
0128 std::shared_ptr<rlec::Cache> RunLumiEventChecker::globalBeginRun(edm::Run const& run, edm::EventSetup const&) const {
0129   check(edm::EventID(run.id().run(), 0, 0), false);
0130   return std::shared_ptr<rlec::Cache>{};
0131 }
0132 void RunLumiEventChecker::globalEndRun(edm::Run const& run, edm::EventSetup const&) const {
0133   check(edm::EventID(run.id().run(), 0, 0), false);
0134 }
0135 
0136 std::shared_ptr<rlec::Cache> RunLumiEventChecker::globalBeginLuminosityBlock(edm::LuminosityBlock const& lumi,
0137                                                                              edm::EventSetup const&) const {
0138   check(edm::EventID(lumi.id().run(), lumi.id().luminosityBlock(), 0), false);
0139   return std::shared_ptr<rlec::Cache>{};
0140 }
0141 
0142 void RunLumiEventChecker::globalEndLuminosityBlock(edm::LuminosityBlock const& lumi, edm::EventSetup const&) const {
0143   check(edm::EventID(lumi.id().run(), lumi.id().luminosityBlock(), 0), false);
0144 }
0145 
0146 // ------------ method called once each job just before starting event loop  ------------
0147 void RunLumiEventChecker::beginJob() {}
0148 
0149 // ------------ method called once each job just after ending the event loop  ------------
0150 void RunLumiEventChecker::endJob() {
0151   if (maxNEvents_ == 0 and index_ != ids_.size()) {
0152     throw cms::Exception("WrongNumberOfEvents")
0153         << "Saw " << index_ << " (begin runs)+(begin lumis)+events+(end lumis)+(end runs) but was supposed to see "
0154         << ids_.size() << "\n";
0155   }
0156   if (maxNEvents_ != 0 and (index_ < minNEvents_ or index_ > maxNEvents_)) {
0157     throw cms::Exception("WrongNumberOfEvents")
0158         << "Saw " << index_
0159         << " (begin runs)+(begin lumis)+events+(end lumis)+(end runs) but was supposed to see between " << minNEvents_
0160         << " and " << maxNEvents_;
0161   }
0162 }
0163 
0164 // ------------ method called once each job for validation
0165 void RunLumiEventChecker::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0166   edm::ParameterSetDescription desc;
0167   desc.addUntracked<std::vector<edm::EventID>>("eventSequence");
0168   desc.addUntracked<unsigned int>("minNumberOfEvents", 0)
0169       ->setComment(
0170           "minimum number of Events that must be seen. If max is 0 then this will be ignored and all Events must be "
0171           "present");
0172   desc.addUntracked<unsigned int>("maxNumberOfEvents", 0)
0173       ->setComment("maximum number of Events that must be seen. If set to 0, min and max are ignored");
0174   desc.addUntracked<bool>("unorderedEvents", false)
0175       ->setComment("set to true if events are not guaranteed to be in same order as 'eventSequence' specifies.");
0176   descriptions.add("runLumiEventIDChecker", desc);
0177 }
0178 
0179 //define this as a plug-in
0180 DEFINE_FWK_MODULE(RunLumiEventChecker);