def data_aligned_at_events(table, start_event_number=None, stop_event_number=None, start_index=None, stop_index=None, chunk_size=<NUM_LIT>, try_speedup=False, first_event_aligned=True, fail_on_missing_events=True):
<EOL>start_index_known = False<EOL>stop_index_known = False<EOL>start_index = <NUM_LIT:0> if start_index is None else start_index<EOL>stop_index = table.nrows if stop_index is None else stop_index<EOL>if stop_index < start_index:<EOL><INDENT>raise InvalidInputError('<STR_LIT>')<EOL><DEDENT>table_max_rows = table.nrows<EOL>if stop_event_number is not None and start_event_number is not None and stop_event_number < start_event_number:<EOL><INDENT>raise InvalidInputError('<STR_LIT>')<EOL><DEDENT>if try_speedup and table.colindexed["<STR_LIT>"]:<EOL><INDENT>if start_event_number is not None:<EOL><INDENT>start_condition = '<STR_LIT>' + str(start_event_number)<EOL>start_indices = table.get_where_list(start_condition, start=start_index, stop=stop_index)<EOL>if start_indices.shape[<NUM_LIT:0>] != <NUM_LIT:0>:  <EOL><INDENT>start_index = start_indices[<NUM_LIT:0>]<EOL>start_index_known = True<EOL><DEDENT><DEDENT>if stop_event_number is not None:<EOL><INDENT>stop_condition = '<STR_LIT>' + str(stop_event_number)<EOL>stop_indices = table.get_where_list(stop_condition, start=start_index, stop=stop_index)<EOL>if stop_indices.shape[<NUM_LIT:0>] != <NUM_LIT:0>:  <EOL><INDENT>stop_index = stop_indices[<NUM_LIT:0>]<EOL>stop_index_known = True<EOL><DEDENT><DEDENT><DEDENT>if start_index_known and stop_index_known and start_index + chunk_size >= stop_index:  <EOL><INDENT>yield table.read(start=start_index, stop=stop_index), stop_index<EOL><DEDENT>else:  <EOL><INDENT>current_start_index = start_index<EOL>if start_event_number is not None:<EOL><INDENT>while current_start_index < stop_index:<EOL><INDENT>current_stop_index = min(current_start_index + chunk_size, stop_index)<EOL>array_chunk = table.read(start=current_start_index, stop=current_stop_index)  <EOL>last_event_in_chunk = array_chunk["<STR_LIT>"][-<NUM_LIT:1>]<EOL>if last_event_in_chunk < start_event_number:<EOL><INDENT>current_start_index = current_start_index + chunk_size  <EOL><DEDENT>else:<EOL><INDENT>first_event_in_chunk = array_chunk["<STR_LIT>"][<NUM_LIT:0>]<EOL><INDENT>if stop_event_number is not None and first_event_in_chunk >= stop_event_number and start_index != <NUM_LIT:0> and start_index == current_start_index:<EOL><INDENT>raise InvalidInputError('<STR_LIT>' % stop_event_number)<EOL><DEDENT><DEDENT>if array_chunk.shape[<NUM_LIT:0>] == chunk_size and first_event_in_chunk == last_event_in_chunk:<EOL><INDENT>raise InvalidInputError('<STR_LIT>')<EOL><DEDENT>if not first_event_aligned and first_event_in_chunk == start_event_number and start_index != <NUM_LIT:0> and start_index == current_start_index:  <EOL><INDENT>if fail_on_missing_events:<EOL><INDENT>raise InvalidInputError('<STR_LIT>' % start_event_number)<EOL><DEDENT>chunk_start_index = np.searchsorted(array_chunk["<STR_LIT>"], start_event_number + <NUM_LIT:1>, side='<STR_LIT:left>')<EOL><DEDENT>elif fail_on_missing_events and first_event_in_chunk > start_event_number and start_index == current_start_index:<EOL><INDENT>raise InvalidInputError('<STR_LIT>' % start_event_number)<EOL><DEDENT>elif first_event_aligned and first_event_in_chunk == start_event_number and start_index == current_start_index:<EOL><INDENT>chunk_start_index = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>chunk_start_index = np.searchsorted(array_chunk["<STR_LIT>"], start_event_number, side='<STR_LIT:left>')<EOL>if fail_on_missing_events and array_chunk["<STR_LIT>"][chunk_start_index] != start_event_number and start_index == current_start_index:<EOL><INDENT>raise InvalidInputError('<STR_LIT>' % start_event_number)<EOL>
Takes the table with a event_number column and returns chunks with the size up to chunk_size. The chunks are chosen in a way that the events are not splitted.
    Additional parameters can be set to increase the readout speed. Events between a certain range can be selected.
    Also the start and the stop indices limiting the table size can be specified to improve performance.
    The event_number column must be sorted.
    In case of try_speedup is True, it is important to create an index of event_number column with pytables before using this function. Otherwise the queries are slowed down.

    Parameters
    ----------
    table : pytables.table
        The data.
    start_event_number : int
        The retruned data contains events with event number >= start_event_number. If None, no limit is set.
    stop_event_number : int
        The retruned data contains events with event number < stop_event_number. If None, no limit is set.
    start_index : int
        Start index of data. If None, no limit is set.
    stop_index : int
        Stop index of data. If None, no limit is set.
    chunk_size : int
        Maximum chunk size per read.
    try_speedup : bool
        If True, try to reduce the index range to read by searching for the indices of start and stop event number. If these event numbers are usually
        not in the data this speedup can even slow down the function!

    The following parameters are not used when try_speedup is True:

    first_event_aligned : bool
        If True, assuming that the first event is aligned to the data chunk and will be added. If False, the lowest event number of the first chunk will not be read out.
    fail_on_missing_events : bool
        If True, an error is given when start_event_number or stop_event_number is not part of the data.

    Returns
    -------
    Iterator of tuples
        Data of the actual data chunk and start index for the next chunk.

    Example
    -------
    start_index = 0
    for scan_parameter in scan_parameter_range:
        start_event_number, stop_event_number = event_select_function(scan_parameter)
        for data, start_index in data_aligned_at_events(table, start_event_number=start_event_number, stop_event_number=stop_event_number, start_index=start_index):
            do_something(data)

    for data, index in data_aligned_at_events(table):
        do_something(data)