00001 import os,coral,datetime,fnmatch,time
00002 from RecoLuminosity.LumiDB import nameDealer,revisionDML,dataDML,lumiTime,CommonUtil,selectionParser,hltTrgSeedMapper,lumiCorrections,lumiParameters
00003
00004
00005
00006
00007
00008 def _getnorm(schema,norm):
00009 if isinstance(norm,int) or isinstance(norm,float) or CommonUtil.is_floatstr(norm) or CommonUtil.is_intstr(norm):
00010 return float(norm)
00011 if not isinstance(norm,str):
00012 raise ValueError('wrong parameter type')
00013 normdataid=dataDML.guessnormIdByName(schema,norm)
00014 normresult=dataDML.luminormById(schema,normdataid)
00015 return normresult[2]
00016 def _decidenormFromContext(schema,amodetag,egev):
00017 normdataid=dataDML.guessnormIdByContext(schema,amodetag,egev)
00018 normresult=dataDML.luminormById(schema,normdataid)
00019 return normresult[2]
00020 def _decidenormForRun(schema,run):
00021 rundata=dataDML.runsummary(schema,run)
00022 amodetagForRun=rundata[1]
00023 egevForRun=rundata[2]
00024 normdataid=dataDML.guessnormIdByContext(schema,amodetagForRun,egevForRun)
00025 normresult=dataDML.luminormById(schema,normdataid)
00026 return normresult[2]
00027
00028 def runsummary(schema,irunlsdict):
00029 '''
00030 output [[run,l1key,amodetag,egev,hltkey,fillnum,sequence,starttime,stoptime]]
00031 '''
00032 result=[]
00033 for run in sorted(irunlsdict):
00034 runinfo=dataDML.runsummary(schema,run)
00035 runinfo.insert(0,run)
00036 result.append(runinfo)
00037 return result
00038 def fillInRange(schema,fillmin=1000,fillmax=9999,amodetag='PROTPHYS',startT=None,stopT=None):
00039 '''
00040 output [fill]
00041 '''
00042 fills=dataDML.fillInRange(schema,fillmin,fillmax,amodetag,startT,stopT)
00043 return fills
00044 def fillrunMap(schema,fillnum=None,runmin=None,runmax=None,startT=None,stopT=None,l1keyPattern=None,hltkeyPattern=None,amodetag=None):
00045 '''
00046 output: {fill:[runnum,...]}
00047 '''
00048 return dataDML.fillrunMap(schema,fillnum=fillnum,runmin=runmin,runmax=runmax,startT=startT,stopT=stopT,l1keyPattern=l1keyPattern,hltkeyPattern=hltkeyPattern,amodetag=amodetag)
00049
00050 def runList(schema,fillnum=None,runmin=None,runmax=None,startT=None,stopT=None,l1keyPattern=None,hltkeyPattern=None,amodetag=None,nominalEnergy=None,energyFlut=0.2,requiretrg=True,requirehlt=True,lumitype='HF'):
00051 '''
00052 output: [runnumber,...]
00053 '''
00054 return dataDML.runList(schema,fillnum,runmin,runmax,startT,stopT,l1keyPattern,hltkeyPattern,amodetag,nominalEnergy,energyFlut,requiretrg,requirehlt,lumitype)
00055
00056 def hltpathsForRange(schema,runlist,hltpathname=None,hltpathpattern=None):
00057 '''
00058 input: runlist [run], (required)
00059 datatag: data version (optional)
00060 output : {runnumber,[(hltpath,l1seedexpr,l1bitname)...]}
00061 '''
00062 result={}
00063 for run in runlist:
00064 hlttrgmap=dataDML.hlttrgMappingByrun(schema,run,hltpathname=hltpathname,hltpathpattern=hltpathpattern)
00065 result[run]=[]
00066 for hltpath in sorted(hlttrgmap):
00067 l1seedexpr=hlttrgmap[hltpath]
00068 l1bitname=hltTrgSeedMapper.findUniqueSeed(hltpath,l1seedexpr)
00069 result[run].append((hltpath,l1seedexpr,l1bitname))
00070 return result
00071 def trgbitsForRange(schema,runlist,datatag=None):
00072 '''
00073 input: runlist [run],(required)
00074 datatag: data version (optional)
00075 output: {runnumber:[datasource,normbit,[bitname,..]]}
00076 '''
00077 result={}
00078 for run in runlist:
00079 trgdataid=dataDML.guessTrgDataIdByRun(schema,run)
00080 if not trgdataid :
00081 result[run]=None
00082 continue
00083 if not result.has_key(run):
00084 result[run]=[]
00085 trgconf=dataDML.trgRunById(schema,trgdataid)
00086 datasource=trgconf[1]
00087 bitzeroname=trgconf[2]
00088 bitnamedict=trgconf[3]
00089 bitnames=[x[1] for x in bitnamedict if x[1]!='False']
00090 result[run].extend([datasource,bitzeroname,bitnames])
00091 return result
00092
00093 def beamForRange(schema,inputRange,withBeamIntensity=False,minIntensity=0.1,tableName=None,branchName=None):
00094 '''
00095 input:
00096 inputRange: {run:[cmsls]} (required)
00097 output : {runnumber:[(lumicmslnum,cmslsnum,beamenergy,beamstatus,[(ibx,b1,b2)])...](4)}
00098 '''
00099 if tableName is None:
00100 tableName=nameDealer.lumidataTableName()
00101 if branchName is None:
00102 branchName='DATA'
00103 result={}
00104 for run in inputRange.keys():
00105 lslist=inputRange[run]
00106 if lslist is not None and len(lslist)==0:
00107 result[run]=[]
00108 continue
00109 lumidataid=dataDML.guessLumiDataIdByRun(schema,run,tableName)
00110 if lumidataid is None:
00111 result[run]=None
00112 continue
00113 lumidata=dataDML.beamInfoById(schema,lumidataid,withBeamIntensity=withBeamIntensity,minIntensity=minIntensity)
00114
00115 result[run]=[]
00116 perrundata=lumidata[1]
00117 if not perrundata:
00118 result[run]=[]
00119 continue
00120 for perlsdata in perrundata:
00121 lumilsnum=perlsdata[0]
00122 cmslsnum=perlsdata[1]
00123 if lslist is not None and cmslsnum not in lslist:
00124 continue
00125 beamstatus=perlsdata[2]
00126 beamenergy=perlsdata[3]
00127 beamintInfolist=[]
00128 if withBeamIntensity:
00129 beamintInfolist=perlsdata[4]
00130 result[run].append((lumilsnum,cmslsnum,beamstatus,beamenergy,beamintInfolist))
00131 return result
00132
00133 def hltForRange(schema,inputRange,hltpathname=None,hltpathpattern=None,withL1Pass=False,withHLTAccept=False,tableName=None,branchName=None):
00134 '''
00135 input:
00136 inputRange: {run:[cmsls]} (required)
00137 hltpathname: exact match hltpathname (optional)
00138 hltpathpattern: regex match hltpathpattern (optional)
00139 branchName : data version
00140 output: {runnumber:[(cmslsnum,[(hltpath,hltprescale,l1pass,hltaccept),...]),(cmslsnum,[])})}
00141 '''
00142
00143
00144
00145
00146 result={}
00147 for run in inputRange.keys():
00148 lslist=inputRange[run]
00149 if lslist is not None and len(lslist)==0:
00150 result[run]=[]
00151 continue
00152 hltdataid=dataDML.guessHltDataIdByRun(schema,run)
00153 if hltdataid is None:
00154 result[run]=None
00155 continue
00156 hltdata=dataDML.hltLSById(schema,hltdataid,hltpathname=hltpathname,hltpathpattern=hltpathpattern,withL1Pass=withL1Pass,withHLTAccept=withHLTAccept)
00157
00158 result[run]=[]
00159 if hltdata and hltdata[1]:
00160 for cmslsnum in sorted(hltdata[1]):
00161 if lslist is not None and cmslsnum not in lslist:
00162 continue
00163 lsdata=[]
00164 for perpathdata in hltdata[1][cmslsnum]:
00165 pathname=perpathdata[0]
00166 prescale=perpathdata[1]
00167 l1pass=None
00168 hltaccept=None
00169 if withL1Pass:
00170 l1pass=perpathdata[2]
00171 if withHLTAccept:
00172 hltaccept=perpathdata[3]
00173 lsdata.append((pathname,prescale,l1pass,hltaccept))
00174 result[run].append((cmslsnum,lsdata))
00175 return result
00176
00177 def trgForRange(schema,inputRange,trgbitname=None,trgbitnamepattern=None,withL1Count=False,withPrescale=False,tableName=None,branchName=None):
00178 '''
00179 input :
00180 inputRange {run:[cmsls]} (required)
00181 trgbitname exact match trgbitname (optional)
00182 trgbitnamepattern match trgbitname (optional)
00183 tableName : trgdata table name
00184 branchName : data version
00185 output
00186 result {run:[cmslsnum,deadfrac,deadtimecount,bitzero_count,bitzero_prescale,[(bitname,prescale,counts)]]}
00187 '''
00188
00189
00190
00191
00192 result={}
00193 withprescaleblob=True
00194 withtrgblob=True
00195 for run in inputRange.keys():
00196 lslist=inputRange[run]
00197 if lslist is not None and len(lslist)==0:
00198 result[run]=[]
00199 continue
00200 trgdataid=dataDML.guessTrgDataIdByRunInBranch(schema,run)
00201 if trgdataid is None:
00202 result[run]=None
00203 continue
00204 trgdata=dataDML.trgLSById(schema,trgdataid,trgbitname=trgbitname,trgbitnamepattern=trgbitnamepattern,withL1Count=withL1Count,withPrescale=withPrescale)
00205
00206 result[run]=[]
00207 if trgdata and trgdata[1]:
00208 lsdict={}
00209 for cmslsnum in sorted(trgdata[1]):
00210 if lslist is not None and cmslsnum not in lslist:
00211 continue
00212 lsdata=[]
00213 deadtimecount=trgdata[1][cmslsnum][0]
00214 bitzerocount=trgdata[1][cmslsnum][1]
00215 bitzeroprescale=trgdata[1][cmslsnum][2]
00216
00217
00218
00219
00220 deadfrac=trgdata[1][cmslsnum][3]
00221 if deadfrac<0 or deadfrac>1.0:
00222 deadfrac=1.0
00223 allbitsinfo=trgdata[1][cmslsnum][4]
00224 lsdata.append(cmslsnum)
00225 lsdata.append(deadfrac)
00226 lsdata.append(deadtimecount)
00227 lsdata.append(bitzerocount)
00228 lsdata.append(bitzeroprescale)
00229 lsdata.append(allbitsinfo)
00230 result[run].append(lsdata)
00231 return result
00232
00233 def instLumiForRange(schema,inputRange,beamstatusfilter=None,withBXInfo=False,bxAlgo=None,xingMinLum=0.0,withBeamIntensity=False,lumitype='HF',branchName=None):
00234 '''
00235 DIRECTLY FROM ROOT FIME NO CORRECTION AT ALL
00236 lumi raw data. beofore normalization and time integral
00237 input:
00238 inputRange {run:[cmsls]} (required)
00239 beamstatus: LS filter on beamstatus (optional)
00240 withBXInfo: get per bunch info (optional)
00241 bxAlgo: algoname for bx values (optional) ['OCC1','OCC2','ET']
00242 xingMinLum: cut on bx lumi value (optional)
00243 withBeamIntensity: get beam intensity info (optional)
00244 branchName: data version
00245 output:
00246 result {run:[lumilsnum(0),cmslsnum(1),timestamp(2),beamstatus(3),beamenergy(4),instlumi(5),instlumierr(6),startorbit(7),numorbit(8),(bxidx,bxvalues,bxerrs)(9),(bxidx,b1intensities,b2intensities)(10),fillnum(11)]}}
00247 lumi unit: HZ/ub
00248 '''
00249 if lumitype not in ['HF','PIXEL']:
00250 raise ValueError('unknown lumitype '+lumitype)
00251 lumitableName=''
00252 lumilstableName=''
00253 if lumitype=='HF':
00254 lumitableName=nameDealer.lumidataTableName()
00255 lumilstableName=nameDealer.lumisummaryv2TableName()
00256 else:
00257 lumitableName=nameDealer.pixellumidataTableName()
00258 lumilstableName=nameDealer.pixellumisummaryv2TableName()
00259
00260 result={}
00261 for run in inputRange.keys():
00262 lslist=inputRange[run]
00263 if lslist is not None and len(lslist)==0:
00264 result[run]=[]
00265 continue
00266 runsummary=dataDML.runsummary(schema,run)
00267 if len(runsummary)==0:
00268 result[run]=None
00269 continue
00270 fillnum=runsummary[4]
00271 runstarttimeStr=runsummary[6]
00272 lumidataid=dataDML.guessLumiDataIdByRun(schema,run,lumitableName)
00273 if lumidataid is None:
00274 result[run]=None
00275 continue
00276 (lumirunnum,perlsresult)=dataDML.lumiLSById(schema,lumidataid,beamstatusfilter,withBXInfo=withBXInfo,bxAlgo=bxAlgo,withBeamIntensity=withBeamIntensity,tableName=lumilstableName)
00277 lsresult=[]
00278 c=lumiTime.lumiTime()
00279 for lumilsnum in perlsresult.keys():
00280 perlsdata=perlsresult[lumilsnum]
00281 cmslsnum=perlsdata[0]
00282 if lslist is not None and lumilsnum not in lslist:
00283 cmslsnum=0
00284 numorbit=perlsdata[6]
00285 startorbit=perlsdata[7]
00286 orbittime=c.OrbitToTime(runstarttimeStr,startorbit,0)
00287 instlumi=perlsdata[1]
00288 instlumierr=perlsdata[2]
00289 beamstatus=perlsdata[4]
00290 beamenergy=perlsdata[5]
00291 bxidxlist=[]
00292 bxvaluelist=[]
00293 bxerrorlist=[]
00294 bxdata=None
00295 beamdata=None
00296 if withBXInfo:
00297 bxinfo=perlsdata[8]
00298 bxvalueArray=None
00299 bxerrArray=None
00300 if bxinfo:
00301 bxvalueArray=bxinfo[0]
00302 bxerrArray=bxinfo[1]
00303 for idx,bxval in enumerate(bxvalueArray):
00304 if bxval>xingMinLum:
00305 bxidxlist.append(idx)
00306 bxvaluelist.append(bxval)
00307 bxerrorlist.append(bxerrArray[idx])
00308 del bxvalueArray[:]
00309 del bxerrArray[:]
00310 bxdata=(bxidxlist,bxvaluelist,bxerrorlist)
00311 if withBeamIntensity:
00312 beaminfo=perlsdata[9]
00313 bxindexlist=[]
00314 b1intensitylist=[]
00315 b2intensitylist=[]
00316 if beaminfo[0] and beaminfo[1] and beaminfo[2]:
00317 bxindexarray=beaminfo[0]
00318 beam1intensityarray=beaminfo[1]
00319 beam2intensityarray=beaminfo[2]
00320 bxindexlist=bxindexarray.tolist()
00321 b1intensitylist=beam1intensityarray.tolist()
00322 b2intensitylist=beam2intensityarray.tolist()
00323 del bxindexarray[:]
00324 del beam1intensityarray[:]
00325 del beam2intensityarray[:]
00326 beamdata=(bxindexlist,b1intensitylist,b2intensitylist)
00327 lsresult.append([lumilsnum,cmslsnum,orbittime,beamstatus,beamenergy,instlumi,instlumierr,startorbit,numorbit,bxdata,beamdata,fillnum])
00328 del perlsdata[:]
00329 result[run]=lsresult
00330 return result
00331
00332 def instCalibratedLumiForRange(schema,inputRange,beamstatus=None,amodetag=None,egev=None,withBXInfo=False,bxAlgo=None,xingMinLum=0.0,withBeamIntensity=False,norm=None,finecorrections=None,driftcorrections=None,usecorrectionv2=False,lumitype='HF',branchName=None):
00333 '''
00334 Inst luminosity after calibration, not time integrated
00335 input:
00336 inputRange {run:[cmsls]} (required)
00337 amodetag : accelerator mode for all the runs (optional) ['PROTPHYS','IONPHYS']
00338 beamstatus: LS filter on beamstatus (optional)
00339 amodetag: amodetag for picking norm(optional)
00340 egev: beamenergy for picking norm(optional)
00341 withBXInfo: get per bunch info (optional)
00342 bxAlgo: algoname for bx values (optional) ['OCC1','OCC2','ET']
00343 xingMinLum: cut on bx lumi value (optional)
00344 withBeamIntensity: get beam intensity info (optional)
00345 norm: if norm is a float, use it directly; if it is a string, consider it norm factor name to use (optional)
00346 lumitype : HF or PIXEL
00347 branchName: data version
00348 finecorrections: const and non-linear corrections
00349 driftcorrections: driftcorrections
00350 output:
00351 result {run:[lumilsnum(0),cmslsnum(1),timestamp(2),beamstatus(3),beamenergy(4),calibratedlumi(5),calibratedlumierr(6),startorbit(7),numorbit(8),(bxidx,bxvalues,bxerrs)(9),(bxidx,b1intensities,b2intensities)(10),fillnum(11)]}}
00352 lumi unit: HZ/ub
00353 '''
00354 result = {}
00355 normval=None
00356 perbunchnormval=None
00357 if norm:
00358 normval=_getnorm(schema,norm)
00359 perbunchnormval=float(normval)/float(1000)
00360 elif amodetag and egev:
00361 normval=_decidenormFromContex(schema,amodetag,egev)
00362 perbunchnormval=float(normval)/float(1000)
00363 instresult=instLumiForRange(schema,inputRange,beamstatusfilter=beamstatus,withBXInfo=withBXInfo,bxAlgo=bxAlgo,xingMinLum=xingMinLum,withBeamIntensity=withBeamIntensity,lumitype=lumitype,branchName=branchName)
00364 for run,perrundata in instresult.items():
00365 if perrundata is None:
00366 result[run]=None
00367 continue
00368 result[run]=[]
00369 if not normval:
00370 normval=_decidenormForRun(schema,run)
00371 perbunchnormval=float(normval)/float(1000)
00372 if not normval:
00373 normval=6370
00374 perbunchnormval=6.37
00375 print '[Warning] using default normalization '+str(normval)
00376 for perlsdata in perrundata:
00377 lumilsnum=perlsdata[0]
00378 cmslsnum=perlsdata[1]
00379 timestamp=perlsdata[2]
00380 bs=perlsdata[3]
00381 beamenergy=perlsdata[4]
00382 fillnum=perlsdata[11]
00383 avglumi=perlsdata[5]*normval
00384 calibratedlumi=avglumi
00385 bxdata=perlsdata[9]
00386 if lumitype=='HF' and finecorrections and finecorrections[run]:
00387 if usecorrectionv2:
00388 if driftcorrections and driftcorrections[run]:
00389 calibratedlumi=lumiCorrections.applyfinecorrectionV2(avglumi,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],driftcorrections[run])
00390 else:
00391 calibratedlumi=lumiCorrections.applyfinecorrectionV2(avglumi,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],1.0)
00392 else:
00393 calibratedlumi=lumiCorrections.applyfinecorrection(avglumi,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2])
00394 if lumitype=='PIXEL' and finecorrections is not None:
00395 calibratedlumi=finecorrections[run]*avglumi
00396 calibratedlumierr=perlsdata[6]*normval
00397 startorbit=perlsdata[7]
00398 numorbit=perlsdata[8]
00399 bxidxlistResult=[]
00400 bxvaluelistResult=[]
00401 bxerrorlistResult=[]
00402 calibratedbxdata=None
00403 beamdata=None
00404 if withBXInfo:
00405 if bxdata:
00406 bxidxlist=bxdata[0]
00407 bxvaluelist=bxdata[1]
00408
00409 bxlumierrlist=bxdata[2]
00410 for idx,bxidx in enumerate(bxidxlist):
00411 bxval=bxvaluelist[idx]
00412 bxlumierr=bxlumierrlist[idx]
00413 if finecorrections and finecorrections[run]:
00414 if usecorrectionv2:
00415 if driftcorrections and driftcorrections[run]:
00416 mybxval=lumiCorrections.applyfinecorrectionBXV2(bxval,avglumi,perbunchnormval,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],driftcorrections[run])
00417 else:
00418 mybxval=lumiCorrections.applyfinecorrectionBXV2(bxval,avglumi,perbunchnormval,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],1.0)
00419 else:
00420 mybxval=lumiCorrections.applyfinecorrectionBX(bxval,avglumi,perbunchnormval,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2])
00421 bxidxlistResult.append(bxidx)
00422 bxvaluelistResult.append(mybxval)
00423 bxerrorlistResult.append(bxlumierr*perbunchnormval)
00424 del bxdata[1][:]
00425 del bxdata[2][:]
00426 calibratedbxdata=(bxidxlistResult,bxvaluelistResult,bxerrorlistResult)
00427 if withBeamIntensity:
00428 beamdata=perlsdata[10]
00429 result[run].append([lumilsnum,cmslsnum,timestamp,bs,beamenergy,calibratedlumi,calibratedlumierr,startorbit,numorbit,calibratedbxdata,beamdata,fillnum])
00430 del perlsdata[:]
00431 return result
00432
00433 def deliveredLumiForRange(schema,inputRange,beamstatus=None,amodetag=None,egev=None,withBXInfo=False,bxAlgo=None,xingMinLum=0.0,withBeamIntensity=False,norm=None,datatag='DATA',finecorrections=None,driftcorrections=None,usecorrectionv2=False,lumitype='HF',branchName=None):
00434 '''
00435 delivered lumi (including calibration,time integral)
00436 input:
00437 inputRange {run:[lsnum]} (required) [lsnum]==None means all ; [lsnum]==[] means selected ls
00438 amodetag : accelerator mode for all the runs (optional) ['PROTPHYS','IONPHYS']
00439 beamstatus: LS filter on beamstatus (optional)
00440 amodetag: amodetag for picking norm(optional)
00441 egev: beamenergy for picking norm(optional)
00442 withBXInfo: get per bunch info (optional)
00443 bxAlgo: algoname for bx values (optional) ['OCC1','OCC2','ET']
00444 xingMinLum: cut on bx lumi value (optional)
00445 withBeamIntensity: get beam intensity info (optional)
00446 norm: norm factor name to use: if float, apply directly, if str search norm by name (optional)
00447 branchName: data version or branch name
00448 output:
00449 result {run:[lumilsnum(0),cmslsnum(1),timestamp(2),beamstatus(3),beamenergy(4),deliveredlumi(5),calibratedlumierr(6),(bxvalues,bxerrs)(7),(bxidx,b1intensities,b2intensities)(8),fillnum(9)]}
00450 avg lumi unit: 1/ub
00451 '''
00452 lumip=lumiParameters.ParametersObject()
00453 result = {}
00454 normval=None
00455 perbunchnormval=None
00456 if norm:
00457 normval=_getnorm(schema,norm)
00458 perbunchnormval=float(normval)/float(1000)
00459 elif amodetag and egev:
00460 normval=_decidenormFromContext(schema,amodetag,egev)
00461 perbunchnormval=float(normval)/float(1000)
00462 instresult=instLumiForRange(schema,inputRange,beamstatusfilter=beamstatus,withBXInfo=withBXInfo,bxAlgo=bxAlgo,xingMinLum=xingMinLum,withBeamIntensity=withBeamIntensity,lumitype=lumitype,branchName=branchName)
00463
00464 for run,perrundata in instresult.items():
00465 if perrundata is None:
00466 result[run]=None
00467 continue
00468 result[run]=[]
00469 if not normval:
00470 normval=_decidenormForRun(schema,run)
00471 perbunchnormval=float(normval)/float(1000)
00472 if not normval:
00473 normval=6370
00474 perbunchnormval=6.37
00475 print '[Warning] using default normalization '+str(normval)
00476 for perlsdata in perrundata:
00477 lumilsnum=perlsdata[0]
00478 cmslsnum=perlsdata[1]
00479 timestamp=perlsdata[2]
00480 bs=perlsdata[3]
00481 beamenergy=perlsdata[4]
00482 calibratedlumi=perlsdata[5]*normval
00483 fillnum=perlsdata[11]
00484 if lumitype=='HF' and finecorrections and finecorrections[run]:
00485 if usecorrectionv2:
00486 if driftcorrections and driftcorrections[run]:
00487 calibratedlumi=lumiCorrections.applyfinecorrectionV2(calibratedlumi,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],driftcorrections[run])
00488 else:
00489 calibratedlumi=lumiCorrections.applyfinecorrectionV2(calibratedlumi,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],1.0)
00490 else:
00491 calibratedlumi=lumiCorrections.applyfinecorrection(calibratedlumi,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2])
00492 if lumitype=='PIXEL' and finecorrections is not None:
00493 calibratedlumi=finecorrections[run]*calibratedlumi
00494 calibratedlumierr=perlsdata[6]*normval
00495 numorbit=perlsdata[8]
00496 numbx=lumip.NBX
00497 lslen=lumip.lslengthsec()
00498 deliveredlumi=calibratedlumi*lslen
00499 calibratedbxdata=None
00500 beamdata=None
00501 if withBXInfo:
00502 bxdata=perlsdata[9]
00503 if bxdata:
00504 calibratedbxdata=(bxdata[0],[x*perbunchnormval for x in bxdata[1]],[x*perbunchnormval for x in bxdata[2]])
00505 del bxdata[1][:]
00506 del bxdata[2][:]
00507 if withBeamIntensity:
00508 beamdata=perlsdata[10]
00509 result[run].append([lumilsnum,cmslsnum,timestamp,bs,beamenergy,deliveredlumi,calibratedlumierr,calibratedbxdata,beamdata,fillnum])
00510 del perlsdata[:]
00511 return result
00512
00513 def lumiForRange(schema,inputRange,beamstatus=None,amodetag=None,egev=None,withBXInfo=False,bxAlgo=None,xingMinLum=0.0,withBeamIntensity=False,norm=None,datatag='DATA',finecorrections=None,driftcorrections=None,usecorrectionv2=False,lumitype='HF',branchName=None):
00514 '''
00515 delivered/recorded lumi
00516 input:
00517 inputRange {run:[cmsls]} (required)
00518 beamstatus: LS filter on beamstatus (optional)
00519 amodetag: amodetag for picking norm(optional)
00520 egev: beamenergy for picking norm(optional)
00521 withBXInfo: get per bunch info (optional)
00522 bxAlgo: algoname for bx values (optional) ['OCC1','OCC2','ET']
00523 xingMinLum: cut on bx lumi value (optional)
00524 withBeamIntensity: get beam intensity info (optional)
00525 normname: norm factor name to use (optional)
00526 branchName: data version
00527 output:
00528 result {run:[lumilsnum(0),cmslsnum(1),timestamp(2),beamstatus(3),beamenergy(4),deliveredlumi(5),recordedlumi(6),calibratedlumierror(7),(bxidx,bxvalues,bxerrs)(8),(bxidx,b1intensities,b2intensities)(9),fillnum(10)]}
00529 lumi unit: 1/ub
00530 '''
00531 if lumitype not in ['HF','PIXEL']:
00532 raise ValueError('unknown lumitype '+lumitype)
00533
00534
00535 lumip=lumiParameters.ParametersObject()
00536 lumitableName=''
00537 lumilstableName=''
00538 if lumitype=='HF':
00539 lumitableName=nameDealer.lumidataTableName()
00540 lumilstableName=nameDealer.lumisummaryv2TableName()
00541 else:
00542 lumitableName=nameDealer.pixellumidataTableName()
00543 lumilstableName=nameDealer.pixellumisummaryv2TableName()
00544 numbx=lumip.NBX
00545 result = {}
00546 normval=None
00547 perbunchnormval=None
00548 if norm:
00549 normval=_getnorm(schema,norm)
00550 perbunchnormval=float(normval)/float(1000)
00551 elif amodetag and egev:
00552 normval=_decidenormFromContext(schema,amodetag,egev)
00553 perbunchnormval=float(normval)/float(1000)
00554 c=lumiTime.lumiTime()
00555 for run in inputRange.keys():
00556 lslist=inputRange[run]
00557 if lslist is not None and len(lslist)==0:
00558 result[run]=[]
00559 continue
00560 cmsrunsummary=dataDML.runsummary(schema,run)
00561 if len(cmsrunsummary)==0:
00562 result[run]=None
00563 continue
00564 startTimeStr=cmsrunsummary[6]
00565 fillnum=cmsrunsummary[4]
00566 lumidataid=None
00567 trgdataid=None
00568 lumidataid=dataDML.guessLumiDataIdByRun(schema,run,lumitableName)
00569 if lumidataid is None :
00570 result[run]=None
00571 continue
00572 trgdataid=dataDML.guessTrgDataIdByRun(schema,run)
00573 (lumirunnum,lumidata)=dataDML.lumiLSById(schema,lumidataid,beamstatus=beamstatus,withBXInfo=withBXInfo,bxAlgo=bxAlgo,withBeamIntensity=withBeamIntensity,tableName=lumilstableName)
00574 if trgdataid is None :
00575 trgdata={}
00576 else:
00577 (trgrunnum,trgdata)=dataDML.trgLSById(schema,trgdataid)
00578
00579 if not normval:
00580 normval=_decidenormForRun(schema,run)
00581 perbunchnormval=float(normval)/float(1000)
00582 if not normval:
00583 normval=6370
00584 perbunchnormval=6.37
00585 print '[Warning] using default normalization '+str(normval)
00586
00587 perrunresult=[]
00588 for lumilsnum,perlsdata in lumidata.items():
00589 cmslsnum=perlsdata[0]
00590 triggeredls=perlsdata[0]
00591 if lslist is not None and cmslsnum not in lslist:
00592
00593 triggeredls=0
00594 recordedlumi=0.0
00595 instlumi=perlsdata[1]
00596 instlumierror=perlsdata[2]
00597 avglumi=instlumi*normval
00598 calibratedlumi=avglumi
00599 if lumitype=='HF' and finecorrections and finecorrections[run]:
00600 if usecorrectionv2:
00601 if driftcorrections and driftcorrections[run]:
00602 calibratedlumi=lumiCorrections.applyfinecorrectionV2(avglumi,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],driftcorrections[run])
00603 else:
00604 calibratedlumi=lumiCorrections.applyfinecorrectionV2(avglumi,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],1.0)
00605 else:
00606 calibratedlumi=lumiCorrections.applyfinecorrection(avglumi,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2])
00607 if lumitype=='PIXEL' and finecorrections is not None:
00608 calibratedlumi=finecorrections[run]*avglumi
00609 calibratedlumierror=instlumierror*normval
00610 bstatus=perlsdata[4]
00611 begev=perlsdata[5]
00612 numorbit=perlsdata[6]
00613 startorbit=perlsdata[7]
00614 timestamp=c.OrbitToTime(startTimeStr,startorbit,0)
00615 lslen=lumip.lslengthsec()
00616 deliveredlumi=calibratedlumi*lslen
00617 recordedlumi=0.0
00618 if triggeredls!=0:
00619 if not trgdata.has_key(cmslsnum):
00620
00621 recordedlumi=0.0
00622 else:
00623 deadcount=trgdata[cmslsnum][0]
00624 bitzerocount=trgdata[cmslsnum][1]
00625 bitzeroprescale=trgdata[cmslsnum][2]
00626 deadfrac=trgdata[cmslsnum][3]
00627 if deadfrac<0 or deadfrac>1.0:
00628 deadfrac=1.0
00629
00630
00631
00632
00633
00634
00635 recordedlumi=deliveredlumi*(1.0-deadfrac)
00636 del trgdata[cmslsnum][:]
00637 bxdata=None
00638 if withBXInfo:
00639 bxinfo=perlsdata[8]
00640 bxvalueArray=None
00641 bxerrArray=None
00642 bxidxlist=[]
00643 bxvaluelist=[]
00644 bxerrorlist=[]
00645 if bxinfo:
00646 bxvalueArray=bxinfo[0]
00647 bxerrArray=bxinfo[1]
00648
00649
00650 for idx,bxval in enumerate(bxvalueArray):
00651 if finecorrections and finecorrections[run]:
00652 if usecorrectionv2:
00653 if driftcorrections and driftcorrections[run]:
00654 mybxval=lumiCorrections.applyfinecorrectionBXV2(bxval,avglumi,perbunchnormval,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],driftcorrections[run])
00655 else:
00656 mybxval=lumiCorrections.applyfinecorrectionBXV2(bxval,avglumi,perbunchnormval,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],1.0)
00657 else:
00658 mybxval=lumiCorrections.applyfinecorrectionBX(bxval,avglumi,perbunchnormval,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2])
00659 else:
00660 mybxval=bxval*perbunchnormval
00661 if mybxval>xingMinLum:
00662 bxidxlist.append(idx)
00663 bxvaluelist.append(mybxval)
00664 bxerrorlist.append(bxerrArray[idx]*perbunchnormval)
00665 del bxvalueArray[:]
00666 del bxerrArray[:]
00667 bxdata=(bxidxlist,bxvaluelist,bxerrorlist)
00668 beamdata=None
00669 if withBeamIntensity:
00670 beaminfo=perlsdata[9]
00671 bxindexlist=[]
00672 b1intensitylist=[]
00673 b2intensitylist=[]
00674 if beaminfo:
00675 bxindexarray=beaminfo[0]
00676 beam1intensityarray=beaminfo[1]
00677 beam2intensityarray=beaminfo[2]
00678 bxindexlist=bxindexarray.tolist()
00679 b1intensitylist=beam1intensityarray.tolist()
00680 b2intensitylist=beam2intensityarray.tolist()
00681 del bxindexarray[:]
00682 del beam1intensityarray[:]
00683 del beam2intensityarray[:]
00684 beamdata=(bxindexlist,b1intensitylist,b2intensitylist)
00685 perrunresult.append([lumilsnum,triggeredls,timestamp,bstatus,begev,deliveredlumi,recordedlumi,calibratedlumierror,bxdata,beamdata,fillnum])
00686 del perlsdata[:]
00687 result[run]=perrunresult
00688 return result
00689
00690 def effectiveLumiForRange(schema,inputRange,hltpathname=None,hltpathpattern=None,amodetag=None,beamstatus=None,egev=None,withBXInfo=False,xingMinLum=0.0,bxAlgo=None,withBeamIntensity=False,norm=None,finecorrections=None,driftcorrections=None,usecorrectionv2=False,lumitype='HF',branchName=None):
00691 '''
00692 input:
00693 inputRange {run:[cmsls]} (required)
00694 hltpathname: selected hltpathname
00695 hltpathpattern: regex select hltpaths
00696 amodetag: amodetag for picking norm(optional)
00697 egev: beamenergy for picking norm(optional)
00698 withBXInfo: get per bunch info (optional)
00699 bxAlgo: algoname for bx values (optional) ['OCC1','OCC2','ET']
00700 xingMinLum: cut on bx lumi value (optional)
00701 withBeamIntensity: get beam intensity info (optional)
00702 normname: norm factor name to use (optional)
00703 branchName: data version
00704 output:
00705 result {run:[lumilsnum(0),cmslsnum(1),timestamp(2),beamstatus(3),beamenergy(4),deliveredlumi(5),recordedlumi(6),calibratedlumierror(7),{hltpath:[l1name,l1prescale,hltprescale,efflumi]},bxdata,beamdata,fillnum]}
00706 lumi unit: 1/ub
00707 '''
00708 if lumitype not in ['HF','PIXEL']:
00709 raise ValueError('unknown lumitype '+lumitype)
00710 if branchName is None:
00711 branchName='DATA'
00712 lumitableName=''
00713 lumilstableName=''
00714 if lumitype=='HF':
00715 lumitableName=nameDealer.lumidataTableName()
00716 lumilstableName=nameDealer.lumisummaryv2TableName()
00717 else:
00718 lumitableName=nameDealer.pixellumidataTableName()
00719 lumilstableName=nameDealer.pixellumisummaryv2TableName()
00720 numbx=3564
00721 result = {}
00722 normval=None
00723 perbunchnormval=None
00724 if norm:
00725 normval=_getnorm(schema,norm)
00726 perbunchnormval=float(normval)/float(1000)
00727 elif amodetag and egev:
00728 normval=_decidenormFromContext(schema,amodetag,egev)
00729 perbunchnormval=float(normval)/float(1000)
00730 c=lumiTime.lumiTime()
00731 lumip=lumiParameters.ParametersObject()
00732 for run in inputRange.keys():
00733 lslist=inputRange[run]
00734 if lslist is not None and len(lslist)==0:
00735 result[run]=[]
00736 continue
00737 cmsrunsummary=dataDML.runsummary(schema,run)
00738 if len(cmsrunsummary)==0:
00739 result[run]=None
00740 continue
00741 startTimeStr=cmsrunsummary[6]
00742 fillnum=cmsrunsummary[4]
00743 lumidataid=None
00744 trgdataid=None
00745 hltdataid=None
00746 lumidataid=dataDML.guessLumiDataIdByRun(schema,run,lumitableName)
00747 trgdataid=dataDML.guessTrgDataIdByRun(schema,run)
00748 hltdataid=dataDML.guessHltDataIdByRun(schema,run)
00749 if lumidataid is None or trgdataid is None or hltdataid is None:
00750 result[run]=None
00751 continue
00752 (lumirunnum,lumidata)=dataDML.lumiLSById(schema,lumidataid,beamstatus,tableName=lumilstableName)
00753 (trgrunnum,trgdata)=dataDML.trgLSById(schema,trgdataid,withPrescale=True)
00754 (hltrunnum,hltdata)=dataDML.hltLSById(schema,hltdataid,hltpathname=hltpathname,hltpathpattern=hltpathpattern)
00755 hlttrgmap=dataDML.hlttrgMappingByrun(schema,run)
00756 if not normval:
00757 normval=_decidenormForRun(schema,run)
00758 perbunchnormval=float(normval)/float(1000)
00759 if not normval:
00760 normval=6370
00761 perbunchnormval=6.37
00762 print '[Warning] using default normalization '+str(normval)
00763
00764 perrunresult=[]
00765 for lumilsnum,perlsdata in lumidata.items():
00766 cmslsnum=perlsdata[0]
00767 triggeredls=perlsdata[0]
00768 if lslist is not None and cmslsnum not in lslist:
00769
00770 triggeredls=0
00771 recordedlumi=0.0
00772 instlumi=perlsdata[1]
00773 instlumierror=perlsdata[2]
00774 avglumi=instlumi*normval
00775 calibratedlumi=avglumi
00776 if lumitype=='HF' and finecorrections and finecorrections[run]:
00777 if usecorrectionv2:
00778 if driftcorrections and driftcorrections[run]:
00779 calibratedlumi=lumiCorrections.applyfinecorrectionV2(avglumi,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],driftcorrections[run])
00780 else:
00781 calibratedlumi=lumiCorrections.applyfinecorrectionV2(avglumi,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],1.0)
00782 else:
00783 calibratedlumi=lumiCorrections.applyfinecorrection(avglumi,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2])
00784 if lumitype=='PIXEL' and finecorrections is not None:
00785 calibratedlumi=finecorrections[run]*avglumi
00786 calibratedlumierror=instlumierror*normval
00787 bstatus=perlsdata[4]
00788 begev=perlsdata[5]
00789 numorbit=perlsdata[6]
00790 startorbit=perlsdata[7]
00791 timestamp=c.OrbitToTime(startTimeStr,startorbit,0)
00792 lslen=lumip.lslengthsec()
00793 deliveredlumi=calibratedlumi*lslen
00794 recordedlumi=0.0
00795 trgprescalemap={}
00796 efflumidict={}
00797 if triggeredls!=0:
00798 if not trgdata.has_key(cmslsnum):
00799
00800 recordedlumi=0.0
00801 else:
00802 deadcount=trgdata[cmslsnum][0]
00803 bitzerocount=trgdata[cmslsnum][1]
00804 bitzeroprescale=trgdata[cmslsnum][2]
00805 deadfrac=trgdata[cmslsnum][3]
00806 if deadfrac<0 or deadfrac>1.0:
00807 deadfrac=1.0
00808
00809
00810
00811
00812
00813
00814 recordedlumi=deliveredlumi*(1.0-deadfrac)
00815 l1bitinfo=trgdata[cmslsnum][4]
00816 if l1bitinfo:
00817 for thisbitinfo in l1bitinfo:
00818 thisbitname=thisbitinfo[0]
00819 thisbitprescale=thisbitinfo[2]
00820 trgprescalemap['"'+thisbitname+'"']=thisbitprescale
00821 del trgdata[cmslsnum][:]
00822 if hltdata.has_key(cmslsnum):
00823 hltpathdata=hltdata[cmslsnum]
00824
00825 for pathidx,thispathinfo in enumerate(hltpathdata):
00826 efflumi=0.0
00827 thispathname=thispathinfo[0]
00828 thisprescale=thispathinfo[1]
00829 thisl1seed=None
00830 l1bitname=None
00831 l1prescale=None
00832 try:
00833 thisl1seed=hlttrgmap[thispathname]
00834 except KeyError:
00835 thisl1seed=None
00836
00837 if thisl1seed:
00838 try:
00839 l1bitname=hltTrgSeedMapper.findUniqueSeed(thispathname,thisl1seed)
00840 if l1bitname :
00841 l1prescale=trgprescalemap[l1bitname]
00842 except KeyError:
00843 l1prescale=None
00844 if l1prescale and thisprescale :
00845 efflumi=recordedlumi/(float(l1prescale)*float(thisprescale))
00846 efflumidict[thispathname]=[l1bitname,l1prescale,thisprescale,efflumi]
00847 elif l1prescale and thisprescale==0:
00848 efflumi=0.0
00849 efflumidict[thispathname]=[l1bitname,l1prescale,thisprescale,efflumi]
00850
00851 bxvaluelist=[]
00852 bxerrorlist=[]
00853 bxdata=None
00854 beamdata=None
00855 if withBXInfo:
00856 bxinfo=lumidata[8]
00857 bxvalueArray=None
00858 bxerrArray=None
00859 if bxinfo:
00860 bxvalueArray=bxinfo[0]
00861 bxerrArray=bxinfo[1]
00862 for idx,bxval in enumerate(bxvalueArray):
00863 if finecorrections and finecorrections[run]:
00864 if usecorrectionv2:
00865 if driftcorrections and driftcorrections[run]:
00866 mybxval=lumiCorrections.applyfinecorrectionBXV2(bxval,avglumi,perbunchnormval,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],driftcorrections[run])
00867 else:
00868 mybxval=lumiCorrections.applyfinecorrectionBXV2(bxval,avglumi,perbunchnormval,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],1.0)
00869 else:
00870 mybxval=lumiCorrections.applyfinecorrectionBX(bxval,avglumi,perbunchnormval,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2])
00871 else:
00872 mybxval=bxval*perbunchnormval
00873 if mybxval>xingMinLum:
00874 bxidxlist.append(idx)
00875 bxvaluelist.append(bxval)
00876 bxerrorlist.append(bxerrArray[idx])
00877 del bxvalueArray[:]
00878 del bxerrArray[:]
00879 bxdata=(bxidxlist,bxvaluelist,bxerrorlist)
00880 if withBeamIntensity:
00881 beaminfo=perlsdata[9]
00882 bxindexlist=[]
00883 b1intensitylist=[]
00884 b2intensitylist=[]
00885 if beaminfo:
00886 bxindexarray=beaminfo[0]
00887 beam1intensityarray=beaminfo[1]
00888 beam2intensityarray=beaminfo[2]
00889 bxindexlist=bxindexarray.tolist()
00890 b1intensitylist=beam1intensityarray.tolist()
00891 b2intensitylist=beam2intensityarray.tolist()
00892 del bxindexarray[:]
00893 del beam1intensityarray[:]
00894 del beam2intensityarray[:]
00895 beamdata=(bxindexlist,b1intensitylist,b2intensitylist)
00896
00897 perrunresult.append([lumilsnum,triggeredls,timestamp,bstatus,begev,deliveredlumi,recordedlumi,calibratedlumierror,efflumidict,bxdata,beamdata,fillnum])
00898 del perlsdata[:]
00899 result[run]=perrunresult
00900
00901 return result
00902
00903 def validation(schema,run=None,cmsls=None):
00904 '''retrieve validation data per run or all
00905 input: run. if not run, retrive all; if cmslsnum selection list pesent, filter out unselected result
00906 output: {run:[[cmslsnum,status,comment]]}
00907 '''
00908 result={}
00909 qHandle=schema.newQuery()
00910 queryHandle.addToTableList(nameDealer.lumivalidationTableName())
00911 queryHandle.addToOutputList('RUNNUM','runnum')
00912 queryHandle.addToOutputList('CMSLSNUM','cmslsnum')
00913 queryHandle.addToOutputList('FLAG','flag')
00914 queryHandle.addToOutputList('COMMENT','comment')
00915 if run:
00916 queryCondition='RUNNUM=:runnum'
00917 queryBind=coral.AttributeList()
00918 queryBind.extend('runnum','unsigned int')
00919 queryBind['runnum'].setData(run)
00920 queryHandle.setCondition(queryCondition,queryBind)
00921 queryResult=coral.AttributeList()
00922 queryResult.extend('runnum','unsigned int')
00923 queryResult.extend('cmslsnum','unsigned int')
00924 queryResult.extend('flag','string')
00925 queryResult.extend('comment','string')
00926 queryHandle.defineOutput(queryResult)
00927 cursor=queryHandle.execute()
00928 while cursor.next():
00929 runnum=cursor.currentRow()['runnum'].data()
00930 if not result.has_key(runnum):
00931 result[runnum]=[]
00932 cmslsnum=cursor.currentRow()['cmslsnum'].data()
00933 flag=cursor.currentRow()['flag'].data()
00934 comment=cursor.currentRow()['comment'].data()
00935 result[runnum].append([cmslsnum,flag,comment])
00936 if run and cmsls and len(cmsls)!=0:
00937 selectedresult={}
00938 for runnum,perrundata in result.items():
00939 for lsdata in perrundata:
00940 if lsdata[0] not in cmsls:
00941 continue
00942 if not selectedresult.has_key(runnum):
00943 selectedresult[runnum]=[]
00944 selectedresult[runnum].append(lsdata)
00945 return selectedresult
00946 else:
00947 return result
00948
00949
00950