31 static std::unique_ptr<TrackstersCache> initializeGlobalCache(
const edm::ParameterSet &);
39 void energyRegressionAndID(
const std::vector<reco::CaloCluster> &
layerClusters, std::vector<Trackster> &
result)
const;
40 void printTrackstersDebug(
const std::vector<Trackster> &,
const char *
label)
const;
41 void assignTimeToCandidates(std::vector<TICLCandidate> &resultCandidates)
const;
42 void dumpTrackster(
const Trackster &)
const;
81 static constexpr
int eidNFeatures_ = 3;
95 optimiseAcrossTracksters_(ps.getParameter<
bool>(
"optimiseAcrossTracksters")),
96 eta_bin_window_(ps.getParameter<
int>(
"eta_bin_window")),
97 phi_bin_window_(ps.getParameter<
int>(
"phi_bin_window")),
98 pt_sigma_high_(ps.getParameter<double>(
"pt_sigma_high")),
99 pt_sigma_low_(ps.getParameter<double>(
"pt_sigma_low")),
100 halo_max_distance2_(ps.getParameter<double>(
"halo_max_distance2")),
101 track_min_pt_(ps.getParameter<double>(
"track_min_pt")),
102 track_min_eta_(ps.getParameter<double>(
"track_min_eta")),
103 track_max_eta_(ps.getParameter<double>(
"track_max_eta")),
104 track_max_missing_outerhits_(ps.getParameter<
int>(
"track_max_missing_outerhits")),
105 cosangle_align_(ps.getParameter<double>(
"cosangle_align")),
106 e_over_h_threshold_(ps.getParameter<double>(
"e_over_h_threshold")),
107 pt_neutral_threshold_(ps.getParameter<double>(
"pt_neutral_threshold")),
108 resol_calo_offset_had_(ps.getParameter<double>(
"resol_calo_offset_had")),
109 resol_calo_scale_had_(ps.getParameter<double>(
"resol_calo_scale_had")),
110 resol_calo_offset_em_(ps.getParameter<double>(
"resol_calo_offset_em")),
111 resol_calo_scale_em_(ps.getParameter<double>(
"resol_calo_scale_em")),
112 debug_(ps.getParameter<
bool>(
"debug")),
113 eidInputName_(ps.getParameter<
std::
string>(
"eid_input_name")),
114 eidOutputNameEnergy_(ps.getParameter<
std::
string>(
"eid_output_name_energy")),
115 eidOutputNameId_(ps.getParameter<
std::
string>(
"eid_output_name_id")),
116 eidMinClusterEnergy_(ps.getParameter<double>(
"eid_min_cluster_energy")),
117 eidNLayers_(ps.getParameter<
int>(
"eid_n_layers")),
118 eidNClusters_(ps.getParameter<
int>(
"eid_n_clusters")),
119 eidSession_(nullptr) {
122 if (trackstersCache ==
nullptr || trackstersCache->
eidGraphDef ==
nullptr) {
124 <<
"TrackstersMergeProducer received an empty graph definition from the global cache";
128 produces<std::vector<Trackster>>();
129 produces<std::vector<TICLCandidate>>();
133 const std::vector<Trackster> &tracksters,
136 for (
auto const &
t : tracksters) {
137 tracksterTile.
fill(tracksterIteration,
t.barycenter().eta(),
t.barycenter().phi(), tracksterId);
138 LogDebug(
"TrackstersMergeProducer") <<
"Adding tracksterId: " << tracksterId <<
" into bin [eta,phi]: [ "
139 << tracksterTile[tracksterIteration].etaBin(
t.barycenter().eta()) <<
", "
140 << tracksterTile[tracksterIteration].
phiBin(
t.barycenter().phi())
141 <<
"] for iteration: " << tracksterIteration << std::endl;
148 auto e_over_h = (
t.raw_em_pt() / ((
t.raw_pt() -
t.raw_em_pt()) != 0. ? (
t.raw_pt() -
t.raw_em_pt()) : 1.));
150 <<
"\nTrackster raw_pt: " <<
t.raw_pt() <<
" raw_em_pt: " <<
t.raw_em_pt() <<
" eoh: " << e_over_h
151 <<
" barycenter: " <<
t.barycenter() <<
" eta,phi (baricenter): " <<
t.barycenter().eta() <<
", "
152 <<
t.barycenter().phi() <<
" eta,phi (eigen): " <<
t.eigenvectors(0).eta() <<
", " <<
t.eigenvectors(0).phi()
153 <<
" pt(eigen): " <<
std::sqrt(
t.eigenvectors(0).Unit().perp2()) *
t.raw_energy() <<
" seedID: " <<
t.seedID()
154 <<
" seedIndex: " <<
t.seedIndex() <<
" size: " <<
t.vertices().size() <<
" average usage: "
155 << (std::accumulate(std::begin(
t.vertex_multiplicity()),
std::end(
t.vertex_multiplicity()), 0.) /
156 (
float)
t.vertex_multiplicity().size())
157 <<
" raw_energy: " <<
t.raw_energy() <<
" regressed energy: " <<
t.regressed_energy()
158 <<
" probs(ga/e/mu/np/cp/nh/am/unk): ";
159 for (
auto const &
p :
t.id_probabilities()) {
162 LogDebug(
"TrackstersMergeProducer") <<
" sigmas: ";
163 for (
auto const &
s :
t.sigmas()) {
164 LogDebug(
"TrackstersMergeProducer") <<
s <<
" ";
166 LogDebug(
"TrackstersMergeProducer") << std::endl;
172 auto resultTrackstersMerged = std::make_unique<std::vector<Trackster>>();
173 auto resultCandidates = std::make_unique<std::vector<TICLCandidate>>();
176 std::vector<bool> usedTrackstersMerged;
177 std::vector<int> indexInMergedCollTRKEM;
178 std::vector<int> indexInMergedCollEM;
179 std::vector<int> indexInMergedCollTRK;
180 std::vector<int> indexInMergedCollHAD;
181 std::vector<bool> usedSeeds;
184 std::map<int, std::vector<std::pair<int, TracksterIterIndex>>> seedToTracksterAssociator;
187 const auto &
tracks = *track_h;
195 const auto &layerClustersTimes = *clustersTime_h;
199 const auto &trackstersEM = *trackstersem_h;
203 const auto &trackstersTRKEM = *tracksterstrkem_h;
207 const auto &trackstersTRK = *tracksterstrk_h;
211 const auto &trackstersHAD = *trackstershad_h;
216 usedSeeds.resize(
tracks.size(),
false);
218 fillTile(tracksterTile, trackstersTRKEM, TracksterIterIndex::TRKEM);
219 fillTile(tracksterTile, trackstersEM, TracksterIterIndex::EM);
220 fillTile(tracksterTile, trackstersTRK, TracksterIterIndex::TRKHAD);
221 fillTile(tracksterTile, trackstersHAD, TracksterIterIndex::HAD);
223 auto totalNumberOfTracksters =
224 trackstersTRKEM.size() + trackstersTRK.size() + trackstersEM.size() + trackstersHAD.size();
225 resultTrackstersMerged->reserve(totalNumberOfTracksters);
226 usedTrackstersMerged.resize(totalNumberOfTracksters,
false);
227 indexInMergedCollTRKEM.reserve(trackstersTRKEM.size());
228 indexInMergedCollEM.reserve(trackstersEM.size());
229 indexInMergedCollTRK.reserve(trackstersTRK.size());
230 indexInMergedCollHAD.reserve(trackstersHAD.size());
239 for (
auto const &
t : trackstersTRKEM) {
240 indexInMergedCollTRKEM.push_back(resultTrackstersMerged->size());
241 seedToTracksterAssociator[
t.seedIndex()].emplace_back(resultTrackstersMerged->size(), TracksterIterIndex::TRKEM);
242 resultTrackstersMerged->push_back(
t);
245 for (
auto const &
t : trackstersEM) {
246 indexInMergedCollEM.push_back(resultTrackstersMerged->size());
247 resultTrackstersMerged->push_back(
t);
250 for (
auto const &
t : trackstersTRK) {
251 indexInMergedCollTRK.push_back(resultTrackstersMerged->size());
252 seedToTracksterAssociator[
t.seedIndex()].emplace_back(resultTrackstersMerged->size(), TracksterIterIndex::TRKHAD);
253 resultTrackstersMerged->push_back(
t);
256 for (
auto const &
t : trackstersHAD) {
257 indexInMergedCollHAD.push_back(resultTrackstersMerged->size());
258 resultTrackstersMerged->push_back(
t);
269 auto trackstersMergedHandle = evt.
put(
std::move(resultTrackstersMerged));
275 for (
unsigned i = 0;
i < trackstersEM.size(); ++
i) {
276 auto mergedIdx = indexInMergedCollEM[
i];
277 usedTrackstersMerged[mergedIdx] =
true;
278 const auto &
t = trackstersEM[
i];
285 t.raw_energy() *
t.barycenter().unit().y(),
286 t.raw_energy() *
t.barycenter().unit().z(),
289 resultCandidates->push_back(tmpCandidate);
293 constexpr
double mpion = 0.13957;
294 constexpr
float mpion2 = mpion * mpion;
295 for (
unsigned i = 0;
i < trackstersHAD.size(); ++
i) {
296 auto mergedIdx = indexInMergedCollHAD[
i];
297 usedTrackstersMerged[mergedIdx] =
true;
298 const auto &
t = trackstersHAD[
i];
304 float momentum =
std::sqrt(
t.raw_energy() *
t.raw_energy() - mpion2);
306 momentum *
t.barycenter().unit().y(),
307 momentum *
t.barycenter().unit().z(),
310 resultCandidates->push_back(tmpCandidate);
314 for (
unsigned i = 0;
i < trackstersTRKEM.size(); ++
i) {
315 auto mergedIdx = indexInMergedCollTRKEM[
i];
316 if (!usedTrackstersMerged[mergedIdx]) {
317 const auto &
t = trackstersTRKEM[
i];
318 auto trackIdx =
t.seedIndex();
320 if (!usedSeeds[trackIdx] and
t.raw_energy() > 0) {
321 usedSeeds[trackIdx] =
true;
322 usedTrackstersMerged[mergedIdx] =
true;
324 std::vector<int> trackstersTRKwithSameSeed;
325 std::vector<int> trackstersTRKEMwithSameSeed;
327 for (
const auto &tracksterIterationPair : seedToTracksterAssociator[trackIdx]) {
328 if (tracksterIterationPair.first != mergedIdx and !usedTrackstersMerged[tracksterIterationPair.first] and
329 trackstersMergedHandle->at(tracksterIterationPair.first).raw_energy() > 0.) {
330 if (tracksterIterationPair.second == TracksterIterIndex::TRKEM) {
331 trackstersTRKEMwithSameSeed.push_back(tracksterIterationPair.first);
332 }
else if (tracksterIterationPair.second == TracksterIterIndex::TRKHAD) {
333 trackstersTRKwithSameSeed.push_back(tracksterIterationPair.first);
338 float tracksterTotalRawPt =
t.raw_pt();
339 std::vector<int> haloTrackstersTRKIdx;
340 bool foundCompatibleTRK =
false;
342 for (
auto otherTracksterIdx : trackstersTRKwithSameSeed) {
343 usedTrackstersMerged[otherTracksterIdx] =
true;
344 tracksterTotalRawPt += trackstersMergedHandle->at(otherTracksterIdx).raw_pt();
347 if ((
t.barycenter() - trackstersMergedHandle->at(otherTracksterIdx).barycenter()).
mag2() <
349 haloTrackstersTRKIdx.push_back(otherTracksterIdx);
352 foundCompatibleTRK =
true;
357 if (trackstersTRKEMwithSameSeed.empty()) {
358 if (foundCompatibleTRK) {
361 double raw_energy =
t.raw_energy();
366 for (
auto otherTracksterIdx : trackstersTRKwithSameSeed) {
368 raw_energy += trackstersMergedHandle->at(otherTracksterIdx).raw_energy();
372 raw_energy *
track.momentum().unit().y(),
373 raw_energy *
track.momentum().unit().z(),
376 resultCandidates->push_back(tmpCandidate);
381 double raw_energy =
t.raw_energy();
384 for (
auto otherTracksterIdx : trackstersTRKwithSameSeed) {
386 raw_energy += trackstersMergedHandle->at(otherTracksterIdx).raw_energy();
392 raw_energy *
track.momentum().unit().y(),
393 raw_energy *
track.momentum().unit().z(),
396 resultCandidates->push_back(tmpCandidate);
401 int closestTrackster = mergedIdx;
403 for (
auto otherTracksterIdx : trackstersTRKEMwithSameSeed) {
404 auto thisPt = tracksterTotalRawPt + trackstersMergedHandle->at(otherTracksterIdx).raw_pt() -
t.raw_pt();
405 closestTrackster =
std::abs(thisPt -
track.pt()) < minPtDiff ? otherTracksterIdx : closestTrackster;
407 usedTrackstersMerged[closestTrackster] =
true;
409 if (foundCompatibleTRK) {
412 double raw_energy = trackstersMergedHandle->at(closestTrackster).raw_energy();
417 for (
auto otherTracksterIdx : trackstersTRKwithSameSeed) {
419 raw_energy += trackstersMergedHandle->at(otherTracksterIdx).raw_energy();
422 float momentum =
std::sqrt(raw_energy * raw_energy - mpion2);
424 momentum *
track.momentum().unit().y(),
425 momentum *
track.momentum().unit().z(),
428 resultCandidates->push_back(tmpCandidate);
433 double raw_energy = trackstersMergedHandle->at(closestTrackster).raw_energy();
437 for (
auto otherTracksterIdx : trackstersTRKwithSameSeed) {
439 raw_energy += trackstersMergedHandle->at(otherTracksterIdx).raw_energy();
444 raw_energy *
track.momentum().unit().y(),
445 raw_energy *
track.momentum().unit().z(),
448 resultCandidates->push_back(tmpCandidate);
451 for (
auto otherTracksterIdx : trackstersTRKEMwithSameSeed) {
452 auto tmpIndex = (otherTracksterIdx != closestTrackster) ? otherTracksterIdx : mergedIdx;
454 const auto &otherTrackster = trackstersMergedHandle->at(tmpIndex);
455 auto gammaEnergy = otherTrackster.raw_energy();
460 gammaEnergy * otherTrackster.barycenter().unit().y(),
461 gammaEnergy * otherTrackster.barycenter().unit().z(),
463 photonCandidate.
setP4(gammaP4);
465 resultCandidates->push_back(photonCandidate);
472 for (
unsigned i = 0;
i < trackstersTRK.size(); ++
i) {
473 auto mergedIdx = indexInMergedCollTRK[
i];
474 const auto &
t = trackstersTRK[
i];
476 if (!usedTrackstersMerged[mergedIdx] and
t.raw_energy() > 0) {
477 auto trackIdx =
t.seedIndex();
479 if (!usedSeeds[trackIdx]) {
480 usedSeeds[trackIdx] =
true;
481 usedTrackstersMerged[mergedIdx] =
true;
488 float momentum =
std::sqrt(
t.raw_energy() *
t.raw_energy() - mpion2);
490 momentum *
track.momentum().unit().y(),
491 momentum *
track.momentum().unit().z(),
494 resultCandidates->push_back(tmpCandidate);
500 if (usedSeeds[
s.index] ==
false) {
510 tmpCandidate.
setP4(p4Polar);
511 resultCandidates->push_back(tmpCandidate);
512 usedSeeds[
s.index] =
true;
517 for (
unsigned i = 0;
i <
tracks.size(); ++
i) {
530 tmpCandidate.
setP4(p4Polar);
531 resultCandidates->push_back(tmpCandidate);
543 std::vector<Trackster> &tracksters)
const {
567 std::vector<int> tracksterIndices;
568 for (
int i = 0;
i < (
int)tracksters.size();
i++) {
573 float sumClusterEnergy = 0.;
579 tracksters[
i].setRegressedEnergy(0.
f);
580 tracksters[
i].zeroProbabilities();
581 tracksterIndices.push_back(
i);
588 int batchSize = (
int)tracksterIndices.size();
589 if (batchSize == 0) {
595 tensorflow::Tensor
input(tensorflow::DT_FLOAT, shape);
597 static constexpr
int inputDimension = 4;
599 std::vector<tensorflow::Tensor>
outputs;
609 for (
int i = 0;
i < batchSize;
i++) {
610 const Trackster &trackster = tracksters[tracksterIndices[
i]];
616 std::vector<int> clusterIndices(trackster.
vertices().size());
618 clusterIndices[
k] =
k;
620 sort(clusterIndices.begin(), clusterIndices.end(), [&
layerClusters, &trackster](
const int &
a,
const int &
b) {
628 for (
const int &
k : clusterIndices) {
665 for (
const int &
i : tracksterIndices) {
666 tracksters[
i].setRegressedEnergy(*(
energy++));
674 float *probs =
outputs[probsIdx].flat<
float>().
data();
676 for (
const int &
i : tracksterIndices) {
677 tracksters[
i].setProbabilities(probs);
678 probs += tracksters[
i].id_probabilities().size();
684 for (
auto &
cand : resultCandidates) {
685 if (
cand.tracksters().size() > 1) {
688 for (
const auto &tr :
cand.tracksters()) {
689 if (tr->timeError() > 0) {
690 auto invTimeESq =
pow(tr->timeError(), -2);
691 time += tr->time() * invTimeESq;
692 timeErr += invTimeESq;
696 timeErr = 1. / timeErr;
707 std::unique_ptr<TrackstersCache>
cache = std::make_unique<TrackstersCache>(
params);
711 if (!graphPath.empty()) {
720 delete cache->eidGraphDef;
721 cache->eidGraphDef =
nullptr;
729 for (
auto const &
t : tracksters) {
731 <<
counter++ <<
" TrackstersMergeProducer (" <<
label <<
") obj barycenter: " <<
t.barycenter()
732 <<
" eta,phi (baricenter): " <<
t.barycenter().eta() <<
", " <<
t.barycenter().phi()
733 <<
" eta,phi (eigen): " <<
t.eigenvectors(0).eta() <<
", " <<
t.eigenvectors(0).phi()
734 <<
" pt(eigen): " <<
std::sqrt(
t.eigenvectors(0).Unit().perp2()) *
t.raw_energy() <<
" seedID: " <<
t.seedID()
735 <<
" seedIndex: " <<
t.seedIndex() <<
" size: " <<
t.vertices().size() <<
" average usage: "
736 << (std::accumulate(std::begin(
t.vertex_multiplicity()),
std::end(
t.vertex_multiplicity()), 0.) /
737 (
float)
t.vertex_multiplicity().size())
738 <<
" raw_energy: " <<
t.raw_energy() <<
" regressed energy: " <<
t.regressed_energy()
739 <<
" probs(ga/e/mu/np/cp/nh/am/unk): ";
740 for (
auto const &
p :
t.id_probabilities()) {
743 LogDebug(
"TrackstersMergeProducer") <<
" sigmas: ";
744 for (
auto const &
s :
t.sigmas()) {
745 LogDebug(
"TrackstersMergeProducer") <<
s <<
" ";
747 LogDebug(
"TrackstersMergeProducer") << std::endl;
761 desc.add<
bool>(
"optimiseAcrossTracksters",
true);
762 desc.add<
int>(
"eta_bin_window", 1);
763 desc.add<
int>(
"phi_bin_window", 1);
764 desc.add<
double>(
"pt_sigma_high", 2.);
765 desc.add<
double>(
"pt_sigma_low", 2.);
766 desc.add<
double>(
"halo_max_distance2", 4.);
767 desc.add<
double>(
"track_min_pt", 1.);
768 desc.add<
double>(
"track_min_eta", 1.48);
769 desc.add<
double>(
"track_max_eta", 3.);
770 desc.add<
int>(
"track_max_missing_outerhits", 5);
771 desc.add<
double>(
"cosangle_align", 0.9945);
772 desc.add<
double>(
"e_over_h_threshold", 1.);
773 desc.add<
double>(
"pt_neutral_threshold", 2.);
774 desc.add<
double>(
"resol_calo_offset_had", 1.5);
775 desc.add<
double>(
"resol_calo_scale_had", 0.15);
776 desc.add<
double>(
"resol_calo_offset_em", 1.5);
777 desc.add<
double>(
"resol_calo_scale_em", 0.15);
778 desc.add<
bool>(
"debug",
true);
779 desc.add<
std::string>(
"eid_graph_path",
"RecoHGCal/TICL/data/tf_models/energy_id_v0.pb");
781 desc.add<
std::string>(
"eid_output_name_energy",
"output/regressed_energy");
782 desc.add<
std::string>(
"eid_output_name_id",
"output/id_probabilities");
783 desc.add<
double>(
"eid_min_cluster_energy", 1.);
784 desc.add<
int>(
"eid_n_layers", 50);
785 desc.add<
int>(
"eid_n_clusters", 10);
786 descriptions.
add(
"trackstersMergeProducer",
desc);