Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 // -*- C++ -*-
0002 //
0003 // Package:    FWCore/Integration
0004 // Class:      RunLumiESAnalyzer
0005 //
0006 /**\class edmtest::RunLumiESAnalyzer
0007 
0008  Description: Used in tests of the EventSetup system,
0009  particularly testing its support of Run and Lumi
0010  transitions.
0011 */
0012 // Original Author:  W. David Dagenhart
0013 //         Created:  18 April 2019
0014 
0015 #include "FWCore/Framework/interface/global/EDAnalyzer.h"
0016 #include "FWCore/Framework/interface/ESHandle.h"
0017 #include "FWCore/Framework/interface/Event.h"
0018 #include "FWCore/Framework/interface/EventSetup.h"
0019 #include "FWCore/Framework/interface/MakerMacros.h"
0020 #include "FWCore/Framework/interface/Run.h"
0021 #include "FWCore/Integration/interface/ESTestRecords.h"
0022 #include "FWCore/Integration/test/IOVTestInfo.h"
0023 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0024 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0025 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0026 #include "FWCore/Utilities/interface/ESGetToken.h"
0027 #include "FWCore/Utilities/interface/ESInputTag.h"
0028 #include "FWCore/Utilities/interface/Exception.h"
0029 
0030 #include <memory>
0031 
0032 namespace {
0033   struct Cache {
0034     Cache() : value(0) {}
0035     //Using mutable since we want to update the value.
0036     mutable std::atomic<unsigned int> value;
0037   };
0038 
0039   struct UnsafeCache {
0040     UnsafeCache() : value(0) {}
0041     unsigned int value;
0042   };
0043 }  //end anonymous namespace
0044 
0045 namespace edmtest {
0046 
0047   class RunLumiESAnalyzer : public edm::global::EDAnalyzer<edm::StreamCache<UnsafeCache>,
0048                                                            edm::RunCache<Cache>,
0049                                                            edm::LuminosityBlockCache<Cache>> {
0050   public:
0051     explicit RunLumiESAnalyzer(edm::ParameterSet const&);
0052 
0053     std::unique_ptr<UnsafeCache> beginStream(edm::StreamID) const override;
0054     void streamBeginRun(edm::StreamID, edm::Run const&, edm::EventSetup const&) const override;
0055     void streamBeginLuminosityBlock(edm::StreamID, edm::LuminosityBlock const&, edm::EventSetup const&) const override;
0056     void streamEndLuminosityBlock(edm::StreamID, edm::LuminosityBlock const&, edm::EventSetup const&) const override;
0057     void streamEndRun(edm::StreamID, edm::Run const&, edm::EventSetup const&) const override;
0058 
0059     std::shared_ptr<Cache> globalBeginRun(edm::Run const&, edm::EventSetup const&) const override;
0060     void globalEndRun(edm::Run const& iRun, edm::EventSetup const&) const override;
0061     std::shared_ptr<Cache> globalBeginLuminosityBlock(edm::LuminosityBlock const&,
0062                                                       edm::EventSetup const&) const override;
0063     void globalEndLuminosityBlock(edm::LuminosityBlock const& iLB, edm::EventSetup const&) const override;
0064 
0065     void analyze(edm::StreamID, edm::Event const&, edm::EventSetup const&) const override;
0066 
0067     static void fillDescriptions(edm::ConfigurationDescriptions&);
0068 
0069   private:
0070     void checkIOVInfo(edm::EventSetup const& eventSetup,
0071                       unsigned int run,
0072                       unsigned int lumiNumber,
0073                       edm::ESHandle<IOVTestInfo> const& iovTestInfo,
0074                       const char* functionName) const;
0075 
0076     edm::ESGetToken<IOVTestInfo, ESTestRecordC> const esToken_;
0077     edm::ESGetToken<IOVTestInfo, ESTestRecordC> const tokenBeginRun_;
0078     edm::ESGetToken<IOVTestInfo, ESTestRecordC> const tokenBeginLumi_;
0079     edm::ESGetToken<IOVTestInfo, ESTestRecordC> const tokenEndLumi_;
0080     edm::ESGetToken<IOVTestInfo, ESTestRecordC> const tokenEndRun_;
0081   };
0082 
0083   RunLumiESAnalyzer::RunLumiESAnalyzer(edm::ParameterSet const&)
0084       : esToken_{esConsumes(edm::ESInputTag("", ""))},
0085         tokenBeginRun_{esConsumes<edm::Transition::BeginRun>(edm::ESInputTag("", ""))},
0086         tokenBeginLumi_{esConsumes<edm::Transition::BeginLuminosityBlock>(edm::ESInputTag("", ""))},
0087         tokenEndLumi_{esConsumes<edm::Transition::EndLuminosityBlock>(edm::ESInputTag("", ""))},
0088         tokenEndRun_{esConsumes<edm::Transition::EndRun>(edm::ESInputTag("", ""))} {}
0089 
0090   std::unique_ptr<UnsafeCache> RunLumiESAnalyzer::beginStream(edm::StreamID iID) const {
0091     return std::make_unique<UnsafeCache>();
0092   }
0093 
0094   void RunLumiESAnalyzer::checkIOVInfo(edm::EventSetup const& eventSetup,
0095                                        unsigned int run,
0096                                        unsigned int lumiNumber,
0097                                        edm::ESHandle<IOVTestInfo> const& iovTestInfo,
0098                                        const char* functionName) const {
0099     ESTestRecordC recordC = eventSetup.get<ESTestRecordC>();
0100     edm::ValidityInterval iov = recordC.validityInterval();
0101 
0102     if (iovTestInfo->iovStartRun_ != run || iovTestInfo->iovEndRun_ != run ||
0103         iovTestInfo->iovStartLumi_ != lumiNumber || iovTestInfo->iovEndLumi_ != lumiNumber) {
0104       throw cms::Exception("TestFailure")
0105           << functionName << ": values read from EventSetup do not agree with auxiliary";
0106     }
0107 
0108     if (iov.first().eventID().run() != run || iov.last().eventID().run() != run ||
0109         iov.first().luminosityBlockNumber() != lumiNumber || iov.last().luminosityBlockNumber() != lumiNumber) {
0110       throw cms::Exception("TestFailure") << functionName << ": values from EventSetup IOV do not agree with auxiliary";
0111     }
0112   }
0113 
0114   void RunLumiESAnalyzer::streamBeginRun(edm::StreamID, edm::Run const& iRun, edm::EventSetup const& eventSetup) const {
0115     auto run = iRun.runAuxiliary().run();
0116     unsigned int lumiNumber = 0;
0117     edm::ESHandle<IOVTestInfo> iovTestInfo = eventSetup.getHandle(tokenBeginRun_);
0118     checkIOVInfo(eventSetup, run, lumiNumber, iovTestInfo, "RunLumiESAnalyzer::streamBeginRun");
0119   }
0120 
0121   void RunLumiESAnalyzer::streamBeginLuminosityBlock(edm::StreamID,
0122                                                      edm::LuminosityBlock const& iLumi,
0123                                                      edm::EventSetup const& eventSetup) const {
0124     auto run = iLumi.luminosityBlockAuxiliary().run();
0125     unsigned int lumiNumber = iLumi.luminosityBlockAuxiliary().luminosityBlock();
0126     edm::ESHandle<IOVTestInfo> iovTestInfo = eventSetup.getHandle(tokenBeginLumi_);
0127     checkIOVInfo(eventSetup, run, lumiNumber, iovTestInfo, "RunLumiESAnalyzer::streamBeginLuminosityBlock");
0128   }
0129 
0130   void RunLumiESAnalyzer::streamEndLuminosityBlock(edm::StreamID,
0131                                                    edm::LuminosityBlock const& iLumi,
0132                                                    edm::EventSetup const& eventSetup) const {
0133     auto run = iLumi.luminosityBlockAuxiliary().run();
0134     unsigned int lumiNumber = iLumi.luminosityBlockAuxiliary().luminosityBlock();
0135     edm::ESHandle<IOVTestInfo> iovTestInfo = eventSetup.getHandle(tokenEndLumi_);
0136     checkIOVInfo(eventSetup, run, lumiNumber, iovTestInfo, "RunLumiESAnalyzer::streamEndLuminosityBlock");
0137   }
0138 
0139   void RunLumiESAnalyzer::streamEndRun(edm::StreamID, edm::Run const& iRun, edm::EventSetup const& eventSetup) const {
0140     auto run = iRun.runAuxiliary().run();
0141     unsigned int lumiNumber = 4294967295;
0142     edm::ESHandle<IOVTestInfo> iovTestInfo = eventSetup.getHandle(tokenEndRun_);
0143     checkIOVInfo(eventSetup, run, lumiNumber, iovTestInfo, "RunLumiESAnalyzer::streamEndRun");
0144   }
0145 
0146   std::shared_ptr<Cache> RunLumiESAnalyzer::globalBeginRun(edm::Run const& iRun,
0147                                                            edm::EventSetup const& eventSetup) const {
0148     auto run = iRun.runAuxiliary().run();
0149     unsigned int lumiNumber = 0;
0150     edm::ESHandle<IOVTestInfo> iovTestInfo = eventSetup.getHandle(tokenBeginRun_);
0151     checkIOVInfo(eventSetup, run, lumiNumber, iovTestInfo, "RunLumiESAnalyzer::globalBeginRun");
0152     return std::make_shared<Cache>();
0153   }
0154 
0155   void RunLumiESAnalyzer::globalEndRun(edm::Run const& iRun, edm::EventSetup const& eventSetup) const {
0156     auto run = iRun.runAuxiliary().run();
0157     unsigned int lumiNumber = 4294967295;
0158     edm::ESHandle<IOVTestInfo> iovTestInfo = eventSetup.getHandle(tokenEndRun_);
0159     checkIOVInfo(eventSetup, run, lumiNumber, iovTestInfo, "RunLumiESAnalyzer::globalEndRun");
0160   }
0161 
0162   std::shared_ptr<Cache> RunLumiESAnalyzer::globalBeginLuminosityBlock(edm::LuminosityBlock const& iLumi,
0163                                                                        edm::EventSetup const& eventSetup) const {
0164     auto run = iLumi.luminosityBlockAuxiliary().run();
0165     unsigned int lumiNumber = iLumi.luminosityBlockAuxiliary().luminosityBlock();
0166     edm::ESHandle<IOVTestInfo> iovTestInfo = eventSetup.getHandle(tokenBeginLumi_);
0167     checkIOVInfo(eventSetup, run, lumiNumber, iovTestInfo, "RunLumiESAnalyzer::globalBeginLuminosityBlock");
0168     return std::make_shared<Cache>();
0169   }
0170 
0171   void RunLumiESAnalyzer::globalEndLuminosityBlock(edm::LuminosityBlock const& iLumi,
0172                                                    edm::EventSetup const& eventSetup) const {
0173     auto run = iLumi.luminosityBlockAuxiliary().run();
0174     unsigned int lumiNumber = iLumi.luminosityBlockAuxiliary().luminosityBlock();
0175     edm::ESHandle<IOVTestInfo> iovTestInfo = eventSetup.getHandle(tokenEndLumi_);
0176     checkIOVInfo(eventSetup, run, lumiNumber, iovTestInfo, "RunLumiESAnalyzer::globalEndLuminosityBlock");
0177   }
0178 
0179   void RunLumiESAnalyzer::analyze(edm::StreamID, edm::Event const& event, edm::EventSetup const& eventSetup) const {
0180     auto run = event.eventAuxiliary().run();
0181     auto lumiNumber = event.eventAuxiliary().luminosityBlock();
0182     edm::ESHandle<IOVTestInfo> iovTestInfo = eventSetup.getHandle(esToken_);
0183     checkIOVInfo(eventSetup, run, lumiNumber, iovTestInfo, "RunLumiESAnalyzer::analyzer");
0184   }
0185 
0186   void RunLumiESAnalyzer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0187     edm::ParameterSetDescription desc;
0188     descriptions.addDefault(desc);
0189   }
0190 }  // namespace edmtest
0191 using namespace edmtest;
0192 DEFINE_FWK_MODULE(RunLumiESAnalyzer);