CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TrackletCalculator.cc
Go to the documentation of this file.
10 
13 
14 using namespace std;
15 using namespace trklet;
16 
17 TrackletCalculator::TrackletCalculator(string name, Settings const& settings, Globals* globals)
18  : TrackletCalculatorBase(name, settings, globals) {
19  for (unsigned int ilayer = 0; ilayer < N_LAYER; ilayer++) {
20  vector<TrackletProjectionsMemory*> tmp(settings.nallstubs(ilayer), nullptr);
21  trackletprojlayers_.push_back(tmp);
22  }
23 
24  for (unsigned int idisk = 0; idisk < N_DISK; idisk++) {
25  vector<TrackletProjectionsMemory*> tmp(settings.nallstubs(idisk + N_LAYER), nullptr);
26  trackletprojdisks_.push_back(tmp);
27  }
28 
30 
31  // set TC index
32  iTC_ = name_[7] - 'A';
33 
34  TCIndex_ = (iSeed_ << 4) + iTC_;
35  assert(TCIndex_ >= 0 && TCIndex_ <= (int)settings_.ntrackletmax());
36 
38  double phicritFactor =
40  if (std::abs(phicritFactor - 2.) > 0.25)
41  edm::LogPrint("Tracklet")
42  << "TrackletCalculator::TrackletCalculator phicrit approximation may be invalid! Please check.";
43  }
44 
45  // write the drinv and invt inverse tables
47  void (*writeLUT)(const VarInv&, const string&) = nullptr;
48  if (settings.writeInvTable()) { // Verilog version
49  writeLUT = [](const VarInv& x, const string& basename) -> void {
50  ofstream fs(basename + ".tab");
51  return x.writeLUT(fs, VarBase::verilog);
52  };
53  } else { // HLS version
54  writeLUT = [](const VarInv& x, const string& basename) -> void {
55  ofstream fs(basename + ".tab");
56  return x.writeLUT(fs, VarBase::hls);
57  };
58  }
59  writeInvTable(writeLUT);
60  }
61 
62  // write the firmware design for the calculation of the tracklet parameters
63  // and projections
64  if ((settings_.writeVerilog() || settings_.writeHLS()) && iTC_ == 0) {
65  void (*writeDesign)(const vector<VarBase*>&, const string&) = nullptr;
66  if (settings.writeVerilog()) { // Verilog version
67  writeDesign = [](const vector<VarBase*>& v, const string& basename) -> void {
68  ofstream fs(basename + ".v");
69  return VarBase::verilog_print(v, fs);
70  };
71  } else { // HLS version
72  writeDesign = [](const vector<VarBase*>& v, const string& basename) -> void {
73  ofstream fs(basename + ".cpp");
74  return VarBase::hls_print(v, fs);
75  };
76  }
77  writeFirmwareDesign(writeDesign);
78  }
79 }
80 
82  outputProj = dynamic_cast<TrackletProjectionsMemory*>(memory);
83  assert(outputProj != nullptr);
84 }
85 
87  if (settings_.writetrace()) {
88  edm::LogVerbatim("Tracklet") << "In " << name_ << " adding output to " << memory->getName() << " to output "
89  << output;
90  }
91  if (output == "trackpar") {
92  auto* tmp = dynamic_cast<TrackletParametersMemory*>(memory);
93  assert(tmp != nullptr);
95  return;
96  }
97 
98  if (output.substr(0, 7) == "projout") {
99  //output is on the form 'projoutL2PHIC' or 'projoutD3PHIB'
100  auto* tmp = dynamic_cast<TrackletProjectionsMemory*>(memory);
101  assert(tmp != nullptr);
102 
103  unsigned int layerdisk = output[8] - '1'; //layer or disk counting from 0
104  unsigned int phiregion = output[12] - 'A'; //phiregion counting from 0
105 
106  if (output[7] == 'L') {
107  assert(layerdisk < N_LAYER);
108  assert(phiregion < trackletprojlayers_[layerdisk].size());
109  //check that phiregion not already initialized
110  assert(trackletprojlayers_[layerdisk][phiregion] == nullptr);
111  trackletprojlayers_[layerdisk][phiregion] = tmp;
112  return;
113  }
114 
115  if (output[7] == 'D') {
116  assert(layerdisk < N_DISK);
117  assert(phiregion < trackletprojdisks_[layerdisk].size());
118  //check that phiregion not already initialized
119  assert(trackletprojdisks_[layerdisk][phiregion] == nullptr);
120  trackletprojdisks_[layerdisk][phiregion] = tmp;
121  return;
122  }
123  }
124 
125  throw cms::Exception("BadConfig") << __FILE__ << " " << __LINE__ << " Could not find output : " << output;
126 }
127 
129  if (settings_.writetrace()) {
130  edm::LogVerbatim("Tracklet") << "In " << name_ << " adding input from " << memory->getName() << " to input "
131  << input;
132  }
133  if (input == "innerallstubin") {
134  auto* tmp = dynamic_cast<AllStubsMemory*>(memory);
135  assert(tmp != nullptr);
136  innerallstubs_.push_back(tmp);
137  return;
138  }
139  if (input == "outerallstubin") {
140  auto* tmp = dynamic_cast<AllStubsMemory*>(memory);
141  assert(tmp != nullptr);
142  outerallstubs_.push_back(tmp);
143  return;
144  }
145  if (input.substr(0, 8) == "stubpair") {
146  auto* tmp = dynamic_cast<StubPairsMemory*>(memory);
147  assert(tmp != nullptr);
148  stubpairs_.push_back(tmp);
149  return;
150  }
151  throw cms::Exception("BadConfig") << __FILE__ << " " << __LINE__ << " Could not find intput : " << input;
152 }
153 
154 void TrackletCalculator::execute(unsigned int iSector, double phimin, double phimax) {
155  unsigned int countall = 0;
156  unsigned int countsel = 0;
157 
158  phimin_ = phimin;
159  phimax_ = phimax;
160  iSector_ = iSector;
161 
162  //Helpfull to have for debugging the HLS code - will keep here for now.
163  //bool print = (iSector == 3) && (getName() == "TC_L1L2G");
164  //print = false;
165 
166  for (auto& stubpair : stubpairs_) {
168  edm::LogVerbatim("Tracklet") << "Will break on too many tracklets in " << getName();
169  break;
170  }
171  for (unsigned int i = 0; i < stubpair->nStubPairs(); i++) {
172  countall++;
173  const Stub* innerFPGAStub = stubpair->getVMStub1(i).stub();
174  const L1TStub* innerStub = innerFPGAStub->l1tstub();
175 
176  const Stub* outerFPGAStub = stubpair->getVMStub2(i).stub();
177  const L1TStub* outerStub = outerFPGAStub->l1tstub();
178 
179  if (settings_.debugTracklet()) {
180  edm::LogVerbatim("Tracklet") << "TrackletCalculator execute " << getName() << "[" << iSector << "]";
181  }
182 
183  if (innerFPGAStub->layerdisk() < N_LAYER && (getName() != "TC_D1L2A" && getName() != "TC_D1L2B")) {
184  if (outerFPGAStub->layerdisk() >= N_LAYER) {
185  //overlap seeding
186  bool accept = overlapSeeding(outerFPGAStub, outerStub, innerFPGAStub, innerStub);
187  if (accept)
188  countsel++;
189  } else {
190  //barrel+barrel seeding
191  bool accept = barrelSeeding(innerFPGAStub, innerStub, outerFPGAStub, outerStub);
192  if (accept)
193  countsel++;
194  }
195  } else {
196  if (outerFPGAStub->layerdisk() >= N_LAYER) {
197  //disk+disk seeding
198  bool accept = diskSeeding(innerFPGAStub, innerStub, outerFPGAStub, outerStub);
199  if (accept)
200  countsel++;
201  } else if (innerFPGAStub->layerdisk() >= N_LAYER) {
202  //layer+disk seeding
203  bool accept = overlapSeeding(innerFPGAStub, innerStub, outerFPGAStub, outerStub);
204  if (accept)
205  countsel++;
206  } else {
207  throw cms::Exception("LogicError") << __FILE__ << " " << __LINE__ << " invalid seeding";
208  }
209  }
210 
212  edm::LogVerbatim("Tracklet") << "Will break on number of tracklets in " << getName();
213  break;
214  }
215 
216  if (countall >= settings_.maxStep("TC")) {
217  if (settings_.debugTracklet())
218  edm::LogVerbatim("Tracklet") << "Will break on MAXTC 1";
219  break;
220  }
221  if (settings_.debugTracklet()) {
222  edm::LogVerbatim("Tracklet") << "TrackletCalculator execute done";
223  }
224  }
225  if (countall >= settings_.maxStep("TC")) {
226  if (settings_.debugTracklet())
227  edm::LogVerbatim("Tracklet") << "Will break on MAXTC 2";
228  break;
229  }
230  }
231 
232  if (settings_.writeMonitorData("TC")) {
233  globals_->ofstream("trackletcalculator.txt") << getName() << " " << countall << " " << countsel << endl;
234  }
235 }
236 
237 void TrackletCalculator::writeInvTable(void (*writeLUT)(const VarInv&, const string&)) {
238  switch (iSeed_) {
239  case 0: // L1L2
240  writeLUT(globals_->ITC_L1L2()->drinv, settings_.tablePath() + "TC_L1L2_drinv");
241  writeLUT(globals_->ITC_L1L2()->invt, settings_.tablePath() + "TC_L1L2_invt");
242  break;
243  case 1: // L2L3
244  writeLUT(globals_->ITC_L2L3()->drinv, settings_.tablePath() + "TC_L2L3_drinv");
245  writeLUT(globals_->ITC_L2L3()->invt, settings_.tablePath() + "TC_L2L3_invt");
246  break;
247  case 2: // L3L4
248  writeLUT(globals_->ITC_L3L4()->drinv, settings_.tablePath() + "TC_L3L4_drinv");
249  writeLUT(globals_->ITC_L3L4()->invt, settings_.tablePath() + "TC_L3L4_invt");
250  break;
251  case 3: // L5L6
252  writeLUT(globals_->ITC_L5L6()->drinv, settings_.tablePath() + "TC_L5L6_drinv");
253  writeLUT(globals_->ITC_L5L6()->invt, settings_.tablePath() + "TC_L5L6_invt");
254  break;
255  case 4: // D1D2
256  writeLUT(globals_->ITC_F1F2()->drinv, settings_.tablePath() + "TC_F1F2_drinv");
257  writeLUT(globals_->ITC_F1F2()->invt, settings_.tablePath() + "TC_F1F2_invt");
258  writeLUT(globals_->ITC_B1B2()->drinv, settings_.tablePath() + "TC_B1B2_drinv");
259  writeLUT(globals_->ITC_B1B2()->invt, settings_.tablePath() + "TC_B1B2_invt");
260  break;
261  case 5: // D3D4
262  writeLUT(globals_->ITC_F3F4()->drinv, settings_.tablePath() + "TC_F3F4_drinv");
263  writeLUT(globals_->ITC_F3F4()->invt, settings_.tablePath() + "TC_F3F4_invt");
264  writeLUT(globals_->ITC_B3B4()->drinv, settings_.tablePath() + "TC_B3B4_drinv");
265  writeLUT(globals_->ITC_B3B4()->invt, settings_.tablePath() + "TC_B3B4_invt");
266  break;
267  case 6: // L1D1
268  writeLUT(globals_->ITC_L1F1()->drinv, settings_.tablePath() + "TC_L1F1_drinv");
269  writeLUT(globals_->ITC_L1F1()->invt, settings_.tablePath() + "TC_L1F1_invt");
270  writeLUT(globals_->ITC_L1B1()->drinv, settings_.tablePath() + "TC_L1B1_drinv");
271  writeLUT(globals_->ITC_L1B1()->invt, settings_.tablePath() + "TC_L1B1_invt");
272  break;
273  case 7: // L2D1
274  writeLUT(globals_->ITC_L2F1()->drinv, settings_.tablePath() + "TC_L2F1_drinv");
275  writeLUT(globals_->ITC_L2F1()->invt, settings_.tablePath() + "TC_L2F1_invt");
276  writeLUT(globals_->ITC_L2B1()->drinv, settings_.tablePath() + "TC_L2B1_drinv");
277  writeLUT(globals_->ITC_L2B1()->invt, settings_.tablePath() + "TC_L2B1_invt");
278  break;
279  }
280 }
281 
282 void TrackletCalculator::writeFirmwareDesign(void (*writeDesign)(const vector<VarBase*>&, const string&)) {
283  switch (iSeed_) {
284  case 0: // L1L2
285  {
286  const vector<VarBase*> v = {&globals_->ITC_L1L2()->rinv_final, &globals_->ITC_L1L2()->phi0_final,
299  writeDesign(v, "TC_L1L2");
300  } break;
301  case 1: // L2L3
302  {
303  const vector<VarBase*> v = {&globals_->ITC_L2L3()->rinv_final, &globals_->ITC_L2L3()->phi0_final,
316  writeDesign(v, "TC_L2L3");
317  } break;
318  case 2: // L3L4
319  {
320  const vector<VarBase*> v = {&globals_->ITC_L3L4()->rinv_final, &globals_->ITC_L3L4()->phi0_final,
333  writeDesign(v, "TC_L3L4");
334  } break;
335  case 3: // L5L6
336  {
337  const vector<VarBase*> v = {&globals_->ITC_L5L6()->rinv_final, &globals_->ITC_L5L6()->phi0_final,
350  writeDesign(v, "TC_L5L6");
351  } break;
352  case 4: // D1D2
353  {
354  const vector<VarBase*> v = {&globals_->ITC_F1F2()->rinv_final, &globals_->ITC_F1F2()->phi0_final,
364  writeDesign(v, "TC_F1F2");
365  }
366  {
367  const vector<VarBase*> v = {&globals_->ITC_B1B2()->rinv_final, &globals_->ITC_B1B2()->phi0_final,
377  writeDesign(v, "TC_B1B2");
378  }
379  break;
380  case 5: // D3D4
381  {
382  const vector<VarBase*> v = {&globals_->ITC_F3F4()->rinv_final, &globals_->ITC_F3F4()->phi0_final,
392  writeDesign(v, "TC_F3F4");
393  }
394  {
395  const vector<VarBase*> v = {&globals_->ITC_B3B4()->rinv_final, &globals_->ITC_B3B4()->phi0_final,
405  writeDesign(v, "TC_B3B4");
406  }
407  break;
408  case 6: // L1D1
409  {
410  const vector<VarBase*> v = {&globals_->ITC_L1F1()->rinv_final, &globals_->ITC_L1F1()->phi0_final,
421  writeDesign(v, "TC_L1F1");
422  }
423  {
424  const vector<VarBase*> v = {&globals_->ITC_L1B1()->rinv_final, &globals_->ITC_L1B1()->phi0_final,
435  writeDesign(v, "TC_L1B1");
436  }
437  break;
438  case 7: // L2D1
439  {
440  const vector<VarBase*> v = {&globals_->ITC_L2F1()->rinv_final, &globals_->ITC_L2F1()->phi0_final,
451  writeDesign(v, "TC_L2F1");
452  }
453  {
454  const vector<VarBase*> v = {&globals_->ITC_L2B1()->rinv_final, &globals_->ITC_L2B1()->phi0_final,
465  writeDesign(v, "TC_L2B1");
466  }
467  break;
468  }
469 }
Log< level::Info, true > LogVerbatim
bool writeHLSInvTable() const
Definition: Settings.h:198
constexpr int N_DISK
Definition: Settings.h:22
bool diskSeeding(const Stub *innerFPGAStub, const L1TStub *innerStub, const Stub *outerFPGAStub, const L1TStub *outerStub)
std::string name_
Definition: ProcessBase.h:38
IMATH_TrackletCalculator * ITC_L2L3()
Definition: Globals.h:49
unsigned int ntrackletmax() const
Definition: Settings.h:331
static void hls_print(const std::vector< VarBase * > &v, std::ofstream &fs)
Definition: imath.h:275
void writeFirmwareDesign(void(*writeDesign)(const std::vector< VarBase * > &, const std::string &))
void initLayerDisksandISeed(unsigned int &layerdisk1, unsigned int &layerdisk2, unsigned int &iSeed)
Definition: ProcessBase.cc:63
Settings const & settings_
Definition: ProcessBase.h:40
Globals * globals_
Definition: ProcessBase.h:41
double rcrit() const
Definition: Settings.h:288
double K() const
Definition: imath.h:246
IMATH_TrackletCalculatorOverlap * ITC_L2B1()
Definition: Globals.h:61
assert(be >=bs)
bool accept(const edm::Event &event, const edm::TriggerResults &triggerTable, const std::string &triggerPath)
Definition: TopDQMHelpers.h:31
bool debugTracklet() const
Definition: Settings.h:182
bool writeVerilog() const
Definition: Settings.h:195
IMATH_TrackletCalculatorDisk * ITC_B1B2()
Definition: Globals.h:55
void addOutput(MemoryBase *memory, std::string output) override
static struct trklet::VarBase::Verilog verilog
std::vector< AllStubsMemory * > innerallstubs_
static std::string const input
Definition: EdmProvDump.cc:47
bool overlapSeeding(const Stub *innerFPGAStub, const L1TStub *innerStub, const Stub *outerFPGAStub, const L1TStub *outerStub)
IMATH_TrackletCalculatorDisk * ITC_F3F4()
Definition: Globals.h:54
IMATH_TrackletCalculatorOverlap * ITC_L1F1()
Definition: Globals.h:58
void addInput(MemoryBase *memory, std::string input) override
std::string const & getName() const
Definition: ProcessBase.h:22
std::string const & getName() const
Definition: MemoryBase.h:19
std::vector< StubPairsMemory * > stubpairs_
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
IMATH_TrackletCalculator * ITC_L1L2()
Definition: Globals.h:48
void addOutputProjection(TrackletProjectionsMemory *&outputProj, MemoryBase *memory)
unsigned int nallstubs(unsigned int layerdisk) const
Definition: Settings.h:107
L1TStub * l1tstub()
Definition: Stub.h:77
TrackletParametersMemory * trackletpars_
unsigned int maxStep(std::string module) const
Definition: Settings.h:116
Log< level::Warning, true > LogPrint
bool usephicritapprox() const
Definition: Settings.h:235
static void verilog_print(const std::vector< VarBase * > &v, std::ofstream &fs)
Definition: imath.h:274
std::vector< std::vector< TrackletProjectionsMemory * > > trackletprojdisks_
bool writeInvTable() const
Definition: Settings.h:197
std::vector< AllStubsMemory * > outerallstubs_
static struct trklet::VarBase::HLS hls
IMATH_TrackletCalculatorDisk * ITC_B3B4()
Definition: Globals.h:56
std::vector< std::vector< TrackletProjectionsMemory * > > trackletprojlayers_
IMATH_TrackletCalculatorDisk * ITC_F1F2()
Definition: Globals.h:53
void execute(unsigned int iSector, double phimin, double phimax)
void writeLUT(std::ofstream &fs) const
Definition: imath.h:1007
IMATH_TrackletCalculator * ITC_L5L6()
Definition: Globals.h:51
IMATH_TrackletCalculator * ITC_L3L4()
Definition: Globals.h:50
bool writeHLS() const
Definition: Settings.h:196
unsigned int layerdisk() const
Definition: Stub.cc:185
void writeInvTable(void(*writeLUT)(const VarInv &, const std::string &))
IMATH_TrackletCalculatorOverlap * ITC_L1B1()
Definition: Globals.h:59
IMATH_TrackletCalculatorOverlap * ITC_L2F1()
Definition: Globals.h:60
std::ofstream & ofstream(std::string fname)
Definition: Globals.cc:44
std::string tablePath() const
Definition: Settings.h:193
tmp
align.sh
Definition: createJobs.py:716
tuple size
Write out results.
bool writeTable() const
Definition: Settings.h:189
bool writeMonitorData(std::string module) const
Definition: Settings.h:109
bool writetrace() const
Definition: Settings.h:183
constexpr int N_LAYER
Definition: Settings.h:21
bool barrelSeeding(const Stub *innerFPGAStub, const L1TStub *innerStub, const Stub *outerFPGAStub, const L1TStub *outerStub)