Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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