CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/RecoLuminosity/LumiDB/scripts/lumiContext.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 VERSION='2.00'
00003 import os,sys,time
00004 from RecoLuminosity.LumiDB import sessionManager,lumiTime,inputFilesetParser,csvSelectionParser,csvReporter,argparse,CommonUtil,lumiCalcAPI,lumiReport,lumiTime
00005 
00006 def parseInputFiles(inputfilename,dbrunlist,optaction):
00007     '''
00008     output ({run:[cmsls,cmsls,...]},[[resultlines]])
00009     '''
00010     selectedrunlsInDB={}
00011     resultlines=[]
00012     p=inputFilesetParser.inputFilesetParser(inputfilename)
00013     runlsbyfile=p.runsandls()
00014     selectedProcessedRuns=p.selectedRunsWithresult()
00015     selectedNonProcessedRuns=p.selectedRunsWithoutresult()
00016     resultlines=p.resultlines()
00017     for runinfile in selectedNonProcessedRuns:
00018         if runinfile not in dbrunlist:
00019             continue
00020         selectedrunlsInDB[runinfile]=runlsbyfile[runinfile]
00021     return (selectedrunlsInDB,resultlines)
00022 
00023 
00024 ##############################
00025 ## ######################## ##
00026 ## ## ################## ## ##
00027 ## ## ## Main Program ## ## ##
00028 ## ## ################## ## ##
00029 ## ######################## ##
00030 ##############################
00031 
00032 if __name__ == '__main__':
00033     parser = argparse.ArgumentParser(prog=os.path.basename(sys.argv[0]),description = "Additional information needed in the lumi calculation",formatter_class=argparse.ArgumentDefaultsHelpFormatter)
00034     allowedActions = ['hltbyls','hltconf','trgconf','trgbyls', 'beambyls','runsummary']
00035     amodetagChoices = [ "PROTPHYS","IONPHYS" ]
00036     beamModeChoices = ["stable"]
00037     #
00038     # parse arguments
00039     #  
00040     #
00041     # basic arguments
00042     #
00043     parser.add_argument('action',choices=allowedActions,
00044                         help='command actions')
00045     parser.add_argument('-c',dest='connect',action='store',
00046                         required=False,
00047                         help='connect string to lumiDB,optional',
00048                         default='frontier://LumiCalc/CMS_LUMI_PROD')
00049     parser.add_argument('-P',dest='authpath',action='store',
00050                         required=False,
00051                         help='path to authentication file (optional)')
00052     parser.add_argument('-r',dest='runnumber',action='store',
00053                         type=int,
00054                         required=False,
00055                         help='run number (optional)')
00056     parser.add_argument('-o',dest='outputfile',action='store',
00057                         required=False,
00058                         help='output to csv file (optional)')
00059     #
00060     #optional arg to select exact run and ls
00061     #
00062     parser.add_argument('-i',dest='inputfile',action='store',
00063                         required=False,
00064                         help='run/ls selection file (optional)')
00065     parser.add_argument('--name',dest='name',action='store',
00066                        help='hltpath/l1bit name/pattern'
00067                        )
00068     #
00069     #optional args to filter *runs*, they do not select on LS level.
00070     #
00071     parser.add_argument('-b',dest='beammode',action='store',
00072                         choices=beamModeChoices,
00073                         required=False,
00074                         help='beam mode choices [stable] (optional)')
00075     parser.add_argument('--fill',dest='fillnum',action='store',
00076                         default=None,required=False,
00077                         help='fill number (optional) ')
00078     parser.add_argument('--amodetag',dest='amodetag',action='store',
00079                         choices=amodetagChoices,
00080                         required=False,
00081                         help='specific accelerator mode choices [PROTOPHYS,IONPHYS] (optional)')
00082     parser.add_argument('--beamenergy',dest='beamenergy',action='store',
00083                         type=float,
00084                         default=None,
00085                         help='nominal beam energy in GeV')
00086     parser.add_argument('--beamfluctuation',dest='beamfluctuation',
00087                         type=float,action='store',
00088                         default=0.2,
00089                         required=False,
00090                         help='fluctuation in fraction allowed to nominal beam energy, default 0.2, to be used together with -beamenergy  (optional)')
00091     parser.add_argument('--minintensity',dest='minintensity',
00092                         type=float,action='store',
00093                         default=0.1,
00094                         required=False,
00095                         help='filter on beam intensity , effective with --with-beamintensity (optional)')
00096     parser.add_argument('--begin',dest='begin',action='store',
00097                         default=None,
00098                         required=False,
00099                         help='min run start time, mm/dd/yy hh:mm:ss (optional)')
00100     parser.add_argument('--end',dest='end',action='store',
00101                         default=None,required=False,
00102                         help='max run start time, mm/dd/yy hh:mm:ss (optional)')
00103     
00104 
00105     #
00106     #command configuration 
00107     #
00108     parser.add_argument('--siteconfpath',dest='siteconfpath',action='store',
00109                         default=None,
00110                         required=False,
00111                         help='specific path to site-local-config.xml file, optional. If path undefined, fallback to cern proxy&server')
00112     #
00113     #switches
00114     #
00115     parser.add_argument('--with-beamintensity',dest='withbeamintensity',action='store_true',
00116                         help='dump beam intensity' )
00117     parser.add_argument('--verbose',dest='verbose',action='store_true',
00118                         help='verbose mode for printing' )
00119     parser.add_argument('--debug',dest='debug',action='store_true',
00120                         help='debug')
00121     
00122     options=parser.parse_args()
00123     if options.authpath:
00124         os.environ['CORAL_AUTH_PATH'] = options.authpath
00125         
00126     pbeammode = None
00127     sname=options.name
00128     isdetail=False
00129     spattern=None
00130     if sname is not None:
00131         isdetail=True
00132         if sname=='*' or sname=='all':
00133             sname=None
00134         elif 1 in [c in sname for c in '*?[]']: #is a fnmatch pattern
00135             spattern=sname
00136             sname=None
00137     if  options.action == 'beambyls' and options.withbeamintensity and not options.outputfile:
00138         print '[warning] --with-beamintensity must write data to a file, none specified using default "beamintensity.csv"'
00139         options.outputfile='beamintensity.csv'
00140     if options.beammode=='stable':
00141         pbeammode    = 'STABLE BEAMS'
00142     if options.verbose:
00143         print 'General configuration'
00144         print '\tconnect: ',options.connect
00145         print '\tauthpath: ',options.authpath
00146         print '\tsiteconfpath: ',options.siteconfpath
00147         print '\toutputfile: ',options.outputfile
00148         print '\tname: ',sname
00149         print '\tnamepattern: ',spattern
00150         if options.runnumber: # if runnumber specified, do not go through other run selection criteria
00151             print '\tselect specific run== ',options.runnumber
00152         else:
00153             print '\trun selections == '
00154             print '\tinput selection file: ',options.inputfile
00155             print '\tbeam mode: ',options.beammode 
00156             print '\tfill: ',options.fillnum
00157             print '\tamodetag: ',options.amodetag
00158             print '\tbegin: ',options.begin
00159             print '\tend: ',options.end
00160             print '\tbeamenergy: ',options.beamenergy 
00161             if options.beamenergy:
00162                 print '\tbeam energy: ',str(options.beamenergy)+'+/-'+str(options.beamfluctuation*options.beamenergy)+'(GeV)'
00163     svc=sessionManager.sessionManager(options.connect,authpath=options.authpath,debugON=options.debug)
00164     session=svc.openSession(isReadOnly=True,cpp2sqltype=[('unsigned int','NUMBER(10)'),('unsigned long long','NUMBER(20)')])
00165     
00166     irunlsdict={}
00167     iresults=[]
00168     if options.runnumber: # if runnumber specified, do not go through other run selection criteria
00169         irunlsdict[options.runnumber]=None
00170     else:
00171         reqTrg=False
00172         reqHlt=False
00173         if options.action=='trgbyls':
00174             reqTrg=True
00175         if options.action=='hltbyls':
00176             reqHlt=True
00177         session.transaction().start(True)
00178         schema=session.nominalSchema()
00179         runlist=lumiCalcAPI.runList(schema,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)
00180         session.transaction().commit()
00181         if options.inputfile:
00182             (irunlsdict,iresults)=parseInputFiles(options.inputfile,runlist,options.action)
00183         else:
00184             for run in runlist:
00185                 irunlsdict[run]=None
00186     if options.verbose:  
00187         print 'Selected run:ls'
00188         for run in sorted(irunlsdict):
00189             if irunlsdict[run] is not None:
00190                 print '\t%d : %s'%(run,','.join([str(ls) for ls in irunlsdict[run]]))
00191             else:
00192                 print '\t%d : all'%run
00193     if options.action == 'trgbyls':
00194         session.transaction().start(True)
00195         result=lumiCalcAPI.trgForRange(session.nominalSchema(),irunlsdict,trgbitname=sname,trgbitnamepattern=spattern,withL1Count=True,withPrescale=True)
00196         session.transaction().commit()
00197         if not options.outputfile:
00198             lumiReport.toScreenLSTrg(result,iresults=iresults,isverbose=isdetail)
00199         else:
00200             lumiReport.toCSVLSTrg(result,options.outputfile,iresults=iresults,isverbose=isdetail)
00201     if options.action == 'hltbyls':
00202         withL1Pass=False
00203         withHLTAccept=False
00204         if options.verbose:
00205             withL1Pass=True
00206             withHLTAccept=True
00207         session.transaction().start(True)
00208         result=lumiCalcAPI.hltForRange(session.nominalSchema(),irunlsdict,hltpathname=sname,hltpathpattern=spattern,withL1Pass=withL1Pass,withHLTAccept=withHLTAccept)
00209         session.transaction().commit()
00210         if not options.outputfile:
00211             lumiReport.toScreenLSHlt(result,iresults=iresults,isverbose=options.verbose)
00212         else:
00213             lumiReport.toCSVLSHlt(result,options.outputfile,iresults,options.verbose)
00214     if options.action == 'trgconf':
00215         session.transaction().start(True)
00216         result=lumiCalcAPI.trgbitsForRange(session.nominalSchema(),irunlsdict,datatag=None)        
00217         session.transaction().commit()
00218         if not options.outputfile:
00219             lumiReport.toScreenConfTrg(result,iresults,options.verbose)
00220         else:
00221             lumiReport.toCSVConfTrg(result,options.outputfile,iresults,options.verbose)
00222     if options.action == 'hltconf':
00223         session.transaction().start(True)
00224         result=lumiCalcAPI.hltpathsForRange(session.nominalSchema(),irunlsdict,hltpathname=sname,hltpathpattern=spattern)
00225         session.transaction().commit()
00226         if not options.outputfile:
00227             lumiReport.toScreenConfHlt(result,iresults,options.verbose)
00228         else:
00229             lumiReport.toCSVConfHlt(result,options.outputfile,iresults,options.verbose)
00230     if options.action == 'beambyls':
00231         session.transaction().start(True)
00232         dumpbeamintensity=False
00233         if options.outputfile and options.verbose:
00234             dumpbeamintensity=True
00235         result=lumiCalcAPI.beamForRange(session.nominalSchema(),irunlsdict,withBeamIntensity=options.withbeamintensity,minIntensity=options.minintensity)
00236         session.transaction().commit()
00237         if not options.outputfile:
00238             lumiReport.toScreenLSBeam(result,iresults=iresults,dumpIntensity=False)
00239         else:
00240             lumiReport.toCSVLSBeam(result,options.outputfile,resultlines=iresults,dumpIntensity=options.withbeamintensity,isverbose=options.verbose)
00241     if options.action == 'runsummary':
00242         session.transaction().start(True)
00243         result=lumiCalcAPI.runsummary(session.nominalSchema(),irunlsdict)
00244         session.transaction().commit()
00245         c=lumiTime.lumiTime()
00246         for r in result:
00247             run=r[0]
00248             fill='n/a'
00249             if r[5]:
00250                 fill=str(r[5])
00251             starttime=c.StrToDatetime(r[7])
00252             starttime=starttime.strftime('%m/%d/%y %H:%M:%S')
00253             stoptime=c.StrToDatetime(r[8])
00254             stoptime=stoptime.strftime('%m/%d/%y %H:%M:%S')
00255             l1key=r[1]
00256             hltkey=r[4]
00257             amodetag=r[2]
00258             egev='n/a'
00259             if r[3]:
00260                 egev=str(r[3])
00261             sequence=r[6]
00262             print 'Run ',str(run),' Fill ',fill,' Amodetag ',amodetag,' egev ',egev
00263             print '\tStart '+starttime,'                  ',' Stop ',stoptime
00264             print '\tL1key ',l1key,' HLTkey ',hltkey
00265     del session
00266     del svc