BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
TLine2D.cxx
Go to the documentation of this file.
1//-----------------------------------------------------------------------------
2// $Id: TLine2D.cxx,v 1.5 2010/03/31 09:58:59 liucy Exp $
3//-----------------------------------------------------------------------------
4// Filename : TLine2D.cc
5// Section : Tracking
6// Owner : Yoshi Iwasaki
7// Email : yoshihito.iwasaki@kek.jp
8//-----------------------------------------------------------------------------
9// Description : A class to represent a line in 2D.
10// See http://bsunsrv1.kek.jp/~yiwasaki/tracking/
11//-----------------------------------------------------------------------------
12// $Log: TLine2D.cxx,v $
13// Revision 1.5 2010/03/31 09:58:59 liucy
14// CLHEP based on 05.17
15//
16// Revision 1.2 2005/09/09 07:47:07 zangsl
17// 20050909 boss4.2 freeze
18//
19// Revision 1.1 1999/11/19 09:13:08 yiwasaki
20// Trasan 1.65d : new conf. finder updates, no change in default conf. finder
21//
22//
23//-----------------------------------------------------------------------------
24
25#define TLINE2D_INLINE_DEFINE_HERE
26// #define HEP_SHORT_NAMES
27#include "TrkReco/TLine2D.h"
28#include "TrkReco/TPoint2D.h"
29
30TLine2D::TLine2D() : _slope( 1 ), _yOffset( 0 ), _det( 0 ), _list( 0 ) {}
31
32TLine2D::TLine2D( double a, double b ) : _slope( a ), _yOffset( b ), _det( 0 ), _list( 0 ) {}
33
34TLine2D::TLine2D( const AList<TPoint2D>& a ) : _slope( 1 ), _yOffset( 0 ), _det( 0 ) {
35 _list = new CAList<TPoint2D>();
36 _list->append( a );
37}
38
40 if ( _list ) delete _list;
41}
42
43void TLine2D::append( const TPoint2D& a ) {
44 if ( !_list ) _list = new CAList<TPoint2D>();
45 _list->append( a );
46}
47
48void TLine2D::remove( const TPoint2D& a ) {
49 if ( !_list ) return;
50 _list->remove( a );
51}
52
53const CAList<TPoint2D>& TLine2D::list( void ) const {
54 if ( !_list ) _list = new CAList<TPoint2D>();
55 return *_list;
56}
57
58int TLine2D::fit( void ) {
59 if ( !_list ) return -1;
60
61 unsigned n = _list->length();
62 if ( !n ) return -1;
63
64 if ( n == 2 )
65 {
66 double x0 = ( *_list )[0]->x();
67 double y0 = ( *_list )[0]->y();
68 double x1 = ( *_list )[1]->x();
69 double y1 = ( *_list )[1]->y();
70 if ( x0 == x1 ) return -2;
71 _slope = ( y0 - y1 ) / ( x0 - x1 );
72 _yOffset = -_slope * x1 + y1;
73
74 return 0;
75 }
76
77 double sum = double( n );
78 double sumX = 0., sumY = 0., sumX2 = 0., sumXY = 0., sumY2 = 0.;
79 for ( unsigned i = 0; i < n; i++ )
80 {
81 const TPoint2D& p = *( *_list )[i];
82 double x = p.x();
83 double y = p.y();
84 sumX += x;
85 sumY += y;
86 sumX2 += x * x;
87 sumXY += x * y;
88 sumY2 += y * y;
89 }
90
91 _det = sum * sumX2 - sumX * sumX;
92 if ( _det == 0. ) return -3;
93
94 _slope = ( sumXY * sum - sumX * sumY ) / _det;
95 _yOffset = ( sumX2 * sumY - sumX * sumXY ) / _det;
96
97 return 0;
98}
99
100double TLine2D::distance( const TPoint2D& p ) const {
101 double ydif = p.y() - _yOffset;
102 double vmag = sqrt( 1. + _slope * _slope );
103 double dot = ( p.x() + ydif * _slope ) / vmag;
104 double xmag2 = p.x() * p.x() + ydif * ydif;
105 return sqrt( xmag2 - dot * dot );
106}
const Int_t n
TLine2D()
Constructors.
Definition TLine2D.cxx:30
double distance(const TPoint2D &) const
Definition TLine2D.cxx:100
const CAList< TPoint2D > & list(void) const
Definition TLine2D.cxx:53
void append(const TPoint2D &)
Definition TLine2D.cxx:43
virtual ~TLine2D()
Destructor.
Definition TLine2D.cxx:39
void remove(const TPoint2D &)
Definition TLine2D.cxx:48
int fit(void)
Definition TLine2D.cxx:58