%% plot chargelitudes

clear all; close all;
load xeko234_alldata

n_cutoff = 0;
n_firstTTX = 9;
n_firstCTZ = 19;

figure(1); hold off;

for i = 1:length(dat)
    tmp_charge = dat{i}(:,6);
    subplot(2,1,1);
    h = plot(i*ones(size(tmp_charge))+0.15*randn(size(tmp_charge)),tmp_charge,'.k');
    hold on;
    if i >= n_firstCTZ
        set(h,'Color','r');
    elseif i >= n_firstTTX
        set(h,'Color','b');
    end
    charge.mean(i) = nanmean(tmp_charge);
    charge.n(i) = length(tmp_charge);
    charge.stderr(i) = nanstd(tmp_charge)/sqrt(charge.n(i));
    disp([fidlist{i},': ',num2str(charge.mean(i)),' +/- ', ...
        num2str(charge.stderr(i)), ' pA', ...
        '   (n=',num2str(charge.n(i)),')']);
end
ylabel('Charge Transfer (fC)','FontSize',14);

chargeMean(:,1) = charge.mean(charge.mean<60 & charge.mean>1/100);
chargeMean(:,2) = charge.stderr(charge.mean<60 & charge.mean>1/100);

subplot(2,1,2); hold off;
errorbar(1:n_firstTTX-1,chargeMean(1:n_firstTTX-1,1),chargeMean(1:n_firstTTX-1,2),'ko'); hold on;
errorbar(n_firstTTX:n_firstCTZ-1,chargeMean(n_firstTTX:n_firstCTZ-1,1),chargeMean(n_firstTTX:n_firstCTZ-1,2),'bo');
errorbar(n_firstCTZ:size(chargeMean,1),chargeMean(n_firstCTZ:end,1),chargeMean(n_firstCTZ:end,2),'ro');


chargeMean_cutoff(:,1) = charge.mean(charge.n>=n_cutoff);
chargeMean_cutoff(:,2) = charge.stderr(charge.n>=n_cutoff);
n_CTZ_inc = 0;

subplot(2,1,1);
plot([0.5,size(chargeMean,1)+0.5],[mean(chargeMean_cutoff(1:n_firstTTX-1-n_CTZ_inc,1)) mean(chargeMean_cutoff(1:n_firstTTX-1-n_CTZ_inc,1))],'k--');
plot([0.5,size(chargeMean,1)+0.5],[mean(chargeMean_cutoff(n_firstTTX-n_CTZ_inc:n_firstCTZ-1-n_CTZ_inc,1)) mean(chargeMean_cutoff(n_firstTTX-n_CTZ_inc:n_firstCTZ-1-n_CTZ_inc,1))],'b--');
%plot([0.5,size(chargeMean,1)+0.5],[mean(chargeMean_cutoff(n_firstCTZ-n_CTZ_inc:end,1)) mean(chargeMean_cutoff(n_firstCTZ-n_CTZ_inc:end,1))],'r--');
plot([0.5,size(chargeMean,1)+0.5],[mean(chargeMean_cutoff([n_firstCTZ-n_CTZ_inc:24,26:27],1)) mean(chargeMean_cutoff([n_firstCTZ-n_CTZ_inc:24,26:27],1))],'r--');
%axis([0 29 -1 70]);
xlim([0 29]);

subplot(2,1,2);
plot([0.5,size(chargeMean,1)+0.5],[mean(chargeMean_cutoff(1:n_firstTTX-1-n_CTZ_inc,1)) mean(chargeMean_cutoff(1:n_firstTTX-1-n_CTZ_inc,1))],'k--');
plot([0.5,size(chargeMean,1)+0.5],[mean(chargeMean_cutoff(n_firstTTX-n_CTZ_inc:n_firstCTZ-1-n_CTZ_inc,1)) mean(chargeMean_cutoff(n_firstTTX-n_CTZ_inc:n_firstCTZ-1-n_CTZ_inc,1))],'b--');
%plot([0.5,size(chargeMean,1)+0.5],[mean(chargeMean_cutoff(n_firstCTZ-n_CTZ_inc:end,1)) mean(chargeMean_cutoff(n_firstCTZ-n_CTZ_inc:end,1))],'r--');
plot([0.5,size(chargeMean,1)+0.5],[mean(chargeMean_cutoff([n_firstCTZ-n_CTZ_inc:24,26:27],1)) mean(chargeMean_cutoff([n_firstCTZ-n_CTZ_inc:24,26:27],1))],'r--');
%axis([0 29 0 25]);
xlim([0 29]);

ylabel('Charge Transfer (fC)','FontSize',14);