00001
00002 VERSION='1.02'
00003 import os,sys
00004 import re
00005 import coral
00006 from RecoLuminosity.LumiDB import argparse
00007
00008 class constants(object):
00009 def __init__(self):
00010 self.debug=False
00011 self.runinfodb=''
00012 self.runinfoschema='CMS_RUNINFO'
00013 self.runsessionparameterTable='RUNSESSION_PARAMETER'
00014 self.hltconfname='CMS.LVL0:HLT_KEY_DESCRIPTION'
00015 self.tsckeyname='CMS.TRG:TSC_KEY'
00016 self.fillnumname='CMS.SCAL:FILLN'
00017 def fillnumForRun(dbsession,c,runnum):
00018 '''select string_value from cms_runinfo.runsession_parameter where runnumber=129265 and name='CMS.SCAL:FILLN' and rownum<=1;
00019
00020 '''
00021 result=''
00022 try:
00023 dbsession.transaction().start(True)
00024 schema=dbsession.schema(c.runinfoschema)
00025 if not schema:
00026 raise Exception, 'cannot connect to schema '+c.runinfoschema
00027 if not schema.existsTable(c.runsessionparameterTable):
00028 raise Exception, 'non-existing table '+c.runsessionparameterTable
00029
00030 fillOutput=coral.AttributeList()
00031 fillOutput.extend("fillnum","string")
00032
00033 bindVarList=coral.AttributeList()
00034 bindVarList.extend("name","string")
00035 bindVarList.extend("runnumber","unsigned int")
00036
00037 bindVarList["name"].setData(c.fillnumname)
00038 bindVarList["runnumber"].setData(int(runnum))
00039
00040 query=schema.newQuery()
00041 query.addToTableList(c.runsessionparameterTable)
00042 query.addToOutputList('STRING_VALUE','value')
00043 query.setCondition('NAME=:name AND RUNNUMBER=:runnumber',bindVarList)
00044 query.limitReturnedRows(1)
00045 query.defineOutput(fillOutput)
00046
00047 cursor=query.execute()
00048 while cursor.next():
00049 result=cursor.currentRow()['fillnum'].data()
00050 del query
00051 dbsession.transaction().commit()
00052
00053 return result
00054 except Exception,e:
00055 print str(e)
00056 dbsession.transaction().rollback()
00057 del dbsession
00058
00059 def hltkeyForRun(dbsession,c,runnum):
00060 '''
00061 select runnumber,string_value from cms_runinfo.runsession_parameter where name=:runsessionparametername and runnumber=:runnum
00062 '''
00063 result={}
00064 try:
00065 dbsession.transaction().start(True)
00066 schema=dbsession.schema(c.runinfoschema)
00067 if not schema:
00068 raise Exception, 'cannot connect to schema '+c.runinfoschema
00069 if not schema.existsTable(c.runsessionparameterTable):
00070 raise Exception, 'non-existing table '+c.runsessionparameterTable
00071
00072 hltkeyOutput=coral.AttributeList()
00073 hltkeyOutput.extend("runnum","unsigned int")
00074 hltkeyOutput.extend("hltkey","string")
00075
00076 bindVarList=coral.AttributeList()
00077 bindVarList.extend("name","string")
00078 bindVarList.extend("runnumber","unsigned int")
00079
00080 bindVarList["name"].setData(c.hltconfname)
00081 bindVarList["runnumber"].setData(int(runnum))
00082
00083 query=schema.newQuery()
00084 query.addToTableList(c.runsessionparameterTable)
00085 query.addToOutputList('RUNNUMBER','runnumber')
00086 query.addToOutputList('STRING_VALUE','value')
00087 query.setCondition('NAME=:name AND RUNNUMBER=:runnumber',bindVarList)
00088 query.defineOutput(hltkeyOutput)
00089
00090 cursor=query.execute()
00091 while cursor.next():
00092 runnum=cursor.currentRow()['runnum'].data()
00093 hltkey=cursor.currentRow()['hltkey'].data()
00094 result[runnum]=hltkey
00095 del query
00096 dbsession.transaction().commit()
00097
00098 return result
00099 except Exception,e:
00100 print str(e)
00101 dbsession.transaction().rollback()
00102 del dbsession
00103
00104 def l1keyForRun(dbsession,c,runnum):
00105 '''
00106 select runnumber,string_value from cms_runinfo.runsession_parameter where name=:runsessionparametername and runnumber=:runnum
00107 '''
00108 result={}
00109 try:
00110 dbsession.transaction().start(True)
00111 schema=dbsession.schema(c.runinfoschema)
00112 if not schema:
00113 raise Exception, 'cannot connect to schema '+c.runinfoschema
00114 if not schema.existsTable(c.runsessionparameterTable):
00115 raise Exception, 'non-existing table '+c.runsessionparameterTable
00116
00117 l1keyOutput=coral.AttributeList()
00118 l1keyOutput.extend("runnum","unsigned int")
00119 l1keyOutput.extend("l1key","string")
00120
00121 bindVarList=coral.AttributeList()
00122 bindVarList.extend("name","string")
00123 bindVarList.extend("runnumber","unsigned int")
00124
00125 bindVarList["name"].setData(c.tsckeyname)
00126 bindVarList["runnumber"].setData(int(runnum))
00127
00128 query=schema.newQuery()
00129 query.addToTableList(c.runsessionparameterTable)
00130 query.addToOutputList('RUNNUMBER','runnumber')
00131 query.addToOutputList('STRING_VALUE','value')
00132 query.setCondition('NAME=:name AND RUNNUMBER=:runnumber',bindVarList)
00133 query.defineOutput(l1keyOutput)
00134
00135 cursor=query.execute()
00136 while cursor.next():
00137 runnum=cursor.currentRow()['runnum'].data()
00138 l1key=cursor.currentRow()['l1key'].data()
00139 result[runnum]=l1key
00140 del query
00141 dbsession.transaction().commit()
00142
00143 return result
00144 except Exception,e:
00145 print str(e)
00146 dbsession.transaction().rollback()
00147 del dbsession
00148
00149 def main():
00150 c=constants()
00151 parser = argparse.ArgumentParser(prog=os.path.basename(sys.argv[0]),description="Dump Run info")
00152 parser.add_argument('-c',dest='connect',action='store',required=True,help='connect string to trigger DB(required)')
00153 parser.add_argument('-P',dest='authpath',action='store',required=True,help='path to authentication file')
00154 parser.add_argument('-r',dest='runnumber',action='store',required=True,help='run number')
00155 parser.add_argument('action',choices=['hltkey','l1key','fill'],help='information to show')
00156 parser.add_argument('--debug',dest='debug',action='store_true',help='debug')
00157 parser.add_argument('--collision-only',dest='collisiononly',action='store_true',help='return only collision runs')
00158 args=parser.parse_args()
00159 runnumber=args.runnumber
00160 c.runinfodb=args.connect
00161 if args.authpath and len(args.authpath)!=0:
00162 os.environ['CORAL_AUTH_PATH']=args.authpath
00163 svc=coral.ConnectionService()
00164 session=svc.connect(c.runinfodb,accessMode=coral.access_ReadOnly)
00165 session.typeConverter().setCppTypeForSqlType("unsigned int","NUMBER(10)")
00166 session.typeConverter().setCppTypeForSqlType("unsigned long long","NUMBER(20)")
00167 if args.debug:
00168 msg=coral.MessageStream('')
00169 msg.setMsgVerbosity(coral.message_Level_Debug)
00170
00171 if args.action == 'hltkey':
00172 p=re.compile(r'^/cdaq/physics/.+')
00173 result=hltkeyForRun(session,c,runnumber)
00174 print 'runnumber hltkey'
00175 for runnum,hltkey in result.items():
00176 if not args.collisiononly:
00177 print runnum,hltkey
00178 if args.collisiononly and re.match(p,hltkey):
00179 fillnum=fillnumForRun(session,c,runnumber)
00180 if len(fillnum)!=0:
00181 print runnum,hltkey
00182 if args.action == 'l1key':
00183 p=re.compile(r'^TSC_.+_collisions_.+')
00184 result=l1keyForRun(session,c,runnumber)
00185 print 'runnumber tsc_key'
00186 for runnum,l1key in result.items():
00187 if not args.collisiononly:
00188 print runnum,l1key
00189 if args.collisiononly and re.match(p,l1key):
00190 fillnum=fillnumForRun(session,c,runnumber)
00191 if len(fillnum)!=0:
00192 print runnum,l1key
00193 if args.action == 'fill':
00194 result=fillnumForRun(session,c,runnumber)
00195 print 'runnumber fill'
00196 if not args.collisiononly:
00197 print runnumber,result
00198 else:
00199 if len(result)!=0:
00200 print runnumber,result
00201 del session
00202 del svc
00203
00204 if __name__=='__main__':
00205 main()
00206