00001
00002
00003
00004
00005
00006
00007
00008
00009 import os,sys,time
00010 import coral
00011 from RecoLuminosity.LumiDB import sessionManager,lumiTime,inputFilesetParser,csvSelectionParser,selectionParser,csvReporter,argparse,CommonUtil,revisionDML,lumiCalcAPI,lumiReport,RegexValidator,normDML
00012
00013 beamChoices=['PROTPHYS','IONPHYS','PAPHYS']
00014
00015 def parseInputFiles(inputfilename):
00016 '''
00017 output ({run:[cmsls,cmsls,...]},[[resultlines]])
00018 '''
00019 selectedrunlsInDB={}
00020 resultlines=[]
00021 p=inputFilesetParser.inputFilesetParser(inputfilename)
00022 runlsbyfile=p.runsandls()
00023 selectedProcessedRuns=p.selectedRunsWithresult()
00024 selectedNonProcessedRuns=p.selectedRunsWithoutresult()
00025 resultlines=p.resultlines()
00026 for runinfile in selectedNonProcessedRuns:
00027 selectedrunlsInDB[runinfile]=runlsbyfile[runinfile]
00028 return (selectedrunlsInDB,resultlines)
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 if __name__ == '__main__':
00039 parser = argparse.ArgumentParser(prog=os.path.basename(sys.argv[0]),description = "Lumi Calculation",formatter_class=argparse.ArgumentDefaultsHelpFormatter)
00040 allowedActions = ['overview', 'delivered', 'recorded', 'lumibyls','lumibylsXing']
00041 beamModeChoices = [ "stable"]
00042 amodetagChoices = [ "PROTPHYS","IONPHYS",'PAPHYS' ]
00043 xingAlgoChoices =[ "OCC1","OCC2","ET"]
00044
00045
00046
00047
00048
00049
00050
00051 parser.add_argument('action',choices=allowedActions,
00052 help='command actions')
00053 parser.add_argument('-c',dest='connect',action='store',
00054 required=False,
00055 help='connect string to lumiDB,optional',
00056 default='frontier://LumiCalc/CMS_LUMI_PROD')
00057 parser.add_argument('-P',dest='authpath',action='store',
00058 required=False,
00059 help='path to authentication file')
00060 parser.add_argument('-r',dest='runnumber',action='store',
00061 type=int,
00062 required=False,
00063 help='run number')
00064 parser.add_argument('-o',dest='outputfile',action='store',
00065 required=False,
00066 help='output to csv file' )
00067
00068
00069
00070
00071 parser.add_argument('-i',dest='inputfile',action='store',
00072 required=False,
00073 help='lumi range selection file')
00074
00075
00076
00077 parser.add_argument('--hltpath',dest='hltpath',action='store',
00078 default=None,required=False,
00079 help='specific hltpath or hltpath pattern to calculate the effectived luminosity')
00080
00081
00082
00083 parser.add_argument('--normtag',dest='normtag',action='store',
00084 required=False,
00085 help='version of lumi norm/correction')
00086 parser.add_argument('--datatag',dest='datatag',action='store',
00087 required=False,
00088 help='version of lumi/trg/hlt data')
00089
00090
00091
00092
00093 parser.add_argument('-f','--fill',dest='fillnum',action='store',
00094 default=None,required=False,
00095 help='fill number (optional) ')
00096 parser.add_argument('--amodetag',dest='amodetag',action='store',
00097 choices=amodetagChoices,
00098 required=False,
00099 help='specific accelerator mode choices [PROTOPHYS,IONPHYS,PAPHYS] (optional)')
00100 parser.add_argument('--beamenergy',dest='beamenergy',action='store',
00101 type=float,
00102 default=None,
00103 help='nominal beam energy in GeV')
00104 parser.add_argument('--beamfluctuation',dest='beamfluctuation',
00105 type=float,action='store',
00106 default=0.2,
00107 required=False,
00108 help='fluctuation in fraction allowed to nominal beam energy, default 0.2, to be used together with -beamenergy (optional)')
00109
00110 parser.add_argument('--begin',dest='begin',action='store',
00111 default=None,
00112 required=False,
00113 type=RegexValidator.RegexValidator("^\d\d/\d\d/\d\d \d\d:\d\d:\d\d$|^\d{6}$|^\d{4}$","wrong format"),
00114 help='min run start time (mm/dd/yy hh:mm:ss),min fill or min run'
00115 )
00116 parser.add_argument('--end',dest='end',action='store',
00117 default=None,
00118 required=False,
00119 type=RegexValidator.RegexValidator("^\d\d/\d\d/\d\d \d\d:\d\d:\d\d$|^\d{6}$|^\d{4}$","wrong format"),
00120 help='max run start time (mm/dd/yy hh:mm:ss),max fill or max run'
00121 )
00122 parser.add_argument('--minBiasXsec',dest='minbiasxsec',action='store',
00123 default=69300.0,
00124 type=float,
00125 required=False,
00126 help='minbias cross-section in ub'
00127 )
00128
00129
00130
00131 parser.add_argument('-b',dest='beammode',action='store',
00132 choices=beamModeChoices,
00133 required=False,
00134 help='beam mode choices [stable]')
00135
00136 parser.add_argument('--xingMinLum', dest = 'xingMinLum',
00137 type=float,
00138 default=1e-03,
00139 required=False,
00140 help='Minimum perbunch luminosity to print, default=1e-03/ub')
00141
00142 parser.add_argument('--xingAlgo', dest = 'xingAlgo',
00143 default='OCC1',
00144 required=False,
00145 help='algorithm name for per-bunch lumi ')
00146
00147
00148
00149
00150 parser.add_argument('-n',dest='scalefactor',action='store',
00151 type=float,
00152 default=1.0,
00153 required=False,
00154 help='user defined global scaling factor on displayed lumi values,optional')
00155
00156
00157
00158
00159 parser.add_argument('--siteconfpath',dest='siteconfpath',action='store',
00160 default=None,
00161 required=False,
00162 help='specific path to site-local-config.xml file, optional. If path undefined, fallback to cern proxy&server')
00163
00164
00165
00166 parser.add_argument('--without-correction',
00167 dest='withoutNorm',
00168 action='store_true',
00169 help='without any correction/calibration'
00170 )
00171 parser.add_argument('--without-checkforupdate',
00172 dest='withoutCheckforupdate',
00173 action='store_true',
00174 help='without check for update'
00175 )
00176
00177
00178
00179
00180
00181 parser.add_argument('--nowarning',
00182 dest='nowarning',
00183 action='store_true',
00184 help='suppress bad for lumi warnings'
00185 )
00186 parser.add_argument('--debug',
00187 dest='debug',
00188 action='store_true',
00189 help='debug'
00190 )
00191 options=parser.parse_args()
00192
00193 if not options.runnumber and not options.inputfile and not options.fillnum and not options.begin:
00194 raise RuntimeError('at least one run selection argument in [-r,-f,-i,--begin] is required')
00195 reqrunmin=None
00196 reqfillmin=None
00197 reqtimemin=None
00198 reqrunmax=None
00199 reqfillmax=None
00200 reqtimemax=None
00201 timeFilter=[None,None]
00202 pbeammode = None
00203 if options.beammode=='stable':
00204 pbeammode = 'STABLE BEAMS'
00205 iresults=[]
00206 reqTrg=False
00207 reqHlt=False
00208 if options.action=='overview' or options.action=='lumibyls' or options.action=='lumibylsXing':
00209 reqTrg=True
00210 if options.action=='recorded':
00211 reqTrg=True
00212 reqHlt=True
00213 if options.begin:
00214 (reqrunmin,reqfillmin,reqtimemin)=CommonUtil.parseTime(options.begin)
00215 if reqtimemin:
00216 lute=lumiTime.lumiTime()
00217 reqtimeminT=lute.StrToDatetime(reqtimemin,customfm='%m/%d/%y %H:%M:%S')
00218 timeFilter[0]=reqtimeminT
00219 if options.end:
00220 (reqrunmax,reqfillmax,reqtimemax)=CommonUtil.parseTime(options.end)
00221 if reqtimemax:
00222 lute=lumiTime.lumiTime()
00223 reqtimemaxT=lute.StrToDatetime(reqtimemax,customfm='%m/%d/%y %H:%M:%S')
00224 timeFilter[1]=reqtimemaxT
00225
00226
00227
00228
00229 workingversion='UNKNOWN'
00230 updateversion='NONE'
00231 thiscmmd=sys.argv[0]
00232 if not options.withoutCheckforupdate:
00233 from RecoLuminosity.LumiDB import checkforupdate
00234 cmsswWorkingBase=os.environ['CMSSW_BASE']
00235 if not cmsswWorkingBase:
00236 print 'Please check out RecoLuminosity/LumiDB from CVS,scram b,cmsenv'
00237 sys.exit(0)
00238 c=checkforupdate.checkforupdate()
00239 workingversion=c.runningVersion(cmsswWorkingBase,'lumiCalc2.py',isverbose=False)
00240 if workingversion:
00241 updateversionList=c.checkforupdate(workingversion,isverbose=False)
00242 if updateversionList:
00243 updateversion=updateversionList[-1][0]
00244
00245
00246
00247 if options.authpath:
00248 os.environ['CORAL_AUTH_PATH'] = options.authpath
00249
00250
00251
00252
00253
00254 if options.action=='recorded':
00255 if not options.hltpath:
00256 raise RuntimeError('argument --hltpath pathname is required for recorded action')
00257
00258 svc=sessionManager.sessionManager(options.connect,
00259 authpath=options.authpath,
00260 siteconfpath=options.siteconfpath,
00261 debugON=options.debug)
00262 session=svc.openSession(isReadOnly=True,cpp2sqltype=[('unsigned int','NUMBER(10)'),('unsigned long long','NUMBER(20)')])
00263
00264
00265
00266
00267 irunlsdict={}
00268 rruns=[]
00269 session.transaction().start(True)
00270 if options.runnumber:
00271 irunlsdict[options.runnumber]=None
00272 rruns=irunlsdict.keys()
00273 else:
00274 runlist=lumiCalcAPI.runList(session.nominalSchema(),options.fillnum,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=False,requirehlt=False)
00275 if options.inputfile:
00276 (irunlsdict,iresults)=parseInputFiles(options.inputfile)
00277 rruns=[val for val in runlist if val in irunlsdict.keys()]
00278 for selectedrun in irunlsdict.keys():
00279 if selectedrun not in rruns:
00280 del irunlsdict[selectedrun]
00281 else:
00282 for run in runlist:
00283 irunlsdict[run]=None
00284 rruns=irunlsdict.keys()
00285
00286
00287
00288 datatagname=options.datatag
00289 if not datatagname:
00290 (datatagid,datatagname)=revisionDML.currentDataTag(session.nominalSchema())
00291 dataidmap=revisionDML.dataIdsByTagId(session.nominalSchema(),datatagid,runlist=rruns,withcomment=False)
00292
00293 else:
00294 dataidmap=revisionDML.dataIdsByTagName(session.nominalSchema(),datatagname,runlist=rruns,withcomment=False)
00295
00296
00297
00298
00299 normname='NONE'
00300 normid=0
00301 normvalueDict={}
00302 if not options.withoutNorm:
00303 normname=options.normtag
00304 if not normname:
00305 normmap=normDML.normIdByType(session.nominalSchema(),lumitype='HF',defaultonly=True)
00306 if len(normmap):
00307 normname=normmap.keys()[0]
00308 normid=normmap[normname]
00309 else:
00310 normid=normDML.normIdByName(session.nominalSchema(),normname)
00311 if not normid:
00312 raise RuntimeError('[ERROR] cannot resolve norm/correction')
00313 sys.exit(-1)
00314 normvalueDict=normDML.normValueById(session.nominalSchema(),normid)
00315 session.transaction().commit()
00316 lumiReport.toScreenHeader(thiscmmd,datatagname,normname,workingversion,updateversion,'HF')
00317 if not dataidmap:
00318 print '[INFO] No qualified data found, do nothing'
00319 sys.exit(0)
00320
00321
00322
00323
00324 session.transaction().start(True)
00325 GrunsummaryData=lumiCalcAPI.runsummaryMap(session.nominalSchema(),irunlsdict)
00326 if options.action == 'delivered':
00327 result=lumiCalcAPI.deliveredLumiForIds(session.nominalSchema(),irunlsdict,dataidmap,runsummaryMap=GrunsummaryData,beamstatusfilter=pbeammode,timeFilter=timeFilter,normmap=normvalueDict,lumitype='HF')
00328 lumiReport.toScreenTotDelivered(result,iresults,options.scalefactor,irunlsdict=irunlsdict,noWarning=options.nowarning,toFile=options.outputfile)
00329 if options.action == 'overview':
00330 result=lumiCalcAPI.lumiForIds(session.nominalSchema(),irunlsdict,dataidmap,runsummaryMap=GrunsummaryData,beamstatusfilter=pbeammode,timeFilter=timeFilter,normmap=normvalueDict,lumitype='HF')
00331 lumiReport.toScreenOverview(result,iresults,options.scalefactor,irunlsdict=irunlsdict,noWarning=options.nowarning,toFile=options.outputfile)
00332 if options.action == 'lumibyls':
00333 if not options.hltpath:
00334 result=lumiCalcAPI.lumiForIds(session.nominalSchema(),irunlsdict,dataidmap,runsummaryMap=GrunsummaryData,beamstatusfilter=pbeammode,timeFilter=timeFilter,normmap=normvalueDict,lumitype='HF',minbiasXsec=options.minbiasxsec)
00335 lumiReport.toScreenLumiByLS(result,iresults,options.scalefactor,irunlsdict=irunlsdict,noWarning=options.nowarning,toFile=options.outputfile)
00336 else:
00337 hltname=options.hltpath
00338 hltpat=None
00339 if hltname=='*' or hltname=='all':
00340 hltname=None
00341 elif 1 in [c in hltname for c in '*?[]']:
00342 hltpat=hltname
00343 hltname=None
00344 result=lumiCalcAPI.effectiveLumiForIds(session.nominalSchema(),irunlsdict,dataidmap,runsummaryMap=GrunsummaryData,beamstatusfilter=pbeammode,timeFilter=timeFilter,normmap=normvalueDict,hltpathname=hltname,hltpathpattern=hltpat,withBXInfo=False,bxAlgo=None,xingMinLum=options.xingMinLum,withBeamIntensity=False,lumitype='HF')
00345 lumiReport.toScreenLSEffective(result,iresults,options.scalefactor,irunlsdict=irunlsdict,noWarning=options.nowarning,toFile=options.outputfile)
00346 if options.action == 'recorded':
00347 hltname=options.hltpath
00348 hltpat=None
00349 if hltname is not None:
00350 if hltname=='*' or hltname=='all':
00351 hltname=None
00352 elif 1 in [c in hltname for c in '*?[]']:
00353 hltpat=hltname
00354 hltname=None
00355 result=lumiCalcAPI.effectiveLumiForIds(session.nominalSchema(),irunlsdict,dataidmap,runsummaryMap=GrunsummaryData,beamstatusfilter=pbeammode,timeFilter=timeFilter,normmap=normvalueDict,hltpathname=hltname,hltpathpattern=hltpat,withBXInfo=False,bxAlgo=None,xingMinLum=options.xingMinLum,withBeamIntensity=False,lumitype='HF')
00356 lumiReport.toScreenTotEffective(result,iresults,options.scalefactor,irunlsdict=irunlsdict,noWarning=options.nowarning,toFile=options.outputfile)
00357 if options.action == 'lumibylsXing':
00358 result=lumiCalcAPI.lumiForIds(session.nominalSchema(),irunlsdict,dataidmap,runsummaryMap=GrunsummaryData,beamstatusfilter=pbeammode,timeFilter=timeFilter,normmap=normvalueDict,withBXInfo=True,bxAlgo=options.xingAlgo,xingMinLum=options.xingMinLum,withBeamIntensity=False,lumitype='HF')
00359 outfile=options.outputfile
00360 if not outfile:
00361 print '[WARNING] no output file given. lumibylsXing writes per-bunch lumi only to default file lumibylsXing.csv'
00362 outfile='lumibylsXing.csv'
00363 lumiReport.toCSVLumiByLSXing(result,options.scalefactor,outfile,irunlsdict=irunlsdict,noWarning=options.nowarning)
00364 session.transaction().commit()
00365 del session
00366 del svc