forked from EKPCN/MAPSA_Software
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calibration.py
400 lines (305 loc) · 9.89 KB
/
calibration.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
from classes import *
from xml.dom import minidom
#import elementtree.ElementTree
#from elementtree.ElementTree import Element, SubElement, Comment
import xml.etree.ElementTree
from xml.etree.ElementTree import Element, SubElement, Comment
import sys, select, os, array
from array import array
import ROOT
from ROOT import TGraph, TCanvas, gPad, TFile
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import show, plot
from optparse import OptionParser
parser = OptionParser()
parser.add_option('-s', '--setting', metavar='F', type='string', action='store',
default = 'none',
dest = 'setting',
help = 'settings ie default, calibration, testbeam etc')
parser.add_option('-c', '--charge', metavar='F', type='int', action='store',
default = 70,
dest = 'charge',
help = 'Charge for caldac')
parser.add_option('-w', '--shutterdur', metavar='F', type='int', action='store',
default = 0xFFFFF,
dest = 'shutterdur',
help = 'shutter duration')
parser.add_option('-n', '--number', metavar='F', type='int', action='store',
default = 0x5,
dest = 'number',
help = 'number of calstrobe pulses to send')
parser.add_option('-r', '--res', metavar='F', type='int', action='store',
default = 1,
dest = 'res',
help = 'resolution 1,2,3... 1 is best')
parser.add_option('-y', '--string ', metavar='F', type='string', action='store',
default = '',
dest = 'string',
help = 'extra string')
(options, args) = parser.parse_args()
a = uasic(connection="file://connections_test.xml",device="board0")
mapsa = MAPSA(a)
read = a._hw.getNode("Control").getNode('firm_ver').read()
a._hw.dispatch()
print "Running firmware version " + str(read)
#a._hw.getNode("Control").getNode("logic_reset").write(0x1)
#a._hw.dispatch()
a._hw.getNode("Control").getNode("MPA_clock_enable").write(0x1)
a._hw.dispatch()
smode = 0x0 # shutter options
sdur = options.shutterdur
snum = options.number # strobe settings
sdel = 0xF
slen = 0xF
sdist = 0xFF
dcindex=1
buffnum=1
mpa = [] # get all MPAs
for i in range(1,7):
mpa.append(mapsa.getMPA(i))
Confnum=1
configarr = []
if options.setting=='calibration':
CE=1 # Set Calibration Enable bit
else:
CE=0
SP=0 # Signal polarity
config = mapsa.config(Config=1,string='default') # load default cfg
config.upload()
confdict = {'OM':[3]*6,'RT':[0]*6,'SCW':[0]*6,'SH2':[0]*6,'SH1':[0]*6,'THDAC':[0]*6,'CALDAC':[options.charge]*6,'PML':[1]*6,'ARL':[1]*6,'CEL':[CE]*6,'CW':[0]*6,'PMR':[1]*6,'ARR':[1]*6,'CER':[CE]*6,'SP':[SP]*6,'SR':[1]*6,'TRIMDACL':[30]*6,'TRIMDACR':[30]*6}
config.modifyfull(confdict)
mapsa.daq().Strobe_settings(snum,sdel,slen,sdist,cal=CE)
x1 = array('d')
y1 = []
for x in range(0,256): # start threshold scan
if x%options.res!=0:
continue
if x%10==0:
print "THDAC " + str(x)
config.modifyperiphery('THDAC',[x]*6) # change threshold for all 6 MPAs
config.upload()
config.write()
mapsa.daq().Sequencer_init(smode,sdur)
pix,mem = mapsa.daq().read_data(buffnum) # pix = [[MPA1_header,MPA1_header,MPA1_pix1,..,MPA1_pix48],..,[MPA6]]
ipix=0
for p in pix: # Loop over MPAs
p.pop(0) # Drop double header
p.pop(0)
y1.append([]) # Append an empty array
y1[ipix].append(array('d',p)) # Append array of 48 pixels for each MPA and THDAC [MPA1[THDAC1[Pix1,..,Pix48] , .. , THDAC256[Pix1,..,Pix48]],..,MPA6[],[][]..]
ipix+=1
x1.append(x) # Array of THDAC 0-255
print "Generating nominal per pixel trimdac values"
calibconfs = config._confs
calibconfsxmlroot = config._confsxmlroot
c1 = TCanvas('c1', '', 700, 900)
c1.Divide(2,3)
#gr1 = []
xvec = np.array(x1)
thdacvv = []
yarrv = []
grarr = []
xdvals = []
for i in range(0,6): # loop over MPA
backup=TFile("plots/backup_preCalibration_"+options.string+"_MPA"+str(i)+".root","recreate")
calibconfxmlroot = calibconfsxmlroot[i]
xdvals.append(0.)
c1.cd(i+1)
thdacv = []
yarr = np.array(y1[i]) # [ THDAC1[Pix1,..,Pix48] , .. , THDAC256[Pix1,..,Pix48] ]
grarr.append([])
gr1 = []
yarrv.append(yarr) # Equivalent to y1 without empty brackets at the end
for iy1 in range(0,len(yarr[0,:])): # loop over 48 pixels
yvec = yarr[:,iy1] # loop over 256 THDAC for one pixel
if max(yvec)==0:
print "zero"
gr1.append(TGraph(len(x1)-1,array('d',xvec),array('d',yvec)))
if iy1==0:
gr1[iy1].SetTitle(';DAC Value (1.456 mV);Counts (1/1.456)')
grarr[i].append(gr1[iy1])
grarr[i][iy1].Draw()
gr1[iy1].Write(str(iy1))
else:
grarr[i].append(gr1[iy1])
grarr[i][iy1].Draw('same')
gr1[iy1].Write(str(iy1))
gPad.Update()
halfmax = max(yvec)/2.0
maxbin = np.where(yvec==max(yvec))
for ibin in range(0,len(xvec)-1): # Loop over all THDAC
xval = xvec[ibin]
xval1 = xvec[ibin+1]
yval = yvec[ibin]
yval1 = yvec[ibin+1]
if (yval1-halfmax)<0.0 and ibin>maxbin[0][0]: # Falling edge right before half maximum
if iy1%2==0: # Left pixel (config always for two pixels (32bit))
prev_trim = int(calibconfxmlroot[(iy1)/2+1].find('TRIMDACL').text)
else: # Right pixel
prev_trim = int(calibconfxmlroot[(iy1+1)/2].find('TRIMDACR').text)
xdacval = (abs(yval-halfmax)*xval + abs(yval1-halfmax)*xval1)/(abs(yval-halfmax) + abs(yval1-halfmax)) # calculate x @ half maximum
#if abs(yval-halfmax)<abs(yval1-halfmax):
# xdacval = xval
#else:
# xdacval = xval1
#print "xdacval " + str(xdacval)
trimdac = 31 + prev_trim - int(round(xdacval*1.456/3.75)) # x*th_step/trim_step
xdvals[i] += xdacval*1.456/3.75
if ibin%100==0:
print("halfmax: %s" %(halfmax))
print("maxbin: %s" %(maxbin))
print("prev_trim: %s" %(prev_trim))
print("xdaxval: %s" %(xdacval))
print("trimdac: %s" %(trimdac))
thdacv.append(trimdac)
break
if ibin==len(xvec)-2: # No curve, save old value
if iy1%2==0: # Left pixel
prev_trim = int(calibconfxmlroot[(iy1)/2+1].find('TRIMDACL').text)
else: # Right pixel
prev_trim = int(calibconfxmlroot[(iy1+1)/2].find('TRIMDACR').text)
trimdac = int(prev_trim)
thdacv.append(trimdac)
print "UNTRIMMED"
break
thdacvv.append(thdacv)
print thdacv
ave = 0
for x in xdvals:
ave+=x/48. # average per MPA
ave/=6. # average of all MPAs
offset = []
avearr = []
mpacorr = []
for i in range(0,6):
thdacv = thdacvv[i] # [MPA1[TRIMDAC1,..,TRIMDAC256],..,MPA6[]]
ave15 = 0
for j in thdacvv[i]:
ave15+=j
ave15/=len(thdacvv[i])
avearr.append(ave15)
mpacorr.append(xdvals[i]/48.-ave)
#print 'average correction'
#print avearr
#print mpacorr
for i in range(0,6):
thdacv = thdacvv[i]
range1 = min(thdacv)
range2 = max(thdacv)
offset.append(15-int(round(avearr[i]+mpacorr[i])))
#print offset
thdacvvorg = []
cols = [[],[],[],[],[],[]]
for iy1 in range(0,len(yarrv[0][0,:])):
thdacvvorg.append(np.array(thdacvv)[:,iy1])
upldac = []
for i in range(0,6):
thdacv = thdacvv[i]
upldac.append(thdacv[iy1]+offset[i])
for u in range(0,len(upldac)):
upldac[u] = max(0,upldac[u])
upldac[u] = min(31,upldac[u])
if upldac[u]==31:
cols[u].append(2)
elif upldac[u]==0:
cols[u].append(4)
else:
cols[u].append(1)
#print upldac
if iy1%2==0:
config.modifypixel((iy1)/2+1,'TRIMDACL',upldac)
else:
config.modifypixel((iy1+1)/2,'TRIMDACR',upldac)
c1.Print('plots/Scurve_Calibration'+options.string+'_pre.root', 'root')
c1.Print('plots/Scurve_Calibration'+options.string+'_pre.pdf', 'pdf')
c1.Print('plots/Scurve_Calibration'+options.string+'_pre.png', 'png')
config.modifyperiphery('THDAC',[100]*6)
config.upload()
config.write()
for i in range(0,6):
xmlrootfile = config._confsxmltree[i]
print xmlrootfile
a = config._confsxmlroot[i]
print "writing data/Conf_calibrated_MPA"+str(i+1)+"_config1.xml"
xmlrootfile.write("data/Conf_calibrated_MPA"+str(i+1)+"_config1.xml")
### Same threshold scan with calibrated pixel
print "Testing Calibration"
config1 = mapsa.config(Config=1,string='calibrated')
config1.upload()
config1.modifyperiphery('OM',[3]*6)
config1.modifyperiphery('RT',[0]*6)
config1.modifyperiphery('SCW',[0]*6)
config1.modifyperiphery('SH2',[0]*6)
config1.modifyperiphery('SH1',[0]*6)
config1.modifyperiphery('THDAC',[0]*6)
config1.modifyperiphery('CALDAC', [options.charge]*6)
for x in range(1,25):
config1.modifypixel(x,'PML', [1]*6)
config1.modifypixel(x,'ARL', [1]*6)
config1.modifypixel(x,'CEL', [CE]*6)
config1.modifypixel(x,'CW', [0]*6)
config1.modifypixel(x,'PMR', [1]*6)
config1.modifypixel(x,'ARR', [1]*6)
config1.modifypixel(x,'CER', [CE]*6)
config1.modifypixel(x,'SP', [SP]*6)
config1.modifypixel(x,'SR', [1]*6)
config1.write()
x1 = array('d')
y1 = []
for x in range(0,256):
if x%options.res!=0:
continue
if x%10==0:
print "THDAC " + str(x)
config1.modifyperiphery('THDAC',[x]*6)
config1.upload()
config1.write()
mapsa.daq().Sequencer_init(smode,sdur)
pix,mem = mapsa.daq().read_data(buffnum)
ipix=0
for p in pix:
p.pop(0)
p.pop(0)
y1.append([])
y1[ipix].append(array('d',p))
ipix+=1
x1.append(x)
c2 = TCanvas('c2', '', 700, 900)
c2.Divide(2,3)
xvec = np.array(x1)
yarrv = []
gr2arr = []
means = []
for i in range(0,6):
backup=TFile("plots/backup_postCalibration_"+options.string+"_MPA"+str(i)+".root","recreate")
c2.cd(i+1)
yarr = np.array(y1[i])
gr2arr.append([])
gr2 = []
means.append(0.)
yarrv.append(yarr)
for iy1 in range(0,len(yarr[0,:])):
yvec = yarr[:,iy1]
gr2.append(TGraph(len(x1)-1,array('d',xvec),array('d',yvec)))
if iy1==0:
gr2[iy1].SetTitle(';DAC Value (1.456 mV);Counts (1/1.456)')
gr2arr[i].append(gr2[iy1])
gr2arr[i][iy1].SetLineColor(cols[i][iy1])
gr2arr[i][iy1].Draw()
gr2[iy1].Write(str(iy1))
else:
gr2arr[i].append(gr2[iy1])
gr2arr[i][iy1].SetLineColor(cols[i][iy1])
gr2arr[i][iy1].Draw('same')
gr2[iy1].Write(str(iy1))
gPad.Update()
means[i]+=gr2[iy1].GetMean(1)
print 'Means'
for m in means:
print m/48.
c2.Print('plots/Scurve_Calibration'+options.string+'_post.root', 'root')
c2.Print('plots/Scurve_Calibration'+options.string+'_post.pdf', 'pdf')
c2.Print('plots/Scurve_Calibration'+options.string+'_post.png', 'png')
print ""
print "Done"