/
write_threshold_file.py
162 lines (130 loc) · 5.21 KB
/
write_threshold_file.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
import sys
import numpy as np
import h5py
from matplotlib import pyplot as plt
import os
import json
from tqdm import tqdm
from scipy.optimize import curve_fit
def io_channel_to_tile(io_channel):
return np.floor((io_channel.astype(int)-1-((io_channel.astype(int)-1)%4))/4+1).astype(int)
def unique_id(io_group, io_channel, chip_id, channel_id):
return np.add( np.add(np.add(io_group*1000, 4*io_channel_to_tile(io_channel))*1000, chip_id)*100, channel_id)
def tile_to_io_channel(tile):
io_channel=[]
for i in range(1,5,1):
io_channel.append( ((tile-1)*4)+i )
return io_channel
def unique_to_possible_io_channels(unique):
io_channel=ch_id(int(unique))
tile=io_channel_to_tile(io_channel)
return tile_to_io_channel(tile)
def ch_id(unique):
return np.mod(unique, 100)
def chip_id(unique):
return (unique//100)%1000
def ioch(unique):
return ((unique//100)//1000)%1000
def iog(unique):
return (((unique//100)//1000)//1000)%1000
vcm_dac = 68.
vref_dac = 223.
def convert_adc_to_mv(adc, vref_dac=223, vcm_dac=68, vdda=1650):
vcm = vcm_dac/256*vdda
vref = vref_dac/256*vdda
return adc*(vref-vcm)/256 + vcm
def convert_mv_to_adc(mv, vref_dac, vcm_dac, vdda=1650):
vcm = vcm_dac/256*vdda
vref = vref_dac/256*vdda
return (mv-vcm)*256/(vref-vcm)
def change_vref_vcm(mv, vref_old, vcm_old, vref_new, vcm_new):
return convert_adc_to_mv(convert_mv_to_adc(mv, vref_old, vcm_old), vref_new, vcm_new)
import json
def get_channel_dict(filename, vref_dac=vref_dac, vcm_dac=vcm_dac, plot=True, tag=''):
ntrigs = []
print('Analyzing {}...'.format(filename))
with h5py.File(filename) as f:
count = 0
packets = f['packets']
ppackets = packets[ packets['channel_id'] < 64 ]
ppackets = ppackets[ np.logical_and(ppackets['chip_id'] < 111, ppackets['chip_id'] > 10) ]
#ppackets = ppackets[np.logical_and(ppackets['io_channel'].astype(int) > 20,ppackets['io_channel'].astype(int) < 25) ]
print('percent valid packets:\t', np.sum( ppackets['valid_parity']==1 )/len(ppackets))
data = ppackets[ np.logical_and( ppackets['packet_type']==0, ppackets['valid_parity']==1 ) ]
print('total data packets:\t', len(data) )
uniques = unique_id(data['io_group'].astype(int), data['io_channel'].astype(int), data['chip_id'].astype(int), data['channel_id'].astype(int))
luniq = np.array(list(set(uniques)))
np.random.shuffle(luniq)
print(len(list(luniq)), 'total channels')
d = {}
dw = convert_adc_to_mv(data['dataword'],vref_dac,vcm_dac)
counter=0
total_channels = len(luniq)
fig=plt.figure()
ax = fig.add_subplot()
ax.hist(dw, bins=50)
ax.set_yscale('log')
ax.set_title('all datawords')
pbar=tqdm(total=len(list(luniq)))
for chan in luniq:
pbar.update(1)
mask = uniques==chan
d[int(chan)] = [ np.quantile(dw[mask], 0.05), np.median(dw[mask]), np.std(dw[mask]) ]
ntr = len(dw[mask])
if ntr < 1e4: ntrigs.append(ntr)
if count >15: return
if plot:
#if len(dw[mask]) < 10: continue
#if not chan in gt250: continue
count+=1
if count > 20: continue
fig = plt.figure()
ax = fig.add_subplot()
ax.set_title('sample channel: '+ str(chan) )
ax.hist(dw[mask], bins=25, label='packets', range=(min(dw[mask])-100, min(dw[mask])+100))
ax.axvline(np.quantile(dw[mask], 0.05), label='threshold', color='red', linestyle='--')
ax.set_xlabel('front end voltage [mV]')
ax.legend()
plt.show()
counter+=1
pbar.close()
if plot:
fig = plt.figure()
ax = fig.add_subplot()
ax.hist(ntrigs, bins=50)
ax.set_yscale('log')
ax.set_title('Number of Triggers')
plt.show()
with open('extracted-'+tag+'.json', 'w') as ff: json.dump(d, ff)
return
def get_key_channel(channel):
chkey='{}-{}-{}'.format(iog(int(channel)), ioch(int(channel)), chip_id(int(channel)))
chan = ch_id(int(channel))
return chkey, chan
def main(thresholdfile, pedfile):
#get_channel_dict(pedfile, tag='ped', plot=False)
all_thresholds={}
allthr=[]
pedestal = {}
with open(pedfile, 'r') as f: pedestal=json.load(f)
thresh = {}
with open(thresholdfile, 'r') as f: thresh=json.load(f)
allthr=[]
thr={}
for channel in thresh.keys():
if not channel in pedestal.keys():
print(channel)
continue
if thresh[channel][2]>2.: continue
allthr.append(thresh[channel][1]-pedestal[channel][0])
thr[channel]=thresh[channel][1]-pedestal[channel][0]
fig=plt.figure()
ax=fig.add_subplot()
ax.set_title('Extracted Thresholds: Calo0 Run')
ax.set_xlabel('channel threshold [mV]')
ax.hist(allthr, bins=100)#, range=(100, 300))
with open('threshold-log.json', 'w') as f: json.dump(thr, f, indent=4)
plt.show()
if __name__=='__main__':
if len(sys.argv)==3:
main(sys.argv[1], sys.argv[2])