00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #define TLINE2D_INLINE_DEFINE_HERE
00026 #define HEP_SHORT_NAMES
00027 #include "CLHEP/Alist/ConstAList.h"
00028 #include "TrkReco/TPoint2D.h"
00029 #include "TrkReco/TLine2D.h"
00030
00031 TLine2D::TLine2D() : _slope(1), _yOffset(0), _det(0), _list(0) {
00032 }
00033
00034 TLine2D::TLine2D(double a, double b)
00035 : _slope(a),
00036 _yOffset(b),
00037 _det(0),
00038 _list(0) {
00039 }
00040
00041 TLine2D::TLine2D(const AList<TPoint2D> & a) : _slope(1), _yOffset(0), _det(0) {
00042 _list = new CAList<TPoint2D>();
00043 _list->append(a);
00044 }
00045
00046 TLine2D::~TLine2D() {
00047 if (_list) delete _list;
00048 }
00049
00050 void
00051 TLine2D::append(const TPoint2D & a) {
00052 if (! _list)
00053 _list = new CAList<TPoint2D>();
00054 _list->append(a);
00055 }
00056
00057 void
00058 TLine2D::remove(const TPoint2D & a) {
00059 if (! _list) return;
00060 _list->remove(a);
00061 }
00062
00063 const CAList<TPoint2D> &
00064 TLine2D::list(void) const {
00065 if (! _list)
00066 _list = new CAList<TPoint2D>();
00067 return * _list;
00068 }
00069
00070 int
00071 TLine2D::fit(void) {
00072 if (! _list) return -1;
00073
00074 unsigned n = _list->length();
00075 if (! n) return -1;
00076
00077 if (n == 2) {
00078 double x0 = (* _list)[0]->x();
00079 double y0 = (* _list)[0]->y();
00080 double x1 = (* _list)[1]->x();
00081 double y1 = (* _list)[1]->y();
00082 if (x0 == x1) return -2;
00083 _slope = (y0 - y1) / (x0 - x1);
00084 _yOffset = - _slope * x1 + y1;
00085
00086 return 0;
00087 }
00088
00089 double sum = double(n);
00090 double sumX = 0., sumY = 0., sumX2 = 0., sumXY = 0., sumY2 = 0.;
00091 for (unsigned i = 0; i < n; i++) {
00092 const TPoint2D & p = * (* _list)[i];
00093 double x = p.x();
00094 double y = p.y();
00095 sumX += x;
00096 sumY += y;
00097 sumX2 += x * x;
00098 sumXY += x * y;
00099 sumY2 += y * y;
00100 }
00101
00102 _det = sum * sumX2 - sumX * sumX;
00103 if (_det == 0.) return -3;
00104
00105 _slope = (sumXY * sum - sumX * sumY) / _det;
00106 _yOffset = (sumX2 * sumY - sumX * sumXY) / _det;
00107
00108 return 0;
00109 }
00110
00111 double
00112 TLine2D::distance(const TPoint2D & p) const {
00113 double ydif = p.y() - _yOffset;
00114 double vmag = sqrt(1. + _slope * _slope);
00115 double dot = (p.x() + ydif * _slope) / vmag;
00116 double xmag2 = p.x() * p.x() + ydif * ydif;
00117 return sqrt(xmag2 - dot * dot);
00118 }