ATLAS Offline Software
ReadBchFromCool.py
Go to the documentation of this file.
1 #!/bin/env python
2 
3 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
4 #
5 # ReadBchFromCool.py --schema='COOLOFL_TILE/CONDBR2' --folder='OFL02' --tag='UPD4'
6 # Sanya Solodkov 2011-07-15
7 # change: Yuri Smirnov 2015-08-29, correction for EB* modules' nbad counter
8 
9 import getopt,sys,os
10 os.environ['TERM'] = 'linux'
11 
12 def usage():
13  print ("Usage: ",sys.argv[0]," [OPTION] ... ")
14  print ("Dumps the TileCal status bits from various schemas / folders")
15  print ("")
16  print ("-h, --help shows this help")
17  print ("-f, --folder= specify status folder to use ONL01 or OFL02, don't need to specify full path")
18  print ("-t, --tag= specify tag to use, f.i. UPD1 or UPD4 or full suffix like RUN2-HLT-UPD1-00")
19  print ("-r, --run= specify run number, by default uses latest iov")
20  print ("-l, --lumi= specify lumi block number, default is 0")
21  print ("-b, --begin= specify run number of first iov in multi-iov mode, by default uses very first iov")
22  print ("-e, --end= specify run number of last iov in multi-iov mode, by default uses latest iov")
23  print ("-m, --module= specify module to use, default is not set")
24  print ("-N, --chmin= specify minimal channel to use, default is 0")
25  print ("-X, --chmax= specify maximal channel to use, default is 47")
26  print ("-c, --chan= specify channel to use , default is all channels from chmin to chmax")
27  print ("-g, --gain=, -a, --adc= specify adc(gain) to use, default is 2 (i.e. both low and high gains)")
28  print ("-C, --comment print comment for every IOV")
29  print ("-i, --iov print IOVs only for every module")
30  print ("-I, --IOV print IOVs only")
31  print ("-d, --default print also default values stored in AUX01-AUX20")
32  print ("-B, --blob print additional blob info")
33  print ("-H, --hex print frag id instead of module name")
34  print ("-P, --pmt print pmt number in addition to channel number")
35  print ("-s, --schema= specify schema to use, like 'COOLOFL_TILE/CONDBR2' or 'sqlite://;schema=tileSqlite.db;dbname=CONDBR2' or tileSqlite.db")
36  print ("-D, --dbname= specify dbname part of schema if schema only contains file name, default is CONDBR2")
37  print ("-S, --server= specify server - ORACLE or FRONTIER, default is FRONTIER")
38  print ("-w, --warning suppress warning messages about missing drawers in DB")
39 
40 letters = "hr:l:s:t:f:D:S:dBHPwm:b:e:a:g:c:N:X:CiI"
41 keywords = ["help","run=","lumi=","schema=","tag=","folder=","dbname=","server=","default","blob","hex","pmt","warning","module=","begin=","end=","chmin=","chmax=","gain=","adc=","chan=","comment","iov","IOV"]
42 
43 try:
44  opts, extraparams = getopt.getopt(sys.argv[1:],letters,keywords)
45 except getopt.GetoptError as err:
46  print (str(err))
47  usage()
48  sys.exit(2)
49 
50 # defaults
51 run = 2147483647
52 lumi = 0
53 schema = 'COOLOFL_TILE/CONDBR2'
54 dbname = ''
55 server = ''
56 folderPath = "/TILE/OFL02/STATUS/ADC"
57 tag = "UPD4"
58 rosmin = 1
59 rosmax = 5
60 blob = False
61 hexid = False
62 pmt = False
63 warn = 1
64 modmin = 0
65 modmax = 99999
66 modulename="AUX-1"
67 partname=""
68 one_mod = False
69 mod = -1
70 ros = -1
71 chan_n= -1
72 chanmin = -1
73 chanmax = -1
74 gain_n = -1
75 gainmin = -1
76 gainmax = -1
77 begin = -1
78 end = 2147483647
79 iov = False
80 iovonly = False
81 IOVONLY = False
82 comment = False
83 
84 for o, a in opts:
85  a = a.strip()
86  if o in ("-f","--folder"):
87  if '/TILE' in a:
88  folderPath = a
89  else:
90  folderPath = "/TILE/%s/STATUS/ADC" % a
91  elif o in ("-t","--tag"):
92  tag = a
93  elif o in ("-s","--schema"):
94  schema = a
95  elif o in ("-D","--dbname"):
96  dbname = a
97  elif o in ("-S","--server"):
98  server = a
99  elif o in ("-r","--run"):
100  run = int(a)
101  elif o in ("-l","--lumi"):
102  lumi = int(a)
103  elif o in ("-b","--begin"):
104  begin = int(a)
105  iov = True
106  one_mod = True
107  elif o in ("-e","--end"):
108  end = int(a)
109  iov = True
110  one_mod = True
111  elif o in ("-i","--iov"):
112  iov = True
113  iovonly = True
114  if modulename=='AUX-1':
115  modulename='ALL00'
116  elif o in ("-I","--IOV"):
117  iov = True
118  IOVONLY = True
119  if modulename=='AUX-1':
120  modulename='ALL00'
121  elif o in ("-a","--adc","-g","--gain"):
122  gain_n = int(a)
123  elif o in ("-m","--module"):
124  modulename = a
125  one_mod = True
126  elif o in ("-c","--chan"):
127  chan_n = int(a)
128  elif o in ("-N","--chmin"):
129  chanmin = int(a)
130  elif o in ("-X","--chmax"):
131  chanmax = int(a)
132  elif o in ("-C","--comment"):
133  comment = True
134  elif o in ("-d","--default"):
135  rosmin = 0
136  elif o in ("-B","--blob"):
137  blob = True
138  elif o in ("-H","--hex"):
139  hexid = True
140  elif o in ("-P","--pmt"):
141  pmt = True
142  elif o in ("-w","--warning"):
143  warn = -1
144  elif o in ("-h","--help"):
145  usage()
146  sys.exit(2)
147  else:
148  raise RuntimeError("unhandled option")
149 
150 
151 from TileCalibBlobPython import TileCalibTools
152 from TileCalibBlobPython import TileBchTools
153 from TileCalibBlobPython.TileCalibTools import MAXRUN, MAXLBK
154 from TileCalibBlobObjs.Classes import TileCalibUtils
155 
156 from TileCalibBlobPython.TileCalibLogger import getLogger
157 log = getLogger("ReadBch")
158 import logging
159 logLevel=logging.DEBUG
160 log.setLevel(logLevel)
161 log1 = getLogger("TileCalibTools")
162 log1.setLevel(logLevel)
163 
164 
165 #=== check parameters
166 if len(dbname)<7 and run!=2147483647:
167  dbname = 'COMP200' if run<232000 else 'CONDBR2'
168 
169 if 'COOLO' not in schema and ':' not in schema and ';' not in schema:
170  schema='sqlite://;schema='+schema+';dbname='+(dbname if len(dbname) else 'CONDBR2')
171 
172 if 'sqlite:' not in schema and len(dbname):
173  schema=schema.replace('CONDBR2',dbname)
174  schema=schema.replace('COMP200',dbname)
175 
176 if len(tag)==0:
177  folderPath='/TILE/ONL01/STATUS/ADC'
178  if 'COOLOFL' in schema:
179  if 'COMP200' in schema:
180  schema='COOLONL_TILE/COMP200'
181  if 'CONDBR2' in schema:
182  schema='COOLONL_TILE/CONDBR2'
183  log.warning("tag is empty, using %s folder and schema %s", folderPath,schema)
184 
185 if schema=='COOLONL_TILE/COMP200':
186  if folderPath!="/TILE/ONL01/STATUS/ADC" and folderPath!="/TILE/OFL01/STATUS/ADC":
187  log.warning("Folder %s doesn't exist in schema %s", folderPath,schema)
188  folderPath="/TILE/ONL01/STATUS/ADC"
189  log.warning("Changing folder to %s", folderPath)
190 
191 if schema=='COOLONL_TILE/CONDBR2':
192  if folderPath!="/TILE/ONL01/STATUS/ADC":
193  log.warning("Folder %s doesn't exist in schema %s ", folderPath,schema)
194  folderPath="/TILE/ONL01/STATUS/ADC"
195  log.warning("Changing folder to %s", folderPath)
196 
197 if schema=='COOLOFL_TILE/COMP200' or schema=='COOLOFL_TILE/CONDBR2':
198  if folderPath!="/TILE/OFL02/STATUS/ADC":
199  log.warning("Folder %s doesn't exist in schema %s ", folderPath,schema)
200  folderPath="/TILE/OFL02/STATUS/ADC"
201  log.warning("Changing folder to %s", folderPath)
202 
203 
204 #=== set database
205 db = TileCalibTools.openDbConn(schema,server)
206 folderTag = TileCalibTools.getFolderTag(db if 'CONDBR2' in schema else schema, folderPath, tag)
207 log.info("Initializing folder %s with tag %s", folderPath, folderTag)
208 
209 #=== create bad channel manager
210 mgr = TileBchTools.TileBchMgr()
211 mgr.setLogLvl(logLevel)
212 mgr.initialize(db, folderPath, folderTag, (run,lumi), warn, -2)
213 if iov or comment or warn<0:
214  blobReader = TileCalibTools.TileBlobReader(db,folderPath,folderTag)
215 
216 #=== Dump the current isBad definition
218 if len(list(isBadDef.keys())):
219  log.info( "isBad Definition: " )
220  for prbCode in sorted(isBadDef.keys()):
221  prbDesc = isBadDef[prbCode]
222  msg = "- %2i (%s)" % (prbCode,prbDesc)
223  log.info( msg )
224 #=== Dump the current isBadTiming definition
226 if len(list(isBadTimingDef.keys())):
227  log.info( "isBadTiming Definition: " )
228  for prbCode in sorted(isBadTimingDef.keys()):
229  prbDesc = isBadTimingDef[prbCode]
230  msg = "- %2i (%s)" % (prbCode,prbDesc)
231  log.info( msg )
232 
233 
234 #=== check ROS and module numbers
235 if one_mod:
236  partname = modulename[:3]
237  mod = int(modulename[3:]) -1
238 
239 part_dict = {'AUX':0,'LBA':1,'LBC':2,'EBA':3,'EBC':4}
240 if partname in part_dict:
241  ros = part_dict[partname]
242  rosmin = ros
243  rosmax = ros+1
244 else:
245  ros = -1
246 
247 if mod >= 0:
248  modmin = mod
249  modmax = mod+1
250 elif mod < -1:
251  modmax = modmin
252 
253 if chan_n >= 0 and chan_n < TileCalibUtils.max_chan():
254  chanmin = chan_n
255  chanmax = chan_n+1
256 else:
257  if chanmin<0:
258  chanmin = 0
259  if chanmax<0:
260  chanmax = TileCalibUtils.max_chan()
261  else:
262  chanmax += 1
263 
264 if gain_n >= 0 and gain_n < TileCalibUtils.max_gain():
265  gainmin = gain_n
266  gainmax = gain_n+1
267 else:
268  gainmin = 0
269  gainmax = TileCalibUtils.max_gain()
270 
271 
272 #=== IOV only option
273 if iovonly or IOVONLY:
274  if iovonly:
275  print("")
276  if begin<0:
277  begin = end
278  lastRun = -1
279  allsince = {}
280  roslist = list(range(rosmin,rosmax))
281  if comment:
282  roslist += [-1]
283  for ros in roslist:
284  if ros==-1:
285  modlist = [1000]
286  modName = "Comment"
287  else:
288  modlist=range(modmin, min(modmax,TileCalibUtils.getMaxDrawer(ros)))
289  for mod in modlist:
290  if mod<1000:
291  if hexid:
292  modName = "0x%x" % ((ros<<8)+mod)
293  else:
294  modName = TileCalibUtils.getDrawerString(ros,mod)
295  iovs = ["None"]
296  dbobjs = blobReader.getDBobjsWithinRange(ros,mod)
297  while dbobjs.goToNext():
298  obj = dbobjs.currentRef()
299  objsince = obj.since()
300  sinceRun = objsince >> 32
301  sinceLum = objsince & 0xFFFFFFFF
302  since = "(%d,%d)" % (sinceRun, sinceLum)
303  if sinceRun>lastRun:
304  lastRun = sinceRun
305  if sinceRun>=begin:
306  if iovs[0]!="None":
307  iovs += [since]
308  else:
309  iovs[0] = since
310  if since in allsince:
311  allsince[since] += [modName]
312  else:
313  allsince[since] = [modName]
314  else:
315  iovs[0] = since
316  if iovonly:
317  print("%s %s" % (modName, " ".join(iovs)) )
318  if IOVONLY:
319  print("")
320  all=[]
321  for since in allsince:
322  if comment and allsince[since][-1]!="Comment":
323  allsince[since] += ["NO_COMMENT"]
324  all+=["%s %s" % (since," ".join(allsince[since]))]
325  if len(all)>0:
326  all.sort()
327  for s in all:
328  print(s)
329  else:
330  print("No new IOVs after run",lastRun)
331  #=== close DB
332  db.closeDatabase()
333  sys.exit(0)
334 
335 
336 #=== Filling the iovList
337 iovList = []
338 if iov:
339  if mod>=0:
340  COOL_part = ros
341  COOL_chan = mod
342  else:
343  COOL_part = -1
344  COOL_chan = 1000
345 
346  try:
347  dbobjs = blobReader.getDBobjsWithinRange(COOL_part,COOL_chan)
348  if (dbobjs is None):
349  raise Exception("No DB objects retrieved when building IOV list!")
350  while dbobjs.goToNext():
351  obj = dbobjs.currentRef()
352  objsince = obj.since()
353  sinceRun = objsince >> 32
354  sinceLum = objsince & 0xFFFFFFFF
355  since = (sinceRun, sinceLum)
356  objuntil = obj.until()
357  untilRun = objuntil >> 32
358  untilLum = objuntil & 0xFFFFFFFF
359  until = (untilRun, untilLum)
360  iovList.append((since, until))
361  except Exception:
362  log.warning( "Warning: can not read IOVs from input DB file" )
363  sys.exit(2)
364 
365  be=iovList[0][0][0]
366  en=iovList[-1][0][0]
367 
368  if begin != be or end != en:
369  ib=0
370  ie=len(iovList)
371  for i,iovs in enumerate(iovList):
372  run = iovs[0][0]
373  lumi = iovs[0][1]
374  if (run<begin and run>be) or run==begin :
375  be=run
376  ib=i
377  if run>=end and run<en:
378  en=run
379  ie=i+1
380  log.info( "" )
381  if be != begin:
382  log.info( "Changing begin run from %d to %d (start of IOV)", begin,be)
383  begin=be
384  if en != end:
385  if en>end:
386  log.info( "Changing end run from %d to %d (start of next IOV)", end,en)
387  else:
388  log.info( "Changing end run from %d to %d (start of last IOV)", end,en)
389  end=en
390  iovList=iovList[ib:ie]
391  if COOL_chan == 1000:
392  log.info( "%d IOVs in total for comment field", len(iovList) )
393  else:
394  log.info( "%d IOVs in total for %s", len(iovList),modulename)
395 else:
396  iovList.append(((run,lumi),(MAXRUN, MAXLBK)))
397 
398 log.info( "\n" )
399 
400 
401 
404 dummy = [0]*48
405 barrel = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
406  13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
407  27, 26, 25, 30, 29, 28,-33,-32, 31, 36, 35, 34,
408  39, 38, 37, 42, 41, 40, 45,-44, 43, 48, 47, 46]
409 extbar = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
410  13, 14, 15, 16, 17, 18,-19,-20, 21, 22, 23, 24,
411  -27,-26,-25,-31,-32,-28, 33, 29, 30,-36,-35, 34,
412  44, 38, 37, 43, 42, 41,-45,-39,-40,-48,-47,-46]
413 
414 ch2pmt = [ dummy, barrel, barrel, extbar, extbar ]
415 gname = [ "LG", "HG" ]
416 
417 #=== Get ADC problems
418 
419 #=== get the channel status (if it is bad and will be masked)
420 #=== the channel status depends on the definition of isBad stored
421 #=== in the database drawer 1, channel 0
422 #=== isAffected = has a problem not included in isBad definition
423 #=== isGood = has no problem at all
424 
425 pref = ""
426 for iovs in iovList:
427  if iov:
428  pref = "(%i,%i) " % (iovs[0][0],iovs[0][1])
429  if comment:
430  log.info( pref + blobReader.getComment(iovs[0]) )
431  modOk = False
432  miss = 0
433  good = 0
434  aff = 0
435  bad = 0
436  nMod = 0
437  for ros in range(rosmin,rosmax):
438  for mod in range(modmin, min(modmax,TileCalibUtils.getMaxDrawer(ros))):
439  nMod += 1
440  mgr.updateFromDb(db, folderPath, folderTag, iovs[0], 1, warn, ros, mod)
441  if hexid:
442  modName = "0x%x" % ((ros<<8)+mod)
443  else:
444  modName = TileCalibUtils.getDrawerString(ros,mod)
445  if warn<0:
446  bch = blobReader.getDrawer(ros, mod, iovs[0], False, False)
447  if bch is None:
448  modOk = False
449  miss+=1
450  #print ("%s is missing in DB" % modName)
451  else:
452  modOk = True
453  if blob and bch:
454  print ("%s Blob type: %d Version: %d Nchannels: %d Ngains: %d Nval: %d" % (modName, bch.getObjType(), bch.getObjVersion(), bch.getNChans(), bch.getNGains(), bch.getObjSizeUint32()))
455  nBad=0
456  for chn in range(chanmin,chanmax):
457  chnName = " %2i" % chn
458  for adc in range(gainmin,gainmax):
459 
460  stat = mgr.getAdcStatus(ros,mod,chn,adc)
461  #log.info( "- ADC status = isBad: %d" % stat.isBad() )
462  #log.info( "- ADC status = isGood: %d" % stat.isGood() )
463  #log.info( "- ADC status = isAffected: %d" % stat.isAffected() )
464 
465  #=== get all problems of the channel
466  prbs = mgr.getAdcProblems(ros,mod,chn,adc)
467  #log.info( "ADC Problems: " )
468  if len(prbs) or iov:
469  modOk = False
470  if pmt:
471  msg = "%s pm %02i ch %02i %s " % ( modName, abs(ch2pmt[ros][chn]), chn, gname[adc] )
472  else:
473  msg = "%s %2i %1i " % ( modName,chn,adc )
474  for prbCode in sorted(prbs.keys()):
475  prbDesc = prbs[prbCode]
476  msg += " %5i (%s)" % (prbCode,prbDesc)
477  if stat.isBad():
478  msg += " => BAD"
479  nBad+=1
480  elif stat.isAffected():
481  msg += " => Affected"
482  elif stat.isGood():
483  msg += " => good"
484  print (pref+msg)
485  if modOk:
486  good+=1
487  print ("%s ALL GOOD" % (modName))
488  elif nBad==0:
489  aff+=1
490  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):
491  bad+=1
492  if warn<0:
493  if miss:
494  print ("%3i drawers are missing in DB" % miss)
495  print ("%3i drawers are absolutely good" % good)
496  print ("%3i drawers have good and affected channels" % (aff-miss))
497  print ("%3i drawers have some bad channels" % (nMod-good-bad-aff))
498  print ("%3i drawers are completely bad" % bad)
499  #=== print all bad channels
500  #log.info("listing bad channels")
501  #mgr.listBadAdcs()
502 
503 #=== close DB
504 db.closeDatabase()
TileCalibUtils::getMaxDrawer
static unsigned int getMaxDrawer(unsigned int ros)
Returns the maximal channel number for a given drawer.
Definition: TileCalibUtils.cxx:136
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
TileCalibUtils::badtiming_definition_chan
static unsigned int badtiming_definition_chan()
Python compatibility function.
Definition: TileCalibUtils.h:132
ReadBchFromCool.usage
def usage()
Definition: ReadBchFromCool.py:12
TileCalibUtils::bad_definition_chan
static unsigned int bad_definition_chan()
Python compatibility function.
Definition: TileCalibUtils.h:126
TileCalibUtils::max_gain
static unsigned int max_gain()
Python compatibility function.
Definition: TileCalibUtils.h:114
TileCalibUtils::definitions_draweridx
static unsigned int definitions_draweridx()
Python compatibility function.
Definition: TileCalibUtils.h:124
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
histSizes.list
def list(name, path='/')
Definition: histSizes.py:38
DerivationFramework::TriggerMatchingUtils::sorted
std::vector< typename T::value_type > sorted(T begin, T end)
Helper function to create a sorted vector from an unsorted one.
min
#define min(a, b)
Definition: cfImp.cxx:40
TCS::join
std::string join(const std::vector< std::string > &v, const char c=',')
Definition: Trigger/TrigT1/L1Topo/L1TopoCommon/Root/StringUtils.cxx:10
TileCalibUtils::getDrawerString
static std::string getDrawerString(unsigned int ros, unsigned int drawer)
Return the drawer name, e.g.
Definition: TileCalibUtils.cxx:145
TileCalibUtils::max_chan
static unsigned int max_chan()
Python compatibility function.
Definition: TileCalibUtils.h:112
str
Definition: BTagTrackIpAccessor.cxx:11
dbg::print
void print(std::FILE *stream, std::format_string< Args... > fmt, Args &&... args)
Definition: SGImplSvc.cxx:70
python.CaloCondLogger.getLogger
def getLogger(name="CaloCond")
Definition: CaloCondLogger.py:16