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 
63 void MatchEngine::execute(unsigned int iSector) {
64  unsigned int countall = 0;
65  unsigned int countpass = 0;
66 
67  //bool print = (iSector == 3 && getName() == "ME_L3PHIC20");
68  bool 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  int stubfinerz = vmstub.finerz().value();
226 
227  bool isPSmodule;
228 
229  if (barrel_) {
230  isPSmodule = layerdisk_ < N_PSLAYER;
231  } else {
232  if (layerdisk_ < N_LAYER + 2) {
233  isPSmodule = ((rzbin & 7) < 3) || ((rzbin & 7) == 3 && stubfinerz <= 3);
234  } else {
235  isPSmodule = ((rzbin & 7) < 3) || ((rzbin & 7) == 3 && stubfinerz <= 2);
236  }
237  }
238 
239  assert(isPSmodule == vmstub.isPSmodule());
240 
241  int nbits = isPSmodule ? N_BENDBITS_PS : N_BENDBITS_2S;
242 
243  int deltaphi = projfinephi - vmstub.finephi().value();
244 
245  constexpr int mindeltaphicut = 3;
246  constexpr int maxdeltaphicut = 5;
247  bool passphi = (std::abs(deltaphi) < mindeltaphicut) || (std::abs(deltaphi) > maxdeltaphicut);
248 
249  unsigned int index = (projrinv << nbits) + vmstub.bend().value();
250  if (!barrel_ && isPSmodule) {
251  index += (1 << (nrinv_ + N_BENDBITS_2S));
252  }
253 
254  //Check if stub z position consistent
255  int idrz = stubfinerz - projfinerzadj;
256  bool passz;
257 
258  if (barrel_) {
259  if (isPSseed) {
260  constexpr int drzcut = 1;
261  passz = std::abs(idrz) <= drzcut;
262  } else {
263  constexpr int drzcut = 5;
264  passz = std::abs(idrz) <= drzcut;
265  }
266  } else {
267  if (isPSmodule) {
268  constexpr int drzcut = 1;
269  passz = std::abs(idrz) <= drzcut;
270  } else {
271  constexpr int drzcut = 3;
272  passz = std::abs(idrz) <= drzcut;
273  }
274  }
275 
276  if (print) {
277  edm::LogVerbatim("Tracklet") << "istep index : " << istep << " " << index << " " << vmstub.bend().value()
278  << " rzbin istubtmp : " << rzbin << " " << istubtmp << " dz " << stubfinerz << " "
279  << projfinerzadj << " dphi: " << deltaphi;
280  }
281 
282  //Check if stub bend and proj rinv consistent
283  if (passz && passphi) {
284  if (luttable_.lookup(index)) {
285  Tracklet* proj = vmprojs_->getTracklet(projindex);
286  std::pair<Tracklet*, int> tmp(proj, vmprojs_->getAllProjIndex(projindex));
287  if (settings_.writeMonitorData("Seeds")) {
288  ofstream fout("seeds.txt", ofstream::app);
289  fout << __FILE__ << ":" << __LINE__ << " " << name_ << " " << proj->getISeed() << endl;
290  fout.close();
291  }
292  candmatches_->addMatch(tmp, vmstub.stub());
293  countpass++;
294  }
295  }
296  }
297  }
298 
299  if (settings_.writeMonitorData("ME")) {
300  globals_->ofstream("matchengine.txt") << getName() << " " << countall << " " << countpass << endl;
301  }
302 }
Log< level::Info, true > LogVerbatim
unsigned int nTracklets() const
void initBendMatch(unsigned int layerdisk)
Definition: TrackletLUT.cc:446
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:904
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:50
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
constexpr unsigned int N_PSLAYER
Definition: Settings.h:23
const FPGAWord & finephi() const
Definition: VMStubME.h:23
bool extended() const
Definition: Settings.h:256
void execute(unsigned int iSector)
Definition: MatchEngine.cc:63
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