31 std::vector<std::pair<std::string, edm::EDGetTokenT<ticl::AssociationMap<ticl::mapWithFraction>>>>
33 std::vector<std::pair<std::string, edm::EDGetTokenT<ticl::AssociationMap<ticl::mapWithFraction>>>>
36 std::vector<std::pair<std::string, edm::EDGetTokenT<ticl::AssociationMap<ticl::mapWithFraction>>>>
38 std::vector<std::pair<std::string, edm::EDGetTokenT<ticl::AssociationMap<ticl::mapWithFraction>>>>
41 std::vector<edm::EDGetTokenT<HGCRecHitCollection>>
hitsTokens_;
57 if (
tag.instance() !=
"") {
72 if (
tag.instance() !=
"") {
85 auto hitsTags =
pset.getParameter<std::vector<edm::InputTag>>(
"hits");
86 for (
const auto&
tag : hitsTags) {
93 std::string instanceLabel = tracksterToken.first +
"To" + simTracksterToken.first;
95 std::vector<ticl::Trackster>,
96 std::vector<ticl::Trackster>>>(instanceLabel);
97 std::string reverseInstanceLabel = simTracksterToken.first +
"To" + tracksterToken.first;
99 std::vector<ticl::Trackster>,
100 std::vector<ticl::Trackster>>>(reverseInstanceLabel);
119 const auto& hitToSimClusterMap = *hitToSimClusterMapHandle;
123 const auto& hitToCaloParticleMap = *hitToCaloParticleMapHandle;
130 iEvent.getByToken(tracksterToken.second, recoTrackstersHandle);
131 const auto& recoTracksters = *recoTrackstersHandle;
135 auto tracksterMapTokenIter = std::find_if(
137 return pair.first == tracksterToken.first;
140 iEvent.getByToken(tracksterMapTokenIter->second, hitToTracksterMapHandle);
146 auto tracksterToHitMapTokenIter = std::find_if(
148 return pair.first == tracksterToken.first;
151 iEvent.getByToken(tracksterToHitMapTokenIter->second, tracksterToHitMapHandle);
157 iEvent.getByToken(simTracksterToken.second, simTrackstersHandle);
158 const auto& simTracksters = *simTrackstersHandle;
162 auto simTracksterMapTokenIter =
165 [&simTracksterToken](
const auto& pair) {
return pair.first == simTracksterToken.first; });
167 iEvent.getByToken(simTracksterMapTokenIter->second, hitToSimTracksterMapHandle);
173 auto simTracksterToHitMapTokenIter =
176 [&simTracksterToken](
const auto& pair) {
return pair.first == simTracksterToken.first; });
178 iEvent.getByToken(simTracksterToHitMapTokenIter->second, simTracksterToHitMapHandle);
183 auto tracksterToSimTracksterMap = std::make_unique<
185 recoTrackstersHandle, simTrackstersHandle,
iEvent);
186 auto simTracksterToTracksterMap = std::make_unique<
188 simTrackstersHandle, recoTrackstersHandle,
iEvent);
190 for (
unsigned int tracksterIndex = 0; tracksterIndex < recoTracksters.size(); ++tracksterIndex) {
192 float recoToSimScoresDenominator = 0.f;
195 recoTracksterHitsAndFractions.size());
196 std::vector<unsigned int> associatedSimTracksterIndices;
197 for (
unsigned int i = 0;
i < recoTracksterHitsAndFractions.size(); ++
i) {
198 const auto& [hitIndex, recoFraction] = recoTracksterHitsAndFractions[
i];
199 const auto&
recHit = rechitManager[hitIndex];
200 float squaredRecoFraction = recoFraction * recoFraction;
201 float rechitEnergy =
recHit.energy();
202 float squaredRecHitEnergy = rechitEnergy * rechitEnergy;
203 recoToSimScoresDenominator += squaredRecoFraction * squaredRecHitEnergy;
206 for (
const auto& [simTracksterIndex,
fraction] : hitToSimTracksterVec) {
207 const auto& simTrackster = simTracksters[simTracksterIndex];
208 auto&
seed = simTrackster.seedID();
209 float simFraction = 0;
210 if (
seed == caloParticlesHandle.
id()) {
211 unsigned int caloParticleIndex = simTrackster.seedIndex();
212 auto it = std::find_if(hitToCaloParticleMap[hitIndex].
begin(),
213 hitToCaloParticleMap[hitIndex].
end(),
214 [caloParticleIndex](
const auto& pair) {
return pair.first == caloParticleIndex; });
215 if (
it != hitToCaloParticleMap[hitIndex].
end()) {
216 simFraction =
it->second;
219 unsigned int simClusterIndex = simTracksters[simTracksterIndex].seedIndex();
220 auto it = std::find_if(hitToSimClusterMap[hitIndex].
begin(),
221 hitToSimClusterMap[hitIndex].
end(),
222 [simClusterIndex](
const auto& pair) {
return pair.first == simClusterIndex; });
223 if (
it != hitToSimClusterMap[hitIndex].
end()) {
224 simFraction =
it->second;
227 hitToAssociatedSimTracksterMap.insert(
i, simTracksterIndex, simFraction);
228 associatedSimTracksterIndices.push_back(simTracksterIndex);
231 std::sort(associatedSimTracksterIndices.begin(), associatedSimTracksterIndices.end());
232 associatedSimTracksterIndices.erase(
233 std::unique(associatedSimTracksterIndices.begin(), associatedSimTracksterIndices.end()),
234 associatedSimTracksterIndices.end());
237 for (
unsigned int i = 0;
i < recoTracksterHitsAndFractions.size(); ++
i) {
238 unsigned int hitId = recoTracksterHitsAndFractions[
i].first;
240 for (
unsigned int simTracksterIndex : associatedSimTracksterIndices) {
241 if (std::find_if(simTracksterVec.begin(), simTracksterVec.end(), [simTracksterIndex](
const auto& pair) {
242 return pair.first == simTracksterIndex;
243 }) == simTracksterVec.end()) {
244 hitToAssociatedSimTracksterMap.insert(
i, simTracksterIndex, 0);
249 const float invDenominator = 1.f / recoToSimScoresDenominator;
251 for (
unsigned int i = 0;
i < recoTracksterHitsAndFractions.size(); ++
i) {
252 unsigned int hitIndex = recoTracksterHitsAndFractions[
i].first;
253 const auto&
recHit = rechitManager[hitIndex];
254 float recoFraction = recoTracksterHitsAndFractions[
i].second;
255 float squaredRecoFraction = recoFraction * recoFraction;
256 float squaredRecHitEnergy =
recHit.energy() *
recHit.energy();
257 float recoSharedEnergy =
recHit.energy() * recoFraction;
258 const auto& simTracksterVec = hitToAssociatedSimTracksterMap[
i];
259 for (
const auto& [simTracksterIndex, simFraction] : simTracksterVec) {
262 float squaredFraction =
263 std::min(squaredRecoFraction, (recoFraction - simFraction) * (recoFraction - simFraction));
264 float score = invDenominator * squaredFraction * squaredRecHitEnergy;
265 tracksterToSimTracksterMap->insert(recoTracksterRef, simTracksterRef,
sharedEnergy,
score);
271 for (
unsigned int tracksterIndex = 0; tracksterIndex < simTracksters.size(); ++tracksterIndex) {
273 float simToRecoScoresDenominator = 0.f;
276 simTracksterHitsAndFractions.size());
277 std::vector<unsigned int> associatedRecoTracksterIndices;
278 const auto& simTrackster = simTracksters[tracksterIndex];
279 auto&
seed = simTrackster.seedID();
280 unsigned int simObjectIndex = simTrackster.seedIndex();
281 bool isSimTracksterFromCP = (
seed == caloParticlesHandle.
id());
282 std::vector<float> simFractions(simTracksterHitsAndFractions.size(), 0.f);
283 for (
unsigned int i = 0;
i < simTracksterHitsAndFractions.size(); ++
i) {
284 const auto& [hitIndex, simTracksterFraction] = simTracksterHitsAndFractions[
i];
286 auto it = isSimTracksterFromCP
287 ? (std::find_if(hitToCaloParticleMap[hitIndex].
begin(),
288 hitToCaloParticleMap[hitIndex].
end(),
289 [simObjectIndex](
const auto& pair) {
return pair.first == simObjectIndex; }))
290 : std::find_if(hitToSimClusterMap[hitIndex].
begin(),
291 hitToSimClusterMap[hitIndex].
end(),
292 [simObjectIndex](
const auto& pair) {
return pair.first == simObjectIndex; });
293 if (
it != hitToCaloParticleMap[hitIndex].
end() and
it != hitToSimClusterMap[hitIndex].
end()) {
294 simFractions[
i] =
it->second;
296 float simFraction = simFractions[
i];
297 const auto&
recHit = rechitManager[hitIndex];
298 float squaredSimFraction = simFraction * simFraction;
299 float squaredRecHitEnergy =
recHit.energy() *
recHit.energy();
300 simToRecoScoresDenominator += squaredSimFraction * squaredRecHitEnergy;
303 for (
const auto& [recoTracksterIndex, recoFraction] : hitToRecoTracksterVec) {
304 hitToAssociatedRecoTracksterMap.insert(
i, recoTracksterIndex, recoFraction);
305 associatedRecoTracksterIndices.push_back(recoTracksterIndex);
309 std::sort(associatedRecoTracksterIndices.begin(), associatedRecoTracksterIndices.end());
310 associatedRecoTracksterIndices.erase(
311 std::unique(associatedRecoTracksterIndices.begin(), associatedRecoTracksterIndices.end()),
312 associatedRecoTracksterIndices.end());
314 for (
unsigned int i = 0;
i < simTracksterHitsAndFractions.size(); ++
i) {
315 unsigned int hitIndex = simTracksterHitsAndFractions[
i].first;
317 for (
unsigned int recoTracksterIndex : associatedRecoTracksterIndices) {
319 hitToRecoTracksterVec.begin(), hitToRecoTracksterVec.end(), [recoTracksterIndex](
const auto& pair) {
320 return pair.first == recoTracksterIndex;
321 }) == hitToRecoTracksterVec.end()) {
322 hitToAssociatedRecoTracksterMap.insert(
i, recoTracksterIndex, 0);
327 const float invDenominator = 1.f / simToRecoScoresDenominator;
329 for (
unsigned int i = 0;
i < simTracksterHitsAndFractions.size(); ++
i) {
330 const auto& [hitIndex, simTracksterFraction] = simTracksterHitsAndFractions[
i];
331 float simFraction = simFractions[
i];
332 const auto&
recHit = rechitManager[hitIndex];
333 float squaredSimFraction = simFraction * simFraction;
334 float squaredRecHitEnergy =
recHit.energy() *
recHit.energy();
335 float simSharedEnergy =
recHit.energy() * simFraction;
337 const auto& hitToRecoTracksterVec = hitToAssociatedRecoTracksterMap[
i];
338 for (
const auto& [recoTracksterIndex, recoFraction] : hitToRecoTracksterVec) {
341 float squaredFraction =
342 std::min(squaredSimFraction, (recoFraction - simFraction) * (recoFraction - simFraction));
343 float score = invDenominator * squaredFraction * squaredRecHitEnergy;
344 simTracksterToTracksterMap->insert(simTracksterRef, recoTracksterRef,
sharedEnergy,
score);
349 tracksterToSimTracksterMap->sort(
true);
350 simTracksterToTracksterMap->sort(
true);
353 iEvent.put(
std::move(tracksterToSimTracksterMap), tracksterToken.first +
"To" + simTracksterToken.first);
354 iEvent.put(
std::move(simTracksterToTracksterMap), simTracksterToken.first +
"To" + tracksterToken.first);
362 desc.add<std::vector<edm::InputTag>>(
364 desc.add<std::vector<edm::InputTag>>(
366 desc.add<std::vector<edm::InputTag>>(
"hits",
371 edm::InputTag(
"hitToSimClusterCaloParticleAssociator",
"hitToSimClusterMap"));
373 edm::InputTag(
"hitToSimClusterCaloParticleAssociator",
"hitToCaloParticleMap"));
376 descriptions.
add(
"AllTracksterToSimTracksterAssociatorsByHitsProducer",
desc);
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
~AllTracksterToSimTracksterAssociatorsByHitsProducer() override=default
AllTracksterToSimTracksterAssociatorsByHitsProducer(const edm::ParameterSet &)
std::vector< std::pair< std::string, edm::EDGetTokenT< ticl::AssociationMap< ticl::mapWithFraction > > > > hitToTracksterMapTokens_
std::vector< std::pair< std::string, edm::EDGetTokenT< ticl::AssociationMap< ticl::mapWithFraction > > > > simTracksterToHitMapTokens_
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
std::vector< std::vector< std::pair< unsigned int, float > >> mapWithFraction
std::vector< std::pair< std::string, edm::EDGetTokenT< ticl::AssociationMap< ticl::mapWithFraction > > > > tracksterToHitMapTokens_
def unique(seq, keepstr=True)
edm::EDGetTokenT< std::vector< reco::CaloCluster > > layerClustersToken_
#define DEFINE_FWK_MODULE(type)
std::vector< std::pair< std::string, edm::EDGetTokenT< ticl::AssociationMap< ticl::mapWithFraction > > > > hitToSimTracksterMapTokens_
std::vector< std::vector< std::pair< unsigned int, std::pair< float, float > >> > mapWithFractionAndScore
std::vector< std::pair< std::string, edm::EDGetTokenT< std::vector< ticl::Trackster > > > > simTracksterCollectionTokens_
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_
std::vector< std::pair< std::string, edm::EDGetTokenT< std::vector< ticl::Trackster > > > > tracksterCollectionTokens_
edm::EDGetTokenT< ticl::AssociationMap< ticl::mapWithFraction > > hitToCaloParticleMapToken_
std::vector< edm::EDGetTokenT< HGCRecHitCollection > > hitsTokens_
edm::EDGetTokenT< std::vector< CaloParticle > > caloParticleToken_