172 auto resultTrackstersMerged = std::make_unique<std::vector<Trackster>>();
173 auto resultCandidates = std::make_unique<std::vector<TICLCandidate>>();
176 std::vector<bool> usedTrackstersMerged;
177 std::vector<int> indexInMergedCollTRKEM;
178 std::vector<int> indexInMergedCollEM;
179 std::vector<int> indexInMergedCollTRK;
180 std::vector<int> indexInMergedCollHAD;
181 std::vector<bool> usedSeeds;
184 std::map<int, std::vector<std::pair<int, TracksterIterIndex>>> seedToTracksterAssociator;
187 const auto &
tracks = *track_h;
191 const auto &layerClusters = *cluster_h;
195 const auto &layerClustersTimes = *clustersTime_h;
199 const auto &trackstersEM = *trackstersem_h;
203 const auto &trackstersTRKEM = *tracksterstrkem_h;
207 const auto &trackstersTRK = *tracksterstrk_h;
211 const auto &trackstersHAD = *trackstershad_h;
215 const auto &seedingTrk = *seedingTrk_h;
216 usedSeeds.resize(
tracks.size(),
false);
218 fillTile(tracksterTile, trackstersTRKEM, TracksterIterIndex::TRKEM);
219 fillTile(tracksterTile, trackstersEM, TracksterIterIndex::EM);
220 fillTile(tracksterTile, trackstersTRK, TracksterIterIndex::TRKHAD);
221 fillTile(tracksterTile, trackstersHAD, TracksterIterIndex::HAD);
223 auto totalNumberOfTracksters =
224 trackstersTRKEM.size() + trackstersTRK.size() + trackstersEM.size() + trackstersHAD.size();
225 resultTrackstersMerged->reserve(totalNumberOfTracksters);
226 usedTrackstersMerged.resize(totalNumberOfTracksters,
false);
227 indexInMergedCollTRKEM.reserve(trackstersTRKEM.size());
228 indexInMergedCollEM.reserve(trackstersEM.size());
229 indexInMergedCollTRK.reserve(trackstersTRK.size());
230 indexInMergedCollHAD.reserve(trackstersHAD.size());
239 for (
auto const &
t : trackstersTRKEM) {
240 indexInMergedCollTRKEM.push_back(resultTrackstersMerged->size());
241 seedToTracksterAssociator[
t.seedIndex()].emplace_back(resultTrackstersMerged->size(), TracksterIterIndex::TRKEM);
242 resultTrackstersMerged->push_back(
t);
245 for (
auto const &
t : trackstersEM) {
246 indexInMergedCollEM.push_back(resultTrackstersMerged->size());
247 resultTrackstersMerged->push_back(
t);
250 for (
auto const &
t : trackstersTRK) {
251 indexInMergedCollTRK.push_back(resultTrackstersMerged->size());
252 seedToTracksterAssociator[
t.seedIndex()].emplace_back(resultTrackstersMerged->size(), TracksterIterIndex::TRKHAD);
253 resultTrackstersMerged->push_back(
t);
256 for (
auto const &
t : trackstersHAD) {
257 indexInMergedCollHAD.push_back(resultTrackstersMerged->size());
258 resultTrackstersMerged->push_back(
t);
269 auto trackstersMergedHandle = evt.
put(
std::move(resultTrackstersMerged));
275 for (
unsigned i = 0;
i < trackstersEM.size(); ++
i) {
276 auto mergedIdx = indexInMergedCollEM[
i];
277 usedTrackstersMerged[mergedIdx] =
true;
278 const auto &
t = trackstersEM[
i];
285 t.raw_energy() *
t.barycenter().unit().y(),
286 t.raw_energy() *
t.barycenter().unit().z(),
288 tmpCandidate.
setP4(p4);
289 resultCandidates->push_back(tmpCandidate);
293 constexpr
double mpion = 0.13957;
294 constexpr
float mpion2 = mpion * mpion;
295 for (
unsigned i = 0;
i < trackstersHAD.size(); ++
i) {
296 auto mergedIdx = indexInMergedCollHAD[
i];
297 usedTrackstersMerged[mergedIdx] =
true;
298 const auto &
t = trackstersHAD[
i];
304 float momentum =
std::sqrt(
t.raw_energy() *
t.raw_energy() - mpion2);
306 momentum *
t.barycenter().unit().y(),
307 momentum *
t.barycenter().unit().z(),
309 tmpCandidate.
setP4(p4);
310 resultCandidates->push_back(tmpCandidate);
314 for (
unsigned i = 0;
i < trackstersTRKEM.size(); ++
i) {
315 auto mergedIdx = indexInMergedCollTRKEM[
i];
316 if (!usedTrackstersMerged[mergedIdx]) {
317 const auto &
t = trackstersTRKEM[
i];
318 auto trackIdx =
t.seedIndex();
320 if (!usedSeeds[trackIdx] and
t.raw_energy() > 0) {
321 usedSeeds[trackIdx] =
true;
322 usedTrackstersMerged[mergedIdx] =
true;
324 std::vector<int> trackstersTRKwithSameSeed;
325 std::vector<int> trackstersTRKEMwithSameSeed;
327 for (
const auto &tracksterIterationPair : seedToTracksterAssociator[trackIdx]) {
328 if (tracksterIterationPair.first != mergedIdx and !usedTrackstersMerged[tracksterIterationPair.first] and
329 trackstersMergedHandle->at(tracksterIterationPair.first).raw_energy() > 0.) {
330 if (tracksterIterationPair.second == TracksterIterIndex::TRKEM) {
331 trackstersTRKEMwithSameSeed.push_back(tracksterIterationPair.first);
332 }
else if (tracksterIterationPair.second == TracksterIterIndex::TRKHAD) {
333 trackstersTRKwithSameSeed.push_back(tracksterIterationPair.first);
338 float tracksterTotalRawPt =
t.raw_pt();
339 std::vector<int> haloTrackstersTRKIdx;
340 bool foundCompatibleTRK =
false;
342 for (
auto otherTracksterIdx : trackstersTRKwithSameSeed) {
343 usedTrackstersMerged[otherTracksterIdx] =
true;
344 tracksterTotalRawPt += trackstersMergedHandle->at(otherTracksterIdx).raw_pt();
347 if ((
t.barycenter() - trackstersMergedHandle->at(otherTracksterIdx).barycenter()).
mag2() <
349 haloTrackstersTRKIdx.push_back(otherTracksterIdx);
352 foundCompatibleTRK =
true;
357 if (trackstersTRKEMwithSameSeed.empty()) {
358 if (foundCompatibleTRK) {
361 double raw_energy =
t.raw_energy();
366 for (
auto otherTracksterIdx : trackstersTRKwithSameSeed) {
368 raw_energy += trackstersMergedHandle->at(otherTracksterIdx).raw_energy();
372 raw_energy *
track.momentum().unit().y(),
373 raw_energy *
track.momentum().unit().z(),
375 tmpCandidate.
setP4(p4);
376 resultCandidates->push_back(tmpCandidate);
381 double raw_energy =
t.raw_energy();
384 for (
auto otherTracksterIdx : trackstersTRKwithSameSeed) {
386 raw_energy += trackstersMergedHandle->at(otherTracksterIdx).raw_energy();
392 raw_energy *
track.momentum().unit().y(),
393 raw_energy *
track.momentum().unit().z(),
395 tmpCandidate.
setP4(p4);
396 resultCandidates->push_back(tmpCandidate);
401 int closestTrackster = mergedIdx;
403 for (
auto otherTracksterIdx : trackstersTRKEMwithSameSeed) {
404 auto thisPt = tracksterTotalRawPt + trackstersMergedHandle->at(otherTracksterIdx).raw_pt() -
t.raw_pt();
405 closestTrackster =
std::abs(thisPt -
track.pt()) < minPtDiff ? otherTracksterIdx : closestTrackster;
407 usedTrackstersMerged[closestTrackster] =
true;
409 if (foundCompatibleTRK) {
412 double raw_energy = trackstersMergedHandle->at(closestTrackster).raw_energy();
417 for (
auto otherTracksterIdx : trackstersTRKwithSameSeed) {
419 raw_energy += trackstersMergedHandle->at(otherTracksterIdx).raw_energy();
422 float momentum =
std::sqrt(raw_energy * raw_energy - mpion2);
424 momentum *
track.momentum().unit().y(),
425 momentum *
track.momentum().unit().z(),
427 tmpCandidate.
setP4(p4);
428 resultCandidates->push_back(tmpCandidate);
433 double raw_energy = trackstersMergedHandle->at(closestTrackster).raw_energy();
437 for (
auto otherTracksterIdx : trackstersTRKwithSameSeed) {
439 raw_energy += trackstersMergedHandle->at(otherTracksterIdx).raw_energy();
444 raw_energy *
track.momentum().unit().y(),
445 raw_energy *
track.momentum().unit().z(),
447 tmpCandidate.
setP4(p4);
448 resultCandidates->push_back(tmpCandidate);
451 for (
auto otherTracksterIdx : trackstersTRKEMwithSameSeed) {
452 auto tmpIndex = (otherTracksterIdx != closestTrackster) ? otherTracksterIdx : mergedIdx;
454 const auto &otherTrackster = trackstersMergedHandle->at(tmpIndex);
455 auto gammaEnergy = otherTrackster.raw_energy();
460 gammaEnergy * otherTrackster.barycenter().unit().y(),
461 gammaEnergy * otherTrackster.barycenter().unit().z(),
463 photonCandidate.
setP4(gammaP4);
465 resultCandidates->push_back(photonCandidate);
472 for (
unsigned i = 0;
i < trackstersTRK.size(); ++
i) {
473 auto mergedIdx = indexInMergedCollTRK[
i];
474 const auto &
t = trackstersTRK[
i];
476 if (!usedTrackstersMerged[mergedIdx] and
t.raw_energy() > 0) {
477 auto trackIdx =
t.seedIndex();
479 if (!usedSeeds[trackIdx]) {
480 usedSeeds[trackIdx] =
true;
481 usedTrackstersMerged[mergedIdx] =
true;
488 float momentum =
std::sqrt(
t.raw_energy() *
t.raw_energy() - mpion2);
490 momentum *
track.momentum().unit().y(),
491 momentum *
track.momentum().unit().z(),
493 tmpCandidate.
setP4(p4);
494 resultCandidates->push_back(tmpCandidate);
499 for (
auto const &
s : seedingTrk) {
500 if (usedSeeds[
s.index] ==
false) {
510 tmpCandidate.
setP4(p4Polar);
511 resultCandidates->push_back(tmpCandidate);
512 usedSeeds[
s.index] =
true;
517 for (
unsigned i = 0;
i <
tracks.size(); ++
i) {
530 tmpCandidate.
setP4(p4Polar);
531 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)
const edm::EDGetTokenT< std::vector< TICLSeedingRegion > > seedingTrk_token_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
const double halo_max_distance2_
void assignTimeToCandidates(std::vector< TICLCandidate > &resultCandidates) const
hgcal::RecHitTools rhtools_
auto const & tracks
cannot be loose
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_
void energyRegressionAndID(const std::vector< reco::CaloCluster > &layerClusters, std::vector< Trackster > &result) const
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)
T mag2() const
The vector magnitude squared. Equivalent to vec.dot(vec)
Abs< T >::type abs(const T &t)
const edm::EDGetTokenT< std::vector< Trackster > > trackstershad_token_
const edm::EDGetTokenT< edm::ValueMap< std::pair< float, float > > > clustersTime_token_
const int track_max_missing_outerhits_
const edm::EDGetTokenT< std::vector< reco::Track > > tracks_token_
const double track_max_eta_
void addTrackster(const edm::Ptr< ticl::Trackster > &trackster)
const double track_min_pt_
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
void printTrackstersDebug(const std::vector< Trackster > &, const char *label) const
void setPdgId(int pdgId) final
void setTrackPtr(const edm::Ptr< reco::Track > &trackPtr)
void setP4(const LorentzVector &p4) final
set 4-momentum