The trace method for cotangent sums