Geant4 9.6.0
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Random.cc
Go to the documentation of this file.
1// $Id:$
2// -*- C++ -*-
3//
4// -----------------------------------------------------------------------
5// HEP Random
6// --- HepRandom ---
7// class implementation file
8// -----------------------------------------------------------------------
9// This file is part of Geant4 (simulation toolkit for HEP).
10
11// =======================================================================
12// Gabriele Cosmo - Created: 5th September 1995
13// - Minor corrections: 31st October 1996
14// - Added methods for engine status: 19th November 1996
15// - HepRandom defined as singleton, constructors are
16// kept public for backward compatibility: 27th Feb 1998
17// - Relocated Poisson and Gauss data and simplified
18// initialisation of static generator: 5th Jan 1999
19// =======================================================================
20
22#include "CLHEP/Random/Random.h"
25
26// -----------------------------
27// Static members initialisation
28// -----------------------------
29
31
32namespace CLHEP {
33
34
35namespace {
36
37struct defaults {
38 defaults( HepRandom & g, HepJamesRandom & e )
39 : theGenerator( &g, do_nothing_deleter() )
40 , theEngine ( &e, do_nothing_deleter() )
41 { }
42
43 void resetEngine( HepRandomEngine * newEngine ) {
44 theEngine.reset( newEngine );
45 }
46
47 void resetEngine( HepRandomEngine & newEngine ) {
48 theEngine.reset( &newEngine, do_nothing_deleter() );
49 }
50
51 bool ensureInitialized() {
52 assert( theGenerator.get() != 0 && theEngine.get() != 0 );
53 return true;
54 }
55
56 ~defaults()
57 { }
58
59 shared_ptr<HepRandom > theGenerator;
60 shared_ptr<HepRandomEngine> theEngine;
61}; // defaults
62
63 inline
64 defaults & theDefaults() {
65 static HepRandom theDefaultGenerator;
66 static HepJamesRandom theDefaultEngine;
67 static defaults theDefaults(theDefaultGenerator, theDefaultEngine);
68 return theDefaults;
69 }
70
71} // namespace
72
73//---------------------------- HepRandom ---------------------------------
74
76{ }
77
79{
80 setTheSeed(seed);
81}
82
84{
85 theDefaults().resetEngine( algorithm );
86}
87
89{
90 theDefaults().resetEngine( algorithm );
91}
92
94{ }
95
97{
98 return theDefaults().theEngine->flat();
99}
100
101void HepRandom::flatArray(const int size, double* vect)
102{
103 theDefaults().theEngine->flatArray(size,vect);
104}
105
107 return flat();
108}
109
110std::string HepRandom::name() const {return "HepRandom";}
112 std::cerr << "HepRandom::engine() called -- there is no assigned engine!\n";
113 return *theDefaults().theEngine.get();
114}
115
116std::ostream & operator<< (std::ostream & os, const HepRandom & dist) {
117 return dist.put(os);
118}
119
120std::istream & operator>> (std::istream & is, HepRandom & dist) {
121 return dist.get(is);
122}
123
124std::ostream & HepRandom::put(std::ostream & os) const {return os;}
125std::istream & HepRandom::get(std::istream & is) {return is;}
126
127// --------------------------
128// Static methods definitions
129// --------------------------
130
131void HepRandom::setTheSeed(long seed, int lux)
132{
133 theDefaults().theEngine->setSeed(seed,lux);
134}
135
137{
138 return theDefaults().theEngine->getSeed();
139}
140
141void HepRandom::setTheSeeds(const long* seeds, int aux)
142{
143 theDefaults().theEngine->setSeeds(seeds,aux);
144}
145
147{
148 return theDefaults().theEngine->getSeeds();
149}
150
151void HepRandom::getTheTableSeeds(long* seeds, int index)
152{
153 if ((index >= 0) && (index < 215)) {
154 seeds[0] = seedTable[index][0];
155 seeds[1] = seedTable[index][1];
156 }
157 else seeds = NULL;
158}
159
161{
162 return theDefaults().theGenerator.get();
163}
164
166{
167 return theDefaults().theEngine.get();
168}
169
171{
172 theDefaults().theEngine.reset( theNewEngine, do_nothing_deleter() );
173}
174
175void HepRandom::saveEngineStatus( const char filename[] )
176{
177 theDefaults().theEngine->saveStatus( filename );
178}
179
180void HepRandom::restoreEngineStatus( const char filename[] )
181{
182 theDefaults().theEngine->restoreStatus( filename );
183}
184
185std::ostream& HepRandom::saveFullState ( std::ostream & os ) {
186 os << *getTheEngine();
187 return os;
188}
189
190std::istream& HepRandom::restoreFullState ( std::istream & is ) {
191 is >> *getTheEngine();
192 return is;
193}
194
195std::ostream& HepRandom::saveStaticRandomStates ( std::ostream & os ) {
196 return StaticRandomStates::save(os);
197}
198
199std::istream& HepRandom::restoreStaticRandomStates ( std::istream & is ) {
201}
202
204{
205 theDefaults().theEngine->showStatus();
206}
207
209{
210 return static_cast<int>( theDefaults().ensureInitialized() );
211}
212
213} // namespace CLHEP
shared_ptr< HepRandom > theGenerator
Definition: Random.cc:59
shared_ptr< HepRandomEngine > theEngine
Definition: Random.cc:60
virtual std::istream & get(std::istream &is)
Definition: RandomEngine.cc:61
static HepRandom * getTheGenerator()
Definition: Random.cc:160
static HepRandomEngine * getTheEngine()
Definition: Random.cc:165
static const long * getTheSeeds()
Definition: Random.cc:146
virtual double operator()()
Definition: Random.cc:106
void flatArray(const int size, double *vect)
Definition: Random.cc:101
static std::ostream & saveStaticRandomStates(std::ostream &os)
Definition: Random.cc:195
static std::ostream & saveFullState(std::ostream &os)
Definition: Random.cc:185
virtual HepRandomEngine & engine()
Definition: Random.cc:111
static std::istream & restoreFullState(std::istream &is)
Definition: Random.cc:190
static void restoreEngineStatus(const char filename[]="Config.conf")
Definition: Random.cc:180
static void showEngineStatus()
Definition: Random.cc:203
virtual std::istream & get(std::istream &is)
Definition: Random.cc:125
static int createInstance()
Definition: Random.cc:208
static long getTheSeed()
Definition: Random.cc:136
static const long seedTable[215][2]
Definition: Random.h:157
static void setTheSeeds(const long *seeds, int aux=-1)
Definition: Random.cc:141
virtual ~HepRandom()
Definition: Random.cc:93
virtual std::ostream & put(std::ostream &os) const
Definition: Random.cc:124
double flat()
Definition: Random.cc:96
static void setTheEngine(HepRandomEngine *theNewEngine)
Definition: Random.cc:170
static void setTheSeed(long seed, int lux=3)
Definition: Random.cc:131
virtual std::string name() const
Definition: Random.cc:110
static void saveEngineStatus(const char filename[]="Config.conf")
Definition: Random.cc:175
static void getTheTableSeeds(long *seeds, int index)
Definition: Random.cc:151
static std::istream & restoreStaticRandomStates(std::istream &is)
Definition: Random.cc:199
static std::istream & restore(std::istream &is)
static std::ostream & save(std::ostream &os)
Definition: DoubConv.h:17
std::istream & operator>>(std::istream &is, HepRandom &dist)
Definition: Random.cc:120
std::ostream & operator<<(std::ostream &os, const HepRandom &dist)
Definition: Random.cc:116