Atom counting in ultracold gases using photoionization and ion detection