From 75eb0bca702f07222f6e59b8510f86e6f9bed46a Mon Sep 17 00:00:00 2001
From: Laurent Mercadier <laurent.mercadier@xfel.eu>
Date: Thu, 20 May 2021 22:49:43 +0200
Subject: [PATCH] Add digitizer_type() function

---
 src/toolbox_scs/detectors/digitizers.py | 35 +++++++++++++++++++------
 src/toolbox_scs/mnemonics_machinery.py  |  5 ++--
 2 files changed, 30 insertions(+), 10 deletions(-)

diff --git a/src/toolbox_scs/detectors/digitizers.py b/src/toolbox_scs/detectors/digitizers.py
index 8d42063..26c06cf 100644
--- a/src/toolbox_scs/detectors/digitizers.py
+++ b/src/toolbox_scs/detectors/digitizers.py
@@ -271,6 +271,7 @@ def get_peaks(run,
 
     # 2. Use raw data from digitizer
     # minimum pulse period @ 4.5MHz, according to digitizer type
+    digitizer = digitizer_type(source=source)
     min_distance = 1
     if digitizer == 'FastADC':
         min_distance = 24
@@ -346,8 +347,8 @@ def get_peaks(run,
                                  extra_dim)
     peaks = peaks.where(mask_on, drop=True)
     return peaks.assign_coords({extra_dim: pid})
-    
-                  
+
+
 def channel_peak_params(run, source, key=None, channel=None, board=None):
     """
     Extract peak-integration parameters used by a channel of the digitizer.
@@ -410,7 +411,7 @@ def fastADC_channel_peak_params(run, source, channel=None):
         'baseLength': ('baselineSettings.length.value',),
         'enable': ('enablePeakComputation.value',),
         'pulseStart': ('initialDelay.value',),
-        'pulseLength': ('peakSamples.value',), 
+        'pulseLength': ('peakSamples.value',),
         'npulses': ('numPulses.value',),
         'period': ('pulsePeriod.value',)
     }
@@ -470,14 +471,12 @@ def adq412_channel_peak_params(run, source, key=None,
         board = k[1]
     baseName = f'board{board}.apd.channel_{channel}.'
     source = source.split(':')[0]
-        
     adq412_keys = {
         'baseStart': (baseName + 'baseStart.value',),
         'baseStop': (baseName + 'baseStop.value',),
         'enable': (baseName + 'enable.value',),
         'pulseStart': (baseName + 'pulseStart.value',),
-        'pulseStop': (baseName + 'pulseStop.value',), 
-        'npulses': (baseName + 'numPulses.value',),
+        'pulseStop': (baseName + 'pulseStop.value',),
         'initialDelay': (baseName + 'initialDelay.value',),
         'upperLimit': (baseName + 'upperLimit.value',),
         'npulses': (f'board{board}.apd.numberOfPulses.value',)
@@ -581,7 +580,8 @@ def get_peak_params(run, mnemonic, raw_trace=None, ntrains=200):
         title = 'Digitizer peak params'
     else:
         mnemo_raw = mnemonic
-        min_distance = 24 if "FastADC" in mnemonic else 440
+        digitizer = digitizer_type(mnemonic, run_mnemonics)
+        min_distance = 24 if digitizer == "FastADC" else 440
         title = 'Auto-find peak params'
         if raw_trace is None:
             sel = run.select_trains(np.s_[:ntrains])
@@ -647,7 +647,8 @@ def check_peak_params(run, mnemonic, raw_trace=None, ntrains=200, params=None,
         log.warning('The digitizer did not record peak-integrated data.')
     if not plot:
         return params
-    min_distance = 24 if "FastADC" in mnemonic else 440
+    digitizer = digitizer_type(mnemonic, run_mnemonics)
+    min_distance = 24 if digitizer == "FastADC" else 440
     if 'bunchPatternTable' in run_mnemonics and bunchPattern != 'None':
         sel = run.select_trains(np.s_[:ntrains])
         bp_params = {}
@@ -747,6 +748,24 @@ def plotPeakIntegrationWindow(raw_trace, params, bp_params, show_all=False):
     return fig, ax
 
 
+def digitizer_type(mnemonic=None, mnemo_dict=None, source=None):
+    if mnemonic is not None:
+        source = mnemo_dict[mnemonic]['source']
+    if ':channel' in source:
+        return 'FastADC'
+    if ':network' in source:
+        return 'ADQ412'
+    dic = {'XTD10_MCP': 'FastADC',
+           'FastADC': 'FastADC',
+           'PES': 'ADQ412',
+           'MCP': 'ADQ412'}
+    for k, v in dic.items():
+        if k in mnemonic:
+            return v
+    log.warning(f'Could not find digitizer type from mnemonic {mnemonic}.')
+    return 'ADQ412'
+
+
 def get_tim_peaks(run, mnemonics=None, merge_with=None,
                   bunchPattern='sase3', integParams=None,
                   keepAllSase=False):
diff --git a/src/toolbox_scs/mnemonics_machinery.py b/src/toolbox_scs/mnemonics_machinery.py
index f09e14b..bfc7207 100644
--- a/src/toolbox_scs/mnemonics_machinery.py
+++ b/src/toolbox_scs/mnemonics_machinery.py
@@ -83,7 +83,7 @@ def mnemonics_to_process(mnemo_list, merge_with, detector, func=None):
     merge_with: xarray Dataset
         Dataset that may contain non-processed arrays
     detector: str
-        One in {'ADQ412', 'FastADC', 'XGM', 'BAM'}
+        One in {'ADQ412', 'FastADC', 'XGM', 'BAM', 'PES'}
     func: function
         function that takes one argument, an unprocessed mnemonic string,
         and converts it into a processed one, i.e. from 'MCP2apd' to
@@ -110,7 +110,8 @@ def mnemonics_to_process(mnemo_list, merge_with, detector, func=None):
         default_mnemo = 'SCS_SA3'
         default_processed = 'SCS_SA3'
     if detector == 'ADQ412':
-        det_mnemos = [m for m in _mnemonics if 'MCP' in m]
+        det_mnemos = [m for m in _mnemonics if 'MCP' in m and
+                      'XTD10_' not in m]
         default_mnemo = 'MCP2apd'
         default_processed = 'MCP2peaks'
     if detector == 'FastADC':
-- 
GitLab