28 void energyRegressionAndID(
const std::vector<reco::CaloCluster> &layerClusters, std::vector<Trackster> &
result)
const;
31 static std::unique_ptr<TrackstersCache> initializeGlobalCache(
const edm::ParameterSet &);
37 void fillTile(
TICLTracksterTiles &,
const std::vector<Trackster> &, TracksterIterIndex);
39 void printTrackstersDebug(
const std::vector<Trackster> &,
const char *
label)
const;
40 void dumpTrackster(
const Trackster &)
const;
69 static constexpr
int eidNFeatures_ = 3;
77 clusters_token_(consumes<
std::vector<
reco::CaloCluster>>(ps.getParameter<
edm::
InputTag>(
"layer_clusters"))),
78 tracks_token_(consumes<
std::vector<
reco::Track>>(ps.getParameter<
edm::
InputTag>(
"tracks"))),
79 optimiseAcrossTracksters_(ps.getParameter<
bool>(
"optimiseAcrossTracksters")),
80 eta_bin_window_(ps.getParameter<
int>(
"eta_bin_window")),
81 phi_bin_window_(ps.getParameter<
int>(
"phi_bin_window")),
82 pt_sigma_high_(ps.getParameter<double>(
"pt_sigma_high")),
83 pt_sigma_low_(ps.getParameter<double>(
"pt_sigma_low")),
84 cosangle_align_(ps.getParameter<double>(
"cosangle_align")),
85 e_over_h_threshold_(ps.getParameter<double>(
"e_over_h_threshold")),
86 pt_neutral_threshold_(ps.getParameter<double>(
"pt_neutral_threshold")),
87 resol_calo_offset_(ps.getParameter<double>(
"resol_calo_offset")),
88 resol_calo_scale_(ps.getParameter<double>(
"resol_calo_scale")),
89 debug_(ps.getParameter<
bool>(
"debug")),
90 eidInputName_(ps.getParameter<
std::
string>(
"eid_input_name")),
91 eidOutputNameEnergy_(ps.getParameter<
std::
string>(
"eid_output_name_energy")),
92 eidOutputNameId_(ps.getParameter<
std::
string>(
"eid_output_name_id")),
93 eidMinClusterEnergy_(ps.getParameter<double>(
"eid_min_cluster_energy")),
94 eidNLayers_(ps.getParameter<
int>(
"eid_n_layers")),
95 eidNClusters_(ps.getParameter<
int>(
"eid_n_clusters")),
96 eidSession_(nullptr) {
99 if (trackstersCache ==
nullptr || trackstersCache->
eidGraphDef ==
nullptr) {
101 <<
"TrackstersMergeProducer received an empty graph definition from the global cache";
105 produces<std::vector<Trackster>>();
109 const std::vector<Trackster> &tracksters,
112 for (
auto const &
t : tracksters) {
113 tracksterTile.
fill(tracksterIteration,
t.barycenter().eta(),
t.barycenter().phi(), tracksterId);
114 LogDebug(
"TrackstersMergeProducer") <<
"Adding tracksterId: " << tracksterId <<
" into bin [eta,phi]: [ "
115 << tracksterTile[tracksterIteration].etaBin(
t.barycenter().eta()) <<
", "
116 << tracksterTile[tracksterIteration].
phiBin(
t.barycenter().phi())
117 <<
"] for iteration: " << tracksterIteration << std::endl;
124 auto e_over_h = (
t.raw_em_pt() / ((
t.raw_pt() -
t.raw_em_pt()) != 0. ? (
t.raw_pt() -
t.raw_em_pt()) : 1.));
126 <<
"\nTrackster raw_pt: " <<
t.raw_pt() <<
" raw_em_pt: " <<
t.raw_em_pt() <<
" eoh: " << e_over_h
127 <<
" barycenter: " <<
t.barycenter() <<
" eta,phi (baricenter): " <<
t.barycenter().eta() <<
", "
128 <<
t.barycenter().phi() <<
" eta,phi (eigen): " <<
t.eigenvectors(0).eta() <<
", " <<
t.eigenvectors(0).phi()
129 <<
" pt(eigen): " <<
std::sqrt(
t.eigenvectors(0).Unit().perp2()) *
t.raw_energy() <<
" seedID: " <<
t.seedID()
130 <<
" seedIndex: " <<
t.seedIndex() <<
" size: " <<
t.vertices().size() <<
" average usage: "
131 << (std::accumulate(
std::begin(
t.vertex_multiplicity()),
std::end(
t.vertex_multiplicity()), 0.) /
132 (
float)
t.vertex_multiplicity().size())
133 <<
" raw_energy: " <<
t.raw_energy() <<
" regressed energy: " <<
t.regressed_energy()
134 <<
" probs(ga/e/mu/np/cp/nh/am/unk): ";
135 for (
auto const &
p :
t.id_probabilities()) {
138 LogDebug(
"TrackstersMergeProducer") <<
" sigmas: ";
139 for (
auto const &
s :
t.sigmas()) {
140 LogDebug(
"TrackstersMergeProducer") <<
s <<
" ";
142 LogDebug(
"TrackstersMergeProducer") << std::endl;
147 auto result = std::make_unique<std::vector<Trackster>>();
148 auto mergedTrackstersTRK = std::make_unique<std::vector<Trackster>>();
151 std::vector<bool> usedTrackstersEM;
152 std::vector<bool> usedTrackstersTRK;
153 std::vector<bool> usedTrackstersHAD;
154 std::vector<bool> usedSeeds;
158 const auto &
tracks = *track_h;
162 const auto &layerClusters = *cluster_h;
166 const auto &trackstersEM = *trackstersem_h;
167 usedTrackstersEM.resize(trackstersEM.size(),
false);
171 const auto &trackstersTRK = *tracksterstrk_h;
172 usedTrackstersTRK.resize(trackstersTRK.size(),
false);
176 const auto &trackstersHAD = *trackstershad_h;
177 usedTrackstersHAD.resize(trackstersHAD.size(),
false);
181 const auto &seedingTrk = *seedingTrk_h;
182 usedSeeds.resize(seedingTrk.size(),
false);
184 fillTile(tracksterTile, trackstersEM, TracksterIterIndex::EM);
185 fillTile(tracksterTile, trackstersTRK, TracksterIterIndex::TRK);
186 fillTile(tracksterTile, trackstersHAD, TracksterIterIndex::HAD);
189 for (
auto const &
s : seedingTrk) {
194 <<
"Seed index: " << seedId <<
" internal index: " <<
s.index <<
" origin: " <<
s.origin
195 <<
" mom: " <<
s.directionAtOrigin <<
" pt: " <<
std::sqrt(
s.directionAtOrigin.perp2())
196 <<
" zSide: " <<
s.zSide <<
" collectionID: " <<
s.collectionID <<
" track pt " <<
tracks[
s.index].pt()
207 int tracksterTRK_idx = 0;
208 int tracksterHAD_idx = 0;
210 for (
auto const &
t : trackstersTRK) {
212 int entryEtaBin = tracksterTile[TracksterIterIndex::TRK].etaBin(
t.barycenter().eta());
213 int entryPhiBin = tracksterTile[TracksterIterIndex::TRK].phiBin(
t.barycenter().phi());
214 int bin = tracksterTile[TracksterIterIndex::TRK].globalBin(
t.barycenter().eta(),
t.barycenter().phi());
216 <<
"TrackstersMergeProducer Tracking obj: " <<
t.barycenter() <<
" in bin " <<
bin <<
" etaBin "
217 << entryEtaBin <<
" phiBin " << entryPhiBin << std::endl;
222 auto diff_pt =
t.raw_pt() - trk_pt;
224 auto w_cal = 1. / (pt_err * pt_err);
225 auto w_trk = 1. / (
track.ptError() *
track.ptError());
226 auto diff_pt_sigmas = diff_pt / pt_err;
227 auto e_over_h = (
t.raw_em_pt() / ((
t.raw_pt() -
t.raw_em_pt()) != 0. ? (
t.raw_pt() -
t.raw_em_pt()) : 1.));
229 <<
"trackster_pt: " <<
t.raw_pt() << std::endl
230 <<
"track pt (inner): " <<
track.pt() << std::endl
231 <<
"track eta (inner): " <<
track.eta() << std::endl
232 <<
"track _phi (inner): " <<
track.phi() << std::endl
233 <<
"track pt (outer): " <<
std::sqrt(
track.outerMomentum().perp2()) << std::endl
234 <<
"track eta (outer): " <<
track.outerMomentum().eta() << std::endl
235 <<
"track _phi (outer): " <<
track.outerMomentum().phi() << std::endl
236 <<
"pt_err_track: " <<
track.ptError() << std::endl
237 <<
"diff_pt: " << diff_pt << std::endl
238 <<
"pt_err: " << pt_err << std::endl
239 <<
"diff_pt_sigmas: " << diff_pt_sigmas << std::endl
240 <<
"w_cal: " << w_cal << std::endl
241 <<
"w_trk: " << w_trk << std::endl
242 <<
"average_pt: " << (
t.raw_pt() * w_cal + trk_pt * w_trk) / (w_cal + w_trk) << std::endl
243 <<
"e_over_h: " << e_over_h << std::endl;
249 auto gamma_pt =
std::min(diff_pt,
t.raw_em_pt());
252 <<
" Creating a photon from TRK Trackster with energy " << gamma_pt <<
" and direction "
253 <<
t.eigenvectors(0).eta() <<
", " <<
t.eigenvectors(0).phi() << std::endl;
258 <<
" Adding also a neutral hadron from TRK Trackster with energy " << diff_pt <<
" and direction "
259 <<
t.eigenvectors(0).eta() <<
", " <<
t.eigenvectors(0).phi() << std::endl;
264 <<
" Creating a neutral hadron from TRK Trackster with energy " << diff_pt <<
" and direction "
265 <<
t.eigenvectors(0).eta() <<
", " <<
t.eigenvectors(0).phi() << std::endl;
275 auto average_pt = (w_cal *
t.raw_pt() + trk_pt * w_trk) / (w_cal + w_trk);
277 <<
" Creating electron/charged hadron from TRK Trackster with weighted p_t " << average_pt
278 <<
" and direction " <<
track.eta() <<
", " <<
track.phi() << std::endl;
286 <<
" Creating electron/charged hadron from TRK Trackster with track p_t " << trk_pt <<
" and direction "
287 <<
track.eta() <<
", " <<
track.phi() << std::endl;
290 usedTrackstersTRK[tracksterTRK_idx] =
true;
295 tracksterTRK_idx = 0;
296 for (
auto const &
t : trackstersTRK) {
298 LogDebug(
"TrackstersMergeProducer") <<
" Considering trackster " << tracksterTRK_idx
299 <<
" as used: " << usedTrackstersTRK[tracksterTRK_idx] << std::endl;
301 if (!usedTrackstersTRK[tracksterTRK_idx]) {
303 <<
" Creating a charge hadron from TRK Trackster with track energy " <<
t.raw_energy() <<
" and direction "
304 <<
t.eigenvectors(0).eta() <<
", " <<
t.eigenvectors(0).phi() << std::endl;
310 auto tracksterEM_idx = 0;
311 for (
auto const &
t : trackstersEM) {
313 LogDebug(
"TrackstersMergeProducer") <<
" Considering trackster " << tracksterEM_idx
314 <<
" as used: " << usedTrackstersEM[tracksterEM_idx] << std::endl;
316 if (!usedTrackstersEM[tracksterEM_idx]) {
318 <<
" Creating a photon from EM Trackster with track energy " <<
t.raw_energy() <<
" and direction "
319 <<
t.eigenvectors(0).eta() <<
", " <<
t.eigenvectors(0).phi() << std::endl;
325 tracksterHAD_idx = 0;
326 for (
auto const &
t : trackstersHAD) {
328 LogDebug(
"TrackstersMergeProducer") <<
" Considering trackster " << tracksterHAD_idx
329 <<
" as used: " << usedTrackstersHAD[tracksterHAD_idx] << std::endl;
331 if (!usedTrackstersHAD[tracksterHAD_idx]) {
333 <<
" Creating a neutral hadron from HAD Trackster with track energy " <<
t.raw_energy() <<
" and direction "
334 <<
t.eigenvectors(0).eta() <<
", " <<
t.eigenvectors(0).phi() << std::endl;
349 std::vector<Trackster> &tracksters)
const {
373 std::vector<int> tracksterIndices;
374 for (
int i = 0;
i < (
int)tracksters.size();
i++) {
379 float sumClusterEnergy = 0.;
385 tracksters[
i].setRegressedEnergy(0.
f);
386 tracksters[
i].zeroProbabilities();
387 tracksterIndices.push_back(
i);
394 int batchSize = (
int)tracksterIndices.size();
395 if (batchSize == 0) {
401 tensorflow::Tensor
input(tensorflow::DT_FLOAT, shape);
403 static constexpr
int inputDimension = 4;
405 std::vector<tensorflow::Tensor>
outputs;
415 for (
int i = 0;
i < batchSize;
i++) {
416 const Trackster &trackster = tracksters[tracksterIndices[
i]];
422 std::vector<int> clusterIndices(trackster.
vertices().size());
424 clusterIndices[
k] =
k;
426 sort(clusterIndices.begin(), clusterIndices.end(), [&layerClusters, &trackster](
const int &
a,
const int &
b) {
427 return layerClusters[trackster.
vertices(
a)].energy() > layerClusters[trackster.
vertices(
b)].energy();
434 for (
const int &
k : clusterIndices) {
471 for (
const int &
i : tracksterIndices) {
472 tracksters[
i].setRegressedEnergy(*(
energy++));
480 float *probs =
outputs[probsIdx].flat<
float>().
data();
482 for (
const int &
i : tracksterIndices) {
483 tracksters[
i].setProbabilities(probs);
484 probs += tracksters[
i].id_probabilities().size();
491 std::unique_ptr<TrackstersCache>
cache = std::make_unique<TrackstersCache>(
params);
495 if (!graphPath.empty()) {
504 delete cache->eidGraphDef;
505 cache->eidGraphDef =
nullptr;
513 for (
auto const &
t : tracksters) {
515 <<
counter++ <<
" TrackstersMergeProducer (" <<
label <<
") obj barycenter: " <<
t.barycenter()
516 <<
" eta,phi (baricenter): " <<
t.barycenter().eta() <<
", " <<
t.barycenter().phi()
517 <<
" eta,phi (eigen): " <<
t.eigenvectors(0).eta() <<
", " <<
t.eigenvectors(0).phi()
518 <<
" pt(eigen): " <<
std::sqrt(
t.eigenvectors(0).Unit().perp2()) *
t.raw_energy() <<
" seedID: " <<
t.seedID()
519 <<
" seedIndex: " <<
t.seedIndex() <<
" size: " <<
t.vertices().size() <<
" average usage: "
520 << (std::accumulate(
std::begin(
t.vertex_multiplicity()),
std::end(
t.vertex_multiplicity()), 0.) /
521 (
float)
t.vertex_multiplicity().size())
522 <<
" raw_energy: " <<
t.raw_energy() <<
" regressed energy: " <<
t.regressed_energy()
523 <<
" probs(ga/e/mu/np/cp/nh/am/unk): ";
524 for (
auto const &
p :
t.id_probabilities()) {
527 LogDebug(
"TrackstersMergeProducer") <<
" sigmas: ";
528 for (
auto const &
s :
t.sigmas()) {
529 LogDebug(
"TrackstersMergeProducer") <<
s <<
" ";
531 LogDebug(
"TrackstersMergeProducer") << std::endl;
543 desc.
add<
bool>(
"optimiseAcrossTracksters",
true);
544 desc.
add<
int>(
"eta_bin_window", 1);
545 desc.
add<
int>(
"phi_bin_window", 1);
546 desc.
add<
double>(
"pt_sigma_high", 2.);
547 desc.
add<
double>(
"pt_sigma_low", 2.);
548 desc.
add<
double>(
"cosangle_align", 0.9945);
549 desc.
add<
double>(
"e_over_h_threshold", 1.);
550 desc.
add<
double>(
"pt_neutral_threshold", 2.);
551 desc.
add<
double>(
"resol_calo_offset", 1.5);
552 desc.
add<
double>(
"resol_calo_scale", 0.15);
553 desc.
add<
bool>(
"debug",
true);
554 desc.
add<
std::string>(
"eid_graph_path",
"RecoHGCal/TICL/data/tf_models/energy_id_v0.pb");
556 desc.
add<
std::string>(
"eid_output_name_energy",
"output/regressed_energy");
557 desc.
add<
std::string>(
"eid_output_name_id",
"output/id_probabilities");
558 desc.
add<
double>(
"eid_min_cluster_energy", 1.);
559 desc.
add<
int>(
"eid_n_layers", 50);
560 desc.
add<
int>(
"eid_n_clusters", 10);
561 descriptions.
add(
"trackstersMergeProducer", desc);