Cryo-electron microscopy (cryo-EM) enables macromolecular structure determination in vitro and in situ. In addition to aligning individual particles, accurate registration of sample motion and 3D deformation during exposures is crucial for achieving high resolution. Here we describe M, a software tool that establishes a reference-based, multi-particle refinement framework for cryo-EM data and improves the results of structure determination. M provides a unified optimization framework for both in vitro frame series and in situ tomographic tilt series data. We show that tilt series data can provide the same resolution as frame series, indicating that the alignment step no longer limits the resolution obtainable from tomographic data. In combination with Warp and RELION, M improves upon previous methods, and resolves a 70S ribosome bound to an antibiotic inside bacterial cells at a nominal resolution of 3.7 Å. Thus, computational tools are now available to resolve structures from tomographic in situ cryo-EM data at residue level.