/search.css" rel="stylesheet" type="text/css"/> /search.js">
| Classes | Job Modules | Data Objects | Services | Algorithms | Tools | Packages | Directories | Tracs |

In This Package:

Public Member Functions | Public Attributes
DybAlg::TestShuffle::DumpRandom Class Reference
Inheritance diagram for DybAlg::TestShuffle::DumpRandom:
Inheritance graph
[legend]
Collaboration diagram for DybAlg::TestShuffle::DumpRandom:
Collaboration graph
[legend]

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

 fp
 tfile
 executions
 clhep_random
 rndm_svc
 rndm_engine
 maximum
 epsilon
 root
 clhep

Detailed Description


Dump random numbers to file

Definition at line 9 of file TestShuffle.py.


Constructor & Destructor 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 16 of file TestShuffle.py.

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

Member Function Documentation

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

Definition at line 71 of file TestShuffle.py.

00072                         :
00073         'Return the current CLHEP seed'
00074         seeds = gbl.std.vector('long')();
00075         self.rndm_engine.seeds(seeds)
00076         return seeds[0]

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

Definition at line 77 of file TestShuffle.py.

00078                        :
00079         'Return the current ROOT seed'
00080         return gRandom.GetSeed()

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

Definition at line 81 of file TestShuffle.py.

00082                           :
00083         'Return the next CLHEP random in [0,maximum)'
00084         if self.clhep_random is None:
00085             flat = gbl.Rndm.Flat(0,self.maximum)
00086             self.clhep_random = gbl.Rndm.Numbers(self.rndm_svc,flat)
00087         self.clhep.nused += 1
00088         num = self.clhep_random()
00089         self.clhep.hist.Fill(num)
00090         self.clhep.hused.Fill(self.clhep.nused,num)
00091         self.clhep.graf.SetPoint(self.executions,self.executions,num)
00092         return num

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

Definition at line 93 of file TestShuffle.py.

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

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

Definition at line 103 of file TestShuffle.py.

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

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

Definition at line 118 of file TestShuffle.py.

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

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

Definition at line 144 of file TestShuffle.py.

00145                        :
00146         self.global_thermo_nuclear_war(self.root)
00147         self.global_thermo_nuclear_war(self.clhep)

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

Definition at line 148 of file TestShuffle.py.

00149                         :
00150         sc = DybPythonAlg.initialize(self)
00151         if sc.isFailure(): return sc
00152 
00153         
00154         self.rndm_svc = self.svc('IRndmGenSvc','RndmGenSvc')
00155         self.rndm_engine = self.rndm_svc.engine()
00156         assert self.rndm_engine, 'Could not get random engine from random svc'
00157 
00158         self.play_game()
00159 
00160         return SUCCESS

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

Definition at line 161 of file TestShuffle.py.

00162                              :
00163         'Do some checks on new seed against last seed, return new seed'
00164         if who.last_seed is None:
00165             who.last_seed = who.seed()
00166             return
00167         assert who.last_seed != who.seed(), 'Seed has not changed between executions!: %d'%who.seed()

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

Definition at line 168 of file TestShuffle.py.

00169                          :
00170 
00171         # Seeds should be same to each other
00172         rs = self.root.seed()
00173         cs = self.clhep.seed()
00174         assert rs == cs, 'ROOT seed %d != CLHEP seed %d' % (rs,cs)
00175         
00176         # But they should differ from the previous
00177         self.check_seed(self.root)
00178         self.check_seed(self.clhep)
00179 
00180         return

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

Definition at line 181 of file TestShuffle.py.

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

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

Definition at line 196 of file TestShuffle.py.

00197                             :
00198         msg = '%s is lucky %d out of %d = %.2f percent' % (who.name, who.lucky, who.tries, 
00199                                                            (100.0*who.lucky)/(1.0*who.tries))
00200         print msg
00201         assert who.lucky != who.tries, 'no one, not even %s'%msg

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

Definition at line 202 of file TestShuffle.py.

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


Member Data Documentation

Definition at line 23 of file TestShuffle.py.

Definition at line 23 of file TestShuffle.py.

Definition at line 23 of file TestShuffle.py.

Definition at line 23 of file TestShuffle.py.

Definition at line 23 of file TestShuffle.py.

Definition at line 23 of file TestShuffle.py.

Definition at line 23 of file TestShuffle.py.

Definition at line 23 of file TestShuffle.py.

Definition at line 23 of file TestShuffle.py.

Definition at line 23 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 Fri May 16 2014 10:09:44 for DybAlg by doxygen 1.7.4