61 static std::unique_ptr<TrackstersCache> initializeGlobalCache(
const edm::ParameterSet &);
75 void energyRegressionAndID(
const std::vector<reco::CaloCluster> &
layerClusters,
76 const tensorflow::Session *,
77 std::vector<Trackster> &
result)
const;
78 void printTrackstersDebug(
const std::vector<Trackster> &,
const char *
label)
const;
79 void assignTimeToCandidates(std::vector<TICLCandidate> &resultCandidates)
const;
80 void dumpTrackster(
const Trackster &)
const;
130 std::unique_ptr<GeomDet> firstDisk_[2];
135 static constexpr
int eidNFeatures_ = 3;
146 tracks_time_token_(consumes<
edm::ValueMap<
float>>(ps.getParameter<
edm::
InputTag>(
"tracksTime"))),
147 tracks_time_quality_token_(consumes<
edm::ValueMap<
float>>(ps.getParameter<
edm::
InputTag>(
"tracksTimeQual"))),
148 tracks_time_err_token_(consumes<
edm::ValueMap<
float>>(ps.getParameter<
edm::
InputTag>(
"tracksTimeErr"))),
150 tfDnnLabel_(ps.getParameter<
std::
string>(
"tfDnnLabel")),
154 detector_(ps.getParameter<
std::
string>(
"detector")),
155 propName_(ps.getParameter<
std::
string>(
"propagator")),
159 optimiseAcrossTracksters_(ps.getParameter<
bool>(
"optimiseAcrossTracksters")),
160 useMTDTiming_(ps.getParameter<
bool>(
"useMTDTiming")),
161 eta_bin_window_(ps.getParameter<
int>(
"eta_bin_window")),
162 phi_bin_window_(ps.getParameter<
int>(
"phi_bin_window")),
163 pt_sigma_high_(ps.getParameter<double>(
"pt_sigma_high")),
164 pt_sigma_low_(ps.getParameter<double>(
"pt_sigma_low")),
165 halo_max_distance2_(ps.getParameter<double>(
"halo_max_distance2")),
166 track_min_pt_(ps.getParameter<double>(
"track_min_pt")),
167 track_min_eta_(ps.getParameter<double>(
"track_min_eta")),
168 track_max_eta_(ps.getParameter<double>(
"track_max_eta")),
169 track_max_missing_outerhits_(ps.getParameter<
int>(
"track_max_missing_outerhits")),
170 cosangle_align_(ps.getParameter<double>(
"cosangle_align")),
171 e_over_h_threshold_(ps.getParameter<double>(
"e_over_h_threshold")),
172 pt_neutral_threshold_(ps.getParameter<double>(
"pt_neutral_threshold")),
173 resol_calo_offset_had_(ps.getParameter<double>(
"resol_calo_offset_had")),
174 resol_calo_scale_had_(ps.getParameter<double>(
"resol_calo_scale_had")),
175 resol_calo_offset_em_(ps.getParameter<double>(
"resol_calo_offset_em")),
176 resol_calo_scale_em_(ps.getParameter<double>(
"resol_calo_scale_em")),
177 eidInputName_(ps.getParameter<
std::
string>(
"eid_input_name")),
178 eidOutputNameEnergy_(ps.getParameter<
std::
string>(
"eid_output_name_energy")),
179 eidOutputNameId_(ps.getParameter<
std::
string>(
"eid_output_name_id")),
180 eidMinClusterEnergy_(ps.getParameter<double>(
"eid_min_cluster_energy")),
181 eidNLayers_(ps.getParameter<
int>(
"eid_n_layers")),
182 eidNClusters_(ps.getParameter<
int>(
"eid_n_clusters")),
183 eidSession_(nullptr) {
184 produces<std::vector<Trackster>>();
185 produces<std::vector<TICLCandidate>>();
187 std::string detectorName_ = (
detector_ ==
"HFNose") ?
"HGCalHFNoseSensitive" :
"HGCalEESensitive";
189 esConsumes<HGCalDDDConstants, IdealGeometryRecord, edm::Transition::BeginRun>(
edm::ESInputTag(
"", detectorName_));
214 const std::vector<Trackster> &tracksters,
217 for (
auto const &
t : tracksters) {
218 tracksterTile.
fill(tracksterIteration,
t.barycenter().eta(),
t.barycenter().phi(), tracksterId);
219 LogDebug(
"TrackstersMergeProducer") <<
"Adding tracksterId: " << tracksterId <<
" into bin [eta,phi]: [ " 220 << tracksterTile[tracksterIteration].etaBin(
t.barycenter().eta()) <<
", " 221 << tracksterTile[tracksterIteration].
phiBin(
t.barycenter().phi())
222 <<
"] for iteration: " << tracksterIteration << std::endl;
229 auto e_over_h = (
t.raw_em_pt() / ((
t.raw_pt() -
t.raw_em_pt()) != 0. ? (
t.raw_pt() -
t.raw_em_pt()) : 1.));
231 <<
"\nTrackster raw_pt: " <<
t.raw_pt() <<
" raw_em_pt: " <<
t.raw_em_pt() <<
" eoh: " << e_over_h
232 <<
" barycenter: " <<
t.barycenter() <<
" eta,phi (baricenter): " <<
t.barycenter().eta() <<
", " 233 <<
t.barycenter().phi() <<
" eta,phi (eigen): " <<
t.eigenvectors(0).eta() <<
", " <<
t.eigenvectors(0).phi()
234 <<
" pt(eigen): " <<
std::sqrt(
t.eigenvectors(0).Unit().perp2()) *
t.raw_energy() <<
" seedID: " <<
t.seedID()
235 <<
" seedIndex: " <<
t.seedIndex() <<
" size: " <<
t.vertices().size() <<
" average usage: " 236 << (std::accumulate(std::begin(
t.vertex_multiplicity()), std::end(
t.vertex_multiplicity()), 0.) /
237 (
float)
t.vertex_multiplicity().size())
238 <<
" raw_energy: " <<
t.raw_energy() <<
" regressed energy: " <<
t.regressed_energy()
239 <<
" probs(ga/e/mu/np/cp/nh/am/unk): ";
240 for (
auto const &
p :
t.id_probabilities()) {
243 LogDebug(
"TrackstersMergeProducer") <<
" sigmas: ";
244 for (
auto const &
s :
t.sigmas()) {
245 LogDebug(
"TrackstersMergeProducer") <<
s <<
" ";
247 LogDebug(
"TrackstersMergeProducer") << std::endl;
251 auto resultTrackstersMerged = std::make_unique<std::vector<Trackster>>();
252 auto resultCandidates = std::make_unique<std::vector<TICLCandidate>>();
253 auto resultFromTracks = std::make_unique<std::vector<TICLCandidate>>();
261 const auto &
tracks = *track_h;
287 LogDebug(
"TrackstersMergeProducer") <<
"Results from the linking step : " << std::endl
288 <<
"No. of Tracks : " <<
tracks.size()
289 <<
" No. of Tracksters : " << (*trackstersclue3d_h).size() << std::endl
290 <<
"(neutral candidates have track id -1)" << std::endl;
292 std::vector<TICLCandidate> &
candidates = *resultCandidates;
294 auto track_ptr =
cand.trackPtr();
295 auto trackster_ptrs =
cand.tracksters();
298 track_idx = (track_ptr.isNull()) ? -1 : track_idx;
300 LogDebug(
"TrackstersMergeProducer") <<
"PDG ID " <<
cand.pdgId() <<
" charge " <<
cand.charge() <<
" p " <<
cand.p()
302 LogDebug(
"TrackstersMergeProducer") <<
"track id (p) : " << track_idx <<
" (" 303 << (track_ptr.isNull() ? -1 : track_ptr->p()) <<
") " 304 <<
" trackster ids (E) : ";
310 auto updated_size = 0;
311 for (
const auto &ts_ptr : trackster_ptrs) {
314 LogDebug(
"TrackstersMergeProducer") << ts_idx <<
" (" << ts_ptr->raw_energy() <<
") ";
317 auto &thisTrackster = *ts_ptr;
318 updated_size += thisTrackster.vertices().size();
319 outTrackster.vertices().reserve(updated_size);
320 outTrackster.vertex_multiplicity().reserve(updated_size);
321 std::copy(std::begin(thisTrackster.vertices()),
322 std::end(thisTrackster.vertices()),
323 std::back_inserter(outTrackster.vertices()));
324 std::copy(std::begin(thisTrackster.vertex_multiplicity()),
325 std::end(thisTrackster.vertex_multiplicity()),
326 std::back_inserter(outTrackster.vertex_multiplicity()));
329 LogDebug(
"TrackstersMergeProducer") << std::endl;
332 auto &orig_vtx = outTrackster.vertices();
333 auto vtx_sorted{orig_vtx};
334 std::sort(std::begin(vtx_sorted), std::end(vtx_sorted));
335 for (
unsigned int iLC = 1; iLC < vtx_sorted.size(); ++iLC) {
336 if (vtx_sorted[iLC] == vtx_sorted[iLC - 1]) {
338 const auto lcIdx = vtx_sorted[iLC];
339 const auto firstEl =
std::find(orig_vtx.begin(), orig_vtx.end(), lcIdx);
340 const auto firstPos =
std::distance(std::begin(orig_vtx), firstEl);
342 while (iDup != orig_vtx.end()) {
343 orig_vtx.erase(iDup);
344 outTrackster.vertex_multiplicity().erase(outTrackster.vertex_multiplicity().begin() +
346 outTrackster.vertex_multiplicity()[firstPos] -= 1;
352 outTrackster.zeroProbabilities();
353 if (!outTrackster.vertices().empty()) {
354 resultTrackstersMerged->push_back(outTrackster);
365 assert(resultTrackstersMerged->size() == resultCandidates->size());
367 auto isHad = [](
const Trackster &tracksterMerge) {
372 for (
size_t i = 0;
i < resultTrackstersMerged->size();
i++) {
373 auto const &tm = (*resultTrackstersMerged)[
i];
374 auto &
cand = (*resultCandidates)[
i];
376 cand.setIdProbabilities(tm.id_probabilities());
378 if (!
cand.trackPtr().isNull()) {
379 auto pdgId = isHad(tm) ? 211 : 11;
380 auto const &tk =
cand.trackPtr().get();
382 cand.setCharge(tk->charge());
383 cand.setRawEnergy(tm.raw_energy());
384 auto const ®rE = tm.regressed_energy();
386 regrE * tk->momentum().unit().y(),
387 regrE * tk->momentum().unit().z(),
391 auto pdgId = isHad(tm) ? 130 : 22;
394 cand.setRawEnergy(tm.raw_energy());
395 const float ®rE = tm.regressed_energy();
397 regrE * tm.barycenter().unit().y(),
398 regrE * tm.barycenter().unit().z(),
403 for (
auto &
cand : *resultFromTracks) {
404 auto const &tk =
cand.trackPtr().get();
405 cand.setPdgId(211 * tk->charge());
406 cand.setCharge(tk->charge());
413 resultCandidates->insert(resultCandidates->end(), resultFromTracks->begin(), resultFromTracks->end());
421 const tensorflow::Session *eidSession,
422 std::vector<Trackster> &tracksters)
const {
450 for (
auto &
t : tracksters) {
451 t.setRegressedEnergy(0.
f);
452 t.zeroProbabilities();
457 tensorflow::Tensor
input(tensorflow::DT_FLOAT,
shape);
459 static constexpr
int inputDimension = 4;
461 std::vector<tensorflow::Tensor>
outputs;
478 std::vector<int> clusterIndices(trackster.
vertices().size());
480 clusterIndices[
k] =
k;
482 sort(clusterIndices.begin(), clusterIndices.end(), [&
layerClusters, &trackster](
const int &
a,
const int &
b) {
490 for (
const int &
k : clusterIndices) {
528 float regressedEnergy =
530 tracksters[
i].setRegressedEnergy(regressedEnergy);
538 float *probs =
outputs[probsIdx].flat<
float>().
data();
539 int probsNumber = tracksters[0].id_probabilities().size();
541 tracksters[
i].setProbabilities(&probs[
i * probsNumber]);
547 for (
auto &
cand : resultCandidates) {
548 if (
cand.tracksters().size() > 1) {
550 float invTimeErr = 0.f;
551 for (
const auto &tr :
cand.tracksters()) {
552 if (tr->timeError() > 0) {
553 auto invTimeESq =
pow(tr->timeError(), -2);
554 time += tr->time() * invTimeESq;
555 invTimeErr += invTimeESq;
558 if (invTimeErr > 0) {
560 cand.setTimeError(
sqrt(1.
f / invTimeErr));
569 for (
auto const &
t : tracksters) {
571 <<
counter++ <<
" TrackstersMergeProducer (" <<
label <<
") obj barycenter: " <<
t.barycenter()
572 <<
" eta,phi (baricenter): " <<
t.barycenter().eta() <<
", " <<
t.barycenter().phi()
573 <<
" eta,phi (eigen): " <<
t.eigenvectors(0).eta() <<
", " <<
t.eigenvectors(0).phi()
574 <<
" pt(eigen): " <<
std::sqrt(
t.eigenvectors(0).Unit().perp2()) *
t.raw_energy() <<
" seedID: " <<
t.seedID()
575 <<
" seedIndex: " <<
t.seedIndex() <<
" size: " <<
t.vertices().size() <<
" average usage: " 576 << (std::accumulate(std::begin(
t.vertex_multiplicity()), std::end(
t.vertex_multiplicity()), 0.) /
577 (
float)
t.vertex_multiplicity().size())
578 <<
" raw_energy: " <<
t.raw_energy() <<
" regressed energy: " <<
t.regressed_energy()
579 <<
" probs(ga/e/mu/np/cp/nh/am/unk): ";
580 for (
auto const &
p :
t.id_probabilities()) {
583 LogDebug(
"TrackstersMergeProducer") <<
" sigmas: ";
584 for (
auto const &
s :
t.sigmas()) {
585 LogDebug(
"TrackstersMergeProducer") <<
s <<
" ";
587 LogDebug(
"TrackstersMergeProducer") << std::endl;
609 desc.add<
bool>(
"optimiseAcrossTracksters",
true);
610 desc.add<
bool>(
"useMTDTiming",
true);
611 desc.add<
int>(
"eta_bin_window", 1);
612 desc.add<
int>(
"phi_bin_window", 1);
613 desc.add<
double>(
"pt_sigma_high", 2.);
614 desc.add<
double>(
"pt_sigma_low", 2.);
615 desc.add<
double>(
"halo_max_distance2", 4.);
616 desc.add<
double>(
"track_min_pt", 1.);
617 desc.add<
double>(
"track_min_eta", 1.48);
618 desc.add<
double>(
"track_max_eta", 3.);
619 desc.add<
int>(
"track_max_missing_outerhits", 5);
620 desc.add<
double>(
"cosangle_align", 0.9945);
621 desc.add<
double>(
"e_over_h_threshold", 1.);
622 desc.add<
double>(
"pt_neutral_threshold", 2.);
623 desc.add<
double>(
"resol_calo_offset_had", 1.5);
624 desc.add<
double>(
"resol_calo_scale_had", 0.15);
625 desc.add<
double>(
"resol_calo_offset_em", 1.5);
626 desc.add<
double>(
"resol_calo_scale_em", 0.15);
629 desc.add<
std::string>(
"eid_output_name_energy",
"output/regressed_energy");
630 desc.add<
std::string>(
"eid_output_name_id",
"output/id_probabilities");
631 desc.add<
double>(
"eid_min_cluster_energy", 2.5);
632 desc.add<
int>(
"eid_n_layers", 50);
633 desc.add<
int>(
"eid_n_clusters", 10);
634 descriptions.
add(
"trackstersMergeProducer",
desc);
ticl::Trackster::IterationIndex TracksterIterIndex
std::vector< NamedTensor > NamedTensorList
T getParameter(std::string const &) const
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
void fillTile(TICLTracksterTiles &, const std::vector< Trackster > &, TracksterIterIndex)
const std::vector< std::pair< DetId, float > > & hitsAndFractions() const
void setTrackIdx(int index)
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
const double cosangle_align_
bool get(ProductID const &oid, Handle< PROD > &result) const
const edm::EDGetTokenT< std::vector< Trackster > > tracksters_clue3d_token_
void assignTimeToCandidates(std::vector< TICLCandidate > &resultCandidates) const
tensorflow::Session * eidSession_
const double halo_max_distance2_
const tensorflow::Session * tfSession_
void produce(edm::Event &, const edm::EventSetup &) override
const std::string detector_
void printTrackstersDebug(const std::vector< Trackster > &, const char *label) const
ParameterDescriptionNode * addNode(ParameterDescriptionNode const &node)
hgcal::RecHitTools rhtools_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
const edm::EDGetTokenT< std::vector< reco::CaloCluster > > clusters_token_
const double track_min_eta_
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
const edm::ESGetToken< CaloGeometry, CaloGeometryRecord > geometry_token_
double phi() const
azimuthal angle of cluster centroid
const edm::EDGetTokenT< edm::ValueMap< float > > tracks_time_quality_token_
static std::string const input
PtEtaPhiMLorentzVectorD PtEtaPhiMLorentzVector
Lorentz vector with cartesian internal representation.
void assignPCAtoTracksters(std::vector< Trackster > &, const std::vector< reco::CaloCluster > &, const edm::ValueMap< std::pair< float, float >> &, double, bool energyWeight=true)
const float eidMinClusterEnergy_
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
const std::string eidOutputNameId_
const double pt_sigma_low_
static constexpr int eidNFeatures_
const double pt_neutral_threshold_
T const * product() const
const edm::EDGetTokenT< edm::ValueMap< float > > tracks_time_err_token_
std::vector< float > features(const reco::PreId &ecal, const reco::PreId &hcal, double rho, const reco::BeamSpot &spot, noZS::EcalClusterLazyTools &ecalTools)
const double e_over_h_threshold_
void beginRun(edm::Run const &iEvent, edm::EventSetup const &es) override
const double resol_calo_scale_had_
void energyRegressionAndID(const std::vector< reco::CaloCluster > &layerClusters, const tensorflow::Session *, std::vector< Trackster > &result) const
std::once_flag initializeGeometry_
const bool optimiseAcrossTracksters_
const int phi_bin_window_
void run(Session *session, const NamedTensorList &inputs, const std::vector< std::string > &outputNames, std::vector< Tensor > *outputs, const thread::ThreadPoolOptions &threadPoolOptions)
Abs< T >::type abs(const T &t)
const edm::EDGetTokenT< std::vector< reco::Muon > > muons_token_
#define DEFINE_FWK_MODULE(type)
const double resol_calo_offset_em_
~TrackstersMergeProducer() override
void dumpTrackster(const Trackster &) const
const edm::EDGetTokenT< edm::ValueMap< std::pair< float, float > > > clustersTime_token_
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
const int track_max_missing_outerhits_
const std::string propName_
const std::string tfDnnLabel_
double energy() const
cluster energy
const edm::EDGetTokenT< std::vector< reco::Track > > tracks_token_
std::vector< unsigned int > & vertices()
const std::string eidOutputNameEnergy_
std::unique_ptr< LinkingAlgoBase > linkingAlgo_
const edm::ESGetToken< MagneticField, IdealMagneticFieldRecord > bfield_token_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
const std::string eidInputName_
TrackstersMergeProducer(const edm::ParameterSet &ps)
XYZVectorD XYZVector
spatial vector with cartesian internal representation
edm::ESGetToken< HGCalDDDConstants, IdealGeometryRecord > hdc_token_
std::vector< float > & vertex_multiplicity()
void add(std::string const &label, ParameterSetDescription const &psetDescription)
const double pt_sigma_high_
const double track_max_eta_
const HGCalDDDConstants * hgcons_
const double resol_calo_offset_had_
char data[epos_bytes_allocation]
const edm::ESGetToken< Propagator, TrackingComponentsRecord > propagator_token_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void fill(int index, double eta, double phi, unsigned int objectId)
const double track_min_pt_
const edm::EDGetTokenT< edm::ValueMap< float > > tracks_time_token_
double eta() const
pseudorapidity of cluster centroid
const double resol_calo_scale_em_
const edm::ESGetToken< TfGraphDefWrapper, TfGraphRecord > tfDnnToken_
const int eta_bin_window_