34 namespace algorithms {
46 _refs = std::vector<std::vector<T> >(nBins);
47 _values = std::vector<std::vector<double> >(nBins);
50 dbg<<
"made histogram:\n";
55 dbg<<
"mini=0 value(mini)="<<
value(0)<<std::endl;
63 _refs[index(value)].push_back(ref);
64 _values[index(value)].push_back(value);
70 Assert(i<
int(_refs.size()));
71 Assert(_values[i].size() == _refs[i].size());
72 return _refs[i].size();
78 if (val1<_minValue) val1 = _minValue;
79 if (val2>_maxValue) val2 = _maxValue;
80 int i1=index(val1), i2=index(val2);
81 Assert(i1<
int(_refs.size()));
82 Assert(i2<
int(_refs.size()));
84 int maxCount = getCount(i1);
86 for(
int i=i1+1;i<=i2;++i) {
87 if (getCount(i) > maxCount) {
88 maxCount = getCount(i);
101 if (val1<_minValue) val1 = _minValue;
102 if (val2>_maxValue) val2 = _maxValue;
103 int i1=index(val1), i2=index(val2);
104 Assert(i1<
int(_refs.size()));
105 Assert(i2<
int(_refs.size()));
106 int iPeak1=i1,iPeak2=i2;
107 while(iPeak1+1<=i2 && getCount(iPeak1+1)>=getCount(iPeak1)) ++iPeak1;
108 while(iPeak2-1>=i1 && getCount(iPeak2-1)>=getCount(iPeak2)) --iPeak2;
109 return iPeak1 >= iPeak2;
112 template <
typename T>
115 if (val1<_minValue) val1 = _minValue;
116 if (val2>_maxValue) val2 = _maxValue;
117 int i1=index(val1), i2=index(val2);
118 Assert(i1<
int(_refs.size()));
119 Assert(i2<
int(_refs.size()));
121 int minCount = getCount(i1);
123 for(
int i=i1+1;i<=i2;++i) {
124 if (getCount(i) < minCount) {
125 minCount = getCount(i);
131 int nRefs = _refs.size();
132 while(iValley+1 < nRefs && getCount(iValley+1) < getCount(iValley))
136 return value(iValley);
139 template <
typename T>
142 dbg<<
"Start FindFirstValleyAfter "<<val1<<std::endl;
143 if (val1<_minValue) val1 = _minValue;
145 Assert(i1<
int(_refs.size()));
151 double minCount = getCount(i1);
153 const int nRefs = _refs.size();
154 while(iCheck < nRefs && getCount(iCheck) <= minCount +
155 (hasPoissonNoise ?
int(sqrt(
float(getCount(iCheck)))) : 0)) {
156 if (getCount(iCheck) < minCount) {
157 minCount = getCount(iCheck);
160 if (++iCheck == nRefs)
break;
162 dbg<<
"valley = "<<value(iValley)<<std::endl;
163 return value(iValley);
166 template <
typename T>
169 if (val1>_maxValue) val1 = _maxValue;
171 Assert(i1<
int(_refs.size()));
177 double minCount = getCount(i1);
180 while(getCount(iCheck) <= minCount +
181 (hasPoissonNoise ?
int(sqrt(
float(getCount(iCheck)))) : 0)) {
182 if (getCount(iCheck) < minCount) {
183 minCount = getCount(iCheck);
186 if (iCheck == 0)
break;
190 return value(iValley);
193 template <
typename T>
196 int i = (start<_minValue) ? index(_minValue) : index(start)+1;
197 const int nValues = _values.size();
198 while (i<nValues && _values[i].size() == 0) ++i;
199 if (i == nValues)
return _maxValue;
200 else return *std::min_element(_values[i].begin(),_values[i].end());
203 template <
typename T>
206 if (val1<_minValue) val1 = _minValue;
208 const int nRefs = _refs.size();
212 double maxCount = getCount(i1);
214 while(iCheck < nRefs && maxCount <= getCount(iCheck) +
215 (hasPoissonNoise ?
int(sqrt(
float(getCount(iCheck)))) : 0)) {
216 if (getCount(iCheck) > maxCount) {
217 maxCount = getCount(iCheck);
220 if (++iCheck == nRefs)
break;
225 template <
typename T>
228 if (val1>_maxValue) val1 = _maxValue;
230 Assert(i1 <
int(_refs.size()));
233 double maxCount = getCount(i1);
236 while(maxCount <= getCount(iCheck) +
237 (hasPoissonNoise ?
int(sqrt(
float(getCount(iCheck)))) : 0)) {
238 if (getCount(iCheck) > maxCount) {
239 maxCount = getCount(iCheck);
242 if (iCheck == 0)
break;
249 template <
typename T>
253 int i1 = index(val1);
254 Assert(i1 <
int(_refs.size()));
255 for(
int i=0;i<i1;++i) count += getCount(i);
259 template <
typename T>
263 int i1 = index(val1);
264 const int nRefs = _refs.size();
266 for(
int i=i1+1;i<nRefs;++i) count += getCount(i);
270 template <
typename T>
277 std::vector<double> vals =
278 getValuesInRange(*peak-1.5*_binSize,
281 std::sort(vals.begin(),vals.end());
283 double bestVal=*peak;
286 const int nVals = vals.size();
287 for(
int i=0,j=0;j<nVals;++i) {
289 while (j<nVals && vals[j] < vals[i]+_binSize) ++j;
292 if (j-i > bestCount) {
294 bestVal = (vals[i]+vals[j-1])/2.;
301 template <
typename T>
310 std::vector<double> vals =
311 getValuesInRange(*valley-1.5*_binSize,
312 *valley+1.5*_binSize);
313 const int nVals = vals.size();
314 if (nVals == 0)
return 0;
315 std::sort(vals.begin(),vals.end());
318 double bestVal=*valley;
321 for(
int i=0,j=0; i<nVals && vals[i]<*valley+0.5*_binSize; ++i) {
323 while (j<nVals && vals[j] < vals[i]+_binSize) ++j;
325 double nextBinStart = j<nVals ? vals[j] : *valley+1.5*_binSize;
326 while (i+1<nVals && vals[i+1]+_binSize < nextBinStart) ++i;
329 if (j-i-1 < bestCount) {
331 bestVal = vals[i]+_binSize/2.;
338 template <
typename T>
341 if (index(value) ==
int(_refs.size()))
return 0;
342 Assert(index(value) <
int(_refs.size()));
343 return getCount(index(value));
346 template <
typename T>
384 dbg<<
"starting FindThresh\n";
387 double nTot=0.,meanTot=0.,meanSqTot=0.;
389 if (minVal < _minValue) minVal = _minValue;
390 if (maxVal > _maxValue) maxVal = _maxValue;
391 int i1 = index(minVal);
392 int i2 = index(maxVal);
393 Assert(i1 <
int(_refs.size()));
394 Assert(i2 <
int(_refs.size()));
397 for(
int i=i1;i<=i2;++i) {
398 double dcount = getCount(i);
401 meanSqTot += dcount*i*i;
405 dbg<<
"ntot="<<nTot<<
", meantot="<<meanTot<<
406 ", meansqtot="<<meanSqTot<<std::endl;
408 double sumN=0.,sumNi=0.,sumNii=0.,varBest=-1.;
411 const double minVariance = 0.25;
413 for(
int i=i1;i<=i2;++i) {
414 double dcount = getCount(i);
417 sumNii += dcount*i*i;
418 double fL = sumN/nTot;
420 Assert(fL >=0. && fR >=0.);
421 if (fL == 0. || fR == 0.)
continue;
422 double xL = sumNi/nTot;
423 double xR = (meanTot-xL)/fR;
425 double vL = sumNii/nTot;
426 double vR = (meanSqTot-vL)/fR - xR*xR;
428 if (vL<minVariance) vL = minVariance;
429 if (vR<minVariance) vR = minVariance;
430 double var = fL*fR*std::pow(xR-xL,2)/sqrt(vL*vR);
432 if (var > varBest+EPSILON) {
438 dbg<<
"besti = "<<iBest<<
", bestvar = "<<varBest<<std::endl;
443 dbg<<
"returning thresh = "<<value(iBest)<<std::endl;
447 template <
typename T>
450 if (min < _minValue) min = _minValue;
451 if (max > _maxValue) max = _maxValue;
455 dbg<<
"in getrefs: min,max = "<<min<<
','<<max<<std::endl;
458 const int nRefs1 = _refs[i1].size();
459 for(
int k=0; k<nRefs1; ++k) {
460 if(_values[i1][k]>=min) {
461 dbg<<
"i1 - add ref @ "<<_values[i1][k]<<std::endl;
462 temp.push_back(_refs[i1][k]);
465 for(
int i=i1+1;i<i2;++i) {
466 if (_refs[i].size() > 0)
467 dbg<<
"i - add all ("<<_refs[i].size()<<
") refs near "<<
468 _values[i].front()<<std::endl;
469 temp.insert(temp.end(),_refs[i].begin(),_refs[i].end());
471 const int nRefs2 = _refs[i2].size();
472 for(
int k=0; k<nRefs2; ++k) {
473 if(_values[i2][k] <= max) {
474 dbg<<
"i2 - add ref @ "<<_values[i2][k]<<std::endl;
475 temp.push_back(_refs[i2][k]);
481 template <
typename T>
483 double min,
double max)
const
485 if (min < _minValue) min = _minValue;
486 if (max > _maxValue) max = _maxValue;
490 std::vector<double> temp;
491 const int nVals1 = _values[i1].size();
492 for(
int k=0;k<nVals1;++k)
493 if(_values[i1][k]>=min) temp.push_back(_values[i1][k]);
494 for(
int i=i1+1;i<i2;++i)
495 temp.insert(temp.end(),_values[i].begin(),_values[i].end());
496 const int nVals2 = _values[i2].size();
497 for(
int k=0;k<nVals2;++k)
498 if(_values[i2][k]<=max) temp.push_back(_values[i2][k]);
502 template <
typename T>
505 Assert (value >= _minValue && value <= _maxValue);
506 return int(
floor((value - _minValue)/_binSize));
509 template <
typename T>
512 Assert(i<
int(_refs.size()));
513 return _binSize*(i+0.5)+_minValue;
516 template <
typename T>
519 if (val1 < _minValue) val1 = _minValue;
520 if (val2 > _maxValue) val2 = _maxValue;
523 Assert(i1 <
int(_refs.size()));
524 Assert(i2 <
int(_refs.size()));
527 for(
int i=i1;i<=i2;++i) {
529 fout << sci2(value(i));
531 for(
int j=0;j<getCount(i);++j) fout<<
'*';
int getRefinedPeakCount(double *peak) const
bool hasSinglePeak(double minVal, double maxVal) const
double findFirstPeakBefore(double val1, bool hasPoissonNoise=false) const
int operator[](double value) const
int getTotalCountBefore(double val1) const
Extent< int, N > ceil(Extent< double, N > const &input)
double findFirstValleyAfter(double val1, bool hasPoissonNoise=false) const
std::vector< std::vector< double > > _values
double findFirstValleyBefore(double val1, bool hasPoissonNoise=false) const
Histogram(double binSize, double minValue, double maxValue)
double findPeak(double minVal, double maxVal) const
double findFirstPeakAfter(double val1, bool hasPoissonNoise=false) const
double value(int i) const
std::vector< T > getRefsInRange(double min, double max) const
double findValley(double minVal, double maxVal) const
void print(std::ostream &fout, double val1=-1.e10, double val2=1.e10) const
double findFirstValueAfter(double start) const
int getTotalCountAfter(double val1) const
Extent< int, N > floor(Extent< double, N > const &input)
int getRefinedValleyCount(double *valley) const
int index(double value) const
int getCount(int i) const
std::vector< double > getValuesInRange(double min, double max) const
void add(double value, const T &ref)
double findThresh(double minVal, double maxVal) const
std::vector< std::vector< T > > _refs
std::ostream *const dbgout