Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 
0002 #include "FWCore/Framework/interface/stream/EDFilter.h"
0003 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0004 #include "DataFormats/TestObjects/interface/Thing.h"
0005 #include "DataFormats/TestObjects/interface/ToyProducts.h"
0006 #include "FWCore/Framework/interface/GetterOfProducts.h"
0007 #include "FWCore/Framework/interface/MakerMacros.h"
0008 #include "FWCore/Framework/interface/ModuleLabelMatch.h"
0009 #include "FWCore/Framework/interface/ProcessMatch.h"
0010 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0011 #include "FWCore/Utilities/interface/BranchType.h"
0012 #include "FWCore/Utilities/interface/Exception.h"
0013 
0014 #include <iostream>
0015 #include <string>
0016 #include <vector>
0017 
0018 namespace edm {
0019   class BranchDescription;
0020   class Event;
0021   class EventSetup;
0022 }  // namespace edm
0023 
0024 namespace edmtest {
0025 
0026   class TestGetterOfProducts : public edm::stream::EDFilter<> {
0027   public:
0028     explicit TestGetterOfProducts(edm::ParameterSet const&);
0029     ~TestGetterOfProducts() override;
0030     bool filter(edm::Event&, edm::EventSetup const&) override;
0031 
0032   private:
0033     std::string processName_;
0034     std::vector<std::string> expectedInputTagLabels_;
0035     std::vector<std::string> expectedLabelsAfterGet_;
0036     edm::GetterOfProducts<Thing> getterOfProducts_;
0037   };
0038 
0039   TestGetterOfProducts::TestGetterOfProducts(edm::ParameterSet const& par)
0040       : processName_(par.getParameter<std::string>("processName")),
0041         expectedInputTagLabels_(par.getParameter<std::vector<std::string> >("expectedInputTagLabels")),
0042         expectedLabelsAfterGet_(par.getParameter<std::vector<std::string> >("expectedLabelsAfterGet")),
0043         getterOfProducts_(edm::ProcessMatch(processName_), this) {
0044     callWhenNewProductsRegistered(getterOfProducts_);
0045   }
0046 
0047   TestGetterOfProducts::~TestGetterOfProducts() {}
0048 
0049   bool TestGetterOfProducts::filter(edm::Event& event, edm::EventSetup const&) {
0050     // These are the InputTag's of the products the getter will look for.
0051     // These are the matching products in the ProductRegistry, but they
0052     // may or may not be in a particular Event.
0053     auto const& tokens = getterOfProducts_.tokens();
0054 
0055     if (expectedInputTagLabels_.size() != tokens.size()) {
0056       std::cerr << "Expected number of InputTag's differs from actual number.\n"
0057                 << "In function TestGetterOfProducts::filter\n"
0058                 << "Expected value = " << expectedInputTagLabels_.size() << " actual value = " << tokens.size()
0059                 << std::endl;
0060       abort();
0061     }
0062 
0063     std::vector<std::string>::const_iterator iter = expectedInputTagLabels_.begin();
0064     for (auto const& token : tokens) {
0065       edm::EDConsumerBase::Labels labels;
0066       labelsForToken(token, labels);
0067       if (*iter != labels.module) {
0068         throw cms::Exception("Failed Test")
0069             << "Expected InputTag differs from actual InputTag.\n"
0070             << "In function TestGetterOfProducts::filter\n"
0071             << "Expected value = " << *iter << " actual value = " << labels.module << std::endl;
0072       }
0073       ++iter;
0074     }
0075 
0076     // A handle is added to this vector for each product that is found in the event.
0077     std::vector<edm::Handle<Thing> > handles;
0078     getterOfProducts_.fillHandles(event, handles);
0079 
0080     if (expectedLabelsAfterGet_.size() != handles.size()) {
0081       throw cms::Exception("Failed Test")
0082           << "Expected number of products gotten differs from actual number.\n"
0083           << "In function TestGetterOfProducts::filter\n"
0084           << "Expected value = " << expectedLabelsAfterGet_.size() << " actual value = " << handles.size() << std::endl;
0085     }
0086 
0087     iter = expectedLabelsAfterGet_.begin();
0088     for (auto const& handle : handles) {
0089       if (handle.provenance()->moduleLabel() != *iter) {
0090         throw cms::Exception("Failed Test")
0091             << "Expected module label after get differs from actual module label.\n"
0092             << "In function TestGetterOfProducts::filter\n"
0093             << "Expected value = " << *iter << " actual value = " << handle.provenance()->moduleLabel() << std::endl;
0094         break;
0095       }
0096       ++iter;
0097     }
0098 
0099     return true;
0100   }
0101 
0102   // -----------------------------------------------------------------------------------------------------------
0103 
0104   // Very similar to the above class with these differences.
0105   // 1. The is an EDAnalyzer instead of a EDFilter
0106   // 2. It gets multiple types of products so there are
0107   // two GetterOfProducts objects.
0108   // 3. Initialize one of the GetterOfProducts later in the
0109   // constructor.
0110   // 4. It can be configured to get things from the Run, LuminosityBlock,
0111   // or Event.
0112   class TestGetterOfProductsA : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm::one::WatchLuminosityBlocks> {
0113   public:
0114     explicit TestGetterOfProductsA(edm::ParameterSet const&);
0115     ~TestGetterOfProductsA() override;
0116     void analyze(edm::Event const&, edm::EventSetup const&) override;
0117     void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
0118     void endRun(edm::Run const&, edm::EventSetup const&) override;
0119 
0120     void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override {}
0121     void beginRun(edm::Run const&, edm::EventSetup const&) override {}
0122 
0123   private:
0124     std::string processName_;
0125     edm::BranchType branchType_;
0126     std::vector<std::string> expectedInputTagLabels_;
0127     std::vector<std::string> expectedLabelsAfterGet_;
0128     unsigned expectedNumberOfThingsWithLabelA_;
0129     edm::GetterOfProducts<IntProduct> getterOfIntProducts_;
0130     edm::GetterOfProducts<Thing> getterOfProducts_;
0131     edm::GetterOfProducts<Thing> getterUsingLabel_;
0132   };
0133 
0134   TestGetterOfProductsA::TestGetterOfProductsA(edm::ParameterSet const& par)
0135       : processName_(par.getParameter<std::string>("processName")),
0136         branchType_(edm::InEvent),
0137         expectedInputTagLabels_(par.getParameter<std::vector<std::string> >("expectedInputTagLabels")),
0138         expectedLabelsAfterGet_(par.getParameter<std::vector<std::string> >("expectedLabelsAfterGet")),
0139         expectedNumberOfThingsWithLabelA_(par.getParameter<unsigned>("expectedNumberOfThingsWithLabelA")),
0140         getterOfIntProducts_(),
0141         getterOfProducts_(),
0142         getterUsingLabel_() {
0143     int bt = par.getParameter<int>("branchType");
0144     if (bt == 1)
0145       branchType_ = edm::InLumi;
0146     else if (bt == 2)
0147       branchType_ = edm::InRun;
0148 
0149     getterOfIntProducts_ = edm::GetterOfProducts<IntProduct>(edm::ProcessMatch(processName_), this);
0150     getterOfProducts_ = edm::GetterOfProducts<Thing>(edm::ProcessMatch(processName_), this, branchType_);
0151     getterUsingLabel_ = edm::GetterOfProducts<Thing>(edm::ModuleLabelMatch("A"), this, branchType_);
0152 
0153     callWhenNewProductsRegistered([this](edm::BranchDescription const& bd) {
0154       getterOfIntProducts_(bd);
0155       getterOfProducts_(bd);
0156       getterUsingLabel_(bd);
0157     });
0158   }
0159 
0160   TestGetterOfProductsA::~TestGetterOfProductsA() {}
0161 
0162   void TestGetterOfProductsA::analyze(edm::Event const& event, edm::EventSetup const&) {
0163     if (branchType_ != edm::InEvent)
0164       return;
0165 
0166     auto const& tokens = getterOfProducts_.tokens();
0167 
0168     if (expectedInputTagLabels_.size() != tokens.size()) {
0169       throw cms::Exception("Failed Test")
0170           << "Expected number of InputTag's differs from actual number.\n"
0171           << "In function TestGetterOfProducts::filter\n"
0172           << "Expected value = " << expectedInputTagLabels_.size() << " actual value = " << tokens.size() << std::endl;
0173     }
0174 
0175     std::vector<std::string>::const_iterator iter = expectedInputTagLabels_.begin();
0176     for (auto const& token : tokens) {
0177       edm::EDConsumerBase::Labels labels;
0178       labelsForToken(token, labels);
0179       if (*iter != labels.module) {
0180         throw cms::Exception("Failed Test")
0181             << "Expected InputTag differs from actual InputTag.\n"
0182             << "In function TestGetterOfProducts::filter\n"
0183             << "Expected value = " << *iter << " actual value = " << labels.module << std::endl;
0184       }
0185       ++iter;
0186     }
0187 
0188     std::vector<edm::Handle<Thing> > handles;
0189     getterOfProducts_.fillHandles(event, handles);
0190 
0191     if (expectedLabelsAfterGet_.size() != handles.size()) {
0192       throw cms::Exception("Failed Test")
0193           << "Expected number of products gotten differs from actual number.\n"
0194           << "In function TestGetterOfProducts::filter\n"
0195           << "Expected value = " << expectedLabelsAfterGet_.size() << " actual value = " << handles.size() << std::endl;
0196     }
0197 
0198     iter = expectedLabelsAfterGet_.begin();
0199     for (auto const& handle : handles) {
0200       if (handle.provenance()->moduleLabel() != *iter) {
0201         throw cms::Exception("Failed Test")
0202             << "Expected module label after get differs from actual module label.\n"
0203             << "In function TestGetterOfProducts::filter\n"
0204             << "Expected value = " << *iter << " actual value = " << handle.provenance()->moduleLabel() << std::endl;
0205         break;
0206       }
0207       ++iter;
0208     }
0209 
0210     getterUsingLabel_.fillHandles(event, handles);
0211 
0212     if (expectedNumberOfThingsWithLabelA_ != handles.size()) {
0213       throw cms::Exception("Failed Test")
0214           << "Expected number of products with module label A gotten differs from actual number.\n"
0215           << "In function TestGetterOfProducts::filter\n"
0216           << "Expected value = " << expectedNumberOfThingsWithLabelA_ << " actual value = " << handles.size()
0217           << std::endl;
0218     }
0219 
0220     iter = expectedLabelsAfterGet_.begin();
0221     for (auto const& handle : handles) {
0222       if (handle.provenance()->moduleLabel() != std::string("A")) {
0223         throw cms::Exception("Failed Test") << "Expected module label after get differs from actual module label.\n"
0224                                             << "In function TestGetterOfProducts::filter\n"
0225                                             << "For this get a module label \"A\" is always expected "
0226                                             << " actual value = " << handle.provenance()->moduleLabel() << std::endl;
0227         break;
0228       }
0229       ++iter;
0230     }
0231   }
0232 
0233   void TestGetterOfProductsA::endLuminosityBlock(edm::LuminosityBlock const& lumi, edm::EventSetup const&) {
0234     if (branchType_ != edm::InLumi)
0235       return;
0236 
0237     auto const& tokens = getterOfProducts_.tokens();
0238 
0239     if (expectedInputTagLabels_.size() != tokens.size()) {
0240       throw cms::Exception("Failed Test")
0241           << "Expected number of InputTag's differs from actual number.\n"
0242           << "In function TestGetterOfProducts::endLuminosityBlock\n"
0243           << "Expected value = " << expectedInputTagLabels_.size() << " actual value = " << tokens.size() << std::endl;
0244     }
0245 
0246     std::vector<std::string>::const_iterator iter = expectedInputTagLabels_.begin();
0247     for (auto const& token : tokens) {
0248       edm::EDConsumerBase::Labels labels;
0249       labelsForToken(token, labels);
0250 
0251       if (*iter != labels.module) {
0252         throw cms::Exception("Failed Test")
0253             << "Expected InputTag differs from actual InputTag.\n"
0254             << "In function TestGetterOfProducts::endLuminosityBlock\n"
0255             << "Expected value = " << *iter << " actual value = " << labels.module << std::endl;
0256       }
0257       ++iter;
0258     }
0259 
0260     std::vector<edm::Handle<Thing> > handles;
0261     getterOfProducts_.fillHandles(lumi, handles);
0262 
0263     if (expectedLabelsAfterGet_.size() != handles.size()) {
0264       throw cms::Exception("Failed Test")
0265           << "Expected number of products gotten differs from actual number.\n"
0266           << "In function TestGetterOfProducts::endLuminosityBlock\n"
0267           << "Expected value = " << expectedLabelsAfterGet_.size() << " actual value = " << handles.size() << std::endl;
0268     }
0269 
0270     iter = expectedLabelsAfterGet_.begin();
0271     for (auto const& handle : handles) {
0272       if (handle.provenance()->moduleLabel() != *iter) {
0273         throw cms::Exception("Failed Test")
0274             << "Expected module label after get differs from actual module label.\n"
0275             << "In function TestGetterOfProducts::endLuminosityBlock\n"
0276             << "Expected value = " << *iter << " actual value = " << handle.provenance()->moduleLabel() << std::endl;
0277         break;
0278       }
0279       ++iter;
0280     }
0281 
0282     getterUsingLabel_.fillHandles(lumi, handles);
0283 
0284     if (expectedNumberOfThingsWithLabelA_ != handles.size()) {
0285       throw cms::Exception("Failed Test")
0286           << "Expected number of products with module label A gotten differs from actual number.\n"
0287           << "In function TestGetterOfProducts::endLuminosityBlock\n"
0288           << "Expected value = " << expectedNumberOfThingsWithLabelA_ << " actual value = " << handles.size()
0289           << std::endl;
0290     }
0291 
0292     iter = expectedLabelsAfterGet_.begin();
0293     for (auto const& handle : handles) {
0294       if (handle.provenance()->moduleLabel() != std::string("A")) {
0295         throw cms::Exception("Failed Test") << "Expected module label after get differs from actual module label.\n"
0296                                             << "In function TestGetterOfProducts::endLuminosityBlock\n"
0297                                             << "For this get a module label \"A\" is always expected "
0298                                             << " actual value = " << handle.provenance()->moduleLabel() << std::endl;
0299         break;
0300       }
0301       ++iter;
0302     }
0303   }
0304 
0305   void TestGetterOfProductsA::endRun(edm::Run const& run, edm::EventSetup const&) {
0306     if (branchType_ != edm::InRun)
0307       return;
0308 
0309     auto const& tokens = getterOfProducts_.tokens();
0310 
0311     if (expectedInputTagLabels_.size() != tokens.size()) {
0312       throw cms::Exception("Failed Test")
0313           << "Expected number of InputTag's differs from actual number.\n"
0314           << "In function TestGetterOfProducts::endRun\n"
0315           << "Expected value = " << expectedInputTagLabels_.size() << " actual value = " << tokens.size() << std::endl;
0316     }
0317 
0318     std::vector<std::string>::const_iterator iter = expectedInputTagLabels_.begin();
0319     for (auto const& token : tokens) {
0320       edm::EDConsumerBase::Labels labels;
0321       labelsForToken(token, labels);
0322       if (*iter != labels.module) {
0323         throw cms::Exception("Failed Test")
0324             << "Expected InputTag differs from actual InputTag.\n"
0325             << "In function TestGetterOfProducts::endRun\n"
0326             << "Expected value = " << *iter << " actual value = " << labels.module << std::endl;
0327       }
0328       ++iter;
0329     }
0330 
0331     std::vector<edm::Handle<Thing> > handles;
0332     getterOfProducts_.fillHandles(run, handles);
0333 
0334     if (expectedLabelsAfterGet_.size() != handles.size()) {
0335       throw cms::Exception("Failed Test")
0336           << "Expected number of products gotten differs from actual number.\n"
0337           << "In function TestGetterOfProducts::endRun\n"
0338           << "Expected value = " << expectedLabelsAfterGet_.size() << " actual value = " << handles.size() << std::endl;
0339     }
0340 
0341     iter = expectedLabelsAfterGet_.begin();
0342     for (auto const& handle : handles) {
0343       if (handle.provenance()->moduleLabel() != *iter) {
0344         throw cms::Exception("Failed Test")
0345             << "Expected module label after get differs from actual module label.\n"
0346             << "In function TestGetterOfProducts::endRun\n"
0347             << "Expected value = " << *iter << " actual value = " << handle.provenance()->moduleLabel() << std::endl;
0348         break;
0349       }
0350       ++iter;
0351     }
0352 
0353     getterUsingLabel_.fillHandles(run, handles);
0354 
0355     if (expectedNumberOfThingsWithLabelA_ != handles.size()) {
0356       throw cms::Exception("Failed Test")
0357           << "Expected number of products with module label A gotten differs from actual number.\n"
0358           << "In function TestGetterOfProducts::endRun\n"
0359           << "Expected value = " << expectedNumberOfThingsWithLabelA_ << " actual value = " << handles.size()
0360           << std::endl;
0361     }
0362 
0363     iter = expectedLabelsAfterGet_.begin();
0364     for (auto const& handle : handles) {
0365       if (handle.provenance()->moduleLabel() != std::string("A")) {
0366         throw cms::Exception("Failed Test") << "Expected module label after get differs from actual module label.\n"
0367                                             << "In function TestGetterOfProducts::endRun\n"
0368                                             << "For this get a module label \"A\" is always expected "
0369                                             << " actual value = " << handle.provenance()->moduleLabel() << std::endl;
0370         break;
0371       }
0372       ++iter;
0373     }
0374   }
0375 }  // namespace edmtest
0376 using namespace edmtest;
0377 DEFINE_FWK_MODULE(TestGetterOfProducts);
0378 DEFINE_FWK_MODULE(TestGetterOfProductsA);