%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 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 = load('flash_20170801_1008_wo_filter_12h.dat');
data_hist_wo_filter = load('F1 id220_id220_fiber_coupler_fiber_6m_flash_wo_filter_12h_hist.dat');

data_count_w_filter = load('flash_20170802_0008_w_1300nm_filter_12h.dat');
%data_hist_w_filter = load('');

%data_count_dark = load('');
%data_hist_dark = load('');

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

% evaluating absolute probability of detection of breakdown flash wo filter

single_wo_filter_mean = mean(data_count_wo_filter(:, 2));
single_wo_filter_std = std(data_count_wo_filter(:, 2));

coin_wo_filter_mean = mean(data_count_wo_filter(:, 3));
coin_wo_filter_std = std(data_count_wo_filter(:, 3));

prob_wo_filter_mean = coin_wo_filter_mean/single_wo_filter_mean;
prob_wo_filter_std = abs(prob_wo_filter_mean)*sqrt((single_wo_filter_std/single_wo_filter_mean)^2 +...
    (coin_wo_filter_std/coin_wo_filter_mean)^2);

[prob_wo_filter_mean, prob_wo_filter_std]*100


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

% evaluating absolute probability of detection of breakdown flash w filter

single_w_filter_mean = mean(data_count_w_filter(:, 2));
single_w_filter_std = std(data_count_w_filter(:, 2));

coin_w_filter_mean = mean(data_count_w_filter(:, 3));
coin_w_filter_std = std(data_count_w_filter(:, 3));

prob_w_filter_mean = coin_w_filter_mean/single_w_filter_mean;
prob_w_filter_std = abs(prob_w_filter_mean)*sqrt((single_w_filter_std/single_w_filter_mean)^2 +...
    (coin_w_filter_std/coin_w_filter_mean)^2);

[prob_w_filter_mean, prob_w_filter_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]);

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

