Skip to content

Commit

Permalink
Added edm::cloneEngine function
Browse files Browse the repository at this point in the history
Share the implementation across RandomNumberGenerators
  • Loading branch information
Dr15Jones committed Dec 2, 2022
1 parent dfea1de commit cbdb75c
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 53 deletions.
30 changes: 30 additions & 0 deletions IOMC/RandomEngine/interface/cloneEngine.h
@@ -0,0 +1,30 @@
#ifndef IOMC_RandomEngine_cloneEngine_h
#define IOMC_RandomEngine_cloneEngine_h
// -*- C++ -*-
//
// Package: IOMC/RandomEngine
// Class : cloneEngine
//
/**\function cloneEngine cloneEngine.h "IOMC/RandomEngine/interface/cloneEngine.h"
Description: Function used to clone a random number engine
Usage:
<usage>
*/
//
// Original Author: Christopher Jones
// Created: Fri, 02 Dec 2022 19:32:10 GMT
//
#include <memory>

namespace CLHEP {
class HepRandomEngine;
}

namespace edm {
std::unique_ptr<CLHEP::HepRandomEngine> cloneEngine(CLHEP::HepRandomEngine const&);
};

#endif
21 changes: 2 additions & 19 deletions IOMC/RandomEngine/plugins/RandomNumberGeneratorService.cc
Expand Up @@ -35,6 +35,7 @@
#include "FWCore/Utilities/interface/LuminosityBlockIndex.h"
#include "FWCore/Utilities/interface/StreamID.h"
#include "IOMC/RandomEngine/interface/TRandomAdaptor.h"
#include "IOMC/RandomEngine/interface/cloneEngine.h"
#include "SimDataFormats/RandomEngine/interface/RandomEngineState.h"
#include "SimDataFormats/RandomEngine/interface/RandomEngineStates.h"

Expand Down Expand Up @@ -289,25 +290,7 @@ namespace edm {

std::unique_ptr<CLHEP::HepRandomEngine> RandomNumberGeneratorService::cloneEngine(
LuminosityBlockIndex const& lumiIndex) {
CLHEP::HepRandomEngine& existingEngine = getEngine(lumiIndex);

std::vector<unsigned long> stateL = existingEngine.put();
long seedL = existingEngine.getSeed();
std::unique_ptr<CLHEP::HepRandomEngine> newEngine;
if (stateL[0] == CLHEP::engineIDulong<CLHEP::HepJamesRandom>()) {
newEngine = std::make_unique<CLHEP::HepJamesRandom>(seedL);
} else if (stateL[0] == CLHEP::engineIDulong<CLHEP::RanecuEngine>()) {
newEngine = std::make_unique<CLHEP::RanecuEngine>();
} else if (stateL[0] == CLHEP::engineIDulong<CLHEP::MixMaxRng>()) {
newEngine = std::make_unique<CLHEP::MixMaxRng>(seedL);
} else if (stateL[0] == CLHEP::engineIDulong<TRandomAdaptor>()) {
newEngine = std::make_unique<TRandomAdaptor>(seedL);
} else {
// Sanity check, it should not be possible for this to happen.
throw Exception(errors::Unknown) << "The RandomNumberGeneratorService is trying to clone unknown engine type\n";
}
newEngine->get(stateL);
return newEngine;
return edm::cloneEngine(getEngine(lumiIndex));
}

// PROBABLY TO BE DELETED, This returns the configured seed without
Expand Down
46 changes: 46 additions & 0 deletions IOMC/RandomEngine/src/cloneEngine.cc
@@ -0,0 +1,46 @@
// -*- C++ -*-
//
// Package: IOMC/RandomEngine
// Class : cloneEngine
//
// Implementation:
// [Notes on implementation]
//
// Original Author: Christopher Jones
// Created: Fri, 02 Dec 2022 19:34:37 GMT
//

// system include files

// user include files
#include "IOMC/RandomEngine/interface/cloneEngine.h"
#include "IOMC/RandomEngine/interface/TRandomAdaptor.h"

#include "FWCore/Utilities/interface/EDMException.h"

#include "CLHEP/Random/engineIDulong.h"
#include "CLHEP/Random/JamesRandom.h"
#include "CLHEP/Random/RanecuEngine.h"
#include "CLHEP/Random/MixMaxRng.h"

namespace edm {
std::unique_ptr<CLHEP::HepRandomEngine> cloneEngine(CLHEP::HepRandomEngine const& existingEngine) {
std::vector<unsigned long> stateL = existingEngine.put();
long seedL = existingEngine.getSeed();
std::unique_ptr<CLHEP::HepRandomEngine> newEngine;
if (stateL[0] == CLHEP::engineIDulong<CLHEP::HepJamesRandom>()) {
newEngine = std::make_unique<CLHEP::HepJamesRandom>(seedL);
} else if (stateL[0] == CLHEP::engineIDulong<CLHEP::RanecuEngine>()) {
newEngine = std::make_unique<CLHEP::RanecuEngine>();
} else if (stateL[0] == CLHEP::engineIDulong<CLHEP::MixMaxRng>()) {
newEngine = std::make_unique<CLHEP::MixMaxRng>(seedL);
} else if (stateL[0] == CLHEP::engineIDulong<TRandomAdaptor>()) {
newEngine = std::make_unique<TRandomAdaptor>(seedL);
} else {
// Sanity check, it should not be possible for this to happen.
throw Exception(errors::Unknown) << "The RandomNumberGeneratorService is trying to clone unknown engine type\n";
}
newEngine->get(stateL);
return newEngine;
}
}; // namespace edm
40 changes: 11 additions & 29 deletions Mixing/Base/src/PileupRandomNumberGenerator.cc
Expand Up @@ -20,11 +20,15 @@
#include "FWCore/Utilities/interface/StreamID.h"
#include "DataFormats/Provenance/interface/ModuleDescription.h"

#include "CLHEP/Random/engineIDulong.h"
#include "CLHEP/Random/JamesRandom.h"
#include "CLHEP/Random/RanecuEngine.h"
#include "CLHEP/Random/MixMaxRng.h"
#include "IOMC/RandomEngine/interface/TRandomAdaptor.h"
#include "IOMC/RandomEngine/interface/cloneEngine.h"

#include "CLHEP/Random/RandomEngine.h"

PileupRandomNumberGenerator::PileupRandomNumberGenerator(std::vector<std::string> const& iModuleLabels) : m_seed(0) {
for (auto const& name : iModuleLabels) {
m_modulesToEngines.emplace(name, std::unique_ptr<CLHEP::HepRandomEngine>{});
}
}

CLHEP::HepRandomEngine& PileupRandomNumberGenerator::getEngine(edm::StreamID const&) {
return *m_modulesToEngines[findPresentModule()];
Expand All @@ -36,14 +40,14 @@ CLHEP::HepRandomEngine& PileupRandomNumberGenerator::getEngine(edm::LuminosityBl

std::unique_ptr<CLHEP::HepRandomEngine> PileupRandomNumberGenerator::cloneEngine(edm::LuminosityBlockIndex const&) {
auto& engine = m_modulesToEngines[findPresentModule()];
return cloneEngine(*engine);
return edm::cloneEngine(*engine);
}

void PileupRandomNumberGenerator::setSeed(uint32_t iSeed) { m_seed = iSeed; }

void PileupRandomNumberGenerator::setEngine(CLHEP::HepRandomEngine const& iEngine) {
for (auto& v : m_modulesToEngines) {
v.second = cloneEngine(iEngine);
v.second = edm::cloneEngine(iEngine);
}
}

Expand All @@ -67,28 +71,6 @@ void PileupRandomNumberGenerator::consumes(edm::ConsumesCollector&& iC) const {}

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

std::unique_ptr<CLHEP::HepRandomEngine> PileupRandomNumberGenerator::cloneEngine(
CLHEP::HepRandomEngine const& existingEngine) {
std::vector<unsigned long> stateL = existingEngine.put();
long seedL = existingEngine.getSeed();
std::unique_ptr<CLHEP::HepRandomEngine> newEngine;
if (stateL[0] == CLHEP::engineIDulong<CLHEP::HepJamesRandom>()) {
newEngine = std::make_unique<CLHEP::HepJamesRandom>(seedL);
} else if (stateL[0] == CLHEP::engineIDulong<CLHEP::RanecuEngine>()) {
newEngine = std::make_unique<CLHEP::RanecuEngine>();
} else if (stateL[0] == CLHEP::engineIDulong<CLHEP::MixMaxRng>()) {
newEngine = std::make_unique<CLHEP::MixMaxRng>(seedL);
} else if (stateL[0] == CLHEP::engineIDulong<edm::TRandomAdaptor>()) {
newEngine = std::make_unique<edm::TRandomAdaptor>(seedL);
} else {
// Sanity check, it should not be possible for this to happen.
throw edm::Exception(edm::errors::Unknown)
<< "The RandomNumberGeneratorService is trying to clone unknown engine type\n";
}
newEngine->get(stateL);
return newEngine;
}

const std::string& PileupRandomNumberGenerator::findPresentModule() const {
edm::ModuleCallingContext const* mcc = edm::CurrentModuleOnThread::getCurrentModuleOnThread();
if (mcc == nullptr) {
Expand Down
6 changes: 1 addition & 5 deletions Mixing/Base/src/PileupRandomNumberGenerator.h
Expand Up @@ -31,11 +31,7 @@

class PileupRandomNumberGenerator : public edm::RandomNumberGenerator {
public:
PileupRandomNumberGenerator(std::vector<std::string> const& iModuleLabels) : m_seed(0) {
for (auto const& name : iModuleLabels) {
m_modulesToEngines.emplace(name, std::unique_ptr<CLHEP::HepRandomEngine>{});
}
}
PileupRandomNumberGenerator(std::vector<std::string> const& iModuleLabels);

void setSeed(uint32_t iSeed);
void setEngine(CLHEP::HepRandomEngine const&);
Expand Down

0 comments on commit cbdb75c

Please sign in to comment.