A method of tuning a mass spectrometer using a cost function for optimizing a set of parameters for the mass spectrometer, each parameter corresponding to a different tunable element of the mass spectrometer, the cost function including an intensity term and a rectangularity term, the rectangularity term being a quantification of an extent that a first measured signal corresponding to a first mass-to-charge ratio approximates a rectangle, the method comprising: for each of a plurality of iterations of an optimization process: sending, by a computer system, commands to the mass spectrometer to obtain a calibration measured signal of a calibration sample, wherein the calibration measured signal includes the first measured signal, and wherein the commands specify current values for the set of parameters; receiving the calibration measured signal at the computer system; analyzing, by the computer system, the calibration measured signal to determine a current cost value for the cost function, the current cost value including contributions from the intensity term and the rectangularity term; and selecting, by the computer system, one or more new values for the set of parameters based on the current cost value to optimize the cost function, the one or more new values for including in new commands to be sent to the mass spectrometer for a next iteration; and providing, by the computer system, final values for the set of parameters for use in operating the mass spectrometer to obtain a mass spectrum of a new sample. for each of a plurality of iterations of an optimization process: sending, by a computer system, commands to the mass spectrometer to obtain a calibration measured signal of a calibration sample, wherein the calibration measured signal includes the first measured signal, and wherein the commands specify current values for the set of parameters; receiving the calibration measured signal at the computer system; analyzing, by the computer system, the calibration measured signal to determine a current cost value for the cost function, the current cost value including contributions from the intensity term and the rectangularity term; and selecting, by the computer system, one or more new values for the set of parameters based on the current cost value to optimize the cost function, the one or more new values for including in new commands to be sent to the mass spectrometer for a next iteration; and sending, by a computer system, commands to the mass spectrometer to obtain a calibration measured signal of a calibration sample, wherein the calibration measured signal includes the first measured signal, and wherein the commands specify current values for the set of parameters; receiving the calibration measured signal at the computer system; analyzing, by the computer system, the calibration measured signal to determine a current cost value for the cost function, the current cost value including contributions from the intensity term and the rectangularity term; and selecting, by the computer system, one or more new values for the set of parameters based on the current cost value to optimize the cost function, the one or more new values for including in new commands to be sent to the mass spectrometer for a next iteration; and providing, by the computer system, final values for the set of parameters for use in operating the mass spectrometer to obtain a mass spectrum of a new sample.