CMS 3D CMS Logo

Classes | Functions | Variables

lumiInstPlot Namespace Reference

Classes

class  constants

Functions

def getInstLumiPerLS
def getLumiPerRun
def main

Variables

string VERSION = '1.00'

Function Documentation

def lumiInstPlot::getInstLumiPerLS (   dbsession,
  c,
  runList,
  selectionDict,
  beamstatus = None,
  beamenergy = None,
  beamenergyfluctuation = 0.09 
)
input: runList[runnum], selectionDict{runnum:[ls]}
output:[[runnumber,lsnumber,deliveredInst,recordedInst,norbit,startorbit,runstarttime,runstoptime]]

Definition at line 17 of file lumiInstPlot.py.

00018                                                                                                                   :
00019     '''
00020     input: runList[runnum], selectionDict{runnum:[ls]}
00021     output:[[runnumber,lsnumber,deliveredInst,recordedInst,norbit,startorbit,runstarttime,runstoptime]]
00022     '''
00023     result=[]
00024     selectedRunlist=runList
00025     if len(selectionDict)!=0:
00026         selectedRunlist=[]
00027         allruns=runlist+selectionDict.keys()
00028         dups=CommonUtil.count_dups(allruns)
00029         for runnum,dupcount in dups:
00030             if dupcount==2:
00031                 selectedRunlist.append(runnum)
00032                 
00033     dbsession.transaction().start(True)
00034     for run in selectedRunlist:
00035         q=dbsession.nominalSchema().newQuery()
00036         runsummary=lumiQueryAPI.runsummaryByrun(q,run)
00037         del q
00038         runstarttime=runsummary[3]
00039         runstoptime=runsummary[4]
00040         q=dbsession.nominalSchema().newQuery()
00041         lumiperrun=lumiQueryAPI.lumisummaryByrun(q,run,c.LUMIVERSION,beamstatus,beamenergy,beamenergyfluctuation)
00042         del q
00043         if len(lumiperrun)==0: #no result for this run
00044             result.append([run,1,0.0,0.0,0,0,runstarttime,runstoptime])
00045         else:
00046             for lumiperls in lumiperrun:
00047                 cmslsnum=lumiperls[0]
00048                 instlumi=lumiperls[1]
00049                 recordedlumi=0.0
00050                 numorbit=lumiperls[2]
00051                 startorbit=lumiperls[3]
00052                 deadcount=0
00053                 bitzero=0
00054                 result.append([run,cmslsnum,instlumi,recordedlumi,numorbit,startorbit,runstarttime,runstoptime])
00055     dbsession.transaction().commit()
00056     if c.VERBOSE:
00057         print result
00058     return result              

def lumiInstPlot::getLumiPerRun (   dbsession,
  c,
  run,
  beamstatus = None,
  beamenergy = None,
  beamenergyfluctuation = 0.09 
)
input: run
output:{runnumber:[[lsnumber,deliveredInst,recordedInst,norbit,startorbit,runstarttime,runstoptime]]}

Definition at line 59 of file lumiInstPlot.py.

00060                                                                                              :
00061     '''
00062     input: run
00063     output:{runnumber:[[lsnumber,deliveredInst,recordedInst,norbit,startorbit,runstarttime,runstoptime]]}
00064     '''
00065     result=[]
00066     dbsession.transaction().start(True)
00067     q=dbsession.nominalSchema().newQuery()
00068     runsummary=lumiQueryAPI.runsummaryByrun(q,run)
00069     del q
00070     runstarttime=runsummary[3]
00071     runstoptime=runsummary[4]
00072     fillnum=runsummary[0]
00073     q=dbsession.nominalSchema().newQuery()
00074     lumiperrun=lumiQueryAPI.lumisummaryByrun(q,run,c.LUMIVERSION,beamstatus,beamenergy,beamenergyfluctuation)
00075     del q
00076     q=dbsession.nominalSchema().newQuery()
00077     trgperrun=lumiQueryAPI.trgbitzeroByrun(q,run) # {cmslsnum:[trgcount,deadtime,bitname,prescale]}
00078     del q
00079         
00080     for lumiperls in lumiperrun:
00081         cmslsnum=lumiperls[0]
00082         instlumi=lumiperls[1]
00083         recordedlumi=0.0
00084         numorbit=lumiperls[2]
00085         startorbit=lumiperls[3]
00086         deadcount=0
00087         bitzero=0
00088         if trgperrun.has_key(cmslsnum):
00089             bitzero=trgperrun[cmslsnum][0]
00090             deadcount=trgperrun[cmslsnum][1]
00091             bitzeroprescale=trgperrun[cmslsnum][-1]
00092             try:
00093                 recordedlumi=instlumi*(1.0-float(deadcount)/(float(bitzero)*float(bitzeroprescale)))
00094             except ZeroDivisionError:
00095                 recordedlumi=-0.1 #           
00096         result.append([cmslsnum,instlumi,recordedlumi,numorbit,startorbit,fillnum,runstarttime,runstoptime])
00097     dbsession.transaction().commit()
00098     if c.VERBOSE:
00099         print result
00100     return result          
    
def lumiInstPlot::main ( )

Definition at line 101 of file lumiInstPlot.py.

00102           :
00103     allowedscales=['linear','log','both']
00104     c=constants()
00105     parser = argparse.ArgumentParser(prog=os.path.basename(sys.argv[0]),description="Plot integrated luminosity as function of the time variable of choice")
00106     # add required arguments
00107     parser.add_argument('-c',dest='connect',action='store',required=True,help='connect string to lumiDB')
00108     # add optional arguments
00109     parser.add_argument('-P',dest='authpath',action='store',help='path to authentication file')
00110     parser.add_argument('-n',dest='normfactor',action='store',help='normalization factor (optional, default to 1.0)')
00111     parser.add_argument('-i',dest='inputfile',action='store',help='lumi range selection file (optional)')
00112     parser.add_argument('-o',dest='outputfile',action='store',help='csv outputfile name (optional)')
00113     parser.add_argument('-b',dest='beammode',action='store',help='beam mode, optional, no default')
00114     parser.add_argument('-lumiversion',dest='lumiversion',action='store',help='lumi data version, optional for all, default 0001')
00115     parser.add_argument('-begin',dest='begin',action='store',help='begin xvalue (required)')
00116     parser.add_argument('-end',dest='end',action='store',help='end xvalue(optional). Default to the maximum exists DB')
00117     parser.add_argument('-batch',dest='batch',action='store',help='graphical mode to produce PNG file. Specify graphical file here. Default to lumiSum.png')
00118     parser.add_argument('-yscale',dest='yscale',action='store',required=False,default='linear',help='y_scale')
00119     parser.add_argument('--interactive',dest='interactive',action='store_true',help='graphical mode to draw plot in a TK pannel.')
00120     parser.add_argument('-timeformat',dest='timeformat',action='store',help='specific python timeformat string (optional).  Default mm/dd/yy hh:min:ss.00')
00121     parser.add_argument('-siteconfpath',dest='siteconfpath',action='store',help='specific path to site-local-config.xml file, default to $CMS_PATH/SITECONF/local/JobConfig, if path undefined, fallback to cern proxy&server')
00122     parser.add_argument('action',choices=['peakperday','run'],help='plot type of choice')
00123     #graphical mode options
00124     parser.add_argument('--annotateboundary',dest='annotateboundary',action='store_true',help='annotate boundary run numbers')
00125     parser.add_argument('--verbose',dest='verbose',action='store_true',help='verbose mode, print result also to screen')
00126     parser.add_argument('--debug',dest='debug',action='store_true',help='debug')
00127     # parse arguments
00128     args=parser.parse_args()
00129     connectstring=args.connect
00130     connectparser=connectstrParser.connectstrParser(connectstring)
00131     connectparser.parse()
00132     usedefaultfrontierconfig=False
00133     cacheconfigpath=''
00134     if connectparser.needsitelocalinfo():
00135         if not args.siteconfpath:
00136             cacheconfigpath=os.environ['CMS_PATH']
00137             if cacheconfigpath:
00138                 cacheconfigpath=os.path.join(cacheconfigpath,'SITECONF','local','JobConfig','site-local-config.xml')
00139             else:
00140                 usedefaultfrontierconfig=True
00141         else:
00142             cacheconfigpath=args.siteconfpath
00143             cacheconfigpath=os.path.join(cacheconfigpath,'site-local-config.xml')
00144         p=cacheconfigParser.cacheconfigParser()
00145         if usedefaultfrontierconfig:
00146             p.parseString(c.defaultfrontierConfigString)
00147         else:
00148             p.parse(cacheconfigpath)
00149         connectstring=connectparser.fullfrontierStr(connectparser.schemaname(),p.parameterdict())
00150     runnumber=0
00151     svc = coral.ConnectionService()
00152     if args.debug :
00153         msg=coral.MessageStream('')
00154         msg.setMsgVerbosity(coral.message_Level_Debug)
00155     ifilename=''
00156     ofilename='instlumi.csv'
00157     beammode='stable'
00158     timeformat=''
00159     selectionDict={}
00160     if args.authpath and len(args.authpath)!=0:
00161         os.environ['CORAL_AUTH_PATH']=args.authpath
00162     if args.normfactor:
00163         c.NORM=float(args.normfactor)
00164     if args.lumiversion:
00165         c.LUMIVERSION=args.lumiversion
00166     if args.beammode:
00167         c.BEAMMODE=args.beammode
00168     if args.verbose:
00169         c.VERBOSE=True
00170     if args.inputfile:
00171         ifilename=args.inputfile
00172     if args.batch:
00173         opicname=args.batch
00174     if args.outputfile:
00175         ofilename=args.outputfile
00176     if args.timeformat:
00177         timeformat=args.timeformat
00178     session=svc.connect(connectstring,accessMode=coral.access_Update)
00179     session.typeConverter().setCppTypeForSqlType("unsigned int","NUMBER(10)")
00180     session.typeConverter().setCppTypeForSqlType("unsigned long long","NUMBER(20)")
00181     if ifilename:
00182         ifparser=inputFilesetParser(ifilename)
00183         runsandls=ifparser.runsandls()
00184         keylist=runsandls.keys()
00185         keylist.sort()
00186         for run in keylist:
00187             if selectionDict.has_key(run):
00188                 lslist=runsandls[run]
00189                 lslist.sort()
00190                 selectionDict[run]=lslist
00191     if args.action == 'run':
00192         minRun=int(args.begin)
00193         if not args.end:
00194             maxRun=minRun 
00195         else:
00196             maxRun=int(args.end)            
00197         runList=range(minRun,maxRun+1)
00198     elif args.action == 'peakperday':
00199         session.transaction().start(True)
00200         t=lumiTime.lumiTime()
00201         minTime=t.StrToDatetime(args.begin,timeformat)
00202         if not args.end:
00203             maxTime=datetime.datetime.utcnow() #to now
00204         else:
00205             maxTime=t.StrToDatetime(args.end,timeformat)
00206         #print minTime,maxTime
00207         qHandle=session.nominalSchema().newQuery()
00208         runDict=lumiQueryAPI.runsByTimerange(qHandle,minTime,maxTime)#xrawdata
00209         session.transaction().commit()
00210         runList=runDict.keys()
00211         del qHandle
00212         runList.sort()
00213     else:
00214         print 'unsupported action ',args.action
00215         exit
00216     #print 'runList ',runList
00217     #print 'runDict ', runDict               
00218     fig=Figure(figsize=(6,4.5),dpi=100)
00219     m=matplotRender.matplotRender(fig)
00220 
00221     logfig=Figure(figsize=(6,4.5),dpi=100)
00222     mlog=matplotRender.matplotRender(logfig)
00223     
00224     if args.action == 'peakperday':
00225         l=lumiTime.lumiTime()
00226         lumiperls=getInstLumiPerLS(session,c,runList,selectionDict)
00227         if args.outputfile:
00228             reporter=csvReporter.csvReporter(ofilename)
00229             fieldnames=['day','run','lsnum','maxinstlumi']
00230             reporter.writeRow(fieldnames)
00231         #minDay=minTime.toordinal()
00232         #maxDay=maxTime.toordinal()
00233         daydict={}#{day:[[run,lsnum,instlumi]]}
00234         result={}#{day:[maxrun,maxlsnum,maxinstlumi]}
00235         for lsdata in lumiperls:
00236             runnumber=lsdata[0]
00237             lsnum=lsdata[1]
00238             runstarttimeStr=lsdata[-2]#note: it is a string!!
00239             startorbit=lsdata[5]
00240             deliveredInst=lsdata[2]
00241             lsstarttime=l.OrbitToTime(runstarttimeStr,startorbit)
00242             day=lsstarttime.toordinal()
00243             if not daydict.has_key(day):
00244                 daydict[day]=[]
00245             daydict[day].append([runnumber,lsnum,deliveredInst])
00246         days=daydict.keys()
00247         days.sort()
00248         for day in days:
00249             daydata=daydict[day]
00250             transposeddata=CommonUtil.transposed(daydata,defaultval=0.0)
00251             todaysmaxinst=max(transposeddata[2])
00252             todaysmaxidx=transposeddata[2].index(todaysmaxinst)
00253             todaysmaxrun=transposeddata[0][todaysmaxidx]
00254             todaysmaxls=transposeddata[1][todaysmaxidx]
00255             result[day]=[todaysmaxrun,todaysmaxls,todaysmaxinst]
00256             if args.outputfile :
00257                 reporter.writeRow([day,todaysmaxrun,todaysmaxls,todaysmaxinst])
00258         m.plotPeakPerday_Time(result,minTime,maxTime,annotateBoundaryRunnum=args.annotateboundary,yscale='linear')
00259         mlog.plotPeakPerday_Time(result,minTime,maxTime,annotateBoundaryRunnum=args.annotateboundary,yscale='log')
00260         
00261     if args.action == 'run':
00262         runnumber=runList[0]
00263         lumiperrun=getLumiPerRun(session,c,runnumber)#[[lsnumber,deliveredInst,recordedInst,norbit,startorbit,fillnum,runstarttime,runstoptime]]
00264         #print 'lumiperrun ',lumiperrun
00265         xdata=[]#[runnumber,fillnum,norbit,stattime,stoptime,totalls,ncmsls]
00266         ydata={}#{label:[instlumi]}
00267         ydata['Delivered']=[]
00268         ydata['Recorded']=[]
00269         norbit=lumiperrun[0][3]
00270         fillnum=lumiperrun[0][-3]
00271         starttime=lumiperrun[0][-2]
00272         stoptime=lumiperrun[0][-1]
00273         ncmsls=0
00274         totalls=len(lumiperrun)
00275         for lsdata in lumiperrun:
00276             lsnumber=lsdata[0]
00277             if lsnumber!=0:
00278                 ncmsls+=1
00279             deliveredInst=lsdata[1]
00280             recordedInst=lsdata[2]
00281             ydata['Delivered'].append(deliveredInst)
00282             ydata['Recorded'].append(recordedInst)
00283         xdata=[runnumber,fillnum,norbit,starttime,stoptime,totalls,ncmsls]
00284         m.plotInst_RunLS(xdata,ydata)
00285     del session
00286     del svc
00287     if args.batch and args.yscale=='linear':
00288         m.drawPNG(args.batch)
00289     elif  args.batch and args.yscale=='log':
00290         mlog.drawPNG(args.batch)
00291     elif args.batch and args.yscale=='both':
00292         m.drawPNG(args.batch)
00293         basename,extension=os.path.splitext(args.batch)
00294         logfilename=basename+'_log'+extension        
00295         mlog.drawPNG(logfilename)
00296     else:
00297         raise Exception('unsupported yscale for batch mode : '+args.yscale)
00298     
00299     if not args.interactive:
00300         return
00301     if args.interactive is True and args.yscale=='linear':
00302         m.drawInteractive()
00303     elif args.interactive is True and args.yscale=='log':
00304         mlog.drawInteractive()
00305     else:
00306         raise Exception('unsupported yscale for interactive mode : '+args.yscale)
    

Variable Documentation

string lumiInstPlot::VERSION = '1.00'

Definition at line 2 of file lumiInstPlot.py.