StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
RandEngine.cc
1 /***************************************************************************
2  *
3  * $Id: RandEngine.cc,v 1.4 2016/01/22 17:10:49 smirnovd Exp $
4  *
5  * Author: Gabriele Cosmo - Created: 5th September 1995
6  * modified for SCL bl
7  ***************************************************************************
8  *
9  * Description:
10  * RandEngine.cc,v 1.6 1998/01/23 08:19:53
11  * -----------------------------------------------------------------------
12  * HEP Random
13  * --- RandEngine ---
14  * class implementation file
15  * -----------------------------------------------------------------------
16  * This file is part of Geant4 (simulation toolkit for HEP).
17  *
18  ***************************************************************************
19  *
20  * $Log: RandEngine.cc,v $
21  * Revision 1.4 2016/01/22 17:10:49 smirnovd
22  * StarClassLibrary: Removed deprecated storage class specifier 'register'
23  *
24  * This keyword is deprecated since C++11 and serves no purpose
25  *
26  * "
27  * The register specifier is only allowed for objects declared at block scope and
28  * in function parameter lists. It indicates automatic storage duration, which is
29  * the default for these kinds of declarations. Additionally, the presence of this
30  * keyword may be used as a hint for the optimizer to store the value of this
31  * variable in a CPU register.
32  * "
33  *
34  * Revision 1.3 2012/06/11 15:29:26 fisyak
35  * std namespace
36  *
37  * Revision 1.2 1999/12/07 23:43:04 ullrich
38  * Modified to get rid of warnings on Linux.
39  *
40  * Revision 1.1 1999/01/30 03:58:59 fisyak
41  * Root Version of StarClassLibrary
42  *
43  * Revision 1.1 1999/01/23 00:29:05 ullrich
44  * Initial Revision
45  *
46  **************************************************************************/
47 #include "RandEngine.h"
48 #include <stdlib.h> // for RAND_MAX, tu
49 
50 
51 RandEngine::RandEngine(long seed) : mx(RAND_MAX)
52 {
53  setSeed(seed,0);
54  setSeeds(&theSeed,0);
55  seq = 0;
56 }
57 
58 RandEngine::~RandEngine() {}
59 
60 RandEngine::RandEngine(const RandEngine &p) : mx(RAND_MAX)
61 {
62  // This copy constructor uses "seq" to make the physical copy
63  // of the object preserving its original status.
64 
65  if ((this != &p) && (&p)) {
66  theSeed = p.theSeed;
67  seq = 0;
68  for (HepInt i=0; i<p.seq; ++i) flat();
69  setSeeds(&theSeed,0);
70  }
71 }
72 
73 RandEngine & RandEngine::operator = (const RandEngine &p)
74 {
75  // This operator uses "seq" to make the physical copy
76  // of the object preserving its original status.
77 
78  if ((this != &p) && (&p)) {
79  theSeed = p.theSeed;
80  seq = 0;
81  for (HepInt i=0; i<p.seq; ++i) flat();
82  setSeeds(&theSeed,0);
83  }
84  return *this;
85 }
86 
87 void RandEngine::setSeed(long seed, HepInt)
88 {
89  theSeed = seed;
90  srand( HepInt(seed) );
91  seq = 0;
92 }
93 
94 void RandEngine::setSeeds(const long* seeds, HepInt)
95 {
96  setSeed(seeds ? *seeds : 19780503, 0);
97  theSeeds = seeds;
98 }
99 
100 void RandEngine::saveStatus() const
101 {
102  ofstream outFile("Rand.conf", std::ios::out ) ;
103 
104  if (!outFile.bad()) {
105  outFile << theSeed << endl;
106  outFile << seq << endl;
107  }
108 }
109 
110 void RandEngine::restoreStatus()
111 {
112  // The only way to restore the status of RandEngine is to
113  // keep track of the number of shooted random sequences, reset
114  // the engine and re-shoot them again. The Rand algorithm does
115  // not provide any way of getting its internal status.
116 
117  ifstream inFile("Rand.conf", std::ios::in);
118  long count;
119 
120  if (!inFile.bad() && !inFile.eof()) {
121  inFile >> theSeed;
122  inFile >> count;
123  setSeed(theSeed,0);
124  for (HepInt i=0; i<count; ++i) flat();
125  }
126 }
127 
128 void RandEngine::showStatus() const
129 {
130  cout << endl;
131  cout << "---------- Rand engine status ----------" << endl;
132  cout << " Initial seed = " << theSeed << endl;
133  cout << " Shooted sequences = " << seq << endl;
134  cout << "----------------------------------------" << endl;
135 }
136 
137 HepDouble RandEngine::flat()
138 {
139  HepDouble num = 0.;
140 
141  while (num == 0.)
142  num = rand()/(mx+1);
143  seq++;
144  return num;
145 }
146 
147 void RandEngine::flatArray(const HepInt size, HepDouble* vect)
148 {
149  HepInt i;
150 
151  for (i=0; i<size; ++i)
152  vect[i]=flat();
153 }
154 
155 #ifndef ST_NO_TEMPLATE_DEF_ARGS
156 void RandEngine::flatArray(vector<HepDouble>& vec)
157 #else
158 void RandEngine::flatArray(vector<HepDouble,allocator<HepDouble> >& vec)
159 #endif
160 {
161  for (unsigned int i=0; i<vec.size(); ++i)
162  vec[i]=flat();
163 }