167 std::vector<FPGATrackSimRoad> & roads) {
177 for (
int ireg = 0; ireg < rmap_2nd->
getNRegions(); ireg++) {
184 HitSpatialIndex hitIndex;
187 hitIndex.build(hits);
195 if (track.passedOR() == 0) {
198 const std::vector<FPGATrackSimHit> hitsOnTrack = track.getFPGATrackSimHits();
200 float pt = track.getPt();
202 for (
const auto &thit : hitsOnTrack) {
203 road.
addHit(std::make_shared<const FPGATrackSimHit>(thit));
208 for (
const auto& hit : hits) {
213 std::deque<miniRoad> roadsToExtrapolate;
214 roadsToExtrapolate.push_back(std::move(road));
216 std::vector<miniRoad> completedRoads;
220 const int MAX_ROADS = 10000;
221 while(!roadsToExtrapolate.empty() &&
count < MAX_ROADS ) {
222 miniRoad currentRoad = std::move(roadsToExtrapolate.front());
225 roadsToExtrapolate.pop_front();
228 ATH_MSG_DEBUG(
"\033[1;31m-------------------------- extraploating road "<<
count <<
"------------------ \033[0m");
234 completedRoads.push_back(std::move(currentRoad));
238 std::vector<float> inputTensorValues;
239 std::vector<float> predhit;
256 if ((
m_useCartesian && (abs(predhit[0]) < 25 && abs(predhit[1]) < 25)) ||
259 completedRoads.push_back(std::move(currentRoad));
266 double radius = std::sqrt(predhit[0] * predhit[0] + predhit[1] * predhit[1]);
267 if ((
m_useCartesian && (abs(predhit[0]) > 1024 || abs(predhit[1]) > 1024 || radius > 1024 || abs(predhit[2]) > 3000)) ||
268 (!
m_useCartesian && (abs(predhit[0]) > 1024 || abs(predhit[2]) > 3000))) {
269 completedRoads.push_back(std::move(currentRoad));
274 ATH_MSG_DEBUG(
"Predicted hit at: " << predhit[0] <<
" " << predhit[1] <<
" " << predhit[2]);
278 bool foundhitForRoad =
false;
281 completedRoads.push_back(std::move(currentRoad));
285 unsigned lastLayerInRoad = 0;
286 std::shared_ptr<const FPGATrackSimHit> lastHit;
287 if(!
getLastLayer(currentRoad, lastLayerInRoad, lastHit) or !lastHit) {
291 unsigned layer = lastLayerInRoad+1;
292 bool lastHitWasReal = lastHit->isReal();
293 float lastHitR = lastHit->getR();
295 completedRoads.push_back(std::move(currentRoad));
298 unsigned int hitsInWindow = 0;
301 const double predr = (
m_useCartesian ? sqrt(predhit[0] * predhit[0] + predhit[1] * predhit[1]) : predhit[0]);
302 const double predphi = predhit[1];
303 const double predz = predhit[2];
309 int fineID_index = 0;
314 ATH_MSG_DEBUG(
"No windows for predicted fineID " << fineID <<
", using maximum in provided list instead!");
322 windowR = (fineID_index == -1) ?
327 windowZ = (fineID_index == -1) ?
332 windowPhi = (fineID_index == -1) ?
346 std::vector<std::vector<std::shared_ptr<const FPGATrackSimHit>>> listofHitsFound;
352 const auto* candidateHits = hitIndex.getHits(fineID);
354 listofHitsFound.reserve(candidateHits->size());
358 const float hitr = hit->
getR();
367 const double hitz = hit->
getZ();
368 const double hitphi = hit->
getGPhi();
370 const double dr = abs(hitr - predr);
371 const double dz = abs(hitz - predz);
372 double dphi = abs(hitphi - predphi);
373 while (dphi >
pi) dphi -=
pi;
375 const bool inWindow = (
m_useCartesian && dr < windowR && dz < windowZ) ||
376 (!
m_useCartesian && dphi < windowPhi && dz < windowZ && dr < windowR);
378 if (!inWindow)
continue;
381 std::vector<std::shared_ptr<const FPGATrackSimHit>> theseHits{ makeNonOwningHitPtr(hit) };
382 hitsInWindow = hitsInWindow + 1;
388 const float EPSILON = 0.00001f;
389 const float searchX = hit->
getX();
390 const float searchY = hit->
getY();
391 const float searchZ = hit->
getZ();
395 const auto* coordCandidates = hitIndex.getHitsByCoord(searchX, searchY, searchZ);
396 if (coordCandidates) {
398 if (candidateHit->getIdentifierHash() != searchHash &&
399 abs(candidateHit->getX() - searchX) <
EPSILON &&
400 abs(candidateHit->getY() - searchY) <
EPSILON &&
401 abs(candidateHit->getZ() - searchZ) <
EPSILON) {
402 theseHits.push_back(makeNonOwningHitPtr(candidateHit));
409 ATH_MSG_WARNING(
"For a SP in layer " << layer <<
" Couldn't find a matching strip SP");
413 std::shared_ptr<FPGATrackSimHit> guessedSecondHitPtr = std::make_shared<FPGATrackSimHit>();
414 guessedSecondHitPtr->setX(0);
415 guessedSecondHitPtr->setY(0);
416 guessedSecondHitPtr->setZ(0);
417 guessedSecondHitPtr->setPhysLayer(lastHit->getPhysLayer(
true)+1);
422 theseHits.push_back(guessedSecondHitPtr);
426 listofHitsFound.push_back(std::move(theseHits));
436 std::min(
static_cast<size_t>(
m_maxBranches.value()), listofHitsFound.size()) :
437 listofHitsFound.size();
441 auto comparatorFunc = [&](
const auto&
a,
const auto& b) {
442 return cartesianComparator(
a, b, predr, predz);
444 if (nToSort < listofHitsFound.size()) {
446 listofHitsFound.begin() + nToSort,
447 listofHitsFound.end(),
450 std::sort(listofHitsFound.begin(), listofHitsFound.end(), comparatorFunc);
457 const double zScale2 = zScale * zScale;
458 const double phiScale2 = phiScale * phiScale;
459 const double rScale2 = rScale * rScale;
461 auto comparatorFunc = [&](
const auto&
a,
const auto& b) {
462 return polarComparator(
a, b, predr, predphi, predz, zScale2, phiScale2, rScale2);
464 if (nToSort < listofHitsFound.size()) {
466 listofHitsFound.begin() + nToSort,
467 listofHitsFound.end(),
470 std::sort(listofHitsFound.begin(), listofHitsFound.end(), comparatorFunc);
476 std::vector<std::vector<std::shared_ptr<const FPGATrackSimHit>>> cleanHitsToGrow;
480 int nHitsToChoose = std::min(
int(
m_maxBranches.value()),
int(listofHitsFound.size()));
481 cleanHitsToGrow.reserve(nHitsToChoose);
482 std::copy(listofHitsFound.begin(), listofHitsFound.begin() + nHitsToChoose, std::back_inserter(cleanHitsToGrow));
485 cleanHitsToGrow = std::move(listofHitsFound);
490 for (
auto& hitsFound: cleanHitsToGrow) {
494 if(!
addHitToRoad(newRoad, currentRoad, std::move(hitsFound))) {
498 roadsToExtrapolate.push_back(newRoad);
499 foundhitForRoad =
true;
501 ATH_MSG_DEBUG(
"------ road grown with hit from layer "<<layer<<
" to");
506 if (!foundhitForRoad) {
513 std::vector<std::shared_ptr<const FPGATrackSimHit>> theseHits;
515 if (!
getFakeHit(currentRoad, predhit, fineID, theseHits)) {
525 if (!
addHitToRoad(newroad, currentRoad, std::move(theseHits))) {
529 roadsToExtrapolate.push_back(std::move(newroad));
537 for (
const auto &miniroad : completedRoads) {
543 road.
setX(track.getPhi());
544 road.
setY(track.getQOverPt());
545 road.
setXBin(track.getHoughXBin());
546 road.
setYBin(track.getHoughYBin());
550 std::vector<std::vector<std::shared_ptr<const FPGATrackSimHit>>> roadhits = miniroad.getVecHits();
552 if (roadhits.size() > nexpected) {
553 roadhits.resize(nexpected);
555 else if (roadhits.size() < nexpected) {
556 for (
unsigned layer = roadhits.size(); layer < nexpected; layer++) {
557 std::shared_ptr<FPGATrackSimHit> emptyHitPtr = std::make_shared<FPGATrackSimHit>();
558 emptyHitPtr->setX(0);
559 emptyHitPtr->setY(0);
560 emptyHitPtr->setZ(0);
561 emptyHitPtr->setLayer(layer);
564 roadhits.emplace_back(1,emptyHitPtr);
566 wclayers |= (1 << layer);
570 road.
setHits(std::move(roadhits));
572 m_roads.push_back(std::move(road));
581 roads.emplace_back(
r);
583 ATH_MSG_DEBUG(
"Found " << roads.size() <<
" new roads in second stage.");
585 return StatusCode::SUCCESS;
590 std::vector<std::shared_ptr<const FPGATrackSimHit>> hitsR;
592 std::vector<std::shared_ptr<const FPGATrackSimHit>> hits = thisroad.
getHits();
595 for (
unsigned ihit = 0; ihit < hits.size(); ihit++) {
597 hitsR.push_back(hits[ihit]);
600 std::sort(hitsR.begin(), hitsR.end(), [](
auto&
a,
auto& b){
601 double dist_a = std::hypot(a->getX(), a->getY(), a->getZ());
602 double dist_b = std::hypot(b->getX(), b->getY(), b->getZ());
603 return dist_a < dist_b;
607 for (
const auto& thit : hitsR)
609 if (
m_debugEvent)
ATH_MSG_DEBUG(thit->getX() <<
" " << thit->getY() <<
" " << thit->getZ() <<
" and phi = " << thit->getGPhi());
613 std::vector<std::shared_ptr<const FPGATrackSimHit>> cleanHits;
614 bool skipHit =
false;
615 for (
auto thit : hitsR)
624 cleanHits.push_back(std::move(thit));
629 cleanHits.push_back(std::move(thit));
635 cleanHits.push_back(std::move(thit));
643 else if (thit->isStrip() && thit->isReal())
646 cleanHits.push_back(std::move(thit));
650 ATH_MSG_WARNING(
"No clue how to deal with this hit in the NN predicition ");
662 std::vector<std::shared_ptr<const FPGATrackSimHit>> hitsToEncode;
666 for (
const auto& thit : cleanHits)
668 if (
m_debugEvent)
ATH_MSG_DEBUG(thit->getX() <<
" " << thit->getY() <<
" " << thit->getZ() <<
" " << thit->isStrip() <<
" and gphi = " << thit->getGPhi());
675 for (
const auto& thit : hitsToEncode)
678 inputTensorValues.push_back(thit->getX() /
getXScale());
679 inputTensorValues.push_back(thit->getY() /
getYScale());
680 inputTensorValues.push_back(thit->getZ() /
getZScale());
683 inputTensorValues.push_back(thit->getR() /
getRScale());
684 inputTensorValues.push_back(thit->getGPhi() /
getPhiScale());
685 inputTensorValues.push_back(thit->getZ() /
getZScale());
688 if (
m_debugEvent)
ATH_MSG_DEBUG(thit->getX() <<
" " << thit->getY() <<
" " << thit->getZ() <<
" and gphi = " << thit->getGPhi());
691 return StatusCode::SUCCESS;
730 unsigned guessedLayer(0);
732 std::shared_ptr<FPGATrackSimHit> guessedHitPtr = std::make_shared<FPGATrackSimHit>();
734 guessedHitPtr->setX(predhit[0]);
735 guessedHitPtr->setY(predhit[1]);
736 guessedHitPtr->setZ(predhit[2]);
739 double r = predhit[0];
740 double phi = predhit[1];
741 guessedHitPtr->setX(
r*cos(
phi));
742 guessedHitPtr->setY(
r*sin(
phi));
743 guessedHitPtr->setZ(predhit[2]);
751 guessedLayer = currentRoad.
getNHits();
754 guessedHitPtr->setLayer(guessedLayer);
759 guessedHitPtr->setPhysLayer(0);
767 hits.push_back(guessedHitPtr);
772 std::shared_ptr<FPGATrackSimHit> guessedSecondHitPtr = std::make_shared<FPGATrackSimHit>();
773 guessedSecondHitPtr->setX(0);
774 guessedSecondHitPtr->setY(0);
775 guessedSecondHitPtr->setZ(0);
776 guessedSecondHitPtr->setLayer( guessedLayer + 1 );
780 guessedHitPtr->setPhysLayer(1);
784 hits.push_back(guessedSecondHitPtr);
790 return StatusCode::SUCCESS;