CMS 3D CMS Logo

TrackletEventProcessor.cc
Go to the documentation of this file.
11 
13 
14 #include <iomanip>
15 #include <filesystem>
16 
17 using namespace trklet;
18 using namespace std;
19 
21 
23  if (settings_ && settings_->bookHistos()) {
24  histbase_->close();
25  }
26 }
27 
28 void TrackletEventProcessor::init(Settings const& theSettings, const tt::Setup* setup) {
29  settings_ = &theSettings;
30  globals_ = make_unique<Globals>(*settings_);
31 
32  //Verify consistency
33  if (settings_->kphi0pars() != globals_->ITC_L1L2()->phi0_final.K()) {
34  throw cms::Exception("Inconsistency") << "phi0 conversion parameter inconsistency\n";
35  }
36 
37  if (settings_->krinvpars() != globals_->ITC_L1L2()->rinv_final.K()) {
38  throw cms::Exception("Inconsistency") << "ring conversion parameter inconsistency\n";
39  }
40 
41  if (settings_->ktpars() != globals_->ITC_L1L2()->t_final.K()) {
42  throw cms::Exception("Inconsistency") << "t conversion parameter inconsistency\n";
43  }
44 
45  if (settings_->kphider() != globals_->ITC_L1L2()->der_phiL_final.K()) {
46  throw cms::Exception("Inconsistency")
47  << "t conversion parameter inconsistency:" << settings_->kphider() / globals_->ITC_L1L2()->der_phiL_final.K()
48  << "\n";
49  }
50 
51  if (settings_->debugTracklet()) {
52  edm::LogVerbatim("Tracklet") << "========================================================= \n"
53  << "Conversion factors for global coordinates: \n"
54  << "z kz = " << settings_->kz() << "\n"
55  << "r kr = " << settings_->kr() << "\n"
56  << "phi kphi1 = " << settings_->kphi1() << "\n"
57  << "========================================================= \n"
58  << "Conversion factors for track(let) parameters: \n"
59  << "rinv krinvpars = " << settings_->krinvpars() << "\n"
60  << "phi0 kphi0pars = " << settings_->kphi0pars() << "\n"
61  << "d0 kd0pars = " << settings_->kd0pars() << "\n"
62  << "t ktpars = " << settings_->ktpars() << "\n"
63  << "z0 kz0pars = " << settings_->kz0pars() << "\n"
64  << "========================================================= \n"
65  << "phi0bitshift = " << settings_->phi0bitshift() << "\n"
66  << "d0bitshift = ??? \n"
67  << "=========================================================";
68  }
69 
70  if (settings_->bookHistos()) {
71  histbase_ = new HistBase;
72  histbase_->open();
77 
78  globals_->histograms() = histbase_;
79  }
80 
81  sector_ = make_unique<Sector>(*settings_, globals_.get());
82 
83  if (settings_->extended() || settings_->reduced()) {
84  ifstream inmem(settings_->memoryModulesFile().c_str());
85  assert(inmem.good());
86 
87  ifstream inproc(settings_->processingModulesFile().c_str());
88  assert(inproc.good());
89 
90  ifstream inwire(settings_->wiresFile().c_str());
91  assert(inwire.good());
92 
93  configure(inwire, inmem, inproc);
94 
95  } else {
97 
98  //Write configurations to file.
99  if (settings_->writeConfig()) {
100  std::ofstream wires = openfile(settings_->tablePath(), "wires.dat", __FILE__, __LINE__);
101  std::ofstream memorymodules = openfile(settings_->tablePath(), "memorymodules.dat", __FILE__, __LINE__);
102  std::ofstream processingmodules = openfile(settings_->tablePath(), "processingmodules.dat", __FILE__, __LINE__);
103 
104  config.writeAll(wires, memorymodules, processingmodules);
105  }
106 
107  std::stringstream wires;
108  std::stringstream memorymodules;
109  std::stringstream processingmodules;
110 
111  config.writeAll(wires, memorymodules, processingmodules);
112  configure(wires, memorymodules, processingmodules);
113  }
114 }
115 
116 void TrackletEventProcessor::configure(istream& inwire, istream& inmem, istream& inproc) {
117  // get the memory modules
118  if (settings_->debugTracklet()) {
119  edm::LogVerbatim("Tracklet") << "Will read memory modules";
120  }
121 
122  while (inmem.good()) {
123  string memType, memName, size;
124  inmem >> memType >> memName >> size;
125  if (!inmem.good())
126  continue;
127  if (settings_->writetrace()) {
128  edm::LogVerbatim("Tracklet") << "Read memory: " << memType << " " << memName;
129  }
130  sector_->addMem(memType, memName);
131  }
132 
133  // get the processing modules
134  if (settings_->debugTracklet()) {
135  edm::LogVerbatim("Tracklet") << "Will read processing modules";
136  }
137 
138  while (inproc.good()) {
139  string procType, procName;
140  inproc >> procType >> procName;
141  if (!inproc.good())
142  continue;
143  if (settings_->writetrace()) {
144  edm::LogVerbatim("Tracklet") << "Read process: " << procType << " " << procName;
145  }
146  sector_->addProc(procType, procName);
147  }
148 
149  // get the wiring information
150  if (settings_->debugTracklet()) {
151  edm::LogVerbatim("Tracklet") << "Will read wiring information";
152  }
153 
154  while (inwire.good()) {
155  string line;
156  getline(inwire, line);
157  if (!inwire.good())
158  continue;
159  if (settings_->writetrace()) {
160  edm::LogVerbatim("Tracklet") << "Line : " << line;
161  }
162  stringstream ss(line);
163  string mem, tmp1, procin, tmp2, procout;
164  ss >> mem >> tmp1 >> procin;
165  if (procin == "output=>") {
166  procin = "";
167  ss >> procout;
168  } else {
169  ss >> tmp2 >> procout;
170  }
171 
172  sector_->addWire(mem, procin, procout);
173  }
174 }
175 
177  vector<vector<string>>& streamsTrackRaw,
178  vector<vector<StubStreamData>>& streamsStubRaw) {
179  globals_->event() = &ev;
180 
181  tracks_.clear();
182 
183  eventnum_++;
184  bool first = (eventnum_ == 1);
185 
186  for (unsigned int k = 0; k < N_SECTOR; k++) {
187  sector_->setSector(k);
188 
189  cleanTimer_.start();
190  sector_->clean();
191  cleanTimer_.stop();
192 
194 
195  vector<int> layerstubs(N_LAYER + N_DISK, 0);
196  vector<int> layerstubssector(N_SECTOR * (N_LAYER + N_DISK), 0);
197 
198  for (int j = 0; j < ev.nstubs(); j++) {
199  const L1TStub& stub = ev.stub(j);
200  unsigned int isector = stub.region();
201  if (isector != k) {
202  continue;
203  }
204 
205  const string& dtc = stub.DTClink();
206 
207  layerstubs[stub.layerdisk()]++;
208  layerstubssector[isector * (N_LAYER + N_DISK) + stub.layerdisk()]++;
209 
210  sector_->addStub(stub, dtc);
211  }
212 
213  if (settings_->writeMonitorData("StubsLayerSector")) {
214  for (unsigned int index = 0; index < layerstubssector.size(); index++) {
215  int layerdisk = index % (N_LAYER + N_DISK);
216  int sector = index / (N_LAYER + N_DISK);
217  globals_->ofstream("stubslayersector.txt")
218  << layerdisk << " " << sector << " " << layerstubssector[index] << endl;
219  }
220  }
221 
222  if (settings_->writeMonitorData("StubsLayer")) {
223  for (unsigned int layerdisk = 0; layerdisk < layerstubs.size(); layerdisk++) {
224  globals_->ofstream("stubslayer.txt") << layerdisk << " " << layerstubs[layerdisk] << endl;
225  }
226  }
227 
229 
230  // ----------------------------------------------------------------------------------------
231  // Now start the tracklet processing
232 
233  // VM router
235  sector_->executeIR();
236  if (settings_->writeMem() && k == settings_->writememsect()) {
237  sector_->writeDTCStubs(first);
238  sector_->writeIRStubs(first);
239  }
241 
243  sector_->executeVMR();
244  if (settings_->writeMem() && k == settings_->writememsect()) {
245  sector_->writeVMSTE(first);
246  sector_->writeVMSME(first);
247  sector_->writeAS(first);
248  sector_->writeAIS(first);
249  }
251 
252  // tracklet engine
253  TETimer_.start();
254  sector_->executeTE();
255  TETimer_.stop();
256 
257  // tracklet engine displaced
258  TEDTimer_.start();
259  sector_->executeTED();
260  TEDTimer_.stop();
261 
262  // triplet engine
263  TRETimer_.start();
264  sector_->executeTRE();
265  if (settings_->writeMem() && k == settings_->writememsect()) {
266  sector_->writeST(first);
267  }
268  TRETimer_.stop();
269 
270  // tracklet processor (alternative implementation to TE+TC)
271  TPTimer_.start();
272  sector_->executeTP();
273  TPTimer_.stop();
274 
275  if (settings_->writeMem() && k == settings_->writememsect()) {
276  sector_->writeSP(first);
277  }
278 
279  // tracklet calculator
280  TCTimer_.start();
281  sector_->executeTC();
282  TCTimer_.stop();
283 
284  if (settings_->writeMonitorData("HitEff") || settings_->bookHistos()) {
285  int nTP = globals_->event()->nsimtracks();
286  for (int iTP = 0; iTP < nTP; iTP++) {
287  L1SimTrack simtrk = globals_->event()->simtrack(iTP);
288  if (simtrk.pt() < 2.0)
289  continue;
290  if (std::abs(simtrk.vz()) > 15.0)
291  continue;
292  if (hypot(simtrk.vx(), simtrk.vy()) > 0.1)
293  continue;
294  bool electron = (abs(simtrk.type()) == 11);
295  bool muon = (abs(simtrk.type()) == 13);
296  bool pion = (abs(simtrk.type()) == 211);
297  bool kaon = (abs(simtrk.type()) == 321);
298  bool proton = (abs(simtrk.type()) == 2212);
299  if (!(electron || muon || pion || kaon || proton))
300  continue;
301  int nlayers = 0;
302  int ndisks = 0;
303  int simtrackid = simtrk.trackid();
304  unsigned int hitmask = 0;
305  hitmask = ev.layersHit(simtrackid, nlayers, ndisks); // FIX CPU use.
306  if (nlayers + ndisks < 4)
307  continue;
308 
309  if (settings_->writeMonitorData("HitEff")) {
310  static ofstream outhit("hiteff.txt");
311  outhit << simtrk.eta() << " " << (hitmask & 1) << " " << (hitmask & 2) << " " << (hitmask & 4) << " "
312  << (hitmask & 8) << " " << (hitmask & 16) << " " << (hitmask & 32) << " " << (hitmask & 64) << " "
313  << (hitmask & 128) << " " << (hitmask & 256) << " " << (hitmask & 512) << " " << (hitmask & 1024)
314  << endl;
315  }
316 
317  std::unordered_set<int> matchseed;
318  std::unordered_set<int> matchseedtmp = sector_->seedMatch(iTP);
319  matchseed.insert(matchseedtmp.begin(), matchseedtmp.end());
320  if (settings_->bookHistos()) {
321  for (int iseed = 0; iseed < 8; iseed++) {
322  bool eff = matchseed.find(iseed) != matchseed.end();
323  globals_->histograms()->fillSeedEff(iseed, simtrk.eta(), eff);
324  }
325  }
326  }
327  }
328 
329  // tracklet calculator displaced
330  TCDTimer_.start();
331  sector_->executeTCD();
332  TCDTimer_.stop();
333 
334  // tracklet processor displaced
335  TPDTimer_.start();
336  sector_->executeTPD();
337  TPDTimer_.stop();
338 
339  if (settings_->writeMem() && k == settings_->writememsect()) {
340  sector_->writeTPAR(first);
341  sector_->writeTPROJ(first);
342  }
343 
344  // projection router
345  PRTimer_.start();
346  sector_->executePR();
347  if (settings_->writeMem() && k == settings_->writememsect()) {
348  sector_->writeVMPROJ(first);
349  sector_->writeAP(first);
350  }
351  PRTimer_.stop();
352 
353  // match engine
354  METimer_.start();
355  sector_->executeME();
356  if (settings_->writeMem() && k == settings_->writememsect()) {
357  sector_->writeCM(first);
358  }
359  METimer_.stop();
360 
361  // match calculator
362  MCTimer_.start();
363  sector_->executeMC();
364  MCTimer_.stop();
365 
366  // match processor (alternative to ME+MC)
367  MPTimer_.start();
368  sector_->executeMP();
369  MPTimer_.stop();
370 
371  if (settings_->writeMem() && k == settings_->writememsect()) {
372  sector_->writeMC(first);
373  }
374 
375  // fit track
376  FTTimer_.start();
377  sector_->executeFT(streamsTrackRaw, streamsStubRaw);
378  if ((settings_->writeMem() || settings_->writeMonitorData("IFit")) && k == settings_->writememsect()) {
379  sector_->writeTF(first);
380  }
381  FTTimer_.stop();
382 
383  // purge duplicate
384  PDTimer_.start();
385  sector_->executePD(tracks_);
386  if (((settings_->writeMem() || settings_->writeMonitorData("IFit")) && k == settings_->writememsect()) ||
387  settings_->writeMonitorData("CT")) {
388  sector_->writeCT(first);
389  }
390  PDTimer_.stop();
391  }
392 }
393 
395  if (settings_->bookHistos()) {
396  globals_->histograms()->close();
397  }
398 
399  edm::LogVerbatim("Tracklet") << "Process Times called Average time (ms) Total time (s)"
400  << "\n"
401  << "Cleaning " << setw(10) << cleanTimer_.ntimes() << setw(20)
402  << setprecision(3) << cleanTimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
403  << cleanTimer_.tottime() << "\n"
404  << "Add Stubs " << setw(10) << addStubTimer_.ntimes() << setw(20)
405  << setprecision(3) << addStubTimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
406  << addStubTimer_.tottime() << "\n"
407  << "InputRouter " << setw(10) << InputRouterTimer_.ntimes() << setw(20)
408  << setprecision(3) << InputRouterTimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
409  << InputRouterTimer_.tottime() << "\n"
410  << "VMRouter " << setw(10) << VMRouterTimer_.ntimes() << setw(20)
411  << setprecision(3) << VMRouterTimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
412  << VMRouterTimer_.tottime();
413  if (settings_->combined()) {
414  edm::LogVerbatim("Tracklet") << "TrackletProcessor " << setw(10) << TPTimer_.ntimes() << setw(20)
415  << setprecision(3) << TPTimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
416  << TPTimer_.tottime() << "\n"
417  << "MatchProcessor " << setw(10) << MPTimer_.ntimes() << setw(20)
418  << setprecision(3) << MPTimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
419  << MPTimer_.tottime();
420  } else {
421  edm::LogVerbatim("Tracklet") << "TrackletEngine " << setw(10) << TETimer_.ntimes() << setw(20)
422  << setprecision(3) << TETimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
423  << TETimer_.tottime();
424  if (settings_->extended()) {
425  edm::LogVerbatim("Tracklet") << "TrackletEngineDisplaced" << setw(10) << TEDTimer_.ntimes() << setw(20)
426  << setprecision(3) << TEDTimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
427  << TEDTimer_.tottime() << "\n"
428  << "TripletEngine " << setw(10) << TRETimer_.ntimes() << setw(20)
429  << setprecision(3) << TRETimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
430  << TRETimer_.tottime() << "\n"
431  << "TrackletCalculatorDisplaced" << setw(10) << TCDTimer_.ntimes() << setw(20)
432  << setprecision(3) << TCDTimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
433  << TCDTimer_.tottime() << "\n"
434  << TCDTimer_.tottime() << "\n"
435  << "TrackletProcessorDisplaced" << setw(10) << TPDTimer_.ntimes() << setw(20)
436  << setprecision(3) << TPDTimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
437  << TPDTimer_.tottime();
438  }
439  edm::LogVerbatim("Tracklet") << "TrackletCalculator " << setw(10) << TCTimer_.ntimes() << setw(20)
440  << setprecision(3) << TCTimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
441  << TCTimer_.tottime() << "\n"
442  << "ProjectionRouter " << setw(10) << PRTimer_.ntimes() << setw(20)
443  << setprecision(3) << PRTimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
444  << PRTimer_.tottime() << "\n"
445  << "MatchEngine " << setw(10) << METimer_.ntimes() << setw(20)
446  << setprecision(3) << METimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
447  << METimer_.tottime() << "\n"
448  << "MatchCalculator " << setw(10) << MCTimer_.ntimes() << setw(20)
449  << setprecision(3) << MCTimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
450  << MCTimer_.tottime();
451  }
452  edm::LogVerbatim("Tracklet") << "FitTrack " << setw(10) << FTTimer_.ntimes() << setw(20)
453  << setprecision(3) << FTTimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
454  << FTTimer_.tottime() << "\n"
455  << "PurgeDuplicate " << setw(10) << PDTimer_.ntimes() << setw(20)
456  << setprecision(3) << PDTimer_.avgtime() * 1000.0 << setw(20) << setprecision(3)
457  << PDTimer_.tottime();
458 }
Log< level::Info, true > LogVerbatim
unsigned int ntimes() const
Definition: Timer.h:17
double kz() const
Definition: Settings.h:336
constexpr int N_DISK
Definition: Settings.h:26
MPlex< T, D1, D2, N > hypot(const MPlex< T, D1, D2, N > &a, const MPlex< T, D1, D2, N > &b)
Definition: Matriplex.h:417
bool bookHistos() const
Definition: Settings.h:217
double kphi1() const
Definition: Settings.h:333
void init(Settings const &theSettings, const tt::Setup *setup=nullptr)
void start()
Definition: Timer.cc:5
double kz0pars() const
Definition: Settings.h:425
double kphider() const
Definition: Settings.h:428
std::unique_ptr< Sector > sector_
bool combined() const
Definition: Settings.h:268
Class to process and provide run-time constants used by Track Trigger emulators.
Definition: Setup.h:44
virtual void open()
Definition: HistBase.h:22
std::string const & wiresFile() const
Definition: Settings.h:76
dictionary config
Read in AllInOne config in JSON format.
Definition: DMR_cfg.py:21
const std::string & DTClink() const
Definition: L1TStub.h:117
double vy() const
Definition: L1SimTrack.h:30
bool writeMem() const
Definition: Settings.h:198
virtual void bookDiskResidual()
Definition: HistBase.h:26
virtual void bookLayerResidual()
Definition: HistBase.h:25
double kd0pars() const
Definition: Settings.h:426
double ktpars() const
Definition: Settings.h:424
bool writetrace() const
Definition: Settings.h:193
Definition: config.py:1
double krinvpars() const
Definition: Settings.h:419
std::unique_ptr< Globals > globals_
bool reduced() const
Definition: Settings.h:270
assert(be >=bs)
double tottime() const
Definition: Timer.h:20
double kphi0pars() const
Definition: Settings.h:423
std::string tablePath() const
Definition: Settings.h:203
double pt() const
Definition: L1SimTrack.h:26
int type() const
Definition: L1SimTrack.h:25
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
virtual void bookSeedEff()
Definition: HistBase.h:28
int layerdisk() const
Definition: L1TStub.h:119
uint16_t mem[nChs][nEvts]
double avgtime() const
Definition: Timer.h:18
bool writeMonitorData(std::string module) const
Definition: Settings.h:116
std::string const & memoryModulesFile() const
Definition: Settings.h:75
bool debugTracklet() const
Definition: Settings.h:192
int trackid() const
Definition: L1SimTrack.h:24
constexpr unsigned int N_SECTOR
Definition: Settings.h:23
double vz() const
Definition: L1SimTrack.h:31
double kr() const
Definition: Settings.h:338
int iseed
Definition: AMPTWrapper.h:134
std::string const & processingModulesFile() const
Definition: Settings.h:74
void configure(std::istream &inwire, std::istream &inmem, std::istream &inproc)
bool writeConfig() const
Definition: Settings.h:200
double vx() const
Definition: L1SimTrack.h:29
bool extended() const
Definition: Settings.h:266
virtual void close()
Definition: HistBase.h:23
void stop()
Definition: Timer.cc:6
double eta() const
Definition: L1SimTrack.h:27
virtual void bookTrackletParams()
Definition: HistBase.h:27
void event(SLHCEvent &ev, std::vector< std::vector< std::string >> &streamsTrackRaw, std::vector< std::vector< StubStreamData >> &streamsStubRaw)
unsigned int writememsect() const
Definition: Settings.h:210
std::ofstream openfile(const std::string &dir, const std::string &fname, const char *file, int line)
Definition: Util.h:154
int region() const
Definition: L1TStub.h:121
int phi0bitshift() const
Definition: Settings.h:397
constexpr int N_LAYER
Definition: Settings.h:25