8 import getopt,sys,os,bisect
9 os.environ[
'TERM'] =
'linux'
12 print (
"Usage: ", sys.argv[0],
" [OPTION] ... ")
13 print (
"Update TileCal bad channels in COOL")
15 print (
"-h, --help shows this help")
16 print (
"-f, --folder= specify folder to use, default is /TILE/OFL02/STATUS/ADC")
17 print (
"-t, --tag= specify tag to use, default is RUN2-HLT-UPD1-00")
18 print (
"-r, --run= specify run number, default is 0")
19 print (
"-l, --lumi= specify lumi block number, default is 0")
20 print (
"-b, --begin= specify run number of first iov in multi-iov mode, by default uses very first iov")
21 print (
"-e, --end= specify run number of last iov in multi-iov mode, by default uses latest iov")
22 print (
"-L, --endlumi= specify lumi block number for last iov in multi-iov mode, default is 0")
23 print (
"-A, --adjust in multi-iov mode adjust iov boundaries to nearest iov available in DB, default is False")
24 print (
"-M, --module= specify module to use in multi-IOV update, default is all")
26 print (
"-c, --comment= specify comment which should be written to DB, in multi-iov mode it is appended to old comment")
27 print (
"-C, --Comment= specify comment which should be written to DB, in mutli-iov mode it overwrites old comment")
28 print (
"-U, --user= specify username for comment")
29 print (
"-x, --execfile= specify python file which should be executed, default is bch.py")
30 print (
"-i, --inschema= specify name of input JSON file or CREST_SERVER_PATH")
31 print (
"-o, --outschema= specify name of output JSON file, default is tileCalib.json")
32 print (
"-n, --online write additional file with online bad channel status")
33 print (
"-p, --upd4 write additional file with CURRENT UPD4 tag")
34 print (
"-v, --verbose verbose mode")
35 print (
"-s, --schema= specify input/output schema to use when both input and output schemas are the same")
39 letters =
"hr:l:b:e:L:AM:m:S:s:i:o:t:f:x:c:C:U:npvu"
40 keywords = [
"help",
"run=",
"lumi=",
"begin=",
"end=",
"endlumi=",
"adjust",
"module=",
"mode=",
"server=",
"schema=",
"inschema=",
"outschema=",
"tag=",
"folder=",
"execfile=",
"comment=",
"Comment=",
"user=",
"online",
"upd4",
"verbose",
"update"]
43 opts, extraparams = getopt.getopt(sys.argv[1:], letters, keywords)
44 except getopt.GetoptError
as err:
54 schema =
'tileCalib.json'
58 folderPath =
"/TILE/OFL02/STATUS/ADC"
81 if o
in (
"-f",
"--folder"):
83 elif o
in (
"-t",
"--tag"):
85 elif o
in (
"-S",
"--server"):
87 elif o
in (
"-s",
"--schema"):
91 elif o
in (
"-i",
"--inschema"):
93 elif o
in (
"-o",
"--outschema"):
95 elif o
in (
"-n",
"--online"):
97 elif o
in (
"-p",
"--upd4"):
99 elif o
in (
"-u",
"--update"):
101 elif o
in (
"-r",
"--run"):
103 elif o
in (
"-l",
"--lumi"):
105 elif o
in (
"-b",
"--begin"):
108 elif o
in (
"-e",
"--end"):
111 elif o
in (
"-L",
"--endlumi"):
113 elif o
in (
"-A",
"--adjust"):
115 elif o
in (
"-M",
"--module"):
116 moduleList = a.split(
",")
117 elif o
in (
"-m",
"--mode"):
119 elif o
in (
"-x",
"--execfile"):
121 elif o
in (
"-c",
"--comment"):
123 elif o
in (
"-C",
"--Comment"):
126 elif o
in (
"-U",
"--user"):
128 elif o
in (
"-v",
"--verbose"):
130 elif o
in (
"-h",
"--help"):
134 raise RuntimeError(
"unhandeled option")
136 onl=(
"/TILE/ONL01" in folderPath)
137 if not len(outSchema):
141 if not len(inSchema):
143 update = update
or (inSchema==outSchema)
145 from TileCalibBlobPython
import TileCalibCrest
146 from TileCalibBlobPython
import TileCalibTools
147 from TileCalibBlobPython
import TileBchCrest
148 from TileCalibBlobObjs.Classes
import TileBchPrbs, TileBchDecoder
150 if iov
and end >= TileCalibCrest.MAXRUN:
151 end = TileCalibCrest.MAXRUN
152 endlumi = TileCalibCrest.MAXLBK
153 until = (TileCalibCrest.MAXRUN,TileCalibCrest.MAXLBK)
155 from TileCalibBlobPython.TileCalibLogger
import getLogger
158 logLevel=logging.DEBUG
159 log.setLevel(logLevel)
161 if tag.upper().endswith(
'HEAD'):
163 if len(tag)==0
or tag.endswith(
'HEAD'):
164 folderPath=
'/TILE/ONL01/STATUS/ADC'
165 log.info(
"tag is %s, using %s folder", tag
if tag
else 'empty', folderPath)
166 tag=
'TILEONL01STATUSADC-HEAD'
169 if folderTag.upper().startswith(
"TILE")
or folderTag.upper().startswith(
"CALO"):
171 log.info(
"Initializing bad channels from %s folder %s with tag %s", inSchema, folderPath, folderTag)
175 blobReader = TileCalibCrest.TileBlobReaderCrest(inSchema, folderPath, folderTag,
max(run,beg), lumi, 0, 0)
176 if folderPath
and not (folderTag.upper().startswith(
"TILE")
or folderTag.upper().startswith(
"CALO")):
177 folderTag = blobReader.getFolderTag(folderPath,
None,folderTag)
180 log.info(
"Looking for IOVs" )
181 iovList = blobReader.getIovs()
185 ib=bisect.bisect(iovList,since)-1
188 if iovList[ib] != since:
191 log.info(
"Moving beginning of first IOV with new bad channels from (%d,%d) to (%d,%d)", beg,lumi,since[0],since[1])
194 log.info(
"Creating new IOV starting from (%d,%d) with new bad channels", beg,lumi)
201 log.info(
"Next IOV without new bad channels starts from (%d,%d)", until[0],until[1])
204 ie=bisect.bisect_left(iovList,until)
208 if iovList[ie] != until:
211 log.info(
"Moving end of last IOV from (%d,%d) to (%d,%d)", end,endlumi,until[0],until[1])
213 log.info(
"Keeping end of last IOV at (%d,%d) - new IOV is shorter than IOV in input DB", end,endlumi)
217 iovList = iovList[ib:ie]
218 iovUntil = iovList[1:] + [until]
222 log.info(
"IOVs: %s",
str(iovList) )
224 log.info(
"%d IOVs in total, end of last IOV is %s", ie-ib,
str(until))
230 if "UPD4" in folderTag:
231 run=TileCalibTools.getPromptCalibRunNumber()
232 log.warning(
"Run number is not specified, using minimal run number in calibration loop %d", run )
234 run=TileCalibTools.getLastRunNumber()
235 log.warning(
"Run number is not specified, using current run number %d", run )
237 log.error(
"Bad run number" )
245 log.info(
"Initializing for run %d, lumiblock %d", run,lumi)
253 for since
in iovList:
254 comm=blobReader.getComment(since)
258 commentsSplit+=[blobReader.getComment(since,
True)]
259 mgr = TileBchCrest.TileBchMgr()
260 mgr.setLogLvl(logLevel)
261 mgr.initialize(inSchema, folderPath, folderTag, since)
266 emptyChannelLongBarrel = (30, 31, 43)
267 emptyChannelExtendedBarrel = (18, 19, 24, 25, 26, 27, 28, 29, 33, 34, 42, 43, 44, 45, 46, 47)
279 if verbose
and not iov:
280 log.info(
"============================================================== ")
281 log.info(
"bad channels before update")
289 log.info(
"Masking new bad channels, including file %s", execFile )
292 for io,since
in enumerate(iovList):
298 log.info(
"Updating IOV %s - %s",
str(since),
str(until) )
304 log.error(
"Comment string is not provided, please put comment='bla-bla-bla' line in %s", execFile )
306 except Exception
as e:
308 log.error(
"Problem reading include file %s", execFile )
313 log.info(
"============================================================== ")
314 log.info(
"bad channels after update")
320 if Comment
is not None:
325 comment = comments[io]
326 elif iov
and comments[io]
not in comment:
327 comment +=
" // " + comments[io]
328 if io>0
and since!=until
and 'ALL' not in moduleList:
329 author=commentsSplit[io]
331 if m
in comments[io]:
336 mgr.commitToDb(outSchema, folderPath, folderTag, (TileBchDecoder.BitPat_onl01
if onl
else TileBchDecoder.BitPat_ofl01), author, comment, since, moduleList)
340 until = (TileCalibCrest.MAXRUN,TileCalibCrest.MAXLBK)
342 if curSuffix
and not onl:
344 if len(comment) == 0:
345 comment=blobReader.getComment(since)
346 if comment.find(
"): ") > -1:
347 comment = comment[(comment.find(
"): ")) + 3:]
350 log.info(
"============================================================== ")
352 log.info(
"creating DB with CURRENT UPD4 tag")
354 folderTagUPD4 = blobReader.getFolderTag(folderPath,
None,
"UPD4" )
355 if folderTagUPD4 == folderTag:
356 log.warning(
"CURRENT UPD4 tag %s is identical to the tag in DB which was created already", folderTagUPD4)
357 folderTagUPD4 = blobReader.getFolderTag(folderPath,
None,
"UPD1" )
358 log.warning(
"Additional UPD1 DB with tag %s will be created instead", folderTagUPD4 )
360 mgr.updateFromDb(inSchema, folderPath, folderTagUPD4, since, 0)
363 mgr.commitToDb(outSchema, folderPath, folderTagUPD4, TileBchDecoder.BitPat_ofl01, user, comment, since, moduleList)
366 if onlSuffix
and not onl:
369 comment = blobReader.getComment(since)
370 if comment.find(
"): ") > -1:
371 comment = comment[(comment.find(
"): ")) + 3:]
374 log.info(
"============================================================== ")
376 log.info(
"creating DB with ONLINE status")
379 folderOnl =
"/TILE/ONL01/STATUS/ADC"
380 folderTagOnl =
"TILEONL01STATUSADC-HEAD"
382 mgrOnl = TileBchCrest.TileBchMgr()
383 mgrOnl.setLogLvl(logLevel)
384 mgrOnl.initialize(inSchema, folderOnl, folderTagOnl, since)
388 log.info(
"============================================================== ")
389 log.info(
"online channel status BEFORE update")
393 for ros
in range(1, 5):
394 for mod
in range(0, 64):
395 for chn
in range(0, 48):
396 statlo = mgr.getAdcStatus(ros, mod, chn, 0)
397 stathi = mgr.getAdcStatus(ros, mod, chn, 1)
400 for prb
in [TileBchPrbs.TrigGeneralMask,
401 TileBchPrbs.TrigNoGain,
402 TileBchPrbs.TrigHalfGain,
403 TileBchPrbs.TrigNoisy]:
404 mgrOnl.delAdcProblem(ros, mod, chn, 0, prb)
405 mgrOnl.delAdcProblem(ros, mod, chn, 1, prb)
407 if not statlo.isGood():
408 prbs = statlo.getPrbs()
410 if prb
in [TileBchPrbs.TrigGeneralMask,
411 TileBchPrbs.TrigNoGain,
412 TileBchPrbs.TrigHalfGain,
413 TileBchPrbs.TrigNoisy]:
414 mgrOnl.addAdcProblem(ros, mod, chn, 0, prb)
415 mgrOnl.addAdcProblem(ros, mod, chn, 1, prb)
419 if statlo.isBad()
and stathi.isBad():
420 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.IgnoredInHlt)
421 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineGeneralMaskAdc)
422 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.IgnoredInHlt)
423 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineGeneralMaskAdc)
425 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.IgnoredInHlt)
426 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineGeneralMaskAdc)
427 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.IgnoredInHlt)
428 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineGeneralMaskAdc)
430 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.IgnoredInHlt)
431 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineGeneralMaskAdc)
432 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.IgnoredInHlt)
433 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineGeneralMaskAdc)
436 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.IgnoredInHlt)
437 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineGeneralMaskAdc)
438 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.IgnoredInHlt)
439 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineGeneralMaskAdc)
442 if statlo.isBadTiming()
or stathi.isBadTiming():
443 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineBadTiming)
444 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineBadTiming)
447 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineBadTiming)
448 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineBadTiming)
451 if statlo.isTimingDmuBcOffsetPos()
or stathi.isTimingDmuBcOffsetPos():
452 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineTimingDmuBcOffsetPos)
453 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineTimingDmuBcOffsetPos)
456 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineTimingDmuBcOffsetPos)
457 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineTimingDmuBcOffsetPos)
460 if statlo.isTimingDmuBcOffsetNeg()
or stathi.isTimingDmuBcOffsetNeg():
461 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineTimingDmuBcOffsetNeg)
462 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineTimingDmuBcOffsetNeg)
465 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineTimingDmuBcOffsetNeg)
466 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineTimingDmuBcOffsetNeg)
469 if statlo.isWrongBCID()
or stathi.isWrongBCID():
470 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineWrongBCID)
471 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineWrongBCID)
474 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineWrongBCID)
475 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineWrongBCID)
480 log.info(
"online channel status AFTER update")
484 mgrOnl.commitToDb(outSchema, folderOnl, folderTagOnl, TileBchDecoder.BitPat_onl01, user, comment, since, moduleList)