From 859bde3a2cb0bc4a00f5d7b283ccc336f8379f2b Mon Sep 17 00:00:00 2001
From: Roberto De Pietri <roberto.depietri@unipr.it>
Date: Thu, 10 Aug 2023 10:33:55 +0200
Subject: [PATCH] Fix initial notice problem with conntent not conforming to
 prefered event.

---
 gwcelery/tasks/orchestrator.py            | 36 +++++++++++++++++++----
 gwcelery/tests/test_tasks_orchestrator.py | 13 ++++++--
 2 files changed, 40 insertions(+), 9 deletions(-)

diff --git a/gwcelery/tasks/orchestrator.py b/gwcelery/tasks/orchestrator.py
index 43e3ea1c3..53f2880eb 100644
--- a/gwcelery/tasks/orchestrator.py
+++ b/gwcelery/tasks/orchestrator.py
@@ -997,9 +997,33 @@ def earlywarning_preliminary_initial_update_alert(
     skymap_filename, em_bright_filename, p_astro_filename = filenames
     combined_skymap_filename = None
     combined_skymap_needed = False
-    skymap_needed = (skymap_filename is None)
-    em_bright_needed = (em_bright_filename is None)
-    p_astro_needed = (p_astro_filename is None)
+
+    # Fix required product depending on the pipeline
+    # of the prefered event
+    ev_group = superevent['preferred_event_data']['group'].lower()
+    ev_pipeline = superevent['preferred_event_data']['pipeline'].lower()
+    if ev_group == 'cbc':
+        skymap_needed = (skymap_filename is None)
+        em_bright_needed = (em_bright_filename is None)
+        p_astro_needed = (p_astro_filename is None)
+        filename_skymap = '.multiorder.fits'
+        filename_em_bright = 'em_bright.json'
+        filename_p_astro = ev_pipeline + '.p_astro.json'
+    elif ev_group == 'burst':
+        skymap_needed = (skymap_filename is None)
+        em_bright_needed = False
+        p_astro_needed = False
+        filename_skymap = ev_pipeline + '.multiorder.fits'
+        filename_em_bright = 'not_required'
+        filename_p_astro = 'not_required'
+    else:
+        skymap_needed = (skymap_filename is None)
+        em_bright_needed = False
+        p_astro_needed = False
+        filename_skymap = '.multiorder.fits'
+        filename_em_bright = 'not_required'
+        filename_p_astro = 'not_required'
+
     raven_coinc = ('RAVEN_ALERT' in labels and bool(superevent['em_type']))
     if raven_coinc:
         ext_labels = gracedb.get_labels(superevent['em_type'])
@@ -1017,17 +1041,17 @@ def earlywarning_preliminary_initial_update_alert(
                 continue
             if skymap_needed \
                     and {'sky_loc', 'public'}.issubset(t) \
-                    and f.endswith('.multiorder.fits') \
+                    and f.endswith(filename_skymap) \
                     and 'combined' not in f:
                 skymap_filename = fv
             if em_bright_needed \
                     and 'em_bright' in t \
-                    and f.endswith('.json'):
+                    and f.endswith(filename_em_bright):
                 em_bright_filename = fv
             if p_astro_needed \
                     and {'public'}.issubset(t) \
                     and 'p_astro' in t \
-                    and f.endswith('.json'):
+                    and f.endswith(filename_p_astro):
                 p_astro_filename = fv
             if combined_skymap_needed \
                     and {'sky_loc', 'ext_coinc'}.issubset(t) \
diff --git a/gwcelery/tests/test_tasks_orchestrator.py b/gwcelery/tests/test_tasks_orchestrator.py
index b91287f2e..f095407cd 100644
--- a/gwcelery/tests/test_tasks_orchestrator.py
+++ b/gwcelery/tests/test_tasks_orchestrator.py
@@ -455,6 +455,8 @@ def superevent_initial_alert_download(filename, graceid):
         return json.dumps({'HasNS': 0.0, 'HasRemnant': 0.0})
     elif filename == 'p_astro.json,0':
         return b'{"BNS": 0.94, "NSBH": 0.03, "BBH": 0.02, "Terrestrial": 0.01}'
+    elif filename == 'gstlal.p_astro.json,0':
+        return b'{"BNS": 0.94, "NSBH": 0.03, "BBH": 0.02, "Terrestrial": 0.01}'
     elif filename == 'foobar.multiorder.fits,0':
         return 'contents of foobar.multiorder.fits,0'
     elif 'combined-ext.multiorder.fits' in filename:
@@ -482,7 +484,7 @@ def _mock_get_log(se_id):
              'filename': 'em_bright.json',
              'file_version': 0},
             {'tag_names': ['p_astro', 'public'],
-             'filename': 'p_astro.json',
+             'filename': 'gstlal.p_astro.json',
              'file_version': 0}]
     if se_id == 'S2468':
         logs.append({'tag_names': ['sky_loc', 'ext_coinc'],
@@ -532,6 +534,10 @@ def test_handle_superevent_initial_alert(mock_create_initial_circular,
             'labels': labels,
             'category': 'Production',
             'superevent_id': superevent_id,
+            'preferred_event_data': {
+                'group': 'CBC',
+                'pipeline': 'gstlal',
+                'search': 'AllSky'},
             'em_type': ext_id if labels else '',
             'category': 'Production'}
     }
@@ -561,7 +567,8 @@ def test_handle_superevent_initial_alert(mock_create_initial_circular,
         (superevent_initial_alert_download('foobar.multiorder.fits,0',
                                            superevent_id),
          superevent_initial_alert_download('em_bright.json,0', superevent_id),
-         superevent_initial_alert_download('p_astro.json,0', superevent_id)) +
+         superevent_initial_alert_download('gstlal.p_astro.json,0',
+                                           superevent_id)) +
         ((6 if combined_skymap_needed else 4) * (None,)),
         alert['object'], 'initial', raven_coinc='RAVEN_ALERT' in labels,
         combined_skymap_filename=combined_skymap_filename)
@@ -573,7 +580,7 @@ def test_handle_superevent_initial_alert(mock_create_initial_circular,
     mock_create_tag.assert_has_calls(
         [call('foobar.multiorder.fits,0', 'public', superevent_id),
          call('em_bright.json,0', 'public', superevent_id),
-         call('p_astro.json,0', 'public', superevent_id),
+         call('gstlal.p_astro.json,0', 'public', superevent_id),
          call('S1234-Initial-1.xml', 'public', superevent_id)],
         any_order=True)
     mock_expose.assert_called_once_with(superevent_id)
-- 
GitLab