%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Script to Process Histogram and Counter Data for Breakdown Flash
% Probability
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% id220, 3m fibre - coupler-coupler-3m fibre, 1300nm +- 12nm filter used,...
% 12h integration time for all measurments

data_count_wo_filter_a2b = load('flash_20170810_1108_wo_filter_12h_a2b.dat');
data_count_w_filter_a2b = load('.dat');

data_count_wo_filter_b2a = load('flash_20170808_1708_wo_filter_12h_b2a.dat');
data_count_w_filter_b2a = load('flash_20170809_1508_w_filter_12h_b2a.dat');

data_hist_wo_filter = load('.dat');
data_hist_w_filter = load('.dat');

data_count_dark = load('.dat');
data_hist_dark = load('.dat');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% a2b
% evaluating absolute probability of detection of breakdown flash wo filter

single_wo_filter_a2b_mean = mean(data_count_wo_filter_a2b(:, 2));
single_wo_filter_a2b_std = std(data_count_wo_filter_a2b(:, 2));

%single_wo_filter_a2b_mean = mean(data_count_wo_filter_a2b(:, 1));
%single_wo_filter_a2b_std = std(data_count_wo_filter_a2b(:, 1));

coin_wo_filter_a2b_mean = mean(data_count_wo_filter_a2b(:, 3));
coin_wo_filter_a2b_std = std(data_count_wo_filter_a2b(:, 3));

prob_wo_filter_a2b_mean = coin_wo_filter_a2b_mean/single_wo_filter_a2b_mean;
prob_wo_filter_a2b_std = abs(prob_wo_filter_a2b_mean)*sqrt((single_wo_filter_a2b_std/single_wo_filter_a2b_mean)^2 +...
    (coin_wo_filter_a2b_std/coin_wo_filter_a2b_mean)^2);

[prob_wo_filter_a2b_mean, prob_wo_filter_a2b_std]*100


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% evaluating absolute probability of detection of breakdown flash w filter

single_w_filter_a2b_mean = mean(data_count_w_filter_a2b(:, 2));
single_w_filter_a2b_std = std(data_count_w_filter_a2b(:, 2));


%single_w_filter_a2b_mean = mean(data_count_w_filter_a2b(:, 1));
%single_w_filter_a2b_std = std(data_count_w_filter_a2b(:, 1));

coin_w_filter_a2b_mean = mean(data_count_w_filter_a2b(:, 3));
coin_w_filter_a2b_std = std(data_count_w_filter_a2b(:, 3));

prob_w_filter_a2b_mean = coin_w_filter_a2b_mean/single_w_filter_a2b_mean;
prob_w_filter_a2b_std = abs(prob_w_filter_a2b_mean)*sqrt((single_w_filter_a2b_std/single_w_filter_a2b_mean)^2 +...
    (coin_w_filter_a2b_std/coin_w_filter_a2b_mean)^2);

[prob_w_filter_a2b_mean, prob_w_filter_a2b_std]*100

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% b2a
% evaluating absolute probability of detection of breakdown flash wo filter

single_wo_filter_b2a_mean = mean(data_count_wo_filter_b2a(:, 2));
single_wo_filter_b2a_std = std(data_count_wo_filter_b2a(:, 2));

coin_wo_filter_b2a_mean = mean(data_count_wo_filter_b2a(:, 3));
coin_wo_filter_b2a_std = std(data_count_wo_filter_b2a(:, 3));

prob_wo_filter_b2a_mean = coin_wo_filter_b2a_mean/single_wo_filter_b2a_mean;
prob_wo_filter_b2a_std = abs(prob_wo_filter_b2a_mean)*sqrt((single_wo_filter_b2a_std/single_wo_filter_b2a_mean)^2 +...
    (coin_wo_filter_b2a_std/coin_wo_filter_b2a_mean)^2);

[prob_wo_filter_b2a_mean, prob_wo_filter_b2a_std]*100

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% evaluating absolute probability of detection of breakdown flash w filter

single_w_filter_b2a_mean = mean(data_count_w_filter_b2a(:, 2));
single_w_filter_b2a_std = std(data_count_w_filter_b2a(:, 2));

coin_w_filter_b2a_mean = mean(data_count_w_filter_b2a(:, 3));
coin_w_filter_b2a_std = std(data_count_w_filter_b2a(:, 3));

prob_w_filter_b2a_mean = coin_w_filter_b2a_mean/single_w_filter_b2a_mean;
prob_w_filter_b2a_std = abs(prob_w_filter_b2a_mean)*sqrt((single_w_filter_b2a_std/single_w_filter_b2a_mean)^2 +...
    (coin_w_filter_b2a_std/coin_w_filter_b2a_mean)^2);

[prob_w_filter_b2a_mean, prob_w_filter_b2a_std]*100

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


offset = 127.1500;

semilogy(data_hist_wo_filter(:, 1)*1e9 - offset, data_hist_wo_filter(:, 2), 'r',...
    data_hist_wo_filter(:, 1)*1e9 - offset, data_hist_wo_filter(:, 2), 'b'),...
    xlabel('Time (ns)'),...
    ylabel('No. of Events'),...
    title('No. of Events versus TIme(ns)'),...
    grid;
axis([-100, 100, 0, 1e6]);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% use command to replace null characters
%tr < file-with-nulls -d '\0' > file-without-nulls

%a -> b wo filter [0.4413 +- 0.0236]
%a -> b w  filter [0.0049 +- 0.0023]
%b -> a wo filter [0.4153 +- 0.0341]
%b -> a w  filter [0.0057 +- 0.0033]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

data_count_wo_filter_a2b_raw = load('flash_20170810_1108_wo_filter_12h_a2b_raw');
data_count_wo_filter_a2b_raw(data_count_wo_filter_a2b_raw(:, 3) < 300, :) = [];
data_count_wo_filter_a2b = data_count_wo_filter_a2b_raw;

data_count_w_filter_a2b_raw = load('flash_20170811_00008_w_filter_12h_a2b_raw');
data_count_w_filter_a2b = data_count_w_filter_a2b_raw;

data_count_wo_filter_b2a_raw = load('flash_20170808_1708_wo_filter_12h_b2a_raw');
data_count_wo_filter_b2a_raw(data_count_wo_filter_b2a_raw(:, 3) < 150, :) = [];
data_count_wo_filter_b2a = data_count_wo_filter_b2a_raw;

data_count_w_filter_b2a_raw = load('flash_20170809_1508_w_filter_12h_b2a_raw');
data_count_w_filter_b2a = data_count_w_filter_b2a_raw;

index = 1:size(data_count_wo_filter_a2b_raw, 1);

plot(index,data_count_wo_filter_a2b_raw(:, 3),...
     index,data_count_wo_filter_b2a_raw(:, 3),...
     index,data_count_w_filter_b2a_raw(:, 3)),...
     xlabel('Time (h)'),...
     ylabel('Cold Temperature (^\circC)'),...
     title('Cold Temperature (^\circC) versus Time (h)'),...
     grid;

 plot(index,data_count_wo_filter_a2b_raw(:, 1),...
     index,data_count_wo_filter_a2b_raw(:, 2),...
     index,data_count_wo_filter_b2a_raw(:, 1),...
     index,data_count_wo_filter_b2a_raw(:, 2),...
     index,data_count_w_filter_b2a_raw(:, 1),...
     index,data_count_w_filter_b2a_raw(:, 2)),...
     xlabel('Time (h)'),...
     ylabel('Cold Temperature (^\circC)'),...
     title('Cold Temperature (^\circC) versus Time (h)'),...
     grid;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

plot(index,data_count_wo_filter_a2b(:, 1),...
     index,data_count_wo_filter_a2b(:, 2),...
     index,data_count_wo_filter_b2a(:, 1),...
     index,data_count_wo_filter_b2a(:, 2),...
     index,data_count_w_filter_b2a(:, 1),...
     index,data_count_w_filter_b2a(:, 2)),...
     xlabel('Time (h)'),...
     ylabel('Cold Temperature (^\circC)'),...
     title('Cold Temperature (^\circC) versus Time (h)'),...
     grid;
 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
