Skip to content

Instantly share code, notes, and snippets.

@arnobaer
Last active July 24, 2019 10:01
Show Gist options
  • Save arnobaer/82f00de215ebd5e99ed7ba167e358f83 to your computer and use it in GitHub Desktop.
Save arnobaer/82f00de215ebd5e99ed7ba167e358f83 to your computer and use it in GitHub Desktop.
import random
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--prescale', type=float, default=1.5)
parser.add_argument('--bx-range', type=int, default=10000)
args = parser.parse_args()
# Select prescale
prescale_int = int(args.prescale)
prescale_frac = int(args.prescale * 10) - (prescale_int * 10)
prescale = args.prescale
# Offset patterns
prescale_lut = {
0: [0],
1: [1,0,0,0,0,0,0,0,0,0],
2: [1,0,0,0,0],
3: [1,0,0,1,0,0,1,0,0,0],
4: [1,0,1,0,0],
5: [1,0],
6: [1,1,0,1,0],
7: [1,1,1,0,1,1,0,1,1,0],
8: [1,1,1,1,0],
9: [1,1,1,1,1,1,1,1,1,0],
}
pattern = prescale_lut[prescale_frac]
# Fraction counter, max is size of pattern
prescale_lut_counter = 0
prescale_lut_counter_max = len(pattern)
# Prescale counter, max is prescale integer plus offset (0=1, 1=0)
prescale_counter = 0
prescale_counter_max = prescale_int + pattern[prescale_lut_counter]
# Rate counter
rate_counter = 0
# Simulation parameters
bx_range = args.bx_range
n_events = 0
for bx in range(bx_range):
# Generate random event (True/False)
event = random.randint(0,7) == 0
if event:
n_events += 1
# Advance prescale counter
prescale_counter += 1
# On prescale counter overflow
if prescale_counter >= prescale_counter_max:
prescale_counter = 0
# Advance fraction counter
prescale_lut_counter += 1
# On fraction counter overflow
if prescale_lut_counter >= len(pattern):
prescale_lut_counter = 0
prescale_counter_max = prescale_int + pattern[prescale_lut_counter]
# Increment rate counter
rate_counter += 1
print('n_events:', n_events)
print('prescale:', prescale)
print('rate_counter:', rate_counter)
print('calculated_events:', n_events / prescale)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment