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, unsigned int iSector)
19  : ProcessBase(name, settings, global, iSector) {
20  layer_ = 0;
21  disk_ = 0;
22  string subname = name.substr(3, 2);
23  if (subname.substr(0, 1) == "L")
24  layer_ = stoi(subname.substr(1, 1));
25  else if (subname.substr(0, 1) == "D")
26  disk_ = stoi(subname.substr(1, 1));
27  else
28  throw cms::Exception("BadConfig") << __FILE__ << " " << __LINE__ << " " << name << " subname = " << subname << " "
29  << layer_ << " " << disk_;
30 
31  barrel_ = layer_ > 0;
32 
35 
36  if (nvm_ == 32)
37  nvmbits_ = 5;
38  else if (nvm_ == 16)
39  nvmbits_ = 4;
40 
41  if (layer_ > 0) {
42  unsigned int nbits = 3;
43  if (layer_ >= 4)
44  nbits = 4;
45 
46  for (unsigned int irinv = 0; irinv < 32; irinv++) {
47  double rinv = (irinv - 15.5) * (1 << (settings_.nbitsrinv() - 5)) * settings_.krinvpars();
48 
49  double stripPitch =
51  double projbend = bend(settings_.rmean(layer_ - 1), rinv, stripPitch);
52  for (unsigned int ibend = 0; ibend < (unsigned int)(1 << nbits); ibend++) {
53  double stubbend = benddecode(ibend, layer_ <= 3);
54  bool pass = std::abs(stubbend - projbend) < settings_.bendcutme(layer_ - 1);
55  table_.push_back(pass);
56  }
57  }
58 
59  if (settings_.writeTable()) {
60  if (not std::filesystem::exists(settings_.tablePath())) {
61  int fail = system((string("mkdir -p ") + settings_.tablePath()).c_str());
62  if (fail)
63  throw cms::Exception("BadDir") << __FILE__ << " " << __LINE__ << " could not create directory "
64  << settings_.tablePath();
65  }
66 
67  char layer = '0' + layer_;
68  string fname = "METable_L";
69  fname += layer;
70  fname += ".tab";
71 
72  const string full_fname = settings_.tablePath() + fname;
73  ofstream out(full_fname);
74  if (out.fail())
75  throw cms::Exception("BadFile") << __FILE__ << " " << __LINE__ << " could not create file " << full_fname;
76 
77  out << "{" << endl;
78  for (unsigned int i = 0; i < table_.size(); i++) {
79  if (i != 0) {
80  out << "," << endl;
81  }
82  out << table_[i];
83  }
84  out << "};" << endl;
85  out.close();
86  }
87  }
88 
89  if (disk_ > 0) {
90  for (unsigned int iprojbend = 0; iprojbend < 32; iprojbend++) {
91  double projbend = 0.5 * (iprojbend - 15.0);
92  for (unsigned int ibend = 0; ibend < 8; ibend++) {
93  double stubbend = benddecode(ibend, true);
94  bool pass = std::abs(stubbend - projbend) < settings_.bendcutme(disk_ + 5);
95  tablePS_.push_back(pass);
96  }
97  for (unsigned int ibend = 0; ibend < 16; ibend++) {
98  double stubbend = benddecode(ibend, false);
99  bool pass = std::abs(stubbend - projbend) < settings_.bendcutme(disk_ + 5);
100  table2S_.push_back(pass);
101  }
102  }
103  }
104 }
105 
107  if (settings_.writetrace()) {
108  edm::LogVerbatim("Tracklet") << "In " << name_ << " adding output to " << memory->getName() << " to output "
109  << output;
110  }
111  if (output == "matchout") {
112  auto* tmp = dynamic_cast<CandidateMatchMemory*>(memory);
113  assert(tmp != nullptr);
114  candmatches_ = tmp;
115  return;
116  }
117  throw cms::Exception("BadConfig") << __FILE__ << " " << __LINE__ << " could not find output: " << output;
118 }
119 
121  if (settings_.writetrace()) {
122  edm::LogVerbatim("Tracklet") << "In " << name_ << " adding input from " << memory->getName() << " to input "
123  << input;
124  }
125  if (input == "vmstubin") {
126  auto* tmp = dynamic_cast<VMStubsMEMemory*>(memory);
127  assert(tmp != nullptr);
128  vmstubs_ = tmp;
129  return;
130  }
131  if (input == "vmprojin") {
132  auto* tmp = dynamic_cast<VMProjectionsMemory*>(memory);
133  assert(tmp != nullptr);
134  vmprojs_ = tmp;
135  return;
136  }
137  throw cms::Exception("BadConfig") << __FILE__ << " " << __LINE__ << " could not find input: " << input;
138 }
139 
141  unsigned int countall = 0;
142  unsigned int countpass = 0;
143 
144  constexpr unsigned int kNBitsBuffer = 3;
145 
146  int writeindex = 0;
147  int readindex = 0;
148  std::pair<int, int> projbuffer[1 << kNBitsBuffer]; //iproj zbin
149 
150  //The next projection to read, the number of projections and flag if we have more projections to read
151  int iproj = 0;
152  int nproj = vmprojs_->nTracklets();
153  bool moreproj = iproj < nproj;
154 
155  //Projection that is read from the buffer and compared to stubs
156  int rzbin = 0;
157  int projfinerz = 0;
158  int projfinerzadj = 0;
159  unsigned int projfinephi = 0;
160 
161  int projindex = 0;
162  int projrinv = 0;
163  bool isPSseed = false;
164 
165  //Number of stubs for current zbin and the stub being processed on this clock
166  int nstubs = 0;
167  int istub = 0;
168 
169  //Main processing loops starts here
170  for (unsigned int istep = 0; istep < settings_.maxStep("ME"); istep++) {
171  countall++;
172 
173  int writeindexplus = (writeindex + 1) % (1 << kNBitsBuffer);
174  int writeindexplusplus = (writeindex + 2) % (1 << kNBitsBuffer);
175 
176  //Determine if buffer is full - or near full as a projection
177  //can point to two z bins we might fill two slots in the buffer
178  bool bufferfull = (writeindexplus == readindex) || (writeindexplusplus == readindex);
179 
180  //Determin if buffer is empty
181  bool buffernotempty = (writeindex != readindex);
182 
183  //If we have more projections and the buffer is not full we read
184  //next projection and put in buffer if there are stubs in the
185  //memory the projection points to
186 
187  if ((!moreproj) && (!buffernotempty))
188  break;
189 
190  if (moreproj && (!bufferfull)) {
191  Tracklet* proj = vmprojs_->getTracklet(iproj);
192 
193  int iprojtmp = iproj;
194 
195  iproj++;
196  moreproj = iproj < nproj;
197 
198  unsigned int rzfirst = barrel_ ? proj->zbin1projvm(layer_) : proj->rbin1projvm(disk_);
199  unsigned int rzlast = rzfirst;
200  bool second = (barrel_ ? proj->zbin2projvm(layer_) : proj->rbin2projvm(disk_)) == 1;
201  if (second)
202  rzlast += 1;
203 
204  //Check if there are stubs in the memory
205  int nstubfirst = vmstubs_->nStubsBin(rzfirst);
206  int nstublast = vmstubs_->nStubsBin(rzlast);
207  bool savefirst = nstubfirst != 0;
208  bool savelast = second && (nstublast != 0);
209 
210  int writeindextmp = writeindex;
211  int writeindextmpplus = (writeindex + 1) % (1 << kNBitsBuffer);
212 
213  if (savefirst && savelast) {
214  writeindex = writeindexplusplus;
215  } else if (savefirst || savelast) {
216  writeindex = writeindexplus;
217  }
218 
219  if (savefirst) { //TODO for HLS (make code logic simpler)
220  std::pair<int, int> tmp(iprojtmp, rzfirst);
221  projbuffer[writeindextmp] = tmp;
222  }
223  if (savelast) {
224  std::pair<int, int> tmp(iprojtmp, rzlast + 100); //TODO for HLS (fix flagging that this is second bin)
225  if (savefirst) {
226  projbuffer[writeindextmpplus] = tmp;
227  } else {
228  projbuffer[writeindextmp] = tmp;
229  }
230  }
231  }
232 
233  //If the buffer is not empty we have a projection that we need to process.
234 
235  if (buffernotempty) {
236  int istubtmp = istub;
237 
238  //New projection
239  if (istub == 0) {
240  projindex = projbuffer[readindex].first;
241  rzbin = projbuffer[readindex].second;
242  bool second = false;
243  if (rzbin >= 100) {
244  rzbin -= 100;
245  second = true;
246  }
247 
248  Tracklet* proj = vmprojs_->getTracklet(projindex);
249 
250  FPGAWord fpgaphi = barrel_ ? proj->fpgaphiproj(layer_) : proj->fpgaphiprojdisk(disk_);
251  projfinephi = (fpgaphi.value() >> (fpgaphi.nbits() - (nvmbits_ + 3))) & 7;
252 
253  nstubs = vmstubs_->nStubsBin(rzbin);
254 
255  projfinerz = barrel_ ? proj->finezvm(layer_) : proj->finervm(disk_);
256 
257  projrinv =
258  barrel_
259  ? (16 + (((-2) * proj->fpgaphiprojder(layer_).value()) >> (proj->fpgaphiprojder(layer_).nbits() - 4)))
260  : proj->getBendIndex(disk_).value();
261  assert(projrinv >= 0);
262  if (settings_.extended() && projrinv == 32) {
263  if (settings_.debugTracklet()) {
264  edm::LogVerbatim("Tracklet") << "Extended tracking, projrinv:" << projrinv;
265  }
266  projrinv = 31;
267  }
268  assert(projrinv < 32);
269 
270  isPSseed = proj->PSseed() == 1;
271 
272  //Calculate fine z position
273  if (second) {
274  projfinerzadj = projfinerz - 8;
275  } else {
276  projfinerzadj = projfinerz;
277  }
278  if (nstubs == 1) {
279  istub = 0;
280  readindex = (readindex + 1) % (1 << kNBitsBuffer);
281  } else {
282  istub++;
283  }
284  } else {
285  //Check if last stub, if so, go to next buffer entry
286  if (istub + 1 >= nstubs) {
287  istub = 0;
288  readindex = (readindex + 1) % (1 << kNBitsBuffer);
289  } else {
290  istub++;
291  }
292  }
293 
294  //Read vmstub memory and extract data fields
295  const VMStubME& vmstub = vmstubs_->getVMStubMEBin(rzbin, istubtmp);
296 
297  bool isPSmodule = vmstub.isPSmodule();
298 
299  int stubfinerz = vmstub.finerz().value();
300 
301  int nbits = isPSmodule ? 3 : 4;
302 
303  int deltaphi = projfinephi - vmstub.finephi().value();
304 
305  bool passphi = (abs(deltaphi) < 3) || (abs(deltaphi) > 5);
306 
307  unsigned int index = (projrinv << nbits) + vmstub.bend().value();
308 
309  //Check if stub z position consistent
310  int idrz = stubfinerz - projfinerzadj;
311  bool passz;
312 
313  if (barrel_) {
314  if (isPSseed) {
315  passz = idrz >= -2 && idrz <= 2;
316  } else {
317  passz = idrz >= -5 && idrz <= 5;
318  }
319  } else {
320  if (isPSmodule) {
321  passz = idrz >= -1 && idrz <= 1;
322  } else {
323  passz = idrz >= -5 && idrz <= 5;
324  }
325  }
326 
327  //Check if stub bend and proj rinv consistent
328  if (passz && passphi) {
329  if (barrel_ ? table_[index] : (isPSmodule ? tablePS_[index] : table2S_[index])) {
330  Tracklet* proj = vmprojs_->getTracklet(projindex);
331  std::pair<Tracklet*, int> tmp(proj, vmprojs_->getAllProjIndex(projindex));
332  if (settings_.writeMonitorData("Seeds")) {
333  ofstream fout("seeds.txt", ofstream::app);
334  fout << __FILE__ << ":" << __LINE__ << " " << name_ << "_" << iSector_ << " " << proj->getISeed() << endl;
335  fout.close();
336  }
337  candmatches_->addMatch(tmp, vmstub.stub());
338  countpass++;
339  }
340  }
341  }
342  }
343 
344  if (settings_.writeMonitorData("ME")) {
345  globals_->ofstream("matchengine.txt") << getName() << " " << countall << " " << countpass << endl;
346  }
347 }
trklet::MatchEngine::layer_
int layer_
Definition: MatchEngine.h:33
trklet::MatchEngine::vmprojs_
VMProjectionsMemory * vmprojs_
Definition: MatchEngine.h:29
trklet::MatchEngine::table2S_
std::vector< bool > table2S_
Definition: MatchEngine.h:45
Util.h
mps_fire.i
i
Definition: mps_fire.py:428
trklet::Settings::writetrace
bool writetrace() const
Definition: Settings.h:162
input
static const std::string input
Definition: EdmProvDump.cc:48
MessageLogger.h
trklet::VMProjectionsMemory::nTracklets
unsigned int nTracklets() const
Definition: VMProjectionsMemory.h:22
trklet::Settings::krinvpars
double krinvpars() const
Definition: Settings.h:350
trklet::MatchEngine::disk_
int disk_
Definition: MatchEngine.h:34
convertSQLitetoXML_cfg.output
output
Definition: convertSQLitetoXML_cfg.py:72
memory
Definition: HGCRecHitSoA.h:20
trklet::VMStubME::isPSmodule
bool isPSmodule() const
Definition: VMStubME.h:30
CandidateMatchMemory.h
trklet::Settings
Definition: Settings.h:31
trklet::VMStubsMEMemory::getVMStubMEBin
const VMStubME & getVMStubMEBin(unsigned int bin, unsigned int i) const
Definition: VMStubsMEMemory.h:35
trklet::CandidateMatchMemory::addMatch
void addMatch(std::pair< Tracklet *, int > tracklet, const Stub *stub)
Definition: CandidateMatchMemory.cc:17
cms::cuda::assert
assert(be >=bs)
edm::second
U second(std::pair< T, U > const &p)
Definition: ParameterSet.cc:222
trklet::Settings::rmean
double rmean(unsigned int iLayer) const
Definition: Settings.h:143
trklet::VMStubME::bend
const FPGAWord & bend() const
Definition: VMStubME.h:26
trklet::ProcessBase::nbits
unsigned int nbits(unsigned int power)
Definition: ProcessBase.cc:29
trklet::ProcessBase::settings_
Settings const & settings_
Definition: ProcessBase.h:44
trklet::FPGAWord::nbits
int nbits() const
Definition: FPGAWord.h:25
createJobs.tmp
tmp
align.sh
Definition: createJobs.py:716
trklet::Globals
Definition: Globals.h:32
trklet::Settings::writeTable
bool writeTable() const
Definition: Settings.h:168
trklet::Tracklet
Definition: Tracklet.h:28
trklet::MatchEngine::table_
std::vector< bool > table_
Definition: MatchEngine.h:41
trklet::VMStubME::stub
const Stub * stub() const
Definition: VMStubME.h:28
trklet::N_LAYER
constexpr int N_LAYER
Definition: Settings.h:19
trklet::MatchEngine::nvmbits_
int nvmbits_
Definition: MatchEngine.h:38
trklet::FPGAWord
Definition: FPGAWord.h:9
phase1PixelTopology::layer
constexpr std::array< uint8_t, layerIndexSize > layer
Definition: phase1PixelTopology.h:99
trklet::VMProjectionsMemory::getAllProjIndex
int getAllProjIndex(unsigned int i) const
Definition: VMProjectionsMemory.h:25
trklet::Settings::bendcutme
double bendcutme(unsigned int layerdisk) const
Definition: Settings.h:121
VMStubsMEMemory.h
amptDefault_cfi.proj
proj
Definition: amptDefault_cfi.py:13
trklet::MatchEngine::barrel_
bool barrel_
Definition: MatchEngine.h:36
trklet::MemoryBase
Definition: MemoryBase.h:13
trklet::rinv
double rinv(double phi1, double phi2, double r1, double r2)
Definition: Util.h:173
trklet::Settings::extended
bool extended() const
Definition: Settings.h:225
trklet::Settings::nvmme
unsigned int nvmme(unsigned int layerdisk) const
Definition: Settings.h:91
groupFilesInBlocks.fout
fout
Definition: groupFilesInBlocks.py:162
createfilelist.int
int
Definition: createfilelist.py:10
Globals.h
trklet::MatchEngine::tablePS_
std::vector< bool > tablePS_
Definition: MatchEngine.h:44
trklet::MatchEngine::nvm_
int nvm_
Definition: MatchEngine.h:37
trklet::Settings::nbitsrinv
int nbitsrinv() const
Definition: Settings.h:300
trklet
Definition: AllProjectionsMemory.h:9
trklet::FPGAWord::value
int value() const
Definition: FPGAWord.h:24
trklet::Settings::stripPitch
double stripPitch(bool isPSmodule) const
Definition: Settings.h:237
alignmentValidation.fname
string fname
main script
Definition: alignmentValidation.py:959
trklet::Settings::writeMonitorData
bool writeMonitorData(std::string module) const
Definition: Settings.h:96
trklet::MatchEngine::candmatches_
CandidateMatchMemory * candmatches_
Definition: MatchEngine.h:31
std
Definition: JetResolutionObject.h:76
trklet::ProcessBase
Definition: ProcessBase.h:12
trklet::MatchEngine::execute
void execute()
Definition: MatchEngine.cc:140
trklet::Globals::ofstream
std::ofstream & ofstream(std::string fname)
Definition: Globals.cc:44
trklet::MatchEngine::addInput
void addInput(MemoryBase *memory, std::string input) override
Definition: MatchEngine.cc:120
trklet::Settings::tablePath
std::string const & tablePath() const
Definition: Settings.h:170
edm::LogVerbatim
Log< level::Info, true > LogVerbatim
Definition: MessageLogger.h:128
trklet::bend
double bend(double r, double rinv, double stripPitch)
Definition: Util.h:166
MatchEngine.h
Exception
Definition: hltDiff.cc:245
trklet::ProcessBase::getName
std::string const & getName() const
Definition: ProcessBase.h:22
trklet::ProcessBase::name_
std::string name_
Definition: ProcessBase.h:38
MemoryBase.h
trklet::Settings::nallstubs
unsigned int nallstubs(unsigned int layerdisk) const
Definition: Settings.h:94
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
trklet::ProcessBase::iSector_
unsigned int iSector_
Definition: ProcessBase.h:39
trklet::Settings::debugTracklet
bool debugTracklet() const
Definition: Settings.h:161
trklet::Settings::rcrit
double rcrit() const
Definition: Settings.h:257
Exception.h
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
trklet::Settings::maxStep
unsigned int maxStep(std::string module) const
Definition: Settings.h:103
MillePedeFileConverter_cfg.out
out
Definition: MillePedeFileConverter_cfg.py:31
trklet::VMProjectionsMemory::getTracklet
Tracklet * getTracklet(unsigned int i)
Definition: VMProjectionsMemory.h:24
trklet::benddecode
double benddecode(int ibend, bool isPS)
Definition: Util.h:106
trklet::VMStubME::finerz
const FPGAWord & finerz() const
Definition: VMStubME.h:24
cms::Exception
Definition: Exception.h:70
funct::abs
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
trklet::VMStubME::finephi
const FPGAWord & finephi() const
Definition: VMStubME.h:23
trklet::ProcessBase::globals_
Globals * globals_
Definition: ProcessBase.h:45
trklet::VMStubME
Definition: VMStubME.h:15
trklet::MatchEngine::vmstubs_
VMStubsMEMemory * vmstubs_
Definition: MatchEngine.h:28
trklet::VMStubsMEMemory::nStubsBin
unsigned int nStubsBin(unsigned int bin) const
Definition: VMStubsMEMemory.h:30
VMProjectionsMemory.h
Tracklet.h
trklet::MatchEngine::addOutput
void addOutput(MemoryBase *memory, std::string output) override
Definition: MatchEngine.cc:106