I am trying to learn aperture photometry using python

I noticed the following logic in the image below, where an upper limit was determined by simulating an undetected star and the standard deviation was calculated. Why is this logic needed during photometry? What is the purpose of finding an upper limit and calculating the standard deviation for it?

[![enter image description here](https://i.stack.imgur.com/OP0nx.png)](https://i.stack.imgur.com/OP0nx.png)