168 auto resultTrackstersMerged = std::make_unique<std::vector<Trackster>>();
169 auto resultCandidates = std::make_unique<std::vector<TICLCandidate>>();
172 std::vector<bool> usedTrackstersMerged;
173 std::vector<int> indexInMergedCollTRKEM;
174 std::vector<int> indexInMergedCollEM;
175 std::vector<int> indexInMergedCollTRK;
176 std::vector<int> indexInMergedCollHAD;
177 std::vector<bool> usedSeeds;
180 std::map<int, std::vector<std::pair<int, TracksterIterIndex>>> seedToTracksterAssociator;
181 std::vector<TracksterIterIndex> iterMergedTracksters;
184 const auto &
tracks = *track_h;
192 const auto &trackstersEM = *trackstersem_h;
196 const auto &trackstersTRKEM = *tracksterstrkem_h;
200 const auto &trackstersTRK = *tracksterstrk_h;
204 const auto &trackstersHAD = *trackstershad_h;
209 usedSeeds.resize(
tracks.size(),
false);
211 fillTile(tracksterTile, trackstersTRKEM, TracksterIterIndex::TRKEM);
212 fillTile(tracksterTile, trackstersEM, TracksterIterIndex::EM);
213 fillTile(tracksterTile, trackstersTRK, TracksterIterIndex::TRK);
214 fillTile(tracksterTile, trackstersHAD, TracksterIterIndex::HAD);
216 auto totalNumberOfTracksters =
217 trackstersTRKEM.size() + trackstersTRK.size() + trackstersEM.size() + trackstersHAD.size();
218 resultTrackstersMerged->reserve(totalNumberOfTracksters);
219 iterMergedTracksters.reserve(totalNumberOfTracksters);
220 usedTrackstersMerged.resize(totalNumberOfTracksters,
false);
221 indexInMergedCollTRKEM.reserve(trackstersTRKEM.size());
222 indexInMergedCollEM.reserve(trackstersEM.size());
223 indexInMergedCollTRK.reserve(trackstersTRK.size());
224 indexInMergedCollHAD.reserve(trackstersHAD.size());
233 for (
auto const &
t : trackstersTRKEM) {
234 indexInMergedCollTRKEM.push_back(resultTrackstersMerged->size());
235 seedToTracksterAssociator[
t.seedIndex()].emplace_back(resultTrackstersMerged->size(), TracksterIterIndex::TRKEM);
236 resultTrackstersMerged->push_back(
t);
237 iterMergedTracksters.push_back(TracksterIterIndex::TRKEM);
240 for (
auto const &
t : trackstersEM) {
241 indexInMergedCollEM.push_back(resultTrackstersMerged->size());
242 resultTrackstersMerged->push_back(
t);
243 iterMergedTracksters.push_back(TracksterIterIndex::EM);
246 for (
auto const &
t : trackstersTRK) {
247 indexInMergedCollTRK.push_back(resultTrackstersMerged->size());
248 seedToTracksterAssociator[
t.seedIndex()].emplace_back(resultTrackstersMerged->size(), TracksterIterIndex::TRK);
249 resultTrackstersMerged->push_back(
t);
250 iterMergedTracksters.push_back(TracksterIterIndex::TRK);
253 for (
auto const &
t : trackstersHAD) {
254 indexInMergedCollHAD.push_back(resultTrackstersMerged->size());
255 resultTrackstersMerged->push_back(
t);
256 iterMergedTracksters.push_back(TracksterIterIndex::HAD);
264 auto trackstersMergedHandle = evt.
put(
std::move(resultTrackstersMerged));
270 for (
unsigned i = 0;
i < trackstersEM.size(); ++
i) {
271 auto mergedIdx = indexInMergedCollEM[
i];
272 usedTrackstersMerged[mergedIdx] =
true;
273 const auto &
t = trackstersEM[
i];
280 t.raw_energy() *
t.barycenter().unit().y(),
281 t.raw_energy() *
t.barycenter().unit().z(),
284 resultCandidates->push_back(tmpCandidate);
288 constexpr
double mpion = 0.13957;
289 constexpr
float mpion2 = mpion * mpion;
290 for (
unsigned i = 0;
i < trackstersHAD.size(); ++
i) {
291 auto mergedIdx = indexInMergedCollHAD[
i];
292 usedTrackstersMerged[mergedIdx] =
true;
293 const auto &
t = trackstersHAD[
i];
299 float momentum =
std::sqrt(
t.raw_energy() *
t.raw_energy() - mpion2);
301 momentum *
t.barycenter().unit().y(),
302 momentum *
t.barycenter().unit().z(),
305 resultCandidates->push_back(tmpCandidate);
309 for (
unsigned i = 0;
i < trackstersTRKEM.size(); ++
i) {
310 auto mergedIdx = indexInMergedCollTRKEM[
i];
311 if (!usedTrackstersMerged[mergedIdx]) {
312 const auto &
t = trackstersTRKEM[
i];
313 auto trackIdx =
t.seedIndex();
315 if (!usedSeeds[trackIdx] and
t.raw_energy() > 0) {
316 usedSeeds[trackIdx] =
true;
317 usedTrackstersMerged[mergedIdx] =
true;
319 std::vector<int> trackstersTRKwithSameSeed;
320 std::vector<int> trackstersTRKEMwithSameSeed;
322 for (
const auto &tracksterIterationPair : seedToTracksterAssociator[trackIdx]) {
323 if (tracksterIterationPair.first != mergedIdx and !usedTrackstersMerged[tracksterIterationPair.first] and
324 trackstersMergedHandle->at(tracksterIterationPair.first).raw_energy() > 0.) {
325 if (tracksterIterationPair.second == TracksterIterIndex::TRKEM) {
326 trackstersTRKEMwithSameSeed.push_back(tracksterIterationPair.first);
327 }
else if (tracksterIterationPair.second == TracksterIterIndex::TRK) {
328 trackstersTRKwithSameSeed.push_back(tracksterIterationPair.first);
333 float tracksterTotalRawPt =
t.raw_pt();
334 std::vector<int> haloTrackstersTRKIdx;
335 bool foundCompatibleTRK =
false;
337 for (
auto otherTracksterIdx : trackstersTRKwithSameSeed) {
338 usedTrackstersMerged[otherTracksterIdx] =
true;
339 tracksterTotalRawPt += trackstersMergedHandle->at(otherTracksterIdx).raw_pt();
342 if ((
t.barycenter() - trackstersMergedHandle->at(otherTracksterIdx).barycenter()).
mag2() <
344 haloTrackstersTRKIdx.push_back(otherTracksterIdx);
347 foundCompatibleTRK =
true;
352 if (trackstersTRKEMwithSameSeed.empty()) {
353 if (foundCompatibleTRK) {
356 double raw_energy =
t.raw_energy();
361 for (
auto otherTracksterIdx : trackstersTRKwithSameSeed) {
363 raw_energy += trackstersMergedHandle->at(otherTracksterIdx).raw_energy();
367 raw_energy *
track.momentum().unit().y(),
368 raw_energy *
track.momentum().unit().z(),
371 resultCandidates->push_back(tmpCandidate);
376 double raw_energy =
t.raw_energy();
379 for (
auto otherTracksterIdx : trackstersTRKwithSameSeed) {
381 raw_energy += trackstersMergedHandle->at(otherTracksterIdx).raw_energy();
387 raw_energy *
track.momentum().unit().y(),
388 raw_energy *
track.momentum().unit().z(),
391 resultCandidates->push_back(tmpCandidate);
396 int closestTrackster = mergedIdx;
398 for (
auto otherTracksterIdx : trackstersTRKEMwithSameSeed) {
399 auto thisPt = tracksterTotalRawPt + trackstersMergedHandle->at(otherTracksterIdx).raw_pt() -
t.raw_pt();
400 closestTrackster =
std::abs(thisPt -
track.pt()) < minPtDiff ? otherTracksterIdx : closestTrackster;
402 usedTrackstersMerged[closestTrackster] =
true;
404 if (foundCompatibleTRK) {
407 double raw_energy = trackstersMergedHandle->at(closestTrackster).raw_energy();
412 for (
auto otherTracksterIdx : trackstersTRKwithSameSeed) {
414 raw_energy += trackstersMergedHandle->at(otherTracksterIdx).raw_energy();
417 float momentum =
std::sqrt(raw_energy * raw_energy - mpion2);
419 momentum *
track.momentum().unit().y(),
420 momentum *
track.momentum().unit().z(),
423 resultCandidates->push_back(tmpCandidate);
428 double raw_energy = trackstersMergedHandle->at(closestTrackster).raw_energy();
432 for (
auto otherTracksterIdx : trackstersTRKwithSameSeed) {
434 raw_energy += trackstersMergedHandle->at(otherTracksterIdx).raw_energy();
439 raw_energy *
track.momentum().unit().y(),
440 raw_energy *
track.momentum().unit().z(),
443 resultCandidates->push_back(tmpCandidate);
446 for (
auto otherTracksterIdx : trackstersTRKEMwithSameSeed) {
447 auto tmpIndex = (otherTracksterIdx != closestTrackster) ? otherTracksterIdx : mergedIdx;
449 const auto &otherTrackster = trackstersMergedHandle->at(tmpIndex);
450 auto gammaEnergy = otherTrackster.raw_energy();
455 gammaEnergy * otherTrackster.barycenter().unit().y(),
456 gammaEnergy * otherTrackster.barycenter().unit().z(),
458 photonCandidate.
setP4(gammaP4);
460 resultCandidates->push_back(photonCandidate);
467 for (
unsigned i = 0;
i < trackstersTRK.size(); ++
i) {
468 auto mergedIdx = indexInMergedCollTRK[
i];
469 const auto &
t = trackstersTRK[
i];
471 if (!usedTrackstersMerged[mergedIdx] and
t.raw_energy() > 0) {
472 auto trackIdx =
t.seedIndex();
474 if (!usedSeeds[trackIdx]) {
475 usedSeeds[trackIdx] =
true;
476 usedTrackstersMerged[mergedIdx] =
true;
483 float momentum =
std::sqrt(
t.raw_energy() *
t.raw_energy() - mpion2);
485 momentum *
track.momentum().unit().y(),
486 momentum *
track.momentum().unit().z(),
489 resultCandidates->push_back(tmpCandidate);
495 if (usedSeeds[
s.index] ==
false) {
505 tmpCandidate.
setP4(p4Polar);
506 resultCandidates->push_back(tmpCandidate);
507 usedSeeds[
s.index] =
true;
512 for (
unsigned i = 0;
i <
tracks.size(); ++
i) {
517 usedSeeds[
i] ==
false) {
526 tmpCandidate.
setP4(p4Polar);
527 resultCandidates->push_back(tmpCandidate);