00001 '''
00002 Specs:
00003 -- We use matplotlib OO class level api, we do not use its high-level helper modules. Favor endured stability over simplicity.
00004 -- PNG as default batch file format
00005 -- we support https mode by sending string buf via meme type image/png. Sending a premade static plot to webserver is considered a uploading process instead of https dynamic graphical mode.
00006 '''
00007 import sys,os
00008 import numpy,datetime
00009 import matplotlib
00010 from RecoLuminosity.LumiDB import CommonUtil,lumiTime,csvReporter
00011
00012 batchonly=False
00013 if not os.environ.has_key('DISPLAY') or not os.environ['DISPLAY']:
00014 batchonly=True
00015 matplotlib.use('Agg',warn=False)
00016 else:
00017 try:
00018 from RecoLuminosity.LumiDB import lumiQTWidget
00019 except ImportError:
00020 print 'unable to import GUI backend, switch to batch only mode'
00021 matplotlib.use('Agg',warn=False)
00022 batchonly=True
00023 from matplotlib.backends.backend_agg import FigureCanvasAgg as CanvasBackend
00024 from matplotlib.figure import Figure
00025 from matplotlib.font_manager import fontManager,FontProperties
00026 matplotlib.rcParams['lines.linewidth']=1.5
00027 matplotlib.rcParams['grid.linewidth']=0.2
00028 matplotlib.rcParams['xtick.labelsize']=11
00029 matplotlib.rcParams['ytick.labelsize']=11
00030 matplotlib.rcParams['legend.fontsize']=10
00031 matplotlib.rcParams['axes.labelsize']=11
00032 matplotlib.rcParams['font.weight']=567
00033
00034 def guessLumiUnit(t):
00035 '''
00036 input : largest total lumivalue
00037 output: (unitstring,denomitor)
00038 '''
00039 unitstring='$\mu$b$^{-1}$'
00040 denomitor=1.0
00041 if t>=1.0e3 and t<1.0e06:
00042 denomitor=1.0e3
00043 unitstring='nb$^{-1}$'
00044 elif t>=1.0e6 and t<1.0e9:
00045 denomitor=1.0e6
00046 unitstring='pb$^{-1}$'
00047 elif t>=1.0e9 and t<1.0e12:
00048 denomitor=1.0e9
00049 unitstring='fb$^{-1}$'
00050 elif t>=1.0e12 and t<1.0e15:
00051 denomitor=1.0e12
00052 unitstring='ab$^{-1}$'
00053 elif t<=1.0e-3 and t>1.0e-6:
00054 denomitor=1.0e-3
00055 unitstring='mb$^{-1}$'
00056 elif t<=1.0e-6 and t>1.0e-9:
00057 denomitor=1.0e-6
00058 unitstring='b$^{-1}$'
00059 elif t<=1.0e-9 and t>1.0e-12:
00060 denomitor=1.0e-9
00061 unitstring='kb$^{-1}$'
00062 return (unitstring,denomitor)
00063
00064 class matplotRender():
00065 def __init__(self,fig):
00066 self.__fig=fig
00067 self.__canvas=''
00068 self.colormap={}
00069 self.colormap['Delivered']='r'
00070 self.colormap['Recorded']='b'
00071 self.colormap['Effective']='g'
00072 self.colormap['Max Inst']='r'
00073
00074 def plotSumX_Run(self,rawdata={},resultlines=[],minRun=None,maxRun=None,nticks=6,yscale='linear',withannotation=False,referenceLabel='Delivered',labels=['Delivered','Recorded'],textoutput=None):
00075 '''
00076 input:
00077 rawdata = {'Delivered':[(runnumber,lumiperrun),..],'Recorded':[(runnumber,lumiperrun),..]}
00078 resultlines = [[runnumber,dellumiperrun,reclumiperrun],[runnumber,dellumiperrun,reclumiperrun],]
00079 minRun : minimal runnumber required
00080 maxRun : max runnumber required
00081 yscale: linear,log or both
00082 withannotation: wheather the boundary points should be annotated
00083 referenceLabel: the one variable that decides the total unit and the plot x-axis range
00084 labels: labels of the variables to plot
00085 textoutput: text output file name.
00086 '''
00087 ypoints={}
00088 ytotal={}
00089 for r in resultlines:
00090 runnumber=int(r[0])
00091 if rawdata and runnumber in [t[0] for t in rawdata[referenceLabel]]:continue
00092 if minRun and runnumber<minRun: continue
00093 if maxRun and runnumber>maxRun: continue
00094 for i,lab in enumerate(labels) :
00095 v=float(r[-(len(labels)-i)])
00096 rawdata.setdefault(lab,[]).append((runnumber,v))
00097 if not rawdata:
00098 print '[WARNING]: no data to plot , exit'
00099 return
00100
00101 tot=sum([t[1] for t in rawdata[referenceLabel]])
00102 (unitstring,denomitor)=guessLumiUnit(tot)
00103 csvreport=None
00104 rows=[]
00105 flat=[]
00106 for label,yvalues in rawdata.items():
00107 yvalues.sort()
00108 flat.append([t[1] for t in yvalues])
00109 ypoints[label]=[]
00110 ytotal[label]=0.0
00111 lumivals=[t[1] for t in yvalues]
00112 for i,val in enumerate(lumivals):
00113 ypoints[label].append(sum(lumivals[0:i+1])/denomitor)
00114 ytotal[label]=sum(lumivals)/denomitor
00115 xpoints=[t[0] for t in rawdata[referenceLabel]]
00116 ax=self.__fig.add_subplot(111)
00117 if yscale=='linear':
00118 ax.set_yscale('linear')
00119 elif yscale=='log':
00120 ax.set_yscale('log')
00121 else:
00122 raise 'unsupported yscale ',yscale
00123 ax.set_xlabel(r'Run',position=(0.95,0))
00124 ax.set_ylabel(r'L '+unitstring,position=(0,0.9))
00125 xticklabels=ax.get_xticklabels()
00126 for tx in xticklabels:
00127 tx.set_rotation(30)
00128 majorLocator=matplotlib.ticker.LinearLocator( nticks )
00129 majorFormatter=matplotlib.ticker.FormatStrFormatter('%d')
00130 minorLocator=matplotlib.ticker.LinearLocator(numticks=4*nticks)
00131 ax.xaxis.set_major_locator(majorLocator)
00132 ax.xaxis.set_major_formatter(majorFormatter)
00133 ax.xaxis.set_minor_locator(minorLocator)
00134 ax.set_xbound(lower=xpoints[0],upper=xpoints[-1])
00135 ax.grid(True)
00136 keylist=ypoints.keys()
00137 keylist.sort()
00138 keylist.insert(0,keylist.pop(keylist.index(referenceLabel)))
00139 legendlist=[]
00140 head=['#Run']
00141 textsummaryhead=['#TotalRun']
00142 textsummaryline=['#'+str(len(xpoints))]
00143 for ylabel in keylist:
00144 cl='k'
00145 if self.colormap.has_key(ylabel):
00146 cl=self.colormap[ylabel]
00147 ax.plot(xpoints,ypoints[ylabel],label=ylabel,color=cl,drawstyle='steps')
00148 legendlist.append(ylabel+' '+'%.3f'%(ytotal[ylabel])+' '+unitstring)
00149 textsummaryhead.append('Total'+ylabel)
00150 textsummaryline.append('%.3f'%(ytotal[ylabel])+' '+unitstring)
00151 head.append(ylabel)
00152 if textoutput:
00153 csvreport=csvReporter.csvReporter(textoutput)
00154 csvreport.writeRow(head)
00155 allruns=[int(t[0]) for t in rawdata[referenceLabel]]
00156 flat.insert(0,allruns)
00157 rows=zip(*flat)
00158 csvreport.writeRows([list(t) for t in rows])
00159 csvreport.writeRow(textsummaryhead)
00160 csvreport.writeRow(textsummaryline)
00161
00162
00163 ax.legend(tuple(legendlist),loc='upper left')
00164
00165 self.__fig.subplots_adjust(bottom=0.18,left=0.1)
00166
00167 if withannotation:
00168 trans=matplotlib.transforms.BlendedGenericTransform(ax.transData,ax.transAxes)
00169 ax.text(xpoints[0],1.025,str(xpoints[0]),transform=trans,horizontalalignment='left',size='x-small',color='green',bbox=dict(facecolor='white'))
00170 ax.text(xpoints[-1],1.025,str(xpoints[-1]),transform=trans,horizontalalignment='left',size='x-small',color='green',bbox=dict(facecolor='white'))
00171
00172
00173 def plotSumX_Fill(self,rawdata={},resultlines=[],minFill=None,maxFill=None,nticks=6,yscale='linear',withannotation=False,referenceLabel='Delivered',labels=['Delivered','Recorded'],textoutput=None):
00174 '''
00175 input:
00176 rawdata = {'Delivered':[(fill,runnumber,lumiperrun)],'Recorded':[(fill,runnumber,lumiperrun)]}
00177 resultlines = [[fillnumber,runnumber,dellumiperrun,reclumiperrun],[fillnumber,runnumber,dellumiperrun,reclumiperrun],]
00178 minFill : min fill to draw
00179 maxFill : max fill to draw
00180 yscale: linear,log or both
00181 withannotation: wheather the boundary points should be annotated
00182 textoutput: text output file name.
00183 '''
00184 ytotal={}
00185 ypoints={}
00186 for r in resultlines:
00187 fillnum=int(r[0])
00188 runnum=int(r[1])
00189 if rawdata and (fillnum,runnum) in [(t[0],t[1]) for t in rawdata[referenceLabel]]:continue
00190 if minFill and fillnum<minFill:continue
00191 if maxFill and fillnum>maxFill:continue
00192 for i,lab in enumerate(labels) :
00193 v=float(r[-(len(labels)-i)])
00194 rawdata.setdefault(lab,[]).append((fillnum,runnum,v))
00195
00196 if not rawdata:
00197 print '[WARNING]: no data, do nothing'
00198 return
00199 tot=sum([t[2] for t in rawdata[referenceLabel]])
00200 beginfo=''
00201 endinfo=''
00202 (unitstring,denomitor)=guessLumiUnit(tot)
00203 csvreport=None
00204 rows=[]
00205 flat=[]
00206 for label,yvalues in rawdata.items():
00207 yvalues.sort()
00208 flat.append([t[2] for t in yvalues])
00209 ypoints[label]=[]
00210 ytotal[label]=0.0
00211 lumivals=[t[2] for t in yvalues]
00212 for i,val in enumerate(lumivals):
00213 ypoints[label].append(sum(lumivals[0:i+1])/denomitor)
00214 ytotal[label]=sum(lumivals)/denomitor
00215 xpoints=[t[0] for t in rawdata[referenceLabel]]
00216 ax=self.__fig.add_subplot(111)
00217 ax.set_xlabel(r'LHC Fill Number',position=(0.84,0))
00218 ax.set_ylabel(r'L '+unitstring,position=(0,0.9))
00219 ax.set_xbound(lower=xpoints[0],upper=xpoints[-1])
00220 if yscale=='linear':
00221 ax.set_yscale('linear')
00222 elif yscale=='log':
00223 ax.set_yscale('log')
00224 else:
00225 raise 'unsupported yscale ',yscale
00226 xticklabels=ax.get_xticklabels()
00227 majorLocator=matplotlib.ticker.LinearLocator( nticks )
00228 majorFormatter=matplotlib.ticker.FormatStrFormatter('%d')
00229
00230 ax.xaxis.set_major_locator(majorLocator)
00231 ax.xaxis.set_major_formatter(majorFormatter)
00232
00233 ax.grid(True)
00234 keylist=ypoints.keys()
00235 keylist.sort()
00236 keylist.insert(0,keylist.pop(keylist.index(referenceLabel)))
00237 legendlist=[]
00238 head=['#fill','run']
00239 textsummaryhead=['#TotalFill']
00240 textsummaryline=['#'+str(len(xpoints))]
00241 for ylabel in keylist:
00242 cl='k'
00243 if self.colormap.has_key(ylabel):
00244 cl=self.colormap[ylabel]
00245 ax.plot(xpoints,ypoints[ylabel],label=ylabel,color=cl,drawstyle='steps')
00246 legendlist.append(ylabel+' '+'%.3f'%(ytotal[ylabel])+' '+unitstring)
00247 textsummaryhead.append('Total'+ylabel)
00248 textsummaryline.append('%.3f'%(ytotal[ylabel])+' '+unitstring)
00249 head.append(ylabel)
00250 if textoutput:
00251 csvreport=csvReporter.csvReporter(textoutput)
00252 allfills=[int(t[0]) for t in rawdata[referenceLabel]]
00253 allruns=[int(t[1]) for t in rawdata[referenceLabel]]
00254 flat.insert(0,allfills)
00255 flat.insert(1,allruns)
00256 rows=zip(*flat)
00257 csvreport.writeRow(head)
00258 csvreport.writeRows([list(t) for t in rows])
00259 csvreport.writeRow(textsummaryhead)
00260 csvreport.writeRow(textsummaryline)
00261
00262
00263 if withannotation:
00264 trans=matplotlib.transforms.BlendedGenericTransform(ax.transData,ax.transAxes)
00265 ax.text(xpoints[0],1.025,beginfo,transform=trans,horizontalalignment='left',size='x-small',color='green',bbox=dict(facecolor='white'))
00266 ax.text(xpoints[-1],1.025,endinfo,transform=trans,horizontalalignment='left',size='x-small',color='green',bbox=dict(facecolor='white'))
00267
00268 ax.legend(tuple(legendlist),loc='upper left')
00269
00270 self.__fig.subplots_adjust(bottom=0.1,left=0.1)
00271
00272 def plotSumX_Time(self,rawdata={},resultlines=[],minTime=None,maxTime=None,nticks=6,yscale='linear',withannotation=False,referenceLabel='Delivered',labels=['Delivered','Recorded'],textoutput=None):
00273 '''
00274 input:
00275 rawdata = {'Delivered':[(runnumber,starttimestamp,stoptimestamp,lumiperrun)],'Recorded':[(runnumber,starttimestamp,stoptimestamp,lumiperrun)]}
00276 resultlines = [[runnumber,starttimestampStr,stoptimestampStr,dellumiperrun,reclumiperrun],[runnumber,starttimestampStr,stoptimestampStr,dellumiperrun,reclumiperrun],]
00277 minTime (python DateTime) : min *begin* time to draw: format %m/%d/%y %H:%M:%S
00278 maxTime (python DateTime): max *begin* time to draw %m/%d/%y %H:%M:%S
00279 yscale: linear,log or both
00280 withannotation: wheather the boundary points should be annotated
00281 referenceLabel: the one variable that decides the total unit and the plot x-axis range
00282 labels: labels of the variables to plot
00283 '''
00284 xpoints=[]
00285 ypoints={}
00286 ytotal={}
00287 lut=lumiTime.lumiTime()
00288 if not minTime:
00289 minTime='03/01/10 00:00:00'
00290 minTime=lut.StrToDatetime(minTime,customfm='%m/%d/%y %H:%M:%S')
00291 if not maxTime:
00292 maxTime=datetime.datetime.utcnow()
00293 else:
00294 maxTime=lut.StrToDatetime(maxTime,customfm='%m/%d/%y %H:%M:%S')
00295 for r in resultlines:
00296 runnumber=int(r[0])
00297 starttimeStr=r[1].split('.')[0]
00298 starttime=lut.StrToDatetime(starttimeStr,customfm='%Y-%m-%d %H:%M:%S')
00299 stoptimeStr=r[2].split('.')[0]
00300 stoptime=lut.StrToDatetime(stoptimeStr,customfm='%Y-%m-%d %H:%M:%S')
00301 if rawdata and runnumber in [t[0] for t in rawdata[referenceLabel]]:continue
00302 if starttime<minTime:continue
00303 if starttime>maxTime:continue
00304
00305 for i,lab in enumerate(labels):
00306 v=float(r[-(len(labels)-i)])
00307 rawdata.setdefault(lab,[]).append((runnumber,starttime,stoptime,v))
00308 if not rawdata:
00309 print '[WARNING]: no data, do nothing'
00310 return
00311 tot=sum([t[3] for t in rawdata[referenceLabel]])
00312 (unitstring,denomitor)=guessLumiUnit(tot)
00313 csvreport=None
00314 rows=[]
00315 flat=[]
00316 for label,yvalues in rawdata.items():
00317 yvalues.sort()
00318 flat.append([t[3] for t in yvalues])
00319 if label==referenceLabel:
00320 minTime=yvalues[0][1]
00321 maxTime=yvalues[-1][1]
00322 ypoints[label]=[]
00323 lumivals=[t[3] for t in yvalues]
00324 for i,val in enumerate(lumivals):
00325 ypoints[label].append(sum(lumivals[0:i+1])/denomitor)
00326 ytotal[label]=sum(lumivals)/denomitor
00327 xpoints=[matplotlib.dates.date2num(t[1]) for t in rawdata[referenceLabel]]
00328 ax=self.__fig.add_subplot(111)
00329 ax.set_yscale(yscale)
00330 yearStrMin=minTime.strftime('%Y')
00331 yearStrMax=maxTime.strftime('%Y')
00332 if yearStrMin==yearStrMax:
00333 dateFmt=matplotlib.dates.DateFormatter('%d/%m')
00334 else:
00335 dateFmt=matplotlib.dates.DateFormatter('%d/%m/%y')
00336 majorLoc=matplotlib.ticker.LinearLocator(numticks=nticks)
00337 ax.xaxis.set_major_locator(majorLoc)
00338 minorLoc=matplotlib.ticker.LinearLocator(numticks=nticks*4)
00339 ax.xaxis.set_major_formatter(dateFmt)
00340 ax.set_xlabel(r'Date',position=(0.84,0))
00341 ax.set_ylabel(r'L '+unitstring,position=(0,0.9))
00342 ax.xaxis.set_minor_locator(minorLoc)
00343 ax.set_xbound(lower=xpoints[0],upper=xpoints[-1])
00344 xticklabels=ax.get_xticklabels()
00345 for tx in xticklabels:
00346 tx.set_horizontalalignment('left')
00347 ax.grid(True)
00348 keylist=ypoints.keys()
00349 keylist.sort()
00350 keylist.insert(0,keylist.pop(keylist.index(referenceLabel)))
00351 legendlist=[]
00352 head=['#Run','StartTime','StopTime']
00353 textsummaryhead=['#TotalRun']
00354 textsummaryline=['#'+str(len(xpoints))]
00355 for ylabel in keylist:
00356 cl='k'
00357 if self.colormap.has_key(ylabel):
00358 cl=self.colormap[ylabel]
00359 ax.plot(xpoints,ypoints[ylabel],label=ylabel,color=cl,drawstyle='steps')
00360 legendlist.append(ylabel+' '+'%.3f'%(ytotal[ylabel])+' '+unitstring)
00361 textsummaryhead.append('Total'+ylabel)
00362 textsummaryline.append('%.3f'%(ytotal[ylabel])+' '+unitstring)
00363 head.append(ylabel)
00364 if textoutput:
00365 csvreport=csvReporter.csvReporter(textoutput)
00366 csvreport.writeRow(head)
00367 allruns=[int(t[0]) for t in rawdata[referenceLabel]]
00368 allstarts=[ t[1] for t in rawdata[referenceLabel]]
00369 allstops=[ t[2] for t in rawdata[referenceLabel]]
00370 flat.insert(0,allruns)
00371 flat.insert(1,allstarts)
00372 flat.insert(2,allstops)
00373 rows=zip(*flat)
00374 csvreport.writeRows([list(t) for t in rows])
00375 csvreport.writeRow(textsummaryhead)
00376 csvreport.writeRow(textsummaryline)
00377
00378 trans=matplotlib.transforms.BlendedGenericTransform(ax.transData,ax.transAxes)
00379
00380
00381
00382 if withannotation:
00383 runs=[t[0] for t in rawdata[referenceLabel]]
00384 ax.text(xpoints[0],1.025,str(runs[0]),transform=trans,horizontalalignment='left',size='x-small',color='green',bbox=dict(facecolor='white'))
00385 ax.text(xpoints[-1],1.025,str(runs[-1]),transform=trans,horizontalalignment='left',size='x-small',color='green',bbox=dict(facecolor='white'))
00386
00387 if yearStrMin==yearStrMax:
00388 firsttimeStr=rawdata[referenceLabel][1][1].strftime('%b %d %H:%M')
00389 lasttimeStr=rawdata[referenceLabel][-1][2].strftime('%b %d %H:%M')
00390
00391
00392
00393 ax.set_title('CMS Total Integrated Luminosity '+yearStrMin+' ('+firsttimeStr+' - '+lasttimeStr+' UTC)',size='small')
00394 else:
00395
00396 ax.set_title('CMS Total Integrated Luminosity '+yearStrMin+'-'+yearStrMax,size='small')
00397 ax.legend(tuple(legendlist),loc='upper left')
00398 ax.autoscale_view(tight=True,scalex=True,scaley=False)
00399 self.__fig.autofmt_xdate(bottom=0.18,rotation=15,ha='right')
00400 self.__fig.subplots_adjust(bottom=0.2,left=0.15)
00401
00402 def plotPerdayX_Time(self,rawdata={},resultlines=[],minTime=None,maxTime=None,nticks=6,yscale='linear',withannotation=False,referenceLabel='Delivered',labels=['Delivered','Recorded'],textoutput=None):
00403 '''
00404 Input:
00405 rawdata={'Delivered':[(day,begrun:ls,endrun:ls,lumi)],'Recorded':[(dayofyear,begrun:ls,endrun:ls,lumi)]}
00406 resultlines=[[day,begrun:ls,endrun:ls,deliveredperday,recordedperday],[]]
00407 minTime (python DateTime) : min *begin* time to draw: format %m/%d/%y %H:%M:%S
00408 maxTime (python DateTime): max *begin* time to draw %m/%d/%y %H:%M:%S
00409 withannotation: wheather the boundary points should be annotated
00410 referenceLabel: the one variable that decides the total unit and the plot x-axis range
00411 labels: labels of the variables to plot
00412 '''
00413 xpoints=[]
00414 ypoints={}
00415 ymax={}
00416 lut=lumiTime.lumiTime()
00417 if not minTime:
00418 minTime='03/01/10 00:00:00'
00419 minTime=lut.StrToDatetime(minTime,customfm='%m/%d/%y %H:%M:%S')
00420 if not maxTime:
00421 maxTime=datetime.datetime.utcnow()
00422 else:
00423 maxTime=lut.StrToDatetime(maxTime,customfm='%m/%d/%y %H:%M:%S')
00424 for r in resultlines:
00425 day=int(r[0])
00426 begrunls=r[1]
00427 endrunls=r[2]
00428
00429 if rawdata and day in [t[0] for t in rawdata[referenceLabel]]:continue
00430 if day < minTime.date().toordinal():continue
00431 if day > maxTime.date().toordinal():continue
00432 for i,lab in enumerate(labels):
00433 v=float(r[-(len(labels)-i)])
00434 rawdata.setdefault(lab,[]).append((day,begrunls,endrunls,v))
00435 if not rawdata:
00436 print '[WARNING]: no data, do nothing'
00437 return
00438 maxlum=max([t[3] for t in rawdata[referenceLabel]])
00439 minlum=min([t[3] for t in rawdata[referenceLabel] if t[3]>0])
00440 (unitstring,denomitor)=guessLumiUnit(maxlum)
00441 csvreport=None
00442 rows=[]
00443 flat=[]
00444 for label,yvalues in rawdata.items():
00445 yvalues.sort()
00446 flat.append([t[3] for t in yvalues])
00447 minday=yvalues[0][0]
00448
00449 maxday=yvalues[-1][0]
00450
00451 alldays=range(minday,maxday+1)
00452
00453 ypoints[label]=[]
00454 dayvals=[t[0] for t in yvalues]
00455 lumivals=[t[3] for t in yvalues]
00456
00457 for d in alldays:
00458 if not d in dayvals:
00459 ypoints[label].append(0.0)
00460 else:
00461 thisdaylumi=[t[3] for t in yvalues if t[0]==d][0]
00462 if yscale=='log':
00463 if thisdaylumi<minlum:
00464 thisdaylumi=minlum/denomitor
00465 else:
00466 thisdaylumi=thisdaylumi/denomitor
00467 else:
00468 thisdaylumi=thisdaylumi/denomitor
00469 ypoints[label].append(thisdaylumi)
00470 ymax[label]=max(lumivals)/denomitor
00471 xpoints=alldays
00472 if textoutput:
00473 csvreport=csvReporter.csvReporter(textoutput)
00474 head=['#day','begrunls','endrunls','delivered','recorded']
00475 csvreport.writeRow(head)
00476 flat.insert(0,alldays)
00477 allstarts=[ t[1] for t in rawdata[referenceLabel]]
00478 allstops=[ t[2] for t in rawdata[referenceLabel]]
00479 flat.insert(1,allstarts)
00480 flat.insert(2,allstops)
00481 rows=zip(*flat)
00482 csvreport.writeRows([list(t) for t in rows])
00483
00484 yearStrMin=minTime.strftime('%Y')
00485 yearStrMax=maxTime.strftime('%Y')
00486 if yearStrMin==yearStrMax:
00487 dateFmt=matplotlib.dates.DateFormatter('%d/%m')
00488 else:
00489 dateFmt=matplotlib.dates.DateFormatter('%d/%m/%y')
00490 ax=self.__fig.add_subplot(111)
00491 if yscale=='linear':
00492 ax.set_yscale('linear')
00493 elif yscale=='log':
00494 ax.set_yscale('log')
00495 else:
00496 raise 'unsupported yscale ',yscale
00497 majorLoc=matplotlib.ticker.LinearLocator(numticks=nticks)
00498 minorLoc=matplotlib.ticker.LinearLocator(numticks=nticks*4)
00499 ax.xaxis.set_major_formatter(dateFmt)
00500 ax.set_xlabel(r'Date',position=(0.84,0))
00501 ax.xaxis.set_major_locator(majorLoc)
00502 ax.xaxis.set_minor_locator(minorLoc)
00503 xticklabels=ax.get_xticklabels()
00504 for tx in xticklabels:
00505 tx.set_horizontalalignment('right')
00506 ax.grid(True)
00507 legendlist=[]
00508 ax.set_ylabel(r'L '+unitstring,position=(0,0.9))
00509 textsummaryhead=['#TotalDays']
00510 textsummaryline=['#'+str(len(xpoints))]
00511 for ylabel in labels:
00512 cl='k'
00513 if self.colormap.has_key(ylabel):
00514 cl=self.colormap[ylabel]
00515 ax.plot(xpoints,ypoints[ylabel],label=ylabel,color=cl,drawstyle='steps')
00516 legendlist.append(ylabel+' Max '+'%.3f'%(ymax[ylabel])+' '+unitstring)
00517 textsummaryhead.append('Max'+ylabel)
00518 textsummaryline.append('%.3f'%(ymax[ylabel])+' '+unitstring)
00519 if textoutput:
00520 csvreport.writeRow(textsummaryhead)
00521 csvreport.writeRow(textsummaryline)
00522 ax.legend(tuple(legendlist),loc='upper left')
00523 ax.set_xbound(lower=matplotlib.dates.date2num(minTime),upper=matplotlib.dates.date2num(maxTime))
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534 firstday=datetime.date.fromordinal(rawdata[referenceLabel][0][0])
00535 lastday=datetime.date.fromordinal(rawdata[referenceLabel][-1][0])
00536 firstdayStr=firstday.strftime('%Y %b %d')
00537 lastdayStr=lastday.strftime('%Y %b %d')
00538 ax.set_title('CMS Integrated Luminosity/Day ('+firstdayStr+' - '+lastdayStr+')',size='small')
00539
00540 ax.autoscale_view(tight=True,scalex=True,scaley=False)
00541
00542 self.__fig.autofmt_xdate(bottom=0.18,rotation=15,ha='right')
00543 self.__fig.subplots_adjust(bottom=0.2,left=0.15)
00544
00545 def plotPeakPerday_Time(self,rawdata={},resultlines=[],minTime=None,maxTime=None,nticks=6,withannotation=False,yscale='linear',referenceLabel='Delivered',labels=['Delivered'],textoutput=None):
00546 '''
00547 THIS PLOT IS DELIVERED ONLY
00548 Input:
00549 rawdata={'Delivered':[(day,run,ls,instlumi)]}
00550 resultlines=[[day,run,ls,maxinstlum],[]]
00551 minTime (python DateTime) : min *begin* time to draw: format %m/%d/%y %H:%M:%S
00552 maxTime (python DateTime): max *begin* time to draw %m/%d/%y %H:%M:%S
00553 withannotation: wheather the boundary points should be annotated
00554 referenceLabel: the one variable that decides the total unit and the plot x-axis range
00555 labels: labels of the variables to plot
00556 '''
00557 xpoints=[]
00558 ypoints={}
00559 legendlist=[]
00560 maxinfo=''
00561 ymax={}
00562 lut=lumiTime.lumiTime()
00563 if not minTime:
00564 minTime='03/01/10 00:00:00'
00565 minTime=lut.StrToDatetime(minTime,customfm='%m/%d/%y %H:%M:%S')
00566 if not maxTime:
00567 maxTime=datetime.datetime.utcnow()
00568 else:
00569 maxTime=lut.StrToDatetime(maxTime,customfm='%m/%d/%y %H:%M:%S')
00570 for r in resultlines:
00571 day=int(r[0])
00572 runnumber=int(r[1])
00573 lsnum=int(r[2].split('.')[0])
00574 if rawdata and day in [int(t[0]) for t in rawdata[referenceLabel]]:continue
00575 if day < minTime.date().toordinal():continue
00576 if day > maxTime.date().toordinal():continue
00577 for i,lab in enumerate(labels):
00578 v=float(r[-(len(labels)-i)])
00579 rawdata.setdefault(lab,[]).append((day,runnumber,lsnum,v))
00580 if not rawdata:
00581 print '[WARNING]: no data, do nothing'
00582 return
00583 maxlum=max([t[3] for t in rawdata[referenceLabel]])
00584 minlum=min([t[3] for t in rawdata[referenceLabel] if t[3]>0])
00585 (unitstring,denomitor)=guessLumiUnit(maxlum)
00586 csvreport=None
00587 rows=[]
00588 flat=[]
00589 alldays=[]
00590 for label,yvalues in rawdata.items():
00591 yvalues.sort()
00592 minday=yvalues[0][0]
00593 maxday=yvalues[-1][0]
00594 alldays=range(minday,maxday+1)
00595 ypoints[label]=[]
00596 dayvals=[t[0] for t in yvalues]
00597 lumivals=[t[3] for t in yvalues]
00598 flat.append(lumivals)
00599 for d in alldays:
00600 if not d in dayvals:
00601 ypoints[label].append(0.0)
00602 else:
00603 thisdaylumi=[t[3] for t in yvalues if t[0]==d][0]
00604 if yscale=='log':
00605 if thisdaylumi<minlum:
00606 thisdaylumi=minlum/denomitor
00607 else:
00608 thisdaylumi=thisdaylumi/denomitor
00609 else:
00610 thisdaylumi=thisdaylumi/denomitor
00611 ypoints[label].append(thisdaylumi)
00612 ymax[label]=max(lumivals)/denomitor
00613 xpoints=alldays
00614 if textoutput:
00615 csvreport=csvReporter.csvReporter(textoutput)
00616 head=['#day','run','lsnum','maxinstlumi']
00617 csvreport.writeRow(head)
00618 flat.insert(0,[t[0] for t in yvalues])
00619 allruns=[ t[1] for t in rawdata[referenceLabel]]
00620 allls=[ t[2] for t in rawdata[referenceLabel]]
00621 flat.insert(1,allruns)
00622 flat.insert(2,allls)
00623 rows=zip(*flat)
00624 csvreport.writeRows([list(t) for t in rows])
00625
00626 yearStrMin=minTime.strftime('%Y')
00627 yearStrMax=maxTime.strftime('%Y')
00628 if yearStrMin==yearStrMax:
00629 dateFmt=matplotlib.dates.DateFormatter('%d/%m')
00630 else:
00631 dateFmt=matplotlib.dates.DateFormatter('%d/%m/%y')
00632 ax=self.__fig.add_subplot(111)
00633 if yscale=='linear':
00634 ax.set_yscale('linear')
00635 elif yscale=='log':
00636 ax.set_yscale('log')
00637 else:
00638 raise 'unsupported yscale ',yscale
00639 majorLoc=matplotlib.ticker.LinearLocator(numticks=nticks)
00640 minorLoc=matplotlib.ticker.LinearLocator(numticks=nticks*4)
00641 ax.xaxis.set_major_formatter(dateFmt)
00642 ax.set_xlabel(r'Date',position=(0.84,0))
00643 ax.set_ylabel(r'L '+unitstring,position=(0,0.9))
00644 ax.xaxis.set_major_locator(majorLoc)
00645 ax.xaxis.set_minor_locator(minorLoc)
00646 xticklabels=ax.get_xticklabels()
00647 for tx in xticklabels:
00648 tx.set_horizontalalignment('right')
00649 ax.grid(True)
00650 cl=self.colormap['Max Inst']
00651 textsummaryhead=['#TotalDays']
00652 textsummaryline=['#'+str(len(xpoints))]
00653 for ylabel in labels:
00654 cl='k'
00655 if self.colormap.has_key(ylabel):
00656 cl=self.colormap[ylabel]
00657 ax.plot(xpoints,ypoints[ylabel],label='Max Inst',color=cl,drawstyle='steps')
00658 legendlist.append('Max Inst %.3f'%(ymax[ylabel])+' '+unitstring)
00659 textsummaryhead.append('Max Inst'+ylabel)
00660 textsummaryline.append('%.3f'%(ymax[ylabel])+' '+unitstring)
00661 if textoutput:
00662 csvreport.writeRow(textsummaryhead)
00663 csvreport.writeRow(textsummaryline)
00664 ax.legend(tuple(legendlist),loc='upper left')
00665 ax.set_xbound(lower=matplotlib.dates.date2num(minTime),upper=matplotlib.dates.date2num(maxTime))
00666 if withannotation:
00667
00668 trans=matplotlib.transforms.BlendedGenericTransform(ax.transData,ax.transAxes)
00669 ax.text(xpoints[0],1.025,beginfo,transform=trans,horizontalalignment='left',size='x-small',color='green',bbox=dict(facecolor='white'))
00670 ax.text(xpoints[-1],1.025,endinfo,transform=trans,horizontalalignment='left',size='x-small',color='green',bbox=dict(facecolor='white'))
00671 ax.annotate(maxinfo,xy=(xmax,ymax),xycoords='data',xytext=(0,13),textcoords='offset points',arrowprops=dict(facecolor='green',shrink=0.05),size='x-small',horizontalalignment='center',color='green',bbox=dict(facecolor='white'))
00672
00673 firstday=datetime.date.fromordinal(rawdata[referenceLabel][0][0])
00674 lastday=datetime.date.fromordinal(rawdata[referenceLabel][-1][0])
00675 firstdayStr=firstday.strftime('%Y %b %d')
00676 lastdayStr=lastday.strftime('%Y %b %d')
00677 ax.set_title('CMS Peak Luminosity/Day ('+firstdayStr+' - '+lastdayStr+')',size='small')
00678
00679
00680 ax.autoscale_view(tight=True,scalex=True,scaley=False)
00681
00682 self.__fig.autofmt_xdate(bottom=0.18,rotation=15,ha='right')
00683 self.__fig.subplots_adjust(bottom=0.2,left=0.15)
00684
00685 def plotInst_RunLS(self,rawxdata,rawydata,nticks=6,textoutput=None):
00686 '''
00687 Input: rawxdata [run,fill,starttime,stoptime,totalls,ncmsls]
00688 rawydata {label:[lumi]}
00689 '''
00690 lslength=23.357
00691 lut=lumiTime.lumiTime()
00692 runnum=rawxdata[0]
00693 fill=rawxdata[1]
00694 starttime=lut.DatetimeToStr(rawxdata[2],customfm='%m/%d/%y %H:%M:%S')
00695 stoptime=lut.DatetimeToStr(rawxdata[3],customfm='%m/%d/%y %H:%M:%S')
00696 totalls=rawxdata[-2]
00697 ncmsls=rawxdata[-1]
00698 peakinst=max(rawydata['Delivered'])/lslength
00699 totaldelivered=sum(rawydata['Delivered'])
00700 totalrecorded=sum(rawydata['Recorded'])
00701 xpoints=range(1,totalls+1)
00702
00703 ypoints={}
00704 ymax={}
00705 for ylabel,yvalue in rawydata.items():
00706 ypoints[ylabel]=[y/lslength for y in yvalue]
00707 ymax[ylabel]=max(yvalue)/lslength
00708 left=0.15
00709 width=0.7
00710 bottom=0.1
00711 height=0.65
00712 bottom_h=bottom+height
00713 rect_scatter=[left,bottom,width,height]
00714 rect_table=[left,bottom_h,width,0.25]
00715
00716 nullfmt=matplotlib.ticker.NullFormatter()
00717 nullloc=matplotlib.ticker.NullLocator()
00718 axtab=self.__fig.add_axes(rect_table,frameon=False)
00719 axtab.set_axis_off()
00720 axtab.xaxis.set_major_formatter(nullfmt)
00721 axtab.yaxis.set_major_formatter(nullfmt)
00722 axtab.xaxis.set_major_locator(nullloc)
00723 axtab.yaxis.set_major_locator(nullloc)
00724
00725 ax=self.__fig.add_axes(rect_scatter)
00726
00727 majorLoc=matplotlib.ticker.LinearLocator(numticks=nticks)
00728 minorLoc=matplotlib.ticker.LinearLocator(numticks=nticks*4)
00729 ax.set_xlabel(r'LS',position=(0.96,0))
00730 ax.set_ylabel(r'L $\mu$b$^{-1}$s$^{-1}$',position=(0,0.9))
00731 ax.xaxis.set_major_locator(majorLoc)
00732 ax.xaxis.set_minor_locator(minorLoc)
00733 ax.set_xbound(lower=xpoints[0],upper=xpoints[-1])
00734 xticklabels=ax.get_xticklabels()
00735 for tx in xticklabels:
00736 tx.set_horizontalalignment('right')
00737 ax.grid(True)
00738 keylist=ypoints.keys()
00739 keylist.sort()
00740 legendlist=[]
00741
00742 for ylabel in keylist:
00743 cl='k'
00744 if self.colormap.has_key(ylabel):
00745 cl=self.colormap[ylabel]
00746 ax.plot(xpoints,ypoints[ylabel],'.',label=ylabel,color=cl)
00747 legendlist.append(ylabel)
00748
00749 ax.axvline(xpoints[ncmsls-1],color='green',linewidth=0.2)
00750 (unitstring,denomitor)=guessLumiUnit(totaldelivered)
00751 colLabels=('run','fill','max inst(/$\mu$b/s)','delivered('+unitstring+')','recorded('+unitstring+')')
00752 cellText=[[str(runnum),str(fill),'%.3f'%(peakinst),'%.3f'%(totaldelivered/denomitor),'%.3f'%(totalrecorded/denomitor)]]
00753
00754 sumtable=axtab.table(cellText=cellText,colLabels=colLabels,colWidths=[0.12,0.1,0.27,0.27,0.27],cellLoc='center',loc='center')
00755 trans=matplotlib.transforms.BlendedGenericTransform(ax.transData,ax.transAxes)
00756 axtab.add_table(sumtable)
00757
00758 ax.text(xpoints[0],1.02,starttime[0:17],transform=trans,horizontalalignment='left',size='x-small',color='green',bbox=dict(facecolor='white'))
00759 ax.text(xpoints[ncmsls-1],1.02,stoptime[0:17],transform=trans,horizontalalignment='left',size='x-small',color='green',bbox=dict(facecolor='white'))
00760 ax.legend(tuple(legendlist),loc='upper right',numpoints=1)
00761
00762 def drawHTTPstring(self):
00763 self.__canvas=CanvasBackend(self.__fig)
00764 cherrypy.response.headers['Content-Type']='image/png'
00765 buf=StringIO()
00766 self.__canvas.print_png(buf)
00767 return buf.getvalue()
00768
00769 def drawPNG(self,filename):
00770 self.__canvas=CanvasBackend(self.__fig)
00771 self.__canvas.print_figure(filename)
00772
00773 def drawInteractive(self):
00774 if batchonly:
00775 print 'interactive mode is not available for your setup, exit'
00776 sys.exit()
00777 aw=lumiQTWidget.ApplicationWindow(fig=self.__fig)
00778 aw.show()
00779 aw.destroy()
00780
00781 if __name__=='__main__':
00782 import csv
00783 print '=====testing plotSumX_Run======'
00784 f=open('/afs/cern.ch/cms/lumi/www/plots/operation/totallumivsrun-2011.csv','r')
00785 reader=csv.reader(f,delimiter=',')
00786 resultlines=[]
00787 for row in reader:
00788 if not row[0].isdigit():continue
00789 resultlines.append(row)
00790
00791 fig=Figure(figsize=(7.2,5.4),dpi=120)
00792 m=matplotRender(fig)
00793 m.plotSumX_Run(rawdata={},resultlines=resultlines,minRun=None,maxRun=None,nticks=6,yscale='linear',withannotation=False)
00794
00795 m.drawInteractive()
00796 print 'DONE'
00797
00798 '''
00799 print '=====testing plotSumX_Fill======'
00800 f=open('/afs/cern.ch/cms/lumi/www/plots/operation/totallumivsfill-2011.csv','r')
00801 reader=csv.reader(f,delimiter=',')
00802 resultlines=[]
00803 for row in reader:
00804 if not row[0].isdigit():continue
00805 resultlines.append(row)
00806
00807 fig=Figure(figsize=(7.2,5.4),dpi=120)
00808 m=matplotRender(fig)
00809 m.plotSumX_Fill(rawdata={},resultlines=resultlines,minFill=None,maxFill=None,nticks=6,yscale='linear',withannotation=True)
00810 m.drawPNG('totallumivsfill-2011test.png')
00811 print 'DONE'
00812 print '=====testing plotSumX_Time======'
00813 f=open('/afs/cern.ch/cms/lumi/www/publicplots/totallumivstime-2011.csv','r')
00814 reader=csv.reader(f,delimiter=',')
00815 resultlines=[]
00816 for row in reader:
00817 if not row[0].isdigit():continue
00818 resultlines.append(row)
00819
00820 fig=Figure(figsize=(7.25,5.4),dpi=120)
00821 m=matplotRender(fig)
00822 m.plotSumX_Time(rawdata={},resultlines=resultlines,minTime="03/14/11 09:00:00",maxTime=None,nticks=6,yscale='linear',withannotation=False)
00823 m.drawPNG('totallumivstime-2011test.png')
00824 print 'DONE'
00825
00826 print '=====testing plotPerdayX_Time======'
00827 f=open('/afs/cern.ch/cms/lumi/www/publicplots/lumiperday-2011.csv','r')
00828 reader=csv.reader(f,delimiter=',')
00829 resultlines=[]
00830 for row in reader:
00831 if not row[0].isdigit():continue
00832 resultlines.append(row)
00833
00834 fig=Figure(figsize=(7.25,5.4),dpi=120)
00835 m=matplotRender(fig)
00836 m.plotPerdayX_Time(rawdata={},resultlines=resultlines,minTime="03/14/11 09:00:00",maxTime=None,nticks=6,yscale='linear',withannotation=False)
00837 m.drawPNG('lumiperday-2011test.png')
00838 print 'DONE'
00839
00840 print '=====testing plotPeakPerday_Time======'
00841 f=open('/afs/cern.ch/cms/lumi/www/publicplots/lumipeak-2011.csv','r')
00842 reader=csv.reader(f,delimiter=',')
00843 resultlines=[]
00844 for row in reader:
00845 if not row[0].isdigit():continue
00846 resultlines.append(row)
00847
00848 fig=Figure(figsize=(7.25,5.4),dpi=120)
00849 m=matplotRender(fig)
00850 m.plotPeakPerday_Time(rawdata={},resultlines=resultlines,minTime="03/14/11 09:00:00",maxTime=None,nticks=6,yscale='linear',withannotation=False)
00851 m.drawPNG('lumipeak-2011test.png')
00852 print 'DONE'
00853
00854 '''