Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

E2E NNVtx: A Phase 2 L1T Vertexing algorithm using End to End NN #44315

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions DataFormats/L1TrackTrigger/interface/TTTrack.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,6 @@ void TTTrack<T>::setTrackWordBits() {
}

unsigned int valid = true;
unsigned int mvaQuality = 0;
unsigned int mvaOther = 0;

// missing conversion of global phi to difference from sector center phi
Expand All @@ -457,7 +456,7 @@ void TTTrack<T>::setTrackWordBits() {
0,
theStubPtConsistency_,
theHitPattern_,
mvaQuality,
theTrkMVA1_,
mvaOther,
thePhiSector_);
} else {
Expand All @@ -469,7 +468,7 @@ void TTTrack<T>::setTrackWordBits() {
chi2ZRed(),
chi2BendRed(),
theHitPattern_,
mvaQuality,
theTrkMVA1_,
mvaOther,
thePhiSector_);
}
Expand Down
8 changes: 5 additions & 3 deletions DataFormats/L1TrackTrigger/interface/TTTrack_TrackWord.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ class TTTrack_TrackWord {
{0.0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 6.0, 8.0, 10.0, 20.0, 50.0}};
static constexpr std::array<double, 1 << TrackBitWidths::kBendChi2Size> bendChi2Bins = {
{0.0, 0.75, 1.0, 1.5, 2.25, 3.5, 5.0, 20.0}};
static constexpr std::array<double, 1 << TrackBitWidths::kMVAQualitySize> mvaQualityBins = {
{0.0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.750, 0.875}};

// Sector constants
static constexpr unsigned int nSectors = 9;
Expand Down Expand Up @@ -141,7 +143,7 @@ class TTTrack_TrackWord {
double chi2RZ,
double bendChi2,
unsigned int hitPattern,
unsigned int mvaQuality,
double mvaQuality,
unsigned int mvaOther,
unsigned int sector);
TTTrack_TrackWord(unsigned int valid,
Expand Down Expand Up @@ -224,7 +226,7 @@ class TTTrack_TrackWord {
double getBendChi2() const { return bendChi2Bins[getBendChi2Bits()]; }
unsigned int getHitPattern() const { return getHitPatternBits(); }
unsigned int getNStubs() const { return countSetBits(getHitPatternBits()); }
unsigned int getMVAQuality() const { return getMVAQualityBits(); }
double getMVAQuality() const { return mvaQualityBins[getMVAQualityBits()]; }
unsigned int getMVAOther() const { return getMVAOtherBits(); }

// ----------member functions (setters) ------------
Expand All @@ -236,7 +238,7 @@ class TTTrack_TrackWord {
double chi2RZ,
double bendChi2,
unsigned int hitPattern,
unsigned int mvaQuality,
double mvaQuality,
unsigned int mvaOther,
unsigned int sector);

Expand Down
6 changes: 3 additions & 3 deletions DataFormats/L1TrackTrigger/src/TTTrack_TrackWord.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ TTTrack_TrackWord::TTTrack_TrackWord(unsigned int valid,
double chi2RZ,
double bendChi2,
unsigned int hitPattern,
unsigned int mvaQuality,
double mvaQuality,
unsigned int mvaOther,
unsigned int sector) {
setTrackWord(valid, momentum, POCA, rInv, chi2RPhi, chi2RZ, bendChi2, hitPattern, mvaQuality, mvaOther, sector);
Expand Down Expand Up @@ -65,7 +65,7 @@ void TTTrack_TrackWord::setTrackWord(unsigned int valid,
double chi2RZ,
double bendChi2,
unsigned int hitPattern,
unsigned int mvaQuality,
double mvaQuality,
unsigned int mvaOther,
unsigned int sector) {
// first, derive quantities to be packed
Expand All @@ -85,7 +85,7 @@ void TTTrack_TrackWord::setTrackWord(unsigned int valid,
chi2rz_t chi2RZ_ = getBin(chi2RZ, chi2RZBins);
bendChi2_t bendChi2_ = getBin(bendChi2, bendChi2Bins);
hit_t hitPattern_ = hitPattern;
qualityMVA_t mvaQuality_ = mvaQuality;
qualityMVA_t mvaQuality_ = getBin(mvaQuality, mvaQualityBins);
otherMVA_t mvaOther_ = mvaOther;

// pack the track word
Expand Down
1 change: 1 addition & 0 deletions L1Trigger/L1TTrackMatch/BuildFile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<use name="DataFormats/L1Trigger"/>
<use name="DataFormats/L1TrackTrigger"/>
<use name="DataFormats/Math"/>
<use name="PhysicsTools/TensorFlow"/>
<use name="boost"/>
<use name="xerces-c"/>
<use name="clhep"/>
Expand Down
126 changes: 119 additions & 7 deletions L1Trigger/L1TTrackMatch/plugins/L1TrackVertexAssociationProducer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
std::vector<TTTrack> - Each floating point TTTrack inside this collection inherits from
a bit-accurate TTTrack_TrackWord, used for emulation purposes.
Outputs:
std::vector<TTTrack> - A collection of TTTracks selected from cuts on the TTTrack properties
std::vector<TTTrack> - A collection of TTTracks selected from cuts on the TTTrack_TrackWord properties
std::vector<TTTrackRef> - A collection of TTTrack Refs selected from cuts on the TTTrack properties
std::vector<TTTrackRef> - A collection of TTTrack Refs selected from cuts on the TTTrack_TrackWord properties
*/
//
// Original Author: Alexx Perloff
Expand Down Expand Up @@ -62,7 +62,9 @@
#include "FWCore/Utilities/interface/EDMException.h"
#include "FWCore/Utilities/interface/StreamID.h"
#include "Geometry/Records/interface/TrackerTopologyRcd.h"
#include "PhysicsTools/TensorFlow/interface/TensorFlow.h"
#include "L1Trigger/DemonstratorTools/interface/codecs/tracks.h"
#include "FWCore/ParameterSet/interface/FileInPath.h"

//
// class declaration
Expand Down Expand Up @@ -149,6 +151,76 @@ class L1TrackVertexAssociationProducer : public edm::global::EDProducer<> {
std::vector<double> deltaZMaxEtaBounds_;
std::vector<double> deltaZMax_;
};

struct NNTrackWordSelector {
NNTrackWordSelector(tensorflow::Session* AssociationSesh,
const double AssociationThreshold,
const std::vector<double>& AssociationNetworkZ0binning,
const std::vector<double>& AssociationNetworkEtaBounds,
const std::vector<double>& AssociationNetworkZ0ResBins)
: AssociationSesh_(AssociationSesh),
AssociationThreshold_(AssociationThreshold),
z0_binning_(AssociationNetworkZ0binning),
eta_bins_(AssociationNetworkEtaBounds),
res_bins_(AssociationNetworkZ0ResBins) {}

bool operator()(const TTTrackType& t, const l1t::VertexWord& v) const {
tensorflow::Tensor inputAssoc(tensorflow::DT_FLOAT, {1, 4});
std::vector<tensorflow::Tensor> outputAssoc;

TTTrack_TrackWord::tanl_t etaEmulationBits = t.getTanlWord();
ap_fixed<16, 3> etaEmulation;
etaEmulation.V = (etaEmulationBits.range());

auto lower = std::lower_bound(eta_bins_.begin(), eta_bins_.end(), etaEmulation.to_double());

int resbin = std::distance(eta_bins_.begin(), lower);
float binWidth = z0_binning_[2];
// 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)
float dZ =
abs(floor(((t.getZ0() + z0_binning_[1]) / (binWidth))) - floor(((v.z0() + z0_binning_[1]) / (binWidth))));

// The following constants <14, 9>, <22, 9> are defined by the quantisation of the Neural Network
ap_uint<14> ptEmulationBits = t.getTrackWord()(TTTrack_TrackWord::TrackBitLocations::kRinvMSB - 1,
TTTrack_TrackWord::TrackBitLocations::kRinvLSB);
ap_ufixed<14, 9> ptEmulation;
ptEmulation.V = (ptEmulationBits.range());

ap_ufixed<22, 9> ptEmulation_rescale;
ptEmulation_rescale = ptEmulation.to_double();

ap_ufixed<22, 9> resBinEmulation_rescale;
resBinEmulation_rescale = res_bins_[resbin];

ap_ufixed<22, 9> MVAEmulation_rescale;
MVAEmulation_rescale = t.getMVAQualityBits();

ap_ufixed<22, 9> dZEmulation_rescale;
dZEmulation_rescale = dZ;

inputAssoc.tensor<float, 2>()(0, 0) = ptEmulation_rescale.to_double();
inputAssoc.tensor<float, 2>()(0, 1) = MVAEmulation_rescale.to_double();
inputAssoc.tensor<float, 2>()(0, 2) = resBinEmulation_rescale.to_double() / 16.0;
inputAssoc.tensor<float, 2>()(0, 3) = dZEmulation_rescale.to_double();

// Run Association Network:
tensorflow::run(AssociationSesh_, {{"assoc:0", inputAssoc}}, {"Identity:0"}, &outputAssoc);

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

double NNOutput_exp = 1.0 / (1.0 + exp(-1.0 * (NNOutput)));

return NNOutput_exp >= AssociationThreshold_;
}

private:
tensorflow::Session* AssociationSesh_;
double AssociationThreshold_;
std::vector<double> z0_binning_;
std::vector<double> eta_bins_;
std::vector<double> res_bins_;
};

struct TTTrackWordLinkLimitSelector {
TTTrackWordLinkLimitSelector(const unsigned int fwNTrackSetsTVA) : fwNTrackSetsTVA_(fwNTrackSetsTVA) {
//create a counter for all 18 GTT input links, 2 per phiSector of the TrackFindingProcessors
Expand Down Expand Up @@ -209,6 +281,15 @@ class L1TrackVertexAssociationProducer : public edm::global::EDProducer<> {
const double useDisplacedTracksDeltaZOverride_;
// corresponds to N_TRACK_SETS_TVA in LibHLS https://gitlab.cern.ch/GTT/LibHLS/-/blob/master/DataFormats/Track/interface/TrackConstants.h
const unsigned int fwNTrackSetsTVA_;

//NNVtx:
edm::FileInPath associationGraphPath_;
const double associationThreshold_;
bool useAssociationNetwork_;
tensorflow::GraphDef* associationGraph_;
tensorflow::Session* associationSesh_;
std::vector<double> associationNetworkZ0binning_, associationNetworkEtaBounds_, associationNetworkZ0ResBins_;

int debug_;
};

Expand Down Expand Up @@ -241,7 +322,17 @@ L1TrackVertexAssociationProducer::L1TrackVertexAssociationProducer(const edm::Pa
deltaZMax_(cutSet_.getParameter<std::vector<double>>("deltaZMax")),
useDisplacedTracksDeltaZOverride_(iConfig.getParameter<double>("useDisplacedTracksDeltaZOverride")),
fwNTrackSetsTVA_(iConfig.getParameter<unsigned int>("fwNTrackSetsTVA")),
associationThreshold_(iConfig.getParameter<double>("associationThreshold")),
useAssociationNetwork_(iConfig.getParameter<bool>("useAssociationNetwork")),
associationNetworkZ0binning_(iConfig.getParameter<std::vector<double>>("associationNetworkZ0binning")),
associationNetworkEtaBounds_(iConfig.getParameter<std::vector<double>>("associationNetworkEtaBounds")),
associationNetworkZ0ResBins_(iConfig.getParameter<std::vector<double>>("associationNetworkZ0ResBins")),
debug_(iConfig.getParameter<int>("debug")) {
if (useAssociationNetwork_) {
associationGraphPath_ = iConfig.getParameter<edm::FileInPath>("associationGraph");
associationGraph_ = tensorflow::loadGraphDef(associationGraphPath_.fullPath());
associationSesh_ = tensorflow::createSession(associationGraph_);
}
// Confirm the the configuration makes sense
if (!processSimulatedTracks_ && !processEmulatedTracks_) {
throw cms::Exception("You must process at least one of the track collections (simulated or emulated).");
Expand Down Expand Up @@ -408,6 +499,12 @@ void L1TrackVertexAssociationProducer::produce(edm::StreamID, edm::Event& iEvent
TTTrackDeltaZMaxSelector deltaZSel(deltaZMaxEtaBounds_, deltaZMax_);
TTTrackWordDeltaZMaxSelector deltaZSelEmu(deltaZMaxEtaBounds_, deltaZMax_);

NNTrackWordSelector TTTrackNetworkSelector(associationSesh_,
associationThreshold_,
associationNetworkZ0binning_,
associationNetworkEtaBounds_,
associationNetworkZ0ResBins_);

iEvent.getByToken(l1TracksToken_, l1TracksHandle);
size_t nOutputApproximate = l1TracksHandle->size();

Expand Down Expand Up @@ -454,11 +551,17 @@ void L1TrackVertexAssociationProducer::produce(edm::StreamID, edm::Event& iEvent
[track](const auto& ref) { return (*ref).getTrackWord() == track.getTrackWord(); });
bool passSelectionEmu = (itrEmu != l1SelectedTracksEmulationHandle->end());
// Associated tracks based on the bitwise accurate TTTrack_TrackWord
if (passLinkLimitEmu && passSelectionEmu && deltaZSelEmu(track, l1VerticesEmulationHandle->at(0))) {
vTTTrackAssociatedEmulationOutput->push_back(TTTrackRef(l1TracksHandle, i));
} //end block for satisfying LinkLimitEmu and SelectionEmu criteria
} //end if (processEmulatedTracks_)
} //end loop over input converted tracks
if (useAssociationNetwork_) {
if (passLinkLimitEmu && passSelectionEmu && TTTrackNetworkSelector(track, l1VerticesEmulationHandle->at(0))) {
vTTTrackAssociatedEmulationOutput->push_back(TTTrackRef(l1TracksHandle, i));
}
} else {
if (passLinkLimitEmu && passSelectionEmu && deltaZSelEmu(track, l1VerticesEmulationHandle->at(0))) {
vTTTrackAssociatedEmulationOutput->push_back(TTTrackRef(l1TracksHandle, i));
} //end block for satisfying LinkLimitEmu and SelectionEmu criteria
} //end if use track association NN
} //end if (processEmulatedTracks_)
} //end loop over input converted tracks

if (processSimulatedTracks_) {
iEvent.put(std::move(vTTTrackAssociatedOutput), outputCollectionName_);
Expand Down Expand Up @@ -507,6 +610,15 @@ void L1TrackVertexAssociationProducer::fillDescriptions(edm::ConfigurationDescri
desc.add<bool>("processEmulatedTracks", true)
->setComment("return selected tracks after cutting on the bitwise emulated values");
desc.add<unsigned int>("fwNTrackSetsTVA", 94)->setComment("firmware limit on processed tracks per GTT input link");
desc.add<bool>("useAssociationNetwork", false)->setComment("Enable Association Network");
desc.add<double>("associationThreshold", 0)->setComment("Association Network threshold for PV tracks");
desc.addOptional<edm::FileInPath>("associationGraph")->setComment("Location of Association Network model file");
desc.add<std::vector<double>>("associationNetworkZ0binning", {})
->setComment("z0 binning used for setting the input feature digitisation");
desc.add<std::vector<double>>("associationNetworkEtaBounds", {})
->setComment("Eta bounds used to set z0 resolution input feature");
desc.add<std::vector<double>>("associationNetworkZ0ResBins", {})->setComment("z0 resolution input feature bins");

desc.add<int>("debug", 0)->setComment("Verbosity levels: 0, 1, 2, 3");
descriptions.addWithDefaultLabel(desc);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import FWCore.ParameterSet.Config as cms
from L1Trigger.VertexFinder.l1tVertexProducer_cfi import l1tVertexProducer

l1tTrackVertexAssociationProducer = cms.EDProducer('L1TrackVertexAssociationProducer',
l1TracksInputTag = cms.InputTag("l1tGTTInputProducer","Level1TTTracksConverted"),
Expand All @@ -18,7 +19,18 @@
processSimulatedTracks = cms.bool(True), # return selected tracks after cutting on the floating point values
processEmulatedTracks = cms.bool(True), # return selected tracks after cutting on the bitwise emulated values
fwNTrackSetsTVA = cms.uint32(94), # firmware limit on number of GTT converted tracks considered for primary vertex association
debug = cms.int32(0) # Verbosity levels: 0, 1, 2, 3, 4
debug = cms.int32(0), # Verbosity levels: 0, 1, 2, 3, 4
)

l1tTrackVertexNNAssociationProducer = l1tTrackVertexAssociationProducer.clone(
processSimulatedTracks = cms.bool(True), # return selected tracks after cutting on the floating point values
processEmulatedTracks = cms.bool(True), # return selected tracks after cutting on the bitwise emulated values
useAssociationNetwork = cms.bool(True), #Enable Association Network
associationThreshold = cms.double(0.1), #Association Network threshold for PV tracks
associationGraph = cms.FileInPath("L1Trigger/L1TTrackMatch/data/NNVtx_AssociationModelGraph.pb"), #Location of Association Network model file
associationNetworkZ0binning = l1tVertexProducer.VertexReconstruction.FH_HistogramParameters, #Z0 binning used for setting the input feature digitisation
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
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
)

l1tTrackVertexAssociationProducerExtended = l1tTrackVertexAssociationProducer.clone(
Expand Down
Loading