Belle II Software  release-08-01-10
TrackSegmentFinder.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 //-----------------------------------------------------------------------------
10 // Description : A class to find Track Segments
11 //-----------------------------------------------------------------------------
12 
13 #define TRG_SHORT_NAMES
14 #define TRGCDC_SHORT_NAMES
15 
16 #include <bitset>
17 #include "TVectorD.h"
18 #include "trg/trg/Debug.h"
19 #include "trg/trg/State.h"
20 #include "trg/trg/Channel.h"
21 #include "trg/trg/Utilities.h"
22 #include "trg/cdc/TRGCDC.h"
23 #include "trg/cdc/Cell.h"
24 #include "trg/cdc/Wire.h"
25 #include "trg/cdc/WireHit.h"
26 #include "trg/cdc/Segment.h"
27 #include "trg/cdc/SegmentHit.h"
28 #include "trg/cdc/TrackSegmentFinder.h"
29 #include "framework/datastore/StoreArray.h"
30 #include "framework/datastore/RelationArray.h"
31 #include <framework/geometry/B2Vector3.h>
32 #include "cdc/dataobjects/CDCSimHit.h"
33 #include "mdst/dataobjects/MCParticle.h"
34 #include "trg/cdc/FrontEnd.h"
35 #include "trg/cdc/Merger.h"
36 
37 using namespace std;
38 
39 namespace Belle2 {
45 //...For debug : yi...
46 // vector<TRGSignalVector * > dbgIn;
47 // vector<TRGSignalVector * > dbgOut;
48 
49  TRGCDCTrackSegmentFinder::TRGCDCTrackSegmentFinder(const TRGCDC& TRGCDC,
50  bool makeRootFile,
51  bool logicLUTFlag)
52  :
53  TRGBoard("", TRGClock("", 0, 0), TRGClock("", 0, 0), TRGClock("", 0, 0),
54  TRGClock("", 0, 0)),
55  _cdc(TRGCDC),
56  m_logicLUTFlag(logicLUTFlag),
57  m_makeRootFile(makeRootFile),
58  _type(), _tosbE(), _tosbT() // 2019/07/31 by ytlai
59  {
60 
61  m_hitPatternInformation = new TClonesArray("TVectorD");
62  m_particleEfficiency = new TClonesArray("TVectorD");
63  m_tsInformation = new TClonesArray("TVectorD");
64  m_nnPatternInformation = new TClonesArray("TVectorD");
65 
66  // For ROOT file
67  TDirectory* currentDir = gDirectory;
68  if (m_makeRootFile) {
69  m_fileTSF = new TFile("TSF.root", "RECREATE");
70 
71  m_treeInputTSF = new TTree("m_treeInputTSF", "InputTSF");
72  m_treeInputTSF->Branch("hitPatternInformation", &m_hitPatternInformation,
73  32000, 0);
74 
75  m_treeOutputTSF = new TTree("m_treeOutputTSF", "OutputTSF");
76  m_treeOutputTSF->Branch("particleEfficiency", &m_particleEfficiency, 32000, 0);
77  m_treeOutputTSF->Branch("tsInformation", &m_tsInformation, 32000, 0);
78 
79  // For neural network TSF. Filled only when TSF and priority is hit.
80  m_treeNNTSF = new TTree("m_treeNNTSF", "NNTSF");
81  m_treeNNTSF->Branch("nnPatternInformation", &m_nnPatternInformation, 32000, 0);
82  }
83  currentDir->cd();
84  }
85 
86 
88  const std::string& name,
89  boardType type,
90  const TRGClock& systemClock,
91  const TRGClock& dataClock,
92  const TRGClock& userClockInput,
93  const TRGClock& userClockOutput,
94  const std::vector<TRGCDCSegment*>& tsSL)
95  : TRGBoard(name, systemClock, dataClock, userClockInput, userClockOutput),
96  _cdc(TRGCDC),
97  m_logicLUTFlag(),
98  m_rootTSFFilename(),
99  m_fileTSF(),
100  _type(type),
101  // _tisb(0),
102  _tosbE(0),
103  _tosbT(0),
104  _tsSL(tsSL)
105  {
106 
107  }
109  {
110 
111  delete m_nnPatternInformation;
112  delete m_tsInformation;
113  delete m_particleEfficiency;
115  delete m_treeInputTSF;
116  if (m_makeRootFile) delete m_fileTSF;
117 
118  }
119 
122  {
123  return _type;
124  }
126  {
127  if (m_makeRootFile) {
128  m_fileTSF->Write();
129  m_fileTSF->Close();
130  }
131  }
132 
133  void TRGCDCTrackSegmentFinder::doit(std::vector<TRGCDCSegment*>& tss,
134  const bool trackSegmentClockSimulation,
135  std::vector<TRGCDCSegmentHit*>& segmentHits,
136  std::vector<TRGCDCSegmentHit*>* segmentHitsSL)
137  {
138  TRGDebug::enterStage("Track Segment Finder");
139 
140  // Saves TS information
141  //saveTSInformation(tss);
142 
143  //...Store TS hits...
144  const unsigned n = tss.size();
145  for (unsigned i = 0; i < n; i++) {
146  TCSegment& s = * tss[i];
147  s.simulate(trackSegmentClockSimulation, m_logicLUTFlag,
148  TRGCDC::getTRGCDC()->getCDCHitCollectionName());
149  if (s.signal().active()) {
150  TCSHit* th = new TCSHit(s);
151  s.hit(th);
152  segmentHits.push_back(th);
153  segmentHitsSL[s.layerId()].push_back(th);
154  }
155  }
156 
157  // Save TSF results
158  saveTSFResults(segmentHitsSL);
159 
160  // Saves NNTS information. Only when ts is hit.
161  //saveNNTSInformation(tss);
162 
163  if (TRGDebug::level() > 1) {
164  cout << TRGDebug::tab() << "TS hit list" << endl;
165  string dumpOption = "trigger";
166  if (TRGDebug::level() > 2)
167  dumpOption = "detail";
168  for (unsigned i = 0; i < _cdc.nSegments(); i++) {
169  const TCSegment& s = _cdc.segment(i);
170  if (s.signal().active())
171  s.dump(dumpOption, TRGDebug::tab(4));
172  }
173 
174  cout << TRGDebug::tab() << "TS hit list (2)" << endl;
175  if (TRGDebug::level() > 2)
176  dumpOption = "detail";
177  for (unsigned i = 0; i < segmentHits.size(); i++) {
178  const TCSHit& s = * segmentHits[i];
179  s.dump(dumpOption, TRGDebug::tab(4));
180  }
181 
182  cout << TRGDebug::tab() << "TS hit list (3)" << endl;
183  if (TRGDebug::level() > 2)
184  dumpOption = "detail";
185  for (unsigned j = 0; j < _cdc.nSuperLayers(); j++) {
186  for (unsigned i = 0; i < segmentHitsSL[j].size(); i++) {
187  const vector<TCSHit*>& s = segmentHitsSL[j];
188  for (unsigned k = 0; k < s.size(); k++)
189  s[k]->dump(dumpOption, TRGDebug::tab(4));
190  }
191  }
192  }
193 
194  TRGDebug::leaveStage("Track Segment Finder");
195  }
196 
197 
198  vector <TRGSignalVector*>
200  {
201 
202  //variables for common
203  const string na = "TSCandidate" + TRGUtilities::itostring(
204  tsid) + " in " + name();
205  TCSegment* tsi = _tsSL[tsid];
206  vector <TRGSignalVector*> result;
207 
208  //variables for EvtTime & Low pT
209  vector<bool> fTimeVect;
210  // int tmpFTime = 0 ;
211 
212  //variables for Tracker & N.N
213  vector <bool> tmpOutBool;
214 
215  TRGSignalVector* resultT = new TRGSignalVector(na, eachInput->clock(), 22);
216  TRGSignalVector* resultE = new TRGSignalVector(na, eachInput->clock(), 10);
217  TRGSignalVector* Hitmap = new TRGSignalVector(na + "HitMap",
218  eachInput->clock(), 0);
219  TRGSignalVector pTime(na + "PriorityTime", eachInput->clock(), 0);
220  TRGSignalVector fTime(na + "FastestTime", eachInput->clock(), 0);
221  for (unsigned i = 0; i < 12; i++) {
222  Hitmap->push_back((*eachInput)[i]);
223  (*Hitmap)[i].widen(16);
224  }
225  for (unsigned i = 0; i < 4; i++) {
226  pTime.push_back((*eachInput)[i + 12]);
227  fTime.push_back((*eachInput)[i + 16]);
228  }
229  for (unsigned i = 0; i < 5; i++) {
230  pTime.push_back((*eachInput)[i + 20]);
231  fTime.push_back((*eachInput)[i + 20]);
232  }
233 
234  vector <int> changeTime = Hitmap->stateChanges();
235 
236  int* LUTValue = new int[changeTime.size()];
237  if (changeTime.size()) {
238  int hitPosition = 0;
239  bool fTimeBool[10];
240  int tmpPTime = 0 ;
241  int tmpCTime = 0 ;
242  int tmpOutInt;
243  fTime.state(changeTime[0]).copy2bool(fTimeBool);
244  fTimeBool[9] = true;
245  fTimeVect.insert(fTimeVect.begin(), fTimeBool, fTimeBool + 10);
246  //tmpFTime = mkint(fTime.state(changeTime[0]));
247  bool eOUT = true;
248  for (unsigned i = 0; i < changeTime.size(); i++) {
249  LUTValue[i] = tsi->LUT()->getValue(mkint(Hitmap->state(changeTime[i])));
250 
252  if ((LUTValue[i]) && (eOUT)) {
253  resultE->set(fTimeVect, changeTime[i]);
254  eOUT = false;
255  }
256 
257  bool priority1rise = (*Hitmap)[6].riseEdge(changeTime[i]);
258  bool priority2rise = ((*Hitmap)[7].riseEdge(changeTime[i]) or
259  (*Hitmap)[8].riseEdge(changeTime[i]));
260 
262  //ready for output
263  if (priority1rise) {
264  hitPosition = 3;
265  tmpPTime = mkint(pTime.state(changeTime[i]));
266  tmpCTime = changeTime[i];
267  } else if (priority2rise) {
268  if (!hitPosition) {
269  tmpPTime = mkint(pTime.state(changeTime[i]));
270  tmpCTime = changeTime[i];
271  if ((*Hitmap)[0].state(changeTime[i])) hitPosition = 2;
272  else hitPosition = 1;
273  }
274  }
275 
276  // output selection
277  if ((hitPosition) && (LUTValue[i]) && ((changeTime[i] - tmpCTime) < 16)) {
278  tmpOutInt = tsid * pow(2, 13) + tmpPTime * pow(2, 4) + LUTValue[i] * pow(2,
279  2) + hitPosition;
280  tmpOutBool = mkbool(tmpOutInt, 22);
281  if (hitPosition == 3) {
282  if (priority1rise) resultT->set(tmpOutBool, changeTime[i]);
283  else {
284  if ((LUTValue[i] == 1) | (LUTValue[i] == 2)) {
285  if (!((LUTValue[i - 1] == 1) |
286  (LUTValue[i - 1] == 2)))
287  resultT->set(tmpOutBool, changeTime[i]);
288  } else {
289  if (!(LUTValue[i - 1])) resultT->set(tmpOutBool, changeTime[i]);
290  }
291  }
292  } else {
293  if (priority2rise) resultT->set(tmpOutBool, changeTime[i]);
294  else {
295  if ((LUTValue[i] == 1) | (LUTValue[i] == 2)) {
296  if (!((LUTValue[i - 1] == 1) |
297  (LUTValue[i - 1] == 2)))
298  resultT->set(tmpOutBool, changeTime[i]);
299  } else {
300  if (!(LUTValue[i])) resultT->set(tmpOutBool, changeTime[i]);
301  }
302  }
303  }
304  }
305 
306  }
307 
308  }
309  result.push_back(resultT);
310  result.push_back(resultE);
311 
312  // cppcheck-suppress uninitdata
313  delete[] LUTValue;
314  delete Hitmap;
315 
316  return result;
317  }
318 
320  TRGCDCTrackSegmentFinder::packerOuterTracker(vector<TRGSignalVector*>& hitList,
321  vector<int>& cList,
322  const unsigned maxHit)
323  {
324  TRGSignalVector* result =
325  new TRGSignalVector("", (* hitList[0]).clock(), 21 * maxHit);
326 
327  for (unsigned ci = 0; ci < cList.size(); ci++) {
328  unsigned cntHit = 0;
329  for (unsigned hi = 0; hi < hitList.size(); hi++) {
330  TRGState s = (* hitList[hi]).state(cList[ci]);
331  if (s.active()) {
332  if (cntHit >= maxHit) continue;
333  for (unsigned j = 0; j < 21; j++) {
334  if ((* hitList[hi])[j].state(cList[ci])) {
335  (* result)[21 * (maxHit - 1) - (cntHit * 21) + j]
336  .set(cList[ci], cList[ci] + 1);
337  }
338  }
339  if (TRGDebug::level()) {
340  TRGState t = hitList[hi]->state(cList[ci]).subset(13, 9);
341  cout << TRGDebug::tab() << " hit found : TSF out local ID="
342  << unsigned(t) << "(" << t << ")" << endl;
343  }
344 
345  ++cntHit;
346  // result->dump("", "??? ");
347  }
348  }
349  }
350 
351  return result;
352  }
353 
355  TRGCDCTrackSegmentFinder::packerOuterEvt(vector<TRGSignalVector*> hitList, vector<int> cList,
356  int maxHit)
357  {
358 
359  //TRGSignalVector * result = new TRGSignalVector("",(*hitList[0]).clock() ,N+9*maxHit);
360  TRGSignalVector* result = new TRGSignalVector("", (*hitList[0]).clock(),
361  hitList.size() + 9 * maxHit);
362 
363  for (unsigned ci = 0; ci < cList.size(); ci++) {
364  int cntHit = 0;
365  for (unsigned hi = 0; hi < hitList.size(); hi++) {
366  if ((*hitList[hi]).state(cList[ci]).active()) {
367  (*result)[9 * maxHit + hi].set(cList[ci], cList[ci] + 1);
368  if (cntHit >= maxHit) continue;
369  for (unsigned j = 0; j < (((*hitList[hi])).size() - 1); j++) {
370  if ((*hitList[hi])[j].state(cList[ci]))
371  (*result)[9 * (maxHit - 1) - (cntHit * 9) + j].set(cList[ci], cList[ci] + 1);
372  }
373  cntHit++;
374  }
375  }
376  }
377  return result;
378  }
379 
380  double
382  {
383  double r = 0;
384  bool* binput = new bool[bitInput.size()];
385  bitInput.copy2bool(binput);
386  for (unsigned i = 0; i < bitInput.size(); i++) {
387  if (binput[i])
388  r += pow(2, i);
389  }
390  delete[] binput;
391  return r;
392  }
393 
394  vector<bool>
396  {
397  vector<bool> boutput;
398  boutput.resize(bitSize);
399  int tmpint = N;
400  for (unsigned i = 0; tmpint; i++) {
401  if (tmpint % 2) boutput[i] = true;
402  else boutput[i] = false;
403  tmpint = tmpint / 2;
404  }
405 
406  return boutput;
407  }
408 
409 //
410 
411  void TRGCDCTrackSegmentFinder::saveTSInformation(std::vector<TRGCDCSegment*>&
412  tss)
413  {
414 
415  TClonesArray& hitPatternInformation = *m_hitPatternInformation;
416  hitPatternInformation.Clear();
417 
418  StoreArray<CDCSimHit> SimHits;
419  if (! SimHits.getEntries()) {
420  //cout << "CDCTRGTSF !!! can not access to CDCSimHits" << endl;
421  return;
422  }
423  StoreArray<MCParticle> mcParticles;
424  if (! mcParticles.getEntries()) {
425  //cout << "CDCTRGTSF !!! can not access to MCParticles" << endl;
426  return;
427  }
428 
429  //cout<<"Save TS information"<<endl;
430  // Loop over all TSs. wireHit has only one wireSimHit. Meaning there is a particle overlap hit bug.
431  const unsigned nTSs = tss.size();
432  unsigned nHitTSs = 0;
433  for (unsigned iTS = 0; iTS < nTSs; iTS++) {
434  const TCSegment& ts = * tss[iTS];
435  const TRGCDCWire* priority;
436  const TRGCDCWire* secondPriorityL;
437  const TRGCDCWire* secondPriorityR;
438  // Find priority wires
439  if (ts.wires().size() == 15) {
440  priority = ts.wires()[0];
441  secondPriorityR = ts.wires()[1];
442  secondPriorityL = ts.wires()[2];
443  } else {
444  priority = ts.wires()[5];
445  secondPriorityR = ts.wires()[6];
446  secondPriorityL = ts.wires()[7];
447  }
448  // Find L/R, phi of priority wires
449  vector<int> priorityLRs(3);
450  vector<float> priorityPhis(3);
451  B2Vector3D posOnTrack;
452  B2Vector3D posOnWire;
453  if (priority->hit() != 0) {
454  int iSimHit = priority->hit()->iCDCSimHit();
455  priorityLRs[0] = SimHits[iSimHit]->getPosFlag();
456  posOnTrack = SimHits[iSimHit]->getPosTrack();
457  posOnWire = SimHits[iSimHit]->getPosWire();
458  priorityPhis[0] = (posOnTrack - posOnWire).Phi() + M_PI_2 -
459  posOnWire.Phi();
460  //cout<<ts.name()<<endl;
461  //cout<<"Track: "<<posOnTrack.x()<<" "<<posOnTrack.y()<<" "<<posOnTrack.z()<<endl;
462  //cout<<"Wire: "<<posOnWire.x()<<" "<<posOnWire.y()<<" "<<posOnWire.z()<<endl;
463  //cout<<"Before Phi: "<<(posOnTrack - posOnWire).Phi()<<" PosOnWirePhi: "<<posOnWire.Phi()<<" After Phi: "<<priorityPhis[0]<<endl;
464  //cout<<"LR: "<<priorityLRs[0]<<endl;
465  } else {
466  priorityLRs[0] = -1;
467  priorityPhis[0] = 9999;
468  }
469  if (secondPriorityR->hit() != 0) {
470  int iSimHit = secondPriorityR->hit()->iCDCSimHit();
471  priorityLRs[1] = SimHits[iSimHit]->getPosFlag();
472  posOnTrack = SimHits[iSimHit]->getPosTrack();
473  posOnWire = SimHits[iSimHit]->getPosWire();
474  priorityPhis[1] = (posOnTrack - posOnWire).Phi() + M_PI_2 -
475  posOnWire.Phi();
476  } else {
477  priorityLRs[1] = -1;
478  priorityPhis[1] = 9999;
479  }
480  if (secondPriorityL->hit() != 0) {
481  int iSimHit = secondPriorityL->hit()->iCDCSimHit();
482  priorityLRs[2] = SimHits[iSimHit]->getPosFlag();
483  posOnTrack = SimHits[iSimHit]->getPosTrack();
484  posOnWire = SimHits[iSimHit]->getPosWire();
485  priorityPhis[2] = (posOnTrack - posOnWire).Phi() + M_PI_2 -
486  posOnWire.Phi();
487  } else {
488  priorityLRs[2] = -1;
489  priorityPhis[2] = 9999;
490  }
491 
492  const unsigned nWires = ts.wires().size();
493  unsigned nHitWires = 0;
494  // Find TSPatternInformation for each particle
495  map<int, unsigned> particleTSPattern;
496  // Loop over wires in TS
497  for (unsigned iWire = 0; iWire < nWires; iWire++) {
498  //...Copy signal from a wire...
499  const TRGSignal& wireSignal = ts.wires()[iWire]->signal();
500  if (wireSignal.active()) ++nHitWires;
501  // Find MC particle of hit wire
502  const TRGCDCWireHit* wireHit = ts.wires()[iWire]->hit();
503  if (wireHit != 0) {
504  MCParticle* particle = SimHits[wireHit->iCDCSimHit()]->getRelatedFrom<MCParticle>();
505  int iMCParticle = (particle) ? particle->getArrayIndex() : mcParticles.getEntries();
506  // If new particle
507  if (particleTSPattern[iMCParticle] == 0) {
508  particleTSPattern[iMCParticle] = 1 << iWire;
509  } else {
510  particleTSPattern[iMCParticle] |= 1 << iWire;
511  }
512  }
513  } // Loop over wires in TS
514 
515  // Print pattern for each particle
516  //for(map<int, unsigned >::const_iterator it = particleTSPattern.begin(); it != particleTSPattern.end(); it++ ) {
517  // bitset<15> printPattern((*it).second);
518  // cout<<ts.name()<<" MC Particle: "<< (*it).first <<" pattern: "<<printPattern<<endl;
519  //}
520 
521  if (nHitWires != 0) {
522  // Ignore TSPatterns that have 2 particles passing TS.
523  if (particleTSPattern.size() == 1) {
524  map<int, unsigned>::const_iterator it = particleTSPattern.begin();
525  bitset<15> printPattern((*it).second);
526  //cout<<ts.name()<<" MC Particle: "<< (*it).first <<" pattern: "<<printPattern<<endl;
527  //cout<<(*it).first<<" "<<ts.superLayerId()<<" "<<printPattern<<endl;
528  // Save TS Pattern information
529  TVectorD tsPatternInformation(9);
530  tsPatternInformation[0] = (*it).first;
531  tsPatternInformation[1] = ts.superLayerId();
532  tsPatternInformation[2] = double((*it).second);
533  tsPatternInformation[3] = priorityLRs[0];
534  tsPatternInformation[4] = priorityPhis[0];
535  tsPatternInformation[5] = priorityLRs[1];
536  tsPatternInformation[6] = priorityPhis[1];
537  tsPatternInformation[7] = priorityLRs[2];
538  tsPatternInformation[8] = priorityPhis[2];
539  new (hitPatternInformation[nHitTSs++]) TVectorD(tsPatternInformation);
540  }
541  //cout<<ts.name()<<" has "<<nHitWires<<" hit wires."<<endl;
542  }
543  } // End of loop over all TSs
544  if (m_makeRootFile) m_treeInputTSF->Fill();
545  //cout<<"End saving TS information"<<endl;
546  } // End of save function
547 
548 
549 
550  void TRGCDCTrackSegmentFinder::saveTSFResults(std::vector<TRGCDCSegmentHit*>*
551  segmentHitsSL)
552  {
553 
554  TClonesArray& particleEfficiency = *m_particleEfficiency;
555  TClonesArray& tsInformation = *m_tsInformation;
556  particleEfficiency.Clear();
557  tsInformation.Clear();
558 
559  int mcInformation = 1;
561  StoreArray<CDCSimHit> SimHits;
562  if (! SimHits) {
563  //cout << "CDCTRGTSF !!! can not access to CDCSimHits" << endl;
564  mcInformation = 0;
565  }
566  StoreArray<MCParticle> mcParticles;
567  if (! mcParticles) {
568  //cout << "CDCTRGTSF !!! can not access to MCParticles" << endl;
569  mcInformation = 0;
570  }
571  if (mcInformation) {
572  RelationArray cdcSimHitRel(mcParticles, SimHits);
573  // Make map for hit to mcParticle
574  map<int, int> simHitsMCParticlesMap;
575  // Loop over all particles
576  for (int iPart = 0; iPart < cdcSimHitRel.getEntries(); iPart++) {
577  // Loop over all hits for particle
578  for (unsigned iHit = 0; iHit < cdcSimHitRel[iPart].getToIndices().size();
579  iHit++) {
580  //cout<<"From: "<<cdcSimHitRel[iPart].getFromIndex()<<" To: "<<cdcSimHitRel[iPart].getToIndex(iHit)<<endl;
581  simHitsMCParticlesMap[cdcSimHitRel[iPart].getToIndex(iHit)] =
582  cdcSimHitRel[iPart].getFromIndex();
583  }
584  }
586  //for(map<int, int >::iterator it = simHitsMCParticlesMap.begin(); it != simHitsMCParticlesMap.end(); it++) {
587  // cout<<"SimHit Index: "<<(*it).first<<" MCParticle Index: "<<(*it).second<<endl;
588  //}
589  // Find efficiency for each particle
590  // Find hit TS SuperLayer for particles
591  // particleNHiTS[iMCParticle] = hitTSSL
592  map<int, unsigned> particleNHitTS;
593  // Loop over all hit TSs
594  for (int iSuperLayer = 0; iSuperLayer < 9; iSuperLayer++) {
595  // map<int, bool> particleHitTS;
596  for (unsigned iTS = 0; iTS < segmentHitsSL[iSuperLayer].size(); iTS++) {
597  const TCSegment& ts = segmentHitsSL[iSuperLayer][iTS]->segment();
598  unsigned nWires = ts.wires().size();
599  for (unsigned iWire = 0; iWire < nWires; iWire++) {
600  const TRGCDCWireHit* wireHit = ts.wires()[iWire]->hit();
601  if (wireHit != 0) {
602  int iMCParticle = simHitsMCParticlesMap[wireHit->iCDCSimHit()];
603  if (particleNHitTS[iMCParticle] == 0) {
604  unsigned hitTSSL;
605  hitTSSL = 1 << iSuperLayer;
606  particleNHitTS[iMCParticle] = hitTSSL;
607  } else
608  particleNHitTS[iMCParticle] |= 1 << iSuperLayer;
609  //cout<<ts.name()<<" "<<ts.wires()[iWire]->name()<<" was hit.";
610  //cout<<" Particle was "<<simHitsMCParticlesMap[wireHit->iCDCSimHit()]<<endl;
611  } // If wire is hit
612  } // End of loop over all wires in TS
613  } // End of loop over all TS in super layer
614  } // End of loop over all hit TSs
616  //for( map<int, unsigned>::const_iterator it = particleNHitTS.begin(); it != particleNHitTS.end(); it++) {
617  // bitset<9> printSuperLayers((*it).second);
618  // cout<<"MC particle: "<<(*it).first<<" HitSuperLayers: "<<printSuperLayers<<" nCount: "<<printSuperLayers.count()<<endl;
619  //}
620 
621  // Find last CDC hit for each MC particle
622  // tsEfficiency[i][tsEfficiency,particle pT,#MCTS]
623  vector<vector<float>> tsEfficiency;
624  // Loop over all particles
625  for (int iPart = 0; iPart < cdcSimHitRel.getEntries(); iPart++) {
626  int lastWireHit = -1;
627  // Loop over all hits for particle
628  for (unsigned iHit = 0; iHit < cdcSimHitRel[iPart].getToIndices().size();
629  iHit++) {
630  int iSimHit = cdcSimHitRel[iPart].getToIndex(iHit);
631  if (SimHits[iSimHit]->getWireID().getICLayer() > lastWireHit) lastWireHit =
632  SimHits[iSimHit]->getWireID().getICLayer();
633  //cout<<"Particle: "<<cdcSimHitRel[iPart].getFromIndex()<<" CDCSimHit: "<<iSimHit<<endl;
634  //cout<<"SuperLayer: "<<SimHits[iSimHit]->getWireID().getISuperLayer()<<" wireLayer: "<<SimHits[iSimHit]->getWireID().getICLayer()<<endl;
635  }
636  //cout<<"iMCParticle: "<<cdcSimHitRel[iPart].getFromIndex()<<" Last wire Hit: "<<lastWireHit<<endl;
637  // Calculate last superlayer
638  int lastSLHit = 0;
639  if (lastWireHit >= 53) lastSLHit = 9;
640  else if (lastWireHit >= 47) lastSLHit = 8;
641  else if (lastWireHit >= 41) lastSLHit = 7;
642  else if (lastWireHit >= 35) lastSLHit = 6;
643  else if (lastWireHit >= 29) lastSLHit = 5;
644  else if (lastWireHit >= 23) lastSLHit = 4;
645  else if (lastWireHit >= 17) lastSLHit = 3;
646  else if (lastWireHit >= 11) lastSLHit = 2;
647  else if (lastWireHit >= 5) lastSLHit = 1;
648  // Get number of hit TS for particle
649  int iMCParticle = cdcSimHitRel[iPart].getFromIndex();
650  bitset<9> hitSuperLayers(particleNHitTS[iMCParticle]);
651  int numberHitSuperLayers = hitSuperLayers.count();
652  //cout<<"iMCParticle: "<< iMCParticle << " # hit TS: "<<numberHitSuperLayers<<" MC # TS: "<<lastSLHit<<endl;
653  float mcPt = mcParticles[iMCParticle]->getMomentum().Rho();
654  float efficiency;
655  if (lastSLHit == 0) efficiency = -1;
656  else efficiency = float(numberHitSuperLayers) / lastSLHit;
657  //cout<<"Efficiency: "<<float(numberHitSuperLayers)/lastSLHit<<" MC pT: "<<mcPt<<endl;
658  vector<float> tempEfficiency;
659  tempEfficiency.resize(3);
660  tempEfficiency[0] = efficiency;
661  tempEfficiency[1] = mcPt;
662  tempEfficiency[2] = lastSLHit;
663  tsEfficiency.push_back(tempEfficiency);
664  } // End of looping over all particles
666  //for(unsigned iEfficiency=0; iEfficiency<tsEfficiency.size(); iEfficiency++) {
667  // cout<<"Efficiency: "<<tsEfficiency[iEfficiency][0]<<" Pt: "<<tsEfficiency[iEfficiency][1]<<" #MCTS: "<<tsEfficiency[iEfficiency][2]<<endl;
668  //}
669 
670  // Save TS efficiency for each particle
671  for (unsigned iEfficiency = 0; iEfficiency < tsEfficiency.size();
672  iEfficiency++) {
673  TVectorD t_particleEfficiency(3);
674  t_particleEfficiency[0] = tsEfficiency[iEfficiency][0];
675  t_particleEfficiency[1] = tsEfficiency[iEfficiency][1];
676  t_particleEfficiency[2] = tsEfficiency[iEfficiency][2];
677  new (particleEfficiency[iEfficiency]) TVectorD(t_particleEfficiency);
678  }
679 
680  } // End of no MC information
681 
682  // Save TS information
683  // [FIXME] Doesn't work when second only priority is hit.
684  // Loop over all hit TSs
685  int iHitTS = 0;
686  for (int iSuperLayer = 0; iSuperLayer < 9; iSuperLayer++) {
687  for (unsigned iTS = 0; iTS < segmentHitsSL[iSuperLayer].size(); iTS++) {
688  const TCSegment& ts = segmentHitsSL[iSuperLayer][iTS]->segment();
689  //unsigned nWires = ts.wires().size();
690  unsigned iWire = 5;
691  if (iSuperLayer == 0) iWire = 0;
692  const TRGCDCWireHit* wireHit = ts.wires()[iWire]->hit();
693  if (wireHit != 0) {
694  //cout<<"[TSF]: "<<ts.name()<<" was hit at ";
695  unsigned nHits = ts.wires()[iWire]->signal().nSignals();
696  for (unsigned iHit = 0; iHit < nHits; iHit++) {
697  if (iHit % 2 == 1) continue;
698  TVectorD tempTSInformation(3);
699  tempTSInformation[0] = iSuperLayer;
700  tempTSInformation[1] = ts.localId();
701  tempTSInformation[2] = ts.wires()[iWire]->signal().stateChanges()[iHit];
702  new (tsInformation[iHitTS++]) TVectorD(tempTSInformation);
703  //cout<<ts.wires()[iWire]->signal().stateChanges()[iHit]<<", ";
704  //iHit++;
705  }
706  //cout<<endl;
707  } // If wire is hit
708  } // End of loop over all TS in super layer
709  } // End of loop over all hit TSs
710 
711  if (m_makeRootFile) m_treeOutputTSF->Fill();
712 
713  } // End of saving TSF results
714 
715 
716 
718  std::vector<TRGCDCSegment*>& tss)
719  {
720 
721  StoreArray<CDCSimHit> SimHits;
722  if (! SimHits) {
723  //cout << "CDCTRGTSF !!! can not access to CDCSimHits" << endl;
724  return;
725  }
726 
727  TClonesArray& nnPatternInformation = *m_nnPatternInformation;
728  nnPatternInformation.Clear();
729 
730  // Save Timing information in ROOT file. Fill for each TS.
731  // Loop over all TSs. wireHit has only one wireSimHit. Meaning there is a particle overlap hit bug.
732  const unsigned nTSs = tss.size();
733  unsigned indexSaving = 0;
734  for (unsigned iTS = 0; iTS < nTSs; iTS++) {
735  // If TS is hit
736  const TCSegment& ts = * tss[iTS];
737  const TCSHit* tsHit = ts.hit();
738  if (tsHit) {
739  const TRGCDCWire* priority;
740  if (ts.wires().size() == 15) priority = ts.wires()[0];
741  else priority = ts.wires()[5];
742  // If priority wire is hit
743  if (priority->hit()) {
744 
745  // Calculate timeWires
746  // Fill wire timing. Not hit = 9999. Unit is ns.
747  vector<float> wireTime;
748  if (ts.superLayerId() == 0)
749  wireTime.resize(15);
750  else
751  wireTime.resize(11);
752  // Loop over all wires
753  //cout<<ts.name();
754  const unsigned nWires = ts.wires().size();
755  for (unsigned iWire = 0; iWire < nWires; iWire++) {
756  const TRGCDCWire* wire = ts.wires()[iWire];
757  const TRGCDCWireHit* wireHit = ts.wires()[iWire]->hit();
758  // If wire is hit
759  if (wireHit) {
760  // Only check first change. This could become a bug.
761  wireTime[iWire] = wire->signal().stateChanges()[0];
762  //cout<<ts.wires()[iWire]->name()<<" Clock: "<< ts.wires()[iWire]->signal().clock().frequency()<<" Drift lenght: "<<wireHit->drift()<<" Drift time: "<<ts.wires()[iWire]->signal().stateChanges()[0]<<endl;
763  } else
764  wireTime[iWire] = 9999;
765  //cout<<" "<<wire->name();
766  } // End loop over all wires
767  //cout<<endl;
769  //for(unsigned iWire = 0; iWire < wireTime.size(); iWire++) {
770  // cout<<"Wire: "<<iWire<<" Time: "<<wireTime[iWire]<<endl;
771  //}
772  // Get additional wire information for 6th layer
773  //cout<<" JB: "<<ts.wires().back()->layerId()<<" "<<ts.wires().back()->localId()<<endl;
774  int lastLayer = ts.wires().back()->layerId();
775  int lastWire = ts.wires().back()->localId();
776  int nWiresLayer = _cdc.layer(lastLayer + 1)->nCells();
777  if (nWires == 15) {
778  for (unsigned iWire = 0; iWire < 6; iWire++) {
779  int wireIndex = lastWire - 4 + iWire;
780  if (wireIndex < 0) wireIndex += nWiresLayer;
781  if (wireIndex >= nWiresLayer) wireIndex -= nWiresLayer;
782  //cout<<"Call: "<<(*_cdc.layer(lastLayer+1))[wireIndex]->localId()<<endl;
783  const TRGCDCCell* wire = (*_cdc.layer(lastLayer + 1))[wireIndex];
784  const TRGCDCCellHit* wireHit = wire->hit();
785  // If wire is hit
786  if (wireHit) {
787  // Only check first change. This could become a bug.
788  wireTime.push_back(wire->signal().stateChanges()[0]);
789  //cout<<ts.wires()[iWire]->name()<<" Clock: "<< ts.wires()[iWire]->signal().clock().frequency()<<" Drift lenght: "<<wireHit->drift()<<" Drift time: "<<ts.wires()[iWire]->signal().stateChanges()[0]<<endl;
790  } else
791  wireTime.push_back(9999);
792  } // Loop over all extra wires
793  } else {
794  for (unsigned iWire = 0; iWire < 5; iWire++) {
795  int wireIndex = lastWire - 3 + iWire;
796  if (wireIndex < 0) wireIndex += nWiresLayer;
797  if (wireIndex >= nWiresLayer) wireIndex -= nWiresLayer;
798  //cout<<"Call: "<<(*_cdc.layer(lastLayer+1))[wireIndex]->localId()<<endl;
799  const TRGCDCCell* wire = (*_cdc.layer(lastLayer + 1))[wireIndex];
800  const TRGCDCCellHit* wireHit = wire->hit();
801  // If wire is hit
802  if (wireHit) {
803  // Only check first change. This could become a bug.
804  wireTime.push_back(wire->signal().stateChanges()[0]);
805  //cout<<ts.wires()[iWire]->name()<<" Clock: "<< ts.wires()[iWire]->signal().clock().frequency()<<" Drift lenght: "<<wireHit->drift()<<" Drift time: "<<ts.wires()[iWire]->signal().stateChanges()[0]<<endl;
806  } else
807  wireTime.push_back(9999);
808  } // Loop over all extra wires
809  }
810  //ts.wires()[14]->layerId(), localId()
811  //_cdc._layers[0][localId]->signal().stateChanges()[0]
812 
813 
815  //float fastestTime = 9999;
816  //for(unsigned iWire=0; iWire<wireTime.size();iWire++) {
817  // if (fastestTime > wireTime[iWire]) fastestTime = wireTime[iWire];
818  //}
820  //for(unsigned iWire=0; iWire<wireTime.size();iWire++) {
821  // if(wireTime[iWire] != 9999) wireTime[iWire] -= fastestTime;
822  //}
823 
825  //for(unsigned iWire = 0; iWire < wireTime.size(); iWire++) {
826  // cout<<"Wire: "<<iWire<<" Time: "<<wireTime[iWire]<<endl;
827  //}
828  //cout<<ts.name()<<" is found and priority wire is hit"<<endl;
829  // Calculate mc result
830  float mcLRDriftTime = priority->signal().stateChanges()[0];
831  if (priority->hit()->mcLR()) mcLRDriftTime *= -1;
832  //cout<<ts.name()<<" LRDriftTime: "<<mcLRDriftTime<<endl;
833 
834  // Save timing information in Root
835  TVectorD t_nnPatternInformation;
836  if (ts.superLayerId() == 0) {
837  //t_nnPatternInformation.ResizeTo(17);
838  t_nnPatternInformation.ResizeTo(23);
839  t_nnPatternInformation[0] = ts.superLayerId();
840  t_nnPatternInformation[1] = mcLRDriftTime;
841  //for(unsigned iWire=0; iWire<15; iWire++){
842  for (unsigned iWire = 0; iWire < 21; iWire++)
843  t_nnPatternInformation[iWire + 2] = wireTime[iWire];
844  new (nnPatternInformation[indexSaving++]) TVectorD(t_nnPatternInformation);
845  } else {
846  //t_nnPatternInformation.ResizeTo(13);
847  t_nnPatternInformation.ResizeTo(17);
848  t_nnPatternInformation[0] = ts.superLayerId();
849  t_nnPatternInformation[1] = mcLRDriftTime;
850  //for(unsigned iWire=0; iWire<11; iWire++){
851  for (unsigned iWire = 0; iWire < 15; iWire++)
852  t_nnPatternInformation[iWire + 2] = wireTime[iWire];
853  new (nnPatternInformation[indexSaving++]) TVectorD(t_nnPatternInformation);
854  }
855 
856  } // End of if priority cell is hit
857  } // End of if TS is hit
858  } // End loop of all TSs
859 
860  if (m_makeRootFile) m_treeNNTSF->Fill();
861 
862  } // End of save function
863 
864  void
866  {
867  std::vector<const TRGCDCMerger*>::push_back(a);
868  }
869 
870 
871  void
873  {
874 
875  const string sn = "TSF::simulateOuter : " + name();
877 
878  //...Loop over mergers to create a super layer hit map...
879  for (unsigned m = 0; m < nInput(); m++) {
880  TRGSignalBundle* b = input(m)->signal();
881 
882  for (unsigned i = 0; i < 16; i++) {
883  _secMap.push_back(& ((* b)[0][0][208 + i]));
884  for (unsigned j = 0; j < 5; j++)
885  _hitMap[j].push_back(& ((* b)[0][0][j * 16 + i]));
886  for (unsigned j = 0; j < 4; j++)
887  _priMap.push_back(& ((* b)[0][0][80 + i * 4 + j]));
888  for (unsigned j = 0; j < 4; j++)
889  _fasMap.push_back(& ((* b)[0][0][144 + i * 4 + j]));
890  }
891 
892  for (unsigned i = 0; i < 4; i++)
893  _edg0Map.push_back(& ((* b)[0][0][224 + i]));
894  for (unsigned i = 0; i < 4; i++)
895  _edg1Map.push_back(& ((* b)[0][0][224 + 4 + i]));
896  for (unsigned i = 0; i < 4; i++)
897  _edg2Map.push_back(& ((* b)[0][0][224 + 8 + i]));
898  }
899 
900  //...Storage preparation...
901  const unsigned nTSF = nInput() * 16;
902  vector<TRGSignalVector*> trker[4];
903  vector<int> tsfStateChanges;
904 
905  //...Form a TSF...
906  for (unsigned t = 0; t < nTSF; t++) {
907 
908  const string n = name() + "-" + TRGUtilities::itostring(t);
910  _tsfIn.push_back(s);
911 
912  s->push_back(* _secMap[t]);
913 
914  if (t == 0) {
915  s->push_back(* (_hitMap[0][nTSF - 1]));
916  s->push_back(* (_hitMap[0][0]));
917  s->push_back(* (_hitMap[0][1]));
918  s->push_back(* (_hitMap[1][nTSF - 1]));
919  s->push_back(* (_hitMap[1][0]));
920  s->push_back(* (_hitMap[2][0]));
921  s->push_back(* (_hitMap[3][nTSF - 1]));
922  s->push_back(* (_hitMap[3][0]));
923  s->push_back(* (_hitMap[4][nTSF - 1]));
924  s->push_back(* (_hitMap[4][0]));
925  s->push_back(* (_hitMap[4][1]));
926  } else if (t == (nTSF - 1)) {
927  s->push_back(* (_hitMap[0][nTSF - 2]));
928  s->push_back(* (_hitMap[0][nTSF - 1]));
929  s->push_back(* (_hitMap[0][0]));
930  s->push_back(* (_hitMap[1][nTSF - 2]));
931  s->push_back(* (_hitMap[1][nTSF - 1]));
932  s->push_back(* (_hitMap[2][nTSF - 1]));
933  s->push_back(* (_hitMap[3][nTSF - 2]));
934  s->push_back(* (_hitMap[3][nTSF - 1]));
935 
936  s->push_back(* (_hitMap[4][nTSF - 2]));
937  s->push_back(* (_hitMap[4][nTSF - 1]));
938  s->push_back(* (_hitMap[4][0]));
939  } else {
940  s->push_back(* (_hitMap[0][t - 1]));
941  s->push_back(* (_hitMap[0][t]));
942  s->push_back(* (_hitMap[0][t + 1]));
943  s->push_back(* (_hitMap[1][t - 1]));
944  s->push_back(* (_hitMap[1][t]));
945  s->push_back(* (_hitMap[2][t]));
946  s->push_back(* (_hitMap[3][t - 1]));
947  s->push_back(* (_hitMap[3][t]));
948  s->push_back(* (_hitMap[4][t - 1]));
949  s->push_back(* (_hitMap[4][t]));
950  s->push_back(* (_hitMap[4][t + 1]));
951  }
952 
953  //...Priority timing...
954  s->push_back(* _priMap[t * 4 + 0]);
955  s->push_back(* _priMap[t * 4 + 1]);
956  s->push_back(* _priMap[t * 4 + 2]);
957  s->push_back(* _priMap[t * 4 + 3]);
958 
959  //...Priority timing...
960  priorityTiming(t, nTSF, * s, (* s)[6], (* s)[7], (* s)[8]);
961 
962  //...Fastest timing...
963  fastestTimingOuter(t, nTSF, * s);
964 
965  //...Clock counter is omitted...
966 
967  //...Simulate TSF...
968  vector<TRGSignalVector*> result = simulateOuter(s, t);
969  _toBeDeleted.push_back(result[1]); // Event timing omitted now
970 
971  TRGSignalVector* forTracker0 = result[0];
972  TRGSignalVector* forTracker1 = new TRGSignalVector(* forTracker0);
973 
974  _tsfOut.push_back(forTracker0);
975  _tsfOut.push_back(forTracker1);
976 
977 
978  //...State change list...
979  vector<int> sc = forTracker0->stateChanges();
980  for (unsigned i = 0; i < sc.size(); i++) {
981  bool skip = false;
982  for (unsigned j = 0; j < tsfStateChanges.size(); j++) {
983  if (tsfStateChanges[j] == sc[i]) {
984  skip = true;
985  break;
986  }
987  }
988  if (! skip) tsfStateChanges.push_back(sc[i]);
989  }
990 
991  //...Store it for each tracker division...
992  const unsigned pos = t / (nTSF / 4);
993  if (pos == 0) {
994  addID(* forTracker0, t + (nTSF / 4));
995  addID(* forTracker1, t);
996  trker[3].push_back(forTracker0);
997  trker[0].push_back(forTracker1);
998  } else if (pos == 1) {
999  addID(* forTracker0, t);
1000  addID(* forTracker1, t - 1 * (nTSF / 4));
1001  trker[0].push_back(forTracker0);
1002  trker[1].push_back(forTracker1);
1003  } else if (pos == 2) {
1004  addID(* forTracker0, t - 1 * (nTSF / 4));
1005  addID(* forTracker1, t - 2 * (nTSF / 4));
1006  trker[1].push_back(forTracker0);
1007  trker[2].push_back(forTracker1);
1008  } else {
1009  addID(* forTracker0, t - 2 * (nTSF / 4));
1010  addID(* forTracker1, t - 3 * (nTSF / 4));
1011  trker[2].push_back(forTracker0);
1012  trker[3].push_back(forTracker1);
1013  }
1014 
1015  if (TRGDebug::level())
1016  if (forTracker0->active())
1017  cout << TRGDebug::tab() << "hit found : TSF out="
1018  << t << endl;
1019  }
1020 
1021  //...Sort state changes...
1022  std::sort(tsfStateChanges.begin(), tsfStateChanges.end());
1023 
1024  //...Output for 2D...
1025  for (unsigned i = 0; i < 4; i++) {
1026  string n = name() + "-trker" + TRGUtilities::itostring(i);
1027  TRGSignalVector* tOut = packerOuterTracker(trker[i],
1028  tsfStateChanges,
1029  20);
1030  tOut->name(n);
1031  TRGSignalBundle* b = new TRGSignalBundle(n, clockData());
1032  b->push_back(tOut);
1033  output(i)->signal(b);
1034  _toBeDeleted.push_back(tOut);
1035 
1036  if (TRGCDC::getTRGCDC()->firmwareSimulationMode() & 0x4)
1037  b->dumpCOE("",
1038  TRGCDC::getTRGCDC()->firmwareSimulationStartDataClock(),
1039  TRGCDC::getTRGCDC()->firmwareSimulationStopDataClock());
1040 
1041  // b->dump();
1042  }
1043 
1045  }
1046 
1047  vector <TRGSignalVector*>
1049  {
1050 
1051  //variables for common
1052  const string na = "TSCandidate" + TRGUtilities::itostring(tsid) + " in " +
1053  name();
1054  TCSegment* tsi = _tsSL[tsid];
1055  vector <TRGSignalVector*> result;
1056 
1057  //variables for EvtTime & Low pT
1058  vector<bool> fTimeVect;
1059  // int tmpFTime = 0 ;
1060 
1061  //variables for Tracker & N.N
1062  vector <bool> tmpOutBool;
1063 
1064  //iwTRGSignalVector* resultT = new TRGSignalVector(na, in->clock(),22);
1065  TRGSignalVector* resultT = new TRGSignalVector(na, in->clock(), 13);
1066  TRGSignalVector* resultE = new TRGSignalVector(na, in->clock(), 10);
1067  TRGSignalVector* Hitmap = new TRGSignalVector(na + "HitMap", in->clock(), 0);
1068  TRGSignalVector pTime(na + "PriorityTime", in->clock(), 0);
1069  TRGSignalVector fTime(na + "FastestTime", in->clock(), 0);
1070  for (unsigned i = 0; i < 12; i++) {
1071  Hitmap->push_back((*in)[i]);
1072  (*Hitmap)[i].widen(16);
1073  }
1074  for (unsigned i = 0; i < 4; i++) {
1075  pTime.push_back((*in)[i + 12]);
1076  fTime.push_back((*in)[i + 16]);
1077  }
1078 
1079  //...Clock counter...
1080  const TRGSignalVector& cc = in->clock().clockCounter();
1081  for (unsigned i = 0; i < 5; i++) {
1082  pTime.push_back(cc[i]);
1083  fTime.push_back(cc[i]);
1084  }
1085 
1086  vector <int> changeTime = Hitmap->stateChanges();
1087 
1088  int* LUTValue = new int[changeTime.size()];
1089  if (changeTime.size()) {
1090  int hitPosition = 0;
1091  bool fTimeBool[10];
1092  int tmpPTime = 0 ;
1093  int tmpCTime = 0 ;
1094  int tmpOutInt;
1095  fTime.state(changeTime[0]).copy2bool(fTimeBool);
1096  fTimeBool[9] = true;
1097  fTimeVect.insert(fTimeVect.begin(), fTimeBool, fTimeBool + 10);
1098  //tmpFTime = mkint(fTime.state(changeTime[0]));
1099  bool eOUT = true;
1100  for (unsigned i = 0; i < changeTime.size(); i++) {
1101  LUTValue[i] = tsi->LUT()->getValue(mkint(Hitmap->state(changeTime[i])));
1102 
1104  if ((LUTValue[i]) && (eOUT)) {
1105  resultE->set(fTimeVect, changeTime[i]);
1106  eOUT = false;
1107  }
1108 
1109  bool priority1rise = (*Hitmap)[6].riseEdge(changeTime[i]);
1110  bool priority2rise = ((*Hitmap)[7].riseEdge(changeTime[i]) or
1111  (*Hitmap)[8].riseEdge(changeTime[i]));
1112 
1114  //ready for output
1115  if (priority1rise) {
1116  hitPosition = 3;
1117  tmpPTime = mkint(pTime.state(changeTime[i]));
1118  tmpCTime = changeTime[i];
1119  } else if (priority2rise) {
1120  if (!hitPosition) {
1121  tmpPTime = mkint(pTime.state(changeTime[i]));
1122  tmpCTime = changeTime[i];
1123  if ((*Hitmap)[0].state(changeTime[i])) hitPosition = 2;
1124  else hitPosition = 1;
1125  }
1126  }
1127 
1128  // output selection
1129  if ((hitPosition) && (LUTValue[i]) && ((changeTime[i] - tmpCTime) < 16)) {
1130  //iw tmpOutInt = tsid * pow(2, 13) + tmpPTime * pow(2, 4) +
1131  //iw LUTValue[i] * pow(2,2) + hitPosition;
1132  tmpOutInt = tmpPTime * pow(2, 4) +
1133  LUTValue[i] * pow(2, 2) + hitPosition;
1134  tmpOutBool = mkbool(tmpOutInt, 13); // ID removed : iw
1135  if (hitPosition == 3) {
1136  if (priority1rise) resultT->set(tmpOutBool, changeTime[i]);
1137  else {
1138  if ((LUTValue[i] == 1) | (LUTValue[i] == 2)) {
1139  if (!((LUTValue[i - 1] == 1) |
1140  (LUTValue[i - 1] == 2)))
1141  resultT->set(tmpOutBool, changeTime[i]);
1142  } else {
1143  if (!(LUTValue[i - 1])) resultT->set(tmpOutBool, changeTime[i]);
1144  }
1145  }
1146  } else {
1147  if (priority2rise) resultT->set(tmpOutBool, changeTime[i]);
1148  else {
1149  if ((LUTValue[i] == 1) | (LUTValue[i] == 2)) {
1150  if (!((LUTValue[i - 1] == 1) |
1151  (LUTValue[i - 1] == 2)))
1152  resultT->set(tmpOutBool, changeTime[i]);
1153  } else {
1154  if (!(LUTValue[i])) resultT->set(tmpOutBool, changeTime[i]);
1155  }
1156  }
1157  }
1158  }
1159 
1160  }
1161 
1162  }
1163  result.push_back(resultT);
1164  result.push_back(resultE);
1165 
1166  // cppcheck-suppress uninitdata
1167  delete[] LUTValue;
1168  delete Hitmap;
1169 
1170  return result;
1171  }
1172 
1173  void
1175  const unsigned nTSF,
1176  TRGSignalVector& s,
1177  const TRGSignal& center,
1178  const TRGSignal& right,
1179  const TRGSignal& left) const
1180  {
1181 
1182  unsigned rem = t % 16;
1183 
1184  if (rem != 0) {
1185  s.push_back(* _priMap[t * 4 + 0]);
1186  s.push_back(* _priMap[t * 4 + 1]);
1187  s.push_back(* _priMap[t * 4 + 2]);
1188  s.push_back(* _priMap[t * 4 + 3]);
1189  return;
1190  }
1191 
1192  //...Prepare signal vector for timing cells...
1193  TRGSignalVector tc("timing cell hit", clockData());
1194  tc.push_back(center);
1195  tc.push_back(right);
1196  tc.push_back(left);
1197 
1198  //...No timing hit case...
1199  if (! tc.active()) {
1200  s.push_back(* _priMap[t * 4 + 0]);
1201  s.push_back(* _priMap[t * 4 + 1]);
1202  s.push_back(* _priMap[t * 4 + 2]);
1203  s.push_back(* _priMap[t * 4 + 3]);
1204  return;
1205  }
1206 
1207  const string sn = "TSF priority timing outer";
1209 
1210  //...Prepare signal vector for each timing cells...
1211  TRGSignalVector t0("t0", clockData()); // center
1212  t0.push_back(* _priMap[t * 4 + 0]);
1213  t0.push_back(* _priMap[t * 4 + 1]);
1214  t0.push_back(* _priMap[t * 4 + 2]);
1215  t0.push_back(* _priMap[t * 4 + 3]);
1216 
1217  TRGSignalVector t1("t1", clockData()); // left
1218  unsigned p = t / 16;
1219  if (t == 0)
1220  p = nTSF / 16 - 1;
1221  else p = p - 1;
1222  t1.push_back(* _edg0Map[p * 4 + 0]);
1223  t1.push_back(* _edg0Map[p * 4 + 1]);
1224  t1.push_back(* _edg0Map[p * 4 + 2]);
1225  t1.push_back(* _edg0Map[p * 4 + 3]);
1226 
1227  //...Loop over state changes...
1228  vector<TRGState*> outputTimingStates;
1229  vector<int> clkStates;
1230  vector<int> sc = tc.stateChanges();
1231  vector<bool> sb;
1232  for (unsigned i = 0; i < sc.size(); i++) {
1233  int clk = sc[i];
1234  TRGState stt = tc.state(clk);
1235 
1236  //...Priority cell hit case...
1237  bool sel = true; // true=t0, false=t1
1238  if (stt[0])
1239  sel = true;
1240  else if (stt[1] && (! stt[2]))
1241  sel = false;
1242  else if ((! stt[1]) && stt[2])
1243  sel = true;
1244  else { // (stt[1] && stt[2])
1245  if (unsigned(t0.state(clk)) < unsigned(t1.state(clk)))
1246  sel = true;
1247  else
1248  sel = false;
1249  }
1250 
1251  if (sel) {
1252  outputTimingStates.push_back(new TRGState(t0.state(clk)));
1253  sb.push_back(false);
1254  } else {
1255  outputTimingStates.push_back(new TRGState(t1.state(clk)));
1256  sb.push_back(true);
1257  }
1258  clkStates.push_back(clk);
1259  }
1260 
1261  //...Create signals...
1262  const unsigned n = outputTimingStates.size();
1263  TRGSignalVector st("pri.timing", clockData(), 4);
1264  for (unsigned i = 0; i < n; i++) {
1265  if (sb[i])
1266  s[0].unset(clkStates[i], clkStates[i] + 1);
1267  st.set(* outputTimingStates[i], clkStates[i]);
1268  delete outputTimingStates[i];
1269  }
1270 
1271  //...Store signals...
1272  s.push_back(st[0]);
1273  s.push_back(st[1]);
1274  s.push_back(st[2]);
1275  s.push_back(st[3]);
1276 
1277  if (TRGDebug::level()) {
1278  tc.dump("", TRGDebug::tab() + " ");
1279  t0.dump("", TRGDebug::tab() + " ");
1280  t1.dump("", TRGDebug::tab() + " ");
1281  st.dump("", TRGDebug::tab() + " ");
1282  s.dump("", TRGDebug::tab() + " ");
1283  }
1284 
1286  }
1287 
1288  void
1290  const unsigned nTSF,
1291  TRGSignalVector& s) const
1292  {
1293 
1294  const unsigned rem = t % 16;
1295 
1296  if ((rem != 0) && (rem != 15)) {
1297  s.push_back(* _fasMap[t * 4 + 0]);
1298  s.push_back(* _fasMap[t * 4 + 1]);
1299  s.push_back(* _fasMap[t * 4 + 2]);
1300  s.push_back(* _fasMap[t * 4 + 3]);
1301  return;
1302  }
1303 
1304  //...Check hit map if there is a hit...
1305  bool hit = false;
1306  for (unsigned i = 0; i < 11; i++) {
1307  if (s[i + 1].active()) {
1308  hit = true;
1309  break;
1310  }
1311  }
1312 
1313  //...No hit case...
1314  if (! hit) {
1315  s.push_back(* _fasMap[t * 4 + 0]);
1316  s.push_back(* _fasMap[t * 4 + 1]);
1317  s.push_back(* _fasMap[t * 4 + 2]);
1318  s.push_back(* _fasMap[t * 4 + 3]);
1319  return;
1320  }
1321 
1322  const string sn = "TSF fastest timing outer";
1324 
1325  //...Prepare timing signal vectors
1326  TRGSignalVector t0("t0", clockData()); // main
1327  t0.push_back(* _fasMap[t * 4 + 0]);
1328  t0.push_back(* _fasMap[t * 4 + 1]);
1329  t0.push_back(* _fasMap[t * 4 + 2]);
1330  t0.push_back(* _fasMap[t * 4 + 3]);
1331 
1332  TRGSignalVector t1("t1", clockData()); // edge
1333  TRGSignal ht0("t0 hit", clockData());
1334  TRGSignal ht1("t1 hit", clockData());
1335  if (rem == 0) {
1336  unsigned n = t / 16;
1337  if (n == 0)
1338  n = nTSF / 16 - 1;
1339  else
1340  --n;
1341 
1342  t1.push_back(* _edg2Map[n * 4 + 0]);
1343  t1.push_back(* _edg2Map[n * 4 + 1]);
1344  t1.push_back(* _edg2Map[n * 4 + 2]);
1345  t1.push_back(* _edg2Map[n * 4 + 3]);
1346 
1347  ht0 = s[2];
1348  ht0 |= s[3];
1349  ht0 |= s[5];
1350  ht0 |= s[6];
1351  ht0 |= s[8];
1352  ht0 |= s[10];
1353  ht0 |= s[11];
1354 
1355  ht1 = s[1];
1356  ht1 |= s[4];
1357  ht1 |= s[7];
1358  ht1 |= s[9];
1359  } else {
1360  unsigned n = t / 16 + 1;
1361  if (n >= nTSF / 16)
1362  n = 0;
1363 
1364  t1.push_back(* _edg1Map[n * 4 + 0]);
1365  t1.push_back(* _edg1Map[n * 4 + 1]);
1366  t1.push_back(* _edg1Map[n * 4 + 2]);
1367  t1.push_back(* _edg1Map[n * 4 + 3]);
1368 
1369  ht0 = s[1];
1370  ht0 |= s[2];
1371  ht0 |= s[4];
1372  ht0 |= s[5];
1373  ht0 |= s[6];
1374  ht0 |= s[7];
1375  ht0 |= s[8];
1376  ht0 |= s[9];
1377  ht0 |= s[10];
1378 
1379  ht1 = s[3];
1380  ht1 |= s[11];
1381  }
1382 
1383  //...State changes...
1384  vector<int> sc = t0.stateChanges();
1385  vector<int> tmp = t1.stateChanges();
1386  sc.insert(sc.end(), tmp.begin(), tmp.end());
1387  std::sort(sc.begin(), sc.end());
1388 
1389  //...Loop over state changes...
1390  TRGSignalVector tm("fastest", clockData(), 4);
1391  int last = clockData().max();
1392  for (unsigned i = 0; i < sc.size(); i++) {
1393  if (sc[i] == last)
1394  continue;
1395 
1396  int clk = sc[i];
1397 
1398  TRGState ts0 = t0.state(clk);
1399  TRGState ts1 = t1.state(clk);
1400  unsigned tm0 = unsigned(ts0);
1401  unsigned tm1 = unsigned(ts1);
1402  bool th0 = ht0.state(clk);
1403  bool th1 = ht1.state(clk);
1404 
1405  if ((! th0) && (! th1))
1406  continue;
1407  else if (th0 && th1) {
1408  if (tm1 < tm0)
1409  tm.set(ts1, clk);
1410  else
1411  tm.set(ts0, clk);
1412  } else if (th0)
1413  tm.set(ts0, clk);
1414  else if (th1)
1415  tm.set(ts1, clk);
1416 
1417  last = clk;
1418  }
1419 
1420  //...Store signals...
1421  s.push_back(tm[0]);
1422  s.push_back(tm[1]);
1423  s.push_back(tm[2]);
1424  s.push_back(tm[3]);
1425 
1426  if (TRGDebug::level()) {
1427  ht0.name("t0 hit:" + ht0.name());
1428  ht0.dump("", TRGDebug::tab() + " ");
1429  t0.dump("", TRGDebug::tab() + " ");
1430  ht1.name("t1 hit:" + ht1.name());
1431  ht1.dump("", TRGDebug::tab() + " ");
1432  t1.dump("", TRGDebug::tab() + " ");
1433  tm.dump("", TRGDebug::tab() + " ");
1434  s.dump("", TRGDebug::tab() + " ");
1435  }
1436 
1438  }
1439 
1440  void
1442  {
1443 
1444  const string sn = "TSF::simulateInner : " + name();
1446 
1447  //...Output for 2D : empty bundle temporary...
1448  for (unsigned i = 0; i < 4; i++) {
1449  string n = name() + "-trker" + TRGUtilities::itostring(i);
1450  TRGSignalVector* dummy = new TRGSignalVector(n, clockData(), 420);
1451  TRGSignalBundle* b = new TRGSignalBundle(n, clockData());
1452  b->push_back(dummy);
1453  output(i)->signal(b);
1454  _toBeDeleted.push_back(dummy);
1455 
1456  if (TRGCDC::getTRGCDC()->firmwareSimulationMode() & 0x4)
1457  b->dumpCOE("",
1458  TRGCDC::getTRGCDC()->firmwareSimulationStartDataClock(),
1459  TRGCDC::getTRGCDC()->firmwareSimulationStopDataClock());
1460  }
1461 
1463  return;
1464 
1465 
1466  //...Loop over mergers to create a super layer hit map...
1467  for (unsigned m = 0; m < nInput(); m++) {
1468  TRGSignalBundle* b = input(m)->signal();
1469 
1470  for (unsigned i = 0; i < 16; i++) {
1471  _secMap.push_back(& ((* b)[0][0][208 + i]));
1472  for (unsigned j = 0; j < 5; j++)
1473  _hitMap[j].push_back(& ((* b)[0][0][j * 16 + i]));
1474  for (unsigned j = 0; j < 4; j++)
1475  _priMap.push_back(& ((* b)[0][0][80 + i * 4 + j]));
1476  for (unsigned j = 0; j < 4; j++)
1477  _fasMap.push_back(& ((* b)[0][0][144 + i * 4 + j]));
1478  }
1479 
1480  for (unsigned i = 0; i < 4; i++)
1481  _edg0Map.push_back(& ((* b)[0][0][224 + i]));
1482  for (unsigned i = 0; i < 4; i++)
1483  _edg1Map.push_back(& ((* b)[0][0][224 + 4 + i]));
1484  for (unsigned i = 0; i < 4; i++)
1485  _edg2Map.push_back(& ((* b)[0][0][224 + 8 + i]));
1486  for (unsigned i = 0; i < 4; i++)
1487  _edg3Map.push_back(& ((* b)[0][0][224 + 12 + i]));
1488  for (unsigned i = 0; i < 4; i++)
1489  _edg4Map.push_back(& ((* b)[0][0][224 + 16 + i]));
1490  }
1491 
1492  //...Storage preparation...
1493  const unsigned nTSF = nInput() * 16;
1494  vector<TRGSignalVector*> trker[4];
1495  vector<int> tsfStateChanges;
1496 
1497  //...Form a TSF...
1498  for (unsigned t = 0; t < nTSF; t++) {
1499 
1500  const string n = name() + "-" + TRGUtilities::itostring(t);
1501  TRGSignalVector* s = new TRGSignalVector(n, clockData());
1502  _tsfIn.push_back(s);
1503 
1504  s->push_back(* _secMap[t]);
1505 
1506  if (t == 0) {
1507  s->push_back(* (_hitMap[0][0]));
1508  s->push_back(* (_hitMap[1][nTSF - 1]));
1509  s->push_back(* (_hitMap[1][0]));
1510  s->push_back(* (_hitMap[2][nTSF - 1]));
1511  s->push_back(* (_hitMap[2][0]));
1512  s->push_back(* (_hitMap[2][1]));
1513  s->push_back(* (_hitMap[3][nTSF - 2]));
1514  s->push_back(* (_hitMap[3][nTSF - 1]));
1515  s->push_back(* (_hitMap[3][0]));
1516  s->push_back(* (_hitMap[3][1]));
1517  s->push_back(* (_hitMap[4][nTSF - 2]));
1518  s->push_back(* (_hitMap[4][nTSF - 1]));
1519  s->push_back(* (_hitMap[4][0]));
1520  s->push_back(* (_hitMap[4][1]));
1521  s->push_back(* (_hitMap[4][2]));
1522  } else if (t == 1) {
1523  s->push_back(* (_hitMap[0][1]));
1524  s->push_back(* (_hitMap[1][0]));
1525  s->push_back(* (_hitMap[1][1]));
1526  s->push_back(* (_hitMap[2][0]));
1527  s->push_back(* (_hitMap[2][1]));
1528  s->push_back(* (_hitMap[2][2]));
1529  s->push_back(* (_hitMap[3][nTSF - 1]));
1530  s->push_back(* (_hitMap[3][0]));
1531  s->push_back(* (_hitMap[3][1]));
1532  s->push_back(* (_hitMap[3][2]));
1533  s->push_back(* (_hitMap[4][nTSF - 1]));
1534  s->push_back(* (_hitMap[4][0]));
1535  s->push_back(* (_hitMap[4][1]));
1536  s->push_back(* (_hitMap[4][2]));
1537  s->push_back(* (_hitMap[4][3]));
1538  } else if (t == (nTSF - 2)) {
1539  s->push_back(* (_hitMap[0][t]));
1540  s->push_back(* (_hitMap[1][t - 11]));
1541  s->push_back(* (_hitMap[1][t]));
1542  s->push_back(* (_hitMap[2][t - 1]));
1543  s->push_back(* (_hitMap[2][t]));
1544  s->push_back(* (_hitMap[2][0]));
1545  s->push_back(* (_hitMap[3][t - 2]));
1546  s->push_back(* (_hitMap[3][t - 1]));
1547  s->push_back(* (_hitMap[3][t]));
1548  s->push_back(* (_hitMap[3][0]));
1549  s->push_back(* (_hitMap[4][t - 2]));
1550  s->push_back(* (_hitMap[4][t - 1]));
1551  s->push_back(* (_hitMap[4][t]));
1552  s->push_back(* (_hitMap[4][t + 1]));
1553  s->push_back(* (_hitMap[4][0]));
1554  } else if (t == (nTSF - 1)) {
1555  s->push_back(* (_hitMap[0][t]));
1556  s->push_back(* (_hitMap[1][t - 11]));
1557  s->push_back(* (_hitMap[1][t]));
1558  s->push_back(* (_hitMap[2][t - 1]));
1559  s->push_back(* (_hitMap[2][t]));
1560  s->push_back(* (_hitMap[2][0]));
1561  s->push_back(* (_hitMap[3][t - 2]));
1562  s->push_back(* (_hitMap[3][t - 1]));
1563  s->push_back(* (_hitMap[3][t]));
1564  s->push_back(* (_hitMap[3][0]));
1565  s->push_back(* (_hitMap[4][t - 2]));
1566  s->push_back(* (_hitMap[4][t - 1]));
1567  s->push_back(* (_hitMap[4][t]));
1568  s->push_back(* (_hitMap[4][0]));
1569  s->push_back(* (_hitMap[4][1]));
1570  } else {
1571  s->push_back(* (_hitMap[0][t]));
1572  s->push_back(* (_hitMap[1][t - 11]));
1573  s->push_back(* (_hitMap[1][t]));
1574  s->push_back(* (_hitMap[2][t - 1]));
1575  s->push_back(* (_hitMap[2][t]));
1576  s->push_back(* (_hitMap[2][0]));
1577  s->push_back(* (_hitMap[3][t - 2]));
1578  s->push_back(* (_hitMap[3][t - 1]));
1579  s->push_back(* (_hitMap[3][t]));
1580  s->push_back(* (_hitMap[3][0]));
1581  s->push_back(* (_hitMap[4][t - 2]));
1582  s->push_back(* (_hitMap[4][t - 1]));
1583  s->push_back(* (_hitMap[4][t]));
1584  s->push_back(* (_hitMap[4][t + 1]));
1585  s->push_back(* (_hitMap[4][t + 2]));
1586  }
1587 
1588  //...Priority timing...
1589  s->push_back(* _priMap[t * 4 + 0]);
1590  s->push_back(* _priMap[t * 4 + 1]);
1591  s->push_back(* _priMap[t * 4 + 2]);
1592  s->push_back(* _priMap[t * 4 + 3]);
1593 
1594  //...Priority timing...
1595  priorityTiming(t, nTSF, * s, (* s)[1], (* s)[2], (* s)[3]);
1596 
1597  //...Fastest timing...
1598  fastestTimingInner(t, nTSF, * s);
1599 
1600  //...Clock counter is omitted...
1601 
1602  //...Simulate TSF...
1603  vector<TRGSignalVector*> result = simulateInner(* s, t);
1604 
1605  TRGSignalVector* forTracker = result[0];
1606  _tsfOut.push_back(forTracker);
1607  _toBeDeleted.push_back(result[1]);
1608 
1609  //...State change list...
1610  vector<int> sc = forTracker->stateChanges();
1611  for (unsigned i = 0; i < sc.size(); i++) {
1612  bool skip = false;
1613  for (unsigned j = 0; j < tsfStateChanges.size(); j++) {
1614  if (tsfStateChanges[j] == sc[i]) {
1615  skip = true;
1616  break;
1617  }
1618  }
1619  if (! skip) tsfStateChanges.push_back(sc[i]);
1620  }
1621 
1622  //...Store it for each tracker division...
1623  const unsigned pos = t / (nTSF / 4);
1624  if (pos == 0) {
1625  trker[0].push_back(forTracker);
1626  trker[1].push_back(forTracker);
1627  } else if (pos == 1) {
1628  trker[1].push_back(forTracker);
1629  trker[2].push_back(forTracker);
1630  } else if (pos == 2) {
1631  trker[2].push_back(forTracker);
1632  trker[3].push_back(forTracker);
1633  } else {
1634  trker[3].push_back(forTracker);
1635  trker[0].push_back(forTracker);
1636  }
1637 
1638  //...Test...
1639  // if (TRGDebug::level() && t < 16) {
1640  // bool ok = ((* result[0]) == (* dbgOut[t])); // track part only
1641 
1642  // if (ok) {
1643  // cout << TRGDebug::tab() << name() << "...Comparison OK"
1644  // << endl;
1645  // }
1646  // else {
1647  // cout << TRGDebug::tab() << name()
1648  // << "...Comparison is not OK" << endl;
1649  // dbgOut[t]->dump("", "kt :");
1650  // result[0]->dump("", "2d :");
1651  // dbgIn[t]->dump("", "kt i:");
1652  // s->dump("", "2d i:");
1653  // }
1654  // }
1655  }
1656 
1657  //...Sort state changes...
1658  std::sort(tsfStateChanges.begin(), tsfStateChanges.end());
1659 
1660  //...Output for 2D...
1661  for (unsigned i = 0; i < 4; i++) {
1662  TRGSignalVector* tOut = packerOuterTracker(trker[i],
1663  tsfStateChanges,
1664  20);
1665  string n = name() + "trker" + TRGUtilities::itostring(i);
1666  TRGSignalBundle* b = new TRGSignalBundle(n, clockData());
1667  b->push_back(tOut);
1668  output(i)->signal(b);
1669  }
1670 
1672  }
1673 
1674  void
1676  const unsigned nTSF,
1677  TRGSignalVector& s) const
1678  {
1679 
1680  const unsigned rem = t % 16;
1681 
1682  if ((rem > 1) && (rem < 14)) {
1683  s.push_back(* _fasMap[t * 4 + 0]);
1684  s.push_back(* _fasMap[t * 4 + 1]);
1685  s.push_back(* _fasMap[t * 4 + 2]);
1686  s.push_back(* _fasMap[t * 4 + 3]);
1687  return;
1688  }
1689 
1690  //...Check hit map if there is a hit...
1691  bool hit = false;
1692  for (unsigned i = 0; i < 11; i++) {
1693  if (s[i + 1].active()) {
1694  hit = true;
1695  break;
1696  }
1697  }
1698 
1699  //...No hit case...
1700  if (! hit) {
1701  s.push_back(* _fasMap[t * 4 + 0]);
1702  s.push_back(* _fasMap[t * 4 + 1]);
1703  s.push_back(* _fasMap[t * 4 + 2]);
1704  s.push_back(* _fasMap[t * 4 + 3]);
1705  return;
1706  }
1707 
1708  const string sn = "TSF fastest timing inner";
1710 
1711  //...Prepare timing signal vectors
1712  TRGSignalVector t0("t0", clockData()); // main
1713  t0.push_back(* _fasMap[t * 4 + 0]);
1714  t0.push_back(* _fasMap[t * 4 + 1]);
1715  t0.push_back(* _fasMap[t * 4 + 2]);
1716  t0.push_back(* _fasMap[t * 4 + 3]);
1717 
1718  TRGSignalVector t1("t1", clockData()); // edge
1719  TRGSignal ht0("t0 hit", clockData());
1720  TRGSignal ht1("t1 hit", clockData());
1721  if (rem == 0) {
1722  unsigned n = t / 16;
1723  if (n == 0)
1724  n = nTSF / 16 - 1;
1725  else
1726  --n;
1727 
1728  t1.push_back(* _edg3Map[n * 4 + 0]);
1729  t1.push_back(* _edg3Map[n * 4 + 1]);
1730  t1.push_back(* _edg3Map[n * 4 + 2]);
1731  t1.push_back(* _edg3Map[n * 4 + 3]);
1732 
1733  ht0 = s[0 + 1];
1734  ht0 |= s[2 + 1];
1735  ht0 |= s[4 + 1];
1736  ht0 |= s[5 + 1];
1737  ht0 |= s[8 + 1];
1738  ht0 |= s[9 + 1];
1739  ht0 |= s[12 + 1];
1740  ht0 |= s[13 + 1];
1741 
1742  ht1 = s[1 + 1];
1743  ht1 |= s[3 + 1];
1744  ht1 |= s[6 + 1];
1745  ht1 |= s[7 + 1];
1746  ht1 |= s[10 + 1];
1747  ht1 |= s[11 + 1];
1748  } else if (rem == 1) {
1749  unsigned n = t / 16;
1750  if (n == 0)
1751  n = nTSF / 16 - 1;
1752  else
1753  --n;
1754 
1755  t1.push_back(* _edg4Map[n * 4 + 0]);
1756  t1.push_back(* _edg4Map[n * 4 + 1]);
1757  t1.push_back(* _edg4Map[n * 4 + 2]);
1758  t1.push_back(* _edg4Map[n * 4 + 3]);
1759 
1760  // ht0 = s[0 + 1];
1761  ht0 = s[1 + 1];
1762  ht0 |= s[2 + 1];
1763  ht0 |= s[3 + 1];
1764  ht0 |= s[4 + 1];
1765  ht0 |= s[5 + 1];
1766  ht0 |= s[7 + 1];
1767  ht0 |= s[8 + 1];
1768  ht0 |= s[9 + 1];
1769  ht0 |= s[10 + 1];
1770  ht0 |= s[12 + 1];
1771  ht0 |= s[13 + 1];
1772 
1773  ht1 |= s[6 + 1];
1774  ht1 |= s[11 + 1];
1775  } else if (rem == 14) {
1776  unsigned n = t / 16 + 1;
1777  if (n >= nTSF / 16)
1778  n = 0;
1779 
1780  t1.push_back(* _edg1Map[n * 4 + 0]);
1781  t1.push_back(* _edg1Map[n * 4 + 1]);
1782  t1.push_back(* _edg1Map[n * 4 + 2]);
1783  t1.push_back(* _edg1Map[n * 4 + 3]);
1784 
1785  ht0 = s[0 + 1];
1786  ht0 |= s[1 + 1];
1787  ht0 |= s[2 + 1];
1788  ht0 |= s[3 + 1];
1789  ht0 |= s[4 + 1];
1790  ht0 |= s[5 + 1];
1791  ht0 |= s[6 + 1];
1792  ht0 |= s[7 + 1];
1793  ht0 |= s[8 + 1];
1794  ht0 |= s[9 + 1];
1795  ht0 |= s[10 + 1];
1796  ht0 |= s[11 + 1];
1797  ht0 |= s[12 + 1];
1798 
1799  ht1 = s[13 + 1];
1800  } else {
1801  unsigned n = t / 16 + 1;
1802  if (n >= nTSF / 16)
1803  n = 0;
1804 
1805  t1.push_back(* _edg2Map[n * 4 + 0]);
1806  t1.push_back(* _edg2Map[n * 4 + 1]);
1807  t1.push_back(* _edg2Map[n * 4 + 2]);
1808  t1.push_back(* _edg2Map[n * 4 + 3]);
1809 
1810  ht0 = s[0 + 1];
1811  ht0 |= s[1 + 1];
1812  ht0 |= s[2 + 1];
1813  ht0 |= s[3 + 1];
1814  ht0 |= s[4 + 1];
1815  ht0 |= s[6 + 1];
1816  ht0 |= s[7 + 1];
1817  ht0 |= s[8 + 1];
1818  ht0 |= s[10 + 1];
1819  ht0 |= s[11 + 1];
1820 
1821  ht1 = s[5 + 1];
1822  ht1 |= s[9 + 1];
1823  ht1 |= s[12 + 1];
1824  ht1 |= s[13 + 1];
1825  }
1826 
1827  //...State changes...
1828  vector<int> sc = t0.stateChanges();
1829  vector<int> tmp = t1.stateChanges();
1830  sc.insert(sc.end(), tmp.begin(), tmp.end());
1831  std::sort(sc.begin(), sc.end());
1832 
1833  //...Loop over state changes...
1834  TRGSignalVector tm("fastest", clockData(), 4);
1835  int last = clockData().max();
1836  for (unsigned i = 0; i < sc.size(); i++) {
1837  if (sc[i] == last)
1838  continue;
1839 
1840  int clk = sc[i];
1841 
1842  TRGState ts0 = t0.state(clk);
1843  TRGState ts1 = t1.state(clk);
1844  unsigned tm0 = unsigned(ts0);
1845  unsigned tm1 = unsigned(ts1);
1846  bool th0 = ht0.state(clk);
1847  bool th1 = ht1.state(clk);
1848 
1849  if ((! th0) && (! th1))
1850  continue;
1851  else if (th0 && th1) {
1852  if (tm1 < tm0)
1853  tm.set(ts1, clk);
1854  else
1855  tm.set(ts0, clk);
1856  } else if (th0)
1857  tm.set(ts0, clk);
1858  else if (th1)
1859  tm.set(ts1, clk);
1860 
1861  last = clk;
1862  }
1863 
1864  //...Store signals...
1865  s.push_back(tm[0]);
1866  s.push_back(tm[1]);
1867  s.push_back(tm[2]);
1868  s.push_back(tm[3]);
1869 
1870  if (TRGDebug::level()) {
1871  ht0.name("t0 hit:" + ht0.name());
1872  ht0.dump("", TRGDebug::tab() + " ");
1873  t0.dump("", TRGDebug::tab() + " ");
1874  ht1.name("t1 hit:" + ht1.name());
1875  ht1.dump("", TRGDebug::tab() + " ");
1876  t1.dump("", TRGDebug::tab() + " ");
1877  tm.dump("", TRGDebug::tab() + " ");
1878  s.dump("", TRGDebug::tab() + " ");
1879  }
1880 
1882  }
1883 
1884  vector<TRGSignalVector*>
1886  {
1887 
1888  // This is just a simple coincidence logic. Should be replaced by
1889  // real logic.
1890 
1891  //const int width = 10;
1892 
1893  //...Layer hit...
1894  //TRGSignal l0 = s[0 + 1].widen(width);
1895  //TRGSignal l1 = s[1 + 1].widen(width) | s[2 + 1].widen(width);
1896  //TRGSignal l2 = s[3 + 1].widen(width) | s[4 + 1].widen(width) |
1897  // s[5 + 1].widen(width);
1898  //TRGSignal l3 = s[6 + 1].widen(width) | s[7 + 1].widen(width) |
1899  // s[8 + 1].widen(width) | s[9 + 1].widen(width);
1900  //TRGSignal l4 = s[10 + 1].widen(width) | s[11 + 1].widen(width) |
1901  // s[12 + 1].widen(width) | s[13 + 1].widen(width);
1902 
1903  //...Layer coincidence...
1904  //TRGSignal a0 = l1 & l2 & l3 & l4;
1905  //TRGSignal a1 = l0 & l2 & l3 & l4;
1906  //TRGSignal a2 = l0 & l1 & l3 & l4;
1907  //TRGSignal a3 = l0 & l1 & l2 & l4;
1908  //TRGSignal a4 = l0 & l1 & l2 & l3;
1909 
1910  //...Final hit... (detail hit pattern changes are ignored here)
1911  //TRGSignal a = a0 | a1 | a2 | a3 | a4; TODO should it be used somewhere?
1912 
1913  //...Check timing cells...
1914  vector<int> sc = s.stateChanges();
1915  for (unsigned i = 0; i < sc.size(); i++) {
1916  int clk = sc[i];
1917  TRGState st = s.state(clk).subset(1, 3);
1918 
1919  if (st[0]) {
1920  } else {
1921  if (st[1] && st[2]) {
1922  } else if (st[1]) {
1923  } else if (st[2]) {
1924  }
1925  }
1926  }
1927 
1928  TRGSignalVector* r0 = new TRGSignalVector("tmp0", clockData(), 21);
1929  TRGSignalVector* r1 = new TRGSignalVector("tmp0", clockData(), 21);
1930  vector<TRGSignalVector*> v;
1931  v.push_back(r0);
1932  v.push_back(r1);
1933  return v;
1934  }
1935 
1936  void
1938  {
1939  const TRGState sid(8, id);
1940  TRGSignalVector idv("TSFid", s.clock(), 8);
1941 
1942  //...Encode ID only when s is active...
1943  vector<int> sc = s.stateChanges();
1944  for (unsigned i = 0; i < sc.size(); i++) {
1945  if (s.active(sc[i]))
1946  idv.set(sid, sc[i]);
1947  }
1948 
1949  //...Merge ID vectors...
1950  s += idv;
1951  }
1952 
1953  void
1955  {
1956 
1957  //...Delete old objects...
1958  for (unsigned i = 0; i < nOutput(); i++) {
1959  if (output(i))
1960  if (output(i)->signal())
1961  delete output(i)->signal();
1962  }
1963  for (unsigned i = 0; i < _tsfIn.size(); i++)
1964  delete _tsfIn[i];
1965  _tsfIn.clear();
1966  for (unsigned i = 0; i < _tsfOut.size(); i++)
1967  delete _tsfOut[i];
1968  _tsfOut.clear();
1969  for (unsigned i = 0; i < _toBeDeleted.size(); i++)
1970  delete _toBeDeleted[i];
1971  _toBeDeleted.clear();
1972 
1973  //...Clear old pointers...
1974  for (unsigned i = 0; i < 5; i++)
1975  _hitMap[i].clear();
1976  _priMap.clear();
1977  _fasMap.clear();
1978  _secMap.clear();
1979  _edg0Map.clear();
1980  _edg1Map.clear();
1981  _edg2Map.clear();
1982  _edg3Map.clear();
1983  _edg4Map.clear();
1984 
1985  if (_type == innerType)
1986  simulateInner();
1987  else
1988  simulateOuter();
1989  }
1990 
1991  void
1993  {
1994 
1995  const string sn = "TSF::simulate2 : " + name();
1997 
1998  // if (_type == innerType) {
1999  // cout << "??? tmp skip" << endl;
2000  // TRGDebug::leaveStage(sn);
2001  // return;
2002  // }
2003 
2004  //...Delete old objects...
2005  for (unsigned i = 0; i < nOutput(); i++) {
2006  if (output(i))
2007  if (output(i)->signal())
2008  delete output(i)->signal();
2009  }
2010  for (unsigned i = 0; i < _tsfIn.size(); i++)
2011  delete _tsfIn[i];
2012  _tsfIn.clear();
2013  for (unsigned i = 0; i < _tsfOut.size(); i++)
2014  delete _tsfOut[i];
2015  _tsfOut.clear();
2016  for (unsigned i = 0; i < _toBeDeleted.size(); i++)
2017  delete _toBeDeleted[i];
2018  _toBeDeleted.clear();
2019 
2020  //...Clear old pointers...
2021  for (unsigned i = 0; i < 5; i++)
2022  _hitMap[i].clear();
2023  _priMap.clear();
2024  _fasMap.clear();
2025  _secMap.clear();
2026  _edg0Map.clear();
2027  _edg1Map.clear();
2028  _edg2Map.clear();
2029  _edg3Map.clear();
2030  _edg4Map.clear();
2031 
2032  //...Storage preparation...
2033  const unsigned nTSF = nInput() * 16;
2034  vector<TRGSignalVector*> trker[4];
2035  vector<int> tsfStateChanges;
2036 
2037  //...Creates hit maps...
2038  if (_type == innerType)
2039  hitMapInner();
2040  else
2041  hitMapOuter();
2042 
2043  //...Form a TSF...
2044  for (unsigned t = 0; t < nTSF; t++) {
2045 
2046  const string n = name() + "-" + TRGUtilities::itostring(t);
2047  TRGSignalVector* s = new TRGSignalVector(n, clockData());
2048  _tsfIn.push_back(s);
2049 
2050  //...Make input signals...
2051  if (_type == innerType)
2052  inputInner(t, nTSF, s);
2053  else
2054  inputOuter(t, nTSF, s);
2055 
2056  //...Simulate TSF...
2057  vector<TRGSignalVector*> result = simulateTSF(s, t);
2058  vector<TRGSignalVector*> result2 = simulateTSF2(s, t);
2059  _toBeDeleted.push_back(result[1]); // Event timing omitted now
2060 
2061  if (TRGDebug::level()) {
2062  if (result.size() != result2.size()) {
2063  cout << "TSF::simulateTSF2 has different response(size)"
2064  << endl;
2065  } else {
2066  for (unsigned i = 0; i < result.size(); i++) {
2067  if ((* result[i]) != (* result2[i]))
2068  cout << "TSF::simulateTSF2 has different response"
2069  << "(contents)" << endl;
2070  }
2071  }
2072  }
2073 
2074  TRGSignalVector* forTracker0 = result[0];
2075  TRGSignalVector* forTracker1 = new TRGSignalVector(* forTracker0);
2076 
2077  _tsfOut.push_back(forTracker0);
2078  _tsfOut.push_back(forTracker1);
2079 
2080  //...State change list...
2081  vector<int> sc = forTracker0->stateChanges();
2082  for (unsigned i = 0; i < sc.size(); i++) {
2083  bool skip = false;
2084  for (unsigned j = 0; j < tsfStateChanges.size(); j++) {
2085  if (tsfStateChanges[j] == sc[i]) {
2086  skip = true;
2087  break;
2088  }
2089  }
2090  if (! skip) tsfStateChanges.push_back(sc[i]);
2091  }
2092 
2093  //...Store it for each tracker division...
2094  const unsigned pos = t / (nTSF / 4);
2095  if (pos == 0) {
2096  addID(* forTracker0, t - 3 * (nTSF / 4));
2097  addID(* forTracker1, t);
2098  trker[3].push_back(forTracker0);
2099  trker[0].push_back(forTracker1);
2100  } else if (pos == 1) {
2101  addID(* forTracker0, t);
2102  addID(* forTracker1, t - 1 * (nTSF / 4));
2103  trker[0].push_back(forTracker0);
2104  trker[1].push_back(forTracker1);
2105  } else if (pos == 2) {
2106  addID(* forTracker0, t - 1 * (nTSF / 4));
2107  addID(* forTracker1, t - 2 * (nTSF / 4));
2108  trker[1].push_back(forTracker0);
2109  trker[2].push_back(forTracker1);
2110  } else {
2111  addID(* forTracker0, t - 2 * (nTSF / 4));
2112  addID(* forTracker1, t - 3 * (nTSF / 4));
2113  trker[2].push_back(forTracker0);
2114  trker[3].push_back(forTracker1);
2115  }
2116 
2117  if (TRGDebug::level())
2118  if (forTracker0->active())
2119  cout << TRGDebug::tab() << name() << " : TSF out="
2120  << t << endl;
2121  }
2122 
2123  //...Sort state changes...
2124  std::sort(tsfStateChanges.begin(), tsfStateChanges.end());
2125 
2126  //...Output for 2D...
2127  for (unsigned i = 0; i < 4; i++) {
2128  string n = name() + "-trker" + TRGUtilities::itostring(i);
2129  TRGSignalVector* tOut = packerForTracker(trker[i],
2130  tsfStateChanges,
2131  20);
2132  tOut->name(n);
2133  TRGSignalBundle* b = new TRGSignalBundle(n, clockData());
2134  b->push_back(tOut);
2135  output(i)->signal(b);
2136  _toBeDeleted.push_back(tOut);
2137 
2138  if (TRGCDC::getTRGCDC()->firmwareSimulationMode() & 0x4)
2139  b->dumpCOE("",
2140  TRGCDC::getTRGCDC()->firmwareSimulationStartDataClock(),
2141  TRGCDC::getTRGCDC()->firmwareSimulationStopDataClock());
2142  // b->dump();
2143  }
2144 
2146  }
2147 
2148  void
2150  {
2151 
2152  // dump("detail","??? ");
2153 
2154  //...Loop over mergers to create a super layer hit map...
2155  for (unsigned m = 0; m < nInput(); m++) {
2156  TRGSignalBundle* b = input(m)->signal();
2157 
2158  // b->dump("", "??? ");
2159 
2160  for (unsigned i = 0; i < 16; i++) {
2161  _secMap.push_back(& ((* b)[0][0][208 + i]));
2162  for (unsigned j = 0; j < 5; j++) {
2163  _hitMap[j].push_back(& ((* b)[0][0][j * 16 + i]));
2164 
2165  // _hitMap[j][i]->dump("", "??? " + TRGUtilities::itostring(i) + "-" + TRGUtilities::itostring(j));
2166 
2167  }
2168  for (unsigned j = 0; j < 4; j++)
2169  _priMap.push_back(& ((* b)[0][0][80 + i * 4 + j]));
2170  for (unsigned j = 0; j < 4; j++)
2171  _fasMap.push_back(& ((* b)[0][0][144 + i * 4 + j]));
2172  }
2173 
2174  for (unsigned i = 0; i < 4; i++)
2175  _edg0Map.push_back(& ((* b)[0][0][224 + i]));
2176  for (unsigned i = 0; i < 4; i++)
2177  _edg1Map.push_back(& ((* b)[0][0][224 + 4 + i]));
2178  for (unsigned i = 0; i < 4; i++)
2179  _edg2Map.push_back(& ((* b)[0][0][224 + 8 + i]));
2180  for (unsigned i = 0; i < 4; i++)
2181  _edg3Map.push_back(& ((* b)[0][0][224 + 12 + i]));
2182  for (unsigned i = 0; i < 4; i++)
2183  _edg4Map.push_back(& ((* b)[0][0][224 + 16 + i]));
2184  }
2185  }
2186 
2187  void
2189  {
2190 
2191  //...Loop over mergers to create a super layer hit map...
2192  for (unsigned m = 0; m < nInput(); m++) {
2193  TRGSignalBundle* b = input(m)->signal();
2194 
2195  for (unsigned i = 0; i < 16; i++) {
2196  _secMap.push_back(& ((* b)[0][0][208 + i]));
2197  for (unsigned j = 0; j < 5; j++)
2198  _hitMap[j].push_back(& ((* b)[0][0][j * 16 + i]));
2199  for (unsigned j = 0; j < 4; j++)
2200  _priMap.push_back(& ((* b)[0][0][80 + i * 4 + j]));
2201  for (unsigned j = 0; j < 4; j++)
2202  _fasMap.push_back(& ((* b)[0][0][144 + i * 4 + j]));
2203  }
2204 
2205  for (unsigned i = 0; i < 4; i++)
2206  _edg0Map.push_back(& ((* b)[0][0][224 + i]));
2207  for (unsigned i = 0; i < 4; i++)
2208  _edg1Map.push_back(& ((* b)[0][0][224 + 4 + i]));
2209  for (unsigned i = 0; i < 4; i++)
2210  _edg2Map.push_back(& ((* b)[0][0][224 + 8 + i]));
2211  }
2212  }
2213 
2214  void
2216  const unsigned nTSF,
2217  TRGSignalVector* s)
2218  {
2219  s->push_back(* _secMap[t]);
2220 
2221  if (t == 0) {
2222  s->push_back(* (_hitMap[0][0]));
2223  s->push_back(* (_hitMap[1][nTSF - 1]));
2224  s->push_back(* (_hitMap[1][0]));
2225  s->push_back(* (_hitMap[2][nTSF - 1]));
2226  s->push_back(* (_hitMap[2][0]));
2227  s->push_back(* (_hitMap[2][1]));
2228  s->push_back(* (_hitMap[3][nTSF - 2]));
2229  s->push_back(* (_hitMap[3][nTSF - 1]));
2230  s->push_back(* (_hitMap[3][0]));
2231  s->push_back(* (_hitMap[3][1]));
2232  s->push_back(* (_hitMap[4][nTSF - 2]));
2233  s->push_back(* (_hitMap[4][nTSF - 1]));
2234  s->push_back(* (_hitMap[4][0]));
2235  s->push_back(* (_hitMap[4][1]));
2236  s->push_back(* (_hitMap[4][2]));
2237  } else if (t == 1) {
2238  s->push_back(* (_hitMap[0][1]));
2239  s->push_back(* (_hitMap[1][0]));
2240  s->push_back(* (_hitMap[1][1]));
2241  s->push_back(* (_hitMap[2][0]));
2242  s->push_back(* (_hitMap[2][1]));
2243  s->push_back(* (_hitMap[2][2]));
2244  s->push_back(* (_hitMap[3][nTSF - 1]));
2245  s->push_back(* (_hitMap[3][0]));
2246  s->push_back(* (_hitMap[3][1]));
2247  s->push_back(* (_hitMap[3][2]));
2248  s->push_back(* (_hitMap[4][nTSF - 1]));
2249  s->push_back(* (_hitMap[4][0]));
2250  s->push_back(* (_hitMap[4][1]));
2251  s->push_back(* (_hitMap[4][2]));
2252  s->push_back(* (_hitMap[4][3]));
2253  } else if (t == (nTSF - 2)) {
2254  s->push_back(* (_hitMap[0][t]));
2255  s->push_back(* (_hitMap[1][t - 11]));
2256  s->push_back(* (_hitMap[1][t]));
2257  s->push_back(* (_hitMap[2][t - 1]));
2258  s->push_back(* (_hitMap[2][t]));
2259  s->push_back(* (_hitMap[2][0]));
2260  s->push_back(* (_hitMap[3][t - 2]));
2261  s->push_back(* (_hitMap[3][t - 1]));
2262  s->push_back(* (_hitMap[3][t]));
2263  s->push_back(* (_hitMap[3][0]));
2264  s->push_back(* (_hitMap[4][t - 2]));
2265  s->push_back(* (_hitMap[4][t - 1]));
2266  s->push_back(* (_hitMap[4][t]));
2267  s->push_back(* (_hitMap[4][t + 1]));
2268  s->push_back(* (_hitMap[4][0]));
2269  } else if (t == (nTSF - 1)) {
2270  s->push_back(* (_hitMap[0][t]));
2271  s->push_back(* (_hitMap[1][t - 11]));
2272  s->push_back(* (_hitMap[1][t]));
2273  s->push_back(* (_hitMap[2][t - 1]));
2274  s->push_back(* (_hitMap[2][t]));
2275  s->push_back(* (_hitMap[2][0]));
2276  s->push_back(* (_hitMap[3][t - 2]));
2277  s->push_back(* (_hitMap[3][t - 1]));
2278  s->push_back(* (_hitMap[3][t]));
2279  s->push_back(* (_hitMap[3][0]));
2280  s->push_back(* (_hitMap[4][t - 2]));
2281  s->push_back(* (_hitMap[4][t - 1]));
2282  s->push_back(* (_hitMap[4][t]));
2283  s->push_back(* (_hitMap[4][0]));
2284  s->push_back(* (_hitMap[4][1]));
2285  } else {
2286  // _hitMap[1][t - 1]->dump("", "??? " + TRGUtilities::itostring(1) + "-" + TRGUtilities::itostring(t - 1));
2287 
2288  TRGSignal& ts = * _hitMap[1][t - 1];
2289 
2290  s->push_back(* (_hitMap[0][t]));
2291  // s->push_back(* (_hitMap[1][t - 11])); // Why this makes SegV?
2292  s->push_back(ts);
2293  s->push_back(* (_hitMap[1][t]));
2294  s->push_back(* (_hitMap[2][t - 1]));
2295  s->push_back(* (_hitMap[2][t]));
2296  s->push_back(* (_hitMap[2][0]));
2297  s->push_back(* (_hitMap[3][t - 2]));
2298  s->push_back(* (_hitMap[3][t - 1]));
2299  s->push_back(* (_hitMap[3][t]));
2300  s->push_back(* (_hitMap[3][0]));
2301  s->push_back(* (_hitMap[4][t - 2]));
2302  s->push_back(* (_hitMap[4][t - 1]));
2303  s->push_back(* (_hitMap[4][t]));
2304  s->push_back(* (_hitMap[4][t + 1]));
2305  s->push_back(* (_hitMap[4][t + 2]));
2306  }
2307 
2308  //...Priority timing...
2309 // s->push_back(* _priMap[t * 4 + 0]);
2310 // s->push_back(* _priMap[t * 4 + 1]);
2311 // s->push_back(* _priMap[t * 4 + 2]);
2312 // s->push_back(* _priMap[t * 4 + 3]);
2313 
2314  //...Priority timing...
2315  priorityTiming(t, nTSF, * s, (* s)[1], (* s)[2], (* s)[3]);
2316 
2317  //...Fastest timing...
2318  fastestTimingInner(t, nTSF, * s);
2319 
2320  //...Clock counter is omitted...
2321  }
2322 
2323  void
2325  const unsigned nTSF,
2326  TRGSignalVector* s)
2327  {
2328  s->push_back(* _secMap[t]);
2329 
2330  if (t == 0) {
2331  s->push_back(* (_hitMap[0][nTSF - 1]));
2332  s->push_back(* (_hitMap[0][0]));
2333  s->push_back(* (_hitMap[0][1]));
2334  s->push_back(* (_hitMap[1][nTSF - 1]));
2335  s->push_back(* (_hitMap[1][0]));
2336  s->push_back(* (_hitMap[2][0]));
2337  s->push_back(* (_hitMap[3][nTSF - 1]));
2338  s->push_back(* (_hitMap[3][0]));
2339  s->push_back(* (_hitMap[4][nTSF - 1]));
2340  s->push_back(* (_hitMap[4][0]));
2341  s->push_back(* (_hitMap[4][1]));
2342  } else if (t == (nTSF - 1)) {
2343  s->push_back(* (_hitMap[0][nTSF - 2]));
2344  s->push_back(* (_hitMap[0][nTSF - 1]));
2345  s->push_back(* (_hitMap[0][0]));
2346  s->push_back(* (_hitMap[1][nTSF - 2]));
2347  s->push_back(* (_hitMap[1][nTSF - 1]));
2348  s->push_back(* (_hitMap[2][nTSF - 1]));
2349  s->push_back(* (_hitMap[3][nTSF - 2]));
2350  s->push_back(* (_hitMap[3][nTSF - 1]));
2351 
2352  s->push_back(* (_hitMap[4][nTSF - 2]));
2353  s->push_back(* (_hitMap[4][nTSF - 1]));
2354  s->push_back(* (_hitMap[4][0]));
2355  } else {
2356  s->push_back(* (_hitMap[0][t - 1]));
2357  s->push_back(* (_hitMap[0][t]));
2358  s->push_back(* (_hitMap[0][t + 1]));
2359  s->push_back(* (_hitMap[1][t - 1]));
2360  s->push_back(* (_hitMap[1][t]));
2361  s->push_back(* (_hitMap[2][t]));
2362  s->push_back(* (_hitMap[3][t - 1]));
2363  s->push_back(* (_hitMap[3][t]));
2364  s->push_back(* (_hitMap[4][t - 1]));
2365  s->push_back(* (_hitMap[4][t]));
2366  s->push_back(* (_hitMap[4][t + 1]));
2367  }
2368 
2369  //...Priority timing...
2370 // s->push_back(* _priMap[t * 4 + 0]);
2371 // s->push_back(* _priMap[t * 4 + 1]);
2372 // s->push_back(* _priMap[t * 4 + 2]);
2373 // s->push_back(* _priMap[t * 4 + 3]);
2374 
2375  //...Priority timing...
2376  priorityTiming(t, nTSF, * s, (* s)[6], (* s)[7], (* s)[8]);
2377 
2378  //...Fastest timing...
2379  fastestTimingOuter(t, nTSF, * s);
2380 
2381  //...Clock counter is omitted...
2382  }
2383 
2385  TRGCDCTrackSegmentFinder::packerForTracker(vector<TRGSignalVector*>& hitList,
2386  vector<int>& cList,
2387  const unsigned maxHit)
2388  {
2389 
2390  TRGSignalVector* result =
2391  new TRGSignalVector("", (* hitList[0]).clock(), 21 * maxHit);
2392 
2393  for (unsigned ci = 0; ci < cList.size(); ci++) {
2394  unsigned cntHit = 0;
2395  for (unsigned hi = 0; hi < hitList.size(); hi++) {
2396  TRGState s = (* hitList[hi]).state(cList[ci]);
2397  if (s.active()) {
2398  if (cntHit >= maxHit) continue;
2399  for (unsigned j = 0; j < 21; j++) {
2400  if ((* hitList[hi])[j].state(cList[ci])) {
2401  (* result)[21 * (maxHit - 1) - (cntHit * 21) + j]
2402  .set(cList[ci], cList[ci] + 1);
2403  }
2404  }
2405  if (TRGDebug::level()) {
2406  TRGState t = hitList[hi]->state(cList[ci]).subset(13, 8);
2407  cout << TRGDebug::tab() << " hit found : TSF out local ID="
2408  << unsigned(t) << "(" << t << ")" << endl;
2409  }
2410 
2411  ++cntHit;
2412  // result->dump("", "??? ");
2413  }
2414  }
2415  }
2416 
2417  return result;
2418  }
2419 
2420  vector <TRGSignalVector*>
2422  {
2423 
2424  //variables for common
2425  const string na = "TSF" + TRGUtilities::itostring(tsid) + " in " +
2426  name();
2427  TCSegment* tsi = _tsSL[tsid];
2428  vector <TRGSignalVector*> result;
2429 
2430  //variables for EvtTime & Low pT
2431  vector<bool> fTimeVect;
2432  // int tmpFTime = 0 ;
2433 
2434  //variables for Tracker & N.N
2435  vector <bool> tmpOutBool;
2436 
2437  //iwTRGSignalVector* resultT = new TRGSignalVector(na, in->clock(),22);
2438  TRGSignalVector* resultT = new TRGSignalVector(na, in->clock(), 13);
2439  TRGSignalVector* resultE = new TRGSignalVector(na, in->clock(), 10);
2440  TRGSignalVector* Hitmap = new TRGSignalVector(na + "HitMap", in->clock(), 0);
2441  TRGSignalVector pTime(na + "PriorityTime", in->clock(), 0);
2442  TRGSignalVector fTime(na + "FastestTime", in->clock(), 0);
2443  TRGSignal* pri0 = 0;
2444  TRGSignal* pri1 = 0;
2445  TRGSignal* pri2 = 0;
2446  if (_type == innerType) {
2447  for (unsigned i = 0; i < 16; i++) {
2448  Hitmap->push_back((* in)[i]);
2449  (* Hitmap)[i].widen(16);
2450  }
2451  for (unsigned i = 0; i < 4; i++) {
2452  pTime.push_back((* in)[i + 16]);
2453  fTime.push_back((* in)[i + 20]);
2454  }
2455  pri0 = & (*Hitmap)[1];
2456  pri1 = & (*Hitmap)[2];
2457  pri2 = & (*Hitmap)[3];
2458  } else {
2459  for (unsigned i = 0; i < 12; i++) {
2460  Hitmap->push_back((* in)[i]);
2461  (* Hitmap)[i].widen(16);
2462  }
2463  for (unsigned i = 0; i < 4; i++) {
2464  pTime.push_back((* in)[i + 12]);
2465  fTime.push_back((* in)[i + 16]);
2466  }
2467  pri0 = & (*Hitmap)[6];
2468  pri1 = & (*Hitmap)[7];
2469  pri2 = & (*Hitmap)[8];
2470  }
2471 
2472  //...Clock counter...
2473  const TRGSignalVector& cc = in->clock().clockCounter();
2474  for (unsigned i = 0; i < 5; i++) {
2475  pTime.push_back(cc[i]);
2476  fTime.push_back(cc[i]);
2477  }
2478 
2479  vector <int> changeTime = Hitmap->stateChanges();
2480 
2481  int* LUTValue = new int[changeTime.size()];
2482  if (changeTime.size()) {
2483 
2484  const string fn = "TSF::simulateTSF:tsid=" + to_string(tsid);
2486 
2487  int hitPosition = 0;
2488  bool fTimeBool[10];
2489  int tmpPTime = 0 ;
2490  int tmpCTime = 0 ;
2491  int tmpOutInt;
2492  fTime.state(changeTime[0]).copy2bool(fTimeBool);
2493  fTimeBool[9] = true;
2494  fTimeVect.insert(fTimeVect.begin(), fTimeBool, fTimeBool + 10);
2495  //tmpFTime = mkint(fTime.state(changeTime[0]));
2496  bool eOUT = true;
2497  for (unsigned i = 0; i < changeTime.size(); i++) {
2498  int ct = changeTime[i];
2499 
2500 // LUTValue[i] = tsi->LUT()->getValue(mkint(Hitmap->state(ct)));
2501  LUTValue[i] = tsi->LUT()->getValue(unsigned(Hitmap->state(ct)));
2502 
2504  if ((LUTValue[i]) && (eOUT)) {
2505  resultE->set(fTimeVect, ct);
2506  eOUT = false;
2507  }
2508 
2509  bool priority1rise = pri0->riseEdge(ct);
2510  bool priority2rise = pri1->riseEdge(ct) or pri2->riseEdge(ct);
2511 
2513  //ready for output
2514  if (priority1rise) {
2515  hitPosition = 3;
2516 // tmpPTime = mkint(pTime.state(ct));
2517  tmpPTime = unsigned(pTime.state(ct));
2518  tmpCTime = ct;
2519  } else if (priority2rise) {
2520  if (!hitPosition) {
2521 // tmpPTime = mkint(pTime.state(ct));
2522  tmpPTime = unsigned(pTime.state(ct));
2523  tmpCTime = ct;
2524  if ((*Hitmap)[0].state(ct)) hitPosition = 2;
2525  else hitPosition = 1;
2526  }
2527  }
2528 
2529  // output selection
2530  if ((hitPosition) && (LUTValue[i]) && ((ct - tmpCTime) < 16)) {
2531  tmpOutInt = tmpPTime * pow(2, 4) +
2532  LUTValue[i] * pow(2, 2) + hitPosition;
2533  tmpOutBool = mkbool(tmpOutInt, 13); // ID removed : iw
2534 
2535  if (hitPosition == 3) {
2536  if (priority1rise) {
2537  resultT->set(tmpOutBool, ct);
2538  } else {
2539  if ((LUTValue[i] == 1) | (LUTValue[i] == 2)) {
2540  if (!((LUTValue[i - 1] == 1) |
2541  (LUTValue[i - 1] == 2)))
2542  resultT->set(tmpOutBool, ct);
2543  } else {
2544  if (!(LUTValue[i - 1])) resultT->set(tmpOutBool, ct);
2545  }
2546  }
2547  } else {
2548  if (priority2rise) resultT->set(tmpOutBool, ct);
2549  else {
2550  if ((LUTValue[i] == 1) | (LUTValue[i] == 2)) {
2551  if (!((LUTValue[i - 1] == 1) |
2552  (LUTValue[i - 1] == 2)))
2553  resultT->set(tmpOutBool, ct);
2554  } else {
2555  if (!(LUTValue[i])) resultT->set(tmpOutBool, ct);
2556  }
2557  }
2558  }
2559  }
2560 
2561  if (TRGDebug::level() > 1) {
2562  cout << TRGDebug::tab() << "clk=" << ct
2563  << ", pattern=" << Hitmap->state(ct)
2564  << ", LUT=" << LUTValue[i]
2565  << ", pri=" << priority1rise
2566  << ", sec=" << priority2rise
2567  << ", hitPos=" << hitPosition
2568  << ", (clk-tmpCTime)=" << (ct - tmpCTime)
2569  << endl;
2570  }
2571  }
2572 
2574  }
2575 
2576  result.push_back(resultT);
2577  result.push_back(resultE);
2578 
2579  // cppcheck-suppress uninitdata
2580  delete[] LUTValue;
2581  delete Hitmap;
2582 
2583  return result;
2584  }
2585 
2586  vector <TRGSignalVector*>
2588  {
2589 
2590  //variables for common
2591  const string na = "TSF" + TRGUtilities::itostring(tsid) + " in " +
2592  name();
2593  TCSegment* tsi = _tsSL[tsid];
2594  vector <TRGSignalVector*> result;
2595 
2596  //variables for EvtTime & Low pT
2597  vector<bool> fTimeVect;
2598  // int tmpFTime = 0 ;
2599 
2600  //variables for Tracker & N.N
2601  vector <bool> tmpOutBool;
2602 
2603  TRGSignalVector* resultT = new TRGSignalVector(na, in->clock(), 13);
2604  TRGSignalVector* resultE = new TRGSignalVector(na, in->clock(), 10);
2605  TRGSignalVector* Hitmap = new TRGSignalVector(na + "HitMap",
2606  in->clock(),
2607  0);
2608  TRGSignalVector pTime(na + "PriorityTime", in->clock(), 0);
2609  TRGSignalVector fTime(na + "FastestTime", in->clock(), 0);
2610  TRGSignal* pri0 = 0;
2611  TRGSignal* pri1 = 0;
2612  TRGSignal* pri2 = 0;
2613  if (_type == innerType) {
2614  for (unsigned i = 0; i < 16; i++) {
2615  Hitmap->push_back((* in)[i]);
2616  (* Hitmap)[i].widen(16);
2617  }
2618  for (unsigned i = 0; i < 4; i++) {
2619  pTime.push_back((* in)[i + 16]);
2620  fTime.push_back((* in)[i + 20]);
2621  }
2622  pri0 = & (*Hitmap)[1];
2623  pri1 = & (*Hitmap)[2];
2624  pri2 = & (*Hitmap)[3];
2625  } else {
2626  for (unsigned i = 0; i < 12; i++) {
2627  Hitmap->push_back((* in)[i]);
2628  (* Hitmap)[i].widen(16);
2629  }
2630  for (unsigned i = 0; i < 4; i++) {
2631  pTime.push_back((* in)[i + 12]);
2632  fTime.push_back((* in)[i + 16]);
2633  }
2634  pri0 = & (*Hitmap)[6];
2635  pri1 = & (*Hitmap)[7];
2636  pri2 = & (*Hitmap)[8];
2637  }
2638 
2639  //...Clock counter...
2640  const TRGSignalVector& cc = in->clock().clockCounter();
2641  for (unsigned i = 0; i < 5; i++) {
2642  pTime.push_back(cc[i]);
2643  fTime.push_back(cc[i]);
2644  }
2645 
2646  vector<int> changeTime = Hitmap->stateChanges();
2647 
2648  int* LUTValue = new int[changeTime.size()];
2649  //int oldLUT = 0;
2650  int lastFastHit = in->clock().min();
2651  if (changeTime.size()) {
2652 
2653  int oldLUT = 0; // 2019/07/31 by ytlai, moved to here to reduce scope
2654  const string fn = "TSF::simulateTSF:tsid=" + to_string(tsid);
2656 
2657  int hitPosition = 0;
2658  int tmpPTime = 0 ;
2659  int tmpCTime = 0 ;
2660  int tmpOutInt;
2661  //TRGState ft(10); //TODO variable not used at all
2662  bool eOut = false;
2663  for (unsigned i = 0; i < changeTime.size(); i++) {
2664 
2665  int ct = changeTime[i];
2666  TRGState st = Hitmap->state(ct);
2667 
2668  //...LUT...
2669  // 0:no hit, 1:right, 2:left, 3:LR unknown hit
2670  LUTValue[i] = tsi->LUT()->getValue(unsigned(st));
2671 
2672  //...Any wire hit (for the fastest timing)...
2673  bool active = st.active();
2674  if (active) {
2675  const int timeCounter = ct - lastFastHit;
2676 
2677  //...Record as the fastest timing hit...
2678  if (timeCounter > 15) {
2679  lastFastHit = ct;
2680  TRGState ftnow = fTime.state(ct);
2681  ftnow += TRGState(1, 1);
2682  //ft = ftnow;
2683  eOut = true;
2684  }
2685  }
2686 
2688  if ((LUTValue[i])) {
2689  if (eOut) {
2690  resultE->set(fTimeVect, ct);
2691  eOut = false;
2692  }
2693  }
2694 
2695  bool priority1rise = pri0->riseEdge(ct);
2696  bool priority2rise = pri1->riseEdge(ct) or pri2->riseEdge(ct);
2697 
2699  //ready for output
2700  if (priority1rise) {
2701  hitPosition = 3;
2702  tmpPTime = unsigned(pTime.state(ct));
2703  tmpCTime = ct;
2704  } else if (priority2rise) {
2705  if (!hitPosition) {
2706  tmpPTime = unsigned(pTime.state(ct));
2707  tmpCTime = ct;
2708  if ((*Hitmap)[0].state(ct)) hitPosition = 2;
2709  else hitPosition = 1;
2710  }
2711  }
2712 
2713  // output selection
2714  if ((ct - tmpCTime) < 16) {
2715  if ((hitPosition) && (LUTValue[i])) {
2716  //iw tmpOutInt = tsid * pow(2, 13) + tmpPTime * pow(2, 4) +
2717  //iw LUTValue[i] * pow(2,2) + hitPosition;
2718  tmpOutInt = tmpPTime * pow(2, 4) +
2719  LUTValue[i] * pow(2, 2) + hitPosition;
2720  tmpOutBool = mkbool(tmpOutInt, 13); // ID removed : iw
2721 
2722  if (hitPosition == 3) {
2723  if (priority1rise) {
2724  resultT->set(tmpOutBool, ct);
2725  oldLUT = LUTValue[i];
2726  } else {
2727  if ((LUTValue[i] == 1) | (LUTValue[i] == 2)) {
2728  if (!((oldLUT == 1) |
2729  (oldLUT == 2))) {
2730  resultT->set(tmpOutBool, ct);
2731  oldLUT = LUTValue[i];
2732  }
2733  } else {
2734  if (!(LUTValue[i - 1])) {
2735  resultT->set(tmpOutBool, ct);
2736  oldLUT = LUTValue[i];
2737  }
2738  }
2739  }
2740  } else {
2741  if ((priority2rise) && (!oldLUT)) resultT->set(tmpOutBool, ct);
2742  else {
2743  if ((LUTValue[i] == 1) | (LUTValue[i] == 2)) {
2744  if (!((oldLUT == 1) |
2745  (oldLUT == 2))) {
2746  resultT->set(tmpOutBool, ct);
2747  oldLUT = LUTValue[i];
2748  }
2749  } else {
2750  if (!(oldLUT)) {
2751  resultT->set(tmpOutBool, ct);
2752  oldLUT = LUTValue[i];
2753  }
2754  }
2755  }
2756  }
2757  }
2758  } else {
2759  oldLUT = 0;
2760  hitPosition = 0;
2761  }
2762 
2763  if (TRGDebug::level() > 1) {
2764  cout << TRGDebug::tab() << "clk=" << ct
2765  << ", pattern=" << st
2766  << ", LUT=" << LUTValue[i]
2767  << ", pri=" << priority1rise
2768  << ", sec=" << priority2rise
2769  << ", hitPos=" << hitPosition
2770  << ", (clk-tmpCTime)=" << (ct - tmpCTime)
2771  << endl;
2772  }
2773  }
2775  }
2776 
2777  if (TRGDebug::level()) {
2778  vector<int> sc = resultT->stateChanges();
2779  vector<unsigned> lv;
2780  for (unsigned i = 0; i < sc.size(); i++) {
2781  //...LUT value...
2782  unsigned l = unsigned(resultT->state(sc[i]).subset(2, 2));
2783  lv.push_back(l);
2784  cout << "clk=" << sc[i] << " LUT output[" << i << "]=" << l << endl;
2785  }
2786  bool found1or2 = false;
2787  bool found3 = false;
2788  for (unsigned i = 0; i < lv.size(); i++) {
2789  if (found1or2 && (lv[i] > 0))
2790  cout << "!!! simulateTSF something wrong(found1or2)" << endl;
2791  if (found3 && (lv[i] == 3))
2792  cout << "!!! simulateTSF something wrong(found3)" << endl;
2793 
2794  if ((lv[i] == 1) or (lv[i] == 2))
2795  found1or2 = true;
2796  else if (lv[i] == 3)
2797  found3 = true;
2798  }
2799  }
2800 
2801  result.push_back(resultT);
2802  result.push_back(resultE);
2803 
2804  // cppcheck-suppress uninitdata
2805  delete[] LUTValue;
2806  delete Hitmap;
2807 
2808  return result;
2809  }
2810  vector <TRGSignalVector*>
2812  {
2813 
2814  //variables for common
2815  const string na = "TSF" + TRGUtilities::itostring(tsid) + " in " +
2816  name();
2817  TCSegment* tsi = _tsSL[tsid];
2818  vector <TRGSignalVector*> result;
2819 
2820  //variables for EvtTime & Low pT
2821  vector<bool> fTimeVect;
2822  // int tmpFTime = 0 ;
2823 
2824  //variables for Tracker & N.N
2825  TRGSignalVector* resultT = new TRGSignalVector(na, in->clock(), 13);
2826  TRGSignalVector* resultE = new TRGSignalVector(na, in->clock(), 10);
2827  TRGSignalVector* Hitmap = new TRGSignalVector(na + "HitMap",
2828  in->clock(),
2829  0);
2830  TRGSignalVector pTime(na + "PriorityTime", in->clock(), 0);
2831  TRGSignalVector fTime(na + "FastestTime", in->clock(), 0);
2832  TRGSignal* pri0 = 0;
2833  TRGSignal* pri1 = 0;
2834  TRGSignal* pri2 = 0;
2835 
2836  if (_type == innerType) {
2837  for (unsigned i = 0; i < 16; i++) {
2838  Hitmap->push_back((* in)[i]);
2839  (* Hitmap)[i].widen(16);
2840  }
2841  for (unsigned i = 0; i < 4; i++) {
2842  pTime.push_back((* in)[i + 16]);
2843  fTime.push_back((* in)[i + 20]);
2844  }
2845  pri0 = & (*Hitmap)[1];
2846  pri1 = & (*Hitmap)[2];
2847  pri2 = & (*Hitmap)[3];
2848  } else {
2849  for (unsigned i = 0; i < 12; i++) {
2850  Hitmap->push_back((* in)[i]);
2851  (* Hitmap)[i].widen(16);
2852  }
2853  for (unsigned i = 0; i < 4; i++) {
2854  pTime.push_back((* in)[i + 12]);
2855  fTime.push_back((* in)[i + 16]);
2856  }
2857  pri0 = & (*Hitmap)[6];
2858  pri1 = & (*Hitmap)[7];
2859  pri2 = & (*Hitmap)[8];
2860  }
2861 
2862  //...Clock counter...
2863  const TRGSignalVector& cc = in->clock().clockCounter();
2864  for (unsigned i = 0; i < 5; i++) {
2865  pTime.push_back(cc[i]);
2866  fTime.push_back(cc[i]);
2867  }
2868 
2869  vector<int> changeTime = Hitmap->stateChanges();
2870 
2871  vector<unsigned> luts;
2872  int lastFastHit = in->clock().min();
2873 
2874  if (changeTime.size()) {
2875 
2876  const string fn = "TSF::simulateTSF2:tsid=" + to_string(tsid);
2878 
2879  //TRGState ft(10); //not used, should be?
2880  bool eOut = false;
2881 
2882  unsigned stateHitPos = 0; // 0:wait, 1:active1st, 2:active2nd
2883  unsigned hitPos = 0;
2884  unsigned timing = 0;
2885  int timePosFound = 0;
2886  int counterPos = 0;
2887 
2888  unsigned stateLR = 0; // 0:wait, 1:LUT12, 2:LUT3
2889 // int timeLRFound = 0;
2890 // int counterLR = 0;
2891 
2892  for (unsigned i = 0; i < changeTime.size(); i++) {
2893 
2894  const int ct = changeTime[i];
2895  const TRGState st = Hitmap->state(ct);
2896 
2897  //...LUT...
2898  // 0:no hit, 1:right, 2:left, 3:LR unknown hit
2899  const unsigned lut = tsi->LUT()->getValue(unsigned(st));
2900  luts.push_back(lut);
2901 
2902  //...Any wire hit (for the fastest timing)...
2903  const bool active = st.active();
2904  if (active) {
2905  const int timeCounter = ct - lastFastHit;
2906 
2907  //...Record as the fastest timing hit...
2908  if (timeCounter > 15) {
2909  lastFastHit = ct;
2910  TRGState ftnow = fTime.state(ct);
2911  ftnow += TRGState(1, 1);
2912  //ft = ftnow;
2913  eOut = true;
2914  }
2915  }
2916 
2917  // output for EvtTime & Low pT tracker module
2918  if (lut) {
2919  if (eOut) {
2920  resultE->set(fTimeVect, ct);
2921  eOut = false;
2922  }
2923  }
2924 
2925  //...Hit position...
2926  const bool hit1st = pri0->riseEdge(ct);
2927  const bool hit2nd0 = pri1->riseEdge(ct);
2928  const bool hit2nd1 = pri2->riseEdge(ct);
2929  const bool hit2nd = hit2nd0 || hit2nd1;
2930 
2931  //...Hit position state machine...
2932 
2933  //...State : wait...
2934  if (stateHitPos == 0) {
2935  hitPos = 0;
2936  timing = 0;
2937  timePosFound = 0;
2938  if (hit1st) {
2939  hitPos = 3;
2940  timing = unsigned(pTime.state(ct));
2941  timePosFound = ct;
2942  stateHitPos = 1;
2943  } else if (hit2nd) {
2944  if ((* Hitmap)[0].state(ct))
2945  hitPos = 2;
2946  else
2947  hitPos = 1;
2948  timing = unsigned(pTime.state(ct));
2949  timePosFound = ct;
2950  stateHitPos = 2;
2951  }
2952  }
2953 
2954  //...State : active1st...
2955  else if (stateHitPos == 1) {
2956  counterPos = ct - timePosFound;
2957  if (counterPos > 15)
2958  stateHitPos = 0;
2959  }
2960 
2961  //...State : active2nd...
2962  else if (stateHitPos == 2) {
2963  if (hit1st) {
2964  hitPos = 3;
2965  timing = unsigned(pTime.state(ct));
2966  timePosFound = ct;
2967  stateHitPos = 1;
2968  } else {
2969  counterPos = ct - timePosFound;
2970  if (counterPos > 15)
2971  stateHitPos = 0;
2972  }
2973  }
2974 
2975  //...State unknown...
2976  else {
2977  cout << "TSF::simulateTSF2 !!! strange state in hit position"
2978  << endl;
2979  }
2980 
2981  //...L/R decision state machine...
2982 
2983  //...State : wait...
2984  if (stateLR == 0) {
2985  if (lut != 0) {
2986  const unsigned val = (timing << 4) | (lut << 2) | hitPos;
2987  const TRGState output(13, val);
2988  resultT->set(output, ct);
2989 // timeLRFound = ct;
2990  if ((lut == 1) || (lut == 2))
2991  stateLR = 1;
2992  else
2993  stateLR = 2;
2994 
2995  if (TRGDebug::level()) {
2996  if (((lut == 1) || (lut == 2)) && (stateHitPos == 0))
2997  cout << TRGDebug::tab()
2998  << "!!! state machines incosistent" << endl;
2999  }
3000  }
3001  }
3002 
3003  //...State : LUT12...
3004  else if (stateLR == 1) {
3005 // counterLR = ct - timeLRFound;
3006  if (counterPos > 15)
3007  stateLR = 0;
3008  }
3009 
3010  //...State : LUT3...
3011  else if (stateLR == 2) {
3012  if (lut) {
3013  if ((lut == 1) || (lut == 2)) {
3014  const unsigned val = (timing << 4) | (lut << 2) |
3015  hitPos;
3016  const TRGState output(13, val);
3017  resultT->set(output, ct);
3018 // timeLRFound = ct;
3019  stateLR = 1;
3020  } else {
3021 // counterLR = ct - timeLRFound;
3022  if (counterPos > 15)
3023  stateLR = 0;
3024  }
3025  }
3026  }
3027 
3028  //...State unknown...
3029  else {
3030  cout << "TSF::simulateTSF2 !!! strange state in L/R decision"
3031  << endl;
3032  }
3033 
3034  if (TRGDebug::level() > 1) {
3035  cout << TRGDebug::tab() << "clk=" << ct
3036  << ", ptn=" << st
3037  << ", LUT=" << lut
3038  << ", pri=" << hit1st
3039  << ", sec=" << hit2nd
3040  << ", hPos=" << hitPos
3041  << ", ctr=" << counterPos // << "," << counterLR
3042  << ", states=" << stateHitPos << "," << stateLR
3043  << endl;
3044  }
3045  }
3047  }
3048 
3049  if (TRGDebug::level()) {
3050  vector<int> sc = resultT->stateChanges();
3051  vector<unsigned> lv;
3052  for (unsigned i = 0; i < sc.size(); i++) {
3053  //...LUT value...
3054  unsigned l = unsigned(resultT->state(sc[i]).subset(2, 2));
3055  lv.push_back(l);
3056  cout << "clk=" << sc[i] << " LUT output[" << i << "]=" << l << endl;
3057  }
3058  }
3059 
3060  result.push_back(resultT);
3061  result.push_back(resultE);
3062 
3063  delete Hitmap;
3064 
3065  return result;
3066  }
3067 
3069 } // namespace Belle2
DataType Phi() const
The azimuth angle.
Definition: B2Vector3.h:151
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:32
Low-level class to create/modify relations between StoreArrays.
Definition: RelationArray.h:62
int getEntries() const
Get the number of elements.
Accessor to arrays stored in the data store.
Definition: StoreArray.h:113
int getEntries() const
Get the number of objects in the array.
Definition: StoreArray.h:216
A class to represent a trigger board.
Definition: Board.h:25
A class to represent a wire hit in CDC.
Definition: CellHit.h:74
A class to represent a wire in CDC.
Definition: Cell.h:40
virtual const TRGSignal & signal(void) const =0
returns trigger output. Null will returned if no signal.
A class to represent a CDC merger board.
Definition: Merger.h:36
std::vector< TRGSignal * > _priMap
Internal data of the priority hit timing.
TClonesArray * m_hitPatternInformation
Stores hitpattern information.
std::vector< TRGSignal * > _edg4Map
Internal data of the edge timing information.
std::vector< TRGSignal * > _edg0Map
Internal data of the edge timing information.
TTree * m_treeInputTSF
ROOT TTree for input.
bool m_makeRootFile
make ROOT file or not
TClonesArray * m_particleEfficiency
[Efficiency, Pt, # MC TS] Efficiency = -1 means that # MC TS is 0.
std::vector< TRGSignalVector * > _toBeDeleted
One time info. to be deleted in next event;.
std::vector< TRGSignal * > _edg1Map
Internal data of the edge timing information.
TClonesArray * m_nnPatternInformation
[superlayer id, lrDriftTime, timeWire0, timeWire1, ..., ...]
TTree * m_treeOutputTSF
ROOT TTree for output.
std::vector< TRGSignal * > _edg2Map
Internal data of the edge timing information.
TClonesArray * m_tsInformation
[SuperLayer Id, Wire Id, Priority Timing]
std::vector< TRGSignalVector * > _tsfOut
TSF response storeage.
std::vector< TRGSignal * > _hitMap[5]
Internal data of wire hit map.
TTree * m_treeNNTSF
ROOT Tree for NNTSF.
std::vector< TRGSignal * > _secMap
Internal data of the priority cell hit position flag.
std::vector< TCSegment * > _tsSL
list of TSF
std::vector< TRGSignalVector * > _tsfIn
TSF input storage.
std::vector< TRGSignal * > _edg3Map
Internal data of the edge timing information.
bool m_logicLUTFlag
0 is Logic. 1 is LUT.
std::vector< TRGSignal * > _fasMap
Internal data of the fastest hit timing.
boardType
enum of boardType of TrackSegmentFinder
A class to represent a wire hit in CDC.
Definition: WireHit.h:33
A class to represent a wire in CDC.
Definition: Wire.h:56
The instance of TRGCDC is a singleton.
Definition: TRGCDC.h:69
A class to represent a digitized signal. Unit is nano second.
Definition: Clock.h:38
A class to represent a bundle of SignalVectors.
Definition: SignalBundle.h:26
A class to represent a bundle of digitized signals.
Definition: SignalVector.h:26
A class to represent a digitized signal. Unit is nano second.
Definition: Signal.h:23
A class to represent a state of multi bits.
Definition: State.h:24
vector< TRGSignalVector * > simulateTSFOld(TRGSignalVector *in, unsigned id)
Simulate TSF response (unified version)
unsigned size(void) const
returns bit size.
Definition: State.h:149
static std::string tab(void)
returns tab spaces.
Definition: Debug.cc:47
const TRGSignalVector & set(const TRGState &, int clockPosition)
sets state at given clock.
static TRGCDC * getTRGCDC(void)
returns TRGCDC object.
Definition: TRGCDC.cc:192
void fastestTimingOuter(unsigned tsfID, const unsigned nTSF, TRGSignalVector &s) const
Make TRGSignals for the fastest timing bits.
int mcLR(void) const
returns mc left/right information
Definition: CellHit.h:245
const TRGChannel * input(unsigned i) const
returns input channel i.
Definition: Board.h:158
void simulateOuter(void)
Firmware simulation for the outers. yi.
TRGState subset(unsigned i, unsigned n) const
returns subset from i with n bits.
Definition: State.cc:356
void copy2bool(bool *array) const
returns a filled array.
Definition: State.h:255
TRGState state(int clockPosition) const
returns state at given clock position.
TRGSignalVector * packerOuterTracker(vector< TRGSignalVector * > &, vector< int > &, const unsigned)
Packing output for tracker.
const TRGClock & clock(void) const
returns clock.
Definition: SignalVector.h:125
void priorityTiming(unsigned tsfID, const unsigned nTSF, TRGSignalVector &s, const TRGSignal &center, const TRGSignal &right, const TRGSignal &left) const
Make TRGSignals for the priority timing bits.
const TRGClock & clockData(void) const
returns data clock.
Definition: Board.h:123
TRGSignalVector * packerForTracker(vector< TRGSignalVector * > &, vector< int > &, const unsigned)
Output packer for tracker.
bool riseEdge(int clockPosition) const
returns true if signal is active and rising edge in give clock position.
Definition: Signal.h:304
const CDCHit * hit(void) const
Access to CDCHit.
Definition: CellHit.cc:133
void push_back(const TRGCDCMerger *)
push back the Mergers of this TSF
vector< TRGSignalVector * > findTSHit(TRGSignalVector *eachInput, int)
Use LUT for find TSHit.
vector< TRGSignalVector * > simulateTSF(TRGSignalVector *in, unsigned id)
Simulate TSF response (unified version, 2016/07/12)
void inputOuter(const unsigned id, const unsigned nTSF, TRGSignalVector *s)
Creates input signals to TSF for the outer.
const std::string & name(void) const
returns name.
Definition: Board.h:109
std::vector< int > stateChanges(void) const
returns a list of clock position of state change.
static void enterStage(const std::string &stageName)
Declare that you enter new stage.
Definition: Debug.cc:24
unsigned nInput(void) const
returns input channels.
Definition: Board.h:172
int min(void) const
returns min. clock point.
Definition: Clock.h:194
TRGCDCTrackSegmentFinder(const TRGCDC &, bool makeRootFile, bool logicLUTFlag)
Constructor.
const TRGCDCCellHit * hit(void) const
returns a pointer to a TRGCDCCellHit.
Definition: Cell.h:362
const TRGSignal & signal(void) const override
returns an input to the trigger. This is sync'ed to 1GHz clock.
Definition: Wire.h:226
static std::string itostring(int i)
converts int to string. (Use boost::lexical_cast)
Definition: Utilities.cc:50
void simulateInner(void)
Firmware simulation for the outers. yi.
int max(void) const
returns max. clock point.
Definition: Clock.h:201
unsigned nSegments(void) const
returns # of track segments.
Definition: TRGCDC.h:954
void saveTSFResults(std::vector< TRGCDCSegmentHit * > *segmentHitsSL)
save result of TSF
static void addID(TRGSignalVector &s, unsigned id)
Add TSF ID to timing signal vector for the output.
const TRGCDCWireHit * hit(void) const
returns a pointer to a TRGCDCWireHit.
Definition: Wire.h:144
unsigned nSuperLayers(void) const
returns # of super layers.
Definition: TRGCDC.h:870
void saveTSInformation(std::vector< TRGCDCSegment * > &tss)
save the TS info
const TRGCDCSegment & segment(unsigned id) const
returns a track segment.
Definition: TRGCDC.h:933
void hitMapOuter(void)
Creates the hit maps for the outer.
TRGSignalBundle * signal(void) const
returns signal.
Definition: Channel.h:93
void simulate2(void)
Firmware simulation. Unified version of inner and outer : yi.
bool state(int clockPosition) const
returns true if signal is active in given clock position.
Definition: Signal.h:286
void doit(std::vector< TRGCDCSegment * > &tss, const bool trackSegmentClockSimulation, std::vector< TRGCDCSegmentHit * > &segmentHits, std::vector< TRGCDCSegmentHit * > *segmentHitsSL)
Member functions of doing TSF.
static int level(void)
returns the debug level.
Definition: Debug.cc:67
bool active(void) const
returns true if there is a signal.
Definition: Signal.h:277
const TRGSignalVector & clockCounter(void) const
returns the clock counter.
Definition: Clock.cc:166
void inputInner(const unsigned id, const unsigned nTSF, TRGSignalVector *s)
Creates input signals to TSF for the inner.
const TRGCDCLayer * layer(unsigned id) const
returns a pointer to a layer. 0 will be returned if 'id' is invalid.
Definition: TRGCDC.h:828
static void leaveStage(const std::string &stageName)
Declare that you leave a stage.
Definition: Debug.cc:34
vector< TRGSignalVector * > simulateTSF2(TRGSignalVector *in, unsigned id)
Simulate TSF response (unified version, state machine)
unsigned nCells(void) const
returns # of cells.
Definition: Layer.h:194
unsigned iCDCSimHit(void) const
return index of CDCSimHit
Definition: WireHit.h:111
void saveNNTSInformation(std::vector< TRGCDCSegment * > &tss)
Saves NNTS information. Only when ts is hit.
void simulate(void)
Firmware simulation. yi.
void hitMapInner(void)
Creates the hit maps for the inner.
TRGSignalVector * packerOuterEvt(vector< TRGSignalVector * >, vector< int >, int)
Packing output for evtTime & Low pT.
double mkint(TRGState)
tranformatoin into integer
unsigned nOutput(void) const
returns output channels.
Definition: Board.h:180
boardType type(void) const
board type of TSF
TRGChannel * output(unsigned i) const
returns output channel i.
Definition: Board.h:165
void fastestTimingInner(unsigned tsfID, const unsigned nTSF, TRGSignalVector &s) const
Make TRGSignals for the fastest timing bits.
vector< bool > mkbool(int, int)
tranformatoin into bool
void dump(const std::string &message="", const std::string &pre="") const
dumps contents.
Definition: Board.cc:42
Abstract base class for different kinds of events.