Skip to content

Commit

Permalink
added MixMax random number generator
Browse files Browse the repository at this point in the history
  • Loading branch information
civanch committed Nov 16, 2017
1 parent 717f242 commit e79b467
Show file tree
Hide file tree
Showing 16 changed files with 292 additions and 99 deletions.
81 changes: 42 additions & 39 deletions IOMC/RandomEngine/python/IOMC_cff.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import FWCore.ParameterSet.Config as cms

FullSimEngine = cms.untracked.string('HepJamesRandom')
#FullSimEngine = cms.untracked.string('MixMaxRng')
FastSimEngine = cms.untracked.string('TRandom3')
RandomNumberGeneratorService = cms.Service("RandomNumberGeneratorService",
#
# seed for a source no longer needed - replaces by "generator"
Expand All @@ -10,11 +13,11 @@
# ),
externalLHEProducer = cms.PSet(
initialSeed = cms.untracked.uint32(234567),
engineName = cms.untracked.string('HepJamesRandom')
engineName = FullSimEngine
),
generator = cms.PSet(
initialSeed = cms.untracked.uint32(123456789),
engineName = cms.untracked.string('HepJamesRandom')
engineName = FullSimEngine
),
#
# EvtGenProducer discontinued, replaced by funtionalities in ExternalDecays,
Expand All @@ -26,55 +29,55 @@
# ),
VtxSmeared = cms.PSet(
initialSeed = cms.untracked.uint32(98765432),
engineName = cms.untracked.string('HepJamesRandom')
engineName = FullSimEngine
),
LHCTransport = cms.PSet(
initialSeed = cms.untracked.uint32(87654321),
engineName = cms.untracked.string('TRandom3')
engineName = FastSimEngine
),
hiSignalLHCTransport = cms.PSet(
initialSeed = cms.untracked.uint32(88776655),
engineName = cms.untracked.string('TRandom3')
engineName = FastSimEngine
),
g4SimHits = cms.PSet(
initialSeed = cms.untracked.uint32(11),
engineName = cms.untracked.string('HepJamesRandom')
engineName = FullSimEngine
),
mix = cms.PSet(
initialSeed = cms.untracked.uint32(12345),
engineName = cms.untracked.string('HepJamesRandom')
engineName = FullSimEngine
),
mixData = cms.PSet(
initialSeed = cms.untracked.uint32(12345),
engineName = cms.untracked.string('HepJamesRandom')
engineName = FullSimEngine
),
simSiStripDigiSimLink = cms.PSet(
initialSeed = cms.untracked.uint32(1234567),
engineName = cms.untracked.string('HepJamesRandom')
engineName = FullSimEngine
),
simMuonDTDigis = cms.PSet(
initialSeed = cms.untracked.uint32(1234567),
engineName = cms.untracked.string('HepJamesRandom')
engineName = FullSimEngine
),
simMuonCSCDigis = cms.PSet(
initialSeed = cms.untracked.uint32(11223344),
engineName = cms.untracked.string('HepJamesRandom')
engineName = FullSimEngine
),
simMuonRPCDigis = cms.PSet(
initialSeed = cms.untracked.uint32(1234567),
engineName = cms.untracked.string('HepJamesRandom')
engineName = FullSimEngine
),
#
# HI generation & simulation is a special processing/chain,
# integrated since 330 cycle
#
hiSignal = cms.PSet(
initialSeed = cms.untracked.uint32(123456789),
engineName = cms.untracked.string('HepJamesRandom')
engineName = FullSimEngine
),
hiSignalG4SimHits = cms.PSet(
initialSeed = cms.untracked.uint32(11),
engineName = cms.untracked.string('HepJamesRandom')
engineName = FullSimEngine
),

#
Expand All @@ -83,82 +86,82 @@
#
famosPileUp = cms.PSet(
initialSeed = cms.untracked.uint32(918273),
engineName = cms.untracked.string('TRandom3')
engineName = FastSimEngine
),

mixGenPU = cms.PSet(
initialSeed = cms.untracked.uint32(918273), # intentionally the same as famosPileUp
engineName = cms.untracked.string('TRandom3')
engineName = FastSimEngine
),

mixSimCaloHits = cms.PSet(
initialSeed = cms.untracked.uint32(918273),
engineName = cms.untracked.string('TRandom3')
engineName = FastSimEngine
),

mixRecoTracks = cms.PSet(
initialSeed = cms.untracked.uint32(918273),
engineName = cms.untracked.string('TRandom3')
engineName = FastSimEngine
),

famosSimHits = cms.PSet(
initialSeed = cms.untracked.uint32(13579),
engineName = cms.untracked.string('TRandom3')
engineName = FastSimEngine
),

fastTrackerRecHits = cms.PSet(
initialSeed = cms.untracked.uint32(24680),
engineName = cms.untracked.string('TRandom3')
engineName = FastSimEngine
),

ecalRecHit = cms.PSet(
initialSeed = cms.untracked.uint32(654321),
engineName = cms.untracked.string('TRandom3')
engineName = FastSimEngine
),

ecalPreshowerRecHit = cms.PSet(
initialSeed = cms.untracked.uint32(6541321),
engineName = cms.untracked.string('TRandom3')
engineName = FastSimEngine
),

hbhereco = cms.PSet(
initialSeed = cms.untracked.uint32(541321),
engineName = cms.untracked.string('TRandom3')
engineName = FastSimEngine
),

horeco = cms.PSet(
initialSeed = cms.untracked.uint32(541321),
engineName = cms.untracked.string('TRandom3')
engineName = FastSimEngine
),

hfreco = cms.PSet(
initialSeed = cms.untracked.uint32(541321),
engineName = cms.untracked.string('TRandom3')
engineName = FastSimEngine
),

paramMuons = cms.PSet(
initialSeed = cms.untracked.uint32(54525),
engineName = cms.untracked.string('TRandom3')
engineName = FastSimEngine
),

l1ParamMuons = cms.PSet(
initialSeed = cms.untracked.uint32(6453209),
engineName = cms.untracked.string('TRandom3')
engineName = FastSimEngine
),

MuonSimHits = cms.PSet(
initialSeed = cms.untracked.uint32(987346),
engineName = cms.untracked.string('TRandom3')
engineName = FastSimEngine
),
#CTPPS FastSim
CTPPSFastRecHits = cms.PSet(
initialSeed = cms.untracked.uint32(1357987),
engineName = cms.untracked.string('TRandom3')
engineName = FastSimEngine
),
# filter for simulated beam spot
simBeamSpotFilter = cms.PSet(
initialSeed = cms.untracked.uint32(87654321),
engineName = cms.untracked.string('HepJamesRandom')
engineName = FullSimEngine
)
# to save the status of the last event (useful for crashes)
,saveFileName = cms.untracked.string('')
Expand All @@ -175,46 +178,46 @@
from Configuration.Eras.Modifier_run2_GEM_2017_cff import run2_GEM_2017
run2_GEM_2017.toModify(RandomNumberGeneratorService, simMuonGEMDigis = cms.PSet(
initialSeed = cms.untracked.uint32(1234567),
engineName = cms.untracked.string('HepJamesRandom')) )
engineName = FullSimEngine) )

from Configuration.Eras.Modifier_run3_GEM_cff import run3_GEM
run3_GEM.toModify(
RandomNumberGeneratorService,
simMuonGEMDigis = cms.PSet(
initialSeed = cms.untracked.uint32(1234567),
engineName = cms.untracked.string('HepJamesRandom'))
engineName = FullSimEngine)
)

from Configuration.Eras.Modifier_phase2_muon_cff import phase2_muon
phase2_muon.toModify(
RandomNumberGeneratorService,
simMuonME0Digis = cms.PSet(
initialSeed = cms.untracked.uint32(1234567),
engineName = cms.untracked.string('HepJamesRandom')),
engineName = FullSimEngine),
simMuonME0PseudoDigis = cms.PSet(
initialSeed = cms.untracked.uint32(1234567),
engineName = cms.untracked.string('HepJamesRandom')),
engineName = FullSimEngine),
simMuonME0PseudoReDigis = cms.PSet(
initialSeed = cms.untracked.uint32(7654321),
engineName = cms.untracked.string('HepJamesRandom')),
engineName = FullSimEngine),
simMuonME0PseudoReDigisCoarse = cms.PSet(
initialSeed = cms.untracked.uint32(2234567),
engineName = cms.untracked.string('HepJamesRandom')),
engineName = FullSimEngine),
)

from Configuration.Eras.Modifier_phase2_timing_cff import phase2_timing
phase2_timing.toModify(
RandomNumberGeneratorService,
trackTimeValueMapProducer = cms.PSet(
initialSeed = cms.untracked.uint32(1234567),
engineName = cms.untracked.string('HepJamesRandom')
engineName = FullSimEngine
),
gsfTrackTimeValueMapProducer = cms.PSet(
initialSeed = cms.untracked.uint32(1234567),
engineName = cms.untracked.string('HepJamesRandom')
engineName = FullSimEngine
),
ecalBarrelClusterFastTimer = cms.PSet(
initialSeed = cms.untracked.uint32(1234567),
engineName = cms.untracked.string('HepJamesRandom')
engineName = FullSimEngine
)
)
57 changes: 45 additions & 12 deletions IOMC/RandomEngine/src/RandomNumberGeneratorService.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "CLHEP/Random/engineIDulong.h"
#include "CLHEP/Random/JamesRandom.h"
#include "CLHEP/Random/RanecuEngine.h"
#include "CLHEP/Random/MixMaxRng.h"

#include <algorithm>
#include <cassert>
Expand Down Expand Up @@ -140,7 +141,7 @@ namespace edm {
if(initialSeedSet[0] > maxSeedRanecu ||
initialSeedSet[1] > maxSeedRanecu) { // They need to fit in a 31 bit integer
throw Exception(errors::Configuration)
<< "The RanecuEngine seeds should be in the range 0 to 2147483647.\n"
<< "The RanecuEngine seeds should be in the range 0 to " << maxSeedRanecu << ".\n"
<< "The seeds passed to the RandomNumberGenerationService from the\n"
"configuration file were " << initialSeedSet[0] << " and " << initialSeedSet[1]
<< "\nThis was for the module with label \"" << label << "\".\n";
Expand All @@ -150,20 +151,36 @@ namespace edm {
else {
if(initialSeedSet.size() != 1U) {
throw Exception(errors::Configuration)
<< "Random engines of type \"HepJamesRandom\" and \"TRandom3\n"
<< "Random engines of type \"HepJamesRandom\", \"TRandom3\" and \"MixMaxRng\" \n"
<< "require exactly 1 seed be specified in the configuration.\n"
<< "There were " << initialSeedSet.size() << " seeds set for the\n"
<< "module with label \"" << label << "\".\n" ;
}
if(engineName == "HepJamesRandom") {
if(initialSeedSet[0] > maxSeedHepJames) {
throw Exception(errors::Configuration)
<< "The CLHEP::HepJamesRandom engine seed should be in the range 0 to 900000000.\n"
<< "The CLHEP::HepJamesRandom engine seed should be in the range 0 to " << maxSeedHepJames <<".\n"
<< "The seed passed to the RandomNumberGenerationService from the\n"
"configuration file was " << initialSeedSet[0] << ". This was for \n"
<< "the module with label " << label << ".\n";
}
} else if(engineName != "TRandom3") {
} else if(engineName == "MixMaxRng") {
if(initialSeedSet[0] > maxSeedTRandom3) {
throw Exception(errors::Configuration)
<< "The CLHEP::MixMaxRng engine seed should be in the range 0 to " << maxSeedTRandom3 << ".\n"
<< "The seed passed to the RandomNumberGenerationService from the\n"
"configuration file was " << initialSeedSet[0] << ". This was for \n"
<< "the module with label " << label << ".\n";
}
} else if(engineName == "TRandom3") {
if(initialSeedSet[0] > maxSeedTRandom3) {
throw Exception(errors::Configuration)
<< "The CLHEP::MixMaxRng engine seed should be in the range 0 to " << maxSeedTRandom3 << ".\n"
<< "The seed passed to the RandomNumberGenerationService from the\n"
"configuration file was " << initialSeedSet[0] << ". This was for \n"
<< "the module with label " << label << ".\n";
}
} else {
throw Exception(errors::Configuration)
<< "The random engine name, \"" << engineName
<< "\", does not correspond to a supported engine.\n"
Expand Down Expand Up @@ -557,6 +574,8 @@ namespace edm {
os << " " << i.engine()->name();
if(i.engine()->name() == std::string("HepJamesRandom")) {
os << " " << i.engine()->getSeed();
} else if(i.engine()->name() == std::string("MixMaxRng")) {
os << " " << i.engine()->getSeed();
} else {
os << " engine does not know seeds";
}
Expand All @@ -576,6 +595,8 @@ namespace edm {
os << " " << i.engine()->name();
if(i.engine()->name() == std::string("HepJamesRandom")) {
os << " " << i.engine()->getSeed();
} else if(i.engine()->name() == std::string("MixMaxRng")) {
os << " " << i.engine()->getSeed();
} else {
os << " engine does not know seeds";
}
Expand Down Expand Up @@ -754,6 +775,15 @@ namespace edm {

labelAndEngine->setSeed(engineSeeds[0], 0);
labelAndEngine->setSeed(engineSeeds[1], 1);
} else if(engineStateL[0] == CLHEP::engineIDulong<CLHEP::MixMaxRng>()) {

checkEngineType(engine->name(), std::string("MixMaxRng"), engineLabel);

// This line actually restores the engine state.
engine->setSeed(engineSeedsL[0], 0);
engine->get(engineStateL);

labelAndEngine->setSeed(engineSeeds[0], 0);
} else if(engineStateL[0] == CLHEP::engineIDulong<TRandomAdaptor>()) {

checkEngineType(engine->name(), std::string("TRandom3"), engineLabel);
Expand Down Expand Up @@ -841,17 +871,14 @@ namespace edm {
void
RandomNumberGeneratorService::writeStates(std::vector<RandomEngineState> const& v,
std::ofstream& outFile) {
for(std::vector<RandomEngineState>::const_iterator iter = v.begin(),
iEnd = v.end();
iter != iEnd; ++iter) {

std::vector<std::uint32_t> const& seedVector = iter->getSeed();
for(auto & state : v) {
std::vector<std::uint32_t> const& seedVector = state.getSeed();
std::vector<std::uint32_t>::size_type seedVectorLength = seedVector.size();

std::vector<std::uint32_t> const& stateVector = iter->getState();
std::vector<std::uint32_t> const& stateVector = state.getState();
std::vector<std::uint32_t>::size_type stateVectorLength = stateVector.size();

outFile << "<ModuleLabel>\n" << iter->getLabel() << "\n</ModuleLabel>\n";
outFile << "<ModuleLabel>\n" << state.getLabel() << "\n</ModuleLabel>\n";

outFile << "<SeedLength>\n" << seedVectorLength << "\n</SeedLength>\n" ;
outFile << "<InitialSeeds>\n";
Expand Down Expand Up @@ -1120,6 +1147,12 @@ namespace edm {
if(seedOffset != 0 || eventSeedOffset != 0) {
resetEngineSeeds(engines.back(), name, seeds, seedOffset, eventSeedOffset);
}
} else if(name == "MixMaxRng") {
std::shared_ptr<CLHEP::HepRandomEngine> engine = std::make_shared<CLHEP::MixMaxRng>(seedL);
engines.emplace_back(label, seeds, engine);
if(seedOffset != 0 || eventSeedOffset != 0) {
resetEngineSeeds(engines.back(), name, seeds, seedOffset, eventSeedOffset);
}
} else { // TRandom3, currently the only other possibility

// There is a dangerous conversion from std::uint32_t to long
Expand Down Expand Up @@ -1169,7 +1202,7 @@ namespace edm {
} else {
assert(seeds.size() == 1U);

if(engineName == "HepJamesRandom") {
if(engineName == "HepJamesRandom" || engineName == "MixMaxRng") {
// Wrap around if the offsets push the seed over the maximum allowed value
std::uint32_t mod = maxSeedHepJames + 1U;
offset1 %= mod;
Expand Down
Loading

0 comments on commit e79b467

Please sign in to comment.