do sum calculation in same loop as maskData calculation