Skip to content

Commit

Permalink
Merge pull request cms-sw#10 from Pmeiring/compositeID_emulator_reorg
Browse files Browse the repository at this point in the history
Changes for running testbench
  • Loading branch information
cerminar authored Oct 13, 2022
2 parents 9ebd3ef + 44966d9 commit d22df66
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
14 changes: 7 additions & 7 deletions L1Trigger/Phase2L1ParticleFlow/interface/conifer.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef CONIFER_CPP_H__
#define CONIFER_CPP_H__
#include "FWCore/Utilities/interface/Exception.h"
//#include "FWCore/Utilities/interface/Exception.h"
#include "nlohmann/json.hpp"
#include <fstream>

Expand Down Expand Up @@ -115,16 +115,16 @@ namespace conifer {

std::vector<U> decision_function(std::vector<T> x) const {
/* Do the prediction */
if (x.size() != n_features) {
throw cms::Exception("RuntimeError")
<< "Conifer : Size of feature vector mismatches expected n_features" << std::endl;
}
//if (x.size() != n_features) {
// throw cms::Exception("RuntimeError")
// << "Conifer : Size of feature vector mismatches expected n_features" << std::endl;
//}
std::vector<U> values;
std::vector<std::vector<U>> values_trees;
values_trees.resize(n_classes);
values.resize(n_classes, U(0));
for (unsigned int i = 0; i < n_classes; i++) {
std::transform(trees.begin(), trees.end(), std::back_inserter(values_trees.at(i)), [&i, &x](auto tree_v) {
std::transform(trees.begin(), trees.end(), std::back_inserter(values_trees.at(i)), [&i, &x](std::vector<DecisionTree<T,U>> tree_v) {
return tree_v.at(i).decision_function(x);
});
if (useAddTree) {
Expand Down Expand Up @@ -152,4 +152,4 @@ namespace conifer {

} // namespace conifer

#endif
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ namespace l1ct {
std::vector<double> dEtaValues;
std::vector<double> dPhiValues;
float trkQualityPtMin; // GeV
bool doCompositeTkEle;
bool writeEgSta;

struct IsoParameters {
Expand All @@ -55,7 +56,7 @@ namespace l1ct {
EGIsoEleObjEmu::IsoType hwIsoTypeTkEle;
EGIsoObjEmu::IsoType hwIsoTypeTkEm;

bool doCompositeTkEle;
//bool doCompositeTkEle;
struct CompIDParameters {
CompIDParameters(const edm::ParameterSet &);
CompIDParameters(double hoeMin, double hoeMax, double tkptMin, double tkptMax, double srrtotMin, double srrtotMax, double detaMin, double detaMax, double dptMin, double dptMax, double meanzMin, double meanzMax, double dphiMin, double dphiMax, double tkchi2Min, double tkchi2Max, double tkz0Min, double tkz0Max, double tknstubsMin, double tknstubsMax, double BDTcut_wp97p5, double BDTcut_wp95p0)
Expand Down Expand Up @@ -115,6 +116,7 @@ namespace l1ct {
const std::vector<double> &dEtaValues = {0.015, 0.01},
const std::vector<double> &dPhiValues = {0.07, 0.07},
float trkQualityPtMin = 10.,
bool doCompositeTkEle = false,
bool writeEgSta = false,
const IsoParameters &tkIsoParams_tkEle = {2., 0.6, 0.03, 0.2},
const IsoParameters &tkIsoParams_tkEm = {2., 0.6, 0.07, 0.3},
Expand All @@ -124,7 +126,6 @@ namespace l1ct {
bool doPfIso = false,
EGIsoEleObjEmu::IsoType hwIsoTypeTkEle = EGIsoEleObjEmu::IsoType::TkIso,
EGIsoObjEmu::IsoType hwIsoTypeTkEm = EGIsoObjEmu::IsoType::TkIsoPV,
bool doCompositeTkEle = false,
const CompIDParameters &myCompIDparams = {-1.0, 1566.547607421875, 1.9501149654388428, 11102.0048828125, 0.0, 0.01274710614234209, -0.24224889278411865, 0.23079538345336914, 0.010325592942535877, 184.92538452148438, 325.0653991699219, 499.6089782714844, -6.281332015991211, 6.280326843261719, 0.024048099294304848, 1258.37158203125, -14.94140625, 14.94140625, 4.0, 6.0, 0.5406244, 0.9693441},
int debug = 0)

Expand All @@ -147,6 +148,7 @@ namespace l1ct {
dEtaValues(dEtaValues),
dPhiValues(dPhiValues),
trkQualityPtMin(trkQualityPtMin),
doCompositeTkEle(doCompositeTkEle),
writeEgSta(writeEgSta),
tkIsoParams_tkEle(tkIsoParams_tkEle),
tkIsoParams_tkEm(tkIsoParams_tkEm),
Expand All @@ -156,7 +158,6 @@ namespace l1ct {
doPfIso(doPfIso),
hwIsoTypeTkEle(hwIsoTypeTkEle),
hwIsoTypeTkEm(hwIsoTypeTkEm),
doCompositeTkEle(doCompositeTkEle),
myCompIDparams(myCompIDparams),
debug(debug) {}
};
Expand Down Expand Up @@ -383,7 +384,7 @@ namespace l1ct {
z0_t z0) const;

PFTkEGAlgoEmuConfig cfg;
std::unique_ptr<conifer::BDT<ap_fixed<22,3,AP_RND_CONV,AP_SAT>,ap_fixed<22,3,AP_RND_CONV,AP_SAT>,0>> composite_bdt_;
conifer::BDT<ap_fixed<22,3,AP_RND_CONV,AP_SAT>,ap_fixed<22,3,AP_RND_CONV,AP_SAT>,0> * composite_bdt_;
int debug_;
};
} // namespace l1ct
Expand Down
26 changes: 19 additions & 7 deletions L1Trigger/Phase2L1ParticleFlow/src/egamma/pftkegalgo_ref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
#include <memory>
#include <iostream>
#include <bitset>
#include <vector>

#include "DataFormats/L1TParticleFlow/interface/PFTrack.h"
#include "DataFormats/L1TParticleFlow/interface/PFCluster.h"
// #include "DataFormats/L1TParticleFlow/interface/PFTrack.h"
// #include "DataFormats/L1TParticleFlow/interface/PFCluster.h"


using namespace l1ct;
Expand Down Expand Up @@ -84,8 +85,14 @@ composite_bdt_(nullptr),
debug_(cfg.debug) {
if(cfg.doCompositeTkEle) {
//FIXME: make the name of the file configurable
auto resolvedFileName = edm::FileInPath("L1Trigger/Phase2L1ParticleFlow/data/compositeID.json").fullPath();
composite_bdt_ = std::make_unique<conifer::BDT<ap_fixed<22,3,AP_RND_CONV,AP_SAT>,ap_fixed<22,3,AP_RND_CONV,AP_SAT>,0>> (resolvedFileName);
#ifdef CMSSW_GIT_HASH
auto resolvedFileName = edm::FileInPath("L1Trigger/Phase2L1ParticleFlow/data/compositeID.json").fullPath();
#else
auto resolvedFileName = "compositeID.json";
#endif
std::cout<<resolvedFileName<<std::endl;
composite_bdt_ = new conifer::BDT<ap_fixed<22,3,AP_RND_CONV,AP_SAT>,ap_fixed<22,3,AP_RND_CONV,AP_SAT>,0> (resolvedFileName);
std::cout<<"declared bdt"<<std::endl;
}
}

Expand Down Expand Up @@ -196,12 +203,15 @@ void PFTkEGAlgoEmulator::link_emCalo2tk_composite(const PFRegionEmu &r,
//FIXME: should be configurable
const int nCAND_PER_CLUSTER = 4;
unsigned int nTrackMax = std::min<unsigned>(track.size(), cfg.nTRACK_EGIN);
std::cout<<"doing loose dR matching"<<std::endl;
for (int ic = 0, nc = emcalo.size(); ic < nc; ++ic) {
std::cout<<"cluster "<<ic<<std::endl;
auto &calo = emcalo[ic];

std::vector<CompositeCandidate> candidates;

for (unsigned int itk = 0; itk < nTrackMax; ++itk) {
std::cout<<"track "<<itk<<std::endl;
const auto &tk = track[itk];
if (tk.floatPt() <= cfg.trkQualityPtMin)
continue;
Expand All @@ -219,12 +229,13 @@ void PFTkEGAlgoEmulator::link_emCalo2tk_composite(const PFRegionEmu &r,
candidates.push_back(cand);
}
}
std::cout << "Constructed candidates, now sorting" << std::endl;
// FIXME: find best sort criteria, for now we use dpt
std::sort(candidates.begin(), candidates.end(),
[](const CompositeCandidate & a, const CompositeCandidate & b) -> bool
{ return a.dpt < b.dpt; });
unsigned int nCandPerCluster = std::min<unsigned int>(candidates.size(), nCAND_PER_CLUSTER);
std::cout << "# composit candidates: " << nCandPerCluster << std::endl;
std::cout << "# composite candidates: " << nCandPerCluster << std::endl;
if(nCandPerCluster == 0) continue;

float bdtWP_MVA = cfg.myCompIDparams.BDTcut_wp97p5;
Expand Down Expand Up @@ -273,7 +284,7 @@ float PFTkEGAlgoEmulator::compute_composite_score(CompositeCandidate &cand,
ap_fixed<22,3,AP_RND_CONV,AP_SAT> nstubs = (tk.hwStubs-params.tknstubsMin)/(params.tknstubsMax-params.tknstubsMin);

// Run BDT inference
vector<ap_fixed<22,3,AP_RND_CONV,AP_SAT>> inputs = { hoe, tkpt, srrtot, deta, dpt, meanz, dphi, chi2, tkz0, nstubs } ;
std::vector<ap_fixed<22,3,AP_RND_CONV,AP_SAT>> inputs = { hoe, tkpt, srrtot, deta, dpt, meanz, dphi, chi2, tkz0, nstubs } ;
auto bdt_score = composite_bdt_->decision_function(inputs);

float bdt_score_CON = bdt_score[0];
Expand Down Expand Up @@ -309,7 +320,7 @@ void PFTkEGAlgoEmulator::run(const PFInputRegion &in, OutputRegion &out) const {
<< std::endl;
}
}

std::cout<<"running"<<std::endl;
// FIXME: can be removed in the endcap since now running with the "interceptor".
// Might still be needed in barrel
// filter and select first N elements of input clusters
Expand All @@ -322,6 +333,7 @@ void PFTkEGAlgoEmulator::run(const PFInputRegion &in, OutputRegion &out) const {

std::vector<int> emCalo2tk(emcalo_sel.size(), -1);
std::vector<float> emCaloTkBdtScore(emcalo_sel.size(), -999);
std::cout<<"about to start matching"<<std::endl;

if(cfg.doCompositeTkEle) {
link_emCalo2tk_composite(in.region, emcalo_sel, in.track, emCalo2tk, emCaloTkBdtScore);
Expand Down

0 comments on commit d22df66

Please sign in to comment.