| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

DybAlg::TestShuffle::DumpRandom Class Reference

List of all members.

Public Member Functions

def __init__
def clhep_seed
def root_seed
def clhep_number
def root_number
def check_tries
def global_thermo_nuclear_war
def play_game
def initialize
def check_seed
def check_seeds
def execute
def check_luck
def finalize

Public Attributes

 clhep_random
 rndm_svc
 rndm_engine

Detailed Description

Dump random numbers to file

Definition at line 9 of file TestShuffle.py.


Member Function Documentation

def DybAlg::TestShuffle::DumpRandom::__init__ (   self,
  name = 'DumpRandom',
  filename = 'random.dump' 
)

Make a DumpRandom alg

Arguments:
- "name":object name
- "filename":name of file in to which random numbers will be written

Definition at line 12 of file TestShuffle.py.

00016                                                                      :
00017         '''
00018         
00019         Make a DumpRandom alg
00020         
00021         Arguments:
00022         - "name":object name
00023         - "filename":name of file in to which random numbers will be written
00024         '''
00025         DybPythonAlg.__init__(self,name)
00026         self.fp = open(filename,'w')
00027         self.tfile = TFile(filename+'.root',"RECREATE")
00028         self.executions = 0
00029 
00030         self.clhep_random = None
00031         self.rndm_svc = None
00032         self.rndm_engine = None
00033 
00034         self.maximum = 100.0    # guess a number between 0 and maximum
00035         self.epsilon = 1.0/(int(nuwa.opts.executions)*self.maximum)
00036 
00037         class Chaos(): pass
00038 
00039         root = Chaos()
00040         root.last_seed = None
00041         root.lucky = 0
00042         root.tries = 0
00043         root.last_tries = []
00044         root.seed = self.root_seed
00045         root.number = self.root_number
00046         root.name = 'ROOT'
00047         root.hist = TH1F("rootran","ROOT randoms",1000,0,self.maximum)
00048         root.hused = TH2F("rootused","ROOT randoms",100,0,100,100,0,self.maximum)
00049         root.graf = TGraph()
00050         root.graf.SetName("rootrangraf")
00051         root.nused = 0          # number of randoms used
00052         self.root = root
00053 
00054         clhep = Chaos()
00055         clhep.last_seed = None
00056         clhep.lucky = 0
00057         clhep.tries = 0
00058         clhep.last_tries = []
00059         clhep.seed = self.clhep_seed
00060         clhep.number = self.clhep_number
00061         clhep.name = 'CLHEP'
00062         clhep.hist = TH1F("clhepran","CLHEP randoms",1000,0,self.maximum)
00063         clhep.hused = TH2F("clhepused","CLHEP randoms",100,0,100,100,0,self.maximum)
00064         clhep.graf = TGraph()
00065         clhep.graf.SetName("clheprangraf")
00066         clhep.nused = 0         # number of randoms used
        self.clhep = clhep

def DybAlg::TestShuffle::DumpRandom::clhep_seed (   self  ) 

Definition at line 67 of file TestShuffle.py.

00071                         :
00072         'Return the current CLHEP seed'
        seeds = gbl.std.vector('long')();

def DybAlg::TestShuffle::DumpRandom::root_seed (   self  ) 

Definition at line 73 of file TestShuffle.py.

00077                        :
        'Return the current ROOT seed'

def DybAlg::TestShuffle::DumpRandom::clhep_number (   self  ) 

Definition at line 77 of file TestShuffle.py.

00077                        :
00078         'Return the current ROOT seed'
00079         return gRandom.GetSeed()
00080 
00081     def clhep_number(self):
00082         'Return the next CLHEP random in [0,maximum)'
00083         if self.clhep_random is None:
00084             flat = gbl.Rndm.Flat(0,self.maximum)
00085             self.clhep_random = gbl.Rndm.Numbers(self.rndm_svc,flat)
00086         self.clhep.nused += 1
00087         num = self.clhep_random()
00088         self.clhep.hist.Fill(num)
        self.clhep.hused.Fill(self.clhep.nused,num)

def DybAlg::TestShuffle::DumpRandom::root_number (   self  ) 

Definition at line 89 of file TestShuffle.py.

00093                          :
00094         'Return the next ROOT random in [0,maximum)'
00095         self.root.nused += 1
00096         num = gRandom.Uniform(0,self.maximum)
00097         self.root.hist.Fill(num)
00098         self.root.hused.Fill(self.root.nused,num)
        self.root.graf.SetPoint(self.executions,self.executions,num)

def DybAlg::TestShuffle::DumpRandom::check_tries (   self,
  who,
  tries 
)

Definition at line 99 of file TestShuffle.py.

00103                                    :
00104         assert who.last_tries != tries, '%s is really boring using the same guesses: %s' % (who.name, str(tries))
00105         
00106         if len(who.last_tries) != len(tries):
00107             #print '%s: new tries are different lengths %d != %d' % (who.name,len(who.last_tries),len(tries))
00108             who.last_tries = tries
00109             return
00110 
00111         for t1,t2 in zip(who.last_tries,tries):
00112             #print who.name,t1,t2
00113             assert abs(t1-t2) > self.epsilon, '%s does no pick fresh enough values (%f vs. %f)' % (who.name,t1,t2)

def DybAlg::TestShuffle::DumpRandom::global_thermo_nuclear_war (   self,
  who 
)

Definition at line 114 of file TestShuffle.py.

00118                                            :
00119         'Exercise the random number stream'
00120         answer = who.number()
00121         nguesses = 50
00122         self.fp.write(who.name+": Seed = %d\n"%who.seed())
00123         self.fp.write(who.name+": Let's play guess my number\n")
00124         tries = []
00125         for n in range(nguesses):
00126             guess = who.number()
00127 
00128             assert answer != guess, "No one is that lucky to get %f == %f !" % (answer,guess)
00129 
00130             who.tries += 1
00131             tries.append(guess)
00132             if int(guess) == int(answer):
00133                 self.fp.write(who.name+": Guess #%d: %f is close enough to %f, you win\n" % (n+1, guess,answer))
00134                 if n == 0: 
00135                     who.lucky += 1
00136                     print '%s got lucky %d times' % (who.name,who.lucky)
00137                 return
00138             self.fp.write(who.name+": Guess #%d: %f is wrong\n" % (n+1,guess) )
00139             continue
        self.check_tries(who,tries)

def DybAlg::TestShuffle::DumpRandom::play_game (   self  ) 

Definition at line 140 of file TestShuffle.py.

00141                                 : You failed! The right answer was %f\n" % answer)
00142         return
00143 

def DybAlg::TestShuffle::DumpRandom::initialize (   self  ) 

Definition at line 144 of file TestShuffle.py.

00144                        :
00145         self.global_thermo_nuclear_war(self.root)
00146         self.global_thermo_nuclear_war(self.clhep)
00147 
00148     def initialize(self):
00149         sc = DybPythonAlg.initialize(self)
00150         if sc.isFailure(): return sc
00151 
00152         
00153         self.rndm_svc = self.svc('IRndmGenSvc','RndmGenSvc')
00154         self.rndm_engine = self.rndm_svc.engine()
00155         assert self.rndm_engine, 'Good not get random engine from random svc'
00156 
        self.play_game()

def DybAlg::TestShuffle::DumpRandom::check_seed (   self,
  who 
)

Definition at line 157 of file TestShuffle.py.

00161                              :
00162         'Do some checks on new seed against last seed, return new seed'
00163         if who.last_seed is None:
            who.last_seed = who.seed()

def DybAlg::TestShuffle::DumpRandom::check_seeds (   self  ) 

Definition at line 164 of file TestShuffle.py.

00166                                                                                      : %d'%who.seed()
00167 
00168     def check_seeds(self):
00169 
00170         # Seeds should be same to each other
00171         rs = self.root.seed()
00172         cs = self.clhep.seed()
00173         assert rs == cs, 'ROOT seed %d != CLHEP seed %d' % (rs,cs)
00174         
00175         # But they should differ from the previous
00176         self.check_seed(self.root)

def DybAlg::TestShuffle::DumpRandom::execute (   self  ) 

Definition at line 177 of file TestShuffle.py.

00181                      :
00182         self.root.nused = 0
00183         self.clhep.nused = 0
00184         self.root.numbers = []
00185         self.clhep.numbers = []
00186         self.executions += 1
00187         try:
00188             self.check_seeds()
00189             self.play_game()
00190         except AssertionError,err:
00191             print err
            return FAILURE

def DybAlg::TestShuffle::DumpRandom::check_luck (   self,
  who 
)

Definition at line 192 of file TestShuffle.py.

00196                             :
00197         msg = '%s is lucky %d out of %d = %.2f percent' % (who.name, who.lucky, who.tries, 
                                                           (100.0*who.lucky)/(1.0*who.tries))

def DybAlg::TestShuffle::DumpRandom::finalize (   self  ) 

Definition at line 198 of file TestShuffle.py.

00202                       :
00203         import math
00204 
00205         self.fp.close()
00206 
00207         self.root.hist.Write()
00208         self.root.hused.Write()
00209         self.root.graf.Write()
00210         self.clhep.hist.Write()
00211         self.clhep.hused.Write()
00212         self.clhep.graf.Write()
00213         self.tfile.Close()
00214 
00215         try:
00216             self.check_luck(self.root)
00217             self.check_luck(self.clhep)
00218         except AssertionError,err:
00219             print err
            return FAILURE


Member Data Documentation

DybAlg::TestShuffle::DumpRandom::clhep_random

Definition at line 81 of file TestShuffle.py.

DybAlg::TestShuffle::DumpRandom::rndm_svc

Definition at line 149 of file TestShuffle.py.

DybAlg::TestShuffle::DumpRandom::rndm_engine

Definition at line 150 of file TestShuffle.py.


The documentation for this class was generated from the following file:
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

Generated on Mon Apr 11 20:40:41 2011 for DybAlg by doxygen 1.4.7