From fe5a5148448d07bb8b90726720838db07768fc51 Mon Sep 17 00:00:00 2001
From: Tamas Gal <tgal@km3net.de>
Date: Wed, 20 Feb 2019 16:01:29 +0100
Subject: [PATCH] Add RTTC monitoring

---
 Makefile.in     | 10 +++---
 configure       |  8 +++++
 scripts/rttc.py | 92 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 106 insertions(+), 4 deletions(-)
 create mode 100644 scripts/rttc.py

diff --git a/Makefile.in b/Makefile.in
index 3655a64..63afe5c 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -83,12 +83,14 @@ start:
 	    "echo narf"
 	@tmux select-layout even-vertical
 
-	@# AHRS
+	@# AHRS/RTTC
 	@#
-	@tmux new-window -n ahrs -t ${SESSION_NAME}
-	@tmux split-window -v -t ${SESSION_NAME}:ahrs
-	@tmux send-keys -t ${SESSION_NAME}:ahrs.1  \
+	@tmux new-window -n ahrs-rttc -t ${SESSION_NAME}
+	@tmux split-window -v -t ${SESSION_NAME}:ahrs-rttc
+	@tmux send-keys -t ${SESSION_NAME}:ahrs-rttc.1  \
 	    "python scripts/ahrs_calibration.py -d ${DETECTOR_ID} -p ${MONITORING_LIGIER_PORT}" Enter
+	@tmux send-keys -t ${SESSION_NAME}:ahrs-rttc.2  \
+	    "python scripts/rttc.py -d ${DETECTOR_ID} -l ${DETECTOR_MANAGER_IP}" Enter
 	@tmux select-layout even-vertical
 
 	@# K40
diff --git a/configure b/configure
index 0d95310..080ab6b 100755
--- a/configure
+++ b/configure
@@ -3,6 +3,7 @@
 detector_id=29
 daq_ligier_ip="192.168.0.110"
 daq_ligier_port=5553
+detector_manager_ip="192.168.0.120"
 monitoring_ligier_ip="127.0.0.1"
 monitoring_ligier_port=5553
 logio_ip="127.0.0.1"
@@ -35,6 +36,10 @@ for arg in "$@"; do
         daq_ligier_port=`echo $arg | sed 's/--daq-ligier-port=//'`
         ;;
 
+    --detector-manager-ip=*)
+        detector_manager_ip=`echo $arg | sed 's/--detector-manager-ip=//'`
+        ;;
+
     --monitoring-ligier-port=*)
         monitoring_ligier_port=`echo $arg | sed 's/--monitoring-ligier-port=//'`
         ;;
@@ -63,6 +68,7 @@ for arg in "$@"; do
         echo "  --detector-id             Detector ID                    ${detector_id}"
         echo "  --daq-ligier-ip           DAQ Ligier                     ${daq_ligier_ip}"
         echo "  --daq-ligier-port         Port of the DAQ Ligier         ${daq_ligier_port}"
+        echo "  --detector-manager-ip     Detector Manager for RTTC      ${detector_manager_ip}"
         echo "  --monitoring-ligier-port  Port of the monitoring Ligier  ${monitoring_ligier_port}"
         echo "  --logio-ip                Log.io IP                      ${logio_ip}"
         echo "  --logio-port              Port of the Log.io server      ${logio_port}"
@@ -78,6 +84,7 @@ done
 echo "DETECTOR_ID = ${detector_id}" > Makefile
 echo "DAQ_LIGIER_IP = ${daq_ligier_ip}" >> Makefile
 echo "DAQ_LIGIER_PORT = ${daq_ligier_port}" >> Makefile
+echo "DETECTOR_MANAGER_IP = ${detector_manager_ip}" >> Makefile
 echo "MONITORING_LIGIER_PORT = ${monitoring_ligier_port}" >> Makefile
 echo "LOGIO_IP = ${logio_ip}" >> Makefile
 echo "LOGIO_PORT = ${logio_port}" >> Makefile
@@ -89,6 +96,7 @@ cat Makefile.in >> Makefile
 show_km3mon_banner
 echo "Detector ID:        ${detector_id}"
 echo "DAQ Ligier:         ${daq_ligier_ip}:${daq_ligier_port}"
+echo "Detector Manager:   ${detector_manager_ip}"
 echo "Monitoring Ligier:  ${monitoring_ligier_ip}:${monitoring_ligier_port}"
 echo "Log.io Server:      ${logio_ip}:${logio_port}"
 echo "Webserver:          0.0.0.0:${webserver_port}"
diff --git a/scripts/rttc.py b/scripts/rttc.py
new file mode 100644
index 0000000..5c451d6
--- /dev/null
+++ b/scripts/rttc.py
@@ -0,0 +1,92 @@
+#!/usr/bin/env python
+# coding=utf-8
+# vim: ts=4 sw=4 et
+"""
+Cable round trip time monitor.
+
+Usage:
+    rttc.py [options] -d DET_ID
+    rttc.py (-h | --help)
+
+Options:
+    -d DET_ID       Detector ID.
+    -l DM_IP        The IP of the DetectorManager [default: 127.0.0.1].
+    -o PLOT_DIR     The directory to save the plot [default: plots].
+    -h --help       Show this screen.
+
+"""
+from __future__ import division
+
+import os
+from datetime import datetime
+import matplotlib
+# Force matplotlib to not use any Xwindows backend.
+matplotlib.use('Agg')
+import matplotlib.pyplot as plt
+import matplotlib.dates as md
+import matplotlib.ticker as ticker
+from matplotlib.colors import LogNorm
+import numpy as np
+
+from collections import deque, defaultdict
+from functools import partial
+
+import km3pipe as kp
+from km3pipe import Pipeline, Module
+import km3pipe.style
+km3pipe.style.use('km3pipe')
+
+from km3pipe.logger import logging
+
+
+def main():
+    from docopt import docopt
+    args = docopt(__doc__)
+
+    data = defaultdict(partial(deque, maxlen=1000))
+
+    dm_ip = args['-l']
+    det_id = int(args['-d'])
+    plots_path = args['-o']
+
+    detector = kp.hardware.Detector(det_id=det_id)
+
+    dmm = kp.io.daq.DMMonitor(dm_ip, base='clb/outparams')
+
+    params = []
+    for du in detector.dus:
+        params += ['wr_mu/%d/0' % du] + ['wr_delta/%d/0/%i' % (du, i) for i in range(4)]
+
+    xfmt = md.DateFormatter('%Y-%m-%d %H:%M')
+
+    session = dmm.start_session('rttc_monitoring', params)
+
+    for values in session:
+        for du in detector.dus:
+            i = detector.dus.index(du)
+            idx_start = i * 5
+            idx_stop = idx_start + 5
+            data[du].append((datetime.utcnow(),
+                             [v['value'] for v in values[idx_start:idx_stop]]))
+
+
+        for du in detector.dus:
+            times = []
+            rttc = []
+            for d in data[du]:
+                times.append(d[0])
+                wr_mu, wr_delta0, wr_delta1, wr_delta2, wr_delta3 = d[1]
+                rttc_value = wr_mu - (wr_delta0 + wr_delta1 + wr_delta2 + wr_delta3)
+                rttc.append(rttc_value)
+
+            fig, ax = plt.subplots(figsize=(16, 4))
+            ax.plot(times, rttc, marker="X", markersize=6, linestyle='None')
+            ax.set_xlabel('time [UTC]')
+            ax.set_ylabel('RTTC [ps]')
+            ax.xaxis.set_major_formatter(xfmt)
+            plt.savefig(os.path.join(plots_path, 'rttc_du-%d.png' % du),
+                        dpi=120, bbox_inches="tight")
+            plt.close('all')
+
+if __name__ == '__main__':
+    main()
-- 
GitLab