Geant4  10.02.p03
CLHEP::RandGeneral Class Reference

#include <RandGeneral.h>

Inheritance diagram for CLHEP::RandGeneral:
Collaboration diagram for CLHEP::RandGeneral:

Public Member Functions

 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)
 
virtual ~RandGeneral ()
 
double shoot ()
 
void shootArray (const int size, double *vect)
 
double shoot (HepRandomEngine *anEngine)
 
void shootArray (HepRandomEngine *anEngine, const int size, double *vect)
 
double fire ()
 
void fireArray (const int size, double *vect)
 
double operator() ()
 
std::ostream & put (std::ostream &os) const
 
std::istream & get (std::istream &is)
 
std::string name () const
 
HepRandomEngineengine ()
 
- Public Member Functions inherited from CLHEP::HepRandom
 HepRandom ()
 
 HepRandom (long seed)
 
 HepRandom (HepRandomEngine &algorithm)
 
 HepRandom (HepRandomEngine *algorithm)
 
virtual ~HepRandom ()
 
double flat ()
 
void flatArray (const int size, double *vect)
 
double flat (HepRandomEngine *theNewEngine)
 
void flatArray (HepRandomEngine *theNewEngine, const int size, double *vect)
 

Static Public Member Functions

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

Private Member Functions

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

Private Attributes

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

Additional Inherited Members

- Static Protected Attributes inherited from CLHEP::HepRandom
static const long seedTable [215][2]
 

Detailed Description

Author

Definition at line 39 of file RandGeneral.h.

Constructor & Destructor Documentation

◆ RandGeneral() [1/3]

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

Definition at line 64 of file RandGeneral.cc.

67  : HepRandom(),
68  localEngine(HepRandom::getTheEngine(), do_nothing_deleter()),
69  nBins(theProbSize),
70  InterpolationType(IntType)
71 {
72  prepareTable(aProbFunc);
73 }
void prepareTable(const double *aProbFunc)
Definition: RandGeneral.cc:99
static HepRandomEngine * getTheEngine()
Definition: Random.cc:166
std::shared_ptr< HepRandomEngine > localEngine
Definition: RandGeneral.h:131
Here is the call graph for this function:

◆ RandGeneral() [2/3]

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

Definition at line 75 of file RandGeneral.cc.

79 : HepRandom(),
80  localEngine(&anEngine, do_nothing_deleter()),
81  nBins(theProbSize),
82  InterpolationType(IntType)
83 {
84  prepareTable(aProbFunc);
85 }
void prepareTable(const double *aProbFunc)
Definition: RandGeneral.cc:99
std::shared_ptr< HepRandomEngine > localEngine
Definition: RandGeneral.h:131
Here is the call graph for this function:

◆ RandGeneral() [3/3]

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

Definition at line 87 of file RandGeneral.cc.

91 : HepRandom(),
92  localEngine(anEngine),
93  nBins(theProbSize),
94  InterpolationType(IntType)
95 {
96  prepareTable(aProbFunc);
97 }
void prepareTable(const double *aProbFunc)
Definition: RandGeneral.cc:99
std::shared_ptr< HepRandomEngine > localEngine
Definition: RandGeneral.h:131
Here is the call graph for this function:

◆ ~RandGeneral()

CLHEP::RandGeneral::~RandGeneral ( )
virtual

Definition at line 172 of file RandGeneral.cc.

172  {
173 }

Member Function Documentation

◆ distributionName()

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

Definition at line 125 of file RandGeneral.h.

125 {return "RandGeneral";}

◆ engine()

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

Reimplemented from CLHEP::HepRandom.

Definition at line 57 of file RandGeneral.cc.

57 {return *localEngine;}
std::shared_ptr< HepRandomEngine > localEngine
Definition: RandGeneral.h:131

◆ fire()

double CLHEP::RandGeneral::fire ( )
Here is the caller graph for this function:

◆ fireArray()

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

Definition at line 238 of file RandGeneral.cc.

239 {
240  int i;
241 
242  for (i=0; i<size; ++i) {
243  vect[i] = fire();
244  }
245 }
Here is the call graph for this function:

◆ get()

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

Reimplemented from CLHEP::HepRandom.

Definition at line 264 of file RandGeneral.cc.

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

◆ mapRandom()

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

Definition at line 180 of file RandGeneral.cc.

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

◆ name()

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

Reimplemented from CLHEP::HepRandom.

Definition at line 56 of file RandGeneral.cc.

56 {return "RandGeneral";}
Here is the caller graph for this function:

◆ operator()()

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

Reimplemented from CLHEP::HepRandom.

◆ prepareTable()

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

Definition at line 99 of file RandGeneral.cc.

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

◆ put()

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

Reimplemented from CLHEP::HepRandom.

Definition at line 247 of file RandGeneral.cc.

247  {
248  int pr=os.precision(20);
249  std::vector<unsigned long> t(2);
250  os << " " << name() << "\n";
251  os << "Uvec" << "\n";
252  os << nBins << " " << oneOverNbins << " " << InterpolationType << "\n";
254  os << t[0] << " " << t[1] << "\n";
255  assert (static_cast<int>(theIntegralPdf.size())==nBins+1);
256  for (unsigned int i=0; i<theIntegralPdf.size(); ++i) {
258  os << theIntegralPdf[i] << " " << t[0] << " " << t[1] << "\n";
259  }
260  os.precision(pr);
261  return os;
262 }
std::string name() const
Definition: RandGeneral.cc:56
std::vector< double > theIntegralPdf
Definition: RandGeneral.h:132
static std::vector< unsigned long > dto2longs(double d)
Definition: DoubConv.cc:98
Here is the call graph for this function:

◆ shoot() [1/2]

double CLHEP::RandGeneral::shoot ( )
inline
Here is the caller graph for this function:

◆ 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 228 of file RandGeneral.cc.

230 {
231  int i;
232 
233  for (i=0; i<size; ++i) {
234  vect[i] = shoot(anEngine);
235  }
236 }
Here is the call graph for this function:

◆ useFlatDistribution()

void CLHEP::RandGeneral::useFlatDistribution ( )
private

Definition at line 155 of file RandGeneral.cc.

155  {
156 //
157 // Private method called only by prepareTables in case of user error.
158 //
159  nBins = 1;
160  theIntegralPdf.resize(2);
161  theIntegralPdf[0] = 0;
162  theIntegralPdf[1] = 1;
163  oneOverNbins = 1.0;
164  return;
165 
166 } // UseFlatDistribution()
std::vector< double > theIntegralPdf
Definition: RandGeneral.h:132
Here is the caller graph for this function:

Member Data Documentation

◆ InterpolationType

int CLHEP::RandGeneral::InterpolationType
private

Definition at line 135 of file RandGeneral.h.

◆ localEngine

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

Definition at line 131 of file RandGeneral.h.

◆ nBins

int CLHEP::RandGeneral::nBins
private

Definition at line 133 of file RandGeneral.h.

◆ oneOverNbins

double CLHEP::RandGeneral::oneOverNbins
private

Definition at line 134 of file RandGeneral.h.

◆ theIntegralPdf

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

Definition at line 132 of file RandGeneral.h.


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