CMS 3D CMS Logo

/data/git/CMSSW_5_3_11_patch5/src/RecoLuminosity/LumiDB/scripts/lumiContext.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 ###############################################################################
00004 # Command to display runsummary, L1,HLT and beam data used in lumi caculation #
00005 #                                                                             #
00006 # Author:      Zhen Xie                                                       #
00007 ###############################################################################
00008 
00009 import os,sys,time
00010 from RecoLuminosity.LumiDB import sessionManager,lumiTime,inputFilesetParser,csvSelectionParser,csvReporter,argparse,CommonUtil,lumiCalcAPI,lumiReport,RegexValidator,lumiTime,revisionDML
00011 
00012 def parseInputFiles(inputfilename):
00013     '''
00014     output ({run:[cmsls,cmsls,...]},[[resultlines]])
00015     '''
00016     selectedrunlsInDB={}
00017     resultlines=[]
00018     p=inputFilesetParser.inputFilesetParser(inputfilename)
00019     runlsbyfile=p.runsandls()
00020     selectedProcessedRuns=p.selectedRunsWithresult()
00021     selectedNonProcessedRuns=p.selectedRunsWithoutresult()
00022     resultlines=p.resultlines()
00023     for runinfile in selectedNonProcessedRuns:
00024         selectedrunlsInDB[runinfile]=runlsbyfile[runinfile]
00025     return (selectedrunlsInDB,resultlines)
00026 
00027 ##############################
00028 ## ######################## ##
00029 ## ## ################## ## ##
00030 ## ## ## Main Program ## ## ##
00031 ## ## ################## ## ##
00032 ## ######################## ##
00033 ##############################
00034 
00035 if __name__ == '__main__':
00036     parser = argparse.ArgumentParser(prog=os.path.basename(sys.argv[0]),description = "Additional information needed in the lumi calculation",formatter_class=argparse.ArgumentDefaultsHelpFormatter)
00037     allowedActions = ['hltbyls','hltmenu','trgbyls', 'beambyls','runsummary']
00038     amodetagChoices = [ "PROTPHYS","IONPHYS","PAPHYS" ]
00039     beamModeChoices = ["stable"]
00040     #
00041     # parse arguments
00042     #  
00043     #
00044     ################################################
00045     # basic arguments
00046     ################################################
00047     #
00048     parser.add_argument('action',choices=allowedActions,
00049                         help='command actions')
00050     parser.add_argument('-c',dest='connect',action='store',
00051                         required=False,
00052                         help='connect string to lumiDB,optional',
00053                         default='frontier://LumiCalc/CMS_LUMI_PROD')
00054     parser.add_argument('-P',dest='authpath',action='store',
00055                         required=False,
00056                         help='path to authentication file (optional)')
00057     parser.add_argument('-r',dest='runnumber',action='store',
00058                         type=int,
00059                         required=False,
00060                         help='run number (optional)')
00061     parser.add_argument('-o',dest='outputfile',action='store',
00062                         required=False,
00063                         help='output to csv file (optional)')
00064     #################################################
00065     #arg to select exact run and ls
00066     #################################################
00067     parser.add_argument('-i',dest='inputfile',action='store',
00068                         required=False,
00069                         help='run/ls selection file (optional)')
00070     parser.add_argument('--name',dest='name',action='store',
00071                        help='hltpath/l1bit name/pattern'
00072                        )
00073     #
00074     #optional args to filter *runs*, they do not select on LS level.
00075     #
00076     parser.add_argument('-b',dest='beammode',action='store',
00077                         choices=beamModeChoices,
00078                         required=False,
00079                         help='beam mode choices [stable] (optional)')
00080     parser.add_argument('-f','--fill',dest='fillnum',action='store',
00081                         default=None,required=False,
00082                         help='fill number (optional) ')
00083     parser.add_argument('--amodetag',dest='amodetag',action='store',
00084                         choices=amodetagChoices,
00085                         required=False,
00086                         help='specific accelerator mode choices [PROTOPHYS,IONPHYS] (optional)')
00087     parser.add_argument('--beamenergy',dest='beamenergy',action='store',
00088                         type=float,
00089                         default=None,
00090                         help='nominal beam energy in GeV')
00091     parser.add_argument('--beamfluctuation',dest='beamfluctuation',
00092                         type=float,action='store',
00093                         default=0.2,
00094                         required=False,
00095                         help='fluctuation in fraction allowed to nominal beam energy, default 0.2, to be used together with -beamenergy  (optional)')
00096     parser.add_argument('--minintensity',dest='minintensity',
00097                         type=float,action='store',
00098                         default=0.1,
00099                         required=False,
00100                         help='filter on beam intensity , effective with --with-beamintensity (optional)')
00101     parser.add_argument('--begin',dest='begin',action='store',
00102                         default=None,
00103                         required=False,
00104                         type=RegexValidator.RegexValidator("^\d\d/\d\d/\d\d \d\d:\d\d:\d\d$","must be form mm/dd/yy hh:mm:ss"),
00105                         help='min run start time, mm/dd/yy hh:mm:ss)'
00106                         )
00107     parser.add_argument('--end',dest='end',action='store',
00108                         default=None,
00109                         required=False,
00110                         type=RegexValidator.RegexValidator("^\d\d/\d\d/\d\d \d\d:\d\d:\d\d$","must be form mm/dd/yy hh:mm:ss"),
00111                         help='max run start time, mm/dd/yy hh:mm:ss'
00112                         )
00113     #################################################
00114     #versions control
00115     #################################################
00116     parser.add_argument('--datatag',dest='datatag',action='store',
00117                         required=False,
00118                         help='version of lumi/trg/hlt data'
00119                         )
00120     #
00121     #command configuration 
00122     #
00123     parser.add_argument('--siteconfpath',dest='siteconfpath',action='store',
00124                         default=None,
00125                         required=False,
00126                         help='specific path to site-local-config.xml file, optional. If path undefined, fallback to cern proxy&server')
00127     #################################################
00128     #switches
00129     #################################################                    
00130     parser.add_argument('--with-beamintensity',
00131                         dest='withbeamintensity',
00132                         action='store_true',
00133                         help='dump beam intensity'
00134                         )
00135     parser.add_argument('--without-mask',
00136                         dest='withoutmask',
00137                         action='store_true',
00138                         help='not considering trigger mask'
00139                         )
00140     parser.add_argument('--verbose',
00141                         dest='verbose',
00142                         action='store_true',
00143                         help='verbose mode for printing' )
00144     parser.add_argument('--nowarning',
00145                         dest='nowarning',
00146                         action='store_true',
00147                         help='suppress bad for lumi warnings'
00148                         )
00149     parser.add_argument('--debug',
00150                         dest='debug',
00151                         action='store_true',
00152                         help='debug')
00153     
00154     options=parser.parse_args()
00155     if not options.runnumber and not options.inputfile and not options.fillnum and not options.begin:
00156         raise RuntimeError('at least one run selection argument in [-r,-f,-i,--begin] is required')
00157     reqrunmin=None
00158     reqfillmin=None
00159     reqtimemin=None
00160     reqrunmax=None
00161     reqfillmax=None
00162     reqtimemax=None
00163     timeFilter=[None,None]
00164     pbeammode = None
00165     iresults=[]
00166     reqTrg=False
00167     reqHlt=False
00168     if options.beammode=='stable':
00169         pbeammode = 'STABLE BEAMS'
00170     if options.action=='trgbyls':
00171         reqTrg=True
00172     if options.action=='hltbyls':
00173         reqHlt=True
00174     if options.runnumber:
00175         reqrunmax=options.runnumber
00176         reqrunmin=options.runnumber
00177     if options.fillnum:
00178         reqfillmin=options.fillnum
00179         reqfillmax=options.fillnum
00180     if options.begin:
00181         (reqrunmin,reqfillmin,reqtimemin)=CommonUtil.parseTime(options.begin)
00182         if reqtimemin:
00183             lute=lumiTime.lumiTime()
00184             reqtimeminT=lute.StrToDatetime(reqtimemin,customfm='%m/%d/%y %H:%M:%S')
00185             timeFilter[0]=reqtimeminT
00186     if options.end:
00187         (reqrunmax,reqfillmax,reqtimemax)=CommonUtil.parseTime(options.end)
00188         if reqtimemax:
00189             lute=lumiTime.lumiTime()
00190             reqtimemaxT=lute.StrToDatetime(reqtimemax,customfm='%m/%d/%y %H:%M:%S')
00191             timeFilter[1]=reqtimemaxT
00192     sname=options.name
00193     isdetail=False
00194     spattern=None
00195     if sname is not None:
00196         isdetail=True
00197         if sname=='*' or sname=='all':
00198             sname=None
00199         elif 1 in [c in sname for c in '*?[]']: #is a fnmatch pattern
00200             spattern=sname
00201             sname=None
00202     if  options.action == 'beambyls' and options.withbeamintensity and not options.outputfile:
00203         print '[warning] --with-beamintensity must write data to a file, none specified using default "beamintensity.csv"'
00204         options.outputfile='beamintensity.csv'
00205         
00206     ##############################################################
00207     # check working environment
00208     ##############################################################
00209     #
00210     # check DB environment
00211     #    
00212     if options.authpath:
00213         os.environ['CORAL_AUTH_PATH'] = options.authpath
00214 
00215     svc=sessionManager.sessionManager(options.connect,
00216                                       authpath=options.authpath,
00217                                       siteconfpath=options.siteconfpath,
00218                                       debugON=options.debug)
00219     session=svc.openSession(isReadOnly=True,cpp2sqltype=[('unsigned int','NUMBER(10)'),('unsigned long long','NUMBER(20)')])
00220     
00221     ##############################################################
00222     # check run/ls list
00223     ##############################################################
00224     
00225     irunlsdict={}
00226     rruns=[]
00227     session.transaction().start(True)
00228     filerunlist=None
00229     if options.inputfile:
00230         (irunlsdict,iresults)=parseInputFiles(options.inputfile)
00231         filerunlist=irunlsdict.keys()
00232 
00233     datatagname=options.datatag
00234     if not datatagname:
00235         (datatagid,datatagname)=revisionDML.currentDataTag(session.nominalSchema())
00236     else:
00237         datatagid=revisionDML.getDataTagId(session.nominalSchema(),datatagname)
00238 
00239     dataidmap=lumiCalcAPI.runList(session.nominalSchema(),datatagid,runmin=reqrunmin,runmax=reqrunmax,fillmin=reqfillmin,fillmax=reqfillmax,startT=reqtimemin,stopT=reqtimemax,l1keyPattern=None,hltkeyPattern=None,amodetag=options.amodetag,nominalEnergy=options.beamenergy,energyFlut=options.beamfluctuation,requiretrg=reqTrg,requirehlt=reqHlt,preselectedruns=filerunlist)
00240     if not dataidmap:
00241         print '[INFO] No qualified run found, do nothing'
00242         sys.exit(14)
00243     rruns=[]
00244     #crosscheck dataid value
00245     for irun,(lid,tid,hid) in dataidmap.items():
00246         if not lid:
00247             print '[INFO] No qualified lumi data found for run, ',irun
00248         if reqTrg and not tid:
00249             print '[INFO] No qualified trg data found for run ',irun
00250             continue
00251         if reqHlt and not hid:
00252             print '[INFO] No qualified hlt data found for run ',irun
00253             continue
00254         rruns.append(irun)
00255     if not irunlsdict: #no file
00256         irunlsdict=dict(zip(rruns,[None]*len(rruns)))
00257     else:
00258         for selectedrun in irunlsdict.keys():#if there's further filter on the runlist,clean input dict
00259             if selectedrun not in rruns:
00260                 del irunlsdict[selectedrun]
00261     if not irunlsdict:
00262         print '[INFO] No qualified run found, do nothing'
00263         sys.exit(13)
00264         
00265     session.transaction().commit()
00266     thiscmmd=sys.argv[0]
00267     lumiReport.toScreenHeader(thiscmmd,datatagname,'n/a','n/a','n/a','n/a')
00268     
00269     if options.action == 'trgbyls':
00270         session.transaction().start(True)
00271         result=lumiCalcAPI.trgForIds(session.nominalSchema(),irunlsdict,dataidmap,trgbitname=sname,trgbitnamepattern=spattern,withL1Count=True,withPrescale=True)
00272         session.transaction().commit()
00273         if not options.outputfile:
00274             lumiReport.toScreenLSTrg(result,iresults=iresults,irunlsdict=irunlsdict,noWarning=options.nowarning,withoutmask=options.withoutmask)
00275         else:
00276             lumiReport.toScreenLSTrg(result,iresults=iresults,irunlsdict=irunlsdict,noWarning=options.nowarning,toFile=options.outputfile,withoutmask=options.withoutmask)
00277     #print result
00278         sys.exit(0)
00279     if options.action == 'hltbyls':
00280         if not options.name:
00281             print '[ERROR] --name option is required by hltbyls, do nothing'
00282             sys.exit(0)
00283         withL1Pass=True
00284         withHLTAccept=True
00285         session.transaction().start(True)
00286         result=lumiCalcAPI.hltForIds(session.nominalSchema(),irunlsdict,dataidmap,hltpathname=sname,hltpathpattern=spattern,withL1Pass=withL1Pass,withHLTAccept=withHLTAccept)
00287         session.transaction().commit()
00288         if not options.outputfile:
00289             lumiReport.toScreenLSHlt(result,iresults=iresults)
00290         else:
00291             lumiReport.toScreenLSHlt(result,iresults=iresults,toFile=options.outputfile)
00292     if options.action == 'hltmenu':
00293         session.transaction().start(True)
00294         result=lumiCalcAPI.hltpathsForRange(session.nominalSchema(),irunlsdict,hltpathname=sname,hltpathpattern=spattern)
00295         session.transaction().commit()
00296         #print result
00297         if not options.outputfile:
00298             lumiReport.toScreenConfHlt(result,iresults)
00299         else:
00300             lumiReport.toScreenConfHlt(result,iresults,toFile=options.outputfile)
00301     if options.action == 'beambyls':
00302         session.transaction().start(True)
00303         dumpbeamintensity=False
00304         if options.outputfile and options.verbose:
00305             dumpbeamintensity=True
00306         result=lumiCalcAPI.beamForIds(session.nominalSchema(),irunlsdict,dataidmap,withBeamIntensity=options.withbeamintensity,minIntensity=options.minintensity)
00307         session.transaction().commit()
00308         if not options.outputfile:
00309             lumiReport.toScreenLSBeam(result,iresults=iresults,dumpIntensity=False)
00310         else:
00311             lumiReport.toScreenLSBeam(result,iresults=iresults,dumpIntensity=options.withbeamintensity,toFile=options.outputfile)
00312     if options.action == 'runsummary':
00313         session.transaction().start(True)
00314         result=lumiCalcAPI.runsummary(session.nominalSchema(),irunlsdict)
00315         session.transaction().commit()
00316         c=lumiTime.lumiTime()
00317         for r in result:
00318             run=r[0]
00319             fill='n/a'
00320             if r[5]:
00321                 fill=str(r[5])
00322             starttime=c.StrToDatetime(r[7])
00323             starttime=starttime.strftime('%m/%d/%y %H:%M:%S')
00324             stoptime=c.StrToDatetime(r[8])
00325             stoptime=stoptime.strftime('%m/%d/%y %H:%M:%S')
00326             l1key=r[1]
00327             hltkey=r[4]
00328             amodetag=r[2]
00329             egev='n/a'
00330             if r[3]:
00331                 egev=str(r[3])
00332             sequence=r[6]
00333             print '==='
00334             print 'Run ',str(run),' Fill ',fill,' Amodetag ',amodetag,' egev ',egev
00335             print '\tStart '+starttime,'                  ',' Stop ',stoptime
00336             print '\tL1key ',l1key,' HLTkey ',hltkey
00337     del session
00338     del svc