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