Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 /*----------------------------------------------------------------------
0002 
0003 Test of the EventPrincipal class.
0004 
0005 ----------------------------------------------------------------------*/
0006 #include "DataFormats/Common/interface/BasicHandle.h"
0007 #include "DataFormats/Common/interface/Wrapper.h"
0008 #include "DataFormats/Provenance/interface/BranchDescription.h"
0009 #include "DataFormats/Provenance/interface/BranchID.h"
0010 #include "DataFormats/Provenance/interface/BranchIDListHelper.h"
0011 #include "DataFormats/Provenance/interface/EventAuxiliary.h"
0012 #include "DataFormats/Provenance/interface/LuminosityBlockAuxiliary.h"
0013 #include "DataFormats/Provenance/interface/ParameterSetID.h"
0014 #include "DataFormats/Provenance/interface/Parentage.h"
0015 #include "DataFormats/Provenance/interface/ProcessConfiguration.h"
0016 #include "DataFormats/Provenance/interface/ProductID.h"
0017 #include "DataFormats/Provenance/interface/ProductRegistry.h"
0018 #include "DataFormats/Provenance/interface/Timestamp.h"
0019 #include "DataFormats/Provenance/interface/ProductProvenance.h"
0020 #include "DataFormats/Provenance/interface/RunAuxiliary.h"
0021 #include "DataFormats/Provenance/interface/ThinnedAssociationsHelper.h"
0022 #include "DataFormats/TestObjects/interface/ToyProducts.h"
0023 #include "FWCore/Framework/interface/EventPrincipal.h"
0024 #include "FWCore/Framework/interface/LuminosityBlockPrincipal.h"
0025 #include "FWCore/Framework/interface/RunPrincipal.h"
0026 #include "FWCore/Framework/interface/HistoryAppender.h"
0027 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0028 #include "FWCore/Utilities/interface/EDMException.h"
0029 #include "FWCore/Utilities/interface/GetPassID.h"
0030 #include "FWCore/Utilities/interface/GlobalIdentifier.h"
0031 #include "FWCore/Utilities/interface/ProductKindOfType.h"
0032 #include "FWCore/Utilities/interface/TypeID.h"
0033 #include "FWCore/Reflection/interface/TypeWithDict.h"
0034 #include "FWCore/Version/interface/GetReleaseVersion.h"
0035 
0036 #include "cppunit/extensions/HelperMacros.h"
0037 
0038 #include <map>
0039 #include <memory>
0040 #include <stdexcept>
0041 #include <string>
0042 #include <typeinfo>
0043 
0044 class test_ep : public CppUnit::TestFixture {
0045   CPPUNIT_TEST_SUITE(test_ep);
0046   CPPUNIT_TEST(failgetbyIdTest);
0047   CPPUNIT_TEST(failgetbyLabelTest);
0048   CPPUNIT_TEST(failgetManybyTypeTest);
0049   CPPUNIT_TEST(failgetbyInvalidIdTest);
0050   CPPUNIT_TEST(failgetProvenanceTest);
0051   CPPUNIT_TEST_SUITE_END();
0052 
0053 public:
0054   void setUp();
0055   void tearDown();
0056   void failgetbyIdTest();
0057   void failgetbyLabelTest();
0058   void failgetManybyTypeTest();
0059   void failgetbyInvalidIdTest();
0060   void failgetProvenanceTest();
0061 
0062 private:
0063   std::shared_ptr<edm::ProcessConfiguration> fake_single_module_process(
0064       std::string const& tag,
0065       std::string const& processName,
0066       edm::ParameterSet const& moduleParams,
0067       std::string const& release = edm::getReleaseVersion(),
0068       std::string const& pass = edm::getPassID());
0069   std::shared_ptr<edm::BranchDescription> fake_single_process_branch(
0070       std::string const& tag, std::string const& processName, std::string const& productInstanceName = std::string());
0071 
0072   std::map<std::string, std::shared_ptr<edm::BranchDescription> > branchDescriptions_;
0073   std::map<std::string, std::shared_ptr<edm::ProcessConfiguration> > processConfigurations_;
0074 
0075   std::shared_ptr<edm::ProductRegistry> pProductRegistry_;
0076   std::shared_ptr<edm::LuminosityBlockPrincipal> lbp_;
0077   std::shared_ptr<edm::EventPrincipal> pEvent_;
0078 
0079   edm::EventID eventID_;
0080 
0081   edm::HistoryAppender historyAppender_;
0082 };
0083 
0084 //----------------------------------------------------------------------
0085 // registration of the test so that the runner can find it
0086 
0087 CPPUNIT_TEST_SUITE_REGISTRATION(test_ep);
0088 
0089 //----------------------------------------------------------------------
0090 
0091 std::shared_ptr<edm::ProcessConfiguration> test_ep::fake_single_module_process(std::string const& tag,
0092                                                                                std::string const& processName,
0093                                                                                edm::ParameterSet const& moduleParams,
0094                                                                                std::string const& release,
0095                                                                                std::string const& pass) {
0096   edm::ParameterSet processParams;
0097   processParams.addParameter(processName, moduleParams);
0098   processParams.addParameter<std::string>("@process_name", processName);
0099 
0100   processParams.registerIt();
0101   auto result = std::make_shared<edm::ProcessConfiguration>(processName, processParams.id(), release, pass);
0102   processConfigurations_[tag] = result;
0103   return result;
0104 }
0105 
0106 std::shared_ptr<edm::BranchDescription> test_ep::fake_single_process_branch(std::string const& tag,
0107                                                                             std::string const& processName,
0108                                                                             std::string const& productInstanceName) {
0109   std::string moduleLabel = processName + "dummyMod";
0110   std::string moduleClass("DummyModule");
0111   edm::TypeWithDict dummyType(typeid(edmtest::DummyProduct));
0112   std::string productClassName = dummyType.userClassName();
0113   std::string friendlyProductClassName = dummyType.friendlyClassName();
0114   edm::ParameterSet modParams;
0115   modParams.addParameter<std::string>("@module_type", moduleClass);
0116   modParams.addParameter<std::string>("@module_label", moduleLabel);
0117   modParams.registerIt();
0118   std::shared_ptr<edm::ProcessConfiguration> process(fake_single_module_process(tag, processName, modParams));
0119 
0120   auto result = std::make_shared<edm::BranchDescription>(edm::InEvent,
0121                                                          moduleLabel,
0122                                                          processName,
0123                                                          productClassName,
0124                                                          friendlyProductClassName,
0125                                                          productInstanceName,
0126                                                          moduleClass,
0127                                                          modParams.id(),
0128                                                          dummyType);
0129   branchDescriptions_[tag] = result;
0130   return result;
0131 }
0132 
0133 void test_ep::setUp() {
0134   // Making a functional EventPrincipal is not trivial, so we do it
0135   // all here.
0136   eventID_ = edm::EventID(101, 1, 20);
0137 
0138   // We can only insert products registered in the ProductRegistry.
0139   pProductRegistry_.reset(new edm::ProductRegistry);
0140   pProductRegistry_->addProduct(*fake_single_process_branch("hlt", "HLT"));
0141   pProductRegistry_->addProduct(*fake_single_process_branch("prod", "PROD"));
0142   pProductRegistry_->addProduct(*fake_single_process_branch("test", "TEST"));
0143   pProductRegistry_->addProduct(*fake_single_process_branch("user", "USER"));
0144   pProductRegistry_->addProduct(*fake_single_process_branch("rick", "USER2", "rick"));
0145   pProductRegistry_->setFrozen();
0146   auto branchIDListHelper = std::make_shared<edm::BranchIDListHelper>();
0147   branchIDListHelper->updateFromRegistry(*pProductRegistry_);
0148   auto thinnedAssociationsHelper = std::make_shared<edm::ThinnedAssociationsHelper>();
0149 
0150   // Put products we'll look for into the EventPrincipal.
0151   {
0152     typedef edmtest::DummyProduct PRODUCT_TYPE;
0153     typedef edm::Wrapper<PRODUCT_TYPE> WDP;
0154 
0155     std::unique_ptr<edm::WrapperBase> product = std::make_unique<WDP>(std::make_unique<PRODUCT_TYPE>());
0156 
0157     std::string tag("rick");
0158     assert(branchDescriptions_[tag]);
0159     edm::BranchDescription branch = *branchDescriptions_[tag];
0160 
0161     branch.init();
0162 
0163     edm::ProductRegistry::ProductList const& pl = pProductRegistry_->productList();
0164     edm::BranchKey const bk(branch);
0165     edm::ProductRegistry::ProductList::const_iterator it = pl.find(bk);
0166 
0167     edm::BranchDescription const branchFromRegistry(it->second);
0168 
0169     std::vector<edm::BranchID> const ids;
0170     edm::ProductProvenance prov(branchFromRegistry.branchID(), ids);
0171 
0172     std::shared_ptr<edm::ProcessConfiguration> process(processConfigurations_[tag]);
0173     assert(process);
0174     std::string uuid = edm::createGlobalIdentifier();
0175     edm::Timestamp now(1234567UL);
0176     auto runAux = std::make_shared<edm::RunAuxiliary>(eventID_.run(), now, now);
0177     auto rp = std::make_shared<edm::RunPrincipal>(runAux, pProductRegistry_, *process, &historyAppender_, 0);
0178     edm::LuminosityBlockAuxiliary lumiAux(rp->run(), 1, now, now);
0179     lbp_ = std::make_shared<edm::LuminosityBlockPrincipal>(pProductRegistry_, *process, &historyAppender_, 0);
0180     lbp_->setAux(lumiAux);
0181     lbp_->setRunPrincipal(rp);
0182     edm::EventAuxiliary eventAux(eventID_, uuid, now, true);
0183     pEvent_.reset(new edm::EventPrincipal(pProductRegistry_,
0184                                           branchIDListHelper,
0185                                           thinnedAssociationsHelper,
0186                                           *process,
0187                                           &historyAppender_,
0188                                           edm::StreamID::invalidStreamID()));
0189     pEvent_->fillEventPrincipal(eventAux, nullptr);
0190     pEvent_->setLuminosityBlockPrincipal(lbp_.get());
0191     pEvent_->put(branchFromRegistry, std::move(product), prov);
0192   }
0193   CPPUNIT_ASSERT(pEvent_->size() == 1);
0194 }
0195 
0196 template <class MAP>
0197 void clear_map(MAP& m) {
0198   for (typename MAP::iterator i = m.begin(), e = m.end(); i != e; ++i)
0199     i->second.reset();
0200 }
0201 
0202 void test_ep::tearDown() {
0203   clear_map(branchDescriptions_);
0204   clear_map(processConfigurations_);
0205 
0206   pEvent_.reset();
0207 
0208   pProductRegistry_.reset();
0209 }
0210 
0211 //----------------------------------------------------------------------
0212 // Test functions
0213 //----------------------------------------------------------------------
0214 
0215 void test_ep::failgetbyIdTest() {
0216   edm::ProductID invalid;
0217   CPPUNIT_ASSERT_THROW(pEvent_->getByProductID(invalid), edm::Exception);
0218 
0219   edm::ProductID notpresent(0, 10000);
0220   edm::BasicHandle h(pEvent_->getByProductID(notpresent));
0221   CPPUNIT_ASSERT(h.failedToGet());
0222 }
0223 
0224 void test_ep::failgetbyLabelTest() {
0225   edmtest::IntProduct dummy;
0226   edm::TypeID tid(dummy);
0227 
0228   std::string label("this does not exist");
0229 
0230   edm::BasicHandle h(
0231       pEvent_->getByLabel(edm::PRODUCT_TYPE, tid, label, std::string(), std::string(), nullptr, nullptr, nullptr));
0232   CPPUNIT_ASSERT(h.failedToGet());
0233 }
0234 
0235 void test_ep::failgetManybyTypeTest() {
0236   edmtest::IntProduct dummy;
0237   edm::TypeID tid(dummy);
0238   std::vector<edm::BasicHandle> handles;
0239 
0240   pEvent_->getManyByType(tid, handles, nullptr, nullptr, nullptr);
0241   CPPUNIT_ASSERT(handles.empty());
0242 }
0243 
0244 void test_ep::failgetbyInvalidIdTest() {
0245   //put_a_dummy_product("HLT");
0246   //put_a_product<edmtest::DummyProduct>(pProdConfig_, label);
0247 
0248   edm::ProductID id;
0249   CPPUNIT_ASSERT_THROW(pEvent_->getByProductID(id), edm::Exception);
0250 }
0251 
0252 void test_ep::failgetProvenanceTest() {
0253   edm::BranchID id;
0254   CPPUNIT_ASSERT_THROW(pEvent_->getProvenance(id), edm::Exception);
0255 }