Skip to content

Commit df56735

Browse files
authored
Merge pull request #48696 from Dr15Jones/randomNumberGeneratorConsumer
RandomNumberGeneratorService properly does consumes
2 parents 1b737b8 + 7af0a06 commit df56735

File tree

9 files changed

+53
-33
lines changed

9 files changed

+53
-33
lines changed

FWCore/AbstractServices/interface/RandomNumberGenerator.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ namespace CLHEP {
138138

139139
namespace edm {
140140

141-
class ConsumesCollector;
141+
class EDConsumerBase;
142142
class Event;
143143
class LuminosityBlock;
144144
class LuminosityBlockIndex;
@@ -203,7 +203,8 @@ namespace edm {
203203
virtual std::vector<RandomEngineState> const& getEventCache(StreamID const&) const = 0;
204204
virtual std::vector<RandomEngineState> const& getLumiCache(LuminosityBlockIndex const&) const = 0;
205205

206-
virtual void consumes(ConsumesCollector&& iC) const = 0;
206+
//Can be nullptr if the service is not consuming anything. The object is owned by the service.
207+
virtual EDConsumerBase* consumer() = 0;
207208

208209
/// For debugging purposes only.
209210
virtual void print(std::ostream& os) const = 0;

FWCore/Framework/src/EventProcessor.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,20 @@ namespace edm {
710710
firstException = std::current_exception();
711711
}
712712
}
713+
if (!firstException) {
714+
CMS_SA_ALLOW try {
715+
edm::Service<edm::RandomNumberGenerator> rng;
716+
if (rng.isAvailable()) {
717+
auto consumer = rng->consumer();
718+
if (consumer) {
719+
consumer->updateLookup(InLumi, *preg_->productLookup(InLumi), true);
720+
consumer->updateLookup(InEvent, *preg_->productLookup(InEvent), true);
721+
}
722+
}
723+
} catch (...) {
724+
firstException = std::current_exception();
725+
}
726+
}
713727
if (firstException) {
714728
std::rethrow_exception(firstException);
715729
}
@@ -1649,6 +1663,7 @@ namespace edm {
16491663
Service<RandomNumberGenerator> rng;
16501664
if (rng.isAvailable()) {
16511665
LuminosityBlock lb(lumiPrincipal, ModuleDescription(), nullptr, false);
1666+
lb.setConsumer(rng->consumer());
16521667
rng->preBeginLumi(lb);
16531668
}
16541669

@@ -2274,6 +2289,7 @@ namespace edm {
22742289
Service<RandomNumberGenerator> rng;
22752290
if (rng.isAvailable()) {
22762291
Event ev(*pep, ModuleDescription(), nullptr);
2292+
ev.setConsumer(rng->consumer());
22772293
rng->postEventRead(ev);
22782294
}
22792295

FWCore/Framework/src/Schedule.cc

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -372,19 +372,6 @@ namespace edm {
372372
proc_pset.addParameter<vstring>(std::string("@end_paths"), scheduledEndPaths);
373373
}
374374

375-
class RngEDConsumer : public EDConsumerBase {
376-
public:
377-
explicit RngEDConsumer(std::set<TypeID>& typesConsumed) {
378-
Service<RandomNumberGenerator> rng;
379-
if (rng.isAvailable()) {
380-
rng->consumes(consumesCollector());
381-
for (auto const& consumesInfo : this->moduleConsumesInfos()) {
382-
typesConsumed.emplace(consumesInfo.type());
383-
}
384-
}
385-
}
386-
};
387-
388375
template <typename F>
389376
auto doCleanup(F&& iF) {
390377
auto wrapped = [f = std::move(iF)](std::exception_ptr const* iPtr, edm::WaitingTaskHolder iTask) {
@@ -571,7 +558,18 @@ namespace edm {
571558
}
572559
});
573560
// The RandomNumberGeneratorService is not a module, yet it consumes.
574-
{ RngEDConsumer rngConsumer = RngEDConsumer(productTypesConsumed); }
561+
{
562+
Service<RandomNumberGenerator> rng;
563+
if (rng.isAvailable()) {
564+
//if the service doesn't consume anything, the consumer will be nullptr
565+
auto consumer = rng->consumer();
566+
if (consumer) {
567+
for (auto const& consumesInfo : consumer->moduleConsumesInfos()) {
568+
productTypesConsumed.emplace(consumesInfo.type());
569+
}
570+
}
571+
}
572+
}
575573
preg.setFrozen(productTypesConsumed, elementTypesConsumed, processConfiguration->processName());
576574
}
577575

FWCore/Services/interface/ExternalRandomNumberGeneratorService.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ namespace edm {
4646
std::vector<RandomEngineState> const& getEventCache(StreamID const&) const final;
4747
std::vector<RandomEngineState> const& getLumiCache(LuminosityBlockIndex const&) const final;
4848

49-
void consumes(ConsumesCollector&& iC) const final;
49+
EDConsumerBase* consumer() final;
5050

5151
/// For debugging purposes only.
5252
void print(std::ostream& os) const final;

FWCore/Services/src/ExternalRandomNumberGeneratorService.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ std::vector<RandomEngineState> const& ExternalRandomNumberGeneratorService::getL
8585
return s_dummyStates;
8686
}
8787

88-
void ExternalRandomNumberGeneratorService::consumes(ConsumesCollector&& iC) const {}
88+
EDConsumerBase* ExternalRandomNumberGeneratorService::consumer() { return nullptr; }
8989

9090
/// For debugging purposes only.
9191
void ExternalRandomNumberGeneratorService::print(std::ostream& os) const {}

IOMC/RandomEngine/plugins/RandomNumberGeneratorService.cc

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ namespace edm {
8686
<< "states two different ways in the same process.\n";
8787
}
8888

89+
if (not restoreStateTag_.label().empty()) {
90+
restoreStateBeginLumiToken_ = consumes<RandomEngineStates, InLumi>(restoreStateBeginLumiTag_);
91+
restoreStateToken_ = consumes<RandomEngineStates>(restoreStateTag_);
92+
}
93+
8994
// The saveFileName must correspond to a file name without any path specification.
9095
// Throw if that is not true.
9196
if (!saveFileName_.empty() && (saveFileName_.find('/') != std::string::npos)) {
@@ -218,12 +223,9 @@ namespace edm {
218223
}
219224
}
220225

221-
RandomNumberGeneratorService::~RandomNumberGeneratorService() {}
226+
RandomNumberGeneratorService::~RandomNumberGeneratorService() noexcept(true) {}
222227

223-
void RandomNumberGeneratorService::consumes(ConsumesCollector&& iC) const {
224-
iC.consumes<RandomEngineStates, InLumi>(restoreStateBeginLumiTag_);
225-
iC.consumes<RandomEngineStates>(restoreStateTag_);
226-
}
228+
EDConsumerBase* RandomNumberGeneratorService::consumer() { return this; }
227229

228230
CLHEP::HepRandomEngine& RandomNumberGeneratorService::getEngine(StreamID const& streamID) {
229231
ModuleCallingContext const* mcc = CurrentModuleOnThread::getCurrentModuleOnThread();
@@ -665,8 +667,7 @@ namespace edm {
665667
}
666668
}
667669

668-
Handle<RandomEngineStates> states;
669-
lumi.getByLabel(restoreStateBeginLumiTag_, states);
670+
Handle<RandomEngineStates> states = lumi.getHandle(restoreStateBeginLumiToken_);
670671

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

684685
void RandomNumberGeneratorService::readFromEvent(Event const& event) {
685-
Handle<RandomEngineStates> states;
686-
687-
event.getByLabel(restoreStateTag_, states);
686+
Handle<RandomEngineStates> states = event.getHandle(restoreStateToken_);
688687

689688
if (!states.isValid()) {
690689
throw Exception(errors::ProductNotFound)

IOMC/RandomEngine/plugins/RandomNumberGeneratorService.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313

1414
#include "FWCore/AbstractServices/interface/RandomNumberGenerator.h"
1515
#include "FWCore/ServiceRegistry/interface/ServiceRegistryfwd.h"
16+
#include "FWCore/Framework/interface/EDConsumerBase.h"
1617
#include "FWCore/Utilities/interface/InputTag.h"
18+
#include "FWCore/Utilities/interface/EDGetToken.h"
1719
#include "FWCore/Utilities/interface/get_underlying_safe.h"
1820

1921
#include <atomic>
@@ -45,15 +47,16 @@ namespace edm {
4547
class ParameterSet;
4648
class StreamContext;
4749
class StreamID;
50+
class RandomEngineStates;
4851

4952
namespace service {
5053

5154
class SystemBounds;
5255

53-
class RandomNumberGeneratorService : public RandomNumberGenerator {
56+
class RandomNumberGeneratorService : public RandomNumberGenerator, public EDConsumerBase {
5457
public:
5558
RandomNumberGeneratorService(ParameterSet const& pset, ActivityRegistry& activityRegistry);
56-
~RandomNumberGeneratorService() override;
59+
~RandomNumberGeneratorService() noexcept(true) override;
5760

5861
RandomNumberGeneratorService(RandomNumberGeneratorService const&) = delete;
5962
RandomNumberGeneratorService const& operator=(RandomNumberGeneratorService const&) = delete;
@@ -122,7 +125,7 @@ namespace edm {
122125
std::vector<RandomEngineState> const& getLumiCache(LuminosityBlockIndex const&) const override;
123126
std::vector<RandomEngineState> const& getEventCache(StreamID const&) const override;
124127

125-
void consumes(ConsumesCollector&& iC) const override;
128+
EDConsumerBase* consumer() override;
126129

127130
/// For debugging
128131
void print(std::ostream& os) const override;
@@ -232,6 +235,9 @@ namespace edm {
232235
edm::InputTag restoreStateTag_;
233236
edm::InputTag restoreStateBeginLumiTag_;
234237

238+
edm::EDGetTokenT<RandomEngineStates> restoreStateToken_;
239+
edm::EDGetTokenT<RandomEngineStates> restoreStateBeginLumiToken_;
240+
235241
std::vector<std::vector<RandomEngineState>> eventCache_; // streamID, sorted by module label
236242
std::vector<std::vector<RandomEngineState>> lumiCache_; // luminosityBlockIndex, sorted by module label
237243

Mixing/Base/src/PileupRandomNumberGenerator.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ std::vector<RandomEngineState> const& PileupRandomNumberGenerator::getLumiCache(
6767
return s_dummy;
6868
}
6969

70-
void PileupRandomNumberGenerator::consumes(edm::ConsumesCollector&& iC) const {}
70+
edm::EDConsumerBase* PileupRandomNumberGenerator::consumer() { return nullptr; }
7171

7272
void PileupRandomNumberGenerator::print(std::ostream& os) const {}
7373

Mixing/Base/src/PileupRandomNumberGenerator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class PileupRandomNumberGenerator : public edm::RandomNumberGenerator {
5454
std::vector<RandomEngineState> const& getEventCache(edm::StreamID const&) const final;
5555
std::vector<RandomEngineState> const& getLumiCache(edm::LuminosityBlockIndex const&) const final;
5656

57-
void consumes(edm::ConsumesCollector&& iC) const final;
57+
edm::EDConsumerBase* consumer() final;
5858

5959
/// For debugging purposes only.
6060
void print(std::ostream& os) const final;

0 commit comments

Comments
 (0)