5 from RecoLuminosity.LumiDB
import argparse,selectionParser,csvSelectionParser
9 select LUMISEGMENTNR,DEADTIMEBEAMACTIVE from cms_wbm.LEVEL1_TRIGGER_CONDITIONS where RUNNUMBER=:runnumber order by LUMISEGMENTNR; 11 update TRG set DEADTIME=:deadtimebeamactive where RUNNUM=:runnum and CMSLSNUM=:lsnum 12 --reapply calibration to inst lumi 13 update LUMISUMMARY set INSTLUMI=1.006319*INSTLUMI where RUNNUM in (1,3,57,90) 29 '''return all the runs with starttime or stoptime column NULL in lumi db 30 select runnum from CMSRUNSUMMARY where starttime is NULL or stoptime is NULL 34 emptyBindVarList=coral.AttributeList()
35 dbsession.transaction().
start(
True)
36 schema=dbsession.nominalSchema()
38 raise 'cannot connect to schema ' 39 if not schema.existsTable(c.runsummarytable):
40 raise 'non-existing table '+c.runsummarytable
41 query=schema.newQuery()
42 query.addToTableList(c.runsummarytable)
43 query.addToOutputList(
'RUNNUM',
'runnum')
44 query.setCondition(
'STARTTIME IS NULL AND STOPTIME IS NULL',emptyBindVarList)
45 query.addToOrderList(
'runnum')
46 queryOutput=coral.AttributeList()
47 queryOutput.extend(
'runnum',
'unsigned int')
48 query.defineOutput(queryOutput)
49 cursor=query.execute()
51 result.append(cursor.currentRow()[
'runnum'].
data())
53 dbsession.transaction().commit()
54 except Exception
as e:
56 dbsession.transaction().rollback()
61 get start stop time of run from runinfo database 62 select time from cms_runinfo.runsession_parameter where runnumber=:runnum and name='CMS.LVL0:START_TIME_T'; 63 select time from cms_runinfo.runsession_parameter where runnumber=:runnum and name='CMS.LVL0:STOP_TIME_T'; 66 tableName=
'RUNSESSION_PARAMETER' 68 dbsession.transaction().
start(
True)
69 schema=dbsession.nominalSchema()
71 raise 'cannot connect to schema ' 72 if not schema.existsTable(tableName):
73 raise 'non-existing table '+tableName
77 for runnum
in runnums:
78 startTQuery=schema.newQuery()
79 startTQuery.addToTableList(tableName)
80 startTQuery.addToOutputList(
'TIME',
'starttime')
81 stopTQuery=schema.newQuery()
82 stopTQuery.addToTableList(tableName)
83 stopTQuery.addToOutputList(
'TIME',
'stoptime')
84 startTQueryCondition=coral.AttributeList()
85 stopTQueryCondition=coral.AttributeList()
86 startTQueryOutput=coral.AttributeList()
87 stopTQueryOutput=coral.AttributeList()
88 startTQueryCondition.extend(
'runnum',
'unsigned int')
89 startTQueryCondition.extend(
'name',
'string')
90 startTQueryOutput.extend(
'starttime',
'time stamp')
91 stopTQueryCondition.extend(
'runnum',
'unsigned int')
92 stopTQueryCondition.extend(
'name',
'string')
93 stopTQueryOutput.extend(
'stoptime',
'time stamp')
94 startTQueryCondition[
'runnum'].setData(
int(runnum))
95 startTQueryCondition[
'name'].setData(
'CMS.LVL0:START_TIME_T')
96 startTQuery.setCondition(
'RUNNUMBER=:runnum AND NAME=:name',startTQueryCondition)
97 startTQuery.defineOutput(startTQueryOutput)
98 startTCursor=startTQuery.execute()
99 while next(startTCursor):
100 startTime=startTCursor.currentRow()[
'starttime'].
data()
101 stopTQueryCondition[
'runnum'].setData(
int(runnum))
102 stopTQueryCondition[
'name'].setData(
'CMS.LVL0:STOP_TIME_T')
103 stopTQuery.setCondition(
'RUNNUMBER=:runnum AND NAME=:name',stopTQueryCondition)
104 stopTQuery.defineOutput(stopTQueryOutput)
105 stopTCursor=stopTQuery.execute()
106 while next(stopTCursor):
107 stopTime=stopTCursor.currentRow()[
'stoptime'].
data()
108 if not startTime
or not stopTime:
109 print 'Warning: no startTime or stopTime found for run ',runnum
111 result[runnum]=(startTime,stopTime)
114 dbsession.transaction().commit()
115 except Exception
as e:
117 dbsession.transaction().rollback()
123 Input runtimedict{runnumber:(startTimeT,stopTimeT)} 124 update CMSRUNSUMMARY set STARTTIME=:starttime,STOPTIME=:stoptime where RUNNUM=:runnum 125 #update CMSRUNSUMMARY set STOPTIME=:stoptime where RUNNUM=:runnum 130 dbsession.transaction().
start(
False)
131 schema=dbsession.nominalSchema()
133 raise 'cannot connect to schema' 134 if not schema.existsTable(c.runsummarytable):
135 raise 'non-existing table '+c.runsummarytable
136 inputData=coral.AttributeList()
137 inputData.extend(
'starttime',
'time stamp')
138 inputData.extend(
'stoptime',
'time stamp')
139 inputData.extend(
'runnum',
'unsigned int')
140 runs=sorted(runtimedict.keys())
142 (startTimeT,stopTimeT)=runtimedict[runnum]
143 inputData[
'starttime'].setData(startTimeT)
144 inputData[
'stoptime'].setData(stopTimeT)
145 inputData[
'runnum'].setData(
int(runnum))
146 nchanged=schema.tableHandle(c.runsummarytable).dataEditor().updateRows(
'STARTTIME=:starttime,STOPTIME=:stoptime',
'RUNNUM=:runnum',inputData)
147 print 'run '+
str(runnum)+
' update '+
str(nchanged)+
' row with starttime ,stoptime' 148 print startTimeT,stopTimeT
149 totalchanged=totalchanged+nchanged
151 dbsession.transaction().rollback()
153 dbsession.transaction().commit()
154 except Exception
as e:
156 dbsession.transaction().rollback()
158 print 'total number of rows changed: ',totalchanged
162 update LUMISUMMARY set INSTLUMI=:delta*INSTLUMI where RUNNUM in (1,3,57,90) 166 dbsession.transaction().
start(
False)
167 schema=dbsession.nominalSchema()
169 raise 'cannot connect to schema' 170 if not schema.existsTable(c.lumisummarytable):
171 raise 'non-existing table '+c.lumisummarytable
172 runliststring=
','.
join([
str(x)
for x
in runnums])
173 print 'applying delta '+delta+
' on run list '+runliststring
175 inputData=coral.AttributeList()
176 inputData.extend(
'delta',
'float')
177 inputData[
'delta'].setData(
float(delta))
178 nchanged=schema.tableHandle(c.lumisummarytable).dataEditor().updateRows(
'INSTLUMI=INSTLUMI*:delta',
'RUNNUM in ('+runliststring+
')',inputData)
179 print 'total number of row changed ',nchanged
181 dbsession.transaction().rollback()
183 dbsession.transaction().commit()
185 except Exception
as e:
187 dbsession.transaction().rollback()
192 select lsnr,counts from cms_gt_mon.gt_mon_trig_dead_view where runnr=:runnumber and deadcounter='DeadtimeBeamActive' order by lsnr; 193 return result{lumisection:deadtimebeamactive} 198 dbsession.transaction().
start(
True)
199 schema=dbsession.schema(c.gtmonschema)
202 raise Exception(
'cannot connect to schema '+c.gtmonschema)
203 if not schema.existsView(c.gtdeadview):
204 raise Exception(
'non-existing view '+c.gtdeadview)
206 deadOutput=coral.AttributeList()
207 deadOutput.extend(
"lsnr",
"unsigned int")
208 deadOutput.extend(
"deadcount",
"unsigned long long")
210 deadBindVarList=coral.AttributeList()
211 deadBindVarList.extend(
"runnumber",
"unsigned int")
212 deadBindVarList.extend(
"countername",
"string")
213 deadBindVarList[
"runnumber"].setData(
int(runnum))
214 deadBindVarList[
"countername"].setData(
'DeadtimeBeamActive')
216 query=schema.newQuery()
217 query.addToTableList(c.gtdeadview)
218 query.addToOutputList(
'LSNR',
'lsnr')
219 query.addToOutputList(
'COUNTS',
'deadcount')
220 query.setCondition(
'RUNNR=:runnumber AND DEADCOUNTER=:countername',deadBindVarList)
221 query.addToOrderList(
'lsnr')
222 query.defineOutput(deadOutput)
224 cursor=query.execute()
226 cmslsnum=cursor.currentRow()[
'lsnr'].
data()
227 deadcount=cursor.currentRow()[
'deadcount'].
data()
228 result[cmslsnum]=deadcount
232 except Exception
as e:
234 dbsession.transaction().rollback()
239 select LUMISEGMENTNR,DEADTIMEBEAMACTIVE from cms_wbm.LEVEL1_TRIGGER_CONDITIONS where RUNNUMBER=:runnum order by LUMISEGMENTNR; 240 return result{lumisection:deadtimebeamactive} 245 dbsession.transaction().
start(
True)
246 schema=dbsession.nominalSchema()
248 raise Exception(
'cannot connect to schema'+c.wbmschema)
249 if not schema.existsTable(c.wbmdeadtable):
250 raise Exception(
'non-existing table'+c.wbmdeadtable)
252 deadOutput=coral.AttributeList()
253 deadOutput.extend(
"lsnr",
"unsigned int")
254 deadOutput.extend(
"deadcount",
"unsigned long long")
256 deadBindVarList=coral.AttributeList()
257 deadBindVarList.extend(
"runnum",
"unsigned int")
258 deadBindVarList[
"runnum"].setData(
int(runnum))
260 query=schema.newQuery()
261 query.addToTableList(c.wbmdeadtable)
262 query.addToOutputList(
'LUMISEGMENTNR',
'lsnr')
263 query.addToOutputList(
'DEADTIMEBEAMACTIVE',
'deadcount')
264 query.setCondition(
'RUNNUMBER=:runnum',deadBindVarList)
265 query.addToOrderList(
'LUMISEGMENTNR')
266 query.defineOutput(deadOutput)
268 cursor=query.execute()
270 cmslsnum=cursor.currentRow()[
'lsnr'].
data()
271 deadcount=cursor.currentRow()[
'deadcount'].
data()
272 result[cmslsnum]=deadcount
276 except Exception
as e:
278 dbsession.transaction().rollback()
283 input: deadtimeDict{ls:deadtimebeamactive} 285 update TRG set DEADTIME=:deadtimebeamactive where RUNNUM=:runnum and CMSLSNUM=:lsnum 286 output: number of rows changed 290 dbsession.transaction().
start(
False)
291 schema=dbsession.nominalSchema()
293 raise Exception(
'cannot connect to schema ')
294 if not schema.existsTable(c.lumitrgtable):
295 raise Exception(
'non-existing table '+c.lumitrgtable)
296 for lsnum,deadtimebeamactive
in deadtimeDict.items():
298 inputData=coral.AttributeList()
299 inputData.extend(
'deadtimebeamactive',
'unsigned int')
300 inputData.extend(
'runnum',
'unsigned int')
301 inputData.extend(
'lsnum',
'unsigned int')
302 inputData[
'deadtimebeamactive'].setData(deadtimebeamactive)
303 inputData[
'runnum'].setData(runnum)
304 inputData[
'lsnum'].setData(lsnum)
305 nchanged=schema.tableHandle(c.lumitrgtable).dataEditor().updateRows(
'DEADTIME=:deadtimebeamactive',
'RUNNUM=:runnum AND CMSLSNUM=:lsnum',inputData)
306 print 'rows changed for ls ',
str(lsnum),
str(nchanged)
307 totalchanged+=nchanged
308 dbsession.transaction().commit()
310 except Exception
as e:
312 dbsession.transaction().rollback()
317 parser = argparse.ArgumentParser(prog=os.path.basename(sys.argv[0]),description=
"Patch LumiData")
318 parser.add_argument(
'-c',dest=
'destination',action=
'store',required=
True,help=
'destination lumi db (required)')
319 parser.add_argument(
'-s',dest=
'source',action=
'store',required=
False,help=
'source db (required except for lumicalib)')
320 parser.add_argument(
'-P',dest=
'authpath',action=
'store',required=
True,help=
'path to authentication file (required)')
321 parser.add_argument(
'-r',dest=
'runnumber',action=
'store',required=
False,help=
'run number (optional)')
322 parser.add_argument(
'-i',dest=
'inputfile',action=
'store',required=
False,help=
'run selection file(optional)')
323 parser.add_argument(
'-delta',dest=
'delta',action=
'store',required=
False,help=
'calibration factor wrt old data in lumiDB (required for lumicalib)')
324 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')
325 parser.add_argument(
'--dryrun',dest=
'dryrun',action=
'store_true',help=
'only print datasource query result, do not update destination')
327 parser.add_argument(
'--debug',dest=
'debug',action=
'store_true',help=
'debug')
328 args=parser.parse_args()
329 runnumber=args.runnumber
330 destConnect=args.destination
331 sourceConnect=args.source
332 if args.authpath
and len(args.authpath)!=0:
333 os.environ[
'CORAL_AUTH_PATH']=args.authpath
334 svc=coral.ConnectionService()
337 sourcesession=svc.connect(sourceConnect,accessMode=coral.access_ReadOnly)
338 sourcesession.typeConverter().setCppTypeForSqlType(
"unsigned int",
"NUMBER(10)")
339 sourcesession.typeConverter().setCppTypeForSqlType(
"unsigned long long",
"NUMBER(20)")
340 destsession=svc.connect(destConnect,accessMode=coral.access_Update)
341 destsession.typeConverter().setCppTypeForSqlType(
"unsigned int",
"NUMBER(10)")
342 destsession.typeConverter().setCppTypeForSqlType(
"unsigned long long",
"NUMBER(20)")
344 msg=coral.MessageStream(
'')
345 msg.setMsgVerbosity(coral.message_Level_Debug)
353 if args.action ==
'deadtimeGT':
354 if not sourceConnect:
355 raise Exception(
'deadtimeGT action requies -s option for source connection string')
357 print 'reading from ',sourceConnect
358 print 'run : ',runnumber
359 print 'LS:deadtimebeamactive' 361 if deadresult
and len(deadresult)!=0:
362 for cmsls,deadtimebeamactive
in deadresult.items():
363 print cmsls,deadtimebeamactive
365 print 'no deadtime found for run ',runnumber
368 print 'total LS: ',len(deadresult)
370 if len(deadresult)!=
max( [ x
for x
in deadresult.keys() ] ):
371 print 'total ls: ',len(deadresult)
373 print 'alert: missing Lumi Sections in the middle' 374 for x
in range(1,
max( [ x
for x
in deadresult.keys()] ) ):
375 if x
not in deadresult:
376 print 'filling up LS deadtime with 0: LS : ',x
380 print 'updating ',destConnect
382 print 'number of updated rows ',nupdated
383 elif args.action ==
'deadtimeWBM':
384 if not sourceConnect:
385 raise Exception(
'deadtimeWBM action requies -s option for source connection string')
387 print 'reading from ',sourceConnect
388 print 'run : ',runnumber
389 print 'LS:deadtimebeamactive' 391 if deadresult
and len(deadresult)!=0:
392 for cmsls,deadtimebeamactive
in deadresult.items():
393 print cmsls,deadtimebeamactive
395 print 'no deadtime found for run ',runnumber
398 print 'total LS: ',len(deadresult)
399 if len(deadresult)!=
max( [ (deadresult[x],x)
for x
in deadresult])[1]:
400 print 'alert: missing Lumi Sections in the middle' 401 for x
in range(1,
max( [ (deadresult[x],x)
for x
in deadresult])[1]):
402 if x
not in deadresult:
403 print 'filling up LS deadtime with 0: LS : ',x
407 print 'updating ',destConnect
409 print 'number of updated rows ',nupdated
410 elif args.action ==
'lumicalib':
411 if not args.delta
or args.delta==0:
412 raise Exception(
'Must provide non-zero -delta argument')
415 runnums.append(args.runnumber)
417 basename,extension=os.path.splitext(args.inputfile)
418 if extension==
'.csv':
421 f=open(args.inputfile,
'r') 422 inputfilecontent=f.read() 424 if not fileparsingResult:
425 raise Exception(
'failed to parse the input file '+ifilename)
427 runnums=fileparsingResult.runs()
430 raise Exception(
'Must provide -r or -i argument as input')
432 elif args.action ==
'runtimestamp':
433 if not sourceConnect:
434 raise Exception(
'runtimestamp action requies -s option for source connection string')
435 if not args.runnumber
and not args.inputfile:
437 print 'these runs miss start/stop time: ',runnums
438 print 'total : ',len(runnums)
440 runnums=[
int(args.runnumber)]
442 basename,extension=os.path.splitext(args.inputfile)
443 if extension==
'.csv':
446 f=open(args.inputfile,
'r') 447 inputfilecontent=f.read() 449 if not fileparsingResult:
450 raise Exception(
'failed to parse the input file '+ifilename)
451 runnums=fileparsingResult.runs()
466 if __name__==
'__main__':
def getTimeForRun(dbsession, c, runnums)
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)