def fake_events(n): result = [] for i in range(n): e = Event(n_channels=1, start_time=0, length=100, sample_duration=10) e.event_number = i e.block_id = i // 10 result.append(e) return result
def fake_events(n): result = [] for i in range(n): e = Event(n_channels=1, start_time=0, length=100, sample_duration=10) e.event_number = i e.block_id = i // 10 result.append(e) return result
def get_single_event_in_current_file(self, event_position=1): # Seek to the requested event # print(self.event_positions) self.current_evefile.seek(self.event_positions[event_position]) # self.current_evefile.seek(event_position, whence=io.SEEK_CUR) # Read event event header, check if it is a real data event or file event header or something different. event_event_header = np.fromfile(self.current_evefile, dtype=eve_event_header, count=1)[0] if event_event_header['event_type'] not in [ 3, 4 ]: # 3 = signal event, 4 = header event. Do others occur? raise NotImplementedError( "Event type %i not yet implemented!" % event_event_header['event_type'], self.current_evefile.tell()) if event_event_header['event_type'] == 4: # it might be possible to get another event header along with caen1724.par stuff self.log.error( "Unexpected event header at this position, trying to go on") self.file_caen_pars = np.fromfile(self.current_evefile, dtype=eve_caen1724_par_t, count=1)[0] event_event_header = np.fromfile(self.current_evefile, dtype=eve_event_header, count=1)[0] # Start building the event event = Event( n_channels=14, # never trust the config file start_time=int(event_event_header['event_timestamp'] * units.s # + # event_layer_metadata['utc_time_usec'] * units.us ), sample_duration=int(10 * units.ns), # 10 ns is the inverse of the sampling frequency 10MHz length=self.file_caen_pars['nof_samples'] # nof samples per event ) event.dataset_name = self.current_filename # now metadata available # as eve files do not have event numbers just count them event.event_number = event_position if self.file_caen_pars['zle'] == 0: # Zero length encoding disabled # Data is just a big bunch of samples from one channel, then next channel, etc # unless board's last channel is read. Then signal header from next board and then again data # Each channel has an equal number of samples. for board_i, channels_active in enumerate( self.file_caen_pars["chan_active"]): if channels_active.sum( ) == 0: # if no channel is active there should be no signal header of the current board TODO: Check if that is really the case! continue # skip the current board event_signal_header_raw = np.fromfile(self.current_evefile, dtype=eve_signal_header, count=1)[0] event_signal_header = header_unpacker(event_signal_header_raw) for ch_i, channel_is_active in enumerate(channels_active): if channel_is_active == 0: continue # skip unused channels chdata = np.fromfile(self.current_evefile, dtype=np.int16, count=int( event_signal_header["page_size"])) event.pulses.append( Pulse(channel=ch_i + 8 * board_i, left=0, raw_data=np.array(chdata, dtype=np.int16))) elif self.file_caen_pars['zle'] == 1: # print(len(self.file_caen_pars["chan_active"])) for board_i, channels_active in enumerate( self.file_caen_pars["chan_active"]): # Skip nonexistent board if channels_active.sum( ) == 0: # if no channel is active there should be no signal header of the current board TODO: Check if that is really the case! continue # skip the current board event_signal_header_raw = np.fromfile(self.current_evefile, dtype=eve_signal_header, count=1)[0] event_signal_header = header_unpacker(event_signal_header_raw) channel_mask = event_signal_header["channel_mask"] channels_included = [ i for i in range(8) if (2**i & channel_mask) > 0 ] for ch_i in channels_included: # enumerate(channels_active): position = self.current_evefile.tell() channel_size = np.fromfile(self.current_evefile, dtype=np.uint32, count=1)[0] sample_position = 0 while (self.current_evefile.tell() < position + channel_size * 4): cword = np.fromfile(self.current_evefile, dtype=np.uint32, count=1)[0] if cword < 0x80000000: # if cword is less than 0x80000000 waveform is below zle threshold # skip word sample_position += 2 * cword continue else: chdata = np.fromfile(self.current_evefile, dtype=np.int16, count=2 * (cword - 0x80000000)) event.pulses.append( Pulse(channel=ch_i + 8 * board_i, left=sample_position, raw_data=chdata)) sample_position += 2 * (cword & (2**20 - 1)) # TODO: Check we have read all data for this event affe = hex( np.fromfile(self.current_evefile, dtype=np.uint32, count=1)[0]) if affe != '0xaffe': print( "WARNING : EVENT DID NOT END WITH 0XAFFE!! INSTEAD IT ENDED WITH ", affe) if event_position != len(self.event_positions) - 1: current_pos = self.current_evefile.tell() should_be_at_pos = self.event_positions[event_position + 1] if current_pos != should_be_at_pos: raise RuntimeError( "Error during XED reading: after reading event %d from file " "(event number %d) we should be at position %d, but we are at position %d!" % (event_position, event.event_number, should_be_at_pos, current_pos)) return event
def get_single_event_in_current_file(self, event_position=1): # Seek to the requested event # print(self.event_positions) self.current_evefile.seek(self.event_positions[event_position]) # self.current_evefile.seek(event_position, whence=io.SEEK_CUR) # Read event event header, check if it is a real data event or file event header or something different. event_event_header = np.fromfile(self.current_evefile, dtype=eve_event_header, count=1)[0] if event_event_header['event_type'] not in [3, 4]: # 3 = signal event, 4 = header event. Do others occur? raise NotImplementedError("Event type %i not yet implemented!" % event_event_header['event_type'], self.current_evefile.tell()) if event_event_header['event_type'] == 4: # it might be possible to get another event header along with caen1724.par stuff self.log.error("Unexpected event header at this position, trying to go on") self.file_caen_pars = np.fromfile(self.current_evefile, dtype=eve_caen1724_par_t, count=1)[0] event_event_header = np.fromfile(self.current_evefile, dtype=eve_event_header, count=1)[0] # Start building the event event = Event( n_channels=14, # never trust the config file start_time=int( event_event_header['event_timestamp'] * units.s # + # event_layer_metadata['utc_time_usec'] * units.us ), sample_duration=int(10 * units.ns), # 10 ns is the inverse of the sampling frequency 10MHz length=self.file_caen_pars['nof_samples'] # nof samples per event ) event.dataset_name = self.current_filename # now metadata available # as eve files do not have event numbers just count them event.event_number = event_position if self.file_caen_pars['zle'] == 0: # Zero length encoding disabled # Data is just a big bunch of samples from one channel, then next channel, etc # unless board's last channel is read. Then signal header from next board and then again data # Each channel has an equal number of samples. for board_i, channels_active in enumerate(self.file_caen_pars["chan_active"]): if channels_active.sum() == 0: # if no channel is active there should be no signal header of the current board TODO: Check if that is really the case! continue # skip the current board event_signal_header_raw = np.fromfile(self.current_evefile, dtype=eve_signal_header, count=1)[0] event_signal_header = header_unpacker(event_signal_header_raw) for ch_i, channel_is_active in enumerate(channels_active): if channel_is_active == 0: continue # skip unused channels chdata = np.fromfile(self.current_evefile, dtype=np.int16, count=int(event_signal_header["page_size"])) event.pulses.append(Pulse( channel=ch_i + 8 * board_i, left=0, raw_data=np.array(chdata, dtype=np.int16) )) elif self.file_caen_pars['zle'] == 1: # print(len(self.file_caen_pars["chan_active"])) for board_i, channels_active in enumerate(self.file_caen_pars["chan_active"]): # Skip nonexistent board if channels_active.sum() == 0: # if no channel is active there should be no signal header of the current board TODO: Check if that is really the case! continue # skip the current board event_signal_header_raw = np.fromfile(self.current_evefile, dtype=eve_signal_header, count=1)[0] event_signal_header = header_unpacker(event_signal_header_raw) channel_mask = event_signal_header["channel_mask"] channels_included = [i for i in range(8) if (2 ** i & channel_mask) > 0] for ch_i in channels_included: # enumerate(channels_active): position = self.current_evefile.tell() channel_size = np.fromfile(self.current_evefile, dtype=np.uint32, count=1)[0] sample_position = 0 while (self.current_evefile.tell() < position + channel_size * 4): cword = np.fromfile(self.current_evefile, dtype=np.uint32, count=1)[0] if cword < 0x80000000: # if cword is less than 0x80000000 waveform is below zle threshold # skip word sample_position += 2 * cword continue else: chdata = np.fromfile(self.current_evefile, dtype=np.int16, count=2 * (cword - 0x80000000)) event.pulses.append(Pulse( channel=ch_i + 8 * board_i, left=sample_position, raw_data=chdata )) sample_position += 2 * (cword & (2 ** 20 - 1)) # TODO: Check we have read all data for this event affe = hex(np.fromfile(self.current_evefile, dtype=np.uint32, count=1)[0]) if affe != '0xaffe': print("WARNING : EVENT DID NOT END WITH 0XAFFE!! INSTEAD IT ENDED WITH ", affe) if event_position != len(self.event_positions) - 1: current_pos = self.current_evefile.tell() should_be_at_pos = self.event_positions[event_position + 1] if current_pos != should_be_at_pos: raise RuntimeError("Error during XED reading: after reading event %d from file " "(event number %d) we should be at position %d, but we are at position %d!" % ( event_position, event.event_number, should_be_at_pos, current_pos)) return event