diff --git a/CAENBoard_GENERICLog.txt b/CAENBoard_GENERICLog.txt deleted file mode 100644 index ed3a60d..0000000 --- a/CAENBoard_GENERICLog.txt +++ /dev/null @@ -1,2 +0,0 @@ -[ 0.009][EE][CAENBoard_GENERIC.c::2526]: Open digitizer failed due to a comm error: -1 -[ 0.009][EE][CAENDPPLayer.c::118]: From c_newDigitizer: Error Calling CAENDigitizer API diff --git a/configs/digitiser/wd1_scope_V1730_A4818.conf b/configs/digitiser/wd1_scope_V1730_A4818.conf new file mode 100644 index 0000000..28d02f6 --- /dev/null +++ b/configs/digitiser/wd1_scope_V1730_A4818.conf @@ -0,0 +1,9 @@ +[required] + +dig_name = 'V1730_A4818' +dig_gen = 1 +con_type = 'usb_A4818' +link_num = 42898 +conet_node = 0 +vme_base_address = 0 +dig_authority = 'caen.internal' diff --git a/configs/recording/scope_a4818_V1730_SELFTRIG.conf b/configs/recording/scope_a4818_V1730_SELFTRIG.conf new file mode 100644 index 0000000..7cfed9d --- /dev/null +++ b/configs/recording/scope_a4818_V1730_SELFTRIG.conf @@ -0,0 +1,57 @@ +# SELFTRIG example, where ch1 has a large signal, and both other channels are noise +# So we trigger on ch1, and not the others. All channels are collected per each trigger (unlike DPP-PSD firmware) +[required] + +record_length = 4096 # ns +pre_trigger = 512 # ns +trigger_mode = 'SELFTRIG' # look into the differing methods, trigger on channel based on threshold is an option + # SWTRIG, SELFTRIG, (not yet implemented) +software_timeout = 0 # hard software timeout between each digitiser poll (s) + +[channel_settings] + +ch0 = {'enabled' : False, + 'self_trigger': True, + 'threshold' : 0, + 'polarity' : 'positive'} # ADCs, only applies of self_trigger enabled + +ch1 = {'enabled' : True, + 'self_trigger': True, + 'threshold' : 1600, + 'polarity' : 'positive'} + +ch2 = {'enabled' : False, + 'self_trigger': False, + 'threshold' : 0, + 'polarity' : 'positive'} + +ch3 = {'enabled' : True, + 'self_trigger': False, + 'threshold' : 1, + 'polarity' : 'positive'} + +ch4 = {'enabled' : False, + 'self_trigger': False, + 'threshold' : 0, + 'polarity' : 'positive'} + +ch5 = {'enabled' : True, + 'self_trigger': False, + 'threshold' : 1, + 'polarity' : 'positive'} + +ch6 = {'enabled' : False, + 'self_trigger': False, + 'threshold' : 0, + 'polarity' : 'positive'} + +ch7 = {'enabled' : False, + 'self_trigger': False, + 'threshold' : 0, + 'polarity' : 'positive'} + +[output_settings] + +file_name = '../CARP_FILES/SCOPE_testing/a4818test' # this will always be appended with a timestamp and .h5 +overwrite = True # overwrite the file if you want +h5_flush_size = 10000 # number of values added to h5 files per write diff --git a/configs/recording/scope_a4818_V1730_SWTRIG.conf b/configs/recording/scope_a4818_V1730_SWTRIG.conf new file mode 100644 index 0000000..3de8c3d --- /dev/null +++ b/configs/recording/scope_a4818_V1730_SWTRIG.conf @@ -0,0 +1,58 @@ +# Software trigger example, where channel 1, 3 and 5 are outputting data. +# self_trigger is irrelevant here, as long as the channel is enabled, it will work + +[required] + +record_length = 4096 # ns +pre_trigger = 512 # ns +trigger_mode = 'SWTRIG' # look into the differing methods, trigger on channel based on threshold is an option + # SWTRIG, SELFTRIG, (not yet implemented) +software_timeout = 0 # hard software timeout between each digitiser poll (s) + +[channel_settings] + +ch0 = {'enabled' : False, + 'self_trigger': False, + 'threshold' : 0, + 'polarity' : 'positive'} # ADCs, only applies of self_trigger enabled + +ch1 = {'enabled' : True, + 'self_trigger': True, + 'threshold' : 0, + 'polarity' : 'positive'} + +ch2 = {'enabled' : False, + 'self_trigger': False, + 'threshold' : 600, + 'polarity' : 'positive'} + +ch3 = {'enabled' : True, + 'self_trigger': True, + 'threshold' : 0, + 'polarity' : 'positive'} + +ch4 = {'enabled' : False, + 'self_trigger': False, + 'threshold' : 0, + 'polarity' : 'positive'} + +ch5 = {'enabled' : True, + 'self_trigger': True, + 'threshold' : 0, + 'polarity' : 'positive'} + +ch6 = {'enabled' : False, + 'self_trigger': False, + 'threshold' : 0, + 'polarity' : 'positive'} + +ch7 = {'enabled' : False, + 'self_trigger': False, + 'threshold' : 0, + 'polarity' : 'positive'} + +[output_settings] + +file_name = '../CARP_FILES/SCOPE_testing/a4818test' # this will always be appended with a timestamp and .h5 +overwrite = True # overwrite the file if you want +h5_flush_size = 10000 # number of values added to h5 files per write diff --git a/core/controller.py b/core/controller.py index 3f971ce..a36217f 100644 --- a/core/controller.py +++ b/core/controller.py @@ -28,6 +28,7 @@ from core.worker import AcquisitionWorker from core.writer import Writer from core.tracker import Tracker +from core.functions import get_ch_mapping from felib.digitiser import Digitiser from ui import oscilloscope @@ -35,8 +36,8 @@ from queue import Queue, Empty class Controller: - def __init__(self, - dig_config: Optional[str] = None, + def __init__(self, + dig_config: Optional[str] = None, rec_config: Optional[str] = None): ''' Initialise controller for GUI and digitiser @@ -79,21 +80,21 @@ def __init__(self, self.worker.start() logging.info("Acquisition worker thread started.") - # Multi channel writes to h5 - self.ch_mapping = self.get_ch_mapping() + # Multi channel writes to h5 + self.ch_mapping = get_ch_mapping(self.rec_dict) self.num_ch = len(self.ch_mapping) + self.max_ch = max(self.ch_mapping.keys()) self.h5_flush_size = self.rec_dict['h5_flush_size'] - self.writer_buffers = [Queue(maxsize=1024) for _ in range(self.num_ch)] - self.writers = [Writer( - ch=curr_ch, - flush_size=self.h5_flush_size, - write_buffer=self.writer_buffers[i], - stop_event=self.writer_stop_event, - rec_config = read_config_file(self.rec_config), - dig_config = read_config_file(self.dig_config), - TIMESTAMP = datetime.now().strftime("%H:%M:%S") + self.writer_buffer = Queue(maxsize=1024) + self.writer = Writer( + ch_map = self.ch_mapping, + flush_size = self.h5_flush_size, + write_buffer = self.writer_buffer, + stop_event = self.writer_stop_event, + rec_config = read_config_file(self.rec_config), + dig_config = read_config_file(self.dig_config), + TIMESTAMP = datetime.now().strftime("%H:%M:%S") ) - for curr_ch, i in self.ch_mapping.items()] # gui second self.app = QApplication([]) @@ -105,20 +106,6 @@ def __init__(self, self.connect_digitiser() - def get_ch_mapping(self): - ''' - Extract what channels are being used map them: ch -> index - ''' - mapping = {} - i = 0 - for entry in self.rec_dict: - if 'ch' in entry: - if self.rec_dict[entry]['enabled']: - ch = int(entry[2:]) - mapping[ch] = i - i += 1 - - return mapping def data_handling(self): ''' @@ -132,25 +119,23 @@ def data_handling(self): break try: - # you must pass wf_size and ADCs through. - wf_size, ADCs, ch = data + # you must pass wf_size and ADCs through. + wf_size, ADCs, ch, timestamp = data # update visuals - self.main_window.screen.update_ch(np.arange(0, wf_size, dtype=wf_size.dtype), ADCs) - + self.main_window.screen.update_ch(np.arange(0, wf_size, dtype=wf_size.dtype), ADCs, ch) + # ping the tracker (make this optional) self.tracker.track(ADCs.nbytes) - # push data to writer buffer + # push data to writer buffer if self.recording: - write_data = wf_size, ADCs, self.event_counter + write_data = wf_size, ch, ADCs, self.event_counter, timestamp + self.writer_buffer.put(write_data) - # multi channel writing - ch = int(ch) - i = int(self.ch_mapping[ch]) - self.writer_buffers[i].put(write_data) - - self.event_counter += 1 + # stupid catch to ensure event number only increases with channel + if ch == self.max_ch: + self.event_counter += 1 except Exception as e: logging.exception(f"Error updating display: {e}") @@ -166,7 +151,7 @@ def update_fps(self): def run_app(self): self.main_window.show() return self.app.exec() - + def connect_digitiser(self): ''' Connect to the digitiser using the provided configuration file. @@ -193,7 +178,7 @@ def start_acquisition(self): ''' logging.info("Starting acquisition.") self.cmd_buffer.put(Command(CommandType.START)) - + def stop_acquisition(self): ''' Stop digitiser acquisition. @@ -206,13 +191,12 @@ def start_recording(self): Start recording data. ''' self.recording = True - for w in self.writers: - if not w.is_alive(): - w.start() - logging.info(f"Writer (channel {w.ch}) thread started.") + if not self.writer.is_alive(): + self.writer.start() + logging.info(f'Writer thread started.') logging.info("Starting recording.") - + def stop_recording(self): ''' Stop recording data. @@ -220,10 +204,9 @@ def stop_recording(self): self.recording = False self.writer_stop_event.set() - for w in self.writers: - w.join(timeout=2) + self.writer.join(timeout=2) - logging.info("Stopping recording.") + logging.info("Writer thread stopping recording.") def shutdown(self): ''' @@ -238,8 +221,7 @@ def shutdown(self): # Writer threads self.writer_stop_event.set() - for w in self.writers: - w.join(timeout=2) + self.writer.join(timeout=2) clean_shutdown = True @@ -247,10 +229,9 @@ def shutdown(self): clean_shutdown = False logging.warning("AcquisitionWorker did not stop cleanly.") - for w in self.writers: - if w.is_alive(): - clean_shutdown = False - logging.warning(f"Writer (channel {w.ch}) did not stop cleanly.") + if self.writer.is_alive(): + clean_shutdown = False + logging.warning(f"Writer (channel {w.ch}) did not stop cleanly.") if clean_shutdown: logging.info("Controller shutdown complete.") diff --git a/core/functions.py b/core/functions.py new file mode 100644 index 0000000..bdf4ee1 --- /dev/null +++ b/core/functions.py @@ -0,0 +1,21 @@ + +def get_ch_mapping(rec_dict): + ''' + Extract what channels are being used map them: ch -> index + + So shape will be: + mapping = {0 : 0, 3 : 1, 5 : 2} + for the case where ch0, 3, and 5 are enabled + ''' + mapping = {} + i = 0 + for entry in rec_dict: + if 'ch' in entry: + if rec_dict[entry]['enabled']: + ch = int(entry[2:]) + mapping[ch] = i + i += 1 + + return mapping + + diff --git a/core/worker.py b/core/worker.py index 2788c0f..dbd7cb6 100644 --- a/core/worker.py +++ b/core/worker.py @@ -74,7 +74,7 @@ def start_acquisition(self): logging.info("Digitiser acquisition started successfully.") except Exception as e: logging.exception(f"Start acquisition failed: {e}") - + def connect_digitiser(self, dig_config, rec_config): ''' Connect to digitiser with given configs. @@ -123,21 +123,22 @@ def run(self): data = self.digitiser.acquire() if data is None: continue - - # Non-blocking put to visual buffer - if self.display_buffer.full(): - try: - self.display_buffer.get_nowait() # discard oldest - except Empty: - pass - - # Push to display buffer (etc.) - if not self.display_buffer.full(): - self.display_buffer.put_nowait(data) - - # Notify controller/UI - if self.data_ready_callback: - self.data_ready_callback() + # extract each channel separately and push to buffers + for channel_data in data: + # Non-blocking put to visual buffer + if self.display_buffer.full(): + try: + self.display_buffer.get_nowait() # discard oldest + except Empty: + pass + + # Push to display buffer (etc.) + if not self.display_buffer.full(): + self.display_buffer.put_nowait(channel_data) + + # Notify controller/UI + if self.data_ready_callback: + self.data_ready_callback() except Exception as e: logging.exception(f"Acquisition error: {e}") @@ -154,7 +155,7 @@ def run(self): def cleanup(self): ''' - Cleans up digitiser by calling stop_acquisition and its destructor. + Cleans up digitiser by calling stop_acquisition and its destructor. ''' if self.digitiser: if self.digitiser.isAcquiring: diff --git a/core/writer.py b/core/writer.py index b6d5cc0..5b2eb80 100644 --- a/core/writer.py +++ b/core/writer.py @@ -3,7 +3,7 @@ import logging import tables as tb -import core.df_classes as df_class +import core.df_classes as df_class import core.io as io @@ -11,11 +11,11 @@ class Writer(Thread): ''' Writes channel data to h5 file. ''' - def __init__(self, - ch : int, - flush_size : int, - write_buffer : Queue, - stop_event : Event, + def __init__(self, + ch_map : dict, + flush_size : int, + write_buffer : Queue, + stop_event : Event, rec_config : dict, dig_config : dict, TIMESTAMP : str,): @@ -25,29 +25,34 @@ def __init__(self, ''' super().__init__(daemon=True) - self.ch = ch + self.ch_map = ch_map self.flush_size = flush_size self.write_buffer = write_buffer self.stop_event = stop_event self.rec_config = rec_config self.dig_config = dig_config - self.filename = f"some_name_ch_{self.ch}" self.local_buffer = [] self.wf_size = None if 'file_name' in self.rec_config: file_path = self.rec_config['file_name'] - file_path = f'{file_path}_{self.ch}_{TIMESTAMP}.h5' + file_path = f'{file_path}_data_{TIMESTAMP}.h5' else: - file_path = f'{self.ch}_{TIMESTAMP}.h5' + file_path = f'data_{TIMESTAMP}.h5' # initialise the h5, one per channel, each handled on a separate thread - self.h5file = tb.open_file(f'{file_path}', mode='a') - # configs written + try: + self.h5file = tb.open_file(f'{file_path}', mode='a') + except FileNotFoundError as e: + logging.error(f'FileNotFoundError: Cannot create output file at path {file_path}') + exit() + # configs written io.create_config_table(self.h5file, self.rec_config, 'rec_conf', 'recording config') io.create_config_table(self.h5file, self.dig_config, 'dig_conf', 'digitiser config') # raw waveform group constructed - self.rwf_group = self.h5file.create_group('/', f'ch_{ch}', 'raw waveform') - + self.rwf_group = {} + for ch in self.ch_map.keys(): + self.rwf_group[ch] = self.h5file.create_group('/', f'ch_{ch}', 'raw waveform') + self.rwf_table = {} def write_h5(self): ''' @@ -58,22 +63,28 @@ def write_h5(self): where ADCs is the actual raw waveform array ''' - for wf_size, rwf, evt in self.local_buffer: + for wf_size, ch, rwf, evt, ts in self.local_buffer: # if we know the size of the waveforms already, don't create the class again. if self.wf_size is None: - self.wf_size = wf_size + self.wf_size = wf_size self.rwf_class = df_class.return_rwf_class(self.dig_config['dig_gen'], self.wf_size) - self.rwf_table = self.h5file.create_table(self.rwf_group, 'rwf', self.rwf_class, "raw waveforms") - self.rows = self.rwf_table.row - - self.rows['evt_no'] = evt - self.rows['rwf'] = rwf + # generate all tables once + for ch in self.ch_map.keys(): + self.rwf_table[ch] = self.h5file.create_table(self.rwf_group[ch], 'rwf', self.rwf_class, "raw waveforms") + # open up the rows for the table + self.rows = self.rwf_table[ch].row + + self.rows['evt_no'] = evt + self.rows['rwf'] = rwf + self.rows['channel'] = ch + self.rows['timestamp'] = ts self.rows.append() self.local_buffer.clear() # flush as fast as the buffer provides - self.rwf_table.flush() + for ch_local in self.ch_map.keys(): + self.rwf_table[ch_local].flush() pass @@ -81,7 +92,7 @@ def run(self): ''' Writer hot loop. ''' - logging.info(f"Writer thread started (ch {self.ch}).") + logging.info(f"Writer thread started.") try: while not self.stop_event.is_set(): # First load data from shared buffer into local buffer @@ -99,11 +110,11 @@ def run(self): self.write_h5() except Exception as e: - logging.exception(f"Fatal error in Writer (ch {self.ch}): {e}") + logging.exception(f"Fatal error in Writer: {e}") # When stop_event() is set, call cleanup() self.cleanup() - logging.info(f"Writer thread exited cleanly (ch {self.ch}).") + logging.info(f"Writer thread exited cleanly.") def cleanup(self): ''' diff --git a/felib/digitiser.py b/felib/digitiser.py index 8621172..051779b 100644 --- a/felib/digitiser.py +++ b/felib/digitiser.py @@ -8,6 +8,7 @@ from typing import Optional import time +from core.functions import get_ch_mapping from felib.dig1_utils import generate_digitiser_uri import felib.formats as formats @@ -42,12 +43,12 @@ def __init__(self, dig_dict : dict): else: logging.error("Invalid digitiser generation specified in the configuration.") #raise ValueError("Invalid digitiser generation specified in the configuration.") - + self.URI = self.generate_uri() self.isAcquiring = False self.isConnected = False self.isRecording = False - + self.data_format = [] self.endpoint = None @@ -74,15 +75,15 @@ def generate_uri(self): else: logging.error("Invalid digitiser generation specified in the configuration.") #raise ValueError("Invalid digitiser generation specified in the configuration.") - + def connect(self): ''' Connect to the digitiser using the generated URI. ''' - + logging.info(f'Attemping connection to digitiser {self.dig_name} at {self.URI}.') - + # fake connection for debugging if self.dig_name == 'debug': self.dig = None @@ -114,7 +115,7 @@ def connect(self): return None - def configure(self, + def configure(self, dig_dict : dict, rec_dict : dict): #record_length: Optional[int] = 0, @@ -122,12 +123,15 @@ def configure(self, #trigger_level: Optional[str] = 'SWTRG'): ''' Configure the digitiser with the provided settings and calibrate it. - ''' + ''' self.record_length = rec_dict.get('record_length') self.pre_trigger = rec_dict.get('pre_trigger') self.trigger_mode = rec_dict.get('trigger_mode') + # extract channel mapping + self.ch_mapping = get_ch_mapping(rec_dict) + try: self.dig.par.RECLEN.value = f'{self.record_length}' @@ -143,48 +147,78 @@ def configure(self, # extract channel config of interest ch_dict = rec_dict.get(f'ch{i}') - + + # disable channel if not explicitly called if ch_dict is None: + ch.par.CH_ENABLED.value = 'FALSE' continue - - ch.par.CH_ENABLED.value = 'TRUE' if ch_dict['enabled'] else 'FALSE' - ch.par.CH_PRETRG.value = f'{self.pre_trigger}' + + # normal channel management + ch.par.CH_ENABLED.value = 'TRUE' if ch_dict['enabled'] else 'FALSE' + # for SCOPE, this doesnt need to be set per channel, but i prefer these to be set together + # even if its a bit redundant + if self.dig.par.FWTYPE.value == 'DPP-PSD' : ch.par.CH_PRETRIG.value = f'{self.pre_trigger}' + elif self.dig.par.FWTYPE.value == 'SCOPE' : self.dig.par.POSTTRG.value = f'{self.record_length - self.pre_trigger}' # ensure self trigger only enabled when you don't have SWTRIG enabled - if ch_dict['self_trigger'] and self.trigger_mode != 'SWTRIG': - ch.par.CH_SELF_TRG_ENABLE.value = 'TRUE' + # recall that this functions like so for DPP-PSD, with SCOPE, if a channel is enabled the self-trigger is also enabled + if (ch_dict['self_trigger'] and self.trigger_mode != 'SWTRIG'): + if self.dig.par.FWTYPE.value == 'DPP-PSD' : ch.par.CH_SELF_TRG_ENABLE.value = 'TRUE' + if self.dig.par.FWTYPE.value == 'SCOPE' : ch.par.CH_TRG_GLOBAL_GEN.value = 'TRUE' + ch.par.CH_THRESHOLD.value = str(ch_dict['threshold']) else: # doesn't reset by default! so forcing this here - ch.par.CH_SELF_TRG_ENABLE.value = 'FALSE' - + if self.dig.par.FWTYPE.value == 'DPP-PSD' : ch.par.CH_SELF_TRG_ENABLE.value = 'FALSE' + if self.dig.par.FWTYPE.value == 'SCOPE' : ch.par.CH_TRG_GLOBAL_GEN.value = 'FALSE' + if ch_dict['polarity'] == 'positive': ch.par.CH_POLARITY.value = 'POLARITY_POSITIVE' elif ch_dict['polarity'] == 'negative': ch.par.CH_POLARITY.value = 'POLARITY_NEGATIVE' - else: - ch.par.CH_SELF_TRG_ENABLE.value = 'FALSE' - # technically customisable - + # calculate the true reclen value for outputting reclen_ns = int(self.dig.par.RECLEN.value) self.reclen = int(reclen_ns / int(1e3 / self.dig_info['sample_rate'])) - # if DPP, need to specify that you're looking at waveforms specifically. - if self.dig.par.FWTYPE.value == 'DPP-PSD': - self.dig.par.WAVEFORMS.value = 'TRUE' - self.data_format = formats.DPP(int(self.dig.par.NUMCH.value), int(self.reclen)) - # setting up probe types (READ UP ON THIS) - self.dig.vtrace[0].par.VTRACE_PROBE.value = 'VPROBE_INPUT' - + # set up data format + match self.dig.par.FWTYPE.value: + case 'DPP-PSD': + # enforce waveform formatting + self.dig.par.WAVEFORMS.value = 'TRUE' + self.data_format = formats.DPP( int(self.dig.par.NUMCH.value), int(self.reclen)) + # setting up probe types (READ UP ON THIS) + self.dig.vtrace[0].par.VTRACE_PROBE.value = 'VPROBE_INPUT' + case 'SCOPE': + self.data_format = formats.SCOPE(int(self.dig.par.NUMCH.value), int(self.reclen)) + case _: + logging.exception(f"Firmware type {self.dig.par.FWTYPE.value} not recognised.\nCurrent FWs available are DPP-PSD and SCOPE") + endpoint_path = (self.dig.par.FWTYPE.value).replace('-', '') self.endpoint = self.dig.endpoint[endpoint_path] self.data = self.endpoint.set_read_data_format(self.data_format) - + + # generalised extraction of data parameters + match self.dig.par.FWTYPE.value: + case 'DPP-PSD': + self.channel = self.data[0].value + self.timestamp = self.data[1].value + self.waveform_size = self.data[7].value + self.waveform = self.data[3].value + case 'SCOPE': + # no channel parameter as channels are treated differently + # all channels are within the dataset, check the data format to understand the shape + self.timestamp = self.data[1].value + self.waveform_size = self.data[3].value + self.waveform = self.data[2].value + case _: + logging.exception(f"Firmware type {self.dig.par.FWTYPE.value} not recognised.\nCurrent FWs available are DPP-PSD and SCOPE") + + logging.info(f"Digitiser configured:\nrecord length {self.record_length}, pre-trigger {self.pre_trigger}, trigger mode {self.trigger_mode}.") except Exception as e: logging.exception(f"Failed to configure recording parameters.\n{e}") @@ -207,18 +241,18 @@ def start_acquisition(self): self.dig.cmd.ARMACQUISITION() except Exception as e: logging.exception(f"Starting acquisition failed: {e}") - + # start recording function #self.trigger_and_record() #try: #self.dig.cmd.START() - #self.collect = True + #self.collect = True #print("Digitiser acquisition started.") #except Exception as e: # raise RuntimeError(f"Failed to start digitiser acquisition.\n{e}") #self.collect = True - - + + def stop_acquisition(self): ''' Stop the digitiser acquisition. @@ -242,7 +276,7 @@ def acquire(self): return self.SELFTRIG_record() case _: logging.info(f'Trigger mode {self.trigger_mode} not currently implemented.') - self.stop_acquisition() + self.stop_acquisition() def SW_record(self): @@ -255,10 +289,19 @@ def SW_record(self): try: self.endpoint.has_data(check_timeout) self.endpoint.read_data(read_timeout, self.data) # timeout first number in ms - wf_size = self.data[7].value - ADCs = self.data[3].value - ch = self.data[0].value - return (wf_size, ADCs, ch) + + # SCOPE sends everything, even the disabled channels, so parse the relevant channels here + if self.dig.par.FWTYPE.value == 'SCOPE': + output = [] + # using the mapping extract the relevant channels + for ch in self.ch_mapping.keys(): + output.append((self.waveform_size[ch], self.waveform[ch], ch, self.timestamp)) + # DPP-PSD triggers per channel, so needs to be treated as such + elif self.dig.par.FWTYPE.value == 'DPP-PSD': + output = [(self.waveform_size, self.waveform, self.channel, self.timestamp)] + + return output + except error.Error as ex: #logging.exception("Error in readout:") if ex.code is error.ErrorCode.TIMEOUT: @@ -266,16 +309,16 @@ def SW_record(self): if ex.code is error.ErrorCode.STOP: logging.exception("STOP") raise ex - + # ensure the input and trigger are acceptable (I think?) #assert self.data[3].value == 1 # VPROBE INPUT? I need to understand this #assert self.data[6].value == 1 # VPROBE TRIGGER? - + #waveform_size = self.data[7].value #valid_sample_range = np.arange(0, waveform_size, dtype = waveform_size.dtype) - - + + def SELFTRIG_record(self): ''' Trigger on channels @@ -285,10 +328,19 @@ def SELFTRIG_record(self): try: self.endpoint.has_data(check_timeout) self.endpoint.read_data(read_timeout, self.data) - wf_size = self.data[7].value - ADCs = self.data[3].value - ch = self.data[0].value - return (wf_size, ADCs, ch) + + # SCOPE sends everything, even the disabled channels, so parse the relevant channels here + if self.dig.par.FWTYPE.value == 'SCOPE': + output = [] + # using the mapping extract the relevant channels + for ch in self.ch_mapping.keys(): + output.append((self.waveform_size[ch], self.waveform[ch], ch, self.timestamp)) + # DPP-PSD triggers per channel, so needs to be treated as such + elif self.dig.par.FWTYPE.value == 'DPP-PSD': + output = [(self.waveform_size, self.waveform, self.channel, self.timestamp)] + + return output + except error.Error as ex: #logging.exception("Error in readout:") if ex.code is error.ErrorCode.TIMEOUT: diff --git a/felib/formats.py b/felib/formats.py index add2675..af0bae1 100644 --- a/felib/formats.py +++ b/felib/formats.py @@ -5,7 +5,7 @@ def DPP(nch, record_length): DPP-DSD format nch - number of channels ''' - + # Configure endpoint data_format = [ { @@ -52,4 +52,37 @@ def DPP(nch, record_length): } ] - return data_format \ No newline at end of file + return data_format + + +def SCOPE(nch, record_length): + ''' + SCOPE format + nch - number of channels + ''' + + # Configure endpoint + data_format = [ + { + 'name': 'EVENT_SIZE', + 'type': 'SIZE_T', + }, + { + 'name': 'TIMESTAMP', + 'type': 'U64', + }, + { + 'name': 'WAVEFORM', + 'type': 'U16', + 'dim': 2, + 'shape': [nch, record_length], + }, + { + 'name': 'WAVEFORM_SIZE', + 'type': 'SIZE_T', + 'dim': 1, + 'shape': [nch], + } + ] + + return data_format diff --git a/ui/oscilloscope.py b/ui/oscilloscope.py index 70d6d98..ee66e1e 100644 --- a/ui/oscilloscope.py +++ b/ui/oscilloscope.py @@ -53,9 +53,9 @@ def __init__(self, controller, parent=None): class OscilloScopeScreen(pg.PlotWidget): - def __init__(self, parent = None, plotItem = None, **kwargs): - super().__init__(parent=parent, background='w', plotItem=plotItem, **kwargs) - + def __init__(self, controller, parent = None, plotItem = None, **kwargs): + super().__init__(parent=parent, controller=controller, background='w', plotItem=plotItem, **kwargs) + styles = {'color': 'k', 'font-size': '12px'} self.setLabel('left', 'Voltage (ADCs)', **styles) self.setLabel('bottom', 'Time (ns)', **styles) @@ -64,15 +64,22 @@ def __init__(self, parent = None, plotItem = None, **kwargs): self.setXRange(0, 1, padding = 0.02) self.setYRange(0, 5, padding = 0.02) - self.pen_ch1 = pg.mkPen(color = "b", width = 1) + self.legend = self.addLegend() + + self.pen_ch = {} + self.channels = {} + # create pens for each channel, and plot the defaults + for ch in controller.ch_mapping.keys(): + self.pen_ch[ch] = pg.mkPen(pg.intColor(ch), width = 1) + self.plot_ch([0,1], [0,0], ch) + + - self.plot_ch([0,1], [0,0]) - def plot_ch(self, x, y, ch = 1): - self.data_line_ch = self.plot(x, y, pen=self.pen_ch1) + self.channels[ch] = self.plot(x, y, pen=self.pen_ch[ch], name = f'ch {ch}') def update_ch(self, x, y, ch = 1): - self.data_line_ch.setData(x, y) + self.channels[ch].setData(x, y) class MainWindow(QMainWindow): @@ -81,13 +88,13 @@ def __init__(self, controller, *args, **kwargs): # Define interactivity through controller self.controller = controller - + self.setupUI() - + def setupUI(self): self.setWindowTitle("CAEN Acqusition and Readout Program (CARP)") - - self.screen = OscilloScopeScreen() + + self.screen = OscilloScopeScreen(self.controller) self.control_panel = ControlPanel(self.controller) self.content_layout = QHBoxLayout() @@ -109,4 +116,4 @@ def init(): if __name__ == '__main__': - init() \ No newline at end of file + init()