2 from __future__
import print_function
3 from builtins
import range
7 from RecoLuminosity.LumiDB
import argparse,selectionParser,csvSelectionParser
11 select LUMISEGMENTNR,DEADTIMEBEAMACTIVE from cms_wbm.LEVEL1_TRIGGER_CONDITIONS where RUNNUMBER=:runnumber order by LUMISEGMENTNR; 13 update TRG set DEADTIME=:deadtimebeamactive where RUNNUM=:runnum and CMSLSNUM=:lsnum 14 --reapply calibration to inst lumi 15 update LUMISUMMARY set INSTLUMI=1.006319*INSTLUMI where RUNNUM in (1,3,57,90) 31 '''return all the runs with starttime or stoptime column NULL in lumi db 32 select runnum from CMSRUNSUMMARY where starttime is NULL or stoptime is NULL 36 emptyBindVarList=coral.AttributeList()
37 dbsession.transaction().
start(
True)
38 schema=dbsession.nominalSchema()
40 raise 'cannot connect to schema ' 41 if not schema.existsTable(c.runsummarytable):
42 raise 'non-existing table '+c.runsummarytable
43 query=schema.newQuery()
44 query.addToTableList(c.runsummarytable)
45 query.addToOutputList(
'RUNNUM',
'runnum')
46 query.setCondition(
'STARTTIME IS NULL AND STOPTIME IS NULL',emptyBindVarList)
47 query.addToOrderList(
'runnum')
48 queryOutput=coral.AttributeList()
49 queryOutput.extend(
'runnum',
'unsigned int')
50 query.defineOutput(queryOutput)
51 cursor=query.execute()
53 result.append(cursor.currentRow()[
'runnum'].
data())
55 dbsession.transaction().commit()
56 except Exception
as e:
58 dbsession.transaction().rollback()
63 get start stop time of run from runinfo database 64 select time from cms_runinfo.runsession_parameter where runnumber=:runnum and name='CMS.LVL0:START_TIME_T'; 65 select time from cms_runinfo.runsession_parameter where runnumber=:runnum and name='CMS.LVL0:STOP_TIME_T'; 68 tableName=
'RUNSESSION_PARAMETER' 70 dbsession.transaction().
start(
True)
71 schema=dbsession.nominalSchema()
73 raise 'cannot connect to schema ' 74 if not schema.existsTable(tableName):
75 raise 'non-existing table '+tableName
79 for runnum
in runnums:
80 startTQuery=schema.newQuery()
81 startTQuery.addToTableList(tableName)
82 startTQuery.addToOutputList(
'TIME',
'starttime')
83 stopTQuery=schema.newQuery()
84 stopTQuery.addToTableList(tableName)
85 stopTQuery.addToOutputList(
'TIME',
'stoptime')
86 startTQueryCondition=coral.AttributeList()
87 stopTQueryCondition=coral.AttributeList()
88 startTQueryOutput=coral.AttributeList()
89 stopTQueryOutput=coral.AttributeList()
90 startTQueryCondition.extend(
'runnum',
'unsigned int')
91 startTQueryCondition.extend(
'name',
'string')
92 startTQueryOutput.extend(
'starttime',
'time stamp')
93 stopTQueryCondition.extend(
'runnum',
'unsigned int')
94 stopTQueryCondition.extend(
'name',
'string')
95 stopTQueryOutput.extend(
'stoptime',
'time stamp')
96 startTQueryCondition[
'runnum'].setData(
int(runnum))
97 startTQueryCondition[
'name'].setData(
'CMS.LVL0:START_TIME_T')
98 startTQuery.setCondition(
'RUNNUMBER=:runnum AND NAME=:name',startTQueryCondition)
99 startTQuery.defineOutput(startTQueryOutput)
100 startTCursor=startTQuery.execute()
101 while next(startTCursor):
102 startTime=startTCursor.currentRow()[
'starttime'].
data()
103 stopTQueryCondition[
'runnum'].setData(
int(runnum))
104 stopTQueryCondition[
'name'].setData(
'CMS.LVL0:STOP_TIME_T')
105 stopTQuery.setCondition(
'RUNNUMBER=:runnum AND NAME=:name',stopTQueryCondition)
106 stopTQuery.defineOutput(stopTQueryOutput)
107 stopTCursor=stopTQuery.execute()
108 while next(stopTCursor):
109 stopTime=stopTCursor.currentRow()[
'stoptime'].
data()
110 if not startTime
or not stopTime:
111 print(
'Warning: no startTime or stopTime found for run ',runnum)
113 result[runnum]=(startTime,stopTime)
116 dbsession.transaction().commit()
117 except Exception
as e:
119 dbsession.transaction().rollback()
125 Input runtimedict{runnumber:(startTimeT,stopTimeT)} 126 update CMSRUNSUMMARY set STARTTIME=:starttime,STOPTIME=:stoptime where RUNNUM=:runnum 127 #update CMSRUNSUMMARY set STOPTIME=:stoptime where RUNNUM=:runnum 132 dbsession.transaction().
start(
False)
133 schema=dbsession.nominalSchema()
135 raise 'cannot connect to schema' 136 if not schema.existsTable(c.runsummarytable):
137 raise 'non-existing table '+c.runsummarytable
138 inputData=coral.AttributeList()
139 inputData.extend(
'starttime',
'time stamp')
140 inputData.extend(
'stoptime',
'time stamp')
141 inputData.extend(
'runnum',
'unsigned int')
142 runs=sorted(runtimedict.keys())
144 (startTimeT,stopTimeT)=runtimedict[runnum]
145 inputData[
'starttime'].setData(startTimeT)
146 inputData[
'stoptime'].setData(stopTimeT)
147 inputData[
'runnum'].setData(
int(runnum))
148 nchanged=schema.tableHandle(c.runsummarytable).dataEditor().updateRows(
'STARTTIME=:starttime,STOPTIME=:stoptime',
'RUNNUM=:runnum',inputData)
149 print(
'run '+
str(runnum)+
' update '+
str(nchanged)+
' row with starttime ,stoptime')
150 print(startTimeT,stopTimeT)
151 totalchanged=totalchanged+nchanged
153 dbsession.transaction().rollback()
155 dbsession.transaction().commit()
156 except Exception
as e:
158 dbsession.transaction().rollback()
160 print(
'total number of rows changed: ',totalchanged)
164 update LUMISUMMARY set INSTLUMI=:delta*INSTLUMI where RUNNUM in (1,3,57,90) 168 dbsession.transaction().
start(
False)
169 schema=dbsession.nominalSchema()
171 raise 'cannot connect to schema' 172 if not schema.existsTable(c.lumisummarytable):
173 raise 'non-existing table '+c.lumisummarytable
174 runliststring=
','.
join([
str(x)
for x
in runnums])
175 print(
'applying delta '+delta+
' on run list '+runliststring)
177 inputData=coral.AttributeList()
178 inputData.extend(
'delta',
'float')
179 inputData[
'delta'].setData(
float(delta))
180 nchanged=schema.tableHandle(c.lumisummarytable).dataEditor().updateRows(
'INSTLUMI=INSTLUMI*:delta',
'RUNNUM in ('+runliststring+
')',inputData)
181 print(
'total number of row changed ',nchanged)
183 dbsession.transaction().rollback()
185 dbsession.transaction().commit()
187 except Exception
as e:
189 dbsession.transaction().rollback()
194 select lsnr,counts from cms_gt_mon.gt_mon_trig_dead_view where runnr=:runnumber and deadcounter='DeadtimeBeamActive' order by lsnr; 195 return result{lumisection:deadtimebeamactive} 200 dbsession.transaction().
start(
True)
201 schema=dbsession.schema(c.gtmonschema)
204 raise Exception(
'cannot connect to schema '+c.gtmonschema)
205 if not schema.existsView(c.gtdeadview):
206 raise Exception(
'non-existing view '+c.gtdeadview)
208 deadOutput=coral.AttributeList()
209 deadOutput.extend(
"lsnr",
"unsigned int")
210 deadOutput.extend(
"deadcount",
"unsigned long long")
212 deadBindVarList=coral.AttributeList()
213 deadBindVarList.extend(
"runnumber",
"unsigned int")
214 deadBindVarList.extend(
"countername",
"string")
215 deadBindVarList[
"runnumber"].setData(
int(runnum))
216 deadBindVarList[
"countername"].setData(
'DeadtimeBeamActive')
218 query=schema.newQuery()
219 query.addToTableList(c.gtdeadview)
220 query.addToOutputList(
'LSNR',
'lsnr')
221 query.addToOutputList(
'COUNTS',
'deadcount')
222 query.setCondition(
'RUNNR=:runnumber AND DEADCOUNTER=:countername',deadBindVarList)
223 query.addToOrderList(
'lsnr')
224 query.defineOutput(deadOutput)
226 cursor=query.execute()
228 cmslsnum=cursor.currentRow()[
'lsnr'].
data()
229 deadcount=cursor.currentRow()[
'deadcount'].
data()
230 result[cmslsnum]=deadcount
234 except Exception
as e:
236 dbsession.transaction().rollback()
241 select LUMISEGMENTNR,DEADTIMEBEAMACTIVE from cms_wbm.LEVEL1_TRIGGER_CONDITIONS where RUNNUMBER=:runnum order by LUMISEGMENTNR; 242 return result{lumisection:deadtimebeamactive} 247 dbsession.transaction().
start(
True)
248 schema=dbsession.nominalSchema()
250 raise Exception(
'cannot connect to schema'+c.wbmschema)
251 if not schema.existsTable(c.wbmdeadtable):
252 raise Exception(
'non-existing table'+c.wbmdeadtable)
254 deadOutput=coral.AttributeList()
255 deadOutput.extend(
"lsnr",
"unsigned int")
256 deadOutput.extend(
"deadcount",
"unsigned long long")
258 deadBindVarList=coral.AttributeList()
259 deadBindVarList.extend(
"runnum",
"unsigned int")
260 deadBindVarList[
"runnum"].setData(
int(runnum))
262 query=schema.newQuery()
263 query.addToTableList(c.wbmdeadtable)
264 query.addToOutputList(
'LUMISEGMENTNR',
'lsnr')
265 query.addToOutputList(
'DEADTIMEBEAMACTIVE',
'deadcount')
266 query.setCondition(
'RUNNUMBER=:runnum',deadBindVarList)
267 query.addToOrderList(
'LUMISEGMENTNR')
268 query.defineOutput(deadOutput)
270 cursor=query.execute()
272 cmslsnum=cursor.currentRow()[
'lsnr'].
data()
273 deadcount=cursor.currentRow()[
'deadcount'].
data()
274 result[cmslsnum]=deadcount
278 except Exception
as e:
280 dbsession.transaction().rollback()
285 input: deadtimeDict{ls:deadtimebeamactive} 287 update TRG set DEADTIME=:deadtimebeamactive where RUNNUM=:runnum and CMSLSNUM=:lsnum 288 output: number of rows changed 292 dbsession.transaction().
start(
False)
293 schema=dbsession.nominalSchema()
295 raise Exception(
'cannot connect to schema ')
296 if not schema.existsTable(c.lumitrgtable):
297 raise Exception(
'non-existing table '+c.lumitrgtable)
298 for lsnum,deadtimebeamactive
in deadtimeDict.items():
300 inputData=coral.AttributeList()
301 inputData.extend(
'deadtimebeamactive',
'unsigned int')
302 inputData.extend(
'runnum',
'unsigned int')
303 inputData.extend(
'lsnum',
'unsigned int')
304 inputData[
'deadtimebeamactive'].setData(deadtimebeamactive)
305 inputData[
'runnum'].setData(runnum)
306 inputData[
'lsnum'].setData(lsnum)
307 nchanged=schema.tableHandle(c.lumitrgtable).dataEditor().updateRows(
'DEADTIME=:deadtimebeamactive',
'RUNNUM=:runnum AND CMSLSNUM=:lsnum',inputData)
308 print(
'rows changed for ls ',
str(lsnum),
str(nchanged))
309 totalchanged+=nchanged
310 dbsession.transaction().commit()
312 except Exception
as e:
314 dbsession.transaction().rollback()
319 parser = argparse.ArgumentParser(prog=os.path.basename(sys.argv[0]),description=
"Patch LumiData")
320 parser.add_argument(
'-c',dest=
'destination',action=
'store',required=
True,help=
'destination lumi db (required)')
321 parser.add_argument(
'-s',dest=
'source',action=
'store',required=
False,help=
'source db (required except for lumicalib)')
322 parser.add_argument(
'-P',dest=
'authpath',action=
'store',required=
True,help=
'path to authentication file (required)')
323 parser.add_argument(
'-r',dest=
'runnumber',action=
'store',required=
False,help=
'run number (optional)')
324 parser.add_argument(
'-i',dest=
'inputfile',action=
'store',required=
False,help=
'run selection file(optional)')
325 parser.add_argument(
'-delta',dest=
'delta',action=
'store',required=
False,help=
'calibration factor wrt old data in lumiDB (required for lumicalib)')
326 parser.add_argument(
'action',choices=[
'deadtimeGT',
'deadtimeWBM',
'lumicalib',
'runtimestamp'],help=
'deadtimeGT: patch deadtime to deadtimebeamactive,\ndeadtimeWBM: patch deadtimeWBM to deadtimebeamactive,\nlumicalib: recalibrate inst lumi by delta where delta>1\n runtimestamp: add start,stop run timestamp where empty')
327 parser.add_argument(
'--dryrun',dest=
'dryrun',action=
'store_true',help=
'only print datasource query result, do not update destination')
329 parser.add_argument(
'--debug',dest=
'debug',action=
'store_true',help=
'debug')
330 args=parser.parse_args()
331 runnumber=args.runnumber
332 destConnect=args.destination
333 sourceConnect=args.source
334 if args.authpath
and len(args.authpath)!=0:
335 os.environ[
'CORAL_AUTH_PATH']=args.authpath
336 svc=coral.ConnectionService()
339 sourcesession=svc.connect(sourceConnect,accessMode=coral.access_ReadOnly)
340 sourcesession.typeConverter().setCppTypeForSqlType(
"unsigned int",
"NUMBER(10)")
341 sourcesession.typeConverter().setCppTypeForSqlType(
"unsigned long long",
"NUMBER(20)")
342 destsession=svc.connect(destConnect,accessMode=coral.access_Update)
343 destsession.typeConverter().setCppTypeForSqlType(
"unsigned int",
"NUMBER(10)")
344 destsession.typeConverter().setCppTypeForSqlType(
"unsigned long long",
"NUMBER(20)")
346 msg=coral.MessageStream(
'')
347 msg.setMsgVerbosity(coral.message_Level_Debug)
355 if args.action ==
'deadtimeGT':
356 if not sourceConnect:
357 raise Exception(
'deadtimeGT action requies -s option for source connection string')
359 print(
'reading from ',sourceConnect)
360 print(
'run : ',runnumber)
361 print(
'LS:deadtimebeamactive')
363 if deadresult
and len(deadresult)!=0:
364 for cmsls,deadtimebeamactive
in deadresult.items():
365 print(cmsls,deadtimebeamactive)
367 print(
'no deadtime found for run ',runnumber)
370 print(
'total LS: ',len(deadresult))
372 if len(deadresult)!=
max( [ x
for x
in deadresult.keys() ] ):
373 print(
'total ls: ',len(deadresult))
375 print(
'alert: missing Lumi Sections in the middle')
376 for x
in range(1,
max( [ x
for x
in deadresult.keys()] ) ):
377 if x
not in deadresult:
378 print(
'filling up LS deadtime with 0: LS : ',x)
382 print(
'updating ',destConnect)
384 print(
'number of updated rows ',nupdated)
385 elif args.action ==
'deadtimeWBM':
386 if not sourceConnect:
387 raise Exception(
'deadtimeWBM action requies -s option for source connection string')
389 print(
'reading from ',sourceConnect)
390 print(
'run : ',runnumber)
391 print(
'LS:deadtimebeamactive')
393 if deadresult
and len(deadresult)!=0:
394 for cmsls,deadtimebeamactive
in deadresult.items():
395 print(cmsls,deadtimebeamactive)
397 print(
'no deadtime found for run ',runnumber)
400 print(
'total LS: ',len(deadresult))
401 if len(deadresult)!=
max( [ (deadresult[x],x)
for x
in deadresult])[1]:
402 print(
'alert: missing Lumi Sections in the middle')
403 for x
in range(1,
max( [ (deadresult[x],x)
for x
in deadresult])[1]):
404 if x
not in deadresult:
405 print(
'filling up LS deadtime with 0: LS : ',x)
409 print(
'updating ',destConnect)
411 print(
'number of updated rows ',nupdated)
412 elif args.action ==
'lumicalib':
413 if not args.delta
or args.delta==0:
414 raise Exception(
'Must provide non-zero -delta argument')
417 runnums.append(args.runnumber)
419 basename,extension=os.path.splitext(args.inputfile)
420 if extension==
'.csv':
423 f=open(args.inputfile,
'r') 424 inputfilecontent=f.read() 426 if not fileparsingResult:
427 raise Exception(
'failed to parse the input file '+ifilename)
429 runnums=fileparsingResult.runs()
432 raise Exception(
'Must provide -r or -i argument as input')
434 elif args.action ==
'runtimestamp':
435 if not sourceConnect:
436 raise Exception(
'runtimestamp action requies -s option for source connection string')
437 if not args.runnumber
and not args.inputfile:
439 print(
'these runs miss start/stop time: ',runnums)
440 print(
'total : ',len(runnums))
442 runnums=[
int(args.runnumber)]
444 basename,extension=os.path.splitext(args.inputfile)
445 if extension==
'.csv':
448 f=open(args.inputfile,
'r') 449 inputfilecontent=f.read() 451 if not fileparsingResult:
452 raise Exception(
'failed to parse the input file '+ifilename)
453 runnums=fileparsingResult.runs()
468 if __name__==
'__main__':
def getTimeForRun(dbsession, c, runnums)
S & print(S &os, JobReport::InputFile const &f)
def addTimeForRun(dbsession, c, runtimedict)
def WBMdeadtimeBeamActiveForRun(dbsession, c, runnum)
def missingTimeRuns(dbsession, c)
def recalibrateLumiForRun(dbsession, c, delta, runnums)
static std::string join(char **cmd)
char data[epos_bytes_allocation]
def patchDeadtimeForRun(dbsession, c, runnum, deadtimeDict)
def GTdeadtimeBeamActiveForRun(dbsession, c, runnum)