Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:02:27

0001 
0002 /*----------------------------------------------------------------------
0003 
0004 Toy edm::global::EDFilter modules of
0005 edm::*Cache templates and edm::*Producer classes
0006 for testing purposes only.
0007 
0008 ----------------------------------------------------------------------*/
0009 #include <atomic>
0010 #include <memory>
0011 #include <tuple>
0012 #include <vector>
0013 
0014 #include "DataFormats/TestObjects/interface/ToyProducts.h"
0015 #include "FWCore/Framework/interface/CacheHandle.h"
0016 #include "FWCore/Framework/interface/Event.h"
0017 #include "FWCore/Framework/interface/LuminosityBlock.h"
0018 #include "FWCore/Framework/interface/MakerMacros.h"
0019 #include "FWCore/Framework/interface/moduleAbilities.h"
0020 #include "FWCore/Framework/interface/global/EDFilter.h"
0021 #include "FWCore/Framework/interface/ProcessBlock.h"
0022 #include "FWCore/Framework/interface/Run.h"
0023 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0024 #include "FWCore/Utilities/interface/BranchType.h"
0025 #include "FWCore/Utilities/interface/EDMException.h"
0026 #include "FWCore/Utilities/interface/EDGetToken.h"
0027 #include "FWCore/Utilities/interface/InputTag.h"
0028 
0029 namespace edmtest {
0030   namespace global {
0031 
0032     namespace {
0033       struct Cache {
0034         Cache() : value(0), run(0), lumi(0), strm(0), work(0) {}
0035         //Using mutable since we want to update the value.
0036         mutable std::atomic<unsigned int> value;
0037         mutable std::atomic<unsigned int> run;
0038         mutable std::atomic<unsigned int> lumi;
0039         mutable std::atomic<unsigned int> strm;
0040         mutable std::atomic<unsigned int> work;
0041       };
0042 
0043       struct UnsafeCache {
0044         UnsafeCache() : value(0), run(0), lumi(0), strm(0), work(0) {}
0045         unsigned int value;
0046         unsigned int run;
0047         unsigned int lumi;
0048         unsigned int strm;
0049         unsigned int work;
0050       };
0051 
0052       struct Dummy {};
0053 
0054     }  //end anonymous namespace
0055 
0056     class StreamIntFilter : public edm::global::EDFilter<edm::StreamCache<UnsafeCache>> {
0057     public:
0058       explicit StreamIntFilter(edm::ParameterSet const& p) : trans_(p.getParameter<int>("transitions")) {
0059         produces<unsigned int>();
0060       }
0061 
0062       const unsigned int trans_;
0063       mutable std::atomic<unsigned int> m_count{0};
0064 
0065       std::unique_ptr<UnsafeCache> beginStream(edm::StreamID iID) const override {
0066         ++m_count;
0067         auto sCache = std::make_unique<UnsafeCache>();
0068         ++(sCache->strm);
0069         sCache->value = iID.value();
0070         return sCache;
0071       }
0072 
0073       void streamBeginRun(edm::StreamID iID, edm::Run const&, edm::EventSetup const&) const override {
0074         ++m_count;
0075         auto sCache = streamCache(iID);
0076         if (sCache->value != iID.value()) {
0077           throw cms::Exception("cache value") << (streamCache(iID))->value << " but it was supposed to be " << iID;
0078         }
0079         if (sCache->run != 0 || sCache->lumi != 0 || sCache->work != 0 || sCache->strm != 1) {
0080           throw cms::Exception("out of sequence") << "streamBeginRun out of sequence in Stream " << iID.value();
0081         }
0082         ++(sCache->run);
0083       }
0084 
0085       void streamBeginLuminosityBlock(edm::StreamID iID,
0086                                       edm::LuminosityBlock const&,
0087                                       edm::EventSetup const&) const override {
0088         ++m_count;
0089         auto sCache = streamCache(iID);
0090         if (sCache->value != iID.value()) {
0091           throw cms::Exception("cache value") << (streamCache(iID))->value << " but it was supposed to be " << iID;
0092         }
0093         if (sCache->lumi != 0 || sCache->work != 0) {
0094           throw cms::Exception("out of sequence")
0095               << "streamBeginLuminosityBlock out of sequence in Stream " << iID.value();
0096         }
0097         ++(sCache->lumi);
0098       }
0099 
0100       bool filter(edm::StreamID iID, edm::Event&, edm::EventSetup const&) const override {
0101         ++m_count;
0102         auto sCache = streamCache(iID);
0103         if (sCache->value != iID.value()) {
0104           throw cms::Exception("cache value") << (streamCache(iID))->value << " but it was supposed to be " << iID;
0105         }
0106         ++(sCache->work);
0107         if (sCache->lumi == 0 && sCache->run == 0) {
0108           throw cms::Exception("out of sequence") << "produce out of sequence in Stream " << iID.value();
0109         }
0110 
0111         return true;
0112       }
0113 
0114       void streamEndLuminosityBlock(edm::StreamID iID,
0115                                     edm::LuminosityBlock const&,
0116                                     edm::EventSetup const&) const override {
0117         ++m_count;
0118         auto sCache = streamCache(iID);
0119         if (sCache->value != iID.value()) {
0120           throw cms::Exception("cache value") << (streamCache(iID))->value << " but it was supposed to be " << iID;
0121         }
0122         --(sCache->lumi);
0123         sCache->work = 0;
0124         if (sCache->lumi != 0 || sCache->run == 0) {
0125           throw cms::Exception("out of sequence")
0126               << "streamEndLuminosityBlock out of sequence in Stream " << iID.value();
0127         }
0128       }
0129 
0130       void streamEndRun(edm::StreamID iID, edm::Run const&, edm::EventSetup const&) const override {
0131         ++m_count;
0132         auto sCache = streamCache(iID);
0133         if (sCache->value != iID.value()) {
0134           throw cms::Exception("cache value") << (streamCache(iID))->value << " but it was supposed to be " << iID;
0135         }
0136         --(sCache->run);
0137         sCache->work = 0;
0138         if (sCache->run != 0 || sCache->lumi != 0) {
0139           throw cms::Exception("out of sequence") << "streamEndRun out of sequence in Stream " << iID.value();
0140         }
0141       }
0142 
0143       void endStream(edm::StreamID iID) const override {
0144         ++m_count;
0145         auto sCache = streamCache(iID);
0146         --(sCache->strm);
0147         if (sCache->value != iID.value()) {
0148           throw cms::Exception("cache value") << (streamCache(iID))->value << " but it was supposed to be " << iID;
0149         }
0150         if (sCache->strm != 0 || sCache->run != 0 || sCache->lumi != 0) {
0151           throw cms::Exception("out of sequence") << "endStream out of sequence in Stream " << iID.value();
0152         }
0153       }
0154 
0155       ~StreamIntFilter() {
0156         if (m_count != trans_) {
0157           throw cms::Exception("transitions")
0158               << "StreamIntFilter transitions " << m_count << " but it was supposed to be " << trans_;
0159         }
0160       }
0161     };
0162 
0163     class RunIntFilter : public edm::global::EDFilter<edm::StreamCache<UnsafeCache>, edm::RunCache<Cache>> {
0164     public:
0165       explicit RunIntFilter(edm::ParameterSet const& p)
0166           : trans_(p.getParameter<int>("transitions")), cvalue_(p.getParameter<int>("cachevalue")) {
0167         produces<unsigned int>();
0168       }
0169       const unsigned int trans_;
0170       const unsigned int cvalue_;
0171       mutable std::atomic<unsigned int> m_count{0};
0172 
0173       std::shared_ptr<Cache> globalBeginRun(edm::Run const&, edm::EventSetup const&) const override {
0174         ++m_count;
0175         auto rCache = std::make_shared<Cache>();
0176         ++(rCache->run);
0177         return rCache;
0178       }
0179 
0180       std::unique_ptr<UnsafeCache> beginStream(edm::StreamID) const override { return std::make_unique<UnsafeCache>(); }
0181 
0182       void streamBeginRun(edm::StreamID iID, edm::Run const& iRun, edm::EventSetup const&) const override {
0183         auto rCache = runCache(iRun.index());
0184         if (rCache->run == 0) {
0185           throw cms::Exception("out of sequence") << "streamBeginRun before globalBeginRun in Stream " << iID.value();
0186         }
0187       }
0188 
0189       bool filter(edm::StreamID, edm::Event& iEvent, edm::EventSetup const&) const override {
0190         ++m_count;
0191         auto rCache = runCache(iEvent.getRun().index());
0192         ++(rCache->value);
0193 
0194         return true;
0195       }
0196 
0197       void streamEndRun(edm::StreamID iID, edm::Run const& iRun, edm::EventSetup const&) const override {
0198         auto rCache = runCache(iRun.index());
0199         if (rCache->run == 0) {
0200           throw cms::Exception("out of sequence") << "streamEndRun after globalEndRun in Stream " << iID.value();
0201         }
0202       }
0203 
0204       void globalEndRun(edm::Run const& iRun, edm::EventSetup const&) const override {
0205         ++m_count;
0206         auto rCache = runCache(iRun.index());
0207         if (rCache->value != cvalue_) {
0208           throw cms::Exception("cache value")
0209               << "RunIntFilter cache value " << rCache->value << " but it was supposed to be " << cvalue_;
0210         }
0211         --(rCache->run);
0212       }
0213 
0214       ~RunIntFilter() {
0215         if (m_count != trans_) {
0216           throw cms::Exception("transitions")
0217               << "RunIntFilter transitions " << m_count << " but it was supposed to be " << trans_;
0218         }
0219       }
0220     };
0221 
0222     class LumiIntFilter
0223         : public edm::global::EDFilter<edm::StreamCache<UnsafeCache>, edm::LuminosityBlockCache<Cache>> {
0224     public:
0225       explicit LumiIntFilter(edm::ParameterSet const& p)
0226           : trans_(p.getParameter<int>("transitions")), cvalue_(p.getParameter<int>("cachevalue")) {
0227         produces<unsigned int>();
0228       }
0229       const unsigned int trans_;
0230       const unsigned int cvalue_;
0231       mutable std::atomic<unsigned int> m_count{0};
0232 
0233       std::shared_ptr<Cache> globalBeginLuminosityBlock(edm::LuminosityBlock const&,
0234                                                         edm::EventSetup const&) const override {
0235         ++m_count;
0236         auto lCache = std::make_shared<Cache>();
0237         ++(lCache->lumi);
0238         return lCache;
0239       }
0240 
0241       std::unique_ptr<UnsafeCache> beginStream(edm::StreamID iID) const override {
0242         return std::make_unique<UnsafeCache>();
0243       }
0244 
0245       void streamBeginLuminosityBlock(edm::StreamID iID,
0246                                       edm::LuminosityBlock const& iLB,
0247                                       edm::EventSetup const&) const override {
0248         auto lCache = luminosityBlockCache(iLB.index());
0249         if (lCache->lumi == 0) {
0250           throw cms::Exception("out of sequence")
0251               << "streamBeginLuminosityBlock seen before globalBeginLuminosityBlock in LuminosityBlock"
0252               << iLB.luminosityBlock();
0253         }
0254       }
0255 
0256       bool filter(edm::StreamID, edm::Event& iEvent, edm::EventSetup const&) const override {
0257         ++m_count;
0258         ++(luminosityBlockCache(iEvent.getLuminosityBlock().index())->value);
0259         return true;
0260       }
0261 
0262       void streamEndLuminosityBlock(edm::StreamID iID,
0263                                     edm::LuminosityBlock const& iLB,
0264                                     edm::EventSetup const&) const override {
0265         auto lCache = luminosityBlockCache(iLB.index());
0266         if (lCache->lumi == 0) {
0267           throw cms::Exception("out of sequence")
0268               << "streamEndLuminosityBlock seen before globalEndLuminosityBlock in LuminosityBlock"
0269               << iLB.luminosityBlock();
0270         }
0271       }
0272 
0273       void globalEndLuminosityBlock(edm::LuminosityBlock const& iLB, edm::EventSetup const&) const override {
0274         ++m_count;
0275         auto lCache = luminosityBlockCache(iLB.index());
0276         --(lCache->lumi);
0277         if (lCache->lumi != 0) {
0278           throw cms::Exception("end out of sequence")
0279               << "globalEndLuminosityBlock seen before globalBeginLuminosityBlock in LuminosityBlock"
0280               << iLB.luminosityBlock();
0281         }
0282         if (lCache->value != cvalue_) {
0283           throw cms::Exception("cache value")
0284               << "LumiIntFilter cache value " << lCache->value << " but it was supposed to be " << cvalue_;
0285         }
0286       }
0287 
0288       ~LumiIntFilter() {
0289         if (m_count != trans_) {
0290           throw cms::Exception("transitions")
0291               << "LumiIntFilter transitions " << m_count << " but it was supposed to be " << trans_;
0292         }
0293       }
0294     };
0295 
0296     class RunSummaryIntFilter
0297         : public edm::global::EDFilter<edm::StreamCache<Cache>, edm::RunSummaryCache<UnsafeCache>> {
0298     public:
0299       explicit RunSummaryIntFilter(edm::ParameterSet const& p)
0300           : trans_(p.getParameter<int>("transitions")), cvalue_(p.getParameter<int>("cachevalue")) {
0301         produces<unsigned int>();
0302       }
0303       const unsigned int trans_;
0304       const unsigned int cvalue_;
0305       mutable std::atomic<unsigned int> m_count{0};
0306 
0307       std::unique_ptr<Cache> beginStream(edm::StreamID) const override {
0308         ++m_count;
0309         return std::make_unique<Cache>();
0310       }
0311 
0312       std::shared_ptr<UnsafeCache> globalBeginRunSummary(edm::Run const&, edm::EventSetup const&) const override {
0313         ++m_count;
0314         auto gCache = std::make_shared<UnsafeCache>();
0315         ++(gCache->run);
0316         return gCache;
0317       }
0318 
0319       bool filter(edm::StreamID iID, edm::Event&, edm::EventSetup const&) const override {
0320         ++m_count;
0321         ++((streamCache(iID))->value);
0322 
0323         return true;
0324       }
0325 
0326       void streamEndRunSummary(edm::StreamID iID,
0327                                edm::Run const&,
0328                                edm::EventSetup const&,
0329                                UnsafeCache* gCache) const override {
0330         ++m_count;
0331         if (gCache->run == 0) {
0332           throw cms::Exception("out of sequence")
0333               << "streamEndRunSummary after globalEndRunSummary in Stream " << iID.value();
0334         }
0335         auto sCache = streamCache(iID);
0336         gCache->value += sCache->value;
0337         sCache->value = 0;
0338       }
0339 
0340       void globalEndRunSummary(edm::Run const&, edm::EventSetup const&, UnsafeCache* gCache) const override {
0341         ++m_count;
0342         if (gCache->value != cvalue_) {
0343           throw cms::Exception("cache value")
0344               << "RunSummaryIntFilter cache value " << gCache->value << " but it was supposed to be " << cvalue_;
0345         }
0346         --(gCache->run);
0347       }
0348 
0349       ~RunSummaryIntFilter() {
0350         if (m_count != trans_) {
0351           throw cms::Exception("transitions")
0352               << "RunSummaryIntFilter transitions " << m_count << " but it was supposed to be " << trans_;
0353         }
0354       }
0355     };
0356 
0357     class LumiSummaryIntFilter
0358         : public edm::global::EDFilter<edm::StreamCache<Cache>, edm::LuminosityBlockSummaryCache<UnsafeCache>> {
0359     public:
0360       explicit LumiSummaryIntFilter(edm::ParameterSet const& p)
0361           : trans_(p.getParameter<int>("transitions")), cvalue_(p.getParameter<int>("cachevalue")) {
0362         produces<unsigned int>();
0363       }
0364       const unsigned int trans_;
0365       const unsigned int cvalue_;
0366       mutable std::atomic<unsigned int> m_count{0};
0367 
0368       std::unique_ptr<Cache> beginStream(edm::StreamID) const override {
0369         ++m_count;
0370         return std::make_unique<Cache>();
0371       }
0372 
0373       std::shared_ptr<UnsafeCache> globalBeginLuminosityBlockSummary(edm::LuminosityBlock const& iLB,
0374                                                                      edm::EventSetup const&) const override {
0375         ++m_count;
0376         auto gCache = std::make_shared<UnsafeCache>();
0377         gCache->lumi = iLB.luminosityBlockAuxiliary().luminosityBlock();
0378         return gCache;
0379       }
0380 
0381       bool filter(edm::StreamID iID, edm::Event&, edm::EventSetup const&) const override {
0382         ++m_count;
0383         ++((streamCache(iID))->value);
0384         return true;
0385       }
0386 
0387       void streamEndLuminosityBlockSummary(edm::StreamID iID,
0388                                            edm::LuminosityBlock const& iLB,
0389                                            edm::EventSetup const&,
0390                                            UnsafeCache* gCache) const override {
0391         ++m_count;
0392         if (gCache->lumi != iLB.luminosityBlockAuxiliary().luminosityBlock()) {
0393           throw cms::Exception("out of sequence")
0394               << "streamEndLuminosityBlockSummary unexpected lumi number in Stream " << iID.value();
0395         }
0396         auto sCache = streamCache(iID);
0397         gCache->value += sCache->value;
0398         sCache->value = 0;
0399       }
0400 
0401       void globalEndLuminosityBlockSummary(edm::LuminosityBlock const& iLB,
0402                                            edm::EventSetup const&,
0403                                            UnsafeCache* gCache) const override {
0404         ++m_count;
0405         if (gCache->lumi != iLB.luminosityBlockAuxiliary().luminosityBlock()) {
0406           throw cms::Exception("out of sequence") << "globalEndLuminosityBlockSummary unexpected lumi number";
0407         }
0408         if (gCache->value != cvalue_) {
0409           throw cms::Exception("cache value")
0410               << "LumiSummaryIntFilter cache value " << gCache->value << " but it was supposed to be " << cvalue_;
0411         }
0412       }
0413 
0414       ~LumiSummaryIntFilter() {
0415         if (m_count != trans_) {
0416           throw cms::Exception("transitions")
0417               << "LumiSummaryIntFilter transitions " << m_count << " but it was supposed to be " << trans_;
0418         }
0419       }
0420     };
0421 
0422     class ProcessBlockIntFilter : public edm::global::EDFilter<edm::WatchProcessBlock> {
0423     public:
0424       explicit ProcessBlockIntFilter(edm::ParameterSet const& p) : trans_(p.getParameter<int>("transitions")) {
0425         produces<unsigned int>();
0426         {
0427           auto tag = p.getParameter<edm::InputTag>("consumesBeginProcessBlock");
0428           if (not tag.label().empty()) {
0429             getTokenBegin_ = consumes<unsigned int, edm::InProcess>(tag);
0430           }
0431         }
0432         {
0433           auto tag = p.getParameter<edm::InputTag>("consumesEndProcessBlock");
0434           if (not tag.label().empty()) {
0435             getTokenEnd_ = consumes<unsigned int, edm::InProcess>(tag);
0436           }
0437         }
0438       }
0439 
0440       void beginProcessBlock(edm::ProcessBlock const& processBlock) override {
0441         if (m_count != 0) {
0442           throw cms::Exception("transitions")
0443               << "ProcessBlockIntFilter::begin transitions " << m_count << " but it was supposed to be " << 0;
0444         }
0445         ++m_count;
0446         const unsigned int valueToGet = 31;
0447         if (not getTokenBegin_.isUninitialized()) {
0448           if (processBlock.get(getTokenBegin_) != valueToGet) {
0449             throw cms::Exception("BadValue")
0450                 << "expected " << valueToGet << " but got " << processBlock.get(getTokenBegin_);
0451           }
0452         }
0453       }
0454 
0455       bool filter(edm::StreamID iID, edm::Event&, edm::EventSetup const&) const override {
0456         if (m_count < 1u) {
0457           throw cms::Exception("out of sequence") << "produce before beginProcessBlock " << m_count;
0458         }
0459         ++m_count;
0460         return true;
0461       }
0462 
0463       void endProcessBlock(edm::ProcessBlock const& processBlock) override {
0464         ++m_count;
0465         if (m_count != trans_) {
0466           throw cms::Exception("transitions")
0467               << "ProcessBlockIntFilter::end transitions " << m_count << " but it was supposed to be " << trans_;
0468         }
0469         {
0470           const unsigned int valueToGet = 31;
0471           if (not getTokenBegin_.isUninitialized()) {
0472             if (processBlock.get(getTokenBegin_) != valueToGet) {
0473               throw cms::Exception("BadValue")
0474                   << "expected " << valueToGet << " but got " << processBlock.get(getTokenBegin_);
0475             }
0476           }
0477         }
0478         {
0479           const unsigned int valueToGet = 41;
0480           if (not getTokenEnd_.isUninitialized()) {
0481             if (processBlock.get(getTokenEnd_) != valueToGet) {
0482               throw cms::Exception("BadValue")
0483                   << "expected " << valueToGet << " but got " << processBlock.get(getTokenEnd_);
0484             }
0485           }
0486         }
0487       }
0488 
0489       ~ProcessBlockIntFilter() {
0490         if (m_count != trans_) {
0491           throw cms::Exception("transitions")
0492               << "ProcessBlockIntFilter transitions " << m_count << " but it was supposed to be " << trans_;
0493         }
0494       }
0495 
0496     private:
0497       const unsigned int trans_;
0498       mutable std::atomic<unsigned int> m_count{0};
0499       edm::EDGetTokenT<unsigned int> getTokenBegin_;
0500       edm::EDGetTokenT<unsigned int> getTokenEnd_;
0501     };
0502 
0503     class TestBeginProcessBlockFilter : public edm::global::EDFilter<edm::BeginProcessBlockProducer> {
0504     public:
0505       explicit TestBeginProcessBlockFilter(edm::ParameterSet const& p)
0506           : trans_(p.getParameter<int>("transitions")), token_(produces<edm::Transition::BeginProcessBlock>("begin")) {
0507         produces<unsigned int>();
0508 
0509         auto tag = p.getParameter<edm::InputTag>("consumesBeginProcessBlock");
0510         if (not tag.label().empty()) {
0511           getToken_ = consumes<unsigned int, edm::InProcess>(tag);
0512         }
0513       }
0514 
0515       void beginProcessBlockProduce(edm::ProcessBlock& processBlock) override {
0516         if (m_count != 0) {
0517           throw cms::Exception("transitions")
0518               << "TestBeginProcessBlockFilter transitions " << m_count << " but it was supposed to be " << 0;
0519         }
0520         ++m_count;
0521         const unsigned int valueToPutAndGet = 31;
0522         processBlock.emplace(token_, valueToPutAndGet);
0523 
0524         if (not getToken_.isUninitialized()) {
0525           if (processBlock.get(getToken_) != valueToPutAndGet) {
0526             throw cms::Exception("BadValue")
0527                 << "expected " << valueToPutAndGet << " but got " << processBlock.get(getToken_);
0528           }
0529         }
0530       }
0531 
0532       bool filter(edm::StreamID iID, edm::Event&, edm::EventSetup const&) const override {
0533         if (m_count < 1u) {
0534           throw cms::Exception("out of sequence") << "produce before beginProcessBlockProduce " << m_count;
0535         }
0536         ++m_count;
0537         return true;
0538       }
0539 
0540       ~TestBeginProcessBlockFilter() {
0541         if (m_count != trans_) {
0542           throw cms::Exception("transitions")
0543               << "TestBeginProcessBlockFilter transitions " << m_count << " but it was supposed to be " << trans_;
0544         }
0545       }
0546 
0547     private:
0548       const unsigned int trans_;
0549       mutable std::atomic<unsigned int> m_count{0};
0550       edm::EDPutTokenT<unsigned int> token_;
0551       edm::EDGetTokenT<unsigned int> getToken_;
0552     };
0553 
0554     class TestEndProcessBlockFilter : public edm::global::EDFilter<edm::EndProcessBlockProducer> {
0555     public:
0556       explicit TestEndProcessBlockFilter(edm::ParameterSet const& p)
0557           : trans_(p.getParameter<int>("transitions")),
0558             token_(produces<unsigned int, edm::Transition::EndProcessBlock>("end")) {
0559         produces<unsigned int>();
0560 
0561         auto tag = p.getParameter<edm::InputTag>("consumesEndProcessBlock");
0562         if (not tag.label().empty()) {
0563           getToken_ = consumes<unsigned int, edm::InProcess>(tag);
0564         }
0565       }
0566 
0567       bool filter(edm::StreamID iID, edm::Event&, edm::EventSetup const&) const override {
0568         ++m_count;
0569         return true;
0570       }
0571 
0572       void endProcessBlockProduce(edm::ProcessBlock& processBlock) override {
0573         ++m_count;
0574         if (m_count != trans_) {
0575           throw cms::Exception("transitions")
0576               << "TestEndProcessBlockFilter transitions " << m_count << " but it was supposed to be " << trans_;
0577         }
0578         const unsigned int valueToPutAndGet = 41;
0579         processBlock.emplace(token_, valueToPutAndGet);
0580         if (not getToken_.isUninitialized()) {
0581           if (processBlock.get(getToken_) != valueToPutAndGet) {
0582             throw cms::Exception("BadValue")
0583                 << "expected " << valueToPutAndGet << " but got " << processBlock.get(getToken_);
0584           }
0585         }
0586       }
0587 
0588       ~TestEndProcessBlockFilter() {
0589         if (m_count != trans_) {
0590           throw cms::Exception("transitions")
0591               << "~TestEndProcessBlockFilter transitions " << m_count << " but it was supposed to be " << trans_;
0592         }
0593       }
0594 
0595     private:
0596       const unsigned int trans_;
0597       mutable std::atomic<unsigned int> m_count{0};
0598       edm::EDPutTokenT<unsigned int> token_;
0599       edm::EDGetTokenT<unsigned int> getToken_;
0600     };
0601 
0602     class TestBeginRunFilter : public edm::global::EDFilter<edm::RunCache<Dummy>, edm::BeginRunProducer> {
0603     public:
0604       explicit TestBeginRunFilter(edm::ParameterSet const& p) : trans_(p.getParameter<int>("transitions")) {
0605         produces<unsigned int>();
0606         produces<unsigned int, edm::Transition::BeginRun>("a");
0607       }
0608 
0609       const unsigned int trans_;
0610       mutable std::atomic<unsigned int> m_count{0};
0611       mutable std::atomic<bool> brp{false};
0612 
0613       std::shared_ptr<Dummy> globalBeginRun(edm::Run const& iRun, edm::EventSetup const&) const override {
0614         brp = false;
0615         return std::shared_ptr<Dummy>();
0616       }
0617 
0618       void globalBeginRunProduce(edm::Run&, edm::EventSetup const&) const override {
0619         ++m_count;
0620         brp = true;
0621       }
0622 
0623       bool filter(edm::StreamID iID, edm::Event&, edm::EventSetup const&) const override {
0624         if (!brp) {
0625           throw cms::Exception("out of sequence") << "filter before globalBeginRunProduce in Stream " << iID.value();
0626         }
0627         return true;
0628       }
0629 
0630       void globalEndRun(edm::Run const& iRun, edm::EventSetup const&) const override {}
0631 
0632       ~TestBeginRunFilter() {
0633         if (m_count != trans_) {
0634           throw cms::Exception("transitions")
0635               << "TestBeginRunFilter transitions " << m_count << " but it was supposed to be " << trans_;
0636         }
0637       }
0638     };
0639 
0640     class TestEndRunFilter : public edm::global::EDFilter<edm::RunCache<Dummy>, edm::EndRunProducer> {
0641     public:
0642       explicit TestEndRunFilter(edm::ParameterSet const& p) : trans_(p.getParameter<int>("transitions")) {
0643         produces<unsigned int>();
0644         produces<unsigned int, edm::Transition::EndRun>("a");
0645       }
0646       const unsigned int trans_;
0647       mutable std::atomic<unsigned int> m_count{0};
0648       mutable std::atomic<bool> p{false};
0649 
0650       std::shared_ptr<Dummy> globalBeginRun(edm::Run const& iRun, edm::EventSetup const&) const override {
0651         p = false;
0652         return std::shared_ptr<Dummy>();
0653       }
0654 
0655       bool filter(edm::StreamID, edm::Event&, edm::EventSetup const&) const override {
0656         p = true;
0657         return true;
0658       }
0659 
0660       void globalEndRunProduce(edm::Run&, edm::EventSetup const&) const override {
0661         if (!p) {
0662           throw cms::Exception("out of sequence") << "endRunProduce before produce";
0663         }
0664         ++m_count;
0665       }
0666 
0667       void globalEndRun(edm::Run const& iRun, edm::EventSetup const&) const override {}
0668 
0669       ~TestEndRunFilter() {
0670         if (m_count != trans_) {
0671           throw cms::Exception("transitions")
0672               << "TestEndRunFilter transitions " << m_count << " but it was supposed to be " << trans_;
0673         }
0674       }
0675     };
0676 
0677     class TestBeginLumiBlockFilter
0678         : public edm::global::EDFilter<edm::LuminosityBlockCache<Dummy>, edm::BeginLuminosityBlockProducer> {
0679     public:
0680       explicit TestBeginLumiBlockFilter(edm::ParameterSet const& p) : trans_(p.getParameter<int>("transitions")) {
0681         produces<unsigned int>();
0682         produces<unsigned int, edm::Transition::BeginLuminosityBlock>("a");
0683       }
0684       const unsigned int trans_;
0685       mutable std::atomic<unsigned int> m_count{0};
0686       mutable std::atomic<bool> gblp{false};
0687 
0688       std::shared_ptr<Dummy> globalBeginLuminosityBlock(edm::LuminosityBlock const& iLB,
0689                                                         edm::EventSetup const&) const override {
0690         gblp = false;
0691         return std::shared_ptr<Dummy>();
0692       }
0693 
0694       void globalBeginLuminosityBlockProduce(edm::LuminosityBlock&, edm::EventSetup const&) const override {
0695         ++m_count;
0696         gblp = true;
0697       }
0698 
0699       bool filter(edm::StreamID iID, edm::Event&, const edm::EventSetup&) const override {
0700         if (!gblp) {
0701           throw cms::Exception("out of sequence")
0702               << "filter before globalBeginLuminosityBlockProduce in Stream " << iID.value();
0703         }
0704         return true;
0705       }
0706 
0707       void globalEndLuminosityBlock(edm::LuminosityBlock const& iLB, edm::EventSetup const&) const override {}
0708 
0709       ~TestBeginLumiBlockFilter() {
0710         if (m_count != trans_) {
0711           throw cms::Exception("transitions")
0712               << "TestBeginLumiBlockFilter transitions " << m_count << " but it was supposed to be " << trans_;
0713         }
0714       }
0715     };
0716 
0717     class TestEndLumiBlockFilter
0718         : public edm::global::EDFilter<edm::LuminosityBlockCache<Dummy>, edm::EndLuminosityBlockProducer> {
0719     public:
0720       explicit TestEndLumiBlockFilter(edm::ParameterSet const& p) : trans_(p.getParameter<int>("transitions")) {
0721         produces<unsigned int>();
0722         produces<unsigned int, edm::Transition::EndLuminosityBlock>("a");
0723       }
0724       const unsigned int trans_;
0725       mutable std::atomic<unsigned int> m_count{0};
0726       mutable std::atomic<bool> p{false};
0727 
0728       std::shared_ptr<Dummy> globalBeginLuminosityBlock(edm::LuminosityBlock const& iLB,
0729                                                         edm::EventSetup const&) const override {
0730         p = false;
0731         return std::shared_ptr<Dummy>();
0732       }
0733 
0734       bool filter(edm::StreamID, edm::Event&, edm::EventSetup const&) const override {
0735         p = true;
0736         return true;
0737       }
0738 
0739       void globalEndLuminosityBlockProduce(edm::LuminosityBlock&, edm::EventSetup const&) const override {
0740         if (!p) {
0741           throw cms::Exception("out of sequence") << "endLumiBlockProduce before produce";
0742         }
0743         ++m_count;
0744       }
0745 
0746       void globalEndLuminosityBlock(edm::LuminosityBlock const& iLB, edm::EventSetup const&) const override {}
0747 
0748       ~TestEndLumiBlockFilter() {
0749         if (m_count != trans_) {
0750           throw cms::Exception("transitions")
0751               << "TestEndLumiBlockFilter transitions " << m_count << " but it was supposed to be " << trans_;
0752         }
0753       }
0754     };
0755 
0756     class TestInputProcessBlockCache {
0757     public:
0758       long long int value_ = 0;
0759     };
0760 
0761     class TestInputProcessBlockCache1 {
0762     public:
0763       long long int value_ = 0;
0764     };
0765 
0766     class InputProcessBlockIntFilter
0767         : public edm::global::EDFilter<
0768               edm::InputProcessBlockCache<int, TestInputProcessBlockCache, TestInputProcessBlockCache1>> {
0769     public:
0770       explicit InputProcessBlockIntFilter(edm::ParameterSet const& pset) {
0771         expectedTransitions_ = pset.getParameter<int>("transitions");
0772         expectedByRun_ = pset.getParameter<std::vector<int>>("expectedByRun");
0773         expectedSum_ = pset.getParameter<int>("expectedSum");
0774         {
0775           auto tag = pset.getParameter<edm::InputTag>("consumesBeginProcessBlock");
0776           if (not tag.label().empty()) {
0777             getTokenBegin_ = consumes<IntProduct, edm::InProcess>(tag);
0778           }
0779         }
0780         {
0781           auto tag = pset.getParameter<edm::InputTag>("consumesEndProcessBlock");
0782           if (not tag.label().empty()) {
0783             getTokenEnd_ = consumes<IntProduct, edm::InProcess>(tag);
0784           }
0785         }
0786         {
0787           auto tag = pset.getParameter<edm::InputTag>("consumesBeginProcessBlockM");
0788           if (not tag.label().empty()) {
0789             getTokenBeginM_ = consumes<IntProduct, edm::InProcess>(tag);
0790           }
0791         }
0792         {
0793           auto tag = pset.getParameter<edm::InputTag>("consumesEndProcessBlockM");
0794           if (not tag.label().empty()) {
0795             getTokenEndM_ = consumes<IntProduct, edm::InProcess>(tag);
0796           }
0797         }
0798         registerProcessBlockCacheFiller<int>(
0799             getTokenBegin_, [this](edm::ProcessBlock const& processBlock, std::shared_ptr<int> const& previousCache) {
0800               auto returnValue = std::make_shared<int>(0);
0801               *returnValue += processBlock.get(getTokenBegin_).value;
0802               *returnValue += processBlock.get(getTokenEnd_).value;
0803               ++transitions_;
0804               return returnValue;
0805             });
0806         registerProcessBlockCacheFiller<1>(getTokenBegin_,
0807                                            [this](edm::ProcessBlock const& processBlock,
0808                                                   std::shared_ptr<TestInputProcessBlockCache> const& previousCache) {
0809                                              auto returnValue = std::make_shared<TestInputProcessBlockCache>();
0810                                              returnValue->value_ += processBlock.get(getTokenBegin_).value;
0811                                              returnValue->value_ += processBlock.get(getTokenEnd_).value;
0812                                              ++transitions_;
0813                                              return returnValue;
0814                                            });
0815         registerProcessBlockCacheFiller<TestInputProcessBlockCache1>(
0816             getTokenBegin_,
0817             [this](edm::ProcessBlock const& processBlock,
0818                    std::shared_ptr<TestInputProcessBlockCache1> const& previousCache) {
0819               auto returnValue = std::make_shared<TestInputProcessBlockCache1>();
0820               returnValue->value_ += processBlock.get(getTokenBegin_).value;
0821               returnValue->value_ += processBlock.get(getTokenEnd_).value;
0822               ++transitions_;
0823               return returnValue;
0824             });
0825       }
0826 
0827       void accessInputProcessBlock(edm::ProcessBlock const& processBlock) override {
0828         if (processBlock.processName() == "PROD1") {
0829           sum_ += processBlock.get(getTokenBegin_).value;
0830           sum_ += processBlock.get(getTokenEnd_).value;
0831         }
0832         if (processBlock.processName() == "MERGE") {
0833           sum_ += processBlock.get(getTokenBeginM_).value;
0834           sum_ += processBlock.get(getTokenEndM_).value;
0835         }
0836         ++transitions_;
0837       }
0838 
0839       bool filter(edm::StreamID, edm::Event& event, edm::EventSetup const&) const override {
0840         auto cacheTuple = processBlockCaches(event);
0841         if (!expectedByRun_.empty()) {
0842           if (expectedByRun_.at(event.run() - 1) != *std::get<edm::CacheHandle<int>>(cacheTuple)) {
0843             throw cms::Exception("UnexpectedValue")
0844                 << "InputProcessBlockIntFilter::filter cached value was "
0845                 << *std::get<edm::CacheHandle<int>>(cacheTuple) << " but it was supposed to be "
0846                 << expectedByRun_.at(event.run() - 1);
0847           }
0848           if (expectedByRun_.at(event.run() - 1) != std::get<1>(cacheTuple)->value_) {
0849             throw cms::Exception("UnexpectedValue")
0850                 << "InputProcessBlockIntFilter::filter second cached value was " << std::get<1>(cacheTuple)->value_
0851                 << " but it was supposed to be " << expectedByRun_.at(event.run() - 1);
0852           }
0853           if (expectedByRun_.at(event.run() - 1) !=
0854               std::get<edm::CacheHandle<TestInputProcessBlockCache1>>(cacheTuple)->value_) {
0855             throw cms::Exception("UnexpectedValue")
0856                 << "InputProcessBlockIntFilter::filter third cached value was "
0857                 << std::get<edm::CacheHandle<TestInputProcessBlockCache1>>(cacheTuple)->value_
0858                 << " but it was supposed to be " << expectedByRun_.at(event.run() - 1);
0859           }
0860         }
0861         ++transitions_;
0862         return true;
0863       }
0864 
0865       void endJob() override {
0866         if (transitions_ != expectedTransitions_) {
0867           throw cms::Exception("transitions") << "InputProcessBlockIntFilter transitions " << transitions_
0868                                               << " but it was supposed to be " << expectedTransitions_;
0869         }
0870         if (sum_ != expectedSum_) {
0871           throw cms::Exception("UnexpectedValue")
0872               << "InputProcessBlockIntFilter sum " << sum_ << " but it was supposed to be " << expectedSum_;
0873         }
0874         if (cacheSize() > 0u) {
0875           throw cms::Exception("UnexpectedValue")
0876               << "InputProcessBlockIntFilter cache size not zero at endJob " << cacheSize();
0877         }
0878       }
0879 
0880     private:
0881       edm::EDGetTokenT<IntProduct> getTokenBegin_;
0882       edm::EDGetTokenT<IntProduct> getTokenEnd_;
0883       edm::EDGetTokenT<IntProduct> getTokenBeginM_;
0884       edm::EDGetTokenT<IntProduct> getTokenEndM_;
0885       mutable std::atomic<unsigned int> transitions_{0};
0886       int sum_{0};
0887       unsigned int expectedTransitions_{0};
0888       std::vector<int> expectedByRun_;
0889       int expectedSum_{0};
0890     };
0891 
0892   }  // namespace global
0893 }  // namespace edmtest
0894 
0895 DEFINE_FWK_MODULE(edmtest::global::StreamIntFilter);
0896 DEFINE_FWK_MODULE(edmtest::global::RunIntFilter);
0897 DEFINE_FWK_MODULE(edmtest::global::LumiIntFilter);
0898 DEFINE_FWK_MODULE(edmtest::global::RunSummaryIntFilter);
0899 DEFINE_FWK_MODULE(edmtest::global::LumiSummaryIntFilter);
0900 DEFINE_FWK_MODULE(edmtest::global::ProcessBlockIntFilter);
0901 DEFINE_FWK_MODULE(edmtest::global::TestBeginProcessBlockFilter);
0902 DEFINE_FWK_MODULE(edmtest::global::TestEndProcessBlockFilter);
0903 DEFINE_FWK_MODULE(edmtest::global::TestBeginRunFilter);
0904 DEFINE_FWK_MODULE(edmtest::global::TestBeginLumiBlockFilter);
0905 DEFINE_FWK_MODULE(edmtest::global::TestEndRunFilter);
0906 DEFINE_FWK_MODULE(edmtest::global::TestEndLumiBlockFilter);
0907 DEFINE_FWK_MODULE(edmtest::global::InputProcessBlockIntFilter);