Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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