Calculate histogram