Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions FWCore/AbstractServices/interface/RandomNumberGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ namespace CLHEP {

namespace edm {

class ConsumesCollector;
class EDConsumerBase;
class Event;
class LuminosityBlock;
class LuminosityBlockIndex;
Expand Down Expand Up @@ -203,7 +203,8 @@ namespace edm {
virtual std::vector<RandomEngineState> const& getEventCache(StreamID const&) const = 0;
virtual std::vector<RandomEngineState> 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;
Expand Down
16 changes: 16 additions & 0 deletions FWCore/Framework/src/EventProcessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,20 @@ namespace edm {
firstException = std::current_exception();
}
}
if (!firstException) {
CMS_SA_ALLOW try {
edm::Service<edm::RandomNumberGenerator> 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);
}
Expand Down Expand Up @@ -1649,6 +1663,7 @@ namespace edm {
Service<RandomNumberGenerator> rng;
if (rng.isAvailable()) {
LuminosityBlock lb(lumiPrincipal, ModuleDescription(), nullptr, false);
lb.setConsumer(rng->consumer());
rng->preBeginLumi(lb);
}

Expand Down Expand Up @@ -2274,6 +2289,7 @@ namespace edm {
Service<RandomNumberGenerator> rng;
if (rng.isAvailable()) {
Event ev(*pep, ModuleDescription(), nullptr);
ev.setConsumer(rng->consumer());
rng->postEventRead(ev);
}

Expand Down
26 changes: 12 additions & 14 deletions FWCore/Framework/src/Schedule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -372,19 +372,6 @@ namespace edm {
proc_pset.addParameter<vstring>(std::string("@end_paths"), scheduledEndPaths);
}

class RngEDConsumer : public EDConsumerBase {
public:
explicit RngEDConsumer(std::set<TypeID>& typesConsumed) {
Service<RandomNumberGenerator> rng;
if (rng.isAvailable()) {
rng->consumes(consumesCollector());
for (auto const& consumesInfo : this->moduleConsumesInfos()) {
typesConsumed.emplace(consumesInfo.type());
}
}
}
};

template <typename F>
auto doCleanup(F&& iF) {
auto wrapped = [f = std::move(iF)](std::exception_ptr const* iPtr, edm::WaitingTaskHolder iTask) {
Expand Down Expand Up @@ -571,7 +558,18 @@ namespace edm {
}
});
// The RandomNumberGeneratorService is not a module, yet it consumes.
{ RngEDConsumer rngConsumer = RngEDConsumer(productTypesConsumed); }
{
Service<RandomNumberGenerator> 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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace edm {
std::vector<RandomEngineState> const& getEventCache(StreamID const&) const final;
std::vector<RandomEngineState> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ std::vector<RandomEngineState> 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 {}
19 changes: 9 additions & 10 deletions IOMC/RandomEngine/plugins/RandomNumberGeneratorService.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ namespace edm {
<< "states two different ways in the same process.\n";
}

if (not restoreStateTag_.label().empty()) {
restoreStateBeginLumiToken_ = consumes<RandomEngineStates, InLumi>(restoreStateBeginLumiTag_);
restoreStateToken_ = consumes<RandomEngineStates>(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)) {
Expand Down Expand Up @@ -218,12 +223,9 @@ namespace edm {
}
}

RandomNumberGeneratorService::~RandomNumberGeneratorService() {}
RandomNumberGeneratorService::~RandomNumberGeneratorService() noexcept(true) {}

void RandomNumberGeneratorService::consumes(ConsumesCollector&& iC) const {
iC.consumes<RandomEngineStates, InLumi>(restoreStateBeginLumiTag_);
iC.consumes<RandomEngineStates>(restoreStateTag_);
}
EDConsumerBase* RandomNumberGeneratorService::consumer() { return this; }

CLHEP::HepRandomEngine& RandomNumberGeneratorService::getEngine(StreamID const& streamID) {
ModuleCallingContext const* mcc = CurrentModuleOnThread::getCurrentModuleOnThread();
Expand Down Expand Up @@ -665,8 +667,7 @@ namespace edm {
}
}

Handle<RandomEngineStates> states;
lumi.getByLabel(restoreStateBeginLumiTag_, states);
Handle<RandomEngineStates> states = lumi.getHandle(restoreStateBeginLumiToken_);

if (!states.isValid()) {
throw Exception(errors::ProductNotFound)
Expand All @@ -682,9 +683,7 @@ namespace edm {
}

void RandomNumberGeneratorService::readFromEvent(Event const& event) {
Handle<RandomEngineStates> states;

event.getByLabel(restoreStateTag_, states);
Handle<RandomEngineStates> states = event.getHandle(restoreStateToken_);

if (!states.isValid()) {
throw Exception(errors::ProductNotFound)
Expand Down
12 changes: 9 additions & 3 deletions IOMC/RandomEngine/plugins/RandomNumberGeneratorService.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <atomic>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -122,7 +125,7 @@ namespace edm {
std::vector<RandomEngineState> const& getLumiCache(LuminosityBlockIndex const&) const override;
std::vector<RandomEngineState> const& getEventCache(StreamID const&) const override;

void consumes(ConsumesCollector&& iC) const override;
EDConsumerBase* consumer() override;

/// For debugging
void print(std::ostream& os) const override;
Expand Down Expand Up @@ -232,6 +235,9 @@ namespace edm {
edm::InputTag restoreStateTag_;
edm::InputTag restoreStateBeginLumiTag_;

edm::EDGetTokenT<RandomEngineStates> restoreStateToken_;
edm::EDGetTokenT<RandomEngineStates> restoreStateBeginLumiToken_;

std::vector<std::vector<RandomEngineState>> eventCache_; // streamID, sorted by module label
std::vector<std::vector<RandomEngineState>> lumiCache_; // luminosityBlockIndex, sorted by module label

Expand Down
2 changes: 1 addition & 1 deletion Mixing/Base/src/PileupRandomNumberGenerator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ std::vector<RandomEngineState> 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 {}

Expand Down
2 changes: 1 addition & 1 deletion Mixing/Base/src/PileupRandomNumberGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class PileupRandomNumberGenerator : public edm::RandomNumberGenerator {
std::vector<RandomEngineState> const& getEventCache(edm::StreamID const&) const final;
std::vector<RandomEngineState> 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;
Expand Down