Studies on B hadron production, spectroscopy and decays at CMS