CMS 3D CMS Logo

L1TrackSelectionProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: L1Trigger/L1TTrackMatch
4 // Class: L1TrackSelectionProducer
5 //
18 //
19 // Original Author: Alexx Perloff
20 // Created: Thu, 16 Dec 2021 19:02:50 GMT
21 //
22 //
23 
24 // system include files
25 #include <algorithm>
26 #include <memory>
27 #include <string>
28 #include <vector>
29 
30 // Xilinx HLS includes
31 #include <ap_fixed.h>
32 #include <ap_int.h>
33 
34 // user include files
60 
61 //
62 // class declaration
63 //
64 
66 public:
68  ~L1TrackSelectionProducer() override;
69 
70  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
71 
72 private:
73  // ----------constants, enums and typedefs ---------
74  // Relevant constants for the converted track word
76  kPtSize = TTTrack_TrackWord::TrackBitWidths::kRinvSize - 1, // Width of pt
77  kPtMagSize = 9, // Width of pt magnitude (unsigned)
78  kEtaSize = TTTrack_TrackWord::TrackBitWidths::kTanlSize, // Width of eta
79  kEtaMagSize = 3, // Width of eta magnitude (signed)
80  };
81 
83  typedef std::vector<L1Track> TTTrackCollection;
87  typedef std::unique_ptr<TTTrackRefCollection> TTTrackRefCollectionUPtr;
88 
89  // ----------member functions ----------------------
90  void printDebugInfo(const TTTrackCollectionHandle& l1TracksHandle,
91  const TTTrackRefCollectionUPtr& vTTTrackOutput,
92  const TTTrackRefCollectionUPtr& vTTTrackEmulationOutput) const;
93  void printTrackInfo(edm::LogInfo& log, const L1Track& track, bool printEmulation = false) const;
94  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
95 
96  // ----------selectors -----------------------------
97  // Based on recommendations from https://twiki.cern.ch/twiki/bin/view/CMSPublic/SWGuideGenericSelectors
100  TTTrackPtMinSelector(const edm::ParameterSet& cfg) : ptMin_(cfg.template getParameter<double>("ptMin")) {}
101  bool operator()(const L1Track& t) const { return t.momentum().perp() >= ptMin_; }
102 
103  private:
104  double ptMin_;
105  };
108  TTTrackWordPtMinSelector(const edm::ParameterSet& cfg) : ptMin_(cfg.template getParameter<double>("ptMin")) {}
109  bool operator()(const L1Track& t) const {
110  ap_uint<TrackBitWidths::kPtSize> ptEmulationBits = t.getTrackWord()(
111  TTTrack_TrackWord::TrackBitLocations::kRinvMSB - 1, TTTrack_TrackWord::TrackBitLocations::kRinvLSB);
112  ap_ufixed<TrackBitWidths::kPtSize, TrackBitWidths::kPtMagSize> ptEmulation;
113  ptEmulation.V = ptEmulationBits.range();
114  return ptEmulation.to_double() >= ptMin_;
115  }
116 
117  private:
118  double ptMin_;
119  };
123  : absEtaMax_(cfg.template getParameter<double>("absEtaMax")) {}
124  bool operator()(const L1Track& t) const { return std::abs(t.momentum().eta()) <= absEtaMax_; }
125 
126  private:
127  double absEtaMax_;
128  };
132  : absEtaMax_(cfg.template getParameter<double>("absEtaMax")) {}
133  bool operator()(const L1Track& t) const {
134  TTTrack_TrackWord::tanl_t etaEmulationBits = t.getTanlWord();
135  ap_fixed<TrackBitWidths::kEtaSize, TrackBitWidths::kEtaMagSize> etaEmulation;
136  etaEmulation.V = etaEmulationBits.range();
137  return std::abs(etaEmulation.to_double()) <= absEtaMax_;
138  }
139 
140  private:
141  double absEtaMax_;
142  };
145  TTTrackAbsZ0MaxSelector(const edm::ParameterSet& cfg) : absZ0Max_(cfg.template getParameter<double>("absZ0Max")) {}
146  bool operator()(const L1Track& t) const { return std::abs(t.z0()) <= absZ0Max_; }
147 
148  private:
149  double absZ0Max_;
150  };
154  : absZ0Max_(cfg.template getParameter<double>("absZ0Max")) {}
155  bool operator()(const L1Track& t) const {
156  double floatZ0 = t.undigitizeSignedValue(
157  t.getZ0Bits(), TTTrack_TrackWord::TrackBitWidths::kZ0Size, TTTrack_TrackWord::stepZ0, 0.0);
158  return std::abs(floatZ0) <= absZ0Max_;
159  }
160 
161  private:
162  double absZ0Max_;
163  };
167  : nStubsMin_(cfg.template getParameter<double>("nStubsMin")) {}
168  bool operator()(const L1Track& t) const { return t.getStubRefs().size() >= nStubsMin_; }
169 
170  private:
171  double nStubsMin_;
172  };
176  : nStubsMin_(cfg.template getParameter<double>("nStubsMin")) {}
177  bool operator()(const L1Track& t) const { return t.getNStubs() >= nStubsMin_; }
178 
179  private:
180  double nStubsMin_;
181  };
184  : nPSStubsMin_(nStubsMin), tTopo_(tTopo) {}
186  : nPSStubsMin_(cfg.template getParameter<double>("nPSStubsMin")), tTopo_(tTopo) {}
187  bool operator()(const L1Track& t) const {
188  int nPSStubs = 0;
189  for (const auto& stub : t.getStubRefs()) {
190  DetId detId(stub->getDetId());
191  if (detId.det() == DetId::Detector::Tracker) {
192  if ((detId.subdetId() == StripSubdetector::TOB && tTopo_.tobLayer(detId) <= 3) ||
193  (detId.subdetId() == StripSubdetector::TID && tTopo_.tidRing(detId) <= 9))
194  nPSStubs++;
195  }
196  }
197  return nPSStubs >= nPSStubsMin_;
198  }
199 
200  private:
201  double nPSStubsMin_;
203  };
205  TTTrackBendChi2MaxSelector(double bendChi2Max) : bendChi2Max_(bendChi2Max) {}
207  : bendChi2Max_(cfg.template getParameter<double>("reducedBendChi2Max")) {}
208  bool operator()(const L1Track& t) const { return t.stubPtConsistency() < bendChi2Max_; }
209 
210  private:
211  double bendChi2Max_;
212  };
214  TTTrackWordBendChi2MaxSelector(double bendChi2Max) : bendChi2Max_(bendChi2Max) {}
216  : bendChi2Max_(cfg.template getParameter<double>("reducedBendChi2Max")) {}
217  bool operator()(const L1Track& t) const { return t.getBendChi2() < bendChi2Max_; }
218 
219  private:
220  double bendChi2Max_;
221  };
225  : reducedChi2RZMax_(cfg.template getParameter<double>("reducedChi2RZMax")) {}
226  bool operator()(const L1Track& t) const { return t.chi2ZRed() < reducedChi2RZMax_; }
227 
228  private:
230  };
234  : reducedChi2RZMax_(cfg.template getParameter<double>("reducedChi2RZMax")) {}
235  bool operator()(const L1Track& t) const { return t.getChi2RZ() < reducedChi2RZMax_; }
236 
237  private:
239  };
243  : reducedChi2RPhiMax_(cfg.template getParameter<double>("reducedChi2RPhiMax")) {}
244  bool operator()(const L1Track& t) const { return t.chi2XYRed() < reducedChi2RPhiMax_; }
245 
246  private:
248  };
252  : reducedChi2RPhiMax_(cfg.template getParameter<double>("reducedChi2RPhiMax")) {}
253  bool operator()(const L1Track& t) const { return t.getChi2RPhi() < reducedChi2RPhiMax_; }
254 
255  private:
257  };
258 
270 
271  // ----------member data ---------------------------
279  int debug_;
280 };
281 
282 //
283 // constructors and destructor
284 //
286  : l1TracksToken_(consumes<TTTrackCollection>(iConfig.getParameter<edm::InputTag>("l1TracksInputTag"))),
288  outputCollectionName_(iConfig.getParameter<std::string>("outputCollectionName")),
289  cutSet_(iConfig.getParameter<edm::ParameterSet>("cutSet")),
290 
291  ptMin_(cutSet_.getParameter<double>("ptMin")),
292  absEtaMax_(cutSet_.getParameter<double>("absEtaMax")),
293  absZ0Max_(cutSet_.getParameter<double>("absZ0Max")),
294  bendChi2Max_(cutSet_.getParameter<double>("reducedBendChi2Max")),
295  reducedChi2RZMax_(cutSet_.getParameter<double>("reducedChi2RZMax")),
296  reducedChi2RPhiMax_(cutSet_.getParameter<double>("reducedChi2RPhiMax")),
297  nStubsMin_(cutSet_.getParameter<int>("nStubsMin")),
298  nPSStubsMin_(cutSet_.getParameter<int>("nPSStubsMin")),
299  processSimulatedTracks_(iConfig.getParameter<bool>("processSimulatedTracks")),
300  processEmulatedTracks_(iConfig.getParameter<bool>("processEmulatedTracks")),
301  debug_(iConfig.getParameter<int>("debug")) {
302  // Confirm the the configuration makes sense
304  throw cms::Exception("You must process at least one of the track collections (simulated or emulated).");
305  }
306 
308  produces<TTTrackRefCollection>(outputCollectionName_);
309  }
311  produces<TTTrackRefCollection>(outputCollectionName_ + "Emulation");
312  }
313 }
314 
316 
317 //
318 // member functions
319 //
320 
322  const TTTrackRefCollectionUPtr& vTTTrackOutput,
323  const TTTrackRefCollectionUPtr& vTTTrackEmulationOutput) const {
324  edm::LogInfo log("L1TrackSelectionProducer");
325  log << "The original track collection (pt, eta, phi, nstub, bendchi2, chi2rz, chi2rphi, z0) values are ... \n";
326  for (const auto& track : *l1TracksHandle) {
327  printTrackInfo(log, track, debug_ >= 4);
328  }
329  log << "\t---\n\tNumber of tracks in this selection = " << l1TracksHandle->size() << "\n\n";
331  log << "The selected track collection (pt, eta, phi, nstub, bendchi2, chi2rz, chi2rphi, z0) values are ... \n";
332  for (const auto& track : *vTTTrackOutput) {
333  printTrackInfo(log, *track, debug_ >= 4);
334  }
335  log << "\t---\n\tNumber of tracks in this selection = " << vTTTrackOutput->size() << "\n\n";
336  }
338  log << "The emulation selected track collection (pt, eta, phi, nstub, bendchi2, chi2rz, chi2rphi, z0) values are "
339  "... \n";
340  for (const auto& track : *vTTTrackEmulationOutput) {
341  printTrackInfo(log, *track, debug_ >= 4);
342  }
343  log << "\t---\n\tNumber of tracks in this selection = " << vTTTrackEmulationOutput->size() << "\n\n";
344  }
346  TTTrackRefCollection inSimButNotEmu;
347  TTTrackRefCollection inEmuButNotSim;
348  std::set_difference(vTTTrackOutput->begin(),
349  vTTTrackOutput->end(),
350  vTTTrackEmulationOutput->begin(),
351  vTTTrackEmulationOutput->end(),
352  std::back_inserter(inSimButNotEmu));
353  std::set_difference(vTTTrackEmulationOutput->begin(),
354  vTTTrackEmulationOutput->end(),
355  vTTTrackOutput->begin(),
356  vTTTrackOutput->end(),
357  std::back_inserter(inEmuButNotSim));
358  log << "The set of tracks selected via cuts on the simulated values which are not in the set of tracks selected "
359  "by cutting on the emulated values ... \n";
360  for (const auto& track : inSimButNotEmu) {
361  printTrackInfo(log, *track, debug_ >= 3);
362  }
363  log << "\t---\n\tNumber of tracks in this selection = " << inSimButNotEmu.size() << "\n\n"
364  << "The set of tracks selected via cuts on the emulated values which are not in the set of tracks selected "
365  "by cutting on the simulated values ... \n";
366  for (const auto& track : inEmuButNotSim) {
367  printTrackInfo(log, *track, debug_ >= 3);
368  }
369  log << "\t---\n\tNumber of tracks in this selection = " << inEmuButNotSim.size() << "\n\n";
370  }
371 }
372 
373 void L1TrackSelectionProducer::printTrackInfo(edm::LogInfo& log, const L1Track& track, bool printEmulation) const {
374  log << "\t(" << track.momentum().perp() << ", " << track.momentum().eta() << ", " << track.momentum().phi() << ", "
375  << track.getStubRefs().size() << ", " << track.stubPtConsistency() << ", " << track.chi2ZRed() << ", "
376  << track.chi2XYRed() << ", " << track.z0() << ")\n";
377 
378  if (printEmulation) {
379  ap_uint<TrackBitWidths::kPtSize> ptEmulationBits = track.getTrackWord()(
380  TTTrack_TrackWord::TrackBitLocations::kRinvMSB - 1, TTTrack_TrackWord::TrackBitLocations::kRinvLSB);
381  ap_ufixed<TrackBitWidths::kPtSize, TrackBitWidths::kPtMagSize> ptEmulation;
382  ptEmulation.V = ptEmulationBits.range();
383  TTTrack_TrackWord::tanl_t etaEmulationBits = track.getTanlWord();
384  ap_fixed<TrackBitWidths::kEtaSize, TrackBitWidths::kEtaMagSize> etaEmulation;
385  etaEmulation.V = etaEmulationBits.range();
386  double floatTkZ0 = track.undigitizeSignedValue(
387  track.getZ0Bits(), TTTrack_TrackWord::TrackBitWidths::kZ0Size, TTTrack_TrackWord::stepZ0, 0.0);
388  double floatTkPhi = track.undigitizeSignedValue(
389  track.getPhiBits(), TTTrack_TrackWord::TrackBitWidths::kPhiSize, TTTrack_TrackWord::stepPhi0, 0.0);
390  log << "\t\t(" << ptEmulation.to_double() << ", " << etaEmulation.to_double() << ", " << floatTkPhi << ", "
391  << track.getNStubs() << ", " << track.getBendChi2() << ", " << track.getChi2RZ() << ", " << track.getChi2RPhi()
392  << ", " << floatTkZ0 << ")\n";
393  }
394 }
395 
396 // ------------ method called to produce the data ------------
398  auto vTTTrackOutput = std::make_unique<TTTrackRefCollection>();
399  auto vTTTrackEmulationOutput = std::make_unique<TTTrackRefCollection>();
400 
401  // Tracker Topology
402  const TrackerTopology& tTopo = iSetup.getData(tTopoToken_);
403 
404  TTTrackCollectionHandle l1TracksHandle;
405 
406  iEvent.getByToken(l1TracksToken_, l1TracksHandle);
407  size_t nOutputApproximate = l1TracksHandle->size();
409  vTTTrackOutput->reserve(nOutputApproximate);
410  }
412  vTTTrackEmulationOutput->reserve(nOutputApproximate);
413  }
414 
419  TTTrackNPSStubsMinSelector nPSStubsSel(nPSStubsMin_, tTopo);
420 
421  for (size_t i = 0; i < nOutputApproximate; i++) {
422  const auto& track = l1TracksHandle->at(i);
423 
424  // Select tracks based on the floating point TTTrack
425  if (processSimulatedTracks_ && kinSel(track) && nPSStubsSel(track) && chi2Sel(track)) {
426  vTTTrackOutput->push_back(TTTrackRef(l1TracksHandle, i));
427  }
428 
429  // Select tracks based on the bitwise accurate TTTrack_TrackWord
430  if (processEmulatedTracks_ && kinSelEmu(track) && chi2SelEmu(track)) {
431  vTTTrackEmulationOutput->push_back(TTTrackRef(l1TracksHandle, i));
432  }
433  }
434 
435  if (debug_ >= 2) {
436  printDebugInfo(l1TracksHandle, vTTTrackOutput, vTTTrackEmulationOutput);
437  }
438 
439  // Put the outputs into the event
441  iEvent.put(std::move(vTTTrackOutput), outputCollectionName_);
442  }
444  iEvent.put(std::move(vTTTrackEmulationOutput), outputCollectionName_ + "Emulation");
445  }
446 }
447 
448 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
450  //L1TrackSelectionProducer
452  desc.add<edm::InputTag>("l1TracksInputTag", edm::InputTag("TTTracksFromTrackletEmulation", "Level1TTTracks"));
453  desc.add<std::string>("outputCollectionName", "Level1TTTracksSelected");
454  {
455  edm::ParameterSetDescription descCutSet;
456  descCutSet.add<double>("ptMin", 2.0)->setComment("pt must be greater than this value, [GeV]");
457  descCutSet.add<double>("absEtaMax", 2.4)->setComment("absolute value of eta must be less than this value");
458  descCutSet.add<double>("absZ0Max", 15.0)->setComment("z0 must be less than this value, [cm]");
459  descCutSet.add<int>("nStubsMin", 4)->setComment("number of stubs must be greater than or equal to this value");
460  descCutSet.add<int>("nPSStubsMin", 0)
461  ->setComment("number of stubs in the PS Modules must be greater than or equal to this value");
462 
463  descCutSet.add<double>("reducedBendChi2Max", 2.25)->setComment("bend chi2 must be less than this value");
464  descCutSet.add<double>("reducedChi2RZMax", 5.0)->setComment("chi2rz/dof must be less than this value");
465  descCutSet.add<double>("reducedChi2RPhiMax", 20.0)->setComment("chi2rphi/dof must be less than this value");
466 
467  desc.add<edm::ParameterSetDescription>("cutSet", descCutSet);
468  }
469  desc.add<bool>("processSimulatedTracks", true)
470  ->setComment("return selected tracks after cutting on the floating point values");
471  desc.add<bool>("processEmulatedTracks", true)
472  ->setComment("return selected tracks after cutting on the bitwise emulated values");
473  desc.add<int>("debug", 0)->setComment("Verbosity levels: 0, 1, 2, 3");
474  descriptions.addWithDefaultLabel(desc);
475 }
476 
477 //define this as a plug-in
L1TrackSelectionProducer(const edm::ParameterSet &)
void setComment(std::string const &value)
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
edm::ESGetToken< TrackerTopology, TrackerTopologyRcd > tTopoToken_
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
unsigned int tobLayer(const DetId &id) const
edm::Handle< TTTrackCollection > TTTrackCollectionHandle
float floatZ0(z0_t z0)
Definition: datatypes.h:172
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
AndSelector< TTTrackBendChi2MaxSelector, TTTrackChi2RZMaxSelector, TTTrackChi2RPhiMaxSelector > TTTrackBendChi2Chi2RZChi2RPhiMaxSelector
AndSelector< TTTrackWordBendChi2MaxSelector, TTTrackWordChi2RZMaxSelector, TTTrackWordChi2RPhiMaxSelector > TTTrackWordBendChi2Chi2RZChi2RPhiMaxSelector
AndSelector< TTTrackWordPtMinSelector, TTTrackWordAbsEtaMaxSelector, TTTrackWordAbsZ0MaxSelector, TTTrackWordNStubsMinSelector > TTTrackWordPtMinEtaMaxZ0MaxNStubsMinSelector
AndSelector< TTTrackPtMinSelector, TTTrackAbsEtaMaxSelector, TTTrackAbsZ0MaxSelector, TTTrackNStubsMinSelector > TTTrackPtMinEtaMaxZ0MaxNStubsMinSelector
constexpr float ptMin
TTTrackNPSStubsMinSelector(const edm::ParameterSet &cfg, const TrackerTopology &tTopo)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::unique_ptr< TTTrackRefCollection > TTTrackRefCollectionUPtr
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
TTTrackNPSStubsMinSelector(double nStubsMin, const TrackerTopology &tTopo)
int iEvent
Definition: GenABIO.cc:224
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
edm::Ref< TTTrackCollection > TTTrackRef
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:521
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
const edm::ParameterSet cutSet_
static constexpr auto TOB
TTTrack< Ref_Phase2TrackerDigi_ > L1Track
ParameterDescriptionBase * add(U const &iLabel, T const &value)
std::vector< L1Track > TTTrackCollection
Definition: DetId.h:17
Class to store the L1 Track Trigger tracks.
Definition: TTTrack.h:29
void printTrackInfo(edm::LogInfo &log, const L1Track &track, bool printEmulation=false) const
static constexpr double stepPhi0
void printDebugInfo(const TTTrackCollectionHandle &l1TracksHandle, const TTTrackRefCollectionUPtr &vTTTrackOutput, const TTTrackRefCollectionUPtr &vTTTrackEmulationOutput) const
edm::RefVector< TTTrackCollection > TTTrackRefCollection
HLT enums.
ap_uint< TrackBitWidths::kTanlSize > tanl_t
unsigned int tidRing(const DetId &id) const
static constexpr double stepZ0
static constexpr auto TID
std::vector< std::string > set_difference(std::vector< std::string > const &v1, std::vector< std::string > const &v2)
def move(src, dest)
Definition: eostools.py:511
const edm::EDGetTokenT< TTTrackCollection > l1TracksToken_