173 auto resultTrackstersMerged = std::make_unique<std::vector<Trackster>>();
174 auto resultCandidates = std::make_unique<std::vector<TICLCandidate>>();
179 std::vector<bool> usedTrackstersMerged;
180 std::vector<int> indexInMergedCollTRKEM;
181 std::vector<int> indexInMergedCollEM;
182 std::vector<int> indexInMergedCollTRK;
183 std::vector<int> indexInMergedCollHAD;
184 std::vector<bool> usedSeeds;
187 std::map<int, std::vector<std::pair<int, TracksterIterIndex>>> seedToTracksterAssociator;
190 const auto &
tracks = *track_h;
198 const auto &layerClustersTimes = *clustersTime_h;
202 const auto &trackstersEM = *trackstersem_h;
206 const auto &trackstersTRKEM = *tracksterstrkem_h;
210 const auto &trackstersTRK = *tracksterstrk_h;
214 const auto &trackstersHAD = *trackstershad_h;
219 usedSeeds.resize(
tracks.size(),
false);
221 fillTile(tracksterTile, trackstersTRKEM, TracksterIterIndex::TRKEM);
222 fillTile(tracksterTile, trackstersEM, TracksterIterIndex::EM);
223 fillTile(tracksterTile, trackstersTRK, TracksterIterIndex::TRKHAD);
224 fillTile(tracksterTile, trackstersHAD, TracksterIterIndex::HAD);
226 auto totalNumberOfTracksters =
227 trackstersTRKEM.size() + trackstersTRK.size() + trackstersEM.size() + trackstersHAD.size();
228 resultTrackstersMerged->reserve(totalNumberOfTracksters);
229 usedTrackstersMerged.resize(totalNumberOfTracksters,
false);
230 indexInMergedCollTRKEM.reserve(trackstersTRKEM.size());
231 indexInMergedCollEM.reserve(trackstersEM.size());
232 indexInMergedCollTRK.reserve(trackstersTRK.size());
233 indexInMergedCollHAD.reserve(trackstersHAD.size());
242 for (
auto const &
t : trackstersTRKEM) {
243 indexInMergedCollTRKEM.push_back(resultTrackstersMerged->size());
244 seedToTracksterAssociator[
t.seedIndex()].emplace_back(resultTrackstersMerged->size(), TracksterIterIndex::TRKEM);
245 resultTrackstersMerged->push_back(
t);
248 for (
auto const &
t : trackstersEM) {
249 indexInMergedCollEM.push_back(resultTrackstersMerged->size());
250 resultTrackstersMerged->push_back(
t);
253 for (
auto const &
t : trackstersTRK) {
254 indexInMergedCollTRK.push_back(resultTrackstersMerged->size());
255 seedToTracksterAssociator[
t.seedIndex()].emplace_back(resultTrackstersMerged->size(), TracksterIterIndex::TRKHAD);
256 resultTrackstersMerged->push_back(
t);
259 for (
auto const &
t : trackstersHAD) {
260 indexInMergedCollHAD.push_back(resultTrackstersMerged->size());
261 resultTrackstersMerged->push_back(
t);
272 auto trackstersMergedHandle = evt.
put(
std::move(resultTrackstersMerged));
278 for (
unsigned i = 0;
i < trackstersEM.size(); ++
i) {
279 auto mergedIdx = indexInMergedCollEM[
i];
280 usedTrackstersMerged[mergedIdx] =
true;
281 const auto &
t = trackstersEM[
i];
288 t.raw_energy() *
t.barycenter().unit().y(),
289 t.raw_energy() *
t.barycenter().unit().z(),
291 tmpCandidate.
setP4(p4);
292 resultCandidates->push_back(tmpCandidate);
296 constexpr
double mpion = 0.13957;
297 constexpr
float mpion2 = mpion * mpion;
298 for (
unsigned i = 0;
i < trackstersHAD.size(); ++
i) {
299 auto mergedIdx = indexInMergedCollHAD[
i];
300 usedTrackstersMerged[mergedIdx] =
true;
301 const auto &
t = trackstersHAD[
i];
307 float momentum =
std::sqrt(
t.raw_energy() *
t.raw_energy() - mpion2);
309 momentum *
t.barycenter().unit().y(),
310 momentum *
t.barycenter().unit().z(),
312 tmpCandidate.
setP4(p4);
313 resultCandidates->push_back(tmpCandidate);
317 for (
unsigned i = 0;
i < trackstersTRKEM.size(); ++
i) {
318 auto mergedIdx = indexInMergedCollTRKEM[
i];
319 if (!usedTrackstersMerged[mergedIdx]) {
320 const auto &
t = trackstersTRKEM[
i];
321 auto trackIdx =
t.seedIndex();
323 if (!usedSeeds[trackIdx] and
t.raw_energy() > 0) {
324 usedSeeds[trackIdx] =
true;
325 usedTrackstersMerged[mergedIdx] =
true;
327 std::vector<int> trackstersTRKwithSameSeed;
328 std::vector<int> trackstersTRKEMwithSameSeed;
330 for (
const auto &tracksterIterationPair : seedToTracksterAssociator[trackIdx]) {
331 if (tracksterIterationPair.first != mergedIdx and !usedTrackstersMerged[tracksterIterationPair.first] and
332 trackstersMergedHandle->at(tracksterIterationPair.first).raw_energy() > 0.) {
333 if (tracksterIterationPair.second == TracksterIterIndex::TRKEM) {
334 trackstersTRKEMwithSameSeed.push_back(tracksterIterationPair.first);
335 }
else if (tracksterIterationPair.second == TracksterIterIndex::TRKHAD) {
336 trackstersTRKwithSameSeed.push_back(tracksterIterationPair.first);
341 float tracksterTotalRawPt =
t.raw_pt();
342 std::vector<int> haloTrackstersTRKIdx;
343 bool foundCompatibleTRK =
false;
345 for (
auto otherTracksterIdx : trackstersTRKwithSameSeed) {
346 usedTrackstersMerged[otherTracksterIdx] =
true;
347 tracksterTotalRawPt += trackstersMergedHandle->at(otherTracksterIdx).raw_pt();
350 if ((
t.barycenter() - trackstersMergedHandle->at(otherTracksterIdx).barycenter()).
mag2() <
352 haloTrackstersTRKIdx.push_back(otherTracksterIdx);
355 foundCompatibleTRK =
true;
360 if (trackstersTRKEMwithSameSeed.empty()) {
361 if (foundCompatibleTRK) {
364 double raw_energy =
t.raw_energy();
369 for (
auto otherTracksterIdx : trackstersTRKwithSameSeed) {
371 raw_energy += trackstersMergedHandle->at(otherTracksterIdx).raw_energy();
375 raw_energy *
track.momentum().unit().y(),
376 raw_energy *
track.momentum().unit().z(),
378 tmpCandidate.
setP4(p4);
379 resultCandidates->push_back(tmpCandidate);
384 double raw_energy =
t.raw_energy();
387 for (
auto otherTracksterIdx : trackstersTRKwithSameSeed) {
389 raw_energy += trackstersMergedHandle->at(otherTracksterIdx).raw_energy();
395 raw_energy *
track.momentum().unit().y(),
396 raw_energy *
track.momentum().unit().z(),
398 tmpCandidate.
setP4(p4);
399 resultCandidates->push_back(tmpCandidate);
404 int closestTrackster = mergedIdx;
406 for (
auto otherTracksterIdx : trackstersTRKEMwithSameSeed) {
407 auto thisPt = tracksterTotalRawPt + trackstersMergedHandle->at(otherTracksterIdx).raw_pt() -
t.raw_pt();
408 closestTrackster =
std::abs(thisPt -
track.pt()) < minPtDiff ? otherTracksterIdx : closestTrackster;
410 usedTrackstersMerged[closestTrackster] =
true;
412 if (foundCompatibleTRK) {
415 double raw_energy = trackstersMergedHandle->at(closestTrackster).raw_energy();
420 for (
auto otherTracksterIdx : trackstersTRKwithSameSeed) {
422 raw_energy += trackstersMergedHandle->at(otherTracksterIdx).raw_energy();
425 float momentum =
std::sqrt(raw_energy * raw_energy - mpion2);
427 momentum *
track.momentum().unit().y(),
428 momentum *
track.momentum().unit().z(),
430 tmpCandidate.
setP4(p4);
431 resultCandidates->push_back(tmpCandidate);
436 double raw_energy = trackstersMergedHandle->at(closestTrackster).raw_energy();
440 for (
auto otherTracksterIdx : trackstersTRKwithSameSeed) {
442 raw_energy += trackstersMergedHandle->at(otherTracksterIdx).raw_energy();
447 raw_energy *
track.momentum().unit().y(),
448 raw_energy *
track.momentum().unit().z(),
450 tmpCandidate.
setP4(p4);
451 resultCandidates->push_back(tmpCandidate);
454 for (
auto otherTracksterIdx : trackstersTRKEMwithSameSeed) {
455 auto tmpIndex = (otherTracksterIdx != closestTrackster) ? otherTracksterIdx : mergedIdx;
457 const auto &otherTrackster = trackstersMergedHandle->at(tmpIndex);
458 auto gammaEnergy = otherTrackster.raw_energy();
463 gammaEnergy * otherTrackster.barycenter().unit().y(),
464 gammaEnergy * otherTrackster.barycenter().unit().z(),
466 photonCandidate.
setP4(gammaP4);
468 resultCandidates->push_back(photonCandidate);
475 for (
unsigned i = 0;
i < trackstersTRK.size(); ++
i) {
476 auto mergedIdx = indexInMergedCollTRK[
i];
477 const auto &
t = trackstersTRK[
i];
479 if (!usedTrackstersMerged[mergedIdx] and
t.raw_energy() > 0) {
480 auto trackIdx =
t.seedIndex();
482 if (!usedSeeds[trackIdx]) {
483 usedSeeds[trackIdx] =
true;
484 usedTrackstersMerged[mergedIdx] =
true;
491 float momentum =
std::sqrt(
t.raw_energy() *
t.raw_energy() - mpion2);
493 momentum *
track.momentum().unit().y(),
494 momentum *
track.momentum().unit().z(),
496 tmpCandidate.
setP4(p4);
497 resultCandidates->push_back(tmpCandidate);
503 if (usedSeeds[
s.index] ==
false) {
513 tmpCandidate.
setP4(p4Polar);
514 resultCandidates->push_back(tmpCandidate);
515 usedSeeds[
s.index] =
true;
520 for (
unsigned i = 0;
i <
tracks.size(); ++
i) {
533 tmpCandidate.
setP4(p4Polar);
534 resultCandidates->push_back(tmpCandidate);
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
const edm::EDGetTokenT< std::vector< Trackster > > trackstersem_token_
void fillTile(TICLTracksterTiles &, const std::vector< Trackster > &, TracksterIterIndex)
void assignTimeToCandidates(std::vector< TICLCandidate > &resultCandidates) const
const edm::EDGetTokenT< std::vector< TICLSeedingRegion > > seedingTrk_token_
const double halo_max_distance2_
const tensorflow::Session * tfSession_
void printTrackstersDebug(const std::vector< Trackster > &, const char *label) const
hgcal::RecHitTools rhtools_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
const edm::EDGetTokenT< std::vector< Trackster > > tracksterstrkem_token_
const edm::EDGetTokenT< std::vector< reco::CaloCluster > > clusters_token_
const double track_min_eta_
const edm::ESGetToken< CaloGeometry, CaloGeometryRecord > geometry_token_
PtEtaPhiMLorentzVectorD PtEtaPhiMLorentzVector
Lorentz vector with cartesian internal representation.
void assignPCAtoTracksters(std::vector< Trackster > &, const std::vector< reco::CaloCluster > &, const edm::ValueMap< std::pair< float, float >> &, double, bool energyWeight=true)
void setCharge(Charge q) final
set electric charge
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
const edm::EDGetTokenT< std::vector< Trackster > > tracksterstrk_token_
void setRawEnergy(float rawEnergy)
void energyRegressionAndID(const std::vector< reco::CaloCluster > &layerClusters, const tensorflow::Session *, std::vector< Trackster > &result) const
Abs< T >::type abs(const T &t)
const edm::EDGetTokenT< std::vector< Trackster > > trackstershad_token_
bool getData(T &iHolder) const
const edm::EDGetTokenT< edm::ValueMap< std::pair< float, float > > > clustersTime_token_
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
const int track_max_missing_outerhits_
const edm::EDGetTokenT< std::vector< reco::Track > > tracks_token_
auto const & tracks
cannot be loose
T mag2() const
The vector magnitude squared. Equivalent to vec.dot(vec)
const double track_max_eta_
void addTrackster(const edm::Ptr< ticl::Trackster > &trackster)
const double track_min_pt_
void setPdgId(int pdgId) final
void setTrackPtr(const edm::Ptr< reco::Track > &trackPtr)
void setP4(const LorentzVector &p4) final
set 4-momentum
const edm::ESGetToken< TfGraphDefWrapper, TfGraphRecord > tfDnnToken_