File indexing completed on 2024-10-16 05:06:39
0001
0002
0003 #include "FWCore/Framework/interface/global/EDProducer.h"
0004 #include "FWCore/Framework/interface/Event.h"
0005 #include "FWCore/Framework/interface/MakerMacros.h"
0006 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0008 #include "FWCore/Utilities/interface/EDGetToken.h"
0009 #include "DataFormats/HGCalReco/interface/Trackster.h"
0010 #include "SimDataFormats/Associations/interface/TICLAssociationMap.h"
0011 #include "DataFormats/Provenance/interface/ProductID.h"
0012 #include "DataFormats/HGCRecHit/interface/HGCRecHitCollections.h"
0013 #include "CommonTools/RecoAlgos/interface/MultiVectorManager.h"
0014 #include "DataFormats/CaloRecHit/interface/CaloCluster.h"
0015 #include "SimDataFormats/CaloAnalysis/interface/CaloParticle.h"
0016 #include "SimDataFormats/CaloAnalysis/interface/SimCluster.h"
0017
0018 class AllTracksterToSimTracksterAssociatorsByHitsProducer : public edm::global::EDProducer<> {
0019 public:
0020 explicit AllTracksterToSimTracksterAssociatorsByHitsProducer(const edm::ParameterSet&);
0021 ~AllTracksterToSimTracksterAssociatorsByHitsProducer() override = default;
0022
0023 static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0024
0025 private:
0026 void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
0027
0028 std::vector<std::pair<std::string, edm::EDGetTokenT<std::vector<ticl::Trackster>>>> tracksterCollectionTokens_;
0029 std::vector<std::pair<std::string, edm::EDGetTokenT<std::vector<ticl::Trackster>>>> simTracksterCollectionTokens_;
0030 edm::EDGetTokenT<std::vector<reco::CaloCluster>> layerClustersToken_;
0031 std::vector<std::pair<std::string, edm::EDGetTokenT<ticl::AssociationMap<ticl::mapWithFraction>>>>
0032 hitToTracksterMapTokens_;
0033 std::vector<std::pair<std::string, edm::EDGetTokenT<ticl::AssociationMap<ticl::mapWithFraction>>>>
0034 tracksterToHitMapTokens_;
0035
0036 std::vector<std::pair<std::string, edm::EDGetTokenT<ticl::AssociationMap<ticl::mapWithFraction>>>>
0037 hitToSimTracksterMapTokens_;
0038 std::vector<std::pair<std::string, edm::EDGetTokenT<ticl::AssociationMap<ticl::mapWithFraction>>>>
0039 simTracksterToHitMapTokens_;
0040
0041 std::vector<edm::EDGetTokenT<HGCRecHitCollection>> hitsTokens_;
0042 edm::EDGetTokenT<std::vector<CaloParticle>> caloParticleToken_;
0043 edm::EDGetTokenT<ticl::AssociationMap<ticl::mapWithFraction>> hitToSimClusterMapToken_;
0044 edm::EDGetTokenT<ticl::AssociationMap<ticl::mapWithFraction>> hitToCaloParticleMapToken_;
0045 };
0046
0047 AllTracksterToSimTracksterAssociatorsByHitsProducer::AllTracksterToSimTracksterAssociatorsByHitsProducer(
0048 const edm::ParameterSet& pset)
0049 : caloParticleToken_(consumes<std::vector<CaloParticle>>(pset.getParameter<edm::InputTag>("caloParticles"))),
0050 hitToSimClusterMapToken_(consumes<ticl::AssociationMap<ticl::mapWithFraction>>(
0051 pset.getParameter<edm::InputTag>("hitToSimClusterMap"))),
0052 hitToCaloParticleMapToken_(consumes<ticl::AssociationMap<ticl::mapWithFraction>>(
0053 pset.getParameter<edm::InputTag>("hitToCaloParticleMap"))) {
0054 const auto& tracksterCollections = pset.getParameter<std::vector<edm::InputTag>>("tracksterCollections");
0055 for (const auto& tag : tracksterCollections) {
0056 std::string label = tag.label();
0057 if (tag.instance() != "") {
0058 label += tag.instance();
0059 }
0060 tracksterCollectionTokens_.emplace_back(label, consumes<std::vector<ticl::Trackster>>(tag));
0061 hitToTracksterMapTokens_.emplace_back(label,
0062 consumes<ticl::AssociationMap<ticl::mapWithFraction>>(
0063 edm::InputTag("allHitToTracksterAssociations", "hitTo" + label)));
0064 tracksterToHitMapTokens_.emplace_back(label,
0065 consumes<ticl::AssociationMap<ticl::mapWithFraction>>(
0066 edm::InputTag("allHitToTracksterAssociations", label + "ToHit")));
0067 }
0068
0069 const auto& simTracksterCollections = pset.getParameter<std::vector<edm::InputTag>>("simTracksterCollections");
0070 for (const auto& tag : simTracksterCollections) {
0071 std::string label = tag.label();
0072 if (tag.instance() != "") {
0073 label += tag.instance();
0074 }
0075 simTracksterCollectionTokens_.emplace_back(label, consumes<std::vector<ticl::Trackster>>(tag));
0076 hitToSimTracksterMapTokens_.emplace_back(label,
0077 consumes<ticl::AssociationMap<ticl::mapWithFraction>>(
0078 edm::InputTag("allHitToTracksterAssociations", "hitTo" + label)));
0079 simTracksterToHitMapTokens_.emplace_back(label,
0080 consumes<ticl::AssociationMap<ticl::mapWithFraction>>(
0081 edm::InputTag("allHitToTracksterAssociations", label + "ToHit")));
0082 }
0083
0084
0085 auto hitsTags = pset.getParameter<std::vector<edm::InputTag>>("hits");
0086 for (const auto& tag : hitsTags) {
0087 hitsTokens_.push_back(consumes<HGCRecHitCollection>(tag));
0088 }
0089
0090
0091 for (const auto& tracksterToken : tracksterCollectionTokens_) {
0092 for (const auto& simTracksterToken : simTracksterCollectionTokens_) {
0093 std::string instanceLabel = tracksterToken.first + "To" + simTracksterToken.first;
0094 produces<ticl::AssociationMap<ticl::mapWithFractionAndScore,
0095 std::vector<ticl::Trackster>,
0096 std::vector<ticl::Trackster>>>(instanceLabel);
0097 std::string reverseInstanceLabel = simTracksterToken.first + "To" + tracksterToken.first;
0098 produces<ticl::AssociationMap<ticl::mapWithFractionAndScore,
0099 std::vector<ticl::Trackster>,
0100 std::vector<ticl::Trackster>>>(reverseInstanceLabel);
0101 }
0102 }
0103 }
0104
0105 void AllTracksterToSimTracksterAssociatorsByHitsProducer::produce(edm::StreamID,
0106 edm::Event& iEvent,
0107 const edm::EventSetup&) const {
0108 using namespace edm;
0109
0110 MultiVectorManager<HGCRecHit> rechitManager;
0111 for (const auto& token : hitsTokens_) {
0112 Handle<HGCRecHitCollection> hitsHandle;
0113 iEvent.getByToken(token, hitsHandle);
0114 rechitManager.addVector(*hitsHandle);
0115 }
0116
0117 Handle<ticl::AssociationMap<ticl::mapWithFraction>> hitToSimClusterMapHandle;
0118 iEvent.getByToken(hitToSimClusterMapToken_, hitToSimClusterMapHandle);
0119 const auto& hitToSimClusterMap = *hitToSimClusterMapHandle;
0120
0121 Handle<ticl::AssociationMap<ticl::mapWithFraction>> hitToCaloParticleMapHandle;
0122 iEvent.getByToken(hitToCaloParticleMapToken_, hitToCaloParticleMapHandle);
0123 const auto& hitToCaloParticleMap = *hitToCaloParticleMapHandle;
0124
0125 Handle<std::vector<CaloParticle>> caloParticlesHandle;
0126 iEvent.getByToken(caloParticleToken_, caloParticlesHandle);
0127
0128 for (const auto& tracksterToken : tracksterCollectionTokens_) {
0129 Handle<std::vector<ticl::Trackster>> recoTrackstersHandle;
0130 iEvent.getByToken(tracksterToken.second, recoTrackstersHandle);
0131 const auto& recoTracksters = *recoTrackstersHandle;
0132
0133
0134 Handle<ticl::AssociationMap<ticl::mapWithFraction>> hitToTracksterMapHandle;
0135 auto tracksterMapTokenIter = std::find_if(
0136 hitToTracksterMapTokens_.begin(), hitToTracksterMapTokens_.end(), [&tracksterToken](const auto& pair) {
0137 return pair.first == tracksterToken.first;
0138 });
0139 if (tracksterMapTokenIter != hitToTracksterMapTokens_.end()) {
0140 iEvent.getByToken(tracksterMapTokenIter->second, hitToTracksterMapHandle);
0141 }
0142 const auto& hitToTracksterMap = *hitToTracksterMapHandle;
0143
0144
0145 Handle<ticl::AssociationMap<ticl::mapWithFraction>> tracksterToHitMapHandle;
0146 auto tracksterToHitMapTokenIter = std::find_if(
0147 tracksterToHitMapTokens_.begin(), tracksterToHitMapTokens_.end(), [&tracksterToken](const auto& pair) {
0148 return pair.first == tracksterToken.first;
0149 });
0150 if (tracksterToHitMapTokenIter != tracksterToHitMapTokens_.end()) {
0151 iEvent.getByToken(tracksterToHitMapTokenIter->second, tracksterToHitMapHandle);
0152 }
0153 const auto& tracksterToHitMap = *tracksterToHitMapHandle;
0154
0155 for (const auto& simTracksterToken : simTracksterCollectionTokens_) {
0156 Handle<std::vector<ticl::Trackster>> simTrackstersHandle;
0157 iEvent.getByToken(simTracksterToken.second, simTrackstersHandle);
0158 const auto& simTracksters = *simTrackstersHandle;
0159
0160
0161 Handle<ticl::AssociationMap<ticl::mapWithFraction>> hitToSimTracksterMapHandle;
0162 auto simTracksterMapTokenIter =
0163 std::find_if(hitToSimTracksterMapTokens_.begin(),
0164 hitToSimTracksterMapTokens_.end(),
0165 [&simTracksterToken](const auto& pair) { return pair.first == simTracksterToken.first; });
0166 if (simTracksterMapTokenIter != hitToSimTracksterMapTokens_.end()) {
0167 iEvent.getByToken(simTracksterMapTokenIter->second, hitToSimTracksterMapHandle);
0168 }
0169 const auto& hitToSimTracksterMap = *hitToSimTracksterMapHandle;
0170
0171
0172 Handle<ticl::AssociationMap<ticl::mapWithFraction>> simTracksterToHitMapHandle;
0173 auto simTracksterToHitMapTokenIter =
0174 std::find_if(simTracksterToHitMapTokens_.begin(),
0175 simTracksterToHitMapTokens_.end(),
0176 [&simTracksterToken](const auto& pair) { return pair.first == simTracksterToken.first; });
0177 if (simTracksterToHitMapTokenIter != simTracksterToHitMapTokens_.end()) {
0178 iEvent.getByToken(simTracksterToHitMapTokenIter->second, simTracksterToHitMapHandle);
0179 }
0180 const auto& simTracksterToHitMap = *simTracksterToHitMapHandle;
0181
0182
0183 auto tracksterToSimTracksterMap = std::make_unique<
0184 ticl::AssociationMap<ticl::mapWithFractionAndScore, std::vector<ticl::Trackster>, std::vector<ticl::Trackster>>>(
0185 recoTrackstersHandle, simTrackstersHandle, iEvent);
0186 auto simTracksterToTracksterMap = std::make_unique<
0187 ticl::AssociationMap<ticl::mapWithFractionAndScore, std::vector<ticl::Trackster>, std::vector<ticl::Trackster>>>(
0188 simTrackstersHandle, recoTrackstersHandle, iEvent);
0189
0190 for (unsigned int tracksterIndex = 0; tracksterIndex < recoTracksters.size(); ++tracksterIndex) {
0191 edm::Ref<std::vector<ticl::Trackster>> recoTracksterRef(recoTrackstersHandle, tracksterIndex);
0192 float recoToSimScoresDenominator = 0.f;
0193 const auto& recoTracksterHitsAndFractions = tracksterToHitMap[tracksterIndex];
0194 ticl::AssociationMap<ticl::mapWithFraction> hitToAssociatedSimTracksterMap(
0195 recoTracksterHitsAndFractions.size());
0196 std::vector<unsigned int> associatedSimTracksterIndices;
0197 for (unsigned int i = 0; i < recoTracksterHitsAndFractions.size(); ++i) {
0198 const auto& [hitIndex, recoFraction] = recoTracksterHitsAndFractions[i];
0199 const auto& recHit = rechitManager[hitIndex];
0200 float squaredRecoFraction = recoFraction * recoFraction;
0201 float rechitEnergy = recHit.energy();
0202 float squaredRecHitEnergy = rechitEnergy * rechitEnergy;
0203 recoToSimScoresDenominator += squaredRecoFraction * squaredRecHitEnergy;
0204
0205 const auto& hitToSimTracksterVec = hitToSimTracksterMap[hitIndex];
0206 for (const auto& [simTracksterIndex, fraction] : hitToSimTracksterVec) {
0207 const auto& simTrackster = simTracksters[simTracksterIndex];
0208 auto& seed = simTrackster.seedID();
0209 float simFraction = 0;
0210 if (seed == caloParticlesHandle.id()) {
0211 unsigned int caloParticleIndex = simTrackster.seedIndex();
0212 auto it = std::find_if(hitToCaloParticleMap[hitIndex].begin(),
0213 hitToCaloParticleMap[hitIndex].end(),
0214 [caloParticleIndex](const auto& pair) { return pair.first == caloParticleIndex; });
0215 if (it != hitToCaloParticleMap[hitIndex].end()) {
0216 simFraction = it->second;
0217 }
0218 } else {
0219 unsigned int simClusterIndex = simTracksters[simTracksterIndex].seedIndex();
0220 auto it = std::find_if(hitToSimClusterMap[hitIndex].begin(),
0221 hitToSimClusterMap[hitIndex].end(),
0222 [simClusterIndex](const auto& pair) { return pair.first == simClusterIndex; });
0223 if (it != hitToSimClusterMap[hitIndex].end()) {
0224 simFraction = it->second;
0225 }
0226 }
0227 hitToAssociatedSimTracksterMap.insert(i, simTracksterIndex, simFraction);
0228 associatedSimTracksterIndices.push_back(simTracksterIndex);
0229 }
0230 }
0231 std::sort(associatedSimTracksterIndices.begin(), associatedSimTracksterIndices.end());
0232 associatedSimTracksterIndices.erase(
0233 std::unique(associatedSimTracksterIndices.begin(), associatedSimTracksterIndices.end()),
0234 associatedSimTracksterIndices.end());
0235
0236
0237 for (unsigned int i = 0; i < recoTracksterHitsAndFractions.size(); ++i) {
0238 unsigned int hitId = recoTracksterHitsAndFractions[i].first;
0239 const auto& simTracksterVec = hitToSimTracksterMap[hitId];
0240 for (unsigned int simTracksterIndex : associatedSimTracksterIndices) {
0241 if (std::find_if(simTracksterVec.begin(), simTracksterVec.end(), [simTracksterIndex](const auto& pair) {
0242 return pair.first == simTracksterIndex;
0243 }) == simTracksterVec.end()) {
0244 hitToAssociatedSimTracksterMap.insert(i, simTracksterIndex, 0);
0245 }
0246 }
0247 }
0248
0249 const float invDenominator = 1.f / recoToSimScoresDenominator;
0250
0251 for (unsigned int i = 0; i < recoTracksterHitsAndFractions.size(); ++i) {
0252 unsigned int hitIndex = recoTracksterHitsAndFractions[i].first;
0253 const auto& recHit = rechitManager[hitIndex];
0254 float recoFraction = recoTracksterHitsAndFractions[i].second;
0255 float squaredRecoFraction = recoFraction * recoFraction;
0256 float squaredRecHitEnergy = recHit.energy() * recHit.energy();
0257 float recoSharedEnergy = recHit.energy() * recoFraction;
0258 const auto& simTracksterVec = hitToAssociatedSimTracksterMap[i];
0259 for (const auto& [simTracksterIndex, simFraction] : simTracksterVec) {
0260 edm::Ref<std::vector<ticl::Trackster>> simTracksterRef(simTrackstersHandle, simTracksterIndex);
0261 float sharedEnergy = std::min(simFraction * recHit.energy(), recoSharedEnergy);
0262 float squaredFraction =
0263 std::min(squaredRecoFraction, (recoFraction - simFraction) * (recoFraction - simFraction));
0264 float score = invDenominator * squaredFraction * squaredRecHitEnergy;
0265 tracksterToSimTracksterMap->insert(recoTracksterRef, simTracksterRef, sharedEnergy, score);
0266 }
0267 }
0268 }
0269
0270
0271 for (unsigned int tracksterIndex = 0; tracksterIndex < simTracksters.size(); ++tracksterIndex) {
0272 edm::Ref<std::vector<ticl::Trackster>> simTracksterRef(simTrackstersHandle, tracksterIndex);
0273 float simToRecoScoresDenominator = 0.f;
0274 const auto& simTracksterHitsAndFractions = simTracksterToHitMap[tracksterIndex];
0275 ticl::AssociationMap<ticl::mapWithFraction> hitToAssociatedRecoTracksterMap(
0276 simTracksterHitsAndFractions.size());
0277 std::vector<unsigned int> associatedRecoTracksterIndices;
0278 const auto& simTrackster = simTracksters[tracksterIndex];
0279 auto& seed = simTrackster.seedID();
0280 unsigned int simObjectIndex = simTrackster.seedIndex();
0281 bool isSimTracksterFromCP = (seed == caloParticlesHandle.id());
0282 std::vector<float> simFractions(simTracksterHitsAndFractions.size(), 0.f);
0283 for (unsigned int i = 0; i < simTracksterHitsAndFractions.size(); ++i) {
0284 const auto& [hitIndex, simTracksterFraction] = simTracksterHitsAndFractions[i];
0285
0286 auto it = isSimTracksterFromCP
0287 ? (std::find_if(hitToCaloParticleMap[hitIndex].begin(),
0288 hitToCaloParticleMap[hitIndex].end(),
0289 [simObjectIndex](const auto& pair) { return pair.first == simObjectIndex; }))
0290 : std::find_if(hitToSimClusterMap[hitIndex].begin(),
0291 hitToSimClusterMap[hitIndex].end(),
0292 [simObjectIndex](const auto& pair) { return pair.first == simObjectIndex; });
0293 if (it != hitToCaloParticleMap[hitIndex].end() and it != hitToSimClusterMap[hitIndex].end()) {
0294 simFractions[i] = it->second;
0295 }
0296 float simFraction = simFractions[i];
0297 const auto& recHit = rechitManager[hitIndex];
0298 float squaredSimFraction = simFraction * simFraction;
0299 float squaredRecHitEnergy = recHit.energy() * recHit.energy();
0300 simToRecoScoresDenominator += squaredSimFraction * squaredRecHitEnergy;
0301
0302 const auto& hitToRecoTracksterVec = hitToTracksterMap[hitIndex];
0303 for (const auto& [recoTracksterIndex, recoFraction] : hitToRecoTracksterVec) {
0304 hitToAssociatedRecoTracksterMap.insert(i, recoTracksterIndex, recoFraction);
0305 associatedRecoTracksterIndices.push_back(recoTracksterIndex);
0306 }
0307 }
0308
0309 std::sort(associatedRecoTracksterIndices.begin(), associatedRecoTracksterIndices.end());
0310 associatedRecoTracksterIndices.erase(
0311 std::unique(associatedRecoTracksterIndices.begin(), associatedRecoTracksterIndices.end()),
0312 associatedRecoTracksterIndices.end());
0313
0314 for (unsigned int i = 0; i < simTracksterHitsAndFractions.size(); ++i) {
0315 unsigned int hitIndex = simTracksterHitsAndFractions[i].first;
0316 const auto& hitToRecoTracksterVec = hitToTracksterMap[hitIndex];
0317 for (unsigned int recoTracksterIndex : associatedRecoTracksterIndices) {
0318 if (std::find_if(
0319 hitToRecoTracksterVec.begin(), hitToRecoTracksterVec.end(), [recoTracksterIndex](const auto& pair) {
0320 return pair.first == recoTracksterIndex;
0321 }) == hitToRecoTracksterVec.end()) {
0322 hitToAssociatedRecoTracksterMap.insert(i, recoTracksterIndex, 0);
0323 }
0324 }
0325 }
0326
0327 const float invDenominator = 1.f / simToRecoScoresDenominator;
0328
0329 for (unsigned int i = 0; i < simTracksterHitsAndFractions.size(); ++i) {
0330 const auto& [hitIndex, simTracksterFraction] = simTracksterHitsAndFractions[i];
0331 float simFraction = simFractions[i];
0332 const auto& recHit = rechitManager[hitIndex];
0333 float squaredSimFraction = simFraction * simFraction;
0334 float squaredRecHitEnergy = recHit.energy() * recHit.energy();
0335 float simSharedEnergy = recHit.energy() * simFraction;
0336
0337 const auto& hitToRecoTracksterVec = hitToAssociatedRecoTracksterMap[i];
0338 for (const auto& [recoTracksterIndex, recoFraction] : hitToRecoTracksterVec) {
0339 edm::Ref<std::vector<ticl::Trackster>> recoTracksterRef(recoTrackstersHandle, recoTracksterIndex);
0340 float sharedEnergy = std::min(recoFraction * recHit.energy(), simSharedEnergy);
0341 float squaredFraction =
0342 std::min(squaredSimFraction, (recoFraction - simFraction) * (recoFraction - simFraction));
0343 float score = invDenominator * squaredFraction * squaredRecHitEnergy;
0344 simTracksterToTracksterMap->insert(simTracksterRef, recoTracksterRef, sharedEnergy, score);
0345 }
0346 }
0347 }
0348
0349 tracksterToSimTracksterMap->sort(true);
0350 simTracksterToTracksterMap->sort(true);
0351
0352
0353 iEvent.put(std::move(tracksterToSimTracksterMap), tracksterToken.first + "To" + simTracksterToken.first);
0354 iEvent.put(std::move(simTracksterToTracksterMap), simTracksterToken.first + "To" + tracksterToken.first);
0355 }
0356 }
0357 }
0358
0359 void AllTracksterToSimTracksterAssociatorsByHitsProducer::fillDescriptions(
0360 edm::ConfigurationDescriptions& descriptions) {
0361 edm::ParameterSetDescription desc;
0362 desc.add<std::vector<edm::InputTag>>(
0363 "tracksterCollections", {edm::InputTag("ticlTrackstersCLUE3DHigh"), edm::InputTag("ticlTrackstersLinks")});
0364 desc.add<std::vector<edm::InputTag>>(
0365 "simTracksterCollections", {edm::InputTag("ticlSimTracksters"), edm::InputTag("ticlSimTracksters", "fromCPs")});
0366 desc.add<std::vector<edm::InputTag>>("hits",
0367 {edm::InputTag("HGCalRecHit", "HGCEERecHits"),
0368 edm::InputTag("HGCalRecHit", "HGCHEFRecHits"),
0369 edm::InputTag("HGCalRecHit", "HGCHEBRecHits")});
0370 desc.add<edm::InputTag>("hitToSimClusterMap",
0371 edm::InputTag("hitToSimClusterCaloParticleAssociator", "hitToSimClusterMap"));
0372 desc.add<edm::InputTag>("hitToCaloParticleMap",
0373 edm::InputTag("hitToSimClusterCaloParticleAssociator", "hitToCaloParticleMap"));
0374 desc.add<edm::InputTag>("caloParticles", edm::InputTag("mix", "MergedCaloTruth"));
0375
0376 descriptions.add("AllTracksterToSimTracksterAssociatorsByHitsProducer", desc);
0377 }
0378
0379
0380 DEFINE_FWK_MODULE(AllTracksterToSimTracksterAssociatorsByHitsProducer);