Skip to content

Commit

Permalink
Set random number system in worker thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr15Jones committed Aug 17, 2021
1 parent e058dfb commit bb5bf39
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion SimG4Core/Application/plugins/OscarMTProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ namespace {
explicit StaticRandomEngineSetUnset(CLHEP::HepRandomEngine* engine);
~StaticRandomEngineSetUnset();

CLHEP::HepRandomEngine* currentEngine() { return m_currentEngine; }

private:
CLHEP::HepRandomEngine* m_currentEngine;
CLHEP::HepRandomEngine* m_previousEngine;
Expand All @@ -105,6 +107,7 @@ OscarMTProducer::OscarMTProducer(edm::ParameterSet const& p, const OscarMTMaster
auto token = edm::ServiceRegistry::instance().presentToken();
m_handoff.runAndWait([this, &p, token]() {
edm::ServiceRegistry::Operate guard{token};
StaticRandomEngineSetUnset random(nullptr);
m_runManagerWorker = std::make_unique<RunManagerMTWorker>(p, consumesCollector());
});
m_masterThread = ms;
Expand Down Expand Up @@ -228,6 +231,7 @@ void OscarMTProducer::endRun(const edm::Run&, const edm::EventSetup&) {
edm::LogVerbatim("SimG4CoreApplication") << "OscarMTProducer::endRun";
auto token = edm::ServiceRegistry::instance().presentToken();
m_handoff.runAndWait([this, token]() {
StaticRandomEngineSetUnset random(nullptr);
edm::ServiceRegistry::Operate guard{token};
m_runManagerWorker->endRun();
});
Expand All @@ -236,6 +240,7 @@ void OscarMTProducer::endRun(const edm::Run&, const edm::EventSetup&) {

void OscarMTProducer::produce(edm::Event& e, const edm::EventSetup& es) {
StaticRandomEngineSetUnset random(e.streamID());
auto engine = random.currentEngine();
edm::LogVerbatim("SimG4CoreApplication") << "Produce event " << e.id() << " stream " << e.streamID();
//edm::LogVerbatim("SimG4CoreApplication") << " rand= " << G4UniformRand();

Expand All @@ -245,8 +250,9 @@ void OscarMTProducer::produce(edm::Event& e, const edm::EventSetup& es) {
std::unique_ptr<G4SimEvent> evt;
try {
auto token = edm::ServiceRegistry::instance().presentToken();
m_handoff.runAndWait([this, &e, &es, &evt, token]() {
m_handoff.runAndWait([this, &e, &es, &evt, token, engine]() {
edm::ServiceRegistry::Operate guard{token};
StaticRandomEngineSetUnset random(engine);
evt = m_runManagerWorker->produce(e, es, globalCache()->runManagerMaster());
});
} catch (const SimG4Exception& simg4ex) {
Expand Down

0 comments on commit bb5bf39

Please sign in to comment.