function [minimalDistancePDF,alignmentPDF,minDistance,alignment,relativeVelocity]=minDistancePDF( T )
% This function returns the minimal distance PDF and the alignment PDF (see
% Dissertation B. Deu?en). Furthermore, it returns the raw data for the
% minimal distance, the alignment and the relative velocity of already
% close particles.
posX=T(:,4);
posY=T(:,5);
angle=T(:,6);
velX=T(:,7);
velY=T(:,8);
angle=wrapToPi(angle);
noOfParticles=T(end,2)+1;
tEnd=T(end,1);
minDistance=ones(length(posX),1)*0;
alignment=ones(length(posX),1)*0;
relativeVelocity=ones(length(posX),1)*0;
bP=0.5666;
epsilon=2.5;
aP=epsilon*bP;
for tt=1:tEnd
    for pp=1:noOfParticles
        p=(tt-1)*noOfParticles+pp;
        for qq=1:noOfParticles
            q=(tt-1)*noOfParticles+qq;
            temp=sqrt((posX(p)-posX(q))^2+(posY(p)-posY(q))^2);
            if (temp < minDistance(p) || minDistance(p)==0) && p ~= q
                minDistance(p)=temp;
                alignment(p)=abs(angle(p)-angle(q));
                relativeVelocity(p)=norm([velX(p)-velX(q);velY(p)-velY(q)]);
                if alignment(p)>pi
                    alignment(p)=alignment(p)-pi;
                end
            end
        end
    end
end
N=1000;
h=histogram(minDistance,N,'Normalization','PDF');
k=h.Values;
l=smoothdata(k,'gaussian',200);
x=h.BinEdges-h.BinWidth/2;
h1=histogram(alignment,N,'Normalization','PDF');
k1=h1.Values;
l1=smoothdata(k1,'gaussian',100);
x1=h1.BinEdges-h1.BinWidth/2;
semilogy(x(1:N),l, 'LineWidth', 1.5)
hold on
semilogy(x1(1:N),l1, 'LineWidth', 1.5)
minimalDistancePDF=[x(1:N)', l'];
alignmentPDF=[x1(1:N)', l1'];
end