CMS 3D CMS Logo

MatchEngine.cc
Go to the documentation of this file.
9 
12 
13 #include <filesystem>
14 
15 using namespace std;
16 using namespace trklet;
17 
18 MatchEngine::MatchEngine(string name, Settings const& settings, Globals* global)
19  : ProcessBase(name, settings, global), luttable_(settings) {
21 
23 
25 
26  nrinv_ = NRINVBITS;
27 }
28 
30  if (settings_.writetrace()) {
31  edm::LogVerbatim("Tracklet") << "In " << name_ << " adding output to " << memory->getName() << " to output "
32  << output;
33  }
34  if (output == "matchout") {
35  auto* tmp = dynamic_cast<CandidateMatchMemory*>(memory);
36  assert(tmp != nullptr);
37  candmatches_ = tmp;
38  return;
39  }
40  throw cms::Exception("BadConfig") << __FILE__ << " " << __LINE__ << " could not find output: " << output;
41 }
42 
44  if (settings_.writetrace()) {
45  edm::LogVerbatim("Tracklet") << "In " << name_ << " adding input from " << memory->getName() << " to input "
46  << input;
47  }
48  if (input == "vmstubin") {
49  auto* tmp = dynamic_cast<VMStubsMEMemory*>(memory);
50  assert(tmp != nullptr);
51  vmstubs_ = tmp;
52  return;
53  }
54  if (input == "vmprojin") {
55  auto* tmp = dynamic_cast<VMProjectionsMemory*>(memory);
56  assert(tmp != nullptr);
57  vmprojs_ = tmp;
58  return;
59  }
60  throw cms::Exception("BadConfig") << __FILE__ << " " << __LINE__ << " could not find input: " << input;
61 }
62 
64  unsigned int countall = 0;
65  unsigned int countpass = 0;
66 
67  bool print = (getName() == "ME_L3PHIC20");
68  print = false;
69 
70  constexpr unsigned int kNBitsBuffer = 3;
71 
72  int writeindex = 0;
73  int readindex = 0;
74  std::pair<int, int> projbuffer[1 << kNBitsBuffer]; //iproj zbin
75 
76  //The next projection to read, the number of projections and flag if we have more projections to read
77  int iproj = 0;
78  int nproj = vmprojs_->nTracklets();
79  bool moreproj = iproj < nproj;
80 
81  //Projection that is read from the buffer and compared to stubs
82  int rzbin = 0;
83  int projfinerz = 0;
84  int projfinerzadj = 0;
85  unsigned int projfinephi = 0;
86 
87  int projindex = 0;
88  int projrinv = 0;
89  bool isPSseed = false;
90 
91  //Number of stubs for current zbin and the stub being processed on this clock
92  int nstubs = 0;
93  int istub = 0;
94 
95  //Main processing loops starts here
96  for (unsigned int istep = 0; istep < settings_.maxStep("ME"); istep++) {
97  countall++;
98 
99  int writeindexplus = (writeindex + 1) % (1 << kNBitsBuffer);
100  int writeindexplusplus = (writeindex + 2) % (1 << kNBitsBuffer);
101 
102  //Determine if buffer is full - or near full as a projection
103  //can point to two z bins we might fill two slots in the buffer
104  bool bufferfull = (writeindexplus == readindex) || (writeindexplusplus == readindex);
105 
106  //Determin if buffer is empty
107  bool buffernotempty = (writeindex != readindex);
108 
109  //If we have more projections and the buffer is not full we read
110  //next projection and put in buffer if there are stubs in the
111  //memory the projection points to
112 
113  if ((!moreproj) && (!buffernotempty))
114  break;
115 
116  if (moreproj && (!bufferfull)) {
117  Tracklet* proj = vmprojs_->getTracklet(iproj);
118 
119  int iprojtmp = iproj;
120 
121  iproj++;
122  moreproj = iproj < nproj;
123 
124  unsigned int rzfirst = proj->proj(layerdisk_).fpgarzbin1projvm().value();
125  unsigned int rzlast = rzfirst;
126 
127  bool second = proj->proj(layerdisk_).fpgarzbin2projvm().value();
128 
129  if (second)
130  rzlast += 1;
131 
132  //Check if there are stubs in the memory
133  int nstubfirst = vmstubs_->nStubsBin(rzfirst);
134  int nstublast = vmstubs_->nStubsBin(rzlast);
135  bool savefirst = nstubfirst != 0;
136  bool savelast = second && (nstublast != 0);
137 
138  int writeindextmp = writeindex;
139  int writeindextmpplus = (writeindex + 1) % (1 << kNBitsBuffer);
140 
141  if (savefirst && savelast) {
142  writeindex = writeindexplusplus;
143  } else if (savefirst || savelast) {
144  writeindex = writeindexplus;
145  }
146 
147  if (savefirst) { //TODO for HLS (make code logic simpler)
148  std::pair<int, int> tmp(iprojtmp, rzfirst);
149  projbuffer[writeindextmp] = tmp;
150  }
151  if (savelast) {
152  std::pair<int, int> tmp(iprojtmp, rzlast + 100); //TODO for HLS (fix flagging that this is second bin)
153  if (savefirst) {
154  projbuffer[writeindextmpplus] = tmp;
155  } else {
156  projbuffer[writeindextmp] = tmp;
157  }
158  }
159  }
160 
161  //If the buffer is not empty we have a projection that we need to process.
162 
163  if (buffernotempty) {
164  int istubtmp = istub;
165 
166  //New projection
167  if (istub == 0) {
168  projindex = projbuffer[readindex].first;
169  rzbin = projbuffer[readindex].second;
170  bool second = false;
171  if (rzbin >= 100) {
172  rzbin -= 100;
173  second = true;
174  }
175 
176  Tracklet* proj = vmprojs_->getTracklet(projindex);
177 
178  FPGAWord fpgafinephi = proj->proj(layerdisk_).fpgafinephivm();
179 
180  projfinephi = fpgafinephi.value();
181 
182  nstubs = vmstubs_->nStubsBin(rzbin);
183 
184  projfinerz = proj->proj(layerdisk_).fpgafinerzvm().value();
185 
186  projrinv = barrel_ ? ((1 << (nrinv_ - 1)) + ((-2 * proj->proj(layerdisk_).fpgaphiprojder().value()) >>
187  (proj->proj(layerdisk_).fpgaphiprojder().nbits() - (nrinv_ - 1))))
188  : proj->proj(layerdisk_).getBendIndex().value();
189  assert(projrinv >= 0);
190  if (settings_.extended() && projrinv == (1 << nrinv_)) {
191  if (settings_.debugTracklet()) {
192  edm::LogVerbatim("Tracklet") << "Extended tracking, projrinv:" << projrinv;
193  }
194  projrinv = (1 << nrinv_) - 1;
195  }
196  assert(projrinv < (1 << nrinv_));
197 
198  isPSseed = proj->PSseed();
199 
200  //Calculate fine z position
201  if (second) {
202  projfinerzadj = projfinerz - (1 << NFINERZBITS);
203  } else {
204  projfinerzadj = projfinerz;
205  }
206  if (nstubs == 1) {
207  istub = 0;
208  readindex = (readindex + 1) % (1 << kNBitsBuffer);
209  } else {
210  istub++;
211  }
212  } else {
213  //Check if last stub, if so, go to next buffer entry
214  if (istub + 1 >= nstubs) {
215  istub = 0;
216  readindex = (readindex + 1) % (1 << kNBitsBuffer);
217  } else {
218  istub++;
219  }
220  }
221 
222  //Read vmstub memory and extract data fields
223  const VMStubME& vmstub = vmstubs_->getVMStubMEBin(rzbin, istubtmp);
224 
225  bool isPSmodule = vmstub.isPSmodule();
226 
227  int stubfinerz = vmstub.finerz().value();
228 
229  int nbits = isPSmodule ? N_BENDBITS_PS : N_BENDBITS_2S;
230 
231  int deltaphi = projfinephi - vmstub.finephi().value();
232 
233  constexpr int mindeltaphicut = 3;
234  constexpr int maxdeltaphicut = 5;
235  bool passphi = (std::abs(deltaphi) < mindeltaphicut) || (std::abs(deltaphi) > maxdeltaphicut);
236 
237  unsigned int index = (projrinv << nbits) + vmstub.bend().value();
238  if (!barrel_ && isPSmodule) {
239  index += (1 << (nrinv_ + N_BENDBITS_2S));
240  }
241 
242  //Check if stub z position consistent
243  int idrz = stubfinerz - projfinerzadj;
244  bool passz;
245 
246  if (barrel_) {
247  if (isPSseed) {
248  constexpr int drzcut = 1;
249  passz = std::abs(idrz) <= drzcut;
250  } else {
251  constexpr int drzcut = 5;
252  passz = std::abs(idrz) <= drzcut;
253  }
254  } else {
255  if (isPSmodule) {
256  constexpr int drzcut = 1;
257  passz = std::abs(idrz) <= drzcut;
258  } else {
259  constexpr int drzcut = 3;
260  passz = std::abs(idrz) <= drzcut;
261  }
262  }
263 
264  if (print) {
265  edm::LogVerbatim("Tracklet") << "istep index : " << istep << " " << index << " " << vmstub.bend().value()
266  << " rzbin istubtmp : " << rzbin << " " << istubtmp << " dz " << stubfinerz << " "
267  << projfinerzadj << " dphi: " << deltaphi;
268  }
269 
270  //Check if stub bend and proj rinv consistent
271  if (passz && passphi) {
272  if (luttable_.lookup(index)) {
273  Tracklet* proj = vmprojs_->getTracklet(projindex);
274  std::pair<Tracklet*, int> tmp(proj, vmprojs_->getAllProjIndex(projindex));
275  if (settings_.writeMonitorData("Seeds")) {
276  ofstream fout("seeds.txt", ofstream::app);
277  fout << __FILE__ << ":" << __LINE__ << " " << name_ << " " << proj->getISeed() << endl;
278  fout.close();
279  }
280  candmatches_->addMatch(tmp, vmstub.stub());
281  countpass++;
282  }
283  }
284  }
285  }
286 
287  if (settings_.writeMonitorData("ME")) {
288  globals_->ofstream("matchengine.txt") << getName() << " " << countall << " " << countpass << endl;
289  }
290 }
Log< level::Info, true > LogVerbatim
unsigned int nTracklets() const
void initBendMatch(unsigned int layerdisk)
Definition: TrackletLUT.cc:443
unsigned int maxStep(std::string module) const
Definition: Settings.h:116
const FPGAWord & bend() const
Definition: VMStubME.h:26
const FPGAWord & finerz() const
Definition: VMStubME.h:24
std::string name_
Definition: ProcessBase.h:38
const Stub * stub() const
Definition: VMStubME.h:28
constexpr unsigned int NRINVBITS
Definition: Settings.h:32
VMStubsMEMemory * vmstubs_
Definition: MatchEngine.h:29
Settings const & settings_
Definition: ProcessBase.h:40
constexpr unsigned int N_BENDBITS_2S
Definition: Settings.h:30
Globals * globals_
Definition: ProcessBase.h:41
unsigned int layerdisk_
Definition: MatchEngine.h:34
int lookup(unsigned int index) const
Definition: TrackletLUT.cc:900
bool writetrace() const
Definition: Settings.h:183
void addMatch(std::pair< Tracklet *, int > tracklet, const Stub *stub)
assert(be >=bs)
bool isPSmodule() const
Definition: VMStubME.h:30
constexpr unsigned int N_BENDBITS_PS
Definition: Settings.h:29
VMProjectionsMemory * vmprojs_
Definition: MatchEngine.h:30
static std::string const input
Definition: EdmProvDump.cc:47
U second(std::pair< T, U > const &p)
unsigned int nbits(unsigned int power)
Definition: ProcessBase.cc:17
CandidateMatchMemory * candmatches_
Definition: MatchEngine.h:32
int getAllProjIndex(unsigned int i) const
const VMStubME & getVMStubMEBin(unsigned int bin, unsigned int i) const
void addOutput(MemoryBase *memory, std::string output) override
Definition: MatchEngine.cc:29
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
int value() const
Definition: FPGAWord.h:24
void addInput(MemoryBase *memory, std::string input) override
Definition: MatchEngine.cc:43
bool writeMonitorData(std::string module) const
Definition: Settings.h:109
unsigned int nrinv_
Definition: MatchEngine.h:38
TrackletLUT luttable_
Definition: MatchEngine.h:41
void initLayerDisk(unsigned int pos, int &layer, int &disk)
Definition: ProcessBase.cc:33
constexpr unsigned int NFINERZBITS
Definition: Settings.h:33
bool debugTracklet() const
Definition: Settings.h:182
const FPGAWord & finephi() const
Definition: VMStubME.h:23
bool extended() const
Definition: Settings.h:248
std::ofstream & ofstream(std::string fname)
Definition: Globals.cc:44
unsigned int nStubsBin(unsigned int bin) const
tmp
align.sh
Definition: createJobs.py:716
Tracklet * getTracklet(unsigned int i)
std::string const & getName() const
Definition: ProcessBase.h:22
constexpr int N_LAYER
Definition: Settings.h:21