# Photo's Deblender

## Overview of Deblender

The photo deblender is based on the peaks found in each object. The basic idea is that we take the list of all peaks found within the parent, in any band (a peak need only appear in one band to be considered); each of these peaks is taken to define the centre of a possible child.

For each child, in each band we create a template, an approximate light distribution for the child (for a star, this template would be simply the PSF). We then assume that in each band

```    parent = sum_i (weight_i template_i)
```
and solve for the weights. With these in hand we can then assign the flux associated with each child in each pixel according to the value of `weight_i template_i`, and our task is complete.

Note that, because the children are associated with the master list of peaks detected in any band, the resulting children are defined consistently in each band so it makes sense to (e.g.) ask questions about their colours.

## Photo's Deblending Algorithm

Photo's deblender works by looking at the list of `PEAK`s in an `OBJC`, and treats each as the centre of a child. This list was produced by Measure Objects. The deblender proceeds as follows:
• When an OBJC is passed to the deblender, the first thing that's checked is if it has too many peaks (set as `child_max` in the fpParams.par); if it does the `OBJECT1_NODEBLEND` bit is set, and the deblender gives up.
• Next, each child (i.e. peak) is examined in each band. If, in that band, the peak is consistent with being a PSF the `OBJECT1_DEBLENDED_AS_PSF` bit in `flags` is set, the star is subtracted, and bits are set in an `OBJMASK` that indicate which pixels were modified.
• Then photo goes through each child creating a template, that is:
• If the object is too large for the deblender to handle (basically, more rows or columns than half a frame) the `OBJECT1_TOO_LARGE` bit is set and photo gives up on that child.
• If the object reaches the edge of the frame in any band, the `OBJECT1_EDGE` bit is set, and photo gives up on that child.
• If the `OBJECT1_DEBLENDED_AS_PSF` bit is set, the template is taken to be a PSF, centred at the centre of the child. Otherwise, photo goes through the object looking at pairs of pixels `(rowc + y, colc + x)` and `(rowc - y, colc - x)`, where `(rowc, colc)` is the centre of the child. The template is made up of the smaller of the two pixel values, corrected for bias by adding `0.56(sky sigma)`.

For pixels whose `mirror' lies outside the parent object's OBJMASK, the value of the template is taken to be zero. If one of the pixels lies in an area where a PSF has been subtracted (see above), the other pixel's value is used. If both pixels were PSF contaminated, use the average of the two. In all of these cases, no bias correction is applied.

• Next, we run an object finder on the smoothed template; the smoothing length and threshold are those used in Find Faint Objects. If the template is not detected at this stage it is presumably because the peak that corresponds to the child being processed was too faint to have been detected as an isolated object; the `OBJECT1_NOTDETECTED` bit is set and photo gives up on that `OBJECT1`.
• Once we've found a template in each band, we check that it was detected in at least one of them; if not photo gives up on that child.
• Then minimal templates are created in all bands where we failed to find one; these are simply multiples of the PSF; the `OBJECT1_DEBLENDED_AS_PSF` bit is set to indicate this.
• Finally, all parts of templates that do not lie within the OBJECT1s OBJMASK are set to zero.
• With templates available for all children (in all colours), we solve for their weights. This is done using (unweighted) least-squares. The Normal Equations are solved by eigen value decomposition, which allows us to discover if the problem is ill-posed (i.e. the matrix is singular).

We achieve this by going through all the children in each band looking for the smallest eigenvalue; if it is too small, that child is rejected, and the parent has the `OBJECT1_DEBLEND_PRUNED` bit set (both for the OBJC and also for all the OBJECT1s). The least squares procedure is then repeated, until a solution is reached. If this solution consists of only a single child, the parent's `OBJECT1_BLENDED` bit is turned off, and the deblender stops.

• With the weights for each template in hand, and after setting any negative weights to zero, for each pixel in the parent we evaluate `sum == sum_i (weight_i * template_i)`. If this is at least 10, that pixel in the i-th deblended child has value `weight_i * template_i`; otherwise we re-evaluate `sum` using the smoothed templates. If the resulting `sum` is greater than 2, the pixels are given values of `weight_i * smoothed_template_i` otherwise we share the flux equally among all the children. XXX
At the end of this procedure, each child knows the values of all its pixels that lie within the OBJMASK defining the parent, and it is ready to be passed to Measure Objects.

## Book Keeping for the Deblender

Once an object is deblended, it is represented as an `OBJC` as usual, but with a non-`NULL` `children` field which points at one of its children; in addition its `OBJECT1_BLENDED` bit is set, and `nchild` is one or more.

Each child has a non-`NULL` `parent` field which points to its parent, and may have a `sibbs` field too. It has its `OBJECT1_CHILD` bit set.