From 08ea8e62e7bc9341f8c23695b1b9d458510b2d22 Mon Sep 17 00:00:00 2001
From: Tamas Gal <tgal@km3net.de>
Date: Wed, 20 Feb 2019 14:01:31 +0100
Subject: [PATCH] Retrieve t0set from the DB automatically

---
 scripts/ztplot.py | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)

diff --git a/scripts/ztplot.py b/scripts/ztplot.py
index d6b26b1..7b2936a 100755
--- a/scripts/ztplot.py
+++ b/scripts/ztplot.py
@@ -61,8 +61,15 @@ class ZTPlot(Module):
         self.plots_path = self.require('plots_path')
         self.ytick_distance = self.get('ytick_distance', default=200)
         self.min_dus = self.get('min_dus', default=1)
-        det_id = self.require('det_id')
-        self.calib = kp.calib.Calibration(det_id=det_id)
+        self.det_id = self.require('det_id')
+        self.t0set = None
+        self.calib = None
+
+        self.sds = kp.db.StreamDS()
+
+        self.index = 0
+
+        self._update_calibration()
 
         self.run = True
         self.max_queue = 3
@@ -70,10 +77,19 @@ class ZTPlot(Module):
         self.thread = threading.Thread(target=self.plot, daemon=True)
         self.thread.start()
 
+    def _update_calibration(self):
+        self.print("Updating calibration")
+        self.t0set = self.sds.t0sets(detid=self.det_id).iloc[-1]['CALIBSETID']
+        self.calib = kp.calib.Calibration(det_id=self.det_id, t0set=self.t0set)
+
     def process(self, blob):
         if 'Hits' not in blob:
             return blob
 
+        self.index += 1
+        if self.index % 1000 == 0:
+            self._update_calibration()
+
         hits = blob['Hits']
         hits = self.calib.apply(hits)
         event_info = blob['EventInfo']
@@ -144,9 +160,9 @@ class ZTPlot(Module):
 
         print
         plt.suptitle(
-            "z-t-Plot for DetID-{0}, Run {1}, FrameIndex {2}, "
-            "TriggerCounter {3}, Overlays {4}\n{5} UTC".format(
-                event_info.det_id[0], event_info.run_id[0],
+            "z-t-Plot for DetID-{0} (t0set: {1}), Run {2}, FrameIndex {3}, "
+            "TriggerCounter {4}, Overlays {5}\n{6} UTC".format(
+                event_info.det_id[0], self.t0set, event_info.run_id[0],
                 event_info.frame_index[0], event_info.trigger_counter[0],
                 event_info.overlays[0],
                 datetime.utcfromtimestamp(event_info.utc_seconds)),
-- 
GitLab