The transverse momentum spectra of D$^0$ mesons from b hadron decays are measured at midrapidity ($|y|$ $<$ $1$) in pp and PbPb collisions at a nucleon-nucleon center of mass energy of 5.02 TeV with the CMS detector at the LHC. The D$^0$ mesons from b hadron decays are distinguished from prompt D$^0$ mesons by their decay topologies. In PbPb collisions, the B $\to$ D$^0$ yield is found to be suppressed in the measured $p_\mathrm{T}$ range from 2 to 100 GeV$/c$ as compared to pp collisions. The suppression is weaker than that of prompt D$^0$ mesons and charged hadrons for $p_\mathrm{T}$ around 10 GeV$/c$. While theoretical calculations incorporating partonic energy loss in the quark-gluon plasma can successfully describe the measured B $\to$ D$^0$ suppression at higher $p_\mathrm{T}$, the data show an indication of larger suppression than the model predictions in the range of 2 $\lt$ $p_\mathrm{T}$ $\lt$ 5 GeV$/c$.
The transverse momentum ($p_\mathrm{t}$) spectrum of prompt D$^0$ mesons and their antiparticles has been measured via the hadronic decay channels D$^0 \to \mathrm{K}^- \pi^+$ and $\overline{\mathrm{D}}^0 \to \mathrm{K}^+ \pi^-$ in pp and PbPb collisions at a centre-of-mass energy of 5.02 TeV per nucleon pair with the CMS detector at the LHC. The measurement is performed in the D$^0$ meson $p_\mathrm{t}$ range of 2-100 GeV and in the rapidity range of $|y| < $1. The pp (PbPb) dataset used for this analysis corresponds to an integrated luminosity of 27.4 pb$^{-1}$ (530 $\mu$b$^{-1}$). The measured D$^0$ meson $p_\mathrm{t}$ spectrum in pp collisions is well described by perturbative QCD calculations. The nuclear modification factor, comparing D$^0$ meson yields in PbPb and pp collisions, was extracted for both minimum-bias and the 10% most central PbPb interactions. For central events, the D$^0$ meson yield in the PbPb collisions is suppressed by a factor of 5-6 compared to the pp reference in the $p_\mathrm{t}$ range of 6-10 GeV. For D$^0$ mesons in the high-$p_\mathrm{t}$ range of 60-100 GeV, a significantly smaller suppression is observed. The results are also compared to theoretical calculations.