29 void energyRegressionAndID(
const std::vector<reco::CaloCluster> &
layerClusters, std::vector<Trackster> &
result)
const;
32 static std::unique_ptr<TrackstersCache> initializeGlobalCache(
const edm::ParameterSet &);
38 void fillTile(
TICLTracksterTiles &,
const std::vector<Trackster> &, TracksterIterIndex);
40 void printTrackstersDebug(
const std::vector<Trackster> &,
const char *
label)
const;
41 void dumpTrackster(
const Trackster &)
const;
79 static constexpr
int eidNFeatures_ = 3;
88 clusters_token_(consumes<
std::vector<
reco::CaloCluster>>(ps.getParameter<
edm::
InputTag>(
"layer_clusters"))),
89 tracks_token_(consumes<
std::vector<
reco::Track>>(ps.getParameter<
edm::
InputTag>(
"tracks"))),
91 optimiseAcrossTracksters_(ps.getParameter<
bool>(
"optimiseAcrossTracksters")),
92 eta_bin_window_(ps.getParameter<
int>(
"eta_bin_window")),
93 phi_bin_window_(ps.getParameter<
int>(
"phi_bin_window")),
94 pt_sigma_high_(ps.getParameter<double>(
"pt_sigma_high")),
95 pt_sigma_low_(ps.getParameter<double>(
"pt_sigma_low")),
96 halo_max_distance2_(ps.getParameter<double>(
"halo_max_distance2")),
97 track_min_pt_(ps.getParameter<double>(
"track_min_pt")),
98 track_min_eta_(ps.getParameter<double>(
"track_min_eta")),
99 track_max_eta_(ps.getParameter<double>(
"track_max_eta")),
100 track_max_missing_outerhits_(ps.getParameter<
int>(
"track_max_missing_outerhits")),
101 cosangle_align_(ps.getParameter<double>(
"cosangle_align")),
102 e_over_h_threshold_(ps.getParameter<double>(
"e_over_h_threshold")),
103 pt_neutral_threshold_(ps.getParameter<double>(
"pt_neutral_threshold")),
104 resol_calo_offset_had_(ps.getParameter<double>(
"resol_calo_offset_had")),
105 resol_calo_scale_had_(ps.getParameter<double>(
"resol_calo_scale_had")),
106 resol_calo_offset_em_(ps.getParameter<double>(
"resol_calo_offset_em")),
107 resol_calo_scale_em_(ps.getParameter<double>(
"resol_calo_scale_em")),
108 debug_(ps.getParameter<
bool>(
"debug")),
109 eidInputName_(ps.getParameter<
std::
string>(
"eid_input_name")),
110 eidOutputNameEnergy_(ps.getParameter<
std::
string>(
"eid_output_name_energy")),
111 eidOutputNameId_(ps.getParameter<
std::
string>(
"eid_output_name_id")),
112 eidMinClusterEnergy_(ps.getParameter<double>(
"eid_min_cluster_energy")),
113 eidNLayers_(ps.getParameter<
int>(
"eid_n_layers")),
114 eidNClusters_(ps.getParameter<
int>(
"eid_n_clusters")),
115 eidSession_(nullptr) {
118 if (trackstersCache ==
nullptr || trackstersCache->
eidGraphDef ==
nullptr) {
120 <<
"TrackstersMergeProducer received an empty graph definition from the global cache";
124 produces<std::vector<Trackster>>();
125 produces<std::vector<TICLCandidate>>();
129 const std::vector<Trackster> &tracksters,
132 for (
auto const &
t : tracksters) {
133 tracksterTile.
fill(tracksterIteration,
t.barycenter().eta(),
t.barycenter().phi(), tracksterId);
134 LogDebug(
"TrackstersMergeProducer") <<
"Adding tracksterId: " << tracksterId <<
" into bin [eta,phi]: [ "
135 << tracksterTile[tracksterIteration].etaBin(
t.barycenter().eta()) <<
", "
136 << tracksterTile[tracksterIteration].
phiBin(
t.barycenter().phi())
137 <<
"] for iteration: " << tracksterIteration << std::endl;
144 auto e_over_h = (
t.raw_em_pt() / ((
t.raw_pt() -
t.raw_em_pt()) != 0. ? (
t.raw_pt() -
t.raw_em_pt()) : 1.));
146 <<
"\nTrackster raw_pt: " <<
t.raw_pt() <<
" raw_em_pt: " <<
t.raw_em_pt() <<
" eoh: " << e_over_h
147 <<
" barycenter: " <<
t.barycenter() <<
" eta,phi (baricenter): " <<
t.barycenter().eta() <<
", "
148 <<
t.barycenter().phi() <<
" eta,phi (eigen): " <<
t.eigenvectors(0).eta() <<
", " <<
t.eigenvectors(0).phi()
149 <<
" pt(eigen): " <<
std::sqrt(
t.eigenvectors(0).Unit().perp2()) *
t.raw_energy() <<
" seedID: " <<
t.seedID()
150 <<
" seedIndex: " <<
t.seedIndex() <<
" size: " <<
t.vertices().size() <<
" average usage: "
151 << (std::accumulate(
std::begin(
t.vertex_multiplicity()),
std::end(
t.vertex_multiplicity()), 0.) /
152 (
float)
t.vertex_multiplicity().size())
153 <<
" raw_energy: " <<
t.raw_energy() <<
" regressed energy: " <<
t.regressed_energy()
154 <<
" probs(ga/e/mu/np/cp/nh/am/unk): ";
155 for (
auto const &
p :
t.id_probabilities()) {
158 LogDebug(
"TrackstersMergeProducer") <<
" sigmas: ";
159 for (
auto const &
s :
t.sigmas()) {
160 LogDebug(
"TrackstersMergeProducer") <<
s <<
" ";
162 LogDebug(
"TrackstersMergeProducer") << std::endl;
168 auto resultTrackstersMerged = std::make_unique<std::vector<Trackster>>();
169 auto resultCandidates = std::make_unique<std::vector<TICLCandidate>>();
172 std::vector<bool> usedTrackstersMerged;
173 std::vector<int> indexInMergedCollTRKEM;
174 std::vector<int> indexInMergedCollEM;
175 std::vector<int> indexInMergedCollTRK;
176 std::vector<int> indexInMergedCollHAD;
177 std::vector<bool> usedSeeds;
180 std::map<int, std::vector<std::pair<int, TracksterIterIndex>>> seedToTracksterAssociator;
181 std::vector<TracksterIterIndex> iterMergedTracksters;
184 const auto &
tracks = *track_h;
192 const auto &trackstersEM = *trackstersem_h;
196 const auto &trackstersTRKEM = *tracksterstrkem_h;
200 const auto &trackstersTRK = *tracksterstrk_h;
204 const auto &trackstersHAD = *trackstershad_h;
209 usedSeeds.resize(
tracks.size(),
false);
211 fillTile(tracksterTile, trackstersTRKEM, TracksterIterIndex::TRKEM);
212 fillTile(tracksterTile, trackstersEM, TracksterIterIndex::EM);
213 fillTile(tracksterTile, trackstersTRK, TracksterIterIndex::TRK);
214 fillTile(tracksterTile, trackstersHAD, TracksterIterIndex::HAD);
216 auto totalNumberOfTracksters =
217 trackstersTRKEM.size() + trackstersTRK.size() + trackstersEM.size() + trackstersHAD.size();
218 resultTrackstersMerged->reserve(totalNumberOfTracksters);
219 iterMergedTracksters.reserve(totalNumberOfTracksters);
220 usedTrackstersMerged.resize(totalNumberOfTracksters,
false);
221 indexInMergedCollTRKEM.reserve(trackstersTRKEM.size());
222 indexInMergedCollEM.reserve(trackstersEM.size());
223 indexInMergedCollTRK.reserve(trackstersTRK.size());
224 indexInMergedCollHAD.reserve(trackstersHAD.size());
233 for (
auto const &
t : trackstersTRKEM) {
234 indexInMergedCollTRKEM.push_back(resultTrackstersMerged->size());
235 seedToTracksterAssociator[
t.seedIndex()].emplace_back(resultTrackstersMerged->size(), TracksterIterIndex::TRKEM);
236 resultTrackstersMerged->push_back(
t);
237 iterMergedTracksters.push_back(TracksterIterIndex::TRKEM);
240 for (
auto const &
t : trackstersEM) {
241 indexInMergedCollEM.push_back(resultTrackstersMerged->size());
242 resultTrackstersMerged->push_back(
t);
243 iterMergedTracksters.push_back(TracksterIterIndex::EM);
246 for (
auto const &
t : trackstersTRK) {
247 indexInMergedCollTRK.push_back(resultTrackstersMerged->size());
248 seedToTracksterAssociator[
t.seedIndex()].emplace_back(resultTrackstersMerged->size(), TracksterIterIndex::TRK);
249 resultTrackstersMerged->push_back(
t);
250 iterMergedTracksters.push_back(TracksterIterIndex::TRK);
253 for (
auto const &
t : trackstersHAD) {
254 indexInMergedCollHAD.push_back(resultTrackstersMerged->size());
255 resultTrackstersMerged->push_back(
t);
256 iterMergedTracksters.push_back(TracksterIterIndex::HAD);
264 auto trackstersMergedHandle = evt.
put(
std::move(resultTrackstersMerged));
270 for (
unsigned i = 0;
i < trackstersEM.size(); ++
i) {
271 auto mergedIdx = indexInMergedCollEM[
i];
272 usedTrackstersMerged[mergedIdx] =
true;
273 const auto &
t = trackstersEM[
i];
280 t.raw_energy() *
t.barycenter().unit().y(),
281 t.raw_energy() *
t.barycenter().unit().z(),
284 resultCandidates->push_back(tmpCandidate);
288 constexpr
double mpion = 0.13957;
289 constexpr
float mpion2 = mpion * mpion;
290 for (
unsigned i = 0;
i < trackstersHAD.size(); ++
i) {
291 auto mergedIdx = indexInMergedCollHAD[
i];
292 usedTrackstersMerged[mergedIdx] =
true;
293 const auto &
t = trackstersHAD[
i];
299 float momentum =
std::sqrt(
t.raw_energy() *
t.raw_energy() - mpion2);
301 momentum *
t.barycenter().unit().y(),
302 momentum *
t.barycenter().unit().z(),
305 resultCandidates->push_back(tmpCandidate);
309 for (
unsigned i = 0;
i < trackstersTRKEM.size(); ++
i) {
310 auto mergedIdx = indexInMergedCollTRKEM[
i];
311 if (!usedTrackstersMerged[mergedIdx]) {
312 const auto &
t = trackstersTRKEM[
i];
313 auto trackIdx =
t.seedIndex();
315 if (!usedSeeds[trackIdx] and
t.raw_energy() > 0) {
316 usedSeeds[trackIdx] =
true;
317 usedTrackstersMerged[mergedIdx] =
true;
319 std::vector<int> trackstersTRKwithSameSeed;
320 std::vector<int> trackstersTRKEMwithSameSeed;
322 for (
const auto &tracksterIterationPair : seedToTracksterAssociator[trackIdx]) {
323 if (tracksterIterationPair.first != mergedIdx and !usedTrackstersMerged[tracksterIterationPair.first] and
324 trackstersMergedHandle->at(tracksterIterationPair.first).raw_energy() > 0.) {
325 if (tracksterIterationPair.second == TracksterIterIndex::TRKEM) {
326 trackstersTRKEMwithSameSeed.push_back(tracksterIterationPair.first);
327 }
else if (tracksterIterationPair.second == TracksterIterIndex::TRK) {
328 trackstersTRKwithSameSeed.push_back(tracksterIterationPair.first);
333 float tracksterTotalRawPt =
t.raw_pt();
334 std::vector<int> haloTrackstersTRKIdx;
335 bool foundCompatibleTRK =
false;
337 for (
auto otherTracksterIdx : trackstersTRKwithSameSeed) {
338 usedTrackstersMerged[otherTracksterIdx] =
true;
339 tracksterTotalRawPt += trackstersMergedHandle->at(otherTracksterIdx).raw_pt();
342 if ((
t.barycenter() - trackstersMergedHandle->at(otherTracksterIdx).barycenter()).
mag2() <
344 haloTrackstersTRKIdx.push_back(otherTracksterIdx);
347 foundCompatibleTRK =
true;
352 if (trackstersTRKEMwithSameSeed.empty()) {
353 if (foundCompatibleTRK) {
356 double raw_energy =
t.raw_energy();
361 for (
auto otherTracksterIdx : trackstersTRKwithSameSeed) {
363 raw_energy += trackstersMergedHandle->at(otherTracksterIdx).raw_energy();
367 raw_energy *
track.momentum().unit().y(),
368 raw_energy *
track.momentum().unit().z(),
371 resultCandidates->push_back(tmpCandidate);
376 double raw_energy =
t.raw_energy();
379 for (
auto otherTracksterIdx : trackstersTRKwithSameSeed) {
381 raw_energy += trackstersMergedHandle->at(otherTracksterIdx).raw_energy();
387 raw_energy *
track.momentum().unit().y(),
388 raw_energy *
track.momentum().unit().z(),
391 resultCandidates->push_back(tmpCandidate);
396 int closestTrackster = mergedIdx;
398 for (
auto otherTracksterIdx : trackstersTRKEMwithSameSeed) {
399 auto thisPt = tracksterTotalRawPt + trackstersMergedHandle->at(otherTracksterIdx).raw_pt() -
t.raw_pt();
400 closestTrackster =
std::abs(thisPt -
track.pt()) < minPtDiff ? otherTracksterIdx : closestTrackster;
402 usedTrackstersMerged[closestTrackster] =
true;
404 if (foundCompatibleTRK) {
407 double raw_energy = trackstersMergedHandle->at(closestTrackster).raw_energy();
412 for (
auto otherTracksterIdx : trackstersTRKwithSameSeed) {
414 raw_energy += trackstersMergedHandle->at(otherTracksterIdx).raw_energy();
417 float momentum =
std::sqrt(raw_energy * raw_energy - mpion2);
419 momentum *
track.momentum().unit().y(),
420 momentum *
track.momentum().unit().z(),
423 resultCandidates->push_back(tmpCandidate);
428 double raw_energy = trackstersMergedHandle->at(closestTrackster).raw_energy();
432 for (
auto otherTracksterIdx : trackstersTRKwithSameSeed) {
434 raw_energy += trackstersMergedHandle->at(otherTracksterIdx).raw_energy();
439 raw_energy *
track.momentum().unit().y(),
440 raw_energy *
track.momentum().unit().z(),
443 resultCandidates->push_back(tmpCandidate);
446 for (
auto otherTracksterIdx : trackstersTRKEMwithSameSeed) {
447 auto tmpIndex = (otherTracksterIdx != closestTrackster) ? otherTracksterIdx : mergedIdx;
449 const auto &otherTrackster = trackstersMergedHandle->at(tmpIndex);
450 auto gammaEnergy = otherTrackster.raw_energy();
455 gammaEnergy * otherTrackster.barycenter().unit().y(),
456 gammaEnergy * otherTrackster.barycenter().unit().z(),
458 photonCandidate.
setP4(gammaP4);
460 resultCandidates->push_back(photonCandidate);
467 for (
unsigned i = 0;
i < trackstersTRK.size(); ++
i) {
468 auto mergedIdx = indexInMergedCollTRK[
i];
469 const auto &
t = trackstersTRK[
i];
471 if (!usedTrackstersMerged[mergedIdx] and
t.raw_energy() > 0) {
472 auto trackIdx =
t.seedIndex();
474 if (!usedSeeds[trackIdx]) {
475 usedSeeds[trackIdx] =
true;
476 usedTrackstersMerged[mergedIdx] =
true;
483 float momentum =
std::sqrt(
t.raw_energy() *
t.raw_energy() - mpion2);
485 momentum *
track.momentum().unit().y(),
486 momentum *
track.momentum().unit().z(),
489 resultCandidates->push_back(tmpCandidate);
495 if (usedSeeds[
s.index] ==
false) {
505 tmpCandidate.
setP4(p4Polar);
506 resultCandidates->push_back(tmpCandidate);
507 usedSeeds[
s.index] =
true;
512 for (
unsigned i = 0;
i <
tracks.size(); ++
i) {
517 usedSeeds[
i] ==
false) {
526 tmpCandidate.
setP4(p4Polar);
527 resultCandidates->push_back(tmpCandidate);
536 std::vector<Trackster> &tracksters)
const {
560 std::vector<int> tracksterIndices;
561 for (
int i = 0;
i < (
int)tracksters.size();
i++) {
566 float sumClusterEnergy = 0.;
572 tracksters[
i].setRegressedEnergy(0.
f);
573 tracksters[
i].zeroProbabilities();
574 tracksterIndices.push_back(
i);
581 int batchSize = (
int)tracksterIndices.size();
582 if (batchSize == 0) {
588 tensorflow::Tensor
input(tensorflow::DT_FLOAT, shape);
590 static constexpr
int inputDimension = 4;
592 std::vector<tensorflow::Tensor>
outputs;
602 for (
int i = 0;
i < batchSize;
i++) {
603 const Trackster &trackster = tracksters[tracksterIndices[
i]];
609 std::vector<int> clusterIndices(trackster.
vertices().size());
611 clusterIndices[
k] =
k;
613 sort(clusterIndices.begin(), clusterIndices.end(), [&
layerClusters, &trackster](
const int &
a,
const int &
b) {
621 for (
const int &
k : clusterIndices) {
658 for (
const int &
i : tracksterIndices) {
659 tracksters[
i].setRegressedEnergy(*(
energy++));
667 float *probs =
outputs[probsIdx].flat<
float>().
data();
669 for (
const int &
i : tracksterIndices) {
670 tracksters[
i].setProbabilities(probs);
671 probs += tracksters[
i].id_probabilities().size();
678 std::unique_ptr<TrackstersCache>
cache = std::make_unique<TrackstersCache>(
params);
682 if (!graphPath.empty()) {
691 delete cache->eidGraphDef;
692 cache->eidGraphDef =
nullptr;
700 for (
auto const &
t : tracksters) {
702 <<
counter++ <<
" TrackstersMergeProducer (" <<
label <<
") obj barycenter: " <<
t.barycenter()
703 <<
" eta,phi (baricenter): " <<
t.barycenter().eta() <<
", " <<
t.barycenter().phi()
704 <<
" eta,phi (eigen): " <<
t.eigenvectors(0).eta() <<
", " <<
t.eigenvectors(0).phi()
705 <<
" pt(eigen): " <<
std::sqrt(
t.eigenvectors(0).Unit().perp2()) *
t.raw_energy() <<
" seedID: " <<
t.seedID()
706 <<
" seedIndex: " <<
t.seedIndex() <<
" size: " <<
t.vertices().size() <<
" average usage: "
707 << (std::accumulate(
std::begin(
t.vertex_multiplicity()),
std::end(
t.vertex_multiplicity()), 0.) /
708 (
float)
t.vertex_multiplicity().size())
709 <<
" raw_energy: " <<
t.raw_energy() <<
" regressed energy: " <<
t.regressed_energy()
710 <<
" probs(ga/e/mu/np/cp/nh/am/unk): ";
711 for (
auto const &
p :
t.id_probabilities()) {
714 LogDebug(
"TrackstersMergeProducer") <<
" sigmas: ";
715 for (
auto const &
s :
t.sigmas()) {
716 LogDebug(
"TrackstersMergeProducer") <<
s <<
" ";
718 LogDebug(
"TrackstersMergeProducer") << std::endl;
731 desc.
add<
bool>(
"optimiseAcrossTracksters",
true);
732 desc.
add<
int>(
"eta_bin_window", 1);
733 desc.
add<
int>(
"phi_bin_window", 1);
734 desc.
add<
double>(
"pt_sigma_high", 2.);
735 desc.
add<
double>(
"pt_sigma_low", 2.);
736 desc.
add<
double>(
"halo_max_distance2", 4.);
737 desc.
add<
double>(
"track_min_pt", 1.);
738 desc.
add<
double>(
"track_min_eta", 1.48);
739 desc.
add<
double>(
"track_max_eta", 3.);
740 desc.
add<
int>(
"track_max_missing_outerhits", 5);
741 desc.
add<
double>(
"cosangle_align", 0.9945);
742 desc.
add<
double>(
"e_over_h_threshold", 1.);
743 desc.
add<
double>(
"pt_neutral_threshold", 2.);
744 desc.
add<
double>(
"resol_calo_offset_had", 1.5);
745 desc.
add<
double>(
"resol_calo_scale_had", 0.15);
746 desc.
add<
double>(
"resol_calo_offset_em", 1.5);
747 desc.
add<
double>(
"resol_calo_scale_em", 0.15);
748 desc.
add<
bool>(
"debug",
true);
749 desc.
add<
std::string>(
"eid_graph_path",
"RecoHGCal/TICL/data/tf_models/energy_id_v0.pb");
751 desc.
add<
std::string>(
"eid_output_name_energy",
"output/regressed_energy");
752 desc.
add<
std::string>(
"eid_output_name_id",
"output/id_probabilities");
753 desc.
add<
double>(
"eid_min_cluster_energy", 1.);
754 desc.
add<
int>(
"eid_n_layers", 50);
755 desc.
add<
int>(
"eid_n_clusters", 10);
756 descriptions.
add(
"trackstersMergeProducer", desc);