CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/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     parser.add_argument('--verbose',
00135                         dest='verbose',
00136                         action='store_true',
00137                         help='verbose mode for printing' )
00138     parser.add_argument('--nowarning',
00139                         dest='nowarning',
00140                         action='store_true',
00141                         help='suppress bad for lumi warnings'
00142                         )
00143     parser.add_argument('--debug',
00144                         dest='debug',
00145                         action='store_true',
00146                         help='debug')
00147     
00148     options=parser.parse_args()
00149     #
00150     # check DB environment
00151     #                        
00152     if options.authpath:
00153         os.environ['CORAL_AUTH_PATH'] = options.authpath
00154         
00155     pbeammode = None
00156     sname=options.name
00157     isdetail=False
00158     spattern=None
00159     if sname is not None:
00160         isdetail=True
00161         if sname=='*' or sname=='all':
00162             sname=None
00163         elif 1 in [c in sname for c in '*?[]']: #is a fnmatch pattern
00164             spattern=sname
00165             sname=None
00166     if  options.action == 'beambyls' and options.withbeamintensity and not options.outputfile:
00167         print '[warning] --with-beamintensity must write data to a file, none specified using default "beamintensity.csv"'
00168         options.outputfile='beamintensity.csv'
00169     if options.beammode=='stable':
00170         pbeammode    = 'STABLE BEAMS'
00171     svc=sessionManager.sessionManager(options.connect,
00172                                       authpath=options.authpath,
00173                                       siteconfpath=options.siteconfpath,
00174                                       debugON=options.debug)
00175     session=svc.openSession(isReadOnly=True,cpp2sqltype=[('unsigned int','NUMBER(10)'),('unsigned long long','NUMBER(20)')])
00176     session.transaction().start(True)
00177     irunlsdict={}
00178     rruns=[]
00179     iresults=[]
00180     reqTrg=False
00181     reqHlt=False
00182     if options.action=='trgbyls':
00183         reqTrg=True
00184     if options.action=='hltbyls':
00185         reqHlt=True
00186     if options.runnumber: # if runnumber specified, do not go through other run selection criteria
00187         irunlsdict[options.runnumber]=None
00188         rruns=irunlsdict.keys()
00189     else:
00190         if options.inputfile:
00191             (irunlsdict,iresults)=parseInputFiles(options.inputfile)
00192             if options.fillnum or options.begin or options.end or options.amodetag or options.beamenergy:      
00193                 runlist=lumiCalcAPI.runList(session.nominalSchema(),options.fillnum,runmin=None,runmax=None,startT=options.begin,stopT=options.end,l1keyPattern=None,hltkeyPattern=None,amodetag=options.amodetag,nominalEnergy=options.beamenergy,energyFlut=options.beamfluctuation,requiretrg=reqTrg,requirehlt=reqHlt)
00194                 rruns=[val for val in runlist if val in irunlsdict.keys()]
00195                 for selectedrun in irunlsdict.keys():#if there's further filter on the runlist,clean input dict
00196                     if selectedrun not in rruns:
00197                         del irunlsdict[selectedrun]
00198             else:
00199                 rruns=irunlsdict.keys()
00200         else:
00201             runlist=lumiCalcAPI.runList(session.nominalSchema(),options.fillnum,runmin=None,runmax=None,startT=options.begin,stopT=options.end,l1keyPattern=None,hltkeyPattern=None,amodetag=options.amodetag,nominalEnergy=options.beamenergy,energyFlut=options.beamfluctuation,requiretrg=reqTrg,requirehlt=reqHlt)
00202             for run in runlist:
00203                 irunlsdict[run]=None
00204             rruns=irunlsdict.keys()
00205     datatagname=options.datatag
00206     if not datatagname:
00207         (datatagid,datatagname)=revisionDML.currentDataTag(session.nominalSchema())
00208         dataidmap=revisionDML.dataIdsByTagId(session.nominalSchema(),datatagid,runlist=rruns,withcomment=False)
00209         #{run:(lumidataid,trgdataid,hltdataid,())}
00210     else:
00211         dataidmap=revisionDML.dataIdsByTagName(session.nominalSchema(),datatagname,runlist=rruns,withcomment=False)
00212     session.transaction().commit()
00213     thiscmmd=sys.argv[0]
00214     lumiReport.toScreenHeader(thiscmmd,datatagname,'n/a','n/a','n/a','n/a')
00215     if not dataidmap:
00216         print '[INFO] No qualified data found, do nothing'
00217         sys.exit(0)
00218     
00219     if options.action == 'trgbyls':
00220         session.transaction().start(True)
00221         result=lumiCalcAPI.trgForIds(session.nominalSchema(),irunlsdict,dataidmap,trgbitname=sname,trgbitnamepattern=spattern,withL1Count=True,withPrescale=True)
00222         session.transaction().commit()
00223         if not options.outputfile:
00224             lumiReport.toScreenLSTrg(result,iresults=iresults,irunlsdict=irunlsdict,noWarning=options.nowarning)
00225         else:
00226             lumiReport.toScreenLSTrg(result,iresults=iresults,irunlsdict=irunlsdict,noWarning=options.nowarning,toFile=options.outputfile)
00227     #print result
00228         sys.exit(0)
00229     if options.action == 'hltbyls':
00230         if not options.name:
00231             print '[ERROR] --name option is required by hltbyls, do nothing'
00232             sys.exit(0)
00233         withL1Pass=True
00234         withHLTAccept=True
00235         session.transaction().start(True)
00236         result=lumiCalcAPI.hltForIds(session.nominalSchema(),irunlsdict,dataidmap,hltpathname=sname,hltpathpattern=spattern,withL1Pass=withL1Pass,withHLTAccept=withHLTAccept)
00237         session.transaction().commit()
00238         if not options.outputfile:
00239             lumiReport.toScreenLSHlt(result,iresults=iresults)
00240         else:
00241             lumiReport.toScreenLSHlt(result,iresults=iresults,toFile=options.outputfile)
00242     if options.action == 'hltmenu':
00243         session.transaction().start(True)
00244         result=lumiCalcAPI.hltpathsForRange(session.nominalSchema(),irunlsdict,hltpathname=sname,hltpathpattern=spattern)
00245         session.transaction().commit()
00246         #print result
00247         if not options.outputfile:
00248             lumiReport.toScreenConfHlt(result,iresults)
00249         else:
00250             lumiReport.toScreenConfHlt(result,iresults,toFile=options.outputfile)
00251     if options.action == 'beambyls':
00252         session.transaction().start(True)
00253         dumpbeamintensity=False
00254         if options.outputfile and options.verbose:
00255             dumpbeamintensity=True
00256         result=lumiCalcAPI.beamForIds(session.nominalSchema(),irunlsdict,dataidmap,withBeamIntensity=options.withbeamintensity,minIntensity=options.minintensity)
00257         session.transaction().commit()
00258         if not options.outputfile:
00259             lumiReport.toScreenLSBeam(result,iresults=iresults,dumpIntensity=False)
00260         else:
00261             lumiReport.toScreenLSBeam(result,iresults=iresults,dumpIntensity=options.withbeamintensity,toFile=options.outputfile)
00262     if options.action == 'runsummary':
00263         session.transaction().start(True)
00264         result=lumiCalcAPI.runsummary(session.nominalSchema(),irunlsdict)
00265         session.transaction().commit()
00266         c=lumiTime.lumiTime()
00267         for r in result:
00268             run=r[0]
00269             fill='n/a'
00270             if r[5]:
00271                 fill=str(r[5])
00272             starttime=c.StrToDatetime(r[7])
00273             starttime=starttime.strftime('%m/%d/%y %H:%M:%S')
00274             stoptime=c.StrToDatetime(r[8])
00275             stoptime=stoptime.strftime('%m/%d/%y %H:%M:%S')
00276             l1key=r[1]
00277             hltkey=r[4]
00278             amodetag=r[2]
00279             egev='n/a'
00280             if r[3]:
00281                 egev=str(r[3])
00282             sequence=r[6]
00283             print '==='
00284             print 'Run ',str(run),' Fill ',fill,' Amodetag ',amodetag,' egev ',egev
00285             print '\tStart '+starttime,'                  ',' Stop ',stoptime
00286             print '\tL1key ',l1key,' HLTkey ',hltkey
00287     del session
00288     del svc