From 8d56758a9aa793d86e8971b80a2f496a937c1386 Mon Sep 17 00:00:00 2001 From: Leonardo Cristella Date: Thu, 1 Apr 2021 23:57:04 +0200 Subject: [PATCH 1/8] Associator implementation --- DataFormats/HGCalReco/interface/Trackster.h | 2 + .../TracksterAssociatorByEnergyScoreImpl.cc | 532 ++++++++++++++++++ .../TracksterAssociatorByEnergyScoreImpl.h | 73 +++ ...racksterAssociatorByEnergyScoreProducer.cc | 67 +++ .../python/TSToSCAssociation_cfi.py | 13 + .../TracksterToSimClusterAssociator.h | 50 ++ .../TracksterToSimClusterAssociatorBaseImpl.h | 45 ++ .../Associations/plugins/BuildFile.xml | 1 + .../plugins/TSToSCAssociatorEDProducer.cc | 96 ++++ .../src/TracksterToSimClusterAssociator.cc | 7 + ...TracksterToSimClusterAssociatorBaseImpl.cc | 19 + 11 files changed, 905 insertions(+) create mode 100644 SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.cc create mode 100644 SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.h create mode 100644 SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreProducer.cc create mode 100644 SimCalorimetry/HGCalAssociatorProducers/python/TSToSCAssociation_cfi.py create mode 100644 SimDataFormats/Associations/interface/TracksterToSimClusterAssociator.h create mode 100644 SimDataFormats/Associations/interface/TracksterToSimClusterAssociatorBaseImpl.h create mode 100644 SimDataFormats/Associations/plugins/TSToSCAssociatorEDProducer.cc create mode 100644 SimDataFormats/Associations/src/TracksterToSimClusterAssociator.cc create mode 100644 SimDataFormats/Associations/src/TracksterToSimClusterAssociatorBaseImpl.cc diff --git a/DataFormats/HGCalReco/interface/Trackster.h b/DataFormats/HGCalReco/interface/Trackster.h index ca563529c84f7..e69cf2e1114f8 100644 --- a/DataFormats/HGCalReco/interface/Trackster.h +++ b/DataFormats/HGCalReco/interface/Trackster.h @@ -192,5 +192,7 @@ namespace ticl { // trackster ID probabilities std::array id_probabilities_; }; + + typedef std::vector TracksterCollection; } // namespace ticl #endif diff --git a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.cc b/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.cc new file mode 100644 index 0000000000000..dd0cb2a4f2dc8 --- /dev/null +++ b/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.cc @@ -0,0 +1,532 @@ +#include "TracksterAssociatorByEnergyScoreImpl.h" + +#include "FWCore/MessageLogger/interface/MessageLogger.h" +#include "SimDataFormats/CaloAnalysis/interface/SimCluster.h" + +TracksterAssociatorByEnergyScoreImpl::TracksterAssociatorByEnergyScoreImpl( + edm::EDProductGetter const& productGetter, + bool hardScatterOnly, + std::shared_ptr recHitTools, + const std::unordered_map* hitMap) + : hardScatterOnly_(hardScatterOnly), recHitTools_(recHitTools), hitMap_(hitMap), productGetter_(&productGetter) { + layers_ = recHitTools_->lastLayerBH(); +} + +hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( + const edm::Handle& tCH, const edm::Handle& lCCH, const edm::Handle& sCCH) const { + // Get collections + const auto& tracksters = *tCH.product(); + const auto& layerClusters = *lCCH.product(); + const auto& simClusters = *sCCH.product(); + auto nTracksters = tracksters.size(); + + //There shouldn't be any SimTracks from different crossings, but maybe they will be added later. + //At the moment there should be one SimTrack in each SimCluster. + auto nSimClusters = simClusters.size(); + std::vector sCIndices; + for (unsigned int scId = 0; scId < nSimClusters; ++scId) { + if (hardScatterOnly_ && (simClusters[scId].g4Tracks()[0].eventId().event() != 0 or + simClusters[scId].g4Tracks()[0].eventId().bunchCrossing() != 0)) { + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << "Excluding SimCluster from event: " << simClusters[scId].g4Tracks()[0].eventId().event() + << " with BX: " << simClusters[scId].g4Tracks()[0].eventId().bunchCrossing() << std::endl; + continue; + } + sCIndices.emplace_back(scId); + } + nSimClusters = sCIndices.size(); + + // Initialize tssInSimCluster. To be returned outside, since it contains the + // information to compute the SimCluster-To-Trackster score. + // tssInSimCluster[scId][layerId]: + hgcal::simClusterToTrackster tssInSimCluster; + tssInSimCluster.resize(nSimClusters); + for (unsigned int i = 0; i < nSimClusters; ++i) { + tssInSimCluster[i].resize(layers_ * 2); + for (unsigned int j = 0; j < layers_ * 2; ++j) { + tssInSimCluster[i][j].simClusterId = i; + tssInSimCluster[i][j].energy = 0.f; + tssInSimCluster[i][j].hits_and_fractions.clear(); + } + } + + // Fill detIdToSimClusterId_Map and update tssInSimCluster + std::unordered_map> detIdToSimClusterId_Map; + for (const auto& scId : sCIndices) { + const auto& hits_and_fractions = simClusters[scId].hits_and_fractions(); + for (const auto& it_haf : hits_and_fractions) { + const auto hitid = (it_haf.first); + const auto scLayerId = + recHitTools_->getLayerWithOffset(hitid) + layers_ * ((recHitTools_->zside(hitid) + 1) >> 1) - 1; + + const auto itcheck = hitMap_->find(hitid); + if (itcheck != hitMap_->end()) { + auto hit_find_it = detIdToSimClusterId_Map.find(hitid); + if (hit_find_it == detIdToSimClusterId_Map.end()) { + detIdToSimClusterId_Map[hitid] = std::vector(); + } + detIdToSimClusterId_Map[hitid].emplace_back(scId, it_haf.second); + + const HGCRecHit* hit = itcheck->second; + tssInSimCluster[scId][scLayerId].energy += it_haf.second * hit->energy(); + tssInSimCluster[scId][scLayerId].hits_and_fractions.emplace_back(hitid, it_haf.second); + } + } + } // end of loop over SimClusters + +#ifdef EDM_ML_DEBUG + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << "tssInSimCluster INFO (Only SimCluster filled at the moment)" << std::endl; + for (size_t sc = 0; sc < tssInSimCluster.size(); ++sc) { + LogDebug("TracksterAssociatorByEnergyScoreImpl") << "For SimCluster Idx: " << sc << " we have: " << std::endl; + for (size_t sclay = 0; sclay < tssInSimCluster[sc].size(); ++sclay) { + LogDebug("TracksterAssociatorByEnergyScoreImpl") << " On Layer: " << sclay << " we have:" << std::endl; + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << " SimClusterIdx: " << tssInSimCluster[sc][sclay].simClusterId << std::endl; + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << " Energy: " << tssInSimCluster[sc][sclay].energy << std::endl; + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << " # of clusters : " << nLayerClusters << std::endl; + double tot_energy = 0.; + for (auto const& haf : tssInSimCluster[sc][sclay].hits_and_fractions) { + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << " Hits/fraction/energy: " << (uint32_t)haf.first << "/" << haf.second << "/" + << haf.second * hitMap_->at(haf.first)->energy() << std::endl; + tot_energy += haf.second * hitMap_->at(haf.first)->energy(); + } + LogDebug("TracksterAssociatorByEnergyScoreImpl") << " Tot Sum haf: " << tot_energy << std::endl; + for (auto const& ts : tssInSimCluster[sc][sclay].tracksterIdToEnergyAndScore) { + LogDebug("TracksterAssociatorByEnergyScoreImpl") << " tsIdx/energy/score: " << ts.first << "/" + << ts.second.first << "/" << ts.second.second << std::endl; + } + } + } + + LogDebug("TracksterAssociatorByEnergyScoreImpl") << "detIdToSimClusterId_Map INFO" << std::endl; + for (auto const& sc : detIdToSimClusterId_Map) { + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << "For detId: " << (uint32_t)sc.first + << " we have found the following connections with SimClusters:" << std::endl; + for (auto const& sclu : sc.second) { + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << " SimCluster Id: " << sclu.clusterId << " with fraction: " << sclu.fraction + << " and energy: " << sclu.fraction * hitMap_->at(sc.first)->energy() << std::endl; + } + } +#endif + + // Fill detIdToLayerClusterId_Map and scsInTrackster; update tssInSimCluster + std::unordered_map> detIdToLayerClusterId_Map; + // this contains the ids of the simclusters contributing with at least one + // hit to the layer cluster and the reconstruction error. To be returned + // since this contains the information to compute the + // Trackster-To-SimCluster score. + hgcal::tracksterToSimCluster scsInTrackster; //[tsId][scId]->(energy,score) + scsInTrackster.resize(nTracksters); + + for (unsigned int tsId = 0; tsId < nTracksters; ++tsId) { + for (const auto& lcId : tracksters[tsId].vertices()) { + const std::vector>& hits_and_fractions = layerClusters[lcId].hitsAndFractions(); + unsigned int numberOfHitsInLC = hits_and_fractions.size(); + const auto firstHitDetId = hits_and_fractions[0].first; + int lcLayerId = + recHitTools_->getLayerWithOffset(firstHitDetId) + layers_ * ((recHitTools_->zside(firstHitDetId) + 1) >> 1) - 1; + + for (unsigned int hitId = 0; hitId < numberOfHitsInLC; hitId++) { + const auto rh_detid = hits_and_fractions[hitId].first; + const auto rhFraction = hits_and_fractions[hitId].second; + + auto hit_find_in_LC = detIdToLayerClusterId_Map.find(rh_detid); + if (hit_find_in_LC == detIdToLayerClusterId_Map.end()) { + detIdToLayerClusterId_Map[rh_detid] = std::vector(); + } + detIdToLayerClusterId_Map[rh_detid].emplace_back(lcId, rhFraction); + + auto hit_find_in_SC = detIdToSimClusterId_Map.find(rh_detid); + + if (hit_find_in_SC != detIdToSimClusterId_Map.end()) { + const auto itcheck = hitMap_->find(rh_detid); + const HGCRecHit* hit = itcheck->second; + //Loops through all the simclusters that have the layer cluster rechit under study + //Here is time to update the tssInSimCluster and connect the SimCluster with all + //the layer clusters that have the current rechit detid matched. + for (auto& h : hit_find_in_SC->second) { + //tssInSimCluster[simclusterId][layerId][layerclusterId]-> (energy,score) + //SC_i - > TS_j, TS_k, ... + tssInSimCluster[h.clusterId][lcLayerId].tracksterIdToEnergyAndScore[tsId].first += + h.fraction * hit->energy(); + //TS_i -> SC_j, SC_k, ... + scsInTrackster[tsId].emplace_back(h.clusterId, 0.f); + } + } + } // End loop over hits on a LayerCluster + } // End loop over LayerClusters in Trackster + } // End of loop over Tracksters + +#ifdef EDM_ML_DEBUG + for (unsigned int tsId = 0; tsId < nTracksters; ++tsId) { + for (const auto& lcId : tracksters[tsId].vertices()) { + const auto& hits_and_fractions = layerClusters[lcId].hitsAndFractions(); + unsigned int numberOfHitsInLC = hits_and_fractions.size(); + const auto firstHitDetId = hits_and_fractions[0].first; + int lcLayerId = + recHitTools_->getLayerWithOffset(firstHitDetId) + layers_ * ((recHitTools_->zside(firstHitDetId) + 1) >> 1) - 1; + + // This vector will store, for each hit in the Layercluster, the index of + // the SimCluster that contributed the most, in terms of energy, to it. + // Special values are: + // + // -2 --> the reconstruction fraction of the RecHit is 0 (used in the past to monitor Halo Hits) + // -3 --> same as before with the added condition that no SimCluster has been linked to this RecHit + // -1 --> the reco fraction is >0, but no SimCluster has been linked to it + // >=0 --> index of the linked SimCluster + std::vector hitsToSimClusterId(numberOfHitsInLC); + // This will store the index of the SimCluster linked to the LayerCluster that has the largest number of hits in common. + int maxSCId_byNumberOfHits = -1; + // This will store the maximum number of shared hits between a LayerCluster and a SimCluster + unsigned int maxSCNumberOfHitsInLC = 0; + // This will store the index of the SimCluster linked to the LayerCluster that has the largest energy in common. + int maxSCId_byEnergy = -1; + // This will store the maximum number of shared energy between a LayerCluster and a SimCluster + float maxEnergySharedLCandSC = 0.f; + // This will store the fraction of the LayerCluster energy shared with the best(energy) SimCluster: e_shared/lc_energy + float energyFractionOfLCinSC = 0.f; + // This will store the fraction of the SimCluster energy shared with the Trackster: e_shared/sc_energy + float energyFractionOfSCinLC = 0.f; + std::unordered_map occurrencesSCinLC; + unsigned int numberOfNoiseHitsInLC = 0; + std::unordered_map SCEnergyInLC; + + for (unsigned int hitId = 0; hitId < numberOfHitsInLC; hitId++) { + const auto rh_detid = hits_and_fractions[hitId].first; + const auto rhFraction = hits_and_fractions[hitId].second; + + auto hit_find_in_SC = detIdToSimClusterId_Map.find(rh_detid); + + // if the fraction is zero or the hit does not belong to any SimCluster, + // set the caloparticleId for the hit to -1; this will + // contribute to the number of noise hits + + // MR Remove the case in which the fraction is 0, since this could be a + // real hit that has been marked as halo. + if (rhFraction == 0.) { + hitsToSimClusterId[hitId] = -2; + } + //Now check if there are SimClusters linked to this rechit of the layercluster + if (hit_find_in_SC == detIdToSimClusterId_Map.end()) { + hitsToSimClusterId[hitId] -= 1; + } else { + const auto itcheck = hitMap_->find(rh_detid); + const HGCRecHit* hit = itcheck->second; + auto maxSCEnergyInLC = 0.f; + auto maxSCId = -1; + //Loop through all the linked SimClusters + for (auto& h : hit_find_in_SC->second) { + SCEnergyInLC[h.clusterId] += h.fraction * hit->energy(); + // Keep track of which SimCluster contributed the most, in terms + // of energy, to this specific Trackster. + if (SCEnergyInLC[h.clusterId] > maxSCEnergyInLC) { + maxSCEnergyInLC = SCEnergyInLC[h.clusterId]; + maxSCId = h.clusterId; + } + } + hitsToSimClusterId[hitId] = maxSCId; + } + } // End loop over hits on a LayerCluster + + for (const auto& c : hitsToSimClusterId) { + if (c < 0) { + numberOfNoiseHitsInLC++; + } else { + occurrencesSCinLC[c]++; + } + } + + for (const auto& c : occurrencesSCinLC) { + if (c.second > maxSCNumberOfHitsInLC) { + maxSCId_byNumberOfHits = c.first; + maxSCNumberOfHitsInLC = c.second; + } + } + + for (const auto& c : SCEnergyInLC) { + if (c.second > maxEnergySharedLCandSC) { + maxSCId_byEnergy = c.first; + maxEnergySharedLCandSC = c.second; + } + } + + float totalSCEnergyOnLayer = 0.f; + if (maxSCId_byEnergy >= 0) { + totalSCEnergyOnLayer = tssInSimCluster[maxSCId_byEnergy][lcLayerId].energy; + energyFractionOfSCinLC = maxEnergySharedLCandSC / totalSCEnergyOnLayer; + if (tracksters[tsId].raw_energy() > 0.f) { + energyFractionOfLCinSC = maxEnergySharedLCandSC / tracksters[tsId].raw_energy(); + } + } + + LogDebug("TracksterAssociatorByEnergyScoreImpl") << std::setw(12) << "TracksterID:" + << "\t" << std::setw(10) << "LayerId:" + << "\t" << std::setw(12) << "layerCluster" + << "\t" << std::setw(10) << "lc energy" + << "\t" << std::setw(5) << "nhits" + << "\t" << std::setw(12) << "noise hits" + << "\t" << std::setw(22) << "maxSCId_byNumberOfHits" + << "\t" << std::setw(8) << "nhitsSC" + << "\t" << std::setw(13) << "maxSCId_byEnergy" + << "\t" << std::setw(20) << "maxEnergySharedLCandSC" + << "\t" << std::setw(22) << "totalSCEnergyOnLayer" + << "\t" << std::setw(22) << "energyFractionOfLCinSC" + << "\t" << std::setw(25) << "energyFractionOfSCinLC" + << "\t" << "\n"; + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << std::setw(12) << tsID << "\t" << std::setw(10) << lcLayerId << "\t" << std::setw(12) << lcId << "\t" << std::setw(10) + << tracksters[tsId].raw_energy() << "\t" << std::setw(5) << numberOfHitsInLC << "\t" << std::setw(12) + << numberOfNoiseHitsInLC << "\t" << std::setw(22) << maxSCId_byNumberOfHits << "\t" << std::setw(8) + << maxSCNumberOfHitsInLC << "\t" << std::setw(13) << maxSCId_byEnergy << "\t" << std::setw(20) + << maxEnergySharedLCandSC << "\t" << std::setw(22) << totalSCEnergyOnLayer << "\t" << std::setw(22) + << energyFractionOfLCinSC << "\t" << std::setw(25) << energyFractionOfSCinLC << "\n"; + } // End of loop over LayerClusters in Trackster + } // End of loop over Tracksters + + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << "Improved tssInSimCluster INFO (Now containing the linked tracksters id and energy - score still empty)" + << std::endl; + for (size_t sc = 0; sc < tssInSimCluster.size(); ++sc) { + LogDebug("TracksterAssociatorByEnergyScoreImpl") << "For SimCluster Idx: " << sc << " we have: " << std::endl; + for (size_t sclay = 0; sclay < tssInSimCluster[sc].size(); ++sclay) { + LogDebug("TracksterAssociatorByEnergyScoreImpl") << " On Layer: " << sclay << " we have:" << std::endl; + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << " SimClusterIdx: " << tssInSimCluster[sc][sclay].simClusterId << std::endl; + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << " Energy: " << tssInSimCluster[sc][sclay].energy << std::endl; + double tot_energy = 0.; + for (auto const& haf : tssInSimCluster[sc][sclay].hits_and_fractions) { + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << " Hits/fraction/energy: " << (uint32_t)haf.first << "/" << haf.second << "/" + << haf.second * hitMap_->at(haf.first)->energy() << std::endl; + tot_energy += haf.second * hitMap_->at(haf.first)->energy(); + } + LogDebug("TracksterAssociatorByEnergyScoreImpl") << " Tot Sum haf: " << tot_energy << std::endl; + for (auto const& ts : tssInSimCluster[sc][sclay].tracksterIdToEnergyAndScore) { + LogDebug("TracksterAssociatorByEnergyScoreImpl") << " tsIdx/energy/score: " << ts.first << "/" + << ts.second.first << "/" << ts.second.second << std::endl; + } + } + } + + LogDebug("TracksterAssociatorByEnergyScoreImpl") << "Improved detIdToSimClusterId_Map INFO" << std::endl; + for (auto const& sc : detIdToSimClusterId_Map) { + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << "For detId: " << (uint32_t)sc.first + << " we have found the following connections with SimClusters:" << std::endl; + for (auto const& sclu : sc.second) { + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << " SimCluster Id: " << sclu.clusterId << " with fraction: " << sclu.fraction + << " and energy: " << sclu.fraction * hitMap_->at(sc.first)->energy() << std::endl; + } + } +#endif + + // Update scsInTrackster; compute the score Trackster-to-SimCluster, + // together with the returned AssociationMap + for (unsigned int tsId = 0; tsId < nTracksters; ++tsId) { + // The simclusters contributing to the layer clusters should already be unique. + // find the unique simclusters id contributing to the layer clusters + std::sort(scsInTrackster[tsId].begin(), scsInTrackster[tsId].end()); + auto last = std::unique(scsInTrackster[tsId].begin(), scsInTrackster[tsId].end()); + scsInTrackster[tsId].erase(last, scsInTrackster[tsId].end()); + + // If a reconstructed Trackster has energy 0 but is linked to a + // SimCluster, assigned score 1 + if (tracksters[tsId].raw_energy() == 0. && !scsInTrackster[tsId].empty()) { + for (auto& scPair : scsInTrackster[tsId]) { + scPair.second = 1.; + LogDebug("TracksterAssociatorByEnergyScoreImpl") << "tracksterId : \t " << tsId << "\t SC id : \t" + << scPair.first << "\t score \t " << scPair.second << "\n"; + } + continue; + } + + for (const auto& lcId : tracksters[tsId].vertices()) { + const auto& hits_and_fractions = layerClusters[lcId].hitsAndFractions(); + unsigned int numberOfHitsInLC = hits_and_fractions.size(); + + // Compute the correct normalization + float invTracksterEnergyWeight = 0.f; + for (auto const& haf : layerClusters[lcId].hitsAndFractions()) { + invTracksterEnergyWeight += + (haf.second * hitMap_->at(haf.first)->energy()) * (haf.second * hitMap_->at(haf.first)->energy()); + } + invTracksterEnergyWeight = 1.f / invTracksterEnergyWeight; + for (unsigned int i = 0; i < numberOfHitsInLC; ++i) { + DetId rh_detid = hits_and_fractions[i].first; + float rhFraction = hits_and_fractions[i].second; + + bool hitWithSC = (detIdToSimClusterId_Map.find(rh_detid) != detIdToSimClusterId_Map.end()); + + auto itcheck = hitMap_->find(rh_detid); + const HGCRecHit* hit = itcheck->second; + float hitEnergyWeight = hit->energy() * hit->energy(); + + for (auto& scPair : scsInTrackster[tsId]) { + float scFraction = 0.f; + if (hitWithSC) { + auto findHitIt = std::find(detIdToSimClusterId_Map[rh_detid].begin(), + detIdToSimClusterId_Map[rh_detid].end(), + hgcal::detIdInfoInCluster{scPair.first, 0.f}); + if (findHitIt != detIdToSimClusterId_Map[rh_detid].end()) + scFraction = findHitIt->fraction; + } + scPair.second += + (rhFraction - scFraction) * (rhFraction - scFraction) * hitEnergyWeight * invTracksterEnergyWeight; + #ifdef EDM_ML_DEBUG + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << "rh_detid:\t" << (uint32_t)rh_detid << "\ttracksterId:\t" << tsId << "\t" + << "rhfraction,scFraction:\t" << rhFraction << ", " << scFraction << "\t" + << "hitEnergyWeight:\t" << hitEnergyWeight << "\t" + << "current score:\t" << scPair.second << "\t" + << "invTracksterEnergyWeight:\t" << invTracksterEnergyWeight << "\n"; + #endif + } + } // End of loop over Hits within a LayerCluster + } // End of loop over LayerClusters in Trackster + #ifdef EDM_ML_DEBUG + if (scsInTrackster[tsId].empty()) + LogDebug("TracksterAssociatorByEnergyScoreImpl") << "trackster Id: \t" << tsId << "\tSC id:\t-1 " + << "\t score \t-1" + << "\n"; +#endif + } // End of loop over Tracksters + + // Compute the SimCluster-To-Trackster score + for (const auto& scId : sCIndices) { + for (unsigned int layerId = 0; layerId < layers_ * 2; ++layerId) { + unsigned int SCNumberOfHits = tssInSimCluster[scId][layerId].hits_and_fractions.size(); + if (SCNumberOfHits == 0) + continue; +#ifdef EDM_ML_DEBUG + int tsWithMaxEnergyInSC = -1; + //energy of the most energetic LC from all that were linked to SC + float maxEnergyLCinSC = 0.f; + float SCenergy = tssInSimCluster[scId][layerId].energy; + //most energetic LC from all LCs linked to SC over SC energy. + float SCEnergyFractionInLC = 0.f; + for (auto& ts : tssInSimCluster[scId][layerId].tracksterIdToEnergyAndScore) { + if (ts.second.first > maxEnergyLCinSC) { + maxEnergyLCinSC = ts.second.first; + tsWithMaxEnergyInSC = ts.first; + } + } + if (SCenergy > 0.f) + SCEnergyFractionInLC = maxEnergyLCinSC / SCenergy; + + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << std::setw(8) << "LayerId:\t" << std::setw(12) << "simcluster\t" << std::setw(15) << "sc total energy\t" + << std::setw(15) << "scEnergyOnLayer\t" << std::setw(14) << "SCNhitsOnLayer\t" << std::setw(18) + << "tsWithMaxEnergyInSC\t" << std::setw(15) << "maxEnergyLCinSC\t" << std::setw(20) << "SCEnergyFractionInLC" + << "\n"; + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << std::setw(8) << layerId << "\t" << std::setw(12) << scId << "\t" << std::setw(15) + << simClusters[scId].energy() << "\t" << std::setw(15) << SCenergy << "\t" << std::setw(14) << SCNumberOfHits + << "\t" << std::setw(18) << tsWithMaxEnergyInSC << "\t" << std::setw(15) << maxEnergyLCinSC << "\t" + << std::setw(20) << SCEnergyFractionInLC << "\n"; +#endif + // Compute the correct normalization + float invSCEnergyWeight = 0.f; + for (auto const& haf : tssInSimCluster[scId][layerId].hits_and_fractions) { + invSCEnergyWeight += std::pow(haf.second * hitMap_->at(haf.first)->energy(), 2); + } + invSCEnergyWeight = 1.f / invSCEnergyWeight; + for (unsigned int i = 0; i < SCNumberOfHits; ++i) { + auto& sc_hitDetId = tssInSimCluster[scId][layerId].hits_and_fractions[i].first; + auto& scFraction = tssInSimCluster[scId][layerId].hits_and_fractions[i].second; + + bool hitWithLC = false; + if (scFraction == 0.f) + continue; //hopefully this should never happen + auto hit_find_in_LC = detIdToLayerClusterId_Map.find(sc_hitDetId); + if (hit_find_in_LC != detIdToLayerClusterId_Map.end()) + hitWithLC = true; + auto itcheck = hitMap_->find(sc_hitDetId); + const HGCRecHit* hit = itcheck->second; + float hitEnergyWeight = hit->energy() * hit->energy(); + for (auto& tsPair : tssInSimCluster[scId][layerId].tracksterIdToEnergyAndScore) { + unsigned int tracksterId = tsPair.first; + float tsFraction = 0.f; + + if (hitWithLC) { + auto findHitIt = std::find(detIdToLayerClusterId_Map[sc_hitDetId].begin(), + detIdToLayerClusterId_Map[sc_hitDetId].end(), + hgcal::detIdInfoInCluster{tracksterId, 0.f}); + if (findHitIt != detIdToLayerClusterId_Map[sc_hitDetId].end()) + tsFraction = findHitIt->fraction; + } + tsPair.second.second += + (tsFraction - scFraction) * (tsFraction - scFraction) * hitEnergyWeight * invSCEnergyWeight; +#ifdef EDM_ML_DEBUG + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << "scDetId:\t" << (uint32_t)sc_hitDetId << "\ttracksterId:\t" << tracksterId << "\t" + << "tsFraction,scFraction:\t" << tsFraction << ", " << scFraction << "\t" + << "hitEnergyWeight:\t" << hitEnergyWeight << "\t" + << "current score:\t" << tsPair.second.second << "\t" + << "invSCEnergyWeight:\t" << invSCEnergyWeight << "\n"; +#endif + } // End of loop over LayerClusters linked to hits of this SimCluster + } // End of loop over hits of SimCluster on a Layer +#ifdef EDM_ML_DEBUG + if (tssInSimCluster[scId][layerId].tracksterIdToEnergyAndScore.empty()) + LogDebug("TracksterAssociatorByEnergyScoreImpl") << "SC Id: \t" << scId << "\tTS id:\t-1 " + << "\t score \t-1" + << "\n"; + + for (const auto& tsPair : tssInSimCluster[scId][layerId].tracksterIdToEnergyAndScore) { + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << "SC Id: \t" << scId << "\t TS id: \t" << tsPair.first << "\t score \t" << tsPair.second.second + << "\t shared energy:\t" << tsPair.second.first << "\t shared energy fraction:\t" + << (tsPair.second.first / SCenergy) << "\n"; + } +#endif + } + } + return {scsInTrackster, tssInSimCluster}; +} + +hgcal::RecoToSimCollectionTracksters TracksterAssociatorByEnergyScoreImpl::associateRecoToSim( + const edm::Handle& tCH, const edm::Handle& lCCH, const edm::Handle& sCCH) const { + hgcal::RecoToSimCollectionTracksters returnValue(productGetter_); + const auto& links = makeConnections(tCH, lCCH, sCCH); + + const auto& scsInTrackster = std::get<0>(links); + for (size_t tsId = 0; tsId < scsInTrackster.size(); ++tsId) { + for (auto& scPair : scsInTrackster[tsId]) { + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << "trackster Id: \t" << tsId << "\t SC id: \t" << scPair.first << "\t score \t" << scPair.second << "\n"; + // Fill AssociationMap + returnValue.insert(edm::Ref(tCH, tsId), // Ref to TS + std::make_pair(edm::Ref(sCCH, scPair.first), + scPair.second) // Pair + ); + } + } + return returnValue; +} + +hgcal::SimToRecoCollectionTracksters TracksterAssociatorByEnergyScoreImpl::associateSimToReco( + const edm::Handle& tCH, const edm::Handle& lCCH, const edm::Handle& sCCH) const { + hgcal::SimToRecoCollectionTracksters returnValue(productGetter_); + const auto& links = makeConnections(tCH, lCCH, sCCH); + const auto& tssInSimCluster = std::get<1>(links); + for (size_t scId = 0; scId < tssInSimCluster.size(); ++scId) { + for (size_t layerId = 0; layerId < tssInSimCluster[scId].size(); ++layerId) { + for (auto& tsPair : tssInSimCluster[scId][layerId].tracksterIdToEnergyAndScore) { + returnValue.insert( + edm::Ref(sCCH, scId), // Ref to SC + std::make_pair(edm::Ref(tCH, tsPair.first), // Pair > + ); + } + } + } + return returnValue; +} diff --git a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.h b/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.h new file mode 100644 index 0000000000000..d67b5b672ac20 --- /dev/null +++ b/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.h @@ -0,0 +1,73 @@ +// Original Author: Leonardo Cristella + +#include +#include +#include +#include // shared_ptr + +#include "DataFormats/ForwardDetId/interface/HGCalDetId.h" +#include "DataFormats/HGCRecHit/interface/HGCRecHit.h" +#include "SimDataFormats/Associations/interface/TracksterToSimClusterAssociator.h" +#include "RecoLocalCalo/HGCalRecAlgos/interface/RecHitTools.h" + +namespace edm { + class EDProductGetter; +} + +namespace hgcal { + struct detIdInfoInCluster { + bool operator==(const detIdInfoInCluster &o) const { return clusterId == o.clusterId; }; + long unsigned int clusterId; + float fraction; + detIdInfoInCluster(long unsigned int cId, float fr) { + clusterId = cId; + fraction = fr; + } + }; + + struct detIdInfoInMultiCluster { + bool operator==(const detIdInfoInMultiCluster &o) const { return multiclusterId == o.multiclusterId; }; + unsigned int multiclusterId; + long unsigned int clusterId; + float fraction; + }; + + struct simClusterOnLayer { + unsigned int simClusterId; + float energy = 0; + std::vector> hits_and_fractions; + std::unordered_map> tracksterIdToEnergyAndScore; + }; + + typedef std::vector>> tracksterToSimCluster; + typedef std::vector> simClusterToTrackster; + typedef std::tuple association; +} // namespace hgcal + +class TracksterAssociatorByEnergyScoreImpl : public hgcal::TracksterToSimClusterAssociatorBaseImpl { +public: + explicit TracksterAssociatorByEnergyScoreImpl(edm::EDProductGetter const &, + bool, + std::shared_ptr, + const std::unordered_map *); + + hgcal::RecoToSimCollectionTracksters associateRecoToSim( + const edm::Handle &tCH, + const edm::Handle &lCCH, + const edm::Handle &sCCH) const override; + + hgcal::SimToRecoCollectionTracksters associateSimToReco( + const edm::Handle &tCH, + const edm::Handle &lCCH, + const edm::Handle &sCCH) const override; + +private: + const bool hardScatterOnly_; + std::shared_ptr recHitTools_; + const std::unordered_map *hitMap_; + unsigned layers_; + edm::EDProductGetter const *productGetter_; + hgcal::association makeConnections(const edm::Handle &tCH, + const edm::Handle &lCCH, + const edm::Handle &sCCH) const; +}; diff --git a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreProducer.cc b/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreProducer.cc new file mode 100644 index 0000000000000..769ae760e1afb --- /dev/null +++ b/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreProducer.cc @@ -0,0 +1,67 @@ +// Original author: Leonardo Cristella + +// user include files +#include "FWCore/Framework/interface/Frameworkfwd.h" +#include "FWCore/Framework/interface/global/EDProducer.h" + +#include "FWCore/Framework/interface/Event.h" +#include "FWCore/Framework/interface/MakerMacros.h" + +#include "FWCore/ParameterSet/interface/ParameterSet.h" +#include "FWCore/Utilities/interface/EDGetToken.h" +#include "FWCore/Utilities/interface/ESGetToken.h" + +#include "SimDataFormats/Associations/interface/TracksterToSimClusterAssociator.h" +#include "TracksterAssociatorByEnergyScoreImpl.h" + +class TracksterAssociatorByEnergyScoreProducer : public edm::global::EDProducer<> { +public: + explicit TracksterAssociatorByEnergyScoreProducer(const edm::ParameterSet &); + ~TracksterAssociatorByEnergyScoreProducer() override; + + static void fillDescriptions(edm::ConfigurationDescriptions &descriptions); + +private: + void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override; + edm::EDGetTokenT> hitMap_; + edm::ESGetToken caloGeometry_; + const bool hardScatterOnly_; + std::shared_ptr rhtools_; +}; + +TracksterAssociatorByEnergyScoreProducer::TracksterAssociatorByEnergyScoreProducer(const edm::ParameterSet &ps) + : hitMap_(consumes>(ps.getParameter("hitMapTag"))), + caloGeometry_(esConsumes()), + hardScatterOnly_(ps.getParameter("hardScatterOnly")) { + rhtools_.reset(new hgcal::RecHitTools()); + + // Register the product + produces(); +} + +TracksterAssociatorByEnergyScoreProducer::~TracksterAssociatorByEnergyScoreProducer() {} + +void TracksterAssociatorByEnergyScoreProducer::produce(edm::StreamID, + edm::Event &iEvent, + const edm::EventSetup &es) const { + edm::ESHandle geom = es.getHandle(caloGeometry_); + rhtools_->setGeometry(*geom); + + const std::unordered_map *hitMap = &iEvent.get(hitMap_); + + auto impl = std::make_unique( + iEvent.productGetter(), hardScatterOnly_, rhtools_, hitMap); + auto toPut = std::make_unique(std::move(impl)); + iEvent.put(std::move(toPut)); +} + +void TracksterAssociatorByEnergyScoreProducer::fillDescriptions(edm::ConfigurationDescriptions &cfg) { + edm::ParameterSetDescription desc; + desc.add("hitMapTag", edm::InputTag("hgcalRecHitMapProducer")); + desc.add("hardScatterOnly", true); + + cfg.add("tracksterAssociatorByEnergyScore", desc); +} + +//define this as a plug-in +DEFINE_FWK_MODULE(TracksterAssociatorByEnergyScoreProducer); diff --git a/SimCalorimetry/HGCalAssociatorProducers/python/TSToSCAssociation_cfi.py b/SimCalorimetry/HGCalAssociatorProducers/python/TSToSCAssociation_cfi.py new file mode 100644 index 0000000000000..fe5b6a498740a --- /dev/null +++ b/SimCalorimetry/HGCalAssociatorProducers/python/TSToSCAssociation_cfi.py @@ -0,0 +1,13 @@ +import FWCore.ParameterSet.Config as cms + +tracksterSimClusterAssociation = cms.EDProducer("TSToSCAssociatorEDProducer", + associator = cms.InputTag('tsAssocByEnergyScoreProducer'), + label_scl = cms.InputTag("mix","MergedCaloTruth"), + label_tst = cms.InputTag("ticlTrackstersMerge"), + label_lcl = cms.InputTag("hgcalLayerClusters") +) + +from Configuration.ProcessModifiers.premix_stage2_cff import premix_stage2 +premix_stage2.toModify(tracksterSimClusterAssociation, + label_scl = "mixData:MergedCaloTruth" +) diff --git a/SimDataFormats/Associations/interface/TracksterToSimClusterAssociator.h b/SimDataFormats/Associations/interface/TracksterToSimClusterAssociator.h new file mode 100644 index 0000000000000..f2c8b388dd153 --- /dev/null +++ b/SimDataFormats/Associations/interface/TracksterToSimClusterAssociator.h @@ -0,0 +1,50 @@ +#ifndef SimDataFormats_Associations_TracksterToSimClusterAssociator_h +#define SimDataFormats_Associations_TracksterToSimClusterAssociator_h +// Original Author: Leonardo Cristella + +// system include files +#include + +// user include files + +#include "SimDataFormats/Associations/interface/TracksterToSimClusterAssociatorBaseImpl.h" + +// forward declarations + +namespace hgcal { + + class TracksterToSimClusterAssociator { + public: + TracksterToSimClusterAssociator(std::unique_ptr); + TracksterToSimClusterAssociator() = default; + TracksterToSimClusterAssociator(TracksterToSimClusterAssociator &&) = default; + TracksterToSimClusterAssociator &operator=(TracksterToSimClusterAssociator &&) = default; + ~TracksterToSimClusterAssociator() = default; + + // ---------- const member functions --------------------- + /// Associate a Trackster to SimClusters + hgcal::RecoToSimCollectionTracksters associateRecoToSim(const edm::Handle &tCH, + const edm::Handle &lCCH, + const edm::Handle &sCCH) const { + return m_impl->associateRecoToSim(tCH, lCCH, sCCH); + }; + + /// Associate a SimCluster to Tracksters + hgcal::SimToRecoCollectionTracksters associateSimToReco(const edm::Handle &tCH, + const edm::Handle &lCCH, + const edm::Handle &sCCH) const { + return m_impl->associateSimToReco(tCH, lCCH, sCCH); + } + + private: + TracksterToSimClusterAssociator(const TracksterToSimClusterAssociator &) = delete; // stop default + + const TracksterToSimClusterAssociator &operator=(const TracksterToSimClusterAssociator &) = + delete; // stop default + + // ---------- member data -------------------------------- + std::unique_ptr m_impl; + }; +} // namespace hgcal + +#endif diff --git a/SimDataFormats/Associations/interface/TracksterToSimClusterAssociatorBaseImpl.h b/SimDataFormats/Associations/interface/TracksterToSimClusterAssociatorBaseImpl.h new file mode 100644 index 0000000000000..4d88465ce0b8d --- /dev/null +++ b/SimDataFormats/Associations/interface/TracksterToSimClusterAssociatorBaseImpl.h @@ -0,0 +1,45 @@ +#ifndef SimDataFormats_Associations_TracksterToSimClusterAssociatorBaseImpl_h +#define SimDataFormats_Associations_TracksterToSimClusterAssociatorBaseImpl_h + +/** \class TracksterToSimClusterAssociatorBaseImpl + * + * Base class for TracksterToSimClusterAssociators. Methods take as input + * the handle of Tracksters and the SimCluster collections and return an + * AssociationMap (oneToManyWithQuality) + * + * \author Leonardo Cristella + */ + +#include "DataFormats/Common/interface/Handle.h" +#include "DataFormats/Common/interface/AssociationMap.h" +#include "DataFormats/HGCalReco/interface/Trackster.h" +#include "DataFormats/CaloRecHit/interface/CaloClusterFwd.h" + +#include "SimDataFormats/CaloAnalysis/interface/SimClusterFwd.h" + +namespace hgcal { + + typedef edm::AssociationMap< + edm::OneToManyWithQualityGeneric>> + SimToRecoCollectionTracksters; + typedef edm::AssociationMap> + RecoToSimCollectionTracksters; + + class TracksterToSimClusterAssociatorBaseImpl { + public: + /// Constructor + TracksterToSimClusterAssociatorBaseImpl(); + /// Destructor + virtual ~TracksterToSimClusterAssociatorBaseImpl(); + + /// Associate a Trackster to SimClusters + virtual hgcal::RecoToSimCollectionTracksters associateRecoToSim( + const edm::Handle &tCH, const edm::Handle &lCCH, const edm::Handle &sCCH) const; + + /// Associate a SimCluster to Tracksters + virtual hgcal::SimToRecoCollectionTracksters associateSimToReco( + const edm::Handle &tCH, const edm::Handle &lCCH, const edm::Handle &sCCH) const; + }; +} // namespace hgcal + +#endif diff --git a/SimDataFormats/Associations/plugins/BuildFile.xml b/SimDataFormats/Associations/plugins/BuildFile.xml index bd62ae6ee8f1b..57385a0812e8c 100644 --- a/SimDataFormats/Associations/plugins/BuildFile.xml +++ b/SimDataFormats/Associations/plugins/BuildFile.xml @@ -1,5 +1,6 @@ + diff --git a/SimDataFormats/Associations/plugins/TSToSCAssociatorEDProducer.cc b/SimDataFormats/Associations/plugins/TSToSCAssociatorEDProducer.cc new file mode 100644 index 0000000000000..1956bd26545b7 --- /dev/null +++ b/SimDataFormats/Associations/plugins/TSToSCAssociatorEDProducer.cc @@ -0,0 +1,96 @@ +// +// Original Author: Leonardo Cristella +// Created: Thu Dec 3 10:52:11 CET 2020 +// +// + +// system include files +#include +#include + +// user include files +#include "FWCore/Framework/interface/global/EDProducer.h" + +#include "FWCore/Framework/interface/Event.h" +#include "FWCore/Framework/interface/MakerMacros.h" + +#include "FWCore/Framework/interface/ESHandle.h" + +#include "FWCore/ParameterSet/interface/ParameterSet.h" + +#include "SimDataFormats/Associations/interface/TracksterToSimClusterAssociator.h" + +#include "FWCore/MessageLogger/interface/MessageLogger.h" +#include "DataFormats/HGCalReco/interface/Trackster.h" + +#include "FWCore/Utilities/interface/EDGetToken.h" + +// +// class decleration +// + +class TSToSCAssociatorEDProducer : public edm::global::EDProducer<> { +public: + explicit TSToSCAssociatorEDProducer(const edm::ParameterSet &); + ~TSToSCAssociatorEDProducer() override; + +private: + void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override; + + edm::EDGetTokenT SCCollectionToken_; + edm::EDGetTokenT TSCollectionToken_; + edm::EDGetTokenT LCCollectionToken_; + edm::EDGetTokenT associatorToken_; +}; + +TSToSCAssociatorEDProducer::TSToSCAssociatorEDProducer(const edm::ParameterSet &pset) { + produces(); + produces(); + + SCCollectionToken_ = consumes(pset.getParameter("label_scl")); + TSCollectionToken_ = consumes(pset.getParameter("label_tst")); + LCCollectionToken_ = consumes(pset.getParameter("label_lcl")); + associatorToken_ = + consumes(pset.getParameter("associator")); +} + +TSToSCAssociatorEDProducer::~TSToSCAssociatorEDProducer() {} + +// +// member functions +// + +// ------------ method called to produce the data ------------ +void TSToSCAssociatorEDProducer::produce(edm::StreamID, edm::Event &iEvent, const edm::EventSetup &iSetup) const { + using namespace edm; + + edm::Handle theAssociator; + iEvent.getByToken(associatorToken_, theAssociator); + + Handle SCCollection; + iEvent.getByToken(SCCollectionToken_, SCCollection); + + Handle TSCollection; + iEvent.getByToken(TSCollectionToken_, TSCollection); + + Handle LCCollection; + iEvent.getByToken(LCCollectionToken_, LCCollection); + + // associate LC and SC + LogTrace("AssociatorValidator") << "Calling associateRecoToSim method" + << "\n"; + hgcal::RecoToSimCollectionTracksters recSimColl = theAssociator->associateRecoToSim(TSCollection, LCCollection, SCCollection); + + LogTrace("AssociatorValidator") << "Calling associateSimToReco method" + << "\n"; + hgcal::SimToRecoCollectionTracksters simRecColl = theAssociator->associateSimToReco(TSCollection, LCCollection, SCCollection); + + auto rts = std::make_unique(recSimColl); + auto str = std::make_unique(simRecColl); + + iEvent.put(std::move(rts)); + iEvent.put(std::move(str)); +} + +// define this as a plug-in +DEFINE_FWK_MODULE(TSToSCAssociatorEDProducer); diff --git a/SimDataFormats/Associations/src/TracksterToSimClusterAssociator.cc b/SimDataFormats/Associations/src/TracksterToSimClusterAssociator.cc new file mode 100644 index 0000000000000..791ab3acfecf1 --- /dev/null +++ b/SimDataFormats/Associations/src/TracksterToSimClusterAssociator.cc @@ -0,0 +1,7 @@ +// Original Author: Leonardo Cristella + +#include "SimDataFormats/Associations/interface/TracksterToSimClusterAssociator.h" + +hgcal::TracksterToSimClusterAssociator::TracksterToSimClusterAssociator( + std::unique_ptr ptr) + : m_impl(std::move(ptr)) {} diff --git a/SimDataFormats/Associations/src/TracksterToSimClusterAssociatorBaseImpl.cc b/SimDataFormats/Associations/src/TracksterToSimClusterAssociatorBaseImpl.cc new file mode 100644 index 0000000000000..8e796bfc39f9d --- /dev/null +++ b/SimDataFormats/Associations/src/TracksterToSimClusterAssociatorBaseImpl.cc @@ -0,0 +1,19 @@ +// Original Author: Leonardo Cristella + +#include "SimDataFormats/Associations/interface/TracksterToSimClusterAssociatorBaseImpl.h" + +namespace hgcal { + TracksterToSimClusterAssociatorBaseImpl::TracksterToSimClusterAssociatorBaseImpl(){}; + TracksterToSimClusterAssociatorBaseImpl::~TracksterToSimClusterAssociatorBaseImpl(){}; + + hgcal::RecoToSimCollectionTracksters TracksterToSimClusterAssociatorBaseImpl::associateRecoToSim( + const edm::Handle &tCH, const edm::Handle &lCCH, const edm::Handle &sCCH) const { + return hgcal::RecoToSimCollectionTracksters(); + } + + hgcal::SimToRecoCollectionTracksters TracksterToSimClusterAssociatorBaseImpl::associateSimToReco( + const edm::Handle &tCH, const edm::Handle &lCCH, const edm::Handle &sCCH) const { + return hgcal::SimToRecoCollectionTracksters(); + } + +} // namespace hgcal From 61e694845fb0f8d9d7a6e5a991b03f6e9f10a2c0 Mon Sep 17 00:00:00 2001 From: Leonardo Cristella Date: Sat, 3 Apr 2021 00:05:01 +0200 Subject: [PATCH 2/8] Compute energyWeights across layers --- .../TracksterAssociatorByEnergyScoreImpl.cc | 122 ++++++++++-------- 1 file changed, 66 insertions(+), 56 deletions(-) diff --git a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.cc b/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.cc index dd0cb2a4f2dc8..2af7aed9811c8 100644 --- a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.cc +++ b/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.cc @@ -55,13 +55,13 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( for (const auto& scId : sCIndices) { const auto& hits_and_fractions = simClusters[scId].hits_and_fractions(); for (const auto& it_haf : hits_and_fractions) { - const auto hitid = (it_haf.first); + const auto hitid = it_haf.first; const auto scLayerId = recHitTools_->getLayerWithOffset(hitid) + layers_ * ((recHitTools_->zside(hitid) + 1) >> 1) - 1; const auto itcheck = hitMap_->find(hitid); if (itcheck != hitMap_->end()) { - auto hit_find_it = detIdToSimClusterId_Map.find(hitid); + const auto hit_find_it = detIdToSimClusterId_Map.find(hitid); if (hit_find_it == detIdToSimClusterId_Map.end()) { detIdToSimClusterId_Map[hitid] = std::vector(); } @@ -103,14 +103,14 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( } LogDebug("TracksterAssociatorByEnergyScoreImpl") << "detIdToSimClusterId_Map INFO" << std::endl; - for (auto const& sc : detIdToSimClusterId_Map) { + for (auto const& detId : detIdToSimClusterId_Map) { LogDebug("TracksterAssociatorByEnergyScoreImpl") - << "For detId: " << (uint32_t)sc.first + << "For detId: " << (uint32_t)detId.first << " we have found the following connections with SimClusters:" << std::endl; - for (auto const& sclu : sc.second) { + for (auto const& sc : detId.second) { LogDebug("TracksterAssociatorByEnergyScoreImpl") - << " SimCluster Id: " << sclu.clusterId << " with fraction: " << sclu.fraction - << " and energy: " << sclu.fraction * hitMap_->at(sc.first)->energy() << std::endl; + << " SimCluster Id: " << sc.clusterId << " with fraction: " << sc.fraction + << " and energy: " << sc.fraction * hitMap_->at(detId.first)->energy() << std::endl; } } #endif @@ -118,9 +118,8 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( // Fill detIdToLayerClusterId_Map and scsInTrackster; update tssInSimCluster std::unordered_map> detIdToLayerClusterId_Map; // this contains the ids of the simclusters contributing with at least one - // hit to the layer cluster and the reconstruction error. To be returned - // since this contains the information to compute the - // Trackster-To-SimCluster score. + // hit to the Trackster. To be returned since this contains the information + // to compute the Trackster-To-SimCluster score. hgcal::tracksterToSimCluster scsInTrackster; //[tsId][scId]->(energy,score) scsInTrackster.resize(nTracksters); @@ -136,21 +135,21 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( const auto rh_detid = hits_and_fractions[hitId].first; const auto rhFraction = hits_and_fractions[hitId].second; - auto hit_find_in_LC = detIdToLayerClusterId_Map.find(rh_detid); + const auto hit_find_in_LC = detIdToLayerClusterId_Map.find(rh_detid); if (hit_find_in_LC == detIdToLayerClusterId_Map.end()) { detIdToLayerClusterId_Map[rh_detid] = std::vector(); } detIdToLayerClusterId_Map[rh_detid].emplace_back(lcId, rhFraction); - auto hit_find_in_SC = detIdToSimClusterId_Map.find(rh_detid); + const auto hit_find_in_SC = detIdToSimClusterId_Map.find(rh_detid); if (hit_find_in_SC != detIdToSimClusterId_Map.end()) { const auto itcheck = hitMap_->find(rh_detid); const HGCRecHit* hit = itcheck->second; //Loops through all the simclusters that have the layer cluster rechit under study //Here is time to update the tssInSimCluster and connect the SimCluster with all - //the layer clusters that have the current rechit detid matched. - for (auto& h : hit_find_in_SC->second) { + //the Tracksters that have the current rechit detid matched. + for (const auto& h : hit_find_in_SC->second) { //tssInSimCluster[simclusterId][layerId][layerclusterId]-> (energy,score) //SC_i - > TS_j, TS_k, ... tssInSimCluster[h.clusterId][lcLayerId].tracksterIdToEnergyAndScore[tsId].first += @@ -201,10 +200,10 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( const auto rh_detid = hits_and_fractions[hitId].first; const auto rhFraction = hits_and_fractions[hitId].second; - auto hit_find_in_SC = detIdToSimClusterId_Map.find(rh_detid); + const auto hit_find_in_SC = detIdToSimClusterId_Map.find(rh_detid); // if the fraction is zero or the hit does not belong to any SimCluster, - // set the caloparticleId for the hit to -1; this will + // set the SimCluster Id for the hit to -1; this will // contribute to the number of noise hits // MR Remove the case in which the fraction is 0, since this could be a @@ -221,10 +220,10 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( auto maxSCEnergyInLC = 0.f; auto maxSCId = -1; //Loop through all the linked SimClusters - for (auto& h : hit_find_in_SC->second) { + for (const auto& h : hit_find_in_SC->second) { SCEnergyInLC[h.clusterId] += h.fraction * hit->energy(); // Keep track of which SimCluster contributed the most, in terms - // of energy, to this specific Trackster. + // of energy, to this specific Layer Cluster. if (SCEnergyInLC[h.clusterId] > maxSCEnergyInLC) { maxSCEnergyInLC = SCEnergyInLC[h.clusterId]; maxSCId = h.clusterId; @@ -331,8 +330,8 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( // Update scsInTrackster; compute the score Trackster-to-SimCluster, // together with the returned AssociationMap for (unsigned int tsId = 0; tsId < nTracksters; ++tsId) { - // The simclusters contributing to the layer clusters should already be unique. - // find the unique simclusters id contributing to the layer clusters + // The SimClusters contributing to the Trackster's LayerClusters should already be unique. + // find the unique SimClusters id contributing to the Trackster's LayerClusters std::sort(scsInTrackster[tsId].begin(), scsInTrackster[tsId].end()); auto last = std::unique(scsInTrackster[tsId].begin(), scsInTrackster[tsId].end()); scsInTrackster[tsId].erase(last, scsInTrackster[tsId].end()); @@ -342,39 +341,43 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( if (tracksters[tsId].raw_energy() == 0. && !scsInTrackster[tsId].empty()) { for (auto& scPair : scsInTrackster[tsId]) { scPair.second = 1.; - LogDebug("TracksterAssociatorByEnergyScoreImpl") << "tracksterId : \t " << tsId << "\t SC id : \t" + LogDebug("TracksterAssociatorByEnergyScoreImpl") << "TracksterId : \t " << tsId << "\t SC id : \t" << scPair.first << "\t score \t " << scPair.second << "\n"; } continue; } + float invTracksterEnergyWeight = 0.f; for (const auto& lcId : tracksters[tsId].vertices()) { const auto& hits_and_fractions = layerClusters[lcId].hitsAndFractions(); - unsigned int numberOfHitsInLC = hits_and_fractions.size(); // Compute the correct normalization - float invTracksterEnergyWeight = 0.f; - for (auto const& haf : layerClusters[lcId].hitsAndFractions()) { + for (auto const& haf : hits_and_fractions) { invTracksterEnergyWeight += (haf.second * hitMap_->at(haf.first)->energy()) * (haf.second * hitMap_->at(haf.first)->energy()); } - invTracksterEnergyWeight = 1.f / invTracksterEnergyWeight; + } + invTracksterEnergyWeight = 1.f / invTracksterEnergyWeight; + + for (const auto& lcId : tracksters[tsId].vertices()) { + const auto& hits_and_fractions = layerClusters[lcId].hitsAndFractions(); + unsigned int numberOfHitsInLC = hits_and_fractions.size(); for (unsigned int i = 0; i < numberOfHitsInLC; ++i) { DetId rh_detid = hits_and_fractions[i].first; float rhFraction = hits_and_fractions[i].second; - bool hitWithSC = (detIdToSimClusterId_Map.find(rh_detid) != detIdToSimClusterId_Map.end()); + const bool hitWithSC = (detIdToSimClusterId_Map.find(rh_detid) != detIdToSimClusterId_Map.end()); - auto itcheck = hitMap_->find(rh_detid); + const auto itcheck = hitMap_->find(rh_detid); const HGCRecHit* hit = itcheck->second; float hitEnergyWeight = hit->energy() * hit->energy(); for (auto& scPair : scsInTrackster[tsId]) { float scFraction = 0.f; if (hitWithSC) { - auto findHitIt = std::find(detIdToSimClusterId_Map[rh_detid].begin(), - detIdToSimClusterId_Map[rh_detid].end(), - hgcal::detIdInfoInCluster{scPair.first, 0.f}); + const auto findHitIt = std::find(detIdToSimClusterId_Map[rh_detid].begin(), + detIdToSimClusterId_Map[rh_detid].end(), + hgcal::detIdInfoInCluster{scPair.first, 0.f}); if (findHitIt != detIdToSimClusterId_Map[rh_detid].end()) scFraction = findHitIt->fraction; } @@ -391,64 +394,71 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( } } // End of loop over Hits within a LayerCluster } // End of loop over LayerClusters in Trackster - #ifdef EDM_ML_DEBUG + + #ifdef EDM_ML_DEBUG if (scsInTrackster[tsId].empty()) LogDebug("TracksterAssociatorByEnergyScoreImpl") << "trackster Id: \t" << tsId << "\tSC id:\t-1 " << "\t score \t-1" << "\n"; -#endif + #endif } // End of loop over Tracksters // Compute the SimCluster-To-Trackster score for (const auto& scId : sCIndices) { + float invSCEnergyWeight = 0.f; + for (unsigned int layerId = 0; layerId < layers_ * 2; ++layerId) { - unsigned int SCNumberOfHits = tssInSimCluster[scId][layerId].hits_and_fractions.size(); + const unsigned int SCNumberOfHits = tssInSimCluster[scId][layerId].hits_and_fractions.size(); if (SCNumberOfHits == 0) continue; #ifdef EDM_ML_DEBUG int tsWithMaxEnergyInSC = -1; - //energy of the most energetic LC from all that were linked to SC - float maxEnergyLCinSC = 0.f; + //energy of the most energetic TS from all that were linked to SC + float maxEnergyTSinSC = 0.f; float SCenergy = tssInSimCluster[scId][layerId].energy; - //most energetic LC from all LCs linked to SC over SC energy. - float SCEnergyFractionInLC = 0.f; + //most energetic TS from all TSs linked to SC over SC energy. + float SCEnergyFractionInTS = 0.f; for (auto& ts : tssInSimCluster[scId][layerId].tracksterIdToEnergyAndScore) { - if (ts.second.first > maxEnergyLCinSC) { - maxEnergyLCinSC = ts.second.first; + if (ts.second.first > maxEnergyTSinSC) { + maxEnergyTSinSC = ts.second.first; tsWithMaxEnergyInSC = ts.first; } } if (SCenergy > 0.f) - SCEnergyFractionInLC = maxEnergyLCinSC / SCenergy; + SCEnergyFractionInTS = maxEnergyTSinSC / SCenergy; LogDebug("TracksterAssociatorByEnergyScoreImpl") << std::setw(8) << "LayerId:\t" << std::setw(12) << "simcluster\t" << std::setw(15) << "sc total energy\t" << std::setw(15) << "scEnergyOnLayer\t" << std::setw(14) << "SCNhitsOnLayer\t" << std::setw(18) - << "tsWithMaxEnergyInSC\t" << std::setw(15) << "maxEnergyLCinSC\t" << std::setw(20) << "SCEnergyFractionInLC" + << "tsWithMaxEnergyInSC\t" << std::setw(15) << "maxEnergyLCinTS\t" << std::setw(20) << "SCEnergyFractionInTS" << "\n"; LogDebug("TracksterAssociatorByEnergyScoreImpl") << std::setw(8) << layerId << "\t" << std::setw(12) << scId << "\t" << std::setw(15) << simClusters[scId].energy() << "\t" << std::setw(15) << SCenergy << "\t" << std::setw(14) << SCNumberOfHits - << "\t" << std::setw(18) << tsWithMaxEnergyInSC << "\t" << std::setw(15) << maxEnergyLCinSC << "\t" - << std::setw(20) << SCEnergyFractionInLC << "\n"; + << "\t" << std::setw(18) << tsWithMaxEnergyInSC << "\t" << std::setw(15) << maxEnergyLCinTS << "\t" + << std::setw(20) << SCEnergyFractionInTS << "\n"; #endif // Compute the correct normalization - float invSCEnergyWeight = 0.f; for (auto const& haf : tssInSimCluster[scId][layerId].hits_and_fractions) { invSCEnergyWeight += std::pow(haf.second * hitMap_->at(haf.first)->energy(), 2); } - invSCEnergyWeight = 1.f / invSCEnergyWeight; + } // End loop over layers + invSCEnergyWeight = 1.f / invSCEnergyWeight; + + for (unsigned int layerId = 0; layerId < layers_ * 2; ++layerId) { + const unsigned int SCNumberOfHits = tssInSimCluster[scId][layerId].hits_and_fractions.size(); + for (unsigned int i = 0; i < SCNumberOfHits; ++i) { auto& sc_hitDetId = tssInSimCluster[scId][layerId].hits_and_fractions[i].first; auto& scFraction = tssInSimCluster[scId][layerId].hits_and_fractions[i].second; bool hitWithLC = false; if (scFraction == 0.f) - continue; //hopefully this should never happen - auto hit_find_in_LC = detIdToLayerClusterId_Map.find(sc_hitDetId); + continue; // hopefully this should never happen + const auto hit_find_in_LC = detIdToLayerClusterId_Map.find(sc_hitDetId); if (hit_find_in_LC != detIdToLayerClusterId_Map.end()) hitWithLC = true; - auto itcheck = hitMap_->find(sc_hitDetId); + const auto itcheck = hitMap_->find(sc_hitDetId); const HGCRecHit* hit = itcheck->second; float hitEnergyWeight = hit->energy() * hit->energy(); for (auto& tsPair : tssInSimCluster[scId][layerId].tracksterIdToEnergyAndScore) { @@ -456,9 +466,9 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( float tsFraction = 0.f; if (hitWithLC) { - auto findHitIt = std::find(detIdToLayerClusterId_Map[sc_hitDetId].begin(), - detIdToLayerClusterId_Map[sc_hitDetId].end(), - hgcal::detIdInfoInCluster{tracksterId, 0.f}); + const auto findHitIt = std::find(detIdToLayerClusterId_Map[sc_hitDetId].begin(), + detIdToLayerClusterId_Map[sc_hitDetId].end(), + hgcal::detIdInfoInCluster{tracksterId, 0.f}); if (findHitIt != detIdToLayerClusterId_Map[sc_hitDetId].end()) tsFraction = findHitIt->fraction; } @@ -466,8 +476,8 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( (tsFraction - scFraction) * (tsFraction - scFraction) * hitEnergyWeight * invSCEnergyWeight; #ifdef EDM_ML_DEBUG LogDebug("TracksterAssociatorByEnergyScoreImpl") - << "scDetId:\t" << (uint32_t)sc_hitDetId << "\ttracksterId:\t" << tracksterId << "\t" - << "tsFraction,scFraction:\t" << tsFraction << ", " << scFraction << "\t" + << "SCDetId:\t" << (uint32_t)sc_hitDetId << "\tTracksterId:\t" << tracksterId << "\t" + << "tsFraction, scFraction:\t" << tsFraction << ", " << scFraction << "\t" << "hitEnergyWeight:\t" << hitEnergyWeight << "\t" << "current score:\t" << tsPair.second.second << "\t" << "invSCEnergyWeight:\t" << invSCEnergyWeight << "\n"; @@ -487,8 +497,8 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( << (tsPair.second.first / SCenergy) << "\n"; } #endif - } - } + } // End loop over layers + } // End loop over SimCluster indices return {scsInTrackster, tssInSimCluster}; } @@ -501,7 +511,7 @@ hgcal::RecoToSimCollectionTracksters TracksterAssociatorByEnergyScoreImpl::assoc for (size_t tsId = 0; tsId < scsInTrackster.size(); ++tsId) { for (auto& scPair : scsInTrackster[tsId]) { LogDebug("TracksterAssociatorByEnergyScoreImpl") - << "trackster Id: \t" << tsId << "\t SC id: \t" << scPair.first << "\t score \t" << scPair.second << "\n"; + << "Trackster Id: \t" << tsId << "\t SimCluster id: \t" << scPair.first << "\t score \t" << scPair.second << "\n"; // Fill AssociationMap returnValue.insert(edm::Ref(tCH, tsId), // Ref to TS std::make_pair(edm::Ref(sCCH, scPair.first), From 5050e8f6760e6828fa2fd3f47602ba8d90c85b20 Mon Sep 17 00:00:00 2001 From: Leonardo Cristella Date: Sat, 3 Apr 2021 13:49:09 +0200 Subject: [PATCH 3/8] Final details --- .../plugins/TracksterAssociatorByEnergyScoreImpl.h | 7 ------- .../interface/TracksterToSimClusterAssociatorBaseImpl.h | 4 ++-- .../Associations/plugins/TSToSCAssociatorEDProducer.cc | 6 ++---- 3 files changed, 4 insertions(+), 13 deletions(-) diff --git a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.h b/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.h index d67b5b672ac20..80432b2628007 100644 --- a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.h +++ b/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.h @@ -25,13 +25,6 @@ namespace hgcal { } }; - struct detIdInfoInMultiCluster { - bool operator==(const detIdInfoInMultiCluster &o) const { return multiclusterId == o.multiclusterId; }; - unsigned int multiclusterId; - long unsigned int clusterId; - float fraction; - }; - struct simClusterOnLayer { unsigned int simClusterId; float energy = 0; diff --git a/SimDataFormats/Associations/interface/TracksterToSimClusterAssociatorBaseImpl.h b/SimDataFormats/Associations/interface/TracksterToSimClusterAssociatorBaseImpl.h index 4d88465ce0b8d..138cde9318da4 100644 --- a/SimDataFormats/Associations/interface/TracksterToSimClusterAssociatorBaseImpl.h +++ b/SimDataFormats/Associations/interface/TracksterToSimClusterAssociatorBaseImpl.h @@ -3,8 +3,8 @@ /** \class TracksterToSimClusterAssociatorBaseImpl * - * Base class for TracksterToSimClusterAssociators. Methods take as input - * the handle of Tracksters and the SimCluster collections and return an + * Base class for TracksterToSimClusterAssociator. Methods take as input + * the handles of Tracksters, LayerClusters and SimClusters collections and return an * AssociationMap (oneToManyWithQuality) * * \author Leonardo Cristella diff --git a/SimDataFormats/Associations/plugins/TSToSCAssociatorEDProducer.cc b/SimDataFormats/Associations/plugins/TSToSCAssociatorEDProducer.cc index 1956bd26545b7..994d59d60b324 100644 --- a/SimDataFormats/Associations/plugins/TSToSCAssociatorEDProducer.cc +++ b/SimDataFormats/Associations/plugins/TSToSCAssociatorEDProducer.cc @@ -77,12 +77,10 @@ void TSToSCAssociatorEDProducer::produce(edm::StreamID, edm::Event &iEvent, cons iEvent.getByToken(LCCollectionToken_, LCCollection); // associate LC and SC - LogTrace("AssociatorValidator") << "Calling associateRecoToSim method" - << "\n"; + LogTrace("AssociatorValidator") << "Calling associateRecoToSim method\n"; hgcal::RecoToSimCollectionTracksters recSimColl = theAssociator->associateRecoToSim(TSCollection, LCCollection, SCCollection); - LogTrace("AssociatorValidator") << "Calling associateSimToReco method" - << "\n"; + LogTrace("AssociatorValidator") << "Calling associateSimToReco method\n"; hgcal::SimToRecoCollectionTracksters simRecColl = theAssociator->associateSimToReco(TSCollection, LCCollection, SCCollection); auto rts = std::make_unique(recSimColl); From 02ba5f272582b31343981f1c9f8f4a18cece0fd7 Mon Sep 17 00:00:00 2001 From: Leonardo Cristella Date: Sun, 4 Apr 2021 21:24:28 +0200 Subject: [PATCH 4/8] Remove layer granularity --- .../TracksterAssociatorByEnergyScoreImpl.cc | 267 ++++++++---------- .../TracksterAssociatorByEnergyScoreImpl.h | 2 +- SimDataFormats/Associations/src/classes.h | 4 + .../Associations/src/classes_def.xml | 16 ++ 4 files changed, 141 insertions(+), 148 deletions(-) diff --git a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.cc b/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.cc index 2af7aed9811c8..8cddae8658f68 100644 --- a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.cc +++ b/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.cc @@ -38,16 +38,13 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( // Initialize tssInSimCluster. To be returned outside, since it contains the // information to compute the SimCluster-To-Trackster score. - // tssInSimCluster[scId][layerId]: + // tssInSimCluster[scId]: hgcal::simClusterToTrackster tssInSimCluster; tssInSimCluster.resize(nSimClusters); for (unsigned int i = 0; i < nSimClusters; ++i) { - tssInSimCluster[i].resize(layers_ * 2); - for (unsigned int j = 0; j < layers_ * 2; ++j) { - tssInSimCluster[i][j].simClusterId = i; - tssInSimCluster[i][j].energy = 0.f; - tssInSimCluster[i][j].hits_and_fractions.clear(); - } + tssInSimCluster[i].simClusterId = i; + tssInSimCluster[i].energy = 0.f; + tssInSimCluster[i].hits_and_fractions.clear(); } // Fill detIdToSimClusterId_Map and update tssInSimCluster @@ -56,9 +53,6 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( const auto& hits_and_fractions = simClusters[scId].hits_and_fractions(); for (const auto& it_haf : hits_and_fractions) { const auto hitid = it_haf.first; - const auto scLayerId = - recHitTools_->getLayerWithOffset(hitid) + layers_ * ((recHitTools_->zside(hitid) + 1) >> 1) - 1; - const auto itcheck = hitMap_->find(hitid); if (itcheck != hitMap_->end()) { const auto hit_find_it = detIdToSimClusterId_Map.find(hitid); @@ -68,8 +62,8 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( detIdToSimClusterId_Map[hitid].emplace_back(scId, it_haf.second); const HGCRecHit* hit = itcheck->second; - tssInSimCluster[scId][scLayerId].energy += it_haf.second * hit->energy(); - tssInSimCluster[scId][scLayerId].hits_and_fractions.emplace_back(hitid, it_haf.second); + tssInSimCluster[scId].energy += it_haf.second * hit->energy(); + tssInSimCluster[scId].hits_and_fractions.emplace_back(hitid, it_haf.second); } } } // end of loop over SimClusters @@ -79,26 +73,23 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( << "tssInSimCluster INFO (Only SimCluster filled at the moment)" << std::endl; for (size_t sc = 0; sc < tssInSimCluster.size(); ++sc) { LogDebug("TracksterAssociatorByEnergyScoreImpl") << "For SimCluster Idx: " << sc << " we have: " << std::endl; - for (size_t sclay = 0; sclay < tssInSimCluster[sc].size(); ++sclay) { - LogDebug("TracksterAssociatorByEnergyScoreImpl") << " On Layer: " << sclay << " we have:" << std::endl; - LogDebug("TracksterAssociatorByEnergyScoreImpl") - << " SimClusterIdx: " << tssInSimCluster[sc][sclay].simClusterId << std::endl; - LogDebug("TracksterAssociatorByEnergyScoreImpl") - << " Energy: " << tssInSimCluster[sc][sclay].energy << std::endl; + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << " SimClusterIdx: " << tssInSimCluster[sc].simClusterId << std::endl; + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << " Energy: " << tssInSimCluster[sc].energy << std::endl; + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << " # of clusters : " << nLayerClusters << std::endl; + double tot_energy = 0.; + for (auto const& haf : tssInSimCluster[sc].hits_and_fractions) { LogDebug("TracksterAssociatorByEnergyScoreImpl") - << " # of clusters : " << nLayerClusters << std::endl; - double tot_energy = 0.; - for (auto const& haf : tssInSimCluster[sc][sclay].hits_and_fractions) { - LogDebug("TracksterAssociatorByEnergyScoreImpl") - << " Hits/fraction/energy: " << (uint32_t)haf.first << "/" << haf.second << "/" - << haf.second * hitMap_->at(haf.first)->energy() << std::endl; - tot_energy += haf.second * hitMap_->at(haf.first)->energy(); - } - LogDebug("TracksterAssociatorByEnergyScoreImpl") << " Tot Sum haf: " << tot_energy << std::endl; - for (auto const& ts : tssInSimCluster[sc][sclay].tracksterIdToEnergyAndScore) { - LogDebug("TracksterAssociatorByEnergyScoreImpl") << " tsIdx/energy/score: " << ts.first << "/" - << ts.second.first << "/" << ts.second.second << std::endl; - } + << " Hits/fraction/energy: " << (uint32_t)haf.first << "/" << haf.second << "/" + << haf.second * hitMap_->at(haf.first)->energy() << std::endl; + tot_energy += haf.second * hitMap_->at(haf.first)->energy(); + } + LogDebug("TracksterAssociatorByEnergyScoreImpl") << " Tot Sum haf: " << tot_energy << std::endl; + for (auto const& ts : tssInSimCluster[sc].tracksterIdToEnergyAndScore) { + LogDebug("TracksterAssociatorByEnergyScoreImpl") << " tsIdx/energy/score: " << ts.first << "/" + << ts.second.first << "/" << ts.second.second << std::endl; } } @@ -127,9 +118,6 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( for (const auto& lcId : tracksters[tsId].vertices()) { const std::vector>& hits_and_fractions = layerClusters[lcId].hitsAndFractions(); unsigned int numberOfHitsInLC = hits_and_fractions.size(); - const auto firstHitDetId = hits_and_fractions[0].first; - int lcLayerId = - recHitTools_->getLayerWithOffset(firstHitDetId) + layers_ * ((recHitTools_->zside(firstHitDetId) + 1) >> 1) - 1; for (unsigned int hitId = 0; hitId < numberOfHitsInLC; hitId++) { const auto rh_detid = hits_and_fractions[hitId].first; @@ -150,9 +138,9 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( //Here is time to update the tssInSimCluster and connect the SimCluster with all //the Tracksters that have the current rechit detid matched. for (const auto& h : hit_find_in_SC->second) { - //tssInSimCluster[simclusterId][layerId][layerclusterId]-> (energy,score) + //tssInSimCluster[simclusterId][layerclusterId]-> (energy,score) //SC_i - > TS_j, TS_k, ... - tssInSimCluster[h.clusterId][lcLayerId].tracksterIdToEnergyAndScore[tsId].first += + tssInSimCluster[h.clusterId].tracksterIdToEnergyAndScore[tsId].first += h.fraction * hit->energy(); //TS_i -> SC_j, SC_k, ... scsInTrackster[tsId].emplace_back(h.clusterId, 0.f); @@ -167,9 +155,6 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( for (const auto& lcId : tracksters[tsId].vertices()) { const auto& hits_and_fractions = layerClusters[lcId].hitsAndFractions(); unsigned int numberOfHitsInLC = hits_and_fractions.size(); - const auto firstHitDetId = hits_and_fractions[0].first; - int lcLayerId = - recHitTools_->getLayerWithOffset(firstHitDetId) + layers_ * ((recHitTools_->zside(firstHitDetId) + 1) >> 1) - 1; // This vector will store, for each hit in the Layercluster, the index of // the SimCluster that contributed the most, in terms of energy, to it. @@ -257,7 +242,7 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( float totalSCEnergyOnLayer = 0.f; if (maxSCId_byEnergy >= 0) { - totalSCEnergyOnLayer = tssInSimCluster[maxSCId_byEnergy][lcLayerId].energy; + totalSCEnergyOnLayer = tssInSimCluster[maxSCId_byEnergy].energy; energyFractionOfSCinLC = maxEnergySharedLCandSC / totalSCEnergyOnLayer; if (tracksters[tsId].raw_energy() > 0.f) { energyFractionOfLCinSC = maxEnergySharedLCandSC / tracksters[tsId].raw_energy(); @@ -265,7 +250,6 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( } LogDebug("TracksterAssociatorByEnergyScoreImpl") << std::setw(12) << "TracksterID:" - << "\t" << std::setw(10) << "LayerId:" << "\t" << std::setw(12) << "layerCluster" << "\t" << std::setw(10) << "lc energy" << "\t" << std::setw(5) << "nhits" @@ -279,7 +263,7 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( << "\t" << std::setw(25) << "energyFractionOfSCinLC" << "\t" << "\n"; LogDebug("TracksterAssociatorByEnergyScoreImpl") - << std::setw(12) << tsID << "\t" << std::setw(10) << lcLayerId << "\t" << std::setw(12) << lcId << "\t" << std::setw(10) + << std::setw(12) << tsId << "\t" << std::setw(12) << lcId << "\t" << std::setw(10) << tracksters[tsId].raw_energy() << "\t" << std::setw(5) << numberOfHitsInLC << "\t" << std::setw(12) << numberOfNoiseHitsInLC << "\t" << std::setw(22) << maxSCId_byNumberOfHits << "\t" << std::setw(8) << maxSCNumberOfHitsInLC << "\t" << std::setw(13) << maxSCId_byEnergy << "\t" << std::setw(20) @@ -293,24 +277,21 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( << std::endl; for (size_t sc = 0; sc < tssInSimCluster.size(); ++sc) { LogDebug("TracksterAssociatorByEnergyScoreImpl") << "For SimCluster Idx: " << sc << " we have: " << std::endl; - for (size_t sclay = 0; sclay < tssInSimCluster[sc].size(); ++sclay) { - LogDebug("TracksterAssociatorByEnergyScoreImpl") << " On Layer: " << sclay << " we have:" << std::endl; - LogDebug("TracksterAssociatorByEnergyScoreImpl") - << " SimClusterIdx: " << tssInSimCluster[sc][sclay].simClusterId << std::endl; + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << " SimClusterIdx: " << tssInSimCluster[sc].simClusterId << std::endl; + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << " Energy: " << tssInSimCluster[sc].energy << std::endl; + double tot_energy = 0.; + for (auto const& haf : tssInSimCluster[sc].hits_and_fractions) { LogDebug("TracksterAssociatorByEnergyScoreImpl") - << " Energy: " << tssInSimCluster[sc][sclay].energy << std::endl; - double tot_energy = 0.; - for (auto const& haf : tssInSimCluster[sc][sclay].hits_and_fractions) { - LogDebug("TracksterAssociatorByEnergyScoreImpl") - << " Hits/fraction/energy: " << (uint32_t)haf.first << "/" << haf.second << "/" - << haf.second * hitMap_->at(haf.first)->energy() << std::endl; - tot_energy += haf.second * hitMap_->at(haf.first)->energy(); - } - LogDebug("TracksterAssociatorByEnergyScoreImpl") << " Tot Sum haf: " << tot_energy << std::endl; - for (auto const& ts : tssInSimCluster[sc][sclay].tracksterIdToEnergyAndScore) { - LogDebug("TracksterAssociatorByEnergyScoreImpl") << " tsIdx/energy/score: " << ts.first << "/" - << ts.second.first << "/" << ts.second.second << std::endl; - } + << " Hits/fraction/energy: " << (uint32_t)haf.first << "/" << haf.second << "/" + << haf.second * hitMap_->at(haf.first)->energy() << std::endl; + tot_energy += haf.second * hitMap_->at(haf.first)->energy(); + } + LogDebug("TracksterAssociatorByEnergyScoreImpl") << " Tot Sum haf: " << tot_energy << std::endl; + for (auto const& ts : tssInSimCluster[sc].tracksterIdToEnergyAndScore) { + LogDebug("TracksterAssociatorByEnergyScoreImpl") << " tsIdx/energy/score: " << ts.first << "/" + << ts.second.first << "/" << ts.second.second << std::endl; } } @@ -407,97 +388,91 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( for (const auto& scId : sCIndices) { float invSCEnergyWeight = 0.f; - for (unsigned int layerId = 0; layerId < layers_ * 2; ++layerId) { - const unsigned int SCNumberOfHits = tssInSimCluster[scId][layerId].hits_and_fractions.size(); - if (SCNumberOfHits == 0) - continue; + const unsigned int SCNumberOfHits = tssInSimCluster[scId].hits_and_fractions.size(); + if (SCNumberOfHits == 0) + continue; #ifdef EDM_ML_DEBUG - int tsWithMaxEnergyInSC = -1; - //energy of the most energetic TS from all that were linked to SC - float maxEnergyTSinSC = 0.f; - float SCenergy = tssInSimCluster[scId][layerId].energy; - //most energetic TS from all TSs linked to SC over SC energy. - float SCEnergyFractionInTS = 0.f; - for (auto& ts : tssInSimCluster[scId][layerId].tracksterIdToEnergyAndScore) { - if (ts.second.first > maxEnergyTSinSC) { - maxEnergyTSinSC = ts.second.first; - tsWithMaxEnergyInSC = ts.first; - } + int tsWithMaxEnergyInSC = -1; + //energy of the most energetic TS from all that were linked to SC + float maxEnergyTSinSC = 0.f; + float SCenergy = tssInSimCluster[scId].energy; + //most energetic TS from all TSs linked to SC over SC energy. + float SCEnergyFractionInTS = 0.f; + for (const auto& ts : tssInSimCluster[scId].tracksterIdToEnergyAndScore) { + if (ts.second.first > maxEnergyTSinSC) { + maxEnergyTSinSC = ts.second.first; + tsWithMaxEnergyInSC = ts.first; } - if (SCenergy > 0.f) - SCEnergyFractionInTS = maxEnergyTSinSC / SCenergy; + } + if (SCenergy > 0.f) + SCEnergyFractionInTS = maxEnergyTSinSC / SCenergy; - LogDebug("TracksterAssociatorByEnergyScoreImpl") - << std::setw(8) << "LayerId:\t" << std::setw(12) << "simcluster\t" << std::setw(15) << "sc total energy\t" - << std::setw(15) << "scEnergyOnLayer\t" << std::setw(14) << "SCNhitsOnLayer\t" << std::setw(18) - << "tsWithMaxEnergyInSC\t" << std::setw(15) << "maxEnergyLCinTS\t" << std::setw(20) << "SCEnergyFractionInTS" - << "\n"; - LogDebug("TracksterAssociatorByEnergyScoreImpl") - << std::setw(8) << layerId << "\t" << std::setw(12) << scId << "\t" << std::setw(15) - << simClusters[scId].energy() << "\t" << std::setw(15) << SCenergy << "\t" << std::setw(14) << SCNumberOfHits - << "\t" << std::setw(18) << tsWithMaxEnergyInSC << "\t" << std::setw(15) << maxEnergyLCinTS << "\t" - << std::setw(20) << SCEnergyFractionInTS << "\n"; + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << std::setw(12) << "simcluster\t" << std::setw(15) << "sc total energy\t" + << std::setw(15) << "scEnergyOnLayer\t" << std::setw(14) << "SCNhitsOnLayer\t" << std::setw(18) + << "tsWithMaxEnergyInSC\t" << std::setw(15) << "maxEnergyTSinSC\t" << std::setw(20) << "SCEnergyFractionInTS" + << "\n"; + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << std::setw(12) << scId << "\t" << std::setw(15) + << simClusters[scId].energy() << "\t" << std::setw(15) << SCenergy << "\t" << std::setw(14) << SCNumberOfHits + << "\t" << std::setw(18) << tsWithMaxEnergyInSC << "\t" << std::setw(15) << maxEnergyTSinSC << "\t" + << std::setw(20) << SCEnergyFractionInTS << "\n"; #endif - // Compute the correct normalization - for (auto const& haf : tssInSimCluster[scId][layerId].hits_and_fractions) { - invSCEnergyWeight += std::pow(haf.second * hitMap_->at(haf.first)->energy(), 2); - } - } // End loop over layers + // Compute the correct normalization + for (auto const& haf : tssInSimCluster[scId].hits_and_fractions) { + invSCEnergyWeight += std::pow(haf.second * hitMap_->at(haf.first)->energy(), 2); + } invSCEnergyWeight = 1.f / invSCEnergyWeight; - for (unsigned int layerId = 0; layerId < layers_ * 2; ++layerId) { - const unsigned int SCNumberOfHits = tssInSimCluster[scId][layerId].hits_and_fractions.size(); - - for (unsigned int i = 0; i < SCNumberOfHits; ++i) { - auto& sc_hitDetId = tssInSimCluster[scId][layerId].hits_and_fractions[i].first; - auto& scFraction = tssInSimCluster[scId][layerId].hits_and_fractions[i].second; - - bool hitWithLC = false; - if (scFraction == 0.f) - continue; // hopefully this should never happen - const auto hit_find_in_LC = detIdToLayerClusterId_Map.find(sc_hitDetId); - if (hit_find_in_LC != detIdToLayerClusterId_Map.end()) - hitWithLC = true; - const auto itcheck = hitMap_->find(sc_hitDetId); - const HGCRecHit* hit = itcheck->second; - float hitEnergyWeight = hit->energy() * hit->energy(); - for (auto& tsPair : tssInSimCluster[scId][layerId].tracksterIdToEnergyAndScore) { - unsigned int tracksterId = tsPair.first; - float tsFraction = 0.f; - - if (hitWithLC) { - const auto findHitIt = std::find(detIdToLayerClusterId_Map[sc_hitDetId].begin(), - detIdToLayerClusterId_Map[sc_hitDetId].end(), - hgcal::detIdInfoInCluster{tracksterId, 0.f}); - if (findHitIt != detIdToLayerClusterId_Map[sc_hitDetId].end()) - tsFraction = findHitIt->fraction; - } - tsPair.second.second += - (tsFraction - scFraction) * (tsFraction - scFraction) * hitEnergyWeight * invSCEnergyWeight; + for (unsigned int i = 0; i < SCNumberOfHits; ++i) { + auto& sc_hitDetId = tssInSimCluster[scId].hits_and_fractions[i].first; + auto& scFraction = tssInSimCluster[scId].hits_and_fractions[i].second; + + bool hitWithLC = false; + if (scFraction == 0.f) + continue; // hopefully this should never happen + const auto hit_find_in_LC = detIdToLayerClusterId_Map.find(sc_hitDetId); + if (hit_find_in_LC != detIdToLayerClusterId_Map.end()) + hitWithLC = true; + const auto itcheck = hitMap_->find(sc_hitDetId); + const HGCRecHit* hit = itcheck->second; + float hitEnergyWeight = hit->energy() * hit->energy(); + for (auto& tsPair : tssInSimCluster[scId].tracksterIdToEnergyAndScore) { + unsigned int tracksterId = tsPair.first; + float tsFraction = 0.f; + + if (hitWithLC) { + const auto findHitIt = std::find(detIdToLayerClusterId_Map[sc_hitDetId].begin(), + detIdToLayerClusterId_Map[sc_hitDetId].end(), + hgcal::detIdInfoInCluster{tracksterId, 0.f}); + if (findHitIt != detIdToLayerClusterId_Map[sc_hitDetId].end()) + tsFraction = findHitIt->fraction; + } + tsPair.second.second += + (tsFraction - scFraction) * (tsFraction - scFraction) * hitEnergyWeight * invSCEnergyWeight; #ifdef EDM_ML_DEBUG - LogDebug("TracksterAssociatorByEnergyScoreImpl") - << "SCDetId:\t" << (uint32_t)sc_hitDetId << "\tTracksterId:\t" << tracksterId << "\t" - << "tsFraction, scFraction:\t" << tsFraction << ", " << scFraction << "\t" - << "hitEnergyWeight:\t" << hitEnergyWeight << "\t" - << "current score:\t" << tsPair.second.second << "\t" - << "invSCEnergyWeight:\t" << invSCEnergyWeight << "\n"; + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << "SCDetId:\t" << (uint32_t)sc_hitDetId << "\tTracksterId:\t" << tracksterId << "\t" + << "tsFraction, scFraction:\t" << tsFraction << ", " << scFraction << "\t" + << "hitEnergyWeight:\t" << hitEnergyWeight << "\t" + << "current score:\t" << tsPair.second.second << "\t" + << "invSCEnergyWeight:\t" << invSCEnergyWeight << "\n"; #endif - } // End of loop over LayerClusters linked to hits of this SimCluster - } // End of loop over hits of SimCluster on a Layer + } // End of loop over LayerClusters linked to hits of this SimCluster + } // End of loop over hits of SimCluster on a Layer #ifdef EDM_ML_DEBUG - if (tssInSimCluster[scId][layerId].tracksterIdToEnergyAndScore.empty()) - LogDebug("TracksterAssociatorByEnergyScoreImpl") << "SC Id: \t" << scId << "\tTS id:\t-1 " - << "\t score \t-1" - << "\n"; + if (tssInSimCluster[scId].tracksterIdToEnergyAndScore.empty()) + LogDebug("TracksterAssociatorByEnergyScoreImpl") << "SC Id: \t" << scId << "\tTS id:\t-1 " + << "\t score \t-1" + << "\n"; - for (const auto& tsPair : tssInSimCluster[scId][layerId].tracksterIdToEnergyAndScore) { - LogDebug("TracksterAssociatorByEnergyScoreImpl") - << "SC Id: \t" << scId << "\t TS id: \t" << tsPair.first << "\t score \t" << tsPair.second.second - << "\t shared energy:\t" << tsPair.second.first << "\t shared energy fraction:\t" - << (tsPair.second.first / SCenergy) << "\n"; - } + for (const auto& tsPair : tssInSimCluster[scId].tracksterIdToEnergyAndScore) { + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << "SC Id: \t" << scId << "\t TS id: \t" << tsPair.first << "\t score \t" << tsPair.second.second + << "\t shared energy:\t" << tsPair.second.first << "\t shared energy fraction:\t" + << (tsPair.second.first / SCenergy) << "\n"; + } #endif - } // End loop over layers } // End loop over SimCluster indices return {scsInTrackster, tssInSimCluster}; } @@ -528,14 +503,12 @@ hgcal::SimToRecoCollectionTracksters TracksterAssociatorByEnergyScoreImpl::assoc const auto& links = makeConnections(tCH, lCCH, sCCH); const auto& tssInSimCluster = std::get<1>(links); for (size_t scId = 0; scId < tssInSimCluster.size(); ++scId) { - for (size_t layerId = 0; layerId < tssInSimCluster[scId].size(); ++layerId) { - for (auto& tsPair : tssInSimCluster[scId][layerId].tracksterIdToEnergyAndScore) { - returnValue.insert( - edm::Ref(sCCH, scId), // Ref to SC - std::make_pair(edm::Ref(tCH, tsPair.first), // Pair > - ); - } + for (auto& tsPair : tssInSimCluster[scId].tracksterIdToEnergyAndScore) { + returnValue.insert( + edm::Ref(sCCH, scId), // Ref to SC + std::make_pair(edm::Ref(tCH, tsPair.first), // Pair > + ); } } return returnValue; diff --git a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.h b/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.h index 80432b2628007..fee523ffb65a7 100644 --- a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.h +++ b/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.h @@ -33,7 +33,7 @@ namespace hgcal { }; typedef std::vector>> tracksterToSimCluster; - typedef std::vector> simClusterToTrackster; + typedef std::vector simClusterToTrackster; typedef std::tuple association; } // namespace hgcal diff --git a/SimDataFormats/Associations/src/classes.h b/SimDataFormats/Associations/src/classes.h index 6e12db392a0a9..44f3be3a5a8f5 100644 --- a/SimDataFormats/Associations/src/classes.h +++ b/SimDataFormats/Associations/src/classes.h @@ -8,6 +8,8 @@ #include "SimDataFormats/Associations/interface/VertexToTrackingVertexAssociator.h" #include "SimDataFormats/Associations/interface/LayerClusterToCaloParticleAssociator.h" #include "SimDataFormats/Associations/interface/LayerClusterToSimClusterAssociator.h" +#include "SimDataFormats/Associations/interface/TracksterToSimClusterAssociator.h" +#include "SimDataFormats/Associations/interface/TTTrackTruthPair.h" namespace SimDataFormats_Associations { struct SimDataFormats_Associations { @@ -22,6 +24,8 @@ namespace SimDataFormats_Associations { edm::Wrapper dummy6; + edm::Wrapper dummy7; + reco::VertexSimToRecoCollection vstrc; reco::VertexSimToRecoCollection::const_iterator vstrci; edm::Wrapper wvstrc; diff --git a/SimDataFormats/Associations/src/classes_def.xml b/SimDataFormats/Associations/src/classes_def.xml index 3c66509430337..92156ab082e1e 100644 --- a/SimDataFormats/Associations/src/classes_def.xml +++ b/SimDataFormats/Associations/src/classes_def.xml @@ -15,6 +15,9 @@ + + + @@ -47,6 +50,19 @@ + + + + + + + + + + + + + From 71559269a06f87521fa67d686718b079b63865d8 Mon Sep 17 00:00:00 2001 From: Leonardo Cristella Date: Mon, 5 Apr 2021 00:33:00 +0200 Subject: [PATCH 5/8] code-format --- .../TracksterAssociatorByEnergyScoreImpl.cc | 318 +++++++++--------- .../TracksterAssociatorByEnergyScoreImpl.h | 20 +- ...racksterAssociatorByEnergyScoreProducer.cc | 4 +- .../TracksterToSimClusterAssociator.h | 3 +- .../TracksterToSimClusterAssociatorBaseImpl.h | 8 +- .../plugins/TSToSCAssociatorEDProducer.cc | 9 +- ...TracksterToSimClusterAssociatorBaseImpl.cc | 8 +- 7 files changed, 190 insertions(+), 180 deletions(-) diff --git a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.cc b/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.cc index 8cddae8658f68..6a85fd21d40f8 100644 --- a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.cc +++ b/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.cc @@ -13,7 +13,9 @@ TracksterAssociatorByEnergyScoreImpl::TracksterAssociatorByEnergyScoreImpl( } hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( - const edm::Handle& tCH, const edm::Handle& lCCH, const edm::Handle& sCCH) const { + const edm::Handle& tCH, + const edm::Handle& lCCH, + const edm::Handle& sCCH) const { // Get collections const auto& tracksters = *tCH.product(); const auto& layerClusters = *lCCH.product(); @@ -77,8 +79,7 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( << " SimClusterIdx: " << tssInSimCluster[sc].simClusterId << std::endl; LogDebug("TracksterAssociatorByEnergyScoreImpl") << " Energy: " << tssInSimCluster[sc].energy << std::endl; - LogDebug("TracksterAssociatorByEnergyScoreImpl") - << " # of clusters : " << nLayerClusters << std::endl; + LogDebug("TracksterAssociatorByEnergyScoreImpl") << " # of clusters : " << nLayerClusters << std::endl; double tot_energy = 0.; for (auto const& haf : tssInSimCluster[sc].hits_and_fractions) { LogDebug("TracksterAssociatorByEnergyScoreImpl") @@ -88,8 +89,8 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( } LogDebug("TracksterAssociatorByEnergyScoreImpl") << " Tot Sum haf: " << tot_energy << std::endl; for (auto const& ts : tssInSimCluster[sc].tracksterIdToEnergyAndScore) { - LogDebug("TracksterAssociatorByEnergyScoreImpl") << " tsIdx/energy/score: " << ts.first << "/" - << ts.second.first << "/" << ts.second.second << std::endl; + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << " tsIdx/energy/score: " << ts.first << "/" << ts.second.first << "/" << ts.second.second << std::endl; } } @@ -120,35 +121,34 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( unsigned int numberOfHitsInLC = hits_and_fractions.size(); for (unsigned int hitId = 0; hitId < numberOfHitsInLC; hitId++) { - const auto rh_detid = hits_and_fractions[hitId].first; - const auto rhFraction = hits_and_fractions[hitId].second; - - const auto hit_find_in_LC = detIdToLayerClusterId_Map.find(rh_detid); - if (hit_find_in_LC == detIdToLayerClusterId_Map.end()) { - detIdToLayerClusterId_Map[rh_detid] = std::vector(); - } - detIdToLayerClusterId_Map[rh_detid].emplace_back(lcId, rhFraction); - - const auto hit_find_in_SC = detIdToSimClusterId_Map.find(rh_detid); - - if (hit_find_in_SC != detIdToSimClusterId_Map.end()) { - const auto itcheck = hitMap_->find(rh_detid); - const HGCRecHit* hit = itcheck->second; - //Loops through all the simclusters that have the layer cluster rechit under study - //Here is time to update the tssInSimCluster and connect the SimCluster with all - //the Tracksters that have the current rechit detid matched. - for (const auto& h : hit_find_in_SC->second) { - //tssInSimCluster[simclusterId][layerclusterId]-> (energy,score) - //SC_i - > TS_j, TS_k, ... - tssInSimCluster[h.clusterId].tracksterIdToEnergyAndScore[tsId].first += - h.fraction * hit->energy(); - //TS_i -> SC_j, SC_k, ... - scsInTrackster[tsId].emplace_back(h.clusterId, 0.f); - } - } - } // End loop over hits on a LayerCluster - } // End loop over LayerClusters in Trackster - } // End of loop over Tracksters + const auto rh_detid = hits_and_fractions[hitId].first; + const auto rhFraction = hits_and_fractions[hitId].second; + + const auto hit_find_in_LC = detIdToLayerClusterId_Map.find(rh_detid); + if (hit_find_in_LC == detIdToLayerClusterId_Map.end()) { + detIdToLayerClusterId_Map[rh_detid] = std::vector(); + } + detIdToLayerClusterId_Map[rh_detid].emplace_back(lcId, rhFraction); + + const auto hit_find_in_SC = detIdToSimClusterId_Map.find(rh_detid); + + if (hit_find_in_SC != detIdToSimClusterId_Map.end()) { + const auto itcheck = hitMap_->find(rh_detid); + const HGCRecHit* hit = itcheck->second; + //Loops through all the simclusters that have the layer cluster rechit under study + //Here is time to update the tssInSimCluster and connect the SimCluster with all + //the Tracksters that have the current rechit detid matched. + for (const auto& h : hit_find_in_SC->second) { + //tssInSimCluster[simclusterId][layerclusterId]-> (energy,score) + //SC_i - > TS_j, TS_k, ... + tssInSimCluster[h.clusterId].tracksterIdToEnergyAndScore[tsId].first += h.fraction * hit->energy(); + //TS_i -> SC_j, SC_k, ... + scsInTrackster[tsId].emplace_back(h.clusterId, 0.f); + } + } + } // End loop over hits on a LayerCluster + } // End loop over LayerClusters in Trackster + } // End of loop over Tracksters #ifdef EDM_ML_DEBUG for (unsigned int tsId = 0; tsId < nTracksters; ++tsId) { @@ -182,95 +182,96 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( std::unordered_map SCEnergyInLC; for (unsigned int hitId = 0; hitId < numberOfHitsInLC; hitId++) { - const auto rh_detid = hits_and_fractions[hitId].first; - const auto rhFraction = hits_and_fractions[hitId].second; - - const auto hit_find_in_SC = detIdToSimClusterId_Map.find(rh_detid); - - // if the fraction is zero or the hit does not belong to any SimCluster, - // set the SimCluster Id for the hit to -1; this will - // contribute to the number of noise hits - - // MR Remove the case in which the fraction is 0, since this could be a - // real hit that has been marked as halo. - if (rhFraction == 0.) { - hitsToSimClusterId[hitId] = -2; - } - //Now check if there are SimClusters linked to this rechit of the layercluster - if (hit_find_in_SC == detIdToSimClusterId_Map.end()) { - hitsToSimClusterId[hitId] -= 1; - } else { - const auto itcheck = hitMap_->find(rh_detid); - const HGCRecHit* hit = itcheck->second; - auto maxSCEnergyInLC = 0.f; - auto maxSCId = -1; - //Loop through all the linked SimClusters - for (const auto& h : hit_find_in_SC->second) { - SCEnergyInLC[h.clusterId] += h.fraction * hit->energy(); - // Keep track of which SimCluster contributed the most, in terms - // of energy, to this specific Layer Cluster. - if (SCEnergyInLC[h.clusterId] > maxSCEnergyInLC) { - maxSCEnergyInLC = SCEnergyInLC[h.clusterId]; - maxSCId = h.clusterId; - } - } - hitsToSimClusterId[hitId] = maxSCId; - } + const auto rh_detid = hits_and_fractions[hitId].first; + const auto rhFraction = hits_and_fractions[hitId].second; + + const auto hit_find_in_SC = detIdToSimClusterId_Map.find(rh_detid); + + // if the fraction is zero or the hit does not belong to any SimCluster, + // set the SimCluster Id for the hit to -1; this will + // contribute to the number of noise hits + + // MR Remove the case in which the fraction is 0, since this could be a + // real hit that has been marked as halo. + if (rhFraction == 0.) { + hitsToSimClusterId[hitId] = -2; + } + //Now check if there are SimClusters linked to this rechit of the layercluster + if (hit_find_in_SC == detIdToSimClusterId_Map.end()) { + hitsToSimClusterId[hitId] -= 1; + } else { + const auto itcheck = hitMap_->find(rh_detid); + const HGCRecHit* hit = itcheck->second; + auto maxSCEnergyInLC = 0.f; + auto maxSCId = -1; + //Loop through all the linked SimClusters + for (const auto& h : hit_find_in_SC->second) { + SCEnergyInLC[h.clusterId] += h.fraction * hit->energy(); + // Keep track of which SimCluster contributed the most, in terms + // of energy, to this specific Layer Cluster. + if (SCEnergyInLC[h.clusterId] > maxSCEnergyInLC) { + maxSCEnergyInLC = SCEnergyInLC[h.clusterId]; + maxSCId = h.clusterId; + } + } + hitsToSimClusterId[hitId] = maxSCId; + } } // End loop over hits on a LayerCluster for (const auto& c : hitsToSimClusterId) { - if (c < 0) { - numberOfNoiseHitsInLC++; - } else { - occurrencesSCinLC[c]++; - } + if (c < 0) { + numberOfNoiseHitsInLC++; + } else { + occurrencesSCinLC[c]++; + } } for (const auto& c : occurrencesSCinLC) { - if (c.second > maxSCNumberOfHitsInLC) { - maxSCId_byNumberOfHits = c.first; - maxSCNumberOfHitsInLC = c.second; - } + if (c.second > maxSCNumberOfHitsInLC) { + maxSCId_byNumberOfHits = c.first; + maxSCNumberOfHitsInLC = c.second; + } } for (const auto& c : SCEnergyInLC) { - if (c.second > maxEnergySharedLCandSC) { - maxSCId_byEnergy = c.first; - maxEnergySharedLCandSC = c.second; - } + if (c.second > maxEnergySharedLCandSC) { + maxSCId_byEnergy = c.first; + maxEnergySharedLCandSC = c.second; + } } float totalSCEnergyOnLayer = 0.f; if (maxSCId_byEnergy >= 0) { - totalSCEnergyOnLayer = tssInSimCluster[maxSCId_byEnergy].energy; - energyFractionOfSCinLC = maxEnergySharedLCandSC / totalSCEnergyOnLayer; - if (tracksters[tsId].raw_energy() > 0.f) { - energyFractionOfLCinSC = maxEnergySharedLCandSC / tracksters[tsId].raw_energy(); - } + totalSCEnergyOnLayer = tssInSimCluster[maxSCId_byEnergy].energy; + energyFractionOfSCinLC = maxEnergySharedLCandSC / totalSCEnergyOnLayer; + if (tracksters[tsId].raw_energy() > 0.f) { + energyFractionOfLCinSC = maxEnergySharedLCandSC / tracksters[tsId].raw_energy(); + } } LogDebug("TracksterAssociatorByEnergyScoreImpl") << std::setw(12) << "TracksterID:" - << "\t" << std::setw(12) << "layerCluster" - << "\t" << std::setw(10) << "lc energy" - << "\t" << std::setw(5) << "nhits" - << "\t" << std::setw(12) << "noise hits" - << "\t" << std::setw(22) << "maxSCId_byNumberOfHits" - << "\t" << std::setw(8) << "nhitsSC" - << "\t" << std::setw(13) << "maxSCId_byEnergy" - << "\t" << std::setw(20) << "maxEnergySharedLCandSC" - << "\t" << std::setw(22) << "totalSCEnergyOnLayer" - << "\t" << std::setw(22) << "energyFractionOfLCinSC" - << "\t" << std::setw(25) << "energyFractionOfSCinLC" - << "\t" << "\n"; + << "\t" << std::setw(12) << "layerCluster" + << "\t" << std::setw(10) << "lc energy" + << "\t" << std::setw(5) << "nhits" + << "\t" << std::setw(12) << "noise hits" + << "\t" << std::setw(22) << "maxSCId_byNumberOfHits" + << "\t" << std::setw(8) << "nhitsSC" + << "\t" << std::setw(13) << "maxSCId_byEnergy" + << "\t" << std::setw(20) << "maxEnergySharedLCandSC" + << "\t" << std::setw(22) << "totalSCEnergyOnLayer" + << "\t" << std::setw(22) << "energyFractionOfLCinSC" + << "\t" << std::setw(25) << "energyFractionOfSCinLC" + << "\t" + << "\n"; LogDebug("TracksterAssociatorByEnergyScoreImpl") - << std::setw(12) << tsId << "\t" << std::setw(12) << lcId << "\t" << std::setw(10) - << tracksters[tsId].raw_energy() << "\t" << std::setw(5) << numberOfHitsInLC << "\t" << std::setw(12) - << numberOfNoiseHitsInLC << "\t" << std::setw(22) << maxSCId_byNumberOfHits << "\t" << std::setw(8) - << maxSCNumberOfHitsInLC << "\t" << std::setw(13) << maxSCId_byEnergy << "\t" << std::setw(20) - << maxEnergySharedLCandSC << "\t" << std::setw(22) << totalSCEnergyOnLayer << "\t" << std::setw(22) - << energyFractionOfLCinSC << "\t" << std::setw(25) << energyFractionOfSCinLC << "\n"; - } // End of loop over LayerClusters in Trackster - } // End of loop over Tracksters + << std::setw(12) << tsId << "\t" << std::setw(12) << lcId << "\t" << std::setw(10) + << tracksters[tsId].raw_energy() << "\t" << std::setw(5) << numberOfHitsInLC << "\t" << std::setw(12) + << numberOfNoiseHitsInLC << "\t" << std::setw(22) << maxSCId_byNumberOfHits << "\t" << std::setw(8) + << maxSCNumberOfHitsInLC << "\t" << std::setw(13) << maxSCId_byEnergy << "\t" << std::setw(20) + << maxEnergySharedLCandSC << "\t" << std::setw(22) << totalSCEnergyOnLayer << "\t" << std::setw(22) + << energyFractionOfLCinSC << "\t" << std::setw(25) << energyFractionOfSCinLC << "\n"; + } // End of loop over LayerClusters in Trackster + } // End of loop over Tracksters LogDebug("TracksterAssociatorByEnergyScoreImpl") << "Improved tssInSimCluster INFO (Now containing the linked tracksters id and energy - score still empty)" @@ -290,8 +291,8 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( } LogDebug("TracksterAssociatorByEnergyScoreImpl") << " Tot Sum haf: " << tot_energy << std::endl; for (auto const& ts : tssInSimCluster[sc].tracksterIdToEnergyAndScore) { - LogDebug("TracksterAssociatorByEnergyScoreImpl") << " tsIdx/energy/score: " << ts.first << "/" - << ts.second.first << "/" << ts.second.second << std::endl; + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << " tsIdx/energy/score: " << ts.first << "/" << ts.second.first << "/" << ts.second.second << std::endl; } } @@ -334,8 +335,8 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( // Compute the correct normalization for (auto const& haf : hits_and_fractions) { - invTracksterEnergyWeight += - (haf.second * hitMap_->at(haf.first)->energy()) * (haf.second * hitMap_->at(haf.first)->energy()); + invTracksterEnergyWeight += + (haf.second * hitMap_->at(haf.first)->energy()) * (haf.second * hitMap_->at(haf.first)->energy()); } } invTracksterEnergyWeight = 1.f / invTracksterEnergyWeight; @@ -344,44 +345,44 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( const auto& hits_and_fractions = layerClusters[lcId].hitsAndFractions(); unsigned int numberOfHitsInLC = hits_and_fractions.size(); for (unsigned int i = 0; i < numberOfHitsInLC; ++i) { - DetId rh_detid = hits_and_fractions[i].first; - float rhFraction = hits_and_fractions[i].second; - - const bool hitWithSC = (detIdToSimClusterId_Map.find(rh_detid) != detIdToSimClusterId_Map.end()); - - const auto itcheck = hitMap_->find(rh_detid); - const HGCRecHit* hit = itcheck->second; - float hitEnergyWeight = hit->energy() * hit->energy(); - - for (auto& scPair : scsInTrackster[tsId]) { - float scFraction = 0.f; - if (hitWithSC) { - const auto findHitIt = std::find(detIdToSimClusterId_Map[rh_detid].begin(), - detIdToSimClusterId_Map[rh_detid].end(), - hgcal::detIdInfoInCluster{scPair.first, 0.f}); - if (findHitIt != detIdToSimClusterId_Map[rh_detid].end()) - scFraction = findHitIt->fraction; - } - scPair.second += - (rhFraction - scFraction) * (rhFraction - scFraction) * hitEnergyWeight * invTracksterEnergyWeight; - #ifdef EDM_ML_DEBUG - LogDebug("TracksterAssociatorByEnergyScoreImpl") - << "rh_detid:\t" << (uint32_t)rh_detid << "\ttracksterId:\t" << tsId << "\t" - << "rhfraction,scFraction:\t" << rhFraction << ", " << scFraction << "\t" - << "hitEnergyWeight:\t" << hitEnergyWeight << "\t" - << "current score:\t" << scPair.second << "\t" - << "invTracksterEnergyWeight:\t" << invTracksterEnergyWeight << "\n"; - #endif - } + DetId rh_detid = hits_and_fractions[i].first; + float rhFraction = hits_and_fractions[i].second; + + const bool hitWithSC = (detIdToSimClusterId_Map.find(rh_detid) != detIdToSimClusterId_Map.end()); + + const auto itcheck = hitMap_->find(rh_detid); + const HGCRecHit* hit = itcheck->second; + float hitEnergyWeight = hit->energy() * hit->energy(); + + for (auto& scPair : scsInTrackster[tsId]) { + float scFraction = 0.f; + if (hitWithSC) { + const auto findHitIt = std::find(detIdToSimClusterId_Map[rh_detid].begin(), + detIdToSimClusterId_Map[rh_detid].end(), + hgcal::detIdInfoInCluster{scPair.first, 0.f}); + if (findHitIt != detIdToSimClusterId_Map[rh_detid].end()) + scFraction = findHitIt->fraction; + } + scPair.second += + (rhFraction - scFraction) * (rhFraction - scFraction) * hitEnergyWeight * invTracksterEnergyWeight; +#ifdef EDM_ML_DEBUG + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << "rh_detid:\t" << (uint32_t)rh_detid << "\ttracksterId:\t" << tsId << "\t" + << "rhfraction,scFraction:\t" << rhFraction << ", " << scFraction << "\t" + << "hitEnergyWeight:\t" << hitEnergyWeight << "\t" + << "current score:\t" << scPair.second << "\t" + << "invTracksterEnergyWeight:\t" << invTracksterEnergyWeight << "\n"; +#endif + } } // End of loop over Hits within a LayerCluster - } // End of loop over LayerClusters in Trackster + } // End of loop over LayerClusters in Trackster - #ifdef EDM_ML_DEBUG +#ifdef EDM_ML_DEBUG if (scsInTrackster[tsId].empty()) LogDebug("TracksterAssociatorByEnergyScoreImpl") << "trackster Id: \t" << tsId << "\tSC id:\t-1 " << "\t score \t-1" << "\n"; - #endif +#endif } // End of loop over Tracksters // Compute the SimCluster-To-Trackster score @@ -408,15 +409,14 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( SCEnergyFractionInTS = maxEnergyTSinSC / SCenergy; LogDebug("TracksterAssociatorByEnergyScoreImpl") - << std::setw(12) << "simcluster\t" << std::setw(15) << "sc total energy\t" - << std::setw(15) << "scEnergyOnLayer\t" << std::setw(14) << "SCNhitsOnLayer\t" << std::setw(18) - << "tsWithMaxEnergyInSC\t" << std::setw(15) << "maxEnergyTSinSC\t" << std::setw(20) << "SCEnergyFractionInTS" + << std::setw(12) << "simcluster\t" << std::setw(15) << "sc total energy\t" << std::setw(15) + << "scEnergyOnLayer\t" << std::setw(14) << "SCNhitsOnLayer\t" << std::setw(18) << "tsWithMaxEnergyInSC\t" + << std::setw(15) << "maxEnergyTSinSC\t" << std::setw(20) << "SCEnergyFractionInTS" << "\n"; LogDebug("TracksterAssociatorByEnergyScoreImpl") - << std::setw(12) << scId << "\t" << std::setw(15) - << simClusters[scId].energy() << "\t" << std::setw(15) << SCenergy << "\t" << std::setw(14) << SCNumberOfHits - << "\t" << std::setw(18) << tsWithMaxEnergyInSC << "\t" << std::setw(15) << maxEnergyTSinSC << "\t" - << std::setw(20) << SCEnergyFractionInTS << "\n"; + << std::setw(12) << scId << "\t" << std::setw(15) << simClusters[scId].energy() << "\t" << std::setw(15) + << SCenergy << "\t" << std::setw(14) << SCNumberOfHits << "\t" << std::setw(18) << tsWithMaxEnergyInSC << "\t" + << std::setw(15) << maxEnergyTSinSC << "\t" << std::setw(20) << SCEnergyFractionInTS << "\n"; #endif // Compute the correct normalization for (auto const& haf : tssInSimCluster[scId].hits_and_fractions) { @@ -430,7 +430,7 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( bool hitWithLC = false; if (scFraction == 0.f) - continue; // hopefully this should never happen + continue; // hopefully this should never happen const auto hit_find_in_LC = detIdToLayerClusterId_Map.find(sc_hitDetId); if (hit_find_in_LC != detIdToLayerClusterId_Map.end()) hitWithLC = true; @@ -473,20 +473,22 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( << (tsPair.second.first / SCenergy) << "\n"; } #endif - } // End loop over SimCluster indices + } // End loop over SimCluster indices return {scsInTrackster, tssInSimCluster}; } hgcal::RecoToSimCollectionTracksters TracksterAssociatorByEnergyScoreImpl::associateRecoToSim( - const edm::Handle& tCH, const edm::Handle& lCCH, const edm::Handle& sCCH) const { + const edm::Handle& tCH, + const edm::Handle& lCCH, + const edm::Handle& sCCH) const { hgcal::RecoToSimCollectionTracksters returnValue(productGetter_); const auto& links = makeConnections(tCH, lCCH, sCCH); const auto& scsInTrackster = std::get<0>(links); for (size_t tsId = 0; tsId < scsInTrackster.size(); ++tsId) { for (auto& scPair : scsInTrackster[tsId]) { - LogDebug("TracksterAssociatorByEnergyScoreImpl") - << "Trackster Id: \t" << tsId << "\t SimCluster id: \t" << scPair.first << "\t score \t" << scPair.second << "\n"; + LogDebug("TracksterAssociatorByEnergyScoreImpl") << "Trackster Id: \t" << tsId << "\t SimCluster id: \t" + << scPair.first << "\t score \t" << scPair.second << "\n"; // Fill AssociationMap returnValue.insert(edm::Ref(tCH, tsId), // Ref to TS std::make_pair(edm::Ref(sCCH, scPair.first), @@ -498,15 +500,17 @@ hgcal::RecoToSimCollectionTracksters TracksterAssociatorByEnergyScoreImpl::assoc } hgcal::SimToRecoCollectionTracksters TracksterAssociatorByEnergyScoreImpl::associateSimToReco( - const edm::Handle& tCH, const edm::Handle& lCCH, const edm::Handle& sCCH) const { + const edm::Handle& tCH, + const edm::Handle& lCCH, + const edm::Handle& sCCH) const { hgcal::SimToRecoCollectionTracksters returnValue(productGetter_); const auto& links = makeConnections(tCH, lCCH, sCCH); const auto& tssInSimCluster = std::get<1>(links); for (size_t scId = 0; scId < tssInSimCluster.size(); ++scId) { for (auto& tsPair : tssInSimCluster[scId].tracksterIdToEnergyAndScore) { returnValue.insert( - edm::Ref(sCCH, scId), // Ref to SC - std::make_pair(edm::Ref(tCH, tsPair.first), // Pair (sCCH, scId), // Ref to SC + std::make_pair(edm::Ref(tCH, tsPair.first), // Pair > ); } diff --git a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.h b/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.h index fee523ffb65a7..8c75d5af38797 100644 --- a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.h +++ b/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.h @@ -40,19 +40,17 @@ namespace hgcal { class TracksterAssociatorByEnergyScoreImpl : public hgcal::TracksterToSimClusterAssociatorBaseImpl { public: explicit TracksterAssociatorByEnergyScoreImpl(edm::EDProductGetter const &, - bool, - std::shared_ptr, - const std::unordered_map *); + bool, + std::shared_ptr, + const std::unordered_map *); - hgcal::RecoToSimCollectionTracksters associateRecoToSim( - const edm::Handle &tCH, - const edm::Handle &lCCH, - const edm::Handle &sCCH) const override; + hgcal::RecoToSimCollectionTracksters associateRecoToSim(const edm::Handle &tCH, + const edm::Handle &lCCH, + const edm::Handle &sCCH) const override; - hgcal::SimToRecoCollectionTracksters associateSimToReco( - const edm::Handle &tCH, - const edm::Handle &lCCH, - const edm::Handle &sCCH) const override; + hgcal::SimToRecoCollectionTracksters associateSimToReco(const edm::Handle &tCH, + const edm::Handle &lCCH, + const edm::Handle &sCCH) const override; private: const bool hardScatterOnly_; diff --git a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreProducer.cc b/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreProducer.cc index 769ae760e1afb..8bd086a89574d 100644 --- a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreProducer.cc +++ b/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreProducer.cc @@ -42,8 +42,8 @@ TracksterAssociatorByEnergyScoreProducer::TracksterAssociatorByEnergyScoreProduc TracksterAssociatorByEnergyScoreProducer::~TracksterAssociatorByEnergyScoreProducer() {} void TracksterAssociatorByEnergyScoreProducer::produce(edm::StreamID, - edm::Event &iEvent, - const edm::EventSetup &es) const { + edm::Event &iEvent, + const edm::EventSetup &es) const { edm::ESHandle geom = es.getHandle(caloGeometry_); rhtools_->setGeometry(*geom); diff --git a/SimDataFormats/Associations/interface/TracksterToSimClusterAssociator.h b/SimDataFormats/Associations/interface/TracksterToSimClusterAssociator.h index f2c8b388dd153..0855fb24caa52 100644 --- a/SimDataFormats/Associations/interface/TracksterToSimClusterAssociator.h +++ b/SimDataFormats/Associations/interface/TracksterToSimClusterAssociator.h @@ -39,8 +39,7 @@ namespace hgcal { private: TracksterToSimClusterAssociator(const TracksterToSimClusterAssociator &) = delete; // stop default - const TracksterToSimClusterAssociator &operator=(const TracksterToSimClusterAssociator &) = - delete; // stop default + const TracksterToSimClusterAssociator &operator=(const TracksterToSimClusterAssociator &) = delete; // stop default // ---------- member data -------------------------------- std::unique_ptr m_impl; diff --git a/SimDataFormats/Associations/interface/TracksterToSimClusterAssociatorBaseImpl.h b/SimDataFormats/Associations/interface/TracksterToSimClusterAssociatorBaseImpl.h index 138cde9318da4..236fd1dd3a81c 100644 --- a/SimDataFormats/Associations/interface/TracksterToSimClusterAssociatorBaseImpl.h +++ b/SimDataFormats/Associations/interface/TracksterToSimClusterAssociatorBaseImpl.h @@ -34,11 +34,15 @@ namespace hgcal { /// Associate a Trackster to SimClusters virtual hgcal::RecoToSimCollectionTracksters associateRecoToSim( - const edm::Handle &tCH, const edm::Handle &lCCH, const edm::Handle &sCCH) const; + const edm::Handle &tCH, + const edm::Handle &lCCH, + const edm::Handle &sCCH) const; /// Associate a SimCluster to Tracksters virtual hgcal::SimToRecoCollectionTracksters associateSimToReco( - const edm::Handle &tCH, const edm::Handle &lCCH, const edm::Handle &sCCH) const; + const edm::Handle &tCH, + const edm::Handle &lCCH, + const edm::Handle &sCCH) const; }; } // namespace hgcal diff --git a/SimDataFormats/Associations/plugins/TSToSCAssociatorEDProducer.cc b/SimDataFormats/Associations/plugins/TSToSCAssociatorEDProducer.cc index 994d59d60b324..f6e01e8c6beeb 100644 --- a/SimDataFormats/Associations/plugins/TSToSCAssociatorEDProducer.cc +++ b/SimDataFormats/Associations/plugins/TSToSCAssociatorEDProducer.cc @@ -50,8 +50,7 @@ TSToSCAssociatorEDProducer::TSToSCAssociatorEDProducer(const edm::ParameterSet & SCCollectionToken_ = consumes(pset.getParameter("label_scl")); TSCollectionToken_ = consumes(pset.getParameter("label_tst")); LCCollectionToken_ = consumes(pset.getParameter("label_lcl")); - associatorToken_ = - consumes(pset.getParameter("associator")); + associatorToken_ = consumes(pset.getParameter("associator")); } TSToSCAssociatorEDProducer::~TSToSCAssociatorEDProducer() {} @@ -78,10 +77,12 @@ void TSToSCAssociatorEDProducer::produce(edm::StreamID, edm::Event &iEvent, cons // associate LC and SC LogTrace("AssociatorValidator") << "Calling associateRecoToSim method\n"; - hgcal::RecoToSimCollectionTracksters recSimColl = theAssociator->associateRecoToSim(TSCollection, LCCollection, SCCollection); + hgcal::RecoToSimCollectionTracksters recSimColl = + theAssociator->associateRecoToSim(TSCollection, LCCollection, SCCollection); LogTrace("AssociatorValidator") << "Calling associateSimToReco method\n"; - hgcal::SimToRecoCollectionTracksters simRecColl = theAssociator->associateSimToReco(TSCollection, LCCollection, SCCollection); + hgcal::SimToRecoCollectionTracksters simRecColl = + theAssociator->associateSimToReco(TSCollection, LCCollection, SCCollection); auto rts = std::make_unique(recSimColl); auto str = std::make_unique(simRecColl); diff --git a/SimDataFormats/Associations/src/TracksterToSimClusterAssociatorBaseImpl.cc b/SimDataFormats/Associations/src/TracksterToSimClusterAssociatorBaseImpl.cc index 8e796bfc39f9d..a049ff94788e5 100644 --- a/SimDataFormats/Associations/src/TracksterToSimClusterAssociatorBaseImpl.cc +++ b/SimDataFormats/Associations/src/TracksterToSimClusterAssociatorBaseImpl.cc @@ -7,12 +7,16 @@ namespace hgcal { TracksterToSimClusterAssociatorBaseImpl::~TracksterToSimClusterAssociatorBaseImpl(){}; hgcal::RecoToSimCollectionTracksters TracksterToSimClusterAssociatorBaseImpl::associateRecoToSim( - const edm::Handle &tCH, const edm::Handle &lCCH, const edm::Handle &sCCH) const { + const edm::Handle &tCH, + const edm::Handle &lCCH, + const edm::Handle &sCCH) const { return hgcal::RecoToSimCollectionTracksters(); } hgcal::SimToRecoCollectionTracksters TracksterToSimClusterAssociatorBaseImpl::associateSimToReco( - const edm::Handle &tCH, const edm::Handle &lCCH, const edm::Handle &sCCH) const { + const edm::Handle &tCH, + const edm::Handle &lCCH, + const edm::Handle &sCCH) const { return hgcal::SimToRecoCollectionTracksters(); } From b916fad7f1a4fb3ef100782ff6290d0c11600340 Mon Sep 17 00:00:00 2001 From: Leonardo Cristella Date: Tue, 6 Apr 2021 17:58:15 +0200 Subject: [PATCH 6/8] Consider LC multiplicity among tracksters --- .../TracksterAssociatorByEnergyScoreImpl.cc | 61 +++++++++++-------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.cc b/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.cc index 6a85fd21d40f8..659108d162016 100644 --- a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.cc +++ b/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.cc @@ -116,7 +116,10 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( scsInTrackster.resize(nTracksters); for (unsigned int tsId = 0; tsId < nTracksters; ++tsId) { - for (const auto& lcId : tracksters[tsId].vertices()) { + for (unsigned int i = 0; i < tracksters[tsId].vertices().size(); ++i) { + const auto lcId = tracksters[tsId].vertices(i); + const auto lcFractionInTs = 1.f / tracksters[tsId].vertex_multiplicity(i); + const std::vector>& hits_and_fractions = layerClusters[lcId].hitsAndFractions(); unsigned int numberOfHitsInLC = hits_and_fractions.size(); @@ -141,7 +144,7 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( for (const auto& h : hit_find_in_SC->second) { //tssInSimCluster[simclusterId][layerclusterId]-> (energy,score) //SC_i - > TS_j, TS_k, ... - tssInSimCluster[h.clusterId].tracksterIdToEnergyAndScore[tsId].first += h.fraction * hit->energy(); + tssInSimCluster[h.clusterId].tracksterIdToEnergyAndScore[tsId].first += lcFractionInTs * h.fraction * hit->energy(); //TS_i -> SC_j, SC_k, ... scsInTrackster[tsId].emplace_back(h.clusterId, 0.f); } @@ -330,23 +333,28 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( } float invTracksterEnergyWeight = 0.f; - for (const auto& lcId : tracksters[tsId].vertices()) { - const auto& hits_and_fractions = layerClusters[lcId].hitsAndFractions(); + for (unsigned int i = 0; i < tracksters[tsId].vertices().size(); ++i) { + const auto lcId = tracksters[tsId].vertices(i); + const auto lcFractionInTs = 1.f / tracksters[tsId].vertex_multiplicity(i); + const auto& hits_and_fractions = layerClusters[lcId].hitsAndFractions(); // Compute the correct normalization for (auto const& haf : hits_and_fractions) { invTracksterEnergyWeight += - (haf.second * hitMap_->at(haf.first)->energy()) * (haf.second * hitMap_->at(haf.first)->energy()); + (lcFractionInTs * haf.second * hitMap_->at(haf.first)->energy()) * (lcFractionInTs * haf.second * hitMap_->at(haf.first)->energy()); } } invTracksterEnergyWeight = 1.f / invTracksterEnergyWeight; - for (const auto& lcId : tracksters[tsId].vertices()) { + for (unsigned int i = 0; i < tracksters[tsId].vertices().size(); ++i) { + const auto lcId = tracksters[tsId].vertices(i); + const auto lcFractionInTs = 1.f / tracksters[tsId].vertex_multiplicity(i); + const auto& hits_and_fractions = layerClusters[lcId].hitsAndFractions(); unsigned int numberOfHitsInLC = hits_and_fractions.size(); for (unsigned int i = 0; i < numberOfHitsInLC; ++i) { DetId rh_detid = hits_and_fractions[i].first; - float rhFraction = hits_and_fractions[i].second; + float rhFraction = hits_and_fractions[i].second * lcFractionInTs; const bool hitWithSC = (detIdToSimClusterId_Map.find(rh_detid) != detIdToSimClusterId_Map.end()); @@ -438,27 +446,32 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( const HGCRecHit* hit = itcheck->second; float hitEnergyWeight = hit->energy() * hit->energy(); for (auto& tsPair : tssInSimCluster[scId].tracksterIdToEnergyAndScore) { - unsigned int tracksterId = tsPair.first; + unsigned int tsId = tsPair.first; float tsFraction = 0.f; - if (hitWithLC) { - const auto findHitIt = std::find(detIdToLayerClusterId_Map[sc_hitDetId].begin(), - detIdToLayerClusterId_Map[sc_hitDetId].end(), - hgcal::detIdInfoInCluster{tracksterId, 0.f}); - if (findHitIt != detIdToLayerClusterId_Map[sc_hitDetId].end()) - tsFraction = findHitIt->fraction; - } - tsPair.second.second += - (tsFraction - scFraction) * (tsFraction - scFraction) * hitEnergyWeight * invSCEnergyWeight; + for (unsigned int i = 0; i < tracksters[tsId].vertices().size(); ++i) { + const auto lcId = tracksters[tsId].vertices(i); + const auto lcFractionInTs = 1.f / tracksters[tsId].vertex_multiplicity(i); + + if (hitWithLC) { + const auto findHitIt = std::find(detIdToLayerClusterId_Map[sc_hitDetId].begin(), + detIdToLayerClusterId_Map[sc_hitDetId].end(), + hgcal::detIdInfoInCluster{lcId, 0.f}); + if (findHitIt != detIdToLayerClusterId_Map[sc_hitDetId].end()) + tsFraction = findHitIt->fraction * lcFractionInTs; + } + tsPair.second.second += + (tsFraction - scFraction) * (tsFraction - scFraction) * hitEnergyWeight * invSCEnergyWeight; #ifdef EDM_ML_DEBUG - LogDebug("TracksterAssociatorByEnergyScoreImpl") - << "SCDetId:\t" << (uint32_t)sc_hitDetId << "\tTracksterId:\t" << tracksterId << "\t" - << "tsFraction, scFraction:\t" << tsFraction << ", " << scFraction << "\t" - << "hitEnergyWeight:\t" << hitEnergyWeight << "\t" - << "current score:\t" << tsPair.second.second << "\t" - << "invSCEnergyWeight:\t" << invSCEnergyWeight << "\n"; + LogDebug("TracksterAssociatorByEnergyScoreImpl") + << "SCDetId:\t" << (uint32_t)sc_hitDetId << "\tTracksterId:\t" << tracksterId << "\t" + << "tsFraction, scFraction:\t" << tsFraction << ", " << scFraction << "\t" + << "hitEnergyWeight:\t" << hitEnergyWeight << "\t" + << "current score:\t" << tsPair.second.second << "\t" + << "invSCEnergyWeight:\t" << invSCEnergyWeight << "\n"; #endif - } // End of loop over LayerClusters linked to hits of this SimCluster + } // End of loop over Trackster's LayerClusters + } // End of loop over Tracksters linked to hits of this SimCluster } // End of loop over hits of SimCluster on a Layer #ifdef EDM_ML_DEBUG if (tssInSimCluster[scId].tracksterIdToEnergyAndScore.empty()) From 886d64926c230c57bea1d6c1fdc72e72c19bf497 Mon Sep 17 00:00:00 2001 From: Leonardo Cristella Date: Tue, 6 Apr 2021 19:10:18 +0200 Subject: [PATCH 7/8] code-format --- .../plugins/TracksterAssociatorByEnergyScoreImpl.cc | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.cc b/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.cc index 659108d162016..6c78e2e6cfe5f 100644 --- a/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.cc +++ b/SimCalorimetry/HGCalAssociatorProducers/plugins/TracksterAssociatorByEnergyScoreImpl.cc @@ -144,7 +144,8 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( for (const auto& h : hit_find_in_SC->second) { //tssInSimCluster[simclusterId][layerclusterId]-> (energy,score) //SC_i - > TS_j, TS_k, ... - tssInSimCluster[h.clusterId].tracksterIdToEnergyAndScore[tsId].first += lcFractionInTs * h.fraction * hit->energy(); + tssInSimCluster[h.clusterId].tracksterIdToEnergyAndScore[tsId].first += + lcFractionInTs * h.fraction * hit->energy(); //TS_i -> SC_j, SC_k, ... scsInTrackster[tsId].emplace_back(h.clusterId, 0.f); } @@ -340,8 +341,8 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( const auto& hits_and_fractions = layerClusters[lcId].hitsAndFractions(); // Compute the correct normalization for (auto const& haf : hits_and_fractions) { - invTracksterEnergyWeight += - (lcFractionInTs * haf.second * hitMap_->at(haf.first)->energy()) * (lcFractionInTs * haf.second * hitMap_->at(haf.first)->energy()); + invTracksterEnergyWeight += (lcFractionInTs * haf.second * hitMap_->at(haf.first)->energy()) * + (lcFractionInTs * haf.second * hitMap_->at(haf.first)->energy()); } } invTracksterEnergyWeight = 1.f / invTracksterEnergyWeight; @@ -471,8 +472,8 @@ hgcal::association TracksterAssociatorByEnergyScoreImpl::makeConnections( << "invSCEnergyWeight:\t" << invSCEnergyWeight << "\n"; #endif } // End of loop over Trackster's LayerClusters - } // End of loop over Tracksters linked to hits of this SimCluster - } // End of loop over hits of SimCluster on a Layer + } // End of loop over Tracksters linked to hits of this SimCluster + } // End of loop over hits of SimCluster on a Layer #ifdef EDM_ML_DEBUG if (tssInSimCluster[scId].tracksterIdToEnergyAndScore.empty()) LogDebug("TracksterAssociatorByEnergyScoreImpl") << "SC Id: \t" << scId << "\tTS id:\t-1 " From 087cef266a96961503b660c8d866fa46cd847265 Mon Sep 17 00:00:00 2001 From: Leonardo Cristella Date: Fri, 9 Apr 2021 13:40:07 +0200 Subject: [PATCH 8/8] Fix dependecy --- SimDataFormats/Associations/plugins/BuildFile.xml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/SimDataFormats/Associations/plugins/BuildFile.xml b/SimDataFormats/Associations/plugins/BuildFile.xml index 57385a0812e8c..dc791a6f1a583 100644 --- a/SimDataFormats/Associations/plugins/BuildFile.xml +++ b/SimDataFormats/Associations/plugins/BuildFile.xml @@ -1,6 +1,8 @@ - + + +