CMS 3D CMS Logo

Classes | Functions | Variables

lumiPatch Namespace Reference

Classes

class  constants

Functions

def addTimeForRun
def getTimeForRun
def GTdeadtimeBeamActiveForRun
def main
def missingTimeRuns
def patchDeadtimeForRun
def recalibrateLumiForRun
def WBMdeadtimeBeamActiveForRun

Variables

string VERSION = '1.00'

Function Documentation

def lumiPatch::addTimeForRun (   dbsession,
  c,
  runtimedict 
)
Input runtimedict{runnumber:(startTimeT,stopTimeT)}
update CMSRUNSUMMARY set STARTTIME=:starttime,STOPTIME=:stoptime where RUNNUM=:runnum
#update CMSRUNSUMMARY set STOPTIME=:stoptime where RUNNUM=:runnum

Definition at line 121 of file lumiPatch.py.

00122                                           :
00123     '''
00124     Input runtimedict{runnumber:(startTimeT,stopTimeT)}
00125     update CMSRUNSUMMARY set STARTTIME=:starttime,STOPTIME=:stoptime where RUNNUM=:runnum
00126     #update CMSRUNSUMMARY set STOPTIME=:stoptime where RUNNUM=:runnum
00127     '''
00128     nchanged=0
00129     totalchanged=0
00130     try:
00131         dbsession.transaction().start(False)
00132         schema=dbsession.nominalSchema()
00133         if not schema:
00134             raise 'cannot connect to schema'
00135         if not schema.existsTable(c.runsummarytable):
00136             raise 'non-existing table '+c.runsummarytable
00137         inputData=coral.AttributeList()
00138         inputData.extend('starttime','time stamp')
00139         inputData.extend('stoptime','time stamp')
00140         inputData.extend('runnum','unsigned int')
00141         runs=runtimedict.keys()
00142         runs.sort()
00143         for runnum in runs:
00144             (startTimeT,stopTimeT)=runtimedict[runnum]
00145             inputData['starttime'].setData(startTimeT)
00146             inputData['stoptime'].setData(stopTimeT)
00147             inputData['runnum'].setData(int(runnum))
00148             nchanged=schema.tableHandle(c.runsummarytable).dataEditor().updateRows('STARTTIME=:starttime,STOPTIME=:stoptime','RUNNUM=:runnum',inputData)
00149             print 'run '+str(runnum)+' update '+str(nchanged)+' row  with starttime ,stoptime'
00150             print startTimeT,stopTimeT
00151             totalchanged=totalchanged+nchanged
00152         if c.isdryrun:
00153             dbsession.transaction().rollback()
00154         else:
00155             dbsession.transaction().commit()   
00156     except Exception,e:
00157         print str(e)
00158         dbsession.transaction().rollback()
00159         del dbsession
00160     print 'total number of rows changed: ',totalchanged
    
def lumiPatch::getTimeForRun (   dbsession,
  c,
  runnums 
)
get start stop time of run from runinfo database
select time from cms_runinfo.runsession_parameter where runnumber=:runnum and name='CMS.LVL0:START_TIME_T';
select time from cms_runinfo.runsession_parameter where runnumber=:runnum and name='CMS.LVL0:STOP_TIME_T';

Definition at line 59 of file lumiPatch.py.

00060                                       :
00061     '''
00062     get start stop time of run from runinfo database
00063     select time from cms_runinfo.runsession_parameter where runnumber=:runnum and name='CMS.LVL0:START_TIME_T';
00064     select time from cms_runinfo.runsession_parameter where runnumber=:runnum and name='CMS.LVL0:STOP_TIME_T';
00065     '''
00066     result={}#{runnum:(starttime,stoptime)}
00067     tableName='RUNSESSION_PARAMETER'
00068     try:
00069         dbsession.transaction().start(True)
00070         schema=dbsession.nominalSchema()
00071         if not schema:
00072             raise 'cannot connect to schema '
00073         if not schema.existsTable(tableName):
00074             raise 'non-existing table '+tableName
00075         
00076         startTime=''
00077         stopTime=''
00078         for runnum in runnums:
00079             startTQuery=schema.newQuery()
00080             startTQuery.addToTableList(tableName)
00081             startTQuery.addToOutputList('TIME','starttime')
00082             stopTQuery=schema.newQuery()
00083             stopTQuery.addToTableList(tableName)
00084             stopTQuery.addToOutputList('TIME','stoptime')
00085             startTQueryCondition=coral.AttributeList()
00086             stopTQueryCondition=coral.AttributeList()
00087             startTQueryOutput=coral.AttributeList()
00088             stopTQueryOutput=coral.AttributeList()
00089             startTQueryCondition.extend('runnum','unsigned int')
00090             startTQueryCondition.extend('name','string')
00091             startTQueryOutput.extend('starttime','time stamp')
00092             stopTQueryCondition.extend('runnum','unsigned int')
00093             stopTQueryCondition.extend('name','string')
00094             stopTQueryOutput.extend('stoptime','time stamp')
00095             startTQueryCondition['runnum'].setData(int(runnum))
00096             startTQueryCondition['name'].setData('CMS.LVL0:START_TIME_T')
00097             startTQuery.setCondition('RUNNUMBER=:runnum AND NAME=:name',startTQueryCondition)
00098             startTQuery.defineOutput(startTQueryOutput)
00099             startTCursor=startTQuery.execute()
00100             while startTCursor.next():
00101                 startTime=startTCursor.currentRow()['starttime'].data()           
00102             stopTQueryCondition['runnum'].setData(int(runnum))
00103             stopTQueryCondition['name'].setData('CMS.LVL0:STOP_TIME_T')
00104             stopTQuery.setCondition('RUNNUMBER=:runnum AND NAME=:name',stopTQueryCondition)
00105             stopTQuery.defineOutput(stopTQueryOutput)
00106             stopTCursor=stopTQuery.execute()
00107             while stopTCursor.next():
00108                 stopTime=stopTCursor.currentRow()['stoptime'].data()
00109             if not startTime or not stopTime:
00110                 print 'Warning: no startTime or stopTime found for run ',runnum
00111             else:    
00112                 result[runnum]=(startTime,stopTime)
00113             del startTQuery
00114             del stopTQuery
00115         dbsession.transaction().commit()
00116     except Exception,e:
00117         print str(e)
00118         dbsession.transaction().rollback()
00119         del dbsession
00120     return result

def lumiPatch::GTdeadtimeBeamActiveForRun (   dbsession,
  c,
  runnum 
)
select lsnr,counts from cms_gt_mon.gt_mon_trig_dead_view where runnr=:runnumber and deadcounter='DeadtimeBeamActive' order by lsnr;
return result{lumisection:deadtimebeamactive}

Definition at line 191 of file lumiPatch.py.

00192                                                   :
00193     '''
00194     select lsnr,counts from cms_gt_mon.gt_mon_trig_dead_view where runnr=:runnumber and deadcounter='DeadtimeBeamActive' order by lsnr;
00195     return result{lumisection:deadtimebeamactive}
00196     
00197     '''
00198     result={}
00199     try:
00200         dbsession.transaction().start(True)
00201         schema=dbsession.schema(c.gtmonschema)
00202 
00203         if not schema:
00204             raise Exception('cannot connect to schema '+c.gtmonschema)
00205         if not schema.existsView(c.gtdeadview):
00206             raise Exception('non-existing view '+c.gtdeadview)
00207 
00208         deadOutput=coral.AttributeList()
00209         deadOutput.extend("lsnr","unsigned int")
00210         deadOutput.extend("deadcount","unsigned long long")
00211         
00212         deadBindVarList=coral.AttributeList()
00213         deadBindVarList.extend("runnumber","unsigned int")
00214         deadBindVarList.extend("countername","string")
00215         deadBindVarList["runnumber"].setData(int(runnum))
00216         deadBindVarList["countername"].setData('DeadtimeBeamActive')
00217         
00218         query=schema.newQuery()
00219         query.addToTableList(c.gtdeadview)
00220         query.addToOutputList('LSNR','lsnr')
00221         query.addToOutputList('COUNTS','deadcount')
00222         query.setCondition('RUNNR=:runnumber AND DEADCOUNTER=:countername',deadBindVarList)
00223         query.addToOrderList('lsnr')
00224         query.defineOutput(deadOutput)
00225 
00226         cursor=query.execute()
00227         while cursor.next():
00228             cmslsnum=cursor.currentRow()['lsnr'].data()
00229             deadcount=cursor.currentRow()['deadcount'].data()
00230             result[cmslsnum]=deadcount
00231             #print 'deadcount',deadcount
00232         del query
00233         return result
00234     except Exception,e:
00235         print str(e)
00236         dbsession.transaction().rollback()
00237         del dbsession
        
def lumiPatch::main ( )

Definition at line 316 of file lumiPatch.py.

00317           :
00318     c=constants()
00319     parser = argparse.ArgumentParser(prog=os.path.basename(sys.argv[0]),description="Patch LumiData")
00320     parser.add_argument('-c',dest='destination',action='store',required=True,help='destination lumi db (required)')
00321     parser.add_argument('-s',dest='source',action='store',required=False,help='source db (required except for lumicalib)')
00322     parser.add_argument('-P',dest='authpath',action='store',required=True,help='path to authentication file (required)')
00323     parser.add_argument('-r',dest='runnumber',action='store',required=False,help='run number (optional)')
00324     parser.add_argument('-i',dest='inputfile',action='store',required=False,help='run selection file(optional)')
00325     parser.add_argument('-delta',dest='delta',action='store',required=False,help='calibration factor wrt old data in lumiDB (required for lumicalib)')
00326     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')
00327     parser.add_argument('--dryrun',dest='dryrun',action='store_true',help='only print datasource query result, do not update destination')
00328     
00329     parser.add_argument('--debug',dest='debug',action='store_true',help='debug')
00330     args=parser.parse_args()
00331     runnumber=args.runnumber
00332     destConnect=args.destination
00333     sourceConnect=args.source
00334     if args.authpath and len(args.authpath)!=0:
00335         os.environ['CORAL_AUTH_PATH']=args.authpath
00336     svc=coral.ConnectionService()
00337     sourcesession=None
00338     if sourceConnect:
00339         sourcesession=svc.connect(sourceConnect,accessMode=coral.access_ReadOnly)
00340         sourcesession.typeConverter().setCppTypeForSqlType("unsigned int","NUMBER(10)")
00341         sourcesession.typeConverter().setCppTypeForSqlType("unsigned long long","NUMBER(20)")
00342     destsession=svc.connect(destConnect,accessMode=coral.access_Update)
00343     destsession.typeConverter().setCppTypeForSqlType("unsigned int","NUMBER(10)")
00344     destsession.typeConverter().setCppTypeForSqlType("unsigned long long","NUMBER(20)")
00345     if args.debug:
00346         msg=coral.MessageStream('')
00347         msg.setMsgVerbosity(coral.message_Level_Debug)
00348     if args.dryrun:
00349         c.isdryrun=True
00350     else:
00351         c.isdryrun=False
00352         
00353     deadresult={}
00354 
00355     if args.action == 'deadtimeGT':
00356         if not sourceConnect:
00357             raise Exception('deadtimeGT action requies -s option for source connection string')
00358         deadresult=GTdeadtimeBeamActiveForRun(sourcesession,c,runnumber)
00359         print 'reading from ',sourceConnect
00360         print 'run : ',runnumber
00361         print 'LS:deadtimebeamactive'
00362         #print deadresult
00363         if deadresult and len(deadresult)!=0:
00364             for cmsls,deadtimebeamactive in deadresult.items():
00365                 print cmsls,deadtimebeamactive
00366         else:
00367             print 'no deadtime found for run ',runnumber
00368             print 'exit'
00369             return
00370         print 'total LS: ',len(deadresult)
00371 #        if len(deadresult)!=max( [ (deadresult[x],x) for x in deadresult] )[1]:
00372         if len(deadresult)!=max( [ x for x in deadresult.keys() ] ):
00373             print 'total ls: ',len(deadresult)
00374             #print 'max key: ',max( [ x for x in deadresult.keys()])
00375             print 'alert: missing Lumi Sections in the middle'
00376             for x in range(1,max( [ x for x in deadresult.keys()] ) ):
00377                 if not deadresult.has_key(x):
00378                     print 'filling up LS deadtime with 0: LS : ',x
00379                     deadresult[x]=0
00380         #print deadresult
00381         if not args.dryrun:
00382             print 'updating ',destConnect
00383             nupdated=patchDeadtimeForRun(destsession,c,int(runnumber),deadresult)
00384             print 'number of updated rows ',nupdated
00385     elif args.action == 'deadtimeWBM':
00386         if not sourceConnect:
00387             raise Exception('deadtimeWBM action requies -s option for source connection string')
00388         deadresult=WBMdeadtimeBeamActiveForRun(sourcesession,c,runnumber)
00389         print 'reading from ',sourceConnect
00390         print 'run : ',runnumber
00391         print 'LS:deadtimebeamactive'
00392         #print deadresult
00393         if deadresult and len(deadresult)!=0:
00394             for cmsls,deadtimebeamactive in deadresult.items():
00395                 print cmsls,deadtimebeamactive
00396         else:
00397             print 'no deadtime found for run ',runnumber
00398             print 'exit'
00399             return
00400         print 'total LS: ',len(deadresult)
00401         if len(deadresult)!=max( [ (deadresult[x],x) for x in deadresult])[1]:
00402             print 'alert: missing Lumi Sections in the middle'
00403             for x in range(1,max( [ (deadresult[x],x) for x in deadresult])[1]):
00404                 if not deadresult.has_key(x):
00405                     print 'filling up LS deadtime with 0: LS : ',x
00406                     deadresult[x]=0
00407         print deadresult
00408         if not args.dryrun:
00409             print 'updating ',destConnect
00410             nupdated=patchDeadtimeForRun(destsession,c,int(runnumber),deadresult)
00411             print 'number of updated rows ',nupdated
00412     elif args.action == 'lumicalib':
00413         if not args.delta or args.delta==0:
00414             raise Exception('Must provide non-zero -delta argument')
00415         runnums=[]
00416         if args.runnumber:
00417             runnums.append(args.runnumber)
00418         elif args.inputfile:
00419             basename,extension=os.path.splitext(args.inputfile)
00420             if extension=='.csv':#if file ends with .csv,use csv parser,else parse as json file
00421                 fileparsingResult=csvSelectionParser.csvSelectionParser(args.inputfile)            
00422             else:
00423                 f=open(args.inputfile,'r')
00424                 inputfilecontent=f.read()
00425                 fileparsingResult=selectionParser.selectionParser(inputfilecontent)
00426             if not fileparsingResult:
00427                 raise Exception('failed to parse the input file '+ifilename)
00428             #print fileparsingResult.runsandls()
00429             runnums=fileparsingResult.runs()
00430             #print runnums
00431         else:
00432             raise Exception('Must provide -r or -i argument as input')
00433         nupdated=recalibrateLumiForRun(destsession,c,args.delta,runnums)
00434     elif args.action == 'runtimestamp':
00435         if not sourceConnect:
00436             raise Exception('runtimestamp action requies -s option for source connection string')
00437         if not args.runnumber and not args.inputfile: #if no runnumber nor input file specified, check all
00438             runnums=missingTimeRuns(destsession,c)
00439             print 'these runs miss start/stop time: ',runnums
00440             print 'total : ',len(runnums)
00441         elif args.runnumber:
00442             runnums=[int(args.runnumber)]
00443         elif args.inputfile:
00444             basename,extension=os.path.splitext(args.inputfile)
00445             if extension=='.csv':#if file ends with .csv,use csv parser,else parse as json file
00446                 fileparsingResult=csvSelectionParser.csvSelectionParser(args.inputfile)            
00447             else:
00448                 f=open(args.inputfile,'r')
00449                 inputfilecontent=f.read()
00450                 fileparsingResult=selectionParser.selectionParser(inputfilecontent)
00451             if not fileparsingResult:
00452                 raise Exception('failed to parse the input file '+ifilename)
00453             runnums=fileparsingResult.runs()
00454         result=getTimeForRun(sourcesession,c,runnums)
00455         #for run,(startTimeT,stopTimeT) in result.items():
00456             #print 'run: ',run
00457             #if not startTimeT or not stopTimeT:
00458                 #print 'None'
00459             #else:
00460                 #print 'start: ',startTimeT
00461                 #print 'stop: ',stopTimeT
00462         addTimeForRun(destsession,c,result)
00463     if sourcesession:  
00464         del sourcesession
00465     del destsession
00466     del svc
        
def lumiPatch::missingTimeRuns (   dbsession,
  c 
)
return all the runs with starttime or stoptime column NULL in lumi db
select runnum from CMSRUNSUMMARY where starttime is NULL or stoptime is NULL

Definition at line 28 of file lumiPatch.py.

00029                                 :
00030     '''return all the runs with starttime or stoptime column NULL in lumi db
00031     select runnum from CMSRUNSUMMARY where starttime is NULL or stoptime is NULL
00032     '''
00033     result=[]
00034     try:
00035         emptyBindVarList=coral.AttributeList()
00036         dbsession.transaction().start(True)
00037         schema=dbsession.nominalSchema()
00038         if not schema:
00039             raise 'cannot connect to schema '
00040         if not schema.existsTable(c.runsummarytable):
00041             raise 'non-existing table '+c.runsummarytable
00042         query=schema.newQuery()
00043         query.addToTableList(c.runsummarytable)
00044         query.addToOutputList('RUNNUM','runnum')
00045         query.setCondition('STARTTIME IS NULL AND STOPTIME IS NULL',emptyBindVarList)
00046         query.addToOrderList('runnum')
00047         queryOutput=coral.AttributeList()
00048         queryOutput.extend('runnum','unsigned int')
00049         query.defineOutput(queryOutput)
00050         cursor=query.execute()
00051         while cursor.next():
00052             result.append(cursor.currentRow()['runnum'].data())
00053         del query
00054         dbsession.transaction().commit()
00055     except Exception,e:
00056         print str(e)
00057         dbsession.transaction().rollback()
00058         del dbsession
    return result
def lumiPatch::patchDeadtimeForRun (   dbsession,
  c,
  runnum,
  deadtimeDict 
)
input: deadtimeDict{ls:deadtimebeamactive}
loop over input
update TRG set DEADTIME=:deadtimebeamactive where RUNNUM=:runnum and CMSLSNUM=:lsnum
output: number of rows changed

Definition at line 282 of file lumiPatch.py.

00283                                                         :
00284     '''
00285     input: deadtimeDict{ls:deadtimebeamactive}
00286     loop over input
00287     update TRG set DEADTIME=:deadtimebeamactive where RUNNUM=:runnum and CMSLSNUM=:lsnum
00288     output: number of rows changed
00289     '''
00290     totalchanged=0
00291     try:
00292         dbsession.transaction().start(False)
00293         schema=dbsession.nominalSchema()
00294         if not schema:
00295             raise Exception('cannot connect to schema ')
00296         if not schema.existsTable(c.lumitrgtable):
00297             raise Exception('non-existing table '+c.lumitrgtable)
00298         for lsnum,deadtimebeamactive in deadtimeDict.items():
00299             nchanged=0
00300             inputData=coral.AttributeList()
00301             inputData.extend('deadtimebeamactive','unsigned int')
00302             inputData.extend('runnum','unsigned int')
00303             inputData.extend('lsnum','unsigned int')
00304             inputData['deadtimebeamactive'].setData(deadtimebeamactive)
00305             inputData['runnum'].setData(runnum)
00306             inputData['lsnum'].setData(lsnum)
00307             nchanged=schema.tableHandle(c.lumitrgtable).dataEditor().updateRows('DEADTIME=:deadtimebeamactive','RUNNUM=:runnum AND CMSLSNUM=:lsnum',inputData)
00308             print 'rows changed for ls ',str(lsnum),str(nchanged)
00309             totalchanged+=nchanged
00310         dbsession.transaction().commit()
00311         return totalchanged
00312     except Exception,e:
00313         print str(e)
00314         dbsession.transaction().rollback()
00315         del dbsession
        
def lumiPatch::recalibrateLumiForRun (   dbsession,
  c,
  delta,
  runnums 
)
update LUMISUMMARY set INSTLUMI=:delta*INSTLUMI where RUNNUM in (1,3,57,90)

Definition at line 161 of file lumiPatch.py.

00162                                                     :
00163     '''
00164     update LUMISUMMARY set INSTLUMI=:delta*INSTLUMI where RUNNUM in (1,3,57,90)
00165     '''
00166     updaterows=0
00167     try:
00168         dbsession.transaction().start(False)
00169         schema=dbsession.nominalSchema()
00170         if not schema:
00171             raise 'cannot connect to schema'
00172         if not schema.existsTable(c.lumisummarytable):
00173             raise 'non-existing table '+c.lumisummarytable
00174         runliststring=','.join([str(x) for x in runnums])
00175         print 'applying delta '+delta+' on run list '+runliststring
00176         nchanged=0
00177         inputData=coral.AttributeList()
00178         inputData.extend('delta','float')
00179         inputData['delta'].setData(float(delta))
00180         nchanged=schema.tableHandle(c.lumisummarytable).dataEditor().updateRows('INSTLUMI=INSTLUMI*:delta','RUNNUM in ('+runliststring+')',inputData)
00181         print 'total number of row changed ',nchanged
00182         if c.isdryrun:
00183             dbsession.transaction().rollback()
00184         else:
00185             dbsession.transaction().commit()
00186         return nchanged
00187     except Exception,e:
00188         print str(e)
00189         dbsession.transaction().rollback()
00190         del dbsession
        
def lumiPatch::WBMdeadtimeBeamActiveForRun (   dbsession,
  c,
  runnum 
)
select  LUMISEGMENTNR,DEADTIMEBEAMACTIVE from cms_wbm.LEVEL1_TRIGGER_CONDITIONS where RUNNUMBER=:runnum order by LUMISEGMENTNR;
return result{lumisection:deadtimebeamactive}

Definition at line 238 of file lumiPatch.py.

00239                                                    :
00240     '''
00241     select  LUMISEGMENTNR,DEADTIMEBEAMACTIVE from cms_wbm.LEVEL1_TRIGGER_CONDITIONS where RUNNUMBER=:runnum order by LUMISEGMENTNR;
00242     return result{lumisection:deadtimebeamactive}
00243     
00244     '''
00245     result={}
00246     try:
00247         dbsession.transaction().start(True)
00248         schema=dbsession.nominalSchema()
00249         if not schema:
00250             raise Exception('cannot connect to schema'+c.wbmschema)
00251         if not schema.existsTable(c.wbmdeadtable):
00252             raise Exception('non-existing table'+c.wbmdeadtable)
00253 
00254         deadOutput=coral.AttributeList()
00255         deadOutput.extend("lsnr","unsigned int")
00256         deadOutput.extend("deadcount","unsigned long long")
00257         
00258         deadBindVarList=coral.AttributeList()
00259         deadBindVarList.extend("runnum","unsigned int")
00260         deadBindVarList["runnum"].setData(int(runnum))
00261         
00262         query=schema.newQuery()
00263         query.addToTableList(c.wbmdeadtable)
00264         query.addToOutputList('LUMISEGMENTNR','lsnr')
00265         query.addToOutputList('DEADTIMEBEAMACTIVE','deadcount')
00266         query.setCondition('RUNNUMBER=:runnum',deadBindVarList)
00267         query.addToOrderList('LUMISEGMENTNR')
00268         query.defineOutput(deadOutput)
00269         
00270         cursor=query.execute()
00271         while cursor.next():
00272             cmslsnum=cursor.currentRow()['lsnr'].data()
00273             deadcount=cursor.currentRow()['deadcount'].data()
00274             result[cmslsnum]=deadcount
00275             #print 'deadcount',deadcount
00276         del query
00277         return result
00278     except Exception,e:
00279         print str(e)
00280         dbsession.transaction().rollback()
00281         del dbsession
        

Variable Documentation

string lumiPatch::VERSION = '1.00'

Definition at line 2 of file lumiPatch.py.