00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "TrkReco/TMLink.h"
00014 #include "TrkReco/TMDCWireHit.h"
00015 #include "TrkReco/TMDCWireHitMC.h"
00016 #include "TrkReco/TMDCUtil.h"
00017 #include "TrkReco/TTrackHEP.h"
00018 #include "CLHEP/Alist/ConstAList.h"
00019
00020
00021 const TMDCWire * const
00022 TMLink::wire(void) const {
00023 if (_hit) return _hit->wire();
00024 return NULL;
00025 }
00026
00027 const HepPoint3D &
00028 TMLink::xyPosition(void) const {
00029 return _hit->wire()->xyPosition();
00030 }
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 TMLink::TMLink(TTrack * t, const TMDCWireHit * h, const HepPoint3D & p, const HepPoint3D & d, double dr)
00068 : _track(t),
00069 _hit(h),
00070 _dPhi(0),
00071 _leftRight(0),
00072 _pull(0),
00073 _position(p),
00074 _positionD(d),
00075 _link(0),
00076 _fit2D(0),
00077 _tsfTag(0) {
00078 if (h) {
00079 _cDrift[0] = dr;
00080 _cDrift[1] = dr;
00081 _drift[0] = h->drift(0);
00082 _drift[1] = h->drift(1);
00083 _dDrift[0] = h->dDrift(0);
00084 _dDrift[1] = h->dDrift(1);
00085 }
00086 else {
00087 _cDrift[0] = 0.;
00088 _cDrift[1] = 0.;
00089 _drift[0] = 0.;
00090 _drift[1] = 0.;
00091 _dDrift[0] = 0.;
00092 _dDrift[1] = 0.;
00093 }
00094
00095 for (unsigned i = 0; i < 6; ++i)
00096 _neighbor[i] = NULL;
00097 for (unsigned i = 0; i < 4; ++i)
00098 _arcZ[i];
00099
00100
00101 if (h) {
00102 _onTrack = _onWire = h->xyPosition();
00103 }
00104 }
00105
00106 TMLink::TMLink(const TMLink & l)
00107 : _track(l._track),
00108 _hit(l._hit),
00109 _onTrack(l._onTrack),
00110 _onWire(l._onWire),
00111 _position(l._position),
00112 _positionD(l._positionD),
00113 _dPhi(l._dPhi),
00114 _leftRight(l._leftRight),
00115 _pull(l._pull),
00116 _link(l._link),
00117 _distance(l._distance),
00118
00119
00120
00121
00122 _fit2D(l._fit2D),
00123 _tsfTag(l._tsfTag) {
00124 _drift[0] = l._drift[0];
00125 _drift[1] = l._drift[1];
00126 _dDrift[0] = l._dDrift[0];
00127 _dDrift[1] = l._dDrift[1];
00128 _cDrift[0] = l._cDrift[0];
00129 _cDrift[1] = l._cDrift[1];
00130 for (unsigned i = 0; i < 6; ++i)
00131 _neighbor[i] = l._neighbor[i];
00132 for (unsigned i = 0; i < 4; ++i)
00133 _arcZ[i] = l._arcZ[i];
00134
00135 }
00136
00137 TMLink::~TMLink() {
00138 }
00139
00140 void
00141 TMLink::dump(const std::string & msg, const std::string & pre) const {
00142 std::cout << pre;
00143 if (_track) std::cout << "track#=,";
00144 if (_hit) {
00145 _hit->dump(msg);
00146 }
00147 }
00148
00149 unsigned
00150 NLayers(const AList<TMLink> & list) {
00151 unsigned l0 = 0;
00152 unsigned l1 = 0;
00153 unsigned n = list.length();
00154 for (unsigned i = 0; i < n; i++) {
00155 unsigned id = list[i]->wire()->layerId();
00156 if (id < 32) l0 |= (1 << id);
00157 else l1 |= (1 << (id - 32));
00158 }
00159
00160 unsigned l = 0;
00161 for (unsigned i = 0; i < 32; i++) {
00162 if (l0 & (1 << i)) ++l;
00163 if (l1 & (1 << i)) ++l;
00164 }
00165 return l;
00166 }
00167
00168 void
00169 NHits(const AList<TMLink> & links, unsigned nHits[43]) {
00170 for (unsigned i = 0; i < 43; i++) nHits[i] = 0;
00171 unsigned nLinks = links.length();
00172 for (unsigned i = 0; i < nLinks; i++)
00173 ++nHits[links[i]->wire()->layerId()];
00174 }
00175
00176 void
00177 NHitsSuperLayer(const AList<TMLink> & links, unsigned nHits[11]) {
00178 for (unsigned i = 0; i < 11; i++) nHits[i] = 0;
00179 unsigned nLinks = links.length();
00180 for (unsigned i = 0; i < nLinks; i++)
00181 ++nHits[links[i]->wire()->superLayerId()];
00182 }
00183
00184 void
00185 Dump(const CAList<TMLink> & links, const std::string & msg, const std::string & pre) {
00186 bool mc = (msg.find("mc") != std::string::npos);
00187 bool pull = (msg.find("pull") != std::string::npos);
00188 bool flag = (msg.find("flag") != std::string::npos);
00189 bool sort = (msg.find("sort") != std::string::npos);
00190 bool stereo = (msg.find("stereo") != std::string::npos);
00191 bool detail = (msg.find("detail") != std::string::npos);
00192 bool pos = (msg.find("position") != std::string::npos);
00193 if (detail)
00194 mc = pull = flag = sort = true;
00195
00196 CAList<TMLink> tmp = links;
00197 if (sort)
00198 tmp.sort(SortByWireId);
00199 unsigned n = tmp.length();
00200 unsigned nForFit = 0;
00201 #define MCC_MAX 1000
00202 unsigned MCC0[MCC_MAX];
00203 unsigned MCC1[MCC_MAX];
00204 for (unsigned i = 0; i < MCC_MAX; i++) {
00205 MCC0[i] = 0;
00206 MCC1[i] = 0;
00207 }
00208 bool MCCOverFlow = false;
00209
00210 std::cout << pre;
00211 for (unsigned i = 0; i < n; i++) {
00212 const TMLink & l = * tmp[i];
00213 std::cout << l.wire()->name();
00214
00215 double a = l.pull();
00216 unsigned mcId = 0;
00217 if (mc)
00218 if (l.hit()->mc())
00219 if (l.hit()->mc()->hep())
00220 mcId = l.hit()->mc()->hep()->id();
00221 if (pull) {
00222 std::cout << "[" << a << "]";
00223 }
00224 if (mc) {
00225 std::cout << "(" << mcId << ")";
00226 if (mcId < MCC_MAX) {
00227 ++MCC0[mcId];
00228 if (l.hit()->state() & WireHitFittingValid) {
00229 if (! (l.hit()->state() & WireHitInvalidForFit))
00230 ++MCC1[mcId];
00231 }
00232 }
00233 else {
00234 MCCOverFlow = true;
00235 }
00236 }
00237 if (flag) {
00238 if (l.hit()->state() & WireHitFindingValid)
00239 std::cout << "o";
00240 if (l.hit()->state() & WireHitFittingValid) {
00241 std::cout << "+";
00242 if (! (l.hit()->state() & WireHitInvalidForFit))
00243 ++nForFit;
00244 }
00245 if (l.hit()->state() & WireHitInvalidForFit)
00246 std::cout << "x";
00247 }
00248 if (stereo) {
00249 std::cout << "{" << l.leftRight() << "," << l.zStatus() << "}";
00250 }
00251 if (pos) {
00252 std::cout << ",pos=" << l.position();
00253 }
00254 std::cout << ",";
00255 }
00256 std::cout << " " << n << " l(s)";
00257 if (flag) std::cout << ", fv " << nForFit << " l(s)";
00258 if (mc) {
00259 unsigned nMC = 0;
00260 std::cout << ", mc";
00261 for (unsigned i = 0; i < MCC_MAX; i++) {
00262 if (MCC0[i] > 0) {
00263 ++nMC;
00264 std::cout << i << ":" << MCC0[i] << ",";
00265 }
00266 }
00267 std::cout << " total " << nMC << " contributions";
00268 if (flag) {
00269 nMC = 0;
00270 std::cout << ", fv mc";
00271 for (unsigned i = 0; i < MCC_MAX; i++) {
00272 if (MCC1[i] > 0) {
00273 ++nMC;
00274 std::cout << i << ":" << MCC1[i] << ",";
00275 }
00276 }
00277 std::cout << " total " << nMC << " contributions";
00278 }
00279
00280 if (MCCOverFlow)
00281 std::cout << "(counter overflow)";
00282 }
00283 std::cout << std::endl;
00284 }
00285
00286 void
00287 Dump(const TMLink & link, const std::string & msg, const std::string & pre) {
00288 CAList<TMLink> tmp;
00289 tmp.append(link);
00290 Dump(tmp, msg, pre);
00291 }
00292
00293 unsigned
00294 NStereoHits(const AList<TMLink> & links) {
00295 unsigned nLinks = links.length();
00296 unsigned n = 0;
00297 for (unsigned i = 0; i < nLinks; i++)
00298 if (links[i]->wire()->stereo())
00299 ++n;
00300 return n;
00301 }
00302
00303 unsigned
00304 NAxialHits(const AList<TMLink> & links) {
00305 unsigned nLinks = links.length();
00306 unsigned n = 0;
00307 for (unsigned i = 0; i < nLinks; i++)
00308 if (links[i]->wire()->axial())
00309 ++n;
00310 return n;
00311 }
00312
00313 AList<TMLink>
00314 AxialHits(const AList<TMLink> & links) {
00315 AList<TMLink> a;
00316 unsigned n = links.length();
00317 for (unsigned i = 0; i < n; i++) {
00318 if (links[i]->wire()->axial())
00319 a.append(links[i]);
00320 }
00321 return a;
00322 }
00323
00324 AList<TMLink>
00325 StereoHits(const AList<TMLink> & links) {
00326 AList<TMLink> a;
00327 unsigned n = links.length();
00328 for (unsigned i = 0; i < n; i++) {
00329 if (! links[i]->wire()->axial())
00330 a.append(links[i]);
00331 }
00332 return a;
00333 }
00334
00335 TMLink *
00336 InnerMost(const AList<TMLink> & a) {
00337 unsigned n = a.length();
00338 unsigned minId = 9999;
00339 TMLink * t = 0;
00340 for (unsigned i = 0; i < n; i++) {
00341 unsigned id = a[i]->wire()->id();
00342 if (id < minId) {
00343 minId = id;
00344 t = a[i];
00345 }
00346 }
00347 return t;
00348 }
00349
00350 TMLink *
00351 OuterMost(const AList<TMLink> & a) {
00352 unsigned n = a.length();
00353 unsigned maxId = 0;
00354 TMLink * t = 0;
00355 for (unsigned i = 0; i < n; i++) {
00356 unsigned id = a[i]->wire()->id();
00357 if (id > maxId) {
00358 maxId = id;
00359 t = a[i];
00360 }
00361 }
00362 return t;
00363 }
00364
00365 void
00366 SeparateCores(const AList<TMLink> & input,
00367 AList<TMLink> & cores,
00368 AList<TMLink> & nonCores) {
00369 unsigned n = input.length();
00370 for (unsigned i = 0; i < n; i++) {
00371 TMLink & t = * input[i];
00372 const TMDCWireHit & h = * t.hit();
00373 if (h.state() & WireHitFittingValid)
00374 cores.append(t);
00375 else
00376 nonCores.append(t);
00377 }
00378 }
00379
00380 AList<TMLink>
00381 Cores(const AList<TMLink> & input) {
00382 AList<TMLink> a;
00383 unsigned n = input.length();
00384 for (unsigned i = 0; i < n; i++) {
00385 TMLink & t = * input[i];
00386 const TMDCWireHit & h = * t.hit();
00387 if (h.state() & WireHitFittingValid)
00388 a.append(t);
00389 }
00390 return a;
00391 }
00392
00393 #if defined(__GNUG__)
00394 int
00395 SortByWireId(const TMLink ** a, const TMLink ** b) {
00396 if ((* a)->wire()->id() > (* b)->wire()->id()) return 1;
00397 else if
00398 ((* a)->wire()->id() == (* b)->wire()->id()) return 0;
00399 else return -1;
00400 }
00401
00402 int
00403 SortByX(const TMLink ** a, const TMLink ** b) {
00404 if ((* a)->position().x() > (* b)->position().x()) return 1;
00405 else if ((* a)->position().x() == (* b)->position().x()) return 0;
00406 else return -1;
00407 }
00408
00409 #else
00410 extern "C" int
00411 SortByWireId(const void * av, const void * bv) {
00412 const TMLink ** a((const TMLink**)av);
00413 const TMLink ** b((const TMLink**)bv);
00414 if ((* a)->wire()->id() > (* b)->wire()->id()) return 1;
00415 else if
00416 ((* a)->wire()->id() == (* b)->wire()->id()) return 0;
00417 else return -1;
00418 }
00419
00420 extern "C" int
00421 SortByX(const void* av, const void* bv) {
00422 const TMLink ** a((const TMLink**)av);
00423 const TMLink ** b((const TMLink**)bv);
00424 if ((* a)->position().x() > (* b)->position().x()) return 1;
00425 else if ((* a)->position().x() == (* b)->position().x()) return 0;
00426 else return -1;
00427 }
00428
00429 #endif
00430
00431 unsigned
00432 Width(const AList<TMLink> & list) {
00433 unsigned n = list.length();
00434 if (n < 2) return n;
00435
00436 const TMDCWire * w = list[0]->wire();
00437 unsigned nWires = w->layer()->nWires();
00438 unsigned center = w->localId();
00439
00440 #ifdef TRKRECO_DEBUG_DETAIL
00441 unsigned sId = w->superLayerId();
00442 #endif
00443
00444 unsigned left = 0;
00445 unsigned right = 0;
00446 for (unsigned i = 1; i < n; i++) {
00447 w = list[i]->wire();
00448 unsigned id = w->localId();
00449
00450 unsigned distance0, distance1;
00451 if (id > center) {
00452 distance0 = id - center;
00453 distance1 = nWires - distance0;
00454 }
00455 else {
00456 distance1 = center - id;
00457 distance0 = nWires - distance1;
00458 }
00459
00460 if (distance0 < distance1) {
00461 if (distance0 > right) right = distance0;
00462 }
00463 else {
00464 if (distance1 > left) left = distance1;
00465 }
00466
00467 #ifdef TRKRECO_DEBUG_DETAIL
00468 if (w->superLayerId() != sId)
00469 std::cout << "::width !!! super layer assumption violation" << std::endl;
00470 #endif
00471 }
00472
00473 return right + left + 1;
00474 }
00475
00476 AList<TMLink>
00477 Edges(const AList<TMLink> & list) {
00478 AList<TMLink> a;
00479
00480 unsigned n = list.length();
00481 if (n < 2) return a;
00482 else if (n == 2) return list;
00483
00484 const TMDCWire * w = list[0]->wire();
00485 unsigned nWires = w->layer()->nWires();
00486 unsigned center = w->localId();
00487
00488 unsigned left = 0;
00489 unsigned right = 0;
00490 TMLink * leftL = list[0];
00491 TMLink * rightL = list[0];
00492 for (unsigned i = 1; i < n; i++) {
00493 w = list[i]->wire();
00494 unsigned id = w->localId();
00495
00496 unsigned distance0, distance1;
00497 if (id > center) {
00498 distance0 = id - center;
00499 distance1 = nWires - distance0;
00500 }
00501 else {
00502 distance1 = center - id;
00503 distance0 = nWires - distance1;
00504 }
00505
00506 if (distance0 < distance1) {
00507 if (distance0 > right) {
00508 right = distance0;
00509 rightL = list[i];
00510 }
00511 }
00512 else {
00513 if (distance1 > left) {
00514 left = distance1;
00515 leftL = list[i];
00516 }
00517 }
00518 }
00519
00520 a.append(leftL);
00521 a.append(rightL);
00522 return a;
00523 }
00524
00525 AList<TMLink>
00526 SameLayer(const AList<TMLink> & list, const TMLink & a) {
00527 AList<TMLink> same;
00528 unsigned id = a.wire()->layerId();
00529 unsigned n = list.length();
00530 for (unsigned i = 0; i < n; i++) {
00531 if (list[i]->wire()->layerId() == id) same.append(list[i]);
00532 }
00533 return same;
00534 }
00535
00536 AList<TMLink>
00537 SameSuperLayer(const AList<TMLink> & list, const TMLink & a) {
00538 AList<TMLink> same;
00539 unsigned id = a.wire()->superLayerId();
00540 unsigned n = list.length();
00541 for (unsigned i = 0; i < n; i++) {
00542 if (list[i]->wire()->superLayerId() == id) same.append(list[i]);
00543 }
00544 return same;
00545 }
00546
00547 AList<TMLink>
00548 SameLayer(const AList<TMLink> & list, unsigned id) {
00549 AList<TMLink> same;
00550 unsigned n = list.length();
00551 for (unsigned i = 0; i < n; i++) {
00552 if (list[i]->wire()->layerId() == id) same.append(list[i]);
00553 }
00554 return same;
00555 }
00556
00557 AList<TMLink>
00558 SameSuperLayer(const AList<TMLink> & list, unsigned id) {
00559 AList<TMLink> same;
00560 unsigned n = list.length();
00561 for (unsigned i = 0; i < n; i++) {
00562 if (list[i]->wire()->superLayerId() == id) same.append(list[i]);
00563 }
00564 return same;
00565 }
00566
00567 AList<TMLink>
00568 InOut(const AList<TMLink> & list) {
00569 AList<TMLink> inners;
00570 AList<TMLink> outers;
00571 unsigned n = list.length();
00572 unsigned innerMostLayer = 999;
00573 unsigned outerMostLayer = 0;
00574 for (unsigned i = 0; i < n; i++) {
00575 unsigned id = list[i]->wire()->layerId();
00576 if (id < innerMostLayer) innerMostLayer = id;
00577 else if (id > outerMostLayer) outerMostLayer = id;
00578 }
00579 for (unsigned i = 0; i < n; i++) {
00580 unsigned id = list[i]->wire()->layerId();
00581 if (id == innerMostLayer) inners.append(list[i]);
00582 else if (id == outerMostLayer) outers.append(list[i]);
00583 }
00584 inners.append(outers);
00585 return inners;
00586 }
00587
00588 unsigned
00589 SuperLayer(const AList<TMLink> & list) {
00590 unsigned sl = 0;
00591 unsigned n = list.length();
00592 for (unsigned i = 0; i < n; i++)
00593 sl |= (1 << (list[i]->wire()->superLayerId()));
00594 return sl;
00595 }
00596
00597 unsigned
00598 SuperLayer(const AList<TMLink> & links, unsigned minN) {
00599 unsigned n = links.length();
00600 unsigned nHits[11] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
00601 for (unsigned i = 0; i < n; i++)
00602 ++nHits[links[i]->wire()->superLayerId()];
00603 unsigned sl = 0;
00604 for (unsigned i = 0; i < 11; i++)
00605 if (nHits[i] >= minN)
00606 sl |= (1 << i);
00607 return sl;
00608 }
00609
00610 unsigned
00611 NSuperLayers(const AList<TMLink> & list) {
00612 unsigned l0 = 0;
00613 unsigned n = list.length();
00614 for (unsigned i = 0; i < n; i++) {
00615 unsigned id = list[i]->wire()->superLayerId();
00616 l0 |= (1 << id);
00617 }
00618
00619 unsigned l = 0;
00620 for (unsigned i = 0; i < 11; i++) {
00621 if (l0 & (1 << i)) ++l;
00622 }
00623 return l;
00624 }
00625
00626 unsigned
00627 NSuperLayers(const AList<TMLink> & links, unsigned minN) {
00628 unsigned n = links.length();
00629 unsigned nHits[11] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
00630 for (unsigned i = 0; i < n; i++)
00631 ++nHits[links[i]->wire()->superLayerId()];
00632 unsigned sl = 0;
00633 for (unsigned i = 0; i < 11; i++)
00634 if (nHits[i] >= minN)
00635 ++sl;
00636 return sl;
00637 }
00638
00639 unsigned
00640 NMissingAxialSuperLayers(const AList<TMLink> & links) {
00641 unsigned n = links.length();
00642
00643 unsigned nHits[5] = {0, 0, 0, 0, 0};
00644 for (unsigned i = 0; i < n; i++)
00645 if (links[i]->wire()->axial())
00646 ++nHits[links[i]->wire()->axialStereoLayerId() / 4];
00647 unsigned j = 0;
00648 while (nHits[j] == 0) ++j;
00649 unsigned nMissing = 0;
00650 unsigned nMax = 0;
00651 for (unsigned i = j; i < 5; i++) {
00652 if (nHits[i] == 0) ++nMissing;
00653 else {
00654 if (nMax < nMissing) nMax = nMissing;
00655 nMissing = 0;
00656 }
00657 }
00658 return nMax;
00659 }
00660
00661 const TTrackHEP &
00662 Links2HEP(const AList<TMLink> & links) {
00663 const TTrackHEP * best = NULL;
00664 const AList<TTrackHEP> & list = TTrackHEP::list();
00665 unsigned nHep = list.length();
00666
00667 if (! nHep) return * best;
00668
00669 unsigned * N = (unsigned *) malloc(nHep * sizeof(unsigned));
00670 for (unsigned i = 0; i < nHep; i++) N[i] = 0;
00671
00672 for (unsigned i = 0; i < links.length(); i++) {
00673 const TMLink & l = * links[i];
00674 const TTrackHEP & hep = * l.hit()->mc()->hep();
00675 for (unsigned j = 0; j < nHep; j++)
00676 if (list[j] == & hep)
00677 ++N[j];
00678 }
00679
00680 unsigned nMax = 0;
00681 for (unsigned i = 0; i < nHep; i++) {
00682 if (N[i] > nMax) {
00683 best = list[i];
00684 nMax = N[i];
00685 }
00686 }
00687
00688 return * best;
00689 }
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708