function [asynch_count_by_trial,bgrd_asynch_count_by_trial,asynch_event_sizes_and_trials,bgrd_event_sizes_and_trials,preinduction_asynch_events,postinduction_asynch_events,...
    preinduction_bgrd_events,postinduction_bgrd_events]=careful_asynch_measurer(...
    threshold,asynchcriterionmat,asynchtracemat,trialcountmat,livetrials_preinduc,livetrials_postinduc,current_expt,minutes_per_trial)
%returns a vector with asynch count for each trial and another w/ background asynch count by trial, 
%as well as two two-column arrays (one array for asynch and one array for bgrd) where the first column gives the trial when an event occurred and the second gives the size of that event in fC 
%called by analyze_across_sd_stdp_expts, calls poor_mans_waterfall...

event_jitter=20    %in samples
first_asynch_sample_to_consider=150;
last_asynch_sample_to_consider=2149;
first_bgrd_sample_to_consider=40;
event_time_range=[-30:180];
threshold=4         %manually overriding the input
smooth_event_waveforms=1;
offset_correction=1;
plot_bgrd_events=1;
replace_nought_with_nan_in_occur_matrices=1;

number_of_samples_per_second=10000;
picocoulombs_to_femtocoulombs_converter=1000;   %the membrane current values are stored in picoamps, so the charge calculated will be picocoulombs until multiplied by this to get femtocoulombs
event_length=length(event_time_range);
event_length_fraction_of_second=event_length/number_of_samples_per_second;
all_livetrials=[livetrials_preinduc livetrials_postinduc]
lengthpreinduc=length(livetrials_preinduc);
lengthpostinduc=length(livetrials_postinduc);

switchpoint=find(isinf(asynchcriterionmat(:,1)))    %inf is used in patchfile_analysis to delineate the background portion, prior to the first AP, from the asynch portion, which follows the last AP in the train
prestimulation_asynch_criterion_mat=asynchcriterionmat(1:switchpoint(end)-1,:);
asynchcriterionmat=asynchcriterionmat(switchpoint(end)+1:end,:);
prestimulation_asynch_trace_mat=asynchtracemat(1:switchpoint(end)-1,:);
asynchtracemat=asynchtracemat(switchpoint(end)+1:end,:);

if smooth_event_waveforms
	smoothedasynchtracemat=[];smoothedbgrdtracemat=[];
	for trialforsmoothing=1:size(asynchtracemat,2)
        smoothedasynchtracemat(:,trialforsmoothing)=movingwindowsmooth(asynchtracemat(:,trialforsmoothing),10)';
        smoothedbgrdtracemat(:,trialforsmoothing)=movingwindowsmooth(prestimulation_asynch_trace_mat(:,trialforsmoothing),10)';
	end
	asynchtracemat=smoothedasynchtracemat;prestimulation_asynch_trace_mat=smoothedbgrdtracemat;
end

asynch_count_by_trial=[];bgrd_asynch_count_by_trial=[];preinduction_asynch_events=[];postinduction_asynch_events=[];preinduction_bgrd_events=[];postinduction_bgrd_events=[];
asynch_event_waveforms_at_time_of_occur=zeros(length(livetrials_preinduc)+length(livetrials_postinduc),last_asynch_sample_to_consider+event_time_range(2));
bgrd_event_waveforms_at_time_of_occur=zeros(length(livetrials_preinduc)+length(livetrials_postinduc),size(prestimulation_asynch_trace_mat,1));
for trial=all_livetrials
    if ~isnan(prestimulation_asynch_criterion_mat(1,trial)) %the idea was to have these initially be inf, then switch to nan if a given sweep should be omitted
        asynch_eventtimes_this_trial=find_peaks_within_threshold_crossing_regions(threshold,event_jitter,asynchcriterionmat(first_asynch_sample_to_consider:last_asynch_sample_to_consider,trial));
        asynch_eventtimes_this_trial=asynch_eventtimes_this_trial+first_asynch_sample_to_consider-1;
        asynch_count_by_trial=[asynch_count_by_trial,length(asynch_eventtimes_this_trial)];
        bgrd_eventtimes_this_trial=find_peaks_within_threshold_crossing_regions(threshold,event_jitter,prestimulation_asynch_criterion_mat(first_bgrd_sample_to_consider:end-(event_time_range(end)+1),trial));
        bgrd_eventtimes_this_trial=bgrd_eventtimes_this_trial+first_bgrd_sample_to_consider-1;
        bgrd_asynch_count_by_trial=[bgrd_asynch_count_by_trial,length(bgrd_eventtimes_this_trial)];
        for asynch_eventnum_this_trial=1:length(asynch_eventtimes_this_trial)
             asynch_event_span=asynch_eventtimes_this_trial(asynch_eventnum_this_trial)+event_time_range;
             this_asynch_event=asynchtracemat(asynch_event_span,trial)';
             if offset_correction,this_asynch_event=this_asynch_event-mean(this_asynch_event(1:(-1*event_time_range(1))));end
             if trial<=livetrials_preinduc(end)
                 asynch_event_waveforms_at_time_of_occur(trial-(livetrials_preinduc(1)-1),asynch_event_span)=this_asynch_event;
             elseif trial<=livetrials_postinduc(end)
                 asynch_event_waveforms_at_time_of_occur(trial-(livetrials_postinduc(1)-1)+lengthpreinduc,asynch_event_span)=this_asynch_event;
             end
             mean_current_this_asynch_event=sum(this_asynch_event)/event_length;
             charge_this_asynch_event=-1*mean_current_this_asynch_event*event_length_fraction_of_second*picocoulombs_to_femtocoulombs_converter;
             peak_this_asynch_event=-1*min(this_asynch_event);
             if trial<=trialcountmat(1)
                 preinduction_asynch_events=[preinduction_asynch_events;this_asynch_event asynch_eventtimes_this_trial(asynch_eventnum_this_trial) charge_this_asynch_event peak_this_asynch_event trial];   %note that provisions have not been made for cases where the event is very close to the beginning or the end
             else
                 postinduction_asynch_events=[postinduction_asynch_events;this_asynch_event asynch_eventtimes_this_trial(asynch_eventnum_this_trial) charge_this_asynch_event peak_this_asynch_event trial];
             end
		end
		for bgrd_eventnum_this_trial=1:length(bgrd_eventtimes_this_trial)
             bgrd_event_span=bgrd_eventtimes_this_trial(bgrd_eventnum_this_trial)+event_time_range;
             this_bgrd_event=prestimulation_asynch_trace_mat(bgrd_event_span,trial)';
             if offset_correction,this_bgrd_event=this_bgrd_event-mean(this_bgrd_event(1:(-1*event_time_range(1))));end
             if trial<=livetrials_preinduc(end)
                 bgrd_event_waveforms_at_time_of_occur(trial-(livetrials_preinduc(1)-1),bgrd_event_span)=this_bgrd_event;
             elseif trial<=livetrials_postinduc(end)
                 bgrd_event_waveforms_at_time_of_occur(trial-(livetrials_postinduc(1)-1)+lengthpreinduc,bgrd_event_span)=this_bgrd_event;
             end
             mean_current_this_bgrd_event=sum(this_bgrd_event)/event_length;
             charge_this_bgrd_event=-1*mean_current_this_bgrd_event*event_length_fraction_of_second*picocoulombs_to_femtocoulombs_converter;
             peak_this_bgrd_event=-1*min(this_bgrd_event);
             if trial<=trialcountmat(1)
                 preinduction_bgrd_events=[preinduction_bgrd_events;this_bgrd_event bgrd_eventtimes_this_trial(bgrd_eventnum_this_trial) charge_this_bgrd_event peak_this_bgrd_event trial];   %note that provisions have not been made for cases where the event is very close to the beginning or the end
             else
                 postinduction_bgrd_events=[postinduction_bgrd_events;this_bgrd_event bgrd_eventtimes_this_trial(bgrd_eventnum_this_trial) charge_this_bgrd_event peak_this_bgrd_event trial];
             end
		end 
    else
        asynch_count_by_trial=[asynch_count_by_trial nan];  bgrd_asynch_count_by_trial=[bgrd_asynch_count_by_trial nan];
    end
end
if isempty(preinduction_asynch_events),preinduction_asynch_events=repmat(nan,1,length(event_time_range)+4),end
if isempty(postinduction_asynch_events),postinduction_asynch_events=repmat(nan,1,length(event_time_range)+4),end
if isempty(preinduction_bgrd_events),preinduction_bgrd_events=repmat(nan,1,length(event_time_range)+4),end
if isempty(postinduction_bgrd_events),postinduction_bgrd_events=repmat(nan,1,length(event_time_range)+4),end

asynch_event_sizes_and_trials=fliplr([preinduction_asynch_events;postinduction_asynch_events]);
bgrd_event_sizes_and_trials=fliplr([preinduction_bgrd_events;postinduction_bgrd_events]);

if ~isnan(preinduction_asynch_events(1,1)) && ~isnan(postinduction_asynch_events(1,1))
	zlim_trough=min([min(min(preinduction_asynch_events(:,1:end-4)')) min(min(postinduction_asynch_events(:,1:end-4)'))])
elseif ~isnan(preinduction_asynch_events(1,1))
    zlim_trough=min(min(preinduction_asynch_events(:,1:end-4)')) 
elseif ~isnan(postinduction_asynch_events(1,1))
    zlim_trough=min(min(postinduction_asynch_events(:,1:end-4)'))
end

%a dummy trial is added at the beginning and end, and the colors of these two trials are set at the extremes possible, to trick the waterfall plotter into assigning the intended colors
if ~isnan(preinduction_asynch_events(1,1)),poor_mans_waterfall([repmat(nan,1,size(preinduction_asynch_events,2)-4);preinduction_asynch_events(:,1:end-4);repmat(nan,1,size(preinduction_asynch_events,2)-4)]',...
        current_expt,600,'Asynch events following last AP preinduc',zlim_trough,[1;preinduction_asynch_events(:,end)-livetrials_preinduc(1)+1;lengthpreinduc],minutes_per_trial),end
%a dummy trial is added at the beginning and end, and the colors of these two trials are set at the extremes possible, to trick the waterfall plotter into assigning the intended colors
if ~isnan(postinduction_asynch_events(1,1)),poor_mans_waterfall([repmat(nan,1,size(postinduction_asynch_events,2)-4);postinduction_asynch_events(:,1:end-4);repmat(nan,1,size(postinduction_asynch_events,2)-4)]',...
        current_expt,700,'Asynch events following last AP postinduc',zlim_trough,[1;postinduction_asynch_events(:,end)-livetrials_postinduc(1)+1;lengthpostinduc],minutes_per_trial),end

if any(any(asynch_event_waveforms_at_time_of_occur))
   if replace_nought_with_nan_in_occur_matrices,asynch_event_waveforms_at_time_of_occur(find(~asynch_event_waveforms_at_time_of_occur))=nan;end
   if ~isnan(preinduction_asynch_events(1,1)) && ~isnan(postinduction_asynch_events(1,1))
		poor_mans_waterfall(asynch_event_waveforms_at_time_of_occur(1:lengthpreinduc,:)',current_expt,800,'Asynch events following last AP preinduc',zlim_trough,[1:lengthpreinduc]',minutes_per_trial)
        poor_mans_waterfall(asynch_event_waveforms_at_time_of_occur((lengthpreinduc+1):end,:)',current_expt,900,'Asynch events following last AP postinduc',zlim_trough,[1:lengthpostinduc]',minutes_per_trial)
	elseif ~isnan(preinduction_asynch_events(1,1))
        poor_mans_waterfall(asynch_event_waveforms_at_time_of_occur(1:lengthpreinduc,:)',current_expt,800,'Asynch events following last AP preinduc',zlim_trough,[1:lengthpreinduc]',minutes_per_trial)
	elseif ~isnan(postinduction_asynch_events(1,1))
        poor_mans_waterfall(asynch_event_waveforms_at_time_of_occur((lengthpreinduc+1):end,:)',current_expt,900,'Asynch events following last AP postinduc',zlim_trough,[1:lengthpostinduc]',minutes_per_trial)
	end
end

if plot_bgrd_events
    %a dummy trial is added at the beginning and end, and the colors of these two trials are set at the extremes possible, to trick the waterfall plotter into assigning the intended colors
    if ~isnan(preinduction_bgrd_events(1,1)), poor_mans_waterfall([repmat(nan,1,size(preinduction_bgrd_events,2)-4);preinduction_bgrd_events(:,1:end-4);repmat(nan,1,size(preinduction_bgrd_events,2)-4)]',...
            current_expt,1600,'Background events preinduc',zlim_trough,[1;preinduction_bgrd_events(:,end)-livetrials_preinduc(1)+1;lengthpreinduc],minutes_per_trial),end
    %a dummy trial is added at the beginning and end, and the colors of these two trials are set at the extremes possible, to trick the waterfall plotter into assigning the intended colors
    if ~isnan(postinduction_bgrd_events(1,1)),poor_mans_waterfall([repmat(nan,1,size(postinduction_bgrd_events,2)-4);postinduction_bgrd_events(:,1:end-4);repmat(nan,1,size(postinduction_bgrd_events,2)-4)]',...
            current_expt,1700,'Background events postinduc',zlim_trough,[1;postinduction_bgrd_events(:,end)-livetrials_postinduc(1)+1;lengthpostinduc],minutes_per_trial),end
		
	if any(any(bgrd_event_waveforms_at_time_of_occur))
       if replace_nought_with_nan_in_occur_matrices,bgrd_event_waveforms_at_time_of_occur(find(~bgrd_event_waveforms_at_time_of_occur))=nan;end
       if ~isnan(preinduction_bgrd_events(1,1)) && ~isnan(postinduction_bgrd_events(1,1))
			poor_mans_waterfall(bgrd_event_waveforms_at_time_of_occur(1:lengthpreinduc,:)',current_expt,1800,'Background events preinduc',zlim_trough,[1:lengthpreinduc]',minutes_per_trial)
			poor_mans_waterfall(bgrd_event_waveforms_at_time_of_occur((lengthpreinduc+1):end,:)',current_expt,1900,'Background events postinduc',zlim_trough,[1:lengthpostinduc]',minutes_per_trial)
	   elseif ~isnan(preinduction_bgrd_events(1,1))
            poor_mans_waterfall(bgrd_event_waveforms_at_time_of_occur(1:lengthpreinduc,:)',current_expt,1800,'Background events preinduc',zlim_trough,[1:lengthpreinduc]',minutes_per_trial)
	   elseif ~isnan(postinduction_bgrd_events(1,1))
            poor_mans_waterfall(bgrd_event_waveforms_at_time_of_occur((lengthpreinduc+1):end,:)',current_expt,1900,'Background events postinduc',zlim_trough,[1:lengthpostinduc]',minutes_per_trial)
	   end
	end    
end