We have developed a procedure for fitting experimental and simulated X–ray reflectivity and diffraction data in order to automate and to quantify the characterization of thin–film structures. The optimization method employed is a type of genetic algorithm called‘differential evolution’. The method is capable of rapid convergence to the global minimum of an error function in parameter space even when there are many local minima in addition to the global minimum. We show how to estimate the pointwise errors of the optimized parameters, and how to determine whether the model adequately represents the structure. The procedure is capable of fitting some tens of adjustable parameters, given suitable data.