CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
matplotRender.py
Go to the documentation of this file.
1 '''
2 Specs:
3 -- We use matplotlib OO class level api, we do not use its high-level helper modules. Favor endured stability over simplicity.
4 -- PNG as default batch file format
5 -- we support http mode by sending string buf via meme type image/png. Sending a premade static plot to webserver is considered a uploading process instead of http dynamic graphical mode.
6 '''
7 import sys,os
8 import numpy,datetime
9 import matplotlib
10 from RecoLuminosity.LumiDB import CommonUtil,lumiTime,csvReporter
11 
12 batchonly=False
13 if not os.environ.has_key('DISPLAY') or not os.environ['DISPLAY']:
14  batchonly=True
15  matplotlib.use('Agg',warn=False)
16 else:
17  try:
18  from RecoLuminosity.LumiDB import lumiQTWidget
19  except ImportError:
20  print 'unable to import GUI backend, switch to batch only mode'
21  matplotlib.use('Agg',warn=False)
22  batchonly=True
23 from matplotlib.backends.backend_agg import FigureCanvasAgg as CanvasBackend
24 from matplotlib.figure import Figure
25 from matplotlib.font_manager import fontManager,FontProperties
26 matplotlib.rcParams['lines.linewidth']=1.5
27 matplotlib.rcParams['grid.linewidth']=0.2
28 matplotlib.rcParams['xtick.labelsize']=11
29 matplotlib.rcParams['ytick.labelsize']=11
30 matplotlib.rcParams['legend.fontsize']=10
31 matplotlib.rcParams['axes.labelsize']=11
32 matplotlib.rcParams['font.weight']=567
33 
35  '''
36  input : largest total lumivalue
37  output: (unitstring,denomitor)
38  '''
39  unitstring='$\mu$b$^{-1}$s$^{-1}$'
40  denomitor=1.0
41  if t>=1.0e3 and t<1.0e06:
42  denomitor=1.0e3
43  unitstring='nb$^{-1}$s$^{-1}$'
44  elif t>=1.0e6 and t<1.0e9:
45  denomitor=1.0e6
46  unitstring='pb$^{-1}$s$^{-1}$'
47  elif t>=1.0e9 and t<1.0e12:
48  denomitor=1.0e9
49  unitstring='fb$^{-1}$s$^{-1}$'
50  elif t>=1.0e12 and t<1.0e15:
51  denomitor=1.0e12
52  unitstring='ab$^{-1}$s$^{-1}$'
53  elif t<=1.0e-3 and t>1.0e-6: #left direction
54  denomitor=1.0e-3
55  unitstring='mb$^{-1}$s$^{-1}$'
56  elif t<=1.0e-6 and t>1.0e-9:
57  denomitor=1.0e-6
58  unitstring='b$^{-1}$s$^{-1}$'
59  elif t<=1.0e-9 and t>1.0e-12:
60  denomitor=1.0e-9
61  unitstring='kb$^{-1}$s$^{-1}$'
62  return (unitstring,denomitor)
63 
65  '''
66  input : largest total lumivalue
67  output: (unitstring,denomitor)
68  '''
69  unitstring='$\mu$b$^{-1}$'
70  denomitor=1.0
71  if t>=1.0e3 and t<1.0e06:
72  denomitor=1.0e3
73  unitstring='nb$^{-1}$'
74  elif t>=1.0e6 and t<1.0e9:
75  denomitor=1.0e6
76  unitstring='pb$^{-1}$'
77  elif t>=1.0e9 and t<1.0e12:
78  denomitor=1.0e9
79  unitstring='fb$^{-1}$'
80  elif t>=1.0e12 and t<1.0e15:
81  denomitor=1.0e12
82  unitstring='ab$^{-1}$'
83  elif t<=1.0e-3 and t>1.0e-6: #left direction
84  denomitor=1.0e-3
85  unitstring='mb$^{-1}$'
86  elif t<=1.0e-6 and t>1.0e-9:
87  denomitor=1.0e-6
88  unitstring='b$^{-1}$'
89  elif t<=1.0e-9 and t>1.0e-12:
90  denomitor=1.0e-9
91  unitstring='kb$^{-1}$'
92  return (unitstring,denomitor)
93 
94 class matplotRender():
95  def __init__(self,fig):
96  self.__fig=fig
97  self.__canvas=''
98  self.colormap={}
99  self.colormap['Delivered']='r'
100  self.colormap['Recorded']='b'
101  self.colormap['Effective']='g'
102  self.colormap['Max Inst']='r'
103 
104  def plotSumX_Run(self,rawdata={},resultlines=[],minRun=None,maxRun=None,nticks=6,yscale='linear',withannotation=False,referenceLabel='Delivered',labels=['Delivered','Recorded'],textoutput=None):
105  '''
106  input:
107  rawdata = {'Delivered':[(runnumber,lumiperrun),..],'Recorded':[(runnumber,lumiperrun),..]}
108  resultlines = [[runnumber,dellumiperrun,reclumiperrun],[runnumber,dellumiperrun,reclumiperrun],]
109  minRun : minimal runnumber required
110  maxRun : max runnumber required
111  yscale: linear,log or both
112  withannotation: wheather the boundary points should be annotated
113  referenceLabel: the one variable that decides the total unit and the plot x-axis range
114  labels: labels of the variables to plot
115  textoutput: text output file name.
116  '''
117  ypoints={}
118  ytotal={}
119  for r in resultlines:#parse old text data
120  runnumber=int(r[0])
121  if rawdata and runnumber in [t[0] for t in rawdata[referenceLabel]]:continue#use text input only if not in selected data
122  if minRun and runnumber<minRun: continue
123  if maxRun and runnumber>maxRun: continue
124  for i,lab in enumerate(labels) :
125  v=float(r[-(len(labels)-i)-1])#the values to plot are always the last n fields
126  rawdata.setdefault(lab,[]).append((runnumber,v))
127  if not rawdata:
128  print '[WARNING]: no data to plot , exit'
129  return
130 
131  tot=sum([t[1] for t in rawdata[referenceLabel]])
132  (unitstring,denomitor)=guessLumiUnit(tot)
133  csvreport=None
134  rows=[]
135  flat=[]
136  for label,yvalues in rawdata.items():
137  yvalues.sort()
138  flat.append([t[1] for t in yvalues])
139  ypoints[label]=[]
140  ytotal[label]=0.0
141  lumivals=[t[1] for t in yvalues]
142  for i,val in enumerate(lumivals):
143  ypoints[label].append(sum(lumivals[0:i+1])/denomitor)#integrated lumi
144  ytotal[label]=sum(lumivals)/denomitor
145  xpoints=[t[0] for t in rawdata[referenceLabel]]
146  ax=self.__fig.add_subplot(111)
147  if yscale=='linear':
148  ax.set_yscale('linear')
149  elif yscale=='log':
150  ax.set_yscale('log')
151  else:
152  raise 'unsupported yscale ',yscale
153  ax.set_xlabel(r'Run',position=(0.95,0))
154  ax.set_ylabel(r'L '+unitstring,position=(0,0.9))
155  xticklabels=ax.get_xticklabels()
156  for tx in xticklabels:
157  tx.set_rotation(30)
158  majorLocator=matplotlib.ticker.LinearLocator( nticks )
159  majorFormatter=matplotlib.ticker.FormatStrFormatter('%d')
160  minorLocator=matplotlib.ticker.LinearLocator(numticks=4*nticks)
161  ax.xaxis.set_major_locator(majorLocator)
162  ax.xaxis.set_major_formatter(majorFormatter)
163  ax.xaxis.set_minor_locator(minorLocator)
164  ax.set_xbound(lower=xpoints[0],upper=xpoints[-1])
165  ax.grid(True)
166  keylist=ypoints.keys()
167  keylist.sort()
168  keylist.insert(0,keylist.pop(keylist.index(referenceLabel)))#move refereceLabel to front from now on
169  legendlist=[]
170  head=['#Run']
171  textsummaryhead=['#TotalRun']
172  textsummaryline=['#'+str(len(xpoints))]
173  for ylabel in keylist:
174  cl='k'
175  if self.colormap.has_key(ylabel):
176  cl=self.colormap[ylabel]
177  ax.plot(xpoints,ypoints[ylabel],label=ylabel,color=cl,drawstyle='steps')
178  legendlist.append(ylabel+' '+'%.3f'%(ytotal[ylabel])+' '+unitstring)
179  textsummaryhead.append('Total'+ylabel)
180  textsummaryline.append('%.3f'%(ytotal[ylabel])+' '+unitstring)
181  head.append(ylabel)
182  if textoutput:
183  csvreport=csvReporter.csvReporter(textoutput)
184  csvreport.writeRow(head)
185  allruns=[int(t[0]) for t in rawdata[referenceLabel]]
186  flat.insert(0,allruns)
187  rows=zip(*flat)
188  csvreport.writeRows([list(t) for t in rows])
189  csvreport.writeRow(textsummaryhead)
190  csvreport.writeRow(textsummaryline)
191  #font=FontProperties(size='medium',weight='demibold')
192  #legend
193  ax.legend(tuple(legendlist),loc='upper left')
194  #adjust
195  self.__fig.subplots_adjust(bottom=0.18,left=0.1)
196  #annotations
197  if withannotation:
198  trans=matplotlib.transforms.BlendedGenericTransform(ax.transData,ax.transAxes)
199  ax.text(xpoints[0],1.025,str(xpoints[0]),transform=trans,horizontalalignment='left',size='x-small',color='green',bbox=dict(facecolor='white'))
200  ax.text(xpoints[-1],1.025,str(xpoints[-1]),transform=trans,horizontalalignment='left',size='x-small',color='green',bbox=dict(facecolor='white'))
201 
202 
203  def plotSumX_Fill(self,rawdata={},resultlines=[],minFill=None,maxFill=None,nticks=6,yscale='linear',withannotation=False,referenceLabel='Delivered',labels=['Delivered','Recorded'],textoutput=None):
204  '''
205  input:
206  rawdata = {'Delivered':[(fill,runnumber,lumiperrun)],'Recorded':[(fill,runnumber,lumiperrun)]}
207  resultlines = [[fillnumber,runnumber,dellumiperrun,reclumiperrun],[fillnumber,runnumber,dellumiperrun,reclumiperrun],]
208  minFill : min fill to draw
209  maxFill : max fill to draw
210  yscale: linear,log or both
211  withannotation: wheather the boundary points should be annotated
212  textoutput: text output file name.
213  '''
214  ytotal={}
215  ypoints={}
216  for r in resultlines: #parse old text data
217  fillnum=int(r[0])
218  runnum=int(r[1])
219  if rawdata and (fillnum,runnum) in [(t[0],t[1]) for t in rawdata[referenceLabel]]:continue
220  if minFill and fillnum<minFill:continue
221  if maxFill and fillnum>maxFill:continue
222  for i,lab in enumerate(labels) :
223  v=float(r[-(len(labels)-i)])#the values to plot are always the last n fields
224  rawdata.setdefault(lab,[]).append((fillnum,runnum,v))
225  #print 'fillrunDict ',fillrunDict
226  if not rawdata:
227  print '[WARNING]: no data, do nothing'
228  return
229  tot=sum([t[2] for t in rawdata[referenceLabel]])
230  beginfo=''
231  endinfo=''
232  (unitstring,denomitor)=guessLumiUnit(tot)
233  csvreport=None
234  rows=[]
235  flat=[]
236  for label,yvalues in rawdata.items():
237  yvalues.sort()
238  flat.append([t[2] for t in yvalues])
239  ypoints[label]=[]
240  ytotal[label]=0.0
241  lumivals=[t[2] for t in yvalues]
242  for i,val in enumerate(lumivals):
243  ypoints[label].append(sum(lumivals[0:i+1])/denomitor)
244  ytotal[label]=sum(lumivals)/denomitor
245  xpoints=[t[0] for t in rawdata[referenceLabel]]#after sort
246  ax=self.__fig.add_subplot(111)
247  ax.set_xlabel(r'LHC Fill Number',position=(0.84,0))
248  ax.set_ylabel(r'L '+unitstring,position=(0,0.9))
249  ax.set_xbound(lower=xpoints[0],upper=xpoints[-1])
250  if yscale=='linear':
251  ax.set_yscale('linear')
252  elif yscale=='log':
253  ax.set_yscale('log')
254  else:
255  raise 'unsupported yscale ',yscale
256  xticklabels=ax.get_xticklabels()
257  majorLocator=matplotlib.ticker.LinearLocator( nticks )
258  majorFormatter=matplotlib.ticker.FormatStrFormatter('%d')
259  #minorLocator=matplotlib.ticker.MultipleLocator(sampleinterval)
260  ax.xaxis.set_major_locator(majorLocator)
261  ax.xaxis.set_major_formatter(majorFormatter)
262  #ax.xaxis.set_minor_locator(minorLocator)
263  ax.grid(True)
264  keylist=ypoints.keys()
265  keylist.sort()
266  keylist.insert(0,keylist.pop(keylist.index(referenceLabel)))#move refereceLabel to front from now on
267  legendlist=[]
268  head=['#fill','run']
269  textsummaryhead=['#TotalFill']
270  textsummaryline=['#'+str(len(xpoints))]
271  for ylabel in keylist:
272  cl='k'
273  if self.colormap.has_key(ylabel):
274  cl=self.colormap[ylabel]
275  ax.plot(xpoints,ypoints[ylabel],label=ylabel,color=cl,drawstyle='steps')
276  legendlist.append(ylabel+' '+'%.3f'%(ytotal[ylabel])+' '+unitstring)
277  textsummaryhead.append('Total'+ylabel)
278  textsummaryline.append('%.3f'%(ytotal[ylabel])+' '+unitstring)
279  head.append(ylabel)
280  if textoutput:
281  csvreport=csvReporter.csvReporter(textoutput)
282  allfills=[int(t[0]) for t in rawdata[referenceLabel]]
283  allruns=[int(t[1]) for t in rawdata[referenceLabel]]
284  flat.insert(0,allfills)
285  flat.insert(1,allruns)
286  rows=zip(*flat)
287  csvreport.writeRow(head)
288  csvreport.writeRows([list(t) for t in rows])
289  csvreport.writeRow(textsummaryhead)
290  csvreport.writeRow(textsummaryline)
291  #font=FontProperties(size='medium',weight='demibold')
292  #annotations
293  if withannotation:
294  trans=matplotlib.transforms.BlendedGenericTransform(ax.transData,ax.transAxes)
295  ax.text(xpoints[0],1.025,beginfo,transform=trans,horizontalalignment='left',size='x-small',color='green',bbox=dict(facecolor='white'))
296  ax.text(xpoints[-1],1.025,endinfo,transform=trans,horizontalalignment='left',size='x-small',color='green',bbox=dict(facecolor='white'))
297  #legend
298  ax.legend(tuple(legendlist),loc='upper left')
299  #adjust
300  self.__fig.subplots_adjust(bottom=0.1,left=0.1)
301 
302  def plotSumX_Time(self,rawdata={},resultlines=[],minTime=None,maxTime=None,nticks=6,yscale='linear',withannotation=False,referenceLabel='Delivered',labels=['Delivered','Recorded'],textoutput=None):
303  '''
304  input:
305  rawdata = {'Delivered':[(runnumber,starttimestamp,stoptimestamp,lumiperrun)],'Recorded':[(runnumber,starttimestamp,stoptimestamp,lumiperrun)]}
306  resultlines = [[runnumber,starttimestampStr,stoptimestampStr,dellumiperrun,reclumiperrun],[runnumber,starttimestampStr,stoptimestampStr,dellumiperrun,reclumiperrun],]
307  minTime (python DateTime) : min *begin* time to draw: format %m/%d/%y %H:%M:%S
308  maxTime (python DateTime): max *begin* time to draw %m/%d/%y %H:%M:%S
309  yscale: linear,log or both
310  withannotation: wheather the boundary points should be annotated
311  referenceLabel: the one variable that decides the total unit and the plot x-axis range
312  labels: labels of the variables to plot
313  '''
314  xpoints=[]
315  ypoints={}
316  ytotal={}
317  lut=lumiTime.lumiTime()
318  if not minTime:
319  minTime='03/01/10 00:00:00'
320  minTime=lut.StrToDatetime(minTime,customfm='%m/%d/%y %H:%M:%S')
321  if not maxTime:
322  maxTime=datetime.datetime.utcnow()
323  else:
324  maxTime=lut.StrToDatetime(maxTime,customfm='%m/%d/%y %H:%M:%S')
325  for r in resultlines:
326  runnumber=int(r[0])
327  starttimeStr=r[1].split('.')[0]
328  starttime=lut.StrToDatetime(starttimeStr,customfm='%Y-%m-%d %H:%M:%S')
329  stoptimeStr=r[2].split('.')[0]
330  stoptime=lut.StrToDatetime(stoptimeStr,customfm='%Y-%m-%d %H:%M:%S')
331  if rawdata and runnumber in [t[0] for t in rawdata[referenceLabel]]:continue
332  if starttime<minTime:continue
333  if starttime>maxTime:continue
334 
335  for i,lab in enumerate(labels):
336  v=float(r[-(len(labels)-i)])
337  rawdata.setdefault(lab,[]).append((runnumber,starttime,stoptime,v))
338  if not rawdata:
339  print '[WARNING]: no data, do nothing'
340  return
341  tot=sum([t[3] for t in rawdata[referenceLabel]])
342  (unitstring,denomitor)=guessLumiUnit(tot)
343  csvreport=None
344  rows=[]
345  flat=[]
346  for label,yvalues in rawdata.items():
347  yvalues.sort()
348  flat.append([t[3] for t in yvalues])
349  if label==referenceLabel:
350  minTime=yvalues[0][1]
351  maxTime=yvalues[-1][1]
352  ypoints[label]=[]
353  lumivals=[t[3] for t in yvalues]
354  for i,val in enumerate(lumivals):
355  ypoints[label].append(sum(lumivals[0:i+1])/denomitor)
356  ytotal[label]=sum(lumivals)/denomitor
357  xpoints=[matplotlib.dates.date2num(t[1]) for t in rawdata[referenceLabel]]
358  ax=self.__fig.add_subplot(111)
359  ax.set_yscale(yscale)
360  yearStrMin=minTime.strftime('%Y')
361  yearStrMax=maxTime.strftime('%Y')
362  if yearStrMin==yearStrMax:
363  dateFmt=matplotlib.dates.DateFormatter('%d/%m')
364  else:
365  dateFmt=matplotlib.dates.DateFormatter('%d/%m/%y')
366  majorLoc=matplotlib.ticker.LinearLocator(numticks=nticks)
367  ax.xaxis.set_major_locator(majorLoc)
368  minorLoc=matplotlib.ticker.LinearLocator(numticks=nticks*4)
369  ax.xaxis.set_major_formatter(dateFmt)
370  ax.set_xlabel(r'Date',position=(0.84,0))
371  ax.set_ylabel(r'L '+unitstring,position=(0,0.9))
372  ax.xaxis.set_minor_locator(minorLoc)
373  ax.set_xbound(lower=xpoints[0],upper=xpoints[-1])
374  xticklabels=ax.get_xticklabels()
375  for tx in xticklabels:
376  tx.set_horizontalalignment('left')
377  ax.grid(True)
378  keylist=ypoints.keys()
379  keylist.sort()
380  keylist.insert(0,keylist.pop(keylist.index(referenceLabel)))#move refereceLabel to front from now on
381  legendlist=[]
382  head=['#Run','StartTime','StopTime']
383  textsummaryhead=['#TotalRun']
384  textsummaryline=['#'+str(len(xpoints))]
385  for ylabel in keylist:
386  cl='k'
387  if self.colormap.has_key(ylabel):
388  cl=self.colormap[ylabel]
389  ax.plot(xpoints,ypoints[ylabel],label=ylabel,color=cl,drawstyle='steps')
390  legendlist.append(ylabel+' '+'%.3f'%(ytotal[ylabel])+' '+unitstring)
391  textsummaryhead.append('Total'+ylabel)
392  textsummaryline.append('%.3f'%(ytotal[ylabel])+' '+unitstring)
393  head.append(ylabel)
394  if textoutput:
395  csvreport=csvReporter.csvReporter(textoutput)
396  csvreport.writeRow(head)
397  allruns=[int(t[0]) for t in rawdata[referenceLabel]]
398  allstarts=[ lut.DatetimeToStr(t[1],customfm='%Y-%m-%d %H:%M:%S') for t in rawdata[referenceLabel] ]
399  allstops=[ lut.DatetimeToStr(t[2],customfm='%Y-%m-%d %H:%M:%S') for t in rawdata[referenceLabel] ]
400  flat.insert(0,allruns)
401  flat.insert(1,allstarts)
402  flat.insert(2,allstops)
403  rows=zip(*flat)
404  csvreport.writeRows([list(t) for t in rows])
405  csvreport.writeRow(textsummaryhead)
406  csvreport.writeRow(textsummaryline)
407  #annotations
408  trans=matplotlib.transforms.BlendedGenericTransform(ax.transData,ax.transAxes)
409  #print 'run boundary ',runs[0],runs[-1]
410  #print 'xpoints boundary ',xpoints[0],xpoints[-1]
411  #annotation
412  if withannotation:
413  runs=[t[0] for t in rawdata[referenceLabel]]
414  ax.text(xpoints[0],1.025,str(runs[0]),transform=trans,horizontalalignment='left',size='x-small',color='green',bbox=dict(facecolor='white'))
415  ax.text(xpoints[-1],1.025,str(runs[-1]),transform=trans,horizontalalignment='left',size='x-small',color='green',bbox=dict(facecolor='white'))
416 
417  if yearStrMin==yearStrMax:
418  firsttimeStr=rawdata[referenceLabel][1][1].strftime('%b %d %H:%M') #time range(start) in the title is the first run beg time
419  lasttimeStr=rawdata[referenceLabel][-1][2].strftime('%b %d %H:%M') #time range(stop) in the tile is the last run stop time
420  #firstimeStr=minTime.strftime('%b %d %H:%M')
421  #lasttimeStr=maxTime.strftime('%b %d %H:%M')
422  #ax.set_title('CMS Total Integrated Luminosity '+yearStrMin+' ('+firstimeStr+' - '+lasttimeStr+' UTC)',size='small',family='fantasy')
423  ax.set_title('CMS Total Integrated Luminosity '+yearStrMin+' ('+firsttimeStr+' - '+lasttimeStr+' UTC)',size='small')
424  else:
425  #ax.set_title('CMS Total Integrated Luminosity '+yearStrMin+'-'+yearStrMax,size='small',family='fantasy')
426  ax.set_title('CMS Total Integrated Luminosity '+yearStrMin+'-'+yearStrMax,size='small')
427  ax.legend(tuple(legendlist),loc='upper left')
428  ax.autoscale_view(tight=True,scalex=True,scaley=False)
429  self.__fig.autofmt_xdate(bottom=0.18,rotation=15,ha='right')
430  self.__fig.subplots_adjust(bottom=0.2,left=0.15)
431 
432  def plotPerdayX_Time(self,rawdata={},resultlines=[],minTime=None,maxTime=None,nticks=6,yscale='linear',withannotation=False,referenceLabel='Delivered',labels=['Delivered','Recorded'],textoutput=None):
433  '''
434  Input:
435  rawdata={'Delivered':[(day,begrun:ls,endrun:ls,lumi)],'Recorded':[(dayofyear,begrun:ls,endrun:ls,lumi)]}
436  resultlines=[[day,begrun:ls,endrun:ls,deliveredperday,recordedperday],[]]
437  minTime (python DateTime) : min *begin* time to draw: format %m/%d/%y %H:%M:%S
438  maxTime (python DateTime): max *begin* time to draw %m/%d/%y %H:%M:%S
439  withannotation: wheather the boundary points should be annotated
440  referenceLabel: the one variable that decides the total unit and the plot x-axis range
441  labels: labels of the variables to plot
442  '''
443  xpoints=[]
444  ypoints={}
445  ymax={}
446  lut=lumiTime.lumiTime()
447  if not minTime:
448  minTime='03/01/10 00:00:00'
449  minTime=lut.StrToDatetime(minTime,customfm='%m/%d/%y %H:%M:%S')
450  if not maxTime:
451  maxTime=datetime.datetime.utcnow()
452  else:
453  maxTime=lut.StrToDatetime(maxTime,customfm='%m/%d/%y %H:%M:%S')
454  for r in resultlines:
455  day=int(r[0])
456  begrunls=r[1]
457  endrunls=r[2]
458  #[begrun,begls]=[int(s) for s in r[1].split(':')]
459  if rawdata and day in [t[0] for t in rawdata[referenceLabel]]:continue
460  if day < minTime.date().toordinal():continue
461  if day > maxTime.date().toordinal():continue
462  for i,lab in enumerate(labels):
463  v=float(r[-(len(labels)-i)-1])
464  rawdata.setdefault(lab,[]).append((day,begrunls,endrunls,v))
465  if not rawdata:
466  print '[WARNING]: no data, do nothing'
467  return
468  maxlum=max([t[3] for t in rawdata[referenceLabel]])
469  minlum=min([t[3] for t in rawdata[referenceLabel] if t[3]>0]) #used only for log scale, fin the non-zero bottom
470  (unitstring,denomitor)=guessLumiUnit(maxlum)
471  csvreport=None
472  rows=[]
473  flat=[]
474  MinDay=minTime.date().toordinal()
475  MaxDay=maxTime.date().toordinal()
476  fulldays=range(MinDay,MaxDay+1)
477  allstarts=[]
478  allstops=[]
479  for label,yvalues in rawdata.items():
480  yvalues.sort()
481  flat.append([t[3] for t in yvalues])
482  alldays=[t[0] for t in yvalues]
483  alldates=[str(datetime.date.fromordinal(t)) for t in alldays]
484  ypoints[label]=[]
485  lumivals=[t[3] for t in yvalues]
486  for d in fulldays:
487  if not d in alldays:
488  ypoints[label].append(0.0)
489  else:
490  thisdaylumi=[t[3] for t in yvalues if t[0]==d][0]
491  if yscale=='log':
492  if thisdaylumi<minlum:
493  thisdaylumi=minlum/denomitor
494  else:
495  thisdaylumi=thisdaylumi/denomitor
496  else:
497  thisdaylumi=thisdaylumi/denomitor
498  ypoints[label].append(thisdaylumi)
499  ymax[label]=max(lumivals)/denomitor
500  xpoints=fulldays
501  if textoutput:
502  csvreport=csvReporter.csvReporter(textoutput)
503  head=['#day','begrunls','endrunls','delivered','recorded','date']
504  csvreport.writeRow(head)
505  flat.insert(0,alldays)
506  allstarts=[ t[1] for t in rawdata[referenceLabel]]
507  allstops=[ t[2] for t in rawdata[referenceLabel]]
508  #print 'allstarts ',allstarts
509  flat.insert(1,allstarts)
510  flat.insert(2,allstops)
511  flat.append(alldates)
512  rows=zip(*flat)
513  csvreport.writeRows([list(t) for t in rows])
514  yearStrMin=minTime.strftime('%Y')
515  yearStrMax=maxTime.strftime('%Y')
516  if yearStrMin==yearStrMax:
517  dateFmt=matplotlib.dates.DateFormatter('%d/%m')
518  else:
519  dateFmt=matplotlib.dates.DateFormatter('%d/%m/%y')
520  ax=self.__fig.add_subplot(111)
521  if yscale=='linear':
522  ax.set_yscale('linear')
523  elif yscale=='log':
524  ax.set_yscale('log')
525  else:
526  raise 'unsupported yscale ',yscale
527  majorLoc=matplotlib.ticker.LinearLocator(numticks=nticks)
528  minorLoc=matplotlib.ticker.LinearLocator(numticks=nticks*4)
529  ax.xaxis.set_major_formatter(dateFmt)
530  ax.set_xlabel(r'Date',position=(0.84,0))
531  ax.xaxis.set_major_locator(majorLoc)
532  ax.xaxis.set_minor_locator(minorLoc)
533  xticklabels=ax.get_xticklabels()
534  for tx in xticklabels:
535  tx.set_horizontalalignment('right')
536  ax.grid(True)
537  legendlist=[]
538  ax.set_ylabel(r'L '+unitstring,position=(0,0.9))
539  textsummaryhead=['#TotalRunningDays']
540  textsummaryline=['#'+str(len(alldays))]
541  for ylabel in labels:
542  cl='k'
543  if self.colormap.has_key(ylabel):
544  cl=self.colormap[ylabel]
545  ax.plot(xpoints,ypoints[ylabel],label=ylabel,color=cl,drawstyle='steps')
546  legendlist.append(ylabel+' Max '+'%.3f'%(ymax[ylabel])+' '+unitstring)
547  textsummaryhead.append('Max'+ylabel)
548  textsummaryline.append('%.3f'%(ymax[ylabel])+' '+unitstring)
549  if textoutput:
550  csvreport.writeRow(textsummaryhead)
551  csvreport.writeRow(textsummaryline)
552  ax.legend(tuple(legendlist),loc='upper left')
553  ax.set_xbound(lower=matplotlib.dates.date2num(minTime),upper=matplotlib.dates.date2num(maxTime))
554  #if withannotation:
555  # begtime=boundaryInfo[0][0]
556  # beginfo=boundaryInfo[0][1]
557  # endtime=boundaryInfo[1][0]
558  # endinfo=boundaryInfo[1][1]
559  # #annotations
560  # trans=matplotlib.transforms.BlendedGenericTransform(ax.transData,ax.transAxes)
561  # ax.text(matplotlib.dates.date2num(begtime),1.025,beginfo,transform=trans,horizontalalignment='left',size='x-small',color='green',bbox=dict(facecolor='white'))
562  # ax.text(matplotlib.dates.date2num(endtime),1.025,endinfo,transform=trans,horizontalalignment='left',size='x-small',color='green',bbox=dict(facecolor='white'))
563 
564  firstday=datetime.date.fromordinal(rawdata[referenceLabel][0][0])
565  lastday=datetime.date.fromordinal(rawdata[referenceLabel][-1][0])
566  firstdayStr=firstday.strftime('%Y %b %d')
567  lastdayStr=lastday.strftime('%Y %b %d')
568  ax.set_title('CMS Integrated Luminosity/Day ('+firstdayStr+' - '+lastdayStr+')',size='small')
569  #ax.autoscale(tight=True)
570  ax.autoscale_view(tight=True,scalex=True,scaley=False)
571  #ax.set_xmargin(0.015)
572  self.__fig.autofmt_xdate(bottom=0.18,rotation=15,ha='right')
573  self.__fig.subplots_adjust(bottom=0.2,left=0.15)
574 
575  def plotPeakPerday_Time(self,rawdata={},resultlines=[],minTime=None,maxTime=None,nticks=6,withannotation=False,yscale='linear',referenceLabel='Delivered',labels=['Delivered'],textoutput=None):
576  '''
577  THIS PLOT IS DELIVERED ONLY
578  Input:
579  rawdata={'Delivered':[(day,run,ls,instlumi)]}
580  resultlines=[[day,run,ls,maxinstlum],[]]
581  minTime (python DateTime) : min *begin* time to draw: format %m/%d/%y %H:%M:%S
582  maxTime (python DateTime): max *begin* time to draw %m/%d/%y %H:%M:%S
583  withannotation: wheather the boundary points should be annotated
584  referenceLabel: the one variable that decides the total unit and the plot x-axis range
585  labels: labels of the variables to plot
586  '''
587  xpoints=[]
588  ypoints={}
589  legendlist=[]
590  maxinfo=''
591  ymax={}
592  lut=lumiTime.lumiTime()
593  if not minTime:
594  minTime='03/01/10 00:00:00'
595  minTime=lut.StrToDatetime(minTime,customfm='%m/%d/%y %H:%M:%S')
596  if not maxTime:
597  maxTime=datetime.datetime.utcnow()
598  else:
599  maxTime=lut.StrToDatetime(maxTime,customfm='%m/%d/%y %H:%M:%S')
600  for r in resultlines:
601  day=int(r[0])
602  runnumber=int(r[1])
603  lsnum=int(r[2].split('.')[0])
604  if rawdata and day in [int(t[0]) for t in rawdata[referenceLabel]]:continue
605  if day < minTime.date().toordinal():continue
606  if day > maxTime.date().toordinal():continue
607  for i,lab in enumerate(labels):
608  v=float(r[-(len(labels)-i)-1])
609  rawdata.setdefault(lab,[]).append((day,runnumber,lsnum,v))
610  if not rawdata:
611  print '[WARNING]: no data, do nothing'
612  return
613  maxlum=max([t[3] for t in rawdata[referenceLabel]])
614  minlum=min([t[3] for t in rawdata[referenceLabel] if t[3]>0]) #used only for log scale, fin the non-zero bottom
615  (unitstring,denomitor)=guessInstLumiUnit(maxlum)
616 
617  csvreport=None
618  rows=[]
619  flat=[]
620  MinDay=minTime.date().toordinal()
621  MaxDay=maxTime.date().toordinal()
622  fulldays=range(MinDay,MaxDay+1)
623  for label in rawdata.keys():
624  yvalues=rawdata[label]
625  yvalues.sort()#sort by day
626  alldays=[t[0] for t in yvalues]
627  alldates=[str(datetime.date.fromordinal(t)) for t in alldays]
628  ypoints[label]=[]
629  lumivals=[t[3] for t in yvalues]
630  flat.append(lumivals)
631  for d in fulldays:
632  if not d in alldays:
633  ypoints[label].append(0.0)
634  else:
635  thisdaylumi=[t[3] for t in yvalues if t[0]==d][0]
636  if yscale=='log':
637  if thisdaylumi<minlum:
638  thisdaylumi=minlum/denomitor
639  else:
640  thisdaylumi=thisdaylumi/denomitor
641  else:
642  thisdaylumi=thisdaylumi/denomitor
643  ypoints[label].append(thisdaylumi)
644  ymax[label]=max(lumivals)/denomitor
645  'ymax ',max(lumivals)
646  xpoints=fulldays
647  if textoutput:
648  csvreport=csvReporter.csvReporter(textoutput)
649  head=['#day','run','lsnum','maxinstlumi','date']
650  csvreport.writeRow(head)
651  flat.insert(0,alldays)
652  allruns=[ t[1] for t in rawdata[referenceLabel]]
653  allls=[ t[2] for t in rawdata[referenceLabel]]
654  flat.insert(1,allruns)
655  flat.insert(2,allls)
656  flat.append(alldates)
657  rows=zip(*flat)
658  csvreport.writeRows([list(t) for t in rows])
659 
660  yearStrMin=minTime.strftime('%Y')
661  yearStrMax=maxTime.strftime('%Y')
662  if yearStrMin==yearStrMax:
663  dateFmt=matplotlib.dates.DateFormatter('%d/%m')
664  else:
665  dateFmt=matplotlib.dates.DateFormatter('%d/%m/%y')
666  ax=self.__fig.add_subplot(111)
667  if yscale=='linear':
668  ax.set_yscale('linear')
669  elif yscale=='log':
670  ax.set_yscale('log')
671  else:
672  raise 'unsupported yscale ',yscale
673  majorLoc=matplotlib.ticker.LinearLocator(numticks=nticks)
674  minorLoc=matplotlib.ticker.LinearLocator(numticks=nticks*4)
675  ax.xaxis.set_major_formatter(dateFmt)
676  ax.set_xlabel(r'Date',position=(0.84,0))
677  ax.set_ylabel(r'L '+unitstring,position=(0,0.9))
678  ax.xaxis.set_major_locator(majorLoc)
679  ax.xaxis.set_minor_locator(minorLoc)
680  xticklabels=ax.get_xticklabels()
681  for tx in xticklabels:
682  tx.set_horizontalalignment('right')
683  ax.grid(True)
684  cl=self.colormap['Max Inst']
685  textsummaryhead=['#TotalRunningDays']
686  textsummaryline=['#'+str(len(alldays))]
687  for ylabel in labels:
688  cl='k'
689  if self.colormap.has_key(ylabel):
690  cl=self.colormap[ylabel]
691  ax.plot(xpoints,ypoints[ylabel],label='Max Inst',color=cl,drawstyle='steps')
692  legendlist.append('Max Inst %.3f'%(ymax[ylabel])+' '+unitstring)
693  textsummaryhead.append('Max Inst'+ylabel)
694  textsummaryline.append('%.3f'%(ymax[ylabel])+' '+unitstring)
695  if textoutput:
696  csvreport.writeRow(textsummaryhead)
697  csvreport.writeRow(textsummaryline)
698  ax.legend(tuple(legendlist),loc='upper left')
699  ax.set_xbound(lower=matplotlib.dates.date2num(minTime),upper=matplotlib.dates.date2num(maxTime))
700  if withannotation:
701  #annotations
702  trans=matplotlib.transforms.BlendedGenericTransform(ax.transData,ax.transAxes)
703  ax.text(xpoints[0],1.025,beginfo,transform=trans,horizontalalignment='left',size='x-small',color='green',bbox=dict(facecolor='white'))
704  ax.text(xpoints[-1],1.025,endinfo,transform=trans,horizontalalignment='left',size='x-small',color='green',bbox=dict(facecolor='white'))
705  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'))
706 
707  firstday=datetime.date.fromordinal(rawdata[referenceLabel][0][0])
708  lastday=datetime.date.fromordinal(rawdata[referenceLabel][-1][0])
709  firstdayStr=firstday.strftime('%Y %b %d')
710  lastdayStr=lastday.strftime('%Y %b %d')
711  ax.set_title('CMS Peak Luminosity/Day ('+firstdayStr+' - '+lastdayStr+')',size='small')
712 
713  #ax.autoscale(tight=True)
714  ax.autoscale_view(tight=True,scalex=True,scaley=False)
715  #ax.set_xmargin(0.015)
716  self.__fig.autofmt_xdate(bottom=0.18,rotation=15,ha='right')
717  self.__fig.subplots_adjust(bottom=0.2,left=0.15)
718 
719  def plotInst_RunLS(self,rawxdata,rawydata,nticks=6,textoutput=None):
720  '''
721  Input: rawxdata [run,fill,starttime,stoptime,totalls,ncmsls]
722  rawydata {label:[lumi]}
723  '''
724  lslength=23.357
725  lut=lumiTime.lumiTime()
726  runnum=rawxdata[0]
727  fill=rawxdata[1]
728  starttime=lut.DatetimeToStr(rawxdata[2],customfm='%m/%d/%y %H:%M:%S')
729  stoptime=lut.DatetimeToStr(rawxdata[3],customfm='%m/%d/%y %H:%M:%S')
730  totalls=rawxdata[-2]
731  ncmsls=rawxdata[-1]
732  peakinst=max(rawydata['Delivered'])/lslength
733  totaldelivered=sum(rawydata['Delivered'])
734  totalrecorded=sum(rawydata['Recorded'])
735  xpoints=range(1,totalls+1)
736  #print len(xpoints)
737  ypoints={}
738  ymax={}
739  for ylabel,yvalue in rawydata.items():
740  ypoints[ylabel]=[y/lslength for y in yvalue]
741  ymax[ylabel]=max(yvalue)/lslength
742  left=0.15
743  width=0.7
744  bottom=0.1
745  height=0.65
746  bottom_h=bottom+height
747  rect_scatter=[left,bottom,width,height]
748  rect_table=[left,bottom_h,width,0.25]
749 
750  nullfmt=matplotlib.ticker.NullFormatter()
751  nullloc=matplotlib.ticker.NullLocator()
752  axtab=self.__fig.add_axes(rect_table,frameon=False)
753  axtab.set_axis_off()
754  axtab.xaxis.set_major_formatter(nullfmt)
755  axtab.yaxis.set_major_formatter(nullfmt)
756  axtab.xaxis.set_major_locator(nullloc)
757  axtab.yaxis.set_major_locator(nullloc)
758 
759  ax=self.__fig.add_axes(rect_scatter)
760 
761  majorLoc=matplotlib.ticker.LinearLocator(numticks=nticks)
762  minorLoc=matplotlib.ticker.LinearLocator(numticks=nticks*4)
763  ax.set_xlabel(r'LS',position=(0.96,0))
764  ax.set_ylabel(r'L $\mu$b$^{-1}$s$^{-1}$',position=(0,0.9))
765  ax.xaxis.set_major_locator(majorLoc)
766  ax.xaxis.set_minor_locator(minorLoc)
767  ax.set_xbound(lower=xpoints[0],upper=xpoints[-1])
768  xticklabels=ax.get_xticklabels()
769  for tx in xticklabels:
770  tx.set_horizontalalignment('right')
771  ax.grid(True)
772  keylist=ypoints.keys()
773  keylist.sort()
774  legendlist=[]
775 
776  for ylabel in keylist:
777  cl='k'
778  if self.colormap.has_key(ylabel):
779  cl=self.colormap[ylabel]
780  ax.plot(xpoints,ypoints[ylabel],'.',label=ylabel,color=cl)
781  legendlist.append(ylabel)
782  #ax.axhline(0,color='green',linewidth=0.2)
783  ax.axvline(xpoints[ncmsls-1],color='green',linewidth=0.2)
784  (unitstring,denomitor)=guessLumiUnit(totaldelivered)
785  colLabels=('run','fill','max inst(/$\mu$b/s)','delivered('+unitstring+')','recorded('+unitstring+')')
786  cellText=[[str(runnum),str(fill),'%.3f'%(peakinst),'%.3f'%(totaldelivered/denomitor),'%.3f'%(totalrecorded/denomitor)]]
787 
788  sumtable=axtab.table(cellText=cellText,colLabels=colLabels,colWidths=[0.12,0.1,0.27,0.27,0.27],cellLoc='center',loc='center')
789  trans=matplotlib.transforms.BlendedGenericTransform(ax.transData,ax.transAxes)
790  axtab.add_table(sumtable)
791 
792  ax.text(xpoints[0],1.02,starttime[0:17],transform=trans,horizontalalignment='left',size='x-small',color='green',bbox=dict(facecolor='white'))
793  ax.text(xpoints[ncmsls-1],1.02,stoptime[0:17],transform=trans,horizontalalignment='left',size='x-small',color='green',bbox=dict(facecolor='white'))
794  ax.legend(tuple(legendlist),loc='upper right',numpoints=1)
795 
796  def drawHTTPstring(self):
797  self.__canvas=CanvasBackend(self.__fig)
798  cherrypy.response.headers['Content-Type']='image/png'
799  buf=StringIO()
800  self.__canvas.print_png(buf)
801  return buf.getvalue()
802 
803  def drawPNG(self,filename):
804  self.__canvas=CanvasBackend(self.__fig)
805  self.__canvas.print_figure(filename)
806 
807  def drawInteractive(self):
808  if batchonly:
809  print 'interactive mode is not available for your setup, exit'
810  sys.exit()
812  aw.show()
813  aw.destroy()
814 
815 if __name__=='__main__':
816  import csv
817  print '=====testing plotSumX_Run======'
818  f=open('/afs/cern.ch/cms/lumi/www/plots/operation/totallumivsrun-2011.csv','r')
819  reader=csv.reader(f,delimiter=',')
820  resultlines=[]
821  for row in reader:
822  if not row[0].isdigit():continue
823  resultlines.append(row)
824  #print resultlines
825  fig=Figure(figsize=(7.2,5.4),dpi=120)
827  m.plotSumX_Run(rawdata={},resultlines=resultlines,minRun=None,maxRun=None,nticks=6,yscale='linear',withannotation=False)
828  #m.drawPNG('totallumivsrun-2011test.png')
829  m.drawInteractive()
830  print 'DONE'
831 
832 '''
833  print '=====testing plotSumX_Fill======'
834  f=open('/afs/cern.ch/cms/lumi/www/plots/operation/totallumivsfill-2011.csv','r')
835  reader=csv.reader(f,delimiter=',')
836  resultlines=[]
837  for row in reader:
838  if not row[0].isdigit():continue
839  resultlines.append(row)
840  #print resultlines
841  fig=Figure(figsize=(7.2,5.4),dpi=120)
842  m=matplotRender(fig)
843  m.plotSumX_Fill(rawdata={},resultlines=resultlines,minFill=None,maxFill=None,nticks=6,yscale='linear',withannotation=True)
844  m.drawPNG('totallumivsfill-2011test.png')
845  print 'DONE'
846  print '=====testing plotSumX_Time======'
847  f=open('/afs/cern.ch/cms/lumi/www/publicplots/totallumivstime-2011.csv','r')
848  reader=csv.reader(f,delimiter=',')
849  resultlines=[]
850  for row in reader:
851  if not row[0].isdigit():continue
852  resultlines.append(row)
853  #print resultlines
854  fig=Figure(figsize=(7.25,5.4),dpi=120)
855  m=matplotRender(fig)
856  m.plotSumX_Time(rawdata={},resultlines=resultlines,minTime="03/14/11 09:00:00",maxTime=None,nticks=6,yscale='linear',withannotation=False)
857  m.drawPNG('totallumivstime-2011test.png')
858  print 'DONE'
859 
860  print '=====testing plotPerdayX_Time======'
861  f=open('/afs/cern.ch/cms/lumi/www/publicplots/lumiperday-2011.csv','r')
862  reader=csv.reader(f,delimiter=',')
863  resultlines=[]
864  for row in reader:
865  if not row[0].isdigit():continue
866  resultlines.append(row)
867  #print resultlines
868  fig=Figure(figsize=(7.25,5.4),dpi=120)
869  m=matplotRender(fig)
870  m.plotPerdayX_Time(rawdata={},resultlines=resultlines,minTime="03/14/11 09:00:00",maxTime=None,nticks=6,yscale='linear',withannotation=False)
871  m.drawPNG('lumiperday-2011test.png')
872  print 'DONE'
873 
874  print '=====testing plotPeakPerday_Time======'
875  f=open('/afs/cern.ch/cms/lumi/www/publicplots/lumipeak-2011.csv','r')
876  reader=csv.reader(f,delimiter=',')
877  resultlines=[]
878  for row in reader:
879  if not row[0].isdigit():continue
880  resultlines.append(row)
881  #print resultlines
882  fig=Figure(figsize=(7.25,5.4),dpi=120)
883  m=matplotRender(fig)
884  m.plotPeakPerday_Time(rawdata={},resultlines=resultlines,minTime="03/14/11 09:00:00",maxTime=None,nticks=6,yscale='linear',withannotation=False)
885  m.drawPNG('lumipeak-2011test.png')
886  print 'DONE'
887 
888 '''
tuple zip
Definition: archive.py:476
const T & max(const T &a, const T &b)
double split
Definition: MVATrainer.cc:139
How EventSelector::AcceptEvent() decides whether to accept an event for output otherwise it is excluding the probing of A single or multiple positive and the trigger will pass if any such matching triggers are PASS or EXCEPTION[A criterion thatmatches no triggers at all is detected and causes a throw.] A single negative with an expectation of appropriate bit checking in the decision and the trigger will pass if any such matching triggers are FAIL or EXCEPTION A wildcarded negative criterion that matches more than one trigger in the trigger list("!*","!HLTx*"if it matches 2 triggers or more) will accept the event if all the matching triggers are FAIL.It will reject the event if any of the triggers are PASS or EXCEPTION(this matches the behavior of"!*"before the partial wildcard feature was incorporated).Triggers which are in the READY state are completely ignored.(READY should never be returned since the trigger paths have been run