-
Notifications
You must be signed in to change notification settings - Fork 0
/
acquire_threshold_calibration
executable file
·226 lines (183 loc) · 7.24 KB
/
acquire_threshold_calibration
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from petsys import daqd, config
from copy import deepcopy
import argparse
from time import sleep, time
from sys import stdout
parser = argparse.ArgumentParser(description='Acquire data for threshold calibration')
parser.add_argument("--config", type=str, required=True, help="Configuration file")
parser.add_argument("-o", type=str, dest="outFilePrefix", required=True, help="Data file prefix")
parser.add_argument("--nreads-noise", dest="noise_reads", type=int, required=False, default=4)
parser.add_argument("--nreads-dark", dest="dark_reads", type=int, required=False, default=4)
parser.add_argument("--ext-bias", dest="ext_bias", action="store_true", default=False, help="Prompt user to set bias")
args = parser.parse_args()
if args.ext_bias:
systemConfig = config.ConfigFromFile(args.config, loadMask=0x0)
input("Set no-dark bias voltage and press ENTER")
else:
systemConfig = config.ConfigFromFile(args.config, loadMask=config.LOAD_BIAS_CALIBRATION | config.LOAD_BIAS_SETTINGS)
daqd = daqd.Connection()
daqd.initializeSystem()
if args.ext_bias:
systemConfig.loadToHardware(daqd, bias_enable=config.APPLY_BIAS_OFF)
else:
systemConfig.loadToHardware(daqd, bias_enable=config.APPLY_BIAS_PREBD)
asicsConfig = daqd.getAsicsConfig()
COUNT_MAX = 1.0 * (2**22)
T = COUNT_MAX * (1 / daqd.getSystemFrequency())
thresholdList = [
(0, "vth_t1", "baseline_t" ),
(1, "vth_t2", "baseline_t" ),
(2, "vth_e", "baseline_e")
]
activeAsics = daqd.getActiveAsics()
activeChannels = [ (portID, slaveID, chipID, channelID) for channelID in range(64) for portID, slaveID, chipID in activeAsics ]
# Adjust baseline
for (portID, slaveID, chipID), ac in list(asicsConfig.items()):
if not ac: continue
gc = ac.globalConfig
if daqd.getAsicSubtype(portID, slaveID, chipID) == "2B":
COUNTER_SETTING = 0x4
else:
COUNTER_SETTING = 0b110
print(portID, slaveID, chipID, COUNTER_SETTING)
gc.setValue("counter_en", 0b1)
gc.setValue("counter_period", COUNTER_SETTING)
for cc in ac.channelConfig:
cc.setValue("trigger_mode_1", 0)
cc.setValue("counter_mode", 0xF)
cc.setValue("trigger_b_latched", 0)
counter_sharing = 1
asicSubTypes = set([ daqd.getAsicSubtype(portID, slaveID, chipID) for portID, slaveID, chipID in list(asicsConfig.keys()) ])
if "2B" in asicSubTypes:
counter_sharing = 8
print("counter_sharing = %(counter_sharing)d" % locals())
print("Adjusting baseline")
for thresholdIndex, thresholdName, baselineName in thresholdList:
print(thresholdIndex, thresholdName, baselineName)
N_ITERATIONS = 0
while N_ITERATIONS < 20:
print(baselineName, "ITERATION %d" % N_ITERATIONS)
for ac in list(asicsConfig.values()):
for cc in ac.channelConfig:
cc.setValue("vth_t1", 0)
cc.setValue("vth_t2", 0)
cc.setValue("vth_e", 0)
cc.setValue("trigger_mode_2_b", thresholdIndex)
cc.setValue(thresholdName, 61)
daqd.setAsicsConfig(asicsConfig)
sleep(1 * T)
sleep(counter_sharing * T)
count_high = {}
for portID, slaveID, chipID, channelID in activeChannels:
v = daqd.read_mem_ctrl(portID, slaveID, 5, 24, 64*chipID + channelID, 1)
v = v[0]
v = v / COUNT_MAX
count_high[(portID, slaveID, chipID, channelID)] = v
adjustmentMade = False
for portID, slaveID, chipID, channelID in activeChannels:
b = asicsConfig[(portID, slaveID, chipID)].channelConfig[channelID].getValue(baselineName)
channelOK = True
if count_high[(portID, slaveID, chipID, channelID)] < 0.95:
channelOK = False
if b > 0:
new_b = b - 1
adjustmentMade = True
else:
new_b = b
else:
new_b = b
asicsConfig[(portID, slaveID, chipID)].channelConfig[channelID].setValue(baselineName, new_b)
if not channelOK:
print("Channel (%2d %2d %2d %2d) | %6.1f%% active | %2d -> %2d" % (portID, slaveID, chipID, channelID, 100.0*count_high[(portID, slaveID, chipID, channelID)], b, new_b))
N_ITERATIONS += 1
if not adjustmentMade: break
outFile = open(args.outFilePrefix + "_baseline.tsv", "w")
for portID, slaveID, chipID, channelID in activeChannels:
baseline_T = asicsConfig[(portID, slaveID, chipID)].channelConfig[channelID].getValue("baseline_t")
baseline_E = asicsConfig[(portID, slaveID, chipID)].channelConfig[channelID].getValue("baseline_e")
outFile.write("%d\t%d\t%d\t%d\t%d\t%d\n" % (
portID, slaveID, chipID, channelID,
baseline_T, baseline_E
))
outFile.close()
#
# Measure noise/baseline
#
outFile = open(args.outFilePrefix + "_noise.tsv", "w")
print("Scanning threshold for noise")
noiseProfiles = {}
for thresholdIndex, thresholdName, baselineName in thresholdList:
stdout.write("%6s " % thresholdName); stdout.flush()
for thresholdValue in range(0,64):
for ac in list(asicsConfig.values()):
for cc in ac.channelConfig:
cc.setValue("vth_t1", 0)
cc.setValue("vth_t2", 0)
cc.setValue("vth_e", 0)
cc.setValue("trigger_mode_2_b", thresholdIndex)
cc.setValue(thresholdName, thresholdValue)
daqd.setAsicsConfig(asicsConfig)
sleep(1*T)
next_read_start_time = time() + counter_sharing*T + 1E-3
for n in range(args.dark_reads):
s = next_read_start_time - time()
if s > 0: sleep(s)
next_read_start_time = time() + counter_sharing*T + 1E-3
for portID, slaveID, chipID, channelID in activeChannels:
# Write out fraction of discriminator active time
v = daqd.read_mem_ctrl(portID, slaveID, 5, 24, 64*chipID + channelID, 1)
v = v[0]
v = v / COUNT_MAX
outFile.write("%d\t%d\t%d\t%d\t%s\t%d\t%f\n" % (portID, slaveID, chipID, channelID, thresholdName, thresholdValue, v))
stdout.write(".")
stdout.flush()
stdout.write("\n")
#
# Measure dark counts
#
outFile = open(args.outFilePrefix + "_dark.tsv", "w")
for ac in list(asicsConfig.values()):
if not ac: continue
gc = ac.globalConfig
for cc in ac.channelConfig:
cc.setValue("trigger_mode_1", 0)
cc.setValue("counter_mode", 0xC)
cc.setValue("trigger_b_latched", 0)
cc.setValue("dead_time", 20)
if args.ext_bias:
systemConfig.loadToHardware(daqd, bias_enable=config.APPLY_BIAS_OFF)
input("Set normal operation bias voltage and press ENTER")
else:
systemConfig.loadToHardware(daqd, bias_enable=config.APPLY_BIAS_ON)
darkProfiles = {}
print("Scanning threshold for dark counts")
for thresholdIndex, thresholdName, baselineName in thresholdList:
stdout.write("%6s " % thresholdName); stdout.flush()
for thresholdValue in range(64):
for ac in list(asicsConfig.values()):
for cc in ac.channelConfig:
cc.setValue("vth_t1", 0)
cc.setValue("vth_t2", 0)
cc.setValue("vth_e", 0)
cc.setValue("trigger_mode_2_b", thresholdIndex)
cc.setValue(thresholdName, thresholdValue)
daqd.setAsicsConfig(asicsConfig)
sleep(1*T)
next_read_start_time = time() + counter_sharing*T + 1E-3
for n in range(args.dark_reads):
s = next_read_start_time - time()
if s > 0: sleep(s)
next_read_start_time = time() + counter_sharing*T + 1E-3
for portID, slaveID, chipID, channelID in activeChannels:
# Write out event frequency
v = daqd.read_mem_ctrl(portID, slaveID, 5, 24, 64*chipID + channelID, 1)
v = v[0]
v = v / T
outFile.write("%d\t%d\t%d\t%d\t%s\t%d\t%f\n" % (portID, slaveID, chipID, channelID, thresholdName, thresholdValue, v))
stdout.write(".")
stdout.flush()
stdout.write("\n")
outFile.close()
systemConfig.loadToHardware(daqd, bias_enable=config.APPLY_BIAS_OFF)