CMS 3D CMS Logo

KMTF.cc
Go to the documentation of this file.
2 using namespace Phase2L1GMT;
3 
4 KMTF::KMTF(int verbose, const edm::ParameterSet& iConfig)
5  : verbose_(verbose), trackMaker_(std::make_unique<KMTFCore>(iConfig)) {}
6 
7 KMTF::~KMTF() = default;
8 
9 std::pair<std::vector<l1t::KMTFTrack>, std::vector<l1t::KMTFTrack> > KMTF::process(
10  const l1t::MuonStubRefVector& stubsAll, int bx, unsigned int MAXN) {
11  std::vector<l1t::KMTFTrack> pretracksP2;
12  std::vector<l1t::KMTFTrack> pretracksP3;
13  std::vector<l1t::KMTFTrack> pretracksP4;
14  std::vector<l1t::KMTFTrack> pretracksD2;
15  std::vector<l1t::KMTFTrack> pretracksD3;
16  std::vector<l1t::KMTFTrack> pretracksD4;
17  uint Nstubs4 = 0;
18  uint Nstubs3 = 0;
19  uint Nstubs2 = 0;
20  uint Nstubs1 = 0;
21 
26 
27  for (const auto& stub : stubsAll) {
28  if (stub->bxNum() != bx || stub->id() > 3)
29  continue;
30  if (!stub->isBarrel())
31  continue;
32 
33  if (stub->depthRegion() == 4) {
34  if (Nstubs4 < MAXN) {
35  stubs4.push_back(stub);
36  Nstubs4++;
37  }
38  }
39  if (stub->depthRegion() == 3) {
40  if (Nstubs3 < MAXN) {
41  stubs3.push_back(stub);
42  Nstubs3++;
43  }
44  }
45  if (stub->depthRegion() == 2) {
46  if (Nstubs2 < MAXN) {
47  stubs2.push_back(stub);
48  Nstubs2++;
49  }
50  }
51  if (stub->depthRegion() == 1) {
52  if (Nstubs1 < MAXN) {
53  stubs1.push_back(stub);
54  Nstubs1++;
55  }
56  }
57  }
58 
59  //Sort the seeds by tag so that the emulator is aligned like the firmware
61  if (stubs4.size() > 1) {
62  std::sort(stubs4.begin(), stubs4.end(), sorter);
63  }
64  if (stubs3.size() > 1) {
65  std::sort(stubs3.begin(), stubs3.end(), sorter);
66  }
67  if (stubs2.size() > 1) {
68  std::sort(stubs2.begin(), stubs2.end(), sorter);
69  }
70  if (stubs1.size() > 1) {
71  std::sort(stubs1.begin(), stubs1.end(), sorter);
72  }
73 
74  bool pre_patterns = (verbose_ > 1) && ((Nstubs4 + Nstubs3 + Nstubs2 + Nstubs1) > 2);
75 
76  //OK now process the data almost as in hardware
77  for (unsigned int i = 0; i < 32; ++i) {
78  //print the stubs taking into account
79 
80  bool patterns = pre_patterns && ((i < Nstubs4) || (i < Nstubs3) || (i << Nstubs2));
81 
82  if (patterns) {
83  edm::LogInfo("KMTF") << "KMTFPattern " << std::flush;
84  if (i < Nstubs4)
85  edm::LogInfo("KMTF") << stubs4[0]->coord1() << " " << stubs4[0]->coord2() << " " << stubs4[0]->quality() << " "
86  << " 1 " << stubs4[0]->kmtf_address() << " 0 " << std::flush;
87  else
88  edm::LogInfo("KMTF") << "0 0 0 0 511 0 " << std::flush;
89 
90  if (i < Nstubs3) {
91  for (const auto& s : stubs3) {
92  edm::LogInfo("KMTF") << "" << s->coord1() << " " << s->coord2() << " " << s->quality() << " 1 "
93  << s->kmtf_address() << " 0 " << std::flush;
94  }
95  //pad with zeros
96  for (unsigned int j = stubs3.size(); j < 32; ++j) {
97  edm::LogInfo("KMTF") << "0 0 0 0 511 0 " << std::flush;
98  }
99  } else {
100  for (unsigned int j = stubs3.size(); j < 32; ++j) {
101  edm::LogInfo("KMTF") << "0 0 0 0 511 0 " << std::flush;
102  }
103  for (const auto& s : stubs3) {
104  edm::LogInfo("KMTF") << "" << s->coord1() << " " << s->coord2() << " " << s->quality() << " 1 "
105  << s->kmtf_address() << " 0 " << std::flush;
106  }
107  }
108 
109  if (i < Nstubs2) {
110  for (const auto& s : stubs2) {
111  edm::LogInfo("KMTF") << "" << s->coord1() << " " << s->coord2() << " " << s->quality() << " 1 "
112  << s->kmtf_address() << " 0 " << std::flush;
113  }
114  //pad with zeros
115  for (unsigned int j = stubs2.size(); j < 32; ++j) {
116  edm::LogInfo("KMTF") << "0 0 0 0 511 0 " << std::flush;
117  }
118  } else {
119  for (unsigned int j = stubs2.size(); j < 32; ++j) {
120  edm::LogInfo("KMTF") << "0 0 0 0 511 0 " << std::flush;
121  }
122  for (const auto& s : stubs2) {
123  edm::LogInfo("KMTF") << s->coord1() << " " << s->coord2() << " " << s->quality() << " 1 " << s->kmtf_address()
124  << " 0 " << std::flush;
125  }
126  }
127  if (i < Nstubs1) {
128  for (const auto& s : stubs1) {
129  edm::LogInfo("KMTF") << s->coord1() << " " << s->coord2() << " " << s->quality() << " 1 " << s->kmtf_address()
130  << " 0 " << std::flush;
131  }
132  //pad with zeros
133  for (unsigned int j = stubs1.size(); j < 32; ++j) {
134  edm::LogInfo("KMTF") << "0 0 0 0 511 0 " << std::flush;
135  }
136  } else {
137  for (unsigned int j = stubs1.size(); j < 32; ++j) {
138  edm::LogInfo("KMTF") << "0 0 0 0 511 0 " << std::flush;
139  }
140  for (const auto& s : stubs1) {
141  edm::LogInfo("KMTF") << s->coord1() << " " << s->coord2() << " " << s->quality() << " 1 " << s->kmtf_address()
142  << " 0 " << std::flush;
143  }
144  }
145  }
146 
147  //seed is 4
148  if (i < Nstubs4) {
149  l1t::MuonStubRefVector stubs_proc;
150  if (Nstubs3 > 0)
151  stubs_proc.insert(stubs_proc.end(), stubs3.begin(), stubs3.end());
152  if (Nstubs2 > 0)
153  stubs_proc.insert(stubs_proc.end(), stubs2.begin(), stubs2.end());
154  if (Nstubs1 > 0)
155  stubs_proc.insert(stubs_proc.end(), stubs1.begin(), stubs1.end());
156  std::pair<l1t::KMTFTrack, l1t::KMTFTrack> tracks = trackMaker_->chain(stubs4[0], stubs_proc);
157  if (tracks.first.id() & 0x1)
158  pretracksP4.push_back(tracks.first);
159  if (tracks.second.id() & 0x2)
160  pretracksD4.push_back(tracks.second);
161  if (patterns) {
162  if (tracks.first.id() & 0x1)
163  edm::LogInfo("KMTF") << "1 " << (tracks.first.curvatureAtVertex() < 0 ? 1 : 0) << " "
164  << tracks.first.ptPrompt() << " " << tracks.first.phiAtMuon() / (1 << 5) << " "
165  << tracks.first.coarseEta() << " " << int(tracks.first.dxy() * ap_ufixed<8, 1>(1.606))
166  << " " << tracks.first.rankPrompt() << " " << std::flush;
167 
168  else
169  edm::LogInfo("KMTF") << "0 0 0 0 0 0 0 " << std::flush;
170  if (tracks.second.id() & 0x2)
171  edm::LogInfo("KMTF") << "1 " << (tracks.second.curvatureAtVertex() < 0 ? 1 : 0) << " "
172  << tracks.second.ptDisplaced() << " " << tracks.second.phiAtMuon() / (1 << 5) << " "
173  << tracks.second.coarseEta() << " " << int(tracks.second.dxy() * ap_ufixed<8, 1>(1.606))
174  << " " << tracks.second.rankDisp() << " " << std::flush;
175 
176  else
177  edm::LogInfo("KMTF") << "0 0 0 0 0 0 0 " << std::flush;
178  }
179  } else if (patterns) {
180  edm::LogInfo("KMTF") << "0 0 0 0 0 0 0 " << std::flush;
181  edm::LogInfo("KMTF") << "0 0 0 0 0 0 0 " << std::flush;
182  }
183 
184  if (i < Nstubs3) {
185  l1t::MuonStubRefVector stubs_proc;
186  if (Nstubs2 > 0)
187  stubs_proc.insert(stubs_proc.end(), stubs2.begin(), stubs2.end());
188  if (Nstubs1 > 0)
189  stubs_proc.insert(stubs_proc.end(), stubs1.begin(), stubs1.end());
190  std::pair<l1t::KMTFTrack, l1t::KMTFTrack> tracks = trackMaker_->chain(stubs3[0], stubs_proc);
191  if (tracks.first.id() & 0x1)
192  pretracksP3.push_back(tracks.first);
193  if (tracks.second.id() & 0x2)
194  pretracksD3.push_back(tracks.second);
195  if (patterns) {
196  if (tracks.first.id() & 0x1)
197  edm::LogInfo("KMTF") << "1 " << (tracks.first.curvatureAtVertex() < 0 ? 1 : 0) << " "
198  << tracks.first.ptPrompt() << " " << tracks.first.phiAtMuon() / (1 << 5) << " "
199  << tracks.first.coarseEta() << " " << int(tracks.first.dxy() * ap_ufixed<8, 1>(1.606))
200  << " " << tracks.first.rankPrompt() << " " << std::flush;
201 
202  else
203  edm::LogInfo("KMTF") << "0 0 0 0 0 0 0 " << std::flush;
204  if (tracks.second.id() & 0x2)
205  edm::LogInfo("KMTF") << "1 " << (tracks.second.curvatureAtVertex() < 0 ? 1 : 0) << " "
206  << tracks.second.ptDisplaced() << " " << tracks.second.phiAtMuon() / (1 << 5) << " "
207  << tracks.second.coarseEta() << " " << int(tracks.second.dxy() * ap_ufixed<8, 1>(1.606))
208  << " " << tracks.second.rankDisp() << " " << std::flush;
209 
210  else
211  edm::LogInfo("KMTF") << "0 0 0 0 0 0 0 " << std::flush;
212  }
213  } else if (patterns) {
214  edm::LogInfo("KMTF") << "0 0 0 0 0 0 0 " << std::flush;
215  edm::LogInfo("KMTF") << "0 0 0 0 0 0 0 " << std::flush;
216  }
217  if (i < Nstubs2) {
218  l1t::MuonStubRefVector stubs_proc;
219  if (Nstubs1 > 0)
220  stubs_proc.insert(stubs_proc.end(), stubs1.begin(), stubs1.end());
221  std::pair<l1t::KMTFTrack, l1t::KMTFTrack> tracks = trackMaker_->chain(stubs2[0], stubs_proc);
222  if (tracks.first.id() & 0x1)
223  pretracksP2.push_back(tracks.first);
224  if (tracks.second.id() & 0x2)
225  pretracksD2.push_back(tracks.second);
226  if (patterns) {
227  if (tracks.first.id() & 0x1)
228  edm::LogInfo("KMTF") << "1 " << (tracks.first.curvatureAtVertex() < 0 ? 1 : 0) << " "
229  << tracks.first.ptPrompt() << " " << tracks.first.phiAtMuon() / (1 << 5) << " "
230  << tracks.first.coarseEta() << " " << int(tracks.first.dxy() * ap_ufixed<8, 1>(1.606))
231  << " " << tracks.first.rankPrompt() << " " << std::flush;
232 
233  else
234  edm::LogInfo("KMTF") << "0 0 0 0 0 0 0 " << std::flush;
235  if (tracks.second.id() & 0x2)
236  edm::LogInfo("KMTF") << "1 " << (tracks.second.curvatureAtVertex() < 0 ? 1 : 0) << " "
237  << tracks.second.ptDisplaced() << " " << tracks.second.phiAtMuon() / (1 << 5) << " "
238  << tracks.second.coarseEta() << " " << int(tracks.second.dxy() * ap_ufixed<8, 1>(1.606))
239  << " " << tracks.second.rankDisp();
240  else
241  edm::LogInfo("KMTF") << "0 0 0 0 0 0 0" << std::flush;
242  }
243  } else if (patterns) {
244  edm::LogInfo("KMTF") << "0 0 0 0 0 0 0 " << std::flush;
245  edm::LogInfo("KMTF") << "0 0 0 0 0 0 0";
246  }
247  //Now the shift register emulation in C_++
248  if (stubs4.size() > 1) {
249  l1t::MuonStubRef s4 = stubs4[0];
250  stubs4.erase(stubs4.begin(), stubs4.begin() + 1);
251  stubs4.push_back(s4);
252  }
253  if (stubs3.size() > 1) {
254  l1t::MuonStubRef s3 = stubs3[0];
255  stubs3.erase(stubs3.begin(), stubs3.begin() + 1);
256  stubs3.push_back(s3);
257  }
258  if (stubs2.size() > 1) {
259  l1t::MuonStubRef s2 = stubs2[0];
260  stubs2.erase(stubs2.begin(), stubs2.begin() + 1);
261  stubs2.push_back(s2);
262  }
263  if (stubs1.size() > 1) {
264  l1t::MuonStubRef s1 = stubs1[0];
265  stubs1.erase(stubs1.begin(), stubs1.begin() + 1);
266  stubs1.push_back(s1);
267  }
268  }
269 
270  std::vector<l1t::KMTFTrack> cleanedPrompt = cleanRegion(pretracksP2, pretracksP3, pretracksP4, true);
271  std::vector<l1t::KMTFTrack> cleanedDisp = cleanRegion(pretracksD2, pretracksD3, pretracksD4, false);
272  if (verbose_) {
273  edm::LogInfo("KMTF") << "Prompt pretracks 2=" << int(pretracksP2.size()) << " 3=" << int(pretracksP3.size())
274  << " 4=" << int(pretracksP4.size());
275  edm::LogInfo("KMTF") << "Cleaned Tracks Prompt=" << (int)cleanedPrompt.size()
276  << " Displaced=" << (int)cleanedDisp.size();
277  }
278 
279  if (verbose_ && !cleanedPrompt.empty())
280  for (const auto& t : cleanedPrompt)
281  if (t.id() != 0)
282  edm::LogInfo("KMTF") << "final cleaned sector track from all chains track pt=" << t.ptPrompt()
283  << " pattern=" << t.hitPattern() << " rank=" << t.rankPrompt();
284 
285  sort(cleanedPrompt, true);
286  sort(cleanedDisp, false);
287 
288  if (verbose_ && !cleanedPrompt.empty())
289  for (const auto& t : cleanedPrompt)
290  if (t.id() != 0)
291  edm::LogInfo("KMTF") << "final sorted sector track from all chains track pt=" << t.ptPrompt()
292  << " pattern=" << t.hitPattern() << " rank=" << t.rankPrompt();
293 
294  return std::make_pair(cleanedPrompt, cleanedDisp);
295 }
296 
298  int rank1 = vertex ? source.rankPrompt() : source.rankDisp();
299  int rank2 = vertex ? other.rankPrompt() : other.rankDisp();
300  int id1 = vertex ? source.id() & 0x1 : source.id() & 0x2;
301  int id2 = vertex ? other.id() & 0x1 : other.id() & 0x2;
302  bool keep = true;
303  unsigned int pattern = 0;
304  if (id1 == 0)
305  keep = false;
306  else if (id1 != 0 && id2 != 0) {
307  if (eq && rank1 <= rank2)
308  keep = false;
309  if ((!eq) && rank1 < rank2)
310  keep = false;
311  }
312 
314  for (const auto& s1 : source.stubs()) {
315  bool ok = true;
316  for (const auto& s2 : other.stubs()) {
317  if ((*s1) == (*s2) && (!keep))
318  ok = false;
319  }
320  if (ok) {
321  stubs.push_back(s1);
322  pattern = pattern | (1 << (s1->depthRegion() - 1));
323  }
324  }
325  source.setStubs(stubs);
326  source.setHitPattern(pattern);
327 }
328 
329 std::vector<l1t::KMTFTrack> KMTF::cleanRegion(const std::vector<l1t::KMTFTrack>& tracks2,
330  const std::vector<l1t::KMTFTrack>& tracks3,
331  const std::vector<l1t::KMTFTrack>& tracks4,
332  bool vertex) {
333  std::vector<l1t::KMTFTrack> cleaned2;
334  for (unsigned int i = 0; i < tracks2.size(); ++i) {
335  l1t::KMTFTrack source = tracks2[i];
336 
337  for (unsigned int j = 0; j < tracks2.size(); ++j) {
338  if (i == j)
339  continue;
340  overlapCleanTrack(source, tracks2[j], false, vertex);
341  }
342  for (unsigned int j = 0; j < tracks3.size(); ++j) {
343  overlapCleanTrack(source, tracks3[j], true, vertex);
344  }
345  for (unsigned int j = 0; j < tracks4.size(); ++j) {
346  overlapCleanTrack(source, tracks4[j], true, vertex);
347  }
348 
349  if (source.stubs().size() > 1)
350  cleaned2.push_back(source);
351  }
352 
353  std::vector<l1t::KMTFTrack> cleaned3;
354  for (unsigned int i = 0; i < tracks3.size(); ++i) {
355  l1t::KMTFTrack source = tracks3[i];
356 
357  for (unsigned int j = 0; j < tracks3.size(); ++j) {
358  if (i == j)
359  continue;
360  overlapCleanTrack(source, tracks3[j], false, vertex);
361  }
362  for (unsigned int j = 0; j < tracks2.size(); ++j) {
363  overlapCleanTrack(source, tracks2[j], false, vertex);
364  }
365  for (unsigned int j = 0; j < tracks4.size(); ++j) {
366  overlapCleanTrack(source, tracks4[j], true, vertex);
367  }
368 
369  if (source.stubs().size() > 1)
370  cleaned3.push_back(source);
371  }
372 
373  std::vector<l1t::KMTFTrack> cleaned4;
374  for (unsigned int i = 0; i < tracks4.size(); ++i) {
375  l1t::KMTFTrack source = tracks4[i];
376 
377  for (unsigned int j = 0; j < tracks4.size(); ++j) {
378  if (i == j)
379  continue;
380  overlapCleanTrack(source, tracks4[j], false, vertex);
381  }
382  for (unsigned int j = 0; j < tracks3.size(); ++j) {
383  overlapCleanTrack(source, tracks3[j], false, vertex);
384  }
385  for (unsigned int j = 0; j < tracks2.size(); ++j) {
386  overlapCleanTrack(source, tracks2[j], false, vertex);
387  }
388 
389  if (source.stubs().size() > 1)
390  cleaned4.push_back(source);
391  }
392  uint max234 = std::max(cleaned2.size(), std::max(cleaned3.size(), cleaned4.size()));
393 
394  std::vector<l1t::KMTFTrack> output;
395 
396  for (uint i = 0; i < max234; ++i) {
397  if (i < cleaned2.size())
398  output.push_back(cleaned2[i]);
399  if (i < cleaned3.size())
400  output.push_back(cleaned3[i]);
401  if (i < cleaned4.size())
402  output.push_back(cleaned4[i]);
403  }
404  return output;
405 }
406 
407 void KMTF::swap(std::vector<l1t::KMTFTrack>& list, int i, int j, bool vertex) {
408  const l1t::KMTFTrack& track1 = list[i];
409  const l1t::KMTFTrack& track2 = list[j];
410  int id1 = track1.id();
411  int id2 = track2.id();
412  int pt1 = vertex ? track1.ptPrompt() : track1.ptDisplaced();
413  int pt2 = vertex ? track2.ptPrompt() : track2.ptDisplaced();
414  bool swap = false;
415  if (vertex) {
416  id1 = id1 & 0x1;
417  id2 = id2 & 0x1;
418  } else {
419  id1 = id1 & 0x2;
420  id2 = id2 & 0x2;
421  }
422  if (id1 && (!id2))
423  swap = false;
424  else if ((!id1) && id2)
425  swap = true;
426  else if (id1 && id2) {
427  if (pt1 > pt2)
428  swap = false;
429  else
430  swap = true;
431  } else {
432  swap = false;
433  }
434  if (swap) {
435  l1t::KMTFTrack tmp = list[i];
436  list[i] = list[j];
437  list[j] = tmp;
438  }
439 }
440 
441 void KMTF::sort(std::vector<l1t::KMTFTrack>& in, bool vertex) {
442  l1t::KMTFTrack nullTrack;
443  nullTrack.setPtEtaPhi(0, 0, 0);
444  nullTrack.setIDFlag(false, false);
445  nullTrack.setRank(0, vertex);
446  while (in.size() < 32)
447  in.push_back(nullTrack);
448 
449  for (uint iteration = 0; iteration < 16; ++iteration) {
450  for (uint i = 0; i < 32; i = i + 2) {
451  swap(in, i, i + 1, vertex);
452  }
453  for (uint i = 1; i < 31; i = i + 2) {
454  swap(in, i, i + 1, vertex);
455  }
456  }
457 
458  std::vector<l1t::KMTFTrack> out;
459  for (const auto& track : in) {
460  if ((vertex && (track.id() & 0x1)) || ((!vertex) && (track.id() & 0x2)))
461  out.push_back(track);
462  }
463  in = out;
464 }
int id() const
Definition: KMTFTrack.h:139
int verbose_
Definition: KMTF.h:19
bool verbose
int ptPrompt() const
Definition: KMTFTrack.h:97
void sort(std::vector< l1t::KMTFTrack > &in, bool vertex)
Definition: KMTF.cc:441
void swap(std::vector< l1t::KMTFTrack > &list, int i, int j, bool vertex)
Definition: KMTF.cc:407
void setRank(int rank, bool vertex)
Definition: KMTFTrack.h:247
std::unique_ptr< KMTFCore > trackMaker_
Definition: KMTF.h:20
std::vector< edm::Ref< MuonStubCollection > > MuonStubRefVector
Definition: MuonStub.h:44
int ptDisplaced() const
Definition: KMTFTrack.h:88
std::pair< std::vector< l1t::KMTFTrack >, std::vector< l1t::KMTFTrack > > process(const l1t::MuonStubRefVector &stubsAll, int bx, unsigned int MAXN)
Definition: KMTF.cc:9
void overlapCleanTrack(l1t::KMTFTrack &source, const l1t::KMTFTrack &other, bool eq, bool vertex)
Definition: KMTF.cc:297
void setIDFlag(bool passPrompt, bool passDisp)
Definition: KMTFTrack.h:254
Log< level::Info, false > LogInfo
KMTF(int verbose, const edm::ParameterSet &iConfig)
Definition: KMTF.cc:4
std::vector< l1t::KMTFTrack > cleanRegion(const std::vector< l1t::KMTFTrack > &tracks2, const std::vector< l1t::KMTFTrack > &tracks3, const std::vector< l1t::KMTFTrack > &tracks4, bool vertex)
Definition: KMTF.cc:329
void setPtEtaPhi(double pt, double eta, double phi)
Definition: KMTFTrack.h:229
Definition: output.py:1
tmp
align.sh
Definition: createJobs.py:716
static std::string const source
Definition: EdmProvDump.cc:49