Skip to content

Commit

Permalink
Merge pull request #46895 from felicepantaleo/cleanup_associationMap_…
Browse files Browse the repository at this point in the history
…14_2_X_rebase

 [14_2_X] Improve TICL Association Map and update TICL event content
  • Loading branch information
cmsbuild authored Dec 12, 2024
2 parents cc91330 + 595c929 commit d09e67a
Show file tree
Hide file tree
Showing 22 changed files with 456 additions and 966 deletions.
12 changes: 7 additions & 5 deletions RecoHGCal/Configuration/python/RecoHGCal_EventContent_cff.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
['keep *_pfTICL_*_*'] +
['keep CaloParticles_mix_*_*', 'keep SimClusters_mix_*_*', 'keep *_SimClusterToCaloParticleAssociation*_*_*'] +
['keep *_SimClusterToCaloParticleAssociation*_*_*', 'keep *_layerClusterSimClusterAssociationProducer_*_*','keep *_layerClusterCaloParticleAssociationProducer_*_*', 'keep *_layerClusterSimTracksterAssociationProducer_*_*'] +
['keep *_tracksterSimTracksterAssociation*_*_*' , 'keep *_tracksterSimTracksterFromCPsAssociation*_*_*' ]
['keep *_allTrackstersToSimTrackstersAssociations*_*_*']
)
)

Expand All @@ -50,7 +50,8 @@
'keep *_ticlSimTracksters_*_*',
'keep *_ticlSimTICLCandidates_*_*',
'keep *_ticlSimTrackstersFromCP_*_*',
'keep *_SimTau*_*_*'
'keep *_SimTau*_*_*',
'keep *_allTrackstersToSimTrackstersAssociations*_*_*'
)
)
TICL_FEVT.outputCommands.extend(TICL_RECO.outputCommands)
Expand All @@ -61,6 +62,7 @@
'keep *_ticlSimTrackstersFromCP_*_*',
'keep CaloParticles_mix_*_*', 'keep SimClusters_mix_*_*', 'keep *_SimClusterToCaloParticleAssociation*_*_*',
'keep *_SimClusterToCaloParticleAssociation*_*_*', 'keep *_layerClusterSimClusterAssociationProducer_*_*','keep *_layerClusterCaloParticleAssociationProducer_*_*', 'keep *_layerClusterSimTracksterAssociationProducer_*_*',
'keep *_SimTau*_*_*',
'keep *_allTrackstersToSimTrackstersAssociations*_*_*'

)
Expand Down Expand Up @@ -110,10 +112,10 @@ def cleanOutputAndSet(outputModule, ticl_outputCommads):
'keep *_layerClusterCaloParticleAssociationProducer_*_*',
'keep *_randomEngineStateProducer_*_*',
'keep *_layerClusterSimTracksterAssociationProducer_*_*',
'keep *_tracksterSimTracksterAssociation*_*_*',
'keep *_tracksterSimTracksterFromCPsAssociation*_*_*',
'keep *_SimClusterToCaloParticleAssociation*_*_*',
'keep *_simClusterToCaloParticleAssociator*_*_*'
'keep *_simClusterToCaloParticleAssociator*_*_*',
'keep *_SimTau*_*_*',
'keep *_allTrackstersToSimTrackstersAssociations*_*_*'
])

if hasattr(process, 'FEVTDEBUGEventContent'):
Expand Down
18 changes: 9 additions & 9 deletions RecoHGCal/TICL/plugins/TICLDumper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
#include "CommonTools/UtilAlgos/interface/TFileService.h"

using TracksterToTracksterMap =
ticl::AssociationMap<ticl::mapWithFractionAndScore, std::vector<ticl::Trackster>, std::vector<ticl::Trackster>>;
ticl::AssociationMap<ticl::mapWithSharedEnergyAndScore, std::vector<ticl::Trackster>, std::vector<ticl::Trackster>>;
// Helper class for geometry, magnetic field, etc
class DetectorTools {
public:
Expand Down Expand Up @@ -514,10 +514,10 @@ class TracksterToSimTracksterAssociationHelper {
recoToSim_sharedE.resize(numberOfTracksters);

for (size_t i = 0; i < numberOfTracksters; ++i) {
for (const auto& [simTracksterIndex, sharedEnergyAndScorePair] : recoToSimMap[i]) {
recoToSim[i].push_back(simTracksterIndex);
recoToSim_sharedE[i].push_back(sharedEnergyAndScorePair.first);
recoToSim_score[i].push_back(sharedEnergyAndScorePair.second);
for (const auto& simTracksterElement : recoToSimMap[i]) {
recoToSim[i].push_back(simTracksterElement.index());
recoToSim_sharedE[i].push_back(simTracksterElement.sharedEnergy());
recoToSim_score[i].push_back(simTracksterElement.score());
}
}

Expand All @@ -528,10 +528,10 @@ class TracksterToSimTracksterAssociationHelper {
simToReco_sharedE.resize(numberOfSimTracksters);

for (size_t i = 0; i < numberOfSimTracksters; ++i) {
for (const auto& [recoTracksterIndex, sharedEnergyAndScorePair] : simToRecoMap[i]) {
simToReco[i].push_back(recoTracksterIndex);
simToReco_sharedE[i].push_back(sharedEnergyAndScorePair.first);
simToReco_score[i].push_back(sharedEnergyAndScorePair.second);
for (const auto& recoTracksterElement : simToRecoMap[i]) {
simToReco[i].push_back(recoTracksterElement.index());
simToReco_sharedE[i].push_back(recoTracksterElement.sharedEnergy());
simToReco_score[i].push_back(recoTracksterElement.score());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ AllLayerClusterToTracksterAssociatorsProducer::AllLayerClusterToTracksterAssocia

// Produce separate association maps for each trackster collection using the trackster label
for (const auto& tracksterToken : tracksterCollectionTokens_) {
produces<ticl::AssociationMap<ticl::mapWithFraction, std::vector<reco::CaloCluster>, std::vector<ticl::Trackster>>>(
produces<
ticl::AssociationMap<ticl::mapWithSharedEnergy, std::vector<reco::CaloCluster>, std::vector<ticl::Trackster>>>(
tracksterToken.first);
}
}
Expand All @@ -59,7 +60,7 @@ void AllLayerClusterToTracksterAssociatorsProducer::produce(edm::StreamID,

// Create association map
auto lcToTracksterMap = std::make_unique<
ticl::AssociationMap<ticl::mapWithFraction, std::vector<reco::CaloCluster>, std::vector<ticl::Trackster>>>(
ticl::AssociationMap<ticl::mapWithSharedEnergy, std::vector<reco::CaloCluster>, std::vector<ticl::Trackster>>>(
layer_clusters, tracksters, iEvent);

// Loop over tracksters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ AllTracksterToSimTracksterAssociatorsByHitsProducer::AllTracksterToSimTracksterA
for (const auto& tracksterToken : tracksterCollectionTokens_) {
for (const auto& simTracksterToken : simTracksterCollectionTokens_) {
std::string instanceLabel = tracksterToken.first + "To" + simTracksterToken.first;
produces<ticl::AssociationMap<ticl::mapWithFractionAndScore,
produces<ticl::AssociationMap<ticl::mapWithSharedEnergyAndScore,
std::vector<ticl::Trackster>,
std::vector<ticl::Trackster>>>(instanceLabel);
std::string reverseInstanceLabel = simTracksterToken.first + "To" + tracksterToken.first;
produces<ticl::AssociationMap<ticl::mapWithFractionAndScore,
produces<ticl::AssociationMap<ticl::mapWithSharedEnergyAndScore,
std::vector<ticl::Trackster>,
std::vector<ticl::Trackster>>>(reverseInstanceLabel);
}
Expand Down Expand Up @@ -180,11 +180,13 @@ void AllTracksterToSimTracksterAssociatorsByHitsProducer::produce(edm::StreamID,
const auto& simTracksterToHitMap = *simTracksterToHitMapHandle;

// Create the association maps
auto tracksterToSimTracksterMap = std::make_unique<
ticl::AssociationMap<ticl::mapWithFractionAndScore, std::vector<ticl::Trackster>, std::vector<ticl::Trackster>>>(
auto tracksterToSimTracksterMap = std::make_unique<ticl::AssociationMap<ticl::mapWithSharedEnergyAndScore,
std::vector<ticl::Trackster>,
std::vector<ticl::Trackster>>>(
recoTrackstersHandle, simTrackstersHandle, iEvent);
auto simTracksterToTracksterMap = std::make_unique<
ticl::AssociationMap<ticl::mapWithFractionAndScore, std::vector<ticl::Trackster>, std::vector<ticl::Trackster>>>(
auto simTracksterToTracksterMap = std::make_unique<ticl::AssociationMap<ticl::mapWithSharedEnergyAndScore,
std::vector<ticl::Trackster>,
std::vector<ticl::Trackster>>>(
simTrackstersHandle, recoTrackstersHandle, iEvent);

for (unsigned int tracksterIndex = 0; tracksterIndex < recoTracksters.size(); ++tracksterIndex) {
Expand All @@ -195,33 +197,37 @@ void AllTracksterToSimTracksterAssociatorsByHitsProducer::produce(edm::StreamID,
recoTracksterHitsAndFractions.size());
std::vector<unsigned int> associatedSimTracksterIndices;
for (unsigned int i = 0; i < recoTracksterHitsAndFractions.size(); ++i) {
const auto& [hitIndex, recoFraction] = recoTracksterHitsAndFractions[i];
const auto& hitElement = recoTracksterHitsAndFractions[i];
unsigned int hitIndex = hitElement.index();
float recoFraction = hitElement.fraction();
const auto& recHit = rechitManager[hitIndex];
float squaredRecoFraction = recoFraction * recoFraction;
float rechitEnergy = recHit.energy();
float squaredRecHitEnergy = rechitEnergy * rechitEnergy;
recoToSimScoresDenominator += squaredRecoFraction * squaredRecHitEnergy;

const auto& hitToSimTracksterVec = hitToSimTracksterMap[hitIndex];
for (const auto& [simTracksterIndex, fraction] : hitToSimTracksterVec) {
for (const auto& simTracksterElement : hitToSimTracksterVec) {
auto simTracksterIndex = simTracksterElement.index();
const auto& simTrackster = simTracksters[simTracksterIndex];
auto& seed = simTrackster.seedID();
float simFraction = 0;
if (seed == caloParticlesHandle.id()) {
unsigned int caloParticleIndex = simTrackster.seedIndex();
auto it = std::find_if(hitToCaloParticleMap[hitIndex].begin(),
hitToCaloParticleMap[hitIndex].end(),
[caloParticleIndex](const auto& pair) { return pair.first == caloParticleIndex; });
auto it =
std::find_if(hitToCaloParticleMap[hitIndex].begin(),
hitToCaloParticleMap[hitIndex].end(),
[caloParticleIndex](const auto& pair) { return pair.index() == caloParticleIndex; });
if (it != hitToCaloParticleMap[hitIndex].end()) {
simFraction = it->second;
simFraction = it->fraction();
}
} else {
unsigned int simClusterIndex = simTracksters[simTracksterIndex].seedIndex();
auto it = std::find_if(hitToSimClusterMap[hitIndex].begin(),
hitToSimClusterMap[hitIndex].end(),
[simClusterIndex](const auto& pair) { return pair.first == simClusterIndex; });
[simClusterIndex](const auto& pair) { return pair.index() == simClusterIndex; });
if (it != hitToSimClusterMap[hitIndex].end()) {
simFraction = it->second;
simFraction = it->fraction();
}
}
hitToAssociatedSimTracksterMap.insert(i, simTracksterIndex, simFraction);
Expand All @@ -235,11 +241,11 @@ void AllTracksterToSimTracksterAssociatorsByHitsProducer::produce(edm::StreamID,

// Add missing sim tracksters with 0 shared energy to hitToAssociatedSimTracksterMap
for (unsigned int i = 0; i < recoTracksterHitsAndFractions.size(); ++i) {
unsigned int hitId = recoTracksterHitsAndFractions[i].first;
unsigned int hitId = recoTracksterHitsAndFractions[i].index();
const auto& simTracksterVec = hitToSimTracksterMap[hitId];
for (unsigned int simTracksterIndex : associatedSimTracksterIndices) {
if (std::find_if(simTracksterVec.begin(), simTracksterVec.end(), [simTracksterIndex](const auto& pair) {
return pair.first == simTracksterIndex;
return pair.index() == simTracksterIndex;
}) == simTracksterVec.end()) {
hitToAssociatedSimTracksterMap.insert(i, simTracksterIndex, 0);
}
Expand All @@ -249,14 +255,16 @@ void AllTracksterToSimTracksterAssociatorsByHitsProducer::produce(edm::StreamID,
const float invDenominator = 1.f / recoToSimScoresDenominator;

for (unsigned int i = 0; i < recoTracksterHitsAndFractions.size(); ++i) {
unsigned int hitIndex = recoTracksterHitsAndFractions[i].first;
unsigned int hitIndex = recoTracksterHitsAndFractions[i].index();
const auto& recHit = rechitManager[hitIndex];
float recoFraction = recoTracksterHitsAndFractions[i].second;
float recoFraction = recoTracksterHitsAndFractions[i].fraction();
float squaredRecoFraction = recoFraction * recoFraction;
float squaredRecHitEnergy = recHit.energy() * recHit.energy();
float recoSharedEnergy = recHit.energy() * recoFraction;
const auto& simTracksterVec = hitToAssociatedSimTracksterMap[i];
for (const auto& [simTracksterIndex, simFraction] : simTracksterVec) {
for (const auto& simTracksterElement : simTracksterVec) {
auto simTracksterIndex = simTracksterElement.index();
auto simFraction = simTracksterElement.fraction();
edm::Ref<std::vector<ticl::Trackster>> simTracksterRef(simTrackstersHandle, simTracksterIndex);
float sharedEnergy = std::min(simFraction * recHit.energy(), recoSharedEnergy);
float squaredFraction =
Expand All @@ -281,17 +289,16 @@ void AllTracksterToSimTracksterAssociatorsByHitsProducer::produce(edm::StreamID,
bool isSimTracksterFromCP = (seed == caloParticlesHandle.id());
std::vector<float> simFractions(simTracksterHitsAndFractions.size(), 0.f);
for (unsigned int i = 0; i < simTracksterHitsAndFractions.size(); ++i) {
const auto& [hitIndex, simTracksterFraction] = simTracksterHitsAndFractions[i];

auto hitIndex = simTracksterHitsAndFractions[i].index();
auto it = isSimTracksterFromCP
? (std::find_if(hitToCaloParticleMap[hitIndex].begin(),
hitToCaloParticleMap[hitIndex].end(),
[simObjectIndex](const auto& pair) { return pair.first == simObjectIndex; }))
[simObjectIndex](const auto& pair) { return pair.index() == simObjectIndex; }))
: std::find_if(hitToSimClusterMap[hitIndex].begin(),
hitToSimClusterMap[hitIndex].end(),
[simObjectIndex](const auto& pair) { return pair.first == simObjectIndex; });
[simObjectIndex](const auto& pair) { return pair.index() == simObjectIndex; });
if (it != hitToCaloParticleMap[hitIndex].end() and it != hitToSimClusterMap[hitIndex].end()) {
simFractions[i] = it->second;
simFractions[i] = it->fraction();
}
float simFraction = simFractions[i];
const auto& recHit = rechitManager[hitIndex];
Expand All @@ -300,7 +307,9 @@ void AllTracksterToSimTracksterAssociatorsByHitsProducer::produce(edm::StreamID,
simToRecoScoresDenominator += squaredSimFraction * squaredRecHitEnergy;

const auto& hitToRecoTracksterVec = hitToTracksterMap[hitIndex];
for (const auto& [recoTracksterIndex, recoFraction] : hitToRecoTracksterVec) {
for (const auto& recoTracksterElement : hitToRecoTracksterVec) {
unsigned int recoTracksterIndex = recoTracksterElement.index();
float recoFraction = recoTracksterElement.fraction();
hitToAssociatedRecoTracksterMap.insert(i, recoTracksterIndex, recoFraction);
associatedRecoTracksterIndices.push_back(recoTracksterIndex);
}
Expand All @@ -312,12 +321,12 @@ void AllTracksterToSimTracksterAssociatorsByHitsProducer::produce(edm::StreamID,
associatedRecoTracksterIndices.end());

for (unsigned int i = 0; i < simTracksterHitsAndFractions.size(); ++i) {
unsigned int hitIndex = simTracksterHitsAndFractions[i].first;
unsigned int hitIndex = simTracksterHitsAndFractions[i].index();
const auto& hitToRecoTracksterVec = hitToTracksterMap[hitIndex];
for (unsigned int recoTracksterIndex : associatedRecoTracksterIndices) {
if (std::find_if(
hitToRecoTracksterVec.begin(), hitToRecoTracksterVec.end(), [recoTracksterIndex](const auto& pair) {
return pair.first == recoTracksterIndex;
return pair.index() == recoTracksterIndex;
}) == hitToRecoTracksterVec.end()) {
hitToAssociatedRecoTracksterMap.insert(i, recoTracksterIndex, 0);
}
Expand All @@ -327,15 +336,17 @@ void AllTracksterToSimTracksterAssociatorsByHitsProducer::produce(edm::StreamID,
const float invDenominator = 1.f / simToRecoScoresDenominator;

for (unsigned int i = 0; i < simTracksterHitsAndFractions.size(); ++i) {
const auto& [hitIndex, simTracksterFraction] = simTracksterHitsAndFractions[i];
const auto& hitIndex = simTracksterHitsAndFractions[i].index();
float simFraction = simFractions[i];
const auto& recHit = rechitManager[hitIndex];
float squaredSimFraction = simFraction * simFraction;
float squaredRecHitEnergy = recHit.energy() * recHit.energy();
float simSharedEnergy = recHit.energy() * simFraction;

const auto& hitToRecoTracksterVec = hitToAssociatedRecoTracksterMap[i];
for (const auto& [recoTracksterIndex, recoFraction] : hitToRecoTracksterVec) {
for (const auto& recoTracksterElement : hitToRecoTracksterVec) {
auto recoTracksterIndex = recoTracksterElement.index();
float recoFraction = recoTracksterElement.fraction();
edm::Ref<std::vector<ticl::Trackster>> recoTracksterRef(recoTrackstersHandle, recoTracksterIndex);
float sharedEnergy = std::min(recoFraction * recHit.energy(), simSharedEnergy);
float squaredFraction =
Expand All @@ -346,8 +357,9 @@ void AllTracksterToSimTracksterAssociatorsByHitsProducer::produce(edm::StreamID,
}
}

tracksterToSimTracksterMap->sort(true);
simTracksterToTracksterMap->sort(true);
// Sort the maps by score in ascending order
tracksterToSimTracksterMap->sort([](const auto& a, const auto& b) { return a.score() < b.score(); });
simTracksterToTracksterMap->sort([](const auto& a, const auto& b) { return a.score() < b.score(); });

// After populating the maps, store them in the event
iEvent.put(std::move(tracksterToSimTracksterMap), tracksterToken.first + "To" + simTracksterToken.first);
Expand Down
Loading

0 comments on commit d09e67a

Please sign in to comment.