396 Histogram<PotentialStar*> hist(binSize,minSize,maxSize);
402 Assert(nStart<=
int(objList.size()));
403 for(k=0;k<nStart;++k) {
404 hist.add(objList[k]->getSize()-f(objList[k]->getPos()),objList[k]);
406 xdbg<<
"added "<<nStart<<
" objects\n";
411 double peak1 = hist.findFirstPeakAfter(minSize);
412 xdbg<<
"peak1 = "<<peak1<<
" ("<<hist[peak1]<<
")\n";
413 double valley1 = hist.findFirstValleyAfter(peak1);
414 xdbg<<
"valley1 = "<<valley1<<
" ("<<hist[valley1]<<
")\n";
415 while (valley1 < 0.0) {
417 peak1 = hist.findFirstPeakAfter(valley1);
418 xdbg<<
"new peak1 = "<<peak1<<
" ("<<hist[peak1]<<
")\n";
425 double newValley1 = hist.findFirstValleyAfter(peak1);
426 if (!(newValley1 > valley1)) {
427 std::ostringstream err;
428 err<<
"Couldn't find a stellar peak. ";
429 err<<
"All the objects seem to be basically the same size.";
430 throw SizeMagnitudeStarSelectorException(err.str());
432 valley1 = newValley1;
433 xdbg<<
"new valley1 = "<<valley1<<
" ("<<hist[valley1]<<
")\n";
437 double peak2 = hist.findFirstPeakAfter(valley1);
438 xdbg<<
"peak2 = "<<peak2<<
" ("<<hist[peak2]<<
")\n";
441 double nBinsForHorn = isFirstPass ? 2.5 : 4.5;
442 if (peak2-peak1 < nBinsForHorn*binSize && peak2+binSize < maxSize) {
443 valley1 = hist.findFirstValleyAfter(peak2);
444 peak2 = hist.findFirstPeakAfter(peak2+binSize);
445 xdbg<<
"horn pattern: next peak2 = "<<peak2<<
" ("<<hist[peak2]<<
")\n";
447 double valley2 = hist.findFirstValleyAfter(peak2);
448 xdbg<<
"valley2 = "<<valley2<<
" ("<<hist[valley2]<<
")\n";
449 double valley0 = hist.findFirstValleyBefore(peak1);
450 if (!isFirstPass && valley0 > 0.) valley0 = hist.findFirstValleyBefore(0.);
451 xdbg<<
"valley0 = "<<valley0<<
" ("<<hist[valley0]<<
")\n";
455 std::vector<PotentialStar*> peakList=hist.getRefsInRange(valley0,valley1);
458 double highMag = objList[nStart-1]->getMag();
463 double prevValley=valley1;
465 for(
int count = 0; count < minIter || !done; ++count) {
469 xdbg<<
"count = "<<count<<
", highmag = "<<highMag<<std::endl;
472 const int nObj = objList.size();
473 for(;k<nObj && objList[k]->getMag()<highMag;++k) {
474 hist.add(objList[k]->getSize()-f(objList[k]->getPos()),objList[k]);
476 xdbg<<
"hist has "<<k<<
" objects\n";
479 xdbg<<
"valley0 = "<<valley0<<std::endl;
481 double peak = hist.findFirstPeakAfter(valley0,!isFirstPass);
482 valley0 = hist.findFirstValleyBefore(peak);
483 xdbg<<
"done findpeak: "<<peak<<std::endl;
487 double valley = hist.findFirstValleyAfter(peak,!isFirstPass);
488 xdbg<<
"done findvalley: "<<valley<<std::endl;
492 double nextPeak = hist.findFirstPeakAfter(valley,!isFirstPass);
494 while (std::abs(nextPeak) < std::abs(peak)) {
496 xdbg<<
"negative valley - new peak: "<<peak<<std::endl;
497 valley = hist.findFirstValleyAfter(peak,!isFirstPass);
498 xdbg<<
"new valley: "<<valley<<std::endl;
500 nextPeak = hist.findFirstPeakAfter(valley,!isFirstPass);
511 if (isFirstPass && valley > prevValley+binSize) {
513 xdbg<<
"moved valley to "<<valley<<std::endl;
514 if (peak >= valley) {
523 if (peak > prevValley && std::abs(peak) > std::abs(prevPeak)) {
524 xdbg<<
"New peak passed previous valley.\n";
532 int peakCount = hist.getRefinedPeakCount(&peak);
533 xdbg<<
"peakcount = "<<peakCount<<
" centered at "<<peak<<std::endl;
534 xdbg<<
"before refined: valley = "<<valley<<std::endl;
535 int valleyCount = hist.getRefinedValleyCount(&valley);
536 xdbg<<
"valleycount = "<<valleyCount<<
" centered at "<<valley<<std::endl;
542 if ((valley-peak < nBinsForHorn*binSize) &&
543 (hist.findFirstPeakAfter(valley) - valley) < nBinsForHorn*binSize) {
544 double newValley = hist.findFirstValleyAfter(
545 hist.findFirstPeakAfter(valley));
546 int newValleyCount = hist.getRefinedValleyCount(&newValley);
547 if (((isFirstPass && newValleyCount <= valleyCount) ||
548 (!isFirstPass && valleyCount > 0 && newValleyCount == 0)) &&
549 newValley <= prevValley + nBinsForHorn*binSize) {
551 xdbg<<
"horn pattern detected. new valley = "<<
552 newValley<<std::endl;
553 xdbg<<
"new valley count = "<<newValleyCount<<std::endl;
555 valleyCount = newValleyCount;
564 double signif = (double)valleyCount/(
double)peakCount;
565 dbg<<
"signif = "<<valleyCount<<
"/"<<peakCount<<
" = "<<signif<<std::endl;
572 dbg<<
"reset signif to 0, since valleycount "<<valleyCount<<
" <= ";
578 if (signif <= maxSignifRatio) {
579 dbg<<
"signif = "<<signif<<
" <= "<<maxSignifRatio<<std::endl;
580 xdbg<<
"good signif value\n";
582 double peakStart = std::min(2.*peak-valley,peak-binSize*0.5);
583 std::vector<PotentialStar*> temp =
584 hist.getRefsInRange(peakStart,valley);
585 xdbg<<
"get refs from "<<peakStart<<
" to "<<valley<<std::endl;
586 xdbg<<
"got refs: "<<temp.size()<<std::endl;
591 if (temp.size() >= peakList.size() ||
592 (valley<prevValley && valley>prevPeak) ||
593 (peak > prevPeak) ) {
595 if (temp.size() >= peakList.size()) {
596 xdbg<<
"tempsize = "<<temp.size()<<
" >= "<<peakList.size();
597 }
else if ((valley<prevValley && valley>prevPeak)) {
598 xdbg<<
"valley = "<<valley<<
" < "<<prevValley;
600 xdbg<<
"peak = "<<peak<<
" > "<<prevPeak;
602 xdbg<<
" so keep adding...\n";
608 xdbg<<
"tempsize < "<<peakList.size();
609 xdbg<<
" and peak,valley didn't contract, so stop adding now.\n";
613 dbg<<
"signif = "<<signif<<
" > "<<maxSignifRatio<<std::endl;
619 xdbg<<
"no more to add\n";
620 count = minIter; done =
true;
623 xdbg<<
"done finding peaklist\n";
std::ostream *const dbgout