12 import getopt,sys,os,re,bisect
13 os.environ[
'TERM'] =
'linux'
16 print (
"Usage: ",sys.argv[0],
" [OPTION] ... ")
17 print (
"Updates Cell Noise database using new values from ASCII file")
19 print (
"-h, --help shows this help")
20 print (
"-i, --infile= specify the input sqlite file or full schema string")
21 print (
"-o, --outfile= specify the output sqlite file")
22 print (
"-t, --intag= specify the input tag")
23 print (
"-T, --outtag= specify the output tag")
24 print (
"-f, --folder= specify status folder to use e.g. /TILE/OFL02/NOISE/CELL ")
25 print (
"-d, --dbname= specify the database name e.g. OFLP200")
26 print (
"-S, --server= specify server - ORACLE or FRONTIER, default is FRONTIER")
27 print (
"-x, --txtfile= specify the text file with the new noise constants")
28 print (
"-r, --run= specify run number for start of IOV")
29 print (
"-l, --lumi= specify lumiblock number for start of IOV, default is 0")
30 print (
"-b, --begin= specify run number of first iov in multi-iov mode, by default uses very first iov")
31 print (
"-e, --end= specify run number of last iov in multi-iov mode, by default uses latest iov")
32 print (
"-L, --endlumi= specify lumi block number for last iov in multi-iov mode, default is 0")
33 print (
"-A, --adjust in multi-iov mode adjust iov boundaries to nearest iov available in DB, default is False")
34 print (
"-n, --channel= specify cool channel to use (48 by defalt)")
35 print (
"-s, --scale= specify scale factor for all the fields except ratio field")
36 print (
"-u --update set this flag if output sqlite file should be updated, otherwise it'll be recreated")
37 print (
"--scaleElec= specify separate scale factor for all electronic noise fields except ratio field")
38 print (
"if run number and lumiblock number are omitted - all IOVs from input file are updated")
40 letters =
"hi:o:t:T:f:d:S:x:r:l:b:e:L:A:n:s:u"
41 keywords = [
"help",
"infile=",
"outfile=",
"intag=",
"outtag=",
"folder=",
"dbname=",
"server=",
"txtfile=",
"run=",
"lumi=",
"begin=",
"end=",
"endlumi=",
"adjust",
42 "channel=",
"scale=",
"scaleA=",
"scaleB=",
"scaleD=",
"scaleE=",
"scaleD4=",
"scaleC10=",
"scaleD4sp=",
"scaleC10sp=",
"scaleElec=",
"update"]
45 opts, extraparams = getopt.getopt(sys.argv[1:],letters,keywords)
46 except getopt.GetoptError
as err:
82 if o
in (
"-i",
"--infile"):
84 elif o
in (
"-o",
"--outfile"):
86 elif o
in (
"-t",
"--intag"):
88 elif o
in (
"-T",
"--outtag"):
90 elif o
in (
"-f",
"--folder"):
92 elif o
in (
"-d",
"--dbname"):
94 elif o
in (
"-S",
"--server"):
96 elif o
in (
"-r",
"--run"):
100 elif o
in (
"-l",
"--lumi"):
102 elif o
in (
"-b",
"--begin"):
105 elif o
in (
"-e",
"--end"):
107 elif o
in (
"-L",
"--endlumi"):
109 elif o
in (
"-A",
"--adjust"):
111 elif o
in (
"-u",
"--update"):
113 elif o
in (
"-n",
"--channel"):
115 elif o
in (
"-s",
"--scale"):
117 elif o
in (
"-s",
"--scaleA"):
119 elif o
in (
"-s",
"--scaleB"):
121 elif o
in (
"-s",
"--scaleD"):
123 elif o
in (
"-s",
"--scaleE"):
125 elif o
in (
"-s",
"--scaleD4"):
127 elif o
in (
"-s",
"--scaleC10"):
129 elif o
in (
"-s",
"--scaleD4sp"):
131 elif o
in (
"-s",
"--scaleC10sp"):
132 scaleC10sp =
float(a)
133 elif o
in (
"-s",
"--scaleElec"):
135 elif o
in (
"-x",
"--txtfile"):
137 elif o
in (
"-h",
"--help"):
180 if scaleC10sp == 0.0:
181 scaleC10sp = scaleC10
188 raise Exception(
"Please, provide infile (e.g. --infile=tileSqlite.db or --infile=COOLOFL_TILE/OFLP200)")
190 raise Exception(
"Please, provide outfile (e.g. --outfile=tileSqlite_New.db)")
192 raise Exception(
"Please, provide intag (e.g. --intag=TileOfl02NoiseCell-IOVDEP-01)")
194 raise Exception(
"Please, provide outtag (e.g. --outtag=TileOfl02NoiseCell-IOVDEP-01)")
195 if len(folderPath)<1:
196 raise Exception(
"Please, provide folder (e.g. --folder=/TILE/OFL02/NOISE/CELL)")
198 raise Exception(
"Please, provide dbname (e.g. --dbname=OFLP200 or --dbname=CONDBR2)")
202 from CaloCondBlobAlgs
import CaloCondTools, CaloCondLogger
203 from TileCalibBlobPython
import TileCalibTools
204 from TileCalibBlobPython
import TileCellTools
207 log = CaloCondLogger.getLogger(
"WriteCellNoise")
210 log.setLevel(logging.INFO)
212 log.setLevel(logging.DEBUG)
220 if os.path.isfile(inFile):
221 ischema =
'sqlite://;schema='+inFile+
';dbname='+dbName
223 log.info(
"File %s was not found, assuming it's full schema string" , inFile)
241 elif run<222222
or 'COMP200' in ischema:
244 if (
'OFLP200' in ischema
and rb>=330000)
or rb>=400000:
246 elif (
'OFLP200' in ischema
and rb>=310000)
or rb>=342550:
251 hashMgrDef=TileCellTools.TileCellHashMgr(cabling=cabling)
252 hashMgrA=TileCellTools.TileCellHashMgr(
"UpgradeA")
253 hashMgrBC=TileCellTools.TileCellHashMgr(
"UpgradeBC")
254 hashMgrABC=TileCellTools.TileCellHashMgr(
"UpgradeABC")
258 until = (TileCalibTools.MAXRUN,TileCalibTools.MAXLBK)
259 if end >= TileCalibTools.MAXRUN:
260 end = TileCalibTools.MAXRUN
261 endlumi = TileCalibTools.MAXLBK
265 idb = TileCalibTools.openDbConn(ischema,server)
267 blobReader = TileCalibTools.TileBlobReader(idb,folderPath, inTag)
268 iovList = blobReader.getIOVsWithinRange(-1,chan)
270 log.warning(
"Can not read IOVs from input DB file")
278 ib=bisect.bisect(iovList,since)-1
281 if iovList[ib] != since:
284 log.info(
"Moving beginning of first IOV with new cell noise from (%d,%d) to (%d,%d)" , beg,lumi,since[0],since[1])
287 log.info(
"Creating new IOV starting from (%d,%d) with new cell noise" , beg,lumi)
294 log.info(
"Next IOV with old cell noise starts from (%d,%d)" , until[0],until[1])
297 ie=bisect.bisect_left(iovList,until)
301 if iovList[ie] != until:
304 log.info(
"Moving end of last IOV from (%d,%d) to (%d,%d)" , end,endlumi,until[0],until[1])
306 log.info(
"Keeping end of last IOV at (%d,%d) - new IOV is shorter than IOV in input DB" , end,endlumi)
309 iovList = iovList[ib:ie]
310 iovUntil = iovList[1:] + [until]
313 log.info(
"IOVs: %s" ,
str(iovList))
314 log.info(
"%d IOVs in total, end of last IOV is %s" , ie-ib,
str(until))
318 raise Exception(
"Please, provide run number at command line")
320 log.info(
"Creating single IOV starting from run,lumi %d,%d" , run,lumi)
322 until = (end, endlumi)
327 oschema =
'sqlite://;schema='+outFile+
';dbname='+dbName
328 dbr = CaloCondTools.openDbConn(ischema,server)
329 update = update
or (inFile==outFile)
330 dbw = CaloCondTools.openDbConn(oschema,(
'UPDATE' if update
else 'RECREATE'))
331 reader = CaloCondTools.CaloBlobReader(dbr,folderPath,inTag)
332 writer = CaloCondTools.CaloBlobWriter(dbw,folderPath,
'Flt',(outTag!=
"" and outTag!=
"HEAD"))
339 icell=[0,0,0,0,0,0,0]
347 with open(txtFile,
"r")
as f:
348 cellDataText = f.readlines()
350 for line
in cellDataText:
355 if fields[0].startswith(
"#"):
358 if fields[0][:1].isalpha():
360 if useNames
is not None and not useNames:
361 raise Exception(
"Mixture of formats in inpyt file %s - useNames" % (txtFile))
363 if fields[0]==
'Cell':
364 if useModuleNames
is not None and useModuleNames:
365 raise Exception(
"Mixture of formats in inpyt file %s - useModuleNames" % (txtFile))
370 if useModuleNames
is not None and not useModuleNames:
371 raise Exception(
"Mixture of formats in inpyt file %s - useModuleNames" % (txtFile))
375 if fields[2].isdigit():
376 if useGain
is not None and not useGain:
377 raise Exception(
"Mixture of formats in inpyt file %s - useGain" % (txtFile))
386 if useGain
is not None and useGain:
387 raise Exception(
"Mixture of formats in inpyt file %s - useGain" % (txtFile))
389 raise Exception(
"Too many fields in input file %s" % (txtFile))
392 noise = [-1]+fields[2:]
396 if cellName.startswith(
'BC'):
397 cellName=
'B'+cellName[2:]
398 if not (
'+' in cellName
or '-' in cellName
or '*' in cellName):
399 p = re.search(
"\\d", cellName).
start()
400 cellPos = modName+cellName[:p] +
'+' + cellName[p:]
401 cellNeg = modName+cellName[:p] +
'-' + cellName[p:]
402 dictKey = (cellPos,gain)
403 cellData[dictKey] = noise
404 dictKey = (cellNeg,gain)
405 cellData[dictKey] = noise
406 if (cellName==
'spE1'):
407 for cellNm
in [
'mbE+1',
'mbE-1',
'e4E+1',
'e4E-1']:
408 cellN = modName+cellNm
409 dictKey = (cellN,gain)
410 if dictKey
not in cellData:
411 cellData[dictKey] = noise
413 cellN = modName+cellName
414 dictKey = (cellN,gain)
415 cellData[dictKey] = noise
416 if (cellName==
'spE+1'):
417 for cellNm
in [
'mbE+1',
'e4E+1']:
418 cellN = modName+cellNm
419 dictKey = (cellN,gain)
420 if dictKey
not in cellData:
421 cellData[dictKey] = noise
422 if (cellName==
'spE-1'):
423 for cellNm
in [
'mbE-1',
'e4E-1']:
424 cellN = modName+cellNm
425 dictKey = (cellN,gain)
426 if dictKey
not in cellData:
427 cellData[dictKey] = noise
430 if useNames
is not None and useNames:
431 raise Exception(
"Mixture of formats in inpyt file %s - useNames" % (txtFile))
436 dictKey = (cellHash,cellGain)
437 cellData[dictKey] = noise
438 if icell[gain]<cellHash:
452 log.info(
"No input txt file provided, making copy from input DB to output DB")
459 modName=
"LAr %2d" % chan
464 for io,since
in enumerate(iovList):
474 log.info(
"Updating IOV [%d,%d] - [%d,%d)" , sinceRun, sinceLum, untilRun, untilLum)
476 blobR = reader.getCells(chan,(sinceRun,sinceLum))
479 mcell=blobR.getNChans()
480 mgain=blobR.getNGains()
481 mval=blobR.getObjSizeUint32()
483 log.info(
"input file: ncell: %d ngain %d nval %d" ,
max(icell), igain, ival)
484 log.info(
"input db: ncell: %d ngain %d nval %d" , mcell, mgain, mval)
492 log.info(
"output db: ncell: %d ngain %d nval %d" , ncell, ngain, nval)
494 if ncell>hashMgrA.getHashMax():
496 elif ncell>hashMgrBC.getHashMax():
498 elif ncell>hashMgrDef.getHashMax():
502 log.info(
"Using %s CellMgr with hashMax %d" , hashMgr.getGeometry(),hashMgr.getHashMax())
504 GainDefVec = cppyy.gbl.std.vector(
'float')()
505 for val
in range(nval):
506 GainDefVec.push_back(0.0)
507 defVec = cppyy.gbl.std.vector(
'std::vector<float>')()
508 for gain
in range(ngain):
509 defVec.push_back(GainDefVec)
510 blobW = writer.getCells(chan)
511 blobW.init(defVec,ncell,1)
513 src = [
'Default',
'DB',
'File',
'Scale']
522 for cell
in range(ncell):
523 exist0 = (cell<mcell)
525 (modName,cellName)=hashMgr.getNames(cell)
526 fullName=
"%s %6s" % (modName,cellName)
527 for gain
in range(ngain):
528 exist1 = (exist0
and (gain<mgain))
535 dictKey = (modName+cellName,gn)
537 dictKey = (cellName,gn)
539 dictKey = (cell, gain)
540 noise = cellData.get(dictKey,[])
542 for field
in range(nval):
543 exist = (exist1
and (field<mval))
544 value = GainDefVec[field]
546 strval=
str(noise[field])
547 if strval.startswith(
"*"):
549 value = blobR.getData(cell,gain,field)*coef
550 elif strval.startswith(
"++")
or strval.startswith(
"+-") :
551 coef=
float(strval[1:])
552 value = blobR.getData(cell,gain,field)+coef
553 elif strval==
"keep" or strval==
"None":
556 value =
float(strval)
557 if (value
is None or value<0)
and exist:
558 value = blobR.getData(cell,gain,field)
562 value = blobR.getData(cell,gain,field)
564 if field==1
or field>4:
565 if 'spC' in cellName:
567 elif 'spD' in cellName:
569 elif 'C' in cellName
and '10' in cellName:
571 elif 'D' in cellName
and '4' in cellName:
573 elif 'E' in cellName:
575 elif 'D' in cellName:
577 elif 'B' in cellName:
579 elif 'A' in cellName:
586 src[exist] =
"ScalePileUp %s" %
str(sc)
587 elif field<4
and scaleElec>0.0:
590 src[exist] =
"ScaleElec %s" %
str(scaleElec)
592 blobW.setData( cell, gain, field, value )
593 if rescale
or exist>1:
594 print (
"%s hash %4d gain %d field %d value %f Source %s" % (fullName, cell, gain, field, value, src[exist]))
595 except Exception
as e:
596 log.warning(
"Exception on IOV [%d,%d]-[%d,%d)" , sinceRun, sinceLum, untilRun, untilLum)
597 print (FullName,
"Cell",cell,
"gain",gain,
"field",field,
"value",strval,
"noise vector",noise)
601 writer.register((sinceRun,sinceLum), (untilRun,untilLum), outTag)
603 log.info(
"Using %s CellMgr with hashMax %d" , hashMgr.getGeometry(),hashMgr.getHashMax())