Skip to content

Commit 48e0354

Browse files
authored
Merge pull request #48179 from BenjaminRS/NNVtxAssoc_Masterv1
[L1T] E2E NNVtx Track to Vertex Association in Puppi (Phase 2)
2 parents 03f77d9 + 707b8c6 commit 48e0354

File tree

8 files changed

+275
-8
lines changed

8 files changed

+275
-8
lines changed

L1Trigger/L1TTrackMatch/plugins/L1TrackVertexAssociationProducer.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ class L1TrackVertexAssociationProducer : public edm::global::EDProducer<> {
204204
inputAssoc.tensor<float, 2>()(0, 3) = dZEmulation_rescale.to_double();
205205

206206
// Run Association Network:
207-
tensorflow::run(AssociationSesh_, {{"assoc:0", inputAssoc}}, {"Identity:0"}, &outputAssoc);
207+
tensorflow::run(AssociationSesh_, {{"NNvtx_track_association:0", inputAssoc}}, {"Identity:0"}, &outputAssoc);
208208

209209
double NNOutput = (double)outputAssoc[0].tensor<float, 2>()(0, 0);
210210

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#ifndef L1TRIGGER_PHASE2L1PARTICLEFLOWS_NNVtxAssoc_H
2+
#define L1TRIGGER_PHASE2L1PARTICLEFLOWS_NNVtxAssoc_H
3+
4+
// Package: L1Trigger/Phase2L1ParticleFlow
5+
// Class: NNVtxAssoc
6+
// Description: Designed to run the track to vertex associations created by the E2E NNVtx.
7+
// TTTrackNetworkSelector either accepts or rejects that a PF object's (t) track is associated to a vertex (v).
8+
// Authors: Kai Hong Law and Benjamin Radburn-Smith
9+
// Created: February 2025
10+
11+
#include <string>
12+
#include "DataFormats/L1TParticleFlow/interface/layer1_emulator.h"
13+
14+
namespace edm {
15+
class ParameterSet;
16+
class ParameterSetDescription;
17+
} // namespace edm
18+
namespace tensorflow {
19+
class Session;
20+
} // namespace tensorflow
21+
using namespace l1ct;
22+
23+
class NNVtxAssoc {
24+
public:
25+
NNVtxAssoc(std::string AssociationGraphPath,
26+
const double AssociationThreshold,
27+
const std::vector<double>& AssociationNetworkZ0binning,
28+
const std::vector<double>& AssociationNetworkEtaBounds,
29+
const std::vector<double>& AssociationNetworkZ0ResBins);
30+
31+
void NNVtxAssocDebug();
32+
static edm::ParameterSetDescription getParameterSetDescription();
33+
34+
template <typename T>
35+
bool TTTrackNetworkSelector(const PFRegionEmu& region, const T& t, const l1ct::PVObjEmu& v);
36+
37+
private:
38+
tensorflow::Session* associationSesh_;
39+
double associationThreshold_;
40+
std::vector<double> z0_binning_;
41+
std::vector<double> eta_bins_;
42+
std::vector<double> res_bins_;
43+
std::stringstream log_;
44+
};
45+
#endif

L1Trigger/Phase2L1ParticleFlow/interface/puppi/linpuppi_ref.h

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@
55
#include "linpuppi_bits.h"
66

77
#include <vector>
8+
#include "L1Trigger/Phase2L1ParticleFlow/interface/NNVtxAssoc.h"
9+
#include <memory>
810

911
namespace edm {
1012
class ParameterSet;
1113
class ParameterSetDescription;
1214
} // namespace edm
1315

1416
namespace l1ct {
17+
#ifdef CMSSW_GIT_HASH
18+
const bool withinCMSSW_ = true;
19+
#else
20+
const bool withinCMSSW_ = false;
21+
#endif
1522

1623
class LinPuppiEmulator {
1724
public:
@@ -35,6 +42,12 @@ namespace l1ct {
3542
double priorNe,
3643
double priorPh,
3744
pt_t ptCut,
45+
bool useMLAssociation = false,
46+
const double associationThreshold = 0.0,
47+
std::string associationGraphPath = "",
48+
std::vector<double> associationNetworkZ0binning = {},
49+
std::vector<double> associationNetworkEtaBounds = {},
50+
std::vector<double> associationNetworkZ0ResBins = {},
3851
unsigned int nFinalSort = 0,
3952
SortAlgo finalSortAlgo = SortAlgo::Insertion)
4053
: nTrack_(nTrack),
@@ -56,10 +69,19 @@ namespace l1ct {
5669
priorNe_(1, priorNe),
5770
priorPh_(1, priorPh),
5871
ptCut_(1, ptCut),
72+
useMLAssociation_(useMLAssociation),
5973
nFinalSort_(nFinalSort ? nFinalSort : nOut),
6074
finalSortAlgo_(finalSortAlgo),
6175
debug_(false),
62-
fakePuppi_(false) {}
76+
fakePuppi_(false) {
77+
if (useMLAssociation_ and withinCMSSW_) {
78+
nnVtxAssoc_ = std::make_unique<NNVtxAssoc>(NNVtxAssoc(associationGraphPath,
79+
associationThreshold,
80+
associationNetworkZ0binning,
81+
associationNetworkEtaBounds,
82+
associationNetworkZ0ResBins));
83+
}
84+
}
6385

6486
LinPuppiEmulator(unsigned int nTrack,
6587
unsigned int nIn,
@@ -90,6 +112,12 @@ namespace l1ct {
90112
double priorPh_1,
91113
pt_t ptCut_0,
92114
pt_t ptCut_1,
115+
bool useMLAssociation = false,
116+
const double associationThreshold = 0.0,
117+
std::string associationGraphPath = "",
118+
std::vector<double> associationNetworkZ0binning = {},
119+
std::vector<double> associationNetworkEtaBounds = {},
120+
std::vector<double> associationNetworkZ0ResBins = {},
93121
unsigned int nFinalSort = 0,
94122
SortAlgo finalSortAlgo = SortAlgo::Insertion);
95123

@@ -112,6 +140,12 @@ namespace l1ct {
112140
const std::vector<double> &priorNe,
113141
const std::vector<double> &priorPh,
114142
const std::vector<pt_t> &ptCut,
143+
bool useMLAssociation,
144+
const double associationThreshold,
145+
std::string associationGraphPath,
146+
std::vector<double> associationNetworkZ0binning,
147+
std::vector<double> associationNetworkEtaBounds,
148+
std::vector<double> associationNetworkZ0ResBins,
115149
unsigned int nFinalSort,
116150
SortAlgo finalSortAlgo)
117151
: nTrack_(nTrack),
@@ -133,6 +167,7 @@ namespace l1ct {
133167
priorNe_(priorNe),
134168
priorPh_(priorPh),
135169
ptCut_(ptCut),
170+
useMLAssociation_(useMLAssociation),
136171
nFinalSort_(nFinalSort),
137172
finalSortAlgo_(finalSortAlgo),
138173
debug_(false),
@@ -211,6 +246,11 @@ namespace l1ct {
211246
std::vector<double> alphaSlope_, alphaZero_, alphaCrop_;
212247
std::vector<double> priorNe_, priorPh_;
213248
std::vector<pt_t> ptCut_;
249+
250+
// NNVtx Association:
251+
bool useMLAssociation_;
252+
std::unique_ptr<NNVtxAssoc> nnVtxAssoc_;
253+
214254
unsigned int nFinalSort_; // output after a full sort of charged + neutral
215255
SortAlgo finalSortAlgo_;
216256

L1Trigger/Phase2L1ParticleFlow/python/l1ctLayer1_cff.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
from L1Trigger.Phase2L1ParticleFlow.l1TkEgAlgoEmulator_cfi import tkEgAlgoParameters,tkEgSorterParameters
1111

12+
from L1Trigger.Phase2L1ParticleFlow.mlAssociation_cfi import NNVtxAssociationPSet
13+
switchOnNNAssoc = cms.bool(False)
14+
1215
l1tLayer1Barrel = cms.EDProducer("L1TCorrelatorLayer1Producer",
1316
tracks = cms.InputTag('l1tPFTracksFromL1Tracks'),
1417
muons = cms.InputTag('l1tSAMuonsGmt','prompt'),
@@ -81,6 +84,8 @@
8184
alphaCrop = cms.vdouble( 4 ), # max. absolute value for alpha term
8285
priors = cms.vdouble( 5.0 ),
8386
priorsPhoton = cms.vdouble( 1.0 ),
87+
useMLAssociation = switchOnNNAssoc, #Enable Association Network
88+
NNVtxAssociation = NNVtxAssociationPSet,
8489
),
8590
tkEgAlgoParameters = tkEgAlgoParameters.clone(
8691
nTRACK = 25,
@@ -221,6 +226,8 @@
221226
alphaCrop = cms.vdouble( 3 , 3 ), # max. absolute value for alpha term
222227
priors = cms.vdouble( 5.0, 5.0 ),
223228
priorsPhoton = cms.vdouble( 1.5, 1.5 ), #When e/g ID not applied, use: cms.vdouble( 3.5, 3.5, 7.0 ),
229+
useMLAssociation = switchOnNNAssoc, #Enable Association Network
230+
NNVtxAssociation = NNVtxAssociationPSet,
224231
),
225232
tkEgAlgoParameters = tkEgAlgoParameters.clone(
226233
nTRACK = 30,
@@ -327,6 +334,8 @@
327334
alphaCrop = cms.vdouble( 4 ), # max. absolute value for alpha term
328335
priors = cms.vdouble( 7.0 ),
329336
priorsPhoton = cms.vdouble( 5.0 ), #When e/g ID not applied, use: cms.vdouble( 3.5, 3.5, 7.0 ),
337+
useMLAssociation = switchOnNNAssoc, #Enable Association Network
338+
NNVtxAssociation = NNVtxAssociationPSet,
330339
),
331340
tkEgAlgoParameters = tkEgAlgoParameters.clone(
332341
nTRACK = 30,
@@ -396,7 +405,9 @@
396405
alphaCrop = cms.vdouble( 4 ),
397406
priors = cms.vdouble( 6.0 ),
398407
priorsPhoton = cms.vdouble( 6.0 ),
399-
debug = cms.untracked.bool(False)
408+
debug = cms.untracked.bool(False),
409+
useMLAssociation = switchOnNNAssoc, #Enable Association Network
410+
NNVtxAssociation = NNVtxAssociationPSet,
400411
),
401412
tkEgAlgoParameters = tkEgAlgoParameters.clone(
402413
nTRACK = 5, # to be defined
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import FWCore.ParameterSet.Config as cms
2+
from L1Trigger.VertexFinder.l1tVertexProducer_cfi import l1tVertexProducer
3+
4+
# NNVertex Association Variables
5+
NNVtxAssociationPSet = cms.PSet(
6+
associationThreshold = cms.double(0.1), #Association Network threshold for PV tracks
7+
associationGraph = cms.string("L1Trigger/L1TTrackMatch/data/NNVtx_AssociationModelGraph.pb"),
8+
associationNetworkZ0binning = l1tVertexProducer.VertexReconstruction.FH_HistogramParameters, #Z0 binning used for setting the input feature digitisation
9+
associationNetworkEtaBounds = cms.vdouble(0.0, 0.01984126984126984, 0.03968253968253968, 0.05952380952380952, 0.07936507936507936, 0.0992063492063492, 0.11904761904761904, 0.1388888888888889, 0.15873015873015872, 0.17857142857142855, 0.1984126984126984, 0.21825396825396826, 0.23809523809523808, 0.2579365079365079, 0.2777777777777778, 0.2976190476190476, 0.31746031746031744, 0.33730158730158727, 0.3571428571428571, 0.376984126984127, 0.3968253968253968, 0.41666666666666663, 0.4365079365079365, 0.45634920634920634, 0.47619047619047616, 0.496031746031746, 0.5158730158730158, 0.5357142857142857, 0.5555555555555556, 0.5753968253968254, 0.5952380952380952, 0.615079365079365, 0.6349206349206349, 0.6547619047619048, 0.6746031746031745, 0.6944444444444444, 0.7142857142857142, 0.7341269841269841, 0.753968253968254, 0.7738095238095237, 0.7936507936507936, 0.8134920634920635, 0.8333333333333333, 0.8531746031746031, 0.873015873015873, 0.8928571428571428, 0.9126984126984127, 0.9325396825396824, 0.9523809523809523, 0.9722222222222222, 0.992063492063492, 1.0119047619047619, 1.0317460317460316, 1.0515873015873016, 1.0714285714285714, 1.0912698412698412, 1.1111111111111112, 1.130952380952381, 1.1507936507936507, 1.1706349206349205, 1.1904761904761905, 1.2103174603174602, 1.23015873015873, 1.25, 1.2698412698412698, 1.2896825396825395, 1.3095238095238095, 1.3293650793650793, 1.349206349206349, 1.369047619047619, 1.3888888888888888, 1.4087301587301586, 1.4285714285714284, 1.4484126984126984, 1.4682539682539681, 1.488095238095238, 1.507936507936508, 1.5277777777777777, 1.5476190476190474, 1.5674603174603174, 1.5873015873015872, 1.607142857142857, 1.626984126984127, 1.6468253968253967, 1.6666666666666665, 1.6865079365079365, 1.7063492063492063, 1.726190476190476, 1.746031746031746, 1.7658730158730158, 1.7857142857142856, 1.8055555555555554, 1.8253968253968254, 1.8452380952380951, 1.865079365079365, 1.8849206349206349, 1.9047619047619047, 1.9246031746031744, 1.9444444444444444, 1.9642857142857142, 1.984126984126984, 2.003968253968254, 2.0238095238095237, 2.0436507936507935, 2.0634920634920633, 2.083333333333333, 2.1031746031746033, 2.123015873015873, 2.142857142857143, 2.1626984126984126, 2.1825396825396823, 2.202380952380952, 2.2222222222222223, 2.242063492063492, 2.261904761904762, 2.2817460317460316, 2.3015873015873014, 2.321428571428571, 2.341269841269841, 2.361111111111111, 2.380952380952381, 2.4007936507936507, 2.4206349206349205, 2.4404761904761902, 2.46031746031746, 2.4801587301587302, 2.5), #Eta bounds used to set z0 resolution input feature
10+
associationNetworkZ0ResBins = cms.vdouble(127.0, 126.0, 126.0, 126.0, 125.0, 124.0, 123.0, 122.0, 120.0, 119.0, 117.0, 115.0, 114.0, 112.0, 110.0, 107.0, 105.0, 103.0, 101.0, 98.0, 96.0, 94.0, 91.0, 89.0, 87.0, 85.0, 82.0, 80.0, 78.0, 76.0, 74.0, 72.0, 70.0, 68.0, 66.0, 64.0, 62.0, 61.0, 59.0, 57.0, 56.0, 54.0, 53.0, 51.0, 50.0, 48.0, 47.0, 46.0, 45.0, 43.0, 42.0, 41.0, 40.0, 39.0, 38.0, 37.0, 36.0, 35.0, 34.0, 33.0, 33.0, 32.0, 31.0, 30.0, 30.0, 29.0, 28.0, 28.0, 27.0, 26.0, 26.0, 25.0, 24.0, 24.0, 23.0, 23.0, 22.0, 22.0, 21.0, 21.0, 21.0, 20.0, 20.0, 19.0, 19.0, 18.0, 18.0, 18.0, 17.0, 17.0, 17.0, 16.0, 16.0, 16.0, 15.0, 15.0, 15.0, 15.0, 14.0, 14.0, 14.0, 14.0, 13.0, 13.0, 13.0, 13.0, 12.0, 12.0, 12.0, 12.0, 12.0, 11.0, 11.0, 11.0, 11.0, 11.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 9.0, 9.0, 9.0, 9.0, 9.0, 0.0), #z0 resolution input feature bins
11+
)
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// Package: L1Trigger/Phase2L1ParticleFlow
2+
// Class: NNVtxAssoc
3+
// Description: Designed to run the track to vertex associations created by the E2E NNVtx.
4+
// TTTrackNetworkSelector either accepts or rejects that a PF object's (t) track is associated to a vertex (v).
5+
// Authors: Kai Hong Law and Benjamin Radburn-Smith
6+
// Created: February 2025
7+
8+
#include "L1Trigger/Phase2L1ParticleFlow/interface/NNVtxAssoc.h"
9+
#include "DataFormats/L1TParticleFlow/interface/PFTrack.h"
10+
#include "PhysicsTools/TensorFlow/interface/TensorFlow.h"
11+
#include <iomanip>
12+
13+
NNVtxAssoc::NNVtxAssoc(std::string AssociationGraphPath,
14+
const double AssociationThreshold,
15+
const std::vector<double>& AssociationNetworkZ0binning,
16+
const std::vector<double>& AssociationNetworkEtaBounds,
17+
const std::vector<double>& AssociationNetworkZ0ResBins)
18+
: associationThreshold_(AssociationThreshold),
19+
z0_binning_(AssociationNetworkZ0binning),
20+
eta_bins_(AssociationNetworkEtaBounds),
21+
res_bins_(AssociationNetworkZ0ResBins) {
22+
tensorflow::GraphDef* associationGraph_ = tensorflow::loadGraphDef(AssociationGraphPath);
23+
associationSesh_ = tensorflow::createSession(associationGraph_);
24+
log_.setf(std::ios::fixed, std::ios::floatfield);
25+
log_.precision(3);
26+
}
27+
28+
template <typename T>
29+
bool NNVtxAssoc::TTTrackNetworkSelector(const PFRegionEmu& region, const T& t, const l1ct::PVObjEmu& v) {
30+
tensorflow::Tensor inputAssoc(tensorflow::DT_FLOAT, {1, 4});
31+
std::vector<tensorflow::Tensor> outputAssoc;
32+
33+
auto lower = std::lower_bound(eta_bins_.begin(), eta_bins_.end(), region.floatGlbEta(t.hwVtxEta()));
34+
35+
int resbin = std::distance(eta_bins_.begin(), lower);
36+
float binWidth = z0_binning_[2];
37+
// Calculate integer dZ from track z0 and vertex z0 (use floating point version and convert internally allowing use of both emulator and simulator vertex and track)
38+
float dZ =
39+
abs(floor(((t.floatZ0() + z0_binning_[1]) / (binWidth))) - floor(((v.floatZ0() + z0_binning_[1]) / (binWidth))));
40+
41+
// The following constants <22, 9> are defined by the quantisation of the Neural Network
42+
ap_ufixed<22, 9> ptEmulation_rescale = t.hwPt;
43+
ap_ufixed<22, 9> resBinEmulation_rescale = res_bins_[resbin];
44+
ap_ufixed<22, 9> MVAEmulation_rescale = 0;
45+
ap_ufixed<22, 9> dZEmulation_rescale = dZ;
46+
47+
// Deal with this template class using 2 different objects (t) which have different calls to their PFTracks:
48+
const l1t::PFTrack* srcTrack = nullptr;
49+
if constexpr (std::is_same_v<T, const l1ct::TkObjEmu>)
50+
srcTrack = t.src;
51+
else if constexpr (std::is_same_v<T, const l1ct::PFChargedObjEmu>)
52+
srcTrack = t.srcTrack;
53+
if (srcTrack)
54+
MVAEmulation_rescale = srcTrack->trackWord().getMVAQualityBits();
55+
56+
inputAssoc.tensor<float, 2>()(0, 0) = ptEmulation_rescale.to_double();
57+
inputAssoc.tensor<float, 2>()(0, 1) = MVAEmulation_rescale.to_double();
58+
inputAssoc.tensor<float, 2>()(0, 2) = resBinEmulation_rescale.to_double() / 16.0;
59+
inputAssoc.tensor<float, 2>()(0, 3) = dZEmulation_rescale.to_double();
60+
61+
// Run Association Network:
62+
tensorflow::run(associationSesh_, {{"NNvtx_track_association:0", inputAssoc}}, {"Identity:0"}, &outputAssoc);
63+
64+
double NNOutput = (double)outputAssoc[0].tensor<float, 2>()(0, 0);
65+
double NNOutput_exp = 1.0 / (1.0 + exp(-1.0 * (NNOutput)));
66+
67+
return NNOutput_exp >= associationThreshold_;
68+
}
69+
70+
#ifdef CMSSW_GIT_HASH
71+
#include "FWCore/ParameterSet/interface/ParameterSet.h"
72+
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
73+
74+
edm::ParameterSetDescription NNVtxAssoc::getParameterSetDescription() {
75+
edm::ParameterSetDescription description;
76+
description.add<double>("associationThreshold");
77+
description.add<std::string>("associationGraph");
78+
description.add<std::vector<double>>("associationNetworkZ0binning");
79+
description.add<std::vector<double>>("associationNetworkEtaBounds");
80+
description.add<std::vector<double>>("associationNetworkZ0ResBins");
81+
return description;
82+
}
83+
#endif
84+
85+
void NNVtxAssoc::NNVtxAssocDebug() {
86+
log_ << "-- NNVtxAssocDebug --\n";
87+
log_ << "AssociationThreshold: " << this->associationThreshold_ << "\n";
88+
log_ << "z0_binning: ";
89+
for (auto i : this->z0_binning_)
90+
log_ << i << " ";
91+
log_ << "\n";
92+
log_ << "eta_bins: ";
93+
for (auto i : this->eta_bins_)
94+
log_ << i << " ";
95+
log_ << "\n";
96+
log_ << "res_bins: ";
97+
for (auto i : this->res_bins_)
98+
log_ << i << " ";
99+
log_ << "\n";
100+
edm::LogPrint("NNVtxAssoc") << log_.str();
101+
}
102+
103+
template bool NNVtxAssoc::TTTrackNetworkSelector<const l1ct::TkObjEmu>(const PFRegionEmu&,
104+
const l1ct::TkObjEmu&,
105+
const l1ct::PVObjEmu&);
106+
template bool NNVtxAssoc::TTTrackNetworkSelector<const l1ct::PFChargedObjEmu>(const PFRegionEmu&,
107+
const l1ct::PFChargedObjEmu&,
108+
const l1ct::PVObjEmu&);

0 commit comments

Comments
 (0)