forked from cms-gem-daq-project/vfatqc-python-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonScript_904.py
More file actions
executable file
·745 lines (645 loc) · 27 KB
/
pythonScript_904.py
File metadata and controls
executable file
·745 lines (645 loc) · 27 KB
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
#!/bin/env python
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 31 09:28:14 2016
@author: Hugo
@modifiedby: Jared
@modifiedby: Christine
"""
import sys, os, random, time
if __name__ == "__main__":
from qcoptions import parser
parser = OptionParser()
(options, args) = parser.parse_args()
print "Obsolete, do not use"
exit(1)
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/kernel")
from ipbus import *
import subprocess,datetime
startTime = datetime.datetime.now().strftime("%Y.%m.%d.%H.%M")
print startTime
# Unbuffer output
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
tee = subprocess.Popen(["tee", "%s-log.txt"%(startTime)], stdin=subprocess.PIPE)
os.dup2(tee.stdin.fileno(), sys.stdout.fileno())
os.dup2(tee.stdin.fileno(), sys.stderr.fileno())
Passed = '\033[92m > Passed... \033[0m'
Failed = '\033[91m > Failed... \033[0m'
def txtTitle(str):
print '\033[1m' + str + '\033[0m'
pass
import cProfile, pstats, StringIO
pr = cProfile.Profile()
pr.enable()
gilbIP = raw_input("> Enter the AMC's IP address: ")
Date = raw_input("> Enter the Name of the Test [In case of conflict, the old file will be overwrite]: ")
glib = GLIB(gilbIP.strip())
GLIB_REG_TEST = raw_input("> Number of register tests to perform on the AMC [100]: ")
sys.stdout.flush()
OH_REG_TEST = raw_input("> Number of register tests to perform on the OptoHybrid [100]: ")
sys.stdout.flush()
I2C_TEST = raw_input("> Number of I2C tests to perform on the VFAT2s [100]: ")
sys.stdout.flush()
TK_RD_TEST = raw_input("> Number of tracking data packets to readout [100]: ")
sys.stdout.flush()
RATE_WRITE = raw_input("> Write the data to disk when testing the rate [Y/n]: ")
sys.stdout.flush()
GLIB_REG_TEST = 100 if GLIB_REG_TEST == "" else int(GLIB_REG_TEST)
OH_REG_TEST = 100 if OH_REG_TEST == "" else int(OH_REG_TEST)
I2C_TEST = 100 if I2C_TEST == "" else int(I2C_TEST)
TK_RD_TEST = 100 if TK_RD_TEST == "" else int(TK_RD_TEST)
RATE_WRITE = False if (RATE_WRITE == "N" or RATE_WRITE == "n") else True
THRESH_ABS = 0.1
THRESH_REL = 0.05
THRESH_MAX = 255
THRESH_MIN = 0
N_EVENTS = 1000.00
N_EVENTS_SCURVE = 1000.00
N_EVENTS_TRIM = 1000.00
VCAL_MIN = 0
VCAL_MAX = 255
MAX_TRIM_IT = 26
CHAN_MIN=0
CHAN_MAX=128
DACDef = 16
print
sys.stdout.flush()
####################################################
txtTitle("A. Testing the GLIB's presence")
print " Trying to read the GLIB board ID... If this test fails, the script will stop."
if (glib.get("board_id") != None ):
print Passed
else:
print Failed
sys.exit()
pass
testA = True
print
####################################################
txtTitle("B. Testing the OH's presence")
print " Trying to set the OptoHybrid registers... If this test fails, the script will stop."
glib.set("oh_sys_clk_src", 1)
glib.set("oh_sys_t1_src", 1)
glib.set("oh_sys_trigger_lim", 0)
if (glib.get("oh_sys_t1_src") == 1):
print Passed
else:
print Failed
sys.exit()
pass
testB = True
print
####################################################
txtTitle("C. Testing the GLIB registers")
print " Performing single and FIFO reads on the GLIB counters and ensuring they increment."
countersSingle = []
countersFifo = []
countersTest = True
for i in range(0, GLIB_REG_TEST): countersSingle.append(glib.get("glib_cnt_stb_cnt"))
countersFifo = glib.fifoRead("glib_cnt_stb_cnt", GLIB_REG_TEST)
for i in range(1, GLIB_REG_TEST):
if (countersSingle[i - 1] + 1 != countersSingle[i]):
print "\033[91m > #%d previous %d, current %d \033[0m"%(i, countersSingle[i-1], countersSingle[i])
countersTest = False
pass
if (countersFifo[i - 1] + 1 != countersFifo[i]):
print "\033[91m > #%d previous %d, current %d \033[0m"%(i, countersFifo[i-1], countersFifo[i])
countersTest = False
pass
pass
if (countersTest): print Passed
else: print Failed
testC = countersTest
print
####################################################
txtTitle("D. Testing the OH registers")
print " Performing single and FIFO reads on the OptoHybrid counters and ensuring they increment."
countersSingle = []
countersFifo = []
countersTest = True
for i in range(0, OH_REG_TEST): countersSingle.append(glib.get("oh_cnt_wb_gtx_stb"))
countersFifo = glib.fifoRead("oh_cnt_wb_gtx_stb", OH_REG_TEST)
for i in range(1, OH_REG_TEST):
if (countersSingle[i - 1] + 1 != countersSingle[i]):
print "\033[91m > #%d previous %d, current %d \033[0m"%(i, countersSingle[i-1], countersSingle[i])
countersTest = False
pass
if (countersFifo[i - 1] + 1 != countersFifo[i]):
print "\033[91m > #%d previous %d, current %d \033[0m"%(i, countersFifo[i-1], countersFifo[i])
countersTest = False
pass
pass
if (countersTest): print Passed
else: print Failed
testD = countersTest
print
####################################################
txtTitle("E. Detecting the VFAT2s over I2C")
print " Detecting VFAT2s on the GEM by reading out their chip ID."
presentVFAT2sSingle = []
presentVFAT2sFifo = []
glib.set("ei2c_reset", 0)
glib.get("vfat2_all_chipid0")
chipID0s = glib.fifoRead("ei2c_data", 24)
glib.set("ei2c_reset", 0)
glib.get("vfat2_all_chipid1")
chipID1s = glib.fifoRead("ei2c_data", 24)
chipIDs = []
for i in range(0, 24):
# missing VFAT shows 0x0003XX00 in I2C broadcast result
# 0x05XX0800 in I2C single request mode
# XX is slot number
# so if ((result >> 16) & 0x3) == 0x3, chip is missing
# or if ((result) & 0x30000) == 0x30000, chip is missing
if (((glib.get("vfat2_" + str(i) + "_chipid0") >> 24) & 0x5) != 0x5): presentVFAT2sSingle.append(i)
if (((chipID0s[i] >> 16) & 0x3) != 0x3): presentVFAT2sFifo.append(i)
chipIDs.append(((chipID1s[i]&0xff)<<8)+(chipID0s[i]&0xff))
pass
if (presentVFAT2sSingle == presentVFAT2sFifo): Passed
else: Failed
testE = True
print
for i in range(0,24):
if (chipIDs[i] & 0xffff !=0):
print ("VFAT2 connected at port %d has the ID : 0x%04x"%(i, chipIDs[i] & 0xffff))
else:
print ("No VFAT2 connected at port %d" %i)
pass
pass
print
####################################################
txtTitle("F. Testing the I2C communication with the VFAT2s")
print " Performing random read/write operation on each connect VFAT2."
testF = True
for i in presentVFAT2sSingle:
validOperations = 0
for j in range(0, I2C_TEST):
writeData = random.randint(0, 255)
glib.set("vfat2_" + str(i) + "_ctrl3", writeData)
readData = glib.get("vfat2_" + str(i) + "_ctrl3") & 0xff
if (readData == writeData): validOperations += 1
pass
glib.set("vfat2_" + str(i) + "_ctrl3", 0)
if (validOperations == I2C_TEST): print Passed, "#" + str(i)
else:
print Failed, "#%d received %d, expected %d"%(i, validOperations, I2C_TEST)
testF = False
pass
pass
print
####################################################
txtTitle("G. Reading out tracking data")
print " Sending triggers and testing if the Event Counter adds up."
glib.set("ei2c_reset", 0)
glib.set("vfat2_all_ctrl0", 0)
testG = True
for i in presentVFAT2sSingle:
glib.set("t1_reset", 1)
glib.set("t1_mode", 0)
glib.set("t1_type", 0)
glib.set("t1_n", TK_RD_TEST)
glib.set("t1_interval", 600)
glib.set("vfat2_" + str(i) + "_ctrl0", 55)
glib.set("oh_sys_vfat2_mask", ~(0x1 << i))
glib.set("tk_data_rd", 0)
nPackets = 0
timeOut = 0
ecs = []
glib.set("t1_toggle", 1)
while (glib.get("tk_data_cnt") != 7 * TK_RD_TEST):
timeOut += 1
if (timeOut == 10 * TK_RD_TEST): break
pass
while (glib.get("tk_data_empty") != 1):
packets = glib.fifoRead("tk_data_rd", 7)
if options.debug:
print packets
pass
sys.stdout.flush()
ec = int((0x00000ff0 & packets[0]) >> 4)
nPackets += 1
ecs.append(ec)
pass
glib.set("vfat2_" + str(i) + "_ctrl0", 0)
if (nPackets != TK_RD_TEST):
print Failed, "#%d received %d, expected %d"%(i, nPackets, TK_RD_TEST)
else:
followingECS = True
for j in range(0, TK_RD_TEST - 1):
if (ecs[j + 1] == 0 and ecs[j] == 255):
pass
elif (ecs[j + 1] - ecs[j] != 1):
followingECS = False
print "\033[91m > #%d previous %d, current %d \033[0m"%(i, ecs[j], ecs[j+1])
pass
pass
if (followingECS): print Passed, "#" + str(i)
else:
print Failed, "#%d received %d, expected %d, noncontinuous ECs"%(i, nPackets, TK_RD_TEST)
testG = False
pass
pass
pass
print
####################################################
txtTitle("H. Reading out tracking data")
print " Turning on all VFAT2s and looking that all the Event Counters add up."
testH = True
if (testG):
glib.set("ei2c_reset", 0)
glib.set("vfat2_all_ctrl0", 55)
mask = 0
for i in presentVFAT2sSingle: mask |= (0x1 << i)
glib.set("oh_sys_vfat2_mask", ~mask)
glib.set("t1_reset", 1)
glib.set("t1_mode", 0)
glib.set("t1_type", 2)
glib.set("t1_n", 1)
glib.set("t1_interval", 10)
glib.set("t1_toggle", 1)
glib.set("tk_data_rd", 1)
glib.set("t1_reset", 1)
glib.set("t1_mode", 0)
glib.set("t1_type", 0)
glib.set("t1_n", TK_RD_TEST)
glib.set("t1_interval", 400)
glib.set("t1_toggle", 1)
nPackets = 0
timeOut = 0
ecs = []
while (glib.get("tk_data_cnt") != len(presentVFAT2sSingle) * TK_RD_TEST):
timeOut += 1
if (timeOut == 20 * TK_RD_TEST): break
pass
while (glib.get("tk_data_empty") != 1):
packets = glib.fifoRead("tk_data_rd", 7)
ec = int((0x00000ff0 & packets[0]) >> 4)
nPackets += 1
ecs.append(ec)
pass
glib.set("ei2c_reset", 0)
glib.set("vfat2_all_ctrl0", 0)
if (nPackets != len(presentVFAT2sSingle) * TK_RD_TEST):
print Failed, "#%d received: %d, expected: %d"%(i,nPackets, len(presentVFAT2sSingle) * TK_RD_TEST)
else:
followingECS = True
for i in range(0, TK_RD_TEST - 1):
for j in range(0, len(presentVFAT2sSingle) - 1):
if (ecs[i * len(presentVFAT2sSingle) + j + 1] != ecs[i * len(presentVFAT2sSingle) + j]):
print "\033[91m > #%d saw %d, %d saw %d \033[0m"%(j+1, ecs[i * len(presentVFAT2sSingle) + j + 1],
j, ecs[i * len(presentVFAT2sSingle) + j])
followingECS = False
pass
pass
if (ecs[(i + 1) * len(presentVFAT2sSingle)] == 0 and ecs[i * len(presentVFAT2sSingle)] == 255):
pass
elif (ecs[(i + 1) * len(presentVFAT2sSingle)] - ecs[i * len(presentVFAT2sSingle)] != 1):
print "\033[91m > #%d previous %d, current %d \033[0m"%(i, ecs[i * len(presentVFAT2sSingle)],
ecs[(i+1) * len(presentVFAT2sSingle)])
followingECS = False
pass
pass
if (followingECS): print Passed
else:
print Failed
testH = False
pass
pass
glib.set("t1_reset", 1)
pass
else:
print " Skipping this test as the previous test did not succeed..."
testH = False
print
pass
########################## The Script ################################
# For each VFAT2 Connected
for port in presentVFAT2sSingle:
## this is hacked to ignore middle column
if options.doMiddle:
if port>7 and port<16:
continue
pass
if options.debug:
if port != 1 and port != 5:
continue
pass
if options.special:
if port !=5 :
continue
pass
print "------------------------------------------------------"
print "--------------- Testing VFAT2 port %s" %port,"-----------------"
print "------------------------------------------------------"
nameIP = gilbIP.replace(".","_")
f = open("%s_Data_GLIB_IP_%s_VFAT2_%d_ID_0x%04x"%(str(Date),str(nameIP),port,chipIDs[port]&0xffff),'w')
m = open("%s_SCurve_by_channel_VFAT2_%d_ID_0x%04x"%(str(Date),port,chipIDs[port]&0xffff),'w')
z = open("%s_Setting_GLIB_IP_%s_VFAT2_%d_ID_0x%04x"%(str(Date),str(nameIP),port,chipIDs[port]&0xffff),'w')
#f = open(str(Date)+"_Data_GLIB_IP_"+ str(nameIP)+"_VFAT2_" + str(port)+ "_ID_" + str(chipIDs[port]&0xff),'w')
#m = open(str(Date)+"_SCurve_by_channel"+ "_VFAT2_" + str(port)+ "_ID_" + str(chipIDs[port]&0xff),'w')
#z = open(str(Date)+"_Setting_GLIB_IP_"+ str(nameIP)+"_VFAT2_" + str(port)+ "_ID_" + str(chipIDs[port]&0xff),'w')
z.write(time.strftime("%Y/%m/%d") +"-" +time.strftime("%H:%M:%S")+"\n")
z.write("chip ID: 0x%04x"%(chipIDs[port])+"\n")
TotVCal0 = []
TotVCal31 = []
TotFoundVCal = []
VCal_ref0 = 0
VCal_ref31 = 0
# should make sure all chips are off first?
glib.set("vfat2_all_ctrl0", 0)
glib.set("oh_trigger_source", 1)
glib.set("vfat2_" + str(port) + "_ctrl0", 55)
glib.set("vfat2_" + str(port) + "_ctrl1", 0)
glib.set("vfat2_" + str(port) + "_ctrl2", 48)
glib.set("vfat2_" + str(port) + "_ctrl3", 0)
glib.set("vfat2_" + str(port) + "_ipreampin", 168)
z.write("ipreampin: 168\n")
glib.set("vfat2_" + str(port) + "_ipreampfeed", 80)
z.write("ipreampfeed: 80\n")
glib.set("vfat2_" + str(port) + "_ipreampout", 150)
z.write("ipreampout: 150\n")
glib.set("vfat2_" + str(port) + "_ishaper", 150)
z.write("ishaper: 150\n")
glib.set("vfat2_" + str(port) + "_ishaperfeed", 100)
z.write("ishaperfeed: 100\n")
glib.set("vfat2_" + str(port) + "_icomp", 75)
z.write("icompn: 75\n")
glib.set("vfat2_" + str(port) + "_vthreshold2", 0)
z.write("vthreshold2: 0\n")
glib.set("vfat2_" + str(port) + "_vthreshold1", 0)
z.write("vthreshold1: 0\n")
glib.set("t1_reset", 1)
glib.set("t1_mode", 1)
glib.set("t1_n", 0)
glib.set("t1_interval", 400)
glib.set("t1_delay", 40)
glib.set("t1_toggle", 1)
z.write("DACs default value: " + str(DACDef)+"\n")
for channel in range(CHAN_MIN, CHAN_MAX):
regName = "vfat2_" + str(port) + "_channel" + str(channel + 1)
#regValue = DACDef
regValue = (1 << 6) + 16 # old version, reenabled to allow the script to work
glib.set(regName, regValue)
pass
################## Threshold Scan by VFAT2 #########################
glib.set('scan_reset', 1)
glib.set('scan_mode', 0)
glib.set('scan_vfat2', port)
glib.set('scan_min', 0)
glib.set('scan_max', 255)
glib.set('scan_step', 1)
glib.set('scan_n', 3000)
glib.set('scan_toggle', 1)
while (glib.get("scan_status") != 0): r = 1
data_threshold = glib.fifoRead('scan_data', 256)
print "length of returned data_threshold = %d"%(len(data_threshold))
for d in range (0,len(data_threshold)):
print ((data_threshold[d] & 0xff000000) >> 24), " = ", (100*(data_threshold[d] & 0xffffff)/3000.0)
#if (d==0)
if (100*(data_threshold[d] & 0xffffff)/ 3000.0) < THRESH_ABS and ((100*(data_threshold[d-1] & 0xffffff) / N_EVENTS) - (100*(data_threshold[d] & 0xffffff) / N_EVENTS)) < THRESH_REL:
f.write("Threshold set to: " + str(d-1)+"\n")
glib.set("vfat2_" + str(port) + "_vthreshold1", int(d-1))
z.write("vthreshold1: "+str(d-1)+"\n")
break
pass
z.close()
#print "---------------",int(d),"---- channel test 0-------------------"
if d == 0 or d == 255:
print "ignored"
for d in range (0,len(data_threshold)):
f.write(str((data_threshold[d] & 0xff000000) >> 24)+"\n")
f.write(str(100*(data_threshold[d] & 0xffffff)/3000.0)+"\n")
pass
f.close()
continue
for d in range (0,len(data_threshold)):
f.write(str((data_threshold[d] & 0xff000000) >> 24)+"\n")
f.write(str(100*(data_threshold[d] & 0xffffff)/3000.0)+"\n")
pass
# for each channel, disable the cal pulse
for channel in range(CHAN_MIN, CHAN_MAX):
regName = "vfat2_" + str(port) + "_channel" + str(channel + 1)
regValue = DACDef
glib.set(regName, regValue)
pass
for channel in range(CHAN_MIN, CHAN_MAX):
## debug
if options.debug:
if channel > 10:
continue
pass
print "------------------- channel ", str(channel), "-------------------"
################## S-curve by channel ######################
#### With TRIM DAC to 0
glib.set("vfat2_" + str(port) + "_vthreshold2", 0)
glib.set("vfat2_" + str(port) + "_latency", 37)
regName = "vfat2_" + str(port) + "_channel" + str(channel + 1)
regValue = (1 << 6) # enable cal pulse to channel
glib.set(regName, regValue)
glib.set('scan_reset', 1)
glib.set('scan_mode', 3)
glib.set('scan_channel', channel)
glib.set('scan_vfat2', port)
glib.set('scan_min', VCAL_MIN)
glib.set('scan_max', VCAL_MAX)
glib.set('scan_step', 1)
glib.set('scan_n', int(N_EVENTS_SCURVE))
glib.set('scan_toggle', 1)
while (glib.get("scan_status") != 0): i = 1
data_scurve = glib.fifoRead('scan_data', VCAL_MAX - VCAL_MIN)
glib.set(regName, 0) # disable cal pulse to channel
print
print "---------------- s-curve data trimDAC 0 --------------------"
for d0 in data_scurve:
Eff = (d0 & 0xffffff) / N_EVENTS_SCURVE
VCal = (d0 & 0xff000000) >> 24
print VCal, " => ",Eff
if (Eff >= 0.48):
print VCal, " => ",Eff
TotVCal0.append(VCal)
break
pass
if options.doQC3:
continue
#### With TRIM DAC to 16
regValue = (1 << 6) + 16
glib.set(regName, regValue) # enable cal pulse to channel
glib.set('scan_reset', 1)
glib.set('scan_mode', 3)
glib.set('scan_channel', channel)
glib.set('scan_vfat2', port)
glib.set('scan_min', VCAL_MIN)
glib.set('scan_max', VCAL_MAX)
glib.set('scan_step', 1)
glib.set('scan_n', int(N_EVENTS_SCURVE))
glib.set('scan_toggle', 1)
while (glib.get("scan_status") != 0): i = 1
data_scurve = glib.fifoRead('scan_data', VCAL_MAX - VCAL_MIN)
glib.set(regName, 0) # disable cal pulse to channel
print
print "---------------- s-curve data trimDAC 16 --------------------"
m.write("SCurve_"+str(channel)+"\n")
for d16 in data_scurve:
Eff = (d16 & 0xffffff) / N_EVENTS_SCURVE
VCal = (d16 & 0xff000000) >> 24
m.write(str(VCal)+"\n")
m.write(str(Eff)+"\n")
pass
#### With TRIM DAC to 31
regValue = (1 << 6) + 31
glib.set(regName, regValue) # enable cal pulse to channel
glib.set('scan_reset', 1)
glib.set('scan_mode', 3)
glib.set('scan_channel', channel)
glib.set('scan_vfat2', port)
glib.set('scan_min', VCAL_MIN)
glib.set('scan_max', VCAL_MAX)
glib.set('scan_step', 1)
glib.set('scan_n', int(N_EVENTS_SCURVE))
glib.set('scan_toggle', 1)
while (glib.get("scan_status") != 0): i = 1
data_scurve = glib.fifoRead('scan_data', VCAL_MAX - VCAL_MIN)
glib.set(regName, 0) # disable cal pulse to channel
print
print "---------------- s-curve data trimDAC 31 --------------------"
try:
for d31 in data_scurve:
Eff = (d31 & 0xffffff) / N_EVENTS_SCURVE
VCal = (d31 & 0xff000000) >> 24
if options.debug:
print VCal, " => ",Eff
pass
if (Eff >= 0.48):
print VCal, " => ",Eff
TotVCal31.append(VCal)
break
pass
pass
except:
print "Error while reading the data, they will be ignored"
continue
pass
################## Adjust the trim for each channel ######################
if options.doQC3:
continue
print
print "------------------------ TrimDAC routine ------------------------"
print
h=open("%s_VCal_VFAT2_%d_ID_0x%04x"%(str(Date),port,chipIDs[port]&0xffff),'w')
#h=open(str(Date)+"_VCal_VFAT2_" + str(port)+ "_ID_" + str(chipIDs[port]&0xff),'w')
try:
VCal_ref0 = sum(TotVCal0)/len(TotVCal0)
h.write(str(TotVCal0)+"\n")
VCal_ref31 = sum(TotVCal31)/len(TotVCal31)
h.write(str(TotVCal31)+"\n")
VCal_ref = (VCal_ref0 + VCal_ref31)/2
print "VCal_ref0", VCal_ref0
print "VCal_ref31", VCal_ref31
except:
print "Scurve did not work"
# should be h.close()?
f.close()
continue
g=open("%s_TRIM_DAC_value_VFAT_%d_ID_0x%04x"%(str(Date),port,chipIDs[port]&0xffff),'w')
#g=open(str(Date)+"_TRIM_DAC_value_VFAT_"+str(port)+ "_ID_" + str(chipIDs[port]&0xff),'w')
for channel in range(CHAN_MIN, CHAN_MAX):
if options.debug:
if channel > 10:
continue
pass
TRIM_IT = 0
print "TrimDAC Channel", channel
regName = "vfat2_" + str(port) + "_channel" + str(channel + 1)
trimDAC = 16
foundGood = False
while (foundGood == False):
regValue = (1 << 6) + trimDAC
glib.set(regName, regValue) # enable cal pulse to channel
glib.set('scan_reset', 1)
glib.set('scan_mode', 3)
glib.set('scan_channel', channel)
glib.set('scan_vfat2', port)
glib.set('scan_min', VCAL_MIN)
glib.set('scan_max', VCAL_MAX)
glib.set('scan_step', 1)
glib.set('scan_n', int(N_EVENTS_SCURVE))
glib.set('scan_toggle', 1)
while (glib.get("scan_status") != 0): i = 1
data_trim = glib.fifoRead('scan_data', VCAL_MAX - VCAL_MIN)
glib.set(regName, 0) # disable cal pulse to channel
try:
for d in data_trim:
Eff = (d & 0xffffff) / N_EVENTS_SCURVE
VCal = (d & 0xff000000) >> 24
if (Eff >= 0.48):
print VCal, " => ",Eff
foundVCal = VCal
break
pass
pass
except:
print "Error while reading the data, they will be ignored"
continue
if (foundVCal > VCal_ref and TRIM_IT < MAX_TRIM_IT and trimDAC < 31):
trimDAC += 1
TRIM_IT +=1
elif (foundVCal < VCal_ref and TRIM_IT < MAX_TRIM_IT and trimDAC > 0):
trimDAC -= 1
TRIM_IT +=1
else:
g.write(str(trimDAC)+"\n")
TotFoundVCal.append(foundVCal)
f.write("S_CURVE_"+str(channel)+"\n")
for d in data_trim:
f.write(str((d & 0xff000000) >> 24)+"\n")
f.write(str((d & 0xffffff)/N_EVENTS_TRIM)+"\n")
pass
break
pass
pass
m.close()
h.write(str(TotFoundVCal)+"\n")
h.close()
g.close()
VCalList = []
minVcal = 0
################# Set all the Trim_DAC to the right value #################
g=open("%s_TRIM_DAC_value_VFAT_%d_ID_0x%04x"%(str(Date),port,chipIDs[port]&0xffff),'r')
#g=open(str(Date)+"_TRIM_DAC_value_VFAT_"+str(port)+"_ID_"+ str(chipIDs[port]&0xff),'r')
for channel in range(CHAN_MIN, CHAN_MAX):
if options.debug:
if channel > 10:
continue
pass
regName = "vfat2_" + str(port) + "_channel" + str(channel + 1)
trimDAC = (g.readline()).rstrip('\n')
print trimDAC
regValue = int(trimDAC)
glib.set(regName, regValue)
#regValue = (1 << 6) + int(trimDAC) # old version
pass
g.close()
########################## Final threshold by VFAT2 ######################
f.write("second_threshold\n")
glib.set('scan_reset', 1)
glib.set('scan_mode', 0)
glib.set('scan_vfat2', port)
glib.set('scan_min', 0)
glib.set('scan_max', 255)
glib.set('scan_step', 1)
glib.set('scan_n', 3000)
glib.set('scan_toggle', 1)
while (glib.get("scan_status") != 0): r = 1
data = glib.fifoRead('scan_data', 255)
for d in data:
f.write(str((d & 0xff000000) >> 24)+"\n")
f.write(str(100*(d & 0xffffff)/3000.0)+"\n")
pass
f.close()
pass
pr.disable()
s = StringIO.StringIO()
sortby = 'cumulative'
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats()
print s.getvalue()