BOSS 7.0.3
BESIII Offline Software System
Loading...
Searching...
No Matches
TSegment.cxx
Go to the documentation of this file.
1//-----------------------------------------------------------------------------
2// $Id: TSegment.cxx,v 1.16 2010/03/31 09:58:59 liucy Exp $
3//-----------------------------------------------------------------------------
4// Filename : TSegment.cc
5// Section : Tracking
6// Owner : Yoshi Iwasaki
7// Email : [email protected]
8//-----------------------------------------------------------------------------
9// Description : A class to manage a group of TMLink's.
10// See http://bsunsrv1.kek.jp/~yiwasaki/tracking/
11//-----------------------------------------------------------------------------
12
13#include "TrkReco/TTrack.h"
14#include "TrkReco/TSegment.h"
15#include "TrkReco/TMDCUtil.h"
16#include "TrkReco/TMLink.h"
17#include "TrkReco/TMDC.h"
18#include "TrkReco/TMDCWire.h"
19#include "TrkReco/TMDCWireHit.h"
20//#include "cdc/Range.h"
21#include "TrkReco/Range.h"
22
23#include "CLHEP/Vector/ThreeVector.h"
24#include <math.h>
25#include "CLHEP/Matrix/Vector.h"
26
27TMDC *
28TSegment::_cdc = 0;
29
31: TTrackBase(),
32 _innerWidth(0),
33 _outerWidth(0),
34 _nLayer(0),
35 _clusterType(0),
36 _duality(0.),
37 _nDual(0),
38 _angle(0.),
39 _state(0),
40 _lineTsf(0.,0.,0.) {
41// _times = 4.0;
42// if (_links[0]->wire()->stereo()) _times = 7.0;
43 _times[0] = 7;
44 _times[1] = 7;
45 _times[2] = 4;
46 _times[3] = 4;
47 _times[4] = 4;
48 _times[5] = 5;
49 _times[6] = 5;
50 _times[7] = 5;
51 _times[8] = 5;
52 _times[9] = 4;
53 _times[10] = 4;
54 _fitted = false;
55}
56
58: TTrackBase(a),
59 _innerWidth(0),
60 _outerWidth(0),
61 _nLayer(0),
62 _clusterType(0),
63 _duality(0.),
64 _nDual(0),
65 _angle(0.),
66 _state(0),
67 _lineTsf(0.,0.,0.) {
68 _links.sort(SortByWireId);
69// _times = 4.0;
70// if (_links[0]->wire()->stereo()) _times = 7.0;
71 _times[0] = 7;
72 _times[1] = 7;
73 _times[2] = 4;
74 _times[3] = 4;
75 _times[4] = 4;
76 _times[5] = 5;
77 _times[6] = 5;
78 _times[7] = 5;
79 _times[8] = 5;
80 _times[9] = 4;
81 _times[10] = 4;
82 _fitted = false;
83} //the above two innitial function are temporary for tsf!!!
84
85/*TSegment::TSegment()
86: TTrackBase(),
87 _innerWidth(0),
88 _outerWidth(0),
89 _nLayer(0),
90 _clusterType(0),
91 _duality(0.),
92 _nDual(0),
93 _angle(0.),
94 _state(0) {
95 _fitted = false;
96}
97
98TSegment::TSegment(const AList<TMLink> & a)
99: TTrackBase(a),
100 _innerWidth(0),
101 _outerWidth(0),
102 _nLayer(0),
103 _clusterType(0),
104 _duality(0.),
105 _nDual(0),
106 _angle(0.),
107 _state(0) {
108 _links.sort(SortByWireId);
109 _fitted = false;
110}
111*/
113}
114
115void
116TSegment::dump(const std::string & msg, const std::string & pre) const {
117 if (! _fitted) update();
118 bool def = false;
119 if (msg == "") def = true;
120
121 if (def || msg.find("cluster") != std::string::npos || msg.find("detail") != std::string::npos) {
122 std::cout << pre;
123 std::cout << "#links=" << _links.length();
124 std::cout << "(" << _innerWidth << "," << _outerWidth << ":";
125 std::cout << clusterType() << ")," << _nDual << "," << _duality << ",";
126 std::cout << _angle << std::endl;
127 }
128 if (def || msg.find("vector") != std::string::npos || msg.find("detail") != std::string::npos) {
129 std::cout << pre;
130 std::cout << "pos" << _position << "," << "dir" << _direction;
131 std::cout << std::endl;
132 }
133 if (def || msg.find("state") != std::string::npos || msg.find("detail") != std::string::npos) {
134 std::cout << pre;
135 std::cout << "state=" << _state << std::cout << std::endl;
136 }
137 if (def || msg.find("link") != std::string::npos || msg.find("detail") != std::string::npos) {
138 std::cout << pre;
139 std::cout << "unique links=" << NUniqueLinks(* this);
140 std::cout << ",major links=" << NMajorLinks(* this);
141 std::cout << ",branches=" << NLinkBranches(* this);
142 std::cout << std::endl;
143 }
144 if (! def) TTrackBase::dump(msg, pre);
145}
146
147void
148TSegment::update(void) const {
149 _clusterType = 0;
150 _position = ORIGIN;
151 _direction = ORIGIN;
152 _outerPosition = ORIGIN;
153 _inners.removeAll();
154 _outers.removeAll();
155
156 if (_links.length() == 0) return; //tmp for tsf
157// if (_links.length() < 3) return;
158
159 _innerMostLayer = _links[0]->wire()->layerId();
160 _outerMostLayer = _innerMostLayer;
161 for (unsigned i = 1; i < _links.length(); i++) {
162 unsigned id = _links[i]->wire()->layerId();
163 if (id < _innerMostLayer) _innerMostLayer = id;
164 else if (id > _outerMostLayer) _outerMostLayer = id;
165 }
166 _nLayer = _outerMostLayer - _innerMostLayer + 1;
167
168 double centerX = _links[0]->position().x();
169 HepPoint3D inner = ORIGIN;
170 unsigned nInner = 0;
171 unsigned nOuter = 0;
172 unsigned n = _links.length();
173 for (unsigned i = 0; i < n; i++) {
174 const HepPoint3D & tmp = _links[i]->position();
175/*//--------------------------------tsf---------------------------------//
176 const HepPoint3D & p = _links[i]->positionD();
177 double d = _links[i]->cDrift();
178 HepPoint3D posOnLine = closestPoint(p, _lineTsf);
179 HepPoint3D v = (posOnLine - p).unit();
180 const HepPoint3D tmp = p + d*v;
181*///--------------------------------tsf---------------------------------//
182 _position += tmp;
183 unsigned id = _links[i]->wire()->layerId();
184 if (id == _innerMostLayer) {
185 inner += tmp;
186 ++nInner;
187 _inners.append(_links[i]);
188 }
189 if (id == _outerMostLayer) {
190 _outerPosition += tmp;
191 ++nOuter;
192 _outers.append(_links[i]);
193 }
194 }
195// TTrackBase::dump("hits");
196// std::cout << "0:nin,nout=" << nInner << "," << nOuter << std::endl;
197// std::cout << "0:in,out=" << inner << ", " << _outerPosition << std::endl;
198// std::cout << "0:npos=" << n << std::endl;
199// std::cout << "0:pos=" << _position << std::endl;
200
201 _innerWidth = Width(_inners);
202 _outerWidth = Width(_outers);
203 _position *= (1. / float(n)); //tmp for tsf
204
205 inner *= (1. / (float) nInner);
206 _outerPosition *= (1. / (float) nOuter);
207 _direction = (inner - _outerPosition).unit(); //tmp for tsf
208
209/*//----------------------liuqg add for tsf-----------------------//
210 if (_links.length() > 3) {
211 _position = ORIGIN;
212 _direction = ORIGIN;
213 HepPoint3D nearLine;
214 nearLine = _lineTsf;
215// nearLine = leastSquaresFitting1(_links, _lineTsf);
216// _lineTsf = nearLine;
217 if (nearLine.z() != 0) {
218 HepPoint3D lineVector; //exchange line to vector.
219 lineVector.set(-nearLine.y()/nearLine.z(), nearLine.x()/nearLine.z(), 0.);
220 _direction = (lineVector).unit();
221 }
222 else cout<<"nearLine.z == 0"<<endl;
223 _position = positionTsf(_links, nearLine);
224 //cout<<"pos of seg .. = "<<_position<<endl;
225 //cout<<"dir of seg .. = "<<_direction<<endl;
226 }
227*///----------------------liuqg add for tsf-----------------------//
228
229// std::cout << "1:in,out=" << inner << ", " << _outerPosition << std::endl;
230// std::cout << "1:dir=" << _direction << std::endl;
231// std::cout << "1:pos=" << _position << std::endl;
232
233 _fitted = true;
234}
235
236double
237TSegment::distance(const TSegment & c) const {
238 HepVector3D dir = c.position() - _position;
239 if (dir.x() > M_PI) dir.setX(dir.x() - 2. * M_PI);
240 else if (dir.x() < - M_PI) dir.setX(2. * M_PI + dir.x());
241
242 float radial = fabs(_direction.dot(dir));
243 float radial2 = radial * radial;
244
245 return (dir.mag2() - radial2) > 0.0 ? sqrt(dir.mag2() - radial2) : 0.;
246}
247
248double
249TSegment::distance(const HepPoint3D & p, const HepVector3D & v) const {
250 HepVector3D dir = _position - p;
251 if (dir.x() > M_PI) dir.setX(dir.x() - 2. * M_PI);
252 else if (dir.x() < - M_PI) dir.setX(2. * M_PI + dir.x());
253
254 float radial = fabs(v.unit().dot(dir));
255 float radial2 = radial * radial;
256
257 return (dir.mag2() - radial2) > 0.0 ? sqrt(dir.mag2() - radial2) : 0.;
258}
259
260Range
261TSegment::rangeX(double min, double max) const {
262#ifdef TRKRECO_DEBUG_DETAIL
263 if (min > max) {
264 std::cout << "TSegment::range !!! bad arguments:min,max=";
265 std::cout << min << "," << max << std::endl;
266 }
267#endif
268
269 unsigned n = _links.length();
270 if (n == 0) return Range(0., 0.);
271
272 //...Search for a center...
273 bool found = false;
274 double center;
275 for (unsigned i = 0; i < n; i++) {
276 double x = _links[i]->position().x();
277 if (x < min || x > max) continue;
278 center = x;
279 found = true;
280 break;
281 }
282 if (! found) return Range(0., 0.);
283
284#ifdef TRKRECO_DEBUG_DETAIL
285// std::cout << " center=" << center << std::endl;
286#endif
287
288 double distanceR = 0.;
289 double distanceL = 0.;
290 double distanceMax = max - min;
291 for (unsigned i = 0; i < n; i++) {
292 double x = _links[i]->position().x();
293 if (x < min || x > max) continue;
294
295 double distance0, distance1;
296 if (x > center) {
297 distance0 = x - center;
298 distance1 = distanceMax - distance0;
299 }
300 else {
301 distance1 = center - x;
302 distance0 = distanceMax - distance1;
303 }
304
305 if (distance0 < distance1) {
306 if (distance0 > distanceR) distanceR = distance0;
307 }
308 else {
309 if (distance1 > distanceL) distanceL = distance1;
310 }
311
312#ifdef TRKRECO_DEBUG_DETAIL
313// std::cout << " ";
314// std::cout << _links[i]->wire()->layerId() << "-";
315// std::cout << _links[i]->wire()->localId() << ",";
316// std::cout << _links[i]->position().x();
317// std::cout << ",0,1=" << distance0 << "," << distance1;
318// std::cout << ",l,r=" << distanceL << "," << distanceR;
319// std::cout << std::endl;
320#endif
321 }
322
323 double right = center + distanceR;
324 double left = center - distanceL;
325
326 return Range(left, right);
327}
328
329void
330TSegment::updateType(void) const {
331 if (! nLinks()) return;
332 if (! _fitted) update();
333
334 //...Parameter...
335 unsigned fat = 3;
336 unsigned tall = 3;
337// unsigned tall = 2;
338
339 //...Calculate
340 updateDuality();
341
342 //...Long or short...
343 if ((_innerWidth < fat) && (_outerWidth < fat)) {
344 _clusterType = 1;
345
346 //...Check length across a super layer...
347 if (_nLayer > tall) _clusterType = 2;
348 return;
349 }
350
351 //...A...
352 else if (_outerWidth < fat) {
353 _clusterType = 3;
354 return;
355 }
356
357 //...V...
358 else if (_innerWidth < fat) {
359 _clusterType = 4;
360 return;
361 }
362
363 //...X or parallel...
364 else {
365 bool space = true;
366 for (unsigned i = _innerMostLayer; i <= _outerMostLayer; i++) {
367 unsigned n = 0;
368 AList<TMLink> tmp;
369 for (unsigned j = 0; j < _links.length(); j++)
370 if (_links[j]->wire()->layerId() == i) {
371 ++n;
372 tmp.append(_links[j]);
373 }
374
375 if (n == Width(tmp)) {
376 space = false;
377 break;
378 }
379 }
380
381 _clusterType = 5;
382 if (space) _clusterType = 6;
383 return;
384 }
385}
386
388TSegment::split(void) const {
389 AList<TSegment> list;
390
391 //...Do not split if cluster type is 1, 2, or 7...
392 unsigned t = clusterType();
393#ifdef TRKRECO_DEBUG_DETAIL
394 std::cout << " ... splitting : type=" << t << std::endl;
395#endif
396 if (t == 0) return list;
397 else if (t == 2) {
398 // beta 5
399 //if (_nDual > 2 && _duality > 0.7 && _angle > 0.7) return splitDual();
400
401 // 1.67g
402 // if (_nDual > 2 && _duality > 0.7) return splitDual();
403
404 if (_nDual > 2 && _duality > 0.7 && _angle > 0.7) return splitDual();
405 return list;
406 }
407 else if (t == 1) return list;
408 else if (t == 7) return list;
409
410 //...Parallel...
411 else if (t == 6) return splitParallel();
412 // else if (t == 6) return list;
413
414 //...Avoid splitting of X or parallel...(future implementation)...
415 else if (t > 4) return splitComplicated();
416
417 //...A or V...
418 return splitAV();
419}
420
422TSegment::splitAV(void) const {
423 unsigned t = clusterType();
424 AList<TMLink> seeds[2];
425
426 //...Calculate corner of V or A...
427 const AList<TMLink> * corners;
428 if (t == 3) corners = & _outers;
429 else corners = & _inners;
430 HepPoint3D corner;
431 for (unsigned i = 0; i < corners->length(); i++)
432 corner += (* corners)[i]->wire()->xyPosition();
433 corner *= 1. / (float) corners->length();
434 seeds[0].append(* corners);
435 seeds[1].append(* corners);
436
437 //...Calculdate edges...
438 const AList<TMLink> * links;
439 if (t == 3) links = & _inners;
440 else links = & _outers;
441 AList<TMLink> edge = Edges(* links);
442 HepPoint3D edgePos[2];
443 HepVector3D v[2];
444 for (unsigned i = 0; i < 2; i++) {
445 edgePos[i] = edge[i]->wire()->xyPosition();
446 v[i] = (edgePos[i] - corner).unit();
447 }
448 seeds[0].append(edge[0]);
449 seeds[1].append(edge[1]);
450
451#ifdef TRKRECO_DEBUG_DETAIL
452 std::cout << " corner:" << corner << std::endl;
453 std::cout << " edge:" << edgePos[0] << "(";
454 std::cout << edge[0]->wire()->layerId() << "-";
455 std::cout << edge[0]->wire()->localId() << ")";
456 std::cout << v[0] << std::endl;
457 std::cout << " ";
458 std::cout << edgePos[1] << "(";
459 std::cout << edge[1]->wire()->layerId() << "-";
460 std::cout << edge[1]->wire()->localId() << ")";
461 std::cout << v[1] << std::endl;
462#endif
463
464 //...Examine each hits...
465 unsigned n = _links.length();
466 for (unsigned i = 0; i < n; i++) {
467 TMLink * l = _links[i];
468 if (edge.member(l)) continue;
469 if (corners->member(l)) continue;
470
471 HepVector3D p = l->wire()->xyPosition() - corner;
472 double p2 = p.mag2();
473
474 double dist[2];
475 for (unsigned j = 0; j < 2; j++) {
476 dist[j] = v[j].dot(p);
477 double d2 = dist[j] * dist[j];
478 dist[j] = (p2 - d2) > 0. ? sqrt(p2 - d2) : 0.;
479 }
480 if (dist[0] < dist[1]) seeds[0].append(l);
481 else seeds[1].append(l);
482
483#ifdef TRKRECO_DEBUG_DETAIL
484 std::cout << " p:" << p << std::endl;
485 std::cout << " " << l->wire()->layerId() << "-";
486 std::cout << l->wire()->localId() << ":" << dist[0];
487 std::cout << "," << dist[1] << std::endl;
488#endif
489 }
490
491 AList<TSegment> list;
492 for (unsigned i = 0; i < 2; i++) {
493 if (seeds[i].length()) {
494 TSegment * nc = new TSegment(seeds[i]);
495 AList<TSegment> ncx = nc->split();
496 if (ncx.length() == 0) {
497 nc->solveDualHits();
498 list.append(nc);
499 }
500 else {
501 list.append(ncx);
502 delete nc;
503 }
504 }
505 }
506 return list;
507}
508
510TSegment::splitComplicated(void) const {
511
512 //...Select best hits...
513 AList<TSegment> newClusters;
514 AList<TMLink> goodHits;
515 unsigned n = _links.length();
516 for (unsigned i = 0; i < n; i++) {
517 const TMDCWireHit * h = _links[i]->hit();
518 unsigned state = h->state();
519 if (! (state & WireHitContinuous)) continue;
520 if (! (state & WireHitIsolated)) continue;
521 if ((! (state & WireHitPatternLeft)) &&
522 (! (state & WireHitPatternRight))) continue;
523 goodHits.append(_links[i]);
524 }
525 if (goodHits.length() == 0) return newClusters;
526
527 //...Main loop...
528 goodHits.sort(SortByWireId);
529 AList<TMLink> original(_links);
530 while (goodHits.length()) {
531
532 //...Select an edge hit...
533 TMLink * seed = goodHits.last();
534 const TMDCWire * wire = seed->wire();
535 unsigned localId = wire->localId();
536 AList<TMLink> used;
537 unsigned nn = _links.length();
538 for (unsigned i = 0; i < nn; i++) {
539 TMLink * t = _links[i];
540 const TMDCWire * w = t->wire();
541
542 //...Straight?...
543 if (abs((int) w->localId() - (int) localId) < 2) used.append(t);
544 }
545
546#ifdef TRKRECO_DEBUG_DETAIL
547 std::cout << " seed is " << seed->wire()->name() << std::endl;
548 std::cout << " ";
549 for (unsigned i = 0; i < used.length(); i++) {
550 std::cout << used[i]->wire()->name() << ",";
551 }
552 std::cout << std::endl;
553#endif
554
555 //...Create new cluster...
556 if (used.length() == 0) continue;
557 if (used.length() == nLinks()) return newClusters;
558 TSegment * c = new TSegment(used);
559 AList<TSegment> cx = c->split();
560 if (cx.length() == 0) {
561 c->solveDualHits();
562 newClusters.append(c);
563 }
564 else {
565 newClusters.append(cx);
566 delete c;
567 }
568 goodHits.remove(used);
569 original.remove(used);
570 }
571
572 //...Remainings...
573 if ((original.length()) && (original.length() < nLinks())) {
574 TSegment * c = new TSegment(original);
575 AList<TSegment> cx = c->split();
576 if (cx.length() == 0) {
577 c->solveDualHits();
578 newClusters.append(c);
579 }
580 else {
581 newClusters.append(cx);
582 delete c;
583 }
584 }
585
586 return newClusters;
587}
588
590TSegment::splitParallel(void) const {
591 AList<TMLink> seeds[2];
592 AList<TSegment> newClusters;
593 for (unsigned i = _innerMostLayer; i <= _outerMostLayer; i++) {
594 AList<TMLink> list = SameLayer(_links, i);
595 AList<TMLink> outerList = Edges(list);
596
597#ifdef TRKRECO_DEBUG_DETAIL
598 if (outerList.length() != 2) {
599 std::cout << "TSegment::splitParallel !!! ";
600 std::cout << "This is not a parallel cluster" << std::endl;
601 }
602#endif
603
604 seeds[0].append(outerList[0]);
605 seeds[1].append(outerList[1]);
606 if (list.length() == 2) continue;
607
608 const TMDCWire & wire0 = * outerList[0]->wire();
609 const TMDCWire & wire1 = * outerList[1]->wire();
610 for (unsigned k = 0; k < list.length(); k++) {
611 TMLink * t = list[k];
612 if (t == outerList[0]) continue;
613 if (t == outerList[1]) continue;
614
615 if (abs(wire0.localIdDifference(* t->wire())) <
616 abs(wire1.localIdDifference(* t->wire())))
617 seeds[0].append(t);
618 else
619 seeds[1].append(t);
620 }
621 }
622
623 if ((seeds[0].length()) && (seeds[0].length() < nLinks())) {
624 TSegment * c0 = new TSegment(seeds[0]);
625 AList<TSegment> c0x = c0->split();
626 if (c0x.length()) {
627 newClusters.append(c0x);
628 delete c0;
629 }
630 else {
631 c0->solveDualHits();
632 newClusters.append(c0);
633 }
634 }
635
636 if ((seeds[1].length()) && (seeds[1].length() < nLinks())) {
637 TSegment * c1 = new TSegment(seeds[1]);
638 AList<TSegment> c1x = c1->split();
639 if (c1x.length()) {
640 newClusters.append(c1x);
641 delete c1;
642 }
643 else {
644 c1->solveDualHits();
645 newClusters.append(c1);
646 }
647 }
648
649 return newClusters;
650}
651
652void
653TSegment::updateDuality(void) const {
654 _duality = 0.;
655 _nDual = 0;
656 HepPoint3D x[2];
657 for (unsigned i = _innerMostLayer; i <= _outerMostLayer; i++) {
658 AList<TMLink> list = SameLayer(_links, i);
659 if (i == _innerMostLayer) {
660 for (unsigned j = 0; j < list.length(); j++)
661 x[0] += list[j]->hit()->xyPosition();
662 x[0] *= 1. / float(list.length());
663 }
664 else if (i == _outerMostLayer) {
665 for (unsigned j = 0; j < list.length(); j++)
666 x[1] += list[j]->hit()->xyPosition();
667 x[1] *= 1. / float(list.length());
668 }
669
670 if (list.length() == 2) {
671 if (Width(list) != 2) continue;
672 const TMDCWireHit * h0 = list[0]->hit();
673 const TMDCWireHit * h1 = list[1]->hit();
674
675 double distance = (h0->xyPosition() - h1->xyPosition()).mag();
676 distance = fabs(distance - list[0]->drift() - list[1]->drift());
677 _duality += distance;
678 ++_nDual;
679 }
680 }
681 if (_nDual > 0) _duality /= float(_nDual);
682 _angle = (x[1] - x[0]).unit().dot(x[0].unit());
683}
684
686TSegment::splitDual(void) const {
687#ifdef TRKRECO_DEBUG_DETAIL
688 std::cout << " TSegment::splitDual called" << std::endl;
689#endif
690 AList<TMLink> seeds[2];
691 AList<TMLink> unknown;
692 for (unsigned i = _innerMostLayer; i <= _outerMostLayer; i++) {
693 AList<TMLink> list = SameLayer(_links, i);
694
695 if (list.length() == 2) {
696 if (Width(list) == 2) {
697 seeds[0].append(list[0]);
698 seeds[1].append(list[1]);
699 continue;
700 }
701 }
702 unknown.append(list);
703 }
704
705 if (unknown.length() > 0) {
706#ifdef TRKRECO_DEBUG_DETAIL
707 if (seeds[0].length() == 0)
708 std::cout << "TSegment::splitDual !!! no TMLink for seed 0" << std::endl;
709 if (seeds[1].length() == 0)
710 std::cout << "TSegment::splitDual !!! no TMLink for seed 1" << std::endl;
711#endif
712 const HepPoint3D & p0 = seeds[0][0]->xyPosition();
713 const HepPoint3D & p1 = seeds[1][0]->xyPosition();
714 HepVector3D v0 = (seeds[0].last()->xyPosition() - p0).unit();
715 HepVector3D v1 = (seeds[1].last()->xyPosition() - p1).unit();
716
717 for (unsigned i = 0; i < unknown.length(); i++) {
718 TMLink * t = unknown[i];
719 HepVector3D x0 = t->xyPosition() - p0;
720 double d0 = (x0 - (x0.dot(v0) * v0)).mag();
721 HepVector3D x1 = t->xyPosition() - p1;
722 double d1 = (x1 - (x1.dot(v0) * v0)).mag();
723
724 if (d0 < d1) seeds[0].append(t);
725 else seeds[1].append(t);
726 }
727 }
728
729 AList<TSegment> newClusters;
730 newClusters.append(new TSegment(seeds[0]));
731 newClusters.append(new TSegment(seeds[1]));
732 return newClusters;
733}
734
735int
737 updateType();
738 if (_clusterType == 0) return 0;
739 if (_nDual == 0) return 0;
740 update();
741 return 0;
742
743 updateType();
744 if (_clusterType == 0) return 0;
745 if (_nDual == 0) return 0;
746
747 AList<TMLink> seeds;
748 AList<TMLink> duals;
749 for (unsigned i = _innerMostLayer; i <= _outerMostLayer; i++) {
750 AList<TMLink> list = SameLayer(_links, i);
751
752 if (list.length() == 1) {
753 seeds.append(list[0]);
754 }
755 else if (list.length() == 2) {
756 if (Width(list) > 1) {
757 const TMDCWireHit * h0 = list[0]->hit();
758 const TMDCWireHit * h1 = list[1]->hit();
759 double distance = (h0->xyPosition() - h1->xyPosition()).mag();
760 distance = fabs(distance -
761 list[0]->drift() -
762 list[1]->drift());
763 if (distance > 0.5) duals.append(list);
764#ifdef TRKRECO_DEBUG_DETAIL
765 h0->dump("", " ");
766 h1->dump("", " ");
767 std::cout << " lyr=" << i;
768 std::cout << ", duality distance = " << distance << std::endl;
769#endif
770 }
771 }
772 else if (list.length() == 0) {
773 continue;
774 }
775#ifdef TRKRECO_DEBUG_DETAIL
776 else {
777 std::cout << "TSegment::solveDualHits !!! this is not expected 2";
778 std::cout << std::endl;
779 this->dump("cluster hits mc", " ");
780 }
781#endif
782 }
783
784 //...Solve them...
785 if (seeds.length() < 2) return -1;
786 AList<TMLink> outers = InOut(seeds);
787 const HepPoint3D & p = outers[0]->xyPosition();
788 HepVector3D v = (outers[1]->xyPosition() - p).unit();
789 unsigned n = duals.length() / 2;
790 for (unsigned i = 0; i < n; i++) {
791 TMLink * t0 = duals[i * 2 + 0];
792 TMLink * t1 = duals[i * 2 + 1];
793 HepVector3D x0 = t0->xyPosition() - p;
794 HepVector3D x1 = t1->xyPosition() - p;
795 double d0 = (x0 - (x0.dot(v) * v)).mag();
796 double d1 = (x1 - (x1.dot(v) * v)).mag();
797 if (d0 < d1) _links.remove(t1);
798 else _links.remove(t0);
799 }
800 update();
801 return n;
802}
803
804/*
805//#include "tuple/BelleTupleManager.h"
806#include "BesKernel/BesModule.h"
807#include "BesKernel/BesHistogramService.h"
808#include "BesKernel/BesHObject.h"
809
810void
811CheckSegments(const CAList<TSegment> & list) {
812 static bool first = true;
813// static BelleHistogram * hError;
814// static BelleHistogram * hNHeps[11];
815// static BelleHistogram * hNHits[3];
816 BesHistogramService * svc = Framework()->HistogramSvc();
817 static BesHObject * hError;
818 static BesHObject * hNHeps[11];
819 static BesHObject * hNHits[3];
820
821 if (first) {
822 first = false;
823// extern BelleTupleManager * BASF_Histogram;
824// BelleTupleManager * m = BASF_Histogram;
825
826// hError = m->histogram("segment errors", 10, 0, 10, 20000);
827//
828// hNHeps[0] = m->histogram("segment nheps sl 0", 10, 0., 10.);
829// hNHeps[1] = m->histogram("segment nheps sl 1", 10, 0., 10.);
830// hNHeps[2] = m->histogram("segment nheps sl 2", 10, 0., 10.);
831// hNHeps[3] = m->histogram("segment nheps sl 3", 10, 0., 10.);
832// hNHeps[4] = m->histogram("segment nheps sl 4", 10, 0., 10.);
833// hNHeps[5] = m->histogram("segment nheps sl 5", 10, 0., 10.);
834// hNHeps[6] = m->histogram("segment nheps sl 6", 10, 0., 10.);
835// hNHeps[7] = m->histogram("segment nheps sl 7", 10, 0., 10.);
836// hNHeps[8] = m->histogram("segment nheps sl 8", 10, 0., 10.);
837// hNHeps[9] = m->histogram("segment nheps sl 9", 10, 0., 10.);
838// hNHeps[10] = m->histogram("segment nheps sl 10", 10, 0., 10.);
839//
840// hNHits[0] = m->histogram("segment nhits, nheps = 1", 20, 0., 20.);
841// hNHits[1] = m->histogram("segment nhits, nheps = 2", 20, 0., 20.);
842// hNHits[2] = m->histogram("segment nhits, nheps >= 3", 20, 0., 20.);
843//
844 hError = svc -> Book(1,"segment errors", 10, 0, 10, 20000);
845
846 hNHeps[0] = svc -> Book(1,"segment nheps sl 0", 10, 0., 10.);
847 hNHeps[1] = svc -> Book(1,"segment nheps sl 1", 10, 0., 10.);
848 hNHeps[2] = svc -> Book(1,"segment nheps sl 2", 10, 0., 10.);
849 hNHeps[3] = svc -> Book(1,"segment nheps sl 3", 10, 0., 10.);
850 hNHeps[4] = svc -> Book(1,"segment nheps sl 4", 10, 0., 10.);
851 hNHeps[5] = svc -> Book(1,"segment nheps sl 5", 10, 0., 10.);
852 hNHeps[6] = svc -> Book(1,"segment nheps sl 6", 10, 0., 10.);
853 hNHeps[7] = svc -> Book(1,"segment nheps sl 7", 10, 0., 10.);
854 hNHeps[8] = svc -> Book(1,"segment nheps sl 8", 10, 0., 10.);
855 hNHeps[9] = svc -> Book(1,"segment nheps sl 9", 10, 0., 10.);
856 hNHeps[10] = svc -> Book(1,"segment nheps sl 10", 10, 0., 10.);
857
858 hNHits[0] = svc -> Book(1,"segment nhits, nheps = 1", 20, 0., 20.);
859 hNHits[1] = svc -> Book(1,"segment nhits, nheps = 2", 20, 0., 20.);
860 hNHits[2] = svc -> Book(1,"segment nhits, nheps >= 3", 20, 0., 20.);
861
862 std::cout << "CheckSegments ... initialized" << std::endl;
863 return;
864 }
865
866 unsigned n = list.length();
867 for (unsigned i = 0; i < n; i++) {
868 const TSegment & s = * list[i];
869 const AList<TMLink> & links = s.links();
870 unsigned nLinks = links.length();
871 if (nLinks == 0) {
872// hError->accumulate(0.5);
873 hError->Fill(0.5);
874 continue;
875 }
876
877 unsigned sl = links[0]->wire()->superLayerId();
878 unsigned nHeps = s.nHeps();
879// hNHeps[sl]->accumulate((float) nHeps + .5);
880 hNHeps[sl]->Fill((float) nHeps + .5);
881// if (nHeps == 1) hNHits[0]->accumulate((float) nLinks + .5);
882// else if (nHeps == 2) hNHits[1]->accumulate((float) nLinks + .5);
883// else hNHits[2]->accumulate((float) nLinks + .5);
884 if (nHeps == 1) hNHits[0]->Fill((float) nLinks + .5);
885 else if (nHeps == 2) hNHits[1]->Fill((float) nLinks + .5);
886 else hNHits[2]->Fill((float) nLinks + .5);
887
888 }
889}
890
891void
892CheckSegmentLink(const TSegment & base,
893 const TSegment & next,
894 float distance,
895 float dirAngle) {
896
897 static bool first = true;
898 static BelleHistogram * hAngle[2];
899 static BelleHistogram * hAngle1[2];
900 static BelleHistogram * hDistance[2];
901 static BelleHistogram * hDirAngle[2];
902 static BelleHistogram * hDirAngle1[2];
903
904 if (first) {
905 first = false;
906 extern BelleTupleManager * BASF_Histogram;
907 BelleTupleManager * m = BASF_Histogram;
908
909 hAngle[0] = m->histogram("segment correct link, angle",
910 50, -1., 1., 21000);
911 hAngle[1] = m->histogram("segment wrong link, angle", 50, -1., 1.);
912 hAngle1[0] = m->histogram("segment correct link, angle, wide",
913 50, .8, 1.);
914 hAngle1[1] = m->histogram("segment wrong link, angle, wide",
915 50, .8, 1.);
916 hDistance[0] = m->histogram("segment correct link, dist", 50, 0., 1.);
917 hDistance[1] = m->histogram("segment wrong link, dist", 50, 0., 1.);
918 hDirAngle[0] = m->histogram("segment correct link, dir angle",
919 50, -1, 1.);
920 hDirAngle[1] = m->histogram("segment wrong link, dir angle",
921 50, -1, 1.);
922 hDirAngle1[0] = m->histogram("segment correct link, dir angle, wide",
923 50, .8, 1.);
924 hDirAngle1[1] = m->histogram("segment wrong link, dir angle, wide",
925 50, .8, 1.);
926
927 std::cout << "CheckSegmentLink ... initialized" << std::endl;
928 return;
929 }
930
931 const TTrackHEP * const hep0 = base.hep();
932 const TTrackHEP * const hep1 = next.hep();
933
934 float angle = base.direction().dot(next.direction());
935
936 unsigned id = 0;
937 if (hep0 != hep1) id = 1;
938 hAngle[id]->accumulate(angle);
939 hAngle1[id]->accumulate(angle);
940 hDistance[id]->accumulate(distance);
941 hDirAngle[id]->accumulate(dirAngle);
942 hDirAngle1[id]->accumulate(dirAngle);
943}
944*/
945
946unsigned
948 unsigned n = 0;
949 unsigned nList = list.length();
950 for (unsigned i = 0; i < nList; i++) {
951 const AList<TMLink> & links = list[i]->links();
952 for (unsigned j = 0; j < links.length(); j++) {
953 unsigned state = links[j]->hit()->state();
954 if ((! (state & WireHitPatternLeft)) &&
955 (! (state & WireHitPatternRight)))
956 ++n;
957 }
958 }
959 return n;
960}
961
963Links(const TSegment & s, const TTrack & t) {
965
966 const AList<TMLink> & links = s.links();
967 const AList<TMLink> & trackLinks = t.links();
968 unsigned n = links.length();
969 for (unsigned i = 0; i < n; i++) {
970 if (trackLinks.hasMember(links[i]))
971 a.append(links[i]);
972 }
973
974 return a;
975}
976
977unsigned
979 unsigned n = 0;
980 const TSegment * s = & a;
981 while (s) {
982 unsigned nLinks = s->innerLinks().length();
983 if (nLinks != 1) return n;
984 ++n;
985 s = s->innerLinks()[0];
986 }
987 return n;
988}
989
992 AList<TSegment> links;
993 const TSegment * s = & a;
994 while (s) {
995 unsigned nLinks = s->innerLinks().length();
996 if (nLinks != 1) return links;
997 const TSegment * t = s->innerLinks()[0];
998 links.append((TSegment *) t);
999 s = t;
1000 }
1001 return links;
1002}
1003
1004unsigned
1006 unsigned n = 0;
1007 const TSegment * s = & a;
1008 while (s) {
1009 unsigned nLinks = s->innerLinks().length();
1010 if (nLinks == 0) return n;
1011 ++n;
1012 int tmp = 0, ii = 0;
1013 for (int i = 0; i < nLinks; ++i) {
1014 if(s->innerLinks()[i]->links().length() > tmp) {
1015 tmp = s->innerLinks()[i]->links().length();
1016 ii = i;
1017 }
1018 }
1019 s = s->innerLinks()[ii];
1020// s = s->innerLinks()[0]; //tmp for tsf
1021 }
1022 return n;
1023}
1024
1027 AList<TSegment> links;
1028 const TSegment * s = & a;
1029 while (s) {
1030 unsigned nLinks = s->innerLinks().length();
1031 if (nLinks == 0) return links;
1032 int tmp = 0, ii = 0;
1033 for (int i = 0; i < nLinks; ++i) {
1034 if(s->innerLinks()[i]->links().length() > tmp) {
1035 tmp = s->innerLinks()[i]->links().length();
1036 ii = i;
1037 }
1038 }
1039 const TSegment * t = s->innerLinks()[ii];
1040// const TSegment * t = s->innerLinks()[0]; //tmp for tsf
1041 links.append((TSegment *) t);
1042 s = t;
1043 }
1044 return links;
1045}
1046
1047unsigned
1049 unsigned n = 0;
1050 const TSegment * s = & a;
1051 while (s) {
1052 unsigned nLinks = s->innerLinks().length();
1053 if (nLinks == 0) return n;
1054 if (nLinks > 1) ++n;
1055 s = s->innerLinks()[0];
1056 }
1057 return n;
1058}
1059
1060void
1062 AList<TSegment> & isolated,
1063 AList<TSegment> & crowded) {
1064 unsigned n = in.length();
1065 if (n == 0) return;
1066
1067 for (unsigned i = 0; i < n; i++) {
1068 TSegment & s = * in[i];
1069 if (s.state() & TSegmentCrowd) crowded.append(s);
1070 else isolated.append(s);
1071 }
1072}
1073
1074unsigned
1076 unsigned sl = 0;
1077 unsigned n = list.length();
1078 for (unsigned i = 0; i < n; i++)
1079 sl |= (1 << (list[i]->superLayerId()));
1080 return sl;
1081}
1082
1083TSegment *
1085 const TSegment * o = & a;
1086 while (o->outerLinks().length() == 1)
1087 o = o->outerLinks()[0];
1088 return (TSegment *) o;
1089}
1090
1092Links(const AList<TSegment> & list) {
1093 AList<TMLink> links;
1094 unsigned n = list.length();
1095 for (unsigned i = 0; i < n; i++)
1096 links.append(list[i]->links());
1097 return links;
1098}
1099
1100unsigned
1101TSegment::width(void) const {
1102 unsigned maxWidth = 0;
1103 for (unsigned i = innerMostLayer(); i <= outerMostLayer(); i++) {
1104 AList<TMLink> tmp = SameLayer(links(), i);
1105 unsigned w = Width(tmp);
1106 if (w > maxWidth) maxWidth = w;
1107 }
1108 return maxWidth;
1109}
1110
1111//liuqg add for tsf...................//
1112double
1113TSegment::maxdDistance(TMLink *l) const{
1114 unsigned sl = l->wire()->superLayerId();
1115 unsigned lId = l->wire()->layerId();
1116 double _averR[11] = {9.7, 14.5, 22.14, 28.62, 35.1, 42.39, 48.87, 55.35, 61.83, 69.12, 74.79};
1117 double _averR2[43] = { 7.885, 9.07, 10.29, 11.525,
1118 12.72, 13.875, 15.01, 16.16,
1119 19.7, 21.3, 22.955, 24.555,
1120 26.215, 27.82, 29.465, 31.06,
1121 32.69, 34.265, 35.875, 37.455,
1122 39.975, 41.52, 43.12, 44.76,
1123 46.415, 48.02, 49.685, 51.37,
1124 53.035, 54.595, 56.215, 57.855,
1125 59.475, 60.995, 62.565, 64.165,
1126 66.68, 68.285, 69.915, 71.57,
1127 73.21, 74.76, 76.345};
1128 double radius = _averR2[lId];
1129 const double singleSigma = l->dDrift();
1130 return 2 * singleSigma / (radius * radius);
1131}
1132
1135 //get links in each layer.
1136 AList<TSegment> list;
1137 AList<TMLink> links[4]; //links in each local layer.
1138 AList<TMLink> seeds2; //links in the outer layer.
1139 AList<TMLink> exTsf; //links in other local layers.
1140 TMLink *seed; //link in the mostinner layer.
1141
1142 for (unsigned i = 0; i < _links.length(); ++i) {
1143 TMLink * l = _links[i];
1144 links[l->wire()->localLayerId()].append(l);
1145 }
1146
1147 //prepare for segment finding.
1148 if (links[0].length() == 0) { //find in 234.
1149 if (links[3].length() == 0) return list;
1150 if (links[1].length() != 1) {
1151 cout<<"wrong in tsf, TSegment::splitTsf ...1"<<endl;
1152 return list;
1153 }
1154 seed = links[1][0];
1155 seeds2.append(links[3]);
1156 exTsf.append(links[2]);
1157 }
1158 else if (links[0].length() == 1) { //find in 1234, 124, 134, 123.
1159 seed = links[0][0];
1160 if (links[3].length() > 0) { //1..4
1161 seeds2.append(links[3]);
1162 exTsf.append(links[1]);
1163 exTsf.append(links[2]);
1164 }
1165 else if (links[2].length() > 0) { //1..3
1166 seeds2.append(links[2]);
1167 exTsf.append(links[1]);
1168 }
1169 else return list;
1170 }
1171 else cout<<"wrong in tsf, TSegment::splitTsf ...2"<<endl;
1172 exTsf.append(seeds2); //add the outer layer...
1173
1174 //find segments
1175 for (unsigned i = 0; i < seeds2.length(); ++i) {
1176 if (seed->tsfTag() > 0 && seeds2[i]->tsfTag() > 0) continue;
1177 AList<TSegment> segments;
1178 HepPoint3D line[4];
1179 fitLine(seed, seeds2[i], line);
1180 segments = appendByLine(seed, seeds2[i], seedNeighbors, exTsf, line);
1181 list.append(segments);
1182 segments.removeAll();
1183 }
1184 if (seed->wire()->localLayerId() == 0) { //for 123
1185 exTsf.removeAll();
1186 seeds2.removeAll();
1187 exTsf.append(links[1]);
1188 exTsf.append(links[2]); //add the outer layer...
1189 for (int k = 0; k < links[2].length(); ++k){
1190 if (links[2][k]->tsfTag() == 0) seeds2.append(links[2][k]);
1191 }
1192 for (unsigned i = 0; i < seeds2.length(); ++i) {
1193 if (seed->tsfTag() > 0 && seeds2[i]->tsfTag() > 0) continue;
1194 AList<TSegment> segments2;
1195 HepPoint3D line2[4];
1196 fitLine(seed, seeds2[i], line2);
1197 segments2 = appendByLine(seed, seeds2[i], seedNeighbors, exTsf, line2);
1198 list.append(segments2);
1199 segments2.removeAll();
1200 }
1201 }
1202
1203 return list;
1204}
1205
1206void
1207TSegment::fitLine(TMLink *seed, TMLink *outer, HepPoint3D line[4]) const {
1208 double ax = seed->positionD().x();
1209 double ay = seed->positionD().y();
1210 double ar = seed->cDrift();
1211 double bx = outer->positionD().x();
1212 double by = outer->positionD().y();
1213 double br = outer->cDrift();
1214 //calculate ...
1215 double dis = sqrt((ax - bx) * (ax - bx) + (ay - by) * (ay - by));
1216 double costheta = (bx - ax) / dis;
1217 double sintheta = (by - ay) / dis;
1218 double c1 = (ax * ax - bx * bx + ay * ay - by * by) / (2 * dis);
1219 double c2 = (ax * by - bx * ay) / dis;
1220//cout<<"dis of circles = "<<dis<<" radius of circles N1 = "<<ar<<" N2 = "<<br<<endl;
1221 line[0].setX((br - ar) * costheta + sqrt(dis * dis - (ar - br) * (ar - br)) * sintheta);
1222 line[0].setY((br - ar) * sintheta - sqrt(dis * dis - (ar - br) * (ar - br)) * costheta);
1223 line[0].setZ((br - ar) * c1 - sqrt(dis * dis - (ar - br) * (ar - br)) * c2 + 0.5 * dis * (ar + br));
1224 line[1].setX((br - ar) * costheta - sqrt(dis * dis - (ar - br) * (ar - br)) * sintheta);
1225 line[1].setY((br - ar) * sintheta + sqrt(dis * dis - (ar - br) * (ar - br)) * costheta);
1226 line[1].setZ((br - ar) * c1 + sqrt(dis * dis - (ar - br) * (ar - br)) * c2 + 0.5 * dis * (ar + br));
1227 line[2].setX((br + ar) * costheta + sqrt(dis * dis - (ar + br) * (ar + br)) * sintheta);
1228 line[2].setY((br + ar) * sintheta - sqrt(dis * dis - (ar + br) * (ar + br)) * costheta);
1229 line[2].setZ((br + ar) * c1 - sqrt(dis * dis - (ar + br) * (ar + br)) * c2 + 0.5 * dis * (br - ar));
1230 line[3].setX((br + ar) * costheta - sqrt(dis * dis - (ar + br) * (ar + br)) * sintheta);
1231 line[3].setY((br + ar) * sintheta + sqrt(dis * dis - (ar + br) * (ar + br)) * costheta);
1232 line[3].setZ((br + ar) * c1 + sqrt(dis * dis - (ar + br) * (ar + br)) * c2 + 0.5 * dis * (br - ar));
1233/* for (int i = 0; i < 4; ++i) {
1234 double d1 = distance2(seed, line[i]);
1235 double d2 = distance2(outer, line[i]);
1236 cout<<"dis of line to seed = "<<d1<<" dis to outer = "<<d2<<endl;
1237 cout<<"radius of seed = "<<ar<<" rad to outer = "<<br<<endl;
1238 }
1239*///check the calculation of line. right!!
1240 return;
1241}
1242
1244TSegment::appendByLine(TMLink * seed, TMLink * outer, AList<TMLink> & seedNeighbors, AList<TMLink> & restHits, const HepPoint3D line[4]) {
1245 AList<TSegment> list;
1246 list.removeAll();
1247
1248 const unsigned slId = seed->wire()->superLayerId();
1249
1250//cout<<"seed: lId:"<<seed->wire()->layerId()<<" loId:"<<seed->wire()->localId()<<endl;
1251//cout<<"outer: lId:"<<outer->wire()->layerId()<<" loId:"<<outer->wire()->localId()<<endl;
1252 for (int i = 0; i < 4; ++i) {
1253 AList<TMLink> seeds;
1254 for (int j = 0, size = restHits.length(); j < size; ++j) {
1255 TMLink * l = restHits[j];
1256 if (l->wire()->id() == outer->wire()->id()) continue;
1257//cout<<"restHits: lId:"<<l->wire()->layerId()<<" loId:"<<l->wire()->localId()<<endl;
1258 double maxdis = maxdDistance(l) * _times[slId];
1259 double dis = distance2(l, line[i]);
1260//cout<<"maxdis: "<<maxdis<<" dis: "<<dis<<endl;
1261 if(seeds.hasMember(l)) continue;
1262 if (fabs(dis - l->cDrift()) < maxdis) seeds.append(l);
1263 else continue;
1264 }
1265 seeds.append(seed);
1266 seeds.append(outer);
1267 unsigned nHitsLayer[4] = {0, 0, 0, 0};
1268 unsigned nLayers = 0;
1269 //check...at least 3 layers.
1270 for (int j = 0; j < seeds.length(); ++j)
1271 ++nHitsLayer[seeds[j]->wire()->localLayerId()];
1272 for (int j = 0; j < 4; ++j)
1273 if (nHitsLayer[j] > 0) ++nLayers;
1274 if (nLayers < 3) continue;
1275
1276 //check the neighbor hits in the first locallayer.
1277 for (int k = 0; k < seedNeighbors.length(); ++k) {
1278 TMLink * l =seedNeighbors[k];
1279//cout<<"seedNeighbors: lId:"<<l->wire()->layerId()<<" loId:"<<l->wire()->localId()<<endl;
1280 double maxdis = maxdDistance(l) * _times[slId];
1281 double dis = distance2(l, line[i]);
1282//cout<<"maxdis: "<<maxdis<<" dis: "<<dis<<endl;
1283 if(seeds.hasMember(l)) continue;
1284 if (fabs(dis - l->cDrift()) < maxdis) seeds.append(l);
1285 else continue;
1286 }
1287
1288 //update tag of each link.
1289 for (int k = 0; k < seeds.length(); ++k) {
1290 unsigned a = seeds[k]->tsfTag();
1291 ++a;
1292 seeds[k]->tsfTag(a);
1293 }
1294
1295 //creat segment and update.
1296 TSegment * seg = new TSegment(seeds);
1297 seg->lineTsf(line[i]);
1298 seg->update();
1299
1300 list.append(seg);
1301 }
1302
1303 //salvage in new line...after update
1304// AList<TMLink> all;
1305// all.removeAll();
1306// all.append(restHits);
1307// all.append(seedNeighbors);
1308// for (int j = 0; j < list.length(); ++j)
1309// list[j]->segSalvage(all);
1310
1311 //expand 3layers segment
1312/* unsigned sl = seed->wire()->superLayerId();
1313 if(sl != 10) {
1314 for( int i = 0; i < list.length(); ++i) {
1315 AList<TMLink> segLinks = list[i]->links();
1316 unsigned nHits[4] = {0,0,0,0};
1317 int emptyLayer = -1;
1318 for(int j = 0; j < segLinks.length(); ++j)
1319 ++nHits[segLinks[j]->wire()->localLayerId()];
1320 for (int j = 0; j < 4; ++j) {
1321 if(nHits[j] == 0) emptyLayer = j;
1322 if (emptyLayer != -1) break;
1323 }
1324 if(emptyLayer == -1) continue;
1325
1326 for(int j = 0; j < all.length(); ++j){
1327 TMLink *l = all[j];
1328 if (l->wire()->localLayerId() == emptyLayer){
1329 double maxdis = maxdDistance(l) * _times;
1330 double dis = distance2(l, list[i]->lineTsf());
1331 cout<<"layer: "<<l->wire()->layerId()<<" local: "<<l->wire()->localId()<<endl;
1332 cout<<"maxdis: "<<maxdis<<" dis in empty layer: "<<dis<<endl;
1333 }
1334 }
1335
1336// list[i]->expandSeg(emptyLayer, all);
1337 }
1338 }
1339*/
1340 return list;
1341}
1342
1343double
1344TSegment::distance2(TMLink * hit, HepPoint3D line) const {
1345 double x = hit->positionD().x();
1346 double y = hit->positionD().y();
1347 double a = line.x();
1348 double b = line.y();
1349 double c = line.z();
1350 return fabs(a * x + b * y + c) / sqrt(a * a + b * b);
1351}
1352
1354TSegment::positionTsf(const AList<TMLink> & links, const HepPoint3D line) const {
1355 HepPoint3D sumPos(0., 0., 0.);
1356 int n = 0;
1357 for(int i = 0; i < links.length(); ++i){
1358 const HepPoint3D & p = links[i]->positionD();
1359 double d = links[i]->cDrift();
1360 HepPoint3D posOnLine = closestPoint(p, _lineTsf);
1361 HepPoint3D v = (posOnLine - p).unit();
1362 const HepPoint3D p1 = p + d*v;
1363 sumPos += p1;
1364 ++n;
1365 }
1366 sumPos *= (1. / double(n));
1367 HepPoint3D posTsf = closestPoint(sumPos, line);
1368 return posTsf;
1369}
1370
1372TSegment::leastSquaresFitting1(const AList<TMLink> & links, const HepPoint3D tsfLine) const {
1373 HepPoint3D line0;
1374 line0.setX(tsfLine.x()/sqrt(tsfLine.x()*tsfLine.x()+tsfLine.y()*tsfLine.y()));
1375 line0.setY(tsfLine.y()/sqrt(tsfLine.x()*tsfLine.x()+tsfLine.y()*tsfLine.y()));
1376 line0.setZ(tsfLine.z()/sqrt(tsfLine.x()*tsfLine.x()+tsfLine.y()*tsfLine.y()));
1377
1378 HepPoint3D line;
1379 unsigned n = links.length();
1380 unsigned nTrial = 0;
1381 while(1){
1382 if (nTrial > 10) break;
1383 double a = 0., b = 0.;
1384 double sumXSig = 0., sumYSig = 0., sumX2Sig = 0., sumXYSig = 0., sumSig = 0.;
1385 for (unsigned i = 0; i < n; i++) {
1386 const HepPoint3D & p = links[i]->positionD();
1387 double d = links[i]->cDrift();
1388 HepPoint3D posOnLine = closestPoint(p, _lineTsf);
1389 HepPoint3D v = (posOnLine - p).unit();
1390 const HepPoint3D p1 = p + d*v;
1391 double Sig = maxdDistance(links[i]);
1392 Sig = 1./(Sig*Sig);
1393 double X = p1.x();
1394 double Y = p1.y();
1395 sumXSig += X*Sig;
1396 sumYSig += Y*Sig;
1397 sumX2Sig += X * X * Sig;
1398 sumXYSig += X * Y * Sig;
1399 sumSig += Sig;
1400 }
1401
1402 b = (sumXYSig * sumSig - sumXSig * sumYSig) / (sumX2Sig * sumSig - sumXSig * sumXSig);
1403 a = (sumYSig - sumXSig * b) / sumSig;
1404
1405 line.set(b/sqrt(1+b*b), -1/sqrt(1+b*b), a/sqrt(1+b*b));
1406//cout<<"n: "<<nTrial<<" line = "<<line<<endl;
1407
1408 double chi2 = 0.;
1409 for (unsigned i = 0; i < n; i++) {
1410 const HepPoint3D & p = links[i]->positionD();
1411 double d = links[i]->cDrift();
1412 double Sig = maxdDistance(links[i]);
1413 Sig = 1./(Sig*Sig);
1414 double dis = fabs(p.x()*line.x()+p.y()*line.y()+line.z()) - d;
1415 chi2 += dis * dis * Sig;
1416 }
1417 if (nTrial > 6) {
1418 cout<<"n = "<<n<<" nTrial = "<<nTrial<<" line0 = "<<line0<<endl;
1419 cout<<"chi2 in TSF = "<<chi2<<endl;
1420 }
1421
1422 if (fabs(line.x()-line0.x()) < 0.0001) break;
1423
1424 line0 = line;
1425 ++nTrial;
1426 }//while...
1427 return line;
1428}
1429
1431TSegment::leastSquaresFitting(const AList<TMLink> & links, const HepPoint3D tsfLine) const {
1432 HepPoint3D line0;
1433 line0.setX(tsfLine.x()/sqrt(tsfLine.x()*tsfLine.x()+tsfLine.y()*tsfLine.y()));
1434 line0.setY(tsfLine.y()/sqrt(tsfLine.x()*tsfLine.x()+tsfLine.y()*tsfLine.y()));
1435 line0.setZ(tsfLine.z()/sqrt(tsfLine.x()*tsfLine.x()+tsfLine.y()*tsfLine.y()));
1436
1437// cout<<"tsfLine = "<<tsfLine<<endl;
1438 HepPoint3D line;
1439 int n = links.length();
1440 int nTrial = 0;
1441
1442 while(1){
1443 if (nTrial>15) break;
1444 double a0 = line0.x();
1445 double b0 = line0.y();
1446 double c0 = line0.z();
1447//cout<<"nTrial: "<<nTrial<<" line0"<<a0<<" "<<b0<<" "<<c0<<endl;
1448
1449 if (b0 == 0) {
1450 cout<<"b0 == 0 TSegment::leastSquaresFitting!"<<endl;
1451 break;
1452 }
1453
1454 double a = 0., b = 0., c = 0.;
1455 double sumSigX2 = 0., sumSigX = 0., sumSig = 0.;
1456 for (int i = 0; i < n; ++i) {
1457 const HepPoint3D & p = links[i]->positionD();
1458 double X = p.x();
1459 double Y = p.y();
1460 double Sig = maxdDistance(links[i]);
1461 Sig = 1./(Sig*Sig);
1462 sumSigX2 += Sig*(X-Y*a0/b0)*(X-Y*a0/b0);
1463 sumSigX += Sig*(X-Y*a0/b0);
1464 sumSig += Sig;
1465 }
1466 double detM = sumSigX2*sumSig - sumSigX*sumSigX;
1467 double M11 = sumSig/detM;
1468 double M12 = -sumSigX/detM;
1469 double M21 = M12;
1470 double M22 = sumSigX2/detM;
1471 for (int i = 0; i < n; ++i) {
1472 const HepPoint3D & p = links[i]->positionD();
1473 double X = p.x();
1474 double Y = p.y();
1475 double DRIFT = links[i]->cDrift();
1476 if (a0*p.x() + b0*p.y() + c0*p.z() < 0) DRIFT = (-1) * DRIFT;
1477 double Sig = maxdDistance(links[i]);
1478 Sig = 1./(Sig*Sig);
1479 double D = DRIFT-Y/b0;
1480 a += ((X-Y*a0/b0)*M11+M12)*Sig*D;
1481 c += ((X-Y*a0/b0)*M21+M22)*Sig*D;
1482 }
1483
1484 b = (1 - a0*a)/b0;
1485
1486 line.setX(a/sqrt(a*a+b*b));
1487 line.setY(b/sqrt(a*a+b*b));
1488 line.setZ(c/sqrt(a*a+b*b));
1489 a = line.x();
1490 b = line.y();
1491 c = line.z();
1492// cout<<"line = "<<line<<endl;
1493 double chi2 = 0.;
1494 for (int i = 0; i< n; ++i){
1495 const HepPoint3D & p = links[i]->positionD();
1496 double dis = fabs(a*p.x()+b*p.y()+c);
1497 double DRIFT = links[i]->cDrift();
1498 double Sig = maxdDistance(links[i]);
1499//cout<<"dis: "<<dis<<" DRIFT: "<<DRIFT<<" Sig: "<<Sig<<endl;
1500 Sig = 1./(Sig*Sig);
1501 chi2 += (dis - DRIFT)*(dis - DRIFT)*Sig;
1502 }
1503//cout<<"chi2 = "<<chi2<<endl;
1504
1505 //check...
1506 if(fabs(a-a0)<0.00001) break;
1507 //get line para...
1508 line0 = line;
1509 ++nTrial;
1510/* if(nTrial>9){
1511 cout<<"length of links = "<<n<<endl;
1512 for(int i = 0; i<n; ++i){
1513 const HepPoint3D & p = links[i]->positionD();
1514 cout<<"layer: "<<links[i]->wire()->layerId()<<" localId:"<<links[i]->wire()->localId()<<endl;
1515 cout<<"drift: "<<links[i]->cDrift()<<" dis0: "<<a0*p.x() + b0*p.y() + c0*p.z()
1516 <<" dis1:"<<a*p.x() + b*p.y() + c*p.z()<<endl;
1517 }
1518 }*/
1519 }
1520 return line;
1521}
1522
1523void
1524TSegment::segSalvage(AList<TMLink> & links) {
1525 HepPoint3D line = _lineTsf;
1526 AList<TMLink> segLinks = _links;
1527 for (int i = 0; i < links.length(); ++i) {
1528 TMLink * l = links[i];
1529 if(segLinks.hasMember(l)) continue;
1530 const unsigned slId = l->wire()->superLayerId();
1531 double maxdis = maxdDistance(l) * (_times[slId]);
1532 double dis = distance2(l, line);
1533 if (fabs(dis - l->cDrift()) < maxdis) {
1534 _links.append(l);
1535 unsigned a = l->tsfTag();
1536 ++a;
1537 l->tsfTag(a);
1538 }
1539 else continue;
1540 }
1541 update();
1542}
1543
1544void
1545TSegment::expandSeg(const int empty, AList<TMLink> & allLinks) {
1546 if(empty>=4 || empty<0) return;
1547 AList<TMLink> linksOnEmptyLayer;
1548 for (int i = 0; i < allLinks.length(); ++i)
1549 if (allLinks[i]->wire()->localLayerId() == empty) linksOnEmptyLayer.append(allLinks[i]);
1550
1551 if (linksOnEmptyLayer.length()==0) return;
1552
1553 if (_cdc == 0) _cdc = TMDC::getTMDC();
1554 float maxPhi[4] = {0.,0.,0.,0.};
1555 float minPhi[4] = {999.,999.,999.,999.};
1556 float max = 0., min = 999.;
1557 for (int i = 0; i < _links.length(); ++i) {
1558 TMLink *l = _links[i];
1559 unsigned lId = l->wire()->layerId();
1560 int local = l->wire()->localId();
1561 unsigned loId = l->wire()->localLayerId();
1562 float phi0 = _cdc->layer(lId)->offset();
1563 int nWir = (int) _cdc->layer(lId)->nWires();
1564 float phi = phi0 + local*2*pi/nWir;
1565
1566 if (phi > maxPhi[loId]) maxPhi[loId] = phi;
1567 if (phi < minPhi[loId]) minPhi[loId] = phi;
1568 if (phi > max) max = phi;
1569 if (phi < min) min = phi;
1570 }
1571
1572 AList<TMLink> tmpList;
1573 for (int i = 0; i < linksOnEmptyLayer.length(); ++i) {
1574 TMLink *l = linksOnEmptyLayer[i];
1575 unsigned lId = l->wire()->layerId();
1576 int local = l->wire()->localId();
1577 float phi0 = _cdc->layer(lId)->offset();
1578 int nWir = (int) _cdc->layer(lId)->nWires();
1579 float phi = phi0 + local*2*pi/nWir;
1580
1581 if (empty != 0 || empty != 3) {
1582 if (phi <= maxPhi[empty + 1] && phi >= minPhi[empty - 1]) tmpList.append(l);
1583 else if (phi <= maxPhi[empty - 1] && phi >= minPhi[empty + 1]) tmpList.append(l);
1584 else if (tmpList.length() == 0 && phi <= max && phi >= min) tmpList.append(l);
1585 }
1586 if (empty == 0 || empty == 3) {
1587 if (phi <= maxPhi[1] && phi >= minPhi[2]) tmpList.append(l);
1588 else if (phi <= maxPhi[2] && phi >= minPhi[1]) tmpList.append(l);
1589 else if (tmpList.length() == 0 && phi <= max && phi >= min) tmpList.append(l);
1590 }
1591 }
1592
1593 for(int i = 0; i < tmpList.length(); ++i) {
1594 TMLink * l = tmpList[i];
1595 _links.append(l);
1596 unsigned a = l->tsfTag();
1597 ++a;
1598 l->tsfTag(a);
1599 }
1600 update();
1601}
1602
1603void
1605 unsigned n = _links.length();
1606 //initial...
1607 if (n <= 3) {
1609 return;
1610 }
1611 for (int i = 0; i < n; ++i) {
1612 TMLink *l = _links[i];
1613 if (l->hit()->state() & WireHitPatternRight){
1614 unsigned newState = l->hit()->state()&(~WireHitPatternRight);
1615 l->hit()->state(newState);
1616 }
1617 if (l->hit()->state() & WireHitPatternLeft){
1618 unsigned newState = l->hit()->state()&(~WireHitPatternLeft);
1619 l->hit()->state(newState);
1620 }
1621 }
1622
1623 HepPoint3D originCon(0., 0., 0.);
1624 HepPoint3D dirZ(0., 0., 1.);
1625 HepPoint3D center = closestPoint(originCon, _lineTsf);
1626 HepPoint3D v1 = (center - originCon).unit();
1627 HepPoint3D v2 = dirZ.cross(v1);
1628 for (int i = 0; i < n; ++i) {
1629// cout<<"layerId: "<<_links[i]->wire()->layerId()<<" localId: "<<_links[i]->wire()->localId()<<endl;
1630 const HepPoint3D & p = _links[i]->positionD();
1631 unsigned state = _links[i]->hit()->state();
1632
1633 double cosA = (p - center).unit().dot(v1.unit());
1634 double cosB = (p - center).unit().dot(v2.unit());
1635 if (cosA*cosB < 0) state |= WireHitPatternLeft;
1636 else state |= WireHitPatternRight;
1637
1638 _links[i]->hit()->state(state);
1639 }
1640}
1641
1643TSegment::closestPoint(const HepPoint3D & p, const HepPoint3D line) const {
1644 HepPoint3D Dir(-line.y(), line.x(), 0);
1645 Dir = Dir.unit();
1646 HepPoint3D PointOnLine(1, -(line.x()+line.z())/line.y(), 0);
1647 double dis = (p - PointOnLine).dot(Dir);
1648 HepPoint3D tmp(dis*Dir.x(), dis*Dir.y(), 0.);
1649 HepPoint3D closestPoint = PointOnLine + tmp;
1650
1651 double disPLine = fabs(p.x()*line.x()+p.y()*line.y()+line.z())/sqrt(line.x()*line.x()+line.y()*line.y());
1652 double disP1P2 = sqrt((p.x()-closestPoint.x())*(p.x()-closestPoint.x())+(p.y()-closestPoint.y())*(p.y()-closestPoint.y()));
1653 if (fabs(disPLine-disP1P2)>0.00001) cout<<"TSegment::closestPoint: dis p -> line = "<<disPLine<<" dis p -> pos = "<<disP1P2<<endl;
1654 return closestPoint;
1655}
1656
1657void
1659 unsigned n = _links.length();
1660
1661 for (unsigned i = 0; i < n; i++) {
1662 const TMDCWireHit * h = _links[i]->hit();
1663 const TMDCWire * w = h->wire();
1664 unsigned state = h->state();
1665
1666 //...Cache pointers to a neighbor...
1667 const TMDCWire * neighbor[6];
1668 for (unsigned j = 0; j < 6; j++) neighbor[j] = w->neighbor(j);
1669
1670 //...Decide hit pattern...
1671 unsigned pattern = 0;
1672 for (unsigned j = 0; j < 6; j++) {
1673 if (neighbor[j])
1674 if (neighbor[j]->hit())
1675 pattern += (1 << j);
1676 }
1677 state |= (pattern << WireHitNeighborHit);
1678
1679 //...Solve LR locally...
1680 if ((pattern == 34) || (pattern == 42) ||
1681 (pattern == 40) || (pattern == 10) ||
1682 (pattern == 35) || (pattern == 50))
1684 else if ((pattern == 17) || (pattern == 21) ||
1685 (pattern == 20) || (pattern == 5) ||
1686 (pattern == 19) || (pattern == 49))
1688
1689 //...Store it...
1690 h->state(state);
1691 }
1692}
1693
1694//............................tsf//
const Int_t n
Double_t x[10]
double w
*******INTEGER m_nBinMax INTEGER m_NdiMax !No of bins in histogram for cell exploration division $ !Last vertex $ !Last active cell $ !Last cell in buffer $ !No of sampling when dividing cell $ !No of function total $ !Flag for random ceel for $ !Flag for type of for WtMax $ !Flag which decides whether vertices are included in the sampling $ entire domain is hyp !Maximum effective eevents per saves r n generator level $ !Flag for chat level in !Latex Output unit
Definition: FoamA.h:90
XmlRpcServer s
Definition: HelloServer.cpp:11
const HepPoint3D ORIGIN
Constants.
Definition: TMDCUtil.cxx:47
**********Class see also m_nmax DOUBLE PRECISION m_amel DOUBLE PRECISION m_x2 DOUBLE PRECISION m_alfinv DOUBLE PRECISION m_Xenph INTEGER m_KeyWtm INTEGER m_idyfs DOUBLE PRECISION m_zini DOUBLE PRECISION m_q2 DOUBLE PRECISION m_Wt_KF DOUBLE PRECISION m_WtCut INTEGER m_KFfin *COMMON c_KarLud $ !Input CMS energy[GeV] $ !CMS energy after beam spread beam strahlung[GeV] $ !Beam energy spread[GeV] $ !z boost due to beam spread $ !electron beam mass *ff pair spectrum $ !minimum v
Definition: KarLud.h:35
unsigned NMajorLinks(const TSegment &a)
returns # of links in the major link.
Definition: TSegment.cxx:1005
unsigned NUniqueLinks(const TSegment &a)
checks property of segments.
Definition: TSegment.cxx:978
unsigned NLinkBranches(const TSegment &a)
returns # of link branches in the major link.
Definition: TSegment.cxx:1048
#define M_PI
Definition: TConstant.h:4
unsigned NMajorLinks(const TSegment &a)
returns # of links in the major link.
Definition: TSegment.cxx:1005
unsigned NUniqueLinks(const TSegment &a)
checks property of segments.
Definition: TSegment.cxx:978
TSegment * OuterMostUniqueLink(const TSegment &a)
returns a segment to the outer-most unique segment.
Definition: TSegment.cxx:1084
unsigned NLinkBranches(const TSegment &a)
returns # of link branches in the major link.
Definition: TSegment.cxx:1048
unsigned NCoreLinks(const CAList< TSegment > &list)
returns # of core links in segments.
Definition: TSegment.cxx:947
AList< TSegment > UniqueLinks(const TSegment &a)
returns a list of unique segments in links.
Definition: TSegment.cxx:991
AList< TSegment > MajorLinks(const TSegment &a)
returns a list of segments in major links.
Definition: TSegment.cxx:1026
AList< TMLink > Links(const TSegment &s, const TTrack &t)
returns AList of TMLink used for a track.
Definition: TSegment.cxx:963
unsigned SuperLayer(const AList< TSegment > &list)
returns super layer pattern.
Definition: TSegment.cxx:1075
void SeparateCrowded(const AList< TSegment > &in, AList< TSegment > &isolated, AList< TSegment > &crowded)
returns isolated and crowded list.
Definition: TSegment.cxx:1061
to specify 1-dim region or range by two floats
float offset(void) const
returns offset.
unsigned nWires(void) const
returns # of wires.
void dump(const std::string &message=std::string(""), const std::string &prefix=std::string("")) const
dumps debug information.
Definition: TMDCWireHit.cxx:64
unsigned state(void) const
returns state.
const TMDCWire *const wire(void) const
returns a pointer to a TMDCWire.
const HepPoint3D & xyPosition(void) const
returns drift time
A class to represent a wire in MDC.
unsigned id(void) const
returns id.
unsigned localLayerId(void) const
returns local layer id in a super layer.
unsigned localId(void) const
returns local id in a wire layer.
unsigned layerId(void) const
returns layer id.
const HepPoint3D & xyPosition(void) const
returns middle position of a wire. z componet is 0.
unsigned superLayerId(void) const
returns super layer id.
int localIdDifference(const TMDCWire &) const
returns local id difference.
Definition: TMDCWire.cxx:577
std::string name(void) const
returns name.
static TMDC * getTMDC(void)
Definition: TMDC.cxx:95
const TMDCLayer *const layer(unsigned id) const
returns a pointer to a layer. 0 will be returned if 'id' is invalid.
A class to relate TMDCWireHit and TTrack objects.
void dump(const std::string &message=std::string(""), const std::string &prefix=std::string("")) const
dumps debug information.
Definition: TSegment.cxx:116
const TMLink & center(void) const
returns a TMLink which is the closest to the center.
const AList< TMLink > & outers(void) const
void solveThreeHits(void)
Definition: TSegment.cxx:1658
TSegment()
Constructor.
Definition: TSegment.cxx:30
Range rangeX(double min, double max) const
returns Range of x-coordinate of TMLinks.
Definition: TSegment.cxx:261
virtual ~TSegment()
Destructor.
Definition: TSegment.cxx:112
AList< TSegment > split(void) const
returns a list of sub TSegments in this cluster. If cluster type is 1, 2, or 7, no cluster is returne...
Definition: TSegment.cxx:388
AList< TSegment > splitTsf(AList< TMLink > &)
Definition: TSegment.cxx:1134
unsigned innerMostLayer(void) const
returns inner most layer.
unsigned clusterType(void) const
returns cluster type. 0:empty, 1:short line, 2:long line, 3:V shage(from outside),...
void solveLR(void)
solve LR of hit in TSF.
Definition: TSegment.cxx:1604
int solveDualHits(void)
Definition: TSegment.cxx:736
AList< TSegment > & outerLinks(void)
const HepPoint3D & position(void) const
returns position.
unsigned width(void) const
returns width.
Definition: TSegment.cxx:1101
double distance(const TSegment &) const
calculates distance between two clusters. Smaller value indicates closer.
Definition: TSegment.cxx:237
unsigned outerMostLayer(void) const
returns outer most layer.
const HepPoint3D & lineTsf(void) const
return line of Tsf for pos and dir
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 AList< TMLink > & links(unsigned mask=0) const
returns a list of masked TMLinks assigned to this track. 'mask' will be applied if mask is not 0.
Definition: TTrackBase.cxx:297
unsigned nLinks(unsigned mask=0) const
returns # of masked TMLinks assigned to this track object.
Definition: TTrackBase.cxx:305
A class to represent a track in tracking.
int t()
Definition: t.c:1