From c1ae556cbb4a326717c2618343bf853f660cbb43 Mon Sep 17 00:00:00 2001
From: "chad.hanna" <crh184@psu.edu>
Date: Mon, 15 Jul 2019 05:49:32 -0700
Subject: [PATCH] inspiral.py lloidhandler.py: add pastro to gracedb uploads

---
 gstlal-inspiral/python/inspiral.py     | 6 ++++--
 gstlal-inspiral/python/lloidhandler.py | 5 ++++-
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/gstlal-inspiral/python/inspiral.py b/gstlal-inspiral/python/inspiral.py
index edeafca166..4c5a99c4b1 100644
--- a/gstlal-inspiral/python/inspiral.py
+++ b/gstlal-inspiral/python/inspiral.py
@@ -578,7 +578,7 @@ class GracedBWrapper(object):
 		self.__upload_aux_data(message, filename, tag, fobj.getvalue(), gracedb_ids)
 		del fobj
 
-	def do_alerts(self, last_coincs, psddict, rankingstat_xmldoc_func, seglistdicts):
+	def do_alerts(self, last_coincs, psddict, rankingstat_xmldoc_func, seglistdicts, get_p_astro_func):
 		gracedb_ids = []
 
 		# no-op short circuit
@@ -820,6 +820,8 @@ class GracedBWrapper(object):
 							if self.verbose:
 								print >>sys.stderr, "event assigned grace ID %s" % resp_json["graceid"]
 							gracedb_ids.append(resp_json["graceid"])
+							p_astro = get_p_astro_func(coinc_event.likelihood, last_coincs.sngl_inspirals(coinc_event.coinc_event_id)[0].mass1, last_coincs.sngl_inspirals(coinc_event.coinc_event_id)[0].mass2, coinc_inspiral_index[coinc_event.coinc_event_id].snr, coinc_inspiral_index[coinc_event.coinc_event_id].combined_far)
+							self.__upload_aux_data("GstLAL internally computed p-astro", "gstlal_p_astro.json", "p_astro", p_astro, [gracedb_ids[-1]])
 							break
 					print >>sys.stderr, "gracedb upload of %s failed on attempt %d/%d: %d: %s"  % (filename, attempt, self.retries, resp.status, httplib.responses.get(resp.status, "Unknown"))
 					print >>sys.stderr, resp_json
@@ -835,7 +837,7 @@ class GracedBWrapper(object):
 			except OSError:
 				pass
 			with open(os.path.join("gracedb_uploads", filename), "w") as fileobj:
-                               ligolw_utils.write_fileobj(xmldoc, fileobj, gz = False)
+				ligolw_utils.write_fileobj(xmldoc, fileobj, gz = False)
 
 			xmldoc.unlink()
 
diff --git a/gstlal-inspiral/python/lloidhandler.py b/gstlal-inspiral/python/lloidhandler.py
index e09d011c78..b4b9264ea5 100644
--- a/gstlal-inspiral/python/lloidhandler.py
+++ b/gstlal-inspiral/python/lloidhandler.py
@@ -82,6 +82,7 @@ from ligo.lw.utils import segments as ligolw_segments
 from gstlal import bottle
 from gstlal import far
 from gstlal import inspiral
+from gstlal import p_astro_gstlal
 from gstlal import pipeio
 from gstlal import simplehandler
 from gstlal import streamthinca
@@ -1296,6 +1297,8 @@ class Handler(simplehandler.Handler):
 		ligolw_process.set_process_end_time(process)
 		return xmldoc
 
+	def __get_p_astro_json(self, lr, m1, m2, snr, far):
+		return p_astro_gstlal.compute_p_astro(lr, m1, m2, snr, far, self.rankingstatpdf)
 
 	def __get_rankingstat_xmldoc_for_gracedb(self):
 		# FIXME:  remove this wrapper when the horizon history
@@ -1362,7 +1365,7 @@ class Handler(simplehandler.Handler):
 		assert self.fapfar is not None
 
 		# do alerts
-		self.gracedbwrapper.do_alerts(last_coincs, self.psds, self.__get_rankingstat_xmldoc_for_gracedb, self.segmentstracker.seglistdicts)
+		self.gracedbwrapper.do_alerts(last_coincs, self.psds, self.__get_rankingstat_xmldoc_for_gracedb, self.segmentstracker.seglistdicts, self.__get_p_astro_json)
 
 
 	def web_get_sngls_snr_threshold(self):
-- 
GitLab