CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_9_patch3/src/RecoLuminosity/LumiDB/python/lumiReport.py

Go to the documentation of this file.
00001 ###########################################################
00002 # Luminosity/LumiTag/LumiCorrection report API            #
00003 #                                                         #
00004 # Author:      Zhen Xie                                   #
00005 ###########################################################
00006 
00007 import os,sys,time
00008 from RecoLuminosity.LumiDB import tablePrinter, csvReporter,CommonUtil
00009 from RecoLuminosity.LumiDB.wordWrappers import wrap_always, wrap_onspace, wrap_onspace_strict
00010 
00011 def dumptocsv(fieldnames,result,filename):
00012     '''
00013     utility method to dump result to csv file
00014     '''
00015     assert(filename)
00016     if filename.upper()=='STDOUT':
00017         r=sys.stdout
00018         r.write(','.join(fieldnames)+'\n')
00019         for l in result:
00020             r.write(str(l)+'\n')
00021     else:
00022         r=csvReporter.csvReporter(filename)
00023         r.writeRow(fieldnames)
00024         r.writeRows(result)
00025         r.close()
00026         
00027 def toScreenHeader(commandname,datatagname,normtag,worktag,updatetag,lumitype,toFile=None):
00028     '''
00029     input:
00030        commandname: commandname
00031        datataginfo: tagname
00032        normtag: normtag
00033        worktag: working version
00034        updatetag: updated version if amy
00035     '''
00036     gmtnowStr=time.asctime(time.gmtime())+' UTC'
00037     updatetagStr='None'
00038     if updatetag:
00039         updatetagStr=updatetag
00040     header=''.join(['*']*80)+'\n'
00041     header+='* '+gmtnowStr+'\n'
00042     header+='* lumitype: '+lumitype+' , datatag: '+datatagname+' , normtag: '+normtag+' , worktag: '+worktag+'\n'
00043     header+='* \n'
00044     header+='* by:\n'
00045     header+='* '+commandname+'\n'
00046     header+='* \n'
00047     header+='* update: '+updatetag+'\n'
00048     header+=''.join(['*']*80)+'\n'
00049     if not toFile:
00050         sys.stdout.write(header)
00051     else:
00052         assert(toFile)
00053         if toFile.upper()=='STDOUT':
00054             r=sys.stdout
00055         else:
00056             r=open(toFile,'wb')
00057         r.write(header)
00058         
00059 def toScreenNormSummary(allnorms):
00060     '''
00061     list all known norms summary
00062     input: {normname:[data_id(0),lumitype(1),istypedefault(2),comment(3),creationtime(4)]}
00063     '''
00064     result=[]
00065     labels=[('Name','Type','IsTypeDefault','Comment','CreationTime')]
00066     print ' ==  = '
00067     sorted_allnorms=sorted(allnorms.iteritems(),key=lambda x:x[0],reverse=True)
00068     for (normname,normvalues) in sorted_allnorms:
00069         lumitype=normvalues[1]
00070         istypedefault=str(normvalues[2])
00071         commentStr=normvalues[3]
00072         creationtime=normvalues[4]
00073         result.append([normname,lumitype,istypedefault,commentStr,creationtime])
00074     print tablePrinter.indent (labels+result, hasHeader = True, separateRows = False,prefix = '| ', postfix = ' |', justify = 'left',delim = ' | ', wrapfunc = lambda x: wrap_onspace (x,20) ) 
00075 
00076 def toScreenNormDetail(normname,norminfo,normvalues):
00077     '''
00078     list norm detail
00079     input:
00080         normname
00081         norminfo=[data_id[0],lumitype(1)istypedefault[2],comment[3],creationtime[4]]
00082         normvalues={since:[corrector(0),{paramname:paramvalue}(1),amodetag(2),egev(3),comment(4)]}
00083     '''
00084     lumitype=norminfo[1]
00085     istypedefault=norminfo[2]
00086     print '=========================================================='
00087     print '* Norm: '+normname
00088     print '* Type: '+lumitype
00089     print '* isDefault: '+str(istypedefault)
00090     print '=========================================================='
00091     labels=[('Since','Func','Parameters','amodetag','egev','comment')]
00092 
00093     result=[]
00094     print ' ==  = '
00095     for since in sorted(normvalues):
00096         normdata=normvalues[since]
00097         correctorStr=normdata[0]
00098         paramDict=normdata[1]
00099         paramDictStr=''
00100         count=0
00101         for pname in sorted(paramDict):
00102             pval=paramDict[pname]
00103             if count!=0:
00104                 paramDictStr+=' '
00105             try:
00106                 fpval=float(pval)
00107                 if fpval<1.:
00108                     paramDictStr+=pname+':'+'%.4f'%fpval
00109                 else:
00110                     paramDictStr+=pname+':'+'%.2f'%fpval
00111             except ValueError:
00112                 paramDictStr+=pname+':'+pval
00113             count+=1
00114         amodetag=normdata[2]
00115         egev=str(normdata[3])
00116         comment=normdata[4]
00117         result.append([str(since),correctorStr,paramDictStr,amodetag,egev,comment])
00118     print tablePrinter.indent (labels+result, hasHeader = True, separateRows = False,prefix = '| ', postfix = ' |', justify = 'left',delim = ' | ', wrapfunc = lambda x: wrap_onspace (x,40) ) 
00119 
00120 def toScreenTags(tagdata):
00121     result=[]
00122     labels=[('Name','Min Run','Max Run','Creation Time')]
00123     print ' ==  = '
00124     for tagid in sorted(tagdata):
00125         taginfo=tagdata[tagid]
00126         name=taginfo[0]
00127         minRun=str(taginfo[1])
00128         maxRun='Open'
00129         if taginfo[2]!=0:
00130             maxRun=str(taginfo[2])
00131         creationtime=taginfo[3]
00132         result.append([name,minRun,maxRun,creationtime])
00133     print tablePrinter.indent (labels+result, hasHeader = True, separateRows = False,prefix = '| ', postfix = ' |', justify = 'left',delim = ' | ', wrapfunc = lambda x: wrap_onspace (x,20) ) 
00134 
00135 def toScreenSingleTag(taginfo):
00136     '''
00137     input: {run:(lumidataid,trgdataid,hltdataid,comment)}
00138     '''
00139     result=[]
00140     labels=[('Run','Data Id','Insertion Time','Patch Comment')]
00141     print ' ==  = '
00142     for run in sorted(taginfo):
00143         (lumidataid,trgdataid,hltdataid,(ctimestr,comment))=taginfo[run]
00144         payloadid='-'.join([str(lumidataid),str(trgdataid),str(hltdataid)])
00145         result.append([str(run),payloadid,ctimestr,comment])
00146     print tablePrinter.indent (labels+result, hasHeader = True, separateRows = False,prefix = '| ', postfix = ' |', justify = 'left',delim = ' | ', wrapfunc = lambda x: wrap_onspace (x,25) )
00147     
00148 def toScreenTotDelivered(lumidata,resultlines,scalefactor,irunlsdict=None,noWarning=True,toFile=None):
00149     '''
00150     inputs:
00151     lumidata {run:[lumilsnum(0),cmslsnum(1),timestamp(2),beamstatus(3),beamenergy(4),deliveredlumi(5),calibratedlumierror(6),(bxidx,bxvalues,bxerrs)(7),(bxidx,b1intensities,b2intensities)(8),fillnum)(9)]}  
00152     resultlines [[resultrow1],[resultrow2],...,] existing result row
00153                 ('Run:Fill', 'N_LS','N_CMSLS','Delivered','UTCTime','E(GeV)')
00154     irunlsdict: run/ls selection list. irunlsdict=None means no filter
00155     '''
00156     result=[]
00157     totOldDeliveredLS=0
00158     totOldCMSLS=0
00159     totOldDelivered=0.0
00160     datarunlsdict={}#{run:[ls,...]}from data. construct it only if there is irunlsdict to compare with
00161     for r in resultlines:
00162         runfillstr=r[0]
00163         [runnumstr,fillnumstr]=runfillstr.split(':')
00164         if irunlsdict and not noWarning:
00165             if r[1] is not 'n/a':
00166                 datarunlsdict[int(runnumstr)]=[]
00167         dl=0.0
00168         if(r[3]!='n/a'):            #delivered
00169             dl=float(r[3])#in /ub because it comes from file!
00170             (rr,lumiu)=CommonUtil.guessUnit(dl)
00171             r[3]='%.3f'%(rr)+' ('+lumiu+')'            
00172         sls=0
00173         if(r[1]!='n/a'): #n_ls
00174             sls=int(r[1])
00175         totcmsls=0
00176         if(r[2]!='n/a'):#n_cmsls
00177             totcmsls=int(r[2])
00178         totOldDeliveredLS+=sls
00179         totOldCMSLS+=totcmsls
00180         totOldDelivered+=dl
00181         if(r[5]!='n/a'): #egev
00182             egv=float(r[5])
00183             r[5]='%.1f'%egv
00184         result.append(r)
00185     totls=0
00186     totcmsls=0
00187     totdelivered=0.0
00188     totaltable=[]    
00189     for run in lumidata.keys():
00190         lsdata=lumidata[run]
00191         if not lsdata:
00192             result.append([str(run)+':0','n/a','n/a','n/a','n/a','n/a'])
00193             if irunlsdict and not noWarning:
00194                 datarunlsdict[run]=None
00195             continue
00196         fillnum=0
00197         if lsdata[0] and lsdata[0][9]:
00198             fillnum=lsdata[0][9]
00199         deliveredData=[]
00200         nls=0
00201         existdata=[]
00202         selectedcmsls=[]
00203         for perlsdata in lsdata:
00204             lumilsnum=perlsdata[0]
00205             cmslsnum=perlsdata[1]
00206             if not noWarning:
00207                 if cmslsnum:
00208                     existdata.append(cmslsnum)
00209             if irunlsdict and irunlsdict[run]:
00210                 if lumilsnum and lumilsnum in irunlsdict[run]:
00211                     deliveredData.append(perlsdata[5])
00212                     if cmslsnum:
00213                         selectedcmsls.append(cmslsnum)
00214             else:
00215                 deliveredData.append(perlsdata[5])
00216                 if cmslsnum:                    
00217                     selectedcmsls.append(cmslsnum)
00218         datarunlsdict[run]=existdata
00219         nls=len(deliveredData)
00220         ncmsls=0
00221         if selectedcmsls:
00222             ncmsls=len(selectedcmsls)
00223             totcmsls+=ncmsls
00224         totls+=nls
00225         totlumi=sum(deliveredData)
00226         totdelivered+=totlumi
00227         (totlumival,lumiunit)=CommonUtil.guessUnit(totlumi)
00228         beamenergyPerLS=[float(x[4]) for x in lsdata if x[3]=='STABLE BEAMS']
00229         avgbeamenergy=0.0
00230         if len(beamenergyPerLS):
00231             avgbeamenergy=sum(beamenergyPerLS)/len(beamenergyPerLS)
00232         runstarttime='n/a'
00233         if lsdata[0] and lsdata[0][2]:
00234             runstarttime=lsdata[0][2]
00235             runstarttime=runstarttime.strftime("%m/%d/%y %H:%M:%S")
00236         if not toFile:
00237             result.append([str(run)+':'+str(fillnum),str(nls),str(ncmsls),'%.3f'%(totlumival*scalefactor)+' ('+lumiunit+')',runstarttime,'%.1f'%(avgbeamenergy)])
00238         else:
00239             result.append([str(run)+':'+str(fillnum),str(nls),str(ncmsls),(totlumi*scalefactor),runstarttime,'%.1f'%(avgbeamenergy)])
00240     sortedresult=sorted(result,key=lambda x : int(str(x[0]).split(':')[0]))
00241     #print 'sortedresult ',sortedresult
00242     if not toFile:
00243         labels = [('Run:Fill', 'N_LS','N_CMSLS','Delivered','UTCTime','E(GeV)')]
00244         print ' ==  = '
00245         print tablePrinter.indent (labels+sortedresult, hasHeader = True, separateRows = False,
00246                                    prefix = '| ', postfix = ' |', justify = 'right',
00247                                    delim = ' | ', wrapfunc = lambda x: wrap_onspace (x,40) )
00248         print ' ==  =  Total : '
00249         (totalDeliveredVal,totalDeliveredUni)=CommonUtil.guessUnit(totdelivered+totOldDelivered)
00250         totrowlabels = [('Delivered LS','Total CMS LS','Delivered('+totalDeliveredUni+')')]
00251         totaltable.append([str(totls+totOldDeliveredLS),str(totcmsls+totOldCMSLS),'%.3f'%(totalDeliveredVal*scalefactor)])
00252         print tablePrinter.indent (totrowlabels+totaltable, hasHeader = True, separateRows = False, prefix = '| ',
00253                                    postfix = ' |', justify = 'right', delim = ' | ',
00254                                    wrapfunc = lambda x: wrap_onspace (x, 20))
00255     else:
00256         fieldnames = ['Run:Fill', 'N_LS','N_CMSLS','Delivered(/ub)','UTCTime','E(GeV)']
00257         filename=toFile
00258         dumptocsv(fieldnames,sortedresult,filename)
00259                 
00260 def toScreenOverview(lumidata,resultlines,scalefactor,irunlsdict=None,noWarning=True,toFile=None):
00261     '''
00262     input:
00263     lumidata {run:[lumilsnum(0),cmslsnum(1),timestamp(2),beamstatus(3),beamenergy(4),deliveredlumi(5),recordedlumi(6),calibratedlumierror(7),(bxidx,bxvalues,bxerrs)(8),(bxidx,b1intensities,b2intensities)(9),fillnum(10)]}
00264     resultlines [[resultrow1],[resultrow2],...,] existing result row
00265     '''
00266     result=[]
00267 
00268     totOldDeliveredLS=0
00269     totOldSelectedLS=0
00270     totOldDelivered=0.0
00271     totOldRecorded=0.0
00272     
00273     totaltable=[]
00274     totalDeliveredLS = 0
00275     totalSelectedLS = 0
00276     totalDelivered = 0.0
00277     totalRecorded = 0.0
00278     datarunlsdict={}#{run:[ls,...]}from data. construct it only if there is irunlsdict to compare with
00279     for r in resultlines:
00280         runfillstr=r[0]
00281         [runnumstr,fillnumstr]=runfillstr.split(':')
00282         if irunlsdict and not noWarning:
00283             if r[1] is not 'n/a':
00284                 datarunlsdict[int(runnumstr)]=[]
00285         dl=0.0
00286         if(r[2]!='n/a'):            
00287             dl=float(r[2])#delivered in /ub because it comes from file!
00288             (rr,lumiu)=CommonUtil.guessUnit(dl)
00289             r[2]='%.3f'%(rr)+' ('+lumiu+')'
00290         dls=0
00291         if(r[1]!='n/a'):
00292             dls=int(r[1])
00293         totOldDeliveredLS+=dls
00294         totOldDelivered+=dl
00295         rls=0
00296         if(r[3]!='n/a'):
00297             rlsstr=r[3]
00298             listcomp=rlsstr.split(', ')
00299             for lstr in listcomp:
00300                 enddigs=lstr[1:-1].split('-')
00301                 lsmin=int(enddigs[0])
00302                 lsmax=int(enddigs[1])
00303                 rls=lsmax-lsmin+1
00304                 totOldSelectedLS+=rls
00305         if(r[4]!='n/a'):
00306             rcd=float(r[4])#recorded in /ub because it comes from file!
00307             (rrcd,rlumiu)=CommonUtil.guessUnit(rcd)
00308             r[4]='%.3f'%(rrcd)+' ('+rlumiu+')'
00309         totOldRecorded+=rcd
00310         result.append(r)
00311     for run in lumidata.keys():
00312         lsdata=lumidata[run]
00313         if not lsdata:
00314             result.append([str(run)+':0','n/a','n/a','n/a','n/a'])
00315             if irunlsdict and irunlsdict[run] and not noWarning:
00316                 datarunlsdict[run]=None
00317             continue
00318         fillnum=0
00319         if lsdata[0] and lsdata[0][10]:
00320             fillnum=lsdata[0][10]
00321         deliveredData=[]
00322         recordedData=[]
00323         nls=0
00324         existdata=[]
00325         selectedcmsls=[]
00326         for perlsdata in lsdata:
00327             lumilsnum=perlsdata[0]
00328             cmslsnum=perlsdata[1]
00329             if not noWarning:
00330                 if cmslsnum:
00331                     existdata.append(cmslsnum)
00332             if irunlsdict and irunlsdict[run]:
00333                 if lumilsnum and lumilsnum in irunlsdict[run]:
00334                     if perlsdata[5] is not None:
00335                         deliveredData.append(perlsdata[5])
00336                     if perlsdata[6]:
00337                         recordedData.append(perlsdata[6])
00338                     selectedcmsls.append(lumilsnum)
00339             else:
00340                 deliveredData.append(perlsdata[5])
00341                 if perlsdata[6]:
00342                     recordedData.append(perlsdata[6])
00343                 if cmslsnum:                    
00344                     selectedcmsls.append(cmslsnum)
00345         datarunlsdict[run]=existdata
00346         nls=len(deliveredData)
00347         totdelivered=sum(deliveredData)
00348         totalDelivered+=totdelivered
00349         totalDeliveredLS+=len(deliveredData)
00350         (totdeliveredlumi,deliveredlumiunit)=CommonUtil.guessUnit(totdelivered)
00351         totrecorded=sum(recordedData)
00352         totalRecorded+=totrecorded
00353         (totrecordedlumi,recordedlumiunit)=CommonUtil.guessUnit(totrecorded)
00354         totalSelectedLS+=len(selectedcmsls)
00355         if len(selectedcmsls)==0:
00356             selectedlsStr='n/a'
00357         else:
00358             selectedlsStr = CommonUtil.splitlistToRangeString(selectedcmsls)
00359         if not toFile:
00360             result.append([str(run)+':'+str(fillnum),str(nls),'%.3f'%(totdeliveredlumi*scalefactor)+' ('+deliveredlumiunit+')',selectedlsStr,'%.3f'%(totrecordedlumi*scalefactor)+' ('+recordedlumiunit+')'])
00361         else:
00362             result.append([str(run)+':'+str(fillnum),nls,totdelivered*scalefactor,selectedlsStr,totrecorded*scalefactor])
00363     sortedresult=sorted(result,key=lambda x : int(str(x[0]).split(':')[0]))
00364     if irunlsdict and not noWarning:
00365         for run,cmslslist in irunlsdict.items():
00366             if run not in datarunlsdict.keys() or datarunlsdict[run] is None:
00367                 sys.stdout.write('[WARNING] selected run '+str(run)+' not in lumiDB or has no qualified data\n')
00368                 continue
00369             if cmslslist:
00370                 for ss in cmslslist:
00371                     if ss not in datarunlsdict[run]:
00372                         sys.stdout.write('[WARNING] lumi or trg for selected run/ls '+str(run)+' '+str(ss)+' not in lumiDB\n')
00373     if not toFile:
00374         labels = [('Run:Fill', 'Delivered LS', 'Delivered','Selected LS','Recorded')]    
00375         print ' ==  = '
00376         print tablePrinter.indent (labels+sortedresult, hasHeader = True, separateRows = False,
00377                                    prefix = '| ', postfix = ' |', justify = 'right',
00378                                    delim = ' | ', wrapfunc = lambda x: wrap_onspace (x,20) )
00379         print ' ==  =  Total : '
00380         (totalDeliveredVal,totalDeliveredUni)=CommonUtil.guessUnit(totalDelivered+totOldDelivered)
00381         (totalRecordedVal,totalRecordedUni)=CommonUtil.guessUnit(totalRecorded+totOldRecorded)
00382         totrowlabels = [('Delivered LS','Delivered('+totalDeliveredUni+')','Selected LS','Recorded('+totalRecordedUni+')')]
00383         totaltable.append([str(totalDeliveredLS+totOldDeliveredLS),'%.3f'%(totalDeliveredVal*scalefactor),str(totalSelectedLS+totOldSelectedLS),'%.3f'%(totalRecordedVal*scalefactor)])
00384         print tablePrinter.indent (totrowlabels+totaltable, hasHeader = True, separateRows = False, prefix = '| ',
00385                                    postfix = ' |', justify = 'right', delim = ' | ',
00386                                    wrapfunc = lambda x: wrap_onspace (x, 20))
00387     else:
00388         fieldnames = ['Run:Fill', 'DeliveredLS', 'Delivered(/ub)','SelectedLS','Recorded(/ub)']
00389         filename=toFile
00390         dumptocsv(fieldnames,sortedresult,filename)
00391         
00392 def toScreenLumiByLS(lumidata,resultlines,scalefactor,irunlsdict=None,noWarning=True,toFile=None):
00393     '''
00394     input:
00395     lumidata {run:[lumilsnum(0),cmslsnum(1),timestamp(2),beamstatus(3),beamenergy(4),deliveredlumi(5),recordedlumi(6),calibratedlumierror(7),(bxidx,bxvalues,bxerrs)(8),(bxidx,b1intensities,b2intensities)(9),fillnum(10),pu(11)]}
00396     {run:None}  None means no run in lumiDB, 
00397     {run:[]} [] means no lumi for this run in lumiDB
00398     {run:[....deliveredlumi(5),recordedlumi(6)None]} means no trigger in lumiDB
00399     {run:cmslsnum(1)==0} means either not cmslsnum or is cms but not selected, therefore set recordedlumi=0,efflumi=0
00400     resultlines [[resultrow1],[resultrow2],...,] existing result row
00401     '''
00402     result=[]
00403     totalrow = []
00404     
00405     totalDeliveredLS = 0
00406     totalSelectedLS = 0
00407     totalDelivered = 0.0
00408     totalRecorded = 0.0
00409 
00410     totOldDeliveredLS = 0
00411     totOldSelectedLS = 0
00412     totOldDelivered = 0.0
00413     totOldRecorded = 0.0
00414 
00415     maxlslumi = 0.0
00416     datarunlsdict={}#{run:[ls,...]}from data. construct it only if there is irunlsdict to compare with
00417     for rline in resultlines:
00418         runfillstr=rline[0]
00419         [runnumstr,fillnumstr]=runfillstr.split(':')
00420         if irunlsdict and not noWarning:
00421             if rline[1] is not 'n/a':
00422                 datarunlsdict[int(runnumstr)]=[]
00423         myls=rline[1]
00424         if myls!='n/a':
00425             [luls,cmls]=myls.split(':')
00426             totOldDeliveredLS+=1
00427             if cmls!='0':
00428                 totOldSelectedLS+=1
00429                 if irunlsdict and not noWarning:
00430                     datarunlsdict[int(runnumstr)].append(int(myls))                    
00431         dl=rline[5]
00432         if rline[5]!='n/a':
00433             dl=float(rline[5])#delivered in /ub
00434             if dl>maxlslumi: maxlslumi=dl
00435             rline[5]=dl
00436             totOldDelivered+=dl
00437         rl=rline[6]
00438         if rline[6]!='n/a':
00439            rl=float(rline[6])#recorded in /ub
00440            rline[6]=rl
00441            totOldRecorded+=rl
00442         result.append(rline)
00443         
00444     for run in lumidata.keys():
00445         lsdata=lumidata[run]
00446         if not lsdata:
00447             #result.append([str(run),'n/a','n/a','n/a','n/a','n/a','n/a','n/a'])
00448             if irunlsdict and irunlsdict[run] and not noWarning:
00449                 datarunlsdict[run]=None
00450                 #print '[WARNING] selected but no lumi data for run '+str(run)
00451             continue
00452         fillnum=0
00453         if lsdata[0] and lsdata[0][10]:
00454             fillnum=lsdata[0][10]
00455         existdata=[]
00456         #if irunlsdict and not noWarning:
00457         #    existdata=[x[1] for x in rundata if x[1] ]
00458         #    datarunlsdict[run]=existdata
00459         for perlsdata in lsdata:
00460             lumilsnum=perlsdata[0]
00461             cmslsnum=perlsdata[1]#triggered ls
00462             if not noWarning:
00463                 if cmslsnum:
00464                     existdata.append(cmslsnum)
00465             ts=perlsdata[2]
00466             bs=perlsdata[3]
00467             begev=perlsdata[4]
00468             deliveredlumi=perlsdata[5]
00469             npu=perlsdata[11]
00470             if deliveredlumi>maxlslumi: maxlslumi=deliveredlumi
00471             recordedlumi=0.
00472             if perlsdata[6]:
00473                 recordedlumi=perlsdata[6]
00474             if irunlsdict and irunlsdict[run]:
00475                 if run in irunlsdict and lumilsnum in irunlsdict[run]:
00476                     result.append([str(run)+':'+str(fillnum),str(lumilsnum)+':'+str(cmslsnum),ts.strftime('%m/%d/%y %H:%M:%S'),bs,'%.1f'%begev,deliveredlumi,recordedlumi,npu])                
00477                     totalDelivered+=deliveredlumi
00478                     totalRecorded+=recordedlumi
00479                     totalDeliveredLS+=1
00480                     totalSelectedLS+=1
00481             else:
00482                 result.append([str(run)+':'+str(fillnum),str(lumilsnum)+':'+str(cmslsnum),ts.strftime('%m/%d/%y %H:%M:%S'),bs,'%.1f'%begev,deliveredlumi,recordedlumi,npu])
00483                 totalDelivered+=deliveredlumi
00484                 totalRecorded+=recordedlumi
00485                 totalDeliveredLS+=1
00486                 if cmslsnum :
00487                     totalSelectedLS+=1
00488         datarunlsdict[run]=existdata       
00489     sortedresult=sorted(result,key=lambda x : int(str(x[0]).split(':')[0]))    
00490     if irunlsdict and not noWarning:
00491         for run,cmslslist in irunlsdict.items():
00492             if run not in datarunlsdict.keys() or datarunlsdict[run] is None:
00493                 sys.stdout.write('[WARNING] selected run '+str(run)+' not in lumiDB or has no qualified data\n')
00494                 continue
00495             if cmslslist:
00496                 for ss in cmslslist:
00497                     if ss not in datarunlsdict[run]:
00498                         sys.stdout.write('[WARNING] lumi or trg for selected run/ls '+str(run)+' '+str(ss)+' not in lumiDB\n')
00499     if not toFile:                    
00500         (lsunitstring,unitdenomitor)=CommonUtil.lumiUnitForPrint(maxlslumi*scalefactor)
00501         labels = [ ('Run:Fill','LS','UTCTime','Beam Status','E(GeV)','Del('+lsunitstring+')','Rec('+lsunitstring+')','avgPU') ]                    
00502         perlsresult=[]
00503         for entry in sortedresult:
00504             delumi=entry[5]
00505             if delumi!='n/a':
00506                 delumi='%.3f'%float(float(delumi*scalefactor)/float(unitdenomitor))
00507             reclumi=entry[6]
00508             if reclumi!='n/a':
00509                 reclumi='%.3f'%float(float(reclumi*scalefactor)/float(unitdenomitor))
00510             avgPU=entry[7]
00511             if avgPU!='n/a':                
00512                 if avgPU>0:
00513                     avgPU='%.3f'%avgPU
00514                 else:
00515                     avgPU='0'
00516             perlsresult.append([entry[0],entry[1],entry[2],entry[3],entry[4],delumi,reclumi,avgPU])
00517         totdeliveredlumi=0.0
00518         deliveredlumiunit='/ub'
00519         (totdeliveredlumi,deliveredlumiunit)=CommonUtil.guessUnit((totalDelivered+totOldDelivered)*scalefactor)
00520         totrecordedlumi=0.0
00521         recordedlumiunit='/ub'
00522         (totrecordedlumi,recordedlumiunit)=CommonUtil.guessUnit((totalRecorded+totOldRecorded)*scalefactor)
00523         lastrowlabels = [ ('Delivered LS','Selected LS', 'Delivered('+deliveredlumiunit+')', 'Recorded('+recordedlumiunit+')')]
00524         totalrow.append ([str(totalDeliveredLS+totOldDeliveredLS),str(totalSelectedLS+totOldSelectedLS),'%.3f'%(totdeliveredlumi),'%.3f'%(totrecordedlumi)])
00525         print ' ==  = '
00526         print tablePrinter.indent (labels+perlsresult, hasHeader = True, separateRows = False, prefix = '| ',
00527                                    postfix = ' |', justify = 'right', delim = ' | ',
00528                                    wrapfunc = lambda x: wrap_onspace_strict (x, 22))
00529         print ' ==  =  Total : '
00530         print tablePrinter.indent (lastrowlabels+totalrow, hasHeader = True, separateRows = False, prefix = '| ',
00531                                    postfix = ' |', justify = 'right', delim = ' | ',
00532                                    wrapfunc = lambda x: wrap_onspace (x, 20))    
00533     else:
00534         fieldnames=['Run:Fill','LS','UTCTime','Beam Status','E(GeV)','Delivered(/ub)','Recorded(/ub)','avgPU']
00535         filename=toFile
00536         dumptocsv(fieldnames,sortedresult,filename)
00537 
00538 def toScreenLSEffective(lumidata,resultlines,scalefactor,irunlsdict=None,noWarning=True,toFile=None):
00539     '''
00540     input:  {run:[lumilsnum(0),cmslsnum(1),timestamp(2),beamstatus(3),beamenergy(4),deliveredlumi(5),recordedlumi(6),calibratedlumierror(7),{hltpath:[l1name,l1prescale,hltprescale,efflumi]}(8),bxdata(9),beamdata(10),fillnum(11)]}
00541     '''
00542     result=[]#[run,ls,hltpath,l1bitname,hltpresc,l1presc,efflumi]
00543     totalrow=[]
00544     totSelectedLSDict={}
00545     totRecordedDict={}
00546     totEffectiveDict={}
00547 
00548     totOldSelectedLSDict={}
00549     totOldRecordedDict={}
00550     totOldEffectiveDict={}
00551 
00552     maxlslumi = 0.0
00553     datarunlsdict={}#{run:[ls,...]}from data. construct it only if there is irunlsdict to compare with
00554     for rline in resultlines:
00555         runfillstr=rline[0]
00556         [runnumstr,fillnumstr]=runfillstr.split(':')
00557         if irunlsdict and not noWarning:
00558             if rline[1] is not 'n/a':
00559                 datarunlsdict[int(runnumstr)]=[]
00560         myls=rline[1]
00561         mypath=rline[2]
00562 
00563         if myls and myls!='n/a' and mpath and mpath!='n/a':
00564             totOldSelectedLSDict[mypath]=0
00565             totOldRecordedDict[mypath]=0.
00566             totOldEffectiveDict[mypath]=0.
00567         if myls!='n/a':
00568             [luls,cmls]=myls.split(':')
00569             if cmls!='0':
00570                 if totOldSelectedLSDict.has_key(mypath):
00571                     totOldSelectedLSDict[mypath]+=1
00572                 if irunlsdict and not noWarning:
00573                     datarunlsdict[int(runnumstr)].append(int(myls))         
00574         myrecorded=0.
00575         if rline[6]!='n/a':
00576             myrecorded=float(rline[6])
00577             if myrecorded>maxlslumi:maxlslumi=myrecorded
00578             if totOldRecordedDict.has_key(mypath):
00579                 totOldRecordedDict[mypath]+=myrecorded
00580             rline[6]=myrecorded
00581         myeff={}
00582         if rline[7]!='n/a':
00583             myeff=float(rline[7])
00584             if totOldEffectiveDict.has_key(mypath):
00585                 totOldEffectiveDict[mypath]+=myeff
00586             rline[7]=myeff
00587         result.append(rline)        
00588 
00589     for run in lumidata.keys():#loop over runs
00590         lsdata=lumidata[run]
00591         if not lsdata:
00592             result.append([str(run),'n/a','n/a','n/a','n/a','n/a','n/a','n/a'])
00593             if irunlsdict and irunlsdict[run] and not noWarning:
00594                 datarunlsdict[run]=None
00595             continue
00596         fillnum=0
00597         if lsdata[0] and lsdata[0][11]:
00598             fillnum=lsdata[0][11]
00599         datarunlsdict[run]=[]
00600         for thisls in lsdata:
00601             lumilsnum=thisls[0]
00602             cmslsnum=thisls[1]#triggered ls
00603             if not cmslsnum: continue
00604             efflumiDict=thisls[8]# this ls has no such path?            
00605             recordedlumi=0.
00606             if thisls[6]:
00607                 recordedlumi=thisls[6]
00608             if recordedlumi>maxlslumi:maxlslumi=recordedlumi
00609             if not efflumiDict:
00610                 result.append([str(run)+':'+str(fillnum),str(lumilsnum)+':'+str(cmslsnum),'n/a','n/a','n/a','n/a',recordedlumi,'n/a'])
00611                 continue
00612 
00613             for hltpathname in sorted(efflumiDict):
00614                 if hltpathname and hltpathname !='n/a' :
00615                     if not totRecordedDict.has_key(hltpathname):
00616                         totRecordedDict[hltpathname]=0. 
00617                     if not totSelectedLSDict.has_key(hltpathname):
00618                         totSelectedLSDict[hltpathname]=0
00619                     if not totEffectiveDict.has_key(hltpathname):
00620                         totEffectiveDict[hltpathname]=0.
00621                     totSelectedLSDict[hltpathname]+=1
00622                     totRecordedDict[hltpathname]+=recordedlumi
00623                 pathdata=efflumiDict[hltpathname]
00624                 l1name=pathdata[0]
00625                 cleanl1name='n/a'
00626                 if l1name:
00627                     cleanl1name=l1name.replace('"','')
00628                 l1presc='0'
00629                 if pathdata[1]:
00630                     l1presc=str(pathdata[1])
00631                 hltpresc='0'
00632                 if pathdata[2]:
00633                     hltpresc=str(pathdata[2])
00634                 lumival=0.
00635                 if pathdata[3]:
00636                     lumival=pathdata[3]
00637                 result.append([str(run)+':'+str(fillnum),str(lumilsnum)+':'+str(cmslsnum),hltpathname,cleanl1name,hltpresc,l1presc,recordedlumi,lumival])
00638                 if hltpathname and hltpathname !='n/a' :
00639                     totEffectiveDict[hltpathname]+=lumival
00640                 if irunlsdict and not noWarning:
00641                     datarunlsdict[run].append(int(cmslsnum))
00642     sortedresult=sorted(result,key=lambda x : int(str(x[0]).split(':')[0]))
00643     if irunlsdict and not noWarning:
00644         for run,cmslslist in irunlsdict.items():
00645             if run not in datarunlsdict.keys() or datarunlsdict[run] is None:
00646                 sys.stdout.write('[WARNING] selected run '+str(run)+' not in lumiDB or has no HLT data\n')
00647                 continue
00648             if cmslslist:
00649                 for ss in cmslslist:
00650                     if ss not in datarunlsdict[run]:
00651                         sys.stdout.write('[WARNING] selected run/ls '+str(run)+' '+str(ss)+' not in lumiDB or has no qualified data\n')
00652                         
00653     if not toFile:
00654         (lsunitstring,unitdenomitor)=CommonUtil.lumiUnitForPrint(maxlslumi*scalefactor)
00655         labels = [('Run:Fill','LS','HLTpath','L1bit','HLTpresc','L1presc','Recorded('+lsunitstring+')','Effective('+lsunitstring+')')]
00656         perlsresult=[]
00657         for entry in sortedresult:
00658             reclumi=entry[6]
00659             if reclumi!='n/a':
00660                 reclumi='%.3f'%float(float(reclumi*scalefactor)/float(unitdenomitor))
00661             efflumi=entry[7]
00662             if efflumi!='n/a':
00663                 efflumi='%.3f'%float(float(efflumi*scalefactor)/float(unitdenomitor))
00664             perlsresult.append([entry[0],entry[1],entry[2],entry[3],entry[4],entry[5],reclumi,efflumi])
00665         print ' ==  = '
00666         print tablePrinter.indent (labels+perlsresult, hasHeader = True, separateRows = False,
00667                                    prefix = '| ', postfix = ' |', justify = 'right',
00668                                    delim = ' | ', wrapfunc = lambda x: wrap_onspace_strict(x,25) )
00669         for mpath in sorted(totRecordedDict):
00670             totSelectedLS=totSelectedLSDict[mpath]
00671             if totOldSelectedLSDict.has_key(mpath):
00672                 totSelectedLS+=totOldSelectedLS[mpath]
00673             totRecorded=totRecordedDict[mpath]
00674             if totOldRecordedDict.has_key(mpath):
00675                 totRecorded+=totOldRecorded[mpath]
00676             totRecorded=float(totRecorded*scalefactor)/float(unitdenomitor)
00677             totEffective=totEffectiveDict[mpath]
00678             if totOldEffectiveDict.has_key(mpath):
00679                 totEffective+=totOldEffective[mpath]
00680             totEffective=float(totEffective*scalefactor)/float(unitdenomitor)
00681             totalrow.append([str(totSelectedLS),mpath,'%.3f'%(totRecorded),'%.3f'%(totEffective)])
00682         lastrowlabels = [ ('Selected LS','HLTPath','Recorded('+lsunitstring+')','Effective('+lsunitstring+')')]
00683         print ' ==  =  Total : '
00684         print tablePrinter.indent (lastrowlabels+totalrow, hasHeader = True, separateRows = False, prefix = '| ',
00685                                    postfix = ' |', justify = 'right', delim = ' | ',
00686                                    wrapfunc = lambda x: wrap_onspace (x, 20))
00687     else:
00688         fieldnames = ['Run:Fill','LS','HLTpath','L1bit','HLTpresc','L1presc','Recorded(/ub)','Effective(/ub)']
00689         filename=toFile
00690         dumptocsv(fieldnames,sortedresult,filename)
00691         
00692 def toScreenTotEffective(lumidata,resultlines,scalefactor,irunlsdict=None,noWarning=True,toFile=None):
00693 
00694     '''
00695     input:  {run:[lumilsnum(0),triggeredls(1),timestamp(2),beamstatus(3),beamenergy(4),deliveredlumi(5),recordedlumi(6),calibratedlumierror(7),{hltpath:[l1name,l1prescale,hltprescale,efflumi]}(8),bxdata(9),beamdata](10),fillnum(11)}
00696     screen Run,SelectedLS,Recorded,HLTPath,L1Bit,Effective
00697     '''
00698     result=[]#[run,selectedlsStr,recordedofthisrun,hltpath,l1bit,efflumi]
00699     
00700     totdict={}#{hltpath:[nls,toteff]}
00701     selectedcmsls=[]
00702     alltotrecorded=0.0
00703     alleffective=0.0
00704     recordedPerpathPerrun={}#{path:{run:recorded}}
00705     selectedPerpathPerrun={}#{path:{run:totselected}}
00706     datarunlsdict={}#{run:[ls,...]}from data. construct it only if there is irunlsdict to compare with
00707     for rline in resultlines:
00708         runfillstr=rline[0]
00709         [runnumstr,fillnumstr]=runfillstr.split(':')
00710         myls=rline[1]
00711         if irunlsdict and not noWarning:
00712             if myls is not 'n/a':
00713                 datarunlsdict[int(runnumstr)]=[]  
00714         mypath=rline[3]
00715         if mypath!='n/a':
00716             mypath=mypath.split('(')[0]
00717             if not totdict.has_key(mypath):
00718                 totdict[mypath]=[0,0.0]
00719                 recordedPerpathPerrun[mypath]={}
00720                 selectedPerpathPerrun[mypath]={}
00721         if myls!='n/a':
00722             listcomp=myls.split(', ')
00723             for lstr in listcomp:
00724                 enddigs=lstr[1:-1].split('-')
00725                 lsmin=int(enddigs[0])
00726                 lsmax=int(enddigs[1])
00727                 rls=lsmax-lsmin+1
00728                 totdict[mypath][0]+=rls                
00729             selectedPerrun[mypath].setdefault(int(myrun),totdict[mypath][0])
00730         myrecorded=rline[2]
00731         if myrecorded!='n/a':
00732             recordedPerpathPerrun[mypath].setdefault(int(myrun),float(myrecorded))
00733             (rr,lumiu)=CommonUtil.guessUnit(float(myrecorded))
00734             rline[2]='%.3f'%(rr)+' ('+lumiu+')'
00735         myeff=rline[5]
00736         if myeff!='n/a':
00737             reff=float(myeff)
00738             (rr,lumiu)=CommonUtil.guessUnit(float(reff))
00739             rline[5]='%.3f'%(rr)+' ('+lumiu+')'
00740             totdict[mypath][1]+=reff
00741         result.append(rline)
00742     for run in lumidata.keys():#loop over runs
00743         lsdata=lumidata[run]
00744         hprescdict={}
00745         lprescdict={}
00746         if not lsdata:
00747             result.append([str(run),'n/a','n/a','n/a','n/a','n/a'])
00748             if irunlsdict and irunlsdict[run] and not noWarning:
00749                 datarunlsdict[run]=None
00750             continue
00751         fillnum=0
00752         if lsdata[0] and lsdata[0][11]:
00753             fillnum=lsdata[0][11]
00754         selectedcmsls=[x[1] for x in lsdata if x[1]]
00755         totefflumiDict={}
00756         totrecorded=0.0
00757         toteffective=0.0
00758         pathmap={}#{hltpathname:1lname}
00759         existdata=[]
00760         for thisls in lsdata:            
00761             cmslsnum=thisls[1]
00762             if not noWarning:
00763                 if cmslsnum:
00764                     existdata.append(cmslsnum)
00765             efflumiDict=thisls[8]# this ls has no such path?
00766             recordedlumi=0.0
00767             if thisls[6]:
00768                 recordedlumi=thisls[6]
00769             totrecorded+=recordedlumi
00770             if not efflumiDict:#no hltdata for this LS
00771                 lumival=0.
00772                 if cmslsnum in selectedcmsls:
00773                     selectedcmsls.remove(cmslsnum)
00774                 continue            
00775             for hltpathname in sorted(efflumiDict):
00776                 pathdata=efflumiDict[hltpathname]
00777                 if not totefflumiDict.has_key(hltpathname):
00778                     totefflumiDict[hltpathname]=0.0
00779                     pathmap[hltpathname]='n/a'
00780                 l1name=pathdata[0]
00781                 l1presc=pathdata[1]
00782                 hltpresc=pathdata[2]
00783                 lumival=pathdata[3]
00784                 recordedPerpathPerrun.setdefault(hltpathname,{})
00785                 selectedPerpathPerrun.setdefault(hltpathname,{})
00786                 if not totdict.has_key(hltpathname):
00787                     totdict[hltpathname]=[0,0.0]
00788                 if l1presc is None or hltpresc is None:#if found all null prescales and if it is in the selectedcmsls, remove it because incomplete
00789                     if cmslsnum in selectedcmsls:
00790                         selectedcmsls.remove(cmslsnum)
00791                 else:                    
00792                     if not hprescdict.has_key(hltpathname):
00793                         hprescdict[hltpathname]=[]
00794                     hprescdict[hltpathname].append(hltpresc)
00795                     if not lprescdict.has_key(l1name):
00796                         lprescdict[l1name]=[]
00797                     lprescdict[l1name].append(l1presc)
00798                     if cmslsnum!=0:
00799                         totdict[hltpathname][0]+=1                    
00800                     if lumival:
00801                         totdict[hltpathname][1]+=lumival
00802                         totefflumiDict[hltpathname]+=lumival
00803                         pathmap[hltpathname]=l1name
00804                     recordedPerpathPerrun[hltpathname][run]=totrecorded
00805                     selectedPerpathPerrun[hltpathname][run]=len(selectedcmsls)
00806         if len(selectedcmsls)==0:
00807             selectedlsStr='n/a'
00808         else:
00809             selectedlsStr = CommonUtil.splitlistToRangeString(selectedcmsls)
00810         if irunlsdict and not noWarning:
00811             datarunlsdict[run]=selectedcmsls
00812         
00813         for name in sorted(totefflumiDict):
00814             lname=pathmap[name]
00815             totrecordedinrun=recordedPerpathPerrun[name][run]
00816             hprescs=list(set(hprescdict[name]))
00817             hprescStr='('+','.join(['%d'%(x) for x in hprescs])+')'
00818             (totrecval,totrecunit)=CommonUtil.guessUnit(totrecordedinrun*scalefactor)
00819             effval='n/a'
00820 
00821             effvalStr='n/a'
00822             lprescStr='n/a'
00823             cleanlname=''
00824             if lname!='n/a':
00825                 effval=totefflumiDict[name]*scalefactor
00826                 lprescs=list(set(lprescdict[lname]))
00827                 lprescStr='('+','.join(['%d'%(x) for x in lprescs])+')'
00828                 cleanlname=lname.replace('"','')
00829                 (efflumival,efflumiunit)=CommonUtil.guessUnit(effval)
00830                 effvalStr='%.3f'%(efflumival)+'('+efflumiunit+')'
00831             if not toFile:
00832                 result.append([str(run)+':'+str(fillnum),selectedlsStr,'%.3f'%(totrecval)+'('+totrecunit+')',name+hprescStr,cleanlname+lprescStr,effvalStr])
00833             else:
00834                 result.append([str(run)+':'+str(fillnum),selectedlsStr,totrecordedinrun*scalefactor,name+hprescStr,cleanlname+lprescStr,effval])
00835                 
00836     if irunlsdict and not noWarning:
00837         for run,cmslslist in irunlsdict.items():
00838             if run not in datarunlsdict.keys() or datarunlsdict[run] is None:
00839                 sys.stdout.write('[WARNING] selected run '+str(run)+' not in lumiDB or has no HLT data\n')
00840                 continue
00841             if cmslslist:
00842                 for ss in cmslslist:
00843                     if ss not in datarunlsdict[run]:
00844                         sys.stdout.write('[WARNING] selected run/ls '+str(run)+' '+str(ss)+' not in lumiDB or has no HLT data\n')
00845                         
00846     sortedresult=sorted(result,key=lambda x : int(str(x[0]).split(':')[0]))
00847 
00848     if not toFile:
00849         labels = [('Run:Fill','SelectedLS','Recorded','HLTpath(Presc)','L1bit(Presc)','Effective')]
00850         print ' ==  = '
00851         print tablePrinter.indent (labels+sortedresult, hasHeader = True, separateRows = False,
00852                                    prefix = '| ', postfix = ' |', justify = 'right',
00853                                    delim = ' | ', wrapfunc = lambda x: wrap_onspace_strict(x,22) )
00854         print ' ==  =  Total : '
00855         lastrowlabels=[('HLTPath','SelectedLS','Recorded','Effective')]
00856         totresult=[]
00857         for hname in sorted(totdict):
00858             hdata=totdict[hname]
00859             totnls=hdata[0]
00860             (toteffval,toteffunit)=CommonUtil.guessUnit(hdata[1]*scalefactor)
00861             alltotrecorded=0.0
00862             selectedThispath=selectedPerpathPerrun[hname]
00863             for runnumber,nselected in selectedThispath.items():
00864                 if nselected==0: continue
00865                 alltotrecorded+=recordedPerpathPerrun[hname][runnumber]
00866             (alltotrecordedVal,alltotrecordedunit)=CommonUtil.guessUnit(alltotrecorded*scalefactor)                                                   
00867             totresult.append([hname,str(totnls),'%.3f'%(alltotrecordedVal)+'('+alltotrecordedunit+')','%.3f'%(toteffval)+'('+toteffunit+')'])
00868         print tablePrinter.indent (lastrowlabels+totresult, hasHeader = True, separateRows = False,prefix = '| ', postfix = ' |', justify = 'right',
00869                                    delim = ' | ', wrapfunc = lambda x: wrap_onspace (x,20) )
00870     else:
00871         fieldnames=['Run:Fill','SelectedLS','Recorded','HLTpath(Presc)','L1bit(Presc)','Effective(/ub)']
00872         filename=toFile
00873         dumptocsv(fieldnames,sortedresult,filename)
00874             
00875 def toCSVLumiByLSXing(lumidata,scalefactor,filename,irunlsdict=None,noWarning=True):
00876     '''
00877     input:{run:[lumilsnum(0),cmslsnum(1),timestamp(2),beamstatus(3),beamenergy(4),deliveredlumi(5),recordedlumi(6),calibratedlumierror(7),bxdata(8),beamdata(9),fillnum(10)]}
00878     output:
00879     fieldnames=['Run:Fill','LS','UTCTime','Delivered(/ub)','Recorded(/ub)','BX']
00880     '''
00881     result=[]
00882     assert(filename)
00883     fieldnames=['run:fill','ls','UTCTime','delivered(/ub)','recorded(/ub)','[bx,Hz/ub]']
00884     datarunlsdict={}#{run:[ls,...]}from data. construct it only if there is irunlsdict to compare with     
00885     for run in sorted(lumidata):
00886         rundata=lumidata[run]
00887         if rundata is None:
00888             result.append([str(run)+':0','n/a','n/a','n/a','n/a','n/a'])
00889             if irunlsdict and irunlsdict[run]:
00890                 print '[WARNING] selected but no lumi data for run '+str(run)
00891             continue
00892         fillnum=0
00893         if rundata and rundata[0][10]:
00894             fillnum=rundata[0][10]
00895         if irunlsdict and not noWarning:
00896             existdata=[x[1] for x in rundata if x[1] ]
00897             datarunlsdict[run]=existdata
00898         for lsdata in rundata:
00899             lumilsnum=lsdata[0]
00900             cmslsnum=0
00901             if lsdata and lsdata[1]:
00902                 cmslsnum=lsdata[1]
00903             tsStr='n/a'
00904             if lsdata and lsdata[2]:
00905                 ts=lsdata[2]
00906                 tsStr=ts.strftime('%m/%d/%y %H:%M:%S')
00907             deliveredlumi=0.
00908             if lsdata[5]:
00909                 deliveredlumi=lsdata[5]
00910             recordedlumi=0.
00911             if lsdata[6]:
00912                 recordedlumi=lsdata[6]
00913             (bxidxlist,bxvaluelist,bxerrorlist)=lsdata[8]
00914             if irunlsdict and irunlsdict[run]:
00915                 if run in irunlsdict and cmslsnum in irunlsdict[run]:
00916                     if bxidxlist and bxvaluelist:
00917                         bxresult=[]
00918                         bxinfo=CommonUtil.transposed([bxidxlist,bxvaluelist])
00919                         bxresult=CommonUtil.flatten([str(run)+':'+str(fillnum),str(lumilsnum)+':'+str(cmslsnum),tsStr,deliveredlumi*scalefactor,recordedlumi*scalefactor,bxinfo])
00920                         result.append(bxresult)
00921                     else:
00922                         result.append([str(run)+':'+str(fillnum),str(lumilsnum)+':'+str(cmslsnum),tsStr,deliveredlumi*scalefactor,recordedlumi*scalefactor])
00923             else:
00924                 if bxidxlist and bxvaluelist:
00925                     bxresult=[]
00926                     bxinfo=CommonUtil.transposed([bxidxlist,bxvaluelist])
00927                     bxresult=CommonUtil.flatten([str(run)+':'+str(fillnum),str(lumilsnum)+':'+str(cmslsnum),tsStr,deliveredlumi*scalefactor,recordedlumi*scalefactor,bxinfo])
00928                     result.append(bxresult)
00929                 else:
00930                     result.append([str(run)+':'+str(fillnum),str(lumilsnum)+':'+str(cmslsnum),tsStr,deliveredlumi*scalefactor,recordedlumi*scalefactor])
00931     r=None
00932     if filename.upper()=='STDOUT':
00933         r=sys.stdout
00934         r.write(','.join(fieldnames)+'\n')
00935         for l in result:
00936             r.write(str(l)+'\n')
00937     else:
00938         r=csvReporter.csvReporter(filename)
00939         r.writeRow(fieldnames)
00940         r.writeRows(result)
00941     
00942 def toScreenLSTrg(trgdata,iresults=[],irunlsdict=None,noWarning=True,toFile=None,withoutmask=False):
00943     '''
00944     input:{run:[[cmslsnum,deadfrac,deadtimecount,bitzero_count,bitzero_prescale,[(name,count,presc,mask),]],..]
00945     '''
00946     result=[]
00947     datarunlsdict={}#{run:[ls,...]}from data. construct it only if there is irunlsdict to compare with
00948     for rline in iresults:
00949         runnumStr=rline[0]
00950         cmslsnumStr=rline[1]
00951         if irunlsdict and not noWarning:
00952             if runnumStr is not 'n/a' and not datarunlsdict.has_key(int(runnumStr)):
00953                 datarunlsdict[int(runnumstr)]=[]
00954             if cmslsnumStr!='n/a':
00955                 datarunlsdict[int(runnumStr)].append(int(cmslsnumStr))
00956         result.append(rline)
00957     for run in trgdata.keys():
00958         rundata=trgdata[run]
00959         if not rundata:
00960             ll=[str(run),'n/a','n/a','n/a']
00961             result.append(ll)
00962             if irunlsdict and not noWarning:
00963                 print '[WARNING] selected but no trg data for run '+str(run)
00964             continue
00965         if irunlsdict and not noWarning:
00966             existdata=[x[0] for x in rundata if x[0] ]
00967             datarunlsdict[run]=existdata
00968         deadfrac=0.0
00969         bitdataStr='n/a'
00970         for lsdata in rundata:
00971             cmslsnum=lsdata[0]
00972             deadfrac=lsdata[1]
00973             deadcount=lsdata[2]
00974             bitdata=lsdata[5]# already sorted by name
00975             if bitdata:
00976               if withoutmask:
00977                   flatbitdata=["("+x[0]+',%d'%x[1]+',%d'%x[2]+")" for x in bitdata if x[0]!='False']
00978                   bitdataStr=' '.join(flatbitdata)
00979               else:
00980                   #consider trg mask by default
00981                   flatbitdata=["("+x[0]+',%d'%x[1]+',%d'%x[2]+")" for x in bitdata if x[0]!='False' and x[3]]
00982                   bitdataStr=' '.join(flatbitdata)
00983             if irunlsdict and irunlsdict[run]:
00984                 if run in irunlsdict and cmslsnum in irunlsdict[run]:
00985                     result.append([str(run),str(cmslsnum),'%.4f'%(deadfrac),bitdataStr])
00986             else:
00987                 result.append([str(run),str(cmslsnum),'%.4f'%(deadfrac),bitdataStr])
00988     if irunlsdict and not noWarning:
00989         for run,cmslslist in irunlsdict.items():
00990             if run not in datarunlsdict.keys() or datarunlsdict[run] is None:
00991                 sys.stdout.write('[WARNING] selected run '+str(run)+' not in lumiDB or has no qualified data\n')
00992                 continue
00993             if cmslslist:
00994                 for ss in cmslslist:
00995                     if ss not in datarunlsdict[run]:
00996                         sys.stdout.write('[WARNING] selected run/ls '+str(run)+' '+str(ss)+' not in lumiDB\n')
00997                         
00998     if not toFile:
00999         print ' ==  = '
01000         labels = [('Run', 'LS', 'dfrac','(bitname,count,presc)')]
01001         print tablePrinter.indent (labels+result, hasHeader = True, separateRows = False,prefix = '| ', postfix = ' |', justify = 'left',delim = ' | ', wrapfunc = lambda x: wrap_onspace (x,70) )
01002     else:
01003         filename=toFile
01004         fieldnames=['Run','LS','dfrac','(bitname,count,presc)']
01005         dumptocsv(fieldnames,result,filename)
01006 
01007 def toScreenLSHlt(hltdata,iresults=[],toFile=None):
01008     '''
01009     input:{runnumber:[(cmslsnum,[(hltpath,hltprescale,l1pass,hltaccept),...]),(cmslsnum,[])})}
01010     '''
01011     result=[]
01012     for r in iresults:
01013         result.append(r)
01014     for run in hltdata.keys():
01015         if hltdata[run] is None:            
01016             ll=[str(run),'n/a','n/a','n/a','n/a','n/a']
01017             continue
01018         perrundata=hltdata[run]
01019         for lsdata in perrundata:
01020             cmslsnum=lsdata[0]
01021             allpathinfo=lsdata[1]
01022             allpathresult=[]
01023             for thispathinfo in allpathinfo:
01024                 thispathname=thispathinfo[0]
01025                 thispathpresc=thispathinfo[1]
01026                 thisl1pass=None
01027                 thishltaccept=None
01028                 thispathresult=[]
01029                 thispathresult.append(thispathname)
01030                 if thispathpresc is None:
01031                     thispathpresc='n/a'
01032                 else:
01033                     thispathresult.append('%d'%thispathpresc)
01034                 thisl1pass=thispathinfo[2]
01035                 if thispathinfo[2] is None:
01036                     thispathresult.append('n/a')
01037                 else:
01038                     thispathresult.append('%d'%thisl1pass)
01039                 thishltaccept=thispathinfo[3]
01040                 if thispathinfo[3] is None:
01041                     thispathresult.append('n/a')
01042                 else:
01043                     thispathresult.append('%d'%thishltaccept)
01044 
01045                 thispathresultStr='('+','.join(thispathresult)+')'
01046                 allpathresult.append(thispathresultStr)
01047             result.append([str(run),str(cmslsnum),', '.join(allpathresult)])
01048             
01049     if not toFile:
01050         print ' ==  = '
01051         labels = [('Run', 'LS', '(hltpath,presc,l1pass,hltaccept)')]
01052         print tablePrinter.indent (labels+result, hasHeader = True, separateRows = False,
01053                                    prefix = '| ', postfix = ' |', justify = 'left',
01054                                    delim = ' | ', wrapfunc = lambda x: wrap_onspace (x,70) )
01055     else:
01056         fieldnames=['Run','LS','(hltpath,presc,l1pass,hltaccept)']
01057         filename=toFile
01058         dumptocsv(fieldnames,result,filename)
01059     
01060 def toScreenConfHlt(hltconfdata,iresults=[],toFile=None):
01061     '''
01062     input : {runnumber,[(hltpath,l1seedexpr,l1bitname),...]}
01063     '''
01064     result=[]
01065     for r in iresults:
01066         pp=r[1]
01067         pp=' '.join([pp[i:i+25] for i in range(0,len(pp),25)])
01068         sdepr=r[2]
01069         sdepr=' '.join([sdepr[i:i+25] for i in range(0,len(sdepr),25)])
01070         lb=r[3]
01071         lb=' '.join([lb[i:i+25] for i in range(0,len(lb),25)])
01072         result.append([r[0],pp,sdepr,lb])
01073     for run in sorted(hltconfdata):
01074         pathdata=hltconfdata[run]
01075         if pathdata is None:
01076             result.append([str(run),'n/a','n/a','n/a'])
01077             continue
01078         for thispathinfo in pathdata:
01079             thispath=thispathinfo[0]
01080             thispath=' '.join([thispath[i:i+25] for i in range(0,len(thispath),25)])
01081             thisseed=thispathinfo[1]
01082             thisseed=' '.join([thisseed[i:i+25] for i in range(0,len(thisseed),25)]).replace('"','')
01083             thisbit=thispathinfo[2]
01084             if not thisbit:
01085                 thisbit='n/a'
01086             else:
01087                 thisbit=' '.join([thisbit[i:i+25] for i in range(0,len(thisbit),25)]).replace('"','')
01088             result.append([str(run),thispath,thisseed,thisbit])
01089     if not toFile:
01090         labels=[('Run','hltpath','l1seedexpr','l1bit')]
01091         print ' ==  = '
01092         print tablePrinter.indent (labels+result, hasHeader = True, separateRows = False,
01093                                    prefix = '| ', postfix = ' |', justify = 'left',
01094                                    delim = ' | ', wrapfunc = lambda x: wrap_onspace(x,25) )
01095     else:
01096         filename=toFile
01097         fieldnames=['Run','hltpath','l1seedexpr','l1bit']
01098         dumptocsv(fieldnames,sortedresult,filename)
01099 
01100 def toScreenLSBeam(beamdata,iresults=[],dumpIntensity=False,toFile=None):
01101     '''
01102     input: {run:[(lumilsnum(0),cmslsnum(1),beamstatus(2),beamenergy(3),ncollidingbunches(4),beaminfolist(4)),..]}
01103     beaminfolist:[(bxidx,b1,b2)]
01104     '''
01105     result=[]
01106     for rline in iresults:
01107         result.append(rline)
01108     for run in sorted(beamdata):
01109         perrundata=beamdata[run]
01110         if perrundata is None:            
01111             ll=[str(run),'n/a','n/a']
01112             if dumpIntensity:
01113                 ll.extend('n/a')
01114             continue
01115         for lsdata in perrundata:
01116             lumilsnum=lsdata[0]
01117             cmslsnum=lsdata[1]
01118             beamstatus=lsdata[2]
01119             beamenergy=lsdata[3]
01120             ncollidingbx=lsdata[4]
01121             if not dumpIntensity:
01122                 result.append([str(run),str(lumilsnum)+':'+str(cmslsnum),beamstatus,'%.2f'%beamenergy,str(ncollidingbx)])
01123                 continue
01124             allbxinfo=lsdata[5]
01125             allbxresult=[]
01126             for thisbxinfo in allbxinfo:
01127                 thisbxresultStr='(n/a,n/a,n/a,n/a)'
01128                 bxidx=thisbxinfo[0]
01129                 b1=thisbxinfo[1]
01130                 b2=thisbxinfo[2]
01131                 thisbxresultStr=','.join(['%d'%bxidx,'%.3e'%b1,'%.3e'%b2])
01132                 allbxresult.append(thisbxresultStr)
01133             allbxresultStr=' '.join(allbxresult)
01134             result.append([str(run),str(lumilsnum)+':'+str(cmslsnum),beamstatus,'%.2f'%beamenergy,str(ncollidingbx),allbxresultStr])
01135 
01136     if not toFile:
01137         labels=[('Run','LS','beamstatus','egev','ncollidingbx')]
01138         if dumpIntensity:
01139             labels=[('Run','LS','beamstatus','egev','ncollidingbx','(bxidx,b1,b2)')]
01140         print ' ==  = '
01141         print tablePrinter.indent (labels+result, hasHeader = True, separateRows = False,
01142                                    prefix = '| ', postfix = ' |', justify = 'left',
01143                                    delim = ' | ', wrapfunc = lambda x: wrap_onspace(x,25) )
01144     else:
01145         fieldnames=['Run','LS','beamstatus','egev','ncollidingbx']
01146         if dumpIntensity:
01147             fieldnames.append('(bxidx,b1,b2)')
01148         filename=toFile
01149         dumptocsv(fieldnames,result,filename)
01150 
01151 if __name__ == "__main__":
01152     toScreenHeader('lumiCalc2.py','V04-00-00','v0','pp8TeV')