33 # include "Minuit2/FCNBase.h"
34 # include "Minuit2/FunctionMinimum.h"
35 # include "Minuit2/MnMigrad.h"
36 # include "Minuit2/MnMinos.h"
37 # include "Minuit2/MnPrint.h"
40 #include "boost/shared_ptr.hpp"
45 #include "lsst/afw/detection/FootprintArray.cc"
52 namespace pexExceptions = lsst::pex::exceptions;
53 namespace pexLogging = lsst::pex::logging;
54 namespace afwDet = lsst::afw::detection;
56 namespace afwTable = lsst::afw::table;
57 namespace afwMath = lsst::afw::math;
58 namespace afwGeom = lsst::afw::geom;
59 namespace measBase = lsst::meas::base;
76 afw::table::SourceRecord & source,
77 afw::image::Exposure<float>
const& exposure,
79 meas::base::CentroidResultKey
const & keys
82 typedef afw::image::Image<float> ImageT;
83 ImageT
const&
image = *exposure.getMaskedImage().getImage();
85 source.set(keys.getX(), center.getX());
86 source.set(keys.getY(), center.getY());
88 int x = center.getX() - image.getX0();
89 int y = center.getY() - image.getY0();
91 if (x < 1 || x >= image.getWidth() - 1 || y < 1 || y >= image.getHeight() - 1) {
97 ImageT::xy_locator im = image.xy_at(x, y);
100 (im(-1, 1) + im( 0, 1) + im( 1, 1) +
101 im(-1, 0) + im( 0, 0) + im( 1, 0) +
102 im(-1, -1) + im( 0, -1) + im( 1, -1));
112 -im(-1, 1) + im( 1, 1) +
113 -im(-1, 0) + im( 1, 0) +
114 -im(-1, -1) + im( 1, -1);
116 (im(-1, 1) + im( 0, 1) + im( 1, 1)) -
117 (im(-1, -1) + im( 0, -1) + im( 1, -1));
121 source.set(keys.getX(), xx);
122 source.set(keys.getY(), yy);
129 Control
const & ctrl,
130 std::string
const & name,
131 afw::table::Schema &
schema
132 ) : DipoleCentroidAlgorithm(ctrl, name, schema,
"unweighted first moment centroid"),
134 _centroidExtractor(schema, name)
147 naiveCentroid(source, exposure, peaks[0]->getI(), (peaks[0]->getPeakValue() >= 0 ?
150 if (peaks.size() > 1) {
151 naiveCentroid(source, exposure, peaks[1]->getI(), (peaks[1]->getPeakValue() >= 0 ?
158 _flagHandler.handleFailure(measRecord, error);
167 ) : afw::detection::FootprintFunctor< afw::image::MaskedImage<float> >(mimage),
178 void operator()( afw::image::MaskedImage<float>::xy_locator loc,
226 source.
set(getPositiveKeys().getFlux(), functor.getSumPositive());
227 source.
set(getPositiveKeys().getFluxSigma(), ::sqrt(functor.getVarPositive()));
228 source.
set(_numPositiveKey, functor.getNumPositive());
230 source.
set(getNegativeKeys().getFlux(), functor.getSumNegative());
231 source.
set(getNegativeKeys().getFluxSigma(), ::sqrt(functor.getVarNegative()));
232 source.
set(_numNegativeKey, functor.getNumNegative());
236 _flagHandler.handleFailure(measRecord, error);
252 _psfDipoleFlux(psfDipoleFlux),
256 double Up()
const {
return _errorDef; }
263 virtual double operator()(std::vector<double>
const & params)
const {
272 if ((negFlux > 0.0) || (posFlux < 0.0)) {
276 std::pair<double,int> fit = _psfDipoleFlux.chi2(_source, _exposure, negCenterX, negCenterY, negFlux, posCenterX, posCenterY, posFlux);
277 double chi2 = fit.first;
278 int nPix = fit.second;
279 if (nPix > _maxPix) {
300 double negCenterX,
double negCenterY,
double negFlux,
301 double posCenterX,
double posCenterY,
double posFlux
319 footprint->getBBox());
321 footprint->getBBox());
337 negModelSubim += negSubim;
348 posModelSubim += posSubim;
353 residuals += posModel;
355 residuals *= residuals;
360 return std::pair<double,int>(chi2, nPix);
367 source.
set(_flagMaxPixelsKey,
true);
377 if (footprint->getArea() > _ctrl.maxPixels) {
381 source.
set(_flagMaxPixelsKey,
false);
386 if (peakList.size() == 0) {
390 else if (peakList.size() == 1) {
402 ROOT::Minuit2::MnUserParameters fitPar;
419 ROOT::Minuit2::MnMigrad migrad(minimizerFunc, fitPar);
424 ROOT::Minuit2::FunctionMinimum
min = migrad(_ctrl.maxFnCalls);
426 float minChi2 = min.Fval();
427 bool const isValid = min.IsValid() &&
std::isfinite(minChi2);
429 if (
true || isValid) {
436 std::pair<double,int> fit = chi2(source, exposure,
440 double evalChi2 = fit.first;
441 int nPix = fit.second;
445 source.
set(getNegativeKeys().getFlux(), min.UserState().Value(
NEGFLUXPAR));
446 source.
set(getNegativeKeys().getFluxSigma(), min.UserState().Error(
NEGFLUXPAR));
450 source.
set(getPositiveKeys().getFlux(), min.UserState().Value(
POSFLUXPAR));
451 source.
set(getPositiveKeys().getFluxSigma(), min.UserState().Error(
POSFLUXPAR));
453 source.
set(_chi2dofKey, evalChi2 / (nPix - minimizerFunc.
getNpar()));
454 source.
set(_negCentroid.getX(), minNegCentroid->getX());
455 source.
set(_negCentroid.getY(), minNegCentroid->getY());
456 source.
set(_posCentroid.getX(), minPosCentroid->getX());
457 source.
set(_posCentroid.getY(), minPosCentroid->getY());
458 source.
set(_avgCentroid.getX(), 0.5*(minNegCentroid->getX() + minPosCentroid->getX()));
459 source.
set(_avgCentroid.getY(), 0.5*(minNegCentroid->getY() + minPosCentroid->getY()));
465 _flagHandler.handleFailure(measRecord, error);
An include file to include the public header files for lsst::afw::math.
double getValue(Property const prop=NOTHING) const
Return the value of the desired property (if specified in the constructor)
MaskedImageT getMaskedImage()
Return the MaskedImage.
ImagePtr getImage(bool const noThrow=false) const
Return a (Ptr to) the MaskedImage's image.
An include file to include the header files for lsst::afw::geom.
double indexToPosition(double ind)
Convert image index to image position.
Eigen matrix objects that present a view into an ndarray::Array.
definition of the Trace messaging facilities
MinimizeDipoleChi2(PsfDipoleFlux const &psfDipoleFlux, afw::table::SourceRecord &source, afw::image::Exposure< float > const &exposure)
void fail(afw::table::SourceRecord &measRecord, meas::base::MeasurementError *error=NULL) const
RecordId getId() const
Convenience accessors for the keys in the minimal reference schema.
find sum of pixels in the image
PsfDipoleFlux const & _psfDipoleFlux
Exception to be thrown when a measurement algorithm experiences a known failure mode.
boost::enable_if< typename ExpressionTraits< Scalar >::IsScalar, Scalar >::type sum(Scalar const &scalar)
PixelT Pixel
A pixel in this ImageBase.
An integer coordinate rectangle.
void measure(afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure) const
table::Key< table::Array< Kernel::Pixel > > image
afw::image::Exposure< float > const & _exposure
void setErrorDef(double def)
VariancePtr getVariance(bool const noThrow=false) const
Return a (Ptr to) the MaskedImage's variance.
An include file to include the header files for lsst::afw::image.
void measure(afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure) const
An include file to include the header files for lsst::afw::detection.
A class to manipulate images, masks, and variance as a single object.
virtual double operator()(std::vector< double > const ¶ms) const
NaiveDipoleCentroid(Control const &ctrl, std::string const &name, afw::table::Schema &schema)
void setMaxPix(int maxPix)
void fail(afw::table::SourceRecord &measRecord, meas::base::MeasurementError *error=NULL) const
boost::shared_ptr< lsst::afw::detection::Psf > getPsf()
Return the Exposure's Psf object.
void fail(afw::table::SourceRecord &measRecord, meas::base::MeasurementError *error=NULL) const
#define LSST_EXCEPT(type,...)
std::pair< double, int > chi2(afw::table::SourceRecord &source, afw::image::Exposure< float > const &exposure, double negCenterX, double negCenterY, double negFlux, double posCenterX, double poCenterY, double posFlux) const
boost::shared_ptr< Footprint > getFootprint() const
Statistics makeStatistics(afwImage::Mask< afwImage::MaskPixel > const &msk, int const flags, StatisticsControl const &sctrl)
Specialization to handle Masks.
void set(Key< T > const &key, U const &value)
Set value of a field for the given key.
Record class that contains measurements made on a single exposure.
afw::table::SourceRecord & _source
A polymorphic base class for representing an image's Point Spread Function.
A class to represent a 2-dimensional array of pixels.
Include files required for standard LSST Exception handling.
void measure(afw::table::SourceRecord &measRecord, afw::image::Exposure< float > const &exposure) const