Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 
0002 // This test module will try to get IntProducts in Events,
0003 // Lumis, Runs and ProcessBlocks. The number of IntProducts
0004 // and their InputTags (label, instance, process) must be configured.
0005 
0006 // One can also configure an expected value for the sum of
0007 // all the values in the IntProducts that are found.  Note
0008 // that an IntProduct is just a test product that simply
0009 // contains a single integer.
0010 
0011 // If the expected products are not found or some other
0012 // unexpected behavior occurs, an exception will be thrown.
0013 
0014 #include "DataFormats/Common/interface/Handle.h"
0015 #include "DataFormats/Provenance/interface/Provenance.h"
0016 #include "DataFormats/TestObjects/interface/ToyProducts.h"
0017 #include "FWCore/Framework/interface/CacheHandle.h"
0018 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0019 #include "FWCore/Framework/interface/Event.h"
0020 #include "FWCore/Framework/interface/getProducerParameterSet.h"
0021 #include "FWCore/Framework/interface/GetterOfProducts.h"
0022 #include "FWCore/Framework/interface/LuminosityBlock.h"
0023 #include "FWCore/Framework/interface/MakerMacros.h"
0024 #include "FWCore/Framework/interface/moduleAbilities.h"
0025 #include "FWCore/Framework/interface/ProcessBlock.h"
0026 #include "FWCore/Framework/interface/ProcessMatch.h"
0027 #include "FWCore/Framework/interface/Run.h"
0028 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0029 #include "FWCore/Utilities/interface/BranchType.h"
0030 #include "FWCore/Utilities/interface/Exception.h"
0031 #include "FWCore/Utilities/interface/InputTag.h"
0032 #include "FWCore/Utilities/interface/EDGetToken.h"
0033 
0034 #include <functional>
0035 #include <iostream>
0036 #include <memory>
0037 #include <tuple>
0038 #include <vector>
0039 
0040 namespace edmtest {
0041 
0042   class TestFindProduct : public edm::one::EDAnalyzer<edm::one::WatchRuns,
0043                                                       edm::one::WatchLuminosityBlocks,
0044                                                       edm::WatchProcessBlock,
0045                                                       edm::InputProcessBlockCache<int, long long int>> {
0046   public:
0047     explicit TestFindProduct(edm::ParameterSet const& pset);
0048     ~TestFindProduct() override;
0049 
0050     void analyze(edm::Event const& e, edm::EventSetup const& es) override;
0051     void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
0052     void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override;
0053     void beginRun(edm::Run const&, edm::EventSetup const&) override;
0054     void endRun(edm::Run const&, edm::EventSetup const&) override;
0055     void beginProcessBlock(edm::ProcessBlock const&) override;
0056     void accessInputProcessBlock(edm::ProcessBlock const&) override;
0057     void endProcessBlock(edm::ProcessBlock const&) override;
0058     void endJob() override;
0059 
0060   private:
0061     std::vector<edm::InputTag> inputTags_;
0062     int expectedSum_;
0063     int expectedCache_;
0064     int sum_;
0065     std::vector<edm::InputTag> inputTagsNotFound_;
0066     bool getByTokenFirst_;
0067     std::vector<edm::InputTag> inputTagsView_;
0068     bool runProducerParameterCheck_;
0069     bool testGetterOfProducts_;
0070 
0071     std::vector<edm::InputTag> inputTagsUInt64_;
0072     std::vector<edm::InputTag> inputTagsEndLumi_;
0073     std::vector<edm::InputTag> inputTagsEndRun_;
0074     std::vector<edm::InputTag> inputTagsBeginProcessBlock_;
0075     std::vector<edm::InputTag> inputTagsInputProcessBlock_;
0076     std::vector<edm::InputTag> inputTagsEndProcessBlock_;
0077     std::vector<edm::InputTag> inputTagsEndProcessBlock2_;
0078     std::vector<edm::InputTag> inputTagsEndProcessBlock3_;
0079     std::vector<edm::InputTag> inputTagsEndProcessBlock4_;
0080 
0081     std::vector<edm::EDGetTokenT<IntProduct>> tokens_;
0082     std::vector<edm::EDGetTokenT<IntProduct>> tokensNotFound_;
0083     std::vector<edm::EDGetTokenT<edm::View<int>>> tokensView_;
0084     std::vector<edm::EDGetTokenT<UInt64Product>> tokensUInt64_;
0085     std::vector<edm::EDGetTokenT<IntProduct>> tokensEndLumi_;
0086     std::vector<edm::EDGetTokenT<IntProduct>> tokensEndRun_;
0087     std::vector<edm::EDGetTokenT<IntProduct>> tokensBeginProcessBlock_;
0088     std::vector<edm::EDGetTokenT<IntProduct>> tokensInputProcessBlock_;
0089     std::vector<edm::EDGetTokenT<IntProduct>> tokensEndProcessBlock_;
0090     std::vector<edm::EDGetToken> tokensEndProcessBlock2_;
0091     std::vector<edm::EDGetTokenT<IntProduct>> tokensEndProcessBlock3_;
0092     std::vector<edm::EDGetTokenT<IntProduct>> tokensEndProcessBlock4_;
0093 
0094     edm::GetterOfProducts<IntProduct> getterOfProducts_;
0095 
0096   };  // class TestFindProduct
0097 
0098   //--------------------------------------------------------------------
0099   //
0100   // Implementation details
0101 
0102   TestFindProduct::TestFindProduct(edm::ParameterSet const& pset)
0103       : inputTags_(pset.getUntrackedParameter<std::vector<edm::InputTag>>("inputTags")),
0104         expectedSum_(pset.getUntrackedParameter<int>("expectedSum", 0)),
0105         expectedCache_(pset.getUntrackedParameter<int>("expectedCache", 0)),
0106         sum_(0),
0107         inputTagsNotFound_(),
0108         getByTokenFirst_(pset.getUntrackedParameter<bool>("getByTokenFirst", false)),
0109         inputTagsView_(),
0110         runProducerParameterCheck_(pset.getUntrackedParameter<bool>("runProducerParameterCheck", false)),
0111         testGetterOfProducts_(pset.getUntrackedParameter<bool>("testGetterOfProducts", false)),
0112         getterOfProducts_(edm::ProcessMatch("*"), this, edm::InProcess) {
0113     if (testGetterOfProducts_) {
0114       callWhenNewProductsRegistered(getterOfProducts_);
0115     }
0116     std::vector<edm::InputTag> emptyTagVector;
0117     inputTagsNotFound_ = pset.getUntrackedParameter<std::vector<edm::InputTag>>("inputTagsNotFound", emptyTagVector);
0118     inputTagsView_ = pset.getUntrackedParameter<std::vector<edm::InputTag>>("inputTagsView", emptyTagVector);
0119     inputTagsUInt64_ = pset.getUntrackedParameter<std::vector<edm::InputTag>>("inputTagsUInt64", emptyTagVector);
0120     inputTagsEndLumi_ = pset.getUntrackedParameter<std::vector<edm::InputTag>>("inputTagsEndLumi", emptyTagVector);
0121     inputTagsEndRun_ = pset.getUntrackedParameter<std::vector<edm::InputTag>>("inputTagsEndRun", emptyTagVector);
0122     inputTagsBeginProcessBlock_ =
0123         pset.getUntrackedParameter<std::vector<edm::InputTag>>("inputTagsBeginProcessBlock", emptyTagVector);
0124     inputTagsInputProcessBlock_ =
0125         pset.getUntrackedParameter<std::vector<edm::InputTag>>("inputTagsInputProcessBlock", emptyTagVector);
0126     inputTagsEndProcessBlock_ =
0127         pset.getUntrackedParameter<std::vector<edm::InputTag>>("inputTagsEndProcessBlock", emptyTagVector);
0128     inputTagsEndProcessBlock2_ =
0129         pset.getUntrackedParameter<std::vector<edm::InputTag>>("inputTagsEndProcessBlock2", emptyTagVector);
0130     inputTagsEndProcessBlock3_ =
0131         pset.getUntrackedParameter<std::vector<edm::InputTag>>("inputTagsEndProcessBlock3", emptyTagVector);
0132     inputTagsEndProcessBlock4_ =
0133         pset.getUntrackedParameter<std::vector<edm::InputTag>>("inputTagsEndProcessBlock4", emptyTagVector);
0134 
0135     for (auto const& tag : inputTags_) {
0136       tokens_.push_back(consumes<IntProduct>(tag));
0137     }
0138     for (auto const& tag : inputTagsNotFound_) {
0139       tokensNotFound_.push_back(consumes<IntProduct>(tag));
0140     }
0141     for (auto const& tag : inputTagsView_) {
0142       tokensView_.push_back(consumes<edm::View<int>>(tag));
0143     }
0144     for (auto const& tag : inputTagsUInt64_) {
0145       tokensUInt64_.push_back(consumes<UInt64Product>(tag));
0146     }
0147     for (auto const& tag : inputTagsEndLumi_) {
0148       tokensEndLumi_.push_back(consumes<IntProduct, edm::InLumi>(tag));
0149     }
0150     for (auto const& tag : inputTagsEndRun_) {
0151       tokensEndRun_.push_back(consumes<IntProduct, edm::InRun>(tag));
0152     }
0153     for (auto const& tag : inputTagsBeginProcessBlock_) {
0154       tokensBeginProcessBlock_.push_back(consumes<IntProduct, edm::InProcess>(tag));
0155     }
0156     for (auto const& tag : inputTagsInputProcessBlock_) {
0157       tokensInputProcessBlock_.push_back(consumes<IntProduct, edm::InProcess>(tag));
0158     }
0159     for (auto const& tag : inputTagsEndProcessBlock_) {
0160       tokensEndProcessBlock_.push_back(consumes<IntProduct, edm::InProcess>(tag));
0161     }
0162     for (auto const& tag : inputTagsEndProcessBlock2_) {
0163       tokensEndProcessBlock2_.push_back(consumes<IntProduct, edm::InProcess>(tag));
0164     }
0165     for (auto const& tag : inputTagsEndProcessBlock3_) {
0166       tokensEndProcessBlock3_.push_back(consumes<IntProduct, edm::InProcess>(tag));
0167     }
0168     for (auto const& tag : inputTagsEndProcessBlock4_) {
0169       tokensEndProcessBlock4_.push_back(consumes<IntProduct, edm::InProcess>(tag));
0170     }
0171 
0172     if (!tokensInputProcessBlock_.empty()) {
0173       registerProcessBlockCacheFiller<int>(
0174           tokensInputProcessBlock_[0],
0175           [this](edm::ProcessBlock const& processBlock, std::shared_ptr<int> const& previousCache) {
0176             auto returnValue = std::make_shared<int>(0);
0177             for (auto const& token : tokensInputProcessBlock_) {
0178               *returnValue += processBlock.get(token).value;
0179             }
0180             return returnValue;
0181           });
0182       registerProcessBlockCacheFiller<1>(
0183           tokensInputProcessBlock_[0],
0184           [this](edm::ProcessBlock const& processBlock, std::shared_ptr<long long int> const& previousCache) {
0185             auto returnValue = std::make_shared<long long int>(0);
0186             for (auto const& token : tokensInputProcessBlock_) {
0187               *returnValue += processBlock.get(token).value;
0188             }
0189             return returnValue;
0190           });
0191     }
0192   }
0193 
0194   TestFindProduct::~TestFindProduct() {}
0195 
0196   void TestFindProduct::analyze(edm::Event const& event, edm::EventSetup const&) {
0197     edm::Handle<IntProduct> h;
0198     edm::Handle<IntProduct> hToken;
0199     edm::Handle<edm::View<int>> hView;
0200     edm::Handle<edm::View<int>> hViewToken;
0201 
0202     std::vector<edm::EDGetTokenT<IntProduct>>::const_iterator iToken = tokens_.begin();
0203     for (std::vector<edm::InputTag>::const_iterator iter = inputTags_.begin(), iEnd = inputTags_.end(); iter != iEnd;
0204          ++iter, ++iToken) {
0205       if (getByTokenFirst_) {
0206         event.getByToken(*iToken, hToken);
0207         *hToken;
0208       }
0209 
0210       event.getByLabel(*iter, h);
0211       sum_ += h->value;
0212 
0213       event.getByToken(*iToken, hToken);
0214       if (h->value != hToken->value) {
0215         throw cms::Exception("TestFail")
0216             << "TestFindProduct::analyze getByLabel and getByToken return inconsistent results";
0217       }
0218 
0219       if (runProducerParameterCheck_) {
0220         edm::ParameterSet const* producerPset =
0221             edm::getProducerParameterSet(*hToken.provenance(), event.processHistory());
0222         int par = producerPset->getParameter<int>("ivalue");
0223         // These expected values are just from knowing the values in the
0224         // configuration files for this test.
0225         int expectedParameterValue = 3;
0226         if (!iter->process().empty()) {
0227           if (event.run() == 1) {
0228             expectedParameterValue = 1;
0229           } else {
0230             expectedParameterValue = 2;
0231           }
0232         }
0233         if (par != expectedParameterValue) {
0234           throw cms::Exception("TestFail") << "TestFindProduct::analyze unexpected value from producer parameter set";
0235         }
0236       }
0237     }
0238     iToken = tokensNotFound_.begin();
0239     for (std::vector<edm::InputTag>::const_iterator iter = inputTagsNotFound_.begin(), iEnd = inputTagsNotFound_.end();
0240          iter != iEnd;
0241          ++iter, ++iToken) {
0242       event.getByLabel(*iter, h);
0243       if (h.isValid()) {
0244         throw cms::Exception("TestFail")
0245             << "TestFindProduct::analyze: getByLabel found a product that should not be found "
0246             << h.provenance()->moduleLabel();
0247       }
0248 
0249       event.getByToken(*iToken, hToken);
0250       if (hToken.isValid()) {
0251         throw cms::Exception("TestFail")
0252             << "TestFindProduct::analyze: getByToken found a product that should not be found "
0253             << hToken.provenance()->moduleLabel();
0254       }
0255     }
0256     std::vector<edm::EDGetTokenT<edm::View<int>>>::const_iterator iTokenView = tokensView_.begin();
0257     for (std::vector<edm::InputTag>::const_iterator iter = inputTagsView_.begin(), iEnd = inputTagsView_.end();
0258          iter != iEnd;
0259          ++iter, ++iTokenView) {
0260       if (getByTokenFirst_) {
0261         event.getByToken(*iTokenView, hViewToken);
0262         *hViewToken;
0263       }
0264 
0265       event.getByLabel(*iter, hView);
0266       sum_ += hView->at(0);
0267 
0268       event.getByToken(*iTokenView, hViewToken);
0269       if (hView->at(0) != hViewToken->at(0)) {
0270         throw cms::Exception("TestFail")
0271             << "TestFindProduct::analyze getByLabel and getByToken return inconsistent results";
0272       }
0273     }
0274 
0275     // Get these also and add them into the sum
0276     edm::Handle<UInt64Product> h64;
0277     for (auto const& token : tokensUInt64_) {
0278       event.getByToken(token, h64);
0279       sum_ += h64->value;
0280     }
0281 
0282     if (expectedCache_ != 0) {
0283       std::tuple<edm::CacheHandle<int>, edm::CacheHandle<long long int>> valueTuple = processBlockCaches(event);
0284       {
0285         edm::CacheHandle<int> value = std::get<0>(valueTuple);
0286         if (*value != expectedCache_) {
0287           throw cms::Exception("TestFail") << "TestFindProduct::analyze 0 ProcessBlock cache has unexpected value "
0288                                            << *value << " expected = " << expectedCache_;
0289         }
0290       }
0291       {
0292         edm::CacheHandle<long long int> value = std::get<1>(valueTuple);
0293         if (*value != expectedCache_) {
0294           throw cms::Exception("TestFail") << "TestFindProduct::analyze 1 ProcessBlock cache has unexpected value "
0295                                            << *value << " expected = " << expectedCache_;
0296         }
0297       }
0298     }
0299   }
0300 
0301   void TestFindProduct::beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) {}
0302 
0303   void TestFindProduct::endLuminosityBlock(edm::LuminosityBlock const& lb, edm::EventSetup const&) {
0304     // Get these also and add them into the sum
0305     edm::Handle<IntProduct> h;
0306     for (auto const& token : tokensEndLumi_) {
0307       lb.getByToken(token, h);
0308       sum_ += h->value;
0309     }
0310   }
0311 
0312   void TestFindProduct::beginRun(edm::Run const&, edm::EventSetup const&) {}
0313 
0314   void TestFindProduct::endRun(edm::Run const& run, edm::EventSetup const&) {
0315     // Get these also and add them into the sum
0316     edm::Handle<IntProduct> h;
0317     for (auto const& token : tokensEndRun_) {
0318       run.getByToken(token, h);
0319       sum_ += h->value;
0320     }
0321   }
0322 
0323   void TestFindProduct::beginProcessBlock(edm::ProcessBlock const& processBlock) {
0324     for (auto const& token : tokensBeginProcessBlock_) {
0325       sum_ += processBlock.get(token).value;
0326     }
0327     if (testGetterOfProducts_) {
0328       std::vector<edm::Handle<IntProduct>> handles;
0329       getterOfProducts_.fillHandles(processBlock, handles);
0330       for (auto const& intHandle : handles) {
0331         sum_ += intHandle->value;
0332       }
0333     }
0334   }
0335 
0336   void TestFindProduct::accessInputProcessBlock(edm::ProcessBlock const& processBlock) {
0337     for (auto const& token : tokensInputProcessBlock_) {
0338       int value = processBlock.get(token).value;
0339       sum_ += value;
0340     }
0341   }
0342 
0343   void TestFindProduct::endProcessBlock(edm::ProcessBlock const& processBlock) {
0344     std::vector<int> values;
0345     for (auto const& token : tokensEndProcessBlock_) {
0346       int value = processBlock.get(token).value;
0347       values.push_back(value);
0348       sum_ += value;
0349     }
0350     edm::Handle<IntProduct> h;
0351     unsigned int i = 0;
0352     for (auto val : values) {
0353       if (i < tokensEndProcessBlock2_.size()) {
0354         processBlock.getByToken(tokensEndProcessBlock2_[i], h);
0355         if (h->value != val + 2) {
0356           throw cms::Exception("TestFail") << "TestFindProduct::endProcessBlock 2, received unexpected value";
0357         }
0358       }
0359       if (i < tokensEndProcessBlock3_.size()) {
0360         processBlock.getByToken(tokensEndProcessBlock3_[i], h);
0361         if (h->value != val + 3) {
0362           throw cms::Exception("TestFail") << "TestFindProduct::endProcessBlock 3, received unexpected value";
0363         }
0364       }
0365       if (i < tokensEndProcessBlock4_.size()) {
0366         h = processBlock.getHandle(tokensEndProcessBlock4_[i]);
0367         if (h->value != val + 4) {
0368           throw cms::Exception("TestFail") << "TestFindProduct::endProcessBlock 4, received unexpected value";
0369         }
0370       }
0371       ++i;
0372     }
0373     if (testGetterOfProducts_) {
0374       std::vector<edm::Handle<IntProduct>> handles;
0375       getterOfProducts_.fillHandles(processBlock, handles);
0376       for (auto const& intHandle : handles) {
0377         sum_ += intHandle->value;
0378       }
0379     }
0380   }
0381 
0382   void TestFindProduct::endJob() {
0383     std::cout << "TestFindProduct sum = " << sum_ << std::endl;
0384     if (expectedSum_ != 0 && sum_ != expectedSum_) {
0385       throw cms::Exception("TestFail")
0386           << "TestFindProduct::endJob - Sum of test object values does not equal expected value";
0387     }
0388   }
0389 
0390 }  // namespace edmtest
0391 
0392 using edmtest::TestFindProduct;
0393 DEFINE_FWK_MODULE(TestFindProduct);