20#include "BDSException.hh"
21#include "BDSGlobalConstants.hh"
22#include "BDSRandom.hh"
23#include "BDSUtilities.hh"
29#include "CLHEP/Random/Random.h"
30#include "CLHEP/Random/JamesRandom.h"
32#include "CLHEP/Random/MixMaxRng.h"
34#include "CLHEP/ClhepVersion.h"
44 new std::map<BDSRandomEngineType, std::string> ({
45 {BDSRandomEngineType::hepjames,
"hepjames"},
46 {BDSRandomEngineType::mixmax,
"mixmax"}
51 std::map<G4String, BDSRandomEngineType> types;
52 types[
"hepjames"] = BDSRandomEngineType::hepjames;
53 types[
"mixmax"] = BDSRandomEngineType::mixmax;
57 auto result = types.find(engineType);
58 if (result == types.end())
60 G4String msg =
"\"" + engineType +
"\" is not a valid random engine\n";
61 msg +=
"Available random engines are:\n";
62 for (
auto& it : types)
63 {msg +=
"\"" + it.first +
"\"\n";}
66 return result->second;
69void BDSRandom::CreateRandomNumberGenerator(
const G4String& engineName)
71 auto et = BDSRandom::DetermineRandomEngineType(engineName);
72 G4cout << __METHOD_NAME__ <<
"Engine name: " << et.ToString() << G4endl;
73 switch (et.underlying())
75 case BDSRandomEngineType::hepjames:
76 {CLHEP::HepRandom::setTheEngine(
new CLHEP::HepJamesRandom());
break;}
77 case BDSRandomEngineType::mixmax:
79 {CLHEP::HepRandom::setTheEngine(
new CLHEP::MixMaxRng());
break;}
82 G4String msg = G4String(et.ToString()) +
" is not available in CLHEP version: " + G4String(CLHEP::Version::String());
88 {
throw BDSException(__METHOD_NAME__,
"engine \"" + engineName +
"\" not implemented");
break;}
92void BDSRandom::SetSeed()
95 G4cout << __METHOD_NAME__ <<
"set the seed" << G4endl;
96 G4cout << __METHOD_NAME__ <<
"seed from BDSGlobalConstants = "
98 << __METHOD_NAME__ <<
"seed set in GMAD options: "
105 {seed = time(
nullptr);}
110 G4cout << __METHOD_NAME__ <<
"selected seed = " << seed << G4endl;
113 CLHEP::HepRandom::setTheSeed(seed);
116 G4cout << __METHOD_NAME__ <<
"Random number generator's seed = "
117 << CLHEP::HepRandom::getTheSeed() << G4endl;
119 BDSRandom::PrintFullSeedState();
123void BDSRandom::PrintFullSeedState()
125 G4cout << __METHOD_NAME__ <<
"Random number generator's state: " << G4endl << G4endl;
126 CLHEP::HepRandom::saveFullState(G4cout);
130void BDSRandom::WriteSeedState(
const G4String& suffix)
133 G4String seedstatefilename = baseFileName + suffix +
".seedstate.txt";
134 std::ofstream ofseedstate;
135 ofseedstate.open(seedstatefilename);
136 CLHEP::HepRandom::saveFullState(ofseedstate);
140G4String BDSRandom::GetSeedState()
142 std::stringstream currentState;
143 CLHEP::HepRandom::saveFullState(currentState);
144 return G4String(currentState.str());
147void BDSRandom::LoadSeedState(
const G4String& inSeedFilename)
150 G4cout << __METHOD_NAME__ <<
"loading file: " << inSeedFilename << G4endl;
152 std::ifstream ifseedstate;
153 ifseedstate.open(inSeedFilename);
154 if (ifseedstate.is_open())
155 {CLHEP::HepRandom::restoreFullState(ifseedstate);}
157 {
throw BDSException(__METHOD_NAME__,
"cannot open file : " + inSeedFilename);}
160 BDSRandom::PrintFullSeedState();
164void BDSRandom::SetSeedState(
const G4String& seedState)
166 if (seedState.empty())
167 {G4cout << __METHOD_NAME__ <<
"empty seed state supplied - no seed state set" << G4endl;
return;}
168 std::stringstream ss;
173void BDSRandom::SetSeedState(std::stringstream& seedState)
175 CLHEP::HepRandom::restoreFullState(seedState);
177 BDSRandom::PrintFullSeedState();
General exception with possible name of object and message.
static BDSGlobalConstants * Instance()
Access method.
Improve type-safety of native enum data type in C++.
static std::map< BDSTypeSafeEnum< def, inner >, std::string > * dictionary
G4String LowerCase(const G4String &str)
Utility function to simplify lots of syntax changes for pedantic g4 changes.