function [goodindices] = muon_eventselector(directory, numstart,...
    numend, voltagethresh, pulsethresh)
%[goodindices] = muon_eventselector(directory, numstart,...
%                               numend, voltagethresh, pulsethresh)
%Selects muon events based on desired criteria for pulse height
%(voltagethresh) and width (pulsethresh).  Also creates a folder in the 
%input directory called goodfiles and copies the selected files into it.
%
%INPUT:     directory - the directory where files live (note text must be
%                           surrounded by apostrophes)
%           numstart - the starting event index
%           numend - the ending event index
%           voltagethresh - a POSITIVE voltage beyond which the absolute
%                           value of a data point will register as a pulse
%           pulsethresh - the maximum number of data points that can belong
%                           in a pulse
%
%OUTPUT:    goodindices - a column vector of the file indices of events
%                           that satisfy the given criteria
%
%WARNING: THIS PROGRAM DOES NOT WORK WITH FILNAMES THAT INCLUDE A SPACE.
%   Please eliminate all spaces from filenames.  The filename convention is
%       SCxxx.txt where xxx is the file index.
%
%Edit the function file to change the event parameters.
%Written by David Tam, Columbia Univeristy Dept. of Physics, November 2009

directory
cd(directory)

%mkdir('goodfiles')
goodfiles=[];
goodindices=[];

for findex=numstart:numend
    
    %findex
    
    %fname = [direc 'SC' num2str(findex) '.txt'];
    fname = ['SC' num2str(findex) '.txt'];
    
    fid=fopen(fname);
    event=fscanf(fid,'%g');
    
    %INVERT PULSES
    event = event.*-1;
    
    %set selection parameters
    %voltagethresh=0.1;
    %pulsethresh=50;
    
    iseventgood=1;
    i=1;
    
    %event
    
    if event(i)>voltagethresh
        iseventgood=0;
        continue
    end
    
    %find first pulse, check length
    while event(i)<voltagethresh
        i=i+1;
        if i==length(event)
            iseventgood=0;
            break
        end
    end
    j=0;
    while event(i)>voltagethresh || abs(event(i))==Inf
        i=i+1;
        j=j+1;
        if i==length(event)
            iseventgood=0;
            break
        end
    end
    if j>pulsethresh
        iseventgood=0;
    end
    
    if event(i)>voltagethresh
        iseventgood=0;
        continue
    end
    
    
    %find second pulse, check length
    while event(i)<voltagethresh
        i=i+1;
        if i==length(event)
            iseventgood=0;
            break
        end
    end
    k=0;
    while event(i)>voltagethresh || abs(event(i))==Inf
        i=i+1;
        k=k+1;
        if i==length(event)
            iseventgood=0;
            break
        end
    end
    if k>pulsethresh
        iseventgood=0;
    end
    
    if event(i)>voltagethresh
        iseventgood=0;
        continue
    end
    
    %examine rest of file
    while event(i)<voltagethresh
        i=i+1;
        if i>length(event)
            break
        end
    end
    
    if i~=length(event)+1
        iseventgood=0;
        continue
    end
    
    if iseventgood==1
        %goodfiles = [goodfiles; ['SC ' num2str(findex) '.txt']];
        goodindices = [goodindices; findex];
        cd goodfiles
        dlmwrite(fname,event.*-1,'newline','pc');
        cd ..
    end
    
    fid=fclose(fid);
    
end


%save a text file of the selected filenames
%fid=fopen('goodfiles.txt','w');
%for i=1:length(goodindices)
%    fprintf(fid,'SC%s.txt\t',num2str(goodindices(i)));
%end
%fclose(fid);


end