CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch2/src/RecoLuminosity/LumiDB/python/lumiCalcAPI.py

Go to the documentation of this file.
00001 import os,coral,datetime,fnmatch,time
00002 from RecoLuminosity.LumiDB import nameDealer,revisionDML,dataDML,lumiTime,CommonUtil,selectionParser,hltTrgSeedMapper,normFunctors,lumiParameters
00003 
00004 ########################################################################
00005 # Lumi data management and calculation API                             #
00006 #                                                                      #
00007 # Author:      Zhen Xie                                                #
00008 ########################################################################
00009 
00010 def runsummary(schema,irunlsdict):
00011     '''
00012     output  [[run(0),l1key(1),amodetag(2),egev(3),hltkey(4),fillnum(5),fillscheme(6),starttime(7),stoptime(8)]]
00013     '''
00014     result=[]
00015     for run in sorted(irunlsdict):
00016         runinfo=dataDML.runsummary(schema,run)
00017         runinfo.insert(0,run)
00018         result.append(runinfo)
00019     return result
00020 
00021 def runsummaryMap(schema,irunlsdict,dataidmap,lumitype='HF'):
00022     '''
00023     output: {run:[l1key(0),amodetag(1),egev(2),hltkey(3),fillnum(4),fillscheme(5),starttime(6),stoptime(7)]}
00024     '''
00025     result={}
00026     seqresult=runsummary(schema,irunlsdict)
00027     idresult=dataDML.lumiRunByIds(schema,dataidmap,lumitype)
00028     for [run,l1key,amodetag,hltkey,fillnum,fillscheme] in seqresult:
00029         egev=idresult[run][2]
00030         startT=idresult[run][3]
00031         stopT=idresult[run][4]
00032         result[run]=[l1key,amodetag,egev,hltkey,fillnum,fillscheme,startT,stopT]
00033     return result
00034 
00035 def fillInRange(schema,fillmin=1000,fillmax=9999,amodetag='PROTPHYS',startT=None,stopT=None):
00036     '''
00037     output [fill]
00038     '''
00039     fills=dataDML.fillInRange(schema,fillmin,fillmax,amodetag,startT,stopT)
00040     return fills
00041 def fillrunMap(schema,fillnum=None,runmin=None,runmax=None,startT=None,stopT=None,l1keyPattern=None,hltkeyPattern=None,amodetag=None):
00042     '''
00043     output: {fill:[runnum,...]}
00044     '''
00045     return dataDML.fillrunMap(schema,fillnum=fillnum,runmin=runmin,runmax=runmax,startT=startT,stopT=stopT,l1keyPattern=l1keyPattern,hltkeyPattern=hltkeyPattern,amodetag=amodetag)
00046              
00047 def runList(schema,datatagid,runmin=None,runmax=None,fillmin=None,fillmax=None,startT=None,stopT=None,l1keyPattern=None,hltkeyPattern=None,amodetag=None,nominalEnergy=None,energyFlut=0.2,requiretrg=True,requirehlt=True,preselectedruns=None,lumitype='HF'):
00048     '''
00049     output: [runnumber,...]
00050     '''
00051     return dataDML.runList(schema,datatagid,runmin=runmin,runmax=runmax,fillmin=fillmin,fillmax=fillmax,startT=startT,stopT=stopT,l1keyPattern=l1keyPattern,hltkeyPattern=hltkeyPattern,amodetag=amodetag,nominalEnergy=nominalEnergy,energyFlut=energyFlut,requiretrg=requiretrg,requirehlt=requirehlt,lumitype=lumitype)
00052 
00053 def hltpathsForRange(schema,runlist,hltpathname=None,hltpathpattern=None):
00054     '''
00055     input: runlist [run],     (required)      
00056            datatag: data version (optional)
00057     output : {runnumber,[(hltpath,l1seedexpr,l1bitname)...]}
00058     '''
00059     result={}
00060     for run in runlist:
00061         hlttrgmap=dataDML.hlttrgMappingByrun(schema,run,hltpathname=hltpathname,hltpathpattern=hltpathpattern)
00062         result[run]=[]
00063         for hltpath in sorted(hlttrgmap):
00064             l1seedexpr=hlttrgmap[hltpath]
00065             (exptype,l1bits)=hltTrgSeedMapper.findUniqueSeed(hltpath,l1seedexpr)
00066             l1bitname='n/a'
00067             if l1bits:
00068                 if exptype:
00069                     l1bitname=l1seedexpr
00070                 else:
00071                     l1bitname=l1bits[0]
00072             result[run].append((hltpath,l1seedexpr,l1bitname))
00073     return result
00074 
00075 def beamForRange(schema,inputRange,withBeamIntensity=False,minIntensity=0.1,tableName=None,branchName=None):
00076     '''
00077     input:
00078            inputRange: {run:[cmsls]} (required)
00079     output : {runnumber:[(lumicmslnum,cmslsnum,beamenergy,beamstatus,[(ibx,b1,b2)])...](4)}
00080     '''
00081     if tableName is None:
00082         tableName=nameDealer.lumidataTableName()
00083     if branchName is None:
00084         branchName='DATA'
00085     result={}
00086     for run in inputRange.keys():
00087         lslist=inputRange[run]
00088         if lslist is not None and len(lslist)==0:
00089             result[run]=[]#if no LS is selected for a run
00090             continue
00091         lumidataid=dataDML.guessLumiDataIdByRun(schema,run,tableName)
00092         if lumidataid is None:
00093             result[run]=None
00094             continue #run non exist
00095         lumidata=dataDML.beamInfoById(schema,lumidataid,withBeamIntensity=withBeamIntensity,minIntensity=minIntensity)
00096         #(runnum,[(lumilsnum(0),cmslsnum(1),beamstatus(2),beamenergy(3),beaminfolist(4)),..])
00097         result[run]=[]
00098         perrundata=lumidata[1]
00099         if not perrundata:
00100             result[run]=[]
00101             continue
00102         for perlsdata in perrundata:
00103             lumilsnum=perlsdata[0]
00104             cmslsnum=perlsdata[1]
00105             if lslist is not None and cmslsnum not in lslist:
00106                 continue
00107             beamstatus=perlsdata[2]
00108             beamenergy=perlsdata[3]
00109             beamintInfolist=[]
00110             if withBeamIntensity:
00111                 beamintInfolist=perlsdata[4]
00112             result[run].append((lumilsnum,cmslsnum,beamstatus,beamenergy,beamintInfolist))        
00113     return result
00114 
00115 def beamForIds(schema,irunlsdict,dataidmap,withBeamIntensity=False,minIntensity=0.1):
00116     '''
00117     input:
00118            inputRange: {run:[cmsls]} (required)
00119            dataidmap: {run:(lumiid,trgid,hltid)}
00120     output : {runnumber:[(lumicmslnum(0),cmslsnum(1),beamenergy(2),beamstatus(3),ncollidingbx(4),[(ibx,b1,b2)])...](5)}
00121     '''
00122     result={}
00123     for run in irunlsdict.keys():
00124         result[run]=[]
00125         lslist=irunlsdict[run]
00126         if lslist is not None and len(lslist)==0:
00127             continue
00128         if not dataidmap.has_key(run):
00129             continue #run non exist
00130         lumidataid=dataidmap[run][0]
00131         if lumidataid is None:
00132             result[run]=None
00133             continue
00134         lumidata=dataDML.beamInfoById(schema,lumidataid,withBeamIntensity=withBeamIntensity,minIntensity=minIntensity)
00135         #(runnum,[(lumilsnum(0),cmslsnum(1),beamstatus(2),beamenergy(3),ncollidingbunches(4),beaminfolist(5),..])
00136         if lumidata and lumidata[1]:
00137             perrundata=lumidata[1]
00138             for perlsdata in perrundata:
00139                 lumilsnum=perlsdata[0]
00140                 cmslsnum=perlsdata[1]
00141                 if lslist is not None and cmslsnum not in lslist:
00142                     continue
00143                 beamstatus=perlsdata[2]
00144                 beamenergy=perlsdata[3]
00145                 ncollidingbunches=perlsdata[4]
00146                 beamintInfolist=[]
00147                 if withBeamIntensity:
00148                     beamintInfolist=perlsdata[5]
00149                 result[run].append((lumilsnum,cmslsnum,beamstatus,beamenergy,ncollidingbunches,beamintInfolist))        
00150     return result
00151 
00152 def hltForIds(schema,irunlsdict,dataidmap,hltpathname=None,hltpathpattern=None,withL1Pass=False,withHLTAccept=False):
00153     '''
00154     input:
00155            irunlsdict: {run:[cmsls]} (required)
00156            dataidmap: {run:(lumiid,trgid,hltid)}
00157            hltpathname: exact match hltpathname  (optional) 
00158            hltpathpattern: regex match hltpathpattern (optional)
00159            withL1Pass: with L1 pass count
00160            withHLTAccept: with HLT accept
00161     output: {runnumber:[(cmslsnum,[(hltpath,hltprescale,l1pass,hltaccept),...]),(cmslsnum,[])})}
00162     '''
00163     result={}
00164     for run in irunlsdict.keys():
00165         lslist=irunlsdict[run]
00166         if lslist is not None and len(lslist)==0:
00167             result[run]=[]#if no LS is selected for a run
00168             continue
00169         if not dataidmap.has_key(run):
00170             continue
00171         hltdataid=dataidmap[run][2]
00172         if hltdataid is None:
00173             result[run]=None
00174             continue #run non exist
00175         hltdata=dataDML.hltLSById(schema,hltdataid,hltpathname=hltpathname,hltpathpattern=hltpathpattern,withL1Pass=withL1Pass,withHLTAccept=withHLTAccept)
00176         #(runnum,{cmslsnum:[(pathname,prescale,l1pass,hltaccept),...]})
00177         result[run]=[]            
00178         if hltdata and hltdata[1]:
00179             lsdict={}            
00180             for cmslsnum in sorted(hltdata[1]):
00181                 if lslist is not None and cmslsnum not in lslist:
00182                     continue
00183                 lsdata=[]
00184                 for perpathdata in hltdata[1][cmslsnum]:
00185                     pathname=perpathdata[0]
00186                     prescale=perpathdata[1]
00187                     l1pass=None
00188                     hltaccept=None
00189                     if withL1Pass:
00190                         l1pass=perpathdata[2]
00191                     if withHLTAccept:
00192                         hltaccept=perpathdata[3]
00193                     lsdata.append((pathname,prescale,l1pass,hltaccept))
00194                 result[run].append((cmslsnum,lsdata))
00195     return result
00196     
00197 def trgForIds(schema,irunlsdict,dataidmap,trgbitname=None,trgbitnamepattern=None,withL1Count=False,withPrescale=False):
00198     '''
00199     input :
00200             irunlsdict  {run:[cmsls]} (required)
00201             dataidmap: {run:(lumiid,trgid,hltid)}
00202             trgbitname exact match  trgbitname (optional)
00203             trgbitnamepattern match trgbitname (optional)
00204     output
00205             result {run:[[cmslsnum(0),deadfrac(1),deadtimecount(2),bitzero_count(3),bitzero_prescale(4),[(bitname,prescale,counts,mask)](5)]]}
00206     '''
00207     result={}
00208     for run in irunlsdict.keys():
00209         result[run]=[]
00210         lslist=irunlsdict[run]
00211         if lslist is not None and len(lslist)==0:
00212             #if no LS is selected for a run
00213             continue
00214         if not dataidmap.has_key(run):
00215             continue
00216         trgdataid=dataidmap[run][1]
00217         if trgdataid is None:
00218             result[run]=None
00219             continue        #if run non exist
00220         if trgdataid==0:
00221             continue
00222         trgdata=dataDML.trgLSById(schema,trgdataid,trgbitname=trgbitname,trgbitnamepattern=trgbitnamepattern,withL1Count=withL1Count,withPrescale=withPrescale)
00223     
00224         #(runnum,{cmslsnum:[deadtimecount(0),bitzerocount(1),bitzeroprescale(2),deadfrac(3),[(bitname,trgcount,prescale)](4)]})
00225         if trgdata and trgdata[1]:
00226             lsdict={}
00227             for cmslsnum in sorted(trgdata[1]):
00228                 if lslist is not None and cmslsnum not in lslist:
00229                     continue
00230                 lsdata=[]
00231                 #print trgdata[1][cmslsnum]
00232                 deadtimecount=trgdata[1][cmslsnum][0]
00233                 #bitzerocount=trgdata[1][cmslsnum][1]
00234                 #bitzeroprescale=trgdata[1][cmslsnum][2]
00235                 bitzerocount=0
00236                 bitzeroprescale=0
00237                 deadfrac=trgdata[1][cmslsnum][3]
00238                 if deadfrac<0 or deadfrac>1.0:
00239                     deadfrac=1.0
00240                 allbitsinfo=trgdata[1][cmslsnum][4]
00241                 lsdata.append(cmslsnum)
00242                 lsdata.append(deadfrac)
00243                 lsdata.append(deadtimecount)
00244                 lsdata.append(bitzerocount)
00245                 lsdata.append(bitzeroprescale)
00246                 lsdata.append(allbitsinfo)
00247                 result[run].append(lsdata)
00248     return result
00249 
00250 def instLumiForIds(schema,irunlsdict,dataidmap,runsummaryMap,beamstatusfilter=None,timeFilter=None,withBXInfo=False,bxAlgo=None,xingMinLum=None,withBeamIntensity=False,lumitype='HF'):
00251     '''
00252     FROM ROOT FILE NO CORRECTION AT ALL 
00253     input:
00254            irunlsdict: {run:[cmsls]} 
00255            dataidmap: {run:(lumiid,trgid,hltid)}
00256            runsummaryMap: {run:[l1key(0),amodetag(1),egev(2),hltkey(3),fillnum(4),fillscheme(5),starttime(6),stoptime(7)]}
00257            beamstatus: LS filter on beamstatus (optional)
00258            timeFilter: (minLSBegTime,maxLSBegTime)
00259            withBXInfo: get per bunch info (optional)
00260            bxAlgo: algoname for bx values (optional) ['OCC1','OCC2','ET']
00261            xingMinLum: None means apply no cut
00262            withBeamIntensity: get beam intensity info (optional)
00263            lumitype: luminosity measurement source
00264     output:
00265            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)]}}
00266            
00267            special meanings:
00268            {run:None}  None means selected run not in lumiDB, 
00269            {run:[]} [] means no lumi data for this run in lumiDB
00270            {run:cmslsnum(1)==0} means either not cmslsnum or iscms but not selected
00271            instlumi unit in Hz/ub
00272     '''
00273     if lumitype not in ['HF','PIXEL']:
00274         raise ValueError('unknown lumitype '+lumitype)
00275     lumitableName=''
00276     lumilstableName=''
00277     if lumitype=='HF':
00278         lumitableName=nameDealer.lumidataTableName()
00279         lumilstableName=nameDealer.lumisummaryv2TableName()
00280     else:
00281         lumitableName=nameDealer.pixellumidataTableName()
00282         lumilstableName=nameDealer.pixellumisummaryv2TableName()
00283     result={}
00284     for run in irunlsdict.keys():
00285     #for run,(lumidataid,trgid,hltid ) in dataidmap.items():
00286         lslist=irunlsdict[run]
00287         if lslist is not None and len(lslist)==0:
00288             result[run]=[]#no lumi data for this run in lumiDB
00289             continue
00290         fillnum=runsummaryMap[run][4]
00291         runstarttimeStr=runsummaryMap[run][6]
00292         if not dataidmap.has_key(run):
00293             result[run]=[]#no lumi data for this run in lumiDB
00294             continue
00295         (lumidataid,trgid,hltid )=dataidmap[run]
00296         if lumidataid is None: #selected run not in lumiDB
00297             result[run]=None
00298             continue
00299         (lumirunnum,perlsresult)=dataDML.lumiLSById(schema,lumidataid,beamstatus=beamstatusfilter,withBXInfo=withBXInfo,bxAlgo=bxAlgo,withBeamIntensity=withBeamIntensity,tableName=lumilstableName)
00300         lsresult=[]
00301         c=lumiTime.lumiTime()
00302         for lumilsnum in perlsresult.keys():
00303             perlsdata=perlsresult[lumilsnum]
00304             cmslsnum=perlsdata[0]
00305             if lslist is not None and cmslsnum not in lslist: #ls exists but not selected
00306                 cmslsnum=0
00307             numorbit=perlsdata[6]
00308             startorbit=perlsdata[7]
00309             orbittime=c.OrbitToTime(runstarttimeStr,startorbit,begorbit=0,customfm='%m/%d/%y %H:%M:%S')
00310             if timeFilter:
00311                 if timeFilter[0]:
00312                     if orbittime<timeFilter[0]: continue
00313                 if timeFilter[1]:
00314                     if orbittime>timeFilter[1]: continue
00315             if lumitype=='HF':
00316                 instlumi=perlsdata[1]*1000.0 #HF db avg values are in Hz/mb,change it to Hz/ub
00317                 instlumierr=perlsdata[2]*1000.0
00318             else:
00319                 instlumi=perlsdata[1] #PIXEL avg values are in Hz/ub, need no conversion
00320                 instlumierr=perlsdata[2]
00321             beamstatus=perlsdata[4]
00322             beamenergy=perlsdata[5]
00323             bxidxlist=[]
00324             bxvaluelist=[]
00325             bxerrorlist=[]
00326             bxdata=None
00327             beamdata=None
00328             if withBXInfo:
00329                 bxinfo=perlsdata[8]                
00330                 bxvalueArray=None
00331                 bxerrArray=None
00332                 if bxinfo:
00333                     bxvalueArray=bxinfo[0]
00334                     bxerrArray=bxinfo[1]
00335                     if xingMinLum :
00336                         for idx,bxval in enumerate(bxvalueArray):
00337                             if bxval>xingMinLum:
00338                                 bxidxlist.append(idx)
00339                                 bxvaluelist.append(bxval)
00340                                 bxerrorlist.append(bxerrArray[idx])
00341                     else:
00342                         bxidxlist=range(0,len(bxvalueArray))
00343                         bxvaluelist=bxvalueArray.tolist()
00344                         bxerrorlist=bxerrArray.tolist()
00345                     del bxvalueArray[:]
00346                     del bxerrArray[:]
00347                 bxdata=(bxidxlist,bxvaluelist,bxerrorlist)
00348             if withBeamIntensity:
00349                 beaminfo=perlsdata[9]
00350                 bxindexlist=[]
00351                 b1intensitylist=[]
00352                 b2intensitylist=[]
00353                 if beaminfo[0] and beaminfo[1] and beaminfo[2]:
00354                     bxindexarray=beaminfo[0]
00355                     beam1intensityarray=beaminfo[1]
00356                     beam2intensityarray=beaminfo[2]                    
00357                     bxindexlist=bxindexarray.tolist()
00358                     b1intensitylist=beam1intensityarray.tolist()
00359                     b2intensitylist=beam2intensityarray.tolist()
00360                     del bxindexarray[:]
00361                     del beam1intensityarray[:]
00362                     del beam2intensityarray[:]                    
00363                 beamdata=(bxindexlist,b1intensitylist,b2intensitylist)
00364             lsresult.append([lumilsnum,cmslsnum,orbittime,beamstatus,beamenergy,instlumi,instlumierr,startorbit,numorbit,bxdata,beamdata,fillnum])         
00365             del perlsdata[:]
00366         result[run]=lsresult
00367     return result
00368 
00369 def deliveredLumiForIds(schema,irunlsdict,dataidmap,runsummaryMap,beamstatusfilter=None,timeFilter=None,normmap=None,withBXInfo=False,bxAlgo=None,xingMinLum=None,withBeamIntensity=False,lumitype='HF',minbiasXsec=None):
00370     '''
00371     delivered lumi (including calibration,time integral)
00372     input:
00373        irunlsdict:  {run:[lsnum]}, where [lsnum]==None means all ; [lsnum]==[] means selected ls
00374        dataidmap : {run:(lumiid,trgid,hltid)}
00375        runsummaryMap: {run:[l1key(0),amodetag(1),egev(2),hltkey(3),fillnum(4),fillscheme(5),starttime(6),stoptime(7)]}
00376        beamstatus: LS filter on beamstatus 
00377        normmap: {since:[corrector(0),{paramname:paramvalue}(1),amodetag(2),egev(3),comment(4)]} if normmap empty, means without-correction , if notnormmap means without-correction
00378        withBXInfo: get per bunch info (optional)
00379        bxAlgo: algoname for bx values (optional) ['OCC1','OCC2','ET']
00380        xingMinLum: cut on bx lumi value (optional)
00381        withBeamIntensity: get beam intensity info (optional)
00382        lumitype: luminosity source
00383     output:
00384        result {run:[lumilsnum(0),cmslsnum(1),timestamp(2),beamstatus(3),beamenergy(4),deliveredlumi(5),calibratedlumierr(6),(bxidxlist,bxvalues,bxerrs)(7),(bxidx,b1intensities,b2intensities)(8),fillnum(9),pu(10)]}
00385        
00386        special meanings:
00387        {run:None}  None means no run in lumiDB, 
00388        {run:[]} [] means no lumi for this run in lumiDB
00389        {run:cmslsnum(1)==0} means either not cmslsnum or iscms but not selected 
00390        lumi unit: /ub
00391     '''
00392     result = {}
00393     lumip=lumiParameters.ParametersObject()
00394     lumirundata=dataDML.lumiRunByIds(schema,dataidmap,lumitype=lumitype)
00395     instresult=instLumiForIds(schema,irunlsdict,dataidmap,runsummaryMap,beamstatusfilter=beamstatusfilter,timeFilter=timeFilter,withBXInfo=withBXInfo,bxAlgo=bxAlgo,withBeamIntensity=withBeamIntensity,lumitype=lumitype)
00396     
00397     intglumimap={}
00398     if lumitype=='HF':
00399         intglumimap=dataDML.intglumiForRange(schema,irunlsdict.keys())#some runs need drift correction
00400     allsince=[]
00401     if normmap:
00402         allsince=normmap.keys()
00403         allsince.sort()        
00404     correctorname='fPoly' #HF default
00405     correctionparams={'a0':1.0}
00406     runfillschemeMap={}
00407     fillschemePatternMap={}
00408     if lumitype=='PIXEL':
00409         correctorname='fPolyScheme' #PIXEL default
00410         fillschemePatternMap=dataDML.fillschemePatternMap(schema,'PIXEL')
00411     for run,perrundata in instresult.items():
00412         if perrundata is None:
00413             result[run]=None
00414             continue
00415         intglumi=0.
00416         if normmap and intglumimap and intglumimap.has_key(run) and intglumimap[run]:
00417             intglumi=intglumimap[run]
00418         nBXs=0
00419         if normmap and lumirundata and lumirundata.has_key(run) and lumirundata[run][2]:
00420             nBXs=lumirundata[run][2]
00421         fillschemeStr=''
00422         if normmap and runsummaryMap and runsummaryMap.has_key(run) and runsummaryMap[run][5]:
00423             fillschemeStr=runsummaryMap[run][5]
00424         if allsince:
00425             lastsince=allsince[0]
00426             for since in allsince:
00427                 if run>=since:
00428                     lastsince=since
00429             correctorname=normmap[lastsince][0]
00430             correctionparams=normmap[lastsince][1]
00431             
00432         correctioninput=[0.,intglumi,nBXs,fillschemeStr,fillschemePatternMap]
00433         result[run]=[]
00434         for perlsdata in perrundata:#loop over ls
00435             lumilsnum=perlsdata[0]
00436             cmslsnum=perlsdata[1]
00437             timestamp=perlsdata[2]
00438             bs=perlsdata[3]
00439             beamenergy=perlsdata[4]
00440             instluminonorm=perlsdata[5]
00441             correctioninput[0]=instluminonorm
00442             totcorrectionFac=normFunctors.normFunctionCaller(correctorname,*correctioninput,**correctionparams)
00443             fillnum=perlsdata[11]
00444             instcorrectedlumi=totcorrectionFac*instluminonorm
00445             numorbit=perlsdata[8]
00446             numbx=lumip.NBX
00447             lslen=lumip.lslengthsec()
00448             deliveredlumi=instcorrectedlumi*lslen
00449             calibratedbxdata=None
00450             beamdata=None
00451             pu=0.#avgPU
00452             if nBXs and minbiasXsec:
00453                 pu=(instcorrectedlumi/nBXs)*minbiasXsec/lumip.rotationRate                
00454             if withBXInfo:                
00455                 (bxidxData,bxvaluesData,bxerrsData)=perlsdata[9]
00456                 if lumitype=='HF':
00457                     if xingMinLum:
00458                         bxidxList=[]
00459                         bxvalueList=[]
00460                         bxerrList=[]
00461                         for idx,bxval in enumerate(bxvaluesData):
00462                             correctedbxintlumi=totcorrectionFac*bxval
00463                             correctedbxintlumierr=totcorrectionFac*bxerrsData[idx]
00464                             if correctedbxintlumi>xingMinLum:
00465                                 bxidxList.append(bxidxData[idx])
00466                                 bxvalueList.append(correctedbxintlumi)
00467                                 bxerrList.append(correctedbxintlumierr)
00468                         calibratedbxdata=(bxidxList,bxvalueList,bxerrList)
00469                     else:
00470                         calibratedbxvalue=[totcorrectionFac*x for x in bxvaluesData]
00471                         calibratedlumierr=[totcorrectionFac*x for x in bxerrsData]
00472                         calibratedbxdata=(bxidxData,calibratedbxvalue,calibratedlumierr)
00473             if withBeamIntensity:
00474                 beamdata=perlsdata[10]
00475             calibratedlumierr=0.0
00476             result[run].append([lumilsnum,cmslsnum,timestamp,bs,beamenergy,deliveredlumi,calibratedlumierr,calibratedbxdata,beamdata,fillnum,pu])
00477             del perlsdata[:]
00478     return result
00479 
00480 def lumiForIds(schema,irunlsdict,dataidmap,runsummaryMap,beamstatusfilter=None,timeFilter=None,normmap=None,withBXInfo=False,bxAlgo=None,xingMinLum=None,withBeamIntensity=False,lumitype='HF',minbiasXsec=None):
00481     '''
00482     delivered/recorded lumi  (including calibration,time integral)
00483     input:
00484        irunlsdict:  {run:[lsnum]}, where [lsnum]==None means all ; [lsnum]==[] means no selected ls
00485        dataidmap : {run:(lumiid,trgid,hltid)}
00486        runsummaryMap: {run:[l1key(0),amodetag(1),egev(2),hltkey(3),fillnum(4),fillscheme(5),starttime(6),stoptime(7)]}
00487        beamstatus: LS filter on beamstatus 
00488        normmap: 
00489        withBXInfo: get per bunch info (optional)
00490        bxAlgo: algoname for bx values (optional) ['OCC1','OCC2','ET']
00491        xingMinLum: cut on bx lumi value (optional)
00492        withBeamIntensity: get beam intensity info (optional)
00493        lumitype: luminosity source
00494     output:
00495        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),ncollidingbunches(11)]...]}
00496        special meanings:
00497        {run:None}  None means no run in lumiDB, 
00498        {run:[]} [] means no lumi for this run in lumiDB
00499        {run:[....deliveredlumi(5),recordedlumi(6)None]} means no trigger in lumiDB
00500        {run:cmslsnum(1)==0} means either not cmslsnum or is cms but not selected, therefore set recordedlumi=0,efflumi=0
00501        lumi unit: 1/ub
00502     '''
00503     deliveredresult=deliveredLumiForIds(schema,irunlsdict,dataidmap,runsummaryMap,beamstatusfilter=beamstatusfilter,timeFilter=timeFilter,normmap=normmap,withBXInfo=withBXInfo,bxAlgo=bxAlgo,xingMinLum=xingMinLum,withBeamIntensity=withBeamIntensity,lumitype=lumitype,minbiasXsec=minbiasXsec)
00504     trgresult=trgForIds(schema,irunlsdict,dataidmap)
00505     for run in deliveredresult.keys():#loop over delivered,already selected
00506         perrundata=deliveredresult[run]
00507         if perrundata is None or len(perrundata)==0: #pass through 
00508             continue
00509         alltrgls=[]
00510         if trgresult.has_key(run) and trgresult[run]:
00511             alltrgls=[x[0] for x in trgresult[run]]
00512         for perlsdata in perrundata:#loop over ls
00513             if not perlsdata: continue #no lumi data for this ls
00514             perlsdata.insert(6,None)
00515             if not alltrgls: continue  #no trg for this run,recorded=None 
00516             cmslsnum=perlsdata[1]
00517             if cmslsnum==0:#if not a cmsls or not selected by cms list, set recordedlumi to 0
00518                 recordedlumi=0.0
00519             else:
00520                 try:
00521                     trglsidx=alltrgls.index(cmslsnum)
00522                     deadfrac=trgresult[run][trglsidx][1]
00523                     if deadfrac<0 or deadfrac>1.0: deadfrac=1.0
00524                     deliveredlumi=perlsdata[5]
00525                     recordedlumi=(1.0-deadfrac)*deliveredlumi
00526                 except ValueError:
00527                     #print '[WARNING] no trigger for LS=',cmslsnum
00528                     recordedlumi=None
00529             perlsdata[6]=recordedlumi
00530     return deliveredresult
00531 
00532 def effectiveLumiForIds(schema,irunlsdict,dataidmap,runsummaryMap=None,beamstatusfilter=None,timeFilter=None,normmap=None,hltpathname=None,hltpathpattern=None,withBXInfo=False,bxAlgo=None,xingMinLum=None,withBeamIntensity=False,lumitype='HF',minbiasXsec=None):
00533     '''
00534     delivered/recorded/eff lumi in selected hlt path  (including calibration,time integral)
00535     input:
00536            irunlsdict: {run:[lsnum]}, where [lsnum]==None means all ; [lsnum]==[] means selected ls
00537            dataidmap : {run:(lumiid,trgid,hltid)}
00538            runsummaryMap: {run:[l1key(0),amodetag(1),egev(2),hltkey(3),fillnum(4),fillscheme(5),starttime(6),stoptime(7)]}
00539            beamstatusfilter: LS filter on beamstatus
00540            normmap: {since:[corrector(0),{paramname:paramvalue}(1),amodetag(2),egev(3),comment(4)]} if normmap empty, means without-correction , if notnormmap means without-correction
00541            hltpathname: selected hltpathname
00542            hltpathpattern: regex select hltpaths           
00543            withBXInfo: get per bunch info (optional)
00544            bxAlgo: algoname for bx values (optional) ['OCC1','OCC2','ET']
00545            xingMinLum: cut on bx lumi value (optional)
00546            withBeamIntensity: get beam intensity info (optional)
00547            lumitype: luminosity source
00548     output:
00549            result {run:[lumilsnum(0),cmslsnum(1),timestamp(2),beamstatus(3),beamenergy(4),deliveredlumi(5),recordedlumi(6),calibratedlumierror(7),{hltpath:[l1name,l1prescale,hltprescale,efflumi]}(8),bxdata(9),beamdata(10),fillnum(11),ncollidingbunches(12)]}
00550            {run:None}  None means no run in lumiDB, 
00551            {run:[]} [] means no lumi for this run in lumiDB
00552            {run:[....deliveredlumi(5),recorded(6)==None,]} means no trigger in lumiDB
00553            {run:[....deliveredlumi(5),recorded(6),calibratedlumierror(7)==None]} means no hlt in lumiDB
00554            
00555            lumi unit: 1/ub
00556     '''
00557     deliveredresult=deliveredLumiForIds(schema,irunlsdict,dataidmap,runsummaryMap,beamstatusfilter=beamstatusfilter,timeFilter=timeFilter,normmap=normmap,withBXInfo=withBXInfo,bxAlgo=bxAlgo,xingMinLum=xingMinLum,withBeamIntensity=withBeamIntensity,lumitype=lumitype,minbiasXsec=minbiasXsec)
00558     trgresult=trgForIds(schema,irunlsdict,dataidmap,withPrescale=True) #{run:[cmslsnum,deadfrac,deadtimecount,bitzero_count,bitzero_prescale,[(bitname,prescale,counts,mask)]]}
00559     hltresult=hltForIds(schema,irunlsdict,dataidmap,hltpathname=hltpathname,hltpathpattern=hltpathpattern,withL1Pass=False,withHLTAccept=False) #{runnumber:[(cmslsnum,[(hltpath,hltprescale,l1pass,hltaccept),...]),(cmslsnum,[])})}
00560     for run in deliveredresult.keys(): #loop over delivered
00561         perrundata=deliveredresult[run]
00562         if perrundata is None or len(perrundata)==0:#pass through 
00563             continue
00564         alltrgls=[]
00565         if trgresult.has_key(run) and trgresult[run]:
00566             alltrgls=[x[0] for x in trgresult[run]]
00567         allhltls=[]
00568         if hltresult.has_key(run) and hltresult[run]:
00569             allhltls=[x[0] for x in hltresult[run]]            
00570         l1bitinfo=[]
00571         hltpathinfo=[]
00572         hlttrgmap=dataDML.hlttrgMappingByrun(schema,run,hltpathname=hltpathname,hltpathpattern=hltpathpattern)
00573         
00574         for perlsdata in perrundata: #loop over ls
00575             if not perlsdata: continue #no lumi for this ls
00576             perlsdata.insert(6,None)
00577             perlsdata.insert(8,None)
00578             if not alltrgls: continue  #no trg for this run
00579             cmslsnum=perlsdata[1]
00580             recordedlumi=0.0
00581             if cmslsnum==0:#if not a cmsls or not selected by cms list, set recordedlumi,efflumi to 0
00582                 continue
00583             else:
00584                 try:
00585                     trglsidx=alltrgls.index(cmslsnum)
00586                     deadfrac=trgresult[run][trglsidx][1]
00587                     l1bitinfo=trgresult[run][trglsidx][5]
00588                     if deadfrac<0 or deadfrac>1.0:deadfrac=1.0
00589                     deliveredlumi=perlsdata[5]
00590                     recordedlumi=(1.0-deadfrac)*deliveredlumi
00591                 except ValueError:
00592                     #print '[WARNING] no trigger for LS=',cmslsnum
00593                     continue #do not go further
00594             perlsdata[6]=recordedlumi
00595             if not allhltls: continue #no hlt for this run
00596             try:
00597                 hltlsidx=allhltls.index(cmslsnum)
00598             except ValueError:
00599                 #print '[WARNING] no hlt for LS=',cmslsnum
00600                 continue #do not go further
00601             trgprescalemap={} #{bitname:l1prescale} for this lumi section
00602             if l1bitinfo:
00603                 for thisbitinfo in l1bitinfo:
00604                     thisbitname=thisbitinfo[0]
00605                     thisbitprescale=thisbitinfo[2]
00606                     trgprescalemap['"'+thisbitname+'"']=thisbitprescale
00607             else:
00608                 continue
00609             hltpathdata=hltresult[run][hltlsidx][1]
00610             efflumidict={}#{pathname:[[l1bitname,l1prescale,hltprescale,efflumi]]}       
00611             for pathidx,thispathinfo in enumerate(hltpathdata):
00612                 thispathname=thispathinfo[0]
00613                 thisprescale=thispathinfo[1]
00614                 thisl1seed=None
00615                 l1bitname=None
00616                 l1prescale=None
00617                 try:
00618                     thisl1seed=hlttrgmap[thispathname]
00619                 except KeyError:
00620                     thisl1seed=None
00621                 if thisl1seed:
00622                     try:
00623                         (exptype,l1bits)=hltTrgSeedMapper.findUniqueSeed(thispathname,thisl1seed)
00624                         if l1bits:
00625                             if not exptype:
00626                                 l1bitname=l1bits[0]
00627                                 l1prescale=trgprescalemap[l1bits[0]]#need to match double quoted string!                                
00628                             else:
00629                                 pmin=99999999
00630                                 pmax=0                                
00631                                 for bit in l1bits:
00632                                     l1p=trgprescalemap[bit]
00633                                     if exptype=='OR':
00634                                         if l1p!=0 and l1p<pmin:
00635                                             pmin=l1p
00636                                             l1prescale=l1p
00637                                             l1bitname=bit
00638                                     if exptype=='AND':
00639                                         if l1p!=0 and l1p>pmax:
00640                                             pmax=l1p
00641                                             l1prescale=l1p
00642                                             l1bitname=bit
00643                         else:
00644                             l1prescale=None
00645                     except KeyError:
00646                         l1prescale=None
00647             
00648                 efflumi=0.0
00649                 if l1prescale and thisprescale:#normal both prescaled
00650                     efflumi=recordedlumi/(float(l1prescale)*float(thisprescale))
00651                     efflumidict[thispathname]=[l1bitname,l1prescale,thisprescale,efflumi]
00652                 elif l1prescale and thisprescale==0: #hltpath in menu but masked
00653                     efflumidict[thispathname]=[l1bitname,l1prescale,thisprescale,efflumi]
00654                 else:
00655                     efflumidict[thispathname]=[None,0,thisprescale,efflumi]
00656             perlsdata[8]=efflumidict
00657     return deliveredresult
00658