CLHEP VERSION Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

Random.cc
Go to the documentation of this file.
1// $Id: Random.cc,v 1.6 2010/06/16 17:24:53 garren Exp $
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
21#include "CLHEP/Random/defs.h"
22#include "CLHEP/Random/JamesRandom.h"
23#include "CLHEP/Random/Random.h"
24#include "CLHEP/Random/StaticRandomStates.h"
25#include "CLHEP/Utility/memory.h"
26
27// -----------------------------
28// Static members initialisation
29// -----------------------------
30
31#include "CLHEP/Random/SeedTable.h"
32
33namespace CLHEP {
34
35
36namespace {
37
38struct defaults {
39 defaults( HepRandom & g, HepJamesRandom & e )
40 : theGenerator( &g, do_nothing_deleter() )
41 , theEngine ( &e, do_nothing_deleter() )
42 { }
43
44 void resetEngine( HepRandomEngine * newEngine ) {
45 theEngine.reset( newEngine );
46 }
47
48 void resetEngine( HepRandomEngine & newEngine ) {
49 theEngine.reset( &newEngine, do_nothing_deleter() );
50 }
51
52 bool ensureInitialized() {
53 assert( theGenerator.get() != 0 && theEngine.get() != 0 );
54 return true;
55 }
56
57 ~defaults()
58 { }
59
60 shared_ptr<HepRandom > theGenerator;
61 shared_ptr<HepRandomEngine> theEngine;
62}; // defaults
63
64 inline
65 defaults & theDefaults() {
66 static HepRandom theDefaultGenerator;
67 static HepJamesRandom theDefaultEngine;
68 static defaults theDefaults(theDefaultGenerator, theDefaultEngine);
69 return theDefaults;
70 }
71
72} // namespace
73
74//---------------------------- HepRandom ---------------------------------
75
78
80{
81 setTheSeed(seed);
82}
83
85{
86 theDefaults().resetEngine( algorithm );
87}
88
90{
91 theDefaults().resetEngine( algorithm );
92}
93
96
98{
99 return theDefaults().theEngine->flat();
100}
101
102void HepRandom::flatArray(const int size, double* vect)
103{
104 theDefaults().theEngine->flatArray(size,vect);
105}
106
108 return flat();
109}
110
111std::string HepRandom::name() const {return "HepRandom";}
113 std::cerr << "HepRandom::engine() called -- there is no assigned engine!\n";
114 return *theDefaults().theEngine.get();
115}
116
117std::ostream & operator<< (std::ostream & os, const HepRandom & dist) {
118 return dist.put(os);
119}
120
121std::istream & operator>> (std::istream & is, HepRandom & dist) {
122 return dist.get(is);
123}
124
125std::ostream & HepRandom::put(std::ostream & os) const {return os;}
126std::istream & HepRandom::get(std::istream & is) {return is;}
127
128// --------------------------
129// Static methods definitions
130// --------------------------
131
132void HepRandom::setTheSeed(long seed, int lux)
133{
134 theDefaults().theEngine->setSeed(seed,lux);
135}
136
138{
139 return theDefaults().theEngine->getSeed();
140}
141
142void HepRandom::setTheSeeds(const long* seeds, int aux)
143{
144 theDefaults().theEngine->setSeeds(seeds,aux);
145}
146
148{
149 return theDefaults().theEngine->getSeeds();
150}
151
152void HepRandom::getTheTableSeeds(long* seeds, int index)
153{
154 if ((index >= 0) && (index < 215)) {
155 seeds[0] = seedTable[index][0];
156 seeds[1] = seedTable[index][1];
157 }
158 else seeds = NULL;
159}
160
162{
163 return theDefaults().theGenerator.get();
164}
165
167{
168 return theDefaults().theEngine.get();
169}
170
172{
173 theDefaults().theEngine.reset( theNewEngine, do_nothing_deleter() );
174}
175
176void HepRandom::saveEngineStatus( const char filename[] )
177{
178 theDefaults().theEngine->saveStatus( filename );
179}
180
181void HepRandom::restoreEngineStatus( const char filename[] )
182{
183 theDefaults().theEngine->restoreStatus( filename );
184}
185
186std::ostream& HepRandom::saveFullState ( std::ostream & os ) {
187 os << *getTheEngine();
188 return os;
189}
190
191std::istream& HepRandom::restoreFullState ( std::istream & is ) {
192 is >> *getTheEngine();
193 return is;
194}
195
196std::ostream& HepRandom::saveStaticRandomStates ( std::ostream & os ) {
197 return StaticRandomStates::save(os);
198}
199
200std::istream& HepRandom::restoreStaticRandomStates ( std::istream & is ) {
202}
203
205{
206 theDefaults().theEngine->showStatus();
207}
208
210{
211 return static_cast<int>( theDefaults().ensureInitialized() );
212}
213
214} // namespace CLHEP
shared_ptr< HepRandom > theGenerator
Definition Random.cc:60
shared_ptr< HepRandomEngine > theEngine
Definition Random.cc:61
virtual std::istream & get(std::istream &is)
static HepRandom * getTheGenerator()
Definition Random.cc:161
static HepRandomEngine * getTheEngine()
Definition Random.cc:166
static const long * getTheSeeds()
Definition Random.cc:147
virtual double operator()()
Definition Random.cc:107
void flatArray(const int size, double *vect)
Definition Random.cc:102
static std::ostream & saveStaticRandomStates(std::ostream &os)
Definition Random.cc:196
static std::ostream & saveFullState(std::ostream &os)
Definition Random.cc:186
virtual HepRandomEngine & engine()
Definition Random.cc:112
static std::istream & restoreFullState(std::istream &is)
Definition Random.cc:191
static void restoreEngineStatus(const char filename[]="Config.conf")
Definition Random.cc:181
static void showEngineStatus()
Definition Random.cc:204
virtual std::istream & get(std::istream &is)
Definition Random.cc:126
static int createInstance()
Definition Random.cc:209
static long getTheSeed()
Definition Random.cc:137
static const long seedTable[215][2]
static void setTheSeeds(const long *seeds, int aux=-1)
Definition Random.cc:142
virtual ~HepRandom()
Definition Random.cc:94
virtual std::ostream & put(std::ostream &os) const
Definition Random.cc:125
double flat()
Definition Random.cc:97
static void setTheEngine(HepRandomEngine *theNewEngine)
Definition Random.cc:171
static void setTheSeed(long seed, int lux=3)
Definition Random.cc:132
virtual std::string name() const
Definition Random.cc:111
static void saveEngineStatus(const char filename[]="Config.conf")
Definition Random.cc:176
static void getTheTableSeeds(long *seeds, int index)
Definition Random.cc:152
static std::istream & restoreStaticRandomStates(std::istream &is)
Definition Random.cc:200
static std::istream & restore(std::istream &is)
static std::ostream & save(std::ostream &os)
std::ostream & operator<<(std::ostream &os, const HepAxisAngle &aa)
Definition AxisAngle.cc:86
std::istream & operator>>(std::istream &is, HepAxisAngle &aa)
Definition AxisAngle.cc:96
int g(shared_ptr< X >)