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

Bin TQ MVA output #272

Merged
merged 13 commits into from
May 17, 2024
6 changes: 3 additions & 3 deletions DataFormats/L1TrackTrigger/interface/TTTrack.h
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ double TTTrack<T>::chi2ZRed() const {
return theChi2_Z_ / (theStubRefs.size() - 2.);
}

/// prompt track quality MVA
template <typename T>
double TTTrack<T>::trkMVA1() const {
return theTrkMVA1_;
Expand Down Expand Up @@ -443,7 +444,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 +457,7 @@ void TTTrack<T>::setTrackWordBits() {
0,
theStubPtConsistency_,
theHitPattern_,
mvaQuality,
theTrkMVA1_,
mvaOther,
thePhiSector_);
} else {
Expand All @@ -469,7 +469,7 @@ void TTTrack<T>::setTrackWordBits() {
chi2ZRed(),
chi2BendRed(),
theHitPattern_,
mvaQuality,
theTrkMVA1_,
mvaOther,
thePhiSector_);
}
Expand Down
16 changes: 10 additions & 6 deletions DataFormats/L1TrackTrigger/interface/TTTrack_TrackWord.h
cgsavard marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ class TTTrack_TrackWord {
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}};

// Bin edges for TQ MVA
static constexpr std::array<double, 1 << TrackBitWidths::kMVAQualitySize> tqMVABins = {
{0, 0.125, 0.25, 0.375, 0.5, 0.625, 0.750, 0.875}};

// Sector constants
static constexpr unsigned int nSectors = 9;
static constexpr double sectorWidth = (2. * M_PI) / nSectors;
Expand Down Expand Up @@ -141,8 +145,8 @@ class TTTrack_TrackWord {
double chi2RZ,
double bendChi2,
unsigned int hitPattern,
unsigned int mvaQuality,
unsigned int mvaOther,
double mvaQuality,
double mvaOther,
unsigned int sector);
TTTrack_TrackWord(unsigned int valid,
unsigned int rInv,
Expand Down Expand Up @@ -224,8 +228,8 @@ 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(); }
unsigned int getMVAOther() const { return getMVAOtherBits(); }
double getMVAQuality() const { return tqMVABins[getMVAQualityBits()]; }
double getMVAOther() const { return getMVAOtherBits(); }

// ----------member functions (setters) ------------
void setTrackWord(unsigned int valid,
Expand All @@ -236,8 +240,8 @@ class TTTrack_TrackWord {
double chi2RZ,
double bendChi2,
unsigned int hitPattern,
unsigned int mvaQuality,
unsigned int mvaOther,
double mvaQuality,
double mvaOther,
unsigned int sector);

void setTrackWord(unsigned int valid,
Expand Down
10 changes: 5 additions & 5 deletions DataFormats/L1TrackTrigger/src/TTTrack_TrackWord.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ TTTrack_TrackWord::TTTrack_TrackWord(unsigned int valid,
double chi2RZ,
double bendChi2,
unsigned int hitPattern,
unsigned int mvaQuality,
unsigned int mvaOther,
double mvaQuality,
double mvaOther,
unsigned int sector) {
setTrackWord(valid, momentum, POCA, rInv, chi2RPhi, chi2RZ, bendChi2, hitPattern, mvaQuality, mvaOther, sector);
}
Expand Down Expand Up @@ -65,8 +65,8 @@ void TTTrack_TrackWord::setTrackWord(unsigned int valid,
double chi2RZ,
double bendChi2,
unsigned int hitPattern,
unsigned int mvaQuality,
unsigned int mvaOther,
double mvaQuality,
double mvaOther,
unsigned int sector) {
// first, derive quantities to be packed
float rPhi = localPhi(momentum.phi(), sector); // this needs to be phi relative to the center of the sector
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, tqMVABins);
otherMVA_t mvaOther_ = mvaOther;

// pack the track word
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ void L1FPGATrackProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSe
aTrack.setStubPtConsistency(
StubPtConsistency::getConsistency(aTrack, theTrackerGeom, tTopo, settings_.bfield(), settings_.nHelixPar()));

// set TTTrack word
// set track word before TQ MVA calculated which uses track word variables
aTrack.setTrackWordBits();

if (trackQuality_) {
Expand All @@ -764,6 +764,9 @@ void L1FPGATrackProducer::produce(edm::Event& iEvent, const edm::EventSetup& iSe
// test track word
//aTrack.testTrackWordBits();

// set track word again to set MVA variable from TTTrack into track word
aTrack.setTrackWordBits();
cgsavard marked this conversation as resolved.
Show resolved Hide resolved

L1TkTracksForOutput->push_back(aTrack);
}

Expand Down
18 changes: 9 additions & 9 deletions L1Trigger/TrackFindingTracklet/plugins/ProducerKFout.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ namespace trklet {
vector<double> dZBins_;

std::unique_ptr<L1TrackQuality> trackQualityModel_;
vector<int> tqBins_;
double tqTanlScale_;
double tqZ0Scale_;
static constexpr double ap_fixed_rescale = 32.0;
Expand All @@ -78,13 +77,14 @@ namespace trklet {

int partialTrackWordBits_;

// Helper function to convert floating chi2 to chi2 bin
// Helper function to convert floating value to bin
template <typename T>
unsigned int digitise(const T& bins, double value, double factor) {
unsigned int bin = 0;
for (unsigned int i = 0; i < bins.size() - 1; i++) {
if (value * factor > bins[i] && value * factor <= bins[i + 1])
bin = i;
break;
bin++;
}
return bin;
}
Expand All @@ -111,7 +111,6 @@ namespace trklet {

trackQualityModel_ = std::make_unique<L1TrackQuality>(iConfig.getParameter<edm::ParameterSet>("TrackQualityPSet"));
edm::ParameterSet trackQualityPSset = iConfig.getParameter<edm::ParameterSet>("TrackQualityPSet");
tqBins_ = trackQualityPSset.getParameter<vector<int>>("tqemu_bins");
tqTanlScale_ = trackQualityPSset.getParameter<double>("tqemu_TanlScale");
tqZ0Scale_ = trackQualityPSset.getParameter<double>("tqemu_Z0Scale");
}
Expand Down Expand Up @@ -200,7 +199,7 @@ namespace trklet {
StreamsTrack outputStreamsTracks(setup_->numRegions() * setup_->tfpNumChannel());

// Setup containers for track quality
float tempTQMVA = 0.0;
float tempTQMVAPreSig = 0.0;
// Due to ap_fixed implementation in CMSSW this 10,5 must be specified at compile time, TODO make this a changeable parameter
std::vector<ap_fixed<10, 5>> trackQuality_inputs = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};

Expand Down Expand Up @@ -295,10 +294,11 @@ namespace trklet {
digitise(TTTrack_TrackWord::chi2RZBins, tempchi2rz, (double)setup_->kfoutchi2rzConv())};

// Run BDT emulation and package output into 3 bits

tempTQMVA = trackQualityModel_->runEmulatedTQ(trackQuality_inputs);
tempTQMVA = std::trunc(tempTQMVA * ap_fixed_rescale);
TTBV tqMVA(digitise(tqBins_, tempTQMVA, 1.0), TTTrack_TrackWord::TrackBitWidths::kMVAQualitySize, false);
// output needs sigmoid transformation applied
tempTQMVAPreSig = trackQualityModel_->runEmulatedTQ(trackQuality_inputs);
TTBV tqMVA(digitise(L1TrackQuality::getTqMVAPreSigBins(), tempTQMVAPreSig, 1.0),
TTTrack_TrackWord::TrackBitWidths::kMVAQualitySize,
false);

// Build 32 bit partial tracks for outputting in 64 bit packets
// 12 + 3 + 7 + 3 + 6
Expand Down
41 changes: 15 additions & 26 deletions L1Trigger/TrackTrigger/interface/L1TrackQuality.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,13 @@ C.Brown 28/07/20
#include "DataFormats/L1TrackTrigger/interface/TTTrack.h"
#include "DataFormats/L1TrackTrigger/interface/TTTrack_TrackWord.h"
#include "DataFormats/L1TrackTrigger/interface/TTTypes.h"
#include "PhysicsTools/ONNXRuntime/interface/ONNXRuntime.h"
#include <memory>

#include "conifer.h"
#include "ap_fixed.h"

class L1TrackQuality {
public:
// Enum class used for determining prediction behaviour in setL1TrackQuality
enum class QualityAlgorithm { Cut, GBDT, GBDT_cpp, NN, None };

//Default Constructor
L1TrackQuality();

Expand All @@ -49,36 +45,29 @@ class L1TrackQuality {
// and a single output to be returned which is then used to fill the bits in the Track Word for situations
// where a TTTrack datatype is unavailable to be passed to the track quality
float runEmulatedTQ(std::vector<ap_fixed<10, 5>> inputFeatures);
// To set private member data
void setCutParameters(std::string const& AlgorithmString,
float maxZ0,
float maxEta,
float chi2dofMax,
float bendchi2Max,
float minPt,
int nStubmin);

void setONNXModel(std::string const& AlgorithmString,
edm::FileInPath const& ONNXmodel,
std::string const& ONNXInputName,
std::vector<std::string> const& featureNames);
void setModel(edm::FileInPath const& model, std::vector<std::string> const& featureNames);

void setBonusFeatures(std::vector<float> bonusFeatures);

// TQ MVA bin conversions
static constexpr double invSigmoid(double value) { return -log(1. / value - 1.); }
static constexpr std::array<double, 1 << TTTrack_TrackWord::TrackBitWidths::kMVAQualitySize> getTqMVAPreSigBins() {
return {{-16.,
invSigmoid(TTTrack_TrackWord::tqMVABins[1]),
invSigmoid(TTTrack_TrackWord::tqMVABins[2]),
invSigmoid(TTTrack_TrackWord::tqMVABins[3]),
invSigmoid(TTTrack_TrackWord::tqMVABins[4]),
invSigmoid(TTTrack_TrackWord::tqMVABins[5]),
invSigmoid(TTTrack_TrackWord::tqMVABins[6]),
invSigmoid(TTTrack_TrackWord::tqMVABins[7])}};
}

private:
// Private Member Data
QualityAlgorithm qualityAlgorithm_ = QualityAlgorithm::None;
edm::FileInPath ONNXmodel_;
std::string ONNXInputName_;
edm::FileInPath model_;
std::vector<std::string> featureNames_;
float maxZ0_;
float maxEta_;
float chi2dofMax_;
float bendchi2Max_;
float minPt_;
int nStubsmin_;
bool useHPH_;
std::vector<float> bonusFeatures_;
std::unique_ptr<cms::Ort::ONNXRuntime> runTime_;
};
#endif
17 changes: 2 additions & 15 deletions L1Trigger/TrackTrigger/python/TrackQualityParams_cfi.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
import FWCore.ParameterSet.Config as cms

TrackQualityParams = cms.PSet(qualityAlgorithm = cms.string("GBDT_cpp"), #None, Cut, NN, GBDT, GBDT_cpp
# This emulation GBDT is optimised for the HYBRID_NEWKF emulation and works with the emulation of the KF out module
TrackQualityParams = cms.PSet(# This emulation GBDT is optimised for the HYBRID_NEWKF emulation and works with the emulation of the KF out module
# It is compatible with the HYBRID simulation and will give equivilant performance with this workflow
ONNXmodel = cms.FileInPath("L1Trigger/TrackTrigger/data/L1_TrackQuality_GBDT_emulation_digitized.json"),
# The ONNX model should be found at this path, if you want a local version of the model:
# git clone https://github.com/cms-data/L1Trigger-TrackTrigger.git L1Trigger/TrackTrigger/data
ONNXInputName = cms.string("feature_input"),
model = cms.FileInPath("L1Trigger/TrackTrigger/data/L1_TrackQuality_GBDT_emulation_digitized.json"),
#Vector of strings of training features, in the order that the model was trained with
featureNames = cms.vstring(["tanl", "z0_scaled", "bendchi2_bin", "nstub",
"nlaymiss_interior", "chi2rphi_bin", "chi2rz_bin"]),
# Parameters for cut based classifier, optimized for L1 Track MET
# (Table 3.7 The Phase-2 Upgrade of the CMS Level-1 Trigger http://cds.cern.ch/record/2714892)
maxZ0 = cms.double ( 15. ) , # in cm
maxEta = cms.double ( 2.4 ) ,
chi2dofMax = cms.double( 40. ),
bendchi2Max = cms.double( 2.4 ),
minPt = cms.double( 2. ), # in GeV
nStubsmin = cms.int32( 4 ),
tqemu_bins = cms.vint32( [-480, -62, -35, -16, 0, 16, 35, 62, 480] ),
tqemu_TanlScale = cms.double( 128.0),
tqemu_Z0Scale = cms.double( 64.0 ),
)
Loading