Skip to content

Commit

Permalink
Restructure model handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cerminar committed Dec 11, 2024
1 parent 160e065 commit 6208d26
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 116 deletions.
47 changes: 29 additions & 18 deletions L1Trigger/Phase2L1ParticleFlow/interface/egamma/pftkegalgo_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ namespace l1ct {
std::vector<double> dEtaValues;
std::vector<double> dPhiValues;
float trkQualityPtMin; // GeV
unsigned int algorithm;

enum Algo { undefined = -1, elliptic = 0, compositeEE_v0 = 1, compositeEB_v0 = 2, compositeEE_v1 = 3 };

Algo algorithm;
unsigned int nCompCandPerCluster;
bool writeEgSta;

Expand Down Expand Up @@ -123,7 +126,7 @@ namespace l1ct {
dEtaValues(dEtaValues),
dPhiValues(dPhiValues),
trkQualityPtMin(trkQualityPtMin),
algorithm(algo),
algorithm(Algo::undefined),
nCompCandPerCluster(nCompCandPerCluster),
writeEgSta(writeEgSta),
tkIsoParams_tkEle(tkIsoParams_tkEle),
Expand All @@ -135,7 +138,18 @@ namespace l1ct {
hwIsoTypeTkEle(hwIsoTypeTkEle),
hwIsoTypeTkEm(hwIsoTypeTkEm),
compIDparams(compIDparams),
debug(debug) {}
debug(debug) {
if (algo == 1)
algorithm = Algo::elliptic;
else if (algo == 2)
algorithm = Algo::compositeEE_v0;
else if (algo == 3)
algorithm = Algo::compositeEB_v0;
else if (algo == 4)
algorithm = Algo::compositeEE_v1;
else
throw std::invalid_argument("[PFTkEGAlgoEmuConfig]: Unknown algorithm type");
}

static edm::ParameterSetDescription getParameterSetDescription();
};
Expand Down Expand Up @@ -185,10 +199,10 @@ namespace l1ct {
std::vector<id_score_t> &emCaloTkBdtScore) const;

void link_emCalo2tk_composite_eb_ee(const PFRegionEmu &r,
const std::vector<EmCaloObjEmu> &emcalo,
const std::vector<TkObjEmu> &track,
std::vector<int> &emCalo2tk,
std::vector<id_score_t> &emCaloTkBdtScore) const;
const std::vector<EmCaloObjEmu> &emcalo,
const std::vector<TkObjEmu> &track,
std::vector<int> &emCalo2tk,
std::vector<id_score_t> &emCaloTkBdtScore) const;

// void link_emCalo2tk_composite_ee(const PFRegionEmu &r,
// const std::vector<EmCaloObjEmu> &emcalo,
Expand All @@ -211,19 +225,17 @@ namespace l1ct {
CompositeCandidate &cand,
float sumTkPt,
unsigned int nTkMatch,
const std::vector<EmCaloObjEmu> &emcalo,
const std::vector<TkObjEmu> &track,
const PFTkEGAlgoEmuConfig::CompIDParameters &params) const;
const std::vector<EmCaloObjEmu> &emcalo,
const std::vector<TkObjEmu> &track,
const PFTkEGAlgoEmuConfig::CompIDParameters &params) const;

id_score_t compute_composite_score_ee(CompositeCandidate &cand,
float sumTkPt,
unsigned int nTkMatch,
const std::vector<EmCaloObjEmu> &emcalo,
const std::vector<TkObjEmu> &track,
const PFTkEGAlgoEmuConfig::CompIDParameters &params) const;

const std::vector<EmCaloObjEmu> &emcalo,
const std::vector<TkObjEmu> &track,
const PFTkEGAlgoEmuConfig::CompIDParameters &params) const;

//FIXME: still needed
float deltaPhi(float phi1, float phi2) const;

void sel_emCalo(unsigned int nmax_sel,
Expand Down Expand Up @@ -395,9 +407,8 @@ namespace l1ct {
z0_t z0) const;

PFTkEGAlgoEmuConfig cfg;
conifer::BDT<bdt_feature_t, bdt_score_t> *composite_bdt_;
conifer::BDT<bdt_eb_feature_t, bdt_eb_score_t> *composite_bdt_eb_;
conifer::BDT<bdt_ee_feature_t, bdt_ee_score_t> *composite_bdt_ee_;
// Could use a std::variant
void *model_;

int debug_;
};
Expand Down
141 changes: 43 additions & 98 deletions L1Trigger/Phase2L1ParticleFlow/src/egamma/pftkegalgo_ref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,35 @@ using namespace l1ct;
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"

l1ct::PFTkEGAlgoEmuConfig::PFTkEGAlgoEmuConfig(const edm::ParameterSet &pset)
: nTRACK(pset.getParameter<uint32_t>("nTRACK")),
nTRACK_EGIN(pset.getParameter<uint32_t>("nTRACK_EGIN")),
nEMCALO_EGIN(pset.getParameter<uint32_t>("nEMCALO_EGIN")),
nEM_EGOUT(pset.getParameter<uint32_t>("nEM_EGOUT")),
filterHwQuality(pset.getParameter<bool>("filterHwQuality")),
doBremRecovery(pset.getParameter<bool>("doBremRecovery")),
writeBeforeBremRecovery(pset.getParameter<bool>("writeBeforeBremRecovery")),
caloHwQual(pset.getParameter<int>("caloHwQual")),
doEndcapHwQual(pset.getParameter<bool>("doEndcapHwQual")),
emClusterPtMin(pset.getParameter<double>("caloEtMin")),
dEtaMaxBrem(pset.getParameter<double>("dEtaMaxBrem")),
dPhiMaxBrem(pset.getParameter<double>("dPhiMaxBrem")),
absEtaBoundaries(pset.getParameter<std::vector<double>>("absEtaBoundaries")),
dEtaValues(pset.getParameter<std::vector<double>>("dEtaValues")),
dPhiValues(pset.getParameter<std::vector<double>>("dPhiValues")),
trkQualityPtMin(pset.getParameter<double>("trkQualityPtMin")),
algorithm(pset.getParameter<uint32_t>("algorithm")),
nCompCandPerCluster(pset.getParameter<uint32_t>("nCompCandPerCluster")),
writeEgSta(pset.getParameter<bool>("writeEGSta")),
tkIsoParams_tkEle(pset.getParameter<edm::ParameterSet>("tkIsoParametersTkEle")),
tkIsoParams_tkEm(pset.getParameter<edm::ParameterSet>("tkIsoParametersTkEm")),
pfIsoParams_tkEle(pset.getParameter<edm::ParameterSet>("pfIsoParametersTkEle")),
pfIsoParams_tkEm(pset.getParameter<edm::ParameterSet>("pfIsoParametersTkEm")),
doTkIso(pset.getParameter<bool>("doTkIso")),
doPfIso(pset.getParameter<bool>("doPfIso")),
hwIsoTypeTkEle(static_cast<EGIsoEleObjEmu::IsoType>(pset.getParameter<uint32_t>("hwIsoTypeTkEle"))),
hwIsoTypeTkEm(static_cast<EGIsoObjEmu::IsoType>(pset.getParameter<uint32_t>("hwIsoTypeTkEm"))),
compIDparams(pset.getParameter<edm::ParameterSet>("compositeParametersTkEle")),
debug(pset.getUntrackedParameter<uint32_t>("debug", 0)) {}
: PFTkEGAlgoEmuConfig(pset.getParameter<uint32_t>("nTRACK"),
pset.getParameter<uint32_t>("nTRACK_EGIN"),
pset.getParameter<uint32_t>("nEMCALO_EGIN"),
pset.getParameter<uint32_t>("nEM_EGOUT"),
pset.getParameter<bool>("filterHwQuality"),
pset.getParameter<bool>("doBremRecovery"),
pset.getParameter<bool>("writeBeforeBremRecovery"),
pset.getParameter<int>("caloHwQual"),
pset.getParameter<bool>("doEndcapHwQual"),
pset.getParameter<double>("caloEtMin"),
pset.getParameter<double>("dEtaMaxBrem"),
pset.getParameter<double>("dPhiMaxBrem"),
pset.getParameter<std::vector<double>>("absEtaBoundaries"),
pset.getParameter<std::vector<double>>("dEtaValues"),
pset.getParameter<std::vector<double>>("dPhiValues"),
pset.getParameter<double>("trkQualityPtMin"),
pset.getParameter<uint32_t>("algorithm"),
pset.getParameter<uint32_t>("nCompCandPerCluster"),
pset.getParameter<bool>("writeEGSta"),
IsoParameters(pset.getParameter<edm::ParameterSet>("tkIsoParametersTkEle")),
IsoParameters(pset.getParameter<edm::ParameterSet>("tkIsoParametersTkEm")),
IsoParameters(pset.getParameter<edm::ParameterSet>("pfIsoParametersTkEle")),
IsoParameters(pset.getParameter<edm::ParameterSet>("pfIsoParametersTkEm")),
pset.getParameter<bool>("doTkIso"),
pset.getParameter<bool>("doPfIso"),
static_cast<EGIsoEleObjEmu::IsoType>(pset.getParameter<uint32_t>("hwIsoTypeTkEle")),
static_cast<EGIsoObjEmu::IsoType>(pset.getParameter<uint32_t>("hwIsoTypeTkEm")),
pset.getParameter<edm::ParameterSet>("compositeParametersTkEle"),
pset.getUntrackedParameter<uint32_t>("debug", 0)) {}

edm::ParameterSetDescription l1ct::PFTkEGAlgoEmuConfig::getParameterSetDescription() {
edm::ParameterSetDescription description;
Expand Down Expand Up @@ -130,21 +130,19 @@ edm::ParameterSetDescription l1ct::PFTkEGAlgoEmuConfig::CompIDParameters::getPar
#endif

PFTkEGAlgoEmulator::PFTkEGAlgoEmulator(const PFTkEGAlgoEmuConfig &config)
: cfg(config), composite_bdt_(nullptr), composite_bdt_eb_(nullptr), composite_bdt_ee_(nullptr), debug_(cfg.debug) {
: cfg(config), model_(nullptr), debug_(cfg.debug) {
if (cfg.algorithm == 1 || cfg.algorithm == 2 || cfg.algorithm == 3) {
#ifdef CMSSW_GIT_HASH
auto resolvedFileName = edm::FileInPath(cfg.compIDparams.conifer_model).fullPath();
#else
auto resolvedFileName = cfg.compIDparams.conifer_model;
#endif
if (cfg.algorithm == 1) {
composite_bdt_ = new conifer::BDT<bdt_feature_t, bdt_score_t, false>(resolvedFileName);
} else if (cfg.algorithm == 2) {
// std::cout << resolvedFileName << std::endl;
composite_bdt_eb_ = new conifer::BDT<bdt_eb_feature_t, bdt_eb_score_t, false>(resolvedFileName);
} else if (cfg.algorithm == 3) {
// std::cout << "algo 3: " << resolvedFileName << std::endl;
composite_bdt_ee_ = new conifer::BDT<bdt_ee_feature_t, bdt_ee_score_t, false>(resolvedFileName);
if (cfg.algorithm == PFTkEGAlgoEmuConfig::Algo::compositeEE_v0) {
model_ = new conifer::BDT<bdt_feature_t, bdt_score_t, false>(resolvedFileName);
} else if (cfg.algorithm == PFTkEGAlgoEmuConfig::Algo::compositeEB_v0) {
model_ = new conifer::BDT<bdt_eb_feature_t, bdt_eb_score_t, false>(resolvedFileName);
} else if (cfg.algorithm == PFTkEGAlgoEmuConfig::Algo::compositeEE_v1) {
model_ = new conifer::BDT<bdt_ee_feature_t, bdt_ee_score_t, false>(resolvedFileName);
}
}
}
Expand Down Expand Up @@ -388,18 +386,16 @@ id_score_t PFTkEGAlgoEmulator::compute_composite_score_eb(const PFRegionEmu &r,
// Get the cluster/track objects that form the composite candidate
const auto &calo = emcalo[cand.cluster_idx];
const auto &tk = track[cand.track_idx];
const l1t::PFCluster *pfcl = dynamic_cast<const l1t::PFCluster *>(calo.src);
const l1t::PFTrack *pftk = tk.src;
l1tp2::DigitizedClusterCorrelator digiCl(ap_uint<64>(pfcl->digiWord()));
const l1tp2::CaloCrystalCluster *crycl =
dynamic_cast<const l1tp2::CaloCrystalCluster *>(pfcl->constituentsAndFractions()[0].first.get());
const l1tp2::CaloCrystalCluster *crycl = dynamic_cast<const l1tp2::CaloCrystalCluster *>(calo.src);

// Prepare the input features
// FIXME: use the EmCaloObj to get all the features
bdt_eb_feature_t cl_pt = crycl->pt();
bdt_eb_feature_t cl_ss = crycl->e2x5() / crycl->e5x5();
bdt_eb_feature_t cl_relIso = crycl->isolation() / crycl->pt();
bdt_eb_feature_t cl_staWP = digiCl.passes_iso() && digiCl.passes_ss();
bdt_eb_feature_t cl_looseTkWP = digiCl.passes_looseTkiso() && digiCl.passes_looseTkss();
bdt_eb_feature_t cl_staWP = calo.hwEmID & 0x1;
bdt_eb_feature_t cl_looseTkWP = calo.hwEmID & 0x2;
bdt_eb_feature_t tk_chi2RPhi = pftk->trackWord().getChi2RPhi();
bdt_eb_feature_t tk_ptFrac = tk.floatPt() / sumTkPt;
bdt_eb_feature_t cltk_ptRatio = crycl->pt() / tk.floatPt();
Expand All @@ -409,44 +405,6 @@ id_score_t PFTkEGAlgoEmulator::compute_composite_score_eb(const PFRegionEmu &r,
bdt_eb_feature_t cltk_absDeta = fabs(r.floatGlbEta(tk.hwEta) - crycl->eta());
bdt_eb_feature_t cltk_absDphi = fabs(r.floatGlbPhi(tk.hwPhi) - crycl->phi());

// if(crycl->pt() > 70) {
// std::cout << "--- New pair" << std::endl;
// std::cout << " Cluster: pt: " << crycl->pt() << " eta: " << crycl->eta() << " phi: " << crycl->phi() << " isolation: " << crycl->isolation()
// << " digi: " << std::hex << uint64_t(pfcl->digiWord()) << std::dec << std:: endl;
// std::cout << " Track: pt: " << pftk->pt() << " eta: " << pftk->caloEta() << " phi: " << pftk->caloPhi() << std:: endl;
// std::cout << " dec pt: " << tk.floatPt() << " eta: " << r.floatGlbEta(tk.hwEta) << " deta: " << fabs(r.floatGlbEta(tk.hwEta) - crycl->eta()) << " dec phi: " << r.floatGlbPhi(tk.hwPhi) <<
// " dphi: " << fabs(crycl->phi() - r.floatGlbPhi(tk.hwPhi)) << std::endl;

// std::cout << " . features: "
// << " cl_pt: " << cl_pt
// << " cl_ss: " << cl_ss
// << " cl_relIso: " << cl_relIso
// << " (float): " << crycl->isolation()/crycl->pt()
// << " cl_staWP: " << cl_staWP
// << " cl_looseTkWP: " << cl_looseTkWP
// << " tk_chi2RPhi: " << tk_chi2RPhi
// << " tk_ptFrac: " << tk_ptFrac
// << " cltk_ptRatio: " << cltk_ptRatio
// << " cltk_nTkMatch: " << cltk_nTkMatch
// << " cltk_absDeta: " << cltk_absDeta
// << " (float): " << fabs(r.floatGlbEta(tk.hwEta) - crycl->eta())
// << " cltk_absDphi: " << cltk_absDphi
// << " float: " << fabs(r.floatGlbPhi(tk.hwPhi)- crycl->phi())
// << std::endl;
// }
// 400fb-1 7.5*10^34 -> 60gg
// "CryClu_pt",
// "CryClu_ss",
// "CryClu_relIso",
// "CryClu_standaloneWP",
// "CryClu_looseL1TkMatchWP",
// "Tk_chi2RPhi",
// "Tk_PtFrac",
// "PtRatio",
// "nMatch",
// "abs_dEta",
// "abs_dPhi",

// Run BDT inference
std::vector<bdt_eb_feature_t> inputs = {cl_pt,
cl_ss,
Expand All @@ -459,6 +417,7 @@ id_score_t PFTkEGAlgoEmulator::compute_composite_score_eb(const PFRegionEmu &r,
cltk_nTkMatch,
cltk_absDeta,
cltk_absDphi};
auto *composite_bdt_eb_ = static_cast<conifer::BDT<bdt_eb_feature_t, bdt_eb_score_t, false> *>(model_);
std::vector<bdt_eb_score_t> bdt_score = composite_bdt_eb_->decision_function(inputs);
// std::cout << " out BDT score: " << bdt_score[0] << std::endl;
return bdt_score[0] / 4;
Expand All @@ -470,7 +429,6 @@ id_score_t PFTkEGAlgoEmulator::compute_composite_score_ee(CompositeCandidate &ca
const std::vector<EmCaloObjEmu> &emcalo,
const std::vector<TkObjEmu> &track,
const PFTkEGAlgoEmuConfig::CompIDParameters &params) const {
// std::cout << "Endcap Piero Model" << std::endl;
// Get the cluster/track objects that form the composite candidate
const auto &calo = emcalo[cand.cluster_idx];
const auto &tk = track[cand.track_idx];
Expand All @@ -494,20 +452,6 @@ id_score_t PFTkEGAlgoEmulator::compute_composite_score_ee(CompositeCandidate &ca
bdt_ee_feature_t cltk_absDeta = fabs(cl3d->eta() - pftk->caloEta());
bdt_ee_feature_t cltk_absDphi = fabs(cl3d->phi() - pftk->caloPhi());

// features=[
// "HGCalClu_coreshowerlength",
// "HGCalClu_meanz",
// "HGCalClu_spptot",
// "HGCalClu_seetot",
// "HGCalClu_szz",
// "HGCalClu_multiClassPionIdScore",
// "HGCalClu_multiClassEmIdScore",
// "Tk_PtFrac",
// "PtRatio",
// "abs_dEta",
// "abs_dPhi",
// ]

// Run BDT inference
std::vector<bdt_ee_feature_t> inputs = {cl_coreshowerlength,
cl_meanz,
Expand All @@ -520,7 +464,7 @@ id_score_t PFTkEGAlgoEmulator::compute_composite_score_ee(CompositeCandidate &ca
cltk_ptRatio,
cltk_absDeta,
cltk_absDphi};

auto *composite_bdt_ee_ = static_cast<conifer::BDT<bdt_ee_feature_t, bdt_ee_score_t, false> *>(model_);
std::vector<bdt_ee_score_t> bdt_score = composite_bdt_ee_->decision_function(inputs);
// std::cout << " out BDT score: " << bdt_score[0] << std::endl;
return bdt_score[0] / 4;
Expand Down Expand Up @@ -550,6 +494,7 @@ id_score_t PFTkEGAlgoEmulator::compute_composite_score(CompositeCandidate &cand,

// Run BDT inference
std::vector<bdt_feature_t> inputs = {tkpt, hoe, srrtot, deta, dphi, dpt, meanz, nstubs, chi2rphi, chi2rz, chi2bend};
auto *composite_bdt_ = static_cast<conifer::BDT<bdt_feature_t, bdt_score_t, false> *>(model_);
std::vector<bdt_score_t> bdt_score = composite_bdt_->decision_function(inputs);

return bdt_score[0] / 4;
Expand Down

0 comments on commit 6208d26

Please sign in to comment.