Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 /*----------------------------------------------------------------------
0002 
0003 Test of GenericHandle class.
0004 
0005 ----------------------------------------------------------------------*/
0006 #include "DataFormats/Common/interface/Wrapper.h"
0007 #include "DataFormats/Provenance/interface/BranchIDListHelper.h"
0008 #include "DataFormats/Provenance/interface/EventAuxiliary.h"
0009 #include "DataFormats/Provenance/interface/LuminosityBlockAuxiliary.h"
0010 #include "DataFormats/Provenance/interface/ModuleDescription.h"
0011 #include "DataFormats/Provenance/interface/ProductRegistry.h"
0012 #include "DataFormats/Provenance/interface/RunAuxiliary.h"
0013 #include "DataFormats/Provenance/interface/ThinnedAssociationsHelper.h"
0014 #include "DataFormats/Provenance/interface/Timestamp.h"
0015 #include "DataFormats/TestObjects/interface/ToyProducts.h"
0016 
0017 #include "FWCore/Framework/interface/Event.h"
0018 #include "FWCore/Framework/interface/EventPrincipal.h"
0019 #include "FWCore/Framework/interface/GenericHandle.h"
0020 #include "FWCore/Framework/interface/HistoryAppender.h"
0021 #include "FWCore/Framework/interface/LuminosityBlockPrincipal.h"
0022 #include "FWCore/Framework/interface/RunPrincipal.h"
0023 #include "FWCore/Framework/interface/ProductResolversFactory.h"
0024 #include "FWCore/Framework/interface/SignallingProductRegistryFiller.h"
0025 
0026 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0027 #include "FWCore/Utilities/interface/GlobalIdentifier.h"
0028 #include "FWCore/Reflection/interface/TypeWithDict.h"
0029 
0030 #include "cppunit/extensions/HelperMacros.h"
0031 
0032 #include <iostream>
0033 #include <memory>
0034 #include <string>
0035 
0036 #include "makeDummyProcessConfiguration.h"
0037 
0038 // This is a gross hack, to allow us to test the event
0039 namespace edm {
0040   class ProducerBase {
0041   public:
0042     static void commitEvent(Event& e) { e.commit_(std::vector<ProductResolverIndex>()); }
0043   };
0044 }  // namespace edm
0045 
0046 class testGenericHandle : public CppUnit::TestFixture {
0047   CPPUNIT_TEST_SUITE(testGenericHandle);
0048   CPPUNIT_TEST(failgetbyLabelTest);
0049   CPPUNIT_TEST(getbyLabelTest);
0050   CPPUNIT_TEST(failWrongType);
0051   CPPUNIT_TEST_SUITE_END();
0052 
0053 public:
0054   void setUp() {}
0055   void tearDown() {}
0056   void failgetbyLabelTest();
0057   void failWrongType();
0058   void getbyLabelTest();
0059 
0060   edm::HistoryAppender historyAppender_;
0061 };
0062 
0063 ///registration of the test so that the runner can find it
0064 CPPUNIT_TEST_SUITE_REGISTRATION(testGenericHandle);
0065 
0066 void testGenericHandle::failWrongType() {
0067   try {
0068     //intentionally misspelled type
0069     edm::GenericHandle h("edmtest::DmmyProduct");
0070     CPPUNIT_ASSERT("Failed to throw" == nullptr);
0071   } catch (cms::Exception& x) {
0072     // nothing to do
0073   } catch (...) {
0074     CPPUNIT_ASSERT("Threw wrong kind of exception" == nullptr);
0075   }
0076 }
0077 void testGenericHandle::failgetbyLabelTest() {
0078   edm::EventID id = edm::EventID::firstValidEvent();
0079   edm::Timestamp time;
0080   std::string uuid = edm::createGlobalIdentifier();
0081   auto pc = edmtest::makeDummyProcessConfiguration("PROD");
0082   auto preg = std::make_shared<edm::ProductRegistry>();
0083   preg->setFrozen();
0084   auto rp =
0085       std::make_shared<edm::RunPrincipal>(preg, edm::productResolversFactory::makePrimary, pc, &historyAppender_, 0);
0086   rp->setAux(edm::RunAuxiliary(id.run(), time, time));
0087   auto lbp = std::make_shared<edm::LuminosityBlockPrincipal>(
0088       preg, edm::productResolversFactory::makePrimary, pc, &historyAppender_, 0);
0089   lbp->setAux(edm::LuminosityBlockAuxiliary(rp->run(), 1, time, time));
0090   lbp->setRunPrincipal(rp);
0091   auto branchIDListHelper = std::make_shared<edm::BranchIDListHelper>();
0092   branchIDListHelper->updateFromRegistry(*preg);
0093   auto thinnedAssociationsHelper = std::make_shared<edm::ThinnedAssociationsHelper>();
0094   edm::EventAuxiliary eventAux(id, uuid, time, true);
0095   edm::EventPrincipal ep(preg,
0096                          edm::productResolversFactory::makePrimary,
0097                          branchIDListHelper,
0098                          thinnedAssociationsHelper,
0099                          pc,
0100                          &historyAppender_,
0101                          edm::StreamID::invalidStreamID());
0102   ep.fillEventPrincipal(eventAux, nullptr);
0103   ep.setLuminosityBlockPrincipal(lbp.get());
0104   edm::GenericHandle h("edmtest::DummyProduct");
0105   bool didThrow = true;
0106   try {
0107     edm::ParameterSet pset;
0108     pset.registerIt();
0109     edm::ModuleDescription modDesc(pset.id(), "Blah", "blahs");
0110     edm::Event event(ep, modDesc, nullptr);
0111 
0112     std::string label("this does not exist");
0113     event.getByLabel(label, h);
0114     *h;
0115     didThrow = false;
0116   } catch (cms::Exception& x) {
0117     // nothing to do
0118   } catch (std::exception& x) {
0119     std::cout << "caught std exception " << x.what() << std::endl;
0120     CPPUNIT_ASSERT("Threw std::exception!" == nullptr);
0121   } catch (...) {
0122     CPPUNIT_ASSERT("Threw wrong kind of exception" == nullptr);
0123   }
0124   if (!didThrow) {
0125     CPPUNIT_ASSERT("Failed to throw required exception" == nullptr);
0126   }
0127 }
0128 
0129 void testGenericHandle::getbyLabelTest() {
0130   std::string processName = "PROD";
0131 
0132   typedef edmtest::DummyProduct DP;
0133   typedef edm::Wrapper<DP> WDP;
0134 
0135   auto pr = std::make_unique<DP>();
0136   std::unique_ptr<edm::WrapperBase> pprod = std::make_unique<WDP>(std::move(pr));
0137   std::string label("fred");
0138   std::string productInstanceName("Rick");
0139 
0140   edm::TypeWithDict dummytype(typeid(edmtest::DummyProduct));
0141   std::string className = dummytype.friendlyClassName();
0142 
0143   edm::ParameterSet dummyProcessPset;
0144   dummyProcessPset.registerIt();
0145 
0146   edm::ParameterSet pset;
0147   pset.registerIt();
0148 
0149   edm::ProductDescription product(
0150       edm::InEvent, label, processName, dummytype.userClassName(), className, productInstanceName, dummytype);
0151 
0152   product.init();
0153 
0154   auto preg = std::make_unique<edm::SignallingProductRegistryFiller>();
0155   preg->addProduct(product);
0156   preg->setFrozen();
0157   auto branchIDListHelper = std::make_shared<edm::BranchIDListHelper>();
0158   branchIDListHelper->updateFromRegistry(preg->registry());
0159   auto thinnedAssociationsHelper = std::make_shared<edm::ThinnedAssociationsHelper>();
0160 
0161   edm::ProductRegistry::ProductList const& pl = preg->registry().productList();
0162   edm::BranchKey const bk(product);
0163   edm::ProductRegistry::ProductList::const_iterator it = pl.find(bk);
0164 
0165   edm::EventID col(1L, 1L, 1L);
0166   edm::Timestamp fakeTime;
0167   std::string uuid = edm::createGlobalIdentifier();
0168   auto pc = edmtest::makeDummyProcessConfiguration("PROD", dummyProcessPset.id());
0169   std::shared_ptr<edm::ProductRegistry const> pregc(std::make_shared<edm::ProductRegistry>(preg->moveTo()));
0170   auto rp =
0171       std::make_shared<edm::RunPrincipal>(pregc, edm::productResolversFactory::makePrimary, pc, &historyAppender_, 0);
0172   rp->setAux(edm::RunAuxiliary(col.run(), fakeTime, fakeTime));
0173   auto lbp = std::make_shared<edm::LuminosityBlockPrincipal>(
0174       pregc, edm::productResolversFactory::makePrimary, pc, &historyAppender_, 0);
0175   lbp->setAux(edm::LuminosityBlockAuxiliary(rp->run(), 1, fakeTime, fakeTime));
0176   lbp->setRunPrincipal(rp);
0177   edm::EventAuxiliary eventAux(col, uuid, fakeTime, true);
0178   edm::EventPrincipal ep(pregc,
0179                          edm::productResolversFactory::makePrimary,
0180                          branchIDListHelper,
0181                          thinnedAssociationsHelper,
0182                          pc,
0183                          &historyAppender_,
0184                          edm::StreamID::invalidStreamID());
0185   ep.fillEventPrincipal(eventAux, nullptr);
0186   ep.setLuminosityBlockPrincipal(lbp.get());
0187   edm::ProductDescription const& branchFromRegistry = it->second;
0188   std::vector<edm::BranchID> const ids;
0189   edm::ProductProvenance prov(branchFromRegistry.branchID(), ids);
0190   edm::ProductDescription const desc(branchFromRegistry);
0191   ep.put(desc, std::move(pprod), prov);
0192 
0193   edm::GenericHandle h("edmtest::DummyProduct");
0194   try {
0195     auto processConfiguration = std::make_shared<edm::ProcessConfiguration>();
0196     processConfiguration->setParameterSetID(dummyProcessPset.id());
0197 
0198     edm::ModuleDescription modDesc(
0199         pset.id(), "Blah", "blahs", processConfiguration.get(), edm::ModuleDescription::getUniqueID());
0200     edm::Event event(ep, modDesc, nullptr);
0201 
0202     event.getByLabel(label, productInstanceName, h);
0203   } catch (cms::Exception& x) {
0204     std::cerr << x.explainSelf() << std::endl;
0205     CPPUNIT_ASSERT("Threw cms::Exception unexpectedly" == nullptr);
0206   } catch (std::exception& x) {
0207     std::cerr << x.what() << std::endl;
0208     CPPUNIT_ASSERT("threw std::exception" == nullptr);
0209   } catch (...) {
0210     std::cerr << "Unknown exception type\n";
0211     CPPUNIT_ASSERT("Threw exception unexpectedly" == nullptr);
0212   }
0213   CPPUNIT_ASSERT(h.isValid());
0214   CPPUNIT_ASSERT(h.provenance()->moduleLabel() == label);
0215 }