Belle II Software  release-06-02-00
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 "cdc/dataobjects/CDCSimHit.h"
32 #include "mdst/dataobjects/MCParticle.h"
33 #include "trg/cdc/FrontEnd.h"
34 #include "trg/cdc/Merger.h"
35 
36 using namespace std;
37 
38 namespace Belle2 {
44 //...For debug : yi...
45 // vector<TRGSignalVector * > dbgIn;
46 // vector<TRGSignalVector * > dbgOut;
47 
48  TRGCDCTrackSegmentFinder::TRGCDCTrackSegmentFinder(const TRGCDC& TRGCDC,
49  bool makeRootFile,
50  bool logicLUTFlag)
51  :
52  TRGBoard("", TRGClock("", 0, 0), TRGClock("", 0, 0), TRGClock("", 0, 0),
53  TRGClock("", 0, 0)),
54  _cdc(TRGCDC),
55  m_logicLUTFlag(logicLUTFlag),
56  m_makeRootFile(makeRootFile),
57  _type(), _tosbE(), _tosbT() // 2019/07/31 by ytlai
58  {
59 
60  m_Trg_PI = 3.141592653589793;
61 
62  m_hitPatternInformation = new TClonesArray("TVectorD");
63  m_particleEfficiency = new TClonesArray("TVectorD");
64  m_tsInformation = new TClonesArray("TVectorD");
65  m_nnPatternInformation = new TClonesArray("TVectorD");
66 
67  // For ROOT file
68  TDirectory* currentDir = gDirectory;
69  if (m_makeRootFile) {
70  m_fileTSF = new TFile("TSF.root", "RECREATE");
71 
72  m_treeInputTSF = new TTree("m_treeInputTSF", "InputTSF");
73  m_treeInputTSF->Branch("hitPatternInformation", &m_hitPatternInformation,
74  32000, 0);
75 
76  m_treeOutputTSF = new TTree("m_treeOutputTSF", "OutputTSF");
77  m_treeOutputTSF->Branch("particleEfficiency", &m_particleEfficiency, 32000, 0);
78  m_treeOutputTSF->Branch("tsInformation", &m_tsInformation, 32000, 0);
79 
80  // For neural network TSF. Filled only when TSF and priority is hit.
81  m_treeNNTSF = new TTree("m_treeNNTSF", "NNTSF");
82  m_treeNNTSF->Branch("nnPatternInformation", &m_nnPatternInformation, 32000, 0);
83  }
84  currentDir->cd();
85  }
86 
87 
89  const std::string& name,
90  boardType type,
91  const TRGClock& systemClock,
92  const TRGClock& dataClock,
93  const TRGClock& userClockInput,
94  const TRGClock& userClockOutput,
95  const std::vector<TCSegment*>& tsSL)
96  : TRGBoard(name, systemClock, dataClock, userClockInput, userClockOutput),
97  _cdc(TRGCDC),
98  m_Trg_PI(), // 2019/07/31 by ytlai
99  m_logicLUTFlag(),
100  m_rootTSFFilename(),
101  m_fileTSF(),
102  _type(type),
103  // _tisb(0),
104  _tosbE(0),
105  _tosbT(0),
106  _tsSL(tsSL)
107  {
108 
109  }
111  {
112 
113  delete m_nnPatternInformation;
114  delete m_tsInformation;
115  delete m_particleEfficiency;
117  delete m_treeInputTSF;
118  if (m_makeRootFile) delete m_fileTSF;
119 
120  }
121 
124  {
125  return _type;
126  }
128  {
129  if (m_makeRootFile) {
130  m_fileTSF->Write();
131  m_fileTSF->Close();
132  }
133  }
134 
135  void TRGCDCTrackSegmentFinder::doit(std::vector<TRGCDCSegment*>& tss,
136  const bool trackSegmentClockSimulation,
137  std::vector<TRGCDCSegmentHit*>& segmentHits,
138  std::vector<TRGCDCSegmentHit*>* segmentHitsSL)
139  {
140  TRGDebug::enterStage("Track Segment Finder");
141 
142  // Saves TS information
143  //saveTSInformation(tss);
144 
145  //...Store TS hits...
146  const unsigned n = tss.size();
147  for (unsigned i = 0; i < n; i++) {
148  TCSegment& s = * tss[i];
149  s.simulate(trackSegmentClockSimulation, m_logicLUTFlag,
150  TRGCDC::getTRGCDC()->getCDCHitCollectionName());
151  if (s.signal().active()) {
152  TCSHit* th = new TCSHit(s);
153  s.hit(th);
154  segmentHits.push_back(th);
155  segmentHitsSL[s.layerId()].push_back(th);
156  }
157  }
158 
159  // Save TSF results
160  saveTSFResults(segmentHitsSL);
161 
162  // Saves NNTS information. Only when ts is hit.
163  //saveNNTSInformation(tss);
164 
165  if (TRGDebug::level() > 1) {
166  cout << TRGDebug::tab() << "TS hit list" << endl;
167  string dumpOption = "trigger";
168  if (TRGDebug::level() > 2)
169  dumpOption = "detail";
170  for (unsigned i = 0; i < _cdc.nSegments(); i++) {
171  const TCSegment& s = _cdc.segment(i);
172  if (s.signal().active())
173  s.dump(dumpOption, TRGDebug::tab(4));
174  }
175 
176  cout << TRGDebug::tab() << "TS hit list (2)" << endl;
177  if (TRGDebug::level() > 2)
178  dumpOption = "detail";
179  for (unsigned i = 0; i < segmentHits.size(); i++) {
180  const TCSHit& s = * segmentHits[i];
181  s.dump(dumpOption, TRGDebug::tab(4));
182  }
183 
184  cout << TRGDebug::tab() << "TS hit list (3)" << endl;
185  if (TRGDebug::level() > 2)
186  dumpOption = "detail";
187  for (unsigned j = 0; j < _cdc.nSuperLayers(); j++) {
188  for (unsigned i = 0; i < segmentHitsSL[j].size(); i++) {
189  const vector<TCSHit*>& s = segmentHitsSL[j];
190  for (unsigned k = 0; k < s.size(); k++)
191  s[k]->dump(dumpOption, TRGDebug::tab(4));
192  }
193  }
194  }
195 
196  TRGDebug::leaveStage("Track Segment Finder");
197  }
198 
199 
200  vector <TRGSignalVector*>
201  TSFinder::findTSHit(TRGSignalVector* eachInput, int tsid)
202  {
203 
204  //variables for common
205  const string na = "TSCandidate" + TRGUtilities::itostring(
206  tsid) + " in " + name();
207  TCSegment* tsi = _tsSL[tsid];
208  vector <TRGSignalVector*> result;
209 
210  //variables for EvtTime & Low pT
211  vector<bool> fTimeVect;
212  // int tmpFTime = 0 ;
213 
214  //variables for Tracker & N.N
215  vector <bool> tmpOutBool;
216 
217  TRGSignalVector* resultT = new TRGSignalVector(na, eachInput->clock(), 22);
218  TRGSignalVector* resultE = new TRGSignalVector(na, eachInput->clock(), 10);
219  TRGSignalVector* Hitmap = new TRGSignalVector(na + "HitMap",
220  eachInput->clock(), 0);
221  TRGSignalVector pTime(na + "PriorityTime", eachInput->clock(), 0);
222  TRGSignalVector fTime(na + "FastestTime", eachInput->clock(), 0);
223  for (unsigned i = 0; i < 12; i++) {
224  Hitmap->push_back((*eachInput)[i]);
225  (*Hitmap)[i].widen(16);
226  }
227  for (unsigned i = 0; i < 4; i++) {
228  pTime.push_back((*eachInput)[i + 12]);
229  fTime.push_back((*eachInput)[i + 16]);
230  }
231  for (unsigned i = 0; i < 5; i++) {
232  pTime.push_back((*eachInput)[i + 20]);
233  fTime.push_back((*eachInput)[i + 20]);
234  }
235 
236  vector <int> changeTime = Hitmap->stateChanges();
237 
238  int* LUTValue = new int[changeTime.size()];
239  if (changeTime.size()) {
240  int hitPosition = 0;
241  bool fTimeBool[10];
242  int tmpPTime = 0 ;
243  int tmpCTime = 0 ;
244  int tmpOutInt;
245  fTime.state(changeTime[0]).copy2bool(fTimeBool);
246  fTimeBool[9] = true;
247  fTimeVect.insert(fTimeVect.begin(), fTimeBool, fTimeBool + 10);
248  //tmpFTime = mkint(fTime.state(changeTime[0]));
249  bool eOUT = true;
250  for (unsigned i = 0; i < changeTime.size(); i++) {
251  LUTValue[i] = tsi->LUT()->getValue(mkint(Hitmap->state(changeTime[i])));
252 
254  if ((LUTValue[i]) && (eOUT)) {
255  resultE->set(fTimeVect, changeTime[i]);
256  eOUT = false;
257  }
258 
259  bool priority1rise = (*Hitmap)[6].riseEdge(changeTime[i]);
260  bool priority2rise = ((*Hitmap)[7].riseEdge(changeTime[i]) |
261  (*Hitmap)[8].riseEdge(changeTime[i]));
262 
264  //ready for output
265  if (priority1rise) {
266  hitPosition = 3;
267  tmpPTime = mkint(pTime.state(changeTime[i]));
268  tmpCTime = changeTime[i];
269  } else if (priority2rise) {
270  if (!hitPosition) {
271  tmpPTime = mkint(pTime.state(changeTime[i]));
272  tmpCTime = changeTime[i];
273  if ((*Hitmap)[0].state(changeTime[i])) hitPosition = 2;
274  else hitPosition = 1;
275  }
276  }
277 
278  // output selection
279  if ((hitPosition) && (LUTValue[i]) && ((changeTime[i] - tmpCTime) < 16)) {
280  tmpOutInt = tsid * pow(2, 13) + tmpPTime * pow(2, 4) + LUTValue[i] * pow(2,
281  2) + hitPosition;
282  tmpOutBool = mkbool(tmpOutInt, 22);
283  if (hitPosition == 3) {
284  if (priority1rise) resultT->set(tmpOutBool, changeTime[i]);
285  else {
286  if ((LUTValue[i] == 1) | (LUTValue[i] == 2)) {
287  if (!((LUTValue[i - 1] == 1) |
288  (LUTValue[i - 1] == 2)))
289  resultT->set(tmpOutBool, changeTime[i]);
290  } else {
291  if (!(LUTValue[i - 1])) resultT->set(tmpOutBool, changeTime[i]);
292  }
293  }
294  } else {
295  if (priority2rise) resultT->set(tmpOutBool, changeTime[i]);
296  else {
297  if ((LUTValue[i] == 1) | (LUTValue[i] == 2)) {
298  if (!((LUTValue[i - 1] == 1) |
299  (LUTValue[i - 1] == 2)))
300  resultT->set(tmpOutBool, changeTime[i]);
301  } else {
302  if (!(LUTValue[i])) resultT->set(tmpOutBool, changeTime[i]);
303  }
304  }
305  }
306  }
307 
308  }
309 
310  }
311  result.push_back(resultT);
312  result.push_back(resultE);
313 
314  delete [] LUTValue;
315  delete Hitmap;
316 
317  return result;
318  }
319 
320  TRGSignalVector*
321  TSFinder::packerOuterTracker(vector<TRGSignalVector*>& hitList,
322  vector<int>& cList,
323  const unsigned maxHit)
324  {
325  TRGSignalVector* result =
326  new TRGSignalVector("", (* hitList[0]).clock() , 21 * maxHit);
327 
328  for (unsigned ci = 0; ci < cList.size(); ci++) {
329  unsigned cntHit = 0;
330  for (unsigned hi = 0; hi < hitList.size(); hi++) {
331  TRGState s = (* hitList[hi]).state(cList[ci]);
332  if (s.active()) {
333  if (cntHit >= maxHit) continue;
334  for (unsigned j = 0; j < 21; j++) {
335  if ((* hitList[hi])[j].state(cList[ci])) {
336  (* result)[21 * (maxHit - 1) - (cntHit * 21) + j]
337  .set(cList[ci], cList[ci] + 1);
338  }
339  }
340  if (TRGDebug::level()) {
341  TRGState t = hitList[hi]->state(cList[ci]).subset(13, 9);
342  cout << TRGDebug::tab() << " hit found : TSF out local ID="
343  << unsigned(t) << "(" << t << ")" << endl;
344  }
345 
346  ++cntHit;
347  // result->dump("", "??? ");
348  }
349  }
350  }
351 
352  return result;
353  }
354 
355  TRGSignalVector*
356  TSFinder::packerOuterEvt(vector<TRGSignalVector*> hitList, vector<int> cList,
357  int maxHit)
358  {
359 
360  //TRGSignalVector * result = new TRGSignalVector("",(*hitList[0]).clock() ,N+9*maxHit);
361  TRGSignalVector* result = new TRGSignalVector("", (*hitList[0]).clock() ,
362  hitList.size() + 9 * maxHit);
363 
364  for (unsigned ci = 0; ci < cList.size(); ci++) {
365  int cntHit = 0;
366  for (unsigned hi = 0; hi < hitList.size(); hi++) {
367  if ((*hitList[hi]).state(cList[ci]).active()) {
368  (*result)[9 * maxHit + hi].set(cList[ci], cList[ci] + 1);
369  if (cntHit >= maxHit) continue;
370  for (unsigned j = 0; j < (((*hitList[hi])).size() - 1); j++) {
371  if ((*hitList[hi])[j].state(cList[ci]))
372  (*result)[9 * (maxHit - 1) - (cntHit * 9) + j].set(cList[ci], cList[ci] + 1);
373  }
374  cntHit++;
375  }
376  }
377  }
378  return result;
379  }
380 
381  double
382  TSFinder::mkint(TRGState bitInput)
383  {
384  double r = 0;
385  bool* binput = new bool[bitInput.size()];
386  bitInput.copy2bool(binput);
387  for (unsigned i = 0; i < bitInput.size(); i++) {
388  if (binput[i])
389  r += pow(2, i);
390  }
391  delete[] binput;
392  return r;
393  }
394 
395  vector<bool>
396  TSFinder::mkbool(int N, int bitSize)
397  {
398  vector<bool> boutput;
399  boutput.resize(bitSize);
400  int tmpint = N;
401  for (unsigned i = 0; tmpint; i++) {
402  if (tmpint % 2) boutput[i] = true;
403  else boutput[i] = false;
404  tmpint = tmpint / 2;
405  }
406 
407  return boutput;
408  }
409 
410 //
411 
412  void TRGCDCTrackSegmentFinder::saveTSInformation(std::vector<TRGCDCSegment*>&
413  tss)
414  {
415 
416  TClonesArray& hitPatternInformation = *m_hitPatternInformation;
417  hitPatternInformation.Clear();
418 
419  StoreArray<CDCSimHit> SimHits;
420  if (! SimHits.getEntries()) {
421  //cout << "CDCTRGTSF !!! can not access to CDCSimHits" << endl;
422  return;
423  }
424  StoreArray<MCParticle> mcParticles;
425  if (! mcParticles.getEntries()) {
426  //cout << "CDCTRGTSF !!! can not access to MCParticles" << endl;
427  return;
428  }
429 
430  //cout<<"Save TS information"<<endl;
431  // Loop over all TSs. wireHit has only one wireSimHit. Meaning there is a particle overlap hit bug.
432  const unsigned nTSs = tss.size();
433  unsigned nHitTSs = 0;
434  for (unsigned iTS = 0; iTS < nTSs; iTS++) {
435  const TCSegment& ts = * tss[iTS];
436  const TRGCDCWire* priority;
437  const TRGCDCWire* secondPriorityL;
438  const TRGCDCWire* secondPriorityR;
439  // Find priority wires
440  if (ts.wires().size() == 15) {
441  priority = ts.wires()[0];
442  secondPriorityR = ts.wires()[1];
443  secondPriorityL = ts.wires()[2];
444  } else {
445  priority = ts.wires()[5];
446  secondPriorityR = ts.wires()[6];
447  secondPriorityL = ts.wires()[7];
448  }
449  // Find L/R, phi of priority wires
450  vector<int> priorityLRs(3);
451  vector<float> priorityPhis(3);
452  TVector3 posOnTrack;
453  TVector3 posOnWire;
454  if (priority->hit() != 0) {
455  int iSimHit = priority->hit()->iCDCSimHit();
456  priorityLRs[0] = SimHits[iSimHit]->getPosFlag();
457  posOnTrack = SimHits[iSimHit]->getPosTrack();
458  posOnWire = SimHits[iSimHit]->getPosWire();
459  priorityPhis[0] = (posOnTrack - posOnWire).Phi() + m_Trg_PI / 2 -
460  posOnWire.Phi();
461  //cout<<ts.name()<<endl;
462  //cout<<"Track: "<<posOnTrack.x()<<" "<<posOnTrack.y()<<" "<<posOnTrack.z()<<endl;
463  //cout<<"Wire: "<<posOnWire.x()<<" "<<posOnWire.y()<<" "<<posOnWire.z()<<endl;
464  //cout<<"Before Phi: "<<(posOnTrack - posOnWire).Phi()<<" PosOnWirePhi: "<<posOnWire.Phi()<<" After Phi: "<<priorityPhis[0]<<endl;
465  //cout<<"LR: "<<priorityLRs[0]<<endl;
466  } else {
467  priorityLRs[0] = -1;
468  priorityPhis[0] = 9999;
469  }
470  if (secondPriorityR->hit() != 0) {
471  int iSimHit = secondPriorityR->hit()->iCDCSimHit();
472  priorityLRs[1] = SimHits[iSimHit]->getPosFlag();
473  posOnTrack = SimHits[iSimHit]->getPosTrack();
474  posOnWire = SimHits[iSimHit]->getPosWire();
475  priorityPhis[1] = (posOnTrack - posOnWire).Phi() + m_Trg_PI / 2 -
476  posOnWire.Phi();
477  } else {
478  priorityLRs[1] = -1;
479  priorityPhis[1] = 9999;
480  }
481  if (secondPriorityL->hit() != 0) {
482  int iSimHit = secondPriorityL->hit()->iCDCSimHit();
483  priorityLRs[2] = SimHits[iSimHit]->getPosFlag();
484  posOnTrack = SimHits[iSimHit]->getPosTrack();
485  posOnWire = SimHits[iSimHit]->getPosWire();
486  priorityPhis[2] = (posOnTrack - posOnWire).Phi() + m_Trg_PI / 2 -
487  posOnWire.Phi();
488  } else {
489  priorityLRs[2] = -1;
490  priorityPhis[2] = 9999;
491  }
492 
493  const unsigned nWires = ts.wires().size();
494  unsigned nHitWires = 0;
495  // Find TSPatternInformation for each particle
496  map<int, unsigned> particleTSPattern;
497  // Loop over wires in TS
498  for (unsigned iWire = 0; iWire < nWires; iWire++) {
499  //...Copy signal from a wire...
500  const TRGSignal& wireSignal = ts.wires()[iWire]->signal();
501  if (wireSignal.active()) ++nHitWires;
502  // Find MC particle of hit wire
503  const TRGCDCWireHit* wireHit = ts.wires()[iWire]->hit();
504  if (wireHit != 0) {
505  MCParticle* particle = SimHits[wireHit->iCDCSimHit()]->getRelatedFrom<MCParticle>();
506  int iMCParticle = (particle) ? particle->getArrayIndex() : mcParticles.getEntries();
507  // If new particle
508  if (particleTSPattern[iMCParticle] == 0) {
509  particleTSPattern[iMCParticle] = 1 << iWire;
510  } else {
511  particleTSPattern[iMCParticle] |= 1 << iWire;
512  }
513  }
514  } // Loop over wires in TS
515 
516  // Print pattern for each particle
517  //for(map<int, unsigned >::const_iterator it = particleTSPattern.begin(); it != particleTSPattern.end(); it++ ) {
518  // bitset<15> printPattern((*it).second);
519  // cout<<ts.name()<<" MC Particle: "<< (*it).first <<" pattern: "<<printPattern<<endl;
520  //}
521 
522  if (nHitWires != 0) {
523  // Ignore TSPatterns that have 2 particles passing TS.
524  if (particleTSPattern.size() == 1) {
525  map<int, unsigned>::const_iterator it = particleTSPattern.begin();
526  bitset<15> printPattern((*it).second);
527  //cout<<ts.name()<<" MC Particle: "<< (*it).first <<" pattern: "<<printPattern<<endl;
528  //cout<<(*it).first<<" "<<ts.superLayerId()<<" "<<printPattern<<endl;
529  // Save TS Pattern information
530  TVectorD tsPatternInformation(9);
531  tsPatternInformation[0] = (*it).first;
532  tsPatternInformation[1] = ts.superLayerId();
533  tsPatternInformation[2] = double((*it).second);
534  tsPatternInformation[3] = priorityLRs[0];
535  tsPatternInformation[4] = priorityPhis[0];
536  tsPatternInformation[5] = priorityLRs[1];
537  tsPatternInformation[6] = priorityPhis[1];
538  tsPatternInformation[7] = priorityLRs[2];
539  tsPatternInformation[8] = priorityPhis[2];
540  new(hitPatternInformation[nHitTSs++]) TVectorD(tsPatternInformation);
541  }
542  //cout<<ts.name()<<" has "<<nHitWires<<" hit wires."<<endl;
543  }
544  } // End of loop over all TSs
545  if (m_makeRootFile) m_treeInputTSF->Fill();
546  //cout<<"End saving TS information"<<endl;
547  } // End of save function
548 
549 
550 
551  void TRGCDCTrackSegmentFinder::saveTSFResults(std::vector<TRGCDCSegmentHit*>*
552  segmentHitsSL)
553  {
554 
555  TClonesArray& particleEfficiency = *m_particleEfficiency;
556  TClonesArray& tsInformation = *m_tsInformation;
557  particleEfficiency.Clear();
558  tsInformation.Clear();
559 
560  int mcInformation = 1;
562  StoreArray<CDCSimHit> SimHits;
563  if (! SimHits) {
564  //cout << "CDCTRGTSF !!! can not access to CDCSimHits" << endl;
565  mcInformation = 0;
566  }
567  StoreArray<MCParticle> mcParticles;
568  if (! mcParticles) {
569  //cout << "CDCTRGTSF !!! can not access to MCParticles" << endl;
570  mcInformation = 0;
571  }
572  if (mcInformation) {
573  RelationArray cdcSimHitRel(mcParticles, SimHits);
574  // Make map for hit to mcParticle
575  map<int, int> simHitsMCParticlesMap;
576  // Loop over all particles
577  for (int iPart = 0; iPart < cdcSimHitRel.getEntries(); iPart++) {
578  // Loop over all hits for particle
579  for (unsigned iHit = 0; iHit < cdcSimHitRel[iPart].getToIndices().size();
580  iHit++) {
581  //cout<<"From: "<<cdcSimHitRel[iPart].getFromIndex()<<" To: "<<cdcSimHitRel[iPart].getToIndex(iHit)<<endl;
582  simHitsMCParticlesMap[cdcSimHitRel[iPart].getToIndex(iHit)] =
583  cdcSimHitRel[iPart].getFromIndex();
584  }
585  }
587  //for(map<int, int >::iterator it = simHitsMCParticlesMap.begin(); it != simHitsMCParticlesMap.end(); it++) {
588  // cout<<"SimHit Index: "<<(*it).first<<" MCParticle Index: "<<(*it).second<<endl;
589  //}
590  // Find efficiency for each particle
591  // Find hit TS SuperLayer for particles
592  // particleNHiTS[iMCParticle] = hitTSSL
593  map<int, unsigned> particleNHitTS;
594  // Loop over all hit TSs
595  for (int iSuperLayer = 0; iSuperLayer < 9; iSuperLayer++) {
596  // map<int, bool> particleHitTS;
597  for (unsigned iTS = 0; iTS < segmentHitsSL[iSuperLayer].size(); iTS++) {
598  const TCSegment& ts = segmentHitsSL[iSuperLayer][iTS]->segment();
599  unsigned nWires = ts.wires().size();
600  for (unsigned iWire = 0; iWire < nWires; iWire++) {
601  const TRGCDCWireHit* wireHit = ts.wires()[iWire]->hit();
602  if (wireHit != 0) {
603  int iMCParticle = simHitsMCParticlesMap[wireHit->iCDCSimHit()];
604  if (particleNHitTS[iMCParticle] == 0) {
605  unsigned hitTSSL;
606  hitTSSL = 1 << iSuperLayer;
607  particleNHitTS[iMCParticle] = hitTSSL;
608  } else
609  particleNHitTS[iMCParticle] |= 1 << iSuperLayer;
610  //cout<<ts.name()<<" "<<ts.wires()[iWire]->name()<<" was hit.";
611  //cout<<" Particle was "<<simHitsMCParticlesMap[wireHit->iCDCSimHit()]<<endl;
612  } // If wire is hit
613  } // End of loop over all wires in TS
614  } // End of loop over all TS in super layer
615  } // End of loop over all hit TSs
617  //for( map<int, unsigned>::const_iterator it = particleNHitTS.begin(); it != particleNHitTS.end(); it++) {
618  // bitset<9> printSuperLayers((*it).second);
619  // cout<<"MC particle: "<<(*it).first<<" HitSuperLayers: "<<printSuperLayers<<" nCount: "<<printSuperLayers.count()<<endl;
620  //}
621 
622  // Find last CDC hit for each MC particle
623  // tsEfficiency[i][tsEfficiency,particle pT,#MCTS]
624  vector<vector<float>> tsEfficiency;
625  // Loop over all particles
626  for (int iPart = 0; iPart < cdcSimHitRel.getEntries(); iPart++) {
627  int lastWireHit = -1;
628  // Loop over all hits for particle
629  for (unsigned iHit = 0; iHit < cdcSimHitRel[iPart].getToIndices().size();
630  iHit++) {
631  int iSimHit = cdcSimHitRel[iPart].getToIndex(iHit);
632  if (SimHits[iSimHit]->getWireID().getICLayer() > lastWireHit) lastWireHit =
633  SimHits[iSimHit]->getWireID().getICLayer();
634  //cout<<"Particle: "<<cdcSimHitRel[iPart].getFromIndex()<<" CDCSimHit: "<<iSimHit<<endl;
635  //cout<<"SuperLayer: "<<SimHits[iSimHit]->getWireID().getISuperLayer()<<" wireLayer: "<<SimHits[iSimHit]->getWireID().getICLayer()<<endl;
636  }
637  //cout<<"iMCParticle: "<<cdcSimHitRel[iPart].getFromIndex()<<" Last wire Hit: "<<lastWireHit<<endl;
638  // Calculate last superlayer
639  int lastSLHit = 0;
640  if (lastWireHit >= 53) lastSLHit = 9;
641  else if (lastWireHit >= 47) lastSLHit = 8;
642  else if (lastWireHit >= 41) lastSLHit = 7;
643  else if (lastWireHit >= 35) lastSLHit = 6;
644  else if (lastWireHit >= 29) lastSLHit = 5;
645  else if (lastWireHit >= 23) lastSLHit = 4;
646  else if (lastWireHit >= 17) lastSLHit = 3;
647  else if (lastWireHit >= 11) lastSLHit = 2;
648  else if (lastWireHit >= 5) lastSLHit = 1;
649  // Get number of hit TS for particle
650  int iMCParticle = cdcSimHitRel[iPart].getFromIndex();
651  bitset<9> hitSuperLayers(particleNHitTS[iMCParticle]);
652  int numberHitSuperLayers = hitSuperLayers.count();
653  //cout<<"iMCParticle: "<< iMCParticle << " # hit TS: "<<numberHitSuperLayers<<" MC # TS: "<<lastSLHit<<endl;
654  float mcPt = mcParticles[iMCParticle]->getMomentum().Perp();
655  float efficiency;
656  if (lastSLHit == 0) efficiency = -1;
657  else efficiency = float(numberHitSuperLayers) / lastSLHit;
658  //cout<<"Efficiency: "<<float(numberHitSuperLayers)/lastSLHit<<" MC pT: "<<mcPt<<endl;
659  vector<float> tempEfficiency;
660  tempEfficiency.resize(3);
661  tempEfficiency[0] = efficiency;
662  tempEfficiency[1] = mcPt;
663  tempEfficiency[2] = lastSLHit;
664  tsEfficiency.push_back(tempEfficiency);
665  } // End of looping over all particles
667  //for(unsigned iEfficiency=0; iEfficiency<tsEfficiency.size(); iEfficiency++) {
668  // cout<<"Efficiency: "<<tsEfficiency[iEfficiency][0]<<" Pt: "<<tsEfficiency[iEfficiency][1]<<" #MCTS: "<<tsEfficiency[iEfficiency][2]<<endl;
669  //}
670 
671  // Save TS efficiency for each particle
672  for (unsigned iEfficiency = 0; iEfficiency < tsEfficiency.size();
673  iEfficiency++) {
674  TVectorD t_particleEfficiency(3);
675  t_particleEfficiency[0] = tsEfficiency[iEfficiency][0];
676  t_particleEfficiency[1] = tsEfficiency[iEfficiency][1];
677  t_particleEfficiency[2] = tsEfficiency[iEfficiency][2];
678  new(particleEfficiency[iEfficiency]) TVectorD(t_particleEfficiency);
679  }
680 
681  } // End of no MC information
682 
683  // Save TS information
684  // [FIXME] Doesn't work when second only priority is hit.
685  // Loop over all hit TSs
686  int iHitTS = 0;
687  for (int iSuperLayer = 0; iSuperLayer < 9; iSuperLayer++) {
688  for (unsigned iTS = 0; iTS < segmentHitsSL[iSuperLayer].size(); iTS++) {
689  const TCSegment& ts = segmentHitsSL[iSuperLayer][iTS]->segment();
690  //unsigned nWires = ts.wires().size();
691  unsigned iWire = 5;
692  if (iSuperLayer == 0) iWire = 0;
693  const TRGCDCWireHit* wireHit = ts.wires()[iWire]->hit();
694  if (wireHit != 0) {
695  //cout<<"[TSF]: "<<ts.name()<<" was hit at ";
696  unsigned nHits = ts.wires()[iWire]->signal().nSignals();
697  for (unsigned iHit = 0; iHit < nHits; iHit++) {
698  if (iHit % 2 == 1) continue;
699  TVectorD tempTSInformation(3);
700  tempTSInformation[0] = iSuperLayer;
701  tempTSInformation[1] = ts.localId();
702  tempTSInformation[2] = ts.wires()[iWire]->signal().stateChanges()[iHit];
703  new(tsInformation[iHitTS++]) TVectorD(tempTSInformation);
704  //cout<<ts.wires()[iWire]->signal().stateChanges()[iHit]<<", ";
705  //iHit++;
706  }
707  //cout<<endl;
708  } // If wire is hit
709  } // End of loop over all TS in super layer
710  } // End of loop over all hit TSs
711 
712  if (m_makeRootFile) m_treeOutputTSF->Fill();
713 
714  } // End of saving TSF results
715 
716 
717 
719  std::vector<TRGCDCSegment*>& tss)
720  {
721 
722  StoreArray<CDCSimHit> SimHits;
723  if (! SimHits) {
724  //cout << "CDCTRGTSF !!! can not access to CDCSimHits" << endl;
725  return;
726  }
727 
728  TClonesArray& nnPatternInformation = *m_nnPatternInformation;
729  nnPatternInformation.Clear();
730 
731  // Save Timing information in ROOT file. Fill for each TS.
732  // Loop over all TSs. wireHit has only one wireSimHit. Meaning there is a particle overlap hit bug.
733  const unsigned nTSs = tss.size();
734  unsigned indexSaving = 0;
735  for (unsigned iTS = 0; iTS < nTSs; iTS++) {
736  // If TS is hit
737  const TCSegment& ts = * tss[iTS];
738  const TCSHit* tsHit = ts.hit();
739  if (tsHit) {
740  const TRGCDCWire* priority;
741  if (ts.wires().size() == 15) priority = ts.wires()[0];
742  else priority = ts.wires()[5];
743  // If priority wire is hit
744  if (priority->hit()) {
745 
746  // Calculate timeWires
747  // Fill wire timing. Not hit = 9999. Unit is ns.
748  vector<float> wireTime;
749  if (ts.superLayerId() == 0)
750  wireTime.resize(15);
751  else
752  wireTime.resize(11);
753  // Loop over all wires
754  //cout<<ts.name();
755  const unsigned nWires = ts.wires().size();
756  for (unsigned iWire = 0; iWire < nWires; iWire++) {
757  const TRGCDCWire* wire = ts.wires()[iWire];
758  const TRGCDCWireHit* wireHit = ts.wires()[iWire]->hit();
759  // If wire is hit
760  if (wireHit) {
761  // Only check first change. This could become a bug.
762  wireTime[iWire] = wire->signal().stateChanges()[0];
763  //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;
764  } else
765  wireTime[iWire] = 9999;
766  //cout<<" "<<wire->name();
767  } // End loop over all wires
768  //cout<<endl;
770  //for(unsigned iWire = 0; iWire < wireTime.size(); iWire++) {
771  // cout<<"Wire: "<<iWire<<" Time: "<<wireTime[iWire]<<endl;
772  //}
773  // Get additional wire information for 6th layer
774  //cout<<" JB: "<<ts.wires().back()->layerId()<<" "<<ts.wires().back()->localId()<<endl;
775  int lastLayer = ts.wires().back()->layerId();
776  int lastWire = ts.wires().back()->localId();
777  int nWiresLayer = _cdc.layer(lastLayer + 1)->nCells();
778  if (nWires == 15) {
779  for (unsigned iWire = 0; iWire < 6; iWire++) {
780  int wireIndex = lastWire - 4 + iWire;
781  if (wireIndex < 0) wireIndex += nWiresLayer;
782  if (wireIndex >= nWiresLayer) wireIndex -= nWiresLayer;
783  //cout<<"Call: "<<(*_cdc.layer(lastLayer+1))[wireIndex]->localId()<<endl;
784  const TRGCDCCell* wire = (*_cdc.layer(lastLayer + 1))[wireIndex];
785  const TRGCDCCellHit* wireHit = wire->hit();
786  // If wire is hit
787  if (wireHit) {
788  // Only check first change. This could become a bug.
789  wireTime.push_back(wire->signal().stateChanges()[0]);
790  //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;
791  } else
792  wireTime.push_back(9999);
793  } // Loop over all extra wires
794  } else {
795  for (unsigned iWire = 0; iWire < 5; iWire++) {
796  int wireIndex = lastWire - 3 + iWire;
797  if (wireIndex < 0) wireIndex += nWiresLayer;
798  if (wireIndex >= nWiresLayer) wireIndex -= nWiresLayer;
799  //cout<<"Call: "<<(*_cdc.layer(lastLayer+1))[wireIndex]->localId()<<endl;
800  const TRGCDCCell* wire = (*_cdc.layer(lastLayer + 1))[wireIndex];
801  const TRGCDCCellHit* wireHit = wire->hit();
802  // If wire is hit
803  if (wireHit) {
804  // Only check first change. This could become a bug.
805  wireTime.push_back(wire->signal().stateChanges()[0]);
806  //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;
807  } else
808  wireTime.push_back(9999);
809  } // Loop over all extra wires
810  }
811  //ts.wires()[14]->layerId(), localId()
812  //_cdc._layers[0][localId]->signal().stateChanges()[0]
813 
814 
816  //float fastestTime = 9999;
817  //for(unsigned iWire=0; iWire<wireTime.size();iWire++) {
818  // if (fastestTime > wireTime[iWire]) fastestTime = wireTime[iWire];
819  //}
821  //for(unsigned iWire=0; iWire<wireTime.size();iWire++) {
822  // if(wireTime[iWire] != 9999) wireTime[iWire] -= fastestTime;
823  //}
824 
826  //for(unsigned iWire = 0; iWire < wireTime.size(); iWire++) {
827  // cout<<"Wire: "<<iWire<<" Time: "<<wireTime[iWire]<<endl;
828  //}
829  //cout<<ts.name()<<" is found and priority wire is hit"<<endl;
830  // Calculate mc result
831  float mcLRDriftTime = priority->signal().stateChanges()[0];
832  if (priority->hit()->mcLR()) mcLRDriftTime *= -1;
833  //cout<<ts.name()<<" LRDriftTime: "<<mcLRDriftTime<<endl;
834 
835  // Save timing information in Root
836  TVectorD t_nnPatternInformation;
837  if (ts.superLayerId() == 0) {
838  //t_nnPatternInformation.ResizeTo(17);
839  t_nnPatternInformation.ResizeTo(23);
840  t_nnPatternInformation[0] = ts.superLayerId();
841  t_nnPatternInformation[1] = mcLRDriftTime;
842  //for(unsigned iWire=0; iWire<15; iWire++){
843  for (unsigned iWire = 0; iWire < 21; iWire++)
844  t_nnPatternInformation[iWire + 2] = wireTime[iWire];
845  new(nnPatternInformation[indexSaving++]) TVectorD(t_nnPatternInformation);
846  } else {
847  //t_nnPatternInformation.ResizeTo(13);
848  t_nnPatternInformation.ResizeTo(17);
849  t_nnPatternInformation[0] = ts.superLayerId();
850  t_nnPatternInformation[1] = mcLRDriftTime;
851  //for(unsigned iWire=0; iWire<11; iWire++){
852  for (unsigned iWire = 0; iWire < 15; iWire++)
853  t_nnPatternInformation[iWire + 2] = wireTime[iWire];
854  new(nnPatternInformation[indexSaving++]) TVectorD(t_nnPatternInformation);
855  }
856 
857  } // End of if priority cell is hit
858  } // End of if TS is hit
859  } // End loop of all TSs
860 
861  if (m_makeRootFile) m_treeNNTSF->Fill();
862 
863  } // End of save function
864 
865  void
867  {
868  std::vector<const TRGCDCMerger*>::push_back(a);
869  }
870 
871 
872  void
874  {
875 
876  const string sn = "TSF::simulateOuter : " + name();
878 
879  //...Loop over mergers to create a super layer hit map...
880  for (unsigned m = 0; m < nInput(); m++) {
881  TRGSignalBundle* b = input(m)->signal();
882 
883  for (unsigned i = 0; i < 16; i++) {
884  _secMap.push_back(& ((* b)[0][0][208 + i]));
885  for (unsigned j = 0; j < 5; j++)
886  _hitMap[j].push_back(& ((* b)[0][0][j * 16 + i]));
887  for (unsigned j = 0; j < 4; j++)
888  _priMap.push_back(& ((* b)[0][0][80 + i * 4 + j]));
889  for (unsigned j = 0; j < 4; j++)
890  _fasMap.push_back(& ((* b)[0][0][144 + i * 4 + j]));
891  }
892 
893  for (unsigned i = 0; i < 4; i++)
894  _edg0Map.push_back(& ((* b)[0][0][224 + i]));
895  for (unsigned i = 0; i < 4; i++)
896  _edg1Map.push_back(& ((* b)[0][0][224 + 4 + i]));
897  for (unsigned i = 0; i < 4; i++)
898  _edg2Map.push_back(& ((* b)[0][0][224 + 8 + i]));
899  }
900 
901  //...Storage preparation...
902  const unsigned nTSF = nInput() * 16;
903  vector<TRGSignalVector*> trker[4];
904  vector<int> tsfStateChanges;
905 
906  //...Form a TSF...
907  for (unsigned t = 0; t < nTSF; t++) {
908 
909  const string n = name() + "-" + TRGUtilities::itostring(t);
911  _tsfIn.push_back(s);
912 
913  s->push_back(* _secMap[t]);
914 
915  if (t == 0) {
916  s->push_back(* (_hitMap[0][nTSF - 1]));
917  s->push_back(* (_hitMap[0][0]));
918  s->push_back(* (_hitMap[0][1]));
919  s->push_back(* (_hitMap[1][nTSF - 1]));
920  s->push_back(* (_hitMap[1][0]));
921  s->push_back(* (_hitMap[2][0]));
922  s->push_back(* (_hitMap[3][nTSF - 1]));
923  s->push_back(* (_hitMap[3][0]));
924  s->push_back(* (_hitMap[4][nTSF - 1]));
925  s->push_back(* (_hitMap[4][0]));
926  s->push_back(* (_hitMap[4][1]));
927  } else if (t == (nTSF - 1)) {
928  s->push_back(* (_hitMap[0][nTSF - 2]));
929  s->push_back(* (_hitMap[0][nTSF - 1]));
930  s->push_back(* (_hitMap[0][0]));
931  s->push_back(* (_hitMap[1][nTSF - 2]));
932  s->push_back(* (_hitMap[1][nTSF - 1]));
933  s->push_back(* (_hitMap[2][nTSF - 1]));
934  s->push_back(* (_hitMap[3][nTSF - 2]));
935  s->push_back(* (_hitMap[3][nTSF - 1]));
936 
937  s->push_back(* (_hitMap[4][nTSF - 2]));
938  s->push_back(* (_hitMap[4][nTSF - 1]));
939  s->push_back(* (_hitMap[4][0]));
940  } else {
941  s->push_back(* (_hitMap[0][t - 1]));
942  s->push_back(* (_hitMap[0][t]));
943  s->push_back(* (_hitMap[0][t + 1]));
944  s->push_back(* (_hitMap[1][t - 1]));
945  s->push_back(* (_hitMap[1][t]));
946  s->push_back(* (_hitMap[2][t]));
947  s->push_back(* (_hitMap[3][t - 1]));
948  s->push_back(* (_hitMap[3][t]));
949  s->push_back(* (_hitMap[4][t - 1]));
950  s->push_back(* (_hitMap[4][t]));
951  s->push_back(* (_hitMap[4][t + 1]));
952  }
953 
954  //...Priority timing...
955  s->push_back(* _priMap[t * 4 + 0]);
956  s->push_back(* _priMap[t * 4 + 1]);
957  s->push_back(* _priMap[t * 4 + 2]);
958  s->push_back(* _priMap[t * 4 + 3]);
959 
960  //...Priority timing...
961  priorityTiming(t, nTSF, * s, (* s)[6], (* s)[7], (* s)[8]);
962 
963  //...Fastest timing...
964  fastestTimingOuter(t, nTSF, * s);
965 
966  //...Clock counter is omitted...
967 
968  //...Simulate TSF...
969  vector<TRGSignalVector*> result = simulateOuter(s, t);
970  _toBeDeleted.push_back(result[1]); // Event timing omitted now
971 
972  TRGSignalVector* forTracker0 = result[0];
973  TRGSignalVector* forTracker1 = new TRGSignalVector(* forTracker0);
974 
975  _tsfOut.push_back(forTracker0);
976  _tsfOut.push_back(forTracker1);
977 
978 
979  //...State change list...
980  vector<int> sc = forTracker0->stateChanges();
981  for (unsigned i = 0; i < sc.size(); i++) {
982  bool skip = false;
983  for (unsigned j = 0; j < tsfStateChanges.size(); j++) {
984  if (tsfStateChanges[j] == sc[i]) {
985  skip = true;
986  break;
987  }
988  }
989  if (! skip) tsfStateChanges.push_back(sc[i]);
990  }
991 
992  //...Store it for each tracker division...
993  const unsigned pos = t / (nTSF / 4);
994  if (pos == 0) {
995  addID(* forTracker0, t + (nTSF / 4));
996  addID(* forTracker1, t);
997  trker[3].push_back(forTracker0);
998  trker[0].push_back(forTracker1);
999  } else if (pos == 1) {
1000  addID(* forTracker0, t);
1001  addID(* forTracker1, t - 1 * (nTSF / 4));
1002  trker[0].push_back(forTracker0);
1003  trker[1].push_back(forTracker1);
1004  } else if (pos == 2) {
1005  addID(* forTracker0, t - 1 * (nTSF / 4));
1006  addID(* forTracker1, t - 2 * (nTSF / 4));
1007  trker[1].push_back(forTracker0);
1008  trker[2].push_back(forTracker1);
1009  } else {
1010  addID(* forTracker0, t - 2 * (nTSF / 4));
1011  addID(* forTracker1, t - 3 * (nTSF / 4));
1012  trker[2].push_back(forTracker0);
1013  trker[3].push_back(forTracker1);
1014  }
1015 
1016  if (TRGDebug::level())
1017  if (forTracker0->active())
1018  cout << TRGDebug::tab() << "hit found : TSF out="
1019  << t << endl;
1020  }
1021 
1022  //...Sort state changes...
1023  std::sort(tsfStateChanges.begin(), tsfStateChanges.end());
1024 
1025  //...Output for 2D...
1026  for (unsigned i = 0; i < 4; i++) {
1027  string n = name() + "-trker" + TRGUtilities::itostring(i);
1028  TRGSignalVector* tOut = packerOuterTracker(trker[i],
1029  tsfStateChanges,
1030  20);
1031  tOut->name(n);
1032  TRGSignalBundle* b = new TRGSignalBundle(n, clockData());
1033  b->push_back(tOut);
1034  output(i)->signal(b);
1035  _toBeDeleted.push_back(tOut);
1036 
1037  if (TRGCDC::getTRGCDC()->firmwareSimulationMode() & 0x4)
1038  b->dumpCOE("",
1039  TRGCDC::getTRGCDC()->firmwareSimulationStartDataClock(),
1040  TRGCDC::getTRGCDC()->firmwareSimulationStopDataClock());
1041 
1042  // b->dump();
1043  }
1044 
1046  }
1047 
1048  vector <TRGSignalVector*>
1049  TSFinder::simulateOuter(TRGSignalVector* in, unsigned tsid)
1050  {
1051 
1052  //variables for common
1053  const string na = "TSCandidate" + TRGUtilities::itostring(tsid) + " in " +
1054  name();
1055  TCSegment* tsi = _tsSL[tsid];
1056  vector <TRGSignalVector*> result;
1057 
1058  //variables for EvtTime & Low pT
1059  vector<bool> fTimeVect;
1060  // int tmpFTime = 0 ;
1061 
1062  //variables for Tracker & N.N
1063  vector <bool> tmpOutBool;
1064 
1065  //iwTRGSignalVector* resultT = new TRGSignalVector(na, in->clock(),22);
1066  TRGSignalVector* resultT = new TRGSignalVector(na, in->clock(), 13);
1067  TRGSignalVector* resultE = new TRGSignalVector(na, in->clock(), 10);
1068  TRGSignalVector* Hitmap = new TRGSignalVector(na + "HitMap", in->clock(), 0);
1069  TRGSignalVector pTime(na + "PriorityTime", in->clock(), 0);
1070  TRGSignalVector fTime(na + "FastestTime", in->clock(), 0);
1071  for (unsigned i = 0; i < 12; i++) {
1072  Hitmap->push_back((*in)[i]);
1073  (*Hitmap)[i].widen(16);
1074  }
1075  for (unsigned i = 0; i < 4; i++) {
1076  pTime.push_back((*in)[i + 12]);
1077  fTime.push_back((*in)[i + 16]);
1078  }
1079 
1080  //...Clock counter...
1081  const TRGSignalVector& cc = in->clock().clockCounter();
1082  for (unsigned i = 0; i < 5; i++) {
1083  pTime.push_back(cc[i]);
1084  fTime.push_back(cc[i]);
1085  }
1086 
1087  vector <int> changeTime = Hitmap->stateChanges();
1088 
1089  int* LUTValue = new int[changeTime.size()];
1090  if (changeTime.size()) {
1091  int hitPosition = 0;
1092  bool fTimeBool[10];
1093  int tmpPTime = 0 ;
1094  int tmpCTime = 0 ;
1095  int tmpOutInt;
1096  fTime.state(changeTime[0]).copy2bool(fTimeBool);
1097  fTimeBool[9] = true;
1098  fTimeVect.insert(fTimeVect.begin(), fTimeBool, fTimeBool + 10);
1099  //tmpFTime = mkint(fTime.state(changeTime[0]));
1100  bool eOUT = true;
1101  for (unsigned i = 0; i < changeTime.size(); i++) {
1102  LUTValue[i] = tsi->LUT()->getValue(mkint(Hitmap->state(changeTime[i])));
1103 
1105  if ((LUTValue[i]) && (eOUT)) {
1106  resultE->set(fTimeVect, changeTime[i]);
1107  eOUT = false;
1108  }
1109 
1110  bool priority1rise = (*Hitmap)[6].riseEdge(changeTime[i]);
1111  bool priority2rise = ((*Hitmap)[7].riseEdge(changeTime[i]) |
1112  (*Hitmap)[8].riseEdge(changeTime[i]));
1113 
1115  //ready for output
1116  if (priority1rise) {
1117  hitPosition = 3;
1118  tmpPTime = mkint(pTime.state(changeTime[i]));
1119  tmpCTime = changeTime[i];
1120  } else if (priority2rise) {
1121  if (!hitPosition) {
1122  tmpPTime = mkint(pTime.state(changeTime[i]));
1123  tmpCTime = changeTime[i];
1124  if ((*Hitmap)[0].state(changeTime[i])) hitPosition = 2;
1125  else hitPosition = 1;
1126  }
1127  }
1128 
1129  // output selection
1130  if ((hitPosition) && (LUTValue[i]) && ((changeTime[i] - tmpCTime) < 16)) {
1131  //iw tmpOutInt = tsid * pow(2, 13) + tmpPTime * pow(2, 4) +
1132  //iw LUTValue[i] * pow(2,2) + hitPosition;
1133  tmpOutInt = tmpPTime * pow(2, 4) +
1134  LUTValue[i] * pow(2, 2) + hitPosition;
1135  tmpOutBool = mkbool(tmpOutInt, 13); // ID removed : iw
1136  if (hitPosition == 3) {
1137  if (priority1rise) resultT->set(tmpOutBool, changeTime[i]);
1138  else {
1139  if ((LUTValue[i] == 1) | (LUTValue[i] == 2)) {
1140  if (!((LUTValue[i - 1] == 1) |
1141  (LUTValue[i - 1] == 2)))
1142  resultT->set(tmpOutBool, changeTime[i]);
1143  } else {
1144  if (!(LUTValue[i - 1])) resultT->set(tmpOutBool, changeTime[i]);
1145  }
1146  }
1147  } else {
1148  if (priority2rise) resultT->set(tmpOutBool, changeTime[i]);
1149  else {
1150  if ((LUTValue[i] == 1) | (LUTValue[i] == 2)) {
1151  if (!((LUTValue[i - 1] == 1) |
1152  (LUTValue[i - 1] == 2)))
1153  resultT->set(tmpOutBool, changeTime[i]);
1154  } else {
1155  if (!(LUTValue[i])) resultT->set(tmpOutBool, changeTime[i]);
1156  }
1157  }
1158  }
1159  }
1160 
1161  }
1162 
1163  }
1164  result.push_back(resultT);
1165  result.push_back(resultE);
1166 
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  TSFinder::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*>
2421  TSFinder::simulateTSFOld(TRGSignalVector* in, unsigned tsid)
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) | 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  delete [] LUTValue;
2580  delete Hitmap;
2581 
2582  return result;
2583  }
2584 
2585  vector <TRGSignalVector*>
2586  TSFinder::simulateTSF(TRGSignalVector* in, unsigned tsid)
2587  {
2588 
2589  //variables for common
2590  const string na = "TSF" + TRGUtilities::itostring(tsid) + " in " +
2591  name();
2592  TCSegment* tsi = _tsSL[tsid];
2593  vector <TRGSignalVector*> result;
2594 
2595  //variables for EvtTime & Low pT
2596  vector<bool> fTimeVect;
2597  // int tmpFTime = 0 ;
2598 
2599  //variables for Tracker & N.N
2600  vector <bool> tmpOutBool;
2601 
2602  TRGSignalVector* resultT = new TRGSignalVector(na, in->clock(), 13);
2603  TRGSignalVector* resultE = new TRGSignalVector(na, in->clock(), 10);
2604  TRGSignalVector* Hitmap = new TRGSignalVector(na + "HitMap",
2605  in->clock(),
2606  0);
2607  TRGSignalVector pTime(na + "PriorityTime", in->clock(), 0);
2608  TRGSignalVector fTime(na + "FastestTime", in->clock(), 0);
2609  TRGSignal* pri0 = 0;
2610  TRGSignal* pri1 = 0;
2611  TRGSignal* pri2 = 0;
2612  if (_type == innerType) {
2613  for (unsigned i = 0; i < 16; i++) {
2614  Hitmap->push_back((* in)[i]);
2615  (* Hitmap)[i].widen(16);
2616  }
2617  for (unsigned i = 0; i < 4; i++) {
2618  pTime.push_back((* in)[i + 16]);
2619  fTime.push_back((* in)[i + 20]);
2620  }
2621  pri0 = & (*Hitmap)[1];
2622  pri1 = & (*Hitmap)[2];
2623  pri2 = & (*Hitmap)[3];
2624  } else {
2625  for (unsigned i = 0; i < 12; i++) {
2626  Hitmap->push_back((* in)[i]);
2627  (* Hitmap)[i].widen(16);
2628  }
2629  for (unsigned i = 0; i < 4; i++) {
2630  pTime.push_back((* in)[i + 12]);
2631  fTime.push_back((* in)[i + 16]);
2632  }
2633  pri0 = & (*Hitmap)[6];
2634  pri1 = & (*Hitmap)[7];
2635  pri2 = & (*Hitmap)[8];
2636  }
2637 
2638  //...Clock counter...
2639  const TRGSignalVector& cc = in->clock().clockCounter();
2640  for (unsigned i = 0; i < 5; i++) {
2641  pTime.push_back(cc[i]);
2642  fTime.push_back(cc[i]);
2643  }
2644 
2645  vector<int> changeTime = Hitmap->stateChanges();
2646 
2647  int* LUTValue = new int[changeTime.size()];
2648  //int oldLUT = 0;
2649  int lastFastHit = in->clock().min();
2650  if (changeTime.size()) {
2651 
2652  int oldLUT = 0; // 2019/07/31 by ytlai, moved to here to reduce scope
2653  const string fn = "TSF::simulateTSF:tsid=" + to_string(tsid);
2655 
2656  int hitPosition = 0;
2657  int tmpPTime = 0 ;
2658  int tmpCTime = 0 ;
2659  int tmpOutInt;
2660  //TRGState ft(10); //TODO variable not used at all
2661  bool eOut = false;
2662  for (unsigned i = 0; i < changeTime.size(); i++) {
2663 
2664  int ct = changeTime[i];
2665  TRGState st = Hitmap->state(ct);
2666 
2667  //...LUT...
2668  // 0:no hit, 1:right, 2:left, 3:LR unknown hit
2669  LUTValue[i] = tsi->LUT()->getValue(unsigned(st));
2670 
2671  //...Any wire hit (for the fastest timing)...
2672  bool active = st.active();
2673  if (active) {
2674  const int timeCounter = ct - lastFastHit;
2675 
2676  //...Record as the fastest timing hit...
2677  if (timeCounter > 15) {
2678  lastFastHit = ct;
2679  TRGState ftnow = fTime.state(ct);
2680  ftnow += TRGState(1, 1);
2681  //ft = ftnow;
2682  eOut = true;
2683  }
2684  }
2685 
2687  if ((LUTValue[i])) {
2688  if (eOut) {
2689  resultE->set(fTimeVect, ct);
2690  eOut = false;
2691  }
2692  }
2693 
2694  bool priority1rise = pri0->riseEdge(ct);
2695  bool priority2rise = pri1->riseEdge(ct) | pri2->riseEdge(ct);
2696 
2698  //ready for output
2699  if (priority1rise) {
2700  hitPosition = 3;
2701  tmpPTime = unsigned(pTime.state(ct));
2702  tmpCTime = ct;
2703  } else if (priority2rise) {
2704  if (!hitPosition) {
2705  tmpPTime = unsigned(pTime.state(ct));
2706  tmpCTime = ct;
2707  if ((*Hitmap)[0].state(ct)) hitPosition = 2;
2708  else hitPosition = 1;
2709  }
2710  }
2711 
2712  // output selection
2713  if ((ct - tmpCTime) < 16) {
2714  if ((hitPosition) && (LUTValue[i])) {
2715  //iw tmpOutInt = tsid * pow(2, 13) + tmpPTime * pow(2, 4) +
2716  //iw LUTValue[i] * pow(2,2) + hitPosition;
2717  tmpOutInt = tmpPTime * pow(2, 4) +
2718  LUTValue[i] * pow(2, 2) + hitPosition;
2719  tmpOutBool = mkbool(tmpOutInt, 13); // ID removed : iw
2720 
2721  if (hitPosition == 3) {
2722  if (priority1rise) {
2723  resultT->set(tmpOutBool, ct);
2724  oldLUT = LUTValue[i];
2725  } else {
2726  if ((LUTValue[i] == 1) | (LUTValue[i] == 2)) {
2727  if (!((oldLUT == 1) |
2728  (oldLUT == 2))) {
2729  resultT->set(tmpOutBool, ct);
2730  oldLUT = LUTValue[i];
2731  }
2732  } else {
2733  if (!(LUTValue[i - 1])) {
2734  resultT->set(tmpOutBool, ct);
2735  oldLUT = LUTValue[i];
2736  }
2737  }
2738  }
2739  } else {
2740  if ((priority2rise) && (!oldLUT)) resultT->set(tmpOutBool, ct);
2741  else {
2742  if ((LUTValue[i] == 1) | (LUTValue[i] == 2)) {
2743  if (!((oldLUT == 1) |
2744  (oldLUT == 2))) {
2745  resultT->set(tmpOutBool, ct);
2746  oldLUT = LUTValue[i];
2747  }
2748  } else {
2749  if (!(oldLUT)) {
2750  resultT->set(tmpOutBool, ct);
2751  oldLUT = LUTValue[i];
2752  }
2753  }
2754  }
2755  }
2756  }
2757  } else {
2758  oldLUT = 0;
2759  hitPosition = 0;
2760  }
2761 
2762  if (TRGDebug::level() > 1) {
2763  cout << TRGDebug::tab() << "clk=" << ct
2764  << ", pattern=" << st
2765  << ", LUT=" << LUTValue[i]
2766  << ", pri=" << priority1rise
2767  << ", sec=" << priority2rise
2768  << ", hitPos=" << hitPosition
2769  << ", (clk-tmpCTime)=" << (ct - tmpCTime)
2770  << endl;
2771  }
2772  }
2774  }
2775 
2776  if (TRGDebug::level()) {
2777  vector<int> sc = resultT->stateChanges();
2778  vector<unsigned> lv;
2779  for (unsigned i = 0; i < sc.size(); i++) {
2780  //...LUT value...
2781  unsigned l = unsigned(resultT->state(sc[i]).subset(2, 2));
2782  lv.push_back(l);
2783  cout << "clk=" << sc[i] << " LUT output[" << i << "]=" << l << endl;
2784  }
2785  bool found1or2 = false;
2786  bool found3 = false;
2787  for (unsigned i = 0; i < lv.size(); i++) {
2788  if (found1or2 && (lv[i] > 0))
2789  cout << "!!! simulateTSF something wrong(found1or2)" << endl;
2790  if (found3 && (lv[i] == 3))
2791  cout << "!!! simulateTSF something wrong(found3)" << endl;
2792 
2793  if ((lv[i] == 1) & (lv[i] == 2))
2794  found1or2 = true;
2795  else if (lv[i] == 3)
2796  found3 = true;
2797  }
2798  }
2799 
2800  result.push_back(resultT);
2801  result.push_back(resultE);
2802 
2803  delete [] LUTValue;
2804  delete Hitmap;
2805 
2806  return result;
2807  }
2808  vector <TRGSignalVector*>
2809  TSFinder::simulateTSF2(TRGSignalVector* in, unsigned tsid)
2810  {
2811 
2812  //variables for common
2813  const string na = "TSF" + TRGUtilities::itostring(tsid) + " in " +
2814  name();
2815  TCSegment* tsi = _tsSL[tsid];
2816  vector <TRGSignalVector*> result;
2817 
2818  //variables for EvtTime & Low pT
2819  vector<bool> fTimeVect;
2820  // int tmpFTime = 0 ;
2821 
2822  //variables for Tracker & N.N
2823  TRGSignalVector* resultT = new TRGSignalVector(na, in->clock(), 13);
2824  TRGSignalVector* resultE = new TRGSignalVector(na, in->clock(), 10);
2825  TRGSignalVector* Hitmap = new TRGSignalVector(na + "HitMap",
2826  in->clock(),
2827  0);
2828  TRGSignalVector pTime(na + "PriorityTime", in->clock(), 0);
2829  TRGSignalVector fTime(na + "FastestTime", in->clock(), 0);
2830  TRGSignal* pri0 = 0;
2831  TRGSignal* pri1 = 0;
2832  TRGSignal* pri2 = 0;
2833 
2834  if (_type == innerType) {
2835  for (unsigned i = 0; i < 16; i++) {
2836  Hitmap->push_back((* in)[i]);
2837  (* Hitmap)[i].widen(16);
2838  }
2839  for (unsigned i = 0; i < 4; i++) {
2840  pTime.push_back((* in)[i + 16]);
2841  fTime.push_back((* in)[i + 20]);
2842  }
2843  pri0 = & (*Hitmap)[1];
2844  pri1 = & (*Hitmap)[2];
2845  pri2 = & (*Hitmap)[3];
2846  } else {
2847  for (unsigned i = 0; i < 12; i++) {
2848  Hitmap->push_back((* in)[i]);
2849  (* Hitmap)[i].widen(16);
2850  }
2851  for (unsigned i = 0; i < 4; i++) {
2852  pTime.push_back((* in)[i + 12]);
2853  fTime.push_back((* in)[i + 16]);
2854  }
2855  pri0 = & (*Hitmap)[6];
2856  pri1 = & (*Hitmap)[7];
2857  pri2 = & (*Hitmap)[8];
2858  }
2859 
2860  //...Clock counter...
2861  const TRGSignalVector& cc = in->clock().clockCounter();
2862  for (unsigned i = 0; i < 5; i++) {
2863  pTime.push_back(cc[i]);
2864  fTime.push_back(cc[i]);
2865  }
2866 
2867  vector<int> changeTime = Hitmap->stateChanges();
2868 
2869  vector<unsigned> luts;
2870  int lastFastHit = in->clock().min();
2871 
2872  if (changeTime.size()) {
2873 
2874  const string fn = "TSF::simulateTSF2:tsid=" + to_string(tsid);
2876 
2877  //TRGState ft(10); //not used, should be?
2878  bool eOut = false;
2879 
2880  unsigned stateHitPos = 0; // 0:wait, 1:active1st, 2:active2nd
2881  unsigned hitPos = 0;
2882  unsigned timing = 0;
2883  int timePosFound = 0;
2884  int counterPos = 0;
2885 
2886  unsigned stateLR = 0; // 0:wait, 1:LUT12, 2:LUT3
2887 // int timeLRFound = 0;
2888 // int counterLR = 0;
2889 
2890  for (unsigned i = 0; i < changeTime.size(); i++) {
2891 
2892  const int ct = changeTime[i];
2893  const TRGState st = Hitmap->state(ct);
2894 
2895  //...LUT...
2896  // 0:no hit, 1:right, 2:left, 3:LR unknown hit
2897  const unsigned lut = tsi->LUT()->getValue(unsigned(st));
2898  luts.push_back(lut);
2899 
2900  //...Any wire hit (for the fastest timing)...
2901  const bool active = st.active();
2902  if (active) {
2903  const int timeCounter = ct - lastFastHit;
2904 
2905  //...Record as the fastest timing hit...
2906  if (timeCounter > 15) {
2907  lastFastHit = ct;
2908  TRGState ftnow = fTime.state(ct);
2909  ftnow += TRGState(1, 1);
2910  //ft = ftnow;
2911  eOut = true;
2912  }
2913  }
2914 
2915  // output for EvtTime & Low pT tracker module
2916  if (lut) {
2917  if (eOut) {
2918  resultE->set(fTimeVect, ct);
2919  eOut = false;
2920  }
2921  }
2922 
2923  //...Hit position...
2924  const bool hit1st = pri0->riseEdge(ct);
2925  const bool hit2nd0 = pri1->riseEdge(ct);
2926  const bool hit2nd1 = pri2->riseEdge(ct);
2927  const bool hit2nd = hit2nd0 || hit2nd1;
2928 
2929  //...Hit position state machine...
2930 
2931  //...State : wait...
2932  if (stateHitPos == 0) {
2933  hitPos = 0;
2934  timing = 0;
2935  timePosFound = 0;
2936  if (hit1st) {
2937  hitPos = 3;
2938  timing = unsigned(pTime.state(ct));
2939  timePosFound = ct;
2940  stateHitPos = 1;
2941  } else if (hit2nd) {
2942  if ((* Hitmap)[0].state(ct))
2943  hitPos = 2;
2944  else
2945  hitPos = 1;
2946  timing = unsigned(pTime.state(ct));
2947  timePosFound = ct;
2948  stateHitPos = 2;
2949  }
2950  }
2951 
2952  //...State : active1st...
2953  else if (stateHitPos == 1) {
2954  counterPos = ct - timePosFound;
2955  if (counterPos > 15)
2956  stateHitPos = 0;
2957  }
2958 
2959  //...State : active2nd...
2960  else if (stateHitPos == 2) {
2961  if (hit1st) {
2962  hitPos = 3;
2963  timing = unsigned(pTime.state(ct));
2964  timePosFound = ct;
2965  stateHitPos = 1;
2966  } else {
2967  counterPos = ct - timePosFound;
2968  if (counterPos > 15)
2969  stateHitPos = 0;
2970  }
2971  }
2972 
2973  //...State unknown...
2974  else {
2975  cout << "TSF::simulateTSF2 !!! strange state in hit position"
2976  << endl;
2977  }
2978 
2979  //...L/R decision state machine...
2980 
2981  //...State : wait...
2982  if (stateLR == 0) {
2983  if (lut != 0) {
2984  const unsigned val = (timing << 4) | (lut << 2) | hitPos;
2985  const TRGState output(13, val);
2986  resultT->set(output, ct);
2987 // timeLRFound = ct;
2988  if ((lut == 1) || (lut == 2))
2989  stateLR = 1;
2990  else
2991  stateLR = 2;
2992 
2993  if (TRGDebug::level()) {
2994  if (((lut == 1) || (lut == 2)) && (stateHitPos == 0))
2995  cout << TRGDebug::tab()
2996  << "!!! state machines incosistent" << endl;
2997  }
2998  }
2999  }
3000 
3001  //...State : LUT12...
3002  else if (stateLR == 1) {
3003 // counterLR = ct - timeLRFound;
3004  if (counterPos > 15)
3005  stateLR = 0;
3006  }
3007 
3008  //...State : LUT3...
3009  else if (stateLR == 2) {
3010  if (lut) {
3011  if ((lut == 1) || (lut == 2)) {
3012  const unsigned val = (timing << 4) | (lut << 2) |
3013  hitPos;
3014  const TRGState output(13, val);
3015  resultT->set(output, ct);
3016 // timeLRFound = ct;
3017  stateLR = 1;
3018  } else {
3019 // counterLR = ct - timeLRFound;
3020  if (counterPos > 15)
3021  stateLR = 0;
3022  }
3023  }
3024  }
3025 
3026  //...State unknown...
3027  else {
3028  cout << "TSF::simulateTSF2 !!! strange state in L/R decision"
3029  << endl;
3030  }
3031 
3032  if (TRGDebug::level() > 1) {
3033  cout << TRGDebug::tab() << "clk=" << ct
3034  << ", ptn=" << st
3035  << ", LUT=" << lut
3036  << ", pri=" << hit1st
3037  << ", sec=" << hit2nd
3038  << ", hPos=" << hitPos
3039  << ", ctr=" << counterPos // << "," << counterLR
3040  << ", states=" << stateHitPos << "," << stateLR
3041  << endl;
3042  }
3043  }
3045  }
3046 
3047  if (TRGDebug::level()) {
3048  vector<int> sc = resultT->stateChanges();
3049  vector<unsigned> lv;
3050  for (unsigned i = 0; i < sc.size(); i++) {
3051  //...LUT value...
3052  unsigned l = unsigned(resultT->state(sc[i]).subset(2, 2));
3053  lv.push_back(l);
3054  cout << "clk=" << sc[i] << " LUT output[" << i << "]=" << l << endl;
3055  }
3056  }
3057 
3058  result.push_back(resultT);
3059  result.push_back(resultE);
3060 
3061  delete Hitmap;
3062 
3063  return result;
3064  }
3065 
3067 } // namespace Belle2
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.
TRGSignalVector * packerOuterTracker(vector< TRGSignalVector * > &, vector< int > &, const unsigned)
Packing output for tracker.
vector< TRGSignalVector * > simulateTSF(TRGSignalVector *in, unsigned id)
Simulate TSF response (unified version, 2016/07/12)
std::vector< TRGSignal * > _edg0Map
Internal data of the edge timing information.
TRGSignalVector * packerForTracker(vector< TRGSignalVector * > &, vector< int > &, const unsigned)
Output packer for tracker.
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, ..., ...]
vector< TRGSignalVector * > simulateTSF2(TRGSignalVector *in, unsigned id)
Simulate TSF response (unified version, state machine)
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< 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
static std::string itostring(int i)
converts int to string. (Use boost::lexical_cast)
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:190
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 state(int clockPosition) const
returns state at given clock position.
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
const CDCHit * hit(void) const
Access to CDCHit.
Definition: CellHit.cc:133
void push_back(const TRGCDCMerger *)
push back the Mergers of this TSF
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
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
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
unsigned nCells(void) const
returns # of cells.
Definition: Layer.h:191
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.
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.
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.