20 #include "CLHEP/Random/EngineFactory.h"
21 #include "CLHEP/Random/Random.h"
30 using namespace DD4hep::Simulation;
33 unsigned long crc32ul(
const std::string&
s);
39 class RNDM :
public TRandom {
43 CLHEP::HepRandomEngine* m_engine;
48 m_engine = m_generator->engine();
53 virtual void SetSeed(UInt_t seed=0) {
55 m_generator->setSeed((
long)seed);
58 virtual void SetSeed(ULong_t seed=0) {
60 m_generator->setSeed((
long)seed);
63 virtual Double_t Rndm(Int_t=0) {
64 return m_engine->flat();
67 virtual void RndmArray(Int_t size, Float_t *array) {
68 for (Int_t i=0;i<size;i++) array[i] = m_engine->flat();
71 virtual void RndmArray(Int_t size, Double_t *array) {
72 m_engine->flatArray(size,array);
80 :
Geant4Action(ctxt,nam), m_engine(0), m_rootRandom(0), m_rootOLD(0),
89 m_engine = CLHEP::HepRandom::getTheEngine();
103 if ( s_instance ==
this ) s_instance = 0;
111 if ( !s_instance && throw_exception ) {
112 throw runtime_error(
"No global random number generator defined!");
122 if ( s_instance != ptr ) {
124 throw runtime_error(
"Attempt to declare invalid Geant4Random instance.");
127 throw runtime_error(
"Attempt to declare uninitialized Geant4Random instance.");
130 CLHEP::HepRandomEngine* curr = CLHEP::HepRandom::getTheEngine();
132 ptr->
printP2(
"Moving CLHEP random instance from %p to %p",curr,ptr->
m_engine);
133 CLHEP::HepRandom::setTheEngine(ptr->
m_engine);
145 #include "CLHEP/Random/DualRand.h"
146 #include "CLHEP/Random/JamesRandom.h"
147 #include "CLHEP/Random/MTwistEngine.h"
148 #include "CLHEP/Random/RanecuEngine.h"
149 #include "CLHEP/Random/Ranlux64Engine.h"
150 #include "CLHEP/Random/RanluxEngine.h"
151 #include "CLHEP/Random/RanshiEngine.h"
152 #include "CLHEP/Random/NonRandomEngine.h"
157 ifstream in(
m_file.c_str(), std::ios::in);
158 m_engine = CLHEP::EngineFactory::newEngine(in);
160 except(
"Failed to create CLHEP random engine from file:%s.",
m_file.c_str());
162 m_seed = m_engine->getSeed();
166 if (
m_engineType == CLHEP::HepJamesRandom::engineName() )
167 m_engine =
new CLHEP::HepJamesRandom();
168 else if (
m_engineType == CLHEP::RanecuEngine::engineName() )
169 m_engine =
new CLHEP::RanecuEngine();
170 else if (
m_engineType == CLHEP::Ranlux64Engine::engineName() )
171 m_engine =
new CLHEP::Ranlux64Engine();
172 else if (
m_engineType == CLHEP::MTwistEngine::engineName() )
173 m_engine =
new CLHEP::MTwistEngine();
174 else if (
m_engineType == CLHEP::DualRand::engineName() )
176 else if (
m_engineType == CLHEP::RanluxEngine::engineName() )
177 m_engine =
new CLHEP::RanluxEngine();
178 else if (
m_engineType == CLHEP::RanshiEngine::engineName() )
179 m_engine =
new CLHEP::RanshiEngine();
180 else if (
m_engineType == CLHEP::NonRandomEngine::engineName() )
181 m_engine =
new CLHEP::NonRandomEngine();
190 if ( 0 == s_instance ) {
215 except(
"Failed to save RandomGenerator status. [Not-inited]");
229 error(
"Failed to show RandomGenerator status. [Not-inited]");
232 printP2(
"Random engine status of object of type Geant4Random @ 0x%p",
this);
236 printP2(
" Special instance created of type:%s @ 0x%p",
239 printP2(
" Reused HepRandom engine instance %s @ 0x%p",
242 if (
m_engine == CLHEP::HepRandom::getTheEngine() )
243 printP2(
" Instance is identical to Geant4's HepRandom instance.");
245 printP2(
" Instance is %sidentical to ROOT's gRandom instance.",
263 return gRandom->Rndm(i);
269 gRandom->RndmArray(n,array);
275 gRandom->RndmArray(n,array);
281 return gRandom->Uniform(x1);
287 return gRandom->Uniform(x1,x2);
293 return gRandom->Exp(tau);
299 return gRandom->Gaus(mean,sigma);
305 return gRandom->Landau(mean,sigma);
311 gRandom->Circle(x,y,r);
317 gRandom->Sphere(x,y,z,r);
TRandom * m_rootRandom
Reference to ROOT random instance.
virtual void setSeeds(const long *seeds, int size)
Should initialise the status of the algorithm.
void deletePtr(T *&p)
Helper to delete objects from heap and reset the pointer. Saves many many lines of code...
double uniform(double x1=1)
Create uniformly disributed random numbers in the interval ]0,x1].
static void decrement(T *)
Decrement count according to type information.
unsigned long crc32ul(const std::string &s)
bool m_inited
Flag to remember initialization.
double exp(double tau)
Create exponentially distributed random numbers.
double landau(double mean=0, double sigma=1)
Create landau distributed random numbers.
double rndm_clhep()
Create flat distributed random numbers in the interval ]0,1] calling CLHEP.
void sphere(double &x, double &y, double &z, double r)
Create tuple of randum number on a sphere with radius r.
void error(const char *fmt,...) const
Support of error messages.
void printP2(const char *fmt,...) const
Support for messages with variable output level using output level+2.
void circle(double &x, double &y, double r)
Create tuple of randum number around a circle with radius r.
double rndm(int i=0)
Create flat distributed random numbers in the interval ]0,1].
void except(const char *fmt,...) const
Support of exceptions: Print fatal message and throw runtime_error.
long m_seed
Property: Initial random seed. Default: 123456789.
static Geant4Random * setMainInstance(Geant4Random *ptr)
Make this random generator instance the one used by Geant4.
virtual void saveStatus(const char filename[]="Config.conf") const
Should save on a file specific to the instantiated engine in use the current status.
void initialize()
Initialize the instance.
static Geant4Random * instance(bool throw_exception=true)
Access the main Geant4 random generator instance. Must be created before used!
bool m_replace
Property: Indicator to replace the ROOT gRandom instance.
void rndmArray(int n, float *array)
Create a float array of flat distributed random numbers in the interval ]0,1].
CLHEP::HepRandomEngine * m_engine
Reference to the CLHEP random number engine (valid only after initialization)
virtual void setSeed(long seed)
Should initialise the status of the algorithm according to seed.
virtual ~Geant4Random()
Default destructor.
double gauss(double mean=0, double sigma=1)
Create gaussian distributed random numbers.
static void increment(T *)
Increment count according to type information.
std::string m_engineType
Property: Engine type. default: "HepJamesRandom".
virtual void restoreStatus(const char filename[]="Config.conf")
Should read from a file and restore the last saved engine configuration.
virtual void showStatus() const
Should dump the current engine status on the screen.
Generic context to extend user, run and event information.
Mini interface to THE random generator of the application.
std::string m_file
Property: File name if initialized from file. If set, engine name and seeds are ignored.
Geant4Action & declareProperty(const std::string &nam, T &val)
Declare property.
Default base class for all Geant 4 actions and derivates thereof.