Medical apparatus comprising:-a magnetic resonance imaging (MRI) system for acquiring magnetic resonance data from an imaging zone (Vp), wherein the magnetic resonance imaging system comprises:-a particle beam apparatus (1) having a particle beam line for producing, directing and managing a particle beam of charged particles,-magnetic correcting means (31, 31a, 31b, 31c) for applying a magnetic correction to a magnetic field perturbation within the imaging volume (Vp)-controlling means (33, 4) for controlling said magnetic correcting means (31, 31a, 31b, 31c),wherein said controlling means are configured to provide a spatially optimized magnetic correction within a restricted volume of said imaging volume (Vp) during a spatially optimized magnetic correction period (Po), said spatially optimized magnetic correction period (Po) being posterior to said excitation period (Pe), wherein a selected slice (Vpi) from the imaging volume Vp is excited, and including at least one spatial encoding period (Pp, Pf) of the pulse sequence, said restricted volume comprising said selected slice (Vpi).