BOSS 7.0.2
BESIII Offline Software System
Loading...
Searching...
No Matches
TMLine.cxx
Go to the documentation of this file.
1//-----------------------------------------------------------------------------
2// $Id: TMLine.cxx,v 1.7 2012/05/28 05:16:29 maoh Exp $
3//-----------------------------------------------------------------------------
4// Filename : TMLine.cc
5// Section : Tracking
6// Owner : Yoshi Iwasaki
7// Email : [email protected]
8//-----------------------------------------------------------------------------
9// Description : A class to represent a line in tracking.
10// See http://bsunsrv1.kek.jp/~yiwasaki/tracking/
11//-----------------------------------------------------------------------------
12
13#include "TrkReco/TMLine.h"
14#include "TrkReco/TMDCUtil.h"
15#include "TrkReco/TMDCWire.h"
16#include "TrkReco/TMDCWireHit.h"
17#include "TrkReco/TMDCWireHitMC.h"
18#include "TrkReco/TTrackHEP.h"
19
20const TLineFitter
21TMLine::_fitter = TLineFitter("TMLine Default Line Fitter");
22
24: TTrackBase(),
25 _a(0.),
26 _b(0.),
27 _det(0.),
28 _fittedUpdated(false),
29 _chi2(0.),
30 _reducedChi2(0.) {
31
32 //...Set a defualt fitter...
33 fitter(& TMLine::_fitter);
34}
35
37: TTrackBase(a),
38 _a(0.),
39 _b(0.),
40 _det(0.),
41 _fittedUpdated(false),
42 _chi2(0.),
43 _reducedChi2(0.) {
44
45 //...Set a defualt fitter...
46 fitter(& TMLine::_fitter);
47}
48
50}
51
52void
53TMLine::dump(const std::string & msg, const std::string & pre) const {
54 bool def = false;
55 if (msg == "") def = true;
56
57 if (def || msg.find("line") != std::string::npos || msg.find("detail") != std::string::npos) {
58 std::cout << pre;
59 std::cout << "#links=" << _links.length();
60 std::cout << ",a=" << _a;
61 std::cout << ",b=" << _b;
62 std::cout << ",det=" << _det;
63 std::cout << std::endl;
64 }
65 if (! def) TTrackBase::dump(msg, pre);
66}
67
68// int
69// TMLine::fitx(void) {
70// if (_fitted) return 0;
71
72// unsigned n = _links.length();
73// double sum = double(n);
74// double sumX = 0., sumY = 0., sumX2 = 0., sumXY = 0., sumY2 = 0.;
75// for (unsigned i = 0; i < n; i++) {
76// TMLink & l = * _links[i];
77
78// double x = l.position().x();
79// double y = l.position().y();
80// sumX += x;
81// sumY += y;
82// sumX2 += x * x;
83// sumXY += x * y;
84// sumY2 += y * y;
85// }
86
87// _det = sum * sumX2 - sumX * sumX;
88// #ifdef TRKRECO_DEBUG_DETAIL
89// std::cout << " TMLine::fit ... det=" << _det << std::endl;
90// #endif
91// if(_det == 0. && n != 2){
92// return -1;
93// }else if(_det == 0. && n == 2){
94// double x0 = _links[0]->position().x();
95// double y0 = _links[0]->position().y();
96// double x1 = _links[1]->position().x();
97// double y1 = _links[1]->position().y();
98// if(x0 == x1)return -1;
99// _a = (y0-y1)/(x0-x1);
100// _b = -_a*x1 + y1;
101
102// _fitted = true;
103// return 0;
104// }
105// _a = (sumXY * sum - sumX * sumY) / _det;
106// _b = (sumX2 * sumY - sumX * sumXY) / _det;
107
108// _fitted = true;
109// return 0;
110// }
111
112double
113TMLine::chi2(void) const {
114#ifdef TRKRECO_DEBUG
115 if (! _fitted)
116 std::cout << "TMLine::chi2 !!! fit not performed" << std::endl;
117#endif
118
119 if (_fittedUpdated) return _chi2;
120 _chi2 = 0.;
121 unsigned n = _links.length();
122 for (unsigned i = 0; i < n; i++) {
123 TMLink & l = * _links[i];
124
125 double x = l.position().x();
126 double y = l.position().y();
127 double c = y - _a * x - _b;
128 _chi2 += c * c;
129 }
130 _fittedUpdated = true;
131 return _chi2;
132}
133
134void
135TMLine::refine(AList<TMLink> & list, float maxSigma) {
136 AList<TMLink> bad;
137 unsigned n = _links.length();
138 for (unsigned i = 0; i < n; i++) {
139 TMLink & l = * _links[i];
140 double dist = distance(l);
141 if (dist > maxSigma) bad.append(l);
142 }
143
144#ifdef TRKRECO_DEBUG_DETAIL
145 std::cout << " TMLine::refine ... rejected hits:max distance=" << maxSigma;
146 std::cout << std::endl;
147 bad.sort(SortByWireId);
148 for (unsigned i = 0; i < bad.length(); i++) {
149 TMLink & l = * _links[i];
150 std::cout << " ";
151 std::cout << l.wire()->layerId() << "-";
152 std::cout << l.wire()->localId();
153 std::cout << "(";
154 if (l.hit()->mc()) {
155 if (l.hit()->mc()->hep()) std::cout << l.hit()->mc()->hep()->id();
156 else std::cout << "0";
157 }
158 std::cout << "),";
159 std::cout << l.position() << "," << distance(l);
160 if (distance(l) > maxSigma) std::cout << " X";
161 std::cout << std::endl;
162 }
163#endif
164
165 if (bad.length()) {
166 _links.remove(bad);
167 list.append(bad);
168 _fitted = false;
169 _fittedUpdated = false;
170 }
171}
172
173int
175 std::cout<<" "<<__FILE__<<" "<<__LINE__<<" ERROR : nsl"<<std::endl;
176 // if (_fitted) return 0;
177
178 unsigned n = _links.length();
179 int mask[100] = {0};
180 int nsl[11] = {64,80,96,128,144,160,192,208,240,256,288};
181 int npos = 0, nneg = 0;
182 for (unsigned i = 0; i < n -1 ; i++) {
183 TMLink & l = * _links[i];
184 for (unsigned j = i+1; j < n ; j++) {
185 TMLink & s = * _links[j];
186 if(l.hit()->wire()->layerId() == s.hit()->wire()->layerId()){
187 //... Check 3 consective hits
188 if(i > 0 && (mask[i-1] == 1 && mask[j] == 1) ){
189 TMLink & t = * _links[i-1];
190 if(l.hit()->wire()->layerId() == t.hit()->wire()->layerId()){
191 mask[i] = 1;
192 }
193 }
194 int ilast = nsl[l.hit()->wire()->superLayerId()]-1;
195 int ilocal = l.hit()->wire()->localId();
196 int jlocal = s.hit()->wire()->localId();
197 if(ilocal > 0 && ilocal < ilast){
198 if(abs(jlocal-ilocal) > 1 ) {
199 mask[i] = 1;
200 mask[j] = 1;
201 }
202 }else if(ilocal == 0){
203 if(jlocal > 1 && jlocal < ilast){
204 mask[i] = 1;
205 mask[j] = 1;
206 }
207 }else if(ilocal == ilast){
208 if(jlocal > 0 && jlocal < ilast-1){
209 mask[i] = 1;
210 mask[j] = 1;
211 }
212 }
213 }
214 }
215 //...
216 if(mask[i] == 0){
217 if(l.position().y() >= 0) npos += 1;
218 if(l.position().y() < 0) nneg += 1;
219 }
220 }
221
222 //....
223 double sumX = 0., sumY = 0., sumX2 = 0., sumXY = 0., sumY2 = 0.;
224 int nused = 0;
225 int lyid[2];
226 for (unsigned i = 0; i < n; i++) {
227
228 if(mask[i] == 1) continue;
229
230 TMLink & l = * _links[i];
231
232 double x = l.position().x();
233 double y = l.position().y();
234 if(abs(npos-nneg) > 3){
235 if(npos > nneg && y < 0 ) continue;
236 if(npos < nneg && y > 0 ) continue;
237 }
238 sumX += x;
239 sumY += y;
240 sumX2 += x * x;
241 sumXY += x * y;
242 sumY2 += y * y;
243 if(nused < 2){
244 lyid[nused] = l.hit()->wire()->layerId();
245 }
246 nused += 1;
247 }
248
249 if(nused < 2 || (nused == 2 && lyid[0]==lyid[1])) {
250 return -2;
251 }
252 double sum = double(nused);
253 _det = sum * sumX2 - sumX * sumX;
254 if(_det == 0.) {
255 return -1;
256 }
257 _a = (sumXY * sum - sumX * sumY) / _det;
258 _b = (sumX2 * sumY - sumX * sumXY) / _det;
259
260 _fitted = true;
261 return 0;
262}
263
264int
266 // if (_fitted) return 0;
267
268 unsigned n = _links.length();
269 int mask[100] = {0};
270 int npos = 0, nneg = 0;
271 for (unsigned i = 0; i < n -1 ; i++) {
272 TMLink & l = * _links[i];
273 for (unsigned j = i+1; j < n ; j++) {
274 TMLink & s = * _links[j];
275 if(l.hit()->wire()->layerId() == s.hit()->wire()->layerId()){
276 mask[i] = 1;
277 mask[j] = 1;
278 }
279 }
280 //...
281 if(mask[i] == 0){
282 if(l.position().y() >= 0) npos += 1;
283 if(l.position().y() < 0) nneg += 1;
284 }
285 }
286
287 //....
288 double sumX = 0., sumY = 0., sumX2 = 0., sumXY = 0., sumY2 = 0.;
289 int nused = 0;
290 int lyid[2];
291 for (unsigned i = 0; i < n; i++) {
292
293 if(mask[i] == 1) continue;
294
295 TMLink & l = * _links[i];
296
297 double x = l.position().x();
298 double y = l.position().y();
299 if(npos > nneg && y < 0 ) continue;
300 if(npos < nneg && y > 0 ) continue;
301
302 sumX += x;
303 sumY += y;
304 sumX2 += x * x;
305 sumXY += x * y;
306 sumY2 += y * y;
307 nused += 1;
308 }
309
310 if(nused < 4) {
311 return -2;
312 }
313 double sum = double(nused);
314 _det = sum * sumX2 - sumX * sumX;
315 if(_det == 0.) {
316 return -1;
317 }
318 _a = (sumXY * sum - sumX * sumY) / _det;
319 _b = (sumX2 * sumY - sumX * sumXY) / _det;
320
321 _fitted = true;
322 return 0;
323}
324int
326 // if (_fitted) return 0;
327
328 unsigned n = _links.length();
329 int mask[100] = {0};
330 double phi_ave = 0.;
331 int nphi = 0;
332 double Crad = 180./3.141592;
333 for (unsigned i = 0; i < n -1 ; i++) {
334 TMLink & l = * _links[i];
335 for (unsigned j = i+1; j < n ; j++) {
336 TMLink & s = * _links[j];
337 if(l.hit()->wire()->layerId() == s.hit()->wire()->layerId()){
338 mask[i] = 1;
339 mask[j] = 1;
340 }
341 }
342 //...
343 if(mask[i] != 1){
344 double phi = Crad*atan2(l.position().y(), l.position().x());
345 phi_ave += phi;
346 nphi += 1;
347 }
348 }
349
350 //...
351 if(mask[n-1] != 1){
352 TMLink & l = * _links[n-1];
353 double phi = Crad*atan2(l.position().y(), l.position().x());
354 phi_ave += phi;
355 nphi += 1;
356 }
357 double phi_max = 0.;
358 double phi_min = 0.;
359 if(nphi> 0){
360 phi_max = phi_ave/n + 40;
361 phi_min = phi_ave/n - 40;
362 }
363
364 //....
365 double sumX = 0., sumY = 0., sumX2 = 0., sumXY = 0., sumY2 = 0.;
366 int nused = 0;
367 int lyid[2];
368 for (unsigned i = 0; i < n; i++) {
369
370 if(mask[i] == 1) continue;
371
372 TMLink & l = * _links[i];
373
374 double x = l.position().x();
375 double y = l.position().y();
376 double phi = Crad*atan2(l.position().y(), l.position().x());
377 if(phi > phi_max && phi<phi_min ) continue;
378
379 sumX += x;
380 sumY += y;
381 sumX2 += x * x;
382 sumXY += x * y;
383 sumY2 += y * y;
384 nused += 1;
385 }
386
387 if(nused < 4) {
388 return -2;
389 }
390 double sum = double(nused);
391 _det = sum * sumX2 - sumX * sumX;
392 if(_det == 0.) {
393 return -1;
394 }
395 _a = (sumXY * sum - sumX * sumY) / _det;
396 _b = (sumX2 * sumY - sumX * sumXY) / _det;
397
398 _fitted = true;
399 return 0;
400}
401
402int
404 // if (_fitted) return 0;
405
406 std::cout<<" "<<__FILE__<<" "<<__LINE__<<" ERROR : nsl"<<std::endl;
407 unsigned n = _links.length();
408 int mask[100] = {0};
409 int nsl[11] = {64,80,96,128,144,160,192,208,240,256,288};
410 double phi_ave = 0.;
411 int nphi = 0;
412 double Crad = 180./3.141592;
413 for (unsigned i = 0; i < n -1 ; i++) {
414 TMLink & l = * _links[i];
415 for (unsigned j = i+1; j < n ; j++) {
416 TMLink & s = * _links[j];
417 if(l.hit()->wire()->layerId() == s.hit()->wire()->layerId()){
418 //... Check 3 consective hits
419 if(i > 0 && (mask[i-1] == 1 && mask[j] == 1) ){
420 TMLink & t = * _links[i-1];
421 if(l.hit()->wire()->layerId() == t.hit()->wire()->layerId()){
422 mask[i] = 1;
423 }
424 }
425 int ilast = nsl[l.hit()->wire()->superLayerId()]-1;
426 int ilocal = l.hit()->wire()->localId();
427 int jlocal = s.hit()->wire()->localId();
428 if(ilocal > 0 && ilocal < ilast){
429 if(abs(jlocal-ilocal) > 1 ) {
430 mask[i] = 1;
431 mask[j] = 1;
432 }
433 }else if(ilocal == 0){
434 if(jlocal > 1 && jlocal < ilast){
435 mask[i] = 1;
436 mask[j] = 1;
437 }
438 }else if(ilocal == ilast){
439 if(jlocal > 0 && jlocal < ilast-1){
440 mask[i] = 1;
441 mask[j] = 1;
442 }
443 }
444 }
445 }
446 //...
447 //...
448 if(mask[i] != 1){
449 double phi = Crad*atan2(l.position().y(), l.position().x());
450 phi_ave += phi;
451 nphi += 1;
452 }
453 }
454
455 //...
456 if(mask[n-1] != 1){
457 TMLink & l = * _links[n-1];
458 double phi = Crad*atan2(l.position().y(), l.position().x());
459 phi_ave += phi;
460 nphi += 1;
461 }
462 double phi_max = 0.;
463 double phi_min = 0.;
464 if(nphi> 0){
465 phi_max = phi_ave/n + 40;
466 phi_min = phi_ave/n - 40;
467 }
468
469 //....
470 double sumX = 0., sumY = 0., sumX2 = 0., sumXY = 0., sumY2 = 0.;
471 int nused = 0;
472 int lyid[2];
473 for (unsigned i = 0; i < n; i++) {
474
475 if(mask[i] == 1) continue;
476
477 TMLink & l = * _links[i];
478
479 double x = l.position().x();
480 double y = l.position().y();
481 double phi = Crad*atan2(l.position().y(), l.position().x());
482 if(phi > phi_max && phi<phi_min ) continue;
483
484 sumX += x;
485 sumY += y;
486 sumX2 += x * x;
487 sumXY += x * y;
488 sumY2 += y * y;
489 if(nused < 2){
490 lyid[nused] = l.hit()->wire()->layerId();
491 }
492 nused += 1;
493 }
494
495 if(nused < 2 || (nused == 2 && lyid[0]==lyid[1])) {
496 return -2;
497 }
498 double sum = double(nused);
499 _det = sum * sumX2 - sumX * sumX;
500 if(_det == 0.) {
501 return -1;
502 }
503 _a = (sumXY * sum - sumX * sumY) / _det;
504 _b = (sumX2 * sumY - sumX * sumXY) / _det;
505
506 _fitted = true;
507 return 0;
508}
509
510void
512
513 unsigned n = _links.length();
514 int nlyr[50] = {0};
515 int nneg = 0, npos = 0;
516 for (unsigned i = 0; i < n -1 ; i++) {
517 TMLink & l = * _links[i];
518 nlyr[l.hit()->wire()->layerId()] += 1;
519 if(l.position().y() < 0.){
520 nneg += 1;
521 }else {
522 npos += 1;
523 }
524 }
525
526 //...
527 AList<TMLink> bad;
528 for (unsigned i = 0; i < n; i++) {
529
530 TMLink & l = * _links[i];
531
532 //...if # of hits in a wire layer, don't use...
533 if (nlyr[l.hit()->wire()->layerId()] > 3) {
534 bad.append(l);
535 continue;
536 }
537 //...remove extremely bad poinits
538 if(abs(nneg - npos) > 3) {
539 if(npos > nneg && l.position().y() < 0 ) bad.append(l);
540 if(npos < nneg && l.position().y() > 0 ) bad.append(l);
541 }
542 }
543 //...
544 if (bad.length() > 0 && bad.length() < n) {
545 _links.remove(bad);
546 }
547
548 //... For the next fit
549 _fitted = false;
550 _fittedUpdated = false;
551}
552
553void
555 _links.remove(list);
556 _fitted = false;
557 _fittedUpdated = false;
558}
559
560void
562 _links.append(list);
563 _fitted = false;
564 _fittedUpdated = false;
565}
566
567void
568TMLine::appendByszdistance(AList<TMLink> & list, unsigned isl, float maxSigma) {
569
570 //... intialize
571 unsigned nb = _links.length();
572
573 //....Select good hit
574 unsigned n = list.length();
575 for (unsigned i = 0; i < n; i++) {
576 TMLink & l = * list[i];
577 if(l.hit()->wire()->superLayerId() == isl){
578 double dist = distance(l);
579 if (dist < maxSigma) {
580 _links.append(l);
581 }
582 }
583 }
584
585 unsigned na = _links.length();
586 if(nb != na){
587 AList<TMLink> bad;
588 //... remove duplicated hits
589 for(unsigned i = 0; i<na ;i++){
590 TMLink & l = * _links[i];
591 if(i < na - 1) {
592 TMLink & lnext = * _links[i+1];
593 if(l.hit()->wire()->layerId() == lnext.hit()->wire()->layerId() ){
594 if(l.hit()->wire()->localId() == lnext.hit()->wire()->localId()){
595 bad.append(l);
596 }
597 }
598 }
599 }
600 if(bad.length() > 0) _links.remove(bad);
601 _fitted = false;
602 _fittedUpdated = false;
603 }
604}
605
606double
608#ifdef TRKRECO_DEBUG
609 if (! _fitted)
610 std::cout << "TMLine::reducedChi2 !!! fit not performed" << std::endl;
611#endif
612
613 if (_fittedUpdated) return _reducedChi2;
614 double chi2 = 0.;
615 double scale = 20.;
616 unsigned n = _links.length();
617 for (unsigned i = 0; i < n; i++) {
618 TMLink & l = * _links[i];
619
620 double x = l.position().x();
621 double y = l.position().y();
622 double c = y - _a * x - _b;
623 double err = 1.;
624 if (l.hit()) err = scale * l.hit()->dDrift();
625 chi2 += c * c / err / err;
626 }
627
628 _reducedChi2 = chi2/(n-2);
629 _fittedUpdated = true;
630 return _reducedChi2;
631}
632
633#if defined(__GNUG__)
634
635int
636SortByB(const TMLine ** a, const TMLine ** b) {
637 if (fabs((* a)->b()) > fabs((* b)->b()))
638 return 1;
639 else if (fabs((* a)->b()) == fabs((* b)->b()))
640 return 0;
641 else
642 return -1;
643}
644
645#else
646
647extern "C" int
648SortByB(const void * av, const void * bv) {
649 const TMLine ** a((const TMLine **) av);
650 const TMLine ** b((const TMLine **) bv);
651 if (fabs((* a)->b()) > fabs((* b)->b()))
652 return 1;
653 else if (fabs((* a)->b()) == fabs((* b)->b()))
654 return 0;
655 else
656 return -1;
657}
658
659#endif
const Int_t n
Double_t x[10]
XmlRpcServer s
Definition: HelloServer.cpp:11
int SortByB(const void *av, const void *bv)
Sorter.
Definition: TMLine.cxx:648
A class to fit a TTrackBase object to a line.
const TTrackHEP *const hep(void) const
returns a pointer to a GEN_HEPEVT.
float dDrift(unsigned) const
returns drift distance error.
const TMDCWire *const wire(void) const
returns a pointer to a TMDCWire.
const TMDCWireHitMC *const mc(void) const
returns a pointer to TMDCWireHitMC.
unsigned localId(void) const
returns local id in a wire layer.
unsigned layerId(void) const
returns layer id.
unsigned superLayerId(void) const
returns super layer id.
A class to represent a track in tracking.
double chi2(void) const
returns chi2.
Definition: TMLine.cxx:113
void removeChits()
remove extremly bad points.
Definition: TMLine.cxx:511
void removeSLY(AList< TMLink > &list)
Definition: TMLine.cxx:554
int fit2()
fits itself. Error was happened if return value is not zero.
Definition: TMLine.cxx:174
void refine(AList< TMLink > &list, float maxSigma)
remove bad points by chi2. Bad points are returned in a 'list'. fit() should be called before calling...
Definition: TMLine.cxx:135
int fit2s()
fits itself using single hits in a wire-layer. Error was happened if return value is not zero.
Definition: TMLine.cxx:265
virtual ~TMLine()
Destructor.
Definition: TMLine.cxx:49
double distance(const TMLink &) const
returns distance to a position of TMLink itself. (not to a wire)
void appendByszdistance(AList< TMLink > &list, unsigned isl, float maxSigma)
Definition: TMLine.cxx:568
int fit2p()
fits itself using isolated hits. Error was happened if return value is not zero.
Definition: TMLine.cxx:403
int fit2sp()
fits itself using single hits in a wire-layer. Error was happened if return value is not zero.
Definition: TMLine.cxx:325
void dump(const std::string &message=std::string(""), const std::string &prefix=std::string("")) const
dumps debug information.
Definition: TMLine.cxx:53
TMLine()
Constructor.
Definition: TMLine.cxx:23
void appendSLY(AList< TMLink > &list)
Definition: TMLine.cxx:561
double reducedChi2(void) const
returns reduced-chi2.
Definition: TMLine.cxx:607
A virtual class for a track class in tracking.
virtual void dump(const std::string &message=std::string(""), const std::string &prefix=std::string("")) const
dumps debug information.
Definition: TTrackBase.cxx:58
const TMFitter *const fitter(void) const
returns a pointer to a default fitter.
unsigned id(void) const
returns an id started from 0.
int t()
Definition: t.c:1