#!/usr/bin/env python
import sys
import glob

import matplotlib
matplotlib.use("Agg")
from matplotlib import pyplot

import numpy

from glue.ligolw import lsctables, table, utils

loge, n = [], []
sngl_table = table.get_table(utils.load_filename(sys.argv[1]), lsctables.SnglInspiralTable.tableName)
net_snr = numpy.sqrt(sum([si.snr for si in sngl_table]))
for logf in glob.glob(sys.argv[2] + "/integrate*.out"):
#for logf in glob.glob("sim_id_*/logs/integrate*.out"):
    print logf
    with open(logf) as flog:
        lines = flog.readlines()
        if len(lines) < 100:
            continue
        logevidence = lines[-2].strip().split()[2]
        neff = lines[-1].strip().split()[3]
        loge.append(float(logevidence))
        n.append(float(neff))

pyplot.figure()
pyplot.scatter(loge, n)
pyplot.xlabel("log(evidence)")
pyplot.ylabel("n_eff")
pyplot.grid()
pyplot.axvline(3*net_snr**2/2, color='k')
pyplot.savefig("neff_evid.png")
