Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
RooRandom.cxx
Go to the documentation of this file.
1 /*****************************************************************************
2  * Project: RooFit *
3  * Package: RooFitCore *
4  * @(#)root/roofitcore:$Id$
5  * Authors: *
6  * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7  * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8  * *
9  * Copyright (c) 2000-2005, Regents of the University of California *
10  * and Stanford University. All rights reserved. *
11  * *
12  * Redistribution and use in source and binary forms, *
13  * with or without modification, are permitted according to the terms *
14  * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15  *****************************************************************************/
16 
17 /**
18 \file RooRandom.cxx
19 \class RooRandom
20 \ingroup Roofitcore
21 
22 This class provides a static interface for generating random numbers.
23 By default a private copy of TRandom3 is used to generate all random numbers.
24 **/
25 #include <cassert>
26 
27 #include "RooFit.h"
28 
29 #include "RooRandom.h"
30 #include "RooRandom.h"
32 
33 #include "TRandom3.h"
34 
35 using namespace std;
36 
37 ClassImp(RooRandom);
38  ;
39 
40 
41 TRandom* RooRandom::_theGenerator = 0;
42 RooQuasiRandomGenerator* RooRandom::_theQuasiGenerator = 0;
43 RooRandom::Guard RooRandom::guard;
44 
45 ////////////////////////////////////////////////////////////////////////////////
46 
47 RooRandom::Guard::~Guard()
48 { delete RooRandom::_theGenerator; delete RooRandom::_theQuasiGenerator; }
49 
50 ////////////////////////////////////////////////////////////////////////////////
51 /// Return a pointer to a singleton random-number generator
52 /// implementation. Creates the object the first time it is called.
53 
54 TRandom *RooRandom::randomGenerator()
55 {
56  if (!_theGenerator) _theGenerator= new TRandom3();
57  return _theGenerator;
58 }
59 
60 
61 ////////////////////////////////////////////////////////////////////////////////
62 /// set the random number generator; takes ownership of the object passed as parameter
63 
64 void RooRandom::setRandomGenerator(TRandom* gen)
65 {
66  if (_theGenerator) delete _theGenerator;
67  _theGenerator = gen;
68 }
69 
70 ////////////////////////////////////////////////////////////////////////////////
71 /// Return a pointer to a singleton quasi-random generator
72 /// implementation. Creates the object the first time it is called.
73 
74 RooQuasiRandomGenerator *RooRandom::quasiGenerator()
75 {
76  if(!_theQuasiGenerator) _theQuasiGenerator= new RooQuasiRandomGenerator();
77  return _theQuasiGenerator;
78 }
79 
80 
81 ////////////////////////////////////////////////////////////////////////////////
82 /// Return a number uniformly distributed from (0,1)
83 
84 Double_t RooRandom::uniform(TRandom *generator)
85 {
86  return generator->Rndm();
87 }
88 
89 
90 ////////////////////////////////////////////////////////////////////////////////
91 /// Fill the vector provided with random numbers uniformly distributed from (0,1)
92 
93 void RooRandom::uniform(UInt_t dimension, Double_t vector[], TRandom *generator)
94 {
95  generator->RndmArray(dimension, vector);
96 }
97 
98 
99 ////////////////////////////////////////////////////////////////////////////////
100 /// Return an integer uniformly distributed from [0,n-1]
101 
102 UInt_t RooRandom::integer(UInt_t n, TRandom *generator)
103 {
104  return generator->Integer(n);
105 }
106 
107 
108 ////////////////////////////////////////////////////////////////////////////////
109 /// Return a Gaussian random variable with mean 0 and variance 1.
110 
111 Double_t RooRandom::gaussian(TRandom *generator)
112 {
113  return generator->Gaus();
114 }
115 
116 
117 ////////////////////////////////////////////////////////////////////////////////
118 /// Return a quasi-random number in the range (0,1) using the
119 /// Niederreiter base 2 generator described in Bratley, Fox, Niederreiter,
120 /// ACM Trans. Model. Comp. Sim. 2, 195 (1992).
121 
122 Bool_t RooRandom::quasi(UInt_t dimension, Double_t vector[], RooQuasiRandomGenerator *generator)
123 {
124  return generator->generate(dimension,vector);
125 }