This repository has been archived by the owner on Jan 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtrimChamber.py
executable file
·346 lines (279 loc) · 11.6 KB
/
trimChamber.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
#!/bin/env python
r"""
Trim Chamber
============
FIXME Add detailed description
``trimChamber.py``
=====================
Synopsis
--------
**run_scans.py** **trim** [-**h**] [--**chMax** *CHMAX*] [--**chMin** *CHMIN*] [-**l** *LATENCY*] [-**m** *MSPL*] [-**n** *NEVTS*] [--**trimPoints** *TRIMPOINTS*] [--**vfatmask** *VFATMASK*] [--**armDAC** *ARMDAC*] shelf slot ohMask
Mandatory arguments
-------------------
.. program:: run_Scans.py trim
Positional arguments
--------------------
.. option:: shelf
uTCA crate shelf number
.. option:: slot
AMC slot number in the uTCA crate
.. option:: ohMask
optohybrid mask to apply, a 1 in the n^{th} bit indicates the n^{th} OH should be considered
Optional arguments
------------------
.. option:: -h, --help
show the help message and exit
.. option:: --chMax <CHMAX>
Specify maximum channel number to scan
.. option:: --chMin <CHMIN>
Specify minimum channel number to scan
.. option:: -l, --latency <LATENCY>
Setting of ``CFG_LATENCY`` register
.. option:: -m, --mspl <MSPL>
Setting of ``CFG_PULSE_STRETCH`` register
.. option:: --trimPoints <TRIMPOINTS>
Comma separated list of trim values to use in trimming. a set of Scurves will be taken at each point.
.. option:: --vfatmask <VFATMASK>
If specified, this will use this VFAT mask for all unmasked OptoHybrids in ohMask. Here this is a 24 bit number, where a 1 in the N^{th} bit means ignore the N^{th} VFAT. If this argument is not specified, VFAT masks are determined at runtime automatically.
.. option:: --armDAC <ARMDAC>
``CFG_THR_ARM_DAC`` value to write to all VFATs. If not provided, it looks under ``$DATA_PATH/configs`` for a ``vfatConfig_<DetectorName>.txt`` file where ``DetectorName`` is from the ``chamber_config`` dictionary
Environment
-----------
The following `$SHELL` variables should be defined beforehand:
.. glossary::
:envvar: `BUILD_HOME`
the location of your ``vfatqc-python-scripts`` directory
:envvar: `DATA_PATH`
the location of input data
Then execute:
`source $BUILD_HOME/vfatqc-python-scripts/setup/paths.sh`
"""
"""
Script to set trimdac values on a chamber
By: Christine McLean ([email protected]), Cameron Bravo ([email protected]), Elizabeth Starling ([email protected])
"""
import sys
from array import array
from gempython.tools.vfat_user_functions_uhal import *
from gempython.utils.nesteddict import nesteddict as ndict
from gempython.utils.wrappers import runCommand, envCheck
from gempython.gemplotting.mapping.chamberInfo import chamber_config
if __name__ == '__main__':
from gempython.vfatqc.utils.qcoptions import parser
parser.add_option("--trimRange", type="string", dest="rangeFile", default=None,
help="Specify the file to take trim ranges from", metavar="rangeFile")
parser.add_option("--dirPath", type="string", dest="dirPath", default=None,
help="Specify the path where the scan data should be stored", metavar="dirPath")
parser.add_option("--vt1", type="int", dest="vt1",
help="VThreshold1 DAC value for all VFATs", metavar="vt1", default=100)
parser.add_option("--ztrim", type="float", dest="ztrim", default=4.0,
help="Specify the p value of the trim", metavar="ztrim")
(options, args) = parser.parse_args()
if options.debug:
uhal.setLogLevelTo( uhal.LogLevel.INFO )
else:
uhal.setLogLevelTo( uhal.LogLevel.ERROR )
rangeFile = options.rangeFile
ztrim = options.ztrim
print 'trimming at z = %f'%ztrim
envCheck('DATA_PATH')
envCheck('BUILD_HOME')
dataPath = os.getenv('DATA_PATH')
from gempython.gemplotting.fitting.fitScanData import fitScanData
import subprocess,datetime
startTime = datetime.datetime.now().strftime("%Y.%m.%d.%H.%M")
print startTime
ohboard = getOHObject(options.slot,options.gtx,options.shelf,options.debug)
ohKey = (options.shelf,options.slot,options.gtx)
if options.dirPath == None: dirPath = '%s/%s/trimming/z%f/%s'%(dataPath,chamber_config[ohKey],ztrim,startTime)
else: dirPath = options.dirPath
# bias vfats
biasAllVFATs(ohboard,options.gtx,0x0,enable=False, debug=options.debug)
writeAllVFATs(ohboard, options.gtx, "VThreshold1", options.vt1, 0x0, options.debug)
CHAN_MIN = 0
CHAN_MAX = 128
masks = ndict()
for vfat in range(0,24):
for ch in range(CHAN_MIN,CHAN_MAX):
masks[vfat][ch] = False
#Find trimRange for each VFAT
tRanges = ndict()
tRangeGood = ndict()
trimVcal = ndict()
trimCH = ndict()
goodSup = ndict()
goodInf = ndict()
for vfat in range(0,24):
tRanges[vfat] = 0
tRangeGood[vfat] = False
trimVcal[vfat] = 0
trimCH[vfat] = 0
goodSup[vfat] = -99
goodInf[vfat] = -99
###############
# TRIMDAC = 0
###############
# Configure for initial scan
for vfat in range(0,24):
writeVFAT(ohboard, options.gtx, vfat, "ContReg3", tRanges[vfat], options.debug)
zeroAllVFATChannels(ohboard,options.gtx,mask=0x0,debug=options.debug)
# Scurve scan with trimdac set to 0
filename0 = "%s/SCurveData_trimdac0_range0.root"%dirPath
cmd = [ "ultraScurve.py",
"--shelf=%i"%(options.shelf),
"-s%d"%(options.slot),
"-g%d"%(options.gtx),
"--filename=%s"%(filename0),
"--vfatmask=0x%x"%(options.vfatmask),
"--nevts=%i"%(options.nevts)]
if options.debug:
cmd.append("--debug")
runCommand(cmd)
muFits_0 = fitScanData(filename0)
for vfat in range(0,24):
for ch in range(CHAN_MIN,CHAN_MAX):
if muFits_0[4][vfat][ch] < 0.1: masks[vfat][ch] = True
#calculate the sup and set trimVcal
sup = ndict()
supCH = ndict()
for vfat in range(0,24):
if(tRangeGood[vfat]): continue
sup[vfat] = 999.0
supCH[vfat] = -1
for ch in range(CHAN_MIN,CHAN_MAX):
if(masks[vfat][ch]): continue
if(muFits_0[0][vfat][ch] - ztrim*muFits_0[1][vfat][ch] < sup[vfat] and muFits_0[0][vfat][ch] - ztrim*muFits_0[1][vfat][ch] > 0.1):
sup[vfat] = muFits_0[0][vfat][ch] - ztrim*muFits_0[1][vfat][ch]
supCH[vfat] = ch
goodSup[vfat] = sup[vfat]
trimVcal[vfat] = sup[vfat]
trimCH[vfat] = supCH[vfat]
if rangeFile == None:
#This loop determines the trimRangeDAC for each VFAT
for trimRange in range(0,5):
#Set Trim Ranges
for vfat in range(0,24):
writeVFAT(ohboard, options.gtx, vfat, "ContReg3", tRanges[vfat],options.debug)
###############
# TRIMDAC = 31
###############
#Setting trimdac value
for vfat in range(0,24):
for scCH in range(CHAN_MIN,CHAN_MAX):
writeVFAT(ohboard,options.gtx,vfat,"VFATChannels.ChanReg%d"%(scCH),31,options.debug)
#Scurve scan with trimdac set to 31 (maximum trimming)
filename31 = "%s/SCurveData_trimdac31_range%i.root"%(dirPath,trimRange)
cmd = [ "ultraScurve.py",
"--shelf=%i"%(options.shelf),
"-s%d"%(options.slot),
"-g%d"%(options.gtx),
"--filename=%s"%(filename31),
"--vfatmask=0x%x"%(options.vfatmask),
"--nevts=%i"%(options.nevts)]
if options.debug:
cmd.append("--debug")
runCommand(cmd)
#For each channel, check that the infimum of the scan with trimDAC = 31 is less than the subprimum of the scan with trimDAC = 0. The difference should be greater than the trimdac range.
muFits_31 = fitScanData(filename31)
inf = ndict()
infCH = ndict()
#Check to see if the new trimRange is good
for vfat in range(0,24):
if(tRangeGood[vfat]): continue
sup[vfat] = 999.0
inf[vfat] = 0.0
supCH[vfat] = -1
infCH[vfat] = -1
for ch in range(CHAN_MIN,CHAN_MAX):
if(masks[vfat][ch]): continue
if(muFits_31[0][vfat][ch] - ztrim*muFits_31[1][vfat][ch] > inf[vfat]):
inf[vfat] = muFits_31[0][vfat][ch] - ztrim*muFits_31[1][vfat][ch]
infCH[vfat] = ch
if(muFits_0[0][vfat][ch] - ztrim*muFits_0[1][vfat][ch] < sup[vfat] and muFits_0[0][vfat][ch] - ztrim*muFits_0[1][vfat][ch] > 0.1):
sup[vfat] = muFits_0[0][vfat][ch] - ztrim*muFits_0[1][vfat][ch]
supCH[vfat] = ch
print "vfat: %i"%vfat
print muFits_0[0][vfat]
print muFits_31[0][vfat]
print "sup: %f inf: %f"%(sup[vfat],inf[vfat])
print "supCH: %f infCH: %f"%(supCH[vfat],infCH[vfat])
print " "
if(inf[vfat] <= sup[vfat]):
tRangeGood[vfat] = True
goodSup[vfat] = sup[vfat]
goodInf[vfat] = inf[vfat]
trimVcal[vfat] = sup[vfat]
trimCH[vfat] = supCH[vfat]
else:
tRanges[vfat] += 1
trimVcal[vfat] = sup[vfat]
trimCH[vfat] = supCH[vfat]
print "trimRanges found"
else:
try:
rF = TFile(rangeFile)
for event in rF.scurveTree:
if event.vcal == 10:
if event.vfatCH == 10:
writeVFAT(ohboard, options.gtx, int(event.vfatN), "ContReg3", int(event.trimRange),options.debug)
tRanges[event.vfatN] = event.trimRange
pass
pass
pass
except Exception as e:
print "%s could not be loaded\n"%rangeFile
print e
exit(404)
#Init trimDACs to all zeros
trimDACs = ndict()
for vfat in range(0,24):
for ch in range(CHAN_MIN,CHAN_MAX):
trimDACs[vfat][ch] = 0
# This is a binary search to set each channel's trimDAC
for i in range(0,5):
# First write this steps values to the VFATs
for vfat in range(0,24):
for ch in range(CHAN_MIN,CHAN_MAX):
trimDACs[vfat][ch] += pow(2,4-i)
writeVFAT(ohboard,options.gtx,vfat,"VFATChannels.ChanReg%d"%(ch),trimDACs[vfat][ch],options.debug)
# Run an SCurve
filenameBS = "%s/SCurveData_binarySearch%i.root"%(dirPath,i)
cmd = [ "ultraScurve.py",
"--shelf=%i"%(options.shelf),
"-s%d"%(options.slot),
"-g%d"%(options.gtx),
"--filename=%s"%(filenameBS),
"--vfatmask=0x%x"%(options.vfatmask),
"--nevts=%i"%(options.nevts)]
if options.debug:
cmd.append("--debug")
runCommand(cmd)
# Fit Scurve data
fitData = fitScanData(filenameBS)
# Now use data to determine the new trimDAC value
for vfat in range(0,24):
for ch in range(CHAN_MIN,CHAN_MAX):
if(fitData[0][vfat][ch] - ztrim*fitData[1][vfat][ch] < trimVcal[vfat]): trimDACs[vfat][ch] -= pow(2,4-i)
# Now take a scan with trimDACs found by binary search
for vfat in range(0,24):
for ch in range(CHAN_MIN,CHAN_MAX):
writeVFAT(ohboard,options.gtx,vfat,"VFATChannels.ChanReg%d"%(ch),trimDACs[vfat][ch],options.debug)
filenameFinal = "%s/SCurveData_Trimmed.root"%dirPath
cmd = [ "ultraScurve.py",
"--shelf=%i"%(options.shelf),
"-s%d"%(options.slot),
"-g%d"%(options.gtx),
"--filename=%s"%(filenameFinal),
"--vfatmask=0x%x"%(options.vfatmask),
"--nevts=%i"%(options.nevts)]
if options.debug:
cmd.append("--debug")
runCommand(cmd)
scanFilename = '%s/scanInfo.txt'%dirPath
outF = open(scanFilename,'w')
outF.write('vfat/I:tRange/I:sup/D:inf/D:trimVcal/D:trimCH/D\n')
for vfat in range(0,24):
outF.write('%i %i %f %f %f %i\n'%(vfat,tRanges[vfat],goodSup[vfat],goodInf[vfat],trimVcal[vfat],trimCH[vfat]))
pass
outF.close()
exit(0)