Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 // -*- C++ -*-
0002 //
0003 // Package:     FWCore/Integration
0004 // Class  :     ThingAnalyzer
0005 //
0006 // Implementation:
0007 //     [Notes on implementation]
0008 //
0009 // Original Author:  root
0010 //         Created:  Fri, 21 Apr 2017 13:34:58 GMT
0011 //
0012 
0013 // system include files
0014 #include "FWCore/Framework/interface/global/EDAnalyzer.h"
0015 #include "DataFormats/TestObjects/interface/ThingCollection.h"
0016 #include "FWCore/Framework/interface/Event.h"
0017 #include "FWCore/Framework/interface/LuminosityBlock.h"
0018 #include "FWCore/Framework/interface/Run.h"
0019 #include "FWCore/Framework/interface/MakerMacros.h"
0020 #include "FWCore/Utilities/interface/Exception.h"
0021 #include "FWCore/Utilities/interface/EDGetToken.h"
0022 
0023 // user include files
0024 
0025 namespace edmtest {
0026   struct Empty {};
0027   class ThingAnalyzer : public edm::global::EDAnalyzer<edm::RunCache<Empty>, edm::LuminosityBlockCache<Empty>> {
0028   public:
0029     ThingAnalyzer(edm::ParameterSet const&);
0030 
0031     void analyze(edm::StreamID, edm::Event const&, edm::EventSetup const&) const final;
0032     std::shared_ptr<Empty> globalBeginRun(edm::Run const&, edm::EventSetup const&) const final;
0033     void globalEndRun(edm::Run const&, edm::EventSetup const&) const final;
0034     std::shared_ptr<Empty> globalBeginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) const final;
0035     void globalEndLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) const final;
0036 
0037     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0038 
0039   private:
0040     void shouldBeInvalid(edm::Handle<ThingCollection> const&) const;
0041 
0042     edm::EDGetTokenT<ThingCollection> beginRun_;
0043     edm::EDGetTokenT<ThingCollection> beginLumi_;
0044     edm::EDGetTokenT<ThingCollection> event_;
0045     edm::EDGetTokenT<ThingCollection> endLumi_;
0046     edm::EDGetTokenT<ThingCollection> endRun_;
0047   };
0048 
0049   ThingAnalyzer::ThingAnalyzer(edm::ParameterSet const& iPSet)
0050       : beginRun_(consumes<edm::InRun>(iPSet.getUntrackedParameter<edm::InputTag>("beginRun"))),
0051         beginLumi_(consumes<edm::InLumi>(iPSet.getUntrackedParameter<edm::InputTag>("beginLumi"))),
0052         event_(consumes(iPSet.getUntrackedParameter<edm::InputTag>("event"))),
0053         endLumi_(consumes<edm::InLumi>(iPSet.getUntrackedParameter<edm::InputTag>("endLumi"))),
0054         endRun_(consumes<edm::InRun>(iPSet.getUntrackedParameter<edm::InputTag>("endRun"))) {}
0055 
0056   void ThingAnalyzer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0057     edm::ParameterSetDescription desc;
0058     desc.addUntracked("beginRun", edm::InputTag{"thing", "beginRun"})->setComment("Collection to get from Run");
0059     desc.addUntracked("beginLumi", edm::InputTag{"thing", "beginLumi"})->setComment("Collection to get from Lumi");
0060     desc.addUntracked("event", edm::InputTag{"thing", ""})->setComment("Collection to get from event");
0061     desc.addUntracked("endLumi", edm::InputTag{"thing", "endLumi"})
0062         ->setComment("Collection to get from Lumi but only available at end");
0063     desc.addUntracked("endRun", edm::InputTag{"thing", "endRun"})
0064         ->setComment("Collection to get from Run but only available at end");
0065     descriptions.add("thingAnalyzer", desc);
0066   }
0067 
0068   void ThingAnalyzer::analyze(edm::StreamID, edm::Event const& iEvent, edm::EventSetup const&) const {
0069     auto const& lumi = iEvent.getLuminosityBlock();
0070 
0071     auto const& run = lumi.getRun();
0072 
0073     (void)run.get(beginRun_);
0074 
0075     shouldBeInvalid(run.getHandle(endRun_));
0076 
0077     (void)lumi.get(beginLumi_);
0078 
0079     shouldBeInvalid(lumi.getHandle(endLumi_));
0080 
0081     (void)iEvent.get(event_);
0082   }
0083 
0084   std::shared_ptr<Empty> ThingAnalyzer::globalBeginRun(edm::Run const& iRun, edm::EventSetup const&) const {
0085     (void)iRun.get(beginRun_);
0086 
0087     shouldBeInvalid(iRun.getHandle(endRun_));
0088 
0089     return std::shared_ptr<Empty>();
0090   }
0091 
0092   void ThingAnalyzer::globalEndRun(edm::Run const& iRun, edm::EventSetup const&) const {
0093     (void)iRun.get(beginRun_);
0094 
0095     (void)iRun.get(endRun_);
0096   }
0097 
0098   std::shared_ptr<Empty> ThingAnalyzer::globalBeginLuminosityBlock(edm::LuminosityBlock const& iLumi,
0099                                                                    edm::EventSetup const&) const {
0100     auto const& run = iLumi.getRun();
0101 
0102     (void)run.get(beginRun_);
0103 
0104     shouldBeInvalid(run.getHandle(endRun_));
0105 
0106     (void)iLumi.get(beginLumi_);
0107 
0108     shouldBeInvalid(iLumi.getHandle(endLumi_));
0109 
0110     return std::shared_ptr<Empty>();
0111   }
0112 
0113   void ThingAnalyzer::globalEndLuminosityBlock(edm::LuminosityBlock const& iLumi, edm::EventSetup const&) const {
0114     auto const& run = iLumi.getRun();
0115 
0116     (void)run.get(beginRun_);
0117 
0118     shouldBeInvalid(run.getHandle(endRun_));
0119 
0120     (void)iLumi.get(beginLumi_);
0121 
0122     (void)iLumi.get(endLumi_);
0123   }
0124 
0125   void ThingAnalyzer::shouldBeInvalid(edm::Handle<ThingCollection> const& iHandle) const {
0126     if (iHandle.isValid()) {
0127       throw cms::Exception("ShouldNotBeValid") << "handle was valid when it should not have been";
0128     }
0129   }
0130 
0131 }  // namespace edmtest
0132 
0133 DEFINE_FWK_MODULE(edmtest::ThingAnalyzer);