00194 :
00195 allowedscales=['linear','log','both']
00196 c=constants()
00197 parser = argparse.ArgumentParser(prog=os.path.basename(sys.argv[0]),description="Plot integrated luminosity as function of the time variable of choice",formatter_class=argparse.ArgumentDefaultsHelpFormatter)
00198
00199 parser.add_argument('-c',dest='connect',action='store',required=True,help='connect string to lumiDB')
00200
00201 parser.add_argument('-P',dest='authpath',action='store',help='path to authentication file')
00202 parser.add_argument('-n',dest='normfactor',action='store',help='normalization factor (optional, default to 1.0)')
00203 parser.add_argument('-i',dest='inputfile',action='store',help='lumi range selection file (optional)')
00204 parser.add_argument('-o',dest='outputfile',action='store',help='csv outputfile name (optional)')
00205 parser.add_argument('-lumiversion',dest='lumiversion',default='0001',action='store',required=False,help='lumi data version')
00206 parser.add_argument('-begin',dest='begin',action='store',help='begin value of x-axi (required)')
00207 parser.add_argument('-end',dest='end',action='store',help='end value of x-axi (optional). Default to the maximum exists DB')
00208 parser.add_argument('-beamenergy',dest='beamenergy',action='store',type=float,required=False,help='beamenergy (in GeV) selection criteria,e.g. 3.5e3')
00209 parser.add_argument('-beamfluctuation',dest='beamfluctuation',action='store',type=float,required=False,help='allowed fraction of beamenergy to fluctuate, e.g. 0.1')
00210 parser.add_argument('-beamstatus',dest='beamstatus',action='store',required=False,help='selection criteria beam status,e.g. STABLE BEAMS')
00211 parser.add_argument('-yscale',dest='yscale',action='store',required=False,default='linear',help='y_scale')
00212 parser.add_argument('-hltpath',dest='hltpath',action='store',help='specific hltpath to calculate the recorded luminosity. If specified aoverlays the recorded luminosity for the hltpath on the plot')
00213 parser.add_argument('-batch',dest='batch',action='store',help='graphical mode to produce PNG file. Specify graphical file here. Default to lumiSum.png')
00214 parser.add_argument('--annotateboundary',dest='annotateboundary',action='store_true',help='annotate boundary run numbers')
00215 parser.add_argument('--interactive',dest='interactive',action='store_true',help='graphical mode to draw plot in a TK pannel.')
00216 parser.add_argument('-timeformat',dest='timeformat',action='store',help='specific python timeformat string (optional). Default mm/dd/yy hh:min:ss.00')
00217 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')
00218 parser.add_argument('action',choices=['run','fill','time','perday'],help='x-axis data type of choice')
00219
00220 parser.add_argument('--verbose',dest='verbose',action='store_true',help='verbose mode, print result also to screen')
00221 parser.add_argument('--debug',dest='debug',action='store_true',help='debug')
00222
00223 batchmode=True
00224 args=parser.parse_args()
00225 connectstring=args.connect
00226 begvalue=args.begin
00227 endvalue=args.end
00228 beamstatus=args.beamstatus
00229 beamenergy=args.beamenergy
00230 beamfluctuation=args.beamfluctuation
00231 xaxitype='run'
00232 connectparser=connectstrParser.connectstrParser(connectstring)
00233 connectparser.parse()
00234 usedefaultfrontierconfig=False
00235 cacheconfigpath=''
00236 if connectparser.needsitelocalinfo():
00237 if not args.siteconfpath:
00238 cacheconfigpath=os.environ['CMS_PATH']
00239 if cacheconfigpath:
00240 cacheconfigpath=os.path.join(cacheconfigpath,'SITECONF','local','JobConfig','site-local-config.xml')
00241 else:
00242 usedefaultfrontierconfig=True
00243 else:
00244 cacheconfigpath=args.siteconfpath
00245 cacheconfigpath=os.path.join(cacheconfigpath,'site-local-config.xml')
00246 p=cacheconfigParser.cacheconfigParser()
00247 if usedefaultfrontierconfig:
00248 p.parseString(c.defaultfrontierConfigString)
00249 else:
00250 p.parse(cacheconfigpath)
00251 connectstring=connectparser.fullfrontierStr(connectparser.schemaname(),p.parameterdict())
00252
00253 runnumber=0
00254 svc = coral.ConnectionService()
00255 hltpath=''
00256 if args.hltpath:
00257 hltpath=args.hltpath
00258 if args.debug :
00259 msg=coral.MessageStream('')
00260 msg.setMsgVerbosity(coral.message_Level_Debug)
00261 ifilename=''
00262 ofilename='integratedlumi.png'
00263 timeformat=''
00264 if args.authpath and len(args.authpath)!=0:
00265 os.environ['CORAL_AUTH_PATH']=args.authpath
00266 if args.normfactor:
00267 c.NORM=float(args.normfactor)
00268 if args.lumiversion:
00269 c.LUMIVERSION=args.lumiversion
00270 if args.verbose:
00271 c.VERBOSE=True
00272 if args.inputfile:
00273 ifilename=args.inputfile
00274 if args.batch:
00275 opicname=args.batch
00276 if args.outputfile:
00277 ofilename=args.outputfile
00278 if args.timeformat:
00279 timeformat=args.timeformat
00280 session=svc.connect(connectstring,accessMode=coral.access_Update)
00281 session.typeConverter().setCppTypeForSqlType("unsigned int","NUMBER(10)")
00282 session.typeConverter().setCppTypeForSqlType("unsigned long long","NUMBER(20)")
00283 inputfilecontent=''
00284 fileparsingResult=''
00285 runList=[]
00286 runDict={}
00287 fillDict={}
00288 selectionDict={}
00289 minTime=''
00290 maxTime=''
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304 if len(ifilename)!=0:
00305 ifparser=inputFilesetParser.inputFilesetParser(ifilename)
00306 runsandls=ifparser.runsandls()
00307 keylist=runsandls.keys()
00308 keylist.sort()
00309 for run in keylist:
00310 if not selectionDict.has_key(run):
00311 lslist=runsandls[run]
00312 lslist.sort()
00313 selectionDict[run]=lslist
00314 if args.action == 'run':
00315 if not args.end:
00316 session.transaction().start(True)
00317 schema=session.nominalSchema()
00318 lastrun=max(lumiQueryAPI.allruns(schema,requireRunsummary=True,requireLumisummary=True,requireTrg=True,requireHlt=True))
00319 session.transaction().commit()
00320 else:
00321 lastrun=int(args.end)
00322 for r in range(int(args.begin),lastrun+1):
00323 runList.append(r)
00324 elif args.action == 'fill':
00325 session.transaction().start(True)
00326 maxfill=None
00327 if not args.end:
00328 qHandle=session.nominalSchema().newQuery()
00329 maxfill=max(lumiQueryAPI.allfills(qHandle,filtercrazy=True))
00330 del qHandle
00331 else:
00332 maxfill=int(args.end)
00333 qHandle=session.nominalSchema().newQuery()
00334 fillDict=lumiQueryAPI.runsByfillrange(qHandle,int(args.begin),maxfill)
00335 del qHandle
00336 session.transaction().commit()
00337
00338 for fill in range(int(args.begin),maxfill+1):
00339 if fillDict.has_key(fill):
00340 for run in fillDict[fill]:
00341 runList.append(run)
00342 elif args.action == 'time' or args.action == 'perday':
00343 session.transaction().start(True)
00344 t=lumiTime.lumiTime()
00345 minTime=t.StrToDatetime(args.begin,timeformat)
00346 if not args.end:
00347 maxTime=datetime.datetime.utcnow()
00348 else:
00349 maxTime=t.StrToDatetime(args.end,timeformat)
00350
00351 qHandle=session.nominalSchema().newQuery()
00352 runDict=lumiQueryAPI.runsByTimerange(qHandle,minTime,maxTime)
00353 session.transaction().commit()
00354 runList=runDict.keys()
00355 del qHandle
00356
00357 else:
00358 print 'unsupported action ',args.action
00359 exit
00360 runList.sort()
00361
00362
00363
00364 fig=Figure(figsize=(6,4.5),dpi=100)
00365 m=matplotRender.matplotRender(fig)
00366
00367 logfig=Figure(figsize=(6.8,4.5),dpi=100)
00368 mlog=matplotRender.matplotRender(logfig)
00369
00370 if args.action == 'run':
00371 result={}
00372 result=getLumiInfoForRuns(session,c,runList,selectionDict,hltpath,beamstatus=beamstatus,beamenergy=beamenergy,beamfluctuation=beamfluctuation)
00373 xdata=[]
00374 ydata={}
00375 ydata['Delivered']=[]
00376 ydata['Recorded']=[]
00377 keylist=result.keys()
00378 keylist.sort()
00379 if args.outputfile:
00380 reporter=csvReporter.csvReporter(ofilename)
00381 fieldnames=['run','delivered','recorded']
00382 reporter.writeRow(fieldnames)
00383 for run in keylist:
00384 xdata.append(run)
00385 delivered=result[run][0]
00386 recorded=result[run][1]
00387 ydata['Delivered'].append(delivered)
00388 ydata['Recorded'].append(recorded)
00389 if args.outputfile and (delivered!=0 or recorded!=0):
00390 reporter.writeRow([run,result[run][0],result[run][1]])
00391 m.plotSumX_Run(xdata,ydata,yscale='linear')
00392 mlog.plotSumX_Run(xdata,ydata,yscale='log')
00393 elif args.action == 'fill':
00394 lumiDict={}
00395 lumiDict=getLumiInfoForRuns(session,c,runList,selectionDict,hltpath,beamstatus=beamstatus,beamenergy=beamenergy,beamfluctuation=beamfluctuation)
00396 xdata=[]
00397 ydata={}
00398 ydata['Delivered']=[]
00399 ydata['Recorded']=[]
00400
00401
00402 if args.outputfile:
00403 reporter=csvReporter.csvReporter(ofilename)
00404 fieldnames=['fill','run','delivered','recorded']
00405 reporter.writeRow(fieldnames)
00406 fills=fillDict.keys()
00407 fills.sort()
00408 for fill in fills:
00409 runs=fillDict[fill]
00410 runs.sort()
00411 for run in runs:
00412 xdata.append(run)
00413 ydata['Delivered'].append(lumiDict[run][0])
00414 ydata['Recorded'].append(lumiDict[run][1])
00415 if args.outputfile :
00416 reporter.writeRow([fill,run,lumiDict[run][0],lumiDict[run][1]])
00417
00418 m.plotSumX_Fill(xdata,ydata,fillDict,yscale='linear')
00419 mlog.plotSumX_Fill(xdata,ydata,fillDict,yscale='log')
00420 elif args.action == 'time' :
00421 lumiDict={}
00422 lumiDict=getLumiInfoForRuns(session,c,runList,selectionDict,hltpath,beamstatus=beamstatus,beamenergy=beamenergy,beamfluctuation=beamfluctuation)
00423
00424 xdata={}
00425 ydata={}
00426 ydata['Delivered']=[]
00427 ydata['Recorded']=[]
00428 keylist=lumiDict.keys()
00429 keylist.sort()
00430 if args.outputfile:
00431 reporter=csvReporter.csvReporter(ofilename)
00432 fieldnames=['run','starttime','stoptime','delivered','recorded']
00433 reporter.writeRow(fieldnames)
00434 for run in keylist:
00435 ydata['Delivered'].append(lumiDict[run][0])
00436 ydata['Recorded'].append(lumiDict[run][1])
00437 starttime=runDict[run][0]
00438 stoptime=runDict[run][1]
00439 xdata[run]=[starttime,stoptime]
00440 if args.outputfile :
00441 reporter.writeRow([run,starttime,stoptime,lumiDict[run][0],lumiDict[run][1]])
00442 m.plotSumX_Time(xdata,ydata,minTime,maxTime,hltpath=hltpath,annotateBoundaryRunnum=args.annotateboundary,yscale='linear')
00443 mlog.plotSumX_Time(xdata,ydata,minTime,maxTime,hltpath=hltpath,annotateBoundaryRunnum=args.annotateboundary,yscale='log')
00444 elif args.action == 'perday':
00445 daydict={}
00446 lumibyls=getLumiOrderByLS(session,c,runList,selectionDict,hltpath,beamstatus=beamstatus,beamenergy=beamenergy,beamfluctuation=beamfluctuation)
00447
00448
00449 if args.outputfile:
00450 reporter=csvReporter.csvReporter(ofilename)
00451 fieldnames=['day','begrunls','endrunls','delivered','recorded']
00452 reporter.writeRow(fieldnames)
00453 beginfo=[lumibyls[0][3],str(lumibyls[0][0])+':'+str(lumibyls[0][2])]
00454 endinfo=[lumibyls[-1][3],str(lumibyls[-1][0])+':'+str(lumibyls[-1][2])]
00455 for perlsdata in lumibyls:
00456 lsstarttime=perlsdata[3]
00457 delivered=perlsdata[4]
00458 recorded=perlsdata[5]
00459 day=lsstarttime.toordinal()
00460 if not daydict.has_key(day):
00461 daydict[day]=[]
00462 daydict[day].append([delivered,recorded])
00463 days=daydict.keys()
00464 days.sort()
00465 daymin=days[0]
00466 daymax=days[-1]
00467
00468 resultbyday={}
00469 resultbyday['Delivered']=[]
00470 resultbyday['Recorded']=[]
00471
00472
00473
00474 for day in range(daymin,daymax+1):
00475 if not daydict.has_key(day):
00476 delivered=0.0
00477 recorded=0.0
00478 else:
00479 daydata=daydict[day]
00480 mytransposed=CommonUtil.transposed(daydata,defaultval=0.0)
00481 delivered=sum(mytransposed[0])
00482 recorded=sum(mytransposed[1])
00483 resultbyday['Delivered'].append(delivered)
00484 resultbyday['Recorded'].append(recorded)
00485 if args.outputfile:
00486 reporter.writeRow([day,beginfo[1],endinfo[1],delivered,recorded])
00487
00488
00489
00490 m.plotPerdayX_Time( range(daymin,daymax+1) ,resultbyday,minTime,maxTime,boundaryInfo=[beginfo,endinfo],annotateBoundaryRunnum=args.annotateboundary,yscale='linear')
00491 mlog.plotPerdayX_Time( range(daymin,daymax+1),resultbyday,minTime,maxTime,boundaryInfo=[beginfo,endinfo],annotateBoundaryRunnum=args.annotateboundary,yscale='log')
00492 else:
00493 raise Exception,'must specify the type of x-axi'
00494
00495 del session
00496 del svc
00497
00498 if args.batch and args.yscale=='linear':
00499 m.drawPNG(args.batch)
00500 elif args.batch and args.yscale=='log':
00501 mlog.drawPNG(args.batch)
00502 elif args.batch and args.yscale=='both':
00503 m.drawPNG(args.batch)
00504 basename,extension=os.path.splitext(args.batch)
00505 logfilename=basename+'_log'+extension
00506 mlog.drawPNG(logfilename)
00507 else:
00508 raise Exception('unsupported yscale for batch mode : '+args.yscale)
00509 if not args.interactive:
00510 return
00511 if args.interactive is True and args.yscale=='linear':
00512 m.drawInteractive()
00513 elif args.interactive is True and args.yscale=='log':
00514 mlog.drawInteractive()
00515 else:
raise Exception('unsupported yscale for interactive mode : '+args.yscale)