Highly sensitive all-optical method for measuring magnetic fields