StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StarRandom.cxx
1 #include "StarRandom.h"
2 ClassImp(StarRandom);
3 #include "assert.h"
4 
5 #include "Math/GSLRndmEngines.h"
6 
7 #include <iostream>
8 #include <string>
9 #include "StMessMgr.h"
10 #include "TRandom.h"
11 
12 #include "TSystem.h"
13 
14 using namespace std;
15 StarRandom *StarRandom::sInstance = 0;
16 ROOT::Math::GSLRandomEngine *StarRandom::mEngine = 0;
17 
18 //
19 // Lightweight class to remap ROOT's TRandom generators to StarRandom
20 //
21 class _Random : public TRandom {
22 public:
23  Double_t Rndm( Int_t i=0 ){ return StarRandom::Instance().flat(); }
24 };
25 
26 _Random *gStarRandom = 0;
27 
28 
29 // Include the GSL RNG setup method and global default seed.
30 extern "C"
31 {
32  void gsl_rng_env_setup();
33  unsigned long gsl_rng_default_seed;
34 };
35 
36 
42 // ----------------------------------------------------------------------------
43 StarRandom::StarRandom() : TObject(), mSeed(0), mState()
44 {
45 
46 }
47 // ----------------------------------------------------------------------------
48 StarRandom::~StarRandom()
49 {
50  if ( gStarRandom )
51  {
52  gRandom = 0;
53  delete gStarRandom;
54  }
55 }
56 // ----------------------------------------------------------------------------
58 {
59  if ( !gStarRandom ) gStarRandom = new _Random();
60  if ( gRandom )
61  {
62  Instance().Warning("StarRandom::capture()", "Capturing gRandom");
63  delete gRandom;
64  }
65  gRandom = gStarRandom;
66 }
67 // ----------------------------------------------------------------------------
68 Double_t StarRandom::flat() const { return (*mEngine)();}
69 Double_t StarRandom::flat( const Double_t mn, const Double_t mx ) const { assert(mx>mn); return mn + (mx - mn) * flat(); }
70 Double_t StarRandom::expo( const Double_t mu ) const { return mEngine->Exponential( mu ); }
71 Double_t StarRandom::landau() const { return mEngine->Landau(); }
72 Double_t StarRandom::gauss( const Double_t sigma ) const { return mEngine->Gaussian( sigma ); }
73 TVector2 StarRandom::gauss2d( const Double_t sx, const Double_t sy, const Double_t rho )const
74 {
75  Double_t x, y;
76  mEngine->Gaussian2D( sx, sy, rho, x, y );
77  return TVector2( x, y );
78 }
79 UInt_t StarRandom::poisson( const Double_t mu ) const { return mEngine->Poisson(mu); }
80 // ----------------------------------------------------------------------------
81 Double_t StarRandom::operator()() const { return flat(); }
82 Double_t StarRandom::operator()( const Double_t mn, const Double_t mx ) const
83 {
84  return flat(mn,mx);
85 }
86 // ----------------------------------------------------------------------------
88 {
89  if ( !sInstance )
90  {
91 
92  gsl_rng_env_setup();
93 
94  sInstance = new StarRandom();
95  if ( !mEngine ) mEngine = new ROOT::Math::GSLRandomEngine();
96  mEngine -> Initialize();
97  LOG_INFO << "Initialize random number generator " << mEngine->Name() << endm;
98 
99  if ( !gsl_rng_default_seed )
100  {
101  // Setup default seed using current time (ms since epoch) randomized by process ID
102  long time = gSystem->Now();
103  long pid = gSystem->GetPid();
104  seed( time|(pid<<16) );
105  }
106  else
107  {
108  seed( gsl_rng_default_seed );
109  }
110 
111  LOG_INFO << "Initialize random number seed " << sInstance->mSeed << endm;
112 
113  }
114  return (*sInstance);
115 }
116 // ----------------------------------------------------------------------------
117 void StarRandom::set( ROOT::Math::GSLRandomEngine *engine ){ mEngine = engine; }
118 // ----------------------------------------------------------------------------
119 void StarRandom::seed( UInt_t s ){
120  if (!sInstance) Instance();
121  mEngine->SetSeed(s);
122  //sInstance->mSeed = s;
123  Instance().mSeed = s;
124 }
125 // ----------------------------------------------------------------------------
126 void StarRandom::seed( UShort_t seed1, UShort_t seed2 ) {
127  Int_t myseed1 = seed1;
128  Int_t myseed2 = seed2;
129  seed( (myseed1<<16)|(myseed2) );
130 };
Double_t operator()() const
Return a random number uniformly distributed between 0 and 1.
Definition: StarRandom.cxx:81
Double_t expo(const Double_t mu) const
Return a random number distribted according to exp(-mu)
Definition: StarRandom.cxx:70
Double_t gauss(const Double_t sigma) const
Return a random number distributed according to a gaussian with specified sigma.
Definition: StarRandom.cxx:72
Double_t landau() const
Return a random number distributed according to a landau.
Definition: StarRandom.cxx:71
static StarRandom & Instance()
Obtain the single instance of the random number generator.
Definition: StarRandom.cxx:87
UInt_t poisson(const Double_t mu) const
Definition: StarRandom.cxx:79
A class for providing random number generation.
Definition: StarRandom.h:30
Double_t flat() const
Return a random number uniformly distributed between 0 and 1.
Definition: StarRandom.cxx:68
static void set(ROOT::Math::GSLRandomEngine *engine)
Set the random number generator engine.
Definition: StarRandom.cxx:117
static void seed(UInt_t s)
Definition: StarRandom.cxx:119
TVector2 gauss2d(const Double_t sx, const Double_t sy, const Double_t rho) const
Returns a pair of random numbers generated according to a 2D gaussian.
Definition: StarRandom.cxx:73
static void capture()
Capture gRandom random number generator.
Definition: StarRandom.cxx:57