File indexing completed on 2025-03-13 02:31:55
0001
0002
0003
0004
0005
0006
0007 #include "DataFormats/Common/interface/Handle.h"
0008 #include "DataFormats/Common/interface/OrphanHandle.h"
0009 #include "DataFormats/Common/interface/Wrapper.h"
0010 #include "DataFormats/Provenance/interface/ProductDescription.h"
0011 #include "DataFormats/Provenance/interface/BranchIDListHelper.h"
0012 #include "DataFormats/Provenance/interface/EventAuxiliary.h"
0013 #include "DataFormats/Provenance/interface/EventID.h"
0014 #include "DataFormats/Provenance/interface/LuminosityBlockAuxiliary.h"
0015 #include "DataFormats/Provenance/interface/ModuleDescription.h"
0016 #include "DataFormats/Provenance/interface/ProcessHistory.h"
0017 #include "DataFormats/Provenance/interface/ProcessHistoryRegistry.h"
0018 #include "DataFormats/Provenance/interface/ProductRegistry.h"
0019 #include "DataFormats/Provenance/interface/RunAuxiliary.h"
0020 #include "DataFormats/Provenance/interface/ThinnedAssociationsHelper.h"
0021 #include "DataFormats/Provenance/interface/Timestamp.h"
0022 #include "DataFormats/Provenance/interface/ProductResolverIndexHelper.h"
0023 #include "DataFormats/TestObjects/interface/Thing.h"
0024 #include "DataFormats/TestObjects/interface/ToyProducts.h"
0025 #include "FWCore/Framework/interface/Event.h"
0026 #include "FWCore/Framework/interface/EventPrincipal.h"
0027 #include "FWCore/Framework/interface/HistoryAppender.h"
0028 #include "FWCore/Framework/interface/LuminosityBlockPrincipal.h"
0029 #include "FWCore/Framework/interface/RunPrincipal.h"
0030 #include "FWCore/Framework/interface/EDConsumerBase.h"
0031 #include "FWCore/Framework/interface/ProducerBase.h"
0032 #include "FWCore/Framework/interface/ProductResolversFactory.h"
0033 #include "FWCore/Framework/interface/SignallingProductRegistryFiller.h"
0034 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0035 #include "FWCore/ServiceRegistry/interface/ModuleCallingContext.h"
0036 #include "FWCore/Utilities/interface/Algorithms.h"
0037 #include "FWCore/Utilities/interface/EDMException.h"
0038 #include "FWCore/Utilities/interface/GlobalIdentifier.h"
0039 #include "FWCore/Utilities/interface/InputTag.h"
0040 #include "FWCore/Utilities/interface/ProductKindOfType.h"
0041 #include "FWCore/Utilities/interface/TypeID.h"
0042 #include "FWCore/Reflection/interface/TypeWithDict.h"
0043 #include "Utilities/Testing/interface/CppUnit_testdriver.icpp"
0044
0045 #include "cppunit/extensions/HelperMacros.h"
0046
0047 #include <algorithm>
0048 #include <fstream>
0049 #include <iterator>
0050 #include <limits>
0051 #include <map>
0052 #include <memory>
0053 #include <string>
0054 #include <typeinfo>
0055 #include <vector>
0056
0057 #include "makeDummyProcessConfiguration.h"
0058
0059 using namespace edm;
0060
0061
0062 namespace {
0063 struct IntConsumer : public EDConsumerBase {
0064 IntConsumer(std::vector<InputTag> const& iTags) {
0065 m_tokens.reserve(iTags.size());
0066 for (auto const& tag : iTags) {
0067 m_tokens.push_back(consumes<int>(tag));
0068 }
0069 }
0070
0071 std::vector<EDGetTokenT<int>> m_tokens;
0072 };
0073
0074 struct IntProductConsumer : public EDConsumerBase {
0075 IntProductConsumer(std::vector<InputTag> const& iTags) {
0076 m_tokens.reserve(iTags.size());
0077 for (auto const& tag : iTags) {
0078 m_tokens.push_back(consumes<edmtest::IntProduct>(tag));
0079 }
0080 }
0081
0082 std::vector<EDGetTokenT<edmtest::IntProduct>> m_tokens;
0083 };
0084
0085 template <typename T>
0086 class TestProducer : public edm::ProducerBase {
0087 public:
0088 TestProducer(std::string const& productInstanceName) { token_ = produces(productInstanceName); }
0089 EDPutTokenT<T> token_;
0090 };
0091 }
0092
0093 class testEvent : public CppUnit::TestFixture {
0094 CPPUNIT_TEST_SUITE(testEvent);
0095 CPPUNIT_TEST(emptyEvent);
0096 CPPUNIT_TEST(getByLabelFromEmpty);
0097 CPPUNIT_TEST(getByTokenFromEmpty);
0098 CPPUNIT_TEST(getHandleFromEmpty);
0099 CPPUNIT_TEST(getFromEmpty);
0100 CPPUNIT_TEST(putAnIntProduct);
0101 CPPUNIT_TEST(putAndGetAnIntProduct);
0102 CPPUNIT_TEST(putAndGetAnIntProductByToken);
0103 CPPUNIT_TEST(emplaceAndGetAnIntProductByToken);
0104 CPPUNIT_TEST(getByProductID);
0105 CPPUNIT_TEST(transaction);
0106 CPPUNIT_TEST(getByLabel);
0107 CPPUNIT_TEST(getByToken);
0108 CPPUNIT_TEST(getHandle);
0109 CPPUNIT_TEST(get_product);
0110 CPPUNIT_TEST(printHistory);
0111 CPPUNIT_TEST(deleteProduct);
0112 CPPUNIT_TEST_SUITE_END();
0113
0114 public:
0115 testEvent();
0116 ~testEvent();
0117 void setUp();
0118 void tearDown();
0119 void emptyEvent();
0120 void getByLabelFromEmpty();
0121 void getByTokenFromEmpty();
0122 void getHandleFromEmpty();
0123 void getFromEmpty();
0124 void putAnIntProduct();
0125 void putAndGetAnIntProduct();
0126 void putAndGetAnIntProductByToken();
0127 void emplaceAndGetAnIntProductByToken();
0128 void getByProductID();
0129 void transaction();
0130 void getByLabel();
0131 void getByToken();
0132 void getHandle();
0133 void get_product();
0134 void printHistory();
0135 void deleteProduct();
0136
0137 private:
0138 template <class T>
0139 void registerProduct(std::string const& tag,
0140 std::string const& moduleLabel,
0141 std::string const& moduleClassName,
0142 std::string const& processName,
0143 std::string const& productInstanceName);
0144
0145 template <class T>
0146 void registerProduct(std::string const& tag,
0147 std::string const& moduleLabel,
0148 std::string const& moduleClassName,
0149 std::string const& processName) {
0150 std::string productInstanceName;
0151 registerProduct<T>(tag, moduleLabel, moduleClassName, processName, productInstanceName);
0152 }
0153
0154 template <class T>
0155 ProductID addProduct(std::unique_ptr<T> product,
0156 std::string const& tag,
0157 std::string const& productLabel = std::string());
0158
0159 template <class T>
0160 std::unique_ptr<ProducerBase> putProduct(std::unique_ptr<T> product,
0161 std::string const& productInstanceLabel,
0162 bool doCommit = true);
0163 template <class T>
0164 std::unique_ptr<ProducerBase> putProductUsingToken(std::unique_ptr<T> product,
0165 std::string const& productInstanceLabel,
0166 bool doCommit = true);
0167 template <class T>
0168 std::unique_ptr<ProducerBase> emplaceProduct(T product,
0169 std::string const& productInstanceLabel,
0170 bool doCommit = true);
0171
0172 std::shared_ptr<SignallingProductRegistryFiller> availableProducts_;
0173 std::shared_ptr<BranchIDListHelper> branchIDListHelper_;
0174 std::shared_ptr<ThinnedAssociationsHelper> thinnedAssociationsHelper_;
0175 std::shared_ptr<edm::LuminosityBlockPrincipal> lbp_;
0176 std::shared_ptr<EventPrincipal> principal_;
0177 std::shared_ptr<Event> currentEvent_;
0178 std::shared_ptr<ModuleDescription> currentModuleDescription_;
0179 typedef std::map<std::string, ModuleDescription> modCache_t;
0180 typedef modCache_t::iterator iterator_t;
0181
0182 modCache_t moduleDescriptions_;
0183 ProcessHistoryRegistry processHistoryRegistry_;
0184 std::vector<std::shared_ptr<ProcessConfiguration>> processConfigurations_;
0185 HistoryAppender historyAppender_;
0186 };
0187
0188
0189 CPPUNIT_TEST_SUITE_REGISTRATION(testEvent);
0190
0191 EventID make_id() { return EventID(2112, 1, 25); }
0192 Timestamp make_timestamp() { return Timestamp(1); }
0193
0194 template <class T>
0195 void testEvent::registerProduct(std::string const& tag,
0196 std::string const& moduleLabel,
0197 std::string const& moduleClassName,
0198 std::string const& processName,
0199 std::string const& productInstanceName) {
0200 if (!availableProducts_)
0201 availableProducts_.reset(new SignallingProductRegistryFiller());
0202
0203 ParameterSet moduleParams;
0204 moduleParams.template addParameter<std::string>("@module_type", moduleClassName);
0205 moduleParams.template addParameter<std::string>("@module_label", moduleLabel);
0206 moduleParams.registerIt();
0207
0208 ParameterSet processParams;
0209 processParams.template addParameter<std::string>("@process_name", processName);
0210 processParams.template addParameter<ParameterSet>(moduleLabel, moduleParams);
0211 processParams.registerIt();
0212
0213 auto process = edmtest::makeDummyProcessConfiguration(processName, processParams.id());
0214
0215 auto processX = std::make_shared<ProcessConfiguration>(process);
0216 processConfigurations_.push_back(processX);
0217
0218 TypeWithDict product_type(typeid(T));
0219
0220 ProductDescription branch(InEvent,
0221 moduleLabel,
0222 processName,
0223 product_type.userClassName(),
0224 product_type.friendlyClassName(),
0225 productInstanceName,
0226 product_type);
0227
0228 moduleDescriptions_[tag] = ModuleDescription(
0229 moduleParams.id(), moduleClassName, moduleLabel, processX.get(), ModuleDescription::getUniqueID());
0230 availableProducts_->addProduct(branch);
0231 }
0232
0233
0234
0235 template <class T>
0236 ProductID testEvent::addProduct(std::unique_ptr<T> product, std::string const& tag, std::string const& productLabel) {
0237 iterator_t description = moduleDescriptions_.find(tag);
0238 if (description == moduleDescriptions_.end())
0239 throw edm::Exception(errors::LogicError) << "Failed to find a module description for tag: " << tag << '\n';
0240
0241 ModuleCallingContext mcc(&description->second);
0242 Event temporaryEvent(*principal_, description->second, &mcc);
0243 TestProducer<T> prod(productLabel);
0244 const_cast<std::vector<edm::ProductResolverIndex>&>(prod.putTokenIndexToProductResolverIndex())
0245 .push_back(principal_->productLookup().index(PRODUCT_TYPE,
0246 edm::TypeID(typeid(T)),
0247 description->second.moduleLabel().c_str(),
0248 productLabel.c_str(),
0249 description->second.processName().c_str()));
0250
0251 temporaryEvent.setProducer(&prod, nullptr);
0252 OrphanHandle<T> h = temporaryEvent.put(std::move(product), productLabel);
0253 ProductID id = h.id();
0254 temporaryEvent.commit_(std::vector<ProductResolverIndex>());
0255 return id;
0256 }
0257
0258 template <class T>
0259 std::unique_ptr<ProducerBase> testEvent::putProduct(std::unique_ptr<T> product,
0260 std::string const& productInstanceLabel,
0261 bool doCommit) {
0262 auto prod = std::make_unique<TestProducer<edmtest::IntProduct>>(productInstanceLabel);
0263 auto index = principal_->productLookup().index(PRODUCT_TYPE,
0264 edm::TypeID(typeid(T)),
0265 currentModuleDescription_->moduleLabel().c_str(),
0266 productInstanceLabel.c_str(),
0267 currentModuleDescription_->processName().c_str());
0268 CPPUNIT_ASSERT(index != std::numeric_limits<unsigned int>::max());
0269 const_cast<std::vector<edm::ProductResolverIndex>&>(prod->putTokenIndexToProductResolverIndex()).push_back(index);
0270 currentEvent_->setProducer(prod.get(), nullptr);
0271 currentEvent_->put(std::move(product), productInstanceLabel);
0272 if (doCommit) {
0273 currentEvent_->commit_(std::vector<ProductResolverIndex>());
0274 }
0275 return prod;
0276 }
0277
0278 template <class T>
0279 std::unique_ptr<ProducerBase> testEvent::putProductUsingToken(std::unique_ptr<T> product,
0280 std::string const& productInstanceLabel,
0281 bool doCommit) {
0282 auto prod = std::make_unique<TestProducer<edmtest::IntProduct>>(productInstanceLabel);
0283 EDPutTokenT<edmtest::IntProduct> token = prod->token_;
0284 auto index = principal_->productLookup().index(PRODUCT_TYPE,
0285 edm::TypeID(typeid(T)),
0286 currentModuleDescription_->moduleLabel().c_str(),
0287 productInstanceLabel.c_str(),
0288 currentModuleDescription_->processName().c_str());
0289 CPPUNIT_ASSERT(index != std::numeric_limits<unsigned int>::max());
0290 const_cast<std::vector<edm::ProductResolverIndex>&>(prod->putTokenIndexToProductResolverIndex()).push_back(index);
0291 currentEvent_->setProducer(prod.get(), nullptr);
0292 currentEvent_->put(token, std::move(product));
0293 if (doCommit) {
0294 currentEvent_->commit_(std::vector<ProductResolverIndex>());
0295 }
0296 return prod;
0297 }
0298
0299 template <class T>
0300 std::unique_ptr<ProducerBase> testEvent::emplaceProduct(T product,
0301 std::string const& productInstanceLabel,
0302 bool doCommit) {
0303 auto prod = std::make_unique<TestProducer<edmtest::IntProduct>>(productInstanceLabel);
0304 EDPutTokenT<edmtest::IntProduct> token = prod->token_;
0305 auto index = principal_->productLookup().index(PRODUCT_TYPE,
0306 edm::TypeID(typeid(T)),
0307 currentModuleDescription_->moduleLabel().c_str(),
0308 productInstanceLabel.c_str(),
0309 currentModuleDescription_->processName().c_str());
0310 CPPUNIT_ASSERT(index != std::numeric_limits<unsigned int>::max());
0311 const_cast<std::vector<edm::ProductResolverIndex>&>(prod->putTokenIndexToProductResolverIndex()).push_back(index);
0312 currentEvent_->setProducer(prod.get(), nullptr);
0313 currentEvent_->emplace(token, std::move(product));
0314 if (doCommit) {
0315 currentEvent_->commit_(std::vector<ProductResolverIndex>());
0316 }
0317 return prod;
0318 }
0319
0320 testEvent::testEvent()
0321 : availableProducts_(new SignallingProductRegistryFiller()),
0322 branchIDListHelper_(new BranchIDListHelper()),
0323 thinnedAssociationsHelper_(new ThinnedAssociationsHelper()),
0324 principal_(),
0325 currentEvent_(),
0326 currentModuleDescription_(),
0327 moduleDescriptions_(),
0328 processHistoryRegistry_(),
0329 processConfigurations_() {
0330 typedef edmtest::IntProduct prod_t;
0331 typedef std::vector<edmtest::Thing> vec_t;
0332
0333 registerProduct<prod_t>("nolabel_tag", "modOne", "IntProducer", "EARLY");
0334 registerProduct<prod_t>("int1_tag", "modMulti", "IntProducer", "EARLY", "int1");
0335 registerProduct<prod_t>("int1_tag_late", "modMulti", "IntProducer", "LATE", "int1");
0336 registerProduct<prod_t>("int2_tag", "modMulti", "IntProducer", "EARLY", "int2");
0337 registerProduct<prod_t>("int3_tag", "modMulti", "IntProducer", "EARLY");
0338 registerProduct<vec_t>("thing", "modthing", "ThingProducer", "LATE");
0339 registerProduct<vec_t>("thing2", "modthing", "ThingProducer", "LATE", "inst2");
0340
0341
0342
0343 ParameterSet moduleParams;
0344 std::string moduleLabel("modMulti");
0345 std::string moduleClassName("IntProducer");
0346 moduleParams.addParameter<std::string>("@module_type", moduleClassName);
0347 moduleParams.addParameter<std::string>("@module_label", moduleLabel);
0348 moduleParams.registerIt();
0349
0350 ParameterSet processParams;
0351 std::string processName("CURRENT");
0352 processParams.addParameter<std::string>("@process_name", processName);
0353 processParams.addParameter(moduleLabel, moduleParams);
0354 processParams.registerIt();
0355
0356 auto process = edmtest::makeDummyProcessConfiguration(processName, processParams.id());
0357
0358 TypeWithDict product_type(typeid(prod_t));
0359
0360 auto processX = std::make_shared<ProcessConfiguration>(process);
0361 processConfigurations_.push_back(processX);
0362 currentModuleDescription_.reset(new ModuleDescription(
0363 moduleParams.id(), moduleClassName, moduleLabel, processX.get(), ModuleDescription::getUniqueID()));
0364
0365 std::string productInstanceName("int1");
0366
0367 ProductDescription branch(InEvent,
0368 moduleLabel,
0369 processName,
0370 product_type.userClassName(),
0371 product_type.friendlyClassName(),
0372 productInstanceName,
0373 product_type);
0374
0375 availableProducts_->addProduct(branch);
0376
0377
0378 availableProducts_->setFrozen();
0379 branchIDListHelper_->updateFromRegistry(availableProducts_->registry());
0380 }
0381
0382 testEvent::~testEvent() {}
0383
0384 void testEvent::setUp() {
0385
0386
0387
0388
0389 ParameterSet moduleParamsEarly;
0390 std::string moduleLabelEarly("currentModule");
0391 std::string moduleClassNameEarly("IntProducer");
0392 moduleParamsEarly.addParameter<std::string>("@module_type", moduleClassNameEarly);
0393 moduleParamsEarly.addParameter<std::string>("@module_label", moduleLabelEarly);
0394 moduleParamsEarly.registerIt();
0395
0396 ParameterSet processParamsEarly;
0397 std::string processNameEarly("EARLY");
0398 processParamsEarly.addParameter<std::string>("@process_name", processNameEarly);
0399 processParamsEarly.addParameter(moduleLabelEarly, moduleParamsEarly);
0400 processParamsEarly.registerIt();
0401
0402 auto processEarly = edmtest::makeDummyProcessConfiguration("EARLY", processParamsEarly.id());
0403
0404 ParameterSet moduleParamsLate;
0405 std::string moduleLabelLate("currentModule");
0406 std::string moduleClassNameLate("IntProducer");
0407 moduleParamsLate.addParameter<std::string>("@module_type", moduleClassNameLate);
0408 moduleParamsLate.addParameter<std::string>("@module_label", moduleLabelLate);
0409 moduleParamsLate.registerIt();
0410
0411 ParameterSet processParamsLate;
0412 std::string processNameLate("LATE");
0413 processParamsLate.addParameter<std::string>("@process_name", processNameLate);
0414 processParamsLate.addParameter(moduleLabelLate, moduleParamsLate);
0415 processParamsLate.registerIt();
0416
0417 auto processLate = edmtest::makeDummyProcessConfiguration("LATE", processParamsLate.id());
0418
0419 auto processHistory = std::make_unique<ProcessHistory>();
0420 ProcessHistory& ph = *processHistory;
0421 processHistory->push_back(processEarly);
0422 processHistory->push_back(processLate);
0423
0424 processHistoryRegistry_.registerProcessHistory(ph);
0425
0426 ProcessHistoryID processHistoryID = ph.id();
0427
0428
0429
0430
0431
0432
0433
0434
0435
0436
0437
0438
0439
0440
0441
0442
0443 std::shared_ptr<ProductRegistry const> preg(std::make_shared<ProductRegistry>(availableProducts_->registry()));
0444 std::string uuid = createGlobalIdentifier();
0445 Timestamp time = make_timestamp();
0446 EventID id = make_id();
0447 ProcessConfiguration const& pc = currentModuleDescription_->processConfiguration();
0448 auto rp = std::make_shared<RunPrincipal>(preg, edm::productResolversFactory::makePrimary, pc, &historyAppender_, 0);
0449 rp->setAux(RunAuxiliary(id.run(), time, time));
0450 lbp_ = std::make_shared<LuminosityBlockPrincipal>(
0451 preg, edm::productResolversFactory::makePrimary, pc, &historyAppender_, 0);
0452 lbp_->setAux(LuminosityBlockAuxiliary(rp->run(), 1, time, time));
0453 lbp_->setRunPrincipal(rp);
0454 EventAuxiliary eventAux(id, uuid, time, true);
0455 const_cast<ProcessHistoryID&>(eventAux.processHistoryID()) = processHistoryID;
0456 principal_.reset(new edm::EventPrincipal(preg,
0457 edm::productResolversFactory::makePrimary,
0458 branchIDListHelper_,
0459 thinnedAssociationsHelper_,
0460 pc,
0461 &historyAppender_,
0462 edm::StreamID::invalidStreamID()));
0463 principal_->fillEventPrincipal(eventAux, processHistoryRegistry_.getMapped(eventAux.processHistoryID()));
0464 principal_->setLuminosityBlockPrincipal(lbp_.get());
0465 ModuleCallingContext mcc(currentModuleDescription_.get());
0466 currentEvent_.reset(new Event(*principal_, *currentModuleDescription_, &mcc));
0467 }
0468
0469 void testEvent::tearDown() {
0470 currentEvent_.reset();
0471 principal_.reset();
0472 }
0473
0474 void testEvent::emptyEvent() {
0475 CPPUNIT_ASSERT(currentEvent_);
0476 CPPUNIT_ASSERT(currentEvent_->id() == make_id());
0477 CPPUNIT_ASSERT(currentEvent_->time() == make_timestamp());
0478 CPPUNIT_ASSERT(currentEvent_->size() == 0);
0479 }
0480
0481 void testEvent::getByLabelFromEmpty() {
0482 InputTag inputTag("moduleLabel", "instanceName");
0483 Handle<int> nonesuch;
0484 CPPUNIT_ASSERT(!nonesuch.isValid());
0485 CPPUNIT_ASSERT(!currentEvent_->getByLabel(inputTag, nonesuch));
0486 CPPUNIT_ASSERT(!nonesuch.isValid());
0487 CPPUNIT_ASSERT(nonesuch.failedToGet());
0488 CPPUNIT_ASSERT_THROW(*nonesuch, cms::Exception);
0489 }
0490
0491 void testEvent::getByTokenFromEmpty() {
0492 InputTag inputTag("moduleLabel", "instanceName");
0493
0494 IntConsumer consumer(std::vector<InputTag>{1, inputTag});
0495 consumer.updateLookup(InEvent, principal_->productLookup(), false);
0496 assert(1 == consumer.m_tokens.size());
0497 currentEvent_->setConsumer(&consumer);
0498 Handle<int> nonesuch;
0499 CPPUNIT_ASSERT(!nonesuch.isValid());
0500 CPPUNIT_ASSERT(!currentEvent_->getByToken(consumer.m_tokens[0], nonesuch));
0501 CPPUNIT_ASSERT(!nonesuch.isValid());
0502 CPPUNIT_ASSERT(nonesuch.failedToGet());
0503 CPPUNIT_ASSERT_THROW(*nonesuch, cms::Exception);
0504
0505 {
0506 edm::EventBase const* eb = currentEvent_.get();
0507 Handle<int> nonesuch;
0508 CPPUNIT_ASSERT(!nonesuch.isValid());
0509 CPPUNIT_ASSERT(!eb->getByToken(consumer.m_tokens[0], nonesuch));
0510 CPPUNIT_ASSERT(!nonesuch.isValid());
0511 CPPUNIT_ASSERT(nonesuch.failedToGet());
0512 CPPUNIT_ASSERT_THROW(*nonesuch, cms::Exception);
0513 }
0514 }
0515
0516 void testEvent::getHandleFromEmpty() {
0517 InputTag inputTag("moduleLabel", "instanceName");
0518
0519 IntConsumer consumer(std::vector<InputTag>{1, inputTag});
0520 consumer.updateLookup(InEvent, principal_->productLookup(), false);
0521 assert(1 == consumer.m_tokens.size());
0522 currentEvent_->setConsumer(&consumer);
0523 Handle<int> nonesuch;
0524 CPPUNIT_ASSERT(!(nonesuch = currentEvent_->getHandle(consumer.m_tokens[0])));
0525 CPPUNIT_ASSERT(!nonesuch.isValid());
0526 CPPUNIT_ASSERT(nonesuch.failedToGet());
0527 CPPUNIT_ASSERT_THROW(*nonesuch, cms::Exception);
0528 }
0529
0530 void testEvent::getFromEmpty() {
0531 InputTag inputTag("moduleLabel", "instanceName");
0532
0533 IntConsumer consumer(std::vector<InputTag>{1, inputTag});
0534 consumer.updateLookup(InEvent, principal_->productLookup(), false);
0535 assert(1 == consumer.m_tokens.size());
0536 currentEvent_->setConsumer(&consumer);
0537 CPPUNIT_ASSERT_THROW((void)currentEvent_->get(consumer.m_tokens[0]), cms::Exception);
0538 }
0539
0540 void testEvent::putAnIntProduct() {
0541 auto p = putProduct(std::make_unique<edmtest::IntProduct>(3), "int1", false);
0542 CPPUNIT_ASSERT(currentEvent_->size() == 1);
0543 currentEvent_->commit_(std::vector<ProductResolverIndex>());
0544 CPPUNIT_ASSERT(currentEvent_->size() == 1);
0545 }
0546
0547 void testEvent::putAndGetAnIntProduct() {
0548 auto p = putProduct(std::make_unique<edmtest::IntProduct>(4), "int1");
0549
0550 InputTag should_match("modMulti", "int1", "CURRENT");
0551 InputTag should_not_match("modMulti", "int1", "NONESUCH");
0552 Handle<edmtest::IntProduct> h;
0553 currentEvent_->getByLabel(should_match, h);
0554 CPPUNIT_ASSERT(h.isValid());
0555 CPPUNIT_ASSERT(!currentEvent_->getByLabel(should_not_match, h));
0556 CPPUNIT_ASSERT(!h.isValid());
0557 CPPUNIT_ASSERT_THROW(*h, cms::Exception);
0558 }
0559
0560 void testEvent::putAndGetAnIntProductByToken() {
0561 auto p = putProductUsingToken(std::make_unique<edmtest::IntProduct>(4), "int1");
0562
0563 InputTag should_match("modMulti", "int1", "CURRENT");
0564 InputTag should_not_match("modMulti", "int1", "NONESUCH");
0565 Handle<edmtest::IntProduct> h;
0566 currentEvent_->getByLabel(should_match, h);
0567 CPPUNIT_ASSERT(h.isValid());
0568 CPPUNIT_ASSERT(!currentEvent_->getByLabel(should_not_match, h));
0569 CPPUNIT_ASSERT(!h.isValid());
0570 CPPUNIT_ASSERT_THROW(*h, cms::Exception);
0571 }
0572
0573 void testEvent::emplaceAndGetAnIntProductByToken() {
0574 auto p = emplaceProduct(edmtest::IntProduct{4}, "int1");
0575
0576 InputTag should_match("modMulti", "int1", "CURRENT");
0577 InputTag should_not_match("modMulti", "int1", "NONESUCH");
0578 Handle<edmtest::IntProduct> h;
0579 currentEvent_->getByLabel(should_match, h);
0580 CPPUNIT_ASSERT(h.isValid());
0581 CPPUNIT_ASSERT(!currentEvent_->getByLabel(should_not_match, h));
0582 CPPUNIT_ASSERT(!h.isValid());
0583 CPPUNIT_ASSERT_THROW(*h, cms::Exception);
0584 }
0585
0586 void testEvent::getByProductID() {
0587 typedef edmtest::IntProduct product_t;
0588 typedef std::unique_ptr<product_t> ap_t;
0589 typedef Handle<product_t> handle_t;
0590
0591 ProductID wanted;
0592
0593 {
0594 ap_t one(new product_t(1));
0595 ProductID id1 = addProduct(std::move(one), "int1_tag", "int1");
0596 CPPUNIT_ASSERT(id1 != ProductID());
0597 wanted = id1;
0598
0599 ap_t two(new product_t(2));
0600 ProductID id2 = addProduct(std::move(two), "int2_tag", "int2");
0601 CPPUNIT_ASSERT(id2 != ProductID());
0602 CPPUNIT_ASSERT(id2 != id1);
0603
0604 currentEvent_->commit_(std::vector<ProductResolverIndex>());
0605 CPPUNIT_ASSERT(currentEvent_->size() == 2);
0606 }
0607
0608 handle_t h;
0609 currentEvent_->get(wanted, h);
0610 CPPUNIT_ASSERT(h.isValid());
0611 CPPUNIT_ASSERT(h.id() == wanted);
0612 CPPUNIT_ASSERT(h->value == 1);
0613
0614 ProductID invalid;
0615 CPPUNIT_ASSERT_THROW(currentEvent_->get(invalid, h), cms::Exception);
0616 CPPUNIT_ASSERT(!h.isValid());
0617 ProductID notpresent(0, std::numeric_limits<unsigned short>::max());
0618 CPPUNIT_ASSERT(!currentEvent_->get(notpresent, h));
0619 CPPUNIT_ASSERT(!h.isValid());
0620 CPPUNIT_ASSERT(h.failedToGet());
0621 CPPUNIT_ASSERT_THROW(*h, cms::Exception);
0622
0623 edm::EventBase* baseEvent = currentEvent_.get();
0624 handle_t h1;
0625 baseEvent->get(wanted, h1);
0626 CPPUNIT_ASSERT(h1.isValid());
0627 CPPUNIT_ASSERT(h1.id() == wanted);
0628 CPPUNIT_ASSERT(h1->value == 1);
0629
0630 CPPUNIT_ASSERT_THROW(baseEvent->get(invalid, h1), cms::Exception);
0631 CPPUNIT_ASSERT(!h1.isValid());
0632 CPPUNIT_ASSERT(!baseEvent->get(notpresent, h1));
0633 CPPUNIT_ASSERT(!h1.isValid());
0634 CPPUNIT_ASSERT(h1.failedToGet());
0635 CPPUNIT_ASSERT_THROW(*h1, cms::Exception);
0636 }
0637
0638 void testEvent::transaction() {
0639
0640
0641 CPPUNIT_ASSERT(principal_->size() == 0);
0642 {
0643 typedef edmtest::IntProduct product_t;
0644 typedef std::unique_ptr<product_t> ap_t;
0645
0646 ap_t three(new product_t(3));
0647 auto p = putProduct(std::move(three), "int1", false);
0648
0649 CPPUNIT_ASSERT(principal_->size() == 0);
0650 CPPUNIT_ASSERT(currentEvent_->size() == 1);
0651
0652 }
0653
0654
0655
0656 CPPUNIT_ASSERT(principal_->size() == 0);
0657 }
0658
0659 void testEvent::getByLabel() {
0660 typedef edmtest::IntProduct product_t;
0661 typedef std::unique_ptr<product_t> ap_t;
0662 typedef Handle<product_t> handle_t;
0663
0664 ap_t one(new product_t(1));
0665 ap_t two(new product_t(2));
0666 ap_t three(new product_t(3));
0667 ap_t four(new product_t(4));
0668 addProduct(std::move(one), "int1_tag", "int1");
0669 addProduct(std::move(two), "int2_tag", "int2");
0670 addProduct(std::move(three), "int3_tag");
0671 addProduct(std::move(four), "nolabel_tag");
0672
0673 auto ap_vthing = std::make_unique<std::vector<edmtest::Thing>>();
0674 addProduct(std::move(ap_vthing), "thing", "");
0675
0676 ap_t oneHundred(new product_t(100));
0677 addProduct(std::move(oneHundred), "int1_tag_late", "int1");
0678
0679 auto twoHundred = std::make_unique<edmtest::IntProduct>(200);
0680 putProduct(std::move(twoHundred), "int1");
0681
0682 CPPUNIT_ASSERT(currentEvent_->size() == 7);
0683
0684 handle_t h;
0685 CPPUNIT_ASSERT(currentEvent_->getByLabel("modMulti", h));
0686 CPPUNIT_ASSERT(h->value == 3);
0687
0688 CPPUNIT_ASSERT(currentEvent_->getByLabel("modMulti", "int1", h));
0689 CPPUNIT_ASSERT(h->value == 200);
0690
0691 CPPUNIT_ASSERT(!currentEvent_->getByLabel("modMulti", "nomatch", h));
0692 CPPUNIT_ASSERT(!h.isValid());
0693 CPPUNIT_ASSERT_THROW(*h, cms::Exception);
0694
0695 InputTag inputTag("modMulti", "int1");
0696 CPPUNIT_ASSERT(currentEvent_->getByLabel(inputTag, h));
0697 CPPUNIT_ASSERT(h->value == 200);
0698
0699 CPPUNIT_ASSERT(currentEvent_->getByLabel("modMulti", "int1", h));
0700 CPPUNIT_ASSERT(h->value == 200);
0701
0702 InputTag tag1("modMulti", "int1", "EARLY");
0703 CPPUNIT_ASSERT(currentEvent_->getByLabel(tag1, h));
0704 CPPUNIT_ASSERT(h->value == 1);
0705
0706 InputTag tag2("modMulti", "int1", "LATE");
0707 CPPUNIT_ASSERT(currentEvent_->getByLabel(tag2, h));
0708 CPPUNIT_ASSERT(h->value == 100);
0709
0710 InputTag tag3("modMulti", "int1", "CURRENT");
0711 CPPUNIT_ASSERT(currentEvent_->getByLabel(tag3, h));
0712 CPPUNIT_ASSERT(h->value == 200);
0713
0714 InputTag tag4("modMulti", "int2", "EARLY");
0715 CPPUNIT_ASSERT(currentEvent_->getByLabel(tag4, h));
0716 CPPUNIT_ASSERT(h->value == 2);
0717
0718 InputTag tag5("modOne");
0719 CPPUNIT_ASSERT(currentEvent_->getByLabel(tag5, h));
0720 CPPUNIT_ASSERT(h->value == 4);
0721
0722 CPPUNIT_ASSERT(currentEvent_->getByLabel("modOne", h));
0723 CPPUNIT_ASSERT(h->value == 4);
0724
0725 {
0726 edm::EventBase* baseEvent = currentEvent_.get();
0727 CPPUNIT_ASSERT(baseEvent->getByLabel(inputTag, h));
0728 CPPUNIT_ASSERT(h->value == 200);
0729 }
0730
0731 BasicHandle bh = principal_->getByLabel(
0732 PRODUCT_TYPE, TypeID(typeid(edmtest::IntProduct)), "modMulti", "int1", "LATE", nullptr, nullptr, nullptr);
0733 h = convert_handle<product_t>(std::move(bh));
0734 CPPUNIT_ASSERT(h->value == 100);
0735 BasicHandle bh2(principal_->getByLabel(
0736 PRODUCT_TYPE, TypeID(typeid(edmtest::IntProduct)), "modMulti", "int1", "nomatch", nullptr, nullptr, nullptr));
0737 CPPUNIT_ASSERT(!bh2.isValid());
0738
0739 std::shared_ptr<Wrapper<edmtest::IntProduct> const> ptr =
0740 getProductByTag<edmtest::IntProduct>(*principal_, inputTag, nullptr);
0741 CPPUNIT_ASSERT(ptr->product()->value == 200);
0742 }
0743
0744 void testEvent::getByToken() {
0745 typedef edmtest::IntProduct product_t;
0746 typedef std::unique_ptr<product_t> ap_t;
0747 typedef Handle<product_t> handle_t;
0748
0749 ap_t one(new product_t(1));
0750 ap_t two(new product_t(2));
0751 ap_t three(new product_t(3));
0752 ap_t four(new product_t(4));
0753 addProduct(std::move(one), "int1_tag", "int1");
0754 addProduct(std::move(two), "int2_tag", "int2");
0755 addProduct(std::move(three), "int3_tag");
0756 addProduct(std::move(four), "nolabel_tag");
0757
0758 auto ap_vthing = std::make_unique<std::vector<edmtest::Thing>>();
0759 addProduct(std::move(ap_vthing), "thing", "");
0760
0761 ap_t oneHundred(new product_t(100));
0762 addProduct(std::move(oneHundred), "int1_tag_late", "int1");
0763
0764 auto twoHundred = std::make_unique<edmtest::IntProduct>(200);
0765 putProduct(std::move(twoHundred), "int1");
0766
0767 CPPUNIT_ASSERT(currentEvent_->size() == 7);
0768
0769 IntProductConsumer consumer(std::vector<InputTag>{InputTag("modMulti"),
0770 InputTag("modMulti", "int1"),
0771 InputTag("modMulti", "nomatch"),
0772 InputTag("modMulti", "int1", "EARLY"),
0773 InputTag("modMulti", "int1", "LATE"),
0774 InputTag("modMulti", "int1", "CURRENT"),
0775 InputTag("modMulti", "int2", "EARLY"),
0776 InputTag("modOne")});
0777 consumer.updateLookup(InEvent, principal_->productLookup(), false);
0778
0779 currentEvent_->setConsumer(&consumer);
0780 edm::EventBase const* eb = currentEvent_.get();
0781
0782 const auto modMultiToken = consumer.m_tokens[0];
0783 const auto modMultiInt1Token = consumer.m_tokens[1];
0784 const auto modMultinomatchToken = consumer.m_tokens[2];
0785 const auto modMultiInt1EarlyToken = consumer.m_tokens[3];
0786 const auto modMultiInt1LateToken = consumer.m_tokens[4];
0787 const auto modMultiInt1CurrentToken = consumer.m_tokens[5];
0788 const auto modMultiInt2EarlyToken = consumer.m_tokens[6];
0789 const auto modOneToken = consumer.m_tokens[7];
0790
0791 handle_t h;
0792 CPPUNIT_ASSERT(currentEvent_->getByToken(modMultiToken, h));
0793 CPPUNIT_ASSERT(h->value == 3);
0794 CPPUNIT_ASSERT(eb->getByToken(modMultiToken, h));
0795 CPPUNIT_ASSERT(h->value == 3);
0796
0797 CPPUNIT_ASSERT(currentEvent_->getByToken(modMultiInt1Token, h));
0798 CPPUNIT_ASSERT(h->value == 200);
0799 CPPUNIT_ASSERT(eb->getByToken(modMultiInt1Token, h));
0800 CPPUNIT_ASSERT(h->value == 200);
0801
0802 CPPUNIT_ASSERT(!currentEvent_->getByToken(modMultinomatchToken, h));
0803 CPPUNIT_ASSERT(!h.isValid());
0804 CPPUNIT_ASSERT_THROW(*h, cms::Exception);
0805
0806 CPPUNIT_ASSERT(currentEvent_->getByToken(modMultiInt1Token, h));
0807 CPPUNIT_ASSERT(h->value == 200);
0808 CPPUNIT_ASSERT(eb->getByToken(modMultiInt1Token, h));
0809 CPPUNIT_ASSERT(h->value == 200);
0810
0811 CPPUNIT_ASSERT(currentEvent_->getByToken(modMultiInt1EarlyToken, h));
0812 CPPUNIT_ASSERT(h->value == 1);
0813 CPPUNIT_ASSERT(eb->getByToken(modMultiInt1EarlyToken, h));
0814 CPPUNIT_ASSERT(h->value == 1);
0815
0816 CPPUNIT_ASSERT(currentEvent_->getByToken(modMultiInt1LateToken, h));
0817 CPPUNIT_ASSERT(h->value == 100);
0818 CPPUNIT_ASSERT(eb->getByToken(modMultiInt1LateToken, h));
0819 CPPUNIT_ASSERT(h->value == 100);
0820
0821 CPPUNIT_ASSERT(currentEvent_->getByToken(modMultiInt1CurrentToken, h));
0822 CPPUNIT_ASSERT(h->value == 200);
0823 CPPUNIT_ASSERT(eb->getByToken(modMultiInt1CurrentToken, h));
0824 CPPUNIT_ASSERT(h->value == 200);
0825
0826 CPPUNIT_ASSERT(currentEvent_->getByToken(modMultiInt2EarlyToken, h));
0827 CPPUNIT_ASSERT(h->value == 2);
0828 CPPUNIT_ASSERT(eb->getByToken(modMultiInt2EarlyToken, h));
0829 CPPUNIT_ASSERT(h->value == 2);
0830
0831 CPPUNIT_ASSERT(currentEvent_->getByToken(modOneToken, h));
0832 CPPUNIT_ASSERT(h->value == 4);
0833 CPPUNIT_ASSERT(eb->getByToken(modOneToken, h));
0834 CPPUNIT_ASSERT(h->value == 4);
0835 }
0836
0837 void testEvent::getHandle() {
0838 typedef edmtest::IntProduct product_t;
0839 typedef std::unique_ptr<product_t> ap_t;
0840 typedef Handle<product_t> handle_t;
0841
0842 ap_t one(new product_t(1));
0843 ap_t two(new product_t(2));
0844 ap_t three(new product_t(3));
0845 ap_t four(new product_t(4));
0846 addProduct(std::move(one), "int1_tag", "int1");
0847 addProduct(std::move(two), "int2_tag", "int2");
0848 addProduct(std::move(three), "int3_tag");
0849 addProduct(std::move(four), "nolabel_tag");
0850
0851 auto ap_vthing = std::make_unique<std::vector<edmtest::Thing>>();
0852 addProduct(std::move(ap_vthing), "thing", "");
0853
0854 ap_t oneHundred(new product_t(100));
0855 addProduct(std::move(oneHundred), "int1_tag_late", "int1");
0856
0857 auto twoHundred = std::make_unique<edmtest::IntProduct>(200);
0858 putProduct(std::move(twoHundred), "int1");
0859
0860 CPPUNIT_ASSERT(currentEvent_->size() == 7);
0861
0862 IntProductConsumer consumer(std::vector<InputTag>{InputTag("modMulti"),
0863 InputTag("modMulti", "int1"),
0864 InputTag("modMulti", "nomatch"),
0865 InputTag("modMulti", "int1", "EARLY"),
0866 InputTag("modMulti", "int1", "LATE"),
0867 InputTag("modMulti", "int1", "CURRENT"),
0868 InputTag("modMulti", "int2", "EARLY"),
0869 InputTag("modOne")});
0870 consumer.updateLookup(InEvent, principal_->productLookup(), false);
0871
0872 currentEvent_->setConsumer(&consumer);
0873
0874 const auto modMultiToken = consumer.m_tokens[0];
0875 const auto modMultiInt1Token = consumer.m_tokens[1];
0876 const auto modMultinomatchToken = consumer.m_tokens[2];
0877 const auto modMultiInt1EarlyToken = consumer.m_tokens[3];
0878 const auto modMultiInt1LateToken = consumer.m_tokens[4];
0879 const auto modMultiInt1CurrentToken = consumer.m_tokens[5];
0880 const auto modMultiInt2EarlyToken = consumer.m_tokens[6];
0881 const auto modOneToken = consumer.m_tokens[7];
0882
0883 handle_t h;
0884 CPPUNIT_ASSERT(h = currentEvent_->getHandle(modMultiToken));
0885 CPPUNIT_ASSERT(h->value == 3);
0886
0887 CPPUNIT_ASSERT(h = currentEvent_->getHandle(modMultiInt1Token));
0888 CPPUNIT_ASSERT(h->value == 200);
0889
0890 CPPUNIT_ASSERT(!(h = currentEvent_->getHandle(modMultinomatchToken)));
0891 CPPUNIT_ASSERT(!h.isValid());
0892 CPPUNIT_ASSERT_THROW(*h, cms::Exception);
0893
0894 CPPUNIT_ASSERT(h = currentEvent_->getHandle(modMultiInt1Token));
0895 CPPUNIT_ASSERT(h->value == 200);
0896
0897 CPPUNIT_ASSERT(h = currentEvent_->getHandle(modMultiInt1EarlyToken));
0898 CPPUNIT_ASSERT(h->value == 1);
0899
0900 CPPUNIT_ASSERT(h = currentEvent_->getHandle(modMultiInt1LateToken));
0901 CPPUNIT_ASSERT(h->value == 100);
0902
0903 CPPUNIT_ASSERT(h = currentEvent_->getHandle(modMultiInt1CurrentToken));
0904 CPPUNIT_ASSERT(h->value == 200);
0905
0906 CPPUNIT_ASSERT(h = currentEvent_->getHandle(modMultiInt2EarlyToken));
0907 CPPUNIT_ASSERT(h->value == 2);
0908
0909 CPPUNIT_ASSERT(h = currentEvent_->getHandle(modOneToken));
0910 CPPUNIT_ASSERT(h->value == 4);
0911 }
0912
0913 void testEvent::get_product() {
0914 typedef edmtest::IntProduct product_t;
0915 typedef std::unique_ptr<product_t> ap_t;
0916 typedef Handle<product_t> handle_t;
0917
0918 ap_t one(new product_t(1));
0919 ap_t two(new product_t(2));
0920 ap_t three(new product_t(3));
0921 ap_t four(new product_t(4));
0922 addProduct(std::move(one), "int1_tag", "int1");
0923 addProduct(std::move(two), "int2_tag", "int2");
0924 addProduct(std::move(three), "int3_tag");
0925 addProduct(std::move(four), "nolabel_tag");
0926
0927 auto ap_vthing = std::make_unique<std::vector<edmtest::Thing>>();
0928 addProduct(std::move(ap_vthing), "thing", "");
0929
0930 ap_t oneHundred(new product_t(100));
0931 addProduct(std::move(oneHundred), "int1_tag_late", "int1");
0932
0933 auto twoHundred = std::make_unique<edmtest::IntProduct>(200);
0934 putProduct(std::move(twoHundred), "int1");
0935
0936 CPPUNIT_ASSERT(currentEvent_->size() == 7);
0937
0938 IntProductConsumer consumer(std::vector<InputTag>{InputTag("modMulti"),
0939 InputTag("modMulti", "int1"),
0940 InputTag("modMulti", "nomatch"),
0941 InputTag("modMulti", "int1", "EARLY"),
0942 InputTag("modMulti", "int1", "LATE"),
0943 InputTag("modMulti", "int1", "CURRENT"),
0944 InputTag("modMulti", "int2", "EARLY"),
0945 InputTag("modOne")});
0946 consumer.updateLookup(InEvent, principal_->productLookup(), false);
0947
0948 currentEvent_->setConsumer(&consumer);
0949
0950 const auto modMultiToken = consumer.m_tokens[0];
0951 const auto modMultiInt1Token = consumer.m_tokens[1];
0952 const auto modMultinomatchToken = consumer.m_tokens[2];
0953 const auto modMultiInt1EarlyToken = consumer.m_tokens[3];
0954 const auto modMultiInt1LateToken = consumer.m_tokens[4];
0955 const auto modMultiInt1CurrentToken = consumer.m_tokens[5];
0956 const auto modMultiInt2EarlyToken = consumer.m_tokens[6];
0957 const auto modOneToken = consumer.m_tokens[7];
0958
0959 CPPUNIT_ASSERT(currentEvent_->get(modMultiToken).value == 3);
0960
0961 CPPUNIT_ASSERT(currentEvent_->get(modMultiInt1Token).value == 200);
0962
0963 CPPUNIT_ASSERT_THROW(currentEvent_->get(modMultinomatchToken), cms::Exception);
0964
0965 CPPUNIT_ASSERT(currentEvent_->get(modMultiInt1Token).value == 200);
0966
0967 CPPUNIT_ASSERT(currentEvent_->get(modMultiInt1EarlyToken).value == 1);
0968
0969 CPPUNIT_ASSERT(currentEvent_->get(modMultiInt1LateToken).value == 100);
0970
0971 CPPUNIT_ASSERT(currentEvent_->get(modMultiInt1CurrentToken).value == 200);
0972
0973 CPPUNIT_ASSERT(currentEvent_->get(modMultiInt2EarlyToken).value == 2);
0974
0975 CPPUNIT_ASSERT(currentEvent_->get(modOneToken).value == 4);
0976 }
0977
0978 void testEvent::printHistory() {
0979 ProcessHistory const& history = currentEvent_->processHistory();
0980 std::ofstream out("history.log");
0981
0982 copy_all(history, std::ostream_iterator<ProcessHistory::const_iterator::value_type>(out, "\n"));
0983 }
0984
0985 void testEvent::deleteProduct() {
0986 typedef edmtest::IntProduct product_t;
0987 typedef std::unique_ptr<product_t> ap_t;
0988
0989 ap_t one(new product_t(1));
0990 addProduct(std::move(one), "int1_tag", "int1");
0991
0992 BranchID id;
0993
0994 availableProducts_->callForEachBranch([&id](const ProductDescription& iDesc) {
0995 if (iDesc.moduleLabel() == "modMulti" && iDesc.productInstanceName() == "int1") {
0996 id = iDesc.branchID();
0997 }
0998 });
0999
1000 const ProductResolverBase* phb = principal_->getProductResolver(id);
1001 CPPUNIT_ASSERT(phb != nullptr);
1002
1003 CPPUNIT_ASSERT(!phb->productWasDeleted());
1004 principal_->deleteProduct(id);
1005 CPPUNIT_ASSERT(phb->productWasDeleted());
1006 }