Belle II Software  release-06-01-15
PhotosRandom.cc
1 #include <iostream>
2 #include "PhotosRandom.h"
3 #include "Photos.h"
4 #include "Log.h"
5 
6 namespace Photospp {
7 
8  bool PhotosRandom::init = false;
9  int PhotosRandom::iseed[2] = { 1802, 9373 };
10  int PhotosRandom::i97 = 96;
11  int PhotosRandom::j97 = 32;
12  double PhotosRandom::uran[97] = { 0.0 };
13  double PhotosRandom::cran = 362436.0 / 16777216.0;
14  const double PhotosRandom::cdran = 7654321.0 / 16777216.0;
15  const double PhotosRandom::cmran = 16777213.0 / 16777216.0;
16 
17  void PhotosRandom::setSeed(int s1, int s2)
18  {
19  if (s1 < 0 || s1 > 31327) Log::Fatal("PhotosRandom::setSeed(): Seed(1) out of range [0,31327]", 8);
20  if (s2 < 0 || s2 > 30080) Log::Fatal("PhotosRandom::setSeed(): Seed(2) out of range [0,30080]", 9);
21  iseed[0] = s1;
22  iseed[1] = s2;
23  }
24 
25  /*******************************************************************************
26  PHORIN: PHOton radiation in decays RANdom number generator init
27 
28  Purpose: Initialse PHORAN with the user specified seeds in the
29  array iseed. For details see also: F. James CERN DD-
30  Report November 1988.
31 
32  Author(s): B. van Eijk and F. James Created at: 27/09/89
33  Last Update: 22/02/90
34  Rewritten to C++: 18/10/10
35  by T. Przedzinski (tprzedzi@cern.ch)
36  *******************************************************************************/
37  void PhotosRandom::initialize()
38  {
39  long IS1, IS2, IS3, IS4, IS5;
40  double S, T;
41 
42 // Calculate Marsaglia and Zaman seeds (by F. James)
43  IS1 = (iseed[0] / 177) % 177 + 2;
44  IS2 = iseed[0] % 177 + 2;
45  IS3 = (iseed[1] / 169) % 178 + 1;
46  IS4 = iseed[1] % 169;
47  for (int i = 0; i < 97; i++) {
48  S = 0.0;
49  T = 0.5;
50  for (int j = 0; j < 24; j++) {
51  IS5 = (((IS1 * IS2) % 179) * IS3) % 179;
52  IS1 = IS2;
53  IS2 = IS3;
54  IS3 = IS5;
55  IS4 = (53 * IS4 + 1) % 169;
56  if ((IS4 * IS5) % 64 >= 32) S = S + T;
57  T = 0.5 * T;
58  }
59  uran[i] = S;
60  }
61  init = true;
62  Log::Debug(0) << "PhotosRandom::inititalize(): seed: " << iseed[0] << ", " << iseed[1] << std::endl;
63  }
64 
65  /*******************************************************************************
66  PHORAN: PHOton radiation in decays ret number generator based
67  on Marsaglia Algorithm
68 
69  Purpose: Generate uniformly distributed random numbers between
70  0 and 1. Super long period: 2**144. See also:
71  G. Marsaglia and A. Zaman, FSU-SCR-87-50, for seed mo-
72  difications to this version see: F. James DD-Report,
73  November 1988. The generator has to be initialized by
74  a call to PHORIN ( C++ version: initialize() ).
75 
76  Author(s): B. van Eijk, G. Marsaglia and Created at: 27/09/89
77  A. Zaman Last Update: 27/09/89
78  Rewritten to C++: 18/10/10
79  by T. Przedzinski (tprzedzi@cern.ch)
80  *******************************************************************************/
81  double PhotosRandom::randomReal()
82  {
83  if (!init) Log::Fatal("PhotosRandom::randomReal(): generator not initialized", 1);
84  double ret = 0.0;
85  while (true) {
86  ret = uran[i97] - uran[j97];
87  if (ret < 0.0) ret += 1.;
88  uran[i97] = ret;
89  i97--;
90  if (i97 < 0) i97 = 96;
91  j97--;
92  if (j97 < 0) j97 = 96;
93  cran -= cdran;
94  if (cran < 0.0) cran += cmran;
95  ret -= cran;
96  if (ret < 0.0) ret += 1.0;
97  if (ret > 0.0) break;
98  }
99  return ret;
100  }
101 
102 } // namespace Photospp
103 
static ostream & Debug(unsigned short int code=0, bool count=true)
Four logging entries.
Definition: Log.cc:32
static void Fatal(string text, unsigned short int code=0)
Terminates the program with added default message or 'text'.