-
Notifications
You must be signed in to change notification settings - Fork 0
/
fnal_laser_test.py
232 lines (207 loc) · 9.46 KB
/
fnal_laser_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env python3
from tamalero.ETROC import ETROC
from tamalero.ETROC_Emulator import ETROC2_Emulator as software_ETROC2
from tamalero.DataFrame import DataFrame
from tamalero.utils import get_kcu
from tamalero.ReadoutBoard import ReadoutBoard
from tamalero.colors import red, green, yellow
import numpy as np
from scipy.optimize import curve_fit
from matplotlib import pyplot as plt
from tqdm import tqdm
import pandas as pd
import os
import sys
import json
import time
import pdb
from yaml import load, dump
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
DF = DataFrame('ETROC2')
if __name__ == '__main__':
# argsparser
import argparse
argParser = argparse.ArgumentParser(description = "Argument parser")
argParser.add_argument('--kcu', action='store', default='192.168.0.10', help="IP Address of KCU105 board")
argParser.add_argument('--configuration', action='store', default='modulev0b', choices=['modulev0', 'modulev0b'], help="Board configuration to be loaded")
argParser.add_argument('--module', action='store', default=0, choices=['1','2','3'], help="Module to test")
argParser.add_argument('--host', action='store', default='localhost', help="Hostname for control hub")
argParser.add_argument('--hard_reset', action='store_true', default=False, help="Hard reset of selected ETROC2 chip")
argParser.add_argument('--scan', action='store', default=['full'], choices=['none', 'full', 'simple', 'internal'], help="Which threshold scan to run with ETROC2")
argParser.add_argument('--mode', action='store', default=['dual'], choices=['dual', 'single'], help="Port mode for ETROC2")
argParser.add_argument('--timing_scan', action='store_true', help="Set up internal data generation")
argParser.add_argument('--enable_power_board', action='store_true', help="Enable Power Board (all modules). Jumpers must still be set as well.")
argParser.add_argument('--row', action='store', default=4, help="Pixel row to be tested")
argParser.add_argument('--col', action='store', default=3, help="Pixel column to be tested")
argParser.add_argument('--offset', action='store', default=0, help="The offset from the baseline")
args = argParser.parse_args()
kcu = get_kcu(args.kcu, control_hub=True, host=args.host, verbose=False)
if (kcu == 0):
# if not basic connection was established the get_kcu function returns 0
# this would cause the RB init to fail.
sys.exit(1)
rb_0 = ReadoutBoard(0, kcu=kcu, config=args.configuration)
data = 0xabcd1234
kcu.write_node("LOOPBACK.LOOPBACK", data)
if (data != kcu.read_node("LOOPBACK.LOOPBACK")):
print("No communications with KCU105... quitting")
sys.exit(1)
is_configured = rb_0.DAQ_LPGBT.is_configured()
if not is_configured:
print("RB is not configured, exiting.")
exit(0)
from tamalero.Module import Module
# FIXME the below code is still pretty stupid
modules = []
connected_modules = []
for i in [1,2,3]:
m_tmp = Module(rb=rb_0, i=i, enable_power_board=args.enable_power_board)
modules.append(m_tmp)
if m_tmp.ETROCs[0].connected: # NOTE assume that module is connected if first ETROC is connected
connected_modules.append(i)
print(f"Found {len(connected_modules)} connected modules")
if int(args.module) > 0:
module = int(args.module)
else:
module = connected_modules[0]
print(f"Will proceed with testing Module {module}")
print("Module status:")
modules[module-1].show_status()
etroc = modules[module-1].ETROCs[0]
if args.hard_reset:
etroc.reset(hard=True)
etroc.default_config()
if args.mode == 'single':
print(f"Setting the ETROC in single port mode ('right')")
etroc.set_singlePort("right")
etroc.set_mergeTriggerData("separate")
elif args.mode == 'dual':
print(f"Setting the ETROC in dual port mode ('both')")
etroc.set_singlePort("both")
etroc.set_mergeTriggerData("merge")
# NOTE below is WIP code for tests of the actual data readout
from tamalero.FIFO import FIFO
from tamalero.DataFrame import DataFrame
df = DataFrame()
# NOTE this is for single port tests right now, where we only get elink 2
fifo = FIFO(rb=rb_0)
fifo.select_elink(0)
fifo.ready()
print("\n - Checking elinks")
print("Disabling readout for all elinks but the ETROC under test")
rb_0.disable_etroc_readout(all=True)
rb_0.reset_data_error_count()
#rb_0.enable_etroc_readout()
for lpgbt in etroc.elinks:
if lpgbt == 0:
slave = False
else:
slave = True
for link in etroc.elinks[lpgbt]:
rb_0.enable_etroc_readout(link, slave=slave)
#time.sleep(0.1)
#rb_0.reset_data_error_count()
rb_0.rerun_bitslip()
time.sleep(1.5)
rb_0.reset_data_error_count()
stat = rb_0.get_link_status(link, slave=slave, verbose=False)
if stat:
rb_0.get_link_status(link, slave=slave)
start_time = time.time()
while not stat:
#rb_0.disable_etroc_readout(link, slave=slave)
rb_0.enable_etroc_readout(link, slave=slave)
#time.sleep(0.1)
#rb_0.reset_data_error_count()
rb_0.rerun_bitslip()
time.sleep(1.5)
rb_0.reset_data_error_count()
stat = rb_0.get_link_status(link, slave=slave, verbose=False)
if stat:
rb_0.get_link_status(link, slave=slave)
break
if time.time() - start_time > 2:
print('Link not good, but continuing')
rb_0.get_link_status(link, slave=slave)
break
start_time = time.time()
while True:
try:
fifo.reset()
fifo.send_l1a(10)
_ = fifo.pretty_read(df)
fifo.reset()
break
except:
print("Initial (re)set of FIFO.")
if time.time() - start_time > 1:
print("FIFO state is unexpected.")
raise
etroc.reset()
# with open('results/thresholds.yaml', 'r') as f:
# threshold_matrix = load(f, Loader)
# with open('results/no_temperature_control/N13/185V/noise_scan/thresholds.yaml', 'r') as f:
# threshold_matrix = load(f, Loader)
with open('results/noise_scan/thresholds.yaml', 'r') as f:
threshold_matrix = load(f, Loader)
rb_0.enable_external_trigger()
offset_from_baseline = int(args.offset)
etroc.wr_reg("disDataReadout", 1, broadcast=True)
for i in [3]:
etroc.wr_reg("disDataReadout", 0, row=15, col=i, broadcast=False)
# current_offset = etroc.get_THoffset(row = 15, col = i)
threshold = etroc.auto_threshold_scan(15, i, offset="auto")
etroc.add_THoffset(offset_from_baseline, row = 15, col = i)
print(sum(threshold) + int(offset_from_baseline)) # (sum(threshold) + offset_from_baseline)
etroc.wr_reg("DAC", sum(threshold), row=15, col=i)
# print(f"Old threshold offset: {current_offset}, Matrix threshold value: {int(threshold_matrix[15][i])}.")
# print(f"Setting up threshold values: {int(threshold[0])} for row: {15}, col: {i}.")
# etroc.add_THoffset(offset_from_baseline, row = 15, col = i)
# etroc.add_THoffset(-10, row = 15, col = i)
# etroc.add_THoffset(-10 , row = 15, col = i)
current_offset = (etroc.get_THoffset(row = 15, col = i))
# print(f"New threshold offset: {current_offset}")
# print(threshold)
print(current_offset)
# threshold = 120
# int(threshold_matrix[15][i])
# etroc.wr_reg("DAC", 120, row=15, col=i)
L1Adelay = 14 # NOTE this is what we've found for the laser setup at FNAL.
# We previously found a value of 17. Needs to be checked why it is different now.
if args.timing_scan:
import pickle
print("Running timing scan")
#data = []
results = []
fifo.reset()
rb_0.reset_data_error_count()
#data_count = 0
#for j in range(0, 512): # max delay is 511 bunch crossings
for j in range(0, 40): # max delay is 511 bunch crossings
data_count = 0
trigger_count = 0
data = []
etroc.wr_reg("L1Adelay", j, broadcast=True) # broadcast was missing before.
for i in range(1000):
#etroc.wr_reg("L1Adelay", 0x01f5)
if fifo.is_full():
print("Fifo is full!")
fifo.reset()
if rb_0.read_data_count(0, slave=True) or rb_0.read_packet_count(0, slave=True):
#print("There was a hit (or noise)")
data += fifo.pretty_read(df)
trigger_count += rb_0.read_packet_count(0, slave=True)
data_count += rb_0.read_data_count(0, slave=True)
rb_0.reset_data_error_count()
#data_count += rb_0.read_data_count(0, slave=True)
#fifo.reset()
results.append((j, data_count, trigger_count))
print(j,data_count,trigger_count)
if data_count>0:
L1Adelay = j
print(f"Found L1Adelay of {L1Adelay}")
etroc.wr_reg("L1Adelay", L1Adelay, broadcast=True)
rb_0.disable_external_trigger()