A new algorithm for spectral and spatial reconstruction of proton beams from dosimetric measurements