Belle II Software  release-06-02-00
TRGGDL.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 #define TRG_SHORT_NAMES
10 #define TRGGDL_SHORT_NAMES
11 
12 #include <TRandom.h>
13 #include <fstream>
14 #include <stdio.h>
15 #include "trg/trg/Debug.h"
16 #include "trg/trg/Time.h"
17 #include "trg/trg/State.h"
18 #include "trg/trg/Signal.h"
19 #include "trg/trg/Utilities.h"
20 #include "trg/gdl/TRGGDL.h"
21 // framework - DataStore
22 #include <framework/datastore/StoreObjPtr.h>
23 
24 #include <mdst/dataobjects/TRGSummary.h>
25 #include <trg/grl/dataobjects/TRGGRLInfo.h>
26 
27 #include <framework/logging/Logger.h>
28 
29 #include <TH1I.h>
30 
31 #include <stdexcept>
32 
33 #define N_TIMING_REGISTERS 4
34 
35 using namespace std;
36 
37 namespace Belle2 {
43  void ftd_0_01(bool* b, bool* i);
45  //void dotrigger(std::vector<int>& res, std::vector<int> ob);
46 
47  TRGGDL*
48  TRGGDL::_gdl = 0;
49 
50  void
51  (* TRGGDL::_ftd)(bool* b, bool* i) = 0;
52 
53  string
54  TRGGDL::name(void) const
55  {
56  return "TRGGDL";
57  }
58 
59  string
60  TRGGDL::version(void) const
61  {
62  return string("TRGGDL 0.01");
63  }
64 
65  TRGGDL*
66  TRGGDL::getTRGGDL(const string& configFile,
67  unsigned simulationMode,
68  unsigned fastSimulationMode,
69  unsigned firmwareSimulationMode,
70  const std::string& Phase,
71  bool algFromDB,
72  const std::string& algFilePath,
73  int debugLevel,
74  double timquality_threshold_sfin,
75  double timquality_threshold_fine)
76  {
77  if (_gdl) {
78  //delete _gdl;
79  _gdl = 0;
80  }
81 
82  if (configFile != "good-bye") {
83  _gdl = new TRGGDL(configFile,
84  simulationMode,
85  fastSimulationMode,
86  firmwareSimulationMode,
87  Phase,
88  algFromDB,
89  algFilePath,
90  debugLevel,
91  timquality_threshold_sfin,
92  timquality_threshold_fine);
93  } else {
94  cout << "TRGGDL::getTRGGDL ... good-bye" << endl;
95  // delete _gdl;
96  _gdl = 0;
97  }
98 
99  return _gdl;
100  }
101 
102  TRGGDL*
103  TRGGDL::getTRGGDL(void)
104  {
105  if (! _gdl)
106  cout << "TRGGDL::getTRGGDL !!! TRGGDL is not created yet" << endl;
107  return _gdl;
108  }
109 
110  TRGGDL::TRGGDL(const string& configFile,
111  unsigned simulationMode,
112  unsigned fastSimulationMode,
113  unsigned firmwareSimulationMode,
114  const std::string& Phase,
115  bool algFromDB,
116  const std::string& algFilePath,
117  int debugLevel,
118  double timquality_threshold_sfin,
119  double timquality_threshold_fine)
120  : _debugLevel(debugLevel),
121  _configFilename(configFile),
122  _simulationMode(simulationMode),
123  _fastSimulationMode(fastSimulationMode),
124  _firmwareSimulationMode(firmwareSimulationMode),
125  _Phase(Phase),
126  _algFilePath(algFilePath),
127  _clock(Belle2_GDL::GDLSystemClock),
128  _offset(15.3),
129  _isb(0),
130  _osb(0),
131  _timquality_threshold_sfin(timquality_threshold_sfin),
132  _timquality_threshold_fine(timquality_threshold_fine),
133  _algFromDB(algFromDB)
134  {
135 
136  if (TRGDebug::level()) {
137  cout << "TRGGDL ... TRGGDL initializing with " << _configFilename
138  << endl
139  << " mode=0x" << hex << _simulationMode << dec << endl;
140  }
141 
142  initialize();
143 
144  if (TRGDebug::level()) {
145  cout << "TRGGDL ... TRGGDL created with " << _configFilename << endl;
146  _clock.dump();
147  }
148 
149  for (int i = 0; i < m_InputBitsDB->getninbit(); i++) {
150  _inpBitNames.push_back(std::string(m_InputBitsDB->getinbitname(i)));
151  }
152 
153  for (int i = 0; i < m_FTDLBitsDB->getnoutbit(); i++) {
154  _oupBitNames.push_back(std::string(m_FTDLBitsDB->getoutbitname(i)));
155  }
156 
157  }
158 
159  void
161  {
162  if (_debugLevel > 19) {
163  for (int i = 0; i < m_InputBitsDB->getninbit(); i++) {
164  B2DEBUG(20, "TRGGDL::initialize, inputBits: " << i << ", " << m_InputBitsDB->getinbitname(i));
165  }
166  for (int i = 0; i < m_FTDLBitsDB->getnoutbit(); i++) {
167  B2DEBUG(20, "TRGGDL::initialize, outputBits: " << i << ", " << m_FTDLBitsDB->getoutbitname(i));
168  }
169  for (int i = 0; i < m_AlgsDB->getnalgs(); i++) {
170  B2DEBUG(20, "TRGGDL::initialize, algs: " << i << ", " << m_AlgsDB->getalg(i));
171  }
172  }
173  //if it is firmware simulation, do the cofigurnation
174  //fastsimulation doesn't use the configuration currently
175  if (_simulationMode == 2) configure();
176  }
177 
178  void
180  {
181  }
182 
183  void
184  TRGGDL::dump(const string& msg) const
185  {
186  if (msg.find("name") != string::npos ||
187  msg.find("version") != string::npos ||
188  msg.find("detail") != string::npos ||
189  msg == "") {
190  }
191  if (msg.find("detail") != string::npos ||
192  msg.find("state") != string::npos) {
193  cout << "Debug Level=" << _debugLevel;
194  cout << endl;
195  }
196  }
197 
198  void
200  {
201  }
202 
203  void
205  {
206  }
207 
208  void
210  {
211  /*
212  TRGDebug::enterStage("TRGGDL update");
213 
214  if (TRGDebug::level())
215  cout << TRGDebug::tab() << name() << " do nothing..." << endl;
216 
217  TRGDebug::leaveStage("TRGGDL update");
218  */
219  }
220 
222  {
223  clear();
224  }
225 
226  void
228  {
229  const bool fast = (_simulationMode == 1);
230  const bool firm = (_simulationMode == 2);
231  const bool data = (_simulationMode == 3);
232  if (fast)
233  fastSimulation();
234  if (firm)
236  if (data)
237  dataSimulation();
238  }
239 
240  void
242  {
243  B2DEBUG(20, "TRGGDL::fastSimulation starts.");
244  TRGDebug::enterStage("TRGGDL fastSim");
245 
246  if (TRGDebug::level())
247  cout << TRGDebug::tab() << name() << " do nothing..." << endl;
248 
249  TRGDebug::leaveStage("TRGGDL fastSim");
250 
251  if (!m_InputBitsDB) B2INFO("no database of gdl input bits");
252  int N_InputBits = m_InputBitsDB->getninbit();
253  if (!m_FTDLBitsDB) B2INFO("no database of gdl ftdl bits");
254  int N_OutputBits = m_FTDLBitsDB->getnoutbit();
255  if (!m_PrescalesDB) B2INFO("no database of gdl prescale");
256  int N_AlgsBits = m_AlgsDB->getnalgs();
257  if (!m_AlgsDB) B2INFO("no global database of gdl ftd logics");
258  if (N_OutputBits > N_AlgsBits) {
259  // Why this? This creates a problem in for loops later.
260  B2DEBUG(20, "#Algs and #Ftdl is different");
261  N_OutputBits = N_AlgsBits;
262  }
263 
264  StoreObjPtr<TRGSummary> GDLResult;
265  if (GDLResult) {
266  B2WARNING("TRGGDL::fastSimulation(): TRGSummary exist already, check it!!!!");
267  return;
268  } else {
269  GDLResult.create();
270  StoreObjPtr<TRGGRLInfo> grlinfo("TRGGRLObjects");
271  if (!grlinfo) {
272  B2WARNING("TRGGRLInfo doesn't exist!!!!");
273  return;
274  } else {
275  if (_debugLevel > 89) printf("TRGGDL:TRGGRLInfo found.\n");
276  }
277 
278  _inpBits.clear();
279  int input_summary = 0;
280  for (int i = 0; i < N_InputBits; i++) {
281  if (_debugLevel > 89) printf("TRGGDL:ABC:i(%d)\n", i);
282  if ((i % 32) == 0) {
283  if (i > 0) {
284  GDLResult->setInputBits(i / 32 - 1, input_summary);
285  }
286  input_summary = 0;
287  }
288  if (grlinfo->getInputBits(i)) input_summary |= (1 << (i % 32));
289  _inpBits.push_back(grlinfo->getInputBits(i));
290  if (i == N_InputBits - 1) {
291  GDLResult->setInputBits(i / 32, input_summary);
292  }
293  }
294 
295  int L1Summary = 0;
296  int L1Summary_psnm = 0;
297  std::string str;
298  std::vector<std::string> algs;
299  std::ifstream isload(_algFilePath.c_str(), std::ios::in);
300  int index = 0;
301  while (std::getline(isload, str)) {
302  algs.push_back(str);
303  index++;
304  }
305  isload.close();
306 
307  for (int i = 0; i < N_OutputBits; i++) {
308  if (_debugLevel > 89) printf("TRGGDL:ABB:i(%d)\n", i);
309  if ((i % 32) == 0) {
310  if (i > 0) {
311  GDLResult->setFtdlBits(i / 32 - 1, L1Summary);
312  GDLResult->setPsnmBits(i / 32 - 1, L1Summary_psnm);
313  }
314  L1Summary = 0;
315  L1Summary_psnm = 0;
316  }
317  std::string alg = _algFromDB ? m_AlgsDB->getalg(i) : algs[i];
318  if (_debugLevel > 89) printf("TRGGDL:i(%d), alg(%s)\n", i, m_AlgsDB->getalg(i).c_str());
319  if (isFiredFTDL(_inpBits, alg)) {
320  L1Summary |= (1 << (i % 32));
321  if (doprescale(m_PrescalesDB->getprescales(i))) {
322  L1Summary_psnm |= (1 << (i % 32));
323  }
324  }
325  GDLResult->setPreScale((i / 32), (i % 32), m_PrescalesDB->getprescales(i));
326  if (i == N_OutputBits - 1) {
327  GDLResult->setFtdlBits(i / 32, L1Summary);
328  GDLResult->setPsnmBits(i / 32, L1Summary_psnm);
329  }
330  }
331 
332  //set timing qulaity flag based on MC truth jitter, which is NOT simulated by TSIM
333  TRGSummary::ETimingQuality timQuality = TRGSummary::TTYQ_NONE;
334  double jitter = 0;
335 
336  //get true jitter if SimClockState is valid
337  if (m_simClockState.isValid() && m_hwClock.isValid()) {
338 
339  jitter = m_simClockState->getRelativeBucketNo() / m_hwClock->getAcceleratorRF(); // in ns
340 
341  //set timing quality flag with the jitter
342  if (abs(jitter) < _timquality_threshold_sfin)
343  timQuality = TRGSummary::TTYQ_SFIN;
344  else {
345  if (abs(jitter) < _timquality_threshold_fine)
346  timQuality = TRGSummary::TTYQ_FINE;
347  else
348  timQuality = TRGSummary::TTYQ_CORS;
349  }
350  }
351 
352  B2DEBUG(20, "TRGGDL::set timing quality, jitter = " << jitter << ": timQuality = " << timQuality << " sfin threshold = " <<
353  _timquality_threshold_sfin << " fine threshold = " <<
355 
356  //fill the flag to TRGSummary
357  GDLResult->setTimQuality(timQuality);
358  }
359  }
360 
361  void
363  {
364 // B2DEBUG(20,"TRGGDL::dataSimulation starts.");
366  /*
367  unsigned _exp = bevt->getExperiment();
368  unsigned _run = bevt->getRun();
369  */
370  if (_debugLevel > 9) printf("TRGGDL:dataSimulation Start\n");
371  unsigned _evt = bevt->getEvent();
372 // TRGDebug::enterStage("TRGGDL dataSim");
373 
374 // if (TRGDebug::level())
375 // cout << TRGDebug::tab() << name() << " do nothing..." << endl;
376 
377  if (!m_InputBitsDB) B2INFO("no database of gdl input bits");
378  int N_InputBits = m_InputBitsDB->getninbit();
379  if (!m_FTDLBitsDB) B2INFO("no database of gdl ftdl bits");
380  int N_OutputBits = m_FTDLBitsDB->getnoutbit();
381  if (!m_PrescalesDB) B2INFO("no database of gdl prescale");
382  int N_AlgsBits = m_AlgsDB->getnalgs();
383  if (!m_AlgsDB) B2INFO("no database of gdl ftdl bit logic");
384  if (N_OutputBits > N_AlgsBits) {
385  B2DEBUG(20, "#Algs and #FTDL is different");
386  N_OutputBits = N_AlgsBits;
387  }
388 
389  if (_debugLevel > 19)
390  printf("TRGGDL:N_InputBits(%d), N_OutputBits(%d)\n", N_InputBits, N_OutputBits);
391 
392  StoreObjPtr<TRGSummary> GDLResult;
393  if (! GDLResult) {
394  B2WARNING("TRGGDL::dataSimulation(): TRGSummary not found. Check it!!!!");
395  return;
396  } else {
397 
398  if (_debugLevel > 89) printf("TRGGDL:TRGSummary Found.\n");
399  _inpBits.clear();
400  _ftdBits.clear();
401  _psnBits.clear();
402  for (int i = 0; i < N_InputBits; i++) {
403  bool inputBit;
404  try { inputBit = GDLResult->testInput(i); }
405  catch (const std::exception&) { inputBit = false; }
406  _inpBits.push_back(inputBit);
407  }
408 
409  if (_algFromDB) {
410  for (int i = 0; i < N_OutputBits; i++) {
411  bool ftdl_fired = isFiredFTDL(_inpBits, m_AlgsDB->getalg(i));
412  bool psnm_fired = false;
413  _ftdBits.push_back(ftdl_fired);
414  if (ftdl_fired) {
415  if (doprescale(m_PrescalesDB->getprescales(i))) {
416  psnm_fired = true;
417  }
418  }
419  psnm_fired ? _psnBits.push_back(true) : _psnBits.push_back(false);
420  GDLResult->setPreScale((i / 32), (i % 32), m_PrescalesDB->getprescales(i));
421  if (! strcmp(m_FTDLBitsDB->getoutbitname(i), "hie")) {
422  if (_debugLevel == 971 && ftdl_fired) {
423  int i_ehigh = m_InputBitsDB->getinbitnum("ehigh");
424  int i_bha_veto = m_InputBitsDB->getinbitnum("bha_veto");
425  printf("TRGGDL:hie:i=%d,evt=%u,ps=%d,ehigh=%d,bha_veto=%d,ftdl_fired=%d,psnm_fired=%d,i_ehigh=%d,i_bha_veto=%d,obitname=%s\n",
426  i, _evt, m_PrescalesDB->getprescales(i),
427  _inpBits[i_ehigh] ? 1 : 0,
428  _inpBits[i_bha_veto] ? 1 : 0,
429  ftdl_fired,
430  psnm_fired ? 1 : 0,
431  i_ehigh,
432  i_bha_veto,
433  m_FTDLBitsDB->getoutbitname(i));
434  }
435  }
436  }
437 
438  } else {
439  std::string str;
440  std::vector<std::string> algs;
441  std::ifstream isload(_algFilePath.c_str(), std::ios::in);
442  int index = 0;
443  while (std::getline(isload, str)) {
444  algs.push_back(str);
445  index++;
446  }
447  isload.close();
448 
449  for (int i = 0; i < N_OutputBits; i++) {
450  bool ftdl_fired = isFiredFTDL(_inpBits, algs[i]);
451  bool psnm_fired = false;
452  _ftdBits.push_back(ftdl_fired);
453  if (ftdl_fired) {
454  if (doprescale(m_PrescalesDB->getprescales(i))) {
455  psnm_fired = true;
456  }
457  }
458  psnm_fired ? _psnBits.push_back(true) : _psnBits.push_back(false);
459  GDLResult->setPreScale((i / 32), (i % 32), m_PrescalesDB->getprescales(i));
460  }
461 
462  }
463 
464 
465  }
466  }
467 
468  bool
469  TRGGDL::isFiredFTDL(std::vector<bool> input, std::string alg)
470  {
471  if (alg.length() == 0) return true;
472  const char* cst = alg.c_str();
473  bool reading_word = false;
474  bool result_the_term = true; // init value must be true
475  bool not_flag = false;
476  unsigned begin_word = 0;
477  unsigned word_length = 0;
478  // notation steeing side must follow
479  // no blank between '!' and word
480  for (unsigned i = 0; i < alg.length(); i++) {
481  if (('0' <= cst[i] && cst[i] <= '9') ||
482  ('_' == cst[i]) || ('!' == cst[i])) {
483  if (reading_word) { // must not be '!'
484  word_length++;
485  if (i == alg.length() - 1) {
486  bool fired = input[atoi(alg.substr(begin_word, word_length).c_str())];
487  B2DEBUG(20,
488  alg.substr(begin_word, word_length).c_str()
489  << "(" << fired << ")");
490  if (((!not_flag && fired) || (not_flag && !fired)) && result_the_term) {
491  return true;
492  }
493  }
494  } else {
495  // start of new word
496  reading_word = true;
497  if ('!' == cst[i]) {
498  begin_word = i + 1;
499  not_flag = true;
500  word_length = 0;
501  } else {
502  begin_word = i;
503  not_flag = false;
504  word_length = 1;
505  if (i == alg.length() - 1) {
506  // one char bit ('f',...) comes end of conditions, 'xxx+f'
507  bool fired = input[atoi(alg.substr(begin_word, word_length).c_str())];
508  B2DEBUG(20,
509  alg.substr(begin_word, word_length).c_str()
510  << "(" << fired << ")");
511  if (((!not_flag && fired) || (not_flag && !fired)) && result_the_term) {
512  return true;
513  }
514  }
515  }
516  }
517  } else if ('+' == cst[i] || i == alg.length() - 1) {
518  // End of the term.
519  if (reading_word) { // 'xxx+'
520  if (result_the_term) {
521  bool fired = input[atoi(alg.substr(begin_word, word_length).c_str())];
522  B2DEBUG(20,
523  alg.substr(begin_word, word_length).c_str()
524  << "(" << fired << ")");
525  if ((!not_flag && fired) || (not_flag && !fired)) {
526  return true;
527  } else {
528  // this term is denied by the latest bit
529  }
530  } else {
531  // already false.
532  }
533  reading_word = false;
534  } else {
535  // prior char is blank, 'xxx +'
536  if (result_the_term) {
537  return true;
538  } else {
539  // already false
540  }
541  }
542  result_the_term = true; // go to next term
543  } else {
544  // can be blank (white space) or any delimiter.
545  if (reading_word) {
546  // end of a word, 'xxxx '
547  if (result_the_term) {
548  // worth to try
549  bool fired = input[atoi(alg.substr(begin_word, word_length).c_str())];
550  B2DEBUG(20,
551  alg.substr(begin_word, word_length).c_str()
552  << "(" << fired << ")");
553  if ((!not_flag && fired) || (not_flag && !fired)) {
554  // go to next word
555  } else {
556  result_the_term = false;
557  }
558  } else {
559  // already false
560  }
561  reading_word = false;
562  } else {
563  // 2nd blank 'xx ' or leading blanck '^ '
564  }
565  }
566  }
567  return false;
568  }
569 
570  bool TRGGDL::doprescale(int f)
571  {
572  if (f == 0) return false;
573  if (f == 1) return true;
574  double ran = gRandom->Uniform(f);
575  return (ceil(ran) == f);
576  }
577 
578 
579  void
581  {
582 // TRGDebug::enterStage("TRGGDL firmSim");
583 
584 // if (TRGDebug::level())
585 // cout << TRGDebug::tab() << "Making dummy input signals" << endl;
586 
587  //...Clear signal bundles...
588  if (_isb) {
589  for (unsigned i = 0; i < _isb->size(); i++)
590  delete(* _isb)[i];
591  delete _isb;
592  }
593  if (_osb) {
594  for (unsigned i = 0; i < _osb->size(); i++)
595  delete(* _osb)[i];
596  delete _osb;
597  }
598  if (_tsb) {
599  for (unsigned i = 0; i < _tsb->size(); i++)
600  delete(* _tsb)[i];
601  delete _tsb;
602  }
603  if (_tosb) {
604  for (unsigned i = 0; i < _tosb->size(); i++)
605  delete(* _tosb)[i];
606  delete _tosb;
607  }
608 
609  //...Input bits...
610  const unsigned nInput = _input.size();
611  TRGSignalVector& input = * new TRGSignalVector(name() + "InputSignals",
612  _clock);
613  TRGSignalVector& timing = * new TRGSignalVector(name() + "TimingSignals",
614  _clock);
615  for (unsigned i = 0; i < nInput; i++) {
616 
617  TRGSignal s;
618 
619  //...Create dummy timing signal for even number input bits...
620  if ((i % 2) == 0) {
621  int tdcCount = 100;
622  TRGTime rise = TRGTime(tdcCount, true, _clock, _input[i]);
623  TRGTime fall = rise;
624  fall.shift(1).reverse();
625  s = TRGSignal(rise & fall);
626  s.name(_input[i]);
627  }
628 
629  //...Create dummy timing signal for odd number input bits...
630  else {
631  int tdcCount = 130;
632  TRGTime rise = TRGTime(tdcCount, true, _clock, _input[i]);
633  TRGTime fall = rise;
634  fall.shift(1).reverse();
635  s = TRGSignal(rise & fall);
636  s.name(_input[i]);
637  }
638 
639  //...Add to a signal vector...
640  input += s;
641 
642  //...Timing signal...
643  bool t =
644  (i == 14) || (i == 15) || (i == 16) || (i == 17) ||
645  (i == 41) || (i == 42) || (i == 43) || (i == 44) ||
646  (i == 49) || (i == 50) || (i == 51) || (i == 52) || (i == 53);
647  if (t)
648  timing += s;
649  }
650 
651  //...Make input signal bundle...
652  const string ni = name() + "InputSignalBundle";
653  _isb = new TRGSignalBundle(ni, _clock);
654  _isb->push_back(& input);
655 
656  //...Make output signal bundle...
657  const string no = name() + "OutputSignalBundle";
658  _osb = new TRGSignalBundle(no,
659  _clock,
660  * _isb,
661  14,
663 
664  //...Timing signal bundle...
665  TRGSignalVector& ftd = * new TRGSignalVector(name() + "FTD", _clock);
666 // ftd += (* ((* _osb)[0])[13]);
667 // ftd += * (* _osb)[0][13];
668  TRGSignal ddddd = (* (* _osb)[0])[13];
669  ftd += ddddd;
670 
671  const string nt = name() + "TimingSignalBundle";
672  _tsb = new TRGSignalBundle(nt, _clock);
673  _tsb->push_back(& timing);
674  _tsb->push_back(& ftd);
675 
676  //...Timing out signal bundle...
677  const string nto = name() + "TimingOutSignalBundle";
678  _tosb = new TRGSignalBundle(nto,
679  _clock,
680  * _tsb,
681  3,
682  7,
684 
685  if (TRGDebug::level()) {
686  if (input.active()) {
687  _isb->dump("detail", TRGDebug::tab());
688  _osb->dump("detail", TRGDebug::tab());
689  _tsb->dump("detail", TRGDebug::tab());
690  _tosb->dump("detail", TRGDebug::tab());
691  }
692  }
693 
694  TRGDebug::leaveStage("TRGGDL firmSim");
695  }
696 
697  void
699  {
700 
701  //...Open input data definition...
702  const string fni = _configFilename + ".inp";
703  ifstream infile(fni.c_str(), ios::in);
704  if (infile.fail()) {
705  cout << "TRGGDL !!! can not open file : " << fni << endl;
706  return;
707  }
708  getInput(infile);
709  infile.close();
710 
711  //...Open output data definition...
712  const string fno = _configFilename + ".oup";
713  ifstream outfile(fno.c_str(), ios::in);
714  if (outfile.fail()) {
715  cout << "TRGGDL !!! can not open file : " << fno << endl;
716  return;
717  }
718  getOutput(outfile);
719  outfile.close();
720 
721  //...Open algorithm data definition...
722  const string fna = _configFilename + ".alg";
723  ifstream algfile(fna.c_str(), ios::in);
724  if (algfile.fail()) {
725  cout << "TRGGDL !!! can not open file : " << fna << endl;
726  return;
727  }
728  getAlgorithm(algfile);
729  algfile.close();
730 
731  //...FTD function...
732  string ftd = _configFilename;
733  string::size_type s = ftd.find_last_of("/");
734  if (s != string::npos)
735  ftd = ftd.substr(s + 1);
736  if (ftd == "ftd_0.01") {
737  _ftd = ftd_0_01;
738  }
739 
740  //...Summary...
741  if (TRGDebug::level()) {
742  cout << "TRGGDL Config file = " << _configFilename << endl;
743  cout << " ftd=" << ftd << endl;
744  cout << "TRGGDL Input Bits" << endl;
745  for (unsigned i = 0; i < _input.size(); i++)
746  cout << TRGDebug::tab(4) << i << " : " << _input[i] << endl;
747  cout << "TRGGDL Output Bits" << endl;
748  for (unsigned i = 0; i < _output.size(); i++)
749  cout << TRGDebug::tab(4) << i << " : " << _output[i]
750  << " : " << _algorithm[i] << endl;
751  }
752  }
753 
754  void
755  TRGGDL::getInput(ifstream& ifs)
756  {
757  if (TRGDebug::level()) {
758  cout << "TRGGDL::getInput ... reading input data" << endl;
759  }
760 
761  char b[800];
762  unsigned lines = 0;
763  while (! ifs.eof()) {
764  ifs.getline(b, 800);
765 
766  //...The first word should be input bit number...
767  // const string w0 = TRGUtilities::carstring(b);
768 
769  //...Bit name...
770  string cdr = TRGUtilities::cdrstring(b);
771  const string w1 = TRGUtilities::carstring(cdr);
772 
773  if (w1.size())
774  _input.push_back(w1);
775 
776  ++lines;
777  }
778 
779  }
780 
781  void
782  TRGGDL::getOutput(ifstream& ifs)
783  {
784 
785  if (TRGDebug::level()) {
786  cout << "TRGGDL::getOutput ... reading output data" << endl;
787  }
788 
789  char b[800];
790  unsigned lines = 0;
791  while (! ifs.eof()) {
792  ifs.getline(b, 800);
793 
794  //...The first word should be input bit number...
795  // const string w0 = TRGUtilities::carstring(b);
796 
797  //...Bit name...
798  string cdr = TRGUtilities::cdrstring(b);
799  const string w1 = TRGUtilities::carstring(cdr);
800 
801  if (w1.size())
802  _output.push_back(w1);
803 
804  ++lines;
805  }
806 
807  }
808 
809  void
810  TRGGDL::getAlgorithm(ifstream& ifs)
811  {
812 
813  if (TRGDebug::level()) {
814  cout << "TRGGDL::getAlgorithm ... reading algorithm data" << endl;
815  }
816 
817  char b[800];
818  unsigned lines = 0;
819  while (! ifs.eof()) {
820  ifs.getline(b, 800);
821 
822  //...The first word should be input bit number...
823  // const string w0 = TRGUtilities::carstring(b);
824 
825  //...':'...
826  string cdr = TRGUtilities::cdrstring(b);
827  // const string w1 = TRGUtilities::carstring(cdr);
828 
829  //...Algorithm...
830  cdr = TRGUtilities::cdrstring(cdr);
831  const string w2 = cdr;
832 
833  if (w2.size())
834  _algorithm.push_back(w2);
835 
836  ++lines;
837  }
838 
839  }
840 
841  TRGState
843  {
844 
845  //...Prepare states for output...
846  TRGState s(14);
847 
848  //...Set up bool array...
849  bool* in = new bool[input.size()];
850  bool* ou = new bool[14];
851  input.copy2bool(in);
852 
853  // //...Simulate output bit 4...
854  // s.set(4, in[15] & (! in[18]) & in[33] & (! in[51]));
855 
856  // //...Simulate output bit 10...
857  // s.set(10, in[46]);
858 
859  //...FTD logic...
860  _ftd(ou, in);
861  bool active = false;
862  for (unsigned i = 0; i < 13; i++) {
863  if (ou[i])
864  active = true;
865  s.set(i, ou[i]);
866  }
867  if (active)
868  s.set(13, true);
869 
870  if (TRGDebug::level()) {
871  input.dump("detail", TRGDebug::tab(4) + "GDL in ");
872  s.dump("detail", TRGDebug::tab(4) + "GDL out ");
873  }
874 
875  //...Termination...
876  delete[] in;
877  delete[] ou;
878  return s;
879  }
880 
881  TRGState
883  TRGState& reg,
884  bool& active)
885  {
886 
887  //...Prepare registers...
888  // reg[0] := timing logic active, NOT USED
889  // reg[3:1] := state (0 to 7)
890  // reg[6:4] := counter after timing logic active
891 
892  // reg[1] := waiting for timing of TOP, ECL, and CDC
893  // reg[2] := TOP timing active
894  // reg[3] := ECL timing active
895  // reg[4] := CDC timing active
896  // reg[7:5] := remaining count to issue timing signal
897 
898 
899 
900  //...Input preparations...
901  // Assuming ftl_0.01
902  // Last bit is additional FTD signal
903  bool in[14];
904 
905  //...CDC...
906  in[0] = input[0];
907  in[1] = input[1];
908  in[2] = input[2];
909  in[3] = input[3];
910  /*
911  bool cdc = in[0];
912  bool * cdct = & in[1];
913  */
914 
915  //...ECL...
916  in[4] = input[4];
917  in[5] = input[5];
918  in[6] = input[6];
919  in[7] = input[7];
920  /*
921  bool ecl = in[4];
922  bool * eclt = & in[5];
923  */
924 
925  //...TOP...
926  in[8] = input[8];
927  in[9] = input[9];
928  in[10] = input[10];
929  in[11] = input[11];
930  in[12] = input[12];
931  /*
932  bool top = in[8];
933  bool * topt = & in[9];
934  */
935 
936  //...FTD...
937  in[13] = input[13];
938  bool ftd = in[13];
939 
940  //...Get state info...
941  unsigned state = unsigned(reg.subset(1, 3));
942  unsigned count = unsigned(reg.subset(4, 3));
943 
944  //...State machine...
945  bool timing = false;
946  unsigned source = 0;
947  if (state == 0) {
948  if (ftd) {
949  active = 1;
950  state = 1;
951  count = 1;
952  } else {
953  active = 0;
954  }
955  } else if (state == 1) {
956  if (count == 7) {
957  active = 0;
958  state = 0;
959  timing = true;
960  } else {
961  active = 1;
962  ++count;
963  }
964  }
965 
966  //...Set registers...
967  reg.set(1, 3, state);
968  reg.set(4, 3, count);
969 
970  //...Output...
971  TRGState out(3);
972  out.set(0, 1, timing);
973  out.set(1, 2, source);
974 
975  if (TRGDebug::level()) {
976  input.dump("detail", TRGDebug::tab(4) + "Timing in,");
977  reg.dump("detail", TRGDebug::tab(4) + "Registers,");
978  cout << TRGDebug::tab(4) << "ftd,active,state,count=" << ftd << ","
979  << active << "," << state
980  << "," << count << endl;
981 
982  out.dump("detail", TRGDebug::tab(4) + "Timing out,");
983  }
984 
985  //...Termination...
986  return out;
987  }
988 
989  void
991  {
992  for (std::size_t i = 0; i < _inpBits.size(); i++) {
993  if (_inpBits[i]) h->Fill(i);
994  }
995  }
996 
997  void
999  {
1000  for (std::size_t i = 0; i < _ftdBits.size(); i++) {
1001  if (_ftdBits[i]) h->Fill(i);
1002  }
1003  }
1004 
1005  void
1007  {
1008  for (std::size_t i = 0; i < _psnBits.size(); i++) {
1009  if (_psnBits[i]) h->Fill(i);
1010  }
1011  }
1012 
1014  {
1015  // The number of algorithm logics must be equal to the number of output bits.
1016  if (m_AlgsDB->getnalgs() != m_FTDLBitsDB->getnoutbit()) {
1017  B2FATAL("The number of logics in TRGGDLDBAlgs differs from the number of outpit bits in TRGGDLDBFTDLBits. Please check the content of the IoVs of both payloads."
1018  << LogVar("Logics", m_AlgsDB->getnalgs())
1019  << LogVar("Output bits", m_FTDLBitsDB->getnoutbit()));
1020  }
1021  }
1022 
1024 } // namespace Belle2
bool create(bool replace=false)
Create a default object in the data store.
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:95
The instance of TRGGDL is a singleton.
Definition: TRGGDL.h:44
TRGSignalBundle * _isb
Input signal bundle.
Definition: TRGGDL.h:251
TRGSignalBundle * _tosb
Timing output signal bundle.
Definition: TRGGDL.h:260
unsigned _simulationMode
Simulation mode.
Definition: TRGGDL.h:218
std::string _configFilename
GDL configuration filename.
Definition: TRGGDL.h:215
std::vector< std::string > _algorithm
Algorithm.
Definition: TRGGDL.h:248
int _debugLevel
Debug level.
Definition: TRGGDL.h:212
double _timquality_threshold_sfin
Threshold to determine timing quality flag with MC truth: super fine.
Definition: TRGGDL.h:269
std::vector< std::string > _output
Output names.
Definition: TRGGDL.h:245
const TRGClock & _clock
GDL trigger system clock.
Definition: TRGGDL.h:233
DBObjPtr< TRGGDLDBInputBits > m_InputBitsDB
Data base of GDL input bits.
Definition: TRGGDL.h:263
TRGSignalBundle * _tsb
Timing input signal bundle.
Definition: TRGGDL.h:257
DBObjPtr< HardwareClockSettings > m_hwClock
Hardware Clocks.
Definition: TRGGDL.h:289
double _timquality_threshold_fine
Threshold to determine timing quality flag with MC truth: fine.
Definition: TRGGDL.h:272
std::vector< std::string > _input
Input names.
Definition: TRGGDL.h:242
TRGSignalBundle * _osb
Output signal bundle.
Definition: TRGGDL.h:254
StoreObjPtr< SimClockState > m_simClockState
generated hardware clock state
Definition: TRGGDL.h:287
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
ETimingQuality
trigger timing type quality
Definition: TRGSummary.h:79
A class to represent a signal timing in the trigger system.
Definition: Time.h:25
static std::string carstring(const std::string &s)
CERNLIB car.
static std::string cdrstring(const std::string &s)
CERNLIB cdr.
Class to store variables with their name which were sent to the logging service.
void checkDatabase() const
Check the content of the DBObjects used by this class.
Definition: TRGGDL.cc:1013
void configure(void)
configures trigger modules for firmware simulation.
Definition: TRGGDL.cc:698
static std::string tab(void)
returns tab spaces.
Definition: Debug.cc:47
static TRGState decision(const TRGState &input)
Makes bit pattern(state) using input bit pattern(state).
Definition: TRGGDL.cc:842
std::string name(void) const
returns name.
Definition: TRGGDL.cc:54
void fastClear(void)
clears TRGGDL information.
Definition: TRGGDL.cc:204
TRGState subset(unsigned i, unsigned n) const
returns subset from i with n bits.
Definition: State.cc:356
void accumulatePsn(TH1I *)
Accumulate bit info in histogram.
Definition: TRGGDL.cc:1006
void terminate(void)
terminates when run is finished
Definition: TRGGDL.cc:179
TRGTime & reverse(void)
reverse edge.
Definition: Time.h:141
void accumulateFtd(TH1I *)
Accumulate bit info in histogram.
Definition: TRGGDL.cc:998
static void enterStage(const std::string &stageName)
Declare that you enter new stage.
Definition: Debug.cc:24
const TRGState & set(unsigned position, bool state=true)
sets state at bit i.
Definition: State.h:305
static void(* _ftd)(bool *out, bool *in)
Function to simulate final trigger decision.
Definition: TRGGDL.h:145
void getOutput(std::ifstream &ifs)
Read output data definition.
Definition: TRGGDL.cc:782
TRGTime & shift(int unit)
delays by clock unit.
Definition: Time.h:163
void initialize(void)
initializes GDL.
Definition: TRGGDL.cc:160
void dataSimulation(void)
Data simulation.
Definition: TRGGDL.cc:362
void dump(const std::string &message) const
dumps debug information.
Definition: TRGGDL.cc:184
static TRGState timingDecision(const TRGState &input, TRGState &registers, bool &logicStillActive)
Makes timing decision.
Definition: TRGGDL.cc:882
static int level(void)
returns the debug level.
Definition: Debug.cc:67
void update(bool mcAnalysis=true)
updates TRGGDL information.
Definition: TRGGDL.cc:209
static void leaveStage(const std::string &stageName)
Declare that you leave a stage.
Definition: Debug.cc:34
void accumulateInp(TH1I *)
Accumulate bit info in histogram.
Definition: TRGGDL.cc:990
void getAlgorithm(std::ifstream &ifs)
Read algorithm data definition.
Definition: TRGGDL.cc:810
void fastSimulation(void)
Fast simulation.
Definition: TRGGDL.cc:241
void simulate(void)
fast trigger simulation.
Definition: TRGGDL.cc:227
void clear(void)
clears all TRGGDL information.
Definition: TRGGDL.cc:199
void firmwareSimulation(void)
Firmware simulation.
Definition: TRGGDL.cc:580
void getInput(std::ifstream &ifs)
Read input data definition.
Definition: TRGGDL.cc:755
virtual ~TRGGDL()
Destructor.
Definition: TRGGDL.cc:221
void dump(const std::string &message="", const std::string &pre="") const
dumps contents.
Definition: Clock.cc:88
Abstract base class for different kinds of events.