LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
Ellipse_meas.cc
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 #include <cmath>
26 #include <fstream>
27 
33 
34 namespace lsst {
35 namespace meas {
36 namespace algorithms {
37 namespace shapelet {
38 
40  const std::vector<PixelList>& pix,
41  const std::vector<BVec>* psf,
42  int galOrder, int galOrder2, int maxm,
43  double sigma, long& flag, double thresh, DMatrix* cov)
44  {
45  dbg<<"Start DoMeasure: galOrder = "<<galOrder<<", psf = "<<bool(psf)<<std::endl;
46  dbg<<"fix = "<<_isFixedCen<<" "<<_isFixedGamma<<" "<<_isFixedMu<<std::endl;
47  dbg<<"Thresh = "<<thresh<<std::endl;
48  int npix = 0;
49  for(size_t i=0;i<pix.size();++i) {
50  xdbg<<"npix["<<i<<"] = "<<pix[i].size()<<std::endl;
51  npix += pix[i].size();
52  }
53 
54  int galSize = (galOrder+1)*(galOrder+2)/2;
55  if (npix <= galSize) {
56  dbg<<"Too few pixels ("<<npix<<") for given gal_order. \n";
57  return false;
58  }
59 
60  BVec b(galOrder,sigma);
61 
62  if (!doMeasureShapelet(pix,psf,b,galOrder,galOrder2,maxm)) {
63  xdbg<<"Could not measure a shapelet vector.\n";
64  return false;
65  }
66  if (b(0) <= 0) {
67  xdbg<<"Bad flux in measured shapelet\n";
68  return false;
69  }
70  xdbg<<"b = "<<b<<std::endl;
71 
72  return findRoundFrame(b,psf,galOrder2,thresh,flag,cov);
73  }
74 
76  const BVec& b, bool psf, int galOrder2, double thresh,
77  long& flag, DMatrix* cov)
78  {
79  DVector x(5);
80  DVector f(5);
81 
82  x.setZero();
83 
85 
86 #ifdef NOTHROW
87  solver.noUseCholesky();
88 #endif
89  double ftol = thresh*thresh;
90  double gtol = thresh*ftol;
91  solver.setTol(ftol,gtol);
92  solver.setMinStep(gtol*thresh);
93  solver.setOutput(*dbgout);
94  if (XDEBUG) solver.useVerboseOutput();
95  solver.setMinStep(1.e-6*gtol);
96  solver.setDelta0(0.01);
97  solver.setMaxIter(200);
98  if (psf && !_isFixedMu) {
99  solver.setDelta0(0.01);
100  solver.useDogleg();
101  solver.dontZeroB11();
102  solver.useSVD();
103  } else {
104  solver.useHybrid();
105  }
106  if (solver.solve(x,f)) {
107  dbg<<"Found good round frame:\n";
108  dbg<<"x = "<<EIGEN_Transpose(x)<<std::endl;
109  dbg<<"f = "<<EIGEN_Transpose(f)<<std::endl;
110  double f_normInf = f.TMV_normInf();
111  if (psf && !_isFixedMu && !(f_normInf < solver.getFTol())) {
112  xdbg<<"Oops, Local minimum, not real solution.\n";
113  xdbg<<"f.norm = "<<f.norm()<<std::endl;
114  xdbg<<"f.normInf = "<<f_normInf<<std::endl;
115  xdbg<<"ftol = "<<solver.getFTol()<<std::endl;
116  dbg<<"FLAG SHEAR_LOCAL_MIN\n";
117  flag |= SHEAR_LOCAL_MIN;
118  return false;
119  }
120  } else {
121  dbg<<"findRoundFrame solver failed:\n";
122  dbg<<"x = "<<EIGEN_Transpose(x)<<std::endl;
123  dbg<<"f = "<<EIGEN_Transpose(f)<<std::endl;
124  //if (XDEBUG) if (!solver.testJ(x,f,dbgout,1.e-5)) exit(1);
125  dbg<<"FLAG SHEAR_DIDNT_CONVERGE\n";
126  flag |= SHEAR_DIDNT_CONVERGE;
127  return false;
128  }
129 
130  double sigma = b.getSigma();
131  preShiftBy(std::complex<double>(x(0),x(1))*sigma,
132  std::complex<double>(x(2),x(3)),
133  x(4));
134 
135  dbg<<"ell => "<<*this<<std::endl;
136 
137  if (cov) {
138  solver.useSVD();
139  solver.getCovariance(*cov);
140  xdbg<<"cov = "<<*cov<<std::endl;
141  }
142 
143  return true;
144  }
145 
146 
147 }}}}
const bool XDEBUG
Definition: dbg.h:63
bool doMeasure(const std::vector< PixelList > &pix, const std::vector< BVec > *psf, int order, int order2, int maxm, double sigma, long &flag, double thresh, DMatrix *cov=0)
Definition: Ellipse_meas.cc:39
bool findRoundFrame(const BVec &b, bool psf, int galOrder2, double thresh, long &flag, DMatrix *cov=0)
Definition: Ellipse_meas.cc:75
virtual void setOutput(std::ostream &os)
Definition: NLSolver.h:410
bool solve(DVector &x, DVector &f) const
afw::table::Key< double > sigma
Definition: GaussianPsf.cc:43
void preShiftBy(const std::complex< double > &cen, const std::complex< double > &gamma, const std::complex< double > &mu)
Definition: Ellipse.cc:124
bool doMeasureShapelet(const std::vector< PixelList > &pix, const std::vector< BVec > *psf, BVec &bret, int order, int order2, int maxm, DMatrix *bcov=0) const
Definition: Ellipse.cc:186
virtual void setTol(double fTol, double gTol)
Definition: NLSolver.h:395
double x
virtual void setDelta0(double delta0)
Definition: NLSolver.h:401
virtual void setMinStep(double minStep)
Definition: NLSolver.h:398
afw::table::Key< double > b
virtual void setMaxIter(int maxIter)
Definition: NLSolver.h:399
#define xdbg
Definition: dbg.h:70
#define dbg
Definition: dbg.h:69
std::ostream *const dbgout
Definition: dbg.h:64
#define EIGEN_Transpose(m)
Definition: MyMatrix.h:211