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/Provenance/interface/EventAuxiliary.h"
0007 #include "DataFormats/Provenance/interface/LuminosityBlockAuxiliary.h"
0008 #include "DataFormats/Provenance/interface/ModuleDescription.h"
0009 #include "DataFormats/Provenance/interface/RunAuxiliary.h"
0010 #include "DataFormats/Provenance/interface/ProcessConfiguration.h"
0011 #include "DataFormats/Provenance/interface/ProductRegistry.h"
0012 #include "DataFormats/Provenance/interface/BranchDescription.h"
0013 #include "DataFormats/Provenance/interface/BranchIDListHelper.h"
0014 #include "DataFormats/Provenance/interface/ThinnedAssociationsHelper.h"
0015 #include "DataFormats/Provenance/interface/Timestamp.h"
0016 #include "DataFormats/TestObjects/interface/ToyProducts.h"
0017 
0018 #include "FWCore/Framework/interface/EventPrincipal.h"
0019 #include "FWCore/Framework/interface/HistoryAppender.h"
0020 #include "FWCore/Framework/interface/LuminosityBlockPrincipal.h"
0021 #include "FWCore/Framework/interface/ProducesCollector.h"
0022 #include "FWCore/Framework/interface/RunPrincipal.h"
0023 #include "FWCore/Framework/interface/ProducerBase.h"
0024 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0025 #include "FWCore/Utilities/interface/EDMException.h"
0026 #include "FWCore/Utilities/interface/GetPassID.h"
0027 #include "FWCore/Utilities/interface/GlobalIdentifier.h"
0028 #include "FWCore/Reflection/interface/TypeWithDict.h"
0029 #include "FWCore/Version/interface/GetReleaseVersion.h"
0030 
0031 #include "cppunit/extensions/HelperMacros.h"
0032 
0033 #include <cassert>
0034 #include <iostream>
0035 #include <memory>
0036 #include <stdexcept>
0037 #include <string>
0038 #include <typeinfo>
0039 
0040 #include "FWCore/Framework/interface/Event.h"
0041 
0042 class testEventGetRefBeforePut : public CppUnit::TestFixture {
0043   CPPUNIT_TEST_SUITE(testEventGetRefBeforePut);
0044   CPPUNIT_TEST(failGetProductNotRegisteredTest);
0045   CPPUNIT_TEST(getRefTest);
0046   CPPUNIT_TEST_SUITE_END();
0047 
0048 public:
0049   void setUp() {}
0050   void tearDown() {}
0051   void failGetProductNotRegisteredTest();
0052   void getRefTest();
0053 
0054 private:
0055   edm::HistoryAppender historyAppender_;
0056 };
0057 
0058 ///registration of the test so that the runner can find it
0059 CPPUNIT_TEST_SUITE_REGISTRATION(testEventGetRefBeforePut);
0060 
0061 namespace {
0062   class TestProducer : public edm::ProducerBase {
0063   public:
0064     TestProducer(std::string const& productInstanceName) { produces<edmtest::IntProduct>(productInstanceName); }
0065   };
0066 }  // namespace
0067 
0068 void testEventGetRefBeforePut::failGetProductNotRegisteredTest() {
0069   auto preg = std::make_unique<edm::ProductRegistry>();
0070   preg->setFrozen();
0071   auto branchIDListHelper = std::make_shared<edm::BranchIDListHelper>();
0072   branchIDListHelper->updateFromRegistry(*preg);
0073   auto thinnedAssociationsHelper = std::make_shared<edm::ThinnedAssociationsHelper>();
0074   edm::EventID col(1L, 1L, 1L);
0075   std::string uuid = edm::createGlobalIdentifier();
0076   edm::Timestamp fakeTime;
0077   edm::ProcessConfiguration pc("PROD", edm::ParameterSetID(), edm::getReleaseVersion(), edm::getPassID());
0078   std::shared_ptr<edm::ProductRegistry const> pregc(preg.release());
0079   auto rp = std::make_shared<edm::RunPrincipal>(pregc, pc, &historyAppender_, 0);
0080   rp->setAux(edm::RunAuxiliary(col.run(), fakeTime, fakeTime));
0081   edm::LuminosityBlockAuxiliary lumiAux(rp->run(), 1, fakeTime, fakeTime);
0082   auto lbp = std::make_shared<edm::LuminosityBlockPrincipal>(pregc, pc, &historyAppender_, 0);
0083   lbp->setAux(lumiAux);
0084   lbp->setRunPrincipal(rp);
0085   edm::EventAuxiliary eventAux(col, uuid, fakeTime, true);
0086   edm::EventPrincipal ep(
0087       pregc, branchIDListHelper, thinnedAssociationsHelper, pc, &historyAppender_, edm::StreamID::invalidStreamID());
0088   ep.fillEventPrincipal(eventAux, nullptr);
0089   ep.setLuminosityBlockPrincipal(lbp.get());
0090   try {
0091     edm::ParameterSet pset;
0092     pset.registerIt();
0093     auto processConfiguration = std::make_shared<edm::ProcessConfiguration>();
0094     edm::ModuleDescription modDesc(
0095         pset.id(), "Blah", "blahs", processConfiguration.get(), edm::ModuleDescription::getUniqueID());
0096     edm::Event event(ep, modDesc, nullptr);
0097     edm::ProducerBase prod;
0098     event.setProducer(&prod, nullptr);
0099 
0100     std::string label("this does not exist");
0101     edm::RefProd<edmtest::DummyProduct> ref = event.getRefBeforePut<edmtest::DummyProduct>(label);
0102     CPPUNIT_ASSERT("Failed to throw required exception" == 0);
0103   } catch (edm::Exception& x) {
0104     // nothing to do
0105   } catch (...) {
0106     CPPUNIT_ASSERT("Threw wrong kind of exception" == 0);
0107   }
0108 
0109   try {
0110     edm::ParameterSet pset;
0111     pset.registerIt();
0112     auto processConfiguration = std::make_shared<edm::ProcessConfiguration>();
0113     edm::ModuleDescription modDesc(
0114         pset.id(), "Blah", "blahs", processConfiguration.get(), edm::ModuleDescription::getUniqueID());
0115     edm::Event event(ep, modDesc, nullptr);
0116     edm::ProducerBase prod;
0117     event.setProducer(&prod, nullptr);
0118 
0119     std::string label("this does not exist");
0120     edm::RefProd<edmtest::DummyProduct> ref =
0121         event.getRefBeforePut<edmtest::DummyProduct>(edm::EDPutTokenT<edmtest::DummyProduct>{});
0122     CPPUNIT_ASSERT("Failed to throw required exception" == 0);
0123   } catch (edm::Exception& x) {
0124     // nothing to do
0125   } catch (...) {
0126     CPPUNIT_ASSERT("Threw wrong kind of exception" == 0);
0127   }
0128 }
0129 
0130 void testEventGetRefBeforePut::getRefTest() {
0131   std::string processName = "PROD";
0132 
0133   std::string label("fred");
0134   std::string productInstanceName("Rick");
0135 
0136   edmtest::IntProduct dp;
0137   edm::TypeWithDict dummytype(typeid(edmtest::IntProduct));
0138   std::string className = dummytype.friendlyClassName();
0139 
0140   edm::ParameterSet dummyProcessPset;
0141   dummyProcessPset.registerIt();
0142   auto processConfiguration = std::make_shared<edm::ProcessConfiguration>();
0143   processConfiguration->setParameterSetID(dummyProcessPset.id());
0144 
0145   edm::ParameterSet pset;
0146   pset.registerIt();
0147 
0148   edm::BranchDescription product(edm::InEvent,
0149                                  label,
0150                                  processName,
0151                                  dummytype.userClassName(),
0152                                  className,
0153                                  productInstanceName,
0154                                  "",
0155                                  pset.id(),
0156                                  dummytype);
0157 
0158   product.init();
0159 
0160   auto preg = std::make_unique<edm::ProductRegistry>();
0161   preg->addProduct(product);
0162   preg->setFrozen();
0163   auto branchIDListHelper = std::make_shared<edm::BranchIDListHelper>();
0164   branchIDListHelper->updateFromRegistry(*preg);
0165   auto thinnedAssociationsHelper = std::make_shared<edm::ThinnedAssociationsHelper>();
0166   edm::EventID col(1L, 1L, 1L);
0167   std::string uuid = edm::createGlobalIdentifier();
0168   edm::Timestamp fakeTime;
0169   auto pcPtr = std::make_shared<edm::ProcessConfiguration>(
0170       processName, dummyProcessPset.id(), edm::getReleaseVersion(), edm::getPassID());
0171   edm::ProcessConfiguration& pc = *pcPtr;
0172   std::shared_ptr<edm::ProductRegistry const> pregc(preg.release());
0173   auto rp = std::make_shared<edm::RunPrincipal>(pregc, pc, &historyAppender_, 0);
0174   rp->setAux(edm::RunAuxiliary(col.run(), fakeTime, fakeTime));
0175   edm::LuminosityBlockAuxiliary lumiAux(rp->run(), 1, fakeTime, fakeTime);
0176   auto lbp = std::make_shared<edm::LuminosityBlockPrincipal>(pregc, pc, &historyAppender_, 0);
0177   lbp->setAux(lumiAux);
0178   lbp->setRunPrincipal(rp);
0179   edm::EventAuxiliary eventAux(col, uuid, fakeTime, true);
0180   edm::EventPrincipal ep(
0181       pregc, branchIDListHelper, thinnedAssociationsHelper, pc, &historyAppender_, edm::StreamID::invalidStreamID());
0182   ep.fillEventPrincipal(eventAux, nullptr);
0183   ep.setLuminosityBlockPrincipal(lbp.get());
0184 
0185   edm::RefProd<edmtest::IntProduct> refToProd;
0186   try {
0187     edm::ModuleDescription modDesc("Blah", label, pcPtr.get());
0188 
0189     edm::Event event(ep, modDesc, nullptr);
0190     TestProducer prod(productInstanceName);
0191     const_cast<std::vector<edm::ProductResolverIndex>&>(prod.putTokenIndexToProductResolverIndex()).push_back(0);
0192     event.setProducer(&prod, nullptr);
0193     auto pr = std::make_unique<edmtest::IntProduct>();
0194     pr->value = 10;
0195 
0196     refToProd = event.getRefBeforePut<edmtest::IntProduct>(productInstanceName);
0197     event.put(std::move(pr), productInstanceName);
0198     event.commit_(std::vector<edm::ProductResolverIndex>());
0199   } catch (cms::Exception& x) {
0200     std::cerr << x.explainSelf() << std::endl;
0201     CPPUNIT_ASSERT("Threw exception unexpectedly" == 0);
0202   } catch (std::exception& x) {
0203     std::cerr << x.what() << std::endl;
0204     CPPUNIT_ASSERT("threw std::exception" == 0);
0205   } catch (...) {
0206     std::cerr << "Unknown exception type\n";
0207     CPPUNIT_ASSERT("Threw exception unexpectedly" == 0);
0208   }
0209   CPPUNIT_ASSERT(refToProd->value == 10);
0210 }