GammaWare  Head Version for release 0.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Random.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2004 by Olivier Stezowski *
3  * stezow(AT)ipnl.in2p3.fr *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
23 #ifndef GW_RANDOM_H
24 #include "Random.h"
25 #endif
26 
27 #ifndef ROOT_TList
28 #include <TList.h>
29 #endif
30 
31 #ifndef ROOT_TRandomom2
32 #include <TRandom2.h>
33 #endif
34 
35 #ifndef ROOT_TRandomom3
36 #include <TRandom3.h>
37 #endif
38 
39 using namespace Gw;
40 
42 
43 Random *Random::fgRandom = NULL;
44 
45 Random::Random()
46 {
47  TRandom *r; fROOT = NULL;
48 
49  // default list
50  fList = new TList(); fList->SetOwner(kTRUE);
51 
52  r = new TRandom(); r->SetName("basic"); fList->Add(r);
53  r = new TRandom(0); r->SetName("basic_clock"); fList->Add(r);
54  r = new TRandom2(); r->SetName("medium"); fList->Add(r);
55  r = new TRandom(0); r->SetName("medium_clock"); fList->Add(r);
56  r = new TRandom3(); r->SetName("large"); fList->Add(r);
57  r = new TRandom3(0); r->SetName("large_clock"); fList->Add(r);
58 
59  // current generator, basic
60  SetCurrent();
61 }
62 
64 {
65  // delete the created objects and restore gRandomom
66  delete fList; gRandom = fROOT;
67 }
68 
70 {
71  if ( fgRandom == NULL ) fgRandom = new Random();
72  return fgRandom;
73 }
74 
75 void Random::SetCurrent(const char *name)
76 {
77  TObject *r;
78 
79  if ( (r = fList->FindObject(name)) ) { // ok name find in the list
80  fCurrent = dynamic_cast<TRandom *>(r);
81 
82  // keep a copy of gRandomom and synchronise gRandomom with this
83  if ( fROOT == NULL ) fROOT = gRandom; gRandom = fCurrent;
84  }
85 }
86 
87 void Random::Add(const char *name, TRandom *r)
88 {
89  if ( fList->FindObject(name) == NULL && r != NULL ) { r->SetName(name); fList->Add(r); }
90 }
91 
92 
93 
94 
void Add(const char *, TRandom *)
Add a new TRandomom object to the list.
Definition: Random.cpp:87
header file for Random
To change the internal GammaWare random generator.
Definition: Random.h:64
ClassImp(Random)
static Random * Instance()
to access the unique instance
Definition: Random.cpp:69
virtual ~Random()
Definition: Random.cpp:63
void SetCurrent(const char *name="basic")
to set the current TRandomom object
Definition: Random.cpp:75