Geant4-11
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | Static Public Member Functions | Static Protected Attributes | Private Member Functions | Private Attributes
CLHEP::RandGeneral Class Reference

#include <RandGeneral.h>

Inheritance diagram for CLHEP::RandGeneral:
CLHEP::HepRandom

Public Member Functions

HepRandomEngineengine ()
 
double fire ()
 
void fireArray (const int size, double *vect)
 
double flat ()
 
double flat (HepRandomEngine *theNewEngine)
 
void flatArray (const int size, double *vect)
 
void flatArray (HepRandomEngine *theNewEngine, const int size, double *vect)
 
std::istream & get (std::istream &is)
 
std::string name () const
 
double operator() ()
 
std::ostream & put (std::ostream &os) const
 
 RandGeneral (const double *aProbFunc, int theProbSize, int IntType=0)
 
 RandGeneral (HepRandomEngine &anEngine, const double *aProbFunc, int theProbSize, int IntType=0)
 
 RandGeneral (HepRandomEngine *anEngine, const double *aProbFunc, int theProbSize, int IntType=0)
 
double shoot ()
 
double shoot (HepRandomEngine *anEngine)
 
void shootArray (const int size, double *vect)
 
void shootArray (HepRandomEngine *anEngine, const int size, double *vect)
 
virtual ~RandGeneral ()
 

Static Public Member Functions

static int createInstance ()
 
static std::string distributionName ()
 
static HepRandomEnginegetTheEngine ()
 
static HepRandomgetTheGenerator ()
 
static long getTheSeed ()
 
static const long * getTheSeeds ()
 
static void getTheTableSeeds (long *seeds, int index)
 
static std::istream & restoreDistState (std::istream &is)
 
static void restoreEngineStatus (const char filename[]="Config.conf")
 
static std::istream & restoreFullState (std::istream &is)
 
static std::istream & restoreStaticRandomStates (std::istream &is)
 
static std::ostream & saveDistState (std::ostream &os)
 
static void saveEngineStatus (const char filename[]="Config.conf")
 
static std::ostream & saveFullState (std::ostream &os)
 
static std::ostream & saveStaticRandomStates (std::ostream &os)
 
static void setTheEngine (HepRandomEngine *theNewEngine)
 
static void setTheSeed (long seed, int lxr=3)
 
static void setTheSeeds (const long *seeds, int aux=-1)
 
static void showEngineStatus ()
 

Static Protected Attributes

static const long seedTable [215][2]
 

Private Member Functions

double mapRandom (double rand) const
 
void prepareTable (const double *aProbFunc)
 
void useFlatDistribution ()
 

Private Attributes

int InterpolationType
 
std::shared_ptr< HepRandomEnginelocalEngine
 
int nBins
 
double oneOverNbins
 
std::vector< double > theIntegralPdf
 

Detailed Description

Author

Definition at line 38 of file RandGeneral.h.

Constructor & Destructor Documentation

◆ RandGeneral() [1/3]

CLHEP::RandGeneral::RandGeneral ( const double *  aProbFunc,
int  theProbSize,
int  IntType = 0 
)

Definition at line 66 of file RandGeneral.cc.

69 : HepRandom(),
70 localEngine(HepRandom::getTheEngine(), do_nothing_deleter()),
71 nBins(theProbSize),
72 InterpolationType(IntType)
73{
74 prepareTable(aProbFunc);
75}
static HepRandomEngine * getTheEngine()
Definition: Random.cc:268
void prepareTable(const double *aProbFunc)
Definition: RandGeneral.cc:101
std::shared_ptr< HepRandomEngine > localEngine
Definition: RandGeneral.h:130

References prepareTable().

◆ RandGeneral() [2/3]

CLHEP::RandGeneral::RandGeneral ( HepRandomEngine anEngine,
const double *  aProbFunc,
int  theProbSize,
int  IntType = 0 
)

Definition at line 77 of file RandGeneral.cc.

81: HepRandom(),
82 localEngine(&anEngine, do_nothing_deleter()),
83 nBins(theProbSize),
84 InterpolationType(IntType)
85{
86 prepareTable(aProbFunc);
87}

References prepareTable().

◆ RandGeneral() [3/3]

CLHEP::RandGeneral::RandGeneral ( HepRandomEngine anEngine,
const double *  aProbFunc,
int  theProbSize,
int  IntType = 0 
)

Definition at line 89 of file RandGeneral.cc.

93: HepRandom(),
94 localEngine(anEngine),
95 nBins(theProbSize),
96 InterpolationType(IntType)
97{
98 prepareTable(aProbFunc);
99}

References prepareTable().

◆ ~RandGeneral()

CLHEP::RandGeneral::~RandGeneral ( )
virtual

Definition at line 174 of file RandGeneral.cc.

174 {
175}

Member Function Documentation

◆ createInstance()

int CLHEP::HepRandom::createInstance ( )
staticinherited

◆ distributionName()

static std::string CLHEP::RandGeneral::distributionName ( )
inlinestatic

Definition at line 124 of file RandGeneral.h.

124{return "RandGeneral";}

◆ engine()

HepRandomEngine & CLHEP::RandGeneral::engine ( )
virtual

Reimplemented from CLHEP::HepRandom.

Definition at line 59 of file RandGeneral.cc.

59{return *localEngine;}

References localEngine.

◆ fire()

double CLHEP::RandGeneral::fire ( )

Referenced by fireArray().

◆ fireArray()

void CLHEP::RandGeneral::fireArray ( const int  size,
double *  vect 
)

Definition at line 240 of file RandGeneral.cc.

241{
242 int i;
243
244 for (i=0; i<size; ++i) {
245 vect[i] = fire();
246 }
247}

References fire().

◆ flat() [1/2]

double CLHEP::HepRandom::flat ( )
inherited

◆ flat() [2/2]

double CLHEP::HepRandom::flat ( HepRandomEngine theNewEngine)
inlineinherited

◆ flatArray() [1/2]

void CLHEP::HepRandom::flatArray ( const int  size,
double *  vect 
)
inherited

◆ flatArray() [2/2]

void CLHEP::HepRandom::flatArray ( HepRandomEngine theNewEngine,
const int  size,
double *  vect 
)
inlineinherited

◆ get()

std::istream & CLHEP::RandGeneral::get ( std::istream &  is)
virtual

Reimplemented from CLHEP::HepRandom.

Definition at line 266 of file RandGeneral.cc.

266 {
267 std::string inName;
268 is >> inName;
269 if (inName != name()) {
270 is.clear(std::ios::badbit | is.rdstate());
271 std::cerr << "Mismatch when expecting to read state of a "
272 << name() << " distribution\n"
273 << "Name found was " << inName
274 << "\nistream is left in the badbit state\n";
275 return is;
276 }
277 if (possibleKeywordInput(is, "Uvec", nBins)) {
278 std::vector<unsigned long> t(2);
280 is >> t[0] >> t[1]; oneOverNbins = DoubConv::longs2double(t);
281 theIntegralPdf.resize(nBins+1);
282 for (unsigned int i=0; i<theIntegralPdf.size(); ++i) {
283 is >> theIntegralPdf[i] >> t[0] >> t[1];
285 }
286 return is;
287 }
288 // is >> nBins encompassed by possibleKeywordInput
290 theIntegralPdf.resize(nBins+1);
291 for (unsigned int i=0; i<theIntegralPdf.size(); ++i) is >> theIntegralPdf[i];
292 return is;
293}
static double longs2double(const std::vector< unsigned long > &v)
Definition: DoubConv.cc:110
std::string name() const
Definition: RandGeneral.cc:58
std::vector< double > theIntegralPdf
Definition: RandGeneral.h:131
bool possibleKeywordInput(IS &is, const std::string &key, T &t)
Definition: RandomEngine.h:166

References InterpolationType, CLHEP::DoubConv::longs2double(), name(), nBins, oneOverNbins, CLHEP::possibleKeywordInput(), and theIntegralPdf.

◆ getTheEngine()

HepRandomEngine * CLHEP::HepRandom::getTheEngine ( )
staticinherited

◆ getTheGenerator()

HepRandom * CLHEP::HepRandom::getTheGenerator ( )
staticinherited

◆ getTheSeed()

long CLHEP::HepRandom::getTheSeed ( )
staticinherited

◆ getTheSeeds()

const long * CLHEP::HepRandom::getTheSeeds ( )
staticinherited

◆ getTheTableSeeds()

void CLHEP::HepRandom::getTheTableSeeds ( long *  seeds,
int  index 
)
staticinherited

Definition at line 254 of file Random.cc.

255{
256 if ((index >= 0) && (index < 215)) {
257 seeds[0] = seedTable[index][0];
258 seeds[1] = seedTable[index][1];
259 }
260 else seeds = NULL;
261}
static const long seedTable[215][2]
Definition: Random.h:156

Referenced by CLHEP::HepJamesRandom::HepJamesRandom(), CLHEP::MTwistEngine::MTwistEngine(), CLHEP::RanecuEngine::RanecuEngine(), CLHEP::Ranlux64Engine::Ranlux64Engine(), CLHEP::RanluxEngine::RanluxEngine(), and CLHEP::RanecuEngine::setSeed().

◆ mapRandom()

double CLHEP::RandGeneral::mapRandom ( double  rand) const
private

Definition at line 182 of file RandGeneral.cc.

182 {
183//
184// Private method to take the random (however it is created) and map it
185// according to the distribution.
186//
187
188 int nbelow = 0; // largest k such that I[k] is known to be <= rand
189 int nabove = nBins; // largest k such that I[k] is known to be > rand
190 int middle;
191
192 while (nabove > nbelow+1) {
193 middle = (nabove + nbelow+1)>>1;
194 if (rand >= theIntegralPdf[middle]) {
195 nbelow = middle;
196 } else {
197 nabove = middle;
198 }
199 } // after this loop, nabove is always nbelow+1 and they straddle rad:
200 assert ( nabove == nbelow+1 );
201 assert ( theIntegralPdf[nbelow] <= rand );
202 assert ( theIntegralPdf[nabove] >= rand );
203 // If a defective engine produces rand=1, that will
204 // still give sensible results so we relax the > rand assertion
205
206 if ( InterpolationType == 1 ) {
207
208 return nbelow * oneOverNbins;
209
210 } else {
211
212 double binMeasure = theIntegralPdf[nabove] - theIntegralPdf[nbelow];
213 // binMeasure is always aProbFunc[nbelow],
214 // but we don't have aProbFunc any more so we subtract.
215
216 if ( binMeasure == 0 ) {
217 // rand lies right in a bin of measure 0. Simply return the center
218 // of the range of that bin. (Any value between k/N and (k+1)/N is
219 // equally good, in this rare case.)
220 return (nbelow + .5) * oneOverNbins;
221 }
222
223 double binFraction = (rand - theIntegralPdf[nbelow]) / binMeasure;
224
225 return (nbelow + binFraction) * oneOverNbins;
226 }
227
228} // mapRandom(rand)

References InterpolationType, nBins, oneOverNbins, and theIntegralPdf.

◆ name()

std::string CLHEP::RandGeneral::name ( ) const
virtual

Reimplemented from CLHEP::HepRandom.

Definition at line 58 of file RandGeneral.cc.

58{return "RandGeneral";}

Referenced by source.g4viscp.G4Scene::create_scene(), get(), mcscore.MCParticle::printout(), put(), and source.g4viscp.G4Scene::update_scene().

◆ operator()()

double CLHEP::RandGeneral::operator() ( )
virtual

Reimplemented from CLHEP::HepRandom.

◆ prepareTable()

void CLHEP::RandGeneral::prepareTable ( const double *  aProbFunc)
private

Definition at line 101 of file RandGeneral.cc.

101 {
102//
103// Private method called only by constructors. Prepares theIntegralPdf.
104//
105 if (nBins < 1) {
106 std::cerr <<
107 "RandGeneral constructed with no bins - will use flat distribution\n";
109 return;
110 }
111
112 theIntegralPdf.resize(nBins+1);
113 theIntegralPdf[0] = 0;
114 int ptn;
115 double weight;
116
117 for ( ptn = 0; ptn<nBins; ++ptn ) {
118 weight = aProbFunc[ptn];
119 if ( weight < 0 ) {
120 // We can't stomach negative bin contents, they invalidate the
121 // search algorithm when the distribution is fired.
122 std::cerr <<
123 "RandGeneral constructed with negative-weight bin " << ptn <<
124 " = " << weight << " \n -- will substitute 0 weight \n";
125 weight = 0;
126 }
127 // std::cout << ptn << " " << weight << " " << theIntegralPdf[ptn] << "\n";
128 theIntegralPdf[ptn+1] = theIntegralPdf[ptn] + weight;
129 }
130
131 if ( theIntegralPdf[nBins] <= 0 ) {
132 std::cerr <<
133 "RandGeneral constructed nothing in bins - will use flat distribution\n";
135 return;
136 }
137
138 for ( ptn = 0; ptn < nBins+1; ++ptn ) {
140 // std::cout << ptn << " " << theIntegralPdf[ptn] << "\n";
141 }
142
143 // And another useful variable is ...
144 oneOverNbins = 1.0 / nBins;
145
146 // One last chore:
147
148 if ( (InterpolationType != 0) && (InterpolationType != 1) ) {
149 std::cerr <<
150 "RandGeneral does not recognize IntType " << InterpolationType
151 << "\n Will use type 0 (continuous linear interpolation \n";
153 }
154
155} // prepareTable()
void useFlatDistribution()
Definition: RandGeneral.cc:157

References InterpolationType, nBins, oneOverNbins, theIntegralPdf, and useFlatDistribution().

Referenced by RandGeneral().

◆ put()

std::ostream & CLHEP::RandGeneral::put ( std::ostream &  os) const
virtual

Reimplemented from CLHEP::HepRandom.

Definition at line 249 of file RandGeneral.cc.

249 {
250 int pr=os.precision(20);
251 std::vector<unsigned long> t(2);
252 os << " " << name() << "\n";
253 os << "Uvec" << "\n";
254 os << nBins << " " << oneOverNbins << " " << InterpolationType << "\n";
256 os << t[0] << " " << t[1] << "\n";
257 assert (static_cast<int>(theIntegralPdf.size())==nBins+1);
258 for (unsigned int i=0; i<theIntegralPdf.size(); ++i) {
260 os << theIntegralPdf[i] << " " << t[0] << " " << t[1] << "\n";
261 }
262 os.precision(pr);
263 return os;
264}
static std::vector< unsigned long > dto2longs(double d)
Definition: DoubConv.cc:94

References CLHEP::DoubConv::dto2longs(), InterpolationType, name(), nBins, oneOverNbins, and theIntegralPdf.

◆ restoreDistState()

static std::istream & CLHEP::HepRandom::restoreDistState ( std::istream &  is)
inlinestaticinherited

Definition at line 136 of file Random.h.

136{return is;}

◆ restoreEngineStatus()

void CLHEP::HepRandom::restoreEngineStatus ( const char  filename[] = "Config.conf")
staticinherited

Definition at line 283 of file Random.cc.

284{
285 theDefaults().theEngine->restoreStatus( filename );
286}

References CLHEP::anonymous_namespace{Random.cc}::theDefaults(), and CLHEP::anonymous_namespace{Random.cc}::defaults::theEngine.

◆ restoreFullState()

std::istream & CLHEP::HepRandom::restoreFullState ( std::istream &  is)
staticinherited

Definition at line 293 of file Random.cc.

293 {
294 is >> *getTheEngine();
295 return is;
296}

Referenced by CLHEP::RandFlat::restoreFullState(), and CLHEP::RandGauss::restoreFullState().

◆ restoreStaticRandomStates()

std::istream & CLHEP::HepRandom::restoreStaticRandomStates ( std::istream &  is)
staticinherited

Definition at line 302 of file Random.cc.

302 {
304}
static std::istream & restore(std::istream &is)

References CLHEP::StaticRandomStates::restore().

◆ saveDistState()

static std::ostream & CLHEP::HepRandom::saveDistState ( std::ostream &  os)
inlinestaticinherited

Definition at line 133 of file Random.h.

133{return os;}

◆ saveEngineStatus()

void CLHEP::HepRandom::saveEngineStatus ( const char  filename[] = "Config.conf")
staticinherited

◆ saveFullState()

std::ostream & CLHEP::HepRandom::saveFullState ( std::ostream &  os)
staticinherited

◆ saveStaticRandomStates()

std::ostream & CLHEP::HepRandom::saveStaticRandomStates ( std::ostream &  os)
staticinherited

Definition at line 298 of file Random.cc.

298 {
299 return StaticRandomStates::save(os);
300}
static std::ostream & save(std::ostream &os)

References CLHEP::StaticRandomStates::save().

◆ setTheEngine()

void CLHEP::HepRandom::setTheEngine ( HepRandomEngine theNewEngine)
staticinherited

Definition at line 273 of file Random.cc.

274{
275 theDefaults().theEngine.reset( theNewEngine, do_nothing_deleter() );
276}

References CLHEP::anonymous_namespace{Random.cc}::theDefaults(), and CLHEP::anonymous_namespace{Random.cc}::defaults::theEngine.

Referenced by CLHEP::StaticRandomStates::restore().

◆ setTheSeed()

void CLHEP::HepRandom::setTheSeed ( long  seed,
int  lxr = 3 
)
staticinherited

Definition at line 234 of file Random.cc.

235{
236 theDefaults().theEngine->setSeed(seed,lux);
237}
static constexpr double lux

References CLHEP::lux, CLHEP::anonymous_namespace{Random.cc}::theDefaults(), and CLHEP::anonymous_namespace{Random.cc}::defaults::theEngine.

◆ setTheSeeds()

void CLHEP::HepRandom::setTheSeeds ( const long *  seeds,
int  aux = -1 
)
staticinherited

◆ shoot() [1/2]

double CLHEP::RandGeneral::shoot ( )
inline

◆ shoot() [2/2]

double CLHEP::RandGeneral::shoot ( HepRandomEngine anEngine)

◆ shootArray() [1/2]

void CLHEP::RandGeneral::shootArray ( const int  size,
double *  vect 
)
inline

◆ shootArray() [2/2]

void CLHEP::RandGeneral::shootArray ( HepRandomEngine anEngine,
const int  size,
double *  vect 
)

Definition at line 230 of file RandGeneral.cc.

232{
233 int i;
234
235 for (i=0; i<size; ++i) {
236 vect[i] = shoot(anEngine);
237 }
238}

References shoot().

◆ showEngineStatus()

void CLHEP::HepRandom::showEngineStatus ( )
staticinherited

◆ useFlatDistribution()

void CLHEP::RandGeneral::useFlatDistribution ( )
private

Definition at line 157 of file RandGeneral.cc.

157 {
158//
159// Private method called only by prepareTables in case of user error.
160//
161 nBins = 1;
162 theIntegralPdf.resize(2);
163 theIntegralPdf[0] = 0;
164 theIntegralPdf[1] = 1;
165 oneOverNbins = 1.0;
166 return;
167
168} // UseFlatDistribution()

References nBins, oneOverNbins, and theIntegralPdf.

Referenced by prepareTable().

Field Documentation

◆ InterpolationType

int CLHEP::RandGeneral::InterpolationType
private

Definition at line 134 of file RandGeneral.h.

Referenced by get(), mapRandom(), prepareTable(), and put().

◆ localEngine

std::shared_ptr<HepRandomEngine> CLHEP::RandGeneral::localEngine
private

Definition at line 130 of file RandGeneral.h.

Referenced by engine().

◆ nBins

int CLHEP::RandGeneral::nBins
private

Definition at line 132 of file RandGeneral.h.

Referenced by get(), mapRandom(), prepareTable(), put(), and useFlatDistribution().

◆ oneOverNbins

double CLHEP::RandGeneral::oneOverNbins
private

Definition at line 133 of file RandGeneral.h.

Referenced by get(), mapRandom(), prepareTable(), put(), and useFlatDistribution().

◆ seedTable

const long CLHEP::HepRandom::seedTable
staticprotectedinherited

Definition at line 156 of file Random.h.

◆ theIntegralPdf

std::vector<double> CLHEP::RandGeneral::theIntegralPdf
private

Definition at line 131 of file RandGeneral.h.

Referenced by get(), mapRandom(), prepareTable(), put(), and useFlatDistribution().


The documentation for this class was generated from the following files: