CMS 3D CMS Logo

L1TrackVertexAssociationProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: L1Trigger/L1TTrackMatch
4 // Class: L1TrackVertexAssociationProducer
5 //
18 //
19 // Original Author: Alexx Perloff
20 // Created: Thu, 16 Dec 2021 19:02:50 GMT
21 // Derivative Author: Nick Manganelli
22 // Created: Thu, 16 Feb 2023 16:03:32 GMT
23 //
24 //
25 
26 // system include files
27 #include <algorithm>
28 #include <memory>
29 #include <string>
30 #include <vector>
31 
32 // Xilinx HLS includes
33 #include <ap_fixed.h>
34 #include <ap_int.h>
35 
36 // user include files
65 
66 //
67 // class declaration
68 //
69 
71 public:
74 
75  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
76 
77 private:
78  // ----------constants, enums and typedefs ---------
79  // Relevant constants for the converted track word
81  kPtSize = TTTrack_TrackWord::TrackBitWidths::kRinvSize - 1, // Width of pt
82  kPtMagSize = 9, // Width of pt magnitude (unsigned)
83  kEtaSize = TTTrack_TrackWord::TrackBitWidths::kTanlSize, // Width of eta
84  kEtaMagSize = 3, // Width of eta magnitude (signed)
85  };
86 
88  typedef std::vector<TTTrackType> TTTrackCollectionType;
91  typedef std::unique_ptr<TTTrackRefCollectionType> TTTrackRefCollectionUPtr;
92 
93  // ----------member functions ----------------------
94  void printDebugInfo(const edm::Handle<TTTrackRefCollectionType>& l1SelectedTracksHandle,
95  const edm::Handle<TTTrackRefCollectionType>& l1SelectedTracksEmulationHandle,
96  const TTTrackRefCollectionUPtr& vTTTrackAssociatedOutput,
97  const TTTrackRefCollectionUPtr& vTTTrackAssociatedEmulationOutput) const;
98  void printTrackInfo(edm::LogInfo& log, const TTTrackType& track, bool printEmulation = false) const;
99  void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
100 
101  // ----------selectors -----------------------------
102  // Based on recommendations from https://twiki.cern.ch/twiki/bin/view/CMSPublic/SWGuideGenericSelectors
104  TTTrackDeltaZMaxSelector(const std::vector<double>& deltaZMaxEtaBounds, const std::vector<double>& deltaZMax)
107  : deltaZMaxEtaBounds_(cfg.template getParameter<double>("deltaZMaxEtaBounds")),
108  deltaZMax_(cfg.template getParameter<double>("deltaZMax")) {}
109  bool operator()(const TTTrackType& t, const l1t::Vertex& v) const {
110  size_t etaIndex =
111  std::upper_bound(deltaZMaxEtaBounds_.begin(), deltaZMaxEtaBounds_.end(), std::abs(t.momentum().eta())) -
112  deltaZMaxEtaBounds_.begin() - 1;
113  if (etaIndex > deltaZMax_.size() - 1)
114  etaIndex = deltaZMax_.size() - 1;
115  return std::abs(v.z0() - t.z0()) <= deltaZMax_[etaIndex];
116  }
117 
118  private:
119  std::vector<double> deltaZMaxEtaBounds_;
120  std::vector<double> deltaZMax_;
121  };
123  TTTrackWordDeltaZMaxSelector(const std::vector<double>& deltaZMaxEtaBounds, const std::vector<double>& deltaZMax)
126  : deltaZMaxEtaBounds_(cfg.template getParameter<double>("deltaZMaxEtaBounds")),
127  deltaZMax_(cfg.template getParameter<double>("deltaZMax")) {}
128  bool operator()(const TTTrackType& t, const l1t::VertexWord& v) const {
129  TTTrack_TrackWord::tanl_t etaEmulationBits = t.getTanlWord();
130  ap_fixed<TrackBitWidths::kEtaSize, TrackBitWidths::kEtaMagSize> etaEmulation;
131  etaEmulation.V = etaEmulationBits.range();
132  size_t etaIndex =
133  std::upper_bound(deltaZMaxEtaBounds_.begin(), deltaZMaxEtaBounds_.end(), std::abs(etaEmulation.to_double())) -
134  deltaZMaxEtaBounds_.begin() - 1;
135  if (etaIndex > deltaZMax_.size() - 1)
136  etaIndex = deltaZMax_.size() - 1;
137  l1t::VertexWord::vtxz0_t fixedTkZ0 = t.undigitizeSignedValue(
138  t.getZ0Bits(), TTTrack_TrackWord::TrackBitWidths::kZ0Size, TTTrack_TrackWord::stepZ0, 0.0);
139 
140  ap_uint<TrackBitWidths::kPtSize> ptEmulationBits = t.getTrackWord()(
141  TTTrack_TrackWord::TrackBitLocations::kRinvMSB - 1, TTTrack_TrackWord::TrackBitLocations::kRinvLSB);
142  ap_ufixed<TrackBitWidths::kPtSize, TrackBitWidths::kPtMagSize> ptEmulation;
143  ptEmulation.V = ptEmulationBits.range();
144  return std::abs(v.z0() - fixedTkZ0.to_double()) <= deltaZMax_[etaIndex];
145  }
146 
147  private:
148  std::vector<double> deltaZMaxEtaBounds_;
149  std::vector<double> deltaZMax_;
150  };
151 
152  // ----------member data ---------------------------
160  std::vector<double> deltaZMaxEtaBounds_, deltaZMax_;
162  int debug_;
163 };
164 
165 //
166 // constructors and destructor
167 //
169  : processSimulatedTracks_(iConfig.getParameter<bool>("processSimulatedTracks")),
170  processEmulatedTracks_(iConfig.getParameter<bool>("processEmulatedTracks")),
171  l1VerticesToken_(processSimulatedTracks_
172  ? consumes<l1t::VertexCollection>(iConfig.getParameter<edm::InputTag>("l1VerticesInputTag"))
173  : edm::EDGetTokenT<l1t::VertexCollection>()),
174  l1SelectedTracksToken_(
175  processSimulatedTracks_
176  ? consumes<TTTrackRefCollectionType>(iConfig.getParameter<edm::InputTag>("l1SelectedTracksInputTag"))
177  : edm::EDGetTokenT<TTTrackRefCollectionType>()),
178  l1VerticesEmulationToken_(
179  processEmulatedTracks_
180  ? consumes<l1t::VertexWordCollection>(iConfig.getParameter<edm::InputTag>("l1VerticesEmulationInputTag"))
181  : edm::EDGetTokenT<l1t::VertexWordCollection>()),
182  l1SelectedTracksEmulationToken_(processEmulatedTracks_
183  ? consumes<TTTrackRefCollectionType>(iConfig.getParameter<edm::InputTag>(
184  "l1SelectedTracksEmulationInputTag"))
185  : edm::EDGetTokenT<TTTrackRefCollectionType>()),
186  outputCollectionName_(iConfig.getParameter<std::string>("outputCollectionName")),
187  cutSet_(iConfig.getParameter<edm::ParameterSet>("cutSet")),
188 
189  deltaZMaxEtaBounds_(cutSet_.getParameter<std::vector<double>>("deltaZMaxEtaBounds")),
190  deltaZMax_(cutSet_.getParameter<std::vector<double>>("deltaZMax")),
191  useDisplacedTracksDeltaZOverride_(iConfig.getParameter<double>("useDisplacedTracksDeltaZOverride")),
192  debug_(iConfig.getParameter<int>("debug")) {
193  // Confirm the the configuration makes sense
195  throw cms::Exception("You must process at least one of the track collections (simulated or emulated).");
196  }
197 
198  if (deltaZMax_.size() != deltaZMaxEtaBounds_.size() - 1) {
199  throw cms::Exception("The number of deltaZ cuts does not match the number of eta bins!");
200  }
201 
203  deltaZMax_ = std::vector<double>(deltaZMax_.size(), useDisplacedTracksDeltaZOverride_);
204  }
205 
206  // Get additional input tags and define the EDM output based on the previous configuration parameters
208  produces<TTTrackRefCollectionType>(outputCollectionName_);
210  produces<TTTrackRefCollectionType>(outputCollectionName_ + "Emulation");
211 }
212 
214 
215 //
216 // member functions
217 //
218 
220  const edm::Handle<TTTrackRefCollectionType>& l1SelectedTracksHandle,
221  const edm::Handle<TTTrackRefCollectionType>& l1SelectedTracksEmulationHandle,
222  const TTTrackRefCollectionUPtr& vTTTrackAssociatedOutput,
223  const TTTrackRefCollectionUPtr& vTTTrackAssociatedEmulationOutput) const {
224  edm::LogInfo log("L1TrackVertexAssociationProducer");
226  log << "The original selected track collection (pt, eta, phi, nstub, bendchi2, chi2rz, chi2rphi, z0) values are "
227  "... \n";
228  for (const auto& track : *l1SelectedTracksHandle) {
229  printTrackInfo(log, *track, debug_ >= 4);
230  }
231  log << "\t---\n\tNumber of tracks in this selection = " << l1SelectedTracksHandle->size() << "\n\n";
232  }
234  log << "The original selected emulated track collection (pt, eta, phi, nstub, bendchi2, chi2rz, chi2rphi, z0) "
235  "values are ... \n";
236  for (const auto& track : *l1SelectedTracksEmulationHandle) {
237  printTrackInfo(log, *track, debug_ >= 4);
238  }
239  log << "\t---\n\tNumber of tracks in this selection = " << l1SelectedTracksEmulationHandle->size() << "\n\n";
240  }
242  TTTrackRefCollectionType inSimButNotEmu;
243  TTTrackRefCollectionType inEmuButNotSim;
244  std::set_difference(l1SelectedTracksHandle->begin(),
245  l1SelectedTracksHandle->end(),
246  l1SelectedTracksEmulationHandle->begin(),
247  l1SelectedTracksEmulationHandle->end(),
248  std::back_inserter(inSimButNotEmu));
249  std::set_difference(l1SelectedTracksEmulationHandle->begin(),
250  l1SelectedTracksEmulationHandle->end(),
251  l1SelectedTracksHandle->begin(),
252  l1SelectedTracksHandle->end(),
253  std::back_inserter(inEmuButNotSim));
254  log << "The set of tracks selected via cuts on the simulated values which are not in the set of tracks selected "
255  "by cutting on the emulated values ... \n";
256  for (const auto& track : inSimButNotEmu) {
257  printTrackInfo(log, *track, debug_ >= 3);
258  }
259  log << "\t---\n\tNumber of tracks in this selection = " << inSimButNotEmu.size() << "\n\n"
260  << "The set of tracks selected via cuts on the emulated values which are not in the set of tracks selected "
261  "by cutting on the simulated values ... \n";
262  for (const auto& track : inEmuButNotSim) {
263  printTrackInfo(log, *track, debug_ >= 3);
264  }
265  log << "\t---\n\tNumber of tracks in this selection = " << inEmuButNotSim.size() << "\n\n";
266  }
268  log << "The selected and leading vertex associated track collection (pt, eta, phi, nstub, bendchi2, chi2rz, "
269  "chi2rphi, z0) values are ... \n";
270  for (const auto& track : *vTTTrackAssociatedOutput) {
271  printTrackInfo(log, *track, debug_ >= 4);
272  }
273  log << "\t---\n\tNumber of tracks in this selection = " << vTTTrackAssociatedOutput->size() << "\n\n";
274  }
276  log << "The emulation selected and leading vertex associated track collection (pt, eta, phi, nstub, bendchi2, "
277  "chi2rz, chi2rphi, z0) values are "
278  "... \n";
279  for (const auto& track : *vTTTrackAssociatedEmulationOutput) {
280  printTrackInfo(log, *track, debug_ >= 4);
281  }
282  log << "\t---\n\tNumber of tracks in this selection = " << vTTTrackAssociatedEmulationOutput->size() << "\n\n";
283  }
285  TTTrackRefCollectionType inSimButNotEmu;
286  TTTrackRefCollectionType inEmuButNotSim;
287  std::set_difference(vTTTrackAssociatedOutput->begin(),
288  vTTTrackAssociatedOutput->end(),
289  vTTTrackAssociatedEmulationOutput->begin(),
290  vTTTrackAssociatedEmulationOutput->end(),
291  std::back_inserter(inSimButNotEmu));
292  std::set_difference(vTTTrackAssociatedEmulationOutput->begin(),
293  vTTTrackAssociatedEmulationOutput->end(),
294  vTTTrackAssociatedOutput->begin(),
295  vTTTrackAssociatedOutput->end(),
296  std::back_inserter(inEmuButNotSim));
297  log << "The set of vertex associated tracks selected via cuts on the simulated values which are not in the set of "
298  "tracks selected "
299  "by cutting on the emulated values ... \n";
300  for (const auto& track : inSimButNotEmu) {
301  printTrackInfo(log, *track, debug_ >= 3);
302  }
303  log << "\t---\n\tNumber of tracks in this selection = " << inSimButNotEmu.size() << "\n\n"
304  << "The set of vertex associated tracks selected via cuts on the emulated values which are not in the set of "
305  "tracks selected "
306  "by cutting on the simulated values ... \n";
307  for (const auto& track : inEmuButNotSim) {
308  printTrackInfo(log, *track, debug_ >= 3);
309  }
310  log << "\t---\n\tNumber of tracks in this selection = " << inEmuButNotSim.size() << "\n\n";
311  }
312 }
313 
315  const TTTrackType& track,
316  bool printEmulation) const {
317  log << "\t(" << track.momentum().perp() << ", " << track.momentum().eta() << ", " << track.momentum().phi() << ", "
318  << track.getStubRefs().size() << ", " << track.stubPtConsistency() << ", " << track.chi2ZRed() << ", "
319  << track.chi2XYRed() << ", " << track.z0() << ")\n";
320 
321  if (printEmulation) {
322  ap_uint<TrackBitWidths::kPtSize> ptEmulationBits = track.getTrackWord()(
323  TTTrack_TrackWord::TrackBitLocations::kRinvMSB - 1, TTTrack_TrackWord::TrackBitLocations::kRinvLSB);
324  ap_ufixed<TrackBitWidths::kPtSize, TrackBitWidths::kPtMagSize> ptEmulation;
325  ptEmulation.V = ptEmulationBits.range();
326  TTTrack_TrackWord::tanl_t etaEmulationBits = track.getTanlWord();
327  ap_fixed<TrackBitWidths::kEtaSize, TrackBitWidths::kEtaMagSize> etaEmulation;
328  etaEmulation.V = etaEmulationBits.range();
329  double floatTkZ0 = track.undigitizeSignedValue(
330  track.getZ0Bits(), TTTrack_TrackWord::TrackBitWidths::kZ0Size, TTTrack_TrackWord::stepZ0, 0.0);
331  double floatTkPhi = track.undigitizeSignedValue(
332  track.getPhiBits(), TTTrack_TrackWord::TrackBitWidths::kPhiSize, TTTrack_TrackWord::stepPhi0, 0.0);
333  log << "\t\t(" << ptEmulation.to_double() << ", " << etaEmulation.to_double() << ", " << floatTkPhi << ", "
334  << track.getNStubs() << ", " << track.getBendChi2() << ", " << track.getChi2RZ() << ", " << track.getChi2RPhi()
335  << ", " << floatTkZ0 << ")\n";
336  }
337 }
338 
339 // ------------ method called to produce the data ------------
341  auto vTTTrackAssociatedOutput = std::make_unique<TTTrackRefCollectionType>();
342  auto vTTTrackAssociatedEmulationOutput = std::make_unique<TTTrackRefCollectionType>();
343 
344  // TTTrackCollectionHandle l1TracksHandle;
345  edm::Handle<TTTrackRefCollectionType> l1SelectedTracksHandle;
346  edm::Handle<TTTrackRefCollectionType> l1SelectedTracksEmulationHandle;
347  edm::Handle<l1t::VertexCollection> l1VerticesHandle;
348  edm::Handle<l1t::VertexWordCollection> l1VerticesEmulationHandle;
349 
350  l1t::Vertex leadingVertex;
351  l1t::VertexWord leadingEmulationVertex;
352 
355 
357  iEvent.getByToken(l1SelectedTracksToken_, l1SelectedTracksHandle);
358  iEvent.getByToken(l1VerticesToken_, l1VerticesHandle);
359  size_t nOutputApproximate = l1SelectedTracksHandle->size();
360  leadingVertex = l1VerticesHandle->at(0);
361  if (debug_ >= 2) {
362  edm::LogInfo("L1TrackVertexAssociationProducer") << "leading vertex z0 = " << leadingVertex.z0();
363  }
364  vTTTrackAssociatedOutput->reserve(nOutputApproximate);
365  for (const auto& trackword : *l1SelectedTracksHandle) {
366  auto track = l1tVertexFinder::L1Track(edm::refToPtr(trackword));
367  // Select tracks based on the floating point TTTrack
368  if (deltaZSel(*trackword, leadingVertex)) {
369  vTTTrackAssociatedOutput->push_back(trackword);
370  }
371  }
372  iEvent.put(std::move(vTTTrackAssociatedOutput), outputCollectionName_);
373  }
375  iEvent.getByToken(l1SelectedTracksEmulationToken_, l1SelectedTracksEmulationHandle);
376  iEvent.getByToken(l1VerticesEmulationToken_, l1VerticesEmulationHandle);
377  size_t nOutputApproximateEmulation = l1SelectedTracksEmulationHandle->size();
378  leadingEmulationVertex = l1VerticesEmulationHandle->at(0);
379  if (debug_ >= 2) {
380  edm::LogInfo("L1TrackVertexAssociationProducer")
381  << "leading emulation vertex z0 = " << leadingEmulationVertex.z0();
382  }
383  vTTTrackAssociatedEmulationOutput->reserve(nOutputApproximateEmulation);
384  for (const auto& trackword : *l1SelectedTracksEmulationHandle) {
385  // Select tracks based on the bitwise accurate TTTrack_TrackWord
386  if (deltaZSelEmu(*trackword, leadingEmulationVertex)) {
387  vTTTrackAssociatedEmulationOutput->push_back(trackword);
388  }
389  }
390  iEvent.put(std::move(vTTTrackAssociatedEmulationOutput), outputCollectionName_ + "Emulation");
391  }
392 
394  printDebugInfo(l1SelectedTracksHandle,
395  l1SelectedTracksEmulationHandle,
396  vTTTrackAssociatedOutput,
397  vTTTrackAssociatedEmulationOutput);
398  }
399 }
400 
401 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
404  desc.add<edm::InputTag>("l1SelectedTracksInputTag",
405  edm::InputTag("l1tTrackSelectionProducer", "Level1TTTracksSelected"));
406  desc.add<edm::InputTag>("l1SelectedTracksEmulationInputTag",
407  edm::InputTag("l1tTrackSelectionProducer", "Level1TTTracksSelectedEmulation"));
408  desc.add<edm::InputTag>("l1VerticesInputTag", edm::InputTag("l1tVertexFinder", "L1Vertices"));
409  desc.add<edm::InputTag>("l1VerticesEmulationInputTag",
410  edm::InputTag("l1tVertexFinderEmulator", "L1VerticesEmulation"));
411  desc.add<std::string>("outputCollectionName", "Level1TTTracksSelectedAssociated");
412  {
413  edm::ParameterSetDescription descCutSet;
414  descCutSet.add<std::vector<double>>("deltaZMaxEtaBounds", {0.0, 0.7, 1.0, 1.2, 1.6, 2.0, 2.4})
415  ->setComment("these values define the bin boundaries in |eta|");
416  descCutSet.add<std::vector<double>>("deltaZMax", {0.37, 0.50, 0.60, 0.75, 1.00, 1.60})
417  ->setComment(
418  "delta z must be less than these values, there will be one less value here than in deltaZMaxEtaBounds, "
419  "[cm]");
420  desc.add<edm::ParameterSetDescription>("cutSet", descCutSet);
421  }
422  desc.add<double>("useDisplacedTracksDeltaZOverride", -1.0)
423  ->setComment("override the deltaZ cut value for displaced tracks");
424  desc.add<bool>("processSimulatedTracks", true)
425  ->setComment("return selected tracks after cutting on the floating point values");
426  desc.add<bool>("processEmulatedTracks", true)
427  ->setComment("return selected tracks after cutting on the bitwise emulated values");
428  desc.add<int>("debug", 0)->setComment("Verbosity levels: 0, 1, 2, 3");
429  descriptions.addWithDefaultLabel(desc);
430 }
431 
432 //define this as a plug-in
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
void printTrackInfo(edm::LogInfo &log, const TTTrackType &track, bool printEmulation=false) const
bool operator()(const TTTrackType &t, const l1t::VertexWord &v) const
Ptr< typename C::value_type > refToPtr(Ref< C, typename C::value_type, refhelper::FindUsingAdvance< C, typename C::value_type > > const &ref)
Definition: RefToPtr.h:18
L1TrackVertexAssociationProducer(const edm::ParameterSet &)
TTTrackDeltaZMaxSelector(const std::vector< double > &deltaZMaxEtaBounds, const std::vector< double > &deltaZMax)
bool operator()(const TTTrackType &t, const l1t::Vertex &v) const
delete x;
Definition: CaloConfig.h:22
std::vector< Vertex > VertexCollection
Definition: Vertex.h:31
const edm::EDGetTokenT< l1t::VertexCollection > l1VerticesToken_
const edm::EDGetTokenT< TTTrackRefCollectionType > l1SelectedTracksToken_
TTTrackWordDeltaZMaxSelector(const std::vector< double > &deltaZMaxEtaBounds, const std::vector< double > &deltaZMax)
int iEvent
Definition: GenABIO.cc:224
ap_fixed< VertexBitWidths::kZ0Size, VertexBitWidths::kZ0MagSize, AP_RND_CONV, AP_SAT > vtxz0_t
Definition: VertexWord.h:65
edm::Handle< TTTrackCollectionType > TTTrackCollectionHandle
void produce(edm::StreamID, edm::Event &, const edm::EventSetup &) const override
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
def template(fileName, svg, replaceme="REPLACEME")
Definition: svgfig.py:521
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
std::vector< VertexWord > VertexWordCollection
Definition: VertexWord.h:197
ParameterDescriptionBase * add(U const &iLabel, T const &value)
Log< level::Info, false > LogInfo
size_type size() const
Size of the RefVector.
Definition: RefVector.h:102
Class to store the L1 Track Trigger tracks.
Definition: TTTrack.h:29
const_iterator end() const
Termination of iteration.
Definition: RefVector.h:228
void printDebugInfo(const edm::Handle< TTTrackRefCollectionType > &l1SelectedTracksHandle, const edm::Handle< TTTrackRefCollectionType > &l1SelectedTracksEmulationHandle, const TTTrackRefCollectionUPtr &vTTTrackAssociatedOutput, const TTTrackRefCollectionUPtr &vTTTrackAssociatedEmulationOutput) const
static constexpr double stepPhi0
TTTrack< Ref_Phase2TrackerDigi_ > L1Track
Definition: L1Track.h:8
double z0() const
Definition: VertexWord.h:158
std::unique_ptr< TTTrackRefCollectionType > TTTrackRefCollectionUPtr
HLT enums.
ap_uint< TrackBitWidths::kTanlSize > tanl_t
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
const edm::EDGetTokenT< l1t::VertexWordCollection > l1VerticesEmulationToken_
float z0() const
Definition: Vertex.h:21
edm::RefVector< TTTrackCollectionType > TTTrackRefCollectionType
const_iterator begin() const
Initialize an iterator over the RefVector.
Definition: RefVector.h:223
TTTrack< Ref_Phase2TrackerDigi_ > TTTrackType
static constexpr double stepZ0
const edm::EDGetTokenT< TTTrackRefCollectionType > l1SelectedTracksEmulationToken_
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