Determination of formation and ionization energies of charged defects in two-dimensional materials.