Belle II Software development
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
35using namespace std;
36
37namespace Belle2 {
43 void ftd_0_01(bool* b, const 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, const 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") {
84 simulationMode,
85 fastSimulationMode,
87 Phase,
88 algFromDB,
89 algFilePath,
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*
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)
234 if (firm)
236 if (data)
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 while (std::getline(isload, str)) {
301 algs.push_back(str);
302 }
303 isload.close();
304
305 for (int i = 0; i < N_OutputBits; i++) {
306 if (_debugLevel > 89) printf("TRGGDL:ABB:i(%d)\n", i);
307 if ((i % 32) == 0) {
308 if (i > 0) {
309 GDLResult->setFtdlBits(i / 32 - 1, L1Summary);
310 GDLResult->setPsnmBits(i / 32 - 1, L1Summary_psnm);
311 }
312 L1Summary = 0;
313 L1Summary_psnm = 0;
314 }
315 std::string alg = _algFromDB ? m_AlgsDB->getalg(i) : algs[i];
316 if (_debugLevel > 89) printf("TRGGDL:i(%d), alg(%s)\n", i, m_AlgsDB->getalg(i).c_str());
317 if (isFiredFTDL(_inpBits, alg)) {
318 L1Summary |= (1 << (i % 32));
319 if (doprescale(m_PrescalesDB->getprescales(i))) {
320 L1Summary_psnm |= (1 << (i % 32));
321 }
322 }
323 GDLResult->setPreScale((i / 32), (i % 32), m_PrescalesDB->getprescales(i));
324 if (i == N_OutputBits - 1) {
325 GDLResult->setFtdlBits(i / 32, L1Summary);
326 GDLResult->setPsnmBits(i / 32, L1Summary_psnm);
327 }
328 }
329
330 //set timing qulaity flag based on MC truth jitter, which is NOT simulated by TSIM
331 TRGSummary::ETimingQuality timQuality = TRGSummary::TTYQ_NONE;
332 double jitter = 0;
333
334 //get true jitter if SimClockState is valid
335 if (m_simClockState.isValid() && m_hwClock.isValid()) {
336
337 jitter = m_simClockState->getRelativeBucketNo() / m_hwClock->getAcceleratorRF(); // in ns
338
339 //set timing quality flag with the jitter
340 if (abs(jitter) < _timquality_threshold_sfin)
341 timQuality = TRGSummary::TTYQ_SFIN;
342 else {
343 if (abs(jitter) < _timquality_threshold_fine)
344 timQuality = TRGSummary::TTYQ_FINE;
345 else
346 timQuality = TRGSummary::TTYQ_CORS;
347 }
348 }
349
350 B2DEBUG(20, "TRGGDL::set timing quality, jitter = " << jitter << ": timQuality = " << timQuality << " sfin threshold = " <<
351 _timquality_threshold_sfin << " fine threshold = " <<
353
354 //fill the flag to TRGSummary
355 GDLResult->setTimQuality(timQuality);
356 }
357 }
358
359 void
361 {
362// B2DEBUG(20,"TRGGDL::dataSimulation starts.");
364 /*
365 unsigned _exp = bevt->getExperiment();
366 unsigned _run = bevt->getRun();
367 */
368 if (_debugLevel > 9) printf("TRGGDL:dataSimulation Start\n");
369 unsigned _evt = bevt->getEvent();
370// TRGDebug::enterStage("TRGGDL dataSim");
371
372// if (TRGDebug::level())
373// cout << TRGDebug::tab() << name() << " do nothing..." << endl;
374
375 if (!m_InputBitsDB) B2INFO("no database of gdl input bits");
376 int N_InputBits = m_InputBitsDB->getninbit();
377 if (!m_FTDLBitsDB) B2INFO("no database of gdl ftdl bits");
378 int N_OutputBits = m_FTDLBitsDB->getnoutbit();
379 if (!m_PrescalesDB) B2INFO("no database of gdl prescale");
380 int N_AlgsBits = m_AlgsDB->getnalgs();
381 if (!m_AlgsDB) B2INFO("no database of gdl ftdl bit logic");
382 if (N_OutputBits > N_AlgsBits) {
383 B2DEBUG(20, "#Algs and #FTDL is different");
384 N_OutputBits = N_AlgsBits;
385 }
386
387 if (_debugLevel > 19)
388 printf("TRGGDL:N_InputBits(%d), N_OutputBits(%d)\n", N_InputBits, N_OutputBits);
389
390 StoreObjPtr<TRGSummary> GDLResult;
391 if (! GDLResult) {
392 B2WARNING("TRGGDL::dataSimulation(): TRGSummary not found. Check it!!!!");
393 return;
394 } else {
395
396 if (_debugLevel > 89) printf("TRGGDL:TRGSummary Found.\n");
397 _inpBits.clear();
398 _ftdBits.clear();
399 _psnBits.clear();
400 for (int i = 0; i < N_InputBits; i++) {
401 bool inputBit;
402 try { inputBit = GDLResult->testInput(i); }
403 catch (const std::exception&) { inputBit = false; }
404 _inpBits.push_back(inputBit);
405 }
406
407 if (_algFromDB) {
408 for (int i = 0; i < N_OutputBits; i++) {
409 bool ftdl_fired = isFiredFTDL(_inpBits, m_AlgsDB->getalg(i));
410 bool psnm_fired = false;
411 _ftdBits.push_back(ftdl_fired);
412 if (ftdl_fired) {
413 if (doprescale(m_PrescalesDB->getprescales(i))) {
414 psnm_fired = true;
415 }
416 }
417 psnm_fired ? _psnBits.push_back(true) : _psnBits.push_back(false);
418 GDLResult->setPreScale((i / 32), (i % 32), m_PrescalesDB->getprescales(i));
419 if (! strcmp(m_FTDLBitsDB->getoutbitname(i), "hie")) {
420 if (_debugLevel == 971 && ftdl_fired) {
421 int i_ehigh = m_InputBitsDB->getinbitnum("ehigh");
422 int i_bha_veto = m_InputBitsDB->getinbitnum("bha_veto");
423 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",
424 i, _evt, m_PrescalesDB->getprescales(i),
425 _inpBits[i_ehigh] ? 1 : 0,
426 _inpBits[i_bha_veto] ? 1 : 0,
427 ftdl_fired,
428 psnm_fired ? 1 : 0,
429 i_ehigh,
430 i_bha_veto,
431 m_FTDLBitsDB->getoutbitname(i));
432 }
433 }
434 }
435
436 } else {
437 std::string str;
438 std::vector<std::string> algs;
439 std::ifstream isload(_algFilePath.c_str(), std::ios::in);
440 while (std::getline(isload, str)) {
441 algs.push_back(str);
442 }
443 isload.close();
444
445 for (int i = 0; i < N_OutputBits; i++) {
446 bool ftdl_fired = isFiredFTDL(_inpBits, algs[i]);
447 bool psnm_fired = false;
448 _ftdBits.push_back(ftdl_fired);
449 if (ftdl_fired) {
450 if (doprescale(m_PrescalesDB->getprescales(i))) {
451 psnm_fired = true;
452 }
453 }
454 psnm_fired ? _psnBits.push_back(true) : _psnBits.push_back(false);
455 GDLResult->setPreScale((i / 32), (i % 32), m_PrescalesDB->getprescales(i));
456 }
457
458 }
459
460
461 }
462 }
463
464 bool
465 TRGGDL::isFiredFTDL(std::vector<bool> input, std::string alg)
466 {
467 if (alg.length() == 0) return true;
468 const char* cst = alg.c_str();
469 bool reading_word = false;
470 bool result_the_term = true; // init value must be true
471 bool not_flag = false;
472 unsigned begin_word = 0;
473 unsigned word_length = 0;
474 // notation steeing side must follow
475 // no blank between '!' and word
476 for (unsigned i = 0; i < alg.length(); i++) {
477 if (('0' <= cst[i] && cst[i] <= '9') ||
478 ('_' == cst[i]) || ('!' == cst[i])) {
479 if (reading_word) { // must not be '!'
480 word_length++;
481 if (i == alg.length() - 1) {
482 bool fired = input[atoi(alg.substr(begin_word, word_length).c_str())];
483 B2DEBUG(20,
484 alg.substr(begin_word, word_length).c_str()
485 << "(" << fired << ")");
486 if (((!not_flag && fired) || (not_flag && !fired)) && result_the_term) {
487 return true;
488 }
489 }
490 } else {
491 // start of new word
492 reading_word = true;
493 if ('!' == cst[i]) {
494 begin_word = i + 1;
495 not_flag = true;
496 word_length = 0;
497 } else {
498 begin_word = i;
499 not_flag = false;
500 word_length = 1;
501 if (i == alg.length() - 1) {
502 // one char bit ('f',...) comes end of conditions, 'xxx+f'
503 bool fired = input[atoi(alg.substr(begin_word, word_length).c_str())];
504 B2DEBUG(20,
505 alg.substr(begin_word, word_length).c_str()
506 << "(" << fired << ")");
507 // cppcheck-suppress knownConditionTrueFalse
508 if (((!not_flag && fired) || (not_flag && !fired)) && result_the_term) {
509 return true;
510 }
511 }
512 }
513 }
514 } else if ('+' == cst[i] || i == alg.length() - 1) {
515 // End of the term.
516 if (reading_word) { // 'xxx+'
517 if (result_the_term) {
518 bool fired = input[atoi(alg.substr(begin_word, word_length).c_str())];
519 B2DEBUG(20,
520 alg.substr(begin_word, word_length).c_str()
521 << "(" << fired << ")");
522 if ((!not_flag && fired) || (not_flag && !fired)) {
523 return true;
524 } else {
525 // this term is denied by the latest bit
526 }
527 } else {
528 // already false.
529 }
530 reading_word = false;
531 } else {
532 // prior char is blank, 'xxx +'
533 if (result_the_term) {
534 return true;
535 } else {
536 // already false
537 }
538 }
539 result_the_term = true; // go to next term
540 } else {
541 // can be blank (white space) or any delimiter.
542 if (reading_word) {
543 // end of a word, 'xxxx '
544 if (result_the_term) {
545 // worth to try
546 bool fired = input[atoi(alg.substr(begin_word, word_length).c_str())];
547 B2DEBUG(20,
548 alg.substr(begin_word, word_length).c_str()
549 << "(" << fired << ")");
550 if ((!not_flag && fired) || (not_flag && !fired)) {
551 // go to next word
552 } else {
553 result_the_term = false;
554 }
555 } else {
556 // already false
557 }
558 reading_word = false;
559 } else {
560 // 2nd blank 'xx ' or leading blanck '^ '
561 }
562 }
563 }
564 return false;
565 }
566
567 bool TRGGDL::doprescale(int f)
568 {
569 if (f == 0) return false;
570 if (f == 1) return true;
571 double ran = gRandom->Uniform(f);
572 return (ceil(ran) == f);
573 }
574
575
576 void
578 {
579// TRGDebug::enterStage("TRGGDL firmSim");
580
581// if (TRGDebug::level())
582// cout << TRGDebug::tab() << "Making dummy input signals" << endl;
583
584 //...Clear signal bundles...
585 if (_isb) {
586 for (unsigned i = 0; i < _isb->size(); i++)
587 delete (* _isb)[i];
588 delete _isb;
589 }
590 if (_osb) {
591 for (unsigned i = 0; i < _osb->size(); i++)
592 delete (* _osb)[i];
593 delete _osb;
594 }
595 if (_tsb) {
596 for (unsigned i = 0; i < _tsb->size(); i++)
597 delete (* _tsb)[i];
598 delete _tsb;
599 }
600 if (_tosb) {
601 for (unsigned i = 0; i < _tosb->size(); i++)
602 delete (* _tosb)[i];
603 delete _tosb;
604 }
605
606 //...Input bits...
607 const unsigned nInput = _input.size();
608 TRGSignalVector& input = * new TRGSignalVector(name() + "InputSignals",
609 _clock);
610 TRGSignalVector& timing = * new TRGSignalVector(name() + "TimingSignals",
611 _clock);
612 for (unsigned i = 0; i < nInput; i++) {
613
614 TRGSignal s;
615
616 //...Create dummy timing signal for even number input bits...
617 if ((i % 2) == 0) {
618 int tdcCount = 100;
619 TRGTime rise = TRGTime(tdcCount, true, _clock, _input[i]);
620 TRGTime fall = rise;
621 fall.shift(1).reverse();
622 s = TRGSignal(rise & fall);
623 s.name(_input[i]);
624 }
625
626 //...Create dummy timing signal for odd number input bits...
627 else {
628 int tdcCount = 130;
629 TRGTime rise = TRGTime(tdcCount, true, _clock, _input[i]);
630 TRGTime fall = rise;
631 fall.shift(1).reverse();
632 s = TRGSignal(rise & fall);
633 s.name(_input[i]);
634 }
635
636 //...Add to a signal vector...
637 input += s;
638
639 //...Timing signal...
640 bool t =
641 (i == 14) || (i == 15) || (i == 16) || (i == 17) ||
642 (i == 41) || (i == 42) || (i == 43) || (i == 44) ||
643 (i == 49) || (i == 50) || (i == 51) || (i == 52) || (i == 53);
644 if (t)
645 timing += s;
646 }
647
648 //...Make input signal bundle...
649 const string ni = name() + "InputSignalBundle";
650 _isb = new TRGSignalBundle(ni, _clock);
651 _isb->push_back(& input);
652
653 //...Make output signal bundle...
654 const string no = name() + "OutputSignalBundle";
655 _osb = new TRGSignalBundle(no,
656 _clock,
657 * _isb,
658 14,
660
661 //...Timing signal bundle...
662 TRGSignalVector& ftd = * new TRGSignalVector(name() + "FTD", _clock);
663// ftd += (* ((* _osb)[0])[13]);
664// ftd += * (* _osb)[0][13];
665 TRGSignal ddddd = (* (* _osb)[0])[13];
666 ftd += ddddd;
667
668 const string nt = name() + "TimingSignalBundle";
669 _tsb = new TRGSignalBundle(nt, _clock);
670 _tsb->push_back(& timing);
671 _tsb->push_back(& ftd);
672
673 //...Timing out signal bundle...
674 const string nto = name() + "TimingOutSignalBundle";
675 _tosb = new TRGSignalBundle(nto,
676 _clock,
677 * _tsb,
678 3,
679 7,
681
682 if (TRGDebug::level()) {
683 if (input.active()) {
684 _isb->dump("detail", TRGDebug::tab());
685 _osb->dump("detail", TRGDebug::tab());
686 _tsb->dump("detail", TRGDebug::tab());
687 _tosb->dump("detail", TRGDebug::tab());
688 }
689 }
690
691 TRGDebug::leaveStage("TRGGDL firmSim");
692 }
693
694 void
696 {
697
698 //...Open input data definition...
699 const string fni = _configFilename + ".inp";
700 ifstream infile(fni.c_str(), ios::in);
701 if (infile.fail()) {
702 cout << "TRGGDL !!! can not open file : " << fni << endl;
703 return;
704 }
705 getInput(infile);
706 infile.close();
707
708 //...Open output data definition...
709 const string fno = _configFilename + ".oup";
710 ifstream outfile(fno.c_str(), ios::in);
711 if (outfile.fail()) {
712 cout << "TRGGDL !!! can not open file : " << fno << endl;
713 return;
714 }
715 getOutput(outfile);
716 outfile.close();
717
718 //...Open algorithm data definition...
719 const string fna = _configFilename + ".alg";
720 ifstream algfile(fna.c_str(), ios::in);
721 if (algfile.fail()) {
722 cout << "TRGGDL !!! can not open file : " << fna << endl;
723 return;
724 }
725 getAlgorithm(algfile);
726 algfile.close();
727
728 //...FTD function...
729 string ftd = _configFilename;
730 string::size_type s = ftd.find_last_of("/");
731 if (s != string::npos)
732 ftd = ftd.substr(s + 1);
733 if (ftd == "ftd_0.01") {
734 _ftd = ftd_0_01;
735 }
736
737 //...Summary...
738 if (TRGDebug::level()) {
739 cout << "TRGGDL Config file = " << _configFilename << endl;
740 cout << " ftd=" << ftd << endl;
741 cout << "TRGGDL Input Bits" << endl;
742 for (unsigned i = 0; i < _input.size(); i++)
743 cout << TRGDebug::tab(4) << i << " : " << _input[i] << endl;
744 cout << "TRGGDL Output Bits" << endl;
745 for (unsigned i = 0; i < _output.size(); i++)
746 cout << TRGDebug::tab(4) << i << " : " << _output[i]
747 << " : " << _algorithm[i] << endl;
748 }
749 }
750
751 void
752 TRGGDL::getInput(ifstream& ifs)
753 {
754 if (TRGDebug::level()) {
755 cout << "TRGGDL::getInput ... reading input data" << endl;
756 }
757
758 char b[800];
759 while (! ifs.eof()) {
760 ifs.getline(b, 800);
761
762 //...The first word should be input bit number...
763 // const string w0 = TRGUtilities::carstring(b);
764
765 //...Bit name...
766 string cdr = TRGUtilities::cdrstring(b);
767 const string w1 = TRGUtilities::carstring(cdr);
768
769 if (w1.size())
770 _input.push_back(w1);
771 }
772
773 }
774
775 void
776 TRGGDL::getOutput(ifstream& ifs)
777 {
778
779 if (TRGDebug::level()) {
780 cout << "TRGGDL::getOutput ... reading output data" << endl;
781 }
782
783 char b[800];
784 while (! ifs.eof()) {
785 ifs.getline(b, 800);
786
787 //...The first word should be input bit number...
788 // const string w0 = TRGUtilities::carstring(b);
789
790 //...Bit name...
791 string cdr = TRGUtilities::cdrstring(b);
792 const string w1 = TRGUtilities::carstring(cdr);
793
794 if (w1.size())
795 _output.push_back(w1);
796 }
797
798 }
799
800 void
801 TRGGDL::getAlgorithm(ifstream& ifs)
802 {
803
804 if (TRGDebug::level()) {
805 cout << "TRGGDL::getAlgorithm ... reading algorithm data" << endl;
806 }
807
808 char b[800];
809 while (! ifs.eof()) {
810 ifs.getline(b, 800);
811
812 //...The first word should be input bit number...
813 // const string w0 = TRGUtilities::carstring(b);
814
815 //...':'...
816 string cdr = TRGUtilities::cdrstring(b);
817 // const string w1 = TRGUtilities::carstring(cdr);
818
819 //...Algorithm...
820 cdr = TRGUtilities::cdrstring(cdr);
821 const string w2 = cdr;
822
823 if (w2.size())
824 _algorithm.push_back(w2);
825 }
826
827 }
828
831 {
832
833 //...Prepare states for output...
834 TRGState s(14);
835
836 //...Set up bool array...
837 bool* in = new bool[input.size()];
838 bool* ou = new bool[14];
839 input.copy2bool(in);
840
841 // //...Simulate output bit 4...
842 // s.set(4, in[15] & (! in[18]) & in[33] & (! in[51]));
843
844 // //...Simulate output bit 10...
845 // s.set(10, in[46]);
846
847 //...FTD logic...
848 _ftd(ou, in);
849 bool active = false;
850 for (unsigned i = 0; i < 13; i++) {
851 if (ou[i])
852 active = true;
853 s.set(i, ou[i]);
854 }
855 if (active)
856 s.set(13, true);
857
858 if (TRGDebug::level()) {
859 input.dump("detail", TRGDebug::tab(4) + "GDL in ");
860 s.dump("detail", TRGDebug::tab(4) + "GDL out ");
861 }
862
863 //...Termination...
864 delete[] in;
865 delete[] ou;
866 return s;
867 }
868
871 TRGState& reg,
872 bool& active)
873 {
874
875 //...Prepare registers...
876 // reg[0] := timing logic active, NOT USED
877 // reg[3:1] := state (0 to 7)
878 // reg[6:4] := counter after timing logic active
879
880 // reg[1] := waiting for timing of TOP, ECL, and CDC
881 // reg[2] := TOP timing active
882 // reg[3] := ECL timing active
883 // reg[4] := CDC timing active
884 // reg[7:5] := remaining count to issue timing signal
885
886
887
888 //...Input preparations...
889 // Assuming ftl_0.01
890 // Last bit is additional FTD signal
891 bool in[14];
892
893 //...CDC...
894 in[0] = input[0];
895 in[1] = input[1];
896 in[2] = input[2];
897 in[3] = input[3];
898 /*
899 bool cdc = in[0];
900 bool * cdct = & in[1];
901 */
902
903 //...ECL...
904 in[4] = input[4];
905 in[5] = input[5];
906 in[6] = input[6];
907 in[7] = input[7];
908 /*
909 bool ecl = in[4];
910 bool * eclt = & in[5];
911 */
912
913 //...TOP...
914 in[8] = input[8];
915 in[9] = input[9];
916 in[10] = input[10];
917 in[11] = input[11];
918 in[12] = input[12];
919 /*
920 bool top = in[8];
921 bool * topt = & in[9];
922 */
923
924 //...FTD...
925 in[13] = input[13];
926 bool ftd = in[13];
927
928 //...Get state info...
929 unsigned state = unsigned(reg.subset(1, 3));
930 unsigned count = unsigned(reg.subset(4, 3));
931
932 //...State machine...
933 bool timing = false;
934 unsigned source = 0;
935 if (state == 0) {
936 if (ftd) {
937 active = 1;
938 state = 1;
939 count = 1;
940 } else {
941 active = 0;
942 }
943 } else if (state == 1) {
944 if (count == 7) {
945 active = 0;
946 state = 0;
947 timing = true;
948 } else {
949 active = 1;
950 ++count;
951 }
952 }
953
954 //...Set registers...
955 reg.set(1, 3, state);
956 reg.set(4, 3, count);
957
958 //...Output...
959 TRGState out(3);
960 out.set(0, 1, timing);
961 out.set(1, 2, source);
962
963 if (TRGDebug::level()) {
964 input.dump("detail", TRGDebug::tab(4) + "Timing in,");
965 reg.dump("detail", TRGDebug::tab(4) + "Registers,");
966 cout << TRGDebug::tab(4) << "ftd,active,state,count=" << ftd << ","
967 << active << "," << state
968 << "," << count << endl;
969
970 out.dump("detail", TRGDebug::tab(4) + "Timing out,");
971 }
972
973 //...Termination...
974 return out;
975 }
976
977 void
979 {
980 for (std::size_t i = 0; i < _inpBits.size(); i++) {
981 if (_inpBits[i]) h->Fill(i);
982 }
983 }
984
985 void
987 {
988 for (std::size_t i = 0; i < _ftdBits.size(); i++) {
989 if (_ftdBits[i]) h->Fill(i);
990 }
991 }
992
993 void
995 {
996 for (std::size_t i = 0; i < _psnBits.size(); i++) {
997 if (_psnBits[i]) h->Fill(i);
998 }
999 }
1000
1002 {
1003 // The number of algorithm logics must be equal to the number of output bits.
1004 if (m_AlgsDB->getnalgs() != m_FTDLBitsDB->getnoutbit()) {
1005 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."
1006 << LogVar("Logics", m_AlgsDB->getnalgs())
1007 << LogVar("Output bits", m_FTDLBitsDB->getnoutbit()));
1008 }
1009 }
1010
1012} // 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:96
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
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:1001
void configure(void)
configures trigger modules for firmware simulation.
Definition: TRGGDL.cc:695
int debugLevel(void) const
returns debug level.
Definition: TRGGDL.h:299
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:830
static void(* _ftd)(bool *out, const bool *in)
Function to simulate final trigger decision.
Definition: TRGGDL.h:145
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:994
void terminate(void)
terminates when run is finished
Definition: TRGGDL.cc:179
static TRGGDL * getTRGGDL(void)
returns TRGGDL object.
Definition: TRGGDL.cc:103
TRGTime & reverse(void)
reverse edge.
Definition: Time.h:141
void accumulateFtd(TH1I *)
Accumulate bit info in histogram.
Definition: TRGGDL.cc:986
static std::string cdrstring(const std::string &s)
CERNLIB cdr.
Definition: Utilities.cc:88
static void enterStage(const std::string &stageName)
Declare that you enter new stage.
Definition: Debug.cc:24
static TRGGDL * _gdl
GDL singleton.
Definition: TRGGDL.h:209
const TRGState & set(unsigned position, bool state=true)
sets state at bit i.
Definition: State.h:305
void getOutput(std::ifstream &ifs)
Read output data definition.
Definition: TRGGDL.cc:776
TRGTime & shift(int unit)
delays by clock unit.
Definition: Time.h:163
TRGGDL(const std::string &configFile, unsigned simulationMode, unsigned fastSimulationMode, unsigned firmwareSimulationMode, const std::string &Phase, bool algFromDB=true, const std::string &algFilePath="ftd.alg", int debugLevel=0, double timquality_threshold_sfin=0, double timquality_threshold_fine=0)
Constructor.
Definition: TRGGDL.cc:110
unsigned firmwareSimulationMode(void) const
returns firmware simulation mode.
Definition: TRGGDL.h:327
void initialize(void)
initializes GDL.
Definition: TRGGDL.cc:160
void dataSimulation(void)
Data simulation.
Definition: TRGGDL.cc:360
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:870
static int level(void)
returns the debug level.
Definition: Debug.cc:67
static std::string carstring(const std::string &s)
CERNLIB car.
Definition: Utilities.cc:68
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:978
void getAlgorithm(std::ifstream &ifs)
Read algorithm data definition.
Definition: TRGGDL.cc:801
void fastSimulation(void)
Fast simulation.
Definition: TRGGDL.cc:241
std::string version(void) const
returns version.
Definition: TRGGDL.cc:60
std::string configFile(void) const
returns configuration file name.
Definition: TRGGDL.h:334
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:577
void getInput(std::ifstream &ifs)
Read input data definition.
Definition: TRGGDL.cc:752
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.
STL namespace.