The convolution of the excitation energies, computed by the complete active space self-consistent field (CASSCF) or other CAS-based methods, of an ensemble of geometries generated by molecular dynamic simulations is a usual recipe to obtain the absorption spectrum or the density of states of a chromophore. This approach requires that all the considered geometries have the same molecular orbitals within the active space. However, the different geometrical features and/or the different influence of the solvent or biological environments along the sample geometries makes the preservation of the active space a challenging task, which is usually ignored. In this work, we present an algorithm to correct for the active space of geometry ensembles in CASSCF calculations. The algorithm is based on the calculation of the molecular orbital overlap matrix between a previously selected reference geometry, with the desired active space, and each of the sampled geometries. Depending on the value of the overlap matrix elements, the algorithm determines whether one or more pairs of molecular orbitals of the sampled geometry have to be swapped for a subsequent CASSCF calculation. We have applied the developed algorithm to quantum mechanics/molecular mechanics CASSCF/MM and CASPT2/MM calculations for sets of geometries of the five canonical nucleobases in aqueous solution obtained from classical molecular dynamics simulations. The algorithm shows a very good efficacy since it recovered the correct active space for 76\% of the geometries which presented undesired molecular orbitals in the active space after the first CASSCF wavefunction optimization. In addition, the importance of having the same orbitals within the active space for all the geometries is discussed based on the computed density of states for the solvated nucleobases.