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)
159blobReader = TileCalibCrest.TileBlobReaderCrest(inSchema, folderPath, folderTag,
max(run,beg), lumi, 0, 0)
160if folderPath
and not (folderTag.upper().startswith(
"TILE")
or folderTag.upper().startswith(
"CALO")):
161 folderTag = blobReader.getFolderTag(folderPath,
None,folderTag)
164 log.info(
"Looking for IOVs" )
165 iovList = blobReader.getIovs()
169 ib=bisect.bisect(iovList,since)-1
172 if iovList[ib] != since:
175 log.info(
"Moving beginning of first IOV with new bad channels from (%d,%d) to (%d,%d)", beg,lumi,since[0],since[1])
178 log.info(
"Creating new IOV starting from (%d,%d) with new bad channels", beg,lumi)
185 log.info(
"Next IOV without new bad channels starts from (%d,%d)", until[0],until[1])
188 ie=bisect.bisect_left(iovList,until)
192 if iovList[ie] != until:
195 log.info(
"Moving end of last IOV from (%d,%d) to (%d,%d)", end,endlumi,until[0],until[1])
197 log.info(
"Keeping end of last IOV at (%d,%d) - new IOV is shorter than IOV in input DB", end,endlumi)
201 iovList = iovList[ib:ie]
202 iovUntil = iovList[1:] + [until]
206 log.info(
"IOVs: %s", str(iovList) )
208 log.info(
"%d IOVs in total, end of last IOV is %s", ie-ib,str(until))
214 if "UPD4" in folderTag:
215 run=TileCalibTools.getPromptCalibRunNumber()
216 log.warning(
"Run number is not specified, using minimal run number in calibration loop %d", run )
218 run=TileCalibTools.getLastRunNumber()
219 log.warning(
"Run number is not specified, using current run number %d", run )
221 log.error(
"Bad run number" )
229 log.info(
"Initializing for run %d, lumiblock %d", run,lumi)
238 comm=blobReader.getComment(since)
242 commentsSplit+=[blobReader.getComment(since,
True)]
243 mgr = TileBchCrest.TileBchMgr()
244 mgr.setLogLvl(logLevel)
245 mgr.initialize(inSchema, folderPath, folderTag, since)
250emptyChannelLongBarrel = (30, 31, 43)
251emptyChannelExtendedBarrel = (18, 19, 24, 25, 26, 27, 28, 29, 33, 34, 42, 43, 44, 45, 46, 47)
263if verbose
and not iov:
264 log.info(
"============================================================== ")
265 log.info(
"bad channels before update")
273 log.info(
"Masking new bad channels, including file %s", execFile )
276 for io,since
in enumerate(iovList):
282 log.info(
"Updating IOV %s - %s", str(since), str(until) )
286 exec(compile(open(execFile).
read(),execFile,
'exec'))
288 log.error(
"Comment string is not provided, please put comment='bla-bla-bla' line in %s", execFile )
290 except Exception
as e:
292 log.error(
"Problem reading include file %s", execFile )
297 log.info(
"============================================================== ")
298 log.info(
"bad channels after update")
304 if Comment
is not None:
309 comment = comments[io]
310 elif iov
and comments[io]
not in comment:
311 comment +=
" // " + comments[io]
312 if io>0
and since!=until
and 'ALL' not in moduleList:
313 author=commentsSplit[io]
315 if m
in comments[io]:
320 mgr.commitToDb(outSchema, folderPath, folderTag, (TileBchDecoder.BitPat_onl01
if onl
else TileBchDecoder.BitPat_ofl01), author, comment, since, moduleList)
324until = (TileCalibCrest.MAXRUN,TileCalibCrest.MAXLBK)
326if curSuffix
and not onl:
328 if len(comment) == 0:
329 comment=blobReader.getComment(since)
330 if comment.find(
"): ") > -1:
331 comment = comment[(comment.find(
"): ")) + 3:]
334 log.info(
"============================================================== ")
336 log.info(
"creating DB with CURRENT UPD4 tag")
338 folderTagUPD4 = blobReader.getFolderTag(folderPath,
None,
"UPD4" )
339 if folderTagUPD4 == folderTag:
340 log.warning(
"CURRENT UPD4 tag %s is identical to the tag in DB which was created already", folderTagUPD4)
341 folderTagUPD4 = blobReader.getFolderTag(folderPath,
None,
"UPD1" )
342 log.warning(
"Additional UPD1 DB with tag %s will be created instead", folderTagUPD4 )
344 mgr.updateFromDb(inSchema, folderPath, folderTagUPD4, since, 0)
347 mgr.commitToDb(outSchema, folderPath, folderTagUPD4, TileBchDecoder.BitPat_ofl01, user, comment, since, moduleList)
350if onlSuffix
and not onl:
353 comment = blobReader.getComment(since)
354 if comment.find(
"): ") > -1:
355 comment = comment[(comment.find(
"): ")) + 3:]
358 log.info(
"============================================================== ")
360 log.info(
"creating DB with ONLINE status")
363 folderOnl =
"/TILE/ONL01/STATUS/ADC"
364 folderTagOnl =
"TILEONL01STATUSADC-HEAD"
366 mgrOnl = TileBchCrest.TileBchMgr()
367 mgrOnl.setLogLvl(logLevel)
368 mgrOnl.initialize(inSchema, folderOnl, folderTagOnl, since)
372 log.info(
"============================================================== ")
373 log.info(
"online channel status BEFORE update")
377 for ros
in range(1, 5):
378 for mod
in range(0, 64):
379 for chn
in range(0, 48):
380 statlo = mgr.getAdcStatus(ros, mod, chn, 0)
381 stathi = mgr.getAdcStatus(ros, mod, chn, 1)
384 for prb
in [TileBchPrbs.TrigGeneralMask,
385 TileBchPrbs.TrigNoGain,
386 TileBchPrbs.TrigHalfGain,
387 TileBchPrbs.TrigNoisy]:
388 mgrOnl.delAdcProblem(ros, mod, chn, 0, prb)
389 mgrOnl.delAdcProblem(ros, mod, chn, 1, prb)
391 if not statlo.isGood():
392 prbs = statlo.getPrbs()
394 if prb
in [TileBchPrbs.TrigGeneralMask,
395 TileBchPrbs.TrigNoGain,
396 TileBchPrbs.TrigHalfGain,
397 TileBchPrbs.TrigNoisy]:
398 mgrOnl.addAdcProblem(ros, mod, chn, 0, prb)
399 mgrOnl.addAdcProblem(ros, mod, chn, 1, prb)
403 if statlo.isBad()
and stathi.isBad():
404 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.IgnoredInHlt)
405 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineGeneralMaskAdc)
406 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.IgnoredInHlt)
407 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineGeneralMaskAdc)
409 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.IgnoredInHlt)
410 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineGeneralMaskAdc)
411 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.IgnoredInHlt)
412 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineGeneralMaskAdc)
414 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.IgnoredInHlt)
415 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineGeneralMaskAdc)
416 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.IgnoredInHlt)
417 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineGeneralMaskAdc)
420 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.IgnoredInHlt)
421 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineGeneralMaskAdc)
422 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.IgnoredInHlt)
423 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineGeneralMaskAdc)
426 if statlo.isBadTiming()
or stathi.isBadTiming():
427 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineBadTiming)
428 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineBadTiming)
431 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineBadTiming)
432 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineBadTiming)
435 if statlo.isTimingDmuBcOffsetPos()
or stathi.isTimingDmuBcOffsetPos():
436 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineTimingDmuBcOffsetPos)
437 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineTimingDmuBcOffsetPos)
440 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineTimingDmuBcOffsetPos)
441 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineTimingDmuBcOffsetPos)
444 if statlo.isTimingDmuBcOffsetNeg()
or stathi.isTimingDmuBcOffsetNeg():
445 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineTimingDmuBcOffsetNeg)
446 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineTimingDmuBcOffsetNeg)
449 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineTimingDmuBcOffsetNeg)
450 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineTimingDmuBcOffsetNeg)
453 if statlo.isWrongBCID()
or stathi.isWrongBCID():
454 mgrOnl.addAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineWrongBCID)
455 mgrOnl.addAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineWrongBCID)
458 mgrOnl.delAdcProblem(ros, mod, chn, 0, TileBchPrbs.OnlineWrongBCID)
459 mgrOnl.delAdcProblem(ros, mod, chn, 1, TileBchPrbs.OnlineWrongBCID)
464 log.info(
"online channel status AFTER update")
468 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)