-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanaData.py
369 lines (326 loc) · 11.3 KB
/
anaData.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# Base script to analyse standard data written in txt (yaml) format
# OMH Aug 29, 2018
import os
import time
import sys
import math
import yaml # To read data in txt format
import numpy as np
import pylab as pl
from scipy.optimize import curve_fit
# Binary dat ainterpreter: pyef
# module. Do ./setup.py build & ./setup.py install if not loadable [email Gu Junhua Sept 27 2017]
#import pyef
DISPLAY = 0
print 'DISPLAY = ',DISPLAY
pl.ion()
def loopEvents(RUNID,boardID,TYPE):
#datadir = "/home/pastsoft/data"
datadir = "/home/martineau/GRAND/GRANDproto35/data/"
filename = datadir+TYPE+RUNID+".data.yaml"
if os.path.isfile(filename) is False:
print 'File ',filename,'does not exist. Aborting.'
return
datafile = filename.split('/')
if TYPE == 'C':
nch = 4 # Plot calibrator channel
else:
nch = 3
# Read data
print 'Scanning data file',filename
print "Loading data..."
dataf=yaml.load_all(open(filename))
print "Done."
data = list()
freq=50e6 # Sampling Frequency
dt=1.0/freq
tdeb = 0
tend = 0
date = []
IP = []
TS1Trig = []
SSS = []
TS2 = []
TS1PPS = []
EvtId = []
TrigPattern = []
board = []
imax = []
Amax = []
mub = []
sigb = []
# Loop on events
j = 0; # Index of array filling (because date & data are "append")
#try:
if 1:
for d in dataf:
#determine whether it is a data:
if d['msg_type']=='DATA':
#print d.keys()
print "Event",j,"from ID ",d['source_ip']
print d['event_count']
IP.append(d['source_ip'])
board.append(int(d['source_ip'][-1])-100)
TS1Trig.append(d['ts1trigger'])
SSS.append(d['sss'])
TS2.append(d['ts2'])
TS1PPS.append(d['ts1pps'])
EvtId.append(d['event_count'])
TrigPattern.append(d['trig_pattern'])
date.append(d['received_timestamp_str'])
raw=d['data']
#raw2 = raw[0].split(" ") # Cut raw data list into samples
#raw2 = raw2[0:np.size(raw2)-1] # Remove last element (empty)
raw2 = raw
if TYPE == "P": # Pattern run, keep data value in binary format
draw = [int(a) for a in raw2]
else: # Other run types
hraw2 = [hex(int(a)) for a in raw2] # Transfer back to hexadecimal
draw = [twos_comp(int(a,16), 12) for a in hraw2] #2s complements
if TYPE != "P": # Transfer data to volts
draw = np.array(draw)*1./2048 # in Volts
nsamples = len(draw)/4 # draw corresponds to 4 channels
offset = int(nsamples/2.0) # Offset position at center of waveform
#print nsamples,"samples per channel --> offset = ",offset
thisEvent = np.reshape(draw,(4,nsamples));
data.append(thisEvent) # Write to data list
#print 'Sampling frequency=',freq,'MHz, time step=',dt,'s'
if DISPLAY:
print 'Event ',j
thisEv = pow(10,(thisEvent+np.min(thisEvent)))
t = dt*np.array(range(np.shape(thisEvent)[1]))
t = t* 1e6 #in mus
pl.figure(j)
if TYPE == "C":
pl.subplot(221)
else:
pl.subplot(311)
#pl.plot(t[3:],thisEvent[0][3:])
pl.plot(t[3:],thisEv[0][3:])
pl.xlim(t[3],max(t))
pl.xlabel('Time ($\mu$s)')
if TYPE == "P":
pl.ylabel('LSB')
else:
pl.ylabel('Amplitude (V)')
pl.grid(True)
if TYPE == "C":
pl.subplot(222)
else:
pl.subplot(312)
pl.xlabel('Time ($\mu$s)')
pl.xlim(t[3],max(t))
if TYPE == "0":
pl.ylabel('LSB')
else:
pl.ylabel('Amplitude (V)')
#pl.plot(t[3:],thisEvent[1][3:])
pl.plot(t[3:],thisEv[1][3:])
pl.grid(True)
if TYPE == "C":
pl.subplot(223)
else:
pl.subplot(313)
#pl.plot(t[3:],thisEvent[2][3:])
pl.plot(t[3:],thisEv[2][3:])
pl.xlim(t[3],max(t))
pl.xlabel('Time ($\mu$s)')
if TYPE == "P":
pl.ylabel('LSB')
else:
pl.ylabel('Amplitude (V)')
pl.grid(True)
if TYPE == "C":
pl.subplot(224)
pl.plot(t[3:],thisEvent[3][3:])
pl.xlabel('Time ($\mu$s)')
pl.ylabel('Amplitude (V)')
pl.grid(True)
pl.suptitle('Board {0} Event {1}'.format(board[j],EvtId[j]))
if TYPE == "C": # Plotting calibrator signal in Calibration mode
pl.plot(t[3:],thisEvent[3][3:],'s')
# Fit calibration signal with sine wave
xr = t[3:] #mus
w = 2*np.pi*66.666666 #rad/mus
yr = thisEvent[3][3:]
fitfunc = lambda xr, a, b, c: a*np.sin(w*xr+b)+c # Create fit function
abeg = float(np.max(yr)-np.min(yr))
p, pcov = curve_fit(fitfunc,xr,yr,p0 = [abeg,0.0,0.0]) #Perform fit
print 'Fit results:',p,np.sqrt(np.diag(pcov))
xf=np.linspace(xr[0],xr[-1],10000) # Display fit result wuith nice thinning
pl.plot(xf,fitfunc(xf,p[0],p[1],p[2]))
pl.show()
raw_input()
pl.close(j)
# Assemble stat for summary plots
iimax = np.zeros(shape=(1,nch),dtype=int)
iAmax = np.zeros(shape=(1,nch),dtype=float)
imub = np.zeros(shape=(1,nch))
isigb = np.zeros(shape=(1,nch))
for k in range(nch):
iimax[0,k] = np.argmax(thisEvent[k][3:])+3; # Skip 1st 3 points because could be left overs from previous events
iAmax[0,k] = thisEvent[k][iimax[0,k]];
imub[0,k] = np.mean(thisEvent[k][1:offset-5])
isigb[0,k] = np.std(thisEvent[k][1:offset-5])
#print iimax[0]
imax.append(iimax[0])
Amax.append(iAmax[0])
mub.append(imub[0])
sigb.append(isigb[0])
j = j+1
#except NameError:
# print "NameError"
#except:
# print "Unknown error while reading data (end of file?) ==> abort reading."
if TYPE == "P":
return
SSS = np.array(SSS)
IP = np.array(IP)
TS1Trig = np.array(TS1Trig)
SSS = np.array(SSS)
TS2 = np.array(TS2)
TS1PPS = np.array(TS1PPS)
EvtId = np.array(EvtId )
TrigPattern = np.array(TrigPattern)
board = np.array(board)
imax = np.array(imax)
Amax = np.array(Amax)
mub = np.array(mub)
sigb = np.array(sigb)
board = np.array(board)
nevts = np.size(SSS)
print "Nb events=", nevts
# Now display summary plots
trigtime = np.zeros(shape=(np.size(SSS)))
timein = np.where(SSS>0)
if np.size(timein) > 0: # GPS timing info is available
tdeb = min(SSS[timein])
tend = max(SSS)
dur = tend-tdeb+1 # Run duration [seconds]
t = range(dur)
boards = set(board[np.where(board>0)])
DataRate = np.zeros(shape=(dur,len(boards)))
TrigRate = np.zeros(shape=(dur,len(boards)))
print 'Run start:', date[0]
print 'Boards in run:',list(boards)
j = 0
#for id in boards: # Loop on all boards in run
for id in [int(boardID)]: # Loop on all boards in run
sel = np.where(board == id)
date_end = date[sel[0][-1]]
print 'Run stop:',date_end,'for board',id,' (',np.size(sel),'measurements)'
if np.size(timein) > 0:
# To be implemented: read MaxCoarse info from slow control data and define correction factor accordingly cor = 125e6/MaxCoarse
# See anaTiming.py line 133 and following for guidance
cor = 1.0
else:
cor=1.0
print 'Correction factor for 125MHz clock for board',id,':',cor
# Build trig time
trigtime[sel] = SSS[sel]+(TS2[sel]*4+TS1PPS[sel]-TS1Trig[sel])*2e-9*cor #second.
# Compute trig rate
for i in range(dur):
ts = tdeb+i
thisSec = np.where(SSS[sel]==ts)
thisEvtId = EvtId[sel]
if np.size(thisSec) > 0:
thisSec=thisSec[:][0]
DataRate[i,j] = np.size(thisSec)
TrigRate[i,j] = thisEvtId[thisSec[-1]]-thisEvtId[thisSec[0]]+1 #Nb of events rigged in that second --> trigrate
pl.figure(2)
pl.plot(t,TrigRate[:,j],label='Trig rate - Board '+str(id))
pl.plot(t,DataRate[:,j],label='Data rate - Board '+str(id))
pl.grid(True)
pl.xlabel('Run time (s)')
pl.ylabel('Data rate (Hz)')
pl.legend()
pl.title('Data rate')
# Displays
for k in range(nch):
if 1:
good = np.where( (imax[sel,k][0]>104) & (imax[sel,k][0]<108))
abline = np.where( (Amax[sel,k][0]<0))
azero = np.where( (Amax[sel,k][0]==0))
print 'Channel',k,': good events=',np.size(good),'/',np.size(sel),'=',float(np.size(good))/np.size(sel)
print 'Channel',k,': Max at zero=',np.size(azero),'/',np.size(sel),'=',float(np.size(azero))/np.size(sel)
print 'Channel',k,': Max < zero=',np.size(abline),'/',np.size(sel),'=',float(np.size(abline))/np.size(sel)
pl.figure(id*100+21+k)
pl.subplot(231)
pl.hist(mub[sel,k][0],offset*2)
pl.xlabel('Baseline mean')
pl.title('Board {0}'.format(id))
pl.grid(True)
pl.subplot(235)
pl.plot(mub[sel,k][0],'+')
pl.plot(Amax[sel,k][0],'o')
pl.xlabel('Event ID')
pl.ylabel('Mean amp (bline & max)')
pl.title('Board {0}'.format(id))
pl.grid(True)
pl.subplot(234)
pl.xlabel('Index of signal max')
pl.hist(imax[sel,k][0],offset*2)
pl.title('Board {0}'.format(id))
pl.grid(True)
pl.subplot(236)
pl.xlabel('Max amplitude')
pl.hist(Amax[sel,k][0],offset*2)
pl.title('Board {0}'.format(id))
pl.grid(True)
pl.subplot(232)
diffAmp = Amax[sel,k][0]-mub[sel,k][0]
pl.hist(sigb[sel,k][0],offset*2)
pl.xlabel('Bline std dev')
pl.title('Board {0}'.format(id))
pl.grid(True)
print 'Channel',k,': bline @ ',np.mean((mub[sel,k][0])),'pm',np.std((mub[sel,k][0])),'V. Std dev=',np.mean((sigb[sel,k][0])),'V'
print 'Channel',k,': Peak @ ',np.mean((Amax[sel,k][0])),'V, std dev=',np.std((Amax[sel,k][0])),'V, rel error=',np.std((Amax[sel,k][0]))/np.mean((Amax[sel,k][0]))*100,'%'
print 'Channel',k,': Peak - bline @ ',np.mean((diffAmp)),'V, std dev=',np.std((diffAmp)),'V, rel error=',np.std((diffAmp))/np.mean((diffAmp))*100,'%'
pl.subplot(233)
pl.plot(mub[sel,k][0],sigb[sel,k][0],'+')
pl.xlabel('Baseline mean')
pl.ylabel('Bline std dev')
pl.title('Board {0}'.format(id))
pl.grid(True)
j = j+1
sel = np.where(trigtime>0) #GPS time info present
if np.size(sel)>0:
first = trigtime[sel[0][0]]
last = trigtime[sel[0][-1]]
dur = last-first
print 'Nevents = ',np.size(sel)
print 'Duration [s]',first,last,dur
rate = np.size(sel)/dur
print 'Rate=',rate,'Hz'
pl.figure(18)
pl.plot(trigtime)
pl.xlabel('Evt ID')
pl.ylabel('Trig time [s]')
def get_1stone(val):
if val == '0x1':
return 0
if val == '0x3':
return 1
if val == '0x7':
return 2
if val == '0xf':
return 3
if val == '0x1f':
return 4
if val == '0x3f':
return 5
if val == '0x7f':
return 6
if val == '0xff':
return 7
return 8
def twos_comp(val, bits):
"""compute the 2's compliment of int value val"""
if (val & (1 << (bits - 1))) != 0: # if sign bit is set e.g., 8bit: 128-255
val = val - (1 << bits) # compute negative value
return val
if __name__ == '__main__':
if len(sys.argv)!=4:
print "Usage: >loopEvents RUNID boardID TYPE"
else:
loopEvents(sys.argv[1],sys.argv[2],sys.argv[3])