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 10
/
gbt_utils_extended.py
221 lines (172 loc) · 7.1 KB
/
gbt_utils_extended.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
from xhal.reg_interface_gem.core.reg_extra_ops import rpc_connect, scanGBTPhases, writeGBTConfig, writeGBTPhase
from ctypes import *
import os
def configGBT(cardName, listOfconfigFiles, ohMask = 0xfff, nOHs=12):
"""
Configures all GBT's for OH's defined in ohMask on cardName
cardName - network alias of backend AMC
listOfconfigFiles - List of config filenames to be used for all OH's,
index of element should match GBTx number on OH, max length of 3
ohMask - ohMask to apply, a 1 in the n^th bit indicates the n^th OH should be considered
nOHs - Number of OH's on this AMC
"""
rpc_connect(cardName)
if len(listOfconfigFiles) > 3:
# FIXME less generic exception
raise RuntimeError("Too many GBT filenames provided, at most 3 files should be given",os.EX_USAGE)
for ohN in range(nOHs):
# Skip masked OH's
if( not ((ohMask >> ohN) & 0x1)):
continue
# Program all requested GBT's on this OH
for gbtN,configFile in enumerate(listOfconfigFiles):
config = readGBTConfigFile(configFile)
configBlob = (c_uint8 * len(config))(*config)
rpcRsp = writeGBTConfig(ohN, gbtN, len(config), configBlob)
if rpcRsp > 0:
# Not sure if this should print a warning and skip or raise an exception...for now an exception
raise RuntimeError("Failed to configured GBT{0} of OH{1}".format(gbtN,ohN))
pass
pass
return
def gbtPhaseScan(cardName, ohMask = 0xfff, nOHs=12, nOfRepetitions=100, silent=True, outputFile=None):
"""
Scan the VFAT phases for all optohybrids defined in ohMask.
cardName - network alias of backend AMC
ohMask - ohMask to apply, a 1 in the n^th bit indicates the n^th OH should be considered
nOHs - Number of OH's on this AMC
nOfRepetitions - Number of times the scan is performed.
outputFile - If provided creates a text file with this name and writes phase scan results to the file
"""
rpc_connect(cardName)
dict_phaseScanResults = {}
for ohN in range(nOHs):
# Skip masked OH's
if( not ((ohMask >> ohN) & 0x1)):
continue
# Scan phases
phasesBlob = (c_uint32 * (24*16))()
scanGBTPhases(phasesBlob, ohN, nOfRepetitions, 0, 15, 1)
dict_phaseScanResults[ohN] = phasesBlob
# stdout output
if not silent:
print("="*20)
print("Phase Scan Results for OH{0}".format(ohN))
print("="*20)
printGBTPhaseScanResults(phasesBlob)
pass
# File output
if outputFile is not None:
saveGBTPhaseScanResults(outputFile, ohN, phasesBlob, nOfRepetitions)
pass
pass
return dict_phaseScanResults
def printGBTPhaseScanResults(phasesBlob):
from tabulate import tabulate
# Create a table with the scan results
table = []
for phase in range(16):
row = []
row.append(phase)
for vfatN in range(24):
row.append(phasesBlob[vfatN*16+phase])
table.append(row)
# Create header for the table
header = ["Phase"]
for vfatN in range(24):
header.append("VFAT" + str(vfatN))
# Print the table
print tabulate(table, headers=header)
def readGBTConfigFile(filename):
# Read and parse the configuration file
with open(filename, "r") as f:
content = [int(line, 16) for line in f.readlines()]
# GBTX configuration is 366 registers long
if len(content) < 366:
raise ValueError("The configuration file you provided is too short.", filename)
else:
return content[:366]
def saveGBTPhaseScanResults(filename, ohN, phasesBlob, nOfRepetitions=100):
import csv
# Header for the ROOT file
header = "ohN/I:"
header += "vfatN/I:"
header += "phase/I:"
header += "nRepetitions/I:"
header += "nSuccesses/I\n"
# Parse the data
data = []
for vfatN in range(24):
for phase in range(16):
nOfSuccesses = phasesBlob[vfatN*16+phase]
data.append([ohN, vfatN, phase, nOfRepetitions, nOfSuccesses])
# Write the file
with open(filename, 'wb') as f:
f.write(header)
writer = csv.writer(f, delimiter = ' ')
writer.writerows(data)
def setPhase(cardName, ohN, vfatN, phase):
"""
Write the RX phase of one VFAT of one OptoHybrid.
V3 electronics only.
cardName - network alias of backend AMC
ohN - OptoHybrid to configure.
vfatN - VFAT to configure.
phase - Phase value to write. (Min = 0; max = 15).
"""
rpc_connect(cardName)
rpcRsp = writeGBTPhase(ohN, vfatN, phase)
if rpcRsp > 0:
raise RuntimeError("Failed to write phase {0} to VFAT{1} of OH{2}".format(phase,vfatN,ohN))
return
def setPhaseAllVFATs(cardName, ohN, listOfPhases, debug=False):
"""
Writes all vfat phases on the provided ohN.
cardName - network alias of backend AMC
ohN - OptoHybrid to configure.
listOfPhases - List of phases to write, list position should follow vfat position.
If phase is assigned as 0xdeaddead it will not be written
debug - prints additional debugging info
"""
rpc_connect(cardName)
for vfat,phase in enumerate(listOfPhases):
if phase == 0xdeaddead:
print("Bad phase for OH{0} VFAT{1}".format(ohN,vfat))
continue
if debug:
print("Setting Phase {0} to OH{1} VFAT{2}".format(phase,ohN,vfat))
pass
rpcRsp = writeGBTPhase(ohN, vfat, phase)
if rpcRsp > 0:
raise RuntimeError("Failed to write phase {0} to VFAT{1} of OH{2}".format(phase,vfat,ohN))
pass
return
def setPhaseAllOHs(cardName, dictOfPhases, ohMask=0xfff, nOHs=12, debug=False):
"""
For all OH's in ohMask writes the phase for all VFATs
cardName - network alias of backend AMC
dictOfPhases - dictionary where key is OH number, each key stores a list of phases for each vfat,
the index of the list should match the vfat position.
If phase is assigned as 0xdeaddead it will not be written
ohMask - ohMask to apply, a 1 in the n^th bit indicates the n^th OH should be considered
nOHs - Number of OH's on this AMC
debug - prints additional debugging info
"""
rpc_connect(cardName)
for ohN in range(nOHs):
# Skip masked OH's
if( not ((ohMask >> ohN) & 0x1)):
continue
for vfat,phase in enumerate(dictOfPhases[ohN]):
if phase == 0xdeaddead:
print("Bad phase for OH{0} VFAT{1}".format(ohN,vfat))
continue
if debug:
print("Setting Phase {0} to OH{1} VFAT{2}".format(phase,ohN,vfat))
pass
rpcRsp = writeGBTPhase(ohN, vfat, phase)
if rpcRsp > 0:
raise RuntimeError("Failed to write phase {0} to VFAT{1} of OH{2}".format(phase,vfat,ohN))
pass
pass
return