예제 #1
0
def createWeightedPayloadsNew(fileName,listbeam=[],weighted=True):
    newlistbeam = []
    docreate = False
    docheck = False
    lastPayload = listbeam[0]

    firstToUse = 0
    lastToUse = 0
    for ii in range(0,len(listbeam)):
        docreate = False
        if docheck:
            deltaX = delta(ibeam.X, ibeam.Xerr, inextbeam.X, inextbeam.Xerr) > 1.5
            deltaY = delta(ibeam.Y, ibeam.Yerr, inextbeam.Y, inextbeam.Yerr) > 1.5
            deltaZ = delta(ibeam.Z, ibeam.Zerr, inextbeam.Z, inextbeam.Zerr) > 2.5

            deltasigmaZ = delta(ibeam.sigmaZ, ibeam.sigmaZerr, inextbeam.sigmaZ, inextbeam.sigmaZerr) > 2.5
            deltadxdz   = delta(ibeam.dxdz, ibeam.dxdzerr, inextbeam.dxdz, inextbeam.dxdzerr) > 2.5
            deltadydz   = delta(ibeam.dydz, ibeam.dydzerr, inextbeam.dydz, inextbeam.dydzerr) > 2.5

            deltawidthX = delta(ibeam.beamWidthX, ibeam.beamWidthXerr, inextbeam.beamWidthX, inextbeam.beamWidthXerr) > 3
            deltawidthY = delta(ibeam.beamWidthY, ibeam.beamWidthYerr, inextbeam.beamWidthY, inextbeam.beamWidthYerr) > 3

            #if iNNbeam.Type != -1:
            #    deltaX = deltaX and delta(ibeam.X, ibeam.Xerr, iNNbeam.X, iNNbeam.Xerr) > 1.5
            #    deltaY = deltaY and delta(ibeam.Y, ibeam.Yerr, iNNbeam.Y, iNNbeam.Yerr) > 1.5
            #    deltaZ = deltaZ and delta(ibeam.Z, ibeam.Zerr, iNNbeam.Z, iNNbeam.Zerr) > 1.5
            #		
            #    deltasigmaZ = deltasigmaZ and delta(ibeam.sigmaZ, ibeam.sigmaZerr, iNNbeam.sigmaZ, iNNbeam.sigmaZerr) > 2.5
            #    deltadxdz   = deltadxdz and delta(ibeam.dxdz, ibeam.dxdzerr, iNNbeam.dxdz, iNNbeam.dxdzerr) > 2.5
            #    deltadydz   = deltadydz and delta(ibeam.dydz, ibeam.dydzerr, iNNbeam.dydz, iNNbeam.dydzerr) > 2.5
            #
            #    deltawidthX = deltawidthX and delta(ibeam.beamWidthX, ibeam.beamWidthXerr, iNNbeam.beamWidthX, iNNbeam.beamWidthXerr) > 3
            #    deltawidthY = deltawidthY and delta(ibeam.beamWidthY, ibeam.beamWidthYerr, iNNbeam.beamWidthY, iNNbeam.beamWidthYerr) > 3

            if deltaX or deltaY or deltaZ or deltasigmaZ or deltadxdz or deltadydz or deltawidthX or deltawidthY:
                if ii != 0:
                    docreate = True
                    lastToUse = ii-1
                #print "shift here: x="+str(deltaX)+" y="+str(deltaY)
                #print "x1 = "+ibeam.X + " x1err = "+ibeam.Xerr
                #print "x2 = "+inextbeam.X + " x2err = "+inextbeam.Xerr
                #print "Lumi1: "+str(ibeam.IOVfirst) + " Lumi2: "+str(inextbeam.IOVfirst)
                #print " x= "+ibeam.X+" +/- "+ibeam.Xerr
                #print "weighted average x = "+tmpbeam.X +" +//- "+tmpbeam.Xerr
                print "close payload because of movement in X= "+str(deltaX)+", Y= "+str(deltaY) + ", Z= "+str(deltaZ)+", sigmaZ= "+str(deltasigmaZ)+", dxdz= "+str(deltadxdz)+", dydz= "+str(deltadydz)+", widthX= "+str(deltawidthX)+", widthY= "+str(deltawidthY)
        
        #WARNING this will only be fine for Run based IOVs
        if ii >= len(listbeam) - 1 or listbeam[ii].Run != listbeam[ii+1].Run :
            print "close payload because end of run has been reached. Run " + listbeam[ii].Run
            docreate = True
            lastToUse = ii
        

         # check maximum lumi counts
#?        if countlumi == maxNlumis:
#?            print "close payload because maximum lumi sections accumulated within run "+ibeam.Run
#?            docreate = True
#?            countlumi = 0
        if docreate:
            tmpbeam = BeamSpot()
            for ibeam in listbeam[firstToUse:lastToUse]:
                (tmpbeam.X, tmpbeam.Xerr) = weight(tmpbeam.X, tmpbeam.Xerr, ibeam.X, ibeam.Xerr)
                (tmpbeam.Y, tmpbeam.Yerr) = weight(tmpbeam.Y, tmpbeam.Yerr, ibeam.Y, ibeam.Yerr)
                (tmpbeam.Z, tmpbeam.Zerr) = weight(tmpbeam.Z, tmpbeam.Zerr, ibeam.Z, ibeam.Zerr)
                (tmpbeam.sigmaZ, tmpbeam.sigmaZerr) = weight(tmpbeam.sigmaZ, tmpbeam.sigmaZerr, ibeam.sigmaZ, ibeam.sigmaZerr)
                (tmpbeam.dxdz, tmpbeam.dxdzerr) = weight(tmpbeam.dxdz, tmpbeam.dxdzerr, ibeam.dxdz, ibeam.dxdzerr)
                (tmpbeam.dydz, tmpbeam.dydzerr) = weight(tmpbeam.dydz, tmpbeam.dydzerr, ibeam.dydz, ibeam.dydzerr)
                #print "wx = " + ibeam.beamWidthX + " err= "+ ibeam.beamWidthXerr
                (tmpbeam.beamWidthX, tmpbeam.beamWidthXerr) = weight(tmpbeam.beamWidthX, tmpbeam.beamWidthXerr, ibeam.beamWidthX, ibeam.beamWidthXerr)
                (tmpbeam.beamWidthY, tmpbeam.beamWidthYerr) = weight(tmpbeam.beamWidthY, tmpbeam.beamWidthYerr, ibeam.beamWidthY, ibeam.beamWidthYerr)
            tmpbeam.IOVfirst     = listbeam[firstToUse].IOVfirst
            tmpbeam.IOVBeginTime = listbeam[firstToUse].IOVBeginTime
            tmpbeam.Run          = listbeam[firstToUse].Run
            tmpbeam.Type         = 2
            tmpbeam.IOVlast      = listbeam[lastToUse].IOVlast
            tmpbeam.IOVEndTime   = listbeam[lastToUse].IOVEndTime
            newlistbeam.append(tmpbeam)
            firstToUse = lastToUse+1
            print "Run: " + tmpbeam.Run + " Lumi1: " + str(tmpbeam.IOVfirst) + " Lumi2: " + str(tmpbeam.IOVlast)
            
    payloadfile = open(fileName,"w")
    for iload in newlistbeam:
        dump( iload, payloadfile )
    payloadfile.close()
예제 #2
0
def createWeightedPayloads(fileName,listbeam=[],weighted=True):
    newlistbeam = []
    tmpbeam = BeamSpot()
    docreate = True
    countlumi = 0
    tmprun = ""
    maxNlumis = 60
    if weighted:
        maxNlumis = 999999999
    for ii in range(0,len(listbeam)):
        ibeam = listbeam[ii]
        inextbeam = BeamSpot()
        iNNbeam = BeamSpot()
        if docreate:
            tmpbeam.IOVfirst = ibeam.IOVfirst
            tmpbeam.IOVBeginTime = ibeam.IOVBeginTime
            tmpbeam.Run = ibeam.Run
            tmpbeam.Type = 2
        docheck = False
        docreate = False
	#print "Currently testing ii="+str(ii)+" Lumi1: "+str(ibeam.IOVfirst)
	    
        # check last iov
        if ii < len(listbeam) - 1: 
            inextbeam = listbeam[ii+1]
            docheck = True
            if ii < len(listbeam) -2:
                iNNbeam = listbeam[ii+2]
        else:
            print "close payload because end of data has been reached. Run "+ibeam.Run
            docreate = True
        # check we run over the same run
        if ibeam.Run != inextbeam.Run:
            print "close payload because end of run "+ibeam.Run
            docreate = True
        # check maximum lumi counts
        if countlumi == maxNlumis -1:
            print "close payload because maximum lumi sections accumulated within run "+ibeam.Run
            docreate = True
            countlumi = 0
        # weighted average position
        (tmpbeam.X, tmpbeam.Xerr) = weight(tmpbeam.X, tmpbeam.Xerr, ibeam.X, ibeam.Xerr)
        (tmpbeam.Y, tmpbeam.Yerr) = weight(tmpbeam.Y, tmpbeam.Yerr, ibeam.Y, ibeam.Yerr)
        (tmpbeam.Z, tmpbeam.Zerr) = weight(tmpbeam.Z, tmpbeam.Zerr, ibeam.Z, ibeam.Zerr)
        (tmpbeam.sigmaZ, tmpbeam.sigmaZerr) = weight(tmpbeam.sigmaZ, tmpbeam.sigmaZerr, ibeam.sigmaZ, ibeam.sigmaZerr)
        (tmpbeam.dxdz, tmpbeam.dxdzerr) = weight(tmpbeam.dxdz, tmpbeam.dxdzerr, ibeam.dxdz, ibeam.dxdzerr)
        (tmpbeam.dydz, tmpbeam.dydzerr) = weight(tmpbeam.dydz, tmpbeam.dydzerr, ibeam.dydz, ibeam.dydzerr)
        #print "wx = " + ibeam.beamWidthX + " err= "+ ibeam.beamWidthXerr
        (tmpbeam.beamWidthX, tmpbeam.beamWidthXerr) = weight(tmpbeam.beamWidthX, tmpbeam.beamWidthXerr, ibeam.beamWidthX, ibeam.beamWidthXerr)
        (tmpbeam.beamWidthY, tmpbeam.beamWidthYerr) = weight(tmpbeam.beamWidthY, tmpbeam.beamWidthYerr, ibeam.beamWidthY, ibeam.beamWidthYerr)
        
        if weighted:
            docheck = False
        # check offsets
        #if False:
        if docheck:

            # define minimum limit
            min_limit = 0.0025
            
            # limit for x and y
            limit = float(ibeam.beamWidthX)/2.
            if limit < min_limit: limit = min_limit
            
            # check movements in X
            adelta1 = delta(ibeam.X, ibeam.Xerr, inextbeam.X, inextbeam.Xerr)
            adelta2 = (0.,1.e9)
            adelta1dxdz = delta(ibeam.dxdz, ibeam.dxdzerr, inextbeam.dxdz, inextbeam.dxdzerr)
            adelta2dxdz = (0.,1.e9)
            adelta1dydz = delta(ibeam.dydz, ibeam.dydzerr, inextbeam.dydz, inextbeam.dydzerr)
            adelta2dydz = (0.,1.e9)
            adelta1widthx = delta(ibeam.beamWidthX, ibeam.beamWidthXerr, inextbeam.beamWidthX, inextbeam.beamWidthXerr)
            adelta2widthx = (0.,1.e9)
            adelta1widthy = delta(ibeam.beamWidthY, ibeam.beamWidthYerr, inextbeam.beamWidthY, inextbeam.beamWidthYerr)
            adelta2widthy = (0.,1.e9)
            adelta1z0 = delta(ibeam.Z, ibeam.Zerr, inextbeam.Z, inextbeam.Zerr)
            adelta1sigmaZ = delta(ibeam.sigmaZ, ibeam.sigmaZerr, inextbeam.sigmaZ, inextbeam.sigmaZerr)
            
            if iNNbeam.Type != -1:
                adelta2 = delta(inextbeam.X, inextbeam.Xerr, iNNbeam.X, iNNbeam.Xerr)
                adelta2dxdz = delta(inextbeam.dxdz, inextbeam.dxdzerr, iNNbeam.dxdz, iNNbeam.dxdzerr)
                adelta2dydz = delta(inextbeam.dydz, inextbeam.dydzerr, iNNbeam.dydz, iNNbeam.dydzerr)
                adelta2widthx = delta(inextbeam.beamWidthX, inextbeam.beamWidthXerr, iNNbeam.beamWidthX, iNNbeam.beamWidthXerr)
                adelta2widthy = delta(inextbeam.beamWidthY, inextbeam.beamWidthYerr, iNNbeam.beamWidthY, iNNbeam.beamWidthYerr)
                
            deltaX = deltaSig(adelta1) > 3.5 and adelta1[0] >= limit
            if ii < len(listbeam) -2:
                if deltaX==False and adelta1[0]*adelta2[0] > 0. and  math.fabs(adelta1[0]+adelta2[0]) >= limit:
                    #print " positive, "+str(adelta1[0]+adelta2[0])+ " limit="+str(limit)
                    deltaX = True
                elif deltaX==True and adelta1[0]*adelta2[0]<=0 and adelta2[0] != 0 and math.fabs(adelta1[0]/adelta2[0]) > 0.33 and math.fabs(adelta1[0]/adelta2[0]) < 3:
                    deltaX = False
                    #print " negative, "+str(adelta1[0]/adelta2[0])
                #else:
                #    print str(adelta1[0]/adelta2[0])

            # check movemnts in Y
            adelta1 = delta(ibeam.Y, ibeam.Yerr, inextbeam.Y, inextbeam.Yerr)
            adelta2 = (0.,1.e9)
            if iNNbeam.Type != -1:
                adelta2 = delta(inextbeam.Y, inextbeam.Yerr, iNNbeam.Y, iNNbeam.Yerr)
                
            deltaY = deltaSig(adelta1) > 3.5 and adelta1[0] >= limit
            if ii < len(listbeam) -2:
                if deltaY==False and adelta1[0]*adelta2[0] > 0. and  math.fabs(adelta1[0]+adelta2[0]) >= limit:
                    deltaY = True
                elif deltaY==True and adelta1[0]*adelta2[0]<=0 and adelta2[0] != 0 and math.fabs(adelta1[0]/adelta2[0]) > 0.33 and math.fabs(adelta1[0]/adelta2[0]) < 3:
                    deltaY = False
            # check movements in Z                                                    
            
            limit = float(ibeam.sigmaZ)/2.
            deltaZ = deltaSig(adelta1z0) > 3.5 and math.fabs(adelta1z0[0]) >= limit
            
            deltasigmaZ = deltaSig(adelta1sigmaZ) > 5.0

            # check dxdz
            adelta = delta(ibeam.dxdz, ibeam.dxdzerr, inextbeam.dxdz, inextbeam.dxdzerr)
            deltadxdz   = deltaSig(adelta) > 5.0
            if deltadxdz and adelta1dxdz[0]*adelta2dxdz[0]<=0 and adelta2dxdz[0] != 0 and math.fabs(adelta1dxdz[0]/adelta2dxdz[0]) > 0.33 and math.fabs(adelta1dxdz[0]/adelta2dxdz[0]) < 3:
                deltadxdz = False
            # check dydz
            adelta = delta(ibeam.dydz, ibeam.dydzerr, inextbeam.dydz, inextbeam.dydzerr)
            deltadydz   = deltaSig(adelta) > 5.0
            if deltadydz and adelta1dydz[0]*adelta2dydz[0]<=0 and adelta2dydz[0] != 0 and math.fabs(adelta1dydz[0]/adelta2dydz[0]) > 0.33 and math.fabs(adelta1dydz[0]/adelta2dydz[0]) < 3:
                deltadydz = False
            
            adelta = delta(ibeam.beamWidthX, ibeam.beamWidthXerr, inextbeam.beamWidthX, inextbeam.beamWidthXerr)
            deltawidthX = deltaSig(adelta) > 5
            if deltawidthX and adelta1widthx[0]*adelta2widthx[0]<=0 and adelta2widthx[0] != 0 and math.fabs(adelta1widthx[0]/adelta2widthx[0]) > 0.33 and math.fabs(adelta1widthx[0]/adelta2widthx[0]) < 3:
                deltawidthX = False
                
            adelta = delta(ibeam.beamWidthY, ibeam.beamWidthYerr, inextbeam.beamWidthY, inextbeam.beamWidthYerr) 
            deltawidthY = deltaSig(adelta) > 5
            if deltawidthY and adelta1widthy[0]*adelta2widthy[0]<=0 and adelta2widthy[0] != 0 and math.fabs(adelta1widthy[0]/adelta2widthy[0]) > 0.33 and math.fabs(adelta1widthy[0]/adelta2widthy[0]) < 3:
                deltawidthY = False
            #if iNNbeam.Type != -1:
            #    deltaX = deltaX and delta(ibeam.X, ibeam.Xerr, iNNbeam.X, iNNbeam.Xerr) > 1.5
            #    deltaY = deltaY and delta(ibeam.Y, ibeam.Yerr, iNNbeam.Y, iNNbeam.Yerr) > 1.5
            #    deltaZ = deltaZ and delta(ibeam.Z, ibeam.Zerr, iNNbeam.Z, iNNbeam.Zerr) > 1.5
            #		
            #    deltasigmaZ = deltasigmaZ and delta(ibeam.sigmaZ, ibeam.sigmaZerr, iNNbeam.sigmaZ, iNNbeam.sigmaZerr) > 2.5
            #    deltadxdz   = deltadxdz and delta(ibeam.dxdz, ibeam.dxdzerr, iNNbeam.dxdz, iNNbeam.dxdzerr) > 2.5
            #    deltadydz   = deltadydz and delta(ibeam.dydz, ibeam.dydzerr, iNNbeam.dydz, iNNbeam.dydzerr) > 2.5
            #
            #    deltawidthX = deltawidthX and delta(ibeam.beamWidthX, ibeam.beamWidthXerr, iNNbeam.beamWidthX, iNNbeam.beamWidthXerr) > 3
            #    deltawidthY = deltawidthY and delta(ibeam.beamWidthY, ibeam.beamWidthYerr, iNNbeam.beamWidthY, iNNbeam.beamWidthYerr) > 3

            if deltaX or deltaY or deltaZ or deltasigmaZ or deltadxdz or deltadydz or deltawidthX or deltawidthY:
                docreate = True
                #print "shift here: x="+str(deltaX)+" y="+str(deltaY)
                #print "x1 = "+ibeam.X + " x1err = "+ibeam.Xerr
                #print "x2 = "+inextbeam.X + " x2err = "+inextbeam.Xerr
                #print "Lumi1: "+str(ibeam.IOVfirst) + " Lumi2: "+str(inextbeam.IOVfirst)
                #print " x= "+ibeam.X+" +/- "+ibeam.Xerr
                #print "weighted average x = "+tmpbeam.X +" +//- "+tmpbeam.Xerr
                print "close payload because of movement in X= "+str(deltaX)+", Y= "+str(deltaY) + ", Z= "+str(deltaZ)+", sigmaZ= "+str(deltasigmaZ)+", dxdz= "+str(deltadxdz)+", dydz= "+str(deltadydz)+", widthX= "+str(deltawidthX)+", widthY= "+str(deltawidthY)
        if docreate:
            #if ii == len(listbeam)-1:
            tmpbeam.IOVlast = ibeam.IOVlast
            tmpbeam.IOVEndTime = ibeam.IOVEndTime
            print "  Run: "+tmpbeam.Run +" Lumi1: "+str(tmpbeam.IOVfirst) + " Lumi2: "+str(tmpbeam.IOVlast)
            newlistbeam.append(tmpbeam)
            tmpbeam = BeamSpot()
            countlumi = 0
        tmprun = ibeam.Run
        countlumi += 1

    payloadfile = open(fileName,"w")
    for iload in newlistbeam:
        dump( iload, payloadfile )
    payloadfile.close()
    return newlistbeam
예제 #3
0
def readBeamSpotFile(fileName,listbeam=[],IOVbase="runbase", firstRun='1',lastRun='4999999999'):
    tmpbeam = BeamSpot()
    tmpbeamsize = 0
    
    #firstRun = "1"
    #lastRun  = "4999999999"
    if IOVbase == "lumibase" and firstRun=='1' and lastRun=='4999999999' :
    	firstRun = "1:1"
        lastRun = "4999999999:4999999999"

    inputfiletype = 0
    #print "first = " +firstRun
    #print "last = " +lastRun

    # for bx
    maplist = {}
    hasBX = False
            
    tmpfile = open(fileName)
    atmpline = tmpfile.readline()
    if atmpline.find('Runnumber') != -1:
	inputfiletype = 1
        if len(atmpline.split()) > 2:
            hasBX = True
            print " Input data has been calculated as function of BUNCH CROSSINGS."
    tmpfile.seek(0)

        
    if inputfiletype ==1:

	tmpBX = 0
	for line in tmpfile:
            
	    if line.find('Type') != -1:
		tmpbeam.Type = int(line.split()[1])
		tmpbeamsize += 1
	    if line.find('X0') != -1:
		tmpbeam.X = line.split()[1]
		#tmpbeam.Xerr = line.split()[4]
		tmpbeamsize += 1
            #print " x = " + str(tmpbeam.X)
	    if line.find('Y0') != -1:
		tmpbeam.Y = line.split()[1]
		#tmpbeam.Yerr = line.split()[4]
		tmpbeamsize += 1
            #print " y =" + str(tmpbeam.Y)
	    if line.find('Z0') != -1 and line.find('sigmaZ0') == -1:
		tmpbeam.Z = line.split()[1]
		#tmpbeam.Zerr = line.split()[4]
		tmpbeamsize += 1
	    if line.find('sigmaZ0') !=-1:
		tmpbeam.sigmaZ = line.split()[1]
		#tmpbeam.sigmaZerr = line.split()[5]
		tmpbeamsize += 1
            if line.find('dxdz') != -1:
		tmpbeam.dxdz = line.split()[1]
		#tmpbeam.dxdzerr = line.split()[4]
		tmpbeamsize += 1
	    if line.find('dydz') != -1:
		tmpbeam.dydz = line.split()[1]
		#tmpbeam.dydzerr = line.split()[4]
		tmpbeamsize += 1
	    if line.find('BeamWidthX') != -1:
		tmpbeam.beamWidthX = line.split()[1]
		#tmpbeam.beamWidthXerr = line.split()[6]
		tmpbeamsize += 1
	    if line.find('BeamWidthY') != -1:
		tmpbeam.beamWidthY = line.split()[1]
		#tmpbeam.beamWidthYerr = line.split()[6]
		tmpbeamsize += 1
	    if line.find('Cov(0,j)') != -1:
		tmpbeam.Xerr = str(math.sqrt( float( line.split()[1] ) ) )
		tmpbeamsize += 1
	    if line.find('Cov(1,j)') != -1:
		tmpbeam.Yerr = str(math.sqrt( float( line.split()[2] ) ) )
		tmpbeamsize += 1
	    if line.find('Cov(2,j)') != -1:
		tmpbeam.Zerr = str(math.sqrt( float( line.split()[3] ) ) )
		tmpbeamsize += 1
	    if line.find('Cov(3,j)') != -1:
		tmpbeam.sigmaZerr = str(math.sqrt( float( line.split()[4] ) ) )
		tmpbeamsize += 1
            if line.find('Cov(4,j)') != -1:
		tmpbeam.dxdzerr = str(math.sqrt( float( line.split()[5] ) ) )
		tmpbeamsize += 1
	    if line.find('Cov(5,j)') != -1:
		tmpbeam.dydzerr = str(math.sqrt( float( line.split()[6] ) ) )
		tmpbeamsize += 1
	    if line.find('Cov(6,j)') != -1:
		tmpbeam.beamWidthXerr = str(math.sqrt( float( line.split()[7] ) ) )
                tmpbeam.beamWidthYerr = tmpbeam.beamWidthXerr
		tmpbeamsize += 1
	    if line.find('LumiRange')  != -1:
                if IOVbase=="lumibase":
                    tmpbeam.IOVfirst = line.split()[1]
                    tmpbeam.IOVlast = line.split()[3]
		tmpbeamsize += 1
            if line.find('Runnumber') != -1:
		tmpbeam.Run = line.split()[1]
		if IOVbase == "runbase":
		    tmpbeam.IOVfirst = line.split()[1]
		    tmpbeam.IOVlast = line.split()[1]
                if hasBX:
                    tmpBX = line.split()[3]
		tmpbeamsize += 1
            if line.find('BeginTimeOfFit') != -1:
		tmpbeam.IOVBeginTime = line.split()[1] +" "+line.split()[2] +" "+line.split()[3]
		if IOVbase =="timebase":
		    tmpbeam.IOVfirst =  time.mktime( time.strptime(line.split()[1] +  " " + line.split()[2] + " " + line.split()[3],"%Y.%m.%d %H:%M:%S %Z") )
		tmpbeamsize += 1
            if line.find('EndTimeOfFit') != -1:
		tmpbeam.IOVEndTime = line.split()[1] +" "+line.split()[2] +" "+line.split()[3]
		if IOVbase =="timebase":
		    tmpbeam.IOVlast = time.mktime( time.strptime(line.split()[1] +  " " + line.split()[2] + " " + line.split()[3],"%Y.%m.%d %H:%M:%S %Z") )
		tmpbeamsize += 1
	    if tmpbeamsize == 20:
		if IOVbase=="lumibase":
		    tmprunfirst = int(firstRun.split(":")[0])
		    tmprunlast  = int(lastRun.split(":")[0])
		    tmplumifirst = int(firstRun.split(":")[1])
		    tmplumilast  = int(lastRun.split(":")[1])
		    acceptiov1 = acceptiov2 = False
		    # check lumis in the same run
		    if tmprunfirst == tmprunlast and int(tmpbeam.Run)==tmprunfirst:
			if int(tmpbeam.IOVfirst) >= tmplumifirst and int(tmpbeam.IOVlast)<=tmplumilast:
			    acceptiov1 = acceptiov2 = True
		    # if different runs make sure you select the correct range of lumis
		    elif int(tmpbeam.Run) == tmprunfirst:
			if int(tmpbeam.IOVfirst) >= tmplumifirst: acceptiov1 = True
		    elif int(tmpbeam.Run) == tmprunlast:
			if int(tmpbeam.IOVlast) <= tmplumilast: acceptiov2 = True
		    elif tmprunfirst <= int(tmpbeam.Run) and tmprunlast >= int(tmpbeam.Run): 
			acceptiov1 = acceptiov2 = True
			
		    if acceptiov1 and acceptiov2:
			if tmpbeam.Type != 2:
			    print "invalid fit, skip Run "+str(tmpbeam.Run)+" IOV: "+str(tmpbeam.IOVfirst) + " to "+ str(tmpbeam.IOVlast)
                        elif isnan(tmpbeam.Z) or isnan(tmpbeam.Zerr) or isnan(tmpbeam.sigmaZerr) or isnan(tmpbeam.beamWidthXerr) or isnan(tmpbeam.beamWidthYerr):
                            print "invalid fit, NaN values!! skip Run "+str(tmpbeam.Run)+" IOV: "+str(tmpbeam.IOVfirst) + " to "+ str(tmpbeam.IOVlast)                       
			elif hasBX:
                            if (tmpBX in maplist) == False:
                                maplist[tmpBX] = [tmpbeam]
                            else:
                                maplist[tmpBX].append(tmpbeam)
                        else:
			    listbeam.append(tmpbeam)

		elif int(tmpbeam.IOVfirst) >= int(firstRun) and int(tmpbeam.IOVlast) <= int(lastRun):
		    if tmpbeam.Type != 2:
			print "invalid fit, skip Run "+str(tmpbeam.Run)+" IOV: "+str(tmpbeam.IOVfirst) + " to "+ str(tmpbeam.IOVlast)
                    elif isnan(tmpbeam.Z) or isnan(tmpbeam.Zerr) or isnan(tmpbeam.sigmaZerr) or isnan(tmpbeam.beamWidthXerr) or isnan(tmpbeam.beamWidthYerr):
                        print "invalid fit, NaN values!! skip Run "+str(tmpbeam.Run)+" IOV: "+str(tmpbeam.IOVfirst) + " to "+ str(tmpbeam.IOVlast)
		    else:
			listbeam.append(tmpbeam)
                        
		tmpbeamsize = 0
		tmpbeam = BeamSpot()
                tmpBX = 0
    else:

	for line in tmpfile:
	
	    if line.find('X0') != -1:
		tmpbeam.X = line.split()[2]
		tmpbeam.Xerr = line.split()[4]
		tmpbeamsize += 1
            #print " x = " + str(tmpbeam.X)
	    if line.find('Y0') != -1:
		tmpbeam.Y = line.split()[2]
		tmpbeam.Yerr = line.split()[4]
		tmpbeamsize += 1
            #print " y =" + str(tmpbeam.Y)
	    if line.find('Z0') != -1 and line.find('Sigma Z0') == -1:
		tmpbeam.Z = line.split()[2]
		tmpbeam.Zerr = line.split()[4]
		tmpbeamsize += 1
            #print " z =" + str(tmpbeam.Z)
	    if line.find('Sigma Z0') !=-1:
		tmpbeam.sigmaZ = line.split()[3]
		tmpbeam.sigmaZerr = line.split()[5]
		tmpbeamsize += 1
	    if line.find('dxdz') != -1:
		tmpbeam.dxdz = line.split()[2]
		tmpbeam.dxdzerr = line.split()[4]
		tmpbeamsize += 1
	    if line.find('dydz') != -1:
		tmpbeam.dydz = line.split()[2]
		tmpbeam.dydzerr = line.split()[4]
		tmpbeamsize += 1
	    if line.find('Beam Width X') != -1:
		tmpbeam.beamWidthX = line.split()[4]
		tmpbeam.beamWidthXerr = line.split()[6]
		tmpbeamsize += 1
	    if line.find('Beam Width Y') != -1:
		tmpbeam.beamWidthY = line.split()[4]
		tmpbeam.beamWidthYerr = line.split()[6]
		tmpbeamsize += 1
	#if line.find('Run ') != -1:
	    if line.find('for runs')  != -1:
	    #tmpbeam.IOVfirst = line.split()[6].strip(',')
	        tmpbeam.Run      = line.split()[2]
		if IOVbase == "runbase":
		  tmpbeam.IOVfirst = line.split()[2]
		  tmpbeam.IOVlast = line.split()[4]
		tmpbeamsize += 1
	    if line.find('LumiSection')  != -1:
                if IOVbase=="lumibase":
                    tmpbeam.IOVfirst = line.split()[10]
                    tmpbeam.IOVlast = line.split()[10]
		tmpbeamsize += 1
	    if tmpbeamsize == 10:

		if IOVbase=="lumibase":
		    tmprunfirst = int(firstRun.split(":")[0])
		    tmprunlast  = int(lastRun.split(":")[0])
		    tmplumifirst = int(firstRun.split(":")[1])
		    tmplumilast  = int(lastRun.split(":")[1])
		    acceptiov1 = acceptiov2 = False
		    # check lumis in the same run
		    if tmprunfirst == tmprunlast and int(tmpbeam.Run)==tmprunfirst:
			if int(tmpbeam.IOVfirst) >= tmplumifirst and int(tmpbeam.IOVlast)<=tmplumilast:
			    acceptiov1 = acceptiov2 = True
		    # if different runs make sure you select the correct range of lumis
		    elif int(tmpbeam.Run) == tmprunfirst:
			if int(tmpbeam.IOVfirst) >= tmplumifirst: acceptiov1 = True
		    elif int(tmpbeam.Run) == tmprunlast:
			if int(tmpbeam.IOVlast) <= tmplumilast: acceptiov2 = True
		    elif tmprunfirst <= int(tmpbeam.Run) and tmprunlast >= int(tmpbeam.Run): 
			acceptiov1 = acceptiov2 = True
			
		    if acceptiov1 and acceptiov2:
			if isnan(tmpbeam.Z) or isnan(tmpbeam.Zerr) or isnan(tmpbeam.sigmaZerr) or isnan(tmpbeam.beamWidthXerr) or isnan(tmpbeam.beamWidthYerr):
                            print "invalid fit, NaN values!! skip Run "+str(tmpbeam.Run)+" IOV: "+str(tmpbeam.IOVfirst) + " to "+ str(tmpbeam.IOVlast)                       
			elif hasBX:
                            if (tmpBX in maplist) == False:
                                maplist[tmpBX] = [tmpbeam]
                            else:
                                maplist[tmpBX].append(tmpbeam)
                        else:
			    listbeam.append(tmpbeam)

		elif int(tmpbeam.IOVfirst) >= int(firstRun) and int(tmpbeam.IOVlast) <= int(lastRun):
		    if isnan(tmpbeam.Z) or isnan(tmpbeam.Zerr) or isnan(tmpbeam.sigmaZerr) or isnan(tmpbeam.beamWidthXerr) or isnan(tmpbeam.beamWidthYerr):
                        print "invalid fit, NaN values!! skip Run "+str(tmpbeam.Run)+" IOV: "+str(tmpbeam.IOVfirst) + " to "+ str(tmpbeam.IOVlast)
		    else:
			listbeam.append(tmpbeam)
                        
		tmpbeamsize = 0
		tmpbeam = BeamSpot()
                tmpBX = 0

    tmpfile.close()
    print " got total number of IOVs = " + str(len(listbeam)) + " from file " + fileName
    #print " run " + str(listbeam[3].IOVfirst ) + " " + str( listbeam[3].X )
    if hasBX:
        return maplist
    else:
        return listbeam
예제 #4
0
def createWeightedPayloadsNew(fileName,listbeam=[],weighted=True):
    newlistbeam = []
    docreate = False
    docheck = False
    lastPayload = listbeam[0]

    firstToUse = 0
    lastToUse = 0
    for ii in range(0,len(listbeam)):
        docreate = False
        if docheck:
            deltaX = delta(ibeam.X, ibeam.Xerr, inextbeam.X, inextbeam.Xerr) > 1.5
            deltaY = delta(ibeam.Y, ibeam.Yerr, inextbeam.Y, inextbeam.Yerr) > 1.5
            deltaZ = delta(ibeam.Z, ibeam.Zerr, inextbeam.Z, inextbeam.Zerr) > 2.5

            deltasigmaZ = delta(ibeam.sigmaZ, ibeam.sigmaZerr, inextbeam.sigmaZ, inextbeam.sigmaZerr) > 2.5
            deltadxdz   = delta(ibeam.dxdz, ibeam.dxdzerr, inextbeam.dxdz, inextbeam.dxdzerr) > 2.5
            deltadydz   = delta(ibeam.dydz, ibeam.dydzerr, inextbeam.dydz, inextbeam.dydzerr) > 2.5

            deltawidthX = delta(ibeam.beamWidthX, ibeam.beamWidthXerr, inextbeam.beamWidthX, inextbeam.beamWidthXerr) > 3
            deltawidthY = delta(ibeam.beamWidthY, ibeam.beamWidthYerr, inextbeam.beamWidthY, inextbeam.beamWidthYerr) > 3

            #if iNNbeam.Type != -1:
            #    deltaX = deltaX and delta(ibeam.X, ibeam.Xerr, iNNbeam.X, iNNbeam.Xerr) > 1.5
            #    deltaY = deltaY and delta(ibeam.Y, ibeam.Yerr, iNNbeam.Y, iNNbeam.Yerr) > 1.5
            #    deltaZ = deltaZ and delta(ibeam.Z, ibeam.Zerr, iNNbeam.Z, iNNbeam.Zerr) > 1.5
            #		
            #    deltasigmaZ = deltasigmaZ and delta(ibeam.sigmaZ, ibeam.sigmaZerr, iNNbeam.sigmaZ, iNNbeam.sigmaZerr) > 2.5
            #    deltadxdz   = deltadxdz and delta(ibeam.dxdz, ibeam.dxdzerr, iNNbeam.dxdz, iNNbeam.dxdzerr) > 2.5
            #    deltadydz   = deltadydz and delta(ibeam.dydz, ibeam.dydzerr, iNNbeam.dydz, iNNbeam.dydzerr) > 2.5
            #
            #    deltawidthX = deltawidthX and delta(ibeam.beamWidthX, ibeam.beamWidthXerr, iNNbeam.beamWidthX, iNNbeam.beamWidthXerr) > 3
            #    deltawidthY = deltawidthY and delta(ibeam.beamWidthY, ibeam.beamWidthYerr, iNNbeam.beamWidthY, iNNbeam.beamWidthYerr) > 3

            if deltaX or deltaY or deltaZ or deltasigmaZ or deltadxdz or deltadydz or deltawidthX or deltawidthY:
                if ii != 0:
                    docreate = True
                    lastToUse = ii-1
                #print "shift here: x="+str(deltaX)+" y="+str(deltaY)
                #print "x1 = "+ibeam.X + " x1err = "+ibeam.Xerr
                #print "x2 = "+inextbeam.X + " x2err = "+inextbeam.Xerr
                #print "Lumi1: "+str(ibeam.IOVfirst) + " Lumi2: "+str(inextbeam.IOVfirst)
                #print " x= "+ibeam.X+" +/- "+ibeam.Xerr
                #print "weighted average x = "+tmpbeam.X +" +//- "+tmpbeam.Xerr
                print "close payload because of movement in X= "+str(deltaX)+", Y= "+str(deltaY) + ", Z= "+str(deltaZ)+", sigmaZ= "+str(deltasigmaZ)+", dxdz= "+str(deltadxdz)+", dydz= "+str(deltadydz)+", widthX= "+str(deltawidthX)+", widthY= "+str(deltawidthY)
        
        #WARNING this will only be fine for Run based IOVs
        if ii >= len(listbeam) - 1 or listbeam[ii].Run != listbeam[ii+1].Run :
            print "close payload because end of run has been reached. Run " + listbeam[ii].Run
            docreate = True
            lastToUse = ii
        

         # check maximum lumi counts
#?        if countlumi == maxNlumis:
#?            print "close payload because maximum lumi sections accumulated within run "+ibeam.Run
#?            docreate = True
#?            countlumi = 0
        if docreate:
            tmpbeam = BeamSpot()
            for ibeam in listbeam[firstToUse:lastToUse]:
                (tmpbeam.X, tmpbeam.Xerr) = weight(tmpbeam.X, tmpbeam.Xerr, ibeam.X, ibeam.Xerr)
                (tmpbeam.Y, tmpbeam.Yerr) = weight(tmpbeam.Y, tmpbeam.Yerr, ibeam.Y, ibeam.Yerr)
                (tmpbeam.Z, tmpbeam.Zerr) = weight(tmpbeam.Z, tmpbeam.Zerr, ibeam.Z, ibeam.Zerr)
                (tmpbeam.sigmaZ, tmpbeam.sigmaZerr) = weight(tmpbeam.sigmaZ, tmpbeam.sigmaZerr, ibeam.sigmaZ, ibeam.sigmaZerr)
                (tmpbeam.dxdz, tmpbeam.dxdzerr) = weight(tmpbeam.dxdz, tmpbeam.dxdzerr, ibeam.dxdz, ibeam.dxdzerr)
                (tmpbeam.dydz, tmpbeam.dydzerr) = weight(tmpbeam.dydz, tmpbeam.dydzerr, ibeam.dydz, ibeam.dydzerr)
                #print "wx = " + ibeam.beamWidthX + " err= "+ ibeam.beamWidthXerr
                (tmpbeam.beamWidthX, tmpbeam.beamWidthXerr) = weight(tmpbeam.beamWidthX, tmpbeam.beamWidthXerr, ibeam.beamWidthX, ibeam.beamWidthXerr)
                (tmpbeam.beamWidthY, tmpbeam.beamWidthYerr) = weight(tmpbeam.beamWidthY, tmpbeam.beamWidthYerr, ibeam.beamWidthY, ibeam.beamWidthYerr)
            tmpbeam.IOVfirst     = listbeam[firstToUse].IOVfirst
            tmpbeam.IOVBeginTime = listbeam[firstToUse].IOVBeginTime
            tmpbeam.Run          = listbeam[firstToUse].Run
            tmpbeam.Type         = 2
            tmpbeam.IOVlast      = listbeam[lastToUse].IOVlast
            tmpbeam.IOVEndTime   = listbeam[lastToUse].IOVEndTime
            newlistbeam.append(tmpbeam)
            firstToUse = lastToUse+1
            print "Run: " + tmpbeam.Run + " Lumi1: " + str(tmpbeam.IOVfirst) + " Lumi2: " + str(tmpbeam.IOVlast)
            
    payloadfile = open(fileName,"w")
    for iload in newlistbeam:
        dump( iload, payloadfile )
    payloadfile.close()
예제 #5
0
def createWeightedPayloads(fileName,listbeam=[],weighted=True):
    newlistbeam = []
    tmpbeam = BeamSpot()
    docreate = True
    countlumi = 0
    tmprun = ""
    maxNlumis = 60
    if weighted:
        maxNlumis = 999999999
    for ii in range(0,len(listbeam)):
        ibeam = listbeam[ii]
        inextbeam = BeamSpot()
        iNNbeam = BeamSpot()
        if docreate:
            tmpbeam.IOVfirst = ibeam.IOVfirst
            tmpbeam.IOVBeginTime = ibeam.IOVBeginTime
            tmpbeam.Run = ibeam.Run
            tmpbeam.Type = 2
        docheck = False
        docreate = False
	#print "Currently testing ii="+str(ii)+" Lumi1: "+str(ibeam.IOVfirst)
	    
        # check last iov
        if ii < len(listbeam) - 1: 
            inextbeam = listbeam[ii+1]
            docheck = True
            if ii < len(listbeam) -2:
                iNNbeam = listbeam[ii+2]
        else:
            print "close payload because end of data has been reached. Run "+ibeam.Run
            docreate = True
        # check we run over the same run
        if ibeam.Run != inextbeam.Run:
            print "close payload because end of run "+ibeam.Run
            docreate = True
        # check maximum lumi counts
        if countlumi == maxNlumis -1:
            print "close payload because maximum lumi sections accumulated within run "+ibeam.Run
            docreate = True
            countlumi = 0
        # weighted average position
        (tmpbeam.X, tmpbeam.Xerr) = weight(tmpbeam.X, tmpbeam.Xerr, ibeam.X, ibeam.Xerr)
        (tmpbeam.Y, tmpbeam.Yerr) = weight(tmpbeam.Y, tmpbeam.Yerr, ibeam.Y, ibeam.Yerr)
        (tmpbeam.Z, tmpbeam.Zerr) = weight(tmpbeam.Z, tmpbeam.Zerr, ibeam.Z, ibeam.Zerr)
        (tmpbeam.sigmaZ, tmpbeam.sigmaZerr) = weight(tmpbeam.sigmaZ, tmpbeam.sigmaZerr, ibeam.sigmaZ, ibeam.sigmaZerr)
        (tmpbeam.dxdz, tmpbeam.dxdzerr) = weight(tmpbeam.dxdz, tmpbeam.dxdzerr, ibeam.dxdz, ibeam.dxdzerr)
        (tmpbeam.dydz, tmpbeam.dydzerr) = weight(tmpbeam.dydz, tmpbeam.dydzerr, ibeam.dydz, ibeam.dydzerr)
        #print "wx = " + ibeam.beamWidthX + " err= "+ ibeam.beamWidthXerr
        (tmpbeam.beamWidthX, tmpbeam.beamWidthXerr) = weight(tmpbeam.beamWidthX, tmpbeam.beamWidthXerr, ibeam.beamWidthX, ibeam.beamWidthXerr)
        (tmpbeam.beamWidthY, tmpbeam.beamWidthYerr) = weight(tmpbeam.beamWidthY, tmpbeam.beamWidthYerr, ibeam.beamWidthY, ibeam.beamWidthYerr)
        
        if weighted:
            docheck = False
        # check offsets
        #if False:
        if docheck:

            # define minimum limit
            min_limit = 0.0025
            
            # limit for x and y
            limit = float(ibeam.beamWidthX)/2.
            if limit < min_limit: limit = min_limit
            
            # check movements in X
            adelta1 = delta(ibeam.X, ibeam.Xerr, inextbeam.X, inextbeam.Xerr)
            adelta2 = (0.,1.e9)
            adelta1dxdz = delta(ibeam.dxdz, ibeam.dxdzerr, inextbeam.dxdz, inextbeam.dxdzerr)
            adelta2dxdz = (0.,1.e9)
            adelta1dydz = delta(ibeam.dydz, ibeam.dydzerr, inextbeam.dydz, inextbeam.dydzerr)
            adelta2dydz = (0.,1.e9)
            adelta1widthx = delta(ibeam.beamWidthX, ibeam.beamWidthXerr, inextbeam.beamWidthX, inextbeam.beamWidthXerr)
            adelta2widthx = (0.,1.e9)
            adelta1widthy = delta(ibeam.beamWidthY, ibeam.beamWidthYerr, inextbeam.beamWidthY, inextbeam.beamWidthYerr)
            adelta2widthy = (0.,1.e9)
            adelta1z0 = delta(ibeam.Z, ibeam.Zerr, inextbeam.Z, inextbeam.Zerr)
            adelta1sigmaZ = delta(ibeam.sigmaZ, ibeam.sigmaZerr, inextbeam.sigmaZ, inextbeam.sigmaZerr)
            
            if iNNbeam.Type != -1:
                adelta2 = delta(inextbeam.X, inextbeam.Xerr, iNNbeam.X, iNNbeam.Xerr)
                adelta2dxdz = delta(inextbeam.dxdz, inextbeam.dxdzerr, iNNbeam.dxdz, iNNbeam.dxdzerr)
                adelta2dydz = delta(inextbeam.dydz, inextbeam.dydzerr, iNNbeam.dydz, iNNbeam.dydzerr)
                adelta2widthx = delta(inextbeam.beamWidthX, inextbeam.beamWidthXerr, iNNbeam.beamWidthX, iNNbeam.beamWidthXerr)
                adelta2widthy = delta(inextbeam.beamWidthY, inextbeam.beamWidthYerr, iNNbeam.beamWidthY, iNNbeam.beamWidthYerr)
                
            deltaX = deltaSig(adelta1) > 3.5 and adelta1[0] >= limit
            if ii < len(listbeam) -2:
                if deltaX==False and adelta1[0]*adelta2[0] > 0. and  math.fabs(adelta1[0]+adelta2[0]) >= limit:
                    #print " positive, "+str(adelta1[0]+adelta2[0])+ " limit="+str(limit)
                    deltaX = True
                elif deltaX==True and adelta1[0]*adelta2[0]<=0 and adelta2[0] != 0 and math.fabs(adelta1[0]/adelta2[0]) > 0.33 and math.fabs(adelta1[0]/adelta2[0]) < 3:
                    deltaX = False
                    #print " negative, "+str(adelta1[0]/adelta2[0])
                #else:
                #    print str(adelta1[0]/adelta2[0])

            # check movemnts in Y
            adelta1 = delta(ibeam.Y, ibeam.Yerr, inextbeam.Y, inextbeam.Yerr)
            adelta2 = (0.,1.e9)
            if iNNbeam.Type != -1:
                adelta2 = delta(inextbeam.Y, inextbeam.Yerr, iNNbeam.Y, iNNbeam.Yerr)
                
            deltaY = deltaSig(adelta1) > 3.5 and adelta1[0] >= limit
            if ii < len(listbeam) -2:
                if deltaY==False and adelta1[0]*adelta2[0] > 0. and  math.fabs(adelta1[0]+adelta2[0]) >= limit:
                    deltaY = True
                elif deltaY==True and adelta1[0]*adelta2[0]<=0 and adelta2[0] != 0 and math.fabs(adelta1[0]/adelta2[0]) > 0.33 and math.fabs(adelta1[0]/adelta2[0]) < 3:
                    deltaY = False
            # check movements in Z                                                    
            
            limit = float(ibeam.sigmaZ)/2.
            deltaZ = deltaSig(adelta1z0) > 3.5 and math.fabs(adelta1z0[0]) >= limit
            
            deltasigmaZ = deltaSig(adelta1sigmaZ) > 5.0

            # check dxdz
            adelta = delta(ibeam.dxdz, ibeam.dxdzerr, inextbeam.dxdz, inextbeam.dxdzerr)
            deltadxdz   = deltaSig(adelta) > 5.0
            if deltadxdz and adelta1dxdz[0]*adelta2dxdz[0]<=0 and adelta2dxdz[0] != 0 and math.fabs(adelta1dxdz[0]/adelta2dxdz[0]) > 0.33 and math.fabs(adelta1dxdz[0]/adelta2dxdz[0]) < 3:
                deltadxdz = False
            # check dydz
            adelta = delta(ibeam.dydz, ibeam.dydzerr, inextbeam.dydz, inextbeam.dydzerr)
            deltadydz   = deltaSig(adelta) > 5.0
            if deltadydz and adelta1dydz[0]*adelta2dydz[0]<=0 and adelta2dydz[0] != 0 and math.fabs(adelta1dydz[0]/adelta2dydz[0]) > 0.33 and math.fabs(adelta1dydz[0]/adelta2dydz[0]) < 3:
                deltadydz = False
            
            adelta = delta(ibeam.beamWidthX, ibeam.beamWidthXerr, inextbeam.beamWidthX, inextbeam.beamWidthXerr)
            deltawidthX = deltaSig(adelta) > 5
            if deltawidthX and adelta1widthx[0]*adelta2widthx[0]<=0 and adelta2widthx[0] != 0 and math.fabs(adelta1widthx[0]/adelta2widthx[0]) > 0.33 and math.fabs(adelta1widthx[0]/adelta2widthx[0]) < 3:
                deltawidthX = False
                
            adelta = delta(ibeam.beamWidthY, ibeam.beamWidthYerr, inextbeam.beamWidthY, inextbeam.beamWidthYerr) 
            deltawidthY = deltaSig(adelta) > 5
            if deltawidthY and adelta1widthy[0]*adelta2widthy[0]<=0 and adelta2widthy[0] != 0 and math.fabs(adelta1widthy[0]/adelta2widthy[0]) > 0.33 and math.fabs(adelta1widthy[0]/adelta2widthy[0]) < 3:
                deltawidthY = False
            #if iNNbeam.Type != -1:
            #    deltaX = deltaX and delta(ibeam.X, ibeam.Xerr, iNNbeam.X, iNNbeam.Xerr) > 1.5
            #    deltaY = deltaY and delta(ibeam.Y, ibeam.Yerr, iNNbeam.Y, iNNbeam.Yerr) > 1.5
            #    deltaZ = deltaZ and delta(ibeam.Z, ibeam.Zerr, iNNbeam.Z, iNNbeam.Zerr) > 1.5
            #		
            #    deltasigmaZ = deltasigmaZ and delta(ibeam.sigmaZ, ibeam.sigmaZerr, iNNbeam.sigmaZ, iNNbeam.sigmaZerr) > 2.5
            #    deltadxdz   = deltadxdz and delta(ibeam.dxdz, ibeam.dxdzerr, iNNbeam.dxdz, iNNbeam.dxdzerr) > 2.5
            #    deltadydz   = deltadydz and delta(ibeam.dydz, ibeam.dydzerr, iNNbeam.dydz, iNNbeam.dydzerr) > 2.5
            #
            #    deltawidthX = deltawidthX and delta(ibeam.beamWidthX, ibeam.beamWidthXerr, iNNbeam.beamWidthX, iNNbeam.beamWidthXerr) > 3
            #    deltawidthY = deltawidthY and delta(ibeam.beamWidthY, ibeam.beamWidthYerr, iNNbeam.beamWidthY, iNNbeam.beamWidthYerr) > 3

            if deltaX or deltaY or deltaZ or deltasigmaZ or deltadxdz or deltadydz or deltawidthX or deltawidthY:
                docreate = True
                #print "shift here: x="+str(deltaX)+" y="+str(deltaY)
                #print "x1 = "+ibeam.X + " x1err = "+ibeam.Xerr
                #print "x2 = "+inextbeam.X + " x2err = "+inextbeam.Xerr
                #print "Lumi1: "+str(ibeam.IOVfirst) + " Lumi2: "+str(inextbeam.IOVfirst)
                #print " x= "+ibeam.X+" +/- "+ibeam.Xerr
                #print "weighted average x = "+tmpbeam.X +" +//- "+tmpbeam.Xerr
                print "close payload because of movement in X= "+str(deltaX)+", Y= "+str(deltaY) + ", Z= "+str(deltaZ)+", sigmaZ= "+str(deltasigmaZ)+", dxdz= "+str(deltadxdz)+", dydz= "+str(deltadydz)+", widthX= "+str(deltawidthX)+", widthY= "+str(deltawidthY)
        if docreate:
            #if ii == len(listbeam)-1:
            tmpbeam.IOVlast = ibeam.IOVlast
            tmpbeam.IOVEndTime = ibeam.IOVEndTime
            print "  Run: "+tmpbeam.Run +" Lumi1: "+str(tmpbeam.IOVfirst) + " Lumi2: "+str(tmpbeam.IOVlast)
            newlistbeam.append(tmpbeam)
            tmpbeam = BeamSpot()
            countlumi = 0
        tmprun = ibeam.Run
        countlumi += 1

    payloadfile = open(fileName,"w")
    for iload in newlistbeam:
        dump( iload, payloadfile )
    payloadfile.close()
    return newlistbeam
예제 #6
0
def readBeamSpotFile(fileName,listbeam=[],IOVbase="runbase", firstRun='1',lastRun='4999999999'):
    tmpbeam = BeamSpot()
    tmpbeamsize = 0
    
    #firstRun = "1"
    #lastRun  = "4999999999"
    if IOVbase == "lumibase" and firstRun=='1' and lastRun=='4999999999' :
    	firstRun = "1:1"
        lastRun = "4999999999:4999999999"

    inputfiletype = 0
    #print "first = " +firstRun
    #print "last = " +lastRun

    # for bx
    maplist = {}
    hasBX = False
            
    tmpfile = open(fileName)
    atmpline = tmpfile.readline()
    if atmpline.find('Runnumber') != -1:
	inputfiletype = 1
        if len(atmpline.split()) > 2:
            hasBX = True
            print " Input data has been calculated as function of BUNCH CROSSINGS."
    tmpfile.seek(0)

        
    if inputfiletype ==1:

	tmpBX = 0
	for line in tmpfile:
            
	    if line.find('Type') != -1:
		tmpbeam.Type = int(line.split()[1])
		tmpbeamsize += 1
	    if line.find('X0') != -1:
		tmpbeam.X = line.split()[1]
		#tmpbeam.Xerr = line.split()[4]
		tmpbeamsize += 1
            #print " x = " + str(tmpbeam.X)
	    if line.find('Y0') != -1:
		tmpbeam.Y = line.split()[1]
		#tmpbeam.Yerr = line.split()[4]
		tmpbeamsize += 1
            #print " y =" + str(tmpbeam.Y)
	    if line.find('Z0') != -1 and line.find('sigmaZ0') == -1:
		tmpbeam.Z = line.split()[1]
		#tmpbeam.Zerr = line.split()[4]
		tmpbeamsize += 1
	    if line.find('sigmaZ0') !=-1:
		tmpbeam.sigmaZ = line.split()[1]
		#tmpbeam.sigmaZerr = line.split()[5]
		tmpbeamsize += 1
            if line.find('dxdz') != -1:
		tmpbeam.dxdz = line.split()[1]
		#tmpbeam.dxdzerr = line.split()[4]
		tmpbeamsize += 1
	    if line.find('dydz') != -1:
		tmpbeam.dydz = line.split()[1]
		#tmpbeam.dydzerr = line.split()[4]
		tmpbeamsize += 1
	    if line.find('BeamWidthX') != -1:
		tmpbeam.beamWidthX = line.split()[1]
		#tmpbeam.beamWidthXerr = line.split()[6]
		tmpbeamsize += 1
	    if line.find('BeamWidthY') != -1:
		tmpbeam.beamWidthY = line.split()[1]
		#tmpbeam.beamWidthYerr = line.split()[6]
		tmpbeamsize += 1
	    if line.find('Cov(0,j)') != -1:
		tmpbeam.Xerr = str(math.sqrt( float( line.split()[1] ) ) )
		tmpbeamsize += 1
	    if line.find('Cov(1,j)') != -1:
		tmpbeam.Yerr = str(math.sqrt( float( line.split()[2] ) ) )
		tmpbeamsize += 1
	    if line.find('Cov(2,j)') != -1:
		tmpbeam.Zerr = str(math.sqrt( float( line.split()[3] ) ) )
		tmpbeamsize += 1
	    if line.find('Cov(3,j)') != -1:
		tmpbeam.sigmaZerr = str(math.sqrt( float( line.split()[4] ) ) )
		tmpbeamsize += 1
            if line.find('Cov(4,j)') != -1:
		tmpbeam.dxdzerr = str(math.sqrt( float( line.split()[5] ) ) )
		tmpbeamsize += 1
	    if line.find('Cov(5,j)') != -1:
		tmpbeam.dydzerr = str(math.sqrt( float( line.split()[6] ) ) )
		tmpbeamsize += 1
	    if line.find('Cov(6,j)') != -1:
		tmpbeam.beamWidthXerr = str(math.sqrt( float( line.split()[7] ) ) )
                tmpbeam.beamWidthYerr = tmpbeam.beamWidthXerr
		tmpbeamsize += 1
	    if line.find('LumiRange')  != -1:
                if IOVbase=="lumibase":
                    tmpbeam.IOVfirst = line.split()[1]
                    tmpbeam.IOVlast = line.split()[3]
		tmpbeamsize += 1
            if line.find('Runnumber') != -1:
		tmpbeam.Run = line.split()[1]
		if IOVbase == "runbase":
		    tmpbeam.IOVfirst = line.split()[1]
		    tmpbeam.IOVlast = line.split()[1]
                if hasBX:
                    tmpBX = line.split()[3]
		tmpbeamsize += 1
            if line.find('BeginTimeOfFit') != -1:
		tmpbeam.IOVBeginTime = line.split()[1] +" "+line.split()[2] +" "+line.split()[3]
		if IOVbase =="timebase":
		    tmpbeam.IOVfirst =  time.mktime( time.strptime(line.split()[1] +  " " + line.split()[2] + " " + line.split()[3],"%Y.%m.%d %H:%M:%S %Z") )
		tmpbeamsize += 1
            if line.find('EndTimeOfFit') != -1:
		tmpbeam.IOVEndTime = line.split()[1] +" "+line.split()[2] +" "+line.split()[3]
		if IOVbase =="timebase":
		    tmpbeam.IOVlast = time.mktime( time.strptime(line.split()[1] +  " " + line.split()[2] + " " + line.split()[3],"%Y.%m.%d %H:%M:%S %Z") )
		tmpbeamsize += 1
	    if tmpbeamsize == 20:
		if IOVbase=="lumibase":
		    tmprunfirst = int(firstRun.split(":")[0])
		    tmprunlast  = int(lastRun.split(":")[0])
		    tmplumifirst = int(firstRun.split(":")[1])
		    tmplumilast  = int(lastRun.split(":")[1])
		    acceptiov1 = acceptiov2 = False
		    # check lumis in the same run
		    if tmprunfirst == tmprunlast and int(tmpbeam.Run)==tmprunfirst:
			if int(tmpbeam.IOVfirst) >= tmplumifirst and int(tmpbeam.IOVlast)<=tmplumilast:
			    acceptiov1 = acceptiov2 = True
		    # if different runs make sure you select the correct range of lumis
		    elif int(tmpbeam.Run) == tmprunfirst:
			if int(tmpbeam.IOVfirst) >= tmplumifirst: acceptiov1 = True
		    elif int(tmpbeam.Run) == tmprunlast:
			if int(tmpbeam.IOVlast) <= tmplumilast: acceptiov2 = True
		    elif tmprunfirst <= int(tmpbeam.Run) and tmprunlast >= int(tmpbeam.Run): 
			acceptiov1 = acceptiov2 = True
			
		    if acceptiov1 and acceptiov2:
			if tmpbeam.Type != 2:
			    print "invalid fit, skip Run "+str(tmpbeam.Run)+" IOV: "+str(tmpbeam.IOVfirst) + " to "+ str(tmpbeam.IOVlast)
                        elif isnan(tmpbeam.Z) or isnan(tmpbeam.Zerr) or isnan(tmpbeam.sigmaZerr) or isnan(tmpbeam.beamWidthXerr) or isnan(tmpbeam.beamWidthYerr):
                            print "invalid fit, NaN values!! skip Run "+str(tmpbeam.Run)+" IOV: "+str(tmpbeam.IOVfirst) + " to "+ str(tmpbeam.IOVlast)                       
			elif hasBX:
                            if maplist.has_key(tmpBX) == False:
                                maplist[tmpBX] = [tmpbeam]
                            else:
                                maplist[tmpBX].append(tmpbeam)
                        else:
			    listbeam.append(tmpbeam)

		elif int(tmpbeam.IOVfirst) >= int(firstRun) and int(tmpbeam.IOVlast) <= int(lastRun):
		    if tmpbeam.Type != 2:
			print "invalid fit, skip Run "+str(tmpbeam.Run)+" IOV: "+str(tmpbeam.IOVfirst) + " to "+ str(tmpbeam.IOVlast)
                    elif isnan(tmpbeam.Z) or isnan(tmpbeam.Zerr) or isnan(tmpbeam.sigmaZerr) or isnan(tmpbeam.beamWidthXerr) or isnan(tmpbeam.beamWidthYerr):
                        print "invalid fit, NaN values!! skip Run "+str(tmpbeam.Run)+" IOV: "+str(tmpbeam.IOVfirst) + " to "+ str(tmpbeam.IOVlast)
		    else:
			listbeam.append(tmpbeam)
                        
		tmpbeamsize = 0
		tmpbeam = BeamSpot()
                tmpBX = 0
    else:

	for line in tmpfile:
	
	    if line.find('X0') != -1:
		tmpbeam.X = line.split()[2]
		tmpbeam.Xerr = line.split()[4]
		tmpbeamsize += 1
            #print " x = " + str(tmpbeam.X)
	    if line.find('Y0') != -1:
		tmpbeam.Y = line.split()[2]
		tmpbeam.Yerr = line.split()[4]
		tmpbeamsize += 1
            #print " y =" + str(tmpbeam.Y)
	    if line.find('Z0') != -1 and line.find('Sigma Z0') == -1:
		tmpbeam.Z = line.split()[2]
		tmpbeam.Zerr = line.split()[4]
		tmpbeamsize += 1
            #print " z =" + str(tmpbeam.Z)
	    if line.find('Sigma Z0') !=-1:
		tmpbeam.sigmaZ = line.split()[3]
		tmpbeam.sigmaZerr = line.split()[5]
		tmpbeamsize += 1
	    if line.find('dxdz') != -1:
		tmpbeam.dxdz = line.split()[2]
		tmpbeam.dxdzerr = line.split()[4]
		tmpbeamsize += 1
	    if line.find('dydz') != -1:
		tmpbeam.dydz = line.split()[2]
		tmpbeam.dydzerr = line.split()[4]
		tmpbeamsize += 1
	    if line.find('Beam Width X') != -1:
		tmpbeam.beamWidthX = line.split()[4]
		tmpbeam.beamWidthXerr = line.split()[6]
		tmpbeamsize += 1
	    if line.find('Beam Width Y') != -1:
		tmpbeam.beamWidthY = line.split()[4]
		tmpbeam.beamWidthYerr = line.split()[6]
		tmpbeamsize += 1
	#if line.find('Run ') != -1:
	    if line.find('for runs')  != -1:
	    #tmpbeam.IOVfirst = line.split()[6].strip(',')
	        tmpbeam.Run      = line.split()[2]
		if IOVbase == "runbase":
		  tmpbeam.IOVfirst = line.split()[2]
		  tmpbeam.IOVlast = line.split()[4]
		tmpbeamsize += 1
	    if line.find('LumiSection')  != -1:
                if IOVbase=="lumibase":
                    tmpbeam.IOVfirst = line.split()[10]
                    tmpbeam.IOVlast = line.split()[10]
		tmpbeamsize += 1
	    if tmpbeamsize == 10:

		if IOVbase=="lumibase":
		    tmprunfirst = int(firstRun.split(":")[0])
		    tmprunlast  = int(lastRun.split(":")[0])
		    tmplumifirst = int(firstRun.split(":")[1])
		    tmplumilast  = int(lastRun.split(":")[1])
		    acceptiov1 = acceptiov2 = False
		    # check lumis in the same run
		    if tmprunfirst == tmprunlast and int(tmpbeam.Run)==tmprunfirst:
			if int(tmpbeam.IOVfirst) >= tmplumifirst and int(tmpbeam.IOVlast)<=tmplumilast:
			    acceptiov1 = acceptiov2 = True
		    # if different runs make sure you select the correct range of lumis
		    elif int(tmpbeam.Run) == tmprunfirst:
			if int(tmpbeam.IOVfirst) >= tmplumifirst: acceptiov1 = True
		    elif int(tmpbeam.Run) == tmprunlast:
			if int(tmpbeam.IOVlast) <= tmplumilast: acceptiov2 = True
		    elif tmprunfirst <= int(tmpbeam.Run) and tmprunlast >= int(tmpbeam.Run): 
			acceptiov1 = acceptiov2 = True
			
		    if acceptiov1 and acceptiov2:
			if isnan(tmpbeam.Z) or isnan(tmpbeam.Zerr) or isnan(tmpbeam.sigmaZerr) or isnan(tmpbeam.beamWidthXerr) or isnan(tmpbeam.beamWidthYerr):
                            print "invalid fit, NaN values!! skip Run "+str(tmpbeam.Run)+" IOV: "+str(tmpbeam.IOVfirst) + " to "+ str(tmpbeam.IOVlast)                       
			elif hasBX:
                            if maplist.has_key(tmpBX) == False:
                                maplist[tmpBX] = [tmpbeam]
                            else:
                                maplist[tmpBX].append(tmpbeam)
                        else:
			    listbeam.append(tmpbeam)

		elif int(tmpbeam.IOVfirst) >= int(firstRun) and int(tmpbeam.IOVlast) <= int(lastRun):
		    if isnan(tmpbeam.Z) or isnan(tmpbeam.Zerr) or isnan(tmpbeam.sigmaZerr) or isnan(tmpbeam.beamWidthXerr) or isnan(tmpbeam.beamWidthYerr):
                        print "invalid fit, NaN values!! skip Run "+str(tmpbeam.Run)+" IOV: "+str(tmpbeam.IOVfirst) + " to "+ str(tmpbeam.IOVlast)
		    else:
			listbeam.append(tmpbeam)
                        
		tmpbeamsize = 0
		tmpbeam = BeamSpot()
                tmpBX = 0

    tmpfile.close()
    print " got total number of IOVs = " + str(len(listbeam)) + " from file " + fileName
    #print " run " + str(listbeam[3].IOVfirst ) + " " + str( listbeam[3].X )
    if hasBX:
        return maplist
    else:
        return listbeam