PhotosRandom.cxx

00001 #include <iostream>
00002 #include "PhotosRandom.h"
00003 #include "Photos.h"
00004 #include "Log.h"
00005 
00006 namespace Photospp
00007 {
00008 
00009 bool         PhotosRandom::init    = false;
00010 int          PhotosRandom::iseed[2]= { 1802, 9373 };
00011 int          PhotosRandom::i97     = 96;
00012 int          PhotosRandom::j97     = 32;
00013 double       PhotosRandom::uran[97]= { 0.0 };
00014 double       PhotosRandom::cran    = 362436.0  /16777216.0;
00015 const double PhotosRandom::cdran   = 7654321.0 /16777216.0;
00016 const double PhotosRandom::cmran   = 16777213.0/16777216.0;
00017 
00018 /* PHORANC definition.
00019    Thanks to this function, this generator can be used by PHOTOS FORTRAN */
00020 extern "C" double phoranc_(int *idum)
00021 {
00022         return Photos::randomDouble();
00023 }
00024 
00025 void PhotosRandom::setSeed(int s1,int s2)
00026 {
00027         if(s1<0 || s1>31327) Log::Fatal("PhotosRandom::setSeed(): Seed(1) out of range [0,31327]",8);
00028         if(s2<0 || s2>30080) Log::Fatal("PhotosRandom::setSeed(): Seed(2) out of range [0,30080]",9);
00029         iseed[0]=s1;
00030         iseed[1]=s2;
00031 }
00032 
00033 /*******************************************************************************
00034   PHORIN:   PHOton radiation  in decays RANdom number generator init
00035 
00036   Purpose:  Initialse PHORAN  with  the user  specified seeds in the
00037             array iseed.  For details  see also:  F. James  CERN DD-
00038             Report November 1988.
00039 
00040   Author(s):  B. van Eijk and F. James        Created at:  27/09/89
00041                                               Last Update: 22/02/90
00042                                          Rewritten to C++: 18/10/10
00043                                          by T. Przedzinski  (tprzedzi@cern.ch)
00044 *******************************************************************************/
00045 void PhotosRandom::initialize()
00046 {
00047         long IS1,IS2,IS3,IS4,IS5;
00048         double S,T;
00049 
00050 // Calculate Marsaglia and Zaman seeds (by F. James)
00051         IS1=(iseed[0]/177)%177+2;
00052         IS2= iseed[0]%177+2;
00053         IS3=(iseed[1]/169)%178+1;
00054         IS4= iseed[1]%169;
00055         for(int i=0;i<97;i++)
00056         {
00057                 S=0.0;
00058                 T=0.5;
00059                 for(int j=0;j<24;j++)
00060                 {
00061                         IS5=( ((IS1*IS2)%179)*IS3 )%179;
00062                         IS1=IS2;
00063                         IS2=IS3;
00064                         IS3=IS5;
00065                         IS4=(53*IS4+1)%169;
00066                         if( (IS4*IS5)%64>=32) S=S+T;
00067                         T=0.5*T;
00068                 }
00069                 uran[i]=S;
00070         }
00071         init=true;
00072         Log::Debug(0)<<"PhotosRandom::inititalize(): seed: "<<iseed[0]<<", "<<iseed[1]<<std::endl;
00073 }
00074 
00075 /*******************************************************************************
00076   PHORAN:   PHOton radiation in decays ret number generator based
00077             on Marsaglia Algorithm
00078 
00079   Purpose:  Generate  uniformly  distributed  random numbers between
00080             0 and 1.  Super long period:  2**144.  See also:
00081             G. Marsaglia and A. Zaman,  FSU-SCR-87-50,  for seed mo-
00082             difications  to  this version  see:  F. James DD-Report,
00083             November 1988.  The generator  has  to be initialized by
00084             a call to PHORIN ( C++ version: initialize() ).
00085 
00086   Author(s):  B. van Eijk, G. Marsaglia and   Created at:  27/09/89
00087               A. Zaman                        Last Update: 27/09/89
00088                                          Rewritten to C++: 18/10/10
00089                                          by T. Przedzinski  (tprzedzi@cern.ch)
00090 *******************************************************************************/
00091 double PhotosRandom::randomReal()
00092 {
00093         if(!init) Log::Fatal("PhotosRandom::randomReal(): generator not initialized",1);
00094         double ret=0.0;
00095         while(true)
00096         {
00097                 ret = uran[i97]-uran[j97];
00098                 if(ret<0.0) ret+=1.;
00099                 uran[i97]=ret;
00100                 i97--;
00101                 if(i97<0) i97=96;
00102                 j97--;
00103                 if(j97<0) j97=96;
00104                 cran-=cdran;
00105                 if(cran<0.0) cran+=cmran;
00106                 ret-=cran;
00107                 if(ret<0.0) ret+=1.0;
00108                 if(ret>0.0) break;
00109         }
00110         return ret;
00111 }
00112 
00113 } // namespace Photospp
00114 
Generated on Sun Oct 20 20:23:56 2013 for C++InterfacetoPHOTOS by  doxygen 1.6.3