示例#1
0
    runs = dataDML.runList(schema,
                           fillnum=1616,
                           runmin=160403,
                           runmax=160957,
                           nominalEnergy=3500.0)
    print(runs)
    myrun = runs[0]
    runsummary = dataDML.runsummary(schema, myrun)
    print(runsummary)
    normid = dataDML.guessnormIdByContext(schema, 'PROTPHYS', 3500)
    normval = dataDML.luminormById(schema, normid)[2]
    print('norm in use ', normval)
    (lumidataid, trgdataid,
     hltdataid) = dataDML.guessDataIdByRun(schema, myrun)
    print('all dataids ', lumidataid, trgdataid, hltdataid)
    (runnum, lumiLSdata) = dataDML.lumiLSById(schema, lumidataid)

    lumils = sorted(lumiLSdata.keys())
    for lsnum in lumils:
        print('lumilsnum,cmslsnum,instlumi ', lsnum, lumiLSdata[lsnum][0],
              lumiLSdata[lsnum][1] * normval)

    (runnum, trgLSdata) = dataDML.trgLSById(schema, trgdataid)
    cmsls = trgLSdata.keys()
    cmsls.sort()
    for lsnum in cmsls:
        print('cmslsnum,deadtime,bizerocount,bitzeroprescale,deadfrac ', lsnum,
              trgLSdata[lsnum][0], trgLSdata[lsnum][1], trgLSdata[lsnum][2],
              trgLSdata[lsnum][3])
    [runnum, datasource, npath,
     pathnames] = dataDML.hltRunById(schema, hltdataid)
示例#2
0
def instLumiForIds(schema,irunlsdict,dataidmap,runsummaryMap,beamstatusfilter=None,timeFilter=None,withBXInfo=False,bxAlgo=None,xingMinLum=None,withBeamIntensity=False,lumitype='HF'):
    '''
    FROM ROOT FILE NO CORRECTION AT ALL 
    input:
           irunlsdict: {run:[cmsls]} 
           dataidmap: {run:(lumiid,trgid,hltid)}
           runsummaryMap: {run:[l1key(0),amodetag(1),egev(2),hltkey(3),fillnum(4),fillscheme(5),starttime(6),stoptime(7)]}
           beamstatus: LS filter on beamstatus (optional)
           timeFilter: (minLSBegTime,maxLSBegTime)
           withBXInfo: get per bunch info (optional)
           bxAlgo: algoname for bx values (optional) ['OCC1','OCC2','ET']
           xingMinLum: None means apply no cut
           withBeamIntensity: get beam intensity info (optional)
           lumitype: luminosity measurement source
    output:
           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)]}}
           
           special meanings:
           {run:None}  None means selected run not in lumiDB, 
           {run:[]} [] means no lumi data for this run in lumiDB
           {run:cmslsnum(1)==0} means either not cmslsnum or iscms but not selected
           instlumi unit in Hz/ub
    '''
    if lumitype not in ['HF','PIXEL']:
        raise ValueError('unknown lumitype '+lumitype)
    lumitableName=''
    lumilstableName=''
    if lumitype=='HF':
        lumitableName=nameDealer.lumidataTableName()
        lumilstableName=nameDealer.lumisummaryv2TableName()
    else:
        lumitableName=nameDealer.pixellumidataTableName()
        lumilstableName=nameDealer.pixellumisummaryv2TableName()
    result={}
    for run in irunlsdict.keys():
    #for run,(lumidataid,trgid,hltid ) in dataidmap.items():
        lslist=irunlsdict[run]
        if lslist is not None and len(lslist)==0:
            result[run]=[]#no lumi data for this run in lumiDB
            continue
        fillnum=runsummaryMap[run][4]
        runstarttimeStr=runsummaryMap[run][6]
        if run not in dataidmap:
            result[run]=[]#no lumi data for this run in lumiDB
            continue
        (lumidataid,trgid,hltid )=dataidmap[run]
        if lumidataid is None: #selected run not in lumiDB
            result[run]=None
            continue
        (lumirunnum,perlsresult)=dataDML.lumiLSById(schema,lumidataid,beamstatus=beamstatusfilter,withBXInfo=withBXInfo,bxAlgo=bxAlgo,withBeamIntensity=withBeamIntensity,tableName=lumilstableName)
        lsresult=[]
        c=lumiTime.lumiTime()
        for lumilsnum in perlsresult.keys():
            perlsdata=perlsresult[lumilsnum]
            cmslsnum=perlsdata[0]
            if lslist is not None and cmslsnum not in lslist: #ls exists but not selected
                cmslsnum=0
            numorbit=perlsdata[6]
            startorbit=perlsdata[7]
            orbittime=c.OrbitToTime(runstarttimeStr,startorbit,begorbit=0,customfm='%m/%d/%y %H:%M:%S')
            if timeFilter:
                if timeFilter[0]:
                    if orbittime<timeFilter[0]: continue
                if timeFilter[1]:
                    if orbittime>timeFilter[1]: continue
            if lumitype=='HF':
                instlumi=perlsdata[1]*1000.0 #HF db avg values are in Hz/mb,change it to Hz/ub
                instlumierr=perlsdata[2]*1000.0
            else:
                instlumi=perlsdata[1] #PIXEL avg values are in Hz/ub, need no conversion
                instlumierr=perlsdata[2]
            beamstatus=perlsdata[4]
            beamenergy=perlsdata[5]
            bxidxlist=[]
            bxvaluelist=[]
            bxerrorlist=[]
            bxdata=None
            beamdata=None
            if withBXInfo:
                bxinfo=perlsdata[8]                
                bxvalueArray=None
                bxerrArray=None
                if bxinfo:
                    bxvalueArray=bxinfo[0]
                    bxerrArray=bxinfo[1]
                    if xingMinLum :
                        for idx,bxval in enumerate(bxvalueArray):
                            if bxval>xingMinLum:
                                bxidxlist.append(idx)
                                bxvaluelist.append(bxval)
                                bxerrorlist.append(bxerrArray[idx])
                    else:
                        bxidxlist=range(0,len(bxvalueArray))
                        bxvaluelist=bxvalueArray.tolist()
                        bxerrorlist=bxerrArray.tolist()
                    del bxvalueArray[:]
                    del bxerrArray[:]
                bxdata=(bxidxlist,bxvaluelist,bxerrorlist)
            if withBeamIntensity:
                beaminfo=perlsdata[9]
                bxindexlist=[]
                b1intensitylist=[]
                b2intensitylist=[]
                if beaminfo[0] and beaminfo[1] and beaminfo[2]:
                    bxindexarray=beaminfo[0]
                    beam1intensityarray=beaminfo[1]
                    beam2intensityarray=beaminfo[2]                    
                    bxindexlist=bxindexarray.tolist()
                    b1intensitylist=beam1intensityarray.tolist()
                    b2intensitylist=beam2intensityarray.tolist()
                    del bxindexarray[:]
                    del beam1intensityarray[:]
                    del beam2intensityarray[:]                    
                beamdata=(bxindexlist,b1intensitylist,b2intensitylist)
            lsresult.append([lumilsnum,cmslsnum,orbittime,beamstatus,beamenergy,instlumi,instlumierr,startorbit,numorbit,bxdata,beamdata,fillnum])         
            del perlsdata[:]
        result[run]=lsresult
    return result
示例#3
0
 myconstr='oracle://cms_orcoff_prod/cms_lumi_prod'
 svc=sessionManager.sessionManager(myconstr,authpath='/afs/cern.ch/user/x/xiezhen',debugON=False)
 session=svc.openSession(isReadOnly=True,cpp2sqltype=[('unsigned int','NUMBER(10)'),('unsigned long long','NUMBER(20)')])
 session.transaction().start(True)
 schema=session.nominalSchema()
 runs=dataDML.runList(schema,fillnum=1616,runmin=160403,runmax=160957,nominalEnergy=3500.0)
 print runs
 myrun=runs[0]
 runsummary=dataDML.runsummary(schema,myrun)
 print runsummary
 normid=dataDML.guessnormIdByContext(schema,'PROTPHYS',3500)
 normval=dataDML.luminormById(schema,normid)[2]
 print 'norm in use ',normval
 (lumidataid,trgdataid,hltdataid)=dataDML.guessDataIdByRun(schema,myrun)
 print 'all dataids ',lumidataid,trgdataid,hltdataid
 (runnum,lumiLSdata)=dataDML.lumiLSById(schema,lumidataid)
 
 lumils=lumiLSdata.keys()
 lumils.sort()
 for lsnum in lumils:
     print 'lumilsnum,cmslsnum,instlumi ',lsnum,lumiLSdata[lsnum][0],lumiLSdata[lsnum][1]*normval
     
 (runnum,trgLSdata)=dataDML.trgLSById(schema,trgdataid)
 cmsls=trgLSdata.keys()
 cmsls.sort()
 for lsnum in cmsls:
     print 'cmslsnum,deadtime,bizerocount,bitzeroprescale,deadfrac ',lsnum,trgLSdata[lsnum][0],trgLSdata[lsnum][1],trgLSdata[lsnum][2],trgLSdata[lsnum][3]
 [runnum,datasource,npath,pathnames]=dataDML.hltRunById(schema,hltdataid)
 print 'npath,pathnames ',npath,pathnames
 pathnameList=pathnames.split(',')
 (runnum,hltLSdata)=dataDML.hltLSById(schema,hltdataid)
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):
    '''
    input:
           inputRange  {run:[cmsls]} (required)
           hltpathname: selected hltpathname
           hltpathpattern: regex select hltpaths           
           amodetag: amodetag for  picking norm(optional)
           egev: beamenergy for picking norm(optional)
           withBXInfo: get per bunch info (optional)
           bxAlgo: algoname for bx values (optional) ['OCC1','OCC2','ET']
           xingMinLum: cut on bx lumi value (optional)
           withBeamIntensity: get beam intensity info (optional)
           normname: norm factor name to use (optional)
           branchName: data version
    output:
    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]}
           lumi unit: 1/ub
    '''
    if lumitype not in ['HF','PIXEL']:
        raise ValueError('unknown lumitype '+lumitype)
    if branchName is None:
        branchName='DATA'
    lumitableName=''
    lumilstableName=''
    if lumitype=='HF':
        lumitableName=nameDealer.lumidataTableName()
        lumilstableName=nameDealer.lumisummaryv2TableName()
    else:
        lumitableName=nameDealer.pixellumidataTableName()
        lumilstableName=nameDealer.pixellumisummaryv2TableName()
    numbx=3564
    result = {}
    normval=None
    perbunchnormval=None
    if norm:
        normval=_getnorm(schema,norm)
        perbunchnormval=float(normval)/float(1000)
    elif amodetag and egev:
        normval=_decidenormFromContext(schema,amodetag,egev)
        perbunchnormval=float(normval)/float(1000)
    c=lumiTime.lumiTime()
    lumip=lumiParameters.ParametersObject()
    for run in inputRange.keys():
        lslist=inputRange[run]
        if lslist is not None and len(lslist)==0:#no selected ls, do nothing for this run
            result[run]=[]
            continue
        cmsrunsummary=dataDML.runsummary(schema,run)
        if len(cmsrunsummary)==0:#non existing run
            result[run]=None
            continue
        startTimeStr=cmsrunsummary[6]
        fillnum=cmsrunsummary[4]
        lumidataid=None
        trgdataid=None
        hltdataid=None
        lumidataid=dataDML.guessLumiDataIdByRun(schema,run,lumitableName)
        trgdataid=dataDML.guessTrgDataIdByRun(schema,run)
        hltdataid=dataDML.guessHltDataIdByRun(schema,run)
        if lumidataid is None or trgdataid is None or hltdataid is None:
            result[run]=None
            continue
        (lumirunnum,lumidata)=dataDML.lumiLSById(schema,lumidataid,beamstatus,tableName=lumilstableName)
        (trgrunnum,trgdata)=dataDML.trgLSById(schema,trgdataid,withPrescale=True)
        (hltrunnum,hltdata)=dataDML.hltLSById(schema,hltdataid,hltpathname=hltpathname,hltpathpattern=hltpathpattern)
        hlttrgmap=dataDML.hlttrgMappingByrun(schema,run)
        if not normval:#if norm cannot be decided , look for it according to context per run
            normval=_decidenormForRun(schema,run)
            perbunchnormval=float(normval)/float(1000)
        if not normval:#still not found? resort to global default (should never come here)
            normval=6370
            perbunchnormval=6.37
            print '[Warning] using default normalization '+str(normval)

        perrunresult=[]
        for lumilsnum,perlsdata in lumidata.items():
            cmslsnum=perlsdata[0]            
            triggeredls=perlsdata[0] 
            if lslist is not None and cmslsnum not in lslist:
                #cmslsnum=0
                triggeredls=0
                recordedlumi=0.0
            instlumi=perlsdata[1]
            instlumierror=perlsdata[2]
            avglumi=instlumi*normval
            calibratedlumi=avglumi 
            if lumitype=='HF' and finecorrections and finecorrections[run]:
                if usecorrectionv2:
                    if driftcorrections and driftcorrections[run]:
                        calibratedlumi=lumiCorrections.applyfinecorrectionV2(avglumi,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],driftcorrections[run])
                    else:
                        calibratedlumi=lumiCorrections.applyfinecorrectionV2(avglumi,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],1.0)
                else:
                    calibratedlumi=lumiCorrections.applyfinecorrection(avglumi,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2])
            if lumitype=='PIXEL' and finecorrections is not None:
                calibratedlumi=finecorrections[run]*avglumi
            calibratedlumierror=instlumierror*normval
            bstatus=perlsdata[4]
            begev=perlsdata[5]
            numorbit=perlsdata[6]
            startorbit=perlsdata[7]
            timestamp=c.OrbitToTime(startTimeStr,startorbit,0)
            lslen=lumip.lslengthsec()
            deliveredlumi=calibratedlumi*lslen
            recordedlumi=0.0
            trgprescalemap={}#trgprescalemap for this ls
            efflumidict={}
            if triggeredls!=0:
                if not trgdata.has_key(cmslsnum):
                    #triggeredls=0 #if no trigger, set back to non-cms-active ls
                    recordedlumi=0.0 # no trigger->nobeam recordedlumi=None
                else:
                    deadcount=trgdata[cmslsnum][0] ##subject to change !!
                    bitzerocount=trgdata[cmslsnum][1]
                    bitzeroprescale=trgdata[cmslsnum][2]
                    deadfrac=trgdata[cmslsnum][3]
                    if deadfrac<0 or deadfrac>1.0:
                        deadfrac=1.0
                    #if float(bitzerocount)*float(bitzeroprescale)==0.0:
                    #    deadfrac=1.0
                    #else:
                    #    deadfrac=float(deadcount)/(float(bitzerocount)*float(bitzeroprescale))
                    #if deadfrac>1.0:
                    #    deadfrac=1.0  #artificial correction in case of deadfrac>1
                    recordedlumi=deliveredlumi*(1.0-deadfrac)
                    l1bitinfo=trgdata[cmslsnum][4]
                    if l1bitinfo:
                        for thisbitinfo in l1bitinfo:
                            thisbitname=thisbitinfo[0]
                            thisbitprescale=thisbitinfo[2]
                            #trgprescalemap['"'+thisbitname+'"']=thisbitprescale#note:need to double quote bit name!
                            trgprescalemap['"'+thisbitname+'"']=thisbitprescale

                    del trgdata[cmslsnum][:]
                if hltdata.has_key(cmslsnum):                
                    hltpathdata=hltdata[cmslsnum]
                    #print 'hltpathdata ',hltpathdata
                    for pathidx,thispathinfo in enumerate(hltpathdata):
                        efflumi=0.0                    
                        thispathname=thispathinfo[0]
                        thisprescale=thispathinfo[1]
                        thisl1seed=None
                        l1bitname=None
                        l1prescale=None
                        try:
                            thisl1seed=hlttrgmap[thispathname]
                        except KeyError:
                            thisl1seed=None
                            # print 'hltpath, l1seed, hltprescale ',thispathname,thisl1seed,thisprescale
                        if thisl1seed:                            
                            try:
                                l1bitname=hltTrgSeedMapper.findUniqueSeed(thispathname,thisl1seed)
                                if l1bitname :
                                    l1prescale=trgprescalemap[l1bitname]#need to match double quoted string!
                                else:
                                    l1prescale=None
                            except KeyError:
                                l1prescale=None                           
                        if l1prescale and thisprescale :#normal both prescaled
                            efflumi=recordedlumi/(float(l1prescale)*float(thisprescale))
                            efflumidict[thispathname]=[l1bitname,l1prescale,thisprescale,efflumi]
                        elif l1prescale and thisprescale==0: #hltpath in menu but masked
                            efflumi=0.0
                            efflumidict[thispathname]=[l1bitname,l1prescale,thisprescale,efflumi]
                        else:
                            efflumi=0.0
                            efflumidict[thispathname]=[None,0,thisprescale,efflumi]
                
            bxvaluelist=[]
            bxerrorlist=[]
            bxdata=None
            beamdata=None
            if withBXInfo:
                bxinfo=lumidata[8]
                bxvalueArray=None
                bxerrArray=None
                if bxinfo:
                    bxvalueArray=bxinfo[0]
                    bxerrArray=bxinfo[1]
                    for idx,bxval in enumerate(bxvalueArray):
                        if finecorrections and finecorrections[run]:
                            if usecorrectionv2:
                                if driftcorrections and driftcorrections[run]:
                                    mybxval=lumiCorrections.applyfinecorrectionBXV2(bxval,avglumi,perbunchnormval,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],driftcorrections[run])
                                else:
                                    mybxval=lumiCorrections.applyfinecorrectionBXV2(bxval,avglumi,perbunchnormval,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],1.0)
                            else:
                                mybxval=lumiCorrections.applyfinecorrectionBX(bxval,avglumi,perbunchnormval,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2])
                        else:
                            mybxval=bxval*perbunchnormval
                        if mybxval>xingMinLum:
                            bxidxlist.append(idx)
                            bxvaluelist.append(bxval)
                            bxerrorlist.append(bxerrArray[idx])
                    del bxvalueArray[:]
                    del bxerrArray[:]
                bxdata=(bxidxlist,bxvaluelist,bxerrorlist)    
            if withBeamIntensity:
                beaminfo=perlsdata[9]
                bxindexlist=[]
                b1intensitylist=[]
                b2intensitylist=[]
                if beaminfo:
                    bxindexarray=beaminfo[0]
                    beam1intensityarray=beaminfo[1]
                    beam2intensityarray=beaminfo[2]                    
                    bxindexlist=bxindexarray.tolist()
                    b1intensitylist=beam1intensityarray.tolist()
                    b2intensitylist=beam2intensityarray.tolist()
                    del bxindexarray[:]
                    del beam1intensityarray[:]
                    del beam2intensityarray[:]
                beamdata=(bxindexlist,b1intensitylist,b2intensitylist)
#            print cmslsnum,deliveredlumi,recordedlumi,efflumidict
            perrunresult.append([lumilsnum,triggeredls,timestamp,bstatus,begev,deliveredlumi,recordedlumi,calibratedlumierror,efflumidict,bxdata,beamdata,fillnum])
            del perlsdata[:]
        result[run]=perrunresult
    #print result
    return result
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):
    '''
    delivered/recorded lumi
    input:
           inputRange  {run:[cmsls]} (required)
           beamstatus: LS filter on beamstatus (optional)
           amodetag: amodetag for  picking norm(optional)
           egev: beamenergy for picking norm(optional)
           withBXInfo: get per bunch info (optional)
           bxAlgo: algoname for bx values (optional) ['OCC1','OCC2','ET']
           xingMinLum: cut on bx lumi value (optional)
           withBeamIntensity: get beam intensity info (optional)
           normname: norm factor name to use (optional)
           branchName: data version
    output:
           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)]}
           lumi unit: 1/ub
    '''
    if lumitype not in ['HF','PIXEL']:
        raise ValueError('unknown lumitype '+lumitype)
    #if branchName is None:
    #    branchName='DATA'
    lumip=lumiParameters.ParametersObject()
    lumitableName=''
    lumilstableName=''
    if lumitype=='HF':
        lumitableName=nameDealer.lumidataTableName()
        lumilstableName=nameDealer.lumisummaryv2TableName()
    else:
        lumitableName=nameDealer.pixellumidataTableName()
        lumilstableName=nameDealer.pixellumisummaryv2TableName()
    numbx=lumip.NBX
    result = {}
    normval=None
    perbunchnormval=None
    if norm:
        normval=_getnorm(schema,norm)
        perbunchnormval=float(normval)/float(1000)
    elif amodetag and egev:
        normval=_decidenormFromContext(schema,amodetag,egev)
        perbunchnormval=float(normval)/float(1000)
    c=lumiTime.lumiTime()
    for run in inputRange.keys():#loop over run
        lslist=inputRange[run]
        if lslist is not None and len(lslist)==0:#no selected ls, do nothing for this run
            result[run]=[]
            continue
        cmsrunsummary=dataDML.runsummary(schema,run)
        if len(cmsrunsummary)==0:#non existing run
            result[run]=None
            continue
        startTimeStr=cmsrunsummary[6]
        fillnum=cmsrunsummary[4]
        lumidataid=None
        trgdataid=None
        lumidataid=dataDML.guessLumiDataIdByRun(schema,run,lumitableName)
        if lumidataid is None :
            result[run]=None
            continue
        trgdataid=dataDML.guessTrgDataIdByRun(schema,run)
        (lumirunnum,lumidata)=dataDML.lumiLSById(schema,lumidataid,beamstatus=beamstatus,withBXInfo=withBXInfo,bxAlgo=bxAlgo,withBeamIntensity=withBeamIntensity,tableName=lumilstableName)
        if trgdataid is None :
            trgdata={}
        else:
            (trgrunnum,trgdata)=dataDML.trgLSById(schema,trgdataid)
            
        if not normval:#if norm cannot be decided , look for it according to context per run
            normval=_decidenormForRun(schema,run)
            perbunchnormval=float(normval)/float(1000)
        if not normval:#still not found? resort to global default (should never come here)
            normval=6370
            perbunchnormval=6.37
            print '[Warning] using default normalization '+str(normval)
        
        perrunresult=[]
        for lumilsnum,perlsdata in lumidata.items():
            cmslsnum=perlsdata[0]
            triggeredls=perlsdata[0]
            if lslist is not None and cmslsnum not in lslist:
                #cmslsnum=0
                triggeredls=0
                recordedlumi=0.0
            instlumi=perlsdata[1]
            instlumierror=perlsdata[2]
            avglumi=instlumi*normval
            calibratedlumi=avglumi
            if lumitype=='HF' and finecorrections and finecorrections[run]:
                if usecorrectionv2:
                    if driftcorrections and driftcorrections[run]:
                        calibratedlumi=lumiCorrections.applyfinecorrectionV2(avglumi,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],driftcorrections[run])
                    else:
                        calibratedlumi=lumiCorrections.applyfinecorrectionV2(avglumi,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],1.0)
                else:
                    calibratedlumi=lumiCorrections.applyfinecorrection(avglumi,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2])
            if lumitype=='PIXEL' and finecorrections is not None:
                calibratedlumi=finecorrections[run]*avglumi
            calibratedlumierror=instlumierror*normval
            bstatus=perlsdata[4]
            begev=perlsdata[5]
            numorbit=perlsdata[6]
            startorbit=perlsdata[7]
            timestamp=c.OrbitToTime(startTimeStr,startorbit,0)
            lslen=lumip.lslengthsec()
            deliveredlumi=calibratedlumi*lslen
            recordedlumi=0.0
            if triggeredls!=0:
                if not trgdata.has_key(cmslsnum):                    
                   # triggeredls=0 #if no trigger, set back to non-cms-active ls
                    recordedlumi=0.0 # no trigger->nobeam recordedlumi=None
                else:
                    deadcount=trgdata[cmslsnum][0] ##subject to change !!
                    bitzerocount=trgdata[cmslsnum][1]
                    bitzeroprescale=trgdata[cmslsnum][2]
                    deadfrac=trgdata[cmslsnum][3]
                    if deadfrac<0 or deadfrac>1.0:
                        deadfrac=1.0
                    #if float(bitzerocount)*float(bitzeroprescale)==0.0:
                    #    deadfrac=1.0
                    #else:
                    #    deadfrac=float(deadcount)/(float(bitzerocount)*float(bitzeroprescale))
                    #if deadfrac>1.0:
                    #    deadfrac=1.0  #artificial correction in case of deadfrac>1
                    recordedlumi=deliveredlumi*(1.0-deadfrac)
                    del trgdata[cmslsnum][:]
            bxdata=None
            if withBXInfo:
                bxinfo=perlsdata[8]
                bxvalueArray=None
                bxerrArray=None
                bxidxlist=[]
                bxvaluelist=[]
                bxerrorlist=[]
                if bxinfo:
                    bxvalueArray=bxinfo[0]
                    bxerrArray=bxinfo[1]
                    #if cmslsnum==1:
                    #    print 'bxvalueArray ',bxvalueArray
                    for idx,bxval in enumerate(bxvalueArray):                    
                        if finecorrections and finecorrections[run]:
                            if usecorrectionv2:
                                if driftcorrections and driftcorrections[run]:
                                    mybxval=lumiCorrections.applyfinecorrectionBXV2(bxval,avglumi,perbunchnormval,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],driftcorrections[run])
                                else:
                                    mybxval=lumiCorrections.applyfinecorrectionBXV2(bxval,avglumi,perbunchnormval,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2],finecorrections[run][3],finecorrections[run][4],1.0)
                            else:
                                mybxval=lumiCorrections.applyfinecorrectionBX(bxval,avglumi,perbunchnormval,finecorrections[run][0],finecorrections[run][1],finecorrections[run][2])
                        else:
                            mybxval=bxval*perbunchnormval
                        if mybxval>xingMinLum:
                            bxidxlist.append(idx)
                            bxvaluelist.append(mybxval)
                            bxerrorlist.append(bxerrArray[idx]*perbunchnormval)#no correciton on errors
                    del bxvalueArray[:]
                    del bxerrArray[:]
                bxdata=(bxidxlist,bxvaluelist,bxerrorlist)
            beamdata=None
            if withBeamIntensity:
                beaminfo=perlsdata[9]
                bxindexlist=[]
                b1intensitylist=[]
                b2intensitylist=[]                
                if beaminfo:
                    bxindexarray=beaminfo[0]
                    beam1intensityarray=beaminfo[1]
                    beam2intensityarray=beaminfo[2]                    
                    bxindexlist=bxindexarray.tolist()
                    b1intensitylist=beam1intensityarray.tolist()
                    b2intensitylist=beam2intensityarray.tolist()
                    del bxindexarray[:]
                    del beam1intensityarray[:]
                    del beam2intensityarray[:]
                beamdata=(bxindexlist,b1intensitylist,b2intensitylist)
            perrunresult.append([lumilsnum,triggeredls,timestamp,bstatus,begev,deliveredlumi,recordedlumi,calibratedlumierror,bxdata,beamdata,fillnum])
            del perlsdata[:]
        result[run]=perrunresult    
    return result
def instLumiForRange(schema,inputRange,beamstatusfilter=None,withBXInfo=False,bxAlgo=None,xingMinLum=0.0,withBeamIntensity=False,lumitype='HF',branchName=None):
    '''
    DIRECTLY FROM ROOT FIME NO CORRECTION AT ALL 
    lumi raw data. beofore normalization and time integral
    input:
           inputRange  {run:[cmsls]} (required)
           beamstatus: LS filter on beamstatus (optional)
           withBXInfo: get per bunch info (optional)
           bxAlgo: algoname for bx values (optional) ['OCC1','OCC2','ET']
           xingMinLum: cut on bx lumi value (optional)
           withBeamIntensity: get beam intensity info (optional)
           branchName: data version
    output:
           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)]}}
           lumi unit: HZ/ub
    '''
    if lumitype not in ['HF','PIXEL']:
        raise ValueError('unknown lumitype '+lumitype)
    lumitableName=''
    lumilstableName=''
    if lumitype=='HF':
        lumitableName=nameDealer.lumidataTableName()
        lumilstableName=nameDealer.lumisummaryv2TableName()
    else:
        lumitableName=nameDealer.pixellumidataTableName()
        lumilstableName=nameDealer.pixellumisummaryv2TableName()
        
    result={}
    for run in inputRange.keys():
        lslist=inputRange[run]
        if lslist is not None and len(lslist)==0:
            result[run]=[]#if no LS is selected for a run
            continue
        runsummary=dataDML.runsummary(schema,run)
        if len(runsummary)==0:#if run not found in runsummary
            result[run]=None
            continue
        fillnum=runsummary[4]
        runstarttimeStr=runsummary[6]
        lumidataid=dataDML.guessLumiDataIdByRun(schema,run,lumitableName)
        if lumidataid is None: #if run not found in lumidata
            result[run]=None
            continue
        (lumirunnum,perlsresult)=dataDML.lumiLSById(schema,lumidataid,beamstatusfilter,withBXInfo=withBXInfo,bxAlgo=bxAlgo,withBeamIntensity=withBeamIntensity,tableName=lumilstableName)
        lsresult=[]
        c=lumiTime.lumiTime()
        for lumilsnum in perlsresult.keys():
            perlsdata=perlsresult[lumilsnum]
            cmslsnum=perlsdata[0]
            if lslist is not None and lumilsnum not in lslist:
                cmslsnum=0
            numorbit=perlsdata[6]
            startorbit=perlsdata[7]
            orbittime=c.OrbitToTime(runstarttimeStr,startorbit,0)
            instlumi=perlsdata[1]
            instlumierr=perlsdata[2]
            beamstatus=perlsdata[4]
            beamenergy=perlsdata[5]
            bxidxlist=[]
            bxvaluelist=[]
            bxerrorlist=[]
            bxdata=None
            beamdata=None
            if withBXInfo:
                bxinfo=perlsdata[8]                
                bxvalueArray=None
                bxerrArray=None
                if bxinfo:
                    bxvalueArray=bxinfo[0]
                    bxerrArray=bxinfo[1]
                    for idx,bxval in enumerate(bxvalueArray):
                        if bxval>xingMinLum:
                            bxidxlist.append(idx)
                            bxvaluelist.append(bxval)
                            bxerrorlist.append(bxerrArray[idx])
                    del bxvalueArray[:]
                    del bxerrArray[:]
                bxdata=(bxidxlist,bxvaluelist,bxerrorlist)
            if withBeamIntensity:
                beaminfo=perlsdata[9]
                bxindexlist=[]
                b1intensitylist=[]
                b2intensitylist=[]
                if beaminfo[0] and beaminfo[1] and beaminfo[2]:
                    bxindexarray=beaminfo[0]
                    beam1intensityarray=beaminfo[1]
                    beam2intensityarray=beaminfo[2]                    
                    bxindexlist=bxindexarray.tolist()
                    b1intensitylist=beam1intensityarray.tolist()
                    b2intensitylist=beam2intensityarray.tolist()
                    del bxindexarray[:]
                    del beam1intensityarray[:]
                    del beam2intensityarray[:]                    
                beamdata=(bxindexlist,b1intensitylist,b2intensitylist)
            lsresult.append([lumilsnum,cmslsnum,orbittime,beamstatus,beamenergy,instlumi,instlumierr,startorbit,numorbit,bxdata,beamdata,fillnum])         
            del perlsdata[:]
        result[run]=lsresult
    return result