CMS 3D CMS Logo

TripletEngine.cc
Go to the documentation of this file.
4 
7 
8 #include <algorithm>
9 
10 using namespace std;
11 using namespace trklet;
12 
13 TripletEngine::TripletEngine(string name, Settings const &settings, Globals *global)
14  : ProcessBase(name, settings, global) {
15  stubpairs_.clear();
16  thirdvmstubs_.clear();
17  layer1_ = 0;
18  layer2_ = 0;
19  layer3_ = 0;
20  disk1_ = 0;
21  disk2_ = 0;
22  disk3_ = 0;
23  dct1_ = 0;
24  dct2_ = 0;
25  dct3_ = 0;
26  phi1_ = 0;
27  phi2_ = 0;
28  phi3_ = 0;
29  z1_ = 0;
30  z2_ = 0;
31  z3_ = 0;
32  r1_ = 0;
33  r2_ = 0;
34  r3_ = 0;
35 
36  if (name_[4] == 'L')
37  layer1_ = name_[5] - '0';
38  if (name_[4] == 'D')
39  disk1_ = name_[5] - '0';
40  if (name_[7] == 'L')
41  layer2_ = name_[8] - '0';
42  if (name_[7] == 'D')
43  disk2_ = name_[8] - '0';
44 
45  if (layer1_ == 3 && layer2_ == 4) {
46  layer3_ = 2;
47  iSeed_ = 8;
48  } else if (layer1_ == 5 && layer2_ == 6) {
49  layer3_ = 4;
50  iSeed_ = 9;
51  } else if (layer1_ == 2 && layer2_ == 3) {
52  disk3_ = 1;
53  iSeed_ = 10;
54  } else if (disk1_ == 1 && disk2_ == 2) {
55  layer3_ = 2;
56  iSeed_ = 11;
57  } else
58  throw cms::Exception("LogicError") << __FILE__ << " " << __LINE__ << " Invalid seeding!";
59 
60  if ((layer2_ == 4 && layer3_ == 2) || (layer2_ == 6 && layer3_ == 4)) {
63  }
64  if ((layer2_ == 3 && disk3_ == 1) || (disk2_ == 2 && layer3_ == 2)) {
67  }
69  readTables();
70 }
71 
74  writeTables();
75 }
76 
78  if (settings_.writetrace()) {
79  edm::LogVerbatim("Tracklet") << "In " << name_ << " adding output to " << memory->getName() << " to output "
80  << output;
81  }
82  if (output == "stubtripout") {
83  auto *tmp = dynamic_cast<StubTripletsMemory *>(memory);
84  assert(tmp != nullptr);
86  return;
87  }
88  throw cms::Exception("BadConfig") << __FILE__ << " " << __LINE__ << " Could not find output : " << output;
89 }
90 
92  if (settings_.writetrace()) {
93  edm::LogVerbatim("Tracklet") << "In " << name_ << " adding input from " << memory->getName() << " to input "
94  << input;
95  }
96  if (input == "thirdvmstubin") {
97  auto *tmp = dynamic_cast<VMStubsTEMemory *>(memory);
98  assert(tmp != nullptr);
99  thirdvmstubs_.push_back(tmp);
100  return;
101  }
102  if (input.substr(0, 8) == "stubpair") {
103  auto *tmp = dynamic_cast<StubPairsMemory *>(memory);
104  assert(tmp != nullptr);
105  stubpairs_.push_back(tmp);
106  return;
107  }
108  throw cms::Exception("BadConfig") << __FILE__ << " " << __LINE__ << " Could not find input : " << input;
109 }
110 
112  unsigned int countall = 0;
113  unsigned int countpass = 0;
114  unsigned int nThirdStubs = 0;
115  count_ = 0;
116 
117  for (unsigned int iThirdMem = 0; iThirdMem < thirdvmstubs_.size();
118  nThirdStubs += thirdvmstubs_.at(iThirdMem)->nVMStubs(), iThirdMem++)
119  ;
120 
121  assert(!thirdvmstubs_.empty());
122  assert(!stubpairs_.empty());
123 
124  bool print = false && (getName().substr(0, 10) == "TRE_L2cL3c");
125 
126  print = print && nThirdStubs > 0;
127 
128  int hacksum = 0;
129  if (print) {
130  edm::LogVerbatim("Tracklet") << "In TripletEngine::execute : " << getName() << " " << nThirdStubs << ":";
131  for (unsigned int i = 0; i < thirdvmstubs_.size(); ++i) {
132  edm::LogVerbatim("Tracklet") << thirdvmstubs_.at(i)->getName() << " " << thirdvmstubs_.at(i)->nVMStubs();
133  }
134  int s = 0;
135  std::string oss = "";
136  for (unsigned int i = 0; i < stubpairs_.size(); ++i) {
137  oss += std::to_string(stubpairs_.at(i)->nStubPairs());
138  oss += " ";
139  s += stubpairs_.at(i)->nStubPairs();
140  }
141  hacksum += nThirdStubs * s;
142  edm::LogVerbatim("Tracklet") << oss;
143  for (unsigned int i = 0; i < stubpairs_.size(); ++i) {
144  edm::LogVerbatim("Tracklet") << " " << stubpairs_.at(i)->getName();
145  }
146  }
147 
148  tmpSPTable_.clear();
149 
150  for (unsigned int i = 0; i < stubpairs_.size(); ++i) {
151  for (unsigned int j = 0; j < stubpairs_.at(i)->nStubPairs(); ++j) {
152  if (print)
153  edm::LogVerbatim("Tracklet") << " ***** " << stubpairs_.at(i)->getName() << " "
154  << stubpairs_.at(i)->nStubPairs();
155 
156  auto firstvmstub = stubpairs_.at(i)->getVMStub1(j);
157  auto secondvmstub = stubpairs_.at(i)->getVMStub2(j);
158 
159  if ((layer2_ == 4 && layer3_ == 2) || (layer2_ == 6 && layer3_ == 4)) {
160  constexpr unsigned int vmbitshift = 10;
161  int lookupbits = (int)((firstvmstub.vmbits().value() >> vmbitshift) & 1023); //1023=2^vmbitshift-1
162  int newbin = (lookupbits & 127);
163  int bin = newbin / 8;
164 
165  int start = (bin >> 1);
166  int last = start + (bin & 1);
167 
168  for (int ibin = start; ibin <= last; ibin++) {
169  for (unsigned int k = 0; k < thirdvmstubs_.size(); k++) {
170  string vmsteSuffix = thirdvmstubs_.at(k)->getLastPartOfName();
171  vmsteSuffix = vmsteSuffix.substr(0, vmsteSuffix.find_last_of('n'));
172  if (stubpairs_.at(i)->getLastPartOfName() != vmsteSuffix)
173  continue;
174  for (unsigned int l = 0; l < thirdvmstubs_.at(k)->nVMStubsBinned(ibin); l++) {
175  if (settings_.debugTracklet()) {
176  edm::LogVerbatim("Tracklet") << "In " << getName() << " have third stub";
177  }
178 
179  if (countall >= settings_.maxStep("TRE"))
180  break;
181  countall++;
182 
183  const VMStubTE &thirdvmstub = thirdvmstubs_.at(k)->getVMStubTEBinned(ibin, l);
184 
185  assert(secondphibits_ != -1);
186  assert(thirdphibits_ != -1);
187 
188  unsigned int nvmsecond = settings_.nallstubs(layer2_ - 1) * settings_.nvmte(1, iSeed_);
189  unsigned int nvmbitssecond = nbits(nvmsecond);
190 
191  FPGAWord iphisecondbin = secondvmstub.stub()->iphivmFineBins(nvmbitssecond, secondphibits_);
192 
193  //currently not using same number of bits as in the TED
194  //assert(iphisecondbin==(int)secondvmstub.finephi());
195  FPGAWord iphithirdbin = thirdvmstub.finephi();
196 
197  unsigned int index = (iphisecondbin.value() << thirdphibits_) + iphithirdbin.value();
198 
199  FPGAWord secondbend = secondvmstub.bend();
200  FPGAWord thirdbend = thirdvmstub.bend();
201 
202  index = (index << secondbend.nbits()) + secondbend.value();
203  index = (index << thirdbend.nbits()) + thirdbend.value();
204 
206  (index >= table_.size() || !table_[index])) {
207  if (settings_.debugTracklet()) {
208  edm::LogVerbatim("Tracklet")
209  << "Stub pair rejected because of stub pt cut bends : "
210  << settings_.benddecode(secondvmstub.bend().value(), layer2_ - 1, secondvmstub.isPSmodule())
211  << " " << settings_.benddecode(thirdvmstub.bend().value(), layer3_ - 1, thirdvmstub.isPSmodule());
212  }
213 
214  //FIXME temporarily commented out until bend table fixed
215  //if (!settings_.writeTripletTables())
216  // continue;
217  }
219  if (index >= table_.size())
220  table_.resize(index + 1, false);
221  table_[index] = true;
222 
223  const unsigned spIndex = stubpairs_.at(i)->getIndex(j);
224  const string &tedName = stubpairs_.at(i)->getTEDName(j);
225  if (!tmpSPTable_.count(tedName))
226  tmpSPTable_[tedName];
227  if (spIndex >= tmpSPTable_.at(tedName).size())
228  tmpSPTable_.at(tedName).resize(spIndex + 1);
229  tmpSPTable_.at(tedName).at(spIndex).push_back(stubpairs_.at(i)->getName());
230  }
231 
232  if (settings_.debugTracklet())
233  edm::LogVerbatim("Tracklet") << "Adding layer-layer pair in " << getName();
234  if (settings_.writeMonitorData("Seeds")) {
235  ofstream fout("seeds.txt", ofstream::app);
236  fout << __FILE__ << ":" << __LINE__ << " " << name_ << " " << iSeed_ << endl;
237  fout.close();
238  }
239  stubtriplets_->addStubs(thirdvmstub.stub(),
240  (stubpairs_.at(i))->getVMStub1(j).stub(),
241  (stubpairs_.at(i))->getVMStub2(j).stub());
242 
243  countpass++;
244  }
245  }
246  }
247 
248  }
249 
250  else if (disk2_ == 2 && layer3_ == 2) {
251  int lookupbits = (int)((firstvmstub.vmbits().value() >> 10) & 1023);
252  int newbin = (lookupbits & 127);
253  int bin = newbin / 8;
254 
255  int start = (bin >> 1);
256  int last = start + (bin & 1);
257 
258  if (firstvmstub.stub()->disk().value() < 0) { //TODO - negative disk should come from memory
259  start = settings_.NLONGVMBINS() - last - 1;
260  last = settings_.NLONGVMBINS() - start - 1;
261  }
262 
263  for (int ibin = start; ibin <= last; ibin++) {
264  for (unsigned int k = 0; k < thirdvmstubs_.size(); k++) {
265  string vmsteSuffix = thirdvmstubs_.at(k)->getLastPartOfName();
266  vmsteSuffix = vmsteSuffix.substr(0, vmsteSuffix.find_last_of('n'));
267  if (stubpairs_.at(i)->getLastPartOfName() != vmsteSuffix)
268  continue;
269 
270  for (unsigned int l = 0; l < thirdvmstubs_.at(k)->nVMStubsBinned(ibin); l++) {
271  if (countall >= settings_.maxStep("TRE"))
272  break;
273  countall++;
274 
275  const VMStubTE &thirdvmstub = thirdvmstubs_.at(k)->getVMStubTEBinned(ibin, l);
276 
277  assert(secondphibits_ != -1);
278  assert(thirdphibits_ != -1);
279 
280  FPGAWord iphisecondbin = secondvmstub.finephi();
281  FPGAWord iphithirdbin = thirdvmstub.finephi();
282 
283  unsigned int index = (iphisecondbin.value() << thirdphibits_) + iphithirdbin.value();
284 
285  FPGAWord secondbend = secondvmstub.bend();
286  FPGAWord thirdbend = thirdvmstub.bend();
287 
288  index = (index << secondbend.nbits()) + secondbend.value();
289  index = (index << thirdbend.nbits()) + thirdbend.value();
290 
292  (index >= table_.size() || !table_[index])) {
293  if (settings_.debugTracklet()) {
294  edm::LogVerbatim("Tracklet")
295  << "Stub triplet rejected because of stub pt cut bends : "
296  << settings_.benddecode(secondvmstub.bend().value(), disk2_ + 5, secondvmstub.isPSmodule()) << " "
297  << settings_.benddecode(thirdvmstub.bend().value(), layer3_ - 1, thirdvmstub.isPSmodule());
298  }
299  continue;
300  }
302  if (index >= table_.size())
303  table_.resize(index + 1, false);
304  table_[index] = true;
305 
306  const unsigned spIndex = stubpairs_.at(i)->getIndex(j);
307  const string &tedName = stubpairs_.at(i)->getTEDName(j);
308  if (!tmpSPTable_.count(tedName))
309  tmpSPTable_[tedName];
310  if (spIndex >= tmpSPTable_.at(tedName).size())
311  tmpSPTable_.at(tedName).resize(spIndex + 1);
312  tmpSPTable_.at(tedName).at(spIndex).push_back(stubpairs_.at(i)->getName());
313  }
314 
315  if (settings_.debugTracklet())
316  edm::LogVerbatim("Tracklet") << "Adding layer-disk pair in " << getName();
317  if (settings_.writeMonitorData("Seeds")) {
318  ofstream fout("seeds.txt", ofstream::app);
319  fout << __FILE__ << ":" << __LINE__ << " " << name_ << " " << iSeed_ << endl;
320  fout.close();
321  }
322  stubtriplets_->addStubs(thirdvmstub.stub(),
323  (stubpairs_.at(i))->getVMStub1(j).stub(),
324  (stubpairs_.at(i))->getVMStub2(j).stub());
325  countpass++;
326  }
327  }
328  }
329  }
330 
331  else if (layer2_ == 3 && disk3_ == 1) {
332  int lookupbits = (int)((firstvmstub.vmbits().value() >> 10) & 1023);
333 
334  int newbin = (lookupbits & 127);
335  int bin = newbin / 8;
336 
337  int start = (bin >> 1);
338  int last = start + (bin & 1);
339 
340  for (int ibin = start; ibin <= last; ibin++) {
341  for (unsigned int k = 0; k < thirdvmstubs_.size(); k++) {
342  string vmsteSuffix = thirdvmstubs_.at(k)->getLastPartOfName();
343  vmsteSuffix = vmsteSuffix.substr(0, vmsteSuffix.find_last_of('n'));
344  if (stubpairs_.at(i)->getLastPartOfName() != vmsteSuffix)
345  continue;
346  for (unsigned int l = 0; l < thirdvmstubs_.at(k)->nVMStubsBinned(ibin); l++) {
347  if (countall >= settings_.maxStep("TRE"))
348  break;
349  countall++;
350 
351  const VMStubTE &thirdvmstub = thirdvmstubs_.at(k)->getVMStubTEBinned(ibin, l);
352 
353  assert(secondphibits_ != -1);
354  assert(thirdphibits_ != -1);
355 
356  unsigned int nvmsecond;
357 
358  nvmsecond = settings_.nallstubs(layer2_ - 1) * settings_.nvmte(1, iSeed_);
359  unsigned int nvmbitssecond = nbits(nvmsecond);
360 
361  FPGAWord iphisecondbin = secondvmstub.stub()->iphivmFineBins(nvmbitssecond, secondphibits_);
362 
363  //currentlty not using same number of bits as in the TED
364  //assert(iphisecondbin==(int)secondvmstub.finephi());
365  FPGAWord iphithirdbin = thirdvmstub.finephi();
366 
367  unsigned int index = (iphisecondbin.value() << thirdphibits_) + iphithirdbin.value();
368 
369  FPGAWord secondbend = secondvmstub.bend();
370  FPGAWord thirdbend = thirdvmstub.bend();
371 
372  index = (index << secondbend.nbits()) + secondbend.value();
373  index = (index << thirdbend.nbits()) + thirdbend.value();
374 
376  (index >= table_.size() || !table_[index])) {
377  if (settings_.debugTracklet()) {
378  edm::LogVerbatim("Tracklet")
379  << "Stub pair rejected because of stub pt cut bends : "
380  << settings_.benddecode(secondvmstub.bend().value(), layer2_ - 1, secondvmstub.isPSmodule())
381  << " " << settings_.benddecode(thirdvmstub.bend().value(), disk3_ + 5, thirdvmstub.isPSmodule());
382  }
383  continue;
384  }
386  if (index >= table_.size())
387  table_.resize(index + 1, false);
388  table_[index] = true;
389 
390  const unsigned spIndex = stubpairs_.at(i)->getIndex(j);
391  const string &tedName = stubpairs_.at(i)->getTEDName(j);
392  if (!tmpSPTable_.count(tedName))
393  tmpSPTable_[tedName];
394  if (spIndex >= tmpSPTable_.at(tedName).size())
395  tmpSPTable_.at(tedName).resize(spIndex + 1);
396  tmpSPTable_.at(tedName).at(spIndex).push_back(stubpairs_.at(i)->getName());
397  }
398 
399  if (settings_.debugTracklet())
400  edm::LogVerbatim("Tracklet") << "Adding layer-disk pair in " << getName();
401  if (settings_.writeMonitorData("Seeds")) {
402  ofstream fout("seeds.txt", ofstream::app);
403  fout << __FILE__ << ":" << __LINE__ << " " << name_ << " " << iSeed_ << endl;
404  fout.close();
405  }
406  stubtriplets_->addStubs(thirdvmstub.stub(),
407  (stubpairs_.at(i))->getVMStub1(j).stub(),
408  (stubpairs_.at(i))->getVMStub2(j).stub());
409  countpass++;
410  }
411  }
412  }
413  }
414  }
415  }
416 
417  for (const auto &tedName : tmpSPTable_) {
418  for (unsigned spIndex = 0; spIndex < tedName.second.size(); spIndex++) {
419  if (tedName.second.at(spIndex).empty())
420  continue;
421  vector<string> entry(tedName.second.at(spIndex));
422  sort(entry.begin(), entry.end());
423  entry.erase(unique(entry.begin(), entry.end()), entry.end());
424  const string &spName = entry.at(0);
425 
426  if (!spTable_.count(tedName.first))
427  spTable_[tedName.first];
428  if (spIndex >= spTable_.at(tedName.first).size())
429  spTable_.at(tedName.first).resize(spIndex + 1);
430  if (!spTable_.at(tedName.first).at(spIndex).count(spName))
431  spTable_.at(tedName.first).at(spIndex)[spName] = 0;
432  spTable_.at(tedName.first).at(spIndex)[spName]++;
433  }
434  }
435 
436  if (settings_.writeMonitorData("TRE")) {
437  globals_->ofstream("tripletengine.txt") << getName() << " " << countall << " " << countpass << endl;
438  }
439 }
440 
442  ifstream fin;
443  string tableName, word;
444  unsigned num;
445 
446  string tablePath = settings_.tableTREFile();
447  unsigned int finddir = tablePath.find("table_TRE_");
448  tableName = tablePath.substr(0, finddir) + "table_" + name_ + ".txt";
449 
450  fin.open(tableName, ifstream::in);
451  if (!fin) {
452  throw cms::Exception("BadConfig") << "TripletEngine::readTables, file " << tableName << " not known";
453  }
454  while (!fin.eof()) {
455  fin >> word;
456  num = atoi(word.c_str());
457  table_.push_back(num > 0 ? true : false);
458  }
459  fin.close();
460 }
461 
463  ofstream fout;
464  stringstream tableName;
465 
466  tableName << "table/table_" << name_ << ".txt";
467 
468  fout.open(tableName.str(), ofstream::out);
469  for (const auto entry : table_)
470  fout << entry << endl;
471  fout.close();
472 
473  for (const auto &tedName : spTable_) {
474  tableName.str("");
475  tableName << "table/table_" << tedName.first << "_" << name_ << ".txt";
476 
477  fout.open(tableName.str(), ofstream::out);
478  for (const auto &entry : tedName.second) {
479  for (const auto &spName : entry)
480  fout << spName.first << ":" << spName.second << " ";
481  fout << endl;
482  }
483  fout.close();
484  }
485 }
Definition: start.py:1
Log< level::Info, true > LogVerbatim
std::map< std::string, std::vector< std::vector< std::string > > > tmpSPTable_
Definition: TripletEngine.h:59
unsigned int maxStep(std::string module) const
Definition: Settings.h:116
bool isPSmodule() const
Definition: VMStubTE.h:31
std::string name_
Definition: ProcessBase.h:38
double benddecode(int ibend, int layerdisk, bool isPSmodule) const
Definition: Settings.h:415
bool enableTripletTables() const
Definition: Settings.h:202
const FPGAWord & bend() const
Definition: VMStubTE.h:25
std::string const & tableTREFile() const
Definition: Settings.h:71
Settings const & settings_
Definition: ProcessBase.h:40
Globals * globals_
Definition: ProcessBase.h:41
std::string to_string(const V &value)
Definition: OMSAccess.h:71
void addInput(MemoryBase *memory, std::string input) override
bool writetrace() const
Definition: Settings.h:183
assert(be >=bs)
static std::string const input
Definition: EdmProvDump.cc:50
unsigned int NLONGVMBINS() const
Definition: Settings.h:341
const Stub * stub() const
Definition: VMStubTE.h:29
uint64_t word
unsigned int nbits(unsigned int power)
Definition: ProcessBase.cc:17
std::vector< bool > table_
Definition: TripletEngine.h:61
void addOutput(MemoryBase *memory, std::string output) override
std::vector< VMStubsTEMemory * > thirdvmstubs_
Definition: TripletEngine.h:54
def unique(seq, keepstr=True)
Definition: tier0.py:24
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
int value() const
Definition: FPGAWord.h:24
void addStubs(const Stub *stub1, const Stub *stub2, const Stub *stub3)
StubTripletsMemory * stubtriplets_
Definition: TripletEngine.h:57
bool writeMonitorData(std::string module) const
Definition: Settings.h:109
unsigned int nvmte(unsigned int inner, unsigned int iSeed) const
Definition: Settings.h:101
unsigned int nallstubs(unsigned int layerdisk) const
Definition: Settings.h:107
const FPGAWord & finephi() const
Definition: VMStubTE.h:23
bool debugTracklet() const
Definition: Settings.h:182
std::map< std::string, std::vector< std::map< std::string, unsigned > > > spTable_
Definition: TripletEngine.h:60
int nbits() const
Definition: FPGAWord.h:25
int nfinephi(unsigned int inner, unsigned int iSeed) const
Definition: Settings.h:133
std::ofstream & ofstream(std::string fname)
Definition: Globals.cc:44
std::vector< StubPairsMemory * > stubpairs_
Definition: TripletEngine.h:55
tmp
align.sh
Definition: createJobs.py:716
std::string const & getName() const
Definition: ProcessBase.h:22
bool writeTripletTables() const
Definition: Settings.h:203