function [events,smoothed_events,fitz]=waveform_comparator(expt,scalefactor,evokedevents)
%takes a string input, loads the membrane current recordings using create_basic_text_file
%if evokedevents, removes the stimulus artifacts
%leak-corrects and correctly scales the current recordings
%creates criterion trace for each sweep using template_based_mini_detector
%finds success trials (evoked) or individual events (nonevoked)
%takes waveforms at fixed start time, using find_peaks_within_threshold_crossing_regions on the criterion
%shows each waveform, average waveform, boxcar-smoothed version of each waveform, and average of this
%fits the non-smoothed waveform and returns the fit and the fit parameters for each waveform, plots the fits and the average

%related scripts
%careful_latency_measurement
%find_detection_crits_and_latencies_generalizedform
%mini_waveform_extractor
%plot_natural_oligobouton_waveforms

close all
criterion_threshold=2;
fastemplate=monoexprisedecay_template_generator([14.16371820548567  11.31185487577630  19.35826590564763 0 1],1:50);fastemplate=[0 fastemplate];
extraction_window=-50:220; %for evokedevents, when pulling traces from the hdf file
event_jitter=30;   %used when finding peaks in the detection criterion
event_window=-30:80; %used when pulling events from the trace, based on the criterion
latency_window=20:40; %used when fitting events
samples_per_ms=10;
whiteout=[-3:3]; %for removing the stimulus artifact
baseline=1:abs(extraction_window(1))-5; %for fine-tuned leak correction

[a, pulse_duration_presyn, peak_times]=create_basic_text_file(expt,1,evokedevents,extraction_window,0);
a=a(:,2:2:end);%chgback
if evokedevents
ipis=diff(peak_times');
    for bb=1:size(a,2)  %removing stimulus artifact
        datatrace=artifact_elider(a(:,bb),abs(extraction_window(1)),whiteout); %removing the first part of the artifact
        datatrace=artifact_elider(datatrace,abs(extraction_window(1))+(pulse_duration_presyn*samples_per_ms),whiteout); %removing the second part of the artifact
        a(:,bb)=datatrace;
        if ipis(1,bb)<200,
            datatrace=artifact_elider(a(:,bb),abs(extraction_window(1))+ipis(bb),whiteout); %removing the first part of the artifact
            datatrace=artifact_elider(datatrace,abs(extraction_window(1))+ipis(bb)+(pulse_duration_presyn*samples_per_ms),whiteout); %removing the second part of the artifact
            a(:,bb)=datatrace;
        end
    end
end
a=a*scalefactor;
leak=mean(a);%(baseline,:)); %chgback
leak=repmat(leak,size(a,1),1);
a=a-leak;

clr=colormap(hsv(size(a,2)));
most_basic_colorcoded_plot(clr,a','')

crits=[];
%event_times=zeros(1,size(a,2)); chgback
for i = 1:size(a,2)
    trace=a(:,i);
    
    crit=template_based_mini_detector(trace',fastemplate)';
    
    event_peaks=find_peaks_within_threshold_crossing_regions(criterion_threshold,event_jitter,crit);
    
    if ~isempty(event_peaks)
        %event_times(i)=event_peaks(1);
        event_times{i}=event_peaks
    end
    
    %crits=[crits crit];
end

events=[];
smoothed_events=[];
fitz=[];
for j=1:length(event_times)
    if event_times{j}
        for k=1:length(event_times{j})
            event=a(event_times{j}(k)+event_window,j);
            event=event-mean(event([4:7 12:15]));
            events=[events event];

            smoothed_event=movingwindowsmooth(event,10);
            smoothed_events=[smoothed_events smoothed_event'];

            fitting=iterative_curve_fit_singledecay(event',latency_window, 0, 1);
            fitz=[fitz fitting'];   
        end
    end
end

clr=colormap(hsv(size(events,2)));

most_basic_colorcoded_plot(clr,events','events and mean')
z=mean(events');
plot(z,'k','linewidth',2)

most_basic_colorcoded_plot(clr,smoothed_events','smoothed events and mean')
zz=mean(smoothed_events');
plot(zz,'k','linewidth',2)

most_basic_colorcoded_plot(clr,fitz(1:110,:)','fits and mean')
zzz=mean(fitz(1:110,:)');
plot(zzz,'k','linewidth',2)







