Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pyRandomize.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 // $Id: pyRandomize.cc,v 1.4 2006-06-29 15:33:42 gunter Exp $
27 // $Name: not supported by cvs2svn $
28 // ====================================================================
29 // pyRandomize.cc
30 //
31 // 2005 Q
32 // ====================================================================
33 #include <boost/python.hpp>
34 #include "Randomize.hh"
35 #include <vector>
36 
37 using namespace boost::python;
38 using namespace CLHEP;
39 
40 // ====================================================================
41 // thin wrappers
42 // ====================================================================
43 namespace pyRandomize {
44 
45 // problem with BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS for static method
46 // setTheSeed
47 void f1_setTheSeed(long seed)
48 {
49  HepRandom::setTheSeed(seed);
50 }
51 
52 void f2_setTheSeed(long seed, int lux)
53 {
54  HepRandom::setTheSeed(seed, lux);
55 }
56 
57 // setTheSeeds
58 void f1_setTheSeeds(const list& seedList)
59 {
60  // check size...
61  int idx=0;
62  while(1) {
63  long val= extract<long>(seedList[idx]);
64  if(val==0) break;
65  idx++;
66  }
67  int nsize= idx+1;
68 
69  long* seedArray= new long[nsize]; // should not be deleted!!
70  // (this is a problem with CLHEP.)
71  for (int i=0; i< nsize; i++) {
72  seedArray[i]= extract<long>(seedList[i]);
73  }
74 
75  HepRandom::setTheSeeds(seedArray);
76 }
77 
78 void f2_setTheSeeds(const list& seedList, int aux)
79 {
80  // check size...
81  int idx=0;
82  while(1) {
83  long val= extract<long>(seedList[idx]);
84  if(val==0) break;
85  idx++;
86  }
87  int nsize= idx+1;
88 
89  long* seedArray= new long[nsize];
90  for (int i=0; i< nsize; i++) {
91  seedArray[i]= extract<long>(seedList[i]);
92  }
93 
94  HepRandom::setTheSeeds(seedArray, aux);
95 }
96 
97 // getTheSeeds
99 {
100  list seedList;
101  const long* seeds= HepRandom::getTheSeeds();
102  int idx=0;
103  while(1) {
104  if( seeds[idx]==0) break;
105  seedList.append(seeds[idx]);
106  idx++;
107  }
108  return seedList;
109 }
110 
111 // getTheTableSeeds
113 {
114  long seedPair[2];
115  HepRandom::getTheTableSeeds(seedPair, index);
116 
117  list seedList;
118  seedList.append(seedPair[0]);
119  seedList.append(seedPair[1]);
120 
121  return seedList;
122 }
123 
124 
125 // saveEngineStatus
127 {
128  HepRandom::saveEngineStatus();
129 }
130 
131 void f2_saveEngineStatus(const char* filename)
132 {
133  HepRandom::saveEngineStatus(filename);
134 }
135 
136 // restoreEngineStatus
138 {
139  HepRandom::restoreEngineStatus();
140 }
141 
142 void f2_restoreEngineStatus(const char* filename)
143 {
144  HepRandom::restoreEngineStatus(filename);
145 }
146 
147 // RandBit::shootBit
149 {
150  return RandBit::shootBit();
151 }
152 
153 // RandGaussQ::shoot
155 {
156  return RandGaussQ::shoot();
157 }
158 
159 double f2_RandGaussQ_shoot(double mean, double stdDev)
160 {
161  return RandGaussQ::shoot(mean, stdDev);
162 }
163 
164 
165 // G4UniformRand
167 {
168  return G4UniformRand();
169 }
170 
171 };
172 
173 using namespace pyRandomize;
174 
175 // ====================================================================
176 // module definition
177 // ====================================================================
179 {
180  class_<HepRandom>("HepRandom", "generate random number")
181  // ---
182  .def(init<long>())
183  .def(init<HepRandomEngine&>())
184  .def(init<HepRandomEngine*>())
185  // ---
186  .def("setTheSeed", f1_setTheSeed)
187  .def("setTheSeed", f2_setTheSeed)
188  .staticmethod("setTheSeed")
189  .def("getTheSeed", &HepRandom::getTheSeed)
190  .staticmethod("getTheSeed")
191  .def("setTheSeeds", f1_setTheSeeds)
192  .def("setTheSeeds", f2_setTheSeeds)
193  .staticmethod("setTheSeeds")
194  .def("getTheSeeds", f_getTheSeeds)
195  .staticmethod("getTheSeeds")
196  .def("getTheTableSeeds", f_getTheTableSeeds)
197  .staticmethod("getTheTableSeeds")
198  // ---
199  .def("getTheGenerator", &HepRandom::getTheGenerator,
200  return_value_policy<reference_existing_object>())
201  .staticmethod("getTheGenerator")
202  .def("setTheEngine", &HepRandom::setTheEngine)
203  .staticmethod("setTheEngine")
204  .def("getTheEngine", &HepRandom::getTheEngine,
205  return_value_policy<reference_existing_object>())
206  .staticmethod("getTheEngine")
207  .def("saveEngineStatus", f1_saveEngineStatus)
208  .def("saveEngineStatus", f2_saveEngineStatus)
209  .staticmethod("saveEngineStatus")
210  .def("restoreEngineStatus", f1_restoreEngineStatus)
211  .def("restoreEngineStatus", f2_restoreEngineStatus)
212  .staticmethod("restoreEngineStatus")
213  .def("showEngineStatus", &HepRandom::showEngineStatus)
214  .staticmethod("showEngineStatus")
215  .def("createInstance", &HepRandom::createInstance)
216  .staticmethod("createInstance")
217  ;
218 
219  // ---
220  class_<RandBit, boost::noncopyable>
221  ("RandBit", "generate bit random number", no_init)
222  .def("shootBit", f1_RandBit_shootBit)
223  .staticmethod("shootBit")
224  ;
225 
226  // ---
227  class_<G4RandGauss, boost::noncopyable>
228  ("G4RandGauss", "generate gaussian random number", no_init)
229  .def("shoot", f1_RandGaussQ_shoot)
230  .def("shoot", f2_RandGaussQ_shoot)
231  .staticmethod("shoot")
232  ;
233 
234  // ---
235  def("G4UniformRand", f_G4UniformRand);
236 
237 }
238