19 const auto& tracksters = *tCH.
product();
21 const auto& simTracksters = *sTCH.
product();
22 auto nTracksters = tracksters.size();
25 auto nSimTracksters = simTracksters.size();
26 std::vector<size_t> sTIndices;
27 for (
unsigned int stId = 0; stId < nSimTracksters; ++stId) {
29 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl")
30 <<
"Excluding SimTrackster " << stId <<
" witH no vertices!" << std::endl;
33 sTIndices.emplace_back(stId);
35 nSimTracksters = sTIndices.size();
41 tssInSimTrackster.resize(nSimTracksters);
42 for (
unsigned int i = 0;
i < nSimTracksters; ++
i) {
43 tssInSimTrackster[
i].simTracksterId =
i;
44 tssInSimTrackster[
i].energy = 0.f;
45 tssInSimTrackster[
i].lcs_and_fractions.clear();
49 std::unordered_map<int, std::vector<hgcal::lcInfoInTrackster>> lcToSimTracksterId_Map;
50 for (
const auto& stId : sTIndices) {
51 const auto& lcs = simTracksters[stId].vertices();
53 for (
const auto& lcId : lcs) {
54 const auto fraction = 1.f / simTracksters[stId].vertex_multiplicity(lcInSimTst++);
56 const auto lc_find_it = lcToSimTracksterId_Map.find(lcId);
57 if (lc_find_it == lcToSimTracksterId_Map.end()) {
58 lcToSimTracksterId_Map[lcId] = std::vector<hgcal::lcInfoInTrackster>();
60 lcToSimTracksterId_Map[lcId].emplace_back(stId,
fraction);
63 tssInSimTrackster[stId].lcs_and_fractions.emplace_back(lcId,
fraction);
68 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl")
69 <<
"tssInSimTrackster INFO (Only SimTrackster filled at the moment)" << std::endl;
70 for (
size_t st = 0; st < tssInSimTrackster.size(); ++st) {
71 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl") <<
"For SimTrackster Idx: " << st <<
" we have: " << std::endl;
72 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl")
73 <<
"\tSimTracksterIdx:\t" << tssInSimTrackster[st].simTracksterId << std::endl;
74 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl") <<
"\tEnergy:\t" << tssInSimTrackster[st].energy << std::endl;
75 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl") <<
"\t# of clusters:\t" <<
layerClusters.size() << std::endl;
76 double tot_energy = 0.;
77 for (
auto const& lcaf : tssInSimTrackster[st].lcs_and_fractions) {
78 const auto lcEn = lcaf.second *
layerClusters[lcaf.first].energy();
79 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl")
80 <<
"\tLC/fraction/energy: " << (uint32_t)lcaf.first <<
"/" << lcaf.second <<
"/" << lcEn << std::endl;
83 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl") <<
"\tTot Sum lcaf: " << tot_energy << std::endl;
84 for (
auto const& ts : tssInSimTrackster[st].tracksterIdToEnergyAndScore) {
85 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl")
86 <<
"\ttsIdx/energy/score: " << ts.first <<
"/" << ts.second.first <<
"/" << ts.second.second << std::endl;
90 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl") <<
"lcToSimTracksterId_Map INFO" << std::endl;
91 for (
auto const& lc : lcToSimTracksterId_Map) {
92 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl")
93 <<
"For lcId: " << (uint32_t)lc.first
94 <<
" we have found the following connections with SimTracksters:" << std::endl;
95 for (
auto const& st : lc.second) {
96 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl")
97 <<
"\tSimTrackster Id: " << st.clusterId <<
" with fraction: " << st.fraction
98 <<
" and energy: " << st.fraction *
layerClusters[lc.first].energy() << std::endl;
108 stsInTrackster.resize(nTracksters);
110 for (
unsigned int tsId = 0; tsId < nTracksters; ++tsId) {
111 for (
unsigned int i = 0;
i < tracksters[tsId].vertices().size(); ++
i) {
112 const auto lcId = tracksters[tsId].vertices(
i);
113 const auto lcFractionInTs = 1.f / tracksters[tsId].vertex_multiplicity(
i);
115 const auto lc_find_in_ST = lcToSimTracksterId_Map.find(lcId);
117 if (lc_find_in_ST != lcToSimTracksterId_Map.end()) {
121 for (
const auto& st : lc_find_in_ST->second) {
124 tssInSimTrackster[st.clusterId].tracksterIdToEnergyAndScore[tsId].first +=
127 stsInTrackster[tsId].emplace_back(st.clusterId, 0.f);
134 for (
unsigned int tsId = 0; tsId < nTracksters; ++tsId) {
135 for (
const auto& lcId : tracksters[tsId].
vertices()) {
136 const auto& hits_and_fractions =
layerClusters[lcId].hitsAndFractions();
137 unsigned int numberOfHitsInLC = hits_and_fractions.size();
147 std::vector<int> hitsToSimTracksterId(numberOfHitsInLC);
149 int maxSTId_byNumberOfHits = -1;
151 unsigned int maxSTNumberOfHitsInLC = 0;
153 int maxSTId_byEnergy = -1;
155 float maxEnergySharedLCandST = 0.f;
157 float energyFractionOfLCinST = 0.f;
159 float energyFractionOfSTinLC = 0.f;
160 std::unordered_map<unsigned, unsigned> occurrencesSTinLC;
161 unsigned int numberOfNoiseHitsInLC = 0;
162 std::unordered_map<unsigned, float> STEnergyInLC;
164 const auto lc_find_in_ST = lcToSimTracksterId_Map.find(lcId);
165 for (
unsigned int hitId = 0; hitId < numberOfHitsInLC; hitId++) {
166 const auto rhFraction = hits_and_fractions[hitId].second;
173 if (rhFraction == 0.) {
174 hitsToSimTracksterId[hitId] = -2;
177 if (lc_find_in_ST == lcToSimTracksterId_Map.end()) {
178 hitsToSimTracksterId[hitId] -= 1;
180 auto maxSTEnergyInLC = 0.f;
183 for (
const auto& st : lc_find_in_ST->second) {
184 const auto stId = st.clusterId;
185 STEnergyInLC[stId] += st.fraction *
layerClusters[lcId].energy();
188 if (STEnergyInLC[stId] > maxSTEnergyInLC) {
189 maxSTEnergyInLC = STEnergyInLC[stId];
193 hitsToSimTracksterId[hitId] = maxSTId;
197 for (
const auto&
c : hitsToSimTracksterId) {
199 numberOfNoiseHitsInLC++;
201 occurrencesSTinLC[
c]++;
205 for (
const auto&
c : occurrencesSTinLC) {
206 if (
c.second > maxSTNumberOfHitsInLC) {
207 maxSTId_byNumberOfHits =
c.first;
208 maxSTNumberOfHitsInLC =
c.second;
212 for (
const auto&
c : STEnergyInLC) {
213 if (
c.second > maxEnergySharedLCandST) {
214 maxSTId_byEnergy =
c.first;
215 maxEnergySharedLCandST =
c.second;
219 float totalSTEnergyOnLayer = 0.f;
220 if (maxSTId_byEnergy >= 0) {
221 totalSTEnergyOnLayer = tssInSimTrackster[maxSTId_byEnergy].energy;
222 energyFractionOfSTinLC = maxEnergySharedLCandST / totalSTEnergyOnLayer;
223 if (tracksters[tsId].raw_energy() > 0.
f) {
224 energyFractionOfLCinST = maxEnergySharedLCandST / tracksters[tsId].raw_energy();
228 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl")
229 << std::setw(12) <<
"TracksterID:\t" << std::setw(12) <<
"layerCluster\t" << std::setw(10) <<
"lc energy\t"
230 << std::setw(5) <<
"nhits\t" << std::setw(12) <<
"noise hits\t" << std::setw(22) <<
"maxSTId_byNumberOfHits\t"
231 << std::setw(8) <<
"nhitsST\t" << std::setw(13) <<
"maxSTId_byEnergy\t" << std::setw(20)
232 <<
"maxEnergySharedLCandST\t" << std::setw(22) <<
"totalSTEnergyOnLayer\t" << std::setw(22)
233 <<
"energyFractionOfLCinST\t" << std::setw(25) <<
"energyFractionOfSTinLC\t"
235 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl")
236 << std::setw(12) << tsId <<
"\t" << std::setw(12) << lcId <<
"\t" << std::setw(10)
237 << tracksters[tsId].raw_energy() <<
"\t" << std::setw(5) << numberOfHitsInLC <<
"\t" << std::setw(12)
238 << numberOfNoiseHitsInLC <<
"\t" << std::setw(22) << maxSTId_byNumberOfHits <<
"\t" << std::setw(8)
239 << maxSTNumberOfHitsInLC <<
"\t" << std::setw(13) << maxSTId_byEnergy <<
"\t" << std::setw(20)
240 << maxEnergySharedLCandST <<
"\t" << std::setw(22) << totalSTEnergyOnLayer <<
"\t" << std::setw(22)
241 << energyFractionOfLCinST <<
"\t" << std::setw(25) << energyFractionOfSTinLC <<
"\n";
245 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl")
246 <<
"Improved tssInSimTrackster INFO (Now containing the linked tracksters id and energy - score still empty)"
248 for (
size_t st = 0; st < tssInSimTrackster.size(); ++st) {
249 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl") <<
"For SimTrackster Idx: " << st <<
" we have: " << std::endl;
250 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl")
251 <<
" SimTracksterIdx: " << tssInSimTrackster[st].simTracksterId << std::endl;
252 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl") <<
"\tEnergy:\t" << tssInSimTrackster[st].energy << std::endl;
253 double tot_energy = 0.;
254 for (
auto const& lcaf : tssInSimTrackster[st].lcs_and_fractions) {
255 const auto lcEn = lcaf.second *
layerClusters[lcaf.first].energy();
256 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl")
257 <<
"\tLC/fraction/energy: " << (uint32_t)lcaf.first <<
"/" << lcaf.second <<
"/" << lcEn << std::endl;
260 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl") <<
"\tTot Sum lcaf: " << tot_energy << std::endl;
261 for (
auto const& ts : tssInSimTrackster[st].tracksterIdToEnergyAndScore) {
262 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl")
263 <<
"\ttsIdx/energy/score: " << ts.first <<
"/" << ts.second.first <<
"/" << ts.second.second << std::endl;
267 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl") <<
"Improved lcToSimTracksterId_Map INFO" << std::endl;
268 for (
auto const& lc : lcToSimTracksterId_Map) {
269 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl")
270 <<
"For lcId: " << (uint32_t)lc.first
271 <<
" we have found the following connections with SimTracksters:" << std::endl;
272 for (
auto const& st : lc.second) {
273 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl")
274 <<
" SimTrackster Id: " << st.clusterId <<
" with fraction: " << st.fraction
275 <<
" and energy: " << st.fraction *
layerClusters[lc.first].energy() << std::endl;
282 for (
unsigned int tsId = 0; tsId < nTracksters; ++tsId) {
285 std::sort(stsInTrackster[tsId].begin(), stsInTrackster[tsId].
end());
287 stsInTrackster[tsId].erase(
last, stsInTrackster[tsId].
end());
291 if (tracksters[tsId].raw_energy() == 0. && !stsInTrackster[tsId].
empty()) {
292 for (
auto& stPair : stsInTrackster[tsId]) {
294 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl")
295 <<
"TracksterId:\t " << tsId <<
"\tST id:\t" << stPair.first <<
"\tscore\t " << stPair.second <<
"\n";
300 float invTracksterEnergyWeight = 0.f;
301 for (
unsigned int i = 0;
i < tracksters[tsId].vertices().size(); ++
i) {
302 const auto lcId = tracksters[tsId].vertices(
i);
303 const auto lcFractionInTs = 1.f / tracksters[tsId].vertex_multiplicity(
i);
305 const auto& hits_and_fractions =
layerClusters[lcId].hitsAndFractions();
307 for (
auto const& haf : hits_and_fractions) {
308 invTracksterEnergyWeight +=
std::pow(lcFractionInTs * haf.second *
hitMap_->at(haf.first)->energy(), 2);
311 invTracksterEnergyWeight = 1.f / invTracksterEnergyWeight;
313 for (
unsigned int i = 0;
i < tracksters[tsId].vertices().size(); ++
i) {
314 const auto lcId = tracksters[tsId].vertices(
i);
315 const auto lcFractionInTs = 1.f / tracksters[tsId].vertex_multiplicity(
i);
317 const bool lcWithST = (lcToSimTracksterId_Map.find(lcId) != lcToSimTracksterId_Map.end());
321 for (
auto& stPair : stsInTrackster[tsId]) {
322 float stFraction = 0.f;
324 const auto findLCIt =
std::find(lcToSimTracksterId_Map[lcId].begin(),
325 lcToSimTracksterId_Map[lcId].
end(),
327 if (findLCIt != lcToSimTracksterId_Map[lcId].
end()) {
328 stFraction = findLCIt->fraction;
332 (lcFractionInTs - stFraction) * (lcFractionInTs - stFraction) * lcEnergyWeight * invTracksterEnergyWeight;
334 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl")
335 <<
"lcId:\t" << (uint32_t)lcId <<
"\ttracksterId:\t" << tsId <<
"\ttsFraction,stFraction:\t"
336 << lcFractionInTs <<
", " << stFraction <<
"\tlcEnergyWeight:\t" << lcEnergyWeight <<
"\tcurrent score:\t"
337 << stPair.second <<
"\tinvTracksterEnergyWeight:\t" << invTracksterEnergyWeight <<
"\n";
343 if (stsInTrackster[tsId].
empty())
344 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl") <<
"trackster Id:\t" << tsId <<
"\tST id:\t-1"
350 for (
const auto& stId : sTIndices) {
351 float invSTEnergyWeight = 0.f;
353 const unsigned int STNumberOfLCs = tssInSimTrackster[stId].lcs_and_fractions.size();
354 if (STNumberOfLCs == 0)
358 int tsWithMaxEnergyInST = -1;
360 float maxEnergyTSinST = 0.f;
361 float STenergy = tssInSimTrackster[stId].energy;
363 float STEnergyFractionInTS = 0.f;
364 for (
const auto& ts : tssInSimTrackster[stId].tracksterIdToEnergyAndScore) {
365 if (ts.second.first > maxEnergyTSinST) {
366 maxEnergyTSinST = ts.second.first;
367 tsWithMaxEnergyInST = ts.first;
371 STEnergyFractionInTS = maxEnergyTSinST / STenergy;
373 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl")
374 << std::setw(12) <<
"simTrackster\t" << std::setw(15) <<
"st total energy\t" << std::setw(15)
375 <<
"stEnergyOnLayer\t" << std::setw(14) <<
"STNhitsOnLayer\t" << std::setw(18) <<
"tsWithMaxEnergyInST\t"
376 << std::setw(15) <<
"maxEnergyTSinST\t" << std::setw(20) <<
"STEnergyFractionInTS"
378 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl")
379 << std::setw(12) << stId <<
"\t" << std::setw(15) << simTracksters[stId].raw_energy() <<
"\t" << std::setw(15)
380 << STenergy <<
"\t" << std::setw(14) << STNumberOfLCs <<
"\t" << std::setw(18) << tsWithMaxEnergyInST <<
"\t"
381 << std::setw(15) << maxEnergyTSinST <<
"\t" << std::setw(20) << STEnergyFractionInTS <<
"\n";
385 for (
auto const& lcaf : tssInSimTrackster[stId].lcs_and_fractions) {
388 invSTEnergyWeight = 1.f / invSTEnergyWeight;
390 for (
unsigned int i = 0;
i < STNumberOfLCs; ++
i) {
391 auto& st_lcId = tssInSimTrackster[stId].lcs_and_fractions[
i].first;
392 auto& st_lcFraction = tssInSimTrackster[stId].lcs_and_fractions[
i].second;
394 if (st_lcFraction == 0.
f)
397 for (
auto& tsPair : tssInSimTrackster[stId].tracksterIdToEnergyAndScore) {
398 unsigned int tsId = tsPair.first;
400 for (
unsigned int i = 0;
i < tracksters[tsId].vertices().size(); ++
i) {
401 const auto tsFraction = 1.f / tracksters[tsId].vertex_multiplicity(
i);
403 tsPair.second.second +=
404 (tsFraction - st_lcFraction) * (tsFraction - st_lcFraction) * lcEnergyWeight * invSTEnergyWeight;
406 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl")
407 <<
"STLCId:\t" << (uint32_t)st_lcId <<
"\tTracksterId:\t" << tsId <<
"\t"
408 <<
"tsFraction, stFraction:\t" << tsFraction <<
", " << st_lcFraction <<
"\t"
409 <<
"lcEnergyWeight:\t" << lcEnergyWeight <<
"\t"
410 <<
"current score:\t" << tsPair.second.second <<
"\t"
411 <<
"invSTEnergyWeight:\t" << invSTEnergyWeight <<
"\n";
417 if (tssInSimTrackster[stId].tracksterIdToEnergyAndScore.empty())
418 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl") <<
"ST Id:\t" << stId <<
"\tTS id:\t-1 "
421 for (
const auto& tsPair : tssInSimTrackster[stId].tracksterIdToEnergyAndScore) {
422 LogDebug(
"TSToSimTSAssociatorByEnergyScoreImpl")
423 <<
"ST Id: \t" << stId <<
"\t TS id: \t" << tsPair.first <<
"\t score \t" << tsPair.second.second
424 <<
"\t shared energy:\t" << tsPair.second.first <<
"\t shared energy fraction:\t"
425 << (tsPair.second.first / STenergy) <<
"\n";
429 return {stsInTrackster, tssInSimTrackster};