00001
00002 VERSION='1.00'
00003 import os,sys,datetime
00004 import coral
00005 from RecoLuminosity.LumiDB import lumiTime,argparse,nameDealer,selectionParser,hltTrgSeedMapper,connectstrParser,cacheconfigParser,matplotRender,lumiQueryAPI,inputFilesetParser,CommonUtil,csvReporter
00006 from matplotlib.figure import Figure
00007
00008 class constants(object):
00009 def __init__(self):
00010 self.NORM=1.0
00011 self.LUMIVERSION='0001'
00012 self.BEAMMODE='stable'
00013 self.VERBOSE=False
00014 def defaultfrontierConfigString(self):
00015 return """<frontier-connect><proxy url="https://cmst0frontier.cern.ch:3128"/><proxy url="https://cmst0frontier.cern.ch:3128"/><proxy url="https://cmst0frontier1.cern.ch:3128"/><proxy url="https://cmst0frontier2.cern.ch:3128"/><server url="https://cmsfrontier.cern.ch:8000/FrontierInt"/><server url="https://cmsfrontier.cern.ch:8000/FrontierInt"/><server url="https://cmsfrontier1.cern.ch:8000/FrontierInt"/><server url="https://cmsfrontier2.cern.ch:8000/FrontierInt"/><server url="https://cmsfrontier3.cern.ch:8000/FrontierInt"/><server url="https://cmsfrontier4.cern.ch:8000/FrontierInt"/></frontier-connect>"""
00016
00017 def getInstLumiPerLS(dbsession,c,runList,selectionDict,beamstatus=None,beamenergy=None,beamenergyfluctuation=0.09):
00018 '''
00019 input: runList[runnum], selectionDict{runnum:[ls]}
00020 output:[[runnumber,lsnumber,deliveredInst,recordedInst,norbit,startorbit,runstarttime,runstoptime]]
00021 '''
00022 result=[]
00023 selectedRunlist=runList
00024 if len(selectionDict)!=0:
00025 selectedRunlist=[]
00026 allruns=runlist+selectionDict.keys()
00027 dups=CommonUtil.count_dups(allruns)
00028 for runnum,dupcount in dups:
00029 if dupcount==2:
00030 selectedRunlist.append(runnum)
00031
00032 dbsession.transaction().start(True)
00033 for run in selectedRunlist:
00034 q=dbsession.nominalSchema().newQuery()
00035 runsummary=lumiQueryAPI.runsummaryByrun(q,run)
00036 del q
00037 runstarttime=runsummary[3]
00038 runstoptime=runsummary[4]
00039 q=dbsession.nominalSchema().newQuery()
00040 lumiperrun=lumiQueryAPI.lumisummaryByrun(q,run,c.LUMIVERSION,beamstatus,beamenergy,beamenergyfluctuation)
00041 del q
00042 if len(lumiperrun)==0:
00043 result.append([run,1,0.0,0.0,0,0,runstarttime,runstoptime])
00044 else:
00045 for lumiperls in lumiperrun:
00046 cmslsnum=lumiperls[0]
00047 instlumi=lumiperls[1]
00048 recordedlumi=0.0
00049 numorbit=lumiperls[2]
00050 startorbit=lumiperls[3]
00051 deadcount=0
00052 bitzero=0
00053 result.append([run,cmslsnum,instlumi,recordedlumi,numorbit,startorbit,runstarttime,runstoptime])
00054 dbsession.transaction().commit()
00055 if c.VERBOSE:
00056 print result
00057 return result
00058
00059 def getLumiPerRun(dbsession,c,run,beamstatus=None,beamenergy=None,beamenergyfluctuation=0.09):
00060 '''
00061 input: run
00062 output:{runnumber:[[lsnumber,deliveredInst,recordedInst,norbit,startorbit,runstarttime,runstoptime]]}
00063 '''
00064 result=[]
00065 dbsession.transaction().start(True)
00066 q=dbsession.nominalSchema().newQuery()
00067 runsummary=lumiQueryAPI.runsummaryByrun(q,run)
00068 del q
00069 runstarttime=runsummary[3]
00070 runstoptime=runsummary[4]
00071 fillnum=runsummary[0]
00072 q=dbsession.nominalSchema().newQuery()
00073 lumiperrun=lumiQueryAPI.lumisummaryByrun(q,run,c.LUMIVERSION,beamstatus,beamenergy,beamenergyfluctuation)
00074 del q
00075 q=dbsession.nominalSchema().newQuery()
00076 trgperrun=lumiQueryAPI.trgbitzeroByrun(q,run)
00077 del q
00078
00079 for lumiperls in lumiperrun:
00080 cmslsnum=lumiperls[0]
00081 instlumi=lumiperls[1]
00082 recordedlumi=0.0
00083 numorbit=lumiperls[2]
00084 startorbit=lumiperls[3]
00085 deadcount=0
00086 bitzero=0
00087 if trgperrun.has_key(cmslsnum):
00088 bitzero=trgperrun[cmslsnum][0]
00089 deadcount=trgperrun[cmslsnum][1]
00090 bitzeroprescale=trgperrun[cmslsnum][-1]
00091 try:
00092 recordedlumi=instlumi*(1.0-float(deadcount)/(float(bitzero)*float(bitzeroprescale)))
00093 except ZeroDivisionError:
00094 recordedlumi=-0.1
00095 result.append([cmslsnum,instlumi,recordedlumi,numorbit,startorbit,fillnum,runstarttime,runstoptime])
00096 dbsession.transaction().commit()
00097 if c.VERBOSE:
00098 print result
00099 return result
00100
00101 def main():
00102 allowedscales=['linear','log','both']
00103 c=constants()
00104 parser = argparse.ArgumentParser(prog=os.path.basename(sys.argv[0]),description="Plot integrated luminosity as function of the time variable of choice")
00105
00106 parser.add_argument('-c',dest='connect',action='store',required=True,help='connect string to lumiDB')
00107
00108 parser.add_argument('-P',dest='authpath',action='store',help='path to authentication file')
00109 parser.add_argument('-n',dest='normfactor',action='store',help='normalization factor (optional, default to 1.0)')
00110 parser.add_argument('-i',dest='inputfile',action='store',help='lumi range selection file (optional)')
00111 parser.add_argument('-o',dest='outputfile',action='store',help='csv outputfile name (optional)')
00112 parser.add_argument('-b',dest='beammode',action='store',help='beam mode, optional, no default')
00113 parser.add_argument('-lumiversion',dest='lumiversion',action='store',help='lumi data version, optional for all, default 0001')
00114 parser.add_argument('-begin',dest='begin',action='store',help='begin xvalue (required)')
00115 parser.add_argument('-end',dest='end',action='store',help='end xvalue(optional). Default to the maximum exists DB')
00116 parser.add_argument('-batch',dest='batch',action='store',help='graphical mode to produce PNG file. Specify graphical file here. Default to lumiSum.png')
00117 parser.add_argument('-yscale',dest='yscale',action='store',required=False,default='linear',help='y_scale')
00118 parser.add_argument('--interactive',dest='interactive',action='store_true',help='graphical mode to draw plot in a TK pannel.')
00119 parser.add_argument('-timeformat',dest='timeformat',action='store',help='specific python timeformat string (optional). Default mm/dd/yy hh:min:ss.00')
00120 parser.add_argument('-siteconfpath',dest='siteconfpath',action='store',help='specific path to site-local-config.xml file, default to $CMS_PATH/SITECONF/local/JobConfig, if path undefined, fallback to cern proxy&server')
00121 parser.add_argument('action',choices=['peakperday','run'],help='plot type of choice')
00122
00123 parser.add_argument('--annotateboundary',dest='annotateboundary',action='store_true',help='annotate boundary run numbers')
00124 parser.add_argument('--verbose',dest='verbose',action='store_true',help='verbose mode, print result also to screen')
00125 parser.add_argument('--debug',dest='debug',action='store_true',help='debug')
00126
00127 args=parser.parse_args()
00128 connectstring=args.connect
00129 connectparser=connectstrParser.connectstrParser(connectstring)
00130 connectparser.parse()
00131 usedefaultfrontierconfig=False
00132 cacheconfigpath=''
00133 if connectparser.needsitelocalinfo():
00134 if not args.siteconfpath:
00135 cacheconfigpath=os.environ['CMS_PATH']
00136 if cacheconfigpath:
00137 cacheconfigpath=os.path.join(cacheconfigpath,'SITECONF','local','JobConfig','site-local-config.xml')
00138 else:
00139 usedefaultfrontierconfig=True
00140 else:
00141 cacheconfigpath=args.siteconfpath
00142 cacheconfigpath=os.path.join(cacheconfigpath,'site-local-config.xml')
00143 p=cacheconfigParser.cacheconfigParser()
00144 if usedefaultfrontierconfig:
00145 p.parseString(c.defaultfrontierConfigString)
00146 else:
00147 p.parse(cacheconfigpath)
00148 connectstring=connectparser.fullfrontierStr(connectparser.schemaname(),p.parameterdict())
00149 runnumber=0
00150 svc = coral.ConnectionService()
00151 if args.debug :
00152 msg=coral.MessageStream('')
00153 msg.setMsgVerbosity(coral.message_Level_Debug)
00154 ifilename=''
00155 ofilename='instlumi.csv'
00156 beammode='stable'
00157 timeformat=''
00158 selectionDict={}
00159 if args.authpath and len(args.authpath)!=0:
00160 os.environ['CORAL_AUTH_PATH']=args.authpath
00161 if args.normfactor:
00162 c.NORM=float(args.normfactor)
00163 if args.lumiversion:
00164 c.LUMIVERSION=args.lumiversion
00165 if args.beammode:
00166 c.BEAMMODE=args.beammode
00167 if args.verbose:
00168 c.VERBOSE=True
00169 if args.inputfile:
00170 ifilename=args.inputfile
00171 if args.batch:
00172 opicname=args.batch
00173 if args.outputfile:
00174 ofilename=args.outputfile
00175 if args.timeformat:
00176 timeformat=args.timeformat
00177 session=svc.connect(connectstring,accessMode=coral.access_Update)
00178 session.typeConverter().setCppTypeForSqlType("unsigned int","NUMBER(10)")
00179 session.typeConverter().setCppTypeForSqlType("unsigned long long","NUMBER(20)")
00180 if ifilename:
00181 ifparser=inputFilesetParser(ifilename)
00182 runsandls=ifparser.runsandls()
00183 keylist=runsandls.keys()
00184 keylist.sort()
00185 for run in keylist:
00186 if selectionDict.has_key(run):
00187 lslist=runsandls[run]
00188 lslist.sort()
00189 selectionDict[run]=lslist
00190 if args.action == 'run':
00191 minRun=int(args.begin)
00192 if not args.end:
00193 maxRun=minRun
00194 else:
00195 maxRun=int(args.end)
00196 runList=range(minRun,maxRun+1)
00197 elif args.action == 'peakperday':
00198 session.transaction().start(True)
00199 t=lumiTime.lumiTime()
00200 minTime=t.StrToDatetime(args.begin,timeformat)
00201 if not args.end:
00202 maxTime=datetime.datetime.utcnow()
00203 else:
00204 maxTime=t.StrToDatetime(args.end,timeformat)
00205
00206 qHandle=session.nominalSchema().newQuery()
00207 runDict=lumiQueryAPI.runsByTimerange(qHandle,minTime,maxTime)
00208 session.transaction().commit()
00209 runList=runDict.keys()
00210 del qHandle
00211 runList.sort()
00212 else:
00213 print 'unsupported action ',args.action
00214 exit
00215
00216
00217 fig=Figure(figsize=(6,4.5),dpi=100)
00218 m=matplotRender.matplotRender(fig)
00219
00220 logfig=Figure(figsize=(6,4.5),dpi=100)
00221 mlog=matplotRender.matplotRender(logfig)
00222
00223 if args.action == 'peakperday':
00224 l=lumiTime.lumiTime()
00225 lumiperls=getInstLumiPerLS(session,c,runList,selectionDict)
00226 if args.outputfile:
00227 reporter=csvReporter.csvReporter(ofilename)
00228 fieldnames=['day','run','lsnum','maxinstlumi']
00229 reporter.writeRow(fieldnames)
00230
00231
00232 daydict={}
00233 result={}
00234 for lsdata in lumiperls:
00235 runnumber=lsdata[0]
00236 lsnum=lsdata[1]
00237 runstarttimeStr=lsdata[-2]
00238 startorbit=lsdata[5]
00239 deliveredInst=lsdata[2]
00240 lsstarttime=l.OrbitToTime(runstarttimeStr,startorbit)
00241 day=lsstarttime.toordinal()
00242 if not daydict.has_key(day):
00243 daydict[day]=[]
00244 daydict[day].append([runnumber,lsnum,deliveredInst])
00245 days=daydict.keys()
00246 days.sort()
00247 for day in days:
00248 daydata=daydict[day]
00249 transposeddata=CommonUtil.transposed(daydata,defaultval=0.0)
00250 todaysmaxinst=max(transposeddata[2])
00251 todaysmaxidx=transposeddata[2].index(todaysmaxinst)
00252 todaysmaxrun=transposeddata[0][todaysmaxidx]
00253 todaysmaxls=transposeddata[1][todaysmaxidx]
00254 result[day]=[todaysmaxrun,todaysmaxls,todaysmaxinst]
00255 if args.outputfile :
00256 reporter.writeRow([day,todaysmaxrun,todaysmaxls,todaysmaxinst])
00257 m.plotPeakPerday_Time(result,minTime,maxTime,annotateBoundaryRunnum=args.annotateboundary,yscale='linear')
00258 mlog.plotPeakPerday_Time(result,minTime,maxTime,annotateBoundaryRunnum=args.annotateboundary,yscale='log')
00259
00260 if args.action == 'run':
00261 runnumber=runList[0]
00262 lumiperrun=getLumiPerRun(session,c,runnumber)
00263
00264 xdata=[]
00265 ydata={}
00266 ydata['Delivered']=[]
00267 ydata['Recorded']=[]
00268 norbit=lumiperrun[0][3]
00269 fillnum=lumiperrun[0][-3]
00270 starttime=lumiperrun[0][-2]
00271 stoptime=lumiperrun[0][-1]
00272 ncmsls=0
00273 totalls=len(lumiperrun)
00274 for lsdata in lumiperrun:
00275 lsnumber=lsdata[0]
00276 if lsnumber!=0:
00277 ncmsls+=1
00278 deliveredInst=lsdata[1]
00279 recordedInst=lsdata[2]
00280 ydata['Delivered'].append(deliveredInst)
00281 ydata['Recorded'].append(recordedInst)
00282 xdata=[runnumber,fillnum,norbit,starttime,stoptime,totalls,ncmsls]
00283 m.plotInst_RunLS(xdata,ydata)
00284 del session
00285 del svc
00286 if args.batch and args.yscale=='linear':
00287 m.drawPNG(args.batch)
00288 elif args.batch and args.yscale=='log':
00289 mlog.drawPNG(args.batch)
00290 elif args.batch and args.yscale=='both':
00291 m.drawPNG(args.batch)
00292 basename,extension=os.path.splitext(args.batch)
00293 logfilename=basename+'_log'+extension
00294 mlog.drawPNG(logfilename)
00295 else:
00296 raise Exception('unsupported yscale for batch mode : '+args.yscale)
00297
00298 if not args.interactive:
00299 return
00300 if args.interactive is True and args.yscale=='linear':
00301 m.drawInteractive()
00302 elif args.interactive is True and args.yscale=='log':
00303 mlog.drawInteractive()
00304 else:
00305 raise Exception('unsupported yscale for interactive mode : '+args.yscale)
00306
00307 if __name__=='__main__':
00308 main()