CMS 3D CMS Logo

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