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