Determination of the $\nu_\mu$ spectrum of the CNGS neutrino beam by studying muon events in the OPERA experiment