CMS 3D CMS Logo

List of all members | Public Member Functions | Private Member Functions | Private Attributes
edm::ExternalRandomNumberGeneratorService Class Reference

#include <ExternalRandomNumberGeneratorService.h>

Inheritance diagram for edm::ExternalRandomNumberGeneratorService:
edm::RandomNumberGenerator

Public Member Functions

std::unique_ptr< CLHEP::HepRandomEngine > cloneEngine (LuminosityBlockIndex const &) final
 
void consumes (ConsumesCollector &&iC) const final
 
 ExternalRandomNumberGeneratorService ()
 
CLHEP::HepRandomEngine & getEngine (LuminosityBlockIndex const &) final
 Use this engine in the global begin luminosity block method. More...
 
CLHEP::HepRandomEngine & getEngine (StreamID const &) final
 Use this engine in event methods. More...
 
std::vector< RandomEngineState > const & getEventCache (StreamID const &) const final
 
std::vector< RandomEngineState > const & getLumiCache (LuminosityBlockIndex const &) const final
 
std::vector< unsigned long > getState () const
 
std::uint32_t mySeed () const final
 
void postEventRead (Event const &event) final
 
void preBeginLumi (LuminosityBlock const &lumi) final
 
void print (std::ostream &os) const final
 For debugging purposes only. More...
 
void setEventCache (StreamID, std::vector< RandomEngineState > const &iStates) final
 
void setLumiCache (LuminosityBlockIndex, std::vector< RandomEngineState > const &iStates) final
 
void setState (std::vector< unsigned long > const &, long seed)
 
- Public Member Functions inherited from edm::RandomNumberGenerator
RandomNumberGenerator const & operator= (RandomNumberGenerator const &)=delete
 
 RandomNumberGenerator ()
 
 RandomNumberGenerator (RandomNumberGenerator const &)=delete
 
virtual ~RandomNumberGenerator ()
 

Private Member Functions

std::unique_ptr< CLHEP::HepRandomEngine > createFromState (std::vector< unsigned long > const &, long seed) const
 
 ExternalRandomNumberGeneratorService (ExternalRandomNumberGeneratorService const &)=delete
 
ExternalRandomNumberGeneratorService const & operator= (ExternalRandomNumberGeneratorService const &)=delete
 

Private Attributes

std::unique_ptr< CLHEP::HepRandomEngine > engine_
 

Detailed Description

Definition at line 20 of file ExternalRandomNumberGeneratorService.h.

Constructor & Destructor Documentation

◆ ExternalRandomNumberGeneratorService() [1/2]

ExternalRandomNumberGeneratorService::ExternalRandomNumberGeneratorService ( )

Definition at line 22 of file ExternalRandomNumberGeneratorService.cc.

22 {}

◆ ExternalRandomNumberGeneratorService() [2/2]

edm::ExternalRandomNumberGeneratorService::ExternalRandomNumberGeneratorService ( ExternalRandomNumberGeneratorService const &  )
privatedelete

Member Function Documentation

◆ cloneEngine()

std::unique_ptr< CLHEP::HepRandomEngine > ExternalRandomNumberGeneratorService::cloneEngine ( LuminosityBlockIndex const &  )
finalvirtual

This function is not intended for general use. It is intended for the special case where multiple instances of Pythia 8 will be run concurrently and we want them to be initialized exactly the same. In this special case, the luminosity block engine(s) owned by the service should not be used to generate random numbers in between calls to cloneEngine, because the clone will be in the state that existed at the moment of cloning. Before initializing Pythia, this function should be used to clone the engine owned by the service and the cloned random engine should be used to generate numbers for initialization so that all initializations in the process get identical sequences of random numbers.

Implements edm::RandomNumberGenerator.

Definition at line 39 of file ExternalRandomNumberGeneratorService.cc.

39  {
40  std::vector<unsigned long> stateL = engine_->put();
41 
42  long seedL = engine_->getSeed();
43  return createFromState(stateL, seedL);
44 }

References createFromState(), and engine_.

◆ consumes()

void ExternalRandomNumberGeneratorService::consumes ( ConsumesCollector &&  iC) const
finalvirtual

Implements edm::RandomNumberGenerator.

Definition at line 88 of file ExternalRandomNumberGeneratorService.cc.

88 {}

◆ createFromState()

std::unique_ptr< CLHEP::HepRandomEngine > ExternalRandomNumberGeneratorService::createFromState ( std::vector< unsigned long > const &  stateL,
long  seed 
) const
private

Definition at line 46 of file ExternalRandomNumberGeneratorService.cc.

47  {
48  std::unique_ptr<CLHEP::HepRandomEngine> newEngine;
49  if (stateL[0] == CLHEP::engineIDulong<CLHEP::HepJamesRandom>()) {
50  newEngine = std::make_unique<CLHEP::HepJamesRandom>(seedL);
51  } else if (stateL[0] == CLHEP::engineIDulong<CLHEP::RanecuEngine>()) {
52  newEngine = std::make_unique<CLHEP::RanecuEngine>();
53  } else if (stateL[0] == CLHEP::engineIDulong<CLHEP::MixMaxRng>()) {
54  newEngine = std::make_unique<CLHEP::MixMaxRng>(seedL);
55  //} else if (stateL[0] == CLHEP::engineIDulong<TRandomAdaptor>()) {
56  // newEngine = std::make_unique<TRandomAdaptor>(seedL);
57  } else {
58  // Sanity check, it should not be possible for this to happen.
60  << "The ExternalRandomNumberGeneratorService is trying to clone unknown engine type\n";
61  }
62  if (stateL[0] != CLHEP::engineIDulong<CLHEP::RanecuEngine>()) {
63  newEngine->setSeed(seedL, 0);
64  }
65  newEngine->get(stateL);
66  return newEngine;
67 }

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

Referenced by cloneEngine(), and setState().

◆ getEngine() [1/2]

CLHEP::HepRandomEngine & ExternalRandomNumberGeneratorService::getEngine ( LuminosityBlockIndex const &  )
finalvirtual

Use this engine in the global begin luminosity block method.

Implements edm::RandomNumberGenerator.

Definition at line 35 of file ExternalRandomNumberGeneratorService.cc.

35  {
36  return *engine_;
37 }

References engine_.

◆ getEngine() [2/2]

CLHEP::HepRandomEngine & ExternalRandomNumberGeneratorService::getEngine ( StreamID const &  )
finalvirtual

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 34 of file ExternalRandomNumberGeneratorService.cc.

34 { return *engine_; }

References engine_.

◆ getEventCache()

std::vector< RandomEngineState > const & ExternalRandomNumberGeneratorService::getEventCache ( StreamID const &  ) const
finalvirtual

Implements edm::RandomNumberGenerator.

Definition at line 79 of file ExternalRandomNumberGeneratorService.cc.

79  {
80  return s_dummyStates;
81 }

◆ getLumiCache()

std::vector< RandomEngineState > const & ExternalRandomNumberGeneratorService::getLumiCache ( LuminosityBlockIndex const &  ) const
finalvirtual

Implements edm::RandomNumberGenerator.

Definition at line 83 of file ExternalRandomNumberGeneratorService.cc.

84  {
85  return s_dummyStates;
86 }

◆ getState()

std::vector< unsigned long > ExternalRandomNumberGeneratorService::getState ( ) const

Definition at line 32 of file ExternalRandomNumberGeneratorService.cc.

32 { return engine_->put(); }

References engine_.

Referenced by main().

◆ mySeed()

std::uint32_t ExternalRandomNumberGeneratorService::mySeed ( ) const
finalvirtual

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 69 of file ExternalRandomNumberGeneratorService.cc.

69 { return 0; }

Referenced by main().

◆ operator=()

ExternalRandomNumberGeneratorService const& edm::ExternalRandomNumberGeneratorService::operator= ( ExternalRandomNumberGeneratorService const &  )
privatedelete

◆ postEventRead()

void ExternalRandomNumberGeneratorService::postEventRead ( Event const &  event)
finalvirtual

Implements edm::RandomNumberGenerator.

Definition at line 73 of file ExternalRandomNumberGeneratorService.cc.

73 {}

◆ preBeginLumi()

void ExternalRandomNumberGeneratorService::preBeginLumi ( LuminosityBlock const &  lumi)
finalvirtual

Implements edm::RandomNumberGenerator.

Definition at line 71 of file ExternalRandomNumberGeneratorService.cc.

71 {}

◆ print()

void ExternalRandomNumberGeneratorService::print ( std::ostream &  os) const
finalvirtual

For debugging purposes only.

Implements edm::RandomNumberGenerator.

Definition at line 91 of file ExternalRandomNumberGeneratorService.cc.

91 {}

◆ setEventCache()

void ExternalRandomNumberGeneratorService::setEventCache ( StreamID  ,
std::vector< RandomEngineState > const &  iStates 
)
finalvirtual

Implements edm::RandomNumberGenerator.

Definition at line 77 of file ExternalRandomNumberGeneratorService.cc.

77 {}

◆ setLumiCache()

void ExternalRandomNumberGeneratorService::setLumiCache ( LuminosityBlockIndex  ,
std::vector< RandomEngineState > const &  iStates 
)
finalvirtual

Implements edm::RandomNumberGenerator.

Definition at line 75 of file ExternalRandomNumberGeneratorService.cc.

76  {}

◆ setState()

void ExternalRandomNumberGeneratorService::setState ( std::vector< unsigned long > const &  iState,
long  seed 
)

Definition at line 24 of file ExternalRandomNumberGeneratorService.cc.

24  {
25  if (not engine_) {
26  engine_ = createFromState(iState, iSeed);
27  } else {
28  engine_->get(iState);
29  }
30 }

References createFromState(), and engine_.

Referenced by main().

Member Data Documentation

◆ engine_

std::unique_ptr<CLHEP::HepRandomEngine> edm::ExternalRandomNumberGeneratorService::engine_
private

Definition at line 58 of file ExternalRandomNumberGeneratorService.h.

Referenced by cloneEngine(), getEngine(), getState(), and setState().

edm::ExternalRandomNumberGeneratorService::createFromState
std::unique_ptr< CLHEP::HepRandomEngine > createFromState(std::vector< unsigned long > const &, long seed) const
Definition: ExternalRandomNumberGeneratorService.cc:46
edm::errors::Unknown
Definition: EDMException.h:29
Exception
Definition: hltDiff.cc:246
edm::ExternalRandomNumberGeneratorService::engine_
std::unique_ptr< CLHEP::HepRandomEngine > engine_
Definition: ExternalRandomNumberGeneratorService.h:58