The present invention provides a medical apparatus (100) comprising a magnetic resonance imaging system (110) for acquiring magnetic resonance data from an imaging volume (122) covering at least partially a subject of interest (120), wherein the magnetic resonance imaging system (110) comprises a main magnet (112) for generating a magnetic field within the imaging volume (122), a particle beam apparatus (150) having a particle beam line (152) for a particle beam (154) of charged particles, including a gantry (156) configured for rotating around a rotational axis (R), which is arranged in the longitudinal direction of the main magnet (112), wherein the gantry (156) comprises at least one bending magnet (158) for directing the particle beam (154) to an irradiation volume (124) within the imaging volume (122), an active compensation coil (200), which is arranged to substantially surround at least the imaging volume (122), and a control unit (132) for controlling the active compensation coil (200) for canceling a