CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Classes | Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Private Attributes | Static Private Attributes
edm::service::RandomNumberGeneratorService Class Reference

#include <RandomNumberGeneratorService.h>

Inheritance diagram for edm::service::RandomNumberGeneratorService:
edm::RandomNumberGenerator

Classes

class  LabelAndEngine
 
class  ModuleIDToEngine
 
class  SeedsAndName
 

Public Member Functions

virtual CLHEP::HepRandomEngine & getEngine (StreamID const &streamID) const override
 Use this engine in event methods. More...
 
virtual CLHEP::HepRandomEngine & getEngine (LuminosityBlockIndex const &luminosityBlockIndex) const override
 Use this engine in the global begin luminosity block method. More...
 
virtual std::vector
< RandomEngineState > const & 
getEventCache (StreamID const &) const override
 
virtual std::vector
< RandomEngineState > const & 
getLumiCache (LuminosityBlockIndex const &) const override
 These two are used by the RandomEngineStateProducer. More...
 
virtual std::uint32_t mySeed () const override
 
virtual void postEventRead (Event const &event) override
 
void postForkReacquireResources (unsigned childIndex, unsigned kMaxChildren)
 
void postModuleBeginJob (ModuleDescription const &description)
 
void postModuleBeginStream (StreamContext const &sc, ModuleCallingContext const &mcc)
 
void postModuleConstruction (ModuleDescription const &description)
 
void postModuleEndJob (ModuleDescription const &description)
 
void postModuleEndStream (StreamContext const &sc, ModuleCallingContext const &mcc)
 
void postModuleStreamBeginLumi (StreamContext const &sc, ModuleCallingContext const &mcc)
 
void postModuleStreamBeginRun (StreamContext const &sc, ModuleCallingContext const &mcc)
 
void postModuleStreamEndLumi (StreamContext const &sc, ModuleCallingContext const &mcc)
 
void postModuleStreamEndRun (StreamContext const &sc, ModuleCallingContext const &mcc)
 
void preallocate (SystemBounds const &)
 
virtual void preBeginLumi (LuminosityBlock const &lumi) override
 
void preModuleBeginJob (ModuleDescription const &description)
 
void preModuleBeginStream (StreamContext const &sc, ModuleCallingContext const &mcc)
 
void preModuleConstruction (ModuleDescription const &description)
 
void preModuleEndJob (ModuleDescription const &description)
 
void preModuleEndStream (StreamContext const &sc, ModuleCallingContext const &mcc)
 
void preModuleStreamBeginLumi (StreamContext const &sc, ModuleCallingContext const &mcc)
 
void preModuleStreamBeginRun (StreamContext const &sc, ModuleCallingContext const &mcc)
 
void preModuleStreamEndLumi (StreamContext const &sc, ModuleCallingContext const &mcc)
 
void preModuleStreamEndRun (StreamContext const &sc, ModuleCallingContext const &mcc)
 
virtual void print (std::ostream &os) const override
 For debugging. More...
 
 RandomNumberGeneratorService (ParameterSet const &pset, ActivityRegistry &activityRegistry)
 
virtual ~RandomNumberGeneratorService ()
 
- Public Member Functions inherited from edm::RandomNumberGenerator
 RandomNumberGenerator ()
 
virtual ~RandomNumberGenerator ()
 

Static Public Member Functions

static void fillDescriptions (ConfigurationDescriptions &descriptions)
 

Private Types

typedef std::vector
< std::uint32_t > 
VUint32
 

Private Member Functions

void checkEngineType (std::string const &typeFromConfig, std::string const &typeFromEvent, std::string const &engineLabel) const
 
std::string constructSaveFileName () const
 
void createEnginesInVector (std::vector< LabelAndEngine > &engines, unsigned int seedOffset, unsigned int eventSeedOffset, std::vector< ModuleIDToEngine > &moduleIDVector)
 
RandomNumberGeneratorService
const & 
operator= (RandomNumberGeneratorService const &)=delete
 
void postModuleStreamCheck (StreamContext const &sc, ModuleCallingContext const &mcc)
 
void preModuleStreamCheck (StreamContext const &sc, ModuleCallingContext const &mcc)
 
 RandomNumberGeneratorService (RandomNumberGeneratorService const &)=delete
 
bool readEngineState (std::istream &is, std::vector< RandomEngineState > &cache, std::string const &whichStates, bool &saveToCache)
 
void readEventStatesFromTextFile (std::string const &fileName, std::vector< RandomEngineState > &cache)
 
void readFromEvent (Event const &event)
 
void readFromLuminosityBlock (LuminosityBlock const &lumi)
 
void readLumiStatesFromTextFile (std::string const &fileName, std::vector< RandomEngineState > &cache)
 
void readStatesFromFile (std::string const &fileName, std::vector< RandomEngineState > &cache, std::string const &whichStates)
 
void readVector (std::istream &is, unsigned numItems, std::vector< std::uint32_t > &v)
 
void resetEngineSeeds (LabelAndEngine &labelAndEngine, std::string const &engineName, VUint32 const &seeds, std::uint32_t offset1, std::uint32_t offset2)
 
void restoreFromCache (std::vector< RandomEngineState > const &cache, std::vector< LabelAndEngine > &engines)
 
void saveStatesToFile (std::string const &fileName, StreamID const &streamID, LuminosityBlockIndex const &lumiIndex)
 
void snapShot (std::vector< LabelAndEngine > const &engines, std::vector< RandomEngineState > &cache)
 
void writeStates (std::vector< RandomEngineState > const &v, std::ofstream &outFile)
 
void writeVector (VUint32 const &v, std::ofstream &outFile)
 

Private Attributes

unsigned childIndex_
 
bool enableChecking_
 
std::vector< std::vector
< RandomEngineState > > 
eventCache_
 
std::uint32_t eventSeedOffset_
 
std::vector< std::vector
< RandomEngineState > > 
lumiCache_
 
std::vector< std::vector
< LabelAndEngine > > 
lumiEngines_
 
std::vector< std::vector
< ModuleIDToEngine > > 
lumiModuleIDToEngine_
 
unsigned int nStreams_
 
std::vector< std::shared_ptr
< std::ofstream > > 
outFiles_
 
std::string restoreFileName_
 
edm::InputTag restoreStateBeginLumiTag_
 
edm::InputTag restoreStateTag_
 
std::string saveFileName_
 
std::atomic< bool > saveFileNameRecorded_
 
std::map< std::string,
SeedsAndName
seedsAndNameMap_
 
std::vector< std::vector
< LabelAndEngine > > 
streamEngines_
 
std::vector< std::vector
< ModuleIDToEngine > > 
streamModuleIDToEngine_
 
bool verbose_
 

Static Private Attributes

static const std::uint32_t maxSeedHepJames = 900000000U
 
static const std::uint32_t maxSeedRanecu = 2147483647U
 
static const std::vector
< std::uint32_t >::size_type 
maxSeeds = 65536U
 
static const std::uint32_t maxSeedTRandom3 = 4294967295U
 
static const std::vector
< std::uint32_t >::size_type 
maxStates = 65536U
 
static thread_local std::string moduleLabel_
 

Detailed Description

Description: Manages random number engines for modules

Usage: See comments in base class, FWCore/Utilities/RandomNumberGenerator.h

Author
Chris Jones and W. David Dagenhart, created March 7, 2006 (originally in FWCore/Services)

Definition at line 50 of file RandomNumberGeneratorService.h.

Member Typedef Documentation

typedef std::vector<std::uint32_t> edm::service::RandomNumberGeneratorService::VUint32
private

Definition at line 125 of file RandomNumberGeneratorService.h.

Constructor & Destructor Documentation

edm::service::RandomNumberGeneratorService::RandomNumberGeneratorService ( ParameterSet const &  pset,
ActivityRegistry activityRegistry 
)

Definition at line 63 of file RandomNumberGeneratorService.cc.

References edm::errors::Configuration, enableChecking_, Exception, edm::ParameterSet::exists(), edm::ParameterSet::getParameterNamesForType(), edm::ParameterSet::getParameterSet(), edm::ParameterSet::getUntrackedParameter(), HLT_25ns14e33_v1_cff::InputTag, edm::InputTag::kSkipCurrentProcess, diffTwoXMLs::label, edm::InputTag::label(), maxSeedHepJames, maxSeedRanecu, postForkReacquireResources(), postModuleBeginJob(), postModuleBeginStream(), postModuleConstruction(), postModuleEndJob(), postModuleEndStream(), postModuleStreamBeginLumi(), postModuleStreamBeginRun(), postModuleStreamEndLumi(), postModuleStreamEndRun(), preallocate(), preModuleBeginJob(), preModuleBeginStream(), preModuleConstruction(), preModuleEndJob(), preModuleEndStream(), preModuleStreamBeginLumi(), preModuleStreamBeginRun(), preModuleStreamEndLumi(), preModuleStreamEndRun(), edm::InputTag::process(), restoreFileName_, restoreStateBeginLumiTag_, restoreStateTag_, saveFileName_, seedsAndNameMap_, AlCaHLTBitMon_QueryRunRegistry::string, edm::ActivityRegistry::watchPostForkReacquireResources(), edm::ActivityRegistry::watchPostModuleBeginJob(), edm::ActivityRegistry::watchPostModuleBeginStream(), edm::ActivityRegistry::watchPostModuleConstruction(), edm::ActivityRegistry::watchPostModuleEndJob(), edm::ActivityRegistry::watchPostModuleEndStream(), edm::ActivityRegistry::watchPostModuleStreamBeginLumi(), edm::ActivityRegistry::watchPostModuleStreamBeginRun(), edm::ActivityRegistry::watchPostModuleStreamEndLumi(), edm::ActivityRegistry::watchPostModuleStreamEndRun(), edm::ActivityRegistry::watchPreallocate(), edm::ActivityRegistry::watchPreModuleBeginJob(), edm::ActivityRegistry::watchPreModuleBeginStream(), edm::ActivityRegistry::watchPreModuleConstruction(), edm::ActivityRegistry::watchPreModuleEndJob(), edm::ActivityRegistry::watchPreModuleEndStream(), edm::ActivityRegistry::watchPreModuleStreamBeginLumi(), edm::ActivityRegistry::watchPreModuleStreamBeginRun(), edm::ActivityRegistry::watchPreModuleStreamEndLumi(), and edm::ActivityRegistry::watchPreModuleStreamEndRun().

64  :
65  nStreams_(0),
66  saveFileName_(pset.getUntrackedParameter<std::string>("saveFileName")),
67  saveFileNameRecorded_(false),
68  restoreFileName_(pset.getUntrackedParameter<std::string>("restoreFileName")),
69  enableChecking_(pset.getUntrackedParameter<bool>("enableChecking")),
70  childIndex_(0U),
71  eventSeedOffset_(pset.getUntrackedParameter<unsigned>("eventSeedOffset")),
72  verbose_(pset.getUntrackedParameter<bool>("verbose")) {
73 
74  if(pset.exists("restoreStateTag")) {
75  restoreStateTag_ = pset.getUntrackedParameter<edm::InputTag>("restoreStateTag");
76  if(restoreStateTag_.process() == "") {
78  }
79  } else {
80  restoreStateTag_ = edm::InputTag(pset.getUntrackedParameter<std::string>("restoreStateLabel"), "", edm::InputTag::kSkipCurrentProcess);
81  }
83 
84  if(!restoreFileName_.empty() && !restoreStateTag_.label().empty()) {
86  << "In the configuration for the RandomNumberGeneratorService both\n"
87  << "restoreFileName and restoreStateLabel were set to nonempty values\n"
88  << "which is illegal. It is impossible to restore the random engine\n"
89  << "states two different ways in the same process.\n";
90  }
91 
92  // The saveFileName must correspond to a file name without any path specification.
93  // Throw if that is not true.
94  if(!saveFileName_.empty() && (saveFileName_.find("/") != std::string::npos)) {
96  << "The saveFileName parameter must be a simple file name with no path\n"
97  << "specification. In the configuration, it was given the value \""
98  << saveFileName_ << "\"\n";
99  }
100 
101  std::uint32_t initialSeed;
102  VUint32 initialSeedSet;
103  std::string engineName;
104 
105  std::vector<std::string> pSets = pset.getParameterNamesForType<ParameterSet>();
106  for(auto const& label : pSets) {
107 
108  ParameterSet const& modulePSet = pset.getParameterSet(label);
109  engineName = modulePSet.getUntrackedParameter<std::string>("engineName", std::string("HepJamesRandom"));
110 
111  bool initialSeedExists = modulePSet.exists("initialSeed");
112  bool initialSeedSetExists = modulePSet.exists("initialSeedSet");
113 
114  if(initialSeedExists && initialSeedSetExists) {
116  << "For the module with the label \"" << label << "\",\n"
117  << "both the parameters \"initialSeed\" and \"initialSeedSet\"\n"
118  << "have been set in the configuration. You must set one or\n"
119  << "the other. It is illegal to set both.\n";
120  } else if(!initialSeedExists && !initialSeedSetExists) {
122  << "For the module with the label \"" << label << "\",\n"
123  << "neither the parameter \"initialSeed\" nor \"initialSeedSet\"\n"
124  << "has been set in the configuration. You must set one or\n"
125  << "the other.\n";
126  } else if(initialSeedExists) {
127  initialSeed = modulePSet.getUntrackedParameter<std::uint32_t>("initialSeed");
128  initialSeedSet.clear();
129  initialSeedSet.push_back(initialSeed);
130  } else if(initialSeedSetExists) {
131  initialSeedSet = modulePSet.getUntrackedParameter<VUint32>("initialSeedSet");
132  }
133  seedsAndNameMap_.insert(std::pair<std::string, SeedsAndName>(label, SeedsAndName(initialSeedSet, engineName)));
134 
135  // For the CLHEP::RanecuEngine case, require a seed set containing exactly two seeds.
136  if(engineName == std::string("RanecuEngine")) {
137  if(initialSeedSet.size() != 2U) {
139  << "Random engines of type \"RanecuEngine\" require 2 seeds\n"
140  << "be specified with the parameter named \"initialSeedSet\".\n"
141  << "Either \"initialSeedSet\" was not in the configuration\n"
142  << "or its size was not 2 for the module with label \"" << label << "\".\n" ;
143  }
144  if(initialSeedSet[0] > maxSeedRanecu ||
145  initialSeedSet[1] > maxSeedRanecu) { // They need to fit in a 31 bit integer
147  << "The RanecuEngine seeds should be in the range 0 to 2147483647.\n"
148  << "The seeds passed to the RandomNumberGenerationService from the\n"
149  "configuration file were " << initialSeedSet[0] << " and " << initialSeedSet[1]
150  << "\nThis was for the module with label \"" << label << "\".\n";
151  }
152  }
153  // For the other engines, one seed is required
154  else {
155  if(initialSeedSet.size() != 1U) {
157  << "Random engines of type \"HepJamesRandom\" and \"TRandom3\n"
158  << "require exactly 1 seed be specified in the configuration.\n"
159  << "There were " << initialSeedSet.size() << " seeds set for the\n"
160  << "module with label \"" << label << "\".\n" ;
161  }
162  if(engineName == "HepJamesRandom") {
163  if(initialSeedSet[0] > maxSeedHepJames) {
165  << "The CLHEP::HepJamesRandom engine seed should be in the range 0 to 900000000.\n"
166  << "The seed passed to the RandomNumberGenerationService from the\n"
167  "configuration file was " << initialSeedSet[0] << ". This was for \n"
168  << "the module with label " << label << ".\n";
169  }
170  } else if(engineName != "TRandom3") {
172  << "The random engine name, \"" << engineName
173  << "\", does not correspond to a supported engine.\n"
174  << "This engine was configured for the module with label \"" << label << "\"";
175  }
176  }
177  }
178  activityRegistry.watchPreModuleConstruction(this, &RandomNumberGeneratorService::preModuleConstruction);
179 
180  activityRegistry.watchPreallocate(this, &RandomNumberGeneratorService::preallocate);
181 
182  activityRegistry.watchPostForkReacquireResources(this, &RandomNumberGeneratorService::postForkReacquireResources);
183 
184  if(enableChecking_) {
185 
186  activityRegistry.watchPreModuleBeginStream(this, &RandomNumberGeneratorService::preModuleBeginStream);
187  activityRegistry.watchPostModuleBeginStream(this, &RandomNumberGeneratorService::postModuleBeginStream);
188 
189  activityRegistry.watchPreModuleEndStream(this, &RandomNumberGeneratorService::preModuleEndStream);
190  activityRegistry.watchPostModuleEndStream(this, &RandomNumberGeneratorService::postModuleEndStream);
191 
192  activityRegistry.watchPreModuleStreamBeginRun(this, &RandomNumberGeneratorService::preModuleStreamBeginRun);
193  activityRegistry.watchPostModuleStreamBeginRun(this, &RandomNumberGeneratorService::postModuleStreamBeginRun);
194 
195  activityRegistry.watchPreModuleStreamEndRun(this, &RandomNumberGeneratorService::preModuleStreamEndRun);
196  activityRegistry.watchPostModuleStreamEndRun(this, &RandomNumberGeneratorService::postModuleStreamEndRun);
197 
198  activityRegistry.watchPreModuleStreamBeginLumi(this, &RandomNumberGeneratorService::preModuleStreamBeginLumi);
199  activityRegistry.watchPostModuleStreamBeginLumi(this, &RandomNumberGeneratorService::postModuleStreamBeginLumi);
200 
201  activityRegistry.watchPreModuleStreamEndLumi(this, &RandomNumberGeneratorService::preModuleStreamEndLumi);
202  activityRegistry.watchPostModuleStreamEndLumi(this, &RandomNumberGeneratorService::postModuleStreamEndLumi);
203  }
204 
205  // The next 5 lines support the mySeed function
206  // DELETE THEM when/if that function is deleted.
207  activityRegistry.watchPostModuleConstruction(this, &RandomNumberGeneratorService::postModuleConstruction);
208  activityRegistry.watchPreModuleBeginJob(this, &RandomNumberGeneratorService::preModuleBeginJob);
209  activityRegistry.watchPostModuleBeginJob(this, &RandomNumberGeneratorService::postModuleBeginJob);
210  activityRegistry.watchPreModuleEndJob(this, &RandomNumberGeneratorService::preModuleEndJob);
211  activityRegistry.watchPostModuleEndJob(this, &RandomNumberGeneratorService::postModuleEndJob);
212  }
void preModuleStreamEndLumi(StreamContext const &sc, ModuleCallingContext const &mcc)
void postModuleEndJob(ModuleDescription const &description)
void preModuleEndStream(StreamContext const &sc, ModuleCallingContext const &mcc)
void preModuleConstruction(ModuleDescription const &description)
void postModuleStreamEndRun(StreamContext const &sc, ModuleCallingContext const &mcc)
void preModuleBeginJob(ModuleDescription const &description)
void preModuleBeginStream(StreamContext const &sc, ModuleCallingContext const &mcc)
void postModuleStreamBeginRun(StreamContext const &sc, ModuleCallingContext const &mcc)
void postModuleEndStream(StreamContext const &sc, ModuleCallingContext const &mcc)
void preModuleStreamBeginLumi(StreamContext const &sc, ModuleCallingContext const &mcc)
void preModuleStreamBeginRun(StreamContext const &sc, ModuleCallingContext const &mcc)
static const std::string kSkipCurrentProcess
Definition: InputTag.h:57
void postModuleBeginJob(ModuleDescription const &description)
void preModuleStreamEndRun(StreamContext const &sc, ModuleCallingContext const &mcc)
void preModuleEndJob(ModuleDescription const &description)
void postForkReacquireResources(unsigned childIndex, unsigned kMaxChildren)
void postModuleConstruction(ModuleDescription const &description)
std::string const & label() const
Definition: InputTag.h:43
void postModuleStreamEndLumi(StreamContext const &sc, ModuleCallingContext const &mcc)
std::string const & process() const
Definition: InputTag.h:47
void postModuleBeginStream(StreamContext const &sc, ModuleCallingContext const &mcc)
std::map< std::string, SeedsAndName > seedsAndNameMap_
void postModuleStreamBeginLumi(StreamContext const &sc, ModuleCallingContext const &mcc)
edm::service::RandomNumberGeneratorService::~RandomNumberGeneratorService ( )
virtual

Definition at line 214 of file RandomNumberGeneratorService.cc.

214  {
215  }
edm::service::RandomNumberGeneratorService::RandomNumberGeneratorService ( RandomNumberGeneratorService const &  )
privatedelete

Member Function Documentation

void edm::service::RandomNumberGeneratorService::checkEngineType ( std::string const &  typeFromConfig,
std::string const &  typeFromEvent,
std::string const &  engineLabel 
) const
private

Definition at line 877 of file RandomNumberGeneratorService.cc.

References edm::errors::Configuration, and Exception.

Referenced by restoreFromCache().

879  {
880  if(typeFromConfig != typeFromEvent) {
882  << "The RandomNumberGeneratorService is trying to restore\n"
883  << "the state of the random engine for the module \""
884  << engineLabel << "\". An\n"
885  << "error was detected because the type of the engine in the\n"
886  << "input file and the configuration file do not match.\n"
887  << "In the configuration file the type is \"" << typeFromConfig
888  << "\".\nIn the input file the type is \"" << typeFromEvent << "\". If\n"
889  << "you are not generating any random numbers in this module, then\n"
890  << "remove the line in the configuration file that gives it\n"
891  << "a seed and the error will go away. Otherwise, you must give\n"
892  << "this module the same engine type in the configuration file or\n"
893  << "stop trying to restore the random engine state.\n";
894  }
895  }
std::string edm::service::RandomNumberGeneratorService::constructSaveFileName ( ) const
private

Definition at line 973 of file RandomNumberGeneratorService.cc.

References createBeamHaloJobs::directory, newFWLiteAna::fullName, saveFileName_, and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by postEventRead().

973  {
974  char directory[1500];
975  std::string fullName(getcwd(directory, sizeof(directory)) ? directory : "/PathIsTooBig");
976  fullName += "/" + saveFileName_;
977  return fullName;
978  }
string fullName
void edm::service::RandomNumberGeneratorService::createEnginesInVector ( std::vector< LabelAndEngine > &  engines,
unsigned int  seedOffset,
unsigned int  eventSeedOffset,
std::vector< ModuleIDToEngine > &  moduleIDVector 
)
private

Definition at line 1186 of file RandomNumberGeneratorService.cc.

References assert(), i, diffTwoXMLs::label, bookConverter::max, mergeVDriftHistosByStation::name, resetEngineSeeds(), seedsAndNameMap_, python.multivaluedict::sort(), and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by preallocate().

1189  {
1190  // The vectors we will fill here will be the same size as
1191  // or smaller than seedsAndNameMap_.
1192  engines.reserve(seedsAndNameMap_.size());
1193  moduleIDVector.reserve(seedsAndNameMap_.size());
1194 
1195  for(auto const& i : seedsAndNameMap_) {
1196  unsigned int moduleID = i.second.moduleID();
1197  if(moduleID != std::numeric_limits<unsigned int>::max()) {
1198  std::string const& label = i.first;
1199  std::string const& name = i.second.engineName();
1200  VUint32 const& seeds = i.second.seeds();
1201 
1202  if(name == "RanecuEngine") {
1203  std::shared_ptr<CLHEP::HepRandomEngine> engine = std::make_shared<CLHEP::RanecuEngine>();
1204  engines.emplace_back(label, seeds, engine);
1205  resetEngineSeeds(engines.back(), name, seeds, seedOffset, eventSeedOffset);
1206  }
1207  // For the other engines, one seed is required
1208  else {
1209  long int seedL = static_cast<long int>(seeds[0]);
1210 
1211  if(name == "HepJamesRandom") {
1212  std::shared_ptr<CLHEP::HepRandomEngine> engine = std::make_shared<CLHEP::HepJamesRandom>(seedL);
1213  engines.emplace_back(label, seeds, engine);
1214  if(seedOffset != 0 || eventSeedOffset != 0) {
1215  resetEngineSeeds(engines.back(), name, seeds, seedOffset, eventSeedOffset);
1216  }
1217  } else { // TRandom3, currently the only other possibility
1218 
1219  // There is a dangerous conversion from std::uint32_t to long
1220  // that occurs above. In the next 2 lines we check the
1221  // behavior is what we need for the service to work
1222  // properly. This conversion is forced on us by the
1223  // CLHEP and ROOT interfaces. If the assert ever starts
1224  // to fail we will have to come up with a way to deal
1225  // with this.
1226  std::uint32_t seedu32 = static_cast<std::uint32_t>(seedL);
1227  assert(seeds[0] == seedu32);
1228 
1229  std::shared_ptr<CLHEP::HepRandomEngine> engine = std::make_shared<TRandomAdaptor>(seedL);
1230  engines.emplace_back(label, seeds, engine);
1231  if(seedOffset != 0 || eventSeedOffset != 0) {
1232  resetEngineSeeds(engines.back(), name, seeds, seedOffset, eventSeedOffset);
1233  }
1234  }
1235  }
1236  moduleIDVector.emplace_back(&engines.back(), moduleID);
1237  } // if moduleID valid
1238  } // loop over seedsAndMap
1239  std::sort(moduleIDVector.begin(), moduleIDVector.end());
1240  }
int i
Definition: DBlmapReader.cc:9
assert(m_qm.get())
void resetEngineSeeds(LabelAndEngine &labelAndEngine, std::string const &engineName, VUint32 const &seeds, std::uint32_t offset1, std::uint32_t offset2)
std::map< std::string, SeedsAndName > seedsAndNameMap_
void edm::service::RandomNumberGeneratorService::fillDescriptions ( ConfigurationDescriptions descriptions)
static

Definition at line 336 of file RandomNumberGeneratorService.cc.

References edm::ConfigurationDescriptions::add(), edm::ParameterSetDescription::addNode(), edm::ParameterSetDescription::addOptionalUntracked(), edm::ParameterSetDescription::addUntracked(), edm::friendlyname::emptyString(), edm::RequireZeroOrMore, and AlCaHLTBitMon_QueryRunRegistry::string.

336  {
338 
340  edm::InputTag emptyInputTag("", "", "");
341 
342  desc.addNode( edm::ParameterDescription<edm::InputTag>("restoreStateTag", emptyInputTag, false) xor
343  edm::ParameterDescription<std::string>("restoreStateLabel", emptyString, false) );
344 
345  desc.addUntracked<std::string>("saveFileName", emptyString);
346  desc.addUntracked<std::string>("restoreFileName", emptyString);
347  desc.addUntracked<bool>("enableChecking", false);
348  desc.addUntracked<unsigned>("eventSeedOffset", 0U);
349  desc.addUntracked<bool>("verbose", false);
350 
352  val.addOptionalUntracked<std::uint32_t>("initialSeed");
353  val.addOptionalUntracked<std::vector<std::uint32_t> >("initialSeedSet");
354  val.addOptionalUntracked<std::string>("engineName");
355 
356  ParameterWildcard<ParameterSetDescription> wnode("*", RequireZeroOrMore, true, val);
357  wnode.setComment("The name of each ParameterSet will be the associated module label.");
358  desc.addNode(wnode);
359 
360  descriptions.add("RandomNumberGeneratorService", desc);
361  }
static std::string const emptyString("")
CLHEP::HepRandomEngine & edm::service::RandomNumberGeneratorService::getEngine ( StreamID const &  streamID) const
overridevirtual

Use this engine in event methods.

Use the next 2 functions to get the random number engine. These are the only functions most modules should call.

Implements edm::RandomNumberGenerator.

Definition at line 218 of file RandomNumberGeneratorService.cc.

References edm::errors::Configuration, Exception, edm::CurrentModuleOnThread::getCurrentModuleOnThread(), edm::ModuleDescription::id(), edm::errors::LogicError, edm::ModuleCallingContext::moduleDescription(), edm::ModuleDescription::moduleLabel(), streamModuleIDToEngine_, filterCSVwithJSON::target, and edm::StreamID::value().

218  {
219 
220  ModuleCallingContext const* mcc = CurrentModuleOnThread::getCurrentModuleOnThread();
221  if(mcc == nullptr) {
223  << "RandomNumberGeneratorService::getEngine\n"
224  "Requested a random number engine from the RandomNumberGeneratorService\n"
225  "when no module was active. ModuleCallingContext is null\n";
226  }
227  unsigned int moduleID = mcc->moduleDescription()->id();
228 
229  std::vector<ModuleIDToEngine> const& moduleIDVector = streamModuleIDToEngine_.at(streamID.value());
230  ModuleIDToEngine target(nullptr, moduleID);
231  std::vector<ModuleIDToEngine>::const_iterator iter = std::lower_bound(moduleIDVector.begin(),
232  moduleIDVector.end(),
233  target);
234  if(iter == moduleIDVector.end() || iter->moduleID() != moduleID) {
236  << "The module with label \""
237  << mcc->moduleDescription()->moduleLabel()
238  << "\" requested a random number engine from the \n"
239  "RandomNumberGeneratorService, but that module was not configured\n"
240  "for random numbers. An engine is created only if a seed(s) is provided\n"
241  "in the configuration file. Please add the following PSet to the\n"
242  "configuration file for the RandomNumberGeneratorService:\n\n"
243  " " << mcc->moduleDescription()->moduleLabel() << " = cms.PSet(\n"
244  " initialSeed = cms.untracked.uint32(your_seed),\n"
245  " engineName = cms.untracked.string('TRandom3')\n"
246  " )\n"
247  "where you replace \"your_seed\" with a number and add a comma if necessary\n"
248  "The \"engineName\" parameter is optional. If absent the default is \"HepJamesRandom\".\n";
249 
250  }
251  return *iter->labelAndEngine()->engine();
252  }
static ModuleCallingContext const * getCurrentModuleOnThread()
std::vector< std::vector< ModuleIDToEngine > > streamModuleIDToEngine_
CLHEP::HepRandomEngine & edm::service::RandomNumberGeneratorService::getEngine ( LuminosityBlockIndex const &  luminosityBlockIndex) const
overridevirtual

Use this engine in the global begin luminosity block method.

Implements edm::RandomNumberGenerator.

Definition at line 255 of file RandomNumberGeneratorService.cc.

References edm::errors::Configuration, Exception, edm::CurrentModuleOnThread::getCurrentModuleOnThread(), edm::ModuleDescription::id(), edm::errors::LogicError, lumiModuleIDToEngine_, edm::ModuleCallingContext::moduleDescription(), edm::ModuleDescription::moduleLabel(), filterCSVwithJSON::target, and edm::LuminosityBlockIndex::value().

255  {
256 
257  ModuleCallingContext const* mcc = CurrentModuleOnThread::getCurrentModuleOnThread();
258  if(mcc == nullptr) {
260  << "RandomNumberGeneratorService::getEngine\n"
261  "Requested a random number engine from the RandomNumberGeneratorService\n"
262  "when no module was active. ModuleCallingContext is null\n";
263  }
264  unsigned int moduleID = mcc->moduleDescription()->id();
265 
266  std::vector<ModuleIDToEngine> const& moduleIDVector = lumiModuleIDToEngine_.at(lumiIndex.value());
267  ModuleIDToEngine target(nullptr, moduleID);
268  std::vector<ModuleIDToEngine>::const_iterator iter = std::lower_bound(moduleIDVector.begin(),
269  moduleIDVector.end(),
270  target);
271  if(iter == moduleIDVector.end() || iter->moduleID() != moduleID) {
273  << "The module with label \""
274  << mcc->moduleDescription()->moduleLabel()
275  << "\" requested a random number engine from the \n"
276  "RandomNumberGeneratorService, but that module was not configured\n"
277  "for random numbers. An engine is created only if a seed(s) is provided\n"
278  "in the configuration file. Please add the following PSet to the\n"
279  "configuration file for the RandomNumberGeneratorService:\n\n"
280  " " << mcc->moduleDescription()->moduleLabel() << " = cms.PSet(\n"
281  " initialSeed = cms.untracked.uint32(your_seed),\n"
282  " engineName = cms.untracked.string('TRandom3')\n"
283  " )\n"
284  "where you replace \"your_seed\" with a number and add a comma if necessary\n"
285  "The \"engineName\" parameter is optional. If absent the default is \"HepJamesRandom\".\n";
286 
287  }
288  return *iter->labelAndEngine()->engine();
289  }
static ModuleCallingContext const * getCurrentModuleOnThread()
std::vector< std::vector< ModuleIDToEngine > > lumiModuleIDToEngine_
std::vector< RandomEngineState > const & edm::service::RandomNumberGeneratorService::getEventCache ( StreamID const &  streamID) const
overridevirtual

Implements edm::RandomNumberGenerator.

Definition at line 612 of file RandomNumberGeneratorService.cc.

References eventCache_, and edm::StreamID::value().

612  {
613  return eventCache_.at(streamID.value());
614  }
std::vector< std::vector< RandomEngineState > > eventCache_
std::vector< RandomEngineState > const & edm::service::RandomNumberGeneratorService::getLumiCache ( LuminosityBlockIndex const &  lumiIndex) const
overridevirtual

These two are used by the RandomEngineStateProducer.

Implements edm::RandomNumberGenerator.

Definition at line 607 of file RandomNumberGeneratorService.cc.

References lumiCache_, and edm::LuminosityBlockIndex::value().

607  {
608  return lumiCache_.at(lumiIndex.value());
609  }
std::vector< std::vector< RandomEngineState > > lumiCache_
std::uint32_t edm::service::RandomNumberGeneratorService::mySeed ( ) const
overridevirtual

This returns the seed from the configuration. In the unusual case where an an engine type takes multiple seeds to initialize a sequence, this function only returns the first. As a general rule, this function should not be used, but is available for backward compatibility and debugging. It might be useful for some types of tests. Using this to seed engines constructed in modules is not recommended because (unless done very carefully) it will create duplicate sequences in different threads and/or data races. Also, if engines are created by modules the replay mechanism will be broken. Because it is dangerous and could be misused, this function might be deleted someday if we ever find time to delete all uses of it in CMSSW. There are of order 10 last time I checked ...

Implements edm::RandomNumberGenerator.

Definition at line 298 of file RandomNumberGeneratorService.cc.

References edm::errors::Configuration, Exception, edm::CurrentModuleOnThread::getCurrentModuleOnThread(), diffTwoXMLs::label, edm::errors::LogicError, edm::ModuleCallingContext::moduleDescription(), edm::ModuleDescription::moduleLabel(), moduleLabel_, seedsAndNameMap_, and AlCaHLTBitMon_QueryRunRegistry::string.

298  {
300  ModuleCallingContext const* mcc = CurrentModuleOnThread::getCurrentModuleOnThread();
301  if(mcc == nullptr) {
302  if(!moduleLabel_.empty()) {
303  label = moduleLabel_;
304  }
305  else {
307  << "RandomNumberGeneratorService::getEngine()\n"
308  "Requested a random number engine from the RandomNumberGeneratorService\n"
309  "when no module was active. ModuleCallingContext is null\n";
310  }
311  } else {
312  label = mcc->moduleDescription()->moduleLabel();
313  }
314 
315  std::map<std::string, SeedsAndName>::const_iterator iter = seedsAndNameMap_.find(label);
316  if(iter == seedsAndNameMap_.end()) {
318  << "The module with label \""
319  << label
320  << "\" requested a random number seed from the \n"
321  "RandomNumberGeneratorService, but that module was not configured\n"
322  "for random numbers. An engine is created only if a seed(s) is provided\n"
323  "in the configuration file. Please add the following PSet to the\n"
324  "configuration file for the RandomNumberGeneratorService:\n\n"
325  " " << label << " = cms.PSet(\n"
326  " initialSeed = cms.untracked.uint32(your_seed),\n"
327  " engineName = cms.untracked.string('TRandom3')\n"
328  " )\n"
329  "where you replace \"your_seed\" with a number and add a comma if necessary\n"
330  "The \"engineName\" parameter is optional. If absent the default is \"HepJamesRandom\".\n";
331  }
332  return iter->second.seeds()[0];
333  }
static ModuleCallingContext const * getCurrentModuleOnThread()
std::map< std::string, SeedsAndName > seedsAndNameMap_
RandomNumberGeneratorService const& edm::service::RandomNumberGeneratorService::operator= ( RandomNumberGeneratorService const &  )
privatedelete
void edm::service::RandomNumberGeneratorService::postEventRead ( Event const &  event)
overridevirtual

Implements edm::RandomNumberGenerator.

Definition at line 516 of file RandomNumberGeneratorService.cc.

References constructSaveFileName(), eventCache_, newFWLiteAna::fullName, edm::InputTag::label(), readFromEvent(), restoreFromCache(), restoreStateTag_, saveFileName_, saveFileNameRecorded_, saveStatesToFile(), snapShot(), streamEngines_, edm::Event::streamID(), and AlCaHLTBitMon_QueryRunRegistry::string.

516  {
517 
518  if(!restoreStateTag_.label().empty()) {
519 
520  // This initializes the cache before readFromEvent
521  snapShot(streamEngines_[event.streamID()], eventCache_[event.streamID()]);
522 
523  // copy from Event to event cache
525 
526  // copy from event cache to engines
527  restoreFromCache(eventCache_[event.streamID()], streamEngines_[event.streamID()]);
528 
529  } else {
530  // copy from engines to event cache
531  snapShot(streamEngines_[event.streamID()], eventCache_[event.streamID()]);
532  }
533 
534  // if requested write text file from both caches
535  if(!saveFileName_.empty()) {
536  saveStatesToFile(saveFileName_, event.streamID(), event.getLuminosityBlock().index());
537  bool expected = false;
538  if(saveFileNameRecorded_.compare_exchange_strong(expected, true)) {
540  Service<JobReport> reportSvc;
541  reportSvc->reportRandomStateFile(fullName);
542  }
543  }
544  }
void snapShot(std::vector< LabelAndEngine > const &engines, std::vector< RandomEngineState > &cache)
void saveStatesToFile(std::string const &fileName, StreamID const &streamID, LuminosityBlockIndex const &lumiIndex)
std::vector< std::vector< LabelAndEngine > > streamEngines_
std::vector< std::vector< RandomEngineState > > eventCache_
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
string fullName
std::string const & label() const
Definition: InputTag.h:43
void restoreFromCache(std::vector< RandomEngineState > const &cache, std::vector< LabelAndEngine > &engines)
void edm::service::RandomNumberGeneratorService::postForkReacquireResources ( unsigned  childIndex,
unsigned  kMaxChildren 
)

Definition at line 454 of file RandomNumberGeneratorService.cc.

References assert(), childIndex_, edm::errors::Configuration, gather_cfg::cout, eventSeedOffset_, Exception, i, lumiCache_, lumiEngines_, nStreams_, print(), resetEngineSeeds(), restoreFileName_, saveFileName_, seedsAndNameMap_, snapShot(), streamEngines_, createPayload::suffix, and verbose_.

Referenced by RandomNumberGeneratorService().

454  {
455  assert(nStreams_ == 1);
456  childIndex_ = childIndex;
457 
458  if(!restoreFileName_.empty()) {
460  << "Configuration is illegal. The RandomNumberGeneratorService is configured\n"
461  << "to run replay using a text file to input the random engine states and\n"
462  << "the process is configured to fork multiple processes. No forking is\n"
463  << "is allowed with this type of replay\n";
464  }
465 
466  if (childIndex_ != 0) {
467  std::vector<LabelAndEngine>& engines = streamEngines_[0];
468  for(auto& labelAndEngine : engines) {
469  std::map<std::string, SeedsAndName>::const_iterator seedsAndName = seedsAndNameMap_.find(labelAndEngine.label());
470  assert(seedsAndName != seedsAndNameMap_.end());
471  resetEngineSeeds(labelAndEngine,
472  seedsAndName->second.engineName(),
473  seedsAndName->second.seeds(),
474  childIndex,
476  }
477  }
478  if (kMaxChildren != 0) {
479  for(unsigned int i = 0; i < lumiEngines_.size(); ++i) {
480  std::vector<LabelAndEngine>& engines = lumiEngines_.at(i);
481  for(auto& labelAndEngine : engines) {
482  std::map<std::string, SeedsAndName>::const_iterator seedsAndName = seedsAndNameMap_.find(labelAndEngine.label());
483  assert(seedsAndName != seedsAndNameMap_.end());
484  resetEngineSeeds(labelAndEngine,
485  seedsAndName->second.engineName(),
486  seedsAndName->second.seeds(),
487  kMaxChildren,
488  0);
489  }
491  }
492  }
493 
494  if(!saveFileName_.empty()) {
495  std::ostringstream suffix;
496  suffix << "_" << childIndex;
497  saveFileName_ += suffix.str();
498  }
499  if(verbose_) {
500  print(std::cout);
501  }
502  }
int i
Definition: DBlmapReader.cc:9
assert(m_qm.get())
void resetEngineSeeds(LabelAndEngine &labelAndEngine, std::string const &engineName, VUint32 const &seeds, std::uint32_t offset1, std::uint32_t offset2)
void snapShot(std::vector< LabelAndEngine > const &engines, std::vector< RandomEngineState > &cache)
virtual void print(std::ostream &os) const override
For debugging.
std::vector< std::vector< LabelAndEngine > > streamEngines_
std::vector< std::vector< RandomEngineState > > lumiCache_
tuple cout
Definition: gather_cfg.py:121
std::vector< std::vector< LabelAndEngine > > lumiEngines_
std::map< std::string, SeedsAndName > seedsAndNameMap_
void edm::service::RandomNumberGeneratorService::postModuleBeginJob ( ModuleDescription const &  description)

Definition at line 387 of file RandomNumberGeneratorService.cc.

References moduleLabel_.

Referenced by RandomNumberGeneratorService().

387  {
388  moduleLabel_.clear();
389  }
void edm::service::RandomNumberGeneratorService::postModuleBeginStream ( StreamContext const &  sc,
ModuleCallingContext const &  mcc 
)

Definition at line 552 of file RandomNumberGeneratorService.cc.

References postModuleStreamCheck().

Referenced by RandomNumberGeneratorService().

552  {
553  postModuleStreamCheck(sc, mcc);
554  }
void postModuleStreamCheck(StreamContext const &sc, ModuleCallingContext const &mcc)
void edm::service::RandomNumberGeneratorService::postModuleConstruction ( ModuleDescription const &  description)

Definition at line 377 of file RandomNumberGeneratorService.cc.

References moduleLabel_.

Referenced by RandomNumberGeneratorService().

377  {
378  moduleLabel_.clear();
379  }
void edm::service::RandomNumberGeneratorService::postModuleEndJob ( ModuleDescription const &  description)

Definition at line 397 of file RandomNumberGeneratorService.cc.

References moduleLabel_.

Referenced by RandomNumberGeneratorService().

397  {
398  moduleLabel_.clear();
399  }
void edm::service::RandomNumberGeneratorService::postModuleEndStream ( StreamContext const &  sc,
ModuleCallingContext const &  mcc 
)

Definition at line 562 of file RandomNumberGeneratorService.cc.

References postModuleStreamCheck().

Referenced by RandomNumberGeneratorService().

562  {
563  postModuleStreamCheck(sc, mcc);
564  }
void postModuleStreamCheck(StreamContext const &sc, ModuleCallingContext const &mcc)
void edm::service::RandomNumberGeneratorService::postModuleStreamBeginLumi ( StreamContext const &  sc,
ModuleCallingContext const &  mcc 
)

Definition at line 592 of file RandomNumberGeneratorService.cc.

References postModuleStreamCheck().

Referenced by RandomNumberGeneratorService().

592  {
593  postModuleStreamCheck(sc, mcc);
594  }
void postModuleStreamCheck(StreamContext const &sc, ModuleCallingContext const &mcc)
void edm::service::RandomNumberGeneratorService::postModuleStreamBeginRun ( StreamContext const &  sc,
ModuleCallingContext const &  mcc 
)

Definition at line 572 of file RandomNumberGeneratorService.cc.

References postModuleStreamCheck().

Referenced by RandomNumberGeneratorService().

572  {
573  postModuleStreamCheck(sc, mcc);
574  }
void postModuleStreamCheck(StreamContext const &sc, ModuleCallingContext const &mcc)
void edm::service::RandomNumberGeneratorService::postModuleStreamCheck ( StreamContext const &  sc,
ModuleCallingContext const &  mcc 
)
private

Definition at line 699 of file RandomNumberGeneratorService.cc.

References enableChecking_, edm::service::RandomNumberGeneratorService::LabelAndEngine::engine(), Exception, edm::ModuleDescription::id(), edm::errors::LogicError, edm::ModuleCallingContext::moduleDescription(), edm::ModuleDescription::moduleLabel(), edm::ModuleDescription::moduleName(), edm::StreamContext::streamID(), streamModuleIDToEngine_, filterCSVwithJSON::target, and edm::StreamID::value().

Referenced by postModuleBeginStream(), postModuleEndStream(), postModuleStreamBeginLumi(), postModuleStreamBeginRun(), postModuleStreamEndLumi(), and postModuleStreamEndRun().

699  {
700  if(enableChecking_) {
701  unsigned int moduleID = mcc.moduleDescription()->id();
702  std::vector<ModuleIDToEngine> const& moduleIDVector = streamModuleIDToEngine_.at(sc.streamID().value());
703  ModuleIDToEngine target(nullptr, moduleID);
704  std::vector<ModuleIDToEngine>::const_iterator iter = std::lower_bound(moduleIDVector.begin(),
705  moduleIDVector.end(),
706  target);
707  if(iter != moduleIDVector.end() && iter->moduleID() == moduleID) {
708  LabelAndEngine* labelAndEngine = iter->labelAndEngine();
709  if(iter->engineState() != labelAndEngine->engine()->put()) {
711  << "It is illegal to generate random numbers during beginStream, endStream,\n"
712  "beginRun, endRun, beginLumi, endLumi because that makes it very difficult\n"
713  "to replay the processing of individual events. Random numbers were\n"
714  "generated during one of these methods for the module with class name\n\""
715  << mcc.moduleDescription()->moduleName() << "\" "
716  "and module label \"" << mcc.moduleDescription()->moduleLabel() << "\"\n";
717  }
718  }
719  }
720  }
std::vector< std::vector< ModuleIDToEngine > > streamModuleIDToEngine_
void edm::service::RandomNumberGeneratorService::postModuleStreamEndLumi ( StreamContext const &  sc,
ModuleCallingContext const &  mcc 
)

Definition at line 602 of file RandomNumberGeneratorService.cc.

References postModuleStreamCheck().

Referenced by RandomNumberGeneratorService().

602  {
603  postModuleStreamCheck(sc, mcc);
604  }
void postModuleStreamCheck(StreamContext const &sc, ModuleCallingContext const &mcc)
void edm::service::RandomNumberGeneratorService::postModuleStreamEndRun ( StreamContext const &  sc,
ModuleCallingContext const &  mcc 
)

Definition at line 582 of file RandomNumberGeneratorService.cc.

References postModuleStreamCheck().

Referenced by RandomNumberGeneratorService().

582  {
583  postModuleStreamCheck(sc, mcc);
584  }
void postModuleStreamCheck(StreamContext const &sc, ModuleCallingContext const &mcc)
void edm::service::RandomNumberGeneratorService::preallocate ( SystemBounds const &  sb)

Definition at line 402 of file RandomNumberGeneratorService.cc.

References assert(), edm::errors::Configuration, gather_cfg::cout, createEnginesInVector(), eventCache_, eventSeedOffset_, Exception, lumiCache_, lumiEngines_, lumiModuleIDToEngine_, edm::service::SystemBounds::maxNumberOfConcurrentLuminosityBlocks(), edm::service::SystemBounds::maxNumberOfStreams(), nStreams_, outFiles_, print(), readEventStatesFromTextFile(), readLumiStatesFromTextFile(), restoreFileName_, restoreFromCache(), saveFileName_, snapShot(), streamEngines_, streamModuleIDToEngine_, and verbose_.

Referenced by RandomNumberGeneratorService().

402  {
403 
404  nStreams_ = sb.maxNumberOfStreams();
405  assert(nStreams_ >= 1);
406  if(!restoreFileName_.empty() && nStreams_ != 1) {
408  << "Configuration is illegal. The RandomNumberGeneratorService is configured\n"
409  << "to run replay using a text file to input the random engine states and\n"
410  << "the number of streams is greater than 1. Either set the\n"
411  << "parameter named \"restoreFileName\" in the RandomNumberGeneratorService\n"
412  << "to the empty string or set the parameter \"numberOfStreams\" in the top\n"
413  << "level options parameter set to 1. (Probably these are the default values\n"
414  << "and just not setting the parameters will also work)\n";
415  }
416  unsigned int nConcurrentLumis = sb.maxNumberOfConcurrentLuminosityBlocks();
417 
419  lumiModuleIDToEngine_.resize(nConcurrentLumis);
420  streamEngines_.resize(nStreams_);
421  lumiEngines_.resize(nConcurrentLumis);
422  eventCache_.resize(nStreams_);
423  lumiCache_.resize(nConcurrentLumis);
424  outFiles_.resize(nStreams_);
425 
426  for(unsigned int iStream = 0; iStream < nStreams_; ++iStream) {
427  unsigned int seedOffset = iStream;
429  if(!saveFileName_.empty()) {
430  outFiles_[iStream].reset(new std::ofstream);
431  }
432  }
433  for(unsigned int iLumi = 0; iLumi < nConcurrentLumis; ++iLumi) {
434  unsigned int seedOffset = nStreams_;
435  createEnginesInVector(lumiEngines_[iLumi], seedOffset, 0, lumiModuleIDToEngine_[iLumi]);
436  snapShot(lumiEngines_[iLumi], lumiCache_[iLumi]);
437  if(!restoreFileName_.empty()) {
439  }
440  }
441 
442  if(!restoreFileName_.empty()) {
443  // There is guaranteed to be one stream in this case
447  }
448  if(verbose_) {
449  print(std::cout);
450  }
451  }
assert(m_qm.get())
std::vector< std::shared_ptr< std::ofstream > > outFiles_
void snapShot(std::vector< LabelAndEngine > const &engines, std::vector< RandomEngineState > &cache)
std::vector< std::vector< ModuleIDToEngine > > streamModuleIDToEngine_
void createEnginesInVector(std::vector< LabelAndEngine > &engines, unsigned int seedOffset, unsigned int eventSeedOffset, std::vector< ModuleIDToEngine > &moduleIDVector)
virtual void print(std::ostream &os) const override
For debugging.
std::vector< std::vector< LabelAndEngine > > streamEngines_
std::vector< std::vector< RandomEngineState > > eventCache_
void readEventStatesFromTextFile(std::string const &fileName, std::vector< RandomEngineState > &cache)
std::vector< std::vector< RandomEngineState > > lumiCache_
tuple cout
Definition: gather_cfg.py:121
void readLumiStatesFromTextFile(std::string const &fileName, std::vector< RandomEngineState > &cache)
void restoreFromCache(std::vector< RandomEngineState > const &cache, std::vector< LabelAndEngine > &engines)
std::vector< std::vector< LabelAndEngine > > lumiEngines_
std::vector< std::vector< ModuleIDToEngine > > lumiModuleIDToEngine_
void edm::service::RandomNumberGeneratorService::preBeginLumi ( LuminosityBlock const &  lumi)
overridevirtual

Implements edm::RandomNumberGenerator.

Definition at line 505 of file RandomNumberGeneratorService.cc.

References edm::LuminosityBlock::index(), edm::InputTag::label(), lumiCache_, lumiEngines_, readFromLuminosityBlock(), restoreFromCache(), and restoreStateTag_.

505  {
506 
507  if(!restoreStateTag_.label().empty()) {
508  // Copy from a product in the LuminosityBlock to cache for a particular luminosityBlockIndex
510  }
511  // Copy from cache to engine the state for a particular luminosityBlockIndex
512  restoreFromCache(lumiCache_[lumi.index()], lumiEngines_[lumi.index()]);
513  }
tuple lumi
Definition: fjr2json.py:35
void readFromLuminosityBlock(LuminosityBlock const &lumi)
std::vector< std::vector< RandomEngineState > > lumiCache_
std::string const & label() const
Definition: InputTag.h:43
void restoreFromCache(std::vector< RandomEngineState > const &cache, std::vector< LabelAndEngine > &engines)
std::vector< std::vector< LabelAndEngine > > lumiEngines_
void edm::service::RandomNumberGeneratorService::preModuleBeginJob ( ModuleDescription const &  description)

Definition at line 382 of file RandomNumberGeneratorService.cc.

References edm::ModuleDescription::moduleLabel(), and moduleLabel_.

Referenced by RandomNumberGeneratorService().

382  {
383  moduleLabel_ = description.moduleLabel();
384  }
tuple description
Definition: idDealer.py:66
void edm::service::RandomNumberGeneratorService::preModuleBeginStream ( StreamContext const &  sc,
ModuleCallingContext const &  mcc 
)

These next 12 functions are only used to check that random numbers are not being generated in these methods when enable checking is configured on.

Definition at line 547 of file RandomNumberGeneratorService.cc.

References preModuleStreamCheck().

Referenced by RandomNumberGeneratorService().

547  {
548  preModuleStreamCheck(sc, mcc);
549  }
void preModuleStreamCheck(StreamContext const &sc, ModuleCallingContext const &mcc)
void edm::service::RandomNumberGeneratorService::preModuleConstruction ( ModuleDescription const &  description)

Definition at line 364 of file RandomNumberGeneratorService.cc.

References edm::ModuleDescription::id(), edm::ModuleDescription::moduleLabel(), moduleLabel_, and seedsAndNameMap_.

Referenced by RandomNumberGeneratorService().

364  {
365  std::map<std::string, SeedsAndName>::iterator iter = seedsAndNameMap_.find(description.moduleLabel());
366  if(iter != seedsAndNameMap_.end()) {
367  iter->second.setModuleID(description.id());
368  }
369  // The next line supports the mySeed function
370  // DELETE IT when/if that function is deleted.
371  moduleLabel_ = description.moduleLabel();
372  }
tuple description
Definition: idDealer.py:66
std::map< std::string, SeedsAndName > seedsAndNameMap_
void edm::service::RandomNumberGeneratorService::preModuleEndJob ( ModuleDescription const &  description)

Definition at line 392 of file RandomNumberGeneratorService.cc.

References edm::ModuleDescription::moduleLabel(), and moduleLabel_.

Referenced by RandomNumberGeneratorService().

392  {
393  moduleLabel_ = description.moduleLabel();
394  }
tuple description
Definition: idDealer.py:66
void edm::service::RandomNumberGeneratorService::preModuleEndStream ( StreamContext const &  sc,
ModuleCallingContext const &  mcc 
)

Definition at line 557 of file RandomNumberGeneratorService.cc.

References preModuleStreamCheck().

Referenced by RandomNumberGeneratorService().

557  {
558  preModuleStreamCheck(sc, mcc);
559  }
void preModuleStreamCheck(StreamContext const &sc, ModuleCallingContext const &mcc)
void edm::service::RandomNumberGeneratorService::preModuleStreamBeginLumi ( StreamContext const &  sc,
ModuleCallingContext const &  mcc 
)

Definition at line 587 of file RandomNumberGeneratorService.cc.

References preModuleStreamCheck().

Referenced by RandomNumberGeneratorService().

587  {
588  preModuleStreamCheck(sc, mcc);
589  }
void preModuleStreamCheck(StreamContext const &sc, ModuleCallingContext const &mcc)
void edm::service::RandomNumberGeneratorService::preModuleStreamBeginRun ( StreamContext const &  sc,
ModuleCallingContext const &  mcc 
)

Definition at line 567 of file RandomNumberGeneratorService.cc.

References preModuleStreamCheck().

Referenced by RandomNumberGeneratorService().

567  {
568  preModuleStreamCheck(sc, mcc);
569  }
void preModuleStreamCheck(StreamContext const &sc, ModuleCallingContext const &mcc)
void edm::service::RandomNumberGeneratorService::preModuleStreamCheck ( StreamContext const &  sc,
ModuleCallingContext const &  mcc 
)
private

Definition at line 683 of file RandomNumberGeneratorService.cc.

References enableChecking_, edm::service::RandomNumberGeneratorService::LabelAndEngine::engine(), edm::ModuleDescription::id(), edm::ModuleCallingContext::moduleDescription(), edm::StreamContext::streamID(), streamModuleIDToEngine_, filterCSVwithJSON::target, and edm::StreamID::value().

Referenced by preModuleBeginStream(), preModuleEndStream(), preModuleStreamBeginLumi(), preModuleStreamBeginRun(), preModuleStreamEndLumi(), and preModuleStreamEndRun().

683  {
684  if(enableChecking_) {
685  unsigned int moduleID = mcc.moduleDescription()->id();
686  std::vector<ModuleIDToEngine>& moduleIDVector = streamModuleIDToEngine_.at(sc.streamID().value());
687  ModuleIDToEngine target(nullptr, moduleID);
688  std::vector<ModuleIDToEngine>::iterator iter = std::lower_bound(moduleIDVector.begin(),
689  moduleIDVector.end(),
690  target);
691  if(iter != moduleIDVector.end() && iter->moduleID() == moduleID) {
692  LabelAndEngine* labelAndEngine = iter->labelAndEngine();
693  iter->setEngineState(labelAndEngine->engine()->put());
694  }
695  }
696  }
std::vector< std::vector< ModuleIDToEngine > > streamModuleIDToEngine_
void edm::service::RandomNumberGeneratorService::preModuleStreamEndLumi ( StreamContext const &  sc,
ModuleCallingContext const &  mcc 
)

Definition at line 597 of file RandomNumberGeneratorService.cc.

References preModuleStreamCheck().

Referenced by RandomNumberGeneratorService().

597  {
598  preModuleStreamCheck(sc, mcc);
599  }
void preModuleStreamCheck(StreamContext const &sc, ModuleCallingContext const &mcc)
void edm::service::RandomNumberGeneratorService::preModuleStreamEndRun ( StreamContext const &  sc,
ModuleCallingContext const &  mcc 
)

Definition at line 577 of file RandomNumberGeneratorService.cc.

References preModuleStreamCheck().

Referenced by RandomNumberGeneratorService().

577  {
578  preModuleStreamCheck(sc, mcc);
579  }
void preModuleStreamCheck(StreamContext const &sc, ModuleCallingContext const &mcc)
void edm::service::RandomNumberGeneratorService::print ( std::ostream &  os) const
overridevirtual

For debugging.

Implements edm::RandomNumberGenerator.

Definition at line 617 of file RandomNumberGeneratorService.cc.

References childIndex_, enableChecking_, eventSeedOffset_, i, j, relval_steps::k, lumiEngines_, nStreams_, restoreFileName_, restoreStateBeginLumiTag_, restoreStateTag_, saveFileName_, saveFileNameRecorded_, seedsAndNameMap_, streamEngines_, AlCaHLTBitMon_QueryRunRegistry::string, and verbose_.

Referenced by postForkReacquireResources(), and preallocate().

617  {
618 
619  os << "\n\nRandomNumberGeneratorService dump\n\n";
620 
621  os << " Contents of seedsAndNameMap (label moduleID engineType seeds)\n";
622  for(auto const& entry : seedsAndNameMap_) {
623  os << " " << entry.first
624  << " " << entry.second.moduleID()
625  << " " << entry.second.engineName();
626  for(auto val : entry.second.seeds()) {
627  os << " " << val;
628  }
629  os << "\n";
630  }
631  os << " nStreams_ = " << nStreams_ << "\n";
632  os << " saveFileName_ = " << saveFileName_ << "\n";
633  os << " saveFileNameRecorded_ = " << saveFileNameRecorded_ << "\n";
634  os << " restoreFileName_ = " << restoreFileName_ << "\n";
635  os << " enableChecking_ = " << enableChecking_ << "\n";
636  os << " childIndex_ = " << childIndex_ << "\n";
637  os << " eventSeedOffset_ = " << eventSeedOffset_ << "\n";
638  os << " verbose_ = " << verbose_ << "\n";
639  os << " restoreStateTag_ = " << restoreStateTag_ << "\n";
640  os << " restoreStateBeginLumiTag_ = " << restoreStateBeginLumiTag_ << "\n";
641 
642  os << "\n streamEngines_\n";
643  unsigned int iStream = 0;
644  for(auto const& k : streamEngines_) {
645  os << " Stream " << iStream << "\n";
646  for(auto const& i : k) {
647  os << " " << i.label();
648  for(auto const& j : i.seeds()) {
649  os << " " << j;
650  }
651  os << " " << i.engine()->name();
652  if(i.engine()->name() == std::string("HepJamesRandom")) {
653  os << " " << i.engine()->getSeed();
654  } else {
655  os << " engine does not know seeds";
656  }
657  os << "\n";
658  }
659  ++iStream;
660  }
661  os << "\n lumiEngines_\n";
662  unsigned int iLumi = 0;
663  for(auto const& k : lumiEngines_) {
664  os << " lumiIndex " << iLumi << "\n";
665  for(auto const& i : k) {
666  os << " " << i.label();
667  for(auto const& j : i.seeds()) {
668  os << " " << j;
669  }
670  os << " " << i.engine()->name();
671  if(i.engine()->name() == std::string("HepJamesRandom")) {
672  os << " " << i.engine()->getSeed();
673  } else {
674  os << " engine does not know seeds";
675  }
676  os << "\n";
677  }
678  ++iLumi;
679  }
680  }
int i
Definition: DBlmapReader.cc:9
int j
Definition: DBlmapReader.cc:9
std::vector< std::vector< LabelAndEngine > > streamEngines_
std::vector< std::vector< LabelAndEngine > > lumiEngines_
std::map< std::string, SeedsAndName > seedsAndNameMap_
bool edm::service::RandomNumberGeneratorService::readEngineState ( std::istream &  is,
std::vector< RandomEngineState > &  cache,
std::string const &  whichStates,
bool &  saveToCache 
)
private

Definition at line 1020 of file RandomNumberGeneratorService.cc.

References edm::errors::Configuration, Exception, maxSeeds, maxStates, readVector(), restoreFileName_, RandomEngineState::setLabel(), and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by readStatesFromFile().

1023  {
1024  std::string leading;
1025  std::string trailing;
1026  std::string moduleLabel;
1028  std::vector<std::uint32_t> seedVector;
1029  std::vector<std::uint32_t>::size_type stateVectorSize;
1030  std::vector<std::uint32_t> stateVector;
1031 
1032  // First we need to look for the special strings
1033  // that mark the end of the file and beginning and
1034  // and end of the data for different sections.
1035 
1036  is >> leading;
1037  if(!is.good()) {
1039  << "File \"" << restoreFileName_
1040  << "\" is ill-structured or otherwise corrupted.\n"
1041  << "Cannot read next field and did not hit the end yet.\n";
1042  }
1043 
1044  // This marks the end of the file. We are done.
1045  if(leading == std::string("</RandomEngineStates>")) return false;
1046 
1047  // This marks the end of a section of the data
1048  if(leading == std::string("</Event>") ||
1049  leading == std::string("</Lumi>")) {
1050  saveToCache = false;
1051  return true;
1052  }
1053 
1054  // This marks the beginning of a section
1055  if(leading == std::string("<Event>") ||
1056  leading == std::string("<Lumi>")) {
1057  saveToCache = (leading == whichStates);
1058  return true;
1059  }
1060 
1061  // Process the next engine state
1062 
1063  is >> moduleLabel >> trailing;
1064  if(!is.good() ||
1065  leading != std::string("<ModuleLabel>") ||
1066  trailing != std::string("</ModuleLabel>")) {
1068  << "File \"" << restoreFileName_
1069  << "\" is ill-structured or otherwise corrupted.\n"
1070  << "Cannot read a module label when restoring random engine states.\n";
1071  }
1072 
1073  is >> leading >> seedVectorSize >> trailing;
1074  if(!is.good() ||
1075  leading != std::string("<SeedLength>") ||
1076  trailing != std::string("</SeedLength>")) {
1078  << "File \"" << restoreFileName_
1079  << "\" is ill-structured or otherwise corrupted.\n"
1080  << "Cannot read seed vector length when restoring random engine states.\n";
1081  }
1082 
1083  is >> leading;
1084  if(!is.good() ||
1085  leading != std::string("<InitialSeeds>")) {
1087  << "File \"" << restoreFileName_
1088  << "\" is ill-structured or otherwise corrupted.\n"
1089  << "Cannot read beginning of InitialSeeds when restoring random engine states.\n";
1090  }
1091 
1092  if(seedVectorSize > maxSeeds) {
1094  << "File \"" << restoreFileName_
1095  << "\" is ill-structured or otherwise corrupted.\n"
1096  << "The number of seeds exceeds 64K.\n";
1097  }
1098 
1099  readVector(is, seedVectorSize, seedVector);
1100 
1101  is >> trailing;
1102  if(!is.good() ||
1103  trailing != std::string("</InitialSeeds>")) {
1105  << "File \"" << restoreFileName_
1106  << "\" is ill-structured or otherwise corrupted.\n"
1107  << "Cannot read end of InitialSeeds when restoring random engine states.\n";
1108  }
1109 
1110  is >> leading >> stateVectorSize >> trailing;
1111  if(!is.good() ||
1112  leading != std::string("<FullStateLength>") ||
1113  trailing != std::string("</FullStateLength>")) {
1115  << "File \"" << restoreFileName_
1116  << "\" is ill-structured or otherwise corrupted.\n"
1117  << "Cannot read state vector length when restoring random engine states.\n";
1118  }
1119 
1120  is >> leading;
1121  if(!is.good() ||
1122  leading != std::string("<FullState>")) {
1124  << "File \"" << restoreFileName_
1125  << "\" is ill-structured or otherwise corrupted.\n"
1126  << "Cannot read beginning of FullState when restoring random engine states.\n";
1127  }
1128 
1129  if(stateVectorSize > maxStates) {
1131  << "File \"" << restoreFileName_
1132  << "\" is ill-structured or otherwise corrupted.\n"
1133  << "The number of states exceeds 64K.\n";
1134  }
1135 
1136  readVector(is, stateVectorSize, stateVector);
1137 
1138  is >> trailing;
1139  if(!is.good() ||
1140  trailing != std::string("</FullState>")) {
1142  << "File \"" << restoreFileName_
1143  << "\" is ill-structured or otherwise corrupted.\n"
1144  << "Cannot read end of FullState when restoring random engine states.\n";
1145  }
1146 
1147  if(saveToCache) {
1148  RandomEngineState randomEngineState;
1149  randomEngineState.setLabel(moduleLabel);
1150  std::vector<RandomEngineState>::iterator state =
1151  std::lower_bound(cache.begin(), cache.end(), randomEngineState);
1152 
1153  if(state != cache.end() && moduleLabel == state->getLabel()) {
1154  if(seedVector.size() != state->getSeed().size() ||
1155  stateVector.size() != state->getState().size()) {
1157  << "File \"" << restoreFileName_
1158  << "\" is ill-structured or otherwise corrupted.\n"
1159  << "Vectors containing engine state are the incorrect size for the type of random engine.\n";
1160  }
1161  state->setSeed(seedVector);
1162  state->setState(stateVector);
1163  }
1164  }
1165  return true;
1166  }
void readVector(std::istream &is, unsigned numItems, std::vector< std::uint32_t > &v)
void setLabel(const std::string &value)
uint16_t size_type
static const std::vector< std::uint32_t >::size_type maxStates
static const std::vector< std::uint32_t >::size_type maxSeeds
void edm::service::RandomNumberGeneratorService::readEventStatesFromTextFile ( std::string const &  fileName,
std::vector< RandomEngineState > &  cache 
)
private

Definition at line 981 of file RandomNumberGeneratorService.cc.

References readStatesFromFile(), and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by preallocate().

982  {
983  std::string whichStates("<Event>");
984  readStatesFromFile(fileName, cache, whichStates);
985  }
void readStatesFromFile(std::string const &fileName, std::vector< RandomEngineState > &cache, std::string const &whichStates)
void edm::service::RandomNumberGeneratorService::readFromEvent ( Event const &  event)
private

Definition at line 754 of file RandomNumberGeneratorService.cc.

References eventCache_, Exception, edm::HandleBase::isValid(), edm::errors::ProductNotFound, restoreStateTag_, and edm::Event::streamID().

Referenced by postEventRead().

754  {
755 
757 
758  event.getByLabel(restoreStateTag_, states);
759 
760  if(!states.isValid()) {
762  << "The RandomNumberGeneratorService is trying to restore\n"
763  << "the state of the random engines by reading a product from\n"
764  << "the Event with input tag \"" << restoreStateTag_ << "\".\n"
765  << "It could not find the product.\n"
766  << "Either the product in the Event was dropped or\n"
767  << "not produced or the configured input tag is incorrect or there is a bug somewhere\n";
768  return;
769  }
770  states->getRandomEngineStates(eventCache_.at(event.streamID()));
771  }
std::vector< std::vector< RandomEngineState > > eventCache_
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger but the state exists so we define the behavior If all triggers are the negative crieriion will lead to accepting the event(this again matches the behavior of"!*"before the partial wildcard feature was incorporated).The per-event"cost"of each negative criterion with multiple relevant triggers is about the same as!*was in the past
void edm::service::RandomNumberGeneratorService::readFromLuminosityBlock ( LuminosityBlock const &  lumi)
private

Definition at line 723 of file RandomNumberGeneratorService.cc.

References edm::errors::Configuration, Exception, edm::LuminosityBlock::getByLabel(), edm::LuminosityBlock::index(), edm::Service< T >::isAvailable(), edm::HandleBase::isValid(), lumiCache_, edm::InputTag::process(), edm::errors::ProductNotFound, restoreStateBeginLumiTag_, and restoreStateTag_.

Referenced by preBeginLumi().

723  {
724 
726  if(tns.isAvailable()) {
727  if(tns->getProcessName() == restoreStateTag_.process()) {
729  << "In the configuration for the RandomNumberGeneratorService the\n"
730  << "restoreStateTag contains the current process which is illegal.\n"
731  << "The process name in the replay process should have been changed\n"
732  << "to be different than the original process name and the restoreStateTag\n"
733  << "should contain either the original process name or an empty process name.\n";
734  }
735  }
736 
738  lumi.getByLabel(restoreStateBeginLumiTag_, states);
739 
740  if(!states.isValid()) {
742  << "The RandomNumberGeneratorService is trying to restore\n"
743  << "the state of the random engines by reading a product from\n"
744  << "the LuminosityBlock with input tag \"" << restoreStateBeginLumiTag_ << "\".\n"
745  << "It could not find the product.\n"
746  << "Either the product in the LuminosityBlock was dropped or\n"
747  << "not produced or the configured input tag is incorrect or there is a bug somewhere\n";
748  return;
749  }
750  states->getRandomEngineStates(lumiCache_.at(lumi.index()));
751  }
tuple lumi
Definition: fjr2json.py:35
std::vector< std::vector< RandomEngineState > > lumiCache_
std::string const & process() const
Definition: InputTag.h:47
void edm::service::RandomNumberGeneratorService::readLumiStatesFromTextFile ( std::string const &  fileName,
std::vector< RandomEngineState > &  cache 
)
private

Definition at line 988 of file RandomNumberGeneratorService.cc.

References readStatesFromFile(), and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by preallocate().

989  {
990  std::string whichStates("<Lumi>");
991  readStatesFromFile(fileName, cache, whichStates);
992  }
void readStatesFromFile(std::string const &fileName, std::vector< RandomEngineState > &cache, std::string const &whichStates)
void edm::service::RandomNumberGeneratorService::readStatesFromFile ( std::string const &  fileName,
std::vector< RandomEngineState > &  cache,
std::string const &  whichStates 
)
private

Definition at line 996 of file RandomNumberGeneratorService.cc.

References edm::errors::Configuration, Exception, recoMuon::in, readEngineState(), restoreFileName_, AlCaHLTBitMon_QueryRunRegistry::string, and runonSM::text.

Referenced by readEventStatesFromTextFile(), and readLumiStatesFromTextFile().

998  {
999  std::ifstream inFile;
1000  inFile.open(fileName.c_str(), std::ifstream::in);
1001  if(!inFile) {
1003  << "Unable to open the file \""
1004  << fileName << "\" to restore the random engine states.\n";
1005  }
1006 
1007  std::string text;
1008  inFile >> text;
1009  if(!inFile.good() || text != std::string("<RandomEngineStates>")) {
1011  << "Attempting to read file with random number engine states.\n"
1012  << "File \"" << restoreFileName_
1013  << "\" is ill-structured or otherwise corrupted.\n"
1014  << "Cannot read the file header word.\n";
1015  }
1016  bool saveToCache = false;
1017  while(readEngineState(inFile, cache, whichStates, saveToCache)) {}
1018  }
bool readEngineState(std::istream &is, std::vector< RandomEngineState > &cache, std::string const &whichStates, bool &saveToCache)
tuple text
Definition: runonSM.py:42
void edm::service::RandomNumberGeneratorService::readVector ( std::istream &  is,
unsigned  numItems,
std::vector< std::uint32_t > &  v 
)
private

Definition at line 1169 of file RandomNumberGeneratorService.cc.

References edm::errors::Configuration, data, Exception, i, and restoreFileName_.

Referenced by readEngineState().

1169  {
1170  v.clear();
1171  v.reserve(numItems);
1172  std::uint32_t data;
1173  for(unsigned i = 0; i < numItems; ++i) {
1174  is >> data;
1175  if(!is.good()) {
1177  << "File \"" << restoreFileName_
1178  << "\" is ill-structured or otherwise corrupted.\n"
1179  << "Cannot read vector when restoring random engine states.\n";
1180  }
1181  v.push_back(data);
1182  }
1183  }
int i
Definition: DBlmapReader.cc:9
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
void edm::service::RandomNumberGeneratorService::resetEngineSeeds ( LabelAndEngine labelAndEngine,
std::string const &  engineName,
VUint32 const &  seeds,
std::uint32_t  offset1,
std::uint32_t  offset2 
)
private

Definition at line 1243 of file RandomNumberGeneratorService.cc.

References assert(), edm::service::RandomNumberGeneratorService::LabelAndEngine::engine(), maxSeedHepJames, maxSeedRanecu, maxSeedTRandom3, mod(), and edm::service::RandomNumberGeneratorService::LabelAndEngine::setSeed().

Referenced by createEnginesInVector(), and postForkReacquireResources().

1247  {
1248 
1249  if(engineName == "RanecuEngine") {
1250  assert(seeds.size() == 2U);
1251  // Wrap around if the offsets push the seed over the maximum allowed value
1252  std::uint32_t mod = maxSeedRanecu + 1U;
1253  offset1 %= mod;
1254  offset2 %= mod;
1255  std::uint32_t seed0 = (seeds[0] + offset1) % mod;
1256  seed0 = (seed0 + offset2) % mod;
1257  labelAndEngine.setSeed(seed0, 0);
1258  labelAndEngine.setSeed(seeds[1], 1);
1259  long int seedL[2];
1260  seedL[0] = static_cast<long int>(seed0);
1261  seedL[1] = static_cast<long int>(seeds[1]);
1262  labelAndEngine.engine()->setSeeds(seedL,0);
1263  } else {
1264  assert(seeds.size() == 1U);
1265 
1266  if(engineName == "HepJamesRandom") {
1267  // Wrap around if the offsets push the seed over the maximum allowed value
1268  std::uint32_t mod = maxSeedHepJames + 1U;
1269  offset1 %= mod;
1270  offset2 %= mod;
1271  std::uint32_t seed0 = (seeds[0] + offset1) % mod;
1272  seed0 = (seed0 + offset2) % mod;
1273  labelAndEngine.setSeed(seed0, 0);
1274 
1275  long int seedL = static_cast<long int>(seed0);
1276  labelAndEngine.engine()->setSeed(seedL, 0);
1277  } else {
1278  assert(engineName == "TRandom3");
1279  // Wrap around if the offsets push the seed over the maximum allowed value
1280  // We have to be extra careful with this one because it may also go beyond
1281  // the values 32 bits can hold
1282  std::uint32_t max32 = maxSeedTRandom3;
1283  std::uint32_t seed0 = seeds[0];
1284  if((max32 - seed0) >= offset1) {
1285  seed0 += offset1;
1286  } else {
1287  seed0 = offset1 - (max32 - seed0) - 1U;
1288  }
1289  if((max32 - seed0) >= offset2) {
1290  seed0 += offset2;
1291  } else {
1292  seed0 = offset2 - (max32 - seed0) - 1U;
1293  }
1294  labelAndEngine.setSeed(seed0, 0);
1295 
1296  long seedL = static_cast<long>(seed0);
1297 
1298  // There is a dangerous conversion from std::uint32_t to long
1299  // that occurs above. In the next 2 lines we check the
1300  // behavior is what we need for the service to work
1301  // properly. This conversion is forced on us by the
1302  // CLHEP and ROOT interfaces. If the assert ever starts
1303  // to fail we will have to come up with a way to deal
1304  // with this.
1305  std::uint32_t seedu32 = static_cast<std::uint32_t>(seedL);
1306  assert(seed0 == seedu32);
1307 
1308  labelAndEngine.engine()->setSeed(seedL, 0);
1309  }
1310  }
1311  }
assert(m_qm.get())
T mod(const T &a, const T &b)
Definition: ecalDccMap.h:4
void edm::service::RandomNumberGeneratorService::restoreFromCache ( std::vector< RandomEngineState > const &  cache,
std::vector< LabelAndEngine > &  engines 
)
private

Definition at line 796 of file RandomNumberGeneratorService.cc.

References assert(), checkEngineType(), Exception, AlCaHLTBitMon_QueryRunRegistry::string, edm::errors::Unknown, and relativeConstraints::value.

Referenced by postEventRead(), preallocate(), and preBeginLumi().

797  {
798  std::vector<LabelAndEngine>::iterator labelAndEngine = engines.begin();
799  for(auto const& cachedState : cache) {
800 
801  std::string const& engineLabel = cachedState.getLabel();
802 
803  std::vector<std::uint32_t> const& engineState = cachedState.getState();
804  std::vector<unsigned long> engineStateL;
805  engineStateL.reserve(engineState.size());
806  for(auto const& value : engineState) {
807  engineStateL.push_back(static_cast<unsigned long>(value));
808  }
809 
810  std::vector<std::uint32_t> const& engineSeeds = cachedState.getSeed();
811  std::vector<long> engineSeedsL;
812  engineSeedsL.reserve(engineSeeds.size());
813  for(auto const& val : engineSeeds) {
814  long seedL = static_cast<long>(val);
815  engineSeedsL.push_back(seedL);
816 
817  // There is a dangerous conversion from std::uint32_t to long
818  // that occurs above. In the next 2 lines we check the
819  // behavior is what we need for the service to work
820  // properly. This conversion is forced on us by the
821  // CLHEP and ROOT interfaces. If the assert ever starts
822  // to fail we will have to come up with a way to deal
823  // with this.
824  std::uint32_t seedu32 = static_cast<std::uint32_t>(seedL);
825  assert(val == seedu32);
826  }
827 
828  assert(labelAndEngine != engines.end() && engineLabel == labelAndEngine->label());
829  std::shared_ptr<CLHEP::HepRandomEngine> const& engine = labelAndEngine->engine();
830 
831  // We need to handle each type of engine differently because each
832  // has different requirements on the seed or seeds.
833  if(engineStateL[0] == CLHEP::engineIDulong<CLHEP::HepJamesRandom>()) {
834 
835  checkEngineType(engine->name(), std::string("HepJamesRandom"), engineLabel);
836 
837  // These two lines actually restore the seed and engine state.
838  engine->setSeed(engineSeedsL[0], 0);
839  engine->get(engineStateL);
840 
841  labelAndEngine->setSeed(engineSeeds[0], 0);
842  } else if(engineStateL[0] == CLHEP::engineIDulong<CLHEP::RanecuEngine>()) {
843 
844  checkEngineType(engine->name(), std::string("RanecuEngine"), engineLabel);
845 
846  // This line actually restores the engine state.
847  engine->get(engineStateL);
848 
849  labelAndEngine->setSeed(engineSeeds[0], 0);
850  labelAndEngine->setSeed(engineSeeds[1], 1);
851  } else if(engineStateL[0] == CLHEP::engineIDulong<TRandomAdaptor>()) {
852 
853  checkEngineType(engine->name(), std::string("TRandom3"), engineLabel);
854 
855  // This line actually restores the engine state.
856  engine->setSeed(engineSeedsL[0], 0);
857  engine->get(engineStateL);
858 
859  labelAndEngine->setSeed(engineSeeds[0], 0);
860  } else {
861  // This should not be possible because this code should be able to restore
862  // any kind of engine whose state can be saved.
864  << "The RandomNumberGeneratorService is trying to restore the state\n"
865  "of the random engines. The state in the event indicates an engine\n"
866  "of an unknown type. This should not be possible unless you are\n"
867  "running with an old code release on a new file that was created\n"
868  "with a newer release which had new engine types added. In this case\n"
869  "the only solution is to use a newer release. In any other case, notify\n"
870  "the EDM developers because this should not be possible\n";
871  }
872  ++labelAndEngine;
873  }
874  }
assert(m_qm.get())
void checkEngineType(std::string const &typeFromConfig, std::string const &typeFromEvent, std::string const &engineLabel) const
void edm::service::RandomNumberGeneratorService::saveStatesToFile ( std::string const &  fileName,
StreamID const &  streamID,
LuminosityBlockIndex const &  lumiIndex 
)
private

Definition at line 898 of file RandomNumberGeneratorService.cc.

References edm::errors::Configuration, eventCache_, Exception, mergeVDriftHistosByStation::file, MillePedeFileConverter_cfg::fileName, lumiCache_, nStreams_, dbtoconf::out, GetRecoTauVFromDQM_MC_cff::outFile, outFiles_, estimatePileup_makeJSON::trunc, edm::StreamID::value(), and writeStates().

Referenced by postEventRead().

900  {
901 
902  std::ofstream& outFile = *outFiles_.at(streamID);
903 
904  if(!outFile.is_open()) {
905  std::stringstream file;
906  file << fileName;
907  if(nStreams_ > 1) {
908  file << "_" << streamID.value();
909  }
910 
911  outFile.open(file.str().c_str(), std::ofstream::out | std::ofstream::trunc);
912 
913  if(!outFile) {
915  << "Unable to open the file \""
916  << file.str() << "\" to save the state of the random engines.\n";
917  }
918  }
919 
920  outFile.seekp(0, std::ios_base::beg);
921  outFile << "<RandomEngineStates>\n";
922 
923  outFile << "<Event>\n";
924  writeStates(eventCache_.at(streamID), outFile);
925  outFile << "</Event>\n";
926 
927  outFile << "<Lumi>\n";
928  writeStates(lumiCache_.at(lumiIndex), outFile);
929  outFile << "</Lumi>\n";
930 
931  outFile << "</RandomEngineStates>\n";
932  outFile.flush();
933  }
void writeStates(std::vector< RandomEngineState > const &v, std::ofstream &outFile)
std::vector< std::shared_ptr< std::ofstream > > outFiles_
std::vector< std::vector< RandomEngineState > > eventCache_
std::vector< std::vector< RandomEngineState > > lumiCache_
tuple out
Definition: dbtoconf.py:99
void edm::service::RandomNumberGeneratorService::snapShot ( std::vector< LabelAndEngine > const &  engines,
std::vector< RandomEngineState > &  cache 
)
private

Definition at line 774 of file RandomNumberGeneratorService.cc.

References diffTwoXMLs::label, and AlCaHLTBitMon_QueryRunRegistry::string.

Referenced by postEventRead(), postForkReacquireResources(), and preallocate().

774  {
775  cache.resize(engines.size());
776  std::vector<RandomEngineState>::iterator state = cache.begin();
777 
778  for(std::vector<LabelAndEngine>::const_iterator iter = engines.begin();
779  iter != engines.end();
780  ++iter, ++state) {
781 
782  std::string const& label = iter->label();
783  state->setLabel(label);
784  state->setSeed(iter->seeds());
785 
786  std::vector<unsigned long> stateL = iter->engine()->put();
787  state->clearStateVector();
788  state->reserveStateVector(stateL.size());
789  for(auto element : stateL) {
790  state->push_back_stateVector(static_cast<std::uint32_t>(element));
791  }
792  }
793  }
void edm::service::RandomNumberGeneratorService::writeStates ( std::vector< RandomEngineState > const &  v,
std::ofstream &  outFile 
)
private

Definition at line 936 of file RandomNumberGeneratorService.cc.

References writeVector().

Referenced by saveStatesToFile().

937  {
938  for(std::vector<RandomEngineState>::const_iterator iter = v.begin(),
939  iEnd = v.end();
940  iter != iEnd; ++iter) {
941 
942  std::vector<std::uint32_t> const& seedVector = iter->getSeed();
943  std::vector<std::uint32_t>::size_type seedVectorLength = seedVector.size();
944 
945  std::vector<std::uint32_t> const& stateVector = iter->getState();
946  std::vector<std::uint32_t>::size_type stateVectorLength = stateVector.size();
947 
948  outFile << "<ModuleLabel>\n" << iter->getLabel() << "\n</ModuleLabel>\n";
949 
950  outFile << "<SeedLength>\n" << seedVectorLength << "\n</SeedLength>\n" ;
951  outFile << "<InitialSeeds>\n";
952  writeVector(seedVector, outFile);
953  outFile << "</InitialSeeds>\n";
954  outFile << "<FullStateLength>\n" << stateVectorLength << "\n</FullStateLength>\n";
955  outFile << "<FullState>\n";
956  writeVector(stateVector, outFile);
957  outFile << "</FullState>\n";
958  }
959  }
uint16_t size_type
void writeVector(VUint32 const &v, std::ofstream &outFile)
void edm::service::RandomNumberGeneratorService::writeVector ( VUint32 const &  v,
std::ofstream &  outFile 
)
private

Definition at line 962 of file RandomNumberGeneratorService.cc.

References i.

Referenced by writeStates().

963  {
964  if(v.empty()) return;
965  size_t numItems = v.size();
966  for(size_t i = 0; i < numItems; ++i) {
967  if(i != 0 && i % 10 == 0) outFile << "\n";
968  outFile << std::setw(13) << v[i];
969  }
970  outFile << "\n";
971  }
int i
Definition: DBlmapReader.cc:9

Member Data Documentation

unsigned edm::service::RandomNumberGeneratorService::childIndex_
private

Definition at line 273 of file RandomNumberGeneratorService.h.

Referenced by postForkReacquireResources(), and print().

bool edm::service::RandomNumberGeneratorService::enableChecking_
private
std::vector<std::vector<RandomEngineState> > edm::service::RandomNumberGeneratorService::eventCache_
private
std::uint32_t edm::service::RandomNumberGeneratorService::eventSeedOffset_
private

Definition at line 275 of file RandomNumberGeneratorService.h.

Referenced by postForkReacquireResources(), preallocate(), and print().

std::vector<std::vector<RandomEngineState> > edm::service::RandomNumberGeneratorService::lumiCache_
private
std::vector<std::vector<LabelAndEngine> > edm::service::RandomNumberGeneratorService::lumiEngines_
private
std::vector<std::vector<ModuleIDToEngine> > edm::service::RandomNumberGeneratorService::lumiModuleIDToEngine_
private

Definition at line 218 of file RandomNumberGeneratorService.h.

Referenced by getEngine(), and preallocate().

const std::uint32_t edm::service::RandomNumberGeneratorService::maxSeedHepJames = 900000000U
staticprivate
const std::uint32_t edm::service::RandomNumberGeneratorService::maxSeedRanecu = 2147483647U
staticprivate
const std::vector< std::uint32_t >::size_type edm::service::RandomNumberGeneratorService::maxSeeds = 65536U
staticprivate

Definition at line 283 of file RandomNumberGeneratorService.h.

Referenced by readEngineState().

const std::uint32_t edm::service::RandomNumberGeneratorService::maxSeedTRandom3 = 4294967295U
staticprivate

Definition at line 287 of file RandomNumberGeneratorService.h.

Referenced by resetEngineSeeds().

const std::vector< std::uint32_t >::size_type edm::service::RandomNumberGeneratorService::maxStates = 65536U
staticprivate

Definition at line 284 of file RandomNumberGeneratorService.h.

Referenced by readEngineState().

thread_local std::string edm::service::RandomNumberGeneratorService::moduleLabel_
staticprivate
unsigned int edm::service::RandomNumberGeneratorService::nStreams_
private
std::vector<std::shared_ptr<std::ofstream> > edm::service::RandomNumberGeneratorService::outFiles_
private

Definition at line 259 of file RandomNumberGeneratorService.h.

Referenced by preallocate(), and saveStatesToFile().

std::string edm::service::RandomNumberGeneratorService::restoreFileName_
private
edm::InputTag edm::service::RandomNumberGeneratorService::restoreStateBeginLumiTag_
private
edm::InputTag edm::service::RandomNumberGeneratorService::restoreStateTag_
private
std::string edm::service::RandomNumberGeneratorService::saveFileName_
private
std::atomic<bool> edm::service::RandomNumberGeneratorService::saveFileNameRecorded_
private

Definition at line 258 of file RandomNumberGeneratorService.h.

Referenced by postEventRead(), and print().

std::map<std::string, SeedsAndName> edm::service::RandomNumberGeneratorService::seedsAndNameMap_
private
std::vector<std::vector<LabelAndEngine> > edm::service::RandomNumberGeneratorService::streamEngines_
private
std::vector<std::vector<ModuleIDToEngine> > edm::service::RandomNumberGeneratorService::streamModuleIDToEngine_
private
bool edm::service::RandomNumberGeneratorService::verbose_
private

Definition at line 277 of file RandomNumberGeneratorService.h.

Referenced by postForkReacquireResources(), preallocate(), and print().