function events = get_events (self, tag)
    % events = GET_EVENTS (self, tag)
    %
    % Returns all events associated with tag.
    %
    % Input:
    %  - self = @UbisenseDatabase instance
    %  - tag = tag identifier; either integer value, or the string printed
    %    on the tag (AAA-BBB-CCC-DDD format)
    %
    % Output:
    %  - events = array of (valid) events structures
    %
    % Copyright (C) 2011-2012, Rok Mandeljc <rok.mandeljc@fe.uni-lj.si>
    
    % Convert tag string to tag int
    if isa(tag, 'char'),
        tag = self.get_tag_id_from_string(tag);
    end
    
    %% Query
    % Get all the detections for specified tag
    tag_mask = self.data.tag == tag;
    
    % Remove all invalid detections (x = 0, y = 0 and z = 0)
    invalid = self.data.x == 0 | self.data.y == 0 | self.data.z == 0;
    
    mask = tag_mask & ~invalid;
    
    %% Copy the fields
    fields = fieldnames(self.data);
    for f = 1:numel(fields),
        events.(fields{f}) = self.data.(fields{f})(mask);
    end
end