BOSS 7.0.5
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 : [email protected]
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/TRobustLineFitter.h"
33#include "TrkReco/TTrackBase.h"
34#include "TrkReco/TMLine.h"
35#include "TrkReco/TMLink.h"
36
37TRobustLineFitter::TRobustLineFitter(const std::string & name)
38: TLineFitter(name), _a(0.), _b(0.), _det(0.) {
39}
40
42}
43
44int
46 //...Initial guess...
47 int err = TLineFitter::fit(t);
48 if (err) return err;
49
50 //...Check # of hits...
51 const AList<TMLink> & links = t.links();
52 _n = links.length();
53 if (_n < 3) return 0;
54
55 //...Standard deviation...
56 _a = TLineFitter::a();
57 _b = TLineFitter::b();
58 _det = TLineFitter::det();
59 double chisq = 0.;
60 for (unsigned i = 0; i < _n; i++) {
61 const HepPoint3D & p = links[i]->position();
62 double tmp = p.y() - (_a * p.x() + _b);
63 chisq += tmp * tmp;
64 }
65 double siga = sqrt(chisq / _det);
66
67 //...Decide iteration step...
68 double a1 = _a;
69 double f1 = rofunc(t, a1);
70 double a2 = _a + copysign(3.0 * siga, f1);
71 double f2 = rofunc(t, a2);
72
73 // if initial value f2 >f1, change the search direction
74 if( f1 * f2 > 0. && fabs(f2) > fabs(f1) ){
75 a2 = _a - copysign(3.0 * siga, f1);
76 f2 = rofunc(t, a2);
77 }
78
79 int backwardSearch=0;
80 while (f1 * f2 > 0. ) {
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 backwardSearch++;
89 if(backwardSearch==2){
90 break;
91 }
92
93 double f=f2;
94 a2 = a1;
95 f2 = f1;
96 a1 = _a;
97 f1 = f;
98 }
99 }
100
101 if(backwardSearch==2){
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 _a = 0.5 * (a1 + a2);
109 if (_a == a1 || _a == a2) break;
110 double f = rofunc(t, _a);
111
112 if( f * f1 <0 ){
113 f1=f;
114 a1=_a;
115 backwardSearch--;
116 break;
117 }
118
119 if ( fabs(f) <= fabs(f1) && fabs(f) <= fabs(f2)) {
120 if( fabs(f-f1) > fabs(f-f2) ){
121 f1 = f;
122 a1 = _a;
123 }else{
124 f2 = f;
125 a2 = _a;
126 }
127 }else if( fabs(f) <= fabs(f1) ){
128 f1 = f;
129 a1 = _a;
130 }else if ( fabs(f) <= fabs(f2) ){
131 f2 = f;
132 a2 = _a;
133 }else{
134 if( fabs(f2) > fabs(f1) ){
135 f1=f;
136 a1 = _a;
137 }else{
138 f2 = f;
139 a2 = _a;
140 }
141 }
142 if (fabs(a2-a1) >= a21) break;
143 a21 = fabs(a2-a1);
144 }
145 }
146
147 if(backwardSearch<=1){
148
149 //search zero cross
150 siga = 0.01 * siga;
151
152 double a21(fabs(a2-a1));
153 while (a21 > siga) {
154 _a = 0.5 * (a1 + a2);
155 if (_a == a1 || _a == a2) break;
156 double f = rofunc(t, _a);
157 if (f * f1 >= 0.) {
158 f1 = f;
159 a1 = _a;
160 }
161 else {
162 f2 = f;
163 a2 = _a;
164 }
165 if (fabs(a2-a1) >= a21) break;
166 a21 = fabs(a2-a1);
167 }
168 }
169
170 _det = _det / double(_n);
171
172 if (t.objectType() == Line)
173 ((TMLine &) t).property(_a, _b, _det);
174 fitDone(t);
175 return 0;
176}
177
178double
179TRobustLineFitter::rofunc(const TTrackBase & t, double a) const {
180 double * arr = (double *) malloc(_n * sizeof(double));
181 for (unsigned i = 0; i < _n; i++)
182 arr[i] = t.links()[i]->position().y()
183 - a * t.links()[i]->position().x();
184 if (_n & 1) {
185 _b = select((_n + 1) >> 1, _n, arr);
186 }
187 else {
188 unsigned j = _n >> 1;
189 _b = 0.5 * (select(j, _n, arr) + select(j + 1, _n, arr));
190 }
191
192 _det = 0.;
193 double sum = 0.;
194 for (unsigned i = 0; i < _n; i++) {
195 double x = t.links()[i]->position().x();
196 double y = t.links()[i]->position().y();
197 double d = y - (a * x + _b);
198 _det += fabs(d);
199 if (y != 0.) d /= fabs(y);
200 if (fabs(d) > 1.0e-7) sum += d > 0.0 ? x : - x;
201 }
202 free(arr);
203 return sum;
204}
205
206#define SWAP(a,b) tmp=(a);(a)=(b);(b)=tmp;
207
208double
209TRobustLineFitter::select(unsigned k, unsigned n, double * arr) const {
210 --k;
211 double tmp;
212 unsigned l = 0;
213 unsigned ir = n - 1;
214 while (1) {
215 if (ir <= l + 1) {
216 if (ir == l + 1 && arr[ir] < arr[l]) {
217 SWAP(arr[l],arr[ir]);
218 }
219
220// std::cout << "k = " << k << std::endl;
221// for (unsigned i = 0; i < _n; i++)
222// std::cout << i << " : " << arr[i] << std::endl;
223
224 return arr[k];
225 }
226 else {
227 unsigned mid = (l + ir) >> 1;
228 SWAP(arr[mid],arr[l+1]);
229 if (arr[l + 1] > arr[ir]) {
230 SWAP(arr[l+1],arr[ir]);
231 }
232 if (arr[l] > arr[ir]) {
233 SWAP(arr[l],arr[ir]);
234 }
235 if (arr[l + 1] > arr[l]) {
236 SWAP(arr[l+1],arr[l]);
237 }
238 unsigned i = l + 1;
239 unsigned j = ir;
240 double a = arr[l];
241 while (1) {
242// do i++; while (arr[i] < a);
243// do j--; while (arr[j] > a);
244// while (arr[i] < a) ++i;
245// while (arr[j] > a) --j;
246 while (arr[++i] < a);
247 while (arr[--j] > a);
248 if (j < i) break;
249 SWAP(arr[i],arr[j]);
250 }
251 arr[l] = arr[j];
252 arr[j] = a;
253 if (j >= k) ir = j - 1;
254 if (j <= k) l = i;
255 }
256 }
257}
const Int_t n
TFile * f1
Double_t x[10]
#define SWAP(a, b)
TTree * t
Definition: binning.cxx:23
A class to fit a TTrackBase object to a line.
virtual int fit(TTrackBase &) const
Definition: TLineFitter.cxx:26
void fitDone(TTrackBase &) const
sets the fitted flag. (Bad implementation)
Definition: TMFitter.cxx:24
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.
double y[1000]
TFile f("ana_bhabha660a_dqa_mcPat_zy_old.root")