Question: how to calculate the pair correlation function g(r)?
Answer: Consider each particle you have in turn. ... 
Divide your total count by N, the number of reference particles you considered -- probably the total number of particles in your data.
Divide this number by 4 pi r^2 dr, the volume of the spherical shell (the surface area 4 pi r^2, multiplied by the small thickness dr).