9import getopt,sys,os,bisect
10os.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 (
"-N, --run1= specify run number at which new IOV should be ended")
21 print (
"-l, --lumi= specify lumi block number, default is 0")
22 print (
"-B, --lumi1= specify lumi block number at which new IOV should be eneded, default is 0")
23 print (
"-b, --begin= specify run number of first iov in multi-iov mode, by default uses very first iov")
24 print (
"-e, --end= specify run number of last iov in multi-iov mode, by default uses latest iov")
25 print (
"-L, --endlumi= specify lumi block number for last iov in multi-iov mode, default is 0")
26 print (
"-A, --adjust in multi-iov mode adjust iov boundaries to nearest iov available in DB, default is False")
27 print (
"-M, --module= specify module to use in multi-IOV update, default is all")
28 print (
"-m, --mode= specify update mode: 1 or 2; if not set - choosen automatically, depending on schema")
29 print (
"-c, --comment= specify comment which should be written to DB, in multi-iov mode it is appended to old comment")
30 print (
"-C, --Comment= specify comment which should be written to DB, in mutli-iov mode it overwrites old comment")
31 print (
"-U, --user= specify username for comment")
32 print (
"-x, --execfile= specify python file which should be executed, default is bch.py")
33 print (
"-i, --inschema= specify the input schema to use, default is 'COOLOFL_TILE/CONDBR2'")
34 print (
"-o, --outschema= specify the output schema to use, default is 'sqlite://;schema=tileSqlite.db;dbname=CONDBR2'")
35 print (
"-n, --online write additional sqlite file with online bad channel status")
36 print (
"-p, --upd4 write additional sqlite file with CURRENT UPD4 tag")
37 print (
"-v, --verbose verbose mode")
38 print (
"-s, --schema= specify input/output schema to use when both input and output schemas are the same")
39 print (
"-S, --server= specify server - ORACLE or FRONTIER, default is FRONTIER")
40 print (
"-u --update set this flag if output sqlite file should be updated, otherwise it'll be recreated")
42letters =
"hr:l:N:B:b:e:L:AM:m:S:s:i:o:t:f:x:c:C:U:npvu"
43keywords = [
"help",
"run=",
"lumi=",
"run1=",
"lumi1=",
"begin=",
"end=",
"endlumi=",
"adjust",
"module=",
"mode=",
"server=",
"schema=",
"inschema=",
"outschema=",
"tag=",
"folder=",
"execfile=",
"comment=",
"Comment=",
"user=",
"online",
"upd4",
"verbose",
"update"]
46 opts, extraparams = getopt.getopt(sys.argv[1:], letters, keywords)
47except getopt.GetoptError
as err:
59schema =
'sqlite://;schema=tileSqlite.db;dbname=CONDBR2'
60oraSchema =
'COOLOFL_TILE/CONDBR2'
63folderPath =
"/TILE/OFL02/STATUS/ADC"
86 if o
in (
"-f",
"--folder"):
88 elif o
in (
"-t",
"--tag"):
90 elif o
in (
"-S",
"--server"):
92 elif o
in (
"-s",
"--schema"):
96 elif o
in (
"-i",
"--inschema"):
98 elif o
in (
"-o",
"--outschema"):
100 elif o
in (
"-n",
"--online"):
102 elif o
in (
"-p",
"--upd4"):
104 elif o
in (
"-u",
"--update"):
106 elif o
in (
"-r",
"--run"):
108 elif o
in (
"-l",
"--lumi"):
110 elif o
in (
"-N",
"--run1"):
112 elif o
in (
"-B",
"--lumi1"):
114 elif o
in (
"-b",
"--begin"):
117 elif o
in (
"-e",
"--end"):
120 elif o
in (
"-L",
"--endlumi"):
122 elif o
in (
"-A",
"--adjust"):
124 elif o
in (
"-M",
"--module"):
125 moduleList += a.split(
",")
126 elif o
in (
"-m",
"--mode"):
128 elif o
in (
"-x",
"--execfile"):
130 elif o
in (
"-c",
"--comment"):
132 elif o
in (
"-C",
"--Comment"):
135 elif o
in (
"-U",
"--user"):
137 elif o
in (
"-v",
"--verbose"):
139 elif o
in (
"-h",
"--help"):
143 raise RuntimeError(
"unhandeled option")
145onl=(
"/TILE/ONL01" in folderPath)
147 if inSchema == oraSchema:
148 inSchema = inSchema.replace(
"COOLOFL",
"COOLONL")
149 oraSchema = oraSchema.replace(
"COOLOFL",
"COOLONL")
150if not len(outSchema):
156update = update
or (inSchema==outSchema)
158from TileCalibBlobPython
import TileCalibTools
159from TileCalibBlobPython
import TileBchTools
160from TileCalibBlobObjs.Classes
import TileBchPrbs, TileBchDecoder, TileCalibUtils
162if iov
and end >= TileCalibTools.MAXRUN:
163 end = TileCalibTools.MAXRUN
164 endlumi = TileCalibTools.MAXLBK
165until = (TileCalibTools.MAXRUN,TileCalibTools.MAXLBK)
167from TileCalibBlobPython.TileCalibLogger
import getLogger
168log = getLogger(
"WriteBchToCool")
170log.setLevel(logging.DEBUG)
172dbr = TileCalibTools.openDbConn(inSchema,server)
173folderTag = TileCalibTools.getFolderTag(dbr, folderPath, tag)
174log.info(
"Initializing bad channels from %s folder %s with tag %s", inSchema, folderPath, tag)
181blobReader = TileCalibTools.TileBlobReader(dbr,folderPath, folderTag)
184 log.info(
"Looking for IOVs" )
185 if moduleList!=[
'CMT']:
186 for ros
in range(1,5):
187 for mod
in range(0,64):
189 if len(moduleList)>0
and modName
not in moduleList
and 'ALL' not in moduleList:
191 iovList += blobReader.getIOVsWithinRange(ros,mod)
192 if 'CMT' in moduleList:
194 iovListCMT = blobReader.getIOVsWithinRange(-1,1000)
200 if item1[0]!=item2[0]:
201 return item1[0]-item2[0]
203 return item1[1]-item2[1]
204 iovList=list(
set(iovList))
205 iovList=sorted(iovList,key=functools.cmp_to_key(compare))
210 ib=bisect.bisect(iovList,since)-1
213 if iovList[ib] != since:
216 log.info(
"Moving beginning of first IOV with new bad channels from (%d,%d) to (%d,%d)", beg,lumi,since[0],since[1])
219 log.info(
"Creating new IOV starting from (%d,%d) with new bad channels", beg,lumi)
226 log.info(
"Next IOV without new bad channels starts from (%d,%d)", until[0],until[1])
229 ie=bisect.bisect_left(iovList,until)
233 if iovList[ie] != until:
236 log.info(
"Moving end of last IOV from (%d,%d) to (%d,%d)", end,endlumi,until[0],until[1])
238 log.info(
"Keeping end of last IOV at (%d,%d) - new IOV is shorter than IOV in input DB", end,endlumi)
242 iovList = iovList[ib:ie]
243 iovUntil = iovList[1:] + [until]
247 log.info(
"IOVs: %s",
str(iovList) )
249 if len(iovListMOD)>0
and len(iovListCMT)>0:
254 for io,since
in enumerate(iovList):
255 p = bisect.bisect(iovListCMT,since)
256 if p<len(iovListCMT):
257 iovUntilCMT += [iovListCMT[p]]
258 if iovUntil[io] != iovListCMT[p]:
259 log.info(
"End of iov %s in comments record is %s",
str(since),
str(iovListCMT[p]))
262 iovUntilCMT += [since]
267 iovUntilCMT = iovUntil
269 log.info(
"%d IOVs in total, end of last IOV is %s", ie-ib,
str(until))
275 if "UPD4" in folderTag:
276 run=TileCalibTools.getPromptCalibRunNumber()
277 log.warning(
"Run number is not specified, using minimal run number in calibration loop %d", run )
279 run=TileCalibTools.getNextRunNumber()
280 log.warning(
"Run number is not specified, using next run number %d", run )
282 log.error(
"Bad run number" )
284 if run1>run
or (run1==run
and lumi1>lumi):
290 iovUntilCMT = [until]
293 log.info(
"Initializing for run %d, lumiblock %d", run,lumi)
297 if inSchema == outSchema:
301elif mode != 1
and mode != 2:
302 log.error(
"Bad mode %d", mode )
312 comm=blobReader.getComment(since)
316 commentsSplit+=[blobReader.getComment(since,
True)]
317 mgr = TileBchTools.TileBchMgr()
318 mgr.setLogLvl(logging.DEBUG)
319 mgr.initialize(dbr, folderPath, folderTag, since, mode)
324emptyChannelLongBarrel = (30, 31, 43)
325emptyChannelExtendedBarrel = (18, 19, 24, 25, 26, 27, 28, 29, 33, 34, 42, 43, 44, 45, 46, 47)
337if verbose
and not iov:
338 log.info(
"============================================================== ")
339 log.info(
"bad channels before update")
347 log.info(
"Masking new bad channels, including file %s", execFile )
348 dbw = TileCalibTools.openDbConn(outSchema,(
'UPDATE' if update
else 'RECREATE'))
351 for io,since
in enumerate(iovList):
354 untilCmt=iovUntilCMT[io]
355 if since==until
and since==untilCmt:
358 log.info(
"Updating IOV %s",
str(since) )
361 if since==until
or (since
in iovListCMT
and since
not in iovListMOD):
366 exec(compile(open(execFile).
read(),execFile,
'exec'))
368 log.error(
"Comment string is not provided, please put comment='bla-bla-bla' line in %s", execFile )
370 except Exception
as e:
372 log.error(
"Problem reading include file %s", execFile )
377 log.info(
"============================================================== ")
378 log.info(
"bad channels after update")
384 if Comment
is not None:
388 if comment==
"None" or comment==
"keep":
389 comment = comments[io]
390 elif (iov
and comments[io]
not in comment)
or (
not iov
and adjust):
391 comment +=
" // " + comments[io]
392 if io>0
and since!=until
and 'ALL' not in moduleList:
393 author=commentsSplit[io]
395 if m
in comments[io]:
401 if since==until
and comment!=comments[io]:
402 comment =
"UNDO " + comment
403 mgr.commitToDb(dbw, folderPath, folderTag, (TileBchDecoder.BitPat_onl01
if onl
else TileBchDecoder.BitPat_ofl01), author, comment, since, until, untilCmt, mList)
409until = (TileCalibTools.MAXRUN,TileCalibTools.MAXLBK)
411if len(curSuffix)
and not onl
and "sqlite" in outSchema:
413 if len(comment) == 0:
414 reader = TileCalibTools.TileBlobReader(dbr, folderPath, folderTag)
415 comment=reader.getComment(since)
416 if comment.find(
"): ") > -1:
417 comment = comment[(comment.find(
"): ")) + 3:]
420 log.info(
"============================================================== ")
422 log.info(
"creating DB with CURRENT UPD4 tag")
424 folderTagUPD4 = TileCalibTools.getFolderTag(dbr, folderPath,
"UPD4" )
425 if folderTagUPD4 == folderTag:
426 log.warning(
"CURRENT UPD4 tag %s is identical to the tag in DB which was created already", folderTagUPD4)
427 folderTagUPD4 = TileCalibTools.getFolderTag(dbr, folderPath,
"UPD1" )
428 log.warning(
"Additional UPD1 DB with tag %s will be created instead", folderTagUPD4 )
429 curSchema = outSchema.replace(
".db",
"_upd1.db")
431 curSchema = outSchema.replace(
".db",curSuffix+
".db")
433 folder = dbr.getFolder(folderPath)
434 if folderTagUPD4
not in folder.listTags():
435 log.warning(
"Tag %s not found in input schema, reading previous status from Oracle", folderTagUPD4)
436 dbR = TileCalibTools.openDbConn(oraSchema,server)
437 mgr.updateFromDb(dbR, folderPath, folderTagUPD4, since, 0, 2)
440 mgr.updateFromDb(dbr, folderPath, folderTagUPD4, since, 0, 2)
443 dbW = TileCalibTools.openDbConn(curSchema,(
'UPDATE' if update
else 'RECREATE'))
444 mgr.commitToDb(dbW, folderPath, folderTagUPD4, TileBchDecoder.BitPat_ofl01, user, comment, since, until)
448if len(onlSuffix)
and not onl
and "sqlite" in outSchema:
451 reader = TileCalibTools.TileBlobReader(dbr, folderPath, folderTag)
452 comment = reader.getComment(since)
453 if comment.find(
"): ") > -1:
454 comment = comment[(comment.find(
"): ")) + 3:]
457 log.info(
"============================================================== ")
459 log.info(
"creating DB with ONLINE status")
465 folderOnl =
"/TILE/ONL01/STATUS/ADC"
468 inSchemaOnl = inSchema.replace(
"COOLOFL",
"COOLONL")
469 if inSchemaOnl != inSchema:
471 dbr = TileCalibTools.openDbConn(inSchemaOnl, server)
474 reader = TileCalibTools.TileBlobReader(dbr, folderOnl, folderTagOnl)
476 log.warning(
"No %s folder in %s, reading from Oracle", folderOnl, inSchema)
477 inSchemaOnl = oraSchema.replace(
"COOLOFL",
"COOLONL")
479 dbr = TileCalibTools.openDbConn(inSchemaOnl, server)
481 mgrOnl = TileBchTools.TileBchMgr()
482 mgrOnl.setLogLvl(logging.DEBUG)
483 mgrOnl.initialize(dbr, folderOnl, folderTagOnl, since, 2)
487 log.info(
"============================================================== ")
488 log.info(
"online channel status BEFORE update")
492 for ros
in range(1, 5):
493 for mod
in range(0, 64):
494 for chn
in range(0, 48):
495 statlo = mgr.getAdcStatus(ros, mod, chn, 0)
496 stathi = mgr.getAdcStatus(ros, mod, chn, 1)
499 for prb
in [TileBchPrbs.TrigGeneralMask,
500 TileBchPrbs.TrigNoGain,
501 TileBchPrbs.TrigHalfGain,
502 TileBchPrbs.TrigNoisy]:
503 mgrOnl.delAdcProblem(ros, mod, chn, 0, prb)
504 mgrOnl.delAdcProblem(ros, mod, chn, 1, prb)
506 if not statlo.isGood():
507 prbs = statlo.getPrbs()
509 if prb
in [TileBchPrbs.TrigGeneralMask,
510 TileBchPrbs.TrigNoGain,
511 TileBchPrbs.TrigHalfGain,
512 TileBchPrbs.TrigNoisy]:
513 mgrOnl.addAdcProblem(ros, mod, chn, 0, prb)
514 mgrOnl.addAdcProblem(ros, mod, chn, 1, prb)
518 if statlo.isBad()
and stathi.isBad():
519 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.IgnoredInHlt)
520 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineGeneralMaskAdc)
521 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.IgnoredInHlt)
522 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineGeneralMaskAdc)
524 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.IgnoredInHlt)
525 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineGeneralMaskAdc)
526 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.IgnoredInHlt)
527 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineGeneralMaskAdc)
529 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.IgnoredInHlt)
530 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineGeneralMaskAdc)
531 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.IgnoredInHlt)
532 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineGeneralMaskAdc)
535 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.IgnoredInHlt)
536 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineGeneralMaskAdc)
537 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.IgnoredInHlt)
538 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineGeneralMaskAdc)
541 if statlo.isBadTiming()
or stathi.isBadTiming():
542 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineBadTiming)
543 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineBadTiming)
546 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineBadTiming)
547 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineBadTiming)
550 if statlo.isTimingDmuBcOffsetPos()
or stathi.isTimingDmuBcOffsetPos():
551 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineTimingDmuBcOffsetPos)
552 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineTimingDmuBcOffsetPos)
555 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineTimingDmuBcOffsetPos)
556 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineTimingDmuBcOffsetPos)
559 if statlo.isTimingDmuBcOffsetNeg()
or stathi.isTimingDmuBcOffsetNeg():
560 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineTimingDmuBcOffsetNeg)
561 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineTimingDmuBcOffsetNeg)
564 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineTimingDmuBcOffsetNeg)
565 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineTimingDmuBcOffsetNeg)
568 if statlo.isWrongBCID()
or stathi.isWrongBCID():
569 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineWrongBCID)
570 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineWrongBCID)
573 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineWrongBCID)
574 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineWrongBCID)
579 log.info(
"online channel status AFTER update")
583 onlSchema = outSchema.replace(
".db", onlSuffix +
".db")
584 dbW = TileCalibTools.openDbConn(onlSchema,(
'UPDATE' if update
else 'RECREATE'))
585 mgrOnl.commitToDb(dbW, folderOnl, folderTagOnl, TileBchDecoder.BitPat_onl01, user, comment, since, until)
static std::string getDrawerString(unsigned int ros, unsigned int drawer)
Return the drawer name, e.g.
IovVectorMap_t read(const Folder &theFolder, const SelectionCriterion &choice, const unsigned int limit=10)