Fast calculation of nanodosimetric quantities in treatment planning of proton and ion therapy