#!/usr/bin/env python
import sys

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

import numpy

from glue.lal import Cache
from glue.ligolw import lsctables, table, utils

import xmlutils

if len(sys.argv) > 2:
    sngl_table = table.get_table(utils.load_filename(sys.argv[2]), lsctables.SnglInspiralTable.tableName)
    net_snr = numpy.sqrt(sum([si.snr for si in sngl_table]))

evidence = []
for pfn in Cache.fromfile(open(sys.argv[1])).pfnlist():
    evidence.extend([si.snr for si in xmlutils.db_to_samples(pfn, lsctables.SnglInspiralTable, ["snr"])])

pyplot.figure()
pyplot.hist(evidence, bins=50)
pyplot.xlabel("log(evidence)")
pyplot.grid()
if len(sys.argv) > 2:
    pyplot.axvline(3*net_snr**2/2, color='k')
pyplot.savefig("logevidence_hist.png")
