Skip to content

Commit

Permalink
Propagate multiclass weights + varous cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cerminar committed Dec 17, 2024
1 parent d033f59 commit f950009
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,7 @@ namespace l1ct {
const std::vector<TkObjEmu> &track,
const PFTkEGAlgoEmuConfig::CompIDParameters &params) const;

id_score_t compute_composite_score_eb(const PFRegionEmu &r,
CompositeCandidate &cand,
id_score_t compute_composite_score_eb(CompositeCandidate &cand,
float sumTkPt,
unsigned int nTkMatch,
const std::vector<EmCaloObjEmu> &emcalo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ namespace l1ct {
const std::vector<double> &wp_Pi,
const std::vector<double> &wp_EgEm,
const std::vector<double> &wp_PFEm,
bool slim = false);
bool slim = false,
const std::string &corrector = "",
float correctorEmfMax = -1);
HgcalClusterDecoderEmulator(const edm::ParameterSet &pset);

class MultiClassID {
Expand Down
30 changes: 13 additions & 17 deletions L1Trigger/Phase2L1ParticleFlow/src/egamma/pftkegalgo_ref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ void PFTkEGAlgoEmulator::link_emCalo2tk_composite_eb_ee(const PFRegionEmu &r,
if (cfg.algorithm == PFTkEGAlgoEmuConfig::Algo::compositeEE_v0) {
score = compute_composite_score(cand, emcalo_sel, track, cfg.compIDparams);
} else if (cfg.algorithm == PFTkEGAlgoEmuConfig::Algo::compositeEB_v0) {
score = compute_composite_score_eb(r, cand, sumTkPt, nTkMatch, emcalo_sel, track, cfg.compIDparams);
score = compute_composite_score_eb(cand, sumTkPt, nTkMatch, emcalo_sel, track, cfg.compIDparams);
} else if (cfg.algorithm == PFTkEGAlgoEmuConfig::Algo::compositeEE_v1) {
score = compute_composite_score_ee(cand, sumTkPt, nTkMatch, emcalo_sel, track, cfg.compIDparams);
}
Expand All @@ -330,8 +330,7 @@ void PFTkEGAlgoEmulator::link_emCalo2tk_composite_eb_ee(const PFRegionEmu &r,
}
}

id_score_t PFTkEGAlgoEmulator::compute_composite_score_eb(const PFRegionEmu &r, // FIXME: remove
CompositeCandidate &cand,
id_score_t PFTkEGAlgoEmulator::compute_composite_score_eb(CompositeCandidate &cand,
float sumTkPt,
unsigned int nTkMatch,
const std::vector<EmCaloObjEmu> &emcalo,
Expand All @@ -345,8 +344,8 @@ id_score_t PFTkEGAlgoEmulator::compute_composite_score_eb(const PFRegionEmu &r,
// Prepare the input features
// FIXME: use the EmCaloObj and TkObj to get all the features
// FIXME: make sure that all input features end up in the PFCluster and PFTrack objects with the right precision
// FIXME: 16 bit estimate for the inversion is approximate

// FIXME: 16 bit estimate for the inversion is approximate
ap_ufixed<16, 0> calo_invPt = l1ct::invert_with_shift<pt_t, ap_ufixed<16, 0>, 1024>(calo.hwPt);
// NOTE: this could be computed once per cluster and passed directly to the function
ap_ufixed<16, 0> sumTk_invPt = l1ct::invert_with_shift<pt_t, ap_ufixed<16, 0>, 1024>(pt_t(sumTkPt));
Expand All @@ -360,12 +359,13 @@ id_score_t PFTkEGAlgoEmulator::compute_composite_score_eb(const PFRegionEmu &r,
bdt_eb_feature_t cl_relIso = iso_t(crycl->isolation()) * calo_invPt;
bdt_eb_feature_t cl_staWP = calo.hwEmID & 0x1;
bdt_eb_feature_t cl_looseTkWP = calo.hwEmID & 0x2;
bdt_eb_feature_t tk_chi2RPhi = chi2RPhiBins[tk.hwRedChi2RPhi.to_int()]; // FIXME: should switch to bin #
bdt_eb_feature_t tk_ptFrac = tk.hwPt * sumTk_invPt; // FIXME: could this become sum_tk/calo pt?
bdt_eb_feature_t cltk_ptRatio = calo.hwPt * tk_invPt; // FIXME: che we use the inverse so that we compute the inverse of caloPt once?
bdt_eb_feature_t tk_chi2RPhi = chi2RPhiBins[tk.hwRedChi2RPhi.to_int()]; // FIXME: should switch to bin #
bdt_eb_feature_t tk_ptFrac = tk.hwPt * sumTk_invPt; // FIXME: could this become sum_tk/calo pt?
bdt_eb_feature_t cltk_ptRatio =
calo.hwPt * tk_invPt; // FIXME: could we use the inverse so that we compute the inverse of caloPt once?
bdt_eb_feature_t cltk_nTkMatch = nTkMatch;
bdt_eb_feature_t cltk_absDeta = fabs(tk.floatEta() - calo.floatEta()); // FIXME: switch to hwEta diff
bdt_eb_feature_t cltk_absDphi = fabs(tk.floatPhi() - calo.floatPhi()); // FIXME: switch to hwPhi diff
bdt_eb_feature_t cltk_absDeta = fabs(tk.floatEta() - calo.floatEta()); // FIXME: switch to hwEta diff
bdt_eb_feature_t cltk_absDphi = fabs(tk.floatPhi() - calo.floatPhi()); // FIXME: switch to hwPhi diff

// Run BDT inference
std::vector<bdt_eb_feature_t> inputs = {cl_pt,
Expand All @@ -383,7 +383,7 @@ id_score_t PFTkEGAlgoEmulator::compute_composite_score_eb(const PFRegionEmu &r,
std::vector<bdt_eb_score_t> bdt_score = composite_bdt_eb_->decision_function(inputs);
// std::cout << " out BDT score: " << bdt_score[0] << std::endl;
constexpr unsigned int MAX_SCORE = 1 << (bdt_eb_score_t::iwidth - 1);
return bdt_score[0] / bdt_eb_score_t(MAX_SCORE); // normalize to [-1,1]
return bdt_score[0] / bdt_eb_score_t(MAX_SCORE); // normalize to [-1,1]
}

id_score_t PFTkEGAlgoEmulator::compute_composite_score_ee(CompositeCandidate &cand,
Expand All @@ -404,12 +404,8 @@ id_score_t PFTkEGAlgoEmulator::compute_composite_score_ee(CompositeCandidate &ca
bdt_ee_feature_t cl_spptot = cl3d->sigmaPhiPhiTot();
bdt_ee_feature_t cl_seetot = cl3d->sigmaEtaEtaTot();
bdt_ee_feature_t cl_szz = cl3d->sigmaZZ();
bdt_ee_feature_t cl_multiClassPionIdScore = 0; // FIXME: read from EmCaloObj
bdt_ee_feature_t cl_multiClassEmIdScore = 0; // FIXME: read from EmCaloObj

// bdt_ee_feature_t cl_multiClassPionIdScore = pfcl->piIDScore(); // FIXME: read from EmCaloObj
// bdt_ee_feature_t cl_multiClassEmIdScore = pfcl->emIDScore(); // FIXME: read from EmCaloObj

bdt_ee_feature_t cl_multiClassPionIdScore = calo.floatPiProb();
bdt_ee_feature_t cl_multiClassEmIdScore = calo.floatEmProb();
bdt_ee_feature_t tk_ptFrac = pftk->pt() / sumTkPt;
bdt_ee_feature_t cltk_ptRatio = calo.floatPt() / pftk->pt();
bdt_ee_feature_t cltk_absDeta = fabs(cl3d->eta() - pftk->caloEta());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,18 @@ l1ct::HgcalClusterDecoderEmulator::HgcalClusterDecoderEmulator(const std::string
const std::vector<double> &wp_Pi,
const std::vector<double> &wp_EgEm,
const std::vector<double> &wp_PFEm,
bool slim)
: slim_{slim}, multiclass_id_(model, wp_pt, wp_PU, wp_Pi, wp_EgEm, wp_PFEm) {} // FIXME: need to construct corrector parametrizations
bool slim,
const string &corrector,
float correctorEmfMax)
: slim_{slim},
multiclass_id_(model, wp_pt, wp_PU, wp_Pi, wp_EgEm, wp_PFEm),
corrector_(corrector, correctorEmfMax) {}

l1ct::HgcalClusterDecoderEmulator::~HgcalClusterDecoderEmulator() {}

l1ct::HadCaloObjEmu l1ct::HgcalClusterDecoderEmulator::decode(const l1ct::PFRegionEmu &sector,
const ap_uint<256> &in,
bool &valid) const {

constexpr float ETAPHI_LSB = M_PI / 720;
constexpr float SIGMAZZ_LSB = 778.098 / (1 << 7);
constexpr float SIGMAPHIPHI_LSB = 0.12822 / (1 << 7);
Expand Down Expand Up @@ -86,7 +89,6 @@ l1ct::HadCaloObjEmu l1ct::HgcalClusterDecoderEmulator::decode(const l1ct::PFRegi
// if (sector.floatEtaCenter() > 0)
// w_phi = -w_phi;


l1ct::HadCaloObjEmu out;
out.clear();
if (w_pt == 0)
Expand All @@ -99,30 +101,27 @@ l1ct::HadCaloObjEmu l1ct::HgcalClusterDecoderEmulator::decode(const l1ct::PFRegi
out.hwEmPt = w_empt * l1ct::pt_t(l1ct::Scales::INTPT_LSB);

if (!slim_) {
// FIXME: the scaling here is added to the encoded word.
// FIXME: the scaling here is added to the encoded word.
out.hwSrrTot = w_sigmarrtot * l1ct::srrtot_t(l1ct::Scales::SRRTOT_LSB);
// We just downscale precision and round to the nearest integer
out.hwMeanZ = l1ct::meanz_t(std::min(w_meanz.to_int() + 4, (1 << 12) - 1) >> 3);
// Compute an H/E value: 1/emf - 1 as needed by Composite ID // FIXME: could drop once we move the model to the fraction
ap_ufixed<10, 5, AP_RND_CONV, AP_SAT> w_hoe = 256.0 / (w_emf.to_int() + 0.5) - 1;
out.hwHoe = w_hoe;
}

std::vector<MultiClassID::bdt_feature_t> inputs = {
w_showerlenght,
w_coreshowerlenght,
w_emf / 256,
w_abseta * ETAPHI_LSB,
w_meanz * 0.5, // We use the full resolution here
w_sigmaetaeta * SIGMAETAETA_LSB,
w_sigmaphiphi * SIGMAPHIPHI_LSB,
w_sigmazz * SIGMAZZ_LSB
};
// FIXME: use hardware values everywhere
std::vector<MultiClassID::bdt_feature_t> inputs = {w_showerlenght,
w_coreshowerlenght,
w_emf / 256,
w_abseta * ETAPHI_LSB,
w_meanz * 0.5, // We use the full resolution here
w_sigmaetaeta * SIGMAETAETA_LSB,
w_sigmaphiphi * SIGMAPHIPHI_LSB,
w_sigmazz * SIGMAZZ_LSB};

// evaluate multiclass model
valid = multiclass_id_.evaluate(out, inputs);


// Calibrate pt and set error
if (corrector_.valid()) {
float newpt = corrector_.correctedPt(out.floatPt(), out.floatEmPt(), sector.floatGlbEta(out.hwEta));
Expand All @@ -147,7 +146,6 @@ l1ct::HgcalClusterDecoderEmulator::MultiClassID::MultiClassID(const std::string
multiclass_bdt_ = new conifer::BDT<bdt_feature_t, bdt_score_t, false>(resolvedFileName);
}


bool l1ct::HgcalClusterDecoderEmulator::MultiClassID::evaluate(l1ct::HadCaloObjEmu &cl,
const std::vector<bdt_feature_t> &inputs) const {
auto bdt_score = multiclass_bdt_->decision_function(inputs); //0 is pu, 1 is pi, 2 is eg
Expand All @@ -164,7 +162,7 @@ bool l1ct::HgcalClusterDecoderEmulator::MultiClassID::evaluate(l1ct::HadCaloObjE
}
}
bool passPu = (sm_scores[0] >= wp_PU_[pt_bin]);
bool passPi = (sm_scores[1] >= wp_Pi_[pt_bin]); // FIXME: where do we store this?
// bool passPi = (sm_scores[1] >= wp_Pi_[pt_bin]); // FIXME: where do we store this?
bool passEgEm = (sm_scores[2] >= wp_EgEm_[pt_bin]);
bool passPFEm = (sm_scores[2] >= wp_PFEm_[pt_bin]);

Expand All @@ -173,7 +171,8 @@ bool l1ct::HgcalClusterDecoderEmulator::MultiClassID::evaluate(l1ct::HadCaloObjE
// bit 2: EG Loose ID
cl.hwEmID = passPFEm | (passEgEm << 1) | (passEgEm << 2); // FIXME: for now loose eg WP == tight WP?

// FIXME: add the scores to the HadCaloObjEmu
cl.hwPiProb = sm_scores[1];
cl.hwEgProb = sm_scores[2];
return !passPu;
}

Expand Down

0 comments on commit f950009

Please sign in to comment.