Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-03-13 02:31:55

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/ProductDescription.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/Framework/interface/ProductResolversFactory.h"
0028 #include "FWCore/Framework/interface/SignallingProductRegistryFiller.h"
0029 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0030 #include "FWCore/Utilities/interface/EDMException.h"
0031 #include "FWCore/Utilities/interface/GlobalIdentifier.h"
0032 #include "FWCore/Utilities/interface/ProductKindOfType.h"
0033 #include "FWCore/Utilities/interface/TypeID.h"
0034 #include "FWCore/Reflection/interface/TypeWithDict.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 #include "makeDummyProcessConfiguration.h"
0045 
0046 class test_ep : public CppUnit::TestFixture {
0047   CPPUNIT_TEST_SUITE(test_ep);
0048   CPPUNIT_TEST(failgetbyIdTest);
0049   CPPUNIT_TEST(failgetbyLabelTest);
0050   CPPUNIT_TEST(failgetbyInvalidIdTest);
0051   CPPUNIT_TEST(failgetProvenanceTest);
0052   CPPUNIT_TEST_SUITE_END();
0053 
0054 public:
0055   void setUp();
0056   void tearDown();
0057   void failgetbyIdTest();
0058   void failgetbyLabelTest();
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::shared_ptr<edm::ProductDescription> fake_single_process_branch(
0069       std::string const& tag, std::string const& processName, std::string const& productInstanceName = std::string());
0070 
0071   std::map<std::string, std::shared_ptr<edm::ProductDescription> > productDescriptions_;
0072   std::map<std::string, std::shared_ptr<edm::ProcessConfiguration> > processConfigurations_;
0073 
0074   std::shared_ptr<edm::SignallingProductRegistryFiller> pProductRegistry_;
0075   std::shared_ptr<edm::LuminosityBlockPrincipal> lbp_;
0076   std::shared_ptr<edm::EventPrincipal> pEvent_;
0077 
0078   edm::EventID eventID_;
0079 
0080   edm::HistoryAppender historyAppender_;
0081 };
0082 
0083 //----------------------------------------------------------------------
0084 // registration of the test so that the runner can find it
0085 
0086 CPPUNIT_TEST_SUITE_REGISTRATION(test_ep);
0087 
0088 //----------------------------------------------------------------------
0089 
0090 std::shared_ptr<edm::ProcessConfiguration> test_ep::fake_single_module_process(std::string const& tag,
0091                                                                                std::string const& processName,
0092                                                                                edm::ParameterSet const& moduleParams,
0093                                                                                std::string const& release) {
0094   edm::ParameterSet processParams;
0095   processParams.addParameter(processName, moduleParams);
0096   processParams.addParameter<std::string>("@process_name", processName);
0097 
0098   processParams.registerIt();
0099   auto result = edmtest::makeSharedDummyProcessConfiguration(processName, processParams.id());
0100   processConfigurations_[tag] = result;
0101   return result;
0102 }
0103 
0104 std::shared_ptr<edm::ProductDescription> 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::ProductDescription>(edm::InEvent,
0119                                                           moduleLabel,
0120                                                           processName,
0121                                                           productClassName,
0122                                                           friendlyProductClassName,
0123                                                           productInstanceName,
0124                                                           dummyType);
0125   productDescriptions_[tag] = result;
0126   return result;
0127 }
0128 
0129 void test_ep::setUp() {
0130   // Making a functional EventPrincipal is not trivial, so we do it
0131   // all here.
0132   eventID_ = edm::EventID(101, 1, 20);
0133 
0134   // We can only insert products registered in the ProductRegistry.
0135   pProductRegistry_.reset(new edm::SignallingProductRegistryFiller);
0136   pProductRegistry_->addProduct(*fake_single_process_branch("hlt", "HLT"));
0137   pProductRegistry_->addProduct(*fake_single_process_branch("prod", "PROD"));
0138   pProductRegistry_->addProduct(*fake_single_process_branch("test", "TEST"));
0139   pProductRegistry_->addProduct(*fake_single_process_branch("user", "USER"));
0140   pProductRegistry_->addProduct(*fake_single_process_branch("rick", "USER2", "rick"));
0141   pProductRegistry_->setFrozen();
0142   auto branchIDListHelper = std::make_shared<edm::BranchIDListHelper>();
0143   branchIDListHelper->updateFromRegistry(pProductRegistry_->registry());
0144   auto thinnedAssociationsHelper = std::make_shared<edm::ThinnedAssociationsHelper>();
0145 
0146   // Put products we'll look for into the EventPrincipal.
0147   {
0148     typedef edmtest::DummyProduct PRODUCT_TYPE;
0149     typedef edm::Wrapper<PRODUCT_TYPE> WDP;
0150 
0151     std::unique_ptr<edm::WrapperBase> product = std::make_unique<WDP>(std::make_unique<PRODUCT_TYPE>());
0152 
0153     std::string tag("rick");
0154     assert(productDescriptions_[tag]);
0155     edm::ProductDescription branch = *productDescriptions_[tag];
0156 
0157     branch.init();
0158 
0159     edm::ProductRegistry::ProductList const& pl = pProductRegistry_->registry().productList();
0160     edm::BranchKey const bk(branch);
0161     edm::ProductRegistry::ProductList::const_iterator it = pl.find(bk);
0162 
0163     edm::ProductDescription const branchFromRegistry(it->second);
0164 
0165     std::vector<edm::BranchID> const ids;
0166     edm::ProductProvenance prov(branchFromRegistry.branchID(), ids);
0167 
0168     std::shared_ptr<edm::ProcessConfiguration> process(processConfigurations_[tag]);
0169     assert(process);
0170     std::string uuid = edm::createGlobalIdentifier();
0171     edm::Timestamp now(1234567UL);
0172     auto pRegistry = std::make_shared<edm::ProductRegistry const>(pProductRegistry_->registry());
0173     auto rp = std::make_shared<edm::RunPrincipal>(
0174         pRegistry, edm::productResolversFactory::makePrimary, *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>(
0178         pRegistry, edm::productResolversFactory::makePrimary, *process, &historyAppender_, 0);
0179     lbp_->setAux(lumiAux);
0180     lbp_->setRunPrincipal(rp);
0181     edm::EventAuxiliary eventAux(eventID_, uuid, now, true);
0182     pEvent_.reset(new edm::EventPrincipal(pRegistry,
0183                                           edm::productResolversFactory::makePrimary,
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(productDescriptions_);
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::failgetbyInvalidIdTest() {
0236   //put_a_dummy_product("HLT");
0237   //put_a_product<edmtest::DummyProduct>(pProdConfig_, label);
0238 
0239   edm::ProductID id;
0240   CPPUNIT_ASSERT_THROW(pEvent_->getByProductID(id), edm::Exception);
0241 }
0242 
0243 void test_ep::failgetProvenanceTest() {
0244   edm::BranchID id;
0245   CPPUNIT_ASSERT_THROW(pEvent_->getProvenance(id), edm::Exception);
0246 }