  From the TNG50 cosmological simulation we build a sample of 191 well-resolved
barred galaxies with a stellar mass $\log M_\star > 10$ at $z=0$. We search for
box/peanut bulges (BPs) in this sample, finding them in 55 per cent of cases.
We compute $f_\mathrm{BP}$, the BP probability for barred galaxies as a
function of $M_\star$, and find that this rises to a plateau, as found in
observations of nearby galaxies. The transition mass where $f_\mathrm{BP}$
reaches half the plateau value is $\log M_\star = 10.14$, consistent with the
observational value within measurement errors. We show that this transition in
$f_\mathrm{BP}$ can be attributed to the youth of the bars at low $M_\star$,
which is a consequence of downsizing of galaxies. Young bars, being generally
shorter and weaker, have not yet had time to form BPs. At high mass, while we
find a plateau, the value is at $\sim 60$ per cent, whereas observations
saturate at 100 per cent. We attribute this difference to excessive heating in
TNG50, due to merger activity and to numerical resolution effects. BPs in TNG50
tend to occur in galaxies with more quiescent merger histories. As a result,
the main driver of whether a bar hosts a BP in TNG50 is not the galaxy mass,
but how long and strong the bar is. Separating the BP sample into those that
have visibly buckled and those that have not, we find that fully half of BP
galaxies show clear signs of buckling, despite the excessive heating and
limited vertical resolution of TNG50.
