BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
TRobustLineFitter.cxx
Go to the documentation of this file.
1//-----------------------------------------------------------------------------
2// $Id: TRobustLineFitter.cxx,v 1.7 2010/03/31 09:58:59 liucy Exp $
3//-----------------------------------------------------------------------------
4// Filename : TRobustLineFitter.cc
5// Section : Tracking
6// Owner : Yoshi Iwasaki
7// Email : yoshihito.iwasaki@kek.jp
8//-----------------------------------------------------------------------------
9// Description : A class to fit a TTrackBase object to a line.
10// See http://bsunsrv1.kek.jp/~yiwasaki/tracking/
11//-----------------------------------------------------------------------------
12
13/* for copysign */
14#if defined( __sparc )
15# if defined( __EXTENSIONS__ )
16# include <cmath>
17# else
18# define __EXTENSIONS__
19# include <cmath>
20# undef __EXTENSIONS__
21# endif
22#elif defined( __GNUC__ )
23# if defined( _XOPEN_SOURCE )
24# include <cmath>
25# else
26# define _XOPEN_SOURCE
27# include <cmath>
28# undef _XOPEN_SOURCE
29# endif
30#endif
31
32#include "TrkReco/TMLine.h"
33#include "TrkReco/TMLink.h"
34#include "TrkReco/TRobustLineFitter.h"
35#include "TrkReco/TTrackBase.h"
36
38 : TLineFitter( name ), _a( 0. ), _b( 0. ), _det( 0. ) {}
39
41
43 //...Initial guess...
44 int err = TLineFitter::fit( t );
45 if ( err ) return err;
46
47 //...Check # of hits...
48 const AList<TMLink>& links = t.links();
49 _n = links.length();
50 if ( _n < 3 ) return 0;
51
52 //...Standard deviation...
53 _a = TLineFitter::a();
54 _b = TLineFitter::b();
55 _det = TLineFitter::det();
56 double chisq = 0.;
57 for ( unsigned i = 0; i < _n; i++ )
58 {
59 const HepPoint3D& p = links[i]->position();
60 double tmp = p.y() - ( _a * p.x() + _b );
61 chisq += tmp * tmp;
62 }
63 double siga = sqrt( chisq / _det );
64
65 //...Decide iteration step...
66 double a1 = _a;
67 double f1 = rofunc( t, a1 );
68 double a2 = _a + copysign( 3.0 * siga, f1 );
69 double f2 = rofunc( t, a2 );
70
71 // if initial value f2 >f1, change the search direction
72 if ( f1 * f2 > 0. && fabs( f2 ) > fabs( f1 ) )
73 {
74 a2 = _a - copysign( 3.0 * siga, f1 );
75 f2 = rofunc( t, a2 );
76 }
77
78 int backwardSearch = 0;
79 while ( f1 * f2 > 0. )
80 {
81 _a = 2.0 * a2 - a1;
82 a1 = a2;
83 f1 = f2;
84 a2 = _a;
85 f2 = rofunc( t, a2 );
86
87 if ( f1 * f2 > 0. && ( fabs( f2 ) > fabs( f1 ) || fabs( f2 - f1 ) < 0.01 ) )
88 {
89 backwardSearch++;
90 if ( backwardSearch == 2 ) { break; }
91
92 double f = f2;
93 a2 = a1;
94 f2 = f1;
95 a1 = _a;
96 f1 = f;
97 }
98 }
99
100 if ( backwardSearch == 2 )
101 {
102 // for case of no zero cross
103 // search minimun fabs(f)
104
105 double siga1 = 0.01 * siga;
106 double a21( fabs( a2 - a1 ) );
107 while ( a21 > siga1 )
108 {
109 _a = 0.5 * ( a1 + a2 );
110 if ( _a == a1 || _a == a2 ) break;
111 double f = rofunc( t, _a );
112
113 if ( f * f1 < 0 )
114 {
115 f1 = f;
116 a1 = _a;
117 backwardSearch--;
118 break;
119 }
120
121 if ( fabs( f ) <= fabs( f1 ) && fabs( f ) <= fabs( f2 ) )
122 {
123 if ( fabs( f - f1 ) > fabs( f - f2 ) )
124 {
125 f1 = f;
126 a1 = _a;
127 }
128 else
129 {
130 f2 = f;
131 a2 = _a;
132 }
133 }
134 else if ( fabs( f ) <= fabs( f1 ) )
135 {
136 f1 = f;
137 a1 = _a;
138 }
139 else if ( fabs( f ) <= fabs( f2 ) )
140 {
141 f2 = f;
142 a2 = _a;
143 }
144 else
145 {
146 if ( fabs( f2 ) > fabs( f1 ) )
147 {
148 f1 = f;
149 a1 = _a;
150 }
151 else
152 {
153 f2 = f;
154 a2 = _a;
155 }
156 }
157 if ( fabs( a2 - a1 ) >= a21 ) break;
158 a21 = fabs( a2 - a1 );
159 }
160 }
161
162 if ( backwardSearch <= 1 )
163 {
164
165 // search zero cross
166 siga = 0.01 * siga;
167
168 double a21( fabs( a2 - a1 ) );
169 while ( a21 > siga )
170 {
171 _a = 0.5 * ( a1 + a2 );
172 if ( _a == a1 || _a == a2 ) break;
173 double f = rofunc( t, _a );
174 if ( f * f1 >= 0. )
175 {
176 f1 = f;
177 a1 = _a;
178 }
179 else
180 {
181 f2 = f;
182 a2 = _a;
183 }
184 if ( fabs( a2 - a1 ) >= a21 ) break;
185 a21 = fabs( a2 - a1 );
186 }
187 }
188
189 _det = _det / double( _n );
190
191 if ( t.objectType() == Line ) ( (TMLine&)t ).property( _a, _b, _det );
192 fitDone( t );
193 return 0;
194}
195
196double TRobustLineFitter::rofunc( const TTrackBase& t, double a ) const {
197 double* arr = (double*)malloc( _n * sizeof( double ) );
198 for ( unsigned i = 0; i < _n; i++ )
199 arr[i] = t.links()[i]->position().y() - a * t.links()[i]->position().x();
200 if ( _n & 1 ) { _b = select( ( _n + 1 ) >> 1, _n, arr ); }
201 else
202 {
203 unsigned j = _n >> 1;
204 _b = 0.5 * ( select( j, _n, arr ) + select( j + 1, _n, arr ) );
205 }
206
207 _det = 0.;
208 double sum = 0.;
209 for ( unsigned i = 0; i < _n; i++ )
210 {
211 double x = t.links()[i]->position().x();
212 double y = t.links()[i]->position().y();
213 double d = y - ( a * x + _b );
214 _det += fabs( d );
215 if ( y != 0. ) d /= fabs( y );
216 if ( fabs( d ) > 1.0e-7 ) sum += d > 0.0 ? x : -x;
217 }
218 free( arr );
219 return sum;
220}
221
222#define SWAP( a, b ) \
223 tmp = ( a ); \
224 ( a ) = ( b ); \
225 ( b ) = tmp;
226
227double TRobustLineFitter::select( unsigned k, unsigned n, double* arr ) const {
228 --k;
229 double tmp;
230 unsigned l = 0;
231 unsigned ir = n - 1;
232 while ( 1 )
233 {
234 if ( ir <= l + 1 )
235 {
236 if ( ir == l + 1 && arr[ir] < arr[l] ) { SWAP( arr[l], arr[ir] ); }
237
238 // std::cout << "k = " << k << std::endl;
239 // for (unsigned i = 0; i < _n; i++)
240 // std::cout << i << " : " << arr[i] << std::endl;
241
242 return arr[k];
243 }
244 else
245 {
246 unsigned mid = ( l + ir ) >> 1;
247 SWAP( arr[mid], arr[l + 1] );
248 if ( arr[l + 1] > arr[ir] ) { SWAP( arr[l + 1], arr[ir] ); }
249 if ( arr[l] > arr[ir] ) { SWAP( arr[l], arr[ir] ); }
250 if ( arr[l + 1] > arr[l] ) { SWAP( arr[l + 1], arr[l] ); }
251 unsigned i = l + 1;
252 unsigned j = ir;
253 double a = arr[l];
254 while ( 1 )
255 {
256 // do i++; while (arr[i] < a);
257 // do j--; while (arr[j] > a);
258 // while (arr[i] < a) ++i;
259 // while (arr[j] > a) --j;
260 while ( arr[++i] < a )
261 ;
262 while ( arr[--j] > a )
263 ;
264 if ( j < i ) break;
265 SWAP( arr[i], arr[j] );
266 }
267 arr[l] = arr[j];
268 arr[j] = a;
269 if ( j >= k ) ir = j - 1;
270 if ( j <= k ) l = i;
271 }
272 }
273}
HepGeom::Point3D< double > HepPoint3D
TFile f("ana_bhabha660a_dqa_mcPat_zy_old.root")
const Int_t n
TFile * f1
Double_t x[10]
#define SWAP(a, b)
virtual int fit(TTrackBase &) const
TLineFitter(const std::string &name)
Constructor.
const std::string & name(void) const
returns name.
void fitDone(TTrackBase &) const
sets the fitted flag. (Bad implementation)
Definition TMFitter.cxx:21
A class to represent a track in tracking.
TRobustLineFitter(const std::string &name)
Constructor.
virtual int fit(TTrackBase &) const
virtual ~TRobustLineFitter()
Destructor.
A virtual class for a track class in tracking.
int t()
Definition t.c:1