00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #if defined(__sparc)
00015 # if defined(__EXTENSIONS__)
00016 # include <cmath>
00017 # else
00018 # define __EXTENSIONS__
00019 # include <cmath>
00020 # undef __EXTENSIONS__
00021 # endif
00022 #elif defined(__GNUC__)
00023 # if defined(_XOPEN_SOURCE)
00024 # include <cmath>
00025 # else
00026 # define _XOPEN_SOURCE
00027 # include <cmath>
00028 # undef _XOPEN_SOURCE
00029 # endif
00030 #endif
00031
00032 #define TTRACKMANAGER_INLINE_DEFINE_HERE
00033 #include <values.h>
00034 #include <cstring>
00035 #include <cstdlib>
00036
00037
00038 #include "CLHEP/String/Strings.h"
00039
00040 #include "TrkReco/TrkReco.h"
00041 #include "TrkReco/TMDCWireHit.h"
00042 #include "TrkReco/TMDCWireHitMC.h"
00043 #include "TrkReco/TTrack.h"
00044 #include "TrkReco/TTrackMC.h"
00045 #include "TrkReco/TTrackHEP.h"
00046 #include "TrkReco/TTrackManager.h"
00047 #include "MdcTables/MdcTables.h"
00048 #include "MdcTables/TrkTables.h"
00049
00050 #include "GaudiKernel/MsgStream.h"
00051 #include "GaudiKernel/AlgFactory.h"
00052 #include "GaudiKernel/ISvcLocator.h"
00053 #include "GaudiKernel/IMessageSvc.h"
00054 #include "GaudiKernel/IDataProviderSvc.h"
00055 #include "GaudiKernel/IDataManagerSvc.h"
00056 #include "GaudiKernel/SmartDataPtr.h"
00057 #include "GaudiKernel/IDataProviderSvc.h"
00058 #include "GaudiKernel/PropertyMgr.h"
00059 #include "GaudiKernel/IJobOptionsSvc.h"
00060 #include "GaudiKernel/Bootstrap.h"
00061 #include "GaudiKernel/StatusCode.h"
00062
00063 #include "GaudiKernel/ContainedObject.h"
00064 #include "GaudiKernel/SmartRef.h"
00065 #include "GaudiKernel/SmartRefVector.h"
00066 #include "GaudiKernel/ObjectVector.h"
00067 #include "EventModel/EventModel.h"
00068
00069 #include "EvTimeEvent/RecEsTime.h"
00070
00071
00072 #include "Identifier/Identifier.h"
00073 #include "Identifier/MdcID.h"
00074
00075
00076
00077
00078
00079 TTrackManager::TTrackManager()
00080 : _maxMomentum(10.),
00081 _sigmaCurlerMergeTest(sqrt(100.)),
00082 _nCurlerMergeTest(4),
00083 _debugLevel(0),
00084 _fitter("TTrackManager Fitter"),
00085 _cFitter("TTrackManager 2D Fitter"),
00086 _s(0) {
00087
00088
00089 StatusCode scmgn = Gaudi::svcLocator()->service("MagneticFieldSvc",m_pmgnIMF);
00090 if(scmgn!=StatusCode::SUCCESS) {
00091 std::cout<< "ERROR: Unable to open Magnetic field service"<<std::endl;
00092 }
00093
00094 IRawDataProviderSvc* irawDataProviderSvc;
00095 StatusCode sc = Gaudi::svcLocator()->service("RawDataProviderSvc",irawDataProviderSvc);
00096 m_rawDataProviderSvc = dynamic_cast<RawDataProviderSvc*> (irawDataProviderSvc);
00097 if(sc!=StatusCode::SUCCESS) {
00098 std::cout<< "ERROR: Unable to load RawDataProviderSvc"<<std::endl;
00099 }
00100 }
00101
00102 TTrackManager::~TTrackManager() {
00103 }
00104
00105 std::string
00106 TTrackManager::version(void) const {
00107 return std::string("2.27");
00108 }
00109
00110 void
00111 TTrackManager::dump(const std::string & msg, const std::string & pre) const {
00112 bool def = (msg == "") ? true : false;
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 if (def || msg.find("eventSummary") != std::string::npos || msg.find("detail") != std::string::npos) {
00152 std::cout << pre
00153 << "tracks reconstructed : " << _tracksAll.length()
00154 << std::endl;
00155 std::cout << pre
00156 << "good tracks : " << _tracks.length()
00157 << std::endl;
00158 std::cout << pre
00159 << "2D tracks : " << _tracks2D.length()
00160 << std::endl;
00161 std::cout << pre
00162 << "Track list:" << std::endl;
00163
00164 std::string tab = pre;
00165 std::string spc = " ";
00166 for (unsigned i = 0; i < _tracksAll.length(); i++) {
00167 std::cout << tab << TrackDump(* _tracksAll[i]) << std::endl;
00168 if (msg.find("hits") != std::string::npos || msg.find("detail") != std::string::npos)
00169 Dump(_tracksAll[i]->links(), "hits sort flag");
00170 }
00171 }
00172 }
00173
00174 void
00175 TTrackManager::maskCurlHits(const AList<TMDCWireHit> &axial,
00176 const AList<TMDCWireHit> &stereo,
00177 const AList<TTrack> &tracks) const {
00178
00179
00180 int i = 0;
00181 while(const TTrack *t = tracks[i++]){
00182 int j = 0;
00183 while(TMDCWireHit *a = axial[j++]){
00184 double x = t->helix().center().x() - a->xyPosition().x();
00185 double y = t->helix().center().y() - a->xyPosition().y();
00186 double r = sqrt(x*x+y*y);
00187 double R = fabs(t->helix().radius());
00188 double q = t->helix().center().x()*a->xyPosition().y() -
00189 t->helix().center().y()*a->xyPosition().x();
00190 double qq = q*t->charge();
00191 if(R-2. < r && r < R+2. && qq > 0.){
00192 a->state(a->state() | WireHitUsed);
00193 }
00194 }
00195 j = 0;
00196 while(TMDCWireHit *s = stereo[j++]){
00197 double x = t->helix().center().x() - s->xyPosition().x();
00198 double y = t->helix().center().y() - s->xyPosition().y();
00199 double r = sqrt(x*x+y*y);
00200 double R = fabs(t->helix().radius());
00201 double q = t->helix().center().x()*s->xyPosition().y() -
00202 t->helix().center().y()*s->xyPosition().x();
00203 double qq = q*t->charge();
00204 if(R-2.5 < r && r < R+2.5 && qq > 0.){
00205 s->state(s->state() | WireHitUsed);
00206 }
00207 }
00208 }
00209 }
00210
00211 void
00212 TTrackManager::salvage(const AList<TMDCWireHit> & hits) const {
00213
00214 #ifdef TRKRECO_DEBUG_DETAIL
00215 std::cout << name() << " ... salvaging" << std::endl;
00216 std::cout << " # of given hits=" << hits.length() << std::endl;
00217 #endif
00218
00219
00220 unsigned nTracks = _tracks.length();
00221 if (nTracks == 0) return;
00222 unsigned nHits = hits.length();
00223 if (nHits == 0) return;
00224
00225
00226 for (unsigned i = 0; i < nHits; i++) {
00227 TMDCWireHit & h = * hits[i];
00228
00229
00230 if (h.state() & WireHitUsed) continue;
00231 #ifdef TRKRECO_DEBUG_DETAIL
00232 std::cout << " checking " << h.wire()->name() << std::endl;
00233 #endif
00234
00235
00236 TTrack * best = closest(_tracks, h);
00237 #ifdef TRKRECO_DEBUG_DETAIL
00238 if (! best) {
00239 std::cout << " no track candidate returned";
00240 std::cout << "by TTrackManager::closest" << std::endl;
00241 }
00242 #endif
00243 if (! best) continue;
00244
00245
00246 AList<TMLink> link;
00247 link.append(new TMLink(0, & h));
00248 best->appendByApproach(link, 30.);
00249
00250 best->finder(TrackTrackManager);
00251 }
00252 }
00253
00254 TTrack *
00255 TTrackManager::closest(const AList<TTrack> & tracks,
00256 const TMDCWireHit & hit) const {
00257
00258 TMLink t;
00259 t.hit(& hit);
00260 unsigned n = tracks.length();
00261 double minDistance = MAXDOUBLE;
00262 TTrack * minTrk = NULL;
00263
00264
00265 for (unsigned i = 0; i < n; i++) {
00266 TTrack & trk = * tracks[i];
00267 int err = trk.approach(t);
00268 if (err < 0) continue;
00269 if (minDistance > t.distance()) {
00270 minDistance = t.distance();
00271 minTrk = & trk;
00272 }
00273 }
00274
00275 return minTrk;
00276 }
00277
00278
00279 StatusCode
00280
00281 TTrackManager::makeTds(RecMdcTrackCol* trkcol, RecMdcHitCol* hitcol, int tkStat, int brunge , int cal) {
00282 IMessageSvc *msgSvc;
00283 Gaudi::svcLocator()->service("MessageSvc", msgSvc);
00284
00285 MsgStream log(msgSvc, "TrkReco");
00286
00287 IDataProviderSvc* eventSvc = NULL;
00288 Gaudi::svcLocator()->service("EventDataSvc", eventSvc);
00289
00290 #ifdef TRKRECO_DEBUG
00291 if (eventSvc) {
00292 log << MSG::INFO << "makeTds:event Svc has been found" << endreq;
00293 } else {
00294 log << MSG::FATAL << "makeTds:Could not find eventSvc" << endreq;
00295 return StatusCode::FAILURE ;
00296 }
00297 #endif
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331 unsigned n = _tracks.length();
00332 int nTdsTk = trkcol->size();
00333 for (int i =0; i < n; i++) {
00334 RecMdcTrack* trk = new RecMdcTrack;
00335 TTrack* t = _tracks[i];
00336 int trackindex = i + nTdsTk;
00337
00338 HitRefVec hitrefvec;
00339 AList<TMLink> hits = t->links();
00340 float chisq=0;
00341
00342 HepPoint3D pos = t->helix().pivot();
00343 int charge = -1;
00344 HepVector m_a(5,0);
00345 m_a = t->helix().a();
00346 int statfinder=t->getFinderType();
00347 if(abs(statfinder)>1000&&brunge)statfinder=103;
00348 if(abs(statfinder)>1000&&!brunge)statfinder=3;
00349
00350 if(!brunge)m_a[2] = -m_a[2];
00351 if(abs(m_a[1])>999)m_a[1]=0;
00352 while(m_a[1]<0){m_a[1]+=2*pi;}
00353 while(m_a[1]>2*pi){m_a[1]-=2*pi;}
00355 const double x0 = t->helix().pivot().x();
00356 const double y0 = t->helix().pivot().y();
00357
00358 const double dr = m_a[0];
00359 const double phi0 = m_a[1];
00360 const double kappa = m_a[2];
00361 const double dz = m_a[3];
00362 const double tanl = m_a[4];
00363
00364 const double Bz = -1000*(m_pmgnIMF->getReferField());
00365 const double alpha = 333.564095 / Bz;
00366
00367 const double cox = x0 + dr*cos(phi0) - alpha*cos(phi0)/kappa;
00368 const double coy = y0 + dr*sin(phi0) - alpha*sin(phi0)/kappa;
00369
00370
00371 unsigned nHits = t->links().length();
00372 unsigned nStereos = 0;
00373 unsigned firstLyr = 44;
00374 unsigned lastLyr = 0;
00375 for (unsigned j=0; j<nHits; j++){
00376
00377 TMLink * l = hits[j];
00378
00379 HepPoint3D ontrack = l->positionOnTrack();
00380 HepPoint3D onwire = l->positionOnWire();
00381 HepPoint3D dir(ontrack.y()-coy,cox-ontrack.x(),0);
00382 double pos_phi=onwire.phi();
00383 double dir_phi=dir.phi();
00384 while(pos_phi>pi){pos_phi-=pi;}
00385 while(pos_phi<0){pos_phi+=pi;}
00386 while(dir_phi>pi){dir_phi-=pi;}
00387 while(dir_phi<0){dir_phi+=pi;}
00388 double entrangle=dir_phi-pos_phi;
00389 while(entrangle>pi/2)entrangle-=pi;
00390 while(entrangle<(-1)*pi/2)entrangle+=pi;
00391
00392
00393 int imass = 3;
00394 float tl = t->helix().a()[4];
00395 float f = sqrt(1. + tl * tl);
00396 float s = fabs(t->helix().curv()) * fabs(l->dPhi()) * f;
00397 float p = f / fabs(t->helix().a()[2]);
00398 float Mass[5] = {0.000511,0.105,0.140,0.493677,0.98327};
00399 double tof = s/29.98*sqrt(1.0+(Mass[imass-1]/p)*(Mass[imass-1]/p));
00400
00401 RecMdcHit* hit = new RecMdcHit;
00402 hit->setId(l->hit()->reccdc()->id);
00403
00404 hit->setTrkId(trackindex);
00405 hit->setDriftDistLeft(l->drift(0));
00406 hit->setDriftDistRight(l->drift(1));
00407 hit->setErrDriftDistLeft(l->dDrift(0));
00408 hit->setErrDriftDistRight(l->dDrift(1));
00409 hit->setChisqAdd(l->pull());
00410 hit->setFlagLR(l->leftRight());
00411
00412 hit->setStat(1);
00413
00414 hit->setAdc(l->hit()->reccdc()->adc);
00415
00416 hit->setTdc(l->hit()->reccdc()->timechannel);
00417 hit->setFltLen(tof * 30);
00418
00419 if(cal){hit->setDoca(l->distancenew());}
00420 else{hit->setDoca(l->distance());}
00421 hit->setZhit(l->positionOnTrack().z());
00422
00424 hit->setEntra(entrangle);
00425 hit->setDriftT(l->getDriftTime());
00426
00427
00428 Identifier hitIdf = MdcID:: wire_id(l->wire()->layerId(),l->wire()->localId());
00429 hit->setMdcId(hitIdf);
00430 hitcol->push_back(hit);
00431
00432 SmartRef<RecMdcHit> refhit(hit);
00433 hitrefvec.push_back(refhit);
00434 chisq += l->pull();
00435 if(l->wire()->stereo()) ++nStereos;
00436 if(firstLyr > l->wire()->layerId()) firstLyr = l->wire()->layerId();
00437 if(lastLyr < l->wire()->layerId()) lastLyr = l->wire()->layerId();
00438 }
00439
00440
00441 HepSymMatrix m_ea = t->helix().Ea();
00442 double errorMat[15];
00443 int k = 0;
00444 for (int ie = 0 ; ie < 5 ; ie ++){
00445 for (int je = ie ; je < 5 ; je ++) {
00446 errorMat[k] = m_ea[ie][je];
00447 k++;
00448 }
00449 }
00450
00451 trk->setTrackId(trackindex);
00452 trk->setHelix(m_a);
00453 trk->setPxy(t->pt());
00454 trk->setPx(t->pt() * (-sin(t->helix().phi0())));
00455 trk->setPy(t->pt() * cos(t->helix().phi0()));
00456 trk->setPz(t->pz());
00457 trk->setP(t->ptot());
00458
00459
00460 double theta = acos((t->pz())/t->ptot());
00461 trk->setTheta(theta);
00462
00463 double poca_dr = t->helix().dr();
00464 double poca_phi0 = t->helix().phi0();
00465 trk->setPhi(poca_phi0);
00466 HepPoint3D poca(pos.x()+poca_dr*cos(poca_phi0),
00467 pos.y()+poca_dr*sin(poca_phi0),
00468 pos.z()+t->helix().dz());
00469 trk->setPoca(poca);
00470 trk->setX(poca.x());
00471 trk->setY(poca.y());
00472 trk->setZ(poca.z());
00473 trk->setR(sqrt(poca.x()*poca.x() + poca.y()*poca.y()));
00474 trk->setPivot(pos);
00475 trk->setVX0(pos.x());
00476 trk->setVY0(pos.y());
00477 trk->setVZ0(pos.z());
00478
00479 trk->setError(m_ea);
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491 trk->setChi2(chisq);
00492 trk->setNdof(nHits-5);
00493 if (t->quality() & TrackQuality2D) trk->setNdof(nHits-3);
00494
00495 TMLink * last = OuterMost(hits);
00496 t->approach(*last);
00497 trk->setFiTerm(last->dPhi());
00498
00499 trk->setNhits(nHits);
00500 trk->setNster(nStereos);
00501 trk->setStat(statfinder);
00502
00503
00504 if(!brunge) trk->setCharge(int(t->charge())*(-1));
00505 else trk->setCharge(t->charge());
00506 trk->setVecHits(hitrefvec);
00507 trk->setFirstLayer(firstLyr);
00508 trk->setLastLayer(lastLyr);
00509
00510 trkcol->push_back(trk);
00511 }
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00528 #ifdef TRKRECO_DEBUG
00529 SmartDataPtr<RecMdcTrackCol> newtrkCol(eventSvc,"/Event/Recon/RecMdcTrackCol");
00530 if (!newtrkCol) {
00531 log << MSG::FATAL << "Could not find RecMdcTrackCol" << endreq;
00532 return( StatusCode::FAILURE);
00533 }
00534 log << MSG::INFO << "Begin to check RecMdcTrackCol"<<endreq;
00535 RecMdcTrackCol::iterator iter_trk = newtrkCol->begin();
00536 for( ; iter_trk != newtrkCol->end(); iter_trk++){
00537 const RecMdcTrack* tk = *iter_trk;
00538 std::cout<< "//==== "<<name()<<" print RecMdcTrack No."<<tk->trackId()<<" :"<< std::endl;
00539 cout <<" dr "<<tk->helix(0)
00540 <<" phi0 "<<tk->helix(1)
00541 <<" cpa "<<tk->helix(2)
00542 <<" z0 "<<tk->helix(3)
00543 <<" tanl "<<tk->helix(4)
00544 <<endl;
00545 bool printTrackDetail = true;
00546 if(printTrackDetail){
00547 std::cout<<" q "<<tk->charge()
00548 <<" theta "<<tk->theta()
00549 <<" phi "<<tk->phi()
00550 <<" x0 "<<tk->x()
00551 <<" y0 "<<tk->y()
00552 <<" z0 "<<tk->z()
00553 <<" r0 "<<tk->r()
00554 <<endl;
00555 std::cout <<" p "<<tk->p()
00556 <<" pt "<<tk->pxy()
00557 <<" pxyz("<<tk->px()
00558 <<","<<tk->py()
00559 <<","<<tk->pz()
00560 <<")"<<endl;
00561 std::cout<<" tkStat "<<tk->stat()
00562 <<" chi2 "<<tk->chi2()
00563 <<" ndof "<<tk->ndof()
00564 <<" nhit "<<tk->getNhits()
00565 <<" nst "<<tk->nster()
00566 <<endl;
00567 bool printErrMat = true;
00568 if(printErrMat){
00569 std::cout<< "errmat " << std::endl;
00570 for (int i=0; i<15; i++){ std::cout<< " "<<tk->err(i); }
00571 std::cout<< " " << std::endl;
00572 }
00573 }
00574
00575 bool printHit = true;
00576 if(printHit){
00577 int haveDigi[43][288];
00578 bool printMcTk = true;
00579 if(printMcTk) {
00580 for(int ii=0;ii<43;ii++){
00581 for(int jj=0;jj<43;jj++){
00582 haveDigi[ii][jj]=-9999;
00583 }
00584 }
00585 MdcDigiVec mdcDigiVec = m_rawDataProviderSvc->getMdcDigiVec();
00586 MdcDigiCol::iterator iter = mdcDigiVec.begin();
00587 std::cout<<name()<<"//==== "<<name()<<" print MdcDigiVec, nDigi="<<mdcDigiVec.size()<<" :"<<std::endl;
00588 for (int iDigi=0;iter!= mdcDigiVec.end(); iter++,iDigi++ ) {
00589 long l = MdcID::layer((*iter)->identify());
00590 long w = MdcID::wire((*iter)->identify());
00591 haveDigi[l][w]=(*iter)->getTrackIndex();
00592 }
00593 }
00594
00595 int nhits = tk->getVecHits().size();
00596 std::cout<<"nHits=" <<nhits<< std::endl;
00597 cout<<"No. ";
00598 if(printMcTk) cout<<"mcTkId ";
00599 cout<<"(layer,wire,lr) stat z"<<endl;
00600 for(int ii=0; ii <nhits ; ii++){
00601 Identifier id(tk->getVecHits()[ii]->getMdcId());
00602 int layer = MdcID::layer(id);
00603 int wire = MdcID::wire(id);
00604 cout<<ii<<":";
00605 if(printMcTk) { cout<<haveDigi[layer][wire]; }
00606 cout<<" ("<<layer<<","<<wire
00607 <<","<<tk->getVecHits()[ii]->getFlagLR()
00608 <<") "<<tk->getVecHits()[ii]->getStat()
00609 <<" "<<tk->getVecHits()[ii]->getZhit()<< " "<<std::endl;
00610 }
00611 std::cout << " "<< std::endl;
00612 }
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644 }
00645 #endif
00646 return StatusCode::SUCCESS;
00647 }
00648
00649
00650
00651 void
00652 TTrackManager::saveTables(void) {
00653 #ifdef TRKRECO_DEBUG
00654 std::cout << "TTrackManager::saveTables ... # 3D tracks=" << _tracks.length()
00655 << ", # 2D tracks=" << _tracks2D.length()
00656 << ", all tracks=" << _tracksAll.length() << std::endl;
00657 #endif
00658
00659
00660 AList<TTrack> badTracks;
00661 unsigned n = _tracks.length();
00662 unsigned * id = (unsigned *) malloc(n * sizeof(unsigned));
00663
00664 memset((char *) id, 0, n * sizeof(unsigned));
00665 for (unsigned i = 0; i < n; i++) {
00666 TTrack & t = * _tracks[i];
00667 if (! t.nLinks()) {
00668 badTracks.append((TTrack &) t);
00669 continue;
00670 }
00671
00672
00673 MdcRec_trk * r = 0;
00674 MdcRec_trk_add * a = 0;
00675 int err = copyTrack(t, & r, & a);
00676 if (err) {
00677 badTracks.append(t);
00678 continue;
00679 }
00680 _tracksFinal.append(t);
00681
00682
00683 id[i] = r->id;
00684 r->stat = t.state();
00685 a->kind = t.type();
00686 a->decision = t.finder();
00687 a->stat = t.fitting();
00688
00689
00690
00691
00692
00693
00694 }
00695
00696
00697 for (unsigned i = 0; i < n; i++) {
00698
00699 #ifdef TRKRECO_DEBUG_DETAIL
00700 std::cout << "id[" << i << "]=" << id[i] << std::endl;
00701 #endif
00702 if (! (id[i])) continue;
00703 if (! (_tracks[i]->daughter())) continue;
00704
00705 int dId = _tracks.index(_tracks[i]->daughter());
00706
00707 #ifdef TRKRECO_DEBUG_DETAIL
00708 std::cout << " dId=" << dId;
00709 if (dId >= 0) std::cout << ", id[dId]=" << id[dId];
00710 std::cout << std::endl;
00711 #endif
00712
00713 if (dId >= 0) {
00714 if (id[dId]) {
00715
00716
00717
00718 MdcRec_trk_add* a = &(*MdcRecTrkAddCol::getMdcRecTrkAddCol())[i];
00719 }
00720 }
00721 }
00722 free(id);
00723
00724
00725 _tracks.remove(badTracks);
00726 badTracks.removeAll();
00727
00728
00729 n = _tracks2D.length();
00730 for (unsigned i = 0; i < n; i++) {
00731 TTrack & t = * _tracks2D[i];
00732
00733
00734 MdcRec_trk * r = 0;
00735 MdcRec_trk_add * a = 0;
00736 int err = copyTrack(t, & r, & a);
00737 if (err) {
00738 #ifdef TRKRECO_DEBUG
00739 std::cout << "TTrackManager::saveTables !!! bad 2D tracks found"
00740 << " : err=" << err << std::endl
00741 << TrackDump(t) << std::endl;
00742 #endif
00743 badTracks.append(t);
00744 continue;
00745 }
00746 _tracksFinal.append(t);
00747
00748
00749
00750
00751
00752
00753
00754
00755 r->stat = t.state();
00756 a->kind = t.type();
00757 a->decision = t.finder();
00758
00759 a->quality = TrackQuality2D;
00760 a->stat = t.fitting();
00761
00762 #ifdef TRKRECO_DEBUG
00763 if ((r->ndf == 0) && (r->chiSq > 0.)) {
00764 std::cout << "TTrackManager::saveTables !!! chisq>0 with ndf=0."
00765 << std::endl
00766 << " Here is a track dump"
00767 << " " << TrackDump(t) << std::endl;
00768 t.dump("detail");
00769 }
00770 if ((r->ndf > 0) && (r->chiSq == 0.)) {
00771 std::cout << "TTrackManager::saveTables !!! chisq=0 with ndf>0."
00772 << std::endl
00773 << " Here is a track dump"
00774 << " " << TrackDump(t) << std::endl;
00775 t.dump("detail");
00776 }
00777
00778 if (r->ndf == 0)
00779 std::cout << "TTrackManager::saveTables ... ndf = 0" << std::endl
00780 << " " << TrackDump(t) << std::endl;
00781 if (r->chiSq == 0.)
00782 std::cout << "TTrackManager::saveTables ... chisq = 0" << std::endl
00783 << " " << TrackDump(t) << std::endl;
00784 #endif
00785 }
00786 _tracks2D.remove(badTracks);
00787
00788
00789 ++_s->_nEvents;
00790 _s->_nTracks += _tracks.length();
00791 _s->_nTracksAll += _tracksAll.length();
00792 _s->_nTracks2D += _tracks2D.length();
00793 _s->_nTracksFinal += _tracksFinal.length();
00794 }
00795
00796 void
00797 TTrackManager::saveMCTables(void) const {
00798 unsigned n = _tracksFinal.length();
00799 for (unsigned i = 0; i < n; i++) {
00800 const TTrack & t = * _tracksFinal[i];
00801
00802
00803
00804 MdcRec_trk* r = &(*MdcRecTrkCol::getMdcRecTrkCol())[i];
00805
00806
00807
00808
00809 const AList<TMLink> & hits = t.finalHits();
00810 unsigned nHits = hits.length();
00811 for (unsigned j = 0; j < nHits; j++) {
00812 TMLink * l = hits[j];
00813 MdcRec_wirhit * h = l->hit()->reccdc();
00814 MdcDat_mcwirhit * m = l->hit()->mc()->datcdc();
00815 m->trk = r;
00816
00817
00818 MdcRec_mctrk2hep* c = new MdcRec_mctrk2hep;
00819 MdcRecMctrk2hepCol::getMdcRecMctrk2hepCol()->push_back(*c);
00820 c->wir = h;
00821 c->trk = r;
00822 c->hep = l->hit()->mc()->hep()->gen();
00823 }
00824
00825 const TTrackMC * const mc = t.mc();
00826
00827
00828
00829 MdcRec_mctrk* m = new MdcRec_mctrk;
00830 MdcRecMctrkCol::getMdcRecMctrkCol()->push_back(*m);
00831 m->wirFrac = mc->wireFraction();
00832 m->wirFracHep = mc->wireFractionHEP();
00833 m->charge = mc->charge();
00834 m->ptFrac = mc->ptFraction();
00835 m->pzFrac = mc->pzFraction();
00836 m->quality = mc->quality();
00837 if (mc->hep()) m->hep = mc->hep()->gen();
00838 else m->hep = 0;
00839 }
00840 }
00841
00842 void
00843 TTrackManager::movePivot(void) {
00844 unsigned n = _tracksAll.length();
00845
00846 for (unsigned i = 0; i < n; i++) {
00847 if (_tracksAll[i]->nLinks()) _tracksAll[i]->movePivot();
00848
00849 }
00850 nameTracks();
00851 }
00852
00853 void
00854 TTrackManager::clear(void) {
00855 HepAListDeleteAll(_tracksAll);
00856 _tracks.removeAll();
00857 _tracks2D.removeAll();
00858 _tracksFinal.removeAll();
00859 HepAListDeleteAll(_associateHits);
00860 static bool first = true;
00861 if (first) {
00862 first = false;
00863 int size;
00864
00865
00866
00867
00868 }
00869 }
00870
00871 void
00872 TTrackManager::finish(void) {
00873 refit();
00874 movePivot();
00875 if (_debugLevel > 1) {
00876 std::cout << name() << " ... finishing" << std::endl;
00877
00878 unsigned n = _tracks.length();
00879 for (unsigned i = 0; i < n; i++) {
00880
00881 TTrack & t = * _tracks[i];
00882 std::cout << " " << t.name() << std::endl;
00883 t.dump("hits mc track flag sort", " ");
00884 }
00885 }
00886 }
00887
00888 void
00889 TTrackManager::append(AList<TTrack> & list) {
00890 _tracksAll.append(list);
00891 _tracks.append(selectGoodTracks(list));
00892 list.removeAll();
00893 }
00894
00895 void
00896 TTrackManager::append2D(AList<TTrack> & list) {
00897 _tracksAll.append(list);
00898 _tracks2D.append(selectGoodTracks(list, true));
00899 _tracks2D.sort(SortByPt);
00900 list.removeAll();
00901 }
00902
00903 void
00904 TTrackManager::refit(void) {
00905 #ifdef TRKRECO_DEBUG_DETAIL
00906 std::cout << name() << " ... refitting" << std::endl;
00907 #endif
00908 unsigned n = _tracks.length();
00909 AList<TTrack> bads;
00910 for (unsigned i = 0; i < n; i++) {
00911 TTrack & t = * _tracks[i];
00912 int err;
00913 err = _fitter.fit(t);
00914 if (err < 0) {
00915 bads.append(t);
00916 #ifdef TRKRECO_DEBUG_DETAIL
00917 std::cout << " " << t.name();
00918 std::cout << " rejected because of fitting failure" << std::endl;
00919 #endif
00920 continue;
00921 }
00922 t.refine(30. * 10.);
00923 err = _fitter.fit(t);
00924 if (err < 0) {
00925 bads.append(t);
00926 #ifdef TRKRECO_DEBUG_DETAIL
00927 std::cout << " " << t.name();
00928 std::cout << " rejected because of fitting failure" << std::endl;
00929 #endif
00930 continue;
00931 }
00932 t.refine(30. * 1.);
00933 err = _fitter.fit(t);
00934 if (err < 0) {
00935 bads.append(t);
00936 #ifdef TRKRECO_DEBUG_DETAIL
00937 std::cout << " " << t.name();
00938 std::cout << " rejected because of fitting failure" << std::endl;
00939 #endif
00940 continue;
00941 }
00942 }
00943 _tracks.remove(bads);
00944 }
00945
00946 void
00947 TTrackManager::mask(void) const {
00948 #ifdef TRKRECO_DEBUG_DETAIL
00949 std::cout << name() << " ... masking" << std::endl;
00950 #endif
00951
00952 unsigned n = _tracks.length();
00953 for (unsigned i = 0; i < n; i++) {
00954 TTrack & t = * _tracks[i];
00955
00956
00957
00958 if (! t.cores().length()) continue;
00959
00960
00961 unsigned nHits[43];
00962 NHits(t.cores(), nHits);
00963
00964
00965 bool needMask = false;
00966 for (unsigned j = 0; j < 43; j++) {
00967 if (nHits[j] > 1) {
00968 AList<TMLink> linksInLayer = SameLayer(t.links(), j);
00969 if (Width(linksInLayer) > 2) {
00970 needMask = true;
00971
00972 #ifdef TRKRECO_DEBUG_DETAIL
00973 Dump(linksInLayer, "sort", " -->");
00974 #endif
00975 break;
00976 }
00977 }
00978 }
00979 if (! needMask) continue;
00980
00981 #ifdef TRKRECO_DEBUG_DETAIL
00982 std::cout << " trk" << i << "(id is tmp) needs mask" << std::endl;
00983 std::cout << " type = " << t.type() << std::endl;
00984 #endif
00985
00986
00987 switch (t.type()) {
00988 case TrackTypeNormal:
00989 maskNormal(t);
00990 maskMultiHits(t);
00991 break;
00992 case TrackTypeCurl:
00993 maskCurl(t);
00994 maskMultiHits(t);
00995 break;
00996 default:
00997 break;
00998 }
00999
01000
01001
01002 _fitter.fit(t);
01003
01004 #ifdef TRKRECO_DEBUG_DETAIL
01005 std::cout << " masking result : ";
01006 t.dump("detail sort", " ");
01007 #endif
01008 }
01009 }
01010
01011 void
01012 TTrackManager::nameTracks(void) {
01013 unsigned n = _tracks.length();
01014 for (unsigned i = 0; i < n; i++) {
01015 TTrack & t = * _tracks[i];
01016 t._name = "trk" + itostring(i) + "(" + t._name + ")";
01017 }
01018 AList<TTrack> tmp = _tracksAll;
01019 tmp.remove(_tracks);
01020 unsigned n1 = tmp.length();
01021 for (unsigned i = 0; i < n1; i++) {
01022 TTrack & t = * tmp[i];
01023 t._name = "trk" + itostring(i + n) + "(" + t._name + ")";
01024 }
01025 }
01026
01027 TMLink &
01028 TTrackManager::divide(const TTrack & t, AList<TMLink> * l) const {
01029 TMLink & start = * OuterMost(t.links());
01030 const HepPoint3D & center = t.helix().center();
01031 const HepVector3D a = start.positionOnTrack() - center;
01032 for (unsigned j = 0; j < t.nLinks(); j++) {
01033 if (t[j] == & start) continue;
01034 TMLink & k = * t[j];
01035 const HepVector3D b = k.positionOnTrack() - center;
01036 if (a.cross(b).z() >= 0.) l[0].append(k);
01037 else l[1].append(k);
01038 }
01039
01040 #ifdef TRKRECO_DEBUG_DETAIL
01041 std::cout << " outer link = " << start.hit()->wire()->name() << std::endl;
01042 std::cout << " nLinks of 0 = " << l[0].length() << std::endl;
01043 std::cout << " nLinks of 1 = " << l[1].length() << std::endl;
01044 #endif
01045
01046 if (l[0].length() == 0 || l[1].length() == 0)
01047 return divideByIp(t, l);
01048
01049 return start;
01050 }
01051
01052 TMLink &
01053 TTrackManager::divideByIp(const TTrack & t, AList<TMLink> * l) const {
01054 l[0].removeAll();
01055 l[1].removeAll();
01056
01057 const HepPoint3D & center = t.helix().center();
01058 const HepVector3D a = ORIGIN - center;
01059 for (unsigned j = 0; j < t.nLinks(); j++) {
01060 TMLink & k = * t[j];
01061 const HepVector3D b = k.positionOnTrack() - center;
01062 if (a.cross(b).z() >= 0.) l[0].append(k);
01063 else l[1].append(k);
01064 }
01065
01066
01067 TMLink & start = * OuterMost(t.links());
01068 return start;
01069 }
01070
01071 void
01072 TTrackManager::removeHitsAcrossOverIp(AList<TMLink> & l) const {
01073
01074
01075 unsigned n = l.length();
01076 float phiSum = 0.;
01077 for (unsigned i = 0; i < n; i++) {
01078 const TMDCWire & w = * l[i]->hit()->wire();
01079 unsigned j = w.localId();
01080 unsigned nWire = w.layer()->nWires();
01081
01082 float phi = (float) j / (float) nWire;
01083 phiSum += phi;
01084 }
01085 float average = phiSum / (float) n;
01086
01087 AList<TMLink> cross;
01088 for (unsigned i = 0; i < n; i++) {
01089 const TMDCWire & w = * l[i]->hit()->wire();
01090 unsigned j = w.localId();
01091 unsigned nWire = w.layer()->nWires();
01092
01093 float phi = (float) j / (float) nWire;
01094 float dif = fabs(phi - average);
01095 if (dif > 0.5) dif = 1. - dif;
01096
01097 if (dif > 0.3) cross.append(l[i]);
01098 }
01099 l.remove(cross);
01100
01101 #ifdef TRKRECO_DEBUG_DETAIL
01102 std::cout << " Cross over IP reduction : ";
01103 for (unsigned i = 0; i < cross.length(); i++) {
01104 std::cout << cross[i]->wire()->name() << ",";
01105 }
01106 std::cout << std::endl;
01107 #endif
01108 }
01109
01110
01111 void
01112 TTrackManager::maskOut(TTrack & t, const AList<TMLink> & links) const {
01113 unsigned n = links.length();
01114 if (! n) return;
01115 for (unsigned i = 0; i < n; i++) {
01116 const TMDCWireHit & hit = * links[i]->hit();
01117 hit.state(hit.state() | WireHitInvalidForFit);
01118 }
01119 t._fitted = false;
01120
01121 #ifdef TRKRECO_DEBUG_DETAIL
01122 Dump(links, "detail", " TTrackManager::maskOut ... masking ");
01123 #endif
01124 }
01125
01126 void
01127 TTrackManager::maskMultiHits(TTrack & t) const {
01128 #ifdef TRKRECO_DEBUG_DETAIL
01129 std::cout << "... masking multi-hits" << std::endl;
01130 #endif
01131
01132 if (! t.cores().length()) return;
01133 AList<TMLink> cores = t.cores();
01134 unsigned n = cores.length();
01135 bool layerLimited = false;
01136 AList<TMLink> bads;
01137
01138 cores.sort(SortByWireId);
01139 for (unsigned i = 0; i < n; i++) {
01140 if (layerLimited) {
01141 bads.append(cores[i]);
01142 continue;
01143 }
01144 AList<TMLink> linksInLayer =
01145 SameLayer(cores, cores[i]->wire()->layerId());
01146 if (linksInLayer.length() > 3) {
01147 bads.append(cores[i]);
01148 layerLimited = true;
01149 }
01150 }
01151 maskOut(t, bads);
01152 }
01153
01154 void
01155 TTrackManager::maskNormal(TTrack & t) const {
01156
01157
01158 AList<TMLink> l[2];
01159 TMLink & start = divideByIp(t, l);
01160
01161 #ifdef TRKRECO_DEBUG_DETAIL
01162 std::cout << " normal : divided by IP" << std::endl;
01163 std::cout << " 0=";
01164 for (unsigned j = 0; j < l[0].length(); j++) {
01165 std::cout << "," << l[0][j]->wire()->name();
01166 }
01167 std::cout << std::endl;
01168 std::cout << " 1=";
01169 for (unsigned j = 0; j < l[1].length(); j++) {
01170 std::cout << "," << l[1][j]->wire()->name();
01171 }
01172 std::cout << std::endl;
01173 #endif
01174
01175
01176 unsigned maskSide = 2;
01177
01178
01179 if (NSuperLayers(l[0]) < NSuperLayers(l[1])) maskSide = 0;
01180 else if (NSuperLayers(l[0]) > NSuperLayers(l[1])) maskSide = 1;
01181 #ifdef TRKRECO_DEBUG_DETAIL
01182 std::cout << " NSuperLayers 0, 1 = " << NSuperLayers(l[0]) << ", ";
01183 std::cout << NSuperLayers(l[1]) << std::endl;
01184 #endif
01185 if (maskSide != 2) {
01186 maskOut(t, l[maskSide]);
01187 return;
01188 }
01189
01190
01191 unsigned i0 = InnerMost(l[0])->wire()->layerId();
01192 unsigned i1 = InnerMost(l[1])->wire()->layerId();
01193 if (i0 < i1) maskSide = 1;
01194 else if (i0 > i1) maskSide = 0;
01195 #ifdef TRKRECO_DEBUG_DETAIL
01196 std::cout << " i0, i1 = " << i0 << ", " << i1 << std::endl;
01197 #endif
01198 if (maskSide != 2) {
01199 maskOut(t, l[maskSide]);
01200 return;
01201 }
01202
01203
01204 if (NLayers(l[0]) < NLayers(l[1])) maskSide = 0;
01205 else if (NLayers(l[0]) > NLayers(l[1])) maskSide = 1;
01206 #ifdef TRKRECO_DEBUG_DETAIL
01207 std::cout << " NLayers 0, 1 = " << NLayers(l[0]) << ", ";
01208 std::cout << NLayers(l[1]) << std::endl;
01209 #endif
01210 if (maskSide != 2) {
01211 maskOut(t, l[maskSide]);
01212 return;
01213 }
01214
01215
01216 if (maskSide == 2) {
01217 TTrack * tt[2];
01218 for (unsigned j = 0; j < 2; j++) {
01219 tt[j] = new TTrack(t);
01220 tt[j]->remove(l[j]);
01221 _fitter.fit(* tt[j]);
01222 }
01223 if (tt[1]->pt() > tt[0]->pt()) maskSide = 1;
01224 else maskSide = 0;
01225 #ifdef TRKRECO_DEBUG_DETAIL
01226 std::cout << " pt 0 = " << tt[1]->pt() << std::endl;
01227 std::cout << " pt 1 = " << tt[0]->pt() << std::endl;
01228 #endif
01229 delete tt[0];
01230 delete tt[1];
01231 }
01232 maskOut(t, l[maskSide]);
01233 return;
01234 }
01235
01236 void
01237 TTrackManager::maskCurl(TTrack & t) const {
01238
01239
01240 AList<TMLink> l[2];
01241 TMLink & start = divideByIp(t, l);
01242 if (l[0].length() == 0) return;
01243 if (l[1].length() == 0) return;
01244
01245 #ifdef TRKRECO_DEBUG_DETAIL
01246 std::cout << " curl : divided by IP" << std::endl;
01247 std::cout << " 0:";
01248 Dump(l[0], "flag sort");
01249 std::cout << " 1:";
01250 Dump(l[1], "flag sort");
01251 std::cout << std::endl;
01252 #endif
01253
01254
01255 unsigned maskSide = 2;
01256
01257
01258 if (NSuperLayers(l[0]) < NSuperLayers(l[1])) maskSide = 0;
01259 else if (NSuperLayers(l[0]) > NSuperLayers(l[1])) maskSide = 1;
01260 #ifdef TRKRECO_DEBUG_DETAIL
01261 std::cout << " NSuperLayers 0, 1 = " << NSuperLayers(l[0]) << ", ";
01262 std::cout << NSuperLayers(l[1]) << std::endl;
01263 #endif
01264 if (maskSide != 2) {
01265 maskOut(t, l[maskSide]);
01266 return;
01267 }
01268
01269
01270 TTrack * tt[2];
01271 tt[0] = new TTrack(t);
01272 tt[1] = new TTrack(t);
01273 tt[0]->remove(l[1]);
01274 tt[1]->remove(l[0]);
01275 _fitter.fit(* tt[0]);
01276 _fitter.fit(* tt[1]);
01277 Helix h0 = Helix(tt[0]->helix());
01278 Helix h1 = Helix(tt[1]->helix());
01279
01280
01281 h0.pivot(ORIGIN);
01282 h1.pivot(ORIGIN);
01283 if (fabs(h0.dz()) < fabs(h1.dz())) maskSide = 1;
01284 else maskSide = 0;
01285
01286 delete tt[0];
01287 delete tt[1];
01288 maskOut(t, l[maskSide]);
01289 return;
01290 }
01291
01292 StatusCode
01293 TTrackManager::determineT0(unsigned level, unsigned nMax) {
01294 #ifdef TRKRECO_DEBUG_DETAIL
01295 if (level == 0) {
01296 std::cout << "TTrackManager::determineT0 !!! called with level = 0";
01297 std::cout << std::endl;
01298 }
01299 #endif
01300
01301 IMessageSvc *msgSvc;
01302 Gaudi::svcLocator()->service("MessageSvc", msgSvc);
01303 MsgStream log(msgSvc, "TTrackManager");
01304
01305 IDataProviderSvc* eventSvc = NULL;
01306 Gaudi::svcLocator()->service("EventDataSvc", eventSvc);
01307
01308 static bool first = true;
01309 static unsigned methode = 0;
01310 if (first) {
01311 first = false;
01312
01313 if (level == 1) {
01314 _cFitter.fit2D(true);
01315 }
01316 else if (level == 2) {
01317
01318 }
01319 else if (level == 3) {
01320
01321 }
01322 else if (level == 4) {
01323
01324 _cFitter.propagation(true);
01325 }
01326 else if (level == 5) {
01327
01328 _cFitter.propagation(true);
01329 _cFitter.tof(true);
01330 }
01331 else if (level == 6) {
01332 methode = 1;
01333
01334 _cFitter.propagation(true);
01335 _cFitter.tof(true);
01336 }
01337 else if (level == 7) {
01338 methode = 2;
01339
01340 _cFitter.propagation(true);
01341 _cFitter.tof(true);
01342 }
01343 }
01344
01345 unsigned n = _tracks.length();
01346 if (! n) return StatusCode::SUCCESS;
01347
01348 if (nMax == 0) nMax = n;
01349 if (n > nMax) n = nMax;
01350
01351
01352 _t0 = 999.;
01353
01354
01355 float t0_sta = -1;
01356 float tev = 0;
01357 SmartDataPtr<RecEsTimeCol> aevtimeCol(eventSvc,"/Event/Recon/RecEsTimeCol");
01358 if (aevtimeCol) {
01359 RecEsTimeCol::iterator iter_evt = aevtimeCol->begin();
01360 t0_sta = (*iter_evt)->getStat();
01361 tev = (*iter_evt)->getTest();
01362
01363 }else{
01364 log << MSG::WARNING << "Could not find RecEsTimeCol" << endreq;
01365 }
01366
01367 if (methode == 0) _t0 = T0(n);
01368
01369 else if (methode == 1) _t0 = T0Fit(n);
01370
01371 else if (methode ==2) {
01372 if (t0_sta != 1) {
01373 _t0 = T0Fit(n);
01374
01375 }
01376 }
01377
01378
01379
01380
01381
01382
01383
01384
01385
01386
01387
01388
01389
01390
01391
01392
01393
01394
01395
01396 if (_debugLevel) {
01397 std::cout << "TTrackManager::determineT0 ... methode=" << methode;
01398 std::cout << ", T0 offset=" << - _t0;
01399 std::cout << ", # of tracks used=" << n << std::endl;
01400 }
01401
01402
01403 float t0_rec = 999.;
01404 int t0_recSta = 8;
01405 if(fabs(_t0 + tev) < 4) t0_rec = 0;
01406 if(fabs(_t0 + tev - 8) < 4) t0_rec = 8;
01407 if(fabs(_t0 + tev - 16) < 4) t0_rec = 16;
01408 log << MSG::INFO << "beginning to make RecEsTimeCol" <<endreq;
01409 IDataManagerSvc *dataManSvc = dynamic_cast<IDataManagerSvc*> (eventSvc);
01410 DataObject *aEvTime;
01411 eventSvc->findObject("/Event/Recon/RecEsTimeCol",aEvTime);
01412 if(aEvTime!=NULL && t0_rec<25){
01413 dataManSvc->clearSubTree("/Event/Recon/RecEsTimeCol");
01414 eventSvc->unregisterObject("/Event/Recon/RecEsTimeCol");
01415 }
01416 if(t0_rec<25){
01417
01418 RecEsTimeCol *aEvTimeCol = new RecEsTimeCol;
01419 RecEsTime *aevtime = new RecEsTime;
01420 aevtime -> setTest(t0_rec);
01421 aevtime -> setStat(t0_recSta);
01422 aEvTimeCol->push_back(aevtime);
01423
01424
01425
01426 StatusCode evtime = eventSvc->registerObject("/Event/Recon/RecEsTimeCol", aEvTimeCol);
01427
01428 if(evtime!=StatusCode::SUCCESS) {
01429 log << MSG::FATAL << "Could not register Event Start Time" << endreq;
01430 return( StatusCode::FAILURE);
01431 }
01432 }
01433 return( StatusCode::SUCCESS );
01434 }
01435
01436 float
01437 TTrackManager::T0(unsigned n) {
01438
01439 #define X0 -10.
01440 #define X1 0.
01441 #define X2 10.
01442 #define STEP 10.
01443
01444
01445 float t0Sum = 0.;
01446 for (unsigned i = 0; i < n; i++) {
01447 TTrack & t = * _tracks[i];
01448 float y[3];
01449 for (unsigned j = 0; j < 3; j++) {
01450 float offset = X0 + j * STEP;
01451 _cFitter.fit(t, offset);
01452 y[j] = t.chi2();
01453 }
01454 t0Sum += minimum(y[0], y[1], y[2]);
01455 }
01456 float t0 = t0Sum / (float) n;
01457 if (isnan(t0)) t0 = 0.;
01458
01459
01460 n = _tracks.length();
01461 for (unsigned i = 0; i < n; i++) {
01462 TTrack & t = * _tracks[i];
01463 _cFitter.fit(t, t0);
01464 }
01465
01466
01467
01468
01469
01470
01471
01472
01473 return - t0;
01474 }
01475
01476 float
01477 TTrackManager::T0Fit(unsigned n) {
01478
01479 float tev_err;
01480 float tev_sum0= 0.;
01481 float tev_sum = 0.;
01482 float tev_sum2= 0.;
01483 float w_sum = 0.;
01484
01485
01486
01487 const int cn=_tracks.length();
01488 float* sort = new float[cn];
01489 float ptmax_pre=1.e10;
01490 for (unsigned i = 0; i < cn; i++) {
01491 float ptmax=-999.;
01492 int jmax;
01493 for (unsigned j = 0; j < cn; j++) {
01494 TTrack & tj = * _tracks[j];
01495 float pt = fabs(1./tj.helix().a()[2]);
01496 if( pt < ptmax_pre && pt > ptmax) {
01497 ptmax = pt;
01498 jmax = j;
01499 }
01500 sort[i]=jmax;
01501 }
01502 ptmax_pre=ptmax;
01503 }
01504
01505 for (unsigned i = 0; i < n; i++) {
01506
01507 TTrack & t = * _tracks[sort[i]];
01508
01509
01510 float tev = 0.;
01511 _cFitter.fit(t, tev, tev_err);
01512
01513 float w = 1. / tev_err / tev_err;
01514 tev_sum += w * tev;
01515 w_sum += w;
01516
01517
01518 }
01519
01520 delete [] sort;
01521
01522 float tev_mean = tev_sum / w_sum;
01523
01524
01525
01526
01527 if (isnan(tev_mean)) tev_mean = 0.;
01528
01529
01530
01531
01532
01533
01534
01535
01536 return - tev_mean;
01537 }
01538
01539 float
01540 TTrackManager::minimum(float y0, float y1, float y2) const {
01541 float xMin = X1 + 0.5 * STEP * (y0 - y2) / (y0 + y2 - 2. * y1);
01542 return xMin;
01543 }
01544
01545
01546 void
01547 TTrackManager::merge(void) {
01548
01549
01550 unsigned n = _tracks.length();
01551
01552 AList<TTrack> bads;
01553 unsigned * flagTrk = (unsigned *) malloc(n * sizeof(unsigned));
01554 for (unsigned i = 0; i < n; i++) flagTrk[i] = 0;
01555
01556
01557 for (unsigned i0 = 0; i0 < n; i0++) {
01558
01559 if (flagTrk[i0] != 0) continue;
01560 TTrack & t0 = * _tracks[i0];
01561 if (! (t0.pt() < 0.13)) continue;
01562
01563 unsigned Noverlap(0), Nall(0);
01564 float OverlapRatioMax(-999.);
01565 unsigned MaxID(0);
01566
01567 for (unsigned i1= 0 ; i1 < n; i1++) {
01568
01569 if (i0 == i1 || flagTrk[i1] != 0) continue;
01570 TTrack & t1 = * _tracks[i1];
01571 if (! (t1.pt() < 0.13)) continue;
01572 Nall = t1.links().length();
01573 if (! Nall) continue;
01574
01575 Noverlap = 0;
01576 for (unsigned j = 0; j < Nall; j++) {
01577 TMLink & l = * t1.links()[j];
01578 const TMDCWireHit & whit = * l.hit();
01579 double load(3.);
01580 if (whit.state() & WireHitStereo) load = 4.;
01581
01582 double x = t0.helix().center().x() - l.positionOnTrack().x();
01583 double y = t0.helix().center().y() - l.positionOnTrack().y();
01584 double r = sqrt(x * x + y * y);
01585 double R = fabs(t0.helix().radius());
01586
01587 if ((R - load) < r && r < (R + load)) Noverlap++;
01588 }
01589
01590 if (! Noverlap) continue;
01591 float tmpRatio = float(Noverlap) / float(Nall);
01592
01593 if (tmpRatio > OverlapRatioMax) {
01594 OverlapRatioMax = tmpRatio;
01595 MaxID = i1;
01596 }
01597 }
01598
01599 if (OverlapRatioMax < 0.7) continue;
01600
01601
01602 unsigned MaskID[2] = {MaxID , i0};
01603 AList<TMLink> l[2];
01604
01605 for( unsigned j0=0;j0<2;j0++){
01606 for( unsigned j1=0;j1< _tracks[MaskID[j0]]->nLinks();j1++){
01607 TMLink &k = * _tracks[MaskID[j0]]->links()[j1];
01608 l[j0].append( k );
01609 }
01610 }
01611
01612
01613 _tracks[i0]->append(_tracks[MaxID]->links());
01614 _tracks[MaxID]->append(_tracks[i0]->links());
01615
01616 #ifdef TRKRECO_DEBUG_DETAIL
01617 std::cout << " mask & merge " << std::endl;
01618 std::cout << " 0:";
01619 Dump(l[0], "flag sort");
01620 std::cout << " 1:";
01621 Dump(l[1], "flag sort");
01622 std::cout << std::endl;
01623 #endif
01624
01625
01626 unsigned maskSide = 2;
01627
01628 #if 0
01629
01630 unsigned super0 = NSuperLayers(l[0]);
01631 unsigned super1 = NSuperLayers(l[1]);
01632
01633 if( super0 < super1 ) maskSide = 0;
01634 else if ( super0 > super1 ) maskSide = 1;
01635
01636 #ifdef TRKRECO_DEBUG_DETAIL
01637 std::cout << " NSuperLayers 0, 1 = " << NSuperLayers(l[0]) << ", ";
01638 std::cout << NSuperLayers(l[1]) << std::endl;
01639 #endif
01640
01641 if (maskSide == 2) {
01642 #endif
01643
01644
01645 unsigned inner0 = InnerMost(l[0])->wire()->layerId();
01646 unsigned inner1 = InnerMost(l[1])->wire()->layerId();
01647 if (inner0 < inner1 ) maskSide = 1;
01648 else if (inner0 > inner1) maskSide = 0;
01649
01650 if( maskSide == 2 ){
01651
01652
01653
01654
01655 TTrack * tt[2];
01656 tt[0] = new TTrack( *(_tracks[MaskID[0]]));
01657 tt[1] = new TTrack( *(_tracks[MaskID[1]]));
01658 _fitter.fit(* tt[0]);
01659 _fitter.fit(* tt[1]);
01660 Helix h0 = Helix(tt[0]->helix());
01661 Helix h1 = Helix(tt[1]->helix());
01662
01663
01664 h0.pivot(ORIGIN);
01665 h1.pivot(ORIGIN);
01666 if (fabs(h0.dz()) < fabs(h1.dz())) maskSide = 1;
01667 else maskSide = 0;
01668
01669 delete tt[0];
01670 delete tt[1];
01671 }
01672 #if 0
01673 }
01674 #endif
01675 bads.append(_tracks[MaskID[maskSide]]);
01676 flagTrk[MaskID[maskSide]] = 1;
01677 }
01678
01679
01680 _tracks.remove(bads);
01681
01682
01683 n = _tracks.length();
01684
01685 for( unsigned i=0;i<n;i++){
01686 TTrack & t = * _tracks[i];
01687 for( unsigned j=0;j<t.links().length();j++){
01688 TMLink & l = * t.links()[j];
01689 const TMDCWireHit & whit = * l.hit();
01690
01691 if( !(whit.state() & WireHitFittingValid) ) continue;
01692
01693
01694 double q = t.helix().center().x() * l.positionOnTrack().y() -
01695 t.helix().center().y() * l.positionOnTrack().x();
01696 double qq = q *t.charge();
01697
01698 if( qq > 0 ) whit.state(whit.state() & ~WireHitInvalidForFit);
01699 else whit.state(whit.state() | WireHitInvalidForFit);
01700 }
01701 }
01702
01703 free(flagTrk);
01704 }
01705
01706
01707 int
01708 TTrackManager::copyTrack(TTrack & t,
01709 MdcRec_trk ** pr,
01710 MdcRec_trk_add ** pra) const {
01711
01712 static const unsigned GoodHitMask = (WireHitTimeValid |
01713 WireHitChargeValid |
01714 WireHitFindingValid |
01715 WireHitFittingValid);
01716 int err = 0;
01717
01718
01719 #ifdef TRKRECO_DEBUG_DETAIL
01720 std::cout << " checking hits ... " << t.name()
01721 << " quality = " << t.quality();
01722 std::cout << " : " << t.cores().length() << ", " << t.ndf() << " : ";
01723 unsigned nnn = 0;
01724 #endif
01725 unsigned j = 0;
01726 unsigned nClst = 0;
01727 unsigned nStereos = 0;
01728 unsigned nOccupied = 0;
01729 AList<TMLink> hits;
01730 AList<TMLink> badHits;
01731 unsigned n = t.links().length();
01732 for (unsigned i = 0; i < n; i++) {
01733 TMLink * l = t.links()[i];
01734 MdcRec_wirhit * h = l->hit()->reccdc();
01735
01736 #ifdef TRKRECO_DEBUG_DETAIL
01737 if (h->trk) std::cout << l->wire()->name() << "(n/a),";
01738 #endif
01739
01740 if (h->trk) {
01741 ++nOccupied;
01742 if (! (h->stat & WireHitInvalidForFit))
01743 continue;
01744 }
01745 if ((l->hit()->state() & GoodHitMask) == GoodHitMask) {
01746 if (l->hit()->state() & WireHitInvalidForFit) {
01747 if (! (h->stat & WireHitInvalidForFit))
01748 badHits.append(l);
01749 }
01750 else {
01751 hits.append(l);
01752 if (l->wire()->stereo()) ++nStereos;
01753 }
01754 }
01755 }
01756 t.finalHits(hits);
01757 #ifdef TRKRECO_DEBUG_DETAIL
01758 std::cout << std::endl;
01759 #endif
01760
01761
01762 if (t.quality() & TrackQuality2D) {
01763 if (hits.length() < 3) err = 3;
01764 if (nOccupied > 2) err = 4;
01765 }
01766 else {
01767 if (hits.length() < 5) err = 1;
01768 if (nStereos < 2) err = 2;
01769 }
01770 if (err) return err;
01771
01772
01773
01774
01775
01776
01777 * pr = new MdcRec_trk;
01778 * pra = new MdcRec_trk_add;
01779 MdcRec_trk* r = * pr;
01780 MdcRec_trk_add* ra = * pra;
01781
01782
01783
01784
01785
01786
01787
01788
01789
01790
01791
01792 float chisq = 0.;
01793 unsigned nHits = hits.length();
01794 for (unsigned i = 0; i < nHits; i++) {
01795 TMLink * l = hits[i];
01796 MdcRec_wirhit * h = hits[i]->hit()->reccdc();
01797 h->trk = r;
01798 h->pChiSq = l->pull();
01799 h->lr = l->leftRight();
01800
01801 chisq += h->pChiSq;
01802 }
01803 r->chiSq = chisq;
01804 r->nhits = nHits;
01805 r->nster = nStereos;
01806 r->ndf = nHits - 5;
01807 if (t.quality() & TrackQuality2D)
01808 r->ndf = nHits - 3;
01809
01810
01811 n = badHits.length();
01812 for (unsigned i = 0; i < n; i++) {
01813 MdcRec_wirhit * h = badHits[i]->hit()->reccdc();
01814 h->trk = r;
01815 h->stat |= WireHitInvalidForFit;
01816 }
01817
01818
01819 r->nclus = nClst;
01820
01821
01822 const Vector & a = t.helix().a();
01823 const SymMatrix & ea = t.helix().Ea();
01824 const HepPoint3D & x = t.helix().pivot();
01825 r->helix[0] = a[0];
01826 r->helix[1] = a[1];
01827 r->helix[2] = a[2];
01828 r->helix[3] = a[3];
01829 r->helix[4] = a[4];
01830
01831 r->pivot[0] = x.x();
01832 r->pivot[1] = x.y();
01833 r->pivot[2] = x.z();
01834
01835 r->error[0] = ea[0][0];
01836 r->error[1] = ea[1][0];
01837 r->error[2] = ea[1][1];
01838 r->error[3] = ea[2][0];
01839 r->error[4] = ea[2][1];
01840 r->error[5] = ea[2][2];
01841 r->error[6] = ea[3][0];
01842 r->error[7] = ea[3][1];
01843 r->error[8] = ea[3][2];
01844 r->error[9] = ea[3][3];
01845 r->error[10] = ea[4][0];
01846 r->error[11] = ea[4][1];
01847 r->error[12] = ea[4][2];
01848 r->error[13] = ea[4][3];
01849 r->error[14] = ea[4][4];
01850
01851
01852 TMLink * last = OuterMost(hits);
01853
01854
01855 t.approach(* last);
01856 r->fiTerm = last->dPhi();
01857
01858 return err;
01859 }
01860
01861 void
01862 TTrackManager::sortTracksByQuality(void) {
01863 unsigned n = _tracks.length();
01864 if (n < 2) return;
01865
01866 for (unsigned i = 0; i < n - 1; i++) {
01867 TTrack & t0 = * _tracks[i];
01868 float bestRChisq = HUGE_VAL;
01869 if (t0.ndf() > 0) bestRChisq = t0.chi2() / t0.ndf();
01870 for (unsigned j = i + 1; j < n; j++) {
01871 TTrack & t1 = * _tracks[j];
01872 float rChisq = HUGE_VAL;
01873 if (t1.ndf() > 0) rChisq = t1.chi2() / t1.ndf();
01874 if (rChisq < bestRChisq) {
01875 bestRChisq = rChisq;
01876 _tracks.swap(i, j);
01877 }
01878 }
01879 }
01880 }
01881
01882 void
01883 TTrackManager::sortTracksByPt(void) {
01884 #ifdef TRKRECO_DEBUG_DETAIL
01885 std::cout << "trkmgr::sortTracksByPt : # of tracks="
01886 << _tracks.length() << std::endl;
01887 #endif
01888
01889 unsigned n = _tracks.length();
01890 if (n < 2) return;
01891
01892 for (unsigned i = 0; i < n - 1; i++) {
01893 TTrack & t0 = * _tracks[i];
01894 float bestPt = t0.pt();
01895 for (unsigned j = i + 1; j < n; j++) {
01896 TTrack & t1 = * _tracks[j];
01897 float pt = t1.pt();
01898 #ifdef TRKRECO_DEBUG_DETAIL
01899 std::cout << "i,j=" << i << "," << j
01900 << " : pt i,j=" << bestPt << "," << pt << std::endl;
01901 #endif
01902 if (pt > bestPt) {
01903 bestPt = pt;
01904 _tracks.swap(i, j);
01905 }
01906 }
01907 }
01908 }
01909
01910 void
01911 TTrackManager::treatCurler(MdcTrk & trk1,
01912 MdcRec_trk_add & cdc1,
01913 unsigned flag) const {
01914
01915
01916
01917 if (trk1.zero[2] == 0) return;
01918 if (cdc1.daughter == 0) return;
01919
01920
01921
01922
01923
01924
01925 MdcRec_trk_add & cdc2 = * cdc1.daughter->add;
01926
01927 if (cdc2.daughter == 0) return;
01928 if (cdc2.rectrk == 0) return;
01929
01930 MdcTrk & trk2 = * cdc2.rectrk;
01931
01932 if (trk2.zero[2] == 0) return;
01933
01934
01935
01936
01937
01938
01939
01940
01941 MdcTrk_localz & z1 = * trk1.zero[2];
01942 MdcTrk_localz & z2 = * trk2.zero[2];
01943
01944
01945 MdcRec_trk_add * mother = & cdc1;
01946 MdcRec_trk_add * daughter = & cdc2;
01947
01948
01949
01950
01951
01952
01953
01954
01955
01956
01957
01958
01959
01960
01961
01962
01963
01964
01965
01966
01967
01968
01969 if(flag == 3){
01970 float dz1 = fabs(z1.helix[3]);
01971 float dz2 = fabs(z2.helix[3]);
01972 if (fabs(dz1 - dz2) < 2.) flag = 1;
01973 else flag = 2;
01974 }
01975
01976
01977 if(flag == 1){
01978 float dr1 = fabs(z1.helix[0]);
01979 float dr2 = fabs(z2.helix[0]);
01980 if (dr1 > dr2) {
01981 mother = & cdc2;
01982 daughter = & cdc1;
01983 }
01984 }
01985
01986
01987 else if(flag == 2){
01988 float dz1 = fabs(z1.helix[3]);
01989 float dz2 = fabs(z2.helix[3]);
01990 if (dz1 > dz2) {
01991 mother = & cdc2;
01992 daughter = & cdc1;
01993 }
01994 }
01995
01996
01997 mother->quality &= (~ TrackQualityOutsideCurler);
01998 mother->likelihood[0] = 1.;
01999 mother->decision |= TrackTrackManager;
02000
02001 mother->daughter = daughter->body;
02002 mother->mother = 0;
02003
02004 daughter->quality |= TrackQualityOutsideCurler;
02005 daughter->likelihood[0] = 0.;
02006 daughter->mother = mother->body;
02007 daughter->daughter = 0;
02008 daughter->decision |= TrackTrackManager;
02009 }
02010
02011 void
02012 TTrackManager::sortBanksByPt(void) const {
02013 #ifdef TRKRECO_DEBUG_DETAIL
02014 std::cout << "trkmgr::sortBanksByPt : # of tracks="
02015
02016 << MdcRecTrkAddCol::getMdcRecTrkAddCol()->size() << std::endl;
02017 #endif
02018
02019
02020 unsigned n = MdcRecTrkAddCol::getMdcRecTrkAddCol()->size();
02021 if (n < 2) return;
02022
02023
02024 unsigned * id = (unsigned *) malloc(n * sizeof(unsigned));
02025 for (unsigned i = 0; i < n; i++) id[i] = i;
02026 for (unsigned i = 0; i < n - 1; i++) {
02027
02028
02029
02030
02031
02032
02033
02034
02035
02036
02037
02038
02039 MdcRec_trk & cdc0 = (* MdcRecTrkCol::getMdcRecTrkCol())[i];
02040 MdcRec_trk_add & add0 = (* MdcRecTrkAddCol::getMdcRecTrkAddCol())[i];
02041 MdcRec_mctrk & mc0 = (* MdcRecMctrkCol::getMdcRecMctrkCol())[i];
02042
02043 float bestPt = 1. / fabs(cdc0.helix[2]);
02044 unsigned bestQuality = add0.quality;
02045 for (unsigned j = i + 1; j < n; j++) {
02046
02047
02048
02049
02050
02051
02052
02053
02054
02055
02056
02057
02058 MdcRec_trk & cdc1 = (* MdcRecTrkCol::getMdcRecTrkCol())[j];
02059 MdcRec_trk_add & add1 = (* MdcRecTrkAddCol::getMdcRecTrkAddCol())[j];
02060 MdcRec_mctrk & mc1 = (* MdcRecMctrkCol::getMdcRecMctrkCol())[j];
02061
02062 float pt = 1. / fabs(cdc1.helix[2]);
02063 #ifdef TRKRECO_DEBUG_DETAIL
02064 std::cout << "i,j=" << i << "," << j
02065 << " : quality i,j=" << bestQuality << ","
02066 << add1.quality << std::endl;
02067 #endif
02068 unsigned quality = add1.quality;
02069 if (quality > bestQuality) continue;
02070 else if (quality < bestQuality) {
02071 bestQuality = quality;
02072 bestPt = pt;
02073 swapReccdc(cdc0, add0, mc0, cdc1, add1, mc1);
02074 unsigned tmp = id[i];
02075 id[i] = id[j];
02076 id[j] = tmp;
02077 #ifdef TRKRECO_DEBUG_DETAIL
02078 std::cout << "swapped" << std::endl;
02079 #endif
02080 continue;
02081 }
02082 #ifdef TRKRECO_DEBUG_DETAIL
02083 std::cout << "i,j=" << i << "," << j
02084 << " : pt i,j=" << bestPt << "," << pt << std::endl;
02085 #endif
02086 if (pt > bestPt) {
02087 #ifdef TRKRECO_DEBUG_DETAIL
02088 std::cout << "swapping ... " << & cdc0 << "," << & add0 << ","
02089 << & mc0 << " <-> " << & cdc1 << "," << & add1 << ","
02090 << & mc1 << std::endl;
02091 #endif
02092 bestQuality = quality;
02093 bestPt = pt;
02094 swapReccdc(cdc0, add0, mc0, cdc1, add1, mc1);
02095 unsigned tmp = id[i];
02096 id[i] = id[j];
02097 id[j] = tmp;
02098 #ifdef TRKRECO_DEBUG_DETAIL
02099 std::cout << "swapped" << std::endl;
02100 #endif
02101 }
02102 }
02103 }
02104 #ifdef TRKRECO_DEBUG_DETAIL
02105 std::cout << "trkmgr::sortBanksByPt : first phase finished" << std::endl;
02106 #endif
02107
02108 tagReccdc(id, n);
02109 free(id);
02110
02111 #ifdef TRKRECO_DEBUG_DETAIL
02112 std::cout << "trkmgr::sortBanksByPt : second phase finished" << std::endl;
02113 #endif
02114
02115 #if 0
02116
02117 n = BsCouTab(RECTRK);
02118 id = (unsigned *) malloc(n * sizeof(unsigned));
02119 for (unsigned i = 0; i < n; i++) id[i] = i;
02120 if (n > 1) {
02121 unsigned i = 0;
02122 while (i < n - 1) {
02123 rectrk & t = * (rectrk *) BsGetEnt(RECTRK, i + 1, BBS_No_Index);
02124 if (t.m_prekal == (i + 1)) {
02125 ++i;
02126 continue;
02127 }
02128
02129 rectrk & s = * (rectrk *) BsGetEnt(RECTRK,
02130 t.m_prekal,
02131 BBS_No_Index);
02132
02133 swapRectrk(t, s);
02134 unsigned tmp = id[i];
02135 id[i] = id[s.m_ID - 1];
02136 id[s.m_ID - 1] = tmp;
02137
02138
02139
02140 reccdc_trk_add & a =
02141 * (reccdc_trk_add *) BsGetEnt(RECMDC_TRK_ADD,
02142 t.m_prekal,
02143 BBS_No_Index);
02144 reccdc_trk_add & b =
02145 * (reccdc_trk_add *) BsGetEnt(RECMDC_TRK_ADD,
02146 s.m_prekal,
02147 BBS_No_Index);
02148 a.m_rectrk = t.m_ID;
02149 b.m_rectrk = s.m_ID;
02150 }
02151 }
02152 #else
02153
02154
02155
02156
02157
02158
02159
02160
02161
02162
02163
02164
02165
02166
02167
02168
02169
02170
02171
02172
02173
02174
02175
02176
02177
02178
02179
02180
02181
02182
02183
02184
02185
02186
02187
02188
02189
02190
02191
02192
02193
02194
02195 #endif
02196
02197 tagRectrk(id, n);
02198 free(id);
02199 }
02200
02201 void
02202 TTrackManager::swapReccdc(MdcRec_trk & cdc0,
02203 MdcRec_trk_add & add0,
02204 MdcRec_mctrk & mc0,
02205 MdcRec_trk & cdc1,
02206 MdcRec_trk_add & add1,
02207 MdcRec_mctrk & mc1) const {
02208 #define RECMDC_ACTUAL_SIZE 124
02209 #define RECMDCADD_ACTUAL_SIZE 40
02210 #define RECMDCMC_ACTUAL_SIZE 28
02211
02212 static bool first = true;
02213 static void * swapRegion;
02214 if (first) {
02215 first = false;
02216 swapRegion = malloc(RECMDC_ACTUAL_SIZE);
02217 }
02218
02219 void * s0 = & cdc0.helix[0];
02220 void * s1 = & cdc1.helix[0];
02221 memcpy(swapRegion, s0, RECMDC_ACTUAL_SIZE);
02222 memcpy(s0, s1, RECMDC_ACTUAL_SIZE);
02223 memcpy(s1, swapRegion, RECMDC_ACTUAL_SIZE);
02224
02225 s0 = & add0.quality;
02226 s1 = & add1.quality;
02227 memcpy(swapRegion, s0, RECMDCADD_ACTUAL_SIZE);
02228 memcpy(s0, s1, RECMDCADD_ACTUAL_SIZE);
02229 memcpy(s1, swapRegion, RECMDCADD_ACTUAL_SIZE);
02230
02231 if ((& mc0) && (& mc1)) {
02232 s0 = & mc0.hep;
02233 s1 = & mc1.hep;
02234 memcpy(swapRegion, s0, RECMDCMC_ACTUAL_SIZE);
02235 memcpy(s0, s1, RECMDCMC_ACTUAL_SIZE);
02236 memcpy(s1, swapRegion, RECMDCMC_ACTUAL_SIZE);
02237 }
02238 }
02239
02240 void
02241 TTrackManager::swapRectrk(MdcTrk & rec0,
02242 MdcTrk & rec1) const {
02243 #define RECTRK_ACTUAL_SIZE 84
02244
02245 static bool first = true;
02246 static void * swapRegion;
02247 if (first) {
02248 first = false;
02249 swapRegion = malloc(RECTRK_ACTUAL_SIZE);
02250 }
02251
02252 void * s0 = & rec0.glob[0];
02253 void * s1 = & rec1.glob[0];
02254 memcpy(swapRegion, s0, RECTRK_ACTUAL_SIZE);
02255 memcpy(s0, s1, RECTRK_ACTUAL_SIZE);
02256 memcpy(s1, swapRegion, RECTRK_ACTUAL_SIZE);
02257 }
02258
02259 void
02260 TTrackManager::tagReccdc(unsigned * id0, unsigned nTrk) const {
02261
02262
02263
02264
02265
02266
02267
02268
02269
02270
02271
02272
02273
02274
02275
02276
02277
02278
02279
02280
02281
02282
02283
02284
02285
02286
02287
02288
02289
02290
02291
02292
02293
02294
02295
02296
02297
02298
02299
02300
02301
02302
02303
02304
02305
02306
02307
02308
02309
02310
02311
02312
02313
02314
02315
02316
02317
02318
02319
02320
02321
02322
02323
02324
02325
02326
02327
02328
02329
02330
02331
02332
02333
02334
02335
02336
02337
02338 }
02339
02340 void
02341 TTrackManager::setCurlerFlags(void) {
02342 unsigned n = _tracks.length();
02343 if (n < 2) return;
02344
02345 for (unsigned i = 0; i < n - 1; i++) {
02346 TTrack & t0 = * _tracks[i];
02347 if (t0.type() != TrackTypeCurl) continue;
02348 float c0 = t0.charge();
02349
02350 for (unsigned j = i + 1; j < n; j++) {
02351 TTrack & t1 = * _tracks[j];
02352 float c1 = t1.charge();
02353 if (c0 * c1 > 0.) continue;
02354 if (t1.type() != TrackTypeCurl) continue;
02355
02356 bool toBeMerged = false;
02357 unsigned n0 = t0.testByApproach(t1.cores(), _sigmaCurlerMergeTest);
02358 if (n0 > _nCurlerMergeTest) toBeMerged = true;
02359 if (! toBeMerged) {
02360 unsigned n1 = t1.testByApproach(t0.cores(),
02361 _sigmaCurlerMergeTest);
02362 if (n1 > _nCurlerMergeTest) toBeMerged = true;
02363 }
02364
02365 if (toBeMerged) {
02366
02367 if ((t0.daughter()) || (t1.daughter()))
02368 ++_s->_nToBeMergedMoreThanTwo;
02369 t0.daughter(& t1);
02370 t1.daughter(& t0);
02371
02372 }
02373 }
02374 }
02375 }
02376
02377 void
02378 TTrackManager::salvageAssociateHits(const AList<TMDCWireHit> & hits,
02379 float maxSigma2) {
02380
02381
02382
02383 #ifdef TRKRECO_DEBUG
02384 std::cout << "... trkmgr::salvaging associate hits" << std::endl;
02385 std::cout << " # of given hits=" << hits.length() << std::endl;
02386 #endif
02387
02388
02389 unsigned nTracks = _tracks.length();
02390 if (nTracks == 0) return;
02391 unsigned nHits = hits.length();
02392 if (nHits == 0) return;
02393
02394 static const TPoint2D o(0., 0.);
02395
02396
02397 for (unsigned i = 0; i < nHits; i++) {
02398 TMDCWireHit & h = * hits[i];
02399
02400
02401 if (h.state() & WireHitUsed) continue;
02402 #ifdef TRKRECO_DEBUG_DETAIL
02403 std::cout << " checking " << h.wire()->name();
02404 #endif
02405
02406
02407 AList<TMLink> toBeDeleted;
02408 TMLink * best = NULL;
02409 TTrack * bestTrack = NULL;
02410 for (unsigned j = 0; j < nTracks; j++) {
02411 TTrack & t = * _tracks[j];
02412
02413
02414 TPoint2D c = t.center();
02415 TPoint2D co = - c;
02416 TPoint2D x = h.wire()->xyPosition();
02417
02418 #ifdef TRKRECO_DEBUG_DETAIL
02419 std::cout << " : c= " << co.cross(x - c) * t.charge();
02420 std::cout << ", d=" << fabs((x - c).mag() - fabs(t.radius()));
02421 #endif
02422
02423 if (co.cross(x - c) * t.charge() > 0.)
02424 continue;
02425 if (fabs((x - c).mag() - fabs(t.radius())) > 5.)
02426 continue;
02427
02428
02429 TMLink & link = * new TMLink(0, & h);
02430 int err = t.approach(link);
02431 if (err < 0) {
02432 #ifdef TRKRECO_DEBUG_DETAIL
02433 std::cout << " : " << t.name() << " approach failure";
02434 #endif
02435 toBeDeleted.append(link);
02436 continue;
02437 }
02438
02439
02440 float distance = link.distance();
02441 float diff = fabs(distance - link.hit()->drift());
02442 float sigma = diff / link.hit()->dDrift();
02443 link.pull(sigma * sigma);
02444
02445 #ifdef TRKRECO_DEBUG_DETAIL
02446 std::cout << " : " << t.name() << " pull = " << link.pull();
02447 #endif
02448 if (link.pull() > maxSigma2) {
02449 toBeDeleted.append(link);
02450 continue;
02451 }
02452
02453 if (best) {
02454 if (best->pull() > link.pull()) {
02455 toBeDeleted.append(best);
02456 best = & link;
02457 bestTrack = & t;
02458 }
02459 else {
02460 toBeDeleted.append(link);
02461 }
02462 }
02463 else {
02464 best = & link;
02465 bestTrack = & t;
02466 }
02467 }
02468
02469 if (best) {
02470 bestTrack->append(* best);
02471 best->hit()->state(best->hit()->state() | WireHitInvalidForFit);
02472 _associateHits.append(best);
02473 #ifdef TRKRECO_DEBUG
02474 std::cout << " " << best->hit()->wire()->name();
02475 std::cout << " -> " << bestTrack->name() << std::endl;
02476 #endif
02477 }
02478 HepAListDeleteAll(toBeDeleted);
02479
02480 #ifdef TRKRECO_DEBUG_DETAIL
02481 std::cout << std::endl;
02482 #endif
02483 }
02484 }
02485
02486 void
02487 TTrackManager::maskBadHits(const AList<TTrack> & tracks, float maxSigma2) {
02488 #ifdef TRKRECO_DEBUG
02489 std::cout << "... trkmgr::maskBadHits" << std::endl;
02490 #endif
02491
02492 unsigned n = tracks.length();
02493 for (unsigned i = 0; i < n; i++) {
02494 TTrack & t = * tracks[i];
02495 bool toBeUpdated = false;
02496 const AList<TMLink> links = t.links();
02497 unsigned nHits = links.length();
02498 for (unsigned j = 0; j < nHits; j++) {
02499 if (links[j]->pull() > maxSigma2) {
02500 links[j]->hit()->state(links[j]->hit()->state() |
02501 WireHitInvalidForFit);
02502 toBeUpdated = true;
02503 #ifdef TRKRECO_DEBUG
02504 std::cout << " " << t.name() << " : ";
02505 std::cout << links[j]->wire()->name() << "(pull=";
02506 std::cout << links[j]->pull() << ") is masked" << std::endl;
02507 #endif
02508 }
02509 }
02510 if (toBeUpdated) t.update();
02511 }
02512 }
02513
02514 void
02515 TTrackManager::clearTables(void) const {
02516
02517
02518
02519
02520 unsigned n = MdcRecTrkCol::getMdcRecTrkCol()->size();
02521 for (unsigned i = 0; i < n; i++) delete &(*MdcRecTrkCol::getMdcRecTrkCol())[i];
02522
02523 n = MdcRecTrkAddCol::getMdcRecTrkAddCol()->size();
02524 for (unsigned i = 0; i < n; i++) delete &(*MdcRecTrkAddCol::getMdcRecTrkAddCol())[i];
02525
02526 n = MdcRecMctrkCol::getMdcRecMctrkCol()->size();
02527 for (unsigned i = 0; i < n; i++) delete &(*MdcRecMctrkCol::getMdcRecMctrkCol())[i];
02528
02529 n = MdcRecMctrk2hepCol::getMdcRecMctrk2hepCol()->size();
02530 for (unsigned i = 0; i < n; i++) delete &(*MdcRecMctrk2hepCol::getMdcRecMctrk2hepCol())[i];
02531
02532
02533
02534
02535 n = MdcRecWirhitCol::getMdcRecWirhitCol()->size();
02536 for (unsigned i = 0; i < n; i++) {
02537
02538
02539
02540 MdcRec_wirhit& h = (*MdcRecWirhitCol::getMdcRecWirhitCol())[i];
02541 h.trk = 0;
02542 }
02543
02544 n = MdcDatMcwirhitCol::getMdcDatMcwirhitCol()->size();
02545 for (unsigned i = 0; i < n; i++) {
02546
02547
02548
02549 MdcDat_mcwirhit& h = (*MdcDatMcwirhitCol::getMdcDatMcwirhitCol())[i];
02550 h.trk = 0;
02551 }
02552 }
02553
02554 AList<TTrack>
02555 TTrackManager::selectGoodTracks(const AList<TTrack> & list,
02556 bool track2D) const {
02557 AList<TTrack> goodTracks;
02558 unsigned n = list.length();
02559 for (unsigned i = 0; i < n; i++) {
02560 const TTrack & t = * list[i];
02561 if (! goodTrack(t, track2D)) continue;
02562
02563
02564 if (_maxMomentum > 0.) {
02565 if (t.ptot() > _maxMomentum) {
02566
02567 continue;
02568 }
02569 }
02570
02571 goodTracks.append((TTrack &) t);
02572 }
02573
02574 if (_debugLevel) {
02575 if (list.length() != goodTracks.length()) {
02576 std::cout << "TTrackManager::selectGoodTracks ... bad tracks found"
02577 << std::endl
02578 << " # of bad tracks = "
02579 << list.length() - goodTracks.length()
02580 << " : 2D flag = " << track2D << std::endl;
02581 AList<TTrack> tmp;
02582 tmp.append(list);
02583 tmp.remove(goodTracks);
02584 std::cout << " Track dump" << std::endl;
02585 for (unsigned i = 0; i < tmp.length(); i++) {
02586 std::cout << " " << TrackDump(* tmp[i]) << std::endl;
02587 }
02588 }
02589 }
02590
02591 return goodTracks;
02592 }
02593
02594 bool
02595 TTrackManager::checkNumberOfHits(const TTrack & t, bool track2D) {
02596 const AList<TMLink> & cores = t.cores();
02597
02598 if (track2D) {
02599 unsigned axialHits = NAxialHits(cores);
02600 if (axialHits < 3) return false;
02601 }
02602 else {
02603 unsigned allHits = cores.length();
02604 if (allHits < 5) return false;
02605 unsigned stereoHits = NStereoHits(cores);
02606 if (stereoHits < 2) return false;
02607 unsigned axialHits = allHits - stereoHits;
02608 if (axialHits < 3) return false;
02609 }
02610 return true;
02611 }
02612
02613 void
02614 TTrackManager::determineIP(void) {
02615 static const HepVector3D InitialVertex(0., 0., 0.);
02616
02617
02618
02619 unsigned n = MdcTrkCol::getMdcTrkCol()->size();
02620 AList<MdcTrk_localz> zList;
02621 for (unsigned i = 0; i < n; i++) {
02622
02623 const MdcTrk & t = (* MdcTrkCol::getMdcTrkCol())[i];
02624
02625 if (t.prekal == 0) continue;
02626
02627
02628 const MdcRec_trk_add & c = * t.prekal->add;
02629
02630
02631 if (c.quality) continue;
02632
02633
02634
02635
02636
02637 const MdcTrk_global & g = * t.glob[2];
02638
02639 if (! & g) continue;
02640 if (g.nhits[3] < 2) continue;
02641 if (g.nhits[4] < 2) continue;
02642
02643
02644
02645
02646
02647 MdcTrk_localz & z = * t.zero[2];
02648
02649 if (! & z) continue;
02650
02651 zList.append(z);
02652 }
02653 unsigned nZ = zList.length();
02654 if (nZ < 2) return;
02655
02656
02657
02658
02659
02660
02661
02662
02663 }
02664
02665 void
02666 TTrackManager::tagRectrk(unsigned * id0, unsigned nTrk) const {
02667
02668
02669
02670
02671
02672
02673
02674
02675
02676
02677
02678
02679
02680
02681
02682
02683
02684
02685
02686
02687
02688
02689
02690
02691
02692
02693
02694
02695
02696
02697
02698
02699 }
02700
02701
02702
02703
02704
02705
02706
02707
02708
02709
02710
02711
02712
02713
02714
02715
02716
02717
02718
02719
02720
02721
02722
02723
02724
02725
02726
02727
02728
02729
02730
02731
02732
02733
02734
02735
02736
02737
02738
02739
02740
02741
02742
02743
02744
02745
02746
02747
02748
02749
02750
02751
02752
02753
02754
02755
02756
02757
02758
02759
02760
02761
02762
02763
02764
02765
02766
02767
02768
02769
02770
02771
02772
02773
02774
02775
02776
02777
02778
02779
02780
02781
02782
02783
02784
02785
02786
02787
02788
02789
02790
02791
02792
02793
02794
02795
02796
02797
02798
02799
02800
02801
02802
02803
02804
02805
02806
02807
02808
02809
02810
02811
02812
02813
02814
02815
02816
02817
02818
02819
02820
02821
02822
02823
02824
02825
02826
02827
02828
02829
02830
02831
02832
02833
02834
02835
02836
02837
02838
02839
02840
02841
02842
02843
02844
02845
02846
02847
02848
02849
02850
02851
02852
02853
02854
02855
02856
02857
02858
02859
02860
02861
02862
02863
02864
02865
02866
02867
02868
02869
02870
02871
02872
02873
02874
02875
02876
02877
02878
02879
02880
02881
02882
02883
02884
02885
02886
02887
02888
02889
02890
02891
02892
02893
02894
02895
02896
02897
02898
02899
02900
02901
02902
02903
02904
02905
02906
02907
02908
02909
02910
02911
02912
02913
02914
02915
02916
02917
02918
02919
02920
02921
02922
02923
02924
02925
02926
02927
02928
02929
02930
02931
02932
02933
02934
02935
02936
02937
02938
02939
02940
02941
02942
02943
02944
02945
02946
02947
02948
02949
02950
02951
02952
02953
02954
02955
02956
02957
02958
02959
02960
02961
02962
02963
02964
02965
02966
02967
02968
02969
02970
02971
02972
02973
02974
02975
02976
02977
02978
02979
02980
02981
02982
02983
02984
02985
02986
02987
02988
02989
02990
02991
02992
02993
02994
02995
02996
02997
02998
02999
03000
03001
03002
03003
03004
03005
03006
03007
03008
03009
03010
03011
03012
03013
03014 bool
03015 TTrackManager::goodTrack(const TTrack & t, bool track2D) {
03016
03017
03018 if (! checkNumberOfHits(t, track2D)) return false;
03019
03020
03021 if (HelixHasNan(t.helix())) return false;
03022
03023 return true;
03024 }