% reconstruction point cloud
results = load("/Volumes/home/test/calib/standard_nospill/tracks/z001_t001.mat");
pos = [results.tracks.x];

% ground truth
truth = load("/Volumes/home/array/tcf/rescaled/z001_t001.mat");

% threshold for what will be considered a ghost particle
threshold = 0.02;

nearests = zeros(size(pos, 2), 2);

fprintf("loading done\n");

for i = 1:size(pos, 2)

    reconstruction_ary = zeros(3, size(truth.x, 2));
    reconstruction_ary(1, :) = pos(1, i);
    reconstruction_ary(2, :) = pos(2, i);
    reconstruction_ary(3, :) = pos(3, i);

    distances = sum((reconstruction_ary - truth.x).^2);
    
    [val, index] = min(distances);
    nearests(i, 1) = val;
    nearests(i, 2) = index;
end

a = (nearests(:, 1) - threshold) > 0; 

fprintf("%d ghost particles detected from %d reconstructed particles\n ", sum(a), size(a, 1));