float(averages[iring][s]) + float(samples[s])
                for s in xrange(len(samples))
            ]
            filledCry[iring] += 1

    print "Starting the second loop..."
    eenewdata = {}
    partition = '0'
    total = 0
    unchanged = 0
    averaged = 0
    averagedtoneighbor = 0
    for detid, val in detids.geometry.iteritems():
        key = (partition, detid)
        (x, y, z) = detids.xyz(detid)
        if detids.subdet(detid) == 1: continue  # barrel
        x0 = x - 50
        y0 = y - 50
        ir = int(math.sqrt(x0 * x0 + y0 * y0))
        iring = ringmap.getRing(False, x, y, z)
        #if iring==-1:
        #    continue # not valid x,y
        total += 1
        if ir > maxIr:
            if key in eedata:
                eenewdata[key] = eedata[key]
                unchanged += 1
            else:
                pass  # leave empty crystals that have no measurements in low eta EE
        else:
            if iring not in filledCry: