19 : recoTracksterCollectionToken_(
21 simTracksterCollectionToken_(
23 simTracksterFromCPCollectionToken_(
25 hitToTracksterMapToken_(
35 tracksterToHitMapToken_(
42 auto hitsTags =
pset.getParameter<std::vector<edm::InputTag>>(
"hits");
43 for (
const auto&
tag : hitsTags) {
48 "tracksterToSimTracksterMap");
51 "simTracksterToTracksterMap");
54 "tracksterToSimTracksterFromCPMap");
57 "simTracksterFromCPToTracksterMap");
69 const auto& recoTracksters = *recoTrackstersHandle;
73 const auto& simTracksters = *simTrackstersHandle;
77 const auto& simTrackstersFromCP = *simTrackstersFromCPHandle;
93 const auto& hitToSimClusterMap = *hitToSimClusterMapHandle;
97 const auto& hitToCaloParticleMap = *hitToCaloParticleMapHandle;
121 auto tracksterToSimTracksterMap = std::make_unique<
123 recoTrackstersHandle, simTrackstersHandle,
iEvent);
124 auto tracksterToSimTracksterFromCPMap = std::make_unique<
126 recoTrackstersHandle, simTrackstersFromCPHandle,
iEvent);
128 auto simTracksterToTracksterMap = std::make_unique<
130 simTrackstersHandle, recoTrackstersHandle,
iEvent);
131 auto simTracksterFromCPToTracksterMap = std::make_unique<
133 simTrackstersFromCPHandle, recoTrackstersHandle,
iEvent);
134 for (
unsigned int tracksterIndex = 0; tracksterIndex < recoTracksters.size(); ++tracksterIndex) {
136 float recoToSimScoresDenominator = 0.f;
139 std::vector<unsigned int> associatedSimTracksterIndices;
141 recoTracksterHitsAndFractions.size());
142 std::vector<unsigned int> associatedSimTracksterFromCPIndices;
143 for (
unsigned int i = 0;
i < recoTracksterHitsAndFractions.size(); ++
i) {
144 const auto& [hitIndex, recoFraction] = recoTracksterHitsAndFractions[
i];
145 const auto&
recHit = rechitManager[hitIndex];
146 float squaredRecoFraction = recoFraction * recoFraction;
147 float rechitEnergy =
recHit.energy();
148 float squaredRecHitEnergy = rechitEnergy * rechitEnergy;
149 recoToSimScoresDenominator += squaredRecoFraction * squaredRecHitEnergy;
152 for (
const auto& [simTracksterIndex,
fraction] : hitToSimTracksterVec) {
153 const auto& simTrackster = simTracksters[simTracksterIndex];
154 auto&
seed = simTrackster.seedID();
155 float simFraction = 0;
156 if (
seed == caloParticlesHandle.
id()) {
157 unsigned int caloParticleIndex = simTrackster.seedIndex();
158 auto it = std::find_if(hitToCaloParticleMap[hitIndex].
begin(),
159 hitToCaloParticleMap[hitIndex].
end(),
160 [caloParticleIndex](
const auto& pair) {
return pair.first == caloParticleIndex; });
161 if (
it != hitToCaloParticleMap[hitIndex].
end()) {
162 simFraction =
it->second;
165 unsigned int simClusterIndex = simTracksters[simTracksterIndex].seedIndex();
166 auto it = std::find_if(hitToSimClusterMap[hitIndex].
begin(),
167 hitToSimClusterMap[hitIndex].
end(),
168 [simClusterIndex](
const auto& pair) {
return pair.first == simClusterIndex; });
169 if (
it != hitToSimClusterMap[hitIndex].
end()) {
170 simFraction =
it->second;
173 hitToAssociatedSimTracksterMap.insert(
i, simTracksterIndex, simFraction);
174 associatedSimTracksterIndices.push_back(simTracksterIndex);
179 for (
const auto& [simTracksterIndex, simFraction] : hitToSimTracksterFromCPVec) {
180 unsigned int caloParticleIndex = simTracksters[simTracksterIndex].seedIndex();
181 float caloParticleFraction = 0;
182 auto it = std::find_if(hitToCaloParticleMap[hitIndex].
begin(),
183 hitToCaloParticleMap[hitIndex].
end(),
184 [caloParticleIndex](
const auto& pair) {
return pair.first == caloParticleIndex; });
185 if (
it != hitToCaloParticleMap[hitIndex].
end()) {
186 caloParticleFraction =
it->second;
188 hitToAssociatedSimTracksterFromCPMap.insert(
i, simTracksterIndex, caloParticleFraction);
189 associatedSimTracksterFromCPIndices.push_back(simTracksterIndex);
192 std::sort(associatedSimTracksterIndices.begin(), associatedSimTracksterIndices.end());
193 associatedSimTracksterIndices.erase(
194 std::unique(associatedSimTracksterIndices.begin(), associatedSimTracksterIndices.end()),
195 associatedSimTracksterIndices.end());
197 std::sort(associatedSimTracksterFromCPIndices.begin(), associatedSimTracksterFromCPIndices.end());
198 associatedSimTracksterFromCPIndices.erase(
199 std::unique(associatedSimTracksterFromCPIndices.begin(), associatedSimTracksterFromCPIndices.end()),
200 associatedSimTracksterFromCPIndices.end());
203 for (
unsigned int i = 0;
i < recoTracksterHitsAndFractions.size(); ++
i) {
204 unsigned int hitId = recoTracksterHitsAndFractions[
i].first;
206 for (
unsigned int simTracksterIndex : associatedSimTracksterIndices) {
207 if (std::find_if(simTracksterVec.begin(), simTracksterVec.end(), [simTracksterIndex](
const auto& pair) {
208 return pair.first == simTracksterIndex;
209 }) == simTracksterVec.end()) {
210 hitToAssociatedSimTracksterMap.insert(
i, simTracksterIndex, 0);
215 for (
unsigned int simTracksterIndex : associatedSimTracksterFromCPIndices) {
217 simTracksterFromCPVec.begin(), simTracksterFromCPVec.end(), [simTracksterIndex](
const auto& pair) {
218 return pair.first == simTracksterIndex;
219 }) == simTracksterFromCPVec.end()) {
220 hitToAssociatedSimTracksterFromCPMap.insert(
i, simTracksterIndex, 0);
225 const float invDenominator = 1.f / recoToSimScoresDenominator;
227 for (
unsigned int i = 0;
i < recoTracksterHitsAndFractions.size(); ++
i) {
228 unsigned int hitIndex = recoTracksterHitsAndFractions[
i].first;
229 const auto&
recHit = rechitManager[hitIndex];
230 float recoFraction = recoTracksterHitsAndFractions[
i].second;
231 float squaredRecoFraction = recoFraction * recoFraction;
232 float squaredRecHitEnergy =
recHit.energy() *
recHit.energy();
233 float recoSharedEnergy =
recHit.energy() * recoFraction;
234 const auto& simTracksterVec = hitToAssociatedSimTracksterMap[
i];
235 for (
const auto& [simTracksterIndex, simFraction] : simTracksterVec) {
238 float squaredFraction =
239 std::min(squaredRecoFraction, (recoFraction - simFraction) * (recoFraction - simFraction));
240 float score = invDenominator * squaredFraction * squaredRecHitEnergy;
241 tracksterToSimTracksterMap->insert(recoTracksterRef, simTracksterRef,
sharedEnergy,
score);
244 const auto& simTracksterFromCPVec = hitToAssociatedSimTracksterFromCPMap[
i];
245 for (
const auto& [simTracksterIndex, simFraction] : simTracksterFromCPVec) {
248 float squaredFraction =
249 std::min(squaredRecoFraction, (recoFraction - simFraction) * (recoFraction - simFraction));
250 float score = invDenominator * squaredFraction * squaredRecHitEnergy;
251 tracksterToSimTracksterFromCPMap->insert(recoTracksterRef, simTracksterRef,
sharedEnergy,
score);
257 for (
unsigned int tracksterIndex = 0; tracksterIndex < simTracksters.size(); ++tracksterIndex) {
259 float simToRecoScoresDenominator = 0.f;
262 std::vector<unsigned int> associatedRecoTracksterIndices;
263 const auto& simTrackster = simTracksters[tracksterIndex];
264 auto&
seed = simTrackster.seedID();
265 unsigned int simObjectIndex = simTrackster.seedIndex();
266 bool isSimTracksterFromCP = (
seed == caloParticlesHandle.
id());
267 std::vector<float> simFractions(simTracksterHitsAndFractions.size(), 0.f);
268 for (
unsigned int i = 0;
i < simTracksterHitsAndFractions.size(); ++
i) {
269 const auto& [hitIndex, simTracksterFraction] = simTracksterHitsAndFractions[
i];
271 auto it = isSimTracksterFromCP
272 ? (std::find_if(hitToCaloParticleMap[hitIndex].
begin(),
273 hitToCaloParticleMap[hitIndex].
end(),
274 [simObjectIndex](
const auto& pair) {
return pair.first == simObjectIndex; }))
275 : std::find_if(hitToSimClusterMap[hitIndex].
begin(),
276 hitToSimClusterMap[hitIndex].
end(),
277 [simObjectIndex](
const auto& pair) {
return pair.first == simObjectIndex; });
278 if (
it != hitToCaloParticleMap[hitIndex].
end() and
it != hitToSimClusterMap[hitIndex].
end()) {
279 simFractions[
i] =
it->second;
281 float simFraction = simFractions[
i];
282 const auto&
recHit = rechitManager[hitIndex];
283 float squaredSimFraction = simFraction * simFraction;
284 float squaredRecHitEnergy =
recHit.energy() *
recHit.energy();
285 simToRecoScoresDenominator += squaredSimFraction * squaredRecHitEnergy;
288 for (
const auto& [recoTracksterIndex, recoFraction] : hitToRecoTracksterVec) {
289 hitToAssociatedRecoTracksterMap.insert(
i, recoTracksterIndex, recoFraction);
290 associatedRecoTracksterIndices.push_back(recoTracksterIndex);
294 std::sort(associatedRecoTracksterIndices.begin(), associatedRecoTracksterIndices.end());
295 associatedRecoTracksterIndices.erase(
296 std::unique(associatedRecoTracksterIndices.begin(), associatedRecoTracksterIndices.end()),
297 associatedRecoTracksterIndices.end());
299 for (
unsigned int i = 0;
i < simTracksterHitsAndFractions.size(); ++
i) {
300 unsigned int hitIndex = simTracksterHitsAndFractions[
i].first;
302 for (
unsigned int recoTracksterIndex : associatedRecoTracksterIndices) {
304 hitToRecoTracksterVec.begin(), hitToRecoTracksterVec.end(), [recoTracksterIndex](
const auto& pair) {
305 return pair.first == recoTracksterIndex;
306 }) == hitToRecoTracksterVec.end()) {
307 hitToAssociatedRecoTracksterMap.insert(
i, recoTracksterIndex, 0);
312 const float invDenominator = 1.f / simToRecoScoresDenominator;
314 for (
unsigned int i = 0;
i < simTracksterHitsAndFractions.size(); ++
i) {
315 const auto& [hitIndex, simTracksterFraction] = simTracksterHitsAndFractions[
i];
316 float simFraction = simFractions[
i];
317 const auto&
recHit = rechitManager[hitIndex];
318 float squaredSimFraction = simFraction * simFraction;
319 float squaredRecHitEnergy =
recHit.energy() *
recHit.energy();
320 float simSharedEnergy =
recHit.energy() * simFraction;
322 const auto& hitToRecoTracksterVec = hitToAssociatedRecoTracksterMap[
i];
323 for (
const auto& [recoTracksterIndex, recoFraction] : hitToRecoTracksterVec) {
326 float squaredFraction =
327 std::min(squaredSimFraction, (recoFraction - simFraction) * (recoFraction - simFraction));
328 float score = invDenominator * squaredFraction * squaredRecHitEnergy;
329 simTracksterToTracksterMap->insert(simTracksterRef, recoTracksterRef,
sharedEnergy,
score);
335 for (
unsigned int tracksterIndex = 0; tracksterIndex < simTrackstersFromCP.size(); ++tracksterIndex) {
337 float simToRecoScoresDenominator = 0.f;
340 std::vector<unsigned int> associatedRecoTracksterIndices;
341 std::vector<float> simFractions(simTracksterHitsAndFractions.size(), 0.f);
342 const auto& simTrackster = simTrackstersFromCP[tracksterIndex];
343 unsigned int simObjectIndex = simTrackster.seedIndex();
344 for (
unsigned int i = 0;
i < simTracksterHitsAndFractions.size(); ++
i) {
345 const auto& [hitIndex, simTracksterFraction] = simTracksterHitsAndFractions[
i];
346 auto it = std::find_if(hitToCaloParticleMap[hitIndex].
begin(),
347 hitToCaloParticleMap[hitIndex].
end(),
348 [simObjectIndex](
const auto& pair) {
return pair.first == simObjectIndex; });
349 if (
it != hitToCaloParticleMap[hitIndex].
end()) {
350 simFractions[
i] =
it->second;
352 float simFraction = simFractions[
i];
354 const auto&
recHit = rechitManager[hitIndex];
355 float squaredSimFraction = simFraction * simFraction;
356 float squaredRecHitEnergy =
recHit.energy() *
recHit.energy();
357 simToRecoScoresDenominator += squaredSimFraction * squaredRecHitEnergy;
360 for (
const auto& [recoTracksterIndex, recoFraction] : hitToRecoTracksterVec) {
361 hitToAssociatedRecoTracksterMap.insert(
i, recoTracksterIndex, recoFraction);
362 associatedRecoTracksterIndices.push_back(recoTracksterIndex);
366 std::sort(associatedRecoTracksterIndices.begin(), associatedRecoTracksterIndices.end());
367 associatedRecoTracksterIndices.erase(
368 std::unique(associatedRecoTracksterIndices.begin(), associatedRecoTracksterIndices.end()),
369 associatedRecoTracksterIndices.end());
371 for (
unsigned int i = 0;
i < simTracksterHitsAndFractions.size(); ++
i) {
372 unsigned int hitIndex = simTracksterHitsAndFractions[
i].first;
374 for (
unsigned int recoTracksterIndex : associatedRecoTracksterIndices) {
376 hitToRecoTracksterVec.begin(), hitToRecoTracksterVec.end(), [recoTracksterIndex](
const auto& pair) {
377 return pair.first == recoTracksterIndex;
378 }) == hitToRecoTracksterVec.end()) {
379 hitToAssociatedRecoTracksterMap.insert(
i, recoTracksterIndex, 0.
f);
384 const float invDenominator = 1.f / simToRecoScoresDenominator;
386 for (
unsigned int i = 0;
i < simTracksterHitsAndFractions.size(); ++
i) {
387 const auto& [hitIndex, simTracksterFraction] = simTracksterHitsAndFractions[
i];
388 const auto&
recHit = rechitManager[hitIndex];
389 float simFraction = simFractions[
i];
390 float squaredSimFraction = simFraction * simFraction;
391 float squaredRecHitEnergy =
recHit.energy() *
recHit.energy();
392 float simSharedEnergy =
recHit.energy() * simFraction;
394 const auto& hitToRecoTracksterVec = hitToAssociatedRecoTracksterMap[
i];
395 for (
const auto& [recoTracksterIndex, recoFraction] : hitToRecoTracksterVec) {
398 float squaredFraction =
399 std::min(squaredSimFraction, (recoFraction - simFraction) * (recoFraction - simFraction));
400 float score = invDenominator * squaredFraction * squaredRecHitEnergy;
401 simTracksterFromCPToTracksterMap->insert(simTracksterRef, recoTracksterRef,
sharedEnergy,
score);
405 tracksterToSimTracksterMap->sort(
true);
406 tracksterToSimTracksterFromCPMap->sort(
true);
407 simTracksterToTracksterMap->sort(
true);
408 simTracksterFromCPToTracksterMap->sort(
true);
410 iEvent.put(
std::move(tracksterToSimTracksterMap),
"tracksterToSimTracksterMap");
411 iEvent.put(
std::move(tracksterToSimTracksterFromCPMap),
"tracksterToSimTracksterFromCPMap");
412 iEvent.put(
std::move(simTracksterToTracksterMap),
"simTracksterToTracksterMap");
413 iEvent.put(
std::move(simTracksterFromCPToTracksterMap),
"simTracksterFromCPToTracksterMap");
424 edm::InputTag(
"allHitToTracksterAssociations",
"hitToticlSimTracksters"));
426 edm::InputTag(
"allHitToTracksterAssociations",
"hitToticlSimTrackstersfromCPs"));
428 edm::InputTag(
"hitToSimClusterCaloParticleAssociator",
"hitToSimClusterMap"));
430 edm::InputTag(
"hitToSimClusterCaloParticleAssociator",
"hitToCaloParticleMap"));
433 edm::InputTag(
"allHitToTracksterAssociations",
"ticlSimTrackstersToHit"));
435 edm::InputTag(
"allHitToTracksterAssociations",
"ticlSimTrackstersfromCPsToHit"));
438 desc.add<std::vector<edm::InputTag>>(
"hits",
442 descriptions.
add(
"tracksterToSimTracksterAssociatorByHitsProducer",
desc);
edm::EDGetTokenT< ticl::AssociationMap< ticl::mapWithFraction > > hitToCaloParticleMapToken_
edm::EDGetTokenT< ticl::AssociationMap< ticl::mapWithFraction > > simTracksterToHitMapToken_
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
edm::EDGetTokenT< ticl::AssociationMap< ticl::mapWithFraction > > hitToSimTracksterMapToken_
simTracksterFromCPToHitMap
edm::EDGetTokenT< std::vector< CaloParticle > > caloParticleToken_
TracksterToSimTracksterAssociatorByHitsProducer(const edm::ParameterSet &)
hitToSimTracksterFromCPMap
edm::EDGetTokenT< ticl::AssociationMap< ticl::mapWithFraction > > hitToSimTracksterFromCPMapToken_
edm::EDGetTokenT< std::vector< ticl::Trackster > > simTracksterCollectionToken_
edm::EDGetTokenT< std::vector< ticl::Trackster > > simTracksterFromCPCollectionToken_
std::vector< std::vector< std::pair< unsigned int, float > >> mapWithFraction
std::vector< edm::EDGetTokenT< HGCRecHitCollection > > hitsTokens_
def unique(seq, keepstr=True)
#define DEFINE_FWK_MODULE(type)
edm::EDGetTokenT< ticl::AssociationMap< ticl::mapWithFraction > > simTracksterFromCPToHitMapToken_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
edm::EDGetTokenT< ticl::AssociationMap< ticl::mapWithFraction > > tracksterToHitMapToken_
void add(std::string const &label, ParameterSetDescription const &psetDescription)
void addVector(std::span< const T > vec)
float sharedEnergy(reco::CaloCluster const &clu1, reco::CaloCluster const &clu2, EcalRecHitCollection const &barrelRecHits, EcalRecHitCollection const &endcapRecHits)
edm::EDGetTokenT< ticl::AssociationMap< ticl::mapWithFraction > > hitToSimClusterMapToken_
edm::EDGetTokenT< ticl::AssociationMap< ticl::mapWithFraction > > hitToTracksterMapToken_
edm::EDGetTokenT< std::vector< ticl::Trackster > > recoTracksterCollectionToken_
~TracksterToSimTracksterAssociatorByHitsProducer() override