forked from marksan87/hcalraw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_fiberid_references.py
executable file
·316 lines (243 loc) · 9.89 KB
/
make_fiberid_references.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
#!/usr/bin/env python2
import os, sys
from configuration.patterns import rbxes, lineStart
#Generates .txt file in format same with oneRun.py from a list of RBXs
def WordStrip(iStr="", index=""): #iStr: input string, index: the index-th word in the string that you're looking for
nWord = 0
i=0
while i < len(iStr):
while iStr[i] == " ":
i+=1 #get to next word
if i==len(iStr): break
iStart = i
while iStr[i] != " " and i < len(iStr):
i+=1
if i==len(iStr): break
nWord+=1
if i==len(iStr): i = len(iStr)-1 #remove '/n' a the end of line
if nWord == index: return iStr[iStart:i]
return "#%s word not found in string: %s" %(index, iStr)
def WordSpace(iStr="", nSpace=""):
while len(iStr) < nSpace: iStr = " " + iStr
if len(iStr) == nSpace: return iStr
else: print "Error in fixing #char per word"
def DoubleSpace(iStr=""):
oStr = []
for i in range(0, len(iStr)): oStr.append(iStr[i])
return " ".join(oStr)
def getKeyPosition(mapFileName = ""):
keyPosition = {}
keyPosition["RBXname"] = 8
keyPosition["rm"] = 10
keyPosition["rm_fib"] = 14
keyPosition["fi_ch"] = 15
if "HBEF" in mapFileName:
keyPosition["htr_fib"] = 19
keyPosition["spigo"] = 21
keyPosition["fedid"] = 31
keyPosition["crate"] = 16
keyPosition["slot"] = 17
if "HO" in mapFileName:
keyPosition["htr_fib"] = 21
keyPosition["spigo"] = 23
keyPosition["fedid"] = 25
if "CALIB" in mapFileName:
keyPosition["RBXname"] = 7
keyPosition["rm"] = 9
keyPosition["rm_fib"] = 10
keyPosition["fi_ch"] = 11
keyPosition["htr_fib"] = 15
keyPosition["spigo"] = 17
keyPosition["fedid"] = 19
return keyPosition
def ReformMap(iMapfile = "", ofile = "", oFileOpenMode = "w"):
lines = open(iMapfile, "r").readlines() #opens & read the file
output = open(ofile, oFileOpenMode) #opens & write the file
RBXnameRange = rbxes()
rmRange = ['1','2','3','4']
if "CALIB" in iMapfile: rmRange = ['4','5']
keyPosition = getKeyPosition(iMapfile)
for i in range(0, len(lines)): #loop through Map file
if "## file created" in lines[i]: continue #over pass un_needed lines
if "# side eta phi dphi" in lines[i]: continue
current_line = lines[i]
RBXname = WordStrip(current_line, keyPosition["RBXname"])
rm = str(WordStrip(current_line, keyPosition["rm"]))
rm_fib = str(WordStrip(current_line, keyPosition["rm_fib"]))
fi_ch = str(WordStrip(current_line, keyPosition["fi_ch"]))
htr_fib = str(WordStrip(current_line, keyPosition["htr_fib"]))
if 'uHTR' in iMapfile:
fedid = 'u' + str(WordStrip(current_line, keyPosition["crate"]))
spigo = str(WordStrip(current_line, keyPosition["slot"]))
else:
fedid = str(WordStrip(current_line, keyPosition["fedid"]))
spigo = str(WordStrip(current_line, keyPosition["spigo"]))
spigo = WordSpace(spigo,2)
htr_fib = WordSpace(htr_fib,2)
if RBXname in RBXnameRange:
if rm in rmRange and fi_ch == "0": #save only once per 3 channels
output.writelines("%s%3s %02d %02d: %s %1d %1d\n" % (lineStart, fedid, int(spigo), int(htr_fib), RBXname, int(rm), int(rm_fib)))
output.close()
def phase0():
version = 'G'
dir = "/afs/cern.ch/cms/HCAL/document/Mapping/Hua/2015-mar-4/"
for gen, stems in [("vme", ["HCALmapHO", "HCALmapHBEF", "HCALmapCALIB"]),
("utca", ["HCALmapHBEF_uHTR"]),
]:
oFileName = "data/ref_%s_%s.txt" % (gen, version)
for i, stem in enumerate(stems):
if stem.endswith("uHTR"):
fileName = stem.replace("uHTR", "%s_uHTR.txt" % version)
else:
fileName = "%s_%s.txt" % (stem, version)
fileName = "%s/%s" % (dir, fileName)
print "Reading", fileName
if i:
ReformMap(iMapfile=fileName, ofile=oFileName, oFileOpenMode="a")
else:
ReformMap(iMapfile=fileName, ofile=oFileName)
# sort
sName = "%s_sorted" % oFileName
os.system("sort -g %s > %s" % (oFileName, sName))
os.system("mv %s %s" % (sName, oFileName))
print "sorted reference file saved: %s" % oFileName
def ngReformMap(iMapfile="", ofile="", oFileOpenMode="w", rbx_whitelist=None, h_rbx="rbx", h_rmFi="rm_fi"):
valid_rbxes = rbx_whitelist if rbx_whitelist else rbxes()
output = open(ofile, oFileOpenMode) #opens & write the file
lines = open(iMapfile, "r").readlines()
columns = lines[0].split()
# BE
iCrate = columns.index("crate")
iSlot = columns.index("uhtr")
iUf = columns.index("uhtr_fib")
iFc = columns.index("fib_ch")
# FE
iRbx = columns.index(h_rbx)
iRm = columns.index("rm")
iRf = columns.index(h_rmFi)
for iLine, line in enumerate(lines):
if not iLine or line[0] == "#":
continue
fields = line.split()
if fields[1] == "N/C":
print "hacking line %d" % (1 + iLine)
fields = fields[:3] + ["8"] + fields[3:]
if len(fields) != len(columns):
print "problem on line %d: %d fields vs. %d columns" % (1 + iLine, len(fields), len(columns))
print columns
print fields
for i in range(min(len(fields), len(columns))):
print i, columns[i], fields[i]
sys.exit()
# BE
crate = int(fields[iCrate])
slot = int(fields[iSlot])
uhtr_fib = int(fields[iUf])
# FE
rbx = fields[iRbx]
rm = int(fields[iRm])
rm_fib = int(fields[iRf])
if rbx in valid_rbxes:
if fields[iFc] == "0":
output.writelines("%su%2d %02d %02d: %s %1d %1d\n" % (lineStart, crate, slot, uhtr_fib, rbx, rm, rm_fib))
output.close()
def ngHFMap(iMapfile="", ofile="", oFileOpenMode="w"):
valid_rbxes = rbxes()
output = open(ofile, oFileOpenMode) #opens & write the file
lines = open(iMapfile, "r").readlines()
columns = lines[0].split()
# BE
iCrate = columns.index("Crate")
iSlot = columns.index("uHTR")
iUf = columns.index("uHTR_FI")
iFc = columns.index("FI_CH")
# FE
iRbx = columns.index("ngRBX")
iRm = columns.index("QIE10")
iTb = columns.index("QIETB")
iQf = columns.index("QIEFI")
for iLine, line in enumerate(lines):
if not iLine or line[0] == "#":
continue
fields = line.split()
if len(fields) != 1 + len(columns):
print "problem on line %d: %d fields vs. %d columns" % (1 + iLine, len(fields), len(columns))
print columns
print fields
for i in range(min(len(fields), len(columns))):
print i, columns[i], fields[i]
sys.exit()
# BE
crate = int(fields[iCrate])
slot = int(fields[iSlot])
uhtr_fib = int(fields[iUf])
# FE
rbx = fields[iRbx]
rm = int(fields[iRm])
rm_fib = int(fields[iQf]) - 4
if rbx in valid_rbxes:
if fields[iFc] == "0":
output.writelines("%su%2d %02d %02d: %s %1d %1d\n" % (lineStart, crate, slot, uhtr_fib, rbx, rm, rm_fib))
output.close()
def plan1():
# fileName = "/afs/cern.ch/cms/HCAL/document/Mapping/HBHE/ngHBHE/ngHE/ngHEP17/HBHEP17_template.txt"
fileName = "data/HBHEP17_template.txt"
oFileName = "data/ref_plan1.txt"
ngReformMap(iMapfile=fileName, ofile=oFileName, oFileOpenMode="w", rbx_whitelist=["HBP17", "HEP17"], h_rbx="RBX", h_rmFi="rm_fib")
sName = "%s_sorted" % oFileName
os.system("sort -g %s > %s" % (oFileName, sName))
os.system("mv %s %s" % (sName, oFileName))
print "sorted reference file saved: %s" % oFileName
def ngHF():
fileName = "ngHF2017LMap_20170125_pre04.tsv"
oFileName = "data/ref_ngHF.txt"
ngHFMap(iMapfile=fileName, ofile=oFileName, oFileOpenMode="w")
sName = "%s_sorted" % oFileName
os.system("sort -g %s > %s" % (oFileName, sName))
os.system("mv %s %s" % (sName, oFileName))
print "sorted reference file saved: %s" % oFileName
def calib():
fileName = "HCALmapCALIB_H_v2.tsv"
oFileName = "data/ref_calib.txt"
ngReformMap(iMapfile=fileName, ofile=oFileName, oFileOpenMode="w")
sName = "%s_sorted" % oFileName
os.system("sort -g %s > %s" % (oFileName, sName))
os.system("mv %s %s" % (sName, oFileName))
print "sorted reference file saved: %s" % oFileName
def B904():
crate = 61
slot = 1
uhtr_fib = -1
for iRbx in range(1, 19):
rbx = "HE%d" % iRbx
for rm in range(1, 5):
for rm_fib in range(1, 9):
uhtr_fib += 1
if 23 < uhtr_fib:
uhtr_fib -= 24
slot += 1
if 12 < slot:
slot -= 12
crate += 1
print("%su%2d %02d %02d: %s %1d %1d" % (lineStart, crate, slot, uhtr_fib, rbx, rm, rm_fib))
def ngHE():
for filename in ["HB2018LMap_20171128_ppcol-fix_K.txt", "ngHE2018LMap_20171130_K.txt"]:
f = open(filename)
for line in f:
if line.startswith("#"):
continue
fields =line.split()
if len(fields) != 26:
print fields
continue
# HE column headers
Side, Eta, Phi, dPhi, Depth, Det, RBX, Wedge, BV, QIE11, QIECH, RM, RM_FI, FI_CH, ppCol, ppRow, ppCpl, ppLC, dodec, Crate, uHTR, uHTR_FI, FEDid, QIE11id, TP_FI, TP_CH = fields
print("%su%2d %02d %02d: %s %1d %1d" % (lineStart, int(Crate), int(uHTR), int(uHTR_FI), RBX, int(RM), int(RM_FI)))
f.close()
if __name__ == "__main__":
# phase0()
# plan1()
# ngHF()
# calib()
# B904()
ngHE()