2 from __future__
import print_function
6 from RecoLuminosity.LumiDB
import argparse,selectionParser,csvSelectionParser
10 select LUMISEGMENTNR,DEADTIMEBEAMACTIVE from cms_wbm.LEVEL1_TRIGGER_CONDITIONS where RUNNUMBER=:runnumber order by LUMISEGMENTNR; 12 update TRG set DEADTIME=:deadtimebeamactive where RUNNUM=:runnum and CMSLSNUM=:lsnum 13 --reapply calibration to inst lumi 14 update LUMISUMMARY set INSTLUMI=1.006319*INSTLUMI where RUNNUM in (1,3,57,90) 30 '''return all the runs with starttime or stoptime column NULL in lumi db 31 select runnum from CMSRUNSUMMARY where starttime is NULL or stoptime is NULL 35 emptyBindVarList=coral.AttributeList()
36 dbsession.transaction().
start(
True)
37 schema=dbsession.nominalSchema()
39 raise 'cannot connect to schema ' 40 if not schema.existsTable(c.runsummarytable):
41 raise 'non-existing table '+c.runsummarytable
42 query=schema.newQuery()
43 query.addToTableList(c.runsummarytable)
44 query.addToOutputList(
'RUNNUM',
'runnum')
45 query.setCondition(
'STARTTIME IS NULL AND STOPTIME IS NULL',emptyBindVarList)
46 query.addToOrderList(
'runnum')
47 queryOutput=coral.AttributeList()
48 queryOutput.extend(
'runnum',
'unsigned int')
49 query.defineOutput(queryOutput)
50 cursor=query.execute()
52 result.append(cursor.currentRow()[
'runnum'].
data())
54 dbsession.transaction().commit()
55 except Exception
as e:
57 dbsession.transaction().rollback()
62 get start stop time of run from runinfo database 63 select time from cms_runinfo.runsession_parameter where runnumber=:runnum and name='CMS.LVL0:START_TIME_T'; 64 select time from cms_runinfo.runsession_parameter where runnumber=:runnum and name='CMS.LVL0:STOP_TIME_T'; 67 tableName=
'RUNSESSION_PARAMETER' 69 dbsession.transaction().
start(
True)
70 schema=dbsession.nominalSchema()
72 raise 'cannot connect to schema ' 73 if not schema.existsTable(tableName):
74 raise 'non-existing table '+tableName
78 for runnum
in runnums:
79 startTQuery=schema.newQuery()
80 startTQuery.addToTableList(tableName)
81 startTQuery.addToOutputList(
'TIME',
'starttime')
82 stopTQuery=schema.newQuery()
83 stopTQuery.addToTableList(tableName)
84 stopTQuery.addToOutputList(
'TIME',
'stoptime')
85 startTQueryCondition=coral.AttributeList()
86 stopTQueryCondition=coral.AttributeList()
87 startTQueryOutput=coral.AttributeList()
88 stopTQueryOutput=coral.AttributeList()
89 startTQueryCondition.extend(
'runnum',
'unsigned int')
90 startTQueryCondition.extend(
'name',
'string')
91 startTQueryOutput.extend(
'starttime',
'time stamp')
92 stopTQueryCondition.extend(
'runnum',
'unsigned int')
93 stopTQueryCondition.extend(
'name',
'string')
94 stopTQueryOutput.extend(
'stoptime',
'time stamp')
95 startTQueryCondition[
'runnum'].setData(
int(runnum))
96 startTQueryCondition[
'name'].setData(
'CMS.LVL0:START_TIME_T')
97 startTQuery.setCondition(
'RUNNUMBER=:runnum AND NAME=:name',startTQueryCondition)
98 startTQuery.defineOutput(startTQueryOutput)
99 startTCursor=startTQuery.execute()
100 while next(startTCursor):
101 startTime=startTCursor.currentRow()[
'starttime'].
data()
102 stopTQueryCondition[
'runnum'].setData(
int(runnum))
103 stopTQueryCondition[
'name'].setData(
'CMS.LVL0:STOP_TIME_T')
104 stopTQuery.setCondition(
'RUNNUMBER=:runnum AND NAME=:name',stopTQueryCondition)
105 stopTQuery.defineOutput(stopTQueryOutput)
106 stopTCursor=stopTQuery.execute()
107 while next(stopTCursor):
108 stopTime=stopTCursor.currentRow()[
'stoptime'].
data()
109 if not startTime
or not stopTime:
110 print(
'Warning: no startTime or stopTime found for run ',runnum)
112 result[runnum]=(startTime,stopTime)
115 dbsession.transaction().commit()
116 except Exception
as e:
118 dbsession.transaction().rollback()
124 Input runtimedict{runnumber:(startTimeT,stopTimeT)} 125 update CMSRUNSUMMARY set STARTTIME=:starttime,STOPTIME=:stoptime where RUNNUM=:runnum 126 #update CMSRUNSUMMARY set STOPTIME=:stoptime where RUNNUM=:runnum 131 dbsession.transaction().
start(
False)
132 schema=dbsession.nominalSchema()
134 raise 'cannot connect to schema' 135 if not schema.existsTable(c.runsummarytable):
136 raise 'non-existing table '+c.runsummarytable
137 inputData=coral.AttributeList()
138 inputData.extend(
'starttime',
'time stamp')
139 inputData.extend(
'stoptime',
'time stamp')
140 inputData.extend(
'runnum',
'unsigned int')
141 runs=sorted(runtimedict.keys())
143 (startTimeT,stopTimeT)=runtimedict[runnum]
144 inputData[
'starttime'].setData(startTimeT)
145 inputData[
'stoptime'].setData(stopTimeT)
146 inputData[
'runnum'].setData(
int(runnum))
147 nchanged=schema.tableHandle(c.runsummarytable).dataEditor().updateRows(
'STARTTIME=:starttime,STOPTIME=:stoptime',
'RUNNUM=:runnum',inputData)
148 print(
'run '+
str(runnum)+
' update '+
str(nchanged)+
' row with starttime ,stoptime')
149 print(startTimeT,stopTimeT)
150 totalchanged=totalchanged+nchanged
152 dbsession.transaction().rollback()
154 dbsession.transaction().commit()
155 except Exception
as e:
157 dbsession.transaction().rollback()
159 print(
'total number of rows changed: ',totalchanged)
163 update LUMISUMMARY set INSTLUMI=:delta*INSTLUMI where RUNNUM in (1,3,57,90) 167 dbsession.transaction().
start(
False)
168 schema=dbsession.nominalSchema()
170 raise 'cannot connect to schema' 171 if not schema.existsTable(c.lumisummarytable):
172 raise 'non-existing table '+c.lumisummarytable
173 runliststring=
','.
join([
str(x)
for x
in runnums])
174 print(
'applying delta '+delta+
' on run list '+runliststring)
176 inputData=coral.AttributeList()
177 inputData.extend(
'delta',
'float')
178 inputData[
'delta'].setData(
float(delta))
179 nchanged=schema.tableHandle(c.lumisummarytable).dataEditor().updateRows(
'INSTLUMI=INSTLUMI*:delta',
'RUNNUM in ('+runliststring+
')',inputData)
180 print(
'total number of row changed ',nchanged)
182 dbsession.transaction().rollback()
184 dbsession.transaction().commit()
186 except Exception
as e:
188 dbsession.transaction().rollback()
193 select lsnr,counts from cms_gt_mon.gt_mon_trig_dead_view where runnr=:runnumber and deadcounter='DeadtimeBeamActive' order by lsnr; 194 return result{lumisection:deadtimebeamactive} 199 dbsession.transaction().
start(
True)
200 schema=dbsession.schema(c.gtmonschema)
203 raise Exception(
'cannot connect to schema '+c.gtmonschema)
204 if not schema.existsView(c.gtdeadview):
205 raise Exception(
'non-existing view '+c.gtdeadview)
207 deadOutput=coral.AttributeList()
208 deadOutput.extend(
"lsnr",
"unsigned int")
209 deadOutput.extend(
"deadcount",
"unsigned long long")
211 deadBindVarList=coral.AttributeList()
212 deadBindVarList.extend(
"runnumber",
"unsigned int")
213 deadBindVarList.extend(
"countername",
"string")
214 deadBindVarList[
"runnumber"].setData(
int(runnum))
215 deadBindVarList[
"countername"].setData(
'DeadtimeBeamActive')
217 query=schema.newQuery()
218 query.addToTableList(c.gtdeadview)
219 query.addToOutputList(
'LSNR',
'lsnr')
220 query.addToOutputList(
'COUNTS',
'deadcount')
221 query.setCondition(
'RUNNR=:runnumber AND DEADCOUNTER=:countername',deadBindVarList)
222 query.addToOrderList(
'lsnr')
223 query.defineOutput(deadOutput)
225 cursor=query.execute()
227 cmslsnum=cursor.currentRow()[
'lsnr'].
data()
228 deadcount=cursor.currentRow()[
'deadcount'].
data()
229 result[cmslsnum]=deadcount
233 except Exception
as e:
235 dbsession.transaction().rollback()
240 select LUMISEGMENTNR,DEADTIMEBEAMACTIVE from cms_wbm.LEVEL1_TRIGGER_CONDITIONS where RUNNUMBER=:runnum order by LUMISEGMENTNR; 241 return result{lumisection:deadtimebeamactive} 246 dbsession.transaction().
start(
True)
247 schema=dbsession.nominalSchema()
249 raise Exception(
'cannot connect to schema'+c.wbmschema)
250 if not schema.existsTable(c.wbmdeadtable):
251 raise Exception(
'non-existing table'+c.wbmdeadtable)
253 deadOutput=coral.AttributeList()
254 deadOutput.extend(
"lsnr",
"unsigned int")
255 deadOutput.extend(
"deadcount",
"unsigned long long")
257 deadBindVarList=coral.AttributeList()
258 deadBindVarList.extend(
"runnum",
"unsigned int")
259 deadBindVarList[
"runnum"].setData(
int(runnum))
261 query=schema.newQuery()
262 query.addToTableList(c.wbmdeadtable)
263 query.addToOutputList(
'LUMISEGMENTNR',
'lsnr')
264 query.addToOutputList(
'DEADTIMEBEAMACTIVE',
'deadcount')
265 query.setCondition(
'RUNNUMBER=:runnum',deadBindVarList)
266 query.addToOrderList(
'LUMISEGMENTNR')
267 query.defineOutput(deadOutput)
269 cursor=query.execute()
271 cmslsnum=cursor.currentRow()[
'lsnr'].
data()
272 deadcount=cursor.currentRow()[
'deadcount'].
data()
273 result[cmslsnum]=deadcount
277 except Exception
as e:
279 dbsession.transaction().rollback()
284 input: deadtimeDict{ls:deadtimebeamactive} 286 update TRG set DEADTIME=:deadtimebeamactive where RUNNUM=:runnum and CMSLSNUM=:lsnum 287 output: number of rows changed 291 dbsession.transaction().
start(
False)
292 schema=dbsession.nominalSchema()
294 raise Exception(
'cannot connect to schema ')
295 if not schema.existsTable(c.lumitrgtable):
296 raise Exception(
'non-existing table '+c.lumitrgtable)
297 for lsnum,deadtimebeamactive
in deadtimeDict.items():
299 inputData=coral.AttributeList()
300 inputData.extend(
'deadtimebeamactive',
'unsigned int')
301 inputData.extend(
'runnum',
'unsigned int')
302 inputData.extend(
'lsnum',
'unsigned int')
303 inputData[
'deadtimebeamactive'].setData(deadtimebeamactive)
304 inputData[
'runnum'].setData(runnum)
305 inputData[
'lsnum'].setData(lsnum)
306 nchanged=schema.tableHandle(c.lumitrgtable).dataEditor().updateRows(
'DEADTIME=:deadtimebeamactive',
'RUNNUM=:runnum AND CMSLSNUM=:lsnum',inputData)
307 print(
'rows changed for ls ',
str(lsnum),
str(nchanged))
308 totalchanged+=nchanged
309 dbsession.transaction().commit()
311 except Exception
as e:
313 dbsession.transaction().rollback()
318 parser = argparse.ArgumentParser(prog=os.path.basename(sys.argv[0]),description=
"Patch LumiData")
319 parser.add_argument(
'-c',dest=
'destination',action=
'store',required=
True,help=
'destination lumi db (required)')
320 parser.add_argument(
'-s',dest=
'source',action=
'store',required=
False,help=
'source db (required except for lumicalib)')
321 parser.add_argument(
'-P',dest=
'authpath',action=
'store',required=
True,help=
'path to authentication file (required)')
322 parser.add_argument(
'-r',dest=
'runnumber',action=
'store',required=
False,help=
'run number (optional)')
323 parser.add_argument(
'-i',dest=
'inputfile',action=
'store',required=
False,help=
'run selection file(optional)')
324 parser.add_argument(
'-delta',dest=
'delta',action=
'store',required=
False,help=
'calibration factor wrt old data in lumiDB (required for lumicalib)')
325 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')
326 parser.add_argument(
'--dryrun',dest=
'dryrun',action=
'store_true',help=
'only print datasource query result, do not update destination')
328 parser.add_argument(
'--debug',dest=
'debug',action=
'store_true',help=
'debug')
329 args=parser.parse_args()
330 runnumber=args.runnumber
331 destConnect=args.destination
332 sourceConnect=args.source
333 if args.authpath
and len(args.authpath)!=0:
334 os.environ[
'CORAL_AUTH_PATH']=args.authpath
335 svc=coral.ConnectionService()
338 sourcesession=svc.connect(sourceConnect,accessMode=coral.access_ReadOnly)
339 sourcesession.typeConverter().setCppTypeForSqlType(
"unsigned int",
"NUMBER(10)")
340 sourcesession.typeConverter().setCppTypeForSqlType(
"unsigned long long",
"NUMBER(20)")
341 destsession=svc.connect(destConnect,accessMode=coral.access_Update)
342 destsession.typeConverter().setCppTypeForSqlType(
"unsigned int",
"NUMBER(10)")
343 destsession.typeConverter().setCppTypeForSqlType(
"unsigned long long",
"NUMBER(20)")
345 msg=coral.MessageStream(
'')
346 msg.setMsgVerbosity(coral.message_Level_Debug)
354 if args.action ==
'deadtimeGT':
355 if not sourceConnect:
356 raise Exception(
'deadtimeGT action requies -s option for source connection string')
358 print(
'reading from ',sourceConnect)
359 print(
'run : ',runnumber)
360 print(
'LS:deadtimebeamactive')
362 if deadresult
and len(deadresult)!=0:
363 for cmsls,deadtimebeamactive
in deadresult.items():
364 print(cmsls,deadtimebeamactive)
366 print(
'no deadtime found for run ',runnumber)
369 print(
'total LS: ',len(deadresult))
371 if len(deadresult)!=
max( [ x
for x
in deadresult.keys() ] ):
372 print(
'total ls: ',len(deadresult))
374 print(
'alert: missing Lumi Sections in the middle')
375 for x
in range(1,
max( [ x
for x
in deadresult.keys()] ) ):
376 if x
not in deadresult:
377 print(
'filling up LS deadtime with 0: LS : ',x)
381 print(
'updating ',destConnect)
383 print(
'number of updated rows ',nupdated)
384 elif args.action ==
'deadtimeWBM':
385 if not sourceConnect:
386 raise Exception(
'deadtimeWBM action requies -s option for source connection string')
388 print(
'reading from ',sourceConnect)
389 print(
'run : ',runnumber)
390 print(
'LS:deadtimebeamactive')
392 if deadresult
and len(deadresult)!=0:
393 for cmsls,deadtimebeamactive
in deadresult.items():
394 print(cmsls,deadtimebeamactive)
396 print(
'no deadtime found for run ',runnumber)
399 print(
'total LS: ',len(deadresult))
400 if len(deadresult)!=
max( [ (deadresult[x],x)
for x
in deadresult])[1]:
401 print(
'alert: missing Lumi Sections in the middle')
402 for x
in range(1,
max( [ (deadresult[x],x)
for x
in deadresult])[1]):
403 if x
not in deadresult:
404 print(
'filling up LS deadtime with 0: LS : ',x)
408 print(
'updating ',destConnect)
410 print(
'number of updated rows ',nupdated)
411 elif args.action ==
'lumicalib':
412 if not args.delta
or args.delta==0:
413 raise Exception(
'Must provide non-zero -delta argument')
416 runnums.append(args.runnumber)
418 basename,extension=os.path.splitext(args.inputfile)
419 if extension==
'.csv':
422 f=open(args.inputfile,
'r') 423 inputfilecontent=f.read() 425 if not fileparsingResult:
426 raise Exception(
'failed to parse the input file '+ifilename)
428 runnums=fileparsingResult.runs()
431 raise Exception(
'Must provide -r or -i argument as input')
433 elif args.action ==
'runtimestamp':
434 if not sourceConnect:
435 raise Exception(
'runtimestamp action requies -s option for source connection string')
436 if not args.runnumber
and not args.inputfile:
438 print(
'these runs miss start/stop time: ',runnums)
439 print(
'total : ',len(runnums))
441 runnums=[
int(args.runnumber)]
443 basename,extension=os.path.splitext(args.inputfile)
444 if extension==
'.csv':
447 f=open(args.inputfile,
'r') 448 inputfilecontent=f.read() 450 if not fileparsingResult:
451 raise Exception(
'failed to parse the input file '+ifilename)
452 runnums=fileparsingResult.runs()
467 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)