00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "TrkReco/TLineFitter.h"
00014 #include "TrkReco/TTrackBase.h"
00015 #include "TrkReco/TMLine.h"
00016 #include "TrkReco/TMLink.h"
00017
00018 TLineFitter::TLineFitter(const std::string & name)
00019 : TMFitter(name), _a(0.), _b(0.), _det(0.) {
00020 }
00021
00022 TLineFitter::~TLineFitter() {
00023 }
00024
00025 int
00026 TLineFitter::fit(TTrackBase & t) const {
00027
00028
00029 if (t.fitted()) return TFitAlreadyFitted;
00030
00031
00032 if (t.links().length() < 2) return TFitErrorFewHits;
00033
00034 unsigned n = t.links().length();
00035 if (_det == 0. && n == 2) {
00036 double x0 = t.links()[0]->position().x();
00037 double y0 = t.links()[0]->position().y();
00038 double x1 = t.links()[1]->position().x();
00039 double y1 = t.links()[1]->position().y();
00040 if (x0 == x1) return TFitFailed;
00041 _a = (y0 - y1) / (x0 - x1);
00042 _b = - _a * x1 + y1;
00043 }
00044 else {
00045 double sum = (double) n;
00046 double sumX = 0., sumY = 0., sumX2 = 0., sumXY = 0., sumY2 = 0.;
00047 for (unsigned i = 0; i < n; i++) {
00048 const HepPoint3D & p = t.links()[i]->position();
00049 double x = p.x();
00050 double y = p.y();
00051 sumX += x;
00052 sumY += y;
00053 sumX2 += x * x;
00054 sumXY += x * y;
00055 sumY2 += y * y;
00056 }
00057
00058 _det = sum * sumX2 - sumX * sumX;
00059 #ifdef TRKRECO_DEBUG_DETAIL
00060 cout << " TLineFitter::fit ... det=" << _det << std::endl;
00061 #endif
00062 if (_det == 0.) {
00063 return TFitFailed;
00064 }
00065 else {
00066 _a = (sumXY * sum - sumX * sumY) / _det;
00067 _b = (sumX2 * sumY - sumX * sumXY) / _det;
00068 }
00069 }
00070
00071 if (t.objectType() == Line)
00072 ((TMLine &) t).property(_a, _b, _det);
00073 fitDone(t);
00074 return 0;
00075 }