15import getopt,sys,os,bisect
16os.environ[
'TERM'] =
'linux'
19 print (
"Usage: ", sys.argv[0],
" [OPTION] ... ")
20 print (
"Update TileCal bad channels in COOL")
22 print (
"-h, --help shows this help")
23 print (
"-f, --folder= specify folder to use, default is /TILE/OFL02/STATUS/ADC")
24 print (
"-t, --tag= specify tag to use, default is RUN2-HLT-UPD1-00")
25 print (
"-r, --run= specify run number, default is 0")
26 print (
"-l, --lumi= specify lumi block number, default is 0")
27 print (
"-b, --begin= specify run number of first iov in multi-iov mode, by default uses very first iov")
28 print (
"-e, --end= specify run number of last iov in multi-iov mode, by default uses latest iov")
29 print (
"-L, --endlumi= specify lumi block number for last iov in multi-iov mode, default is 0")
30 print (
"-A, --adjust in multi-iov mode adjust iov boundaries to nearest iov available in DB, default is False")
31 print (
"-M, --module= specify module to use in multi-IOV update, default is all")
32 print (
"-c, --comment= specify comment which should be written to DB, in multi-iov mode it is appended to old comment")
33 print (
"-C, --Comment= specify comment which should be written to DB, in mutli-iov mode it overwrites old comment")
34 print (
"-U, --user= specify username for comment")
35 print (
"-x, --execfile= specify python file which should be executed, default is bch.py")
36 print (
"-i, --inschema= specify name of input JSON file or CREST_SERVER_PATH")
37 print (
"-o, --outschema= specify name of output JSON file, default is tileCalib.json")
38 print (
"-s, --schema= the same as --o, --outschema")
39 print (
"-n, --online write additional file with online bad channel status")
40 print (
"-p, --upd4 write additional file with CURRENT UPD4 tag")
41 print (
"-v, --verbose verbose mode")
43letters =
"hr:l:b:e:L:AM:s:i:o:t:f:x:c:C:U:npv"
44keywords = [
"help",
"run=",
"lumi=",
"begin=",
"end=",
"endlumi=",
"adjust",
"module=",
"schema=",
"inschema=",
"outschema=",
"tag=",
"folder=",
"execfile=",
"comment=",
"Comment=",
"user=",
"online",
"upd4",
"verbose"]
47 opts, extraparams = getopt.getopt(sys.argv[1:], letters, keywords)
48except getopt.GetoptError
as err:
57outSchema =
'tileCalib.json'
58folderPath =
"/TILE/OFL02/STATUS/ADC"
80 if o
in (
"-f",
"--folder"):
82 elif o
in (
"-t",
"--tag"):
84 elif o
in (
"-s",
"--schema"):
86 elif o
in (
"-i",
"--inschema"):
88 elif o
in (
"-o",
"--outschema"):
90 elif o
in (
"-n",
"--online"):
92 elif o
in (
"-p",
"--upd4"):
94 elif o
in (
"-r",
"--run"):
96 elif o
in (
"-l",
"--lumi"):
98 elif o
in (
"-b",
"--begin"):
101 elif o
in (
"-e",
"--end"):
104 elif o
in (
"-L",
"--endlumi"):
106 elif o
in (
"-A",
"--adjust"):
108 elif o
in (
"-M",
"--module"):
109 moduleList = a.split(
",")
110 elif o
in (
"-x",
"--execfile"):
112 elif o
in (
"-c",
"--comment"):
114 elif o
in (
"-C",
"--Comment"):
117 elif o
in (
"-U",
"--user"):
119 elif o
in (
"-v",
"--verbose"):
121 elif o
in (
"-h",
"--help"):
125 raise RuntimeError(
"unhandeled option")
127onl=(
"/TILE/ONL01" in folderPath)
129from TileCalibBlobPython
import TileCalibCrest
130from TileCalibBlobPython
import TileCalibTools
131from TileCalibBlobPython
import TileBchCrest
132from TileCalibBlobObjs.Classes
import TileBchPrbs, TileBchDecoder
134if iov
and end >= TileCalibCrest.MAXRUN:
135 end = TileCalibCrest.MAXRUN
136 endlumi = TileCalibCrest.MAXLBK
137until = (TileCalibCrest.MAXRUN,TileCalibCrest.MAXLBK)
139from TileCalibBlobPython.TileCalibLogger
import getLogger
140log = getLogger(
"WriteBchToCrest")
142logLevel=logging.DEBUG
143log.setLevel(logLevel)
145if tag.upper().endswith(
'HEAD'):
147if len(tag)==0
or tag.endswith(
'HEAD'):
148 folderPath=
'/TILE/ONL01/STATUS/ADC'
149 log.info(
"tag is %s, using %s folder", tag
if tag
else 'empty', folderPath)
150 tag=
'TILEONL01STATUSADC-HEAD'
153if folderTag.upper().startswith(
"TILE")
or folderTag.upper().startswith(
"CALO"):
155log.info(
"Initializing bad channels from %s folder %s with tag %s", inSchema, folderPath, folderTag)
160blobReader = TileCalibCrest.TileBlobReaderCrest(inSchema, folderPath, folderTag,
None,
None, 0, 0)
161tag = blobReader.getTag()
167 log.info(
"Looking for IOVs" )
168 iovList = blobReader.getIovs()
172 ib=bisect.bisect(iovList,since)-1
175 if iovList[ib] != since:
178 log.info(
"Moving beginning of first IOV with new bad channels from (%d,%d) to (%d,%d)", beg,lumi,since[0],since[1])
181 log.info(
"Creating new IOV starting from (%d,%d) with new bad channels", beg,lumi)
188 log.info(
"Next IOV without new bad channels starts from (%d,%d)", until[0],until[1])
191 ie=bisect.bisect_left(iovList,until)
195 if iovList[ie] != until:
198 log.info(
"Moving end of last IOV from (%d,%d) to (%d,%d)", end,endlumi,until[0],until[1])
200 log.info(
"Keeping end of last IOV at (%d,%d) - new IOV is shorter than IOV in input DB", end,endlumi)
204 iovList = iovList[ib:ie]
205 iovUntil = iovList[1:] + [until]
209 log.info(
"IOVs: %s", str(iovList) )
211 log.info(
"%d IOVs in total, end of last IOV is %s", ie-ib,str(until))
217 if "UPD4" in folderTag:
218 run=TileCalibTools.getPromptCalibRunNumber()
219 log.warning(
"Run number is not specified, using minimal run number in calibration loop %d", run )
221 run=TileCalibTools.getNextRunNumber()
222 log.warning(
"Run number is not specified, using next run number %d", run )
224 log.error(
"Bad run number" )
232 log.info(
"Initializing for run %d, lumiblock %d", run,lumi)
241 comm=blobReader.getComment(since)
245 commentsSplit+=[blobReader.getComment(since,
True)]
246 mgr = TileBchCrest.TileBchMgr()
247 mgr.setLogLvl(logLevel)
248 mgr.initialize(inSchema, folderPath, folderTag, since)
253emptyChannelLongBarrel = (30, 31, 43)
254emptyChannelExtendedBarrel = (18, 19, 24, 25, 26, 27, 28, 29, 33, 34, 42, 43, 44, 45, 46, 47)
266if verbose
and not iov:
267 log.info(
"============================================================== ")
268 log.info(
"bad channels before update")
276 log.info(
"Masking new bad channels, including file %s", execFile )
279 for io,since
in enumerate(iovList):
285 log.info(
"Updating IOV %s - %s", str(since), str(until) )
289 exec(compile(open(execFile).
read(),execFile,
'exec'))
291 log.error(
"Comment string is not provided, please put comment='bla-bla-bla' line in %s", execFile )
293 except Exception
as e:
295 log.error(
"Problem reading include file %s", execFile )
300 log.info(
"============================================================== ")
301 log.info(
"bad channels after update")
307 if Comment
is not None:
311 if comment==
"None" or comment==
"keep":
312 comment = comments[io]
313 elif (iov
and comments[io]
not in comment)
or (
not iov
and adjust):
314 comment +=
" // " + comments[io]
315 if io>0
and since!=until
and 'ALL' not in moduleList:
316 author=commentsSplit[io]
318 if m
in comments[io]:
323 mgr.commitToDb(outSchema, folderPath, folderTag, (TileBchDecoder.BitPat_onl01
if onl
else TileBchDecoder.BitPat_ofl01), author, comment, since, moduleList)
327until = (TileCalibCrest.MAXRUN,TileCalibCrest.MAXLBK)
329if curSuffix
and not onl:
331 if len(comment) == 0:
332 comment=blobReader.getComment(since)
333 if comment.find(
"): ") > -1:
334 comment = comment[(comment.find(
"): ")) + 3:]
337 log.info(
"============================================================== ")
339 log.info(
"creating DB with CURRENT UPD4 tag")
341 folderTagUPD4 = blobReader.getFolderTag(folderPath,
None,
"UPD4" )
342 if folderTagUPD4 == folderTag:
343 log.warning(
"CURRENT UPD4 tag %s is identical to the tag in DB which was created already", folderTagUPD4)
344 folderTagUPD4 = blobReader.getFolderTag(folderPath,
None,
"UPD1" )
345 log.warning(
"Additional UPD1 DB with tag %s will be created instead", folderTagUPD4 )
347 mgr.updateFromDb(inSchema, folderPath, folderTagUPD4, since, 0)
350 mgr.commitToDb(outSchema, folderPath, folderTagUPD4, TileBchDecoder.BitPat_ofl01, user, comment, since, moduleList)
353if onlSuffix
and not onl:
356 comment = blobReader.getComment(since)
357 if comment.find(
"): ") > -1:
358 comment = comment[(comment.find(
"): ")) + 3:]
361 log.info(
"============================================================== ")
363 log.info(
"creating DB with ONLINE status")
366 folderOnl =
"/TILE/ONL01/STATUS/ADC"
367 folderTagOnl =
"TILEONL01STATUSADC-HEAD"
369 mgrOnl = TileBchCrest.TileBchMgr()
370 mgrOnl.setLogLvl(logLevel)
371 mgrOnl.initialize(inSchema, folderOnl, folderTagOnl, since)
375 log.info(
"============================================================== ")
376 log.info(
"online channel status BEFORE update")
380 for ros
in range(1, 5):
381 for mod
in range(0, 64):
382 for chn
in range(0, 48):
383 statlo = mgr.getAdcStatus(ros, mod, chn, 0)
384 stathi = mgr.getAdcStatus(ros, mod, chn, 1)
387 for prb
in [TileBchPrbs.TrigGeneralMask,
388 TileBchPrbs.TrigNoGain,
389 TileBchPrbs.TrigHalfGain,
390 TileBchPrbs.TrigNoisy]:
391 mgrOnl.delAdcProblem(ros, mod, chn, 0, prb)
392 mgrOnl.delAdcProblem(ros, mod, chn, 1, prb)
394 if not statlo.isGood():
395 prbs = statlo.getPrbs()
397 if prb
in [TileBchPrbs.TrigGeneralMask,
398 TileBchPrbs.TrigNoGain,
399 TileBchPrbs.TrigHalfGain,
400 TileBchPrbs.TrigNoisy]:
401 mgrOnl.addAdcProblem(ros, mod, chn, 0, prb)
402 mgrOnl.addAdcProblem(ros, mod, chn, 1, prb)
406 if statlo.isBad()
and stathi.isBad():
407 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.IgnoredInHlt)
408 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineGeneralMaskAdc)
409 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.IgnoredInHlt)
410 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineGeneralMaskAdc)
412 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.IgnoredInHlt)
413 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineGeneralMaskAdc)
414 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.IgnoredInHlt)
415 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineGeneralMaskAdc)
417 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.IgnoredInHlt)
418 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineGeneralMaskAdc)
419 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.IgnoredInHlt)
420 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineGeneralMaskAdc)
423 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.IgnoredInHlt)
424 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineGeneralMaskAdc)
425 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.IgnoredInHlt)
426 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineGeneralMaskAdc)
429 if statlo.isBadTiming()
or stathi.isBadTiming():
430 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineBadTiming)
431 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineBadTiming)
434 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineBadTiming)
435 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineBadTiming)
438 if statlo.isTimingDmuBcOffsetPos()
or stathi.isTimingDmuBcOffsetPos():
439 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineTimingDmuBcOffsetPos)
440 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineTimingDmuBcOffsetPos)
443 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineTimingDmuBcOffsetPos)
444 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineTimingDmuBcOffsetPos)
447 if statlo.isTimingDmuBcOffsetNeg()
or stathi.isTimingDmuBcOffsetNeg():
448 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineTimingDmuBcOffsetNeg)
449 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineTimingDmuBcOffsetNeg)
452 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineTimingDmuBcOffsetNeg)
453 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineTimingDmuBcOffsetNeg)
456 if statlo.isWrongBCID()
or stathi.isWrongBCID():
457 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineWrongBCID)
458 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineWrongBCID)
461 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineWrongBCID)
462 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineWrongBCID)
467 log.info(
"online channel status AFTER update")
471 mgrOnl.commitToDb(outSchema, folderOnl, folderTagOnl, TileBchDecoder.BitPat_onl01, user, comment, since, moduleList)
IovVectorMap_t read(const Folder &theFolder, const SelectionCriterion &choice, const unsigned int limit=10)