ATLAS Offline Software
Loading...
Searching...
No Matches
ReadBchFromCrest.py
Go to the documentation of this file.
1#!/bin/env python
2
3# Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
4#
5# File: ReadBchFromCrest.py
6# Sanya Solodkov <Sanya.Solodkov@cern.ch>, 2025-10-20
7#
8# Purpose: Read channel status from CREST DB or from JSON file
9# ReadBchFromCrest.py --schema='CREST' --tag='UPD4'
10#
11
12import getopt,sys,os
13os.environ['TERM'] = 'linux'
14
15def usage():
16 print ("Usage: ",sys.argv[0]," [OPTION] ... ")
17 print ("Dumps the TileCal status bits from various schemas / folders")
18 print ("")
19 print ("-h, --help shows this help")
20 print ("-f, --folder= specify status folder to use ONL01 or OFL02, don't need to specify full path")
21 print ("-t, --tag= specify tag to use, f.i. UPD1 or UPD4 or full suffix like RUN2-HLT-UPD1-00")
22 print ("-H, --hash= specify hash to use, run/lumi will be ignored if hash is provided")
23 print ("-r, --run= specify run number, by default uses latest iov")
24 print ("-l, --lumi= specify lumi block number, default is 0")
25 print ("-b, --begin= specify run number of first iov in multi-iov mode, by default uses very first iov")
26 print ("-e, --end= specify run number of last iov in multi-iov mode, by default uses latest iov")
27 print ("-m, --module= specify module to use, default is not set")
28 print ("-N, --chmin= specify minimal channel to use, default is 0")
29 print ("-X, --chmax= specify maximal channel to use, default is 47")
30 print ("-c, --chan= specify channel to use , default is all channels from chmin to chmax")
31 print ("-g, --gain=, -a, --adc= specify adc(gain) to use, default is 2 (i.e. both low and high gains)")
32 print ("-C, --comment print comment for every IOV")
33 print ("-i, --iov print IOVs only for every module")
34 print ("-I, --IOV print IOVs only")
35 print ("-A, --all print all records for every IOV, including those which were overwritten")
36 print ("-d, --default print also default values stored in AUX01-AUX20")
37 print ("-B, --blob print additional blob info")
38 print ("-x, --hex print frag id instead of module name")
39 print ("-P, --pmt print pmt number in addition to channel number")
40 print ("-s, --schema= specify name of input JSON file or CREST_SERVER_PATH")
41 print ("-w, --warning suppress warning messages about missing drawers in DB")
42
43letters = "hH:r:l:s:t:f:dBxPwm:b:e:a:g:c:N:X:CiIA"
44keywords = ["help","hash=","run=","lumi=","schema=","tag=","folder=","default","blob","hex","pmt","warning","module=","begin=","end=","chmin=","chmax=","gain=","adc=","chan=","comment","iov","IOV","all"]
45
46try:
47 opts, extraparams = getopt.getopt(sys.argv[1:],letters,keywords)
48except getopt.GetoptError as err:
49 print (str(err))
50 usage()
51 sys.exit(2)
52
53# defaults
54run = 2147483647
55lumi = 0
56schema = 'CREST'
57folderPath = "/TILE/OFL02/STATUS/ADC"
58tag = "UPD4"
59hash = None
60rosmin = 1
61rosmax = 5
62blob = False
63hexid = False
64pmt = False
65warn = 1
66modmin = 0
67modmax = 99999
68modulename="AUX-1"
69partname=""
70one_mod = False
71mod = -1
72ros = -1
73chan_n= -1
74chanmin = -1
75chanmax = -1
76gain_n = -1
77gainmin = -1
78gainmax = -1
79begin = -1
80end = 2147483647
81all = False
82iov = False
83iovonly = False
84IOVONLY = False
85comment = False
86
87for o, a in opts:
88 a = a.strip()
89 if o in ("-f","--folder"):
90 if '/TILE' in a:
91 folderPath = a
92 else:
93 folderPath = "/TILE/%s/STATUS/ADC" % a
94 elif o in ("-t","--tag"):
95 tag = a
96 elif o in ("-s","--schema"):
97 schema = a
98 elif o in ("-H","--hash"):
99 hash = a
100 elif o in ("-r","--run"):
101 run = int(a)
102 elif o in ("-l","--lumi"):
103 lumi = int(a)
104 elif o in ("-b","--begin"):
105 begin = int(a)
106 iov = True
107 one_mod = True
108 elif o in ("-e","--end"):
109 end = int(a)
110 iov = True
111 one_mod = True
112 elif o in ("-A","--all"):
113 all = True
114 elif o in ("-i","--iov"):
115 iov = True
116 iovonly = True
117 if modulename=='AUX-1':
118 modulename='ALL00'
119 elif o in ("-I","--IOV"):
120 iov = True
121 IOVONLY = True
122 if modulename=='AUX-1':
123 modulename='ALL00'
124 elif o in ("-a","--adc","-g","--gain"):
125 gain_n = int(a)
126 elif o in ("-m","--module"):
127 modulename = a
128 one_mod = True
129 elif o in ("-c","--chan"):
130 chan_n = int(a)
131 elif o in ("-N","--chmin"):
132 chanmin = int(a)
133 elif o in ("-X","--chmax"):
134 chanmax = int(a)
135 elif o in ("-C","--comment"):
136 comment = True
137 elif o in ("-d","--default"):
138 rosmin = 0
139 elif o in ("-B","--blob"):
140 blob = True
141 elif o in ("-x","--hex"):
142 hexid = True
143 elif o in ("-P","--pmt"):
144 pmt = True
145 elif o in ("-w","--warning"):
146 warn = -1
147 elif o in ("-h","--help"):
148 usage()
149 sys.exit(2)
150 else:
151 raise RuntimeError("unhandled option")
152
153if hash is not None:
154 iov = False
155 run = None
156 lumi = hash
157
158
159from TileCalibBlobPython import TileBchCrest
160from TileCalibBlobObjs.Classes import TileCalibUtils
161
162from TileCalibBlobPython.TileCalibLogger import getLogger
163log = getLogger("ReadBchFrCrest")
164import logging
165logLevel=logging.DEBUG
166log.setLevel(logLevel)
167log1 = getLogger("TileBchCrest")
168log1.setLevel(logLevel)
169log2 = getLogger("TileCalibCrest")
170log2.setLevel(logLevel)
171
172if tag.upper().endswith('HEAD'):
173 tag=tag.upper()
174if len(tag)==0 or tag.endswith('HEAD'):
175 folderPath='/TILE/ONL01/STATUS/ADC'
176 log.info("tag is %s, using %s folder", tag if tag else 'empty', folderPath)
177 if tag=='HEAD':
178 tag=''
179
180folderTag = tag
181tag = tag.upper()
182tag1 = tag.split('_')[1][:4] if '_' in tag else tag[:4]
183if tag1 == "TILE" or tag1 == "CALO" or tag.startswith("TILE") or tag.startswith("CALO"):
184 folderPath=""
185if not os.path.isfile(schema):
186 log.info("Initializing folder %s with tag %s", folderPath, folderTag)
187
188#=== create bad channel manager
189mgr = TileBchCrest.TileBchMgr()
190mgr.setLogLvl(logLevel)
191mgr.initialize(schema, folderPath, folderTag, (run,lumi), warn, -2)
192blobReader = mgr.getBlobReader()
193
194#=== Dump the current isBad definition
196if len(list(isBadDef.keys())):
197 log.info( "isBad Definition: " )
198 for prbCode in sorted(isBadDef.keys()):
199 prbDesc = isBadDef[prbCode]
200 msg = "- %2i (%s)" % (prbCode,prbDesc)
201 log.info( msg )
202#=== Dump the current isBadTiming definition
204if len(list(isBadTimingDef.keys())):
205 log.info( "isBadTiming Definition: " )
206 for prbCode in sorted(isBadTimingDef.keys()):
207 prbDesc = isBadTimingDef[prbCode]
208 msg = "- %2i (%s)" % (prbCode,prbDesc)
209 log.info( msg )
210
211
212#=== check ROS and module numbers
213if one_mod:
214 partname = modulename[:3]
215 mod = int(modulename[3:]) -1
216
217part_dict = {'AUX':0,'LBA':1,'LBC':2,'EBA':3,'EBC':4}
218if partname in part_dict:
219 ros = part_dict[partname]
220 rosmin = ros
221 rosmax = ros+1
222else:
223 ros = -1
224
225if mod >= 0:
226 modmin = mod
227 modmax = mod+1
228elif mod < -1:
229 modmax = modmin
230
231if chan_n >= 0 and chan_n < TileCalibUtils.max_chan():
232 chanmin = chan_n
233 chanmax = chan_n+1
234else:
235 if chanmin<0:
236 chanmin = 0
237 if chanmax<0:
238 chanmax = TileCalibUtils.max_chan()
239 else:
240 chanmax += 1
241
242if gain_n >= 0 and gain_n < TileCalibUtils.max_gain():
243 gainmin = gain_n
244 gainmax = gain_n+1
245else:
246 gainmin = 0
247 gainmax = TileCalibUtils.max_gain()
248
249
250#=== Filling the iovList
251iovList = []
252if iov:
253 if begin<0:
254 if iovonly or IOVONLY:
255 begin = end
256 else:
257 begin=0
258 iovList = blobReader.getIovs((begin,0),(end,0))
259
260 be=iovList[0][0]
261 en=iovList[-1][0]
262
263 if begin != be or end != en:
264 log.info( "" )
265 if be != begin:
266 log.info( "Changing begin run from %d to %d (start of IOV)", begin,be)
267 begin=be
268 if en != end:
269 if en>end:
270 log.info( "Changing end run from %d to %d (start of next IOV)", end,en)
271 else:
272 log.info( "Changing end run from %d to %d (start of last IOV)", end,en)
273 end=en
274 log.info( "%d IOVs in total", len(iovList))
275
276 #=== IOV only option
277 if iovonly or IOVONLY:
278 if comment:
279 for iovs in iovList:
280 blobReader.readIov(iovs)
281 if all:
282 phash = blobReader.getPayloadHash()
283 itime = blobReader.getInsertionTime()
284 for i in range(len(phash)):
285 blobReader.getPayloadByHash(i)
286 log.info("(%i,%i) Insertion time %s Hash %s", iovs[0], iovs[1], itime[i], phash[i] )
287 log.info("(%i,%i) %s", iovs[0], iovs[1], blobReader.getComment(iovs) )
288 else:
289 log.info("(%i,%i) %s", iovs[0], iovs[1], blobReader.getComment(iovs) )
290 option = 1 if iovonly else 0
291 option += (2 if IOVONLY else 0)
292 blobReader.dumpIovs(iovList,rosmin,rosmax,modmin,modmax,option,(rosmin<=0),True)
293 sys.exit(0)
294else:
295 iovList.append((run,lumi))
296
297log.info( "\n" )
298
299
300
303dummy = [0]*48
304barrel = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
305 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
306 27, 26, 25, 30, 29, 28,-33,-32, 31, 36, 35, 34,
307 39, 38, 37, 42, 41, 40, 45,-44, 43, 48, 47, 46]
308extbar = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
309 13, 14, 15, 16, 17, 18,-19,-20, 21, 22, 23, 24,
310 -27,-26,-25,-31,-32,-28, 33, 29, 30,-36,-35, 34,
311 44, 38, 37, 43, 42, 41,-45,-39,-40,-48,-47,-46]
312
313ch2pmt = [ dummy, barrel, barrel, extbar, extbar ]
314gname = [ "LG", "HG" ]
315
316#=== Get ADC problems
317
318#=== get the channel status (if it is bad and will be masked)
319#=== the channel status depends on the definition of isBad stored
320#=== in the database drawer 1, channel 0
321#=== isAffected = has a problem not included in isBad definition
322#=== isGood = has no problem at all
323
324oneModule = ((rosmax-rosmin==1) and (modmax-modmin==1) and iov)
325oldBlob = None
326pref = ""
327for iovs in iovList:
328 blobReader.readIov(iovs)
329 phash = blobReader.getPayloadHash()
330 itime = blobReader.getInsertionTime()
331 hashmax = len(phash) if all else 1
332 for hashind in range(hashmax):
333 if iov:
334 pref = "(%i,%i) " % (iovs[0],iovs[1])
335 mgr.updateFromDb(schema, folderPath, folderTag, iovs, 1, warn)
336 if all:
337 blobReader.getPayloadByHash(hashind)
338 log.info("(%s,%s) Insertion time %s Hash %s", iovs[0], iovs[1], itime[hashind], phash[hashind] )
339 if comment:
340 log.info( pref + str(blobReader.getComment(iovs)) )
341 modOk = False
342 miss = 0
343 good = 0
344 aff = 0
345 bad = 0
346 nMod = 0
347 for ros in range(rosmin,rosmax):
348 for mod in range(modmin, min(modmax,TileCalibUtils.getMaxDrawer(ros))):
349 nMod += 1
350 if hexid:
351 modName = "0x%x" % ((ros<<8)+mod)
352 else:
353 modName = TileCalibUtils.getDrawerString(ros,mod)
354 if oneModule:
355 newBlob = blobReader.getBlob(ros, mod, iovs, False)
356 if oldBlob == newBlob:
357 if hashind==0:
358 log.info( f'{modName} in IOV {iovs} is identical to previous IOV')
359 else:
360 log.info( f'{modName} in payload {hashind+1} of IOV {iovs} is identical to previous payload')
361 continue
362 else:
363 if oldBlob is None:
364 log.info( f'Reading IOV {iovs} for module {modName}')
365 else:
366 if hashind==0:
367 log.info( f'{modName} was updated in IOV {iovs}')
368 else:
369 log.info( f'{modName} was updated in payload {hashind+1} of IOV {iovs}')
370 oldBlob = newBlob
371 if warn<0:
372 bch = blobReader.getDrawer(ros, mod, iovs, False, False)
373 if bch is None:
374 modOk = False
375 miss+=1
376 #print ("%s is missing in DB" % modName)
377 else:
378 modOk = True
379 if blob and bch:
380 print ("%s Blob type: %d Version: %d Nchannels: %d Ngains: %d Nval: %d" % (modName, bch.getObjType(), bch.getObjVersion(), bch.getNChans(), bch.getNGains(), bch.getObjSizeUint32()))
381 nBad=0
382 for chn in range(chanmin,chanmax):
383 chnName = " %2i" % chn
384 for adc in range(gainmin,gainmax):
385
386 stat = mgr.getAdcStatus(ros,mod,chn,adc)
387 #log.info( "- ADC status = isBad: %d" % stat.isBad() )
388 #log.info( "- ADC status = isGood: %d" % stat.isGood() )
389 #log.info( "- ADC status = isAffected: %d" % stat.isAffected() )
390
391 #=== get all problems of the channel
392 prbs = mgr.getAdcProblems(ros,mod,chn,adc)
393 #log.info( "ADC Problems: " )
394 if len(prbs) or iov:
395 modOk = False
396 if pmt:
397 msg = "%s pm %02i ch %02i %s " % ( modName, abs(ch2pmt[ros][chn]), chn, gname[adc] )
398 else:
399 msg = "%s %2i %1i " % ( modName,chn,adc )
400 for prbCode in sorted(prbs.keys()):
401 prbDesc = prbs[prbCode]
402 msg += " %5i (%s)" % (prbCode,prbDesc)
403 if stat.isBad():
404 msg += " => BAD"
405 nBad+=1
406 elif stat.isAffected():
407 msg += " => Affected"
408 elif stat.isGood():
409 msg += " => good"
410 print (pref+msg)
411 if modOk:
412 good+=1
413 print ("%s ALL GOOD" % (modName))
414 elif nBad==0:
415 aff+=1
416 elif nBad==TileCalibUtils.max_gain()*TileCalibUtils.max_chan() or (nBad==90 and 'LB' in modName) or (nBad==64 and 'EB'in modName) or (nBad==60 and 'EBA15' in modName) or (nBad==60 and 'EBC18' in modName):
417 bad+=1
418 if warn<0:
419 if miss:
420 print ("%3i drawers are missing in DB" % miss)
421 print ("%3i drawers are absolutely good" % good)
422 print ("%3i drawers have good and affected channels" % (aff-miss))
423 print ("%3i drawers have some bad channels" % (nMod-good-bad-aff))
424 print ("%3i drawers are completely bad" % bad)
425 #=== print all bad channels
426 #log.info("listing bad channels")
427 #mgr.listBadAdcs()
#define min(a, b)
Definition cfImp.cxx:40
static unsigned int max_gain()
Python compatibility function.
static unsigned int badtiming_definition_chan()
Python compatibility function.
static std::string getDrawerString(unsigned int ros, unsigned int drawer)
Return the drawer name, e.g.
static unsigned int max_chan()
Python compatibility function.
static unsigned int getMaxDrawer(unsigned int ros)
Returns the maximal channel number for a given drawer.
static unsigned int definitions_draweridx()
Python compatibility function.
static unsigned int bad_definition_chan()
Python compatibility function.