00104 {
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 if (8 == tkParam.lPrint){
00116 std::cout << "=======Print before arbitrateHits=======" << std::endl;
00117 }
00118
00119 int nDeleted = 0;
00120 std::vector<MdcTrack*> trksToKill;
00121 trksToKill.reserve(4);
00122
00123 MdcMap<long,long> idMap;
00124
00125
00126 int* usedInTrackNum = new int [nTrack()];
00127
00128 MdcTrack** trkXRef = new MdcTrack* [nTrack()];
00129
00130 int *refitTrack = new int [nTrack()];
00131 for (int i = 0; i < nTrack(); i++) {
00132 refitTrack[i] = 0;
00133 }
00134
00135
00136 int itrack;
00137 for (itrack = 0; itrack < nTrack(); itrack++) {
00138 MdcTrack *atrack = (*this)[itrack];
00139 if (atrack == 0) continue;
00140 idMap.put(atrack->track().id(), itrack);
00141 trkXRef[itrack] = atrack;
00142 }
00143
00144 for (itrack = 0; itrack < nTrack(); itrack++) {
00145
00146 if (8 == tkParam.lPrint) std::cout<<"arbitrate track No."<<itrack<< std::endl;
00147 MdcTrack *atrack = (*this)[itrack];
00148 if (atrack == 0) continue;
00149 TrkRecoTrk& aRecoTrk = atrack->track();
00150 int lRefit = 0;
00151 int trackOld = -1;
00152 const TrkFit* tkFit = aRecoTrk.fitResult();
00153 assert (tkFit != 0);
00154 TrkHitList* hitList = aRecoTrk.hits();
00155 assert (hitList != 0);
00156 restart:
00157 for (int ii = 0; ii < nTrack(); ii++) usedInTrackNum[ii] = 0;
00158
00159
00160 int nPrev = 0;
00161 int nHitDeleted = 0;
00162 int nHitInLayer[43];
00163 int nDeleteInLayer[43];
00164 for(int i=0;i<43;i++){
00165 nHitInLayer[i]=0;
00166 nDeleteInLayer[i]=0;
00167 }
00168 if(8 == tkParam.lPrint) std::cout<< "--arbitrate--"<<std::endl;
00169 for (TrkHitList::hot_iterator ihit(hitList->begin()); ihit != hitList->end(); ++ihit){
00170 int nUsed = ihit->hit()->nUsedHits();
00171 if (8 == tkParam.lPrint){
00172 std::cout<<"nUsed="<<nUsed<<":";
00173 ihit->hit()->printAll(std::cout);
00174 }
00175 if (8 == tkParam.lPrint) {
00176 double deltaChi = -999;
00177 ihit->getFitStuff(deltaChi);
00178 std::cout<< "deltaChi="<<deltaChi<<std::endl;
00179 }
00180 int layer = ihit->layerNumber();
00181 nHitInLayer[layer]++;
00182
00183 if (!ihit->isActive()) {
00184
00185
00186
00187 if(tkParam.lRemoveInActive ) {
00188 if (8 == tkParam.lPrint) {
00189 std::cout<< "=remove above inactive "<<std::endl;
00190 }
00191 TrkFundHit* hit = const_cast<TrkFundHit*> (ihit->hit());
00192 if(hitList->removeHit(hit)){ hit->setUnusedHit(&(*ihit)); }
00193 if(ihit == hitList->end()) break;
00194 --ihit;
00195 nDeleteInLayer[layer]++;
00196 }
00197 continue;
00198 }
00199 if (nUsed > 1) {
00200 bool wasUsed = false;
00201 std::pair<TrkFundHit::hot_iterator,TrkFundHit::hot_iterator> q =
00202 ihit->hit()->getUsedHits();
00203 for (TrkFundHit::hot_iterator i = q.first; i != q.second; ++i) {
00204 if ( !i->isActive() ) continue;
00205 TrkRecoTrk * recoTrk=i->parentTrack();
00206 int id = recoTrk->id();
00207 if (id == aRecoTrk.id()) continue;
00208 long index = 0;
00209 idMap.get(id, index);
00210 assert(index >= 0);
00211 usedInTrackNum[index]++;
00212 if (8 == tkParam.lPrint){
00213 std::cout<<" track "<<itrack<<"&" <<index
00214 << " shared hits "<<usedInTrackNum[index]<<":";
00215 ihit->printAll(std::cout);
00216 }
00217 wasUsed = true;
00218 }
00219 if (wasUsed) nPrev++;
00220 }
00221 }
00222
00223 for (int i=0;i<43;i++){
00224 if (8 == tkParam.lPrint) {
00225 std::cout<<i<<" nHitInLayer "<<nHitInLayer[i]
00226 <<" nDeleteInLayer "<<nDeleteInLayer[i]<<std::endl;
00227 }
00228 if(nHitInLayer[i]>0 && (nHitInLayer[i]-nDeleteInLayer[i])==0) {
00229 nHitDeleted++;
00230 if (8 == tkParam.lPrint) {
00231 cout << "rec hits have been deleted in this layer"<<std::endl;
00232 }
00233 }
00234 }
00235
00236 if (8 == tkParam.lPrint) { cout << " nHitDeleted = "<<nHitDeleted<<std::endl; }
00237
00238
00239 if (nHitDeleted >= tkParam.nHitDeleted) {
00240 if (8 == tkParam.lPrint) {
00241 cout << " nHitDeleted "<<nHitDeleted<<" > "<<tkParam.nHitDeleted
00242 <<" Killing tkNo " << itrack << endl;
00243 }
00244 nDeleted++;
00245 delete &(atrack->track());
00246 atrack->setTrack(0);
00247 trksToKill.push_back(atrack);
00248 continue;
00249 }
00250
00251
00252
00253 int nMost = 0;
00254 int trackMost = 0;
00255 for (int ii = 0; ii < nTrack(); ii++) {
00256 if (8 == tkParam.lPrint){
00257 std::cout<<"tk:"<<itrack<<"&"<<ii
00258 <<" shared "<<usedInTrackNum[ii]<<" hits "<< std::endl;
00259 }
00260 if (usedInTrackNum[ii] > nMost) {
00261 nMost = usedInTrackNum[ii];
00262 trackMost = ii;
00263 }
00264 }
00265
00266
00267 if (trackMost == trackOld) {
00268 std::cout << "ErrMsg(error) MdcTrackListBase:"
00269 << "Something ghastly happened in MdcTrackListBase::arbitrateHits"
00270 << std::endl;
00271 return 0;
00272 }
00273 trackOld = trackMost;
00274
00275
00276
00277
00278 double groupDiff = 0.0;
00279
00280 int nFound = 0;
00281 TrkHitOnTrk **theseHits = 0;
00282 TrkHitOnTrk **thoseHits = 0;
00283 int lGroupHits = 0;
00284
00285 if (nMost >= tkParam.nOverlap) {
00286 if (8 == tkParam.lPrint){
00287 std::cout<<"track "<<trackMost<<" shared "<<nMost<<" hits > Cut nOverlap "
00288 <<tkParam.nOverlap<<", group hits!"<<std::endl;
00289 }
00290 lGroupHits = 1;
00291 theseHits = new TrkHitOnTrk*[nMost];
00292 thoseHits = new TrkHitOnTrk*[nMost];
00293 }
00294
00295
00296
00297
00298
00299 if(8 == tkParam.lPrint) std::cout<<"Go back through hits, looking up overlap hits"<< std::endl;
00300 if (nMost > 0) {
00301 if (8 == tkParam.lPrint) std::cout<<" nHits= "<< hitList->nHit()<< std::endl;
00302 for (TrkHitList::hot_iterator ihit(hitList->begin()); ihit != hitList->end(); ++ihit) {
00303 int nUsed = ihit->hit()->nUsedHits();
00304
00305 if (8 == tkParam.lPrint){
00306 std::cout<< "--hit go back, nUsed="<<nUsed<<":";
00307 ihit->hit()->printAll(std::cout);
00308 }
00309
00310
00311 if (nUsed < 2) { continue; }
00312
00313
00314 if (!ihit->isActive()) {
00315 if (8 == tkParam.lPrint){ std::cout<<"act=0 continue"<<std::endl; }
00316 continue;
00317 }
00318
00319
00320 std::pair<TrkFundHit::hot_iterator,TrkFundHit::hot_iterator> q = ihit->hit()->getUsedHits();
00321 while (q.first!=q.second) {
00322 int dropThisHit = 0;
00323 TrkHitOnTrk *otherHot = const_cast<TrkHitOnTrk*>((--q.second).get());
00324 TrkRecoTrk *otherTrack = otherHot->parentTrack();
00325
00326 if (!otherHot->isActive()) continue;
00327
00328
00329 if ( &aRecoTrk == otherTrack) continue;
00330 int otherId = otherTrack->id();
00331 long otherIndex = -1;
00332 idMap.get(otherId, otherIndex); assert(otherIndex >= 0);
00333
00334
00335 if (lGroupHits && otherIndex != trackMost) continue;
00336
00337 if (lGroupHits) {
00338 if (8 == tkParam.lPrint) {
00339 std::cout<<"group hits "<< std::endl;
00340 }
00341
00342
00343
00344
00345 int aDof = tkFit->nActive() - 5;
00346 assert (otherTrack->fitResult() != 0);
00347 int otherDof = otherTrack->fitResult()->nActive() - 5;
00348 if (aDof <= 0) {groupDiff = 999;}
00349 else if (otherDof <= 0) {groupDiff = -999;}
00350 else {
00351 groupDiff += ihit->resid(0) * ihit->resid(0) * ihit->weight() /
00352 aDof -
00353 otherHot->resid(0) * otherHot->resid(0) * otherHot->weight() /
00354 otherDof;
00355 }
00356 theseHits[nFound] = const_cast<TrkHitOnTrk*>(ihit.get());
00357 thoseHits[nFound] = otherHot;
00358 nFound++;
00359 dropThisHit = 1;
00360 } else {
00361
00362 if (8 == tkParam.lPrint) {
00363 std::cout<<"handle hits individually"<< std::endl;
00364 }
00365 nFound++;
00366 if (fabs(ihit->resid(0)) > fabs(otherHot->resid(0)) ) {
00367
00368 lRefit = 1;
00369
00370
00371 const_cast<TrkHitOnTrk*>(ihit.get())->setActivity(0);
00372 dropThisHit = 1;
00373 if (8 == tkParam.lPrint) {
00374 std::cout<<"dorp hit ";
00375 const_cast<TrkHitOnTrk*>(ihit.get())->print(std::cout);
00376 }
00377 break;
00378 } else {
00379
00380 refitTrack[otherIndex] = 1;
00381
00382 otherHot->setActivity(0);
00383 if (8 == tkParam.lPrint) {
00384 std::cout<<"inactive hit on other track";
00385 const_cast<TrkHitOnTrk*>(ihit.get())->print(std::cout);
00386 }
00387 break;
00388 }
00389 }
00390
00391 if (dropThisHit == 1) break;
00392
00393 }
00394
00395
00396 if (lGroupHits && nFound == nMost || nFound == nPrev) {
00397 if (8 == tkParam.lPrint) {
00398 std::cout<<"we've found all of the shared hits on this track,Quit"<<std::endl;
00399 }
00400 break;
00401 }
00402
00403 }
00404
00405
00406 if (lGroupHits) {
00407 if (8 == tkParam.lPrint) {
00408 cout << "nGroup: " << nMost << " groupDiff: " << groupDiff << endl;
00409 cout << "Track: " << aRecoTrk.id() << " nHit: "
00410 << hitList->nHit() << " nActive: "
00411 << tkFit->nActive() << " chisq/dof: " <<
00412 tkFit->chisq()/(tkFit->nActive() - 5) << endl;
00413 TrkRecoTrk& othTrack = trkXRef[trackMost]->track();
00414 cout << "Track: "<< othTrack.id() << " nHit: " <<
00415 othTrack.hits()->nHit() << " nActive: " <<
00416 othTrack.fitResult()->nActive() << " chisq/dof: " <<
00417 othTrack.fitResult()->chisq() /
00418 (othTrack.fitResult()->nActive() - 5) << endl;
00419 }
00420
00421 if (groupDiff > 0.0) {
00422
00423 lRefit = 1;
00424 for (int ii = 0; ii < nMost; ii++) {
00425 TrkHitOnTrk *alink = theseHits[ii];
00426 TrkFundHit* hit = const_cast<TrkFundHit*> (alink->hit());
00427 if(hitList->removeHit(hit)){
00428 alink->hit()->setUnusedHit(alink);
00429 }
00430
00431 }
00432 if (8 == tkParam.lPrint) std::cout<<"inactive hits on this track, No."<<aRecoTrk.id()<< std::endl;
00433 } else {
00434
00435 refitTrack[trackMost] = 1;
00436 for (int ii = 0; ii < nMost; ii++) {
00437 TrkHitOnTrk *alink = thoseHits[ii];
00438 TrkFundHit* hit = const_cast<TrkFundHit*> (alink->hit());
00439 if(hitList->removeHit(hit)){
00440 alink->hit()->setUnusedHit(alink);
00441 }
00442
00443 }
00444 if (8 == tkParam.lPrint) std::cout<<"inactive hits on other track "<< std::endl;
00445 }
00446 delete [] theseHits;
00447 delete [] thoseHits;
00448
00449 }
00450
00451 }
00452
00453
00454
00455 TrkErrCode fitResult;
00456 long index = -1;
00457 idMap.get(aRecoTrk.id(), index); assert (index >= 0);
00458
00459 if (lRefit || refitTrack[index] == 1) {
00460 if (8 == tkParam.lPrint) {
00461 std::cout<<"after group ,refit track"<<aRecoTrk.id()<< std::endl;
00462 }
00463 fitResult = hitList->fit();
00464 aRecoTrk.status()->addHistory(
00465 TrkErrCode(fitResult.success()?TrkErrCode::succeed:TrkErrCode::fail,14,"Arbitrated"), "MdcTrkRecon");
00466 if (fitResult.failure() && (8 == tkParam.lPrint )) {
00467 fitResult.print(std::cerr);
00468 }
00469
00470
00471 double chisqperDOF;
00472 bool badFit = true;
00473 if (fitResult.success()) {
00474 badFit = false;
00475 int nDOF = tkFit->nActive() - 5;
00476 if (nDOF > 5){
00477 chisqperDOF = tkFit->chisq() / nDOF;
00478 }else{
00479 chisqperDOF = tkFit->chisq();
00480 }
00481
00482 if (chisqperDOF > tkParam.maxChisq) badFit = true;
00483 if (tkFit->nActive() < tkParam.minHits) badFit = true;
00484 double tem2 = (float) hitList->nHit() - tkFit->nActive();
00485 if (tkParam.lUseQualCuts) {
00486 if (tem2 >= tkParam.maxNmissTrack) badFit = true;
00487 if (tem2 /float(hitList->nHit()) > tkParam.maxNmissNorm){
00488 badFit = true;
00489 }
00490 }
00491 if(8== tkParam.lPrint) std::cout<<"fit quality:"<<
00492 " chisqperDof "<<chisqperDOF<<"?>"<<tkParam.maxChisq<<
00493 " nActive "<<tkFit->nActive()<<"?<"<<tkParam.minHits<<
00494 " nHit "<<hitList->nHit()<<" nhit-act "<<tem2<<"?>= nMiss "<<tkParam.maxNmissTrack<<
00495 " hit-act/nhit "<<tem2/float(hitList->nHit())<<"?> MissNorm "<<tkParam.maxNmissNorm
00496 << std::endl;
00497
00498
00499 }
00500 if (8 == tkParam.lPrint) {
00501 cout << "Refitting track " << aRecoTrk.id() << " success = "
00502 << fitResult.success() << "\n";
00503 }
00504
00505 if (fitResult.failure() || badFit ) {
00506 nDeleted++;
00507
00508
00509
00510 if (8 == tkParam.lPrint) {
00511 cout << "fitResult.failure? "<<fitResult.failure()
00512 <<" badFit? "<<badFit <<" Killing tkNo " << itrack << endl;
00513 }
00514 delete &(atrack->track());
00515 atrack->setTrack(0);
00516 trksToKill.push_back(atrack);
00517 continue;
00518 }
00519 }
00520
00521 if (lGroupHits) goto restart;
00522
00523 }
00524 if (8 == tkParam.lPrint) std::cout<<"end of loop over tracks"<< std::endl;
00525
00526
00527 for (int itk = 0; itk < (int)trksToKill.size(); itk++) {
00528 remove(trksToKill[itk]);
00529 if (8 == tkParam.lPrint) std::cout<<"remode dead track No."<<itk<< std::endl;
00530 }
00531 if (8 == tkParam.lPrint) std::cout<<"---end of arbitrateHits"<< std::endl;
00532
00533 delete [] usedInTrackNum;
00534 delete [] refitTrack;
00535 delete [] trkXRef;
00536 return nDeleted;
00537 }