diff --git a/FWCore/AbstractServices/interface/RandomNumberGenerator.h b/FWCore/AbstractServices/interface/RandomNumberGenerator.h index c023e23353cfa..a25ab5d321e51 100644 --- a/FWCore/AbstractServices/interface/RandomNumberGenerator.h +++ b/FWCore/AbstractServices/interface/RandomNumberGenerator.h @@ -138,7 +138,7 @@ namespace CLHEP { namespace edm { - class ConsumesCollector; + class EDConsumerBase; class Event; class LuminosityBlock; class LuminosityBlockIndex; @@ -203,7 +203,8 @@ namespace edm { virtual std::vector const& getEventCache(StreamID const&) const = 0; virtual std::vector const& getLumiCache(LuminosityBlockIndex const&) const = 0; - virtual void consumes(ConsumesCollector&& iC) const = 0; + //Can be nullptr if the service is not consuming anything. The object is owned by the service. + virtual EDConsumerBase* consumer() = 0; /// For debugging purposes only. virtual void print(std::ostream& os) const = 0; diff --git a/FWCore/Framework/src/EventProcessor.cc b/FWCore/Framework/src/EventProcessor.cc index 18cc632cfdf00..028282e9733bc 100644 --- a/FWCore/Framework/src/EventProcessor.cc +++ b/FWCore/Framework/src/EventProcessor.cc @@ -710,6 +710,20 @@ namespace edm { firstException = std::current_exception(); } } + if (!firstException) { + CMS_SA_ALLOW try { + edm::Service rng; + if (rng.isAvailable()) { + auto consumer = rng->consumer(); + if (consumer) { + consumer->updateLookup(InLumi, *preg_->productLookup(InLumi), true); + consumer->updateLookup(InEvent, *preg_->productLookup(InEvent), true); + } + } + } catch (...) { + firstException = std::current_exception(); + } + } if (firstException) { std::rethrow_exception(firstException); } @@ -1649,6 +1663,7 @@ namespace edm { Service rng; if (rng.isAvailable()) { LuminosityBlock lb(lumiPrincipal, ModuleDescription(), nullptr, false); + lb.setConsumer(rng->consumer()); rng->preBeginLumi(lb); } @@ -2274,6 +2289,7 @@ namespace edm { Service rng; if (rng.isAvailable()) { Event ev(*pep, ModuleDescription(), nullptr); + ev.setConsumer(rng->consumer()); rng->postEventRead(ev); } diff --git a/FWCore/Framework/src/Schedule.cc b/FWCore/Framework/src/Schedule.cc index 67a26e68d654c..990667271444f 100644 --- a/FWCore/Framework/src/Schedule.cc +++ b/FWCore/Framework/src/Schedule.cc @@ -372,19 +372,6 @@ namespace edm { proc_pset.addParameter(std::string("@end_paths"), scheduledEndPaths); } - class RngEDConsumer : public EDConsumerBase { - public: - explicit RngEDConsumer(std::set& typesConsumed) { - Service rng; - if (rng.isAvailable()) { - rng->consumes(consumesCollector()); - for (auto const& consumesInfo : this->moduleConsumesInfos()) { - typesConsumed.emplace(consumesInfo.type()); - } - } - } - }; - template auto doCleanup(F&& iF) { auto wrapped = [f = std::move(iF)](std::exception_ptr const* iPtr, edm::WaitingTaskHolder iTask) { @@ -571,7 +558,18 @@ namespace edm { } }); // The RandomNumberGeneratorService is not a module, yet it consumes. - { RngEDConsumer rngConsumer = RngEDConsumer(productTypesConsumed); } + { + Service rng; + if (rng.isAvailable()) { + //if the service doesn't consume anything, the consumer will be nullptr + auto consumer = rng->consumer(); + if (consumer) { + for (auto const& consumesInfo : consumer->moduleConsumesInfos()) { + productTypesConsumed.emplace(consumesInfo.type()); + } + } + } + } preg.setFrozen(productTypesConsumed, elementTypesConsumed, processConfiguration->processName()); } diff --git a/FWCore/Services/interface/ExternalRandomNumberGeneratorService.h b/FWCore/Services/interface/ExternalRandomNumberGeneratorService.h index e7250737deab2..3d13d25428c5f 100644 --- a/FWCore/Services/interface/ExternalRandomNumberGeneratorService.h +++ b/FWCore/Services/interface/ExternalRandomNumberGeneratorService.h @@ -46,7 +46,7 @@ namespace edm { std::vector const& getEventCache(StreamID const&) const final; std::vector const& getLumiCache(LuminosityBlockIndex const&) const final; - void consumes(ConsumesCollector&& iC) const final; + EDConsumerBase* consumer() final; /// For debugging purposes only. void print(std::ostream& os) const final; diff --git a/FWCore/Services/src/ExternalRandomNumberGeneratorService.cc b/FWCore/Services/src/ExternalRandomNumberGeneratorService.cc index 5376475961d12..8478c002e2945 100644 --- a/FWCore/Services/src/ExternalRandomNumberGeneratorService.cc +++ b/FWCore/Services/src/ExternalRandomNumberGeneratorService.cc @@ -85,7 +85,7 @@ std::vector const& ExternalRandomNumberGeneratorService::getL return s_dummyStates; } -void ExternalRandomNumberGeneratorService::consumes(ConsumesCollector&& iC) const {} +EDConsumerBase* ExternalRandomNumberGeneratorService::consumer() { return nullptr; } /// For debugging purposes only. void ExternalRandomNumberGeneratorService::print(std::ostream& os) const {} diff --git a/IOMC/RandomEngine/plugins/RandomNumberGeneratorService.cc b/IOMC/RandomEngine/plugins/RandomNumberGeneratorService.cc index 37b374a225b13..ad23655ee64c9 100644 --- a/IOMC/RandomEngine/plugins/RandomNumberGeneratorService.cc +++ b/IOMC/RandomEngine/plugins/RandomNumberGeneratorService.cc @@ -86,6 +86,11 @@ namespace edm { << "states two different ways in the same process.\n"; } + if (not restoreStateTag_.label().empty()) { + restoreStateBeginLumiToken_ = consumes(restoreStateBeginLumiTag_); + restoreStateToken_ = consumes(restoreStateTag_); + } + // The saveFileName must correspond to a file name without any path specification. // Throw if that is not true. if (!saveFileName_.empty() && (saveFileName_.find('/') != std::string::npos)) { @@ -218,12 +223,9 @@ namespace edm { } } - RandomNumberGeneratorService::~RandomNumberGeneratorService() {} + RandomNumberGeneratorService::~RandomNumberGeneratorService() noexcept(true) {} - void RandomNumberGeneratorService::consumes(ConsumesCollector&& iC) const { - iC.consumes(restoreStateBeginLumiTag_); - iC.consumes(restoreStateTag_); - } + EDConsumerBase* RandomNumberGeneratorService::consumer() { return this; } CLHEP::HepRandomEngine& RandomNumberGeneratorService::getEngine(StreamID const& streamID) { ModuleCallingContext const* mcc = CurrentModuleOnThread::getCurrentModuleOnThread(); @@ -665,8 +667,7 @@ namespace edm { } } - Handle states; - lumi.getByLabel(restoreStateBeginLumiTag_, states); + Handle states = lumi.getHandle(restoreStateBeginLumiToken_); if (!states.isValid()) { throw Exception(errors::ProductNotFound) @@ -682,9 +683,7 @@ namespace edm { } void RandomNumberGeneratorService::readFromEvent(Event const& event) { - Handle states; - - event.getByLabel(restoreStateTag_, states); + Handle states = event.getHandle(restoreStateToken_); if (!states.isValid()) { throw Exception(errors::ProductNotFound) diff --git a/IOMC/RandomEngine/plugins/RandomNumberGeneratorService.h b/IOMC/RandomEngine/plugins/RandomNumberGeneratorService.h index 20cd2d7bf4132..afe6832b1ed0f 100644 --- a/IOMC/RandomEngine/plugins/RandomNumberGeneratorService.h +++ b/IOMC/RandomEngine/plugins/RandomNumberGeneratorService.h @@ -13,7 +13,9 @@ #include "FWCore/AbstractServices/interface/RandomNumberGenerator.h" #include "FWCore/ServiceRegistry/interface/ServiceRegistryfwd.h" +#include "FWCore/Framework/interface/EDConsumerBase.h" #include "FWCore/Utilities/interface/InputTag.h" +#include "FWCore/Utilities/interface/EDGetToken.h" #include "FWCore/Utilities/interface/get_underlying_safe.h" #include @@ -45,15 +47,16 @@ namespace edm { class ParameterSet; class StreamContext; class StreamID; + class RandomEngineStates; namespace service { class SystemBounds; - class RandomNumberGeneratorService : public RandomNumberGenerator { + class RandomNumberGeneratorService : public RandomNumberGenerator, public EDConsumerBase { public: RandomNumberGeneratorService(ParameterSet const& pset, ActivityRegistry& activityRegistry); - ~RandomNumberGeneratorService() override; + ~RandomNumberGeneratorService() noexcept(true) override; RandomNumberGeneratorService(RandomNumberGeneratorService const&) = delete; RandomNumberGeneratorService const& operator=(RandomNumberGeneratorService const&) = delete; @@ -122,7 +125,7 @@ namespace edm { std::vector const& getLumiCache(LuminosityBlockIndex const&) const override; std::vector const& getEventCache(StreamID const&) const override; - void consumes(ConsumesCollector&& iC) const override; + EDConsumerBase* consumer() override; /// For debugging void print(std::ostream& os) const override; @@ -232,6 +235,9 @@ namespace edm { edm::InputTag restoreStateTag_; edm::InputTag restoreStateBeginLumiTag_; + edm::EDGetTokenT restoreStateToken_; + edm::EDGetTokenT restoreStateBeginLumiToken_; + std::vector> eventCache_; // streamID, sorted by module label std::vector> lumiCache_; // luminosityBlockIndex, sorted by module label diff --git a/Mixing/Base/src/PileupRandomNumberGenerator.cc b/Mixing/Base/src/PileupRandomNumberGenerator.cc index 5269c9a0aef91..30d88fac33c75 100644 --- a/Mixing/Base/src/PileupRandomNumberGenerator.cc +++ b/Mixing/Base/src/PileupRandomNumberGenerator.cc @@ -67,7 +67,7 @@ std::vector const& PileupRandomNumberGenerator::getLumiCache( return s_dummy; } -void PileupRandomNumberGenerator::consumes(edm::ConsumesCollector&& iC) const {} +edm::EDConsumerBase* PileupRandomNumberGenerator::consumer() { return nullptr; } void PileupRandomNumberGenerator::print(std::ostream& os) const {} diff --git a/Mixing/Base/src/PileupRandomNumberGenerator.h b/Mixing/Base/src/PileupRandomNumberGenerator.h index 872be5d26f9a5..9723827500044 100644 --- a/Mixing/Base/src/PileupRandomNumberGenerator.h +++ b/Mixing/Base/src/PileupRandomNumberGenerator.h @@ -54,7 +54,7 @@ class PileupRandomNumberGenerator : public edm::RandomNumberGenerator { std::vector const& getEventCache(edm::StreamID const&) const final; std::vector const& getLumiCache(edm::LuminosityBlockIndex const&) const final; - void consumes(edm::ConsumesCollector&& iC) const final; + edm::EDConsumerBase* consumer() final; /// For debugging purposes only. void print(std::ostream& os) const final;