From fb58052dde828aa6081d3fe35521e255b40dc2f3 Mon Sep 17 00:00:00 2001
From: Tamas Gal <tgal@km3net.de>
Date: Mon, 3 Jun 2019 16:26:08 +0200
Subject: [PATCH] Add time_residuals plotter

---
 scripts/time_residuals.py | 76 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 76 insertions(+)
 create mode 100644 scripts/time_residuals.py

diff --git a/scripts/time_residuals.py b/scripts/time_residuals.py
new file mode 100644
index 0000000..f99fed7
--- /dev/null
+++ b/scripts/time_residuals.py
@@ -0,0 +1,76 @@
+#!/usr/bin/env python
+# coding=utf-8
+# vim: ts=4 sw=4 et
+"""
+Creates time residuals plots.
+
+Usage:
+    time_residuals.py [options] TIME_RESIDUALS_FILE
+    time_residuals.py (-h | --help)
+
+Options:
+    -o PLOT_DIR     The directory to save the plot [default: plots].
+    -h --help       Show this screen.
+
+"""
+import os
+from datetime import datetime
+import time
+import matplotlib
+# Force matplotlib to not use any Xwindows backend.
+matplotlib.use('Agg')
+import matplotlib.pyplot as plt
+import numpy as np
+import pandas as pd
+
+import km3pipe as kp
+kp.style.use('km3pipe')
+
+
+def main():
+    from docopt import docopt
+    args = docopt(__doc__)
+
+    plots_path = args['-o']
+    hours = 2
+
+    while True:
+        df = pd.read_csv(
+            args['TIME_RESIDUALS_FILE'],
+            names=['run', 'timestamp', 'du', 'floor', 'dom_id', 't_res', 'Q'])
+        df = df[df.timestamp > time.time() - 60 * 60 * hours]
+
+        fig, axes = plt.subplots(nrows=6,
+                                 ncols=3,
+                                 figsize=(16, 10),
+                                 sharex=True,
+                                 sharey=True,
+                                 constrained_layout=True)
+
+        for ax, floor in zip(axes.flatten(), range(1, 19)):
+            for du in np.unique(df.du):
+                _df = df[df.du == du]
+                t_res = _df[_df.floor == floor].t_res
+                ax.hist(-t_res,
+                        bins=100,
+                        histtype='step',
+                        lw=2,
+                        label=f'Floor {floor} / DU {du}')
+            ax.legend(loc='upper right')
+            if floor > 15:
+                ax.set_xlabel('time residual [ns]')
+            if floor % 3 == 1:
+                ax.set_ylabel('count')
+            ax.set_yscale('log')
+        utc_now = datetime.utcnow().strftime("%c")
+        fig.suptitle(f"Time residuals using ROy reconstructions "
+                     f"from the past {hours} hours - "
+                     f"{utc_now} UTC\n")
+        plt.savefig(os.path.join(plots_path, 'time_residuals'))
+        plt.close('all')
+
+        time.sleep(60)
+
+
+if __name__ == '__main__':
+    main()
-- 
GitLab