|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from hashlib import sha1 |
| 4 | + |
| 5 | +def GetRandomSeed(run='', subrun='', stage='', hexbits=8, model='', test = False): |
| 6 | + """ Get a random seed from has of run + subrun + stage, nBits""" |
| 7 | + run = str(run) |
| 8 | + subrun = str(subrun) |
| 9 | + stage = str(stage) |
| 10 | + model = str(model) |
| 11 | + combine = (run+subrun+stage+model).encode() |
| 12 | + print('Making seed from: ', combine) |
| 13 | + h = sha1(combine) |
| 14 | + # .encode will use UTF-8 if not specified |
| 15 | + """ |
| 16 | + 32 bit unsigned integer -> 8 hexbits: |
| 17 | + max = 0xFFFFFFFF = 2^32-1 = 4294967295 : nd280Control (10) |
| 18 | + 32 bit signed integer -> 7 hexbits: |
| 19 | + max = 0xFFFFFFF = 2^31-1 = 268435455 : nd280MC, neut (9) |
| 20 | + """ |
| 21 | + bits = int(h.hexdigest()[:hexbits], 16) |
| 22 | + |
| 23 | + if(test): |
| 24 | + print('(run+subrun+stage+model).encode() ', (run+subrun+stage+model).encode() ) |
| 25 | + print('h = sha1((run+subrun+stage+model).encode()) ' , (sha1((run+subrun+stage+model).encode()) ) ) |
| 26 | + print('h.hexdigest() ', h.hexdigest() ) |
| 27 | + print('h.hexdigest()[:hexbits] ', h.hexdigest()[:hexbits] ) |
| 28 | + print('int(h.hexdigest()[:hexbits], 16) ', int(h.hexdigest()[:hexbits], 16) , '\n') |
| 29 | + |
| 30 | + print( 'Random seed for ' + run + ' ' + subrun + ' ' + stage + ' ' + model + ' is ' + str(bits) ) |
| 31 | + return bits |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +# Useful to have this as a script, as we often need to know the seed to be able to replicate and bug hunt locally |
| 36 | +# And if the grid job fails, you don't always have the logs to grab the info. |
| 37 | +if __name__ == "__main__": |
| 38 | + |
| 39 | + GetRandomSeed('90410000', '0044', 'agh', 8, '', True) |
| 40 | + GetRandomSeed('2', '44', 'wcs', 7, 'warren2020_NO_10kpc_a1.25_m10.5', True) |
0 commit comments