9 import getopt,sys,os,bisect
10 os.environ[
'TERM'] =
'linux'
13 print (
"Usage: ", sys.argv[0],
" [OPTION] ... ")
14 print (
"Update TileCal bad channels in COOL")
16 print (
"-h, --help shows this help")
17 print (
"-f, --folder= specify folder to use, default is /TILE/OFL02/STATUS/ADC")
18 print (
"-t, --tag= specify tag to use, default is RUN2-HLT-UPD1-00")
19 print (
"-r, --run= specify run number, default is 0")
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 (
"-L, --endlumi= specify lumi block number for last iov in multi-iov mode, default is 0")
24 print (
"-A, --adjust in multi-iov mode adjust iov boundaries to nearest iov available in DB, default is False")
25 print (
"-M, --module= specify module to use in multi-IOV update, default is all")
26 print (
"-m, --mode= specify update mode: 1 or 2; if not set - choosen automatically, depending on schema")
27 print (
"-c, --comment= specify comment which should be written to DB, in multi-iov mode it is appended to old comment")
28 print (
"-C, --Comment= specify comment which should be written to DB, in mutli-iov mode it overwrites old comment")
29 print (
"-U, --user= specify username for comment")
30 print (
"-x, --execfile= specify python file which should be executed, default is bch.py")
31 print (
"-i, --inschema= specify the input schema to use, default is 'COOLOFL_TILE/CONDBR2'")
32 print (
"-o, --outschema= specify the output schema to use, default is 'sqlite://;schema=tileSqlite.db;dbname=CONDBR2'")
33 print (
"-n, --online write additional sqlite file with online bad channel status")
34 print (
"-p, --upd4 write additional sqlite file with CURRENT UPD4 tag")
35 print (
"-v, --verbose verbose mode")
36 print (
"-s, --schema= specify input/output schema to use when both input and output schemas are the same")
37 print (
"-S, --server= specify server - ORACLE or FRONTIER, default is FRONTIER")
38 print (
"-u --update set this flag if output sqlite file should be updated, otherwise it'll be recreated")
40 letters =
"hr:l:b:e:L:AM:m:S:s:i:o:t:f:x:c:C:U:npvu"
41 keywords = [
"help",
"run=",
"lumi=",
"begin=",
"end=",
"endlumi=",
"adjust",
"module=",
"mode=",
"server=",
"schema=",
"inschema=",
"outschema=",
"tag=",
"folder=",
"execfile=",
"comment=",
"Comment=",
"user=",
"online",
"upd4",
"verbose",
"update"]
44 opts, extraparams = getopt.getopt(sys.argv[1:], letters, keywords)
45 except getopt.GetoptError
as err:
55 schema =
'sqlite://;schema=tileSqlite.db;dbname=CONDBR2'
56 oraSchema =
'COOLOFL_TILE/CONDBR2'
59 folderPath =
"/TILE/OFL02/STATUS/ADC"
82 if o
in (
"-f",
"--folder"):
84 elif o
in (
"-t",
"--tag"):
86 elif o
in (
"-S",
"--server"):
88 elif o
in (
"-s",
"--schema"):
92 elif o
in (
"-i",
"--inschema"):
94 elif o
in (
"-o",
"--outschema"):
96 elif o
in (
"-n",
"--online"):
98 elif o
in (
"-p",
"--upd4"):
100 elif o
in (
"-u",
"--update"):
102 elif o
in (
"-r",
"--run"):
104 elif o
in (
"-l",
"--lumi"):
106 elif o
in (
"-b",
"--begin"):
109 elif o
in (
"-e",
"--end"):
112 elif o
in (
"-L",
"--endlumi"):
114 elif o
in (
"-A",
"--adjust"):
116 elif o
in (
"-M",
"--module"):
117 moduleList += a.split(
",")
118 elif o
in (
"-m",
"--mode"):
120 elif o
in (
"-x",
"--execfile"):
122 elif o
in (
"-c",
"--comment"):
124 elif o
in (
"-C",
"--Comment"):
127 elif o
in (
"-U",
"--user"):
129 elif o
in (
"-v",
"--verbose"):
131 elif o
in (
"-h",
"--help"):
135 raise RuntimeError(
"unhandeled option")
137 onl=(
"/TILE/ONL01" in folderPath)
139 if inSchema == oraSchema:
140 inSchema = inSchema.replace(
"COOLOFL",
"COOLONL")
141 oraSchema = oraSchema.replace(
"COOLOFL",
"COOLONL")
142 if not len(outSchema):
146 if not len(inSchema):
148 update = update
or (inSchema==outSchema)
150 from TileCalibBlobPython
import TileCalibTools
151 from TileCalibBlobPython
import TileBchTools
152 from TileCalibBlobObjs.Classes
import TileBchPrbs, TileBchDecoder, TileCalibUtils
154 if iov
and end >= TileCalibTools.MAXRUN:
155 end = TileCalibTools.MAXRUN
156 endlumi = TileCalibTools.MAXLBK
157 until = (TileCalibTools.MAXRUN,TileCalibTools.MAXLBK)
159 from TileCalibBlobPython.TileCalibLogger
import getLogger
162 log.setLevel(logging.DEBUG)
164 dbr = TileCalibTools.openDbConn(inSchema,server)
165 folderTag = TileCalibTools.getFolderTag(dbr, folderPath, tag)
166 log.info(
"Initializing bad channels from %s folder %s with tag %s", inSchema, folderPath, tag)
173 blobReader = TileCalibTools.TileBlobReader(dbr,folderPath, folderTag)
176 log.info(
"Looking for IOVs" )
177 if moduleList!=[
'CMT']:
178 for ros
in range(1,5):
179 for mod
in range(0,64):
181 if len(moduleList)>0
and modName
not in moduleList
and 'ALL' not in moduleList:
183 iovList += blobReader.getIOVsWithinRange(ros,mod)
184 if 'CMT' in moduleList:
186 iovListCMT = blobReader.getIOVsWithinRange(-1,1000)
192 if item1[0]!=item2[0]:
193 return item1[0]-item2[0]
195 return item1[1]-item2[1]
197 iovList=
sorted(iovList,key=functools.cmp_to_key(compare))
202 ib=bisect.bisect(iovList,since)-1
205 if iovList[ib] != since:
208 log.info(
"Moving beginning of first IOV with new bad channels from (%d,%d) to (%d,%d)", beg,lumi,since[0],since[1])
211 log.info(
"Creating new IOV starting from (%d,%d) with new bad channels", beg,lumi)
218 log.info(
"Next IOV without new bad channels starts from (%d,%d)", until[0],until[1])
221 ie=bisect.bisect_left(iovList,until)
225 if iovList[ie] != until:
228 log.info(
"Moving end of last IOV from (%d,%d) to (%d,%d)", end,endlumi,until[0],until[1])
230 log.info(
"Keeping end of last IOV at (%d,%d) - new IOV is shorter than IOV in input DB", end,endlumi)
234 iovList = iovList[ib:ie]
235 iovUntil = iovList[1:] + [until]
239 log.info(
"IOVs: %s",
str(iovList) )
241 if len(iovListMOD)>0
and len(iovListCMT)>0:
246 for io,since
in enumerate(iovList):
247 p = bisect.bisect(iovListCMT,since)
248 if p<len(iovListCMT):
249 iovUntilCMT += [iovListCMT[p]]
250 if iovUntil[io] != iovListCMT[p]:
251 log.info(
"End of iov %s in comments record is %s",
str(since),
str(iovListCMT[p]))
254 iovUntilCMT += [since]
259 iovUntilCMT = iovUntil
261 log.info(
"%d IOVs in total, end of last IOV is %s", ie-ib,
str(until))
267 if "UPD4" in folderTag:
268 run=TileCalibTools.getPromptCalibRunNumber()
269 log.warning(
"Run number is not specified, using minimal run number in calibration loop %d", run )
271 run=TileCalibTools.getLastRunNumber()
272 log.warning(
"Run number is not specified, using current run number %d", run )
274 log.error(
"Bad run number" )
280 iovUntilCMT = [until]
283 log.info(
"Initializing for run %d, lumiblock %d", run,lumi)
287 if inSchema == outSchema:
291 elif mode != 1
and mode != 2:
292 log.error(
"Bad mode %d", mode )
301 for since
in iovList:
302 comm=blobReader.getComment(since)
306 commentsSplit+=[blobReader.getComment(since,
True)]
307 mgr = TileBchTools.TileBchMgr()
308 mgr.setLogLvl(logging.DEBUG)
309 mgr.initialize(dbr, folderPath, folderTag, since, mode)
314 emptyChannelLongBarrel = (30, 31, 43)
315 emptyChannelExtendedBarrel = (18, 19, 24, 25, 26, 27, 28, 29, 33, 34, 42, 43, 44, 45, 46, 47)
327 if verbose
and not iov:
328 log.info(
"============================================================== ")
329 log.info(
"bad channels before update")
337 log.info(
"Masking new bad channels, including file %s", execFile )
338 dbw = TileCalibTools.openDbConn(outSchema,(
'UPDATE' if update
else 'RECREATE'))
341 for io,since
in enumerate(iovList):
344 untilCmt=iovUntilCMT[io]
345 if since==until
and since==untilCmt:
348 log.info(
"Updating IOV %s",
str(since) )
351 if since==until
or (since
in iovListCMT
and since
not in iovListMOD):
358 log.error(
"Comment string is not provided, please put comment='bla-bla-bla' line in %s", execFile )
360 except Exception
as e:
362 log.error(
"Problem reading include file %s", execFile )
367 log.info(
"============================================================== ")
368 log.info(
"bad channels after update")
374 if Comment
is not None:
379 comment = comments[io]
380 elif iov
and comments[io]
not in comment:
381 comment +=
" // " + comments[io]
382 if io>0
and since!=until
and 'ALL' not in moduleList:
383 author=commentsSplit[io]
385 if m
in comments[io]:
391 if since==until
and comment!=comments[io]:
392 comment =
"UNDO " + comment
393 mgr.commitToDb(dbw, folderPath, folderTag, (TileBchDecoder.BitPat_onl01
if onl
else TileBchDecoder.BitPat_ofl01), author, comment, since, until, untilCmt, mList)
399 until = (TileCalibTools.MAXRUN,TileCalibTools.MAXLBK)
401 if len(curSuffix)
and not onl
and "sqlite" in outSchema:
403 if len(comment) == 0:
404 reader = TileCalibTools.TileBlobReader(dbr, folderPath, folderTag)
405 comment=reader.getComment(since)
406 if comment.find(
"): ") > -1:
407 comment = comment[(comment.find(
"): ")) + 3:]
410 log.info(
"============================================================== ")
412 log.info(
"creating DB with CURRENT UPD4 tag")
414 folderTagUPD4 = TileCalibTools.getFolderTag(dbr, folderPath,
"UPD4" )
415 if folderTagUPD4 == folderTag:
416 log.warning(
"CURRENT UPD4 tag %s is identical to the tag in DB which was created already", folderTagUPD4)
417 folderTagUPD4 = TileCalibTools.getFolderTag(dbr, folderPath,
"UPD1" )
418 log.warning(
"Additional UPD1 DB with tag %s will be created instead", folderTagUPD4 )
419 curSchema = outSchema.replace(
".db",
"_upd1.db")
421 curSchema = outSchema.replace(
".db",curSuffix+
".db")
423 folder = dbr.getFolder(folderPath)
424 if folderTagUPD4
not in folder.listTags():
425 log.warning(
"Tag %s not found in input schema, reading previous status from Oracle", folderTagUPD4)
426 dbR = TileCalibTools.openDbConn(oraSchema,server)
427 mgr.updateFromDb(dbR, folderPath, folderTagUPD4, since, 0, 2)
430 mgr.updateFromDb(dbr, folderPath, folderTagUPD4, since, 0, 2)
433 dbW = TileCalibTools.openDbConn(curSchema,(
'UPDATE' if update
else 'RECREATE'))
434 mgr.commitToDb(dbW, folderPath, folderTagUPD4, TileBchDecoder.BitPat_ofl01, user, comment, since, until)
438 if len(onlSuffix)
and not onl
and "sqlite" in outSchema:
441 reader = TileCalibTools.TileBlobReader(dbr, folderPath, folderTag)
442 comment = reader.getComment(since)
443 if comment.find(
"): ") > -1:
444 comment = comment[(comment.find(
"): ")) + 3:]
447 log.info(
"============================================================== ")
449 log.info(
"creating DB with ONLINE status")
455 folderOnl =
"/TILE/ONL01/STATUS/ADC"
458 inSchemaOnl = inSchema.replace(
"COOLOFL",
"COOLONL")
459 if inSchemaOnl != inSchema:
461 dbr = TileCalibTools.openDbConn(inSchemaOnl, server)
464 reader = TileCalibTools.TileBlobReader(dbr, folderOnl, folderTagOnl)
466 log.warning(
"No %s folder in %s, reading from Oracle", folderOnl, inSchema)
467 inSchemaOnl = oraSchema.replace(
"COOLOFL",
"COOLONL")
469 dbr = TileCalibTools.openDbConn(inSchemaOnl, server)
471 mgrOnl = TileBchTools.TileBchMgr()
472 mgrOnl.setLogLvl(logging.DEBUG)
473 mgrOnl.initialize(dbr, folderOnl, folderTagOnl, since, 2)
477 log.info(
"============================================================== ")
478 log.info(
"online channel status BEFORE update")
482 for ros
in range(1, 5):
483 for mod
in range(0, 64):
484 for chn
in range(0, 48):
485 statlo = mgr.getAdcStatus(ros, mod, chn, 0)
486 stathi = mgr.getAdcStatus(ros, mod, chn, 1)
489 for prb
in [TileBchPrbs.TrigGeneralMask,
490 TileBchPrbs.TrigNoGain,
491 TileBchPrbs.TrigHalfGain,
492 TileBchPrbs.TrigNoisy]:
493 mgrOnl.delAdcProblem(ros, mod, chn, 0, prb)
494 mgrOnl.delAdcProblem(ros, mod, chn, 1, prb)
496 if not statlo.isGood():
497 prbs = statlo.getPrbs()
499 if prb
in [TileBchPrbs.TrigGeneralMask,
500 TileBchPrbs.TrigNoGain,
501 TileBchPrbs.TrigHalfGain,
502 TileBchPrbs.TrigNoisy]:
503 mgrOnl.addAdcProblem(ros, mod, chn, 0, prb)
504 mgrOnl.addAdcProblem(ros, mod, chn, 1, prb)
508 if statlo.isBad()
and stathi.isBad():
509 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.IgnoredInHlt)
510 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineGeneralMaskAdc)
511 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.IgnoredInHlt)
512 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineGeneralMaskAdc)
514 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.IgnoredInHlt)
515 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineGeneralMaskAdc)
516 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.IgnoredInHlt)
517 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineGeneralMaskAdc)
519 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.IgnoredInHlt)
520 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineGeneralMaskAdc)
521 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.IgnoredInHlt)
522 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineGeneralMaskAdc)
525 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.IgnoredInHlt)
526 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineGeneralMaskAdc)
527 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.IgnoredInHlt)
528 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineGeneralMaskAdc)
531 if statlo.isBadTiming()
or stathi.isBadTiming():
532 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineBadTiming)
533 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineBadTiming)
536 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineBadTiming)
537 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineBadTiming)
540 if statlo.isTimingDmuBcOffsetPos()
or stathi.isTimingDmuBcOffsetPos():
541 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineTimingDmuBcOffsetPos)
542 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineTimingDmuBcOffsetPos)
545 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineTimingDmuBcOffsetPos)
546 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineTimingDmuBcOffsetPos)
549 if statlo.isTimingDmuBcOffsetNeg()
or stathi.isTimingDmuBcOffsetNeg():
550 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineTimingDmuBcOffsetNeg)
551 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineTimingDmuBcOffsetNeg)
554 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineTimingDmuBcOffsetNeg)
555 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineTimingDmuBcOffsetNeg)
558 if statlo.isWrongBCID()
or stathi.isWrongBCID():
559 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineWrongBCID)
560 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineWrongBCID)
563 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineWrongBCID)
564 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineWrongBCID)
569 log.info(
"online channel status AFTER update")
573 onlSchema = outSchema.replace(
".db", onlSuffix +
".db")
574 dbW = TileCalibTools.openDbConn(onlSchema,(
'UPDATE' if update
else 'RECREATE'))
575 mgrOnl.commitToDb(dbW, folderOnl, folderTagOnl, TileBchDecoder.BitPat_onl01, user, comment, since, until)