19#include "BDSException.hh"
20#include "BDSGlobalConstants.hh"
21#include "BDSRandom.hh"
22#include "BDSUtilities.hh"
28#include "CLHEP/Random/Random.h"
29#include "CLHEP/Random/JamesRandom.h"
31#include "CLHEP/Random/MixMaxRng.h"
33#include "CLHEP/ClhepVersion.h"
43 new std::map<BDSRandomEngineType, std::string> ({
44 {BDSRandomEngineType::hepjames,
"hepjames"},
45 {BDSRandomEngineType::mixmax,
"mixmax"}
50 std::map<G4String, BDSRandomEngineType> types;
51 types[
"hepjames"] = BDSRandomEngineType::hepjames;
52 types[
"mixmax"] = BDSRandomEngineType::mixmax;
56 auto result = types.find(engineType);
57 if (result == types.end())
59 G4String msg =
"\"" + engineType +
"\" is not a valid random engine\n";
60 msg +=
"Available random engines are:\n";
61 for (
auto& it : types)
62 {msg +=
"\"" + it.first +
"\"\n";}
65 return result->second;
68void BDSRandom::CreateRandomNumberGenerator(
const G4String& engineName)
70 auto et = BDSRandom::DetermineRandomEngineType(engineName);
71 G4cout << __METHOD_NAME__ <<
"Engine name: " << et.ToString() << G4endl;
72 switch (et.underlying())
74 case BDSRandomEngineType::hepjames:
75 {CLHEP::HepRandom::setTheEngine(
new CLHEP::HepJamesRandom());
break;}
76 case BDSRandomEngineType::mixmax:
78 {CLHEP::HepRandom::setTheEngine(
new CLHEP::MixMaxRng());
break;}
81 G4String msg = G4String(et.ToString()) +
" is not available in CLHEP version: " + G4String(CLHEP::Version::String());
87 {
throw BDSException(__METHOD_NAME__,
"engine \"" + engineName +
"\" not implemented");
break;}
91void BDSRandom::SetSeed()
94 G4cout << __METHOD_NAME__ <<
"set the seed" << G4endl;
95 G4cout << __METHOD_NAME__ <<
"seed from BDSGlobalConstants = "
97 << __METHOD_NAME__ <<
"seed set in GMAD options: "
104 {seed = time(
nullptr);}
109 G4cout << __METHOD_NAME__ <<
"selected seed = " << seed << G4endl;
112 CLHEP::HepRandom::setTheSeed(seed);
115 G4cout << __METHOD_NAME__ <<
"Random number generator's seed = "
116 << CLHEP::HepRandom::getTheSeed() << G4endl;
118 BDSRandom::PrintFullSeedState();
122void BDSRandom::PrintFullSeedState()
124 G4cout << __METHOD_NAME__ <<
"Random number generator's state: " << G4endl << G4endl;
125 CLHEP::HepRandom::saveFullState(G4cout);
129void BDSRandom::WriteSeedState(
const G4String& suffix)
132 G4String seedstatefilename = baseFileName + suffix +
".seedstate.txt";
133 std::ofstream ofseedstate;
134 ofseedstate.open(seedstatefilename);
135 CLHEP::HepRandom::saveFullState(ofseedstate);
139G4String BDSRandom::GetSeedState()
141 std::stringstream currentState;
142 CLHEP::HepRandom::saveFullState(currentState);
143 return G4String(currentState.str());
146void BDSRandom::LoadSeedState(
const G4String& inSeedFilename)
149 G4cout << __METHOD_NAME__ <<
"loading file: " << inSeedFilename << G4endl;
151 std::ifstream ifseedstate;
152 ifseedstate.open(inSeedFilename);
153 if (ifseedstate.is_open())
154 {CLHEP::HepRandom::restoreFullState(ifseedstate);}
156 {
throw BDSException(__METHOD_NAME__,
"cannot open file : " + inSeedFilename);}
159 BDSRandom::PrintFullSeedState();
163void BDSRandom::SetSeedState(
const G4String& seedState)
165 if (seedState.empty())
166 {G4cout << __METHOD_NAME__ <<
"empty seed state supplied - no seed state set" << G4endl;
return;}
167 std::stringstream ss;
172void BDSRandom::SetSeedState(std::stringstream& seedState)
174 CLHEP::HepRandom::restoreFullState(seedState);
176 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.