6 #include "Eigen/Sparse" 18 LOG_LOGGER _log =
LOG_GET(
"jointcal.PhotometryFit");
24 void PhotometryFit::leastSquareDerivativesMeasurement(CcdImage
const &
ccdImage, TripletList &tripletList,
25 Eigen::VectorXd &grad,
26 MeasuredStarList
const *measuredStarList)
const {
35 if (measuredStarList) assert(&(measuredStarList->front()->getCcdImage()) == &ccdImage);
37 unsigned nparModel = (_fittingModel) ? _photometryModel->getNpar(ccdImage) : 0;
38 unsigned nparFlux = (_fittingFluxes) ? 1 : 0;
39 unsigned nparTotal = nparModel + nparFlux;
41 if (_fittingModel) _photometryModel->getMappingIndices(ccdImage, indices);
43 Eigen::VectorXd H(nparTotal);
45 unsigned kTriplets = tripletList.getNextFreeIndex();
46 const MeasuredStarList &catalog = (measuredStarList) ? *measuredStarList : ccdImage.getCatalogForFit();
48 for (
auto const &measuredStar : catalog) {
49 if (!measuredStar->isValid())
continue;
52 TweakPhotomMeasurementErrors(inPos, *measuredStar, _fluxError);
56 double residual = _photometryModel->transform(ccdImage, *measuredStar, measuredStar->getInstFlux()) -
57 measuredStar->getFittedStar()->getFlux();
60 1.0 / _photometryModel->transform(ccdImage, *measuredStar, measuredStar->getInstFluxErr());
61 double W =
std::pow(inverseSigma, 2);
64 _photometryModel->computeParameterDerivatives(*measuredStar, ccdImage, H);
65 for (
unsigned k = 0; k < indices.size(); k++) {
66 unsigned l = indices[k];
67 tripletList.addTriplet(l, kTriplets, H[k] * inverseSigma);
68 grad[l] += H[k] * W * residual;
72 unsigned index = measuredStar->getFittedStar()->getIndexInMatrix();
74 tripletList.addTriplet(index, kTriplets, -1.0 * inverseSigma);
75 grad[index] += -1.0 * W * residual;
80 tripletList.setNextFreeIndex(kTriplets);
83 void PhotometryFit::leastSquareDerivativesReference(FittedStarList
const &fittedStarList,
84 TripletList &tripletList, Eigen::VectorXd &grad)
const {
91 if (!_fittingFluxes)
return;
95 unsigned kTriplets = tripletList.getNextFreeIndex();
97 for (
auto const &fittedStar : fittedStarList) {
98 auto refStar = fittedStar->getRefStar();
99 if (refStar ==
nullptr)
continue;
106 double inverseSigma = 1.0 / refStar->getFluxErr();
108 double residual = fittedStar->getFlux() - refStar->getFlux();
110 unsigned index = fittedStar->getIndexInMatrix();
112 tripletList.addTriplet(index, kTriplets, 1.0 * inverseSigma);
113 grad(index) += 1.0 *
std::pow(inverseSigma, 2) * residual;
116 tripletList.setNextFreeIndex(kTriplets);
119 void PhotometryFit::accumulateStatImageList(
CcdImageList const &ccdImageList, Chi2Accumulator &accum)
const {
124 for (
auto const &ccdImage : ccdImageList) {
125 auto &catalog = ccdImage->getCatalogForFit();
127 for (
auto const &measuredStar : catalog) {
128 if (!measuredStar->isValid())
continue;
130 _photometryModel->transform(*ccdImage, *measuredStar, measuredStar->getInstFluxErr());
132 TweakPhotomMeasurementErrors(inPos, measuredStar, _fluxError);
135 _photometryModel->transform(*ccdImage, *measuredStar, measuredStar->getInstFlux()) -
136 measuredStar->getFittedStar()->getFlux();
138 double chi2Val =
std::pow(residual / sigma, 2);
139 accum.addEntry(chi2Val, 1, measuredStar);
144 void PhotometryFit::accumulateStatRefStars(Chi2Accumulator &accum)
const {
150 FittedStarList &fittedStarList =
_associations->fittedStarList;
151 for (
auto const &fittedStar : fittedStarList) {
152 auto refStar = fittedStar->getRefStar();
153 if (refStar ==
nullptr)
continue;
154 double chi2 =
std::pow(((fittedStar->getFlux() - refStar->getFlux()) / refStar->getFluxErr()), 2);
155 accum.addEntry(chi2, 1, fittedStar);
162 void PhotometryFit::getIndicesOfMeasuredStar(MeasuredStar
const &measuredStar,
166 _photometryModel->getMappingIndices(measuredStar.getCcdImage(), indices);
168 if (_fittingFluxes) {
169 auto fs = measuredStar.getFittedStar();
170 unsigned fsIndex = fs->getIndexInMatrix();
177 LOGLS_INFO(_log,
"assignIndices: now fitting: " << whatToFit);
179 _fittingFluxes = (
_whatToFit.
find(
"Fluxes") != std::string::npos);
182 _nParModel = (_fittingModel) ? _photometryModel->assignIndices(whatToFit, 0) : 0;
183 unsigned ipar = _nParModel;
185 if (_fittingFluxes) {
191 fittedStar->setIndexInMatrix(ipar);
195 _nParFluxes = ipar - _nParModel;
198 "nParameters total: " <<
_nParTot <<
" model: " << _nParModel <<
" fluxes: " << _nParFluxes);
204 "PhotometryFit::offsetParams : the provided vector length is not compatible with " 205 "the current whatToFit setting");
206 if (_fittingModel) _photometryModel->offsetParams(delta);
208 if (_fittingFluxes) {
213 unsigned index = fittedStar->getIndexInMatrix();
214 fittedStar->getFlux() -= delta(index);
226 ofile <<
"#xccd" << separator <<
"yccd" << separator <<
"mag" << separator <<
"instFlux" << separator
227 <<
"instFluxError" << separator <<
"flux" << separator <<
"fluxError" << separator
228 <<
"transformedFlux" << separator <<
"transformedFluxErr" << separator <<
"fflux" << separator
229 <<
"mjd" << separator <<
"color" << separator <<
"fsindex" << separator <<
"ra" << separator
230 <<
"dec" << separator <<
"chi2" << separator <<
"nm" << separator <<
"chip" << separator <<
"visit" 232 ofile <<
"#coordinates in CCD" << separator << separator <<
"rough mag" << separator
233 <<
"measured instrument flux" << separator <<
"measured instrument flux error" << separator
234 <<
"measured flux" << separator <<
"measured flux error" << separator << separator << separator
235 <<
"fitted flux" << separator <<
"modified Julian date of the measurement" << separator
236 <<
"currently unused" << separator <<
"unique index of the fittedStar" << separator
237 <<
"pos of fitted star" << separator << separator <<
"contribution to Chi2 (1 dof)" << separator
238 <<
"number of measurements of this FittedStar" << separator <<
"chip id" << separator <<
"visit id" 241 for (
auto const &ccdImage : ccdImageList) {
243 for (
auto const &measuredStar : cat) {
244 if (!measuredStar->isValid())
continue;
246 _photometryModel->transform(*ccdImage, *measuredStar, measuredStar->getInstFluxErr());
248 tweakPhotomMeasurementErrors(inPos, measuredStar, _fluxError);
250 double flux = _photometryModel->transform(*ccdImage, *measuredStar, measuredStar->getInstFlux());
252 _photometryModel->transform(*ccdImage, *measuredStar, measuredStar->getInstFluxErr());
253 double jd = ccdImage->getMjd();
254 auto fittedStar = measuredStar->getFittedStar();
255 double residual = flux - fittedStar->getFlux();
256 double chi2Val =
std::pow(residual / sigma, 2);
258 ofile << measuredStar->x << separator << measuredStar->y << separator << fittedStar->getMag()
259 << separator << measuredStar->getInstFlux() << separator << measuredStar->getInstFluxErr()
260 << separator << measuredStar->getFlux() << separator << measuredStar->getFluxErr()
261 << separator << flux << separator << fluxErr << separator << fittedStar->getFlux()
262 << separator << jd << separator << fittedStar->color << separator
263 << fittedStar->getIndexInMatrix() << separator << fittedStar->x << separator
264 << fittedStar->y << separator << chi2Val << separator << fittedStar->getMeasurementCount()
265 << separator << ccdImage->getCcdId() << separator << ccdImage->getVisit() <<
std::endl;
273 ofile <<
"#ra" << separator <<
"dec " << separator <<
"mag" << separator <<
"color" << separator
274 <<
"refFlux" << separator <<
"refFluxErr" << separator <<
"fittedFlux" << separator
275 <<
"fittedFluxErr" << separator <<
"fsindex" << separator <<
"chi2" << separator <<
"nm" 277 ofile <<
"#coordinates of fittedStar" << separator << separator <<
"magnitude" << separator
278 <<
"currently unused" << separator <<
"default refStar flux" << separator
279 <<
"default refStar fluxErr" << separator <<
"fittedStar flux" << separator <<
"fittedStar fluxErr" 280 << separator <<
"unique index of the fittedStar" << separator
281 <<
"refStar contribution to Chi2 (2D dofs)" << separator
282 <<
"number of measurements of this FittedStar" <<
std::endl;
285 for (
auto const &fittedStar : fittedStarList) {
286 const RefStar *refStar = fittedStar->getRefStar();
287 if (refStar ==
nullptr)
continue;
292 ofile << fittedStar->x << separator << fittedStar->y << separator << fittedStar->getMag() << separator
293 << fittedStar->color << separator << refStar->
getFlux() << separator << refStar->
getFluxErr()
294 << separator << fittedStar->getFlux() << separator << fittedStar->getFluxErr() << separator
295 << fittedStar->getIndexInMatrix() << separator << chi2 << separator
296 << fittedStar->getMeasurementCount() <<
std::endl;
Objects used as position anchors, typically USNO stars.
double getFluxErr(size_t filter) const
reference fluxErr in a given filter
void saveChi2RefContributions(std::string const &baseName) const override
Save a CSV file containing residuals of reference terms.
A list of MeasuredStar. They are usually filled in Associations::AddImage.
afw::table::Key< double > sigma
void offsetParams(Eigen::VectorXd const &delta) override
Offset the parameters by the requested quantities.
#define LOGLS_DEBUG(logger, message)
Class for a simple mapping implementing a generic Gtransfo.
A list of FittedStar s. Such a list is typically constructed by Associations.
std::shared_ptr< Associations > _associations
#define LOGLS_INFO(logger, message)
void assignIndices(std::string const &whatToFit) override
Set parameters to fit and assign indices in the big matrix.
#define LSST_EXCEPT(type,...)
double getFlux(size_t filter) const
reference flux in a given filter
std::list< std::shared_ptr< CcdImage > > CcdImageList
T setprecision(T... args)
void saveChi2MeasContributions(std::string const &baseName) const override
Save a CSV file containing residuals of measurement terms.