From 1e7c52e033cc93c8b6fb0e25b9ad7567c1ceaf91 Mon Sep 17 00:00:00 2001
From: Tamas Gal <himself@tamasgal.com>
Date: Mon, 2 Sep 2024 14:16:36 +0200
Subject: [PATCH] Improve time residuals plot

---
 backend/scripts/time_residuals.py | 27 +++++++++++++++++++++------
 1 file changed, 21 insertions(+), 6 deletions(-)

diff --git a/backend/scripts/time_residuals.py b/backend/scripts/time_residuals.py
index 582cb00..4aa840d 100755
--- a/backend/scripts/time_residuals.py
+++ b/backend/scripts/time_residuals.py
@@ -21,6 +21,7 @@ import matplotlib
 # Force matplotlib to not use any Xwindows backend.
 matplotlib.use('Agg')
 import matplotlib.pyplot as plt
+import seaborn as sns
 import numpy as np
 import pandas as pd
 
@@ -28,6 +29,10 @@ import km3pipe as kp
 kp.style.use('km3pipe')
 
 
+LINE_STYLES = ['solid', 'dashed', 'dashdot', 'dotted']
+N_STYLES = len(LINE_STYLES)
+
+
 def main():
     from docopt import docopt
     args = docopt(__doc__)
@@ -42,6 +47,12 @@ def main():
             names=["run", "timestamp", "du", "floor", "dom_id", "t_res", "Q"],
             header=1,
         )
+
+        n_dus = len(np.unique(df.du))
+        n_colors = int(np.ceil(n_dus / N_STYLES))
+        colors = sns.color_palette('colorblind', n_colors=n_colors)
+        linestyles = [(c, s) for c in colors for s in LINE_STYLES]
+
         df = df[df.timestamp > time.time() - 60 * 60 * hours]
         print(f" -> number of entries: {len(df)}")
 
@@ -53,16 +64,20 @@ def main():
                                  constrained_layout=True)
 
         for ax, floor in zip(axes.flatten(), range(1, 19)):
-            for du in np.unique(df.du):
+            for du, linestyle in zip(sorted(np.unique(df.du)), linestyles):
                 _df = df[df.du == du]
                 t_res = _df[_df.floor == floor].t_res
-                t_res = t_res[np.abs(t_res) < 500]
+                t_res = t_res[np.abs(t_res) < 150]
+                du = int(du)
                 print(f"   DU {du} floor {floor}: {len(t_res)} entries")
                 ax.hist(t_res,
-                        bins=100,
+                        bins=60,
                         histtype='step',
                         lw=2,
-                        label=f'Floor {floor} / DU {du}')
+                        label=f'DU {du}',
+                        density=true,
+                        color=linestyle[0],
+                        linestyle=linestyle[1])
             ax.legend(loc='upper right')
             if floor > 15:
                 ax.set_xlabel('time residual [ns]')
@@ -70,14 +85,14 @@ def main():
                 ax.set_ylabel('count')
             ax.set_yscale('log')
         utc_now = datetime.utcnow().strftime("%c")
-        fig.suptitle(f"Time residuals using ROy reconstructions "
+        fig.suptitle(f"Time residuals using FibonacciFit reconstructions "
                      f"from the past {hours} hours - "
                      f"{utc_now} UTC\n")
         plt.savefig(os.path.join(plots_path, 'time_residuals'))
         plt.close('all')
         gc.collect()
 
-        time.sleep(60)
+        time.sleep(300)
 
 
 if __name__ == '__main__':
-- 
GitLab