Isolating the chiral magnetic effect from backgrounds by pair invariant mass