Skip to content

Commit

Permalink
Reduce L1 tracking CPU (cms-sw#172)
Browse files Browse the repository at this point in the history
* Reduce CPU use

* Reduce CPU

* Reduce CPU

* Added track quality plots

* updated geom

* Trivial variable renames

* Moved RunTime call to constructor to save CPU

* Removed debug calcs from VarBase::calculate

* Optimised a bit more VarBase::calculate

* optimise CPU further

* auto format
  • Loading branch information
tomalin authored Jun 21, 2022
1 parent 101776a commit fe0b287
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 101 deletions.
6 changes: 3 additions & 3 deletions L1Trigger/TrackFindingTracklet/interface/imath.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ namespace trklet {
const std::map<const VarBase *, std::set<std::string> > *const previous_cut_strings = nullptr) const;
void add_cut(VarCut *cut, const bool call_set_cut_var = true);
VarBase *cut_var();

// observed range of fval_ (only filled if debug_level > 0)
double minval() const { return minval_; }
double maxval() const { return maxval_; }
void analyze();
Expand All @@ -250,9 +250,9 @@ namespace trklet {
int step() const { return step_; }
int latency() const { return latency_; }
void add_latency(unsigned int l) { latency_ += l; } //only call before using the variable in calculation!
bool calculate(int debug_level);
bool calculate() { return calculate(0); }
bool calculate(int debug_level = 0);
virtual void local_calculate() {}
void calcDebug(int debug_level, long int ival_prev, bool &all_ok);
virtual void print(std::ofstream &fs, Verilog, int l1 = 0, int l2 = 0, int l3 = 0) {
fs << "// VarBase here. Soemthing is wrong!! " << l1 << ", " << l2 << ", " << l3 << "\n";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ void L1FPGATrackProducer::beginRun(const edm::Run& run, const edm::EventSetup& i
setup_ = &iSetup.getData(esGetToken_);
setupHPH_ = &iSetup.getData(esGetTokenHPH_);
if (trackQuality_) {
trackQualityModel_->setHPHSetup(setupHPH_);
trackQualityModel_->beginRun(setupHPH_);
}
// Tracklet pattern reco output channel info.
channelAssignment_ = &iSetup.getData(esGetTokenChannelAssignment_);
Expand Down
77 changes: 40 additions & 37 deletions L1Trigger/TrackFindingTracklet/src/TrackletEventProcessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -281,44 +281,47 @@ void TrackletEventProcessor::event(SLHCEvent& ev,
sector_->executeTC();
TCTimer_.stop();

int nTP = globals_->event()->nsimtracks();
for (int iTP = 0; iTP < nTP; iTP++) {
L1SimTrack simtrk = globals_->event()->simtrack(iTP);
if (simtrk.pt() < 2.0)
continue;
if (std::abs(simtrk.vz()) > 15.0)
continue;
if (hypot(simtrk.vx(), simtrk.vy()) > 0.1)
continue;
bool electron = (abs(simtrk.type()) == 11);
bool muon = (abs(simtrk.type()) == 13);
bool pion = (abs(simtrk.type()) == 211);
bool kaon = (abs(simtrk.type()) == 321);
bool proton = (abs(simtrk.type()) == 2212);
if (!(electron || muon || pion || kaon || proton))
continue;
int nlayers = 0;
int ndisks = 0;
int simtrackid = simtrk.trackid();
unsigned int hitmask = ev.layersHit(simtrackid, nlayers, ndisks);
if (nlayers + ndisks < 4)
continue;

if (settings_->writeMonitorData("HitEff")) {
static ofstream outhit("hiteff.txt");
outhit << simtrk.eta() << " " << (hitmask & 1) << " " << (hitmask & 2) << " " << (hitmask & 4) << " "
<< (hitmask & 8) << " " << (hitmask & 16) << " " << (hitmask & 32) << " " << (hitmask & 64) << " "
<< (hitmask & 128) << " " << (hitmask & 256) << " " << (hitmask & 512) << " " << (hitmask & 1024)
<< endl;
}
if (settings_->writeMonitorData("HitEff") || settings_->bookHistos()) {
int nTP = globals_->event()->nsimtracks();
for (int iTP = 0; iTP < nTP; iTP++) {
L1SimTrack simtrk = globals_->event()->simtrack(iTP);
if (simtrk.pt() < 2.0)
continue;
if (std::abs(simtrk.vz()) > 15.0)
continue;
if (hypot(simtrk.vx(), simtrk.vy()) > 0.1)
continue;
bool electron = (abs(simtrk.type()) == 11);
bool muon = (abs(simtrk.type()) == 13);
bool pion = (abs(simtrk.type()) == 211);
bool kaon = (abs(simtrk.type()) == 321);
bool proton = (abs(simtrk.type()) == 2212);
if (!(electron || muon || pion || kaon || proton))
continue;
int nlayers = 0;
int ndisks = 0;
int simtrackid = simtrk.trackid();
unsigned int hitmask = 0;
hitmask = ev.layersHit(simtrackid, nlayers, ndisks); // FIX CPU use.
if (nlayers + ndisks < 4)
continue;

if (settings_->writeMonitorData("HitEff")) {
static ofstream outhit("hiteff.txt");
outhit << simtrk.eta() << " " << (hitmask & 1) << " " << (hitmask & 2) << " " << (hitmask & 4) << " "
<< (hitmask & 8) << " " << (hitmask & 16) << " " << (hitmask & 32) << " " << (hitmask & 64) << " "
<< (hitmask & 128) << " " << (hitmask & 256) << " " << (hitmask & 512) << " " << (hitmask & 1024)
<< endl;
}

std::unordered_set<int> matchseed;
std::unordered_set<int> matchseedtmp = sector_->seedMatch(iTP);
matchseed.insert(matchseedtmp.begin(), matchseedtmp.end());
if (settings_->bookHistos()) {
for (int iseed = 0; iseed < 8; iseed++) {
bool eff = matchseed.find(iseed) != matchseed.end();
globals_->histograms()->fillSeedEff(iseed, simtrk.eta(), eff);
std::unordered_set<int> matchseed;
std::unordered_set<int> matchseedtmp = sector_->seedMatch(iTP);
matchseed.insert(matchseedtmp.begin(), matchseedtmp.end());
if (settings_->bookHistos()) {
for (int iseed = 0; iseed < 8; iseed++) {
bool eff = matchseed.find(iseed) != matchseed.end();
globals_->histograms()->fillSeedEff(iseed, simtrk.eta(), eff);
}
}
}
}
Expand Down
24 changes: 15 additions & 9 deletions L1Trigger/TrackFindingTracklet/src/imath_calculate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ bool VarBase::calculate(int debug_level) {
if (p3_)
ok3 = p3_->calculate(debug_level);

bool all_ok = debug_level && ok1 && ok2 && ok3;
long int ival_prev = ival_;

local_calculate();

bool all_ok = ok1 && ok2 && ok3 && debug_level;
val_ = ival_ * K_;

if (fval_ > maxval_)
maxval_ = fval_;
if (fval_ < minval_)
minval_ = fval_;
#ifdef IMATH_ROOT
if (globals_->use_root) {
if (h_ == 0) {
Expand All @@ -43,6 +41,18 @@ bool VarBase::calculate(int debug_level) {
}
#endif

if (debug_level)
calcDebug(debug_level, ival_prev, all_ok);

return all_ok;
}

void VarBase::calcDebug(int debug_level, long int ival_prev, bool &all_ok) {
if (fval_ > maxval_)
maxval_ = fval_;
if (fval_ < minval_)
minval_ = fval_;

bool todump = false;
int nmax = sizeof(long int) * 8;
int ns = nmax - nbits_;
Expand All @@ -57,7 +67,6 @@ bool VarBase::calculate(int debug_level) {
all_ok = false;
}

val_ = ival_ * K_;
float ftest = val_;
float tolerance = 0.1 * std::abs(fval_);
if (tolerance < 2 * K_)
Expand All @@ -71,11 +80,8 @@ bool VarBase::calculate(int debug_level) {
}
all_ok = false;
}

if (todump)
edm::LogVerbatim("Tracklet") << dump();

return all_ok;
}

void VarFlag::calculate_step() {
Expand Down
22 changes: 13 additions & 9 deletions L1Trigger/TrackFindingTracklet/test/L1TrackNtupleMaker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ L1TrackNtupleMaker::L1TrackNtupleMaker(edm::ParameterSet const& iConfig) : confi
ttStubMCTruthToken_ = consumes<TTStubAssociationMap<Ref_Phase2TrackerDigi_> >(MCTruthStubInputTag);

TrackingParticleToken_ = consumes<std::vector<TrackingParticle> >(TrackingParticleInputTag);
TrackingVertexToken_ = consumes<std::vector<TrackingVertex> >(TrackingVertexInputTag);
//TrackingVertexToken_ = consumes<std::vector<TrackingVertex> >(TrackingVertexInputTag);
GenJetToken_ = consumes<std::vector<reco::GenJet> >(GenJetInputTag);

getTokenTrackerGeom_ = esConsumes<TrackerGeometry, TrackerDigiGeometryRecord>();
Expand Down Expand Up @@ -713,7 +713,7 @@ void L1TrackNtupleMaker::analyze(const edm::Event& iEvent, const edm::EventSetup
edm::Handle<std::vector<TrackingParticle> > TrackingParticleHandle;
edm::Handle<std::vector<TrackingVertex> > TrackingVertexHandle;
iEvent.getByToken(TrackingParticleToken_, TrackingParticleHandle);
iEvent.getByToken(TrackingVertexToken_, TrackingVertexHandle);
//iEvent.getByToken(TrackingVertexToken_, TrackingVertexHandle);

// -----------------------------------------------------------------------------------------------
// more for TTStubs
Expand Down Expand Up @@ -1097,7 +1097,7 @@ void L1TrackNtupleMaker::analyze(const edm::Event& iEvent, const edm::EventSetup
// ----------------------------------------------------------------------------------------------
// get d0/z0 propagated back to the IP

float tmp_matchtp_t = tan(2.0 * atan(1.0) - 2.0 * atan(exp(-tmp_matchtp_eta)));
float tmp_matchtp_t = 1.0 / tan(2.0 * atan(exp(-tmp_matchtp_eta)));

float delx = -tmp_matchtp_vx;
float dely = -tmp_matchtp_vy;
Expand Down Expand Up @@ -1196,7 +1196,16 @@ void L1TrackNtupleMaker::analyze(const edm::Event& iEvent, const edm::EventSetup
continue; //only care about tracking particles from the primary interaction (except for MyProcess==1, i.e. looking at all TPs)

float tmp_tp_pt = iterTP->pt();
float tmp_tp_charge = iterTP->charge();
float tmp_tp_eta = iterTP->eta();

if (tmp_tp_pt < TP_minPt) // Save CPU by applying these cuts here.
continue;
if (tmp_tp_charge == 0.)
continue;
if (std::abs(tmp_tp_eta) > TP_maxEta)
continue;

float tmp_tp_phi = iterTP->phi();
float tmp_tp_vz = iterTP->vz();
float tmp_tp_vx = iterTP->vx();
Expand All @@ -1208,8 +1217,7 @@ void L1TrackNtupleMaker::analyze(const edm::Event& iEvent, const edm::EventSetup
// ----------------------------------------------------------------------------------------------
// get d0/z0 propagated back to the IP

float tmp_tp_t = tan(2.0 * atan(1.0) - 2.0 * atan(exp(-tmp_tp_eta)));
float tmp_tp_charge = iterTP->charge();
float tmp_tp_t = 1.0 / tan(2.0 * atan(exp(-tmp_tp_eta)));

float delx = -tmp_tp_vx;
float dely = -tmp_tp_vy;
Expand Down Expand Up @@ -1239,10 +1247,6 @@ void L1TrackNtupleMaker::analyze(const edm::Event& iEvent, const edm::EventSetup
if ((MyProcess == 6 || MyProcess == 15 || MyProcess == 211) && abs(tmp_tp_pdgid) != 211)
continue;

if (tmp_tp_pt < TP_minPt)
continue;
if (std::abs(tmp_tp_eta) > TP_maxEta)
continue;
if (std::abs(tmp_tp_z0) > TP_maxZ0)
continue;

Expand Down
4 changes: 2 additions & 2 deletions L1Trigger/TrackFindingTracklet/test/L1TrackQualityPlot.C
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Basic example ROOT script for making tracking performance plots using the ntuples produced by L1TrackNtupleMaker.cc
//
// e.g. in ROOT do: [0] .L L1TrackQualityPlot.C++
// [1] L1TrackQualityPlot("TTbar_PU200_D49")
// [1] L1TrackQualityPlot("TTbar_PU200_D76")
//
// By Claire Savard, July 2020
// Based off of L1TrackNtuplePlot.C
Expand Down Expand Up @@ -677,4 +677,4 @@ void SetPlotStyle() {
// put tick marks on top and RHS of plots
gStyle->SetPadTickX(1);
gStyle->SetPadTickY(1);
}
}
50 changes: 25 additions & 25 deletions L1Trigger/TrackFindingTracklet/test/makeHists.csh
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#!/bin/tcsh
###########################################################################
# Create L1 track histograms & print summary of tracking performance, #
# by running ROOT macro L1TrackNtuplePlot.C on .root file #
# from L1TrackNtupleMaker_cfg.py #
# #
# To use: #
# makeHists.csh rootFileName #
# #
# (where rootFileName is the name of the input .root file, #
# including its directory name, if its not in the current one. #
# If rootFileName not specified, it defaults to TTbar_PU200_hybrid.root) #
###########################################################################
########################################################################
# Create L1 track histograms & print summary of tracking performance, #
# by running ROOT macros L1TrackNtuplePlot.C & L1TrackQualityPlot.C #
# on the .root file from L1TrackNtupleMaker_cfg.py . # # #
# #
# To use: #
# makeHists.csh rootFileName #
# #
# (where rootFileName is the name of the input .root file, #
# including its directory name, if its not in the current one. #
# If rootFileName not specified, it defaults to TTbar_PU200_D76.root) #
########################################################################

if ($#argv == 0) then
set inputFullFileName = "TTbar_PU200_D76.root"
Expand All @@ -32,20 +32,20 @@ set fileName = `basename $inputFullFileName`
# Get stem of filename, removing ".root".
set inputFileStem = `echo $fileName | awk -F . '{print $1;}'`

# Find plotting macro
eval `scramv1 runtime -csh`

# Run track quality MVA plotting macro
set plotMacro = $CMSSW_BASE/src/L1Trigger/TrackFindingTracklet/test/L1TrackQualityPlot.C
if (-e MVA_plots) rm -r MVA_plots
\root -b -q ${plotMacro}'("'${inputFileStem}'","'${dirName}'")'
echo "MVA track quality Histograms written to MVA_plots/"

# Run track performance plotting macro
set plotMacro = $CMSSW_BASE/src/L1Trigger/TrackFindingTracklet/test/L1TrackNtuplePlot.C
if ( -e $plotMacro ) then
# Run plotting macro
if (-e TrkPlots) rm -r TrkPlots
\root -b -q ${plotMacro}'("'${inputFileStem}'","'${dirName}'")' | tail -n 19 >! results.out
cat results.out
echo "Tracking performance summary written to results.out"
echo "Histograms written to TrkPlots/"
else if ( -e ../L1TrackNtuplePlot.C ) then
else
echo "ERROR: $plotMacro not found"
exit(2)
endif
if (-e TrkPlots) rm -r TrkPlots
\root -b -q ${plotMacro}'("'${inputFileStem}'","'${dirName}'")' | tail -n 19 >! results.out
cat results.out
echo "Tracking performance summary written to results.out"
echo "Track performance histograms written to TrkPlots/"

exit
11 changes: 6 additions & 5 deletions L1Trigger/TrackTrigger/interface/L1TrackQuality.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ C.Brown 28/07/20
#include "DataFormats/L1TrackTrigger/interface/TTTrack.h"
#include "DataFormats/L1TrackTrigger/interface/TTTypes.h"
#include "L1Trigger/TrackTrigger/interface/HitPatternHelper.h"

#include "L1Trigger/TrackTrigger/interface/HitPatternHelper.h"
#include "PhysicsTools/ONNXRuntime/interface/ONNXRuntime.h"
#include <memory>

class L1TrackQuality {
public:
Expand Down Expand Up @@ -58,7 +58,7 @@ class L1TrackQuality {
std::string const& ONNXInputName,
std::vector<std::string> const& featureNames);

void setHPHSetup(const hph::Setup* setup);
void beginRun(const hph::Setup* setup);

private:
// Private Member Data
Expand All @@ -72,7 +72,8 @@ class L1TrackQuality {
float bendchi2Max_;
float minPt_;
int nStubsmin_;
const hph::Setup* setup_;
bool useHPH;
const hph::Setup* setupHPH_;
bool useHPH_;
std::unique_ptr<cms::Ort::ONNXRuntime> runTime_;
};
#endif
Loading

0 comments on commit fe0b287

Please sign in to comment.