14 #define TRG_SHORT_NAMES
15 #define TRGGDL_SHORT_NAMES
20 #include "trg/trg/Debug.h"
21 #include "trg/trg/Time.h"
22 #include "trg/trg/State.h"
23 #include "trg/trg/Signal.h"
24 #include "trg/trg/Utilities.h"
25 #include "trg/gdl/TRGGDL.h"
27 #include <framework/datastore/StoreObjPtr.h>
29 #include <mdst/dataobjects/TRGSummary.h>
30 #include <trg/grl/dataobjects/TRGGRLInfo.h>
32 #include <framework/logging/Logger.h>
36 #define N_TIMING_REGISTERS 4
46 void ftd_0_01(
bool* b,
bool* i);
54 (* TRGGDL::_ftd)(
bool* b,
bool* i) = 0;
57 TRGGDL::name(
void)
const
63 TRGGDL::version(
void)
const
65 return string(
"TRGGDL 0.01");
69 TRGGDL::getTRGGDL(
const string& configFile,
70 unsigned simulationMode,
71 unsigned fastSimulationMode,
72 unsigned firmwareSimulationMode,
73 const std::string& Phase,
75 const std::string& algFilePath,
83 if (configFile !=
"good-bye") {
84 _gdl =
new TRGGDL(configFile,
87 firmwareSimulationMode,
93 cout <<
"TRGGDL::getTRGGDL ... good-bye" << endl;
102 TRGGDL::getTRGGDL(
void)
105 cout <<
"TRGGDL::getTRGGDL !!! TRGGDL is not created yet" << endl;
109 TRGGDL::TRGGDL(
const string& configFile,
110 unsigned simulationMode,
111 unsigned fastSimulationMode,
112 unsigned firmwareSimulationMode,
113 const std::string& Phase,
115 const std::string& algFilePath,
117 : _debugLevel(debugLevel),
118 _configFilename(configFile),
119 _simulationMode(simulationMode),
120 _fastSimulationMode(fastSimulationMode),
121 _firmwareSimulationMode(firmwareSimulationMode),
123 _algFilePath(algFilePath),
124 _clock(Belle2_GDL::GDLSystemClock),
128 _algFromDB(algFromDB)
145 _inpBitNames.push_back(std::string(
m_InputBitsDB->getinbitname(i)));
148 for (
int i = 0; i < m_FTDLBitsDB->getnoutbit(); i++) {
149 _oupBitNames.push_back(std::string(m_FTDLBitsDB->getoutbitname(i)));
159 B2DEBUG(20,
"TRGGDL::initialize, inputBits: " << i <<
", " <<
m_InputBitsDB->getinbitname(i));
161 for (
int i = 0; i < m_FTDLBitsDB->getnoutbit(); i++) {
162 B2DEBUG(20,
"TRGGDL::initialize, outputBits: " << i <<
", " << m_FTDLBitsDB->getoutbitname(i));
164 for (
int i = 0; i < db_algs->getnalgs(); i++) {
165 B2DEBUG(20,
"TRGGDL::initialize, algs: " << i <<
", " << db_algs->getalg(i));
181 if (msg.find(
"name") != string::npos ||
182 msg.find(
"version") != string::npos ||
183 msg.find(
"detail") != string::npos ||
186 if (msg.find(
"detail") != string::npos ||
187 msg.find(
"state") != string::npos) {
238 B2DEBUG(20,
"TRGGDL::fastSimulation starts.");
248 if (!m_FTDLBitsDB) B2INFO(
"no database of gdl ftdl bits");
249 int N_OutputBits = m_FTDLBitsDB->getnoutbit();
250 if (!m_PrescalesDB) B2INFO(
"no database of gdl prescale");
254 B2WARNING(
"TRGGDL::fastSimulation(): TRGSummary exist already, check it!!!!");
260 B2WARNING(
"TRGGRLInfo doesn't exist!!!!");
263 if (
_debugLevel > 89) printf(
"TRGGDL:TRGGRLInfo found.\n");
267 int input_summary = 0;
268 for (
int i = 0; i < N_InputBits; i++) {
269 if (
_debugLevel > 89) printf(
"TRGGDL:ABC:i(%d)\n", i);
272 GDLResult->setInputBits(i / 32 - 1, input_summary);
276 if (grlinfo->getInputBits(i)) input_summary |= (1 << (i % 32));
277 _inpBits.push_back(grlinfo->getInputBits(i));
278 if (i == N_InputBits - 1) {
279 GDLResult->setInputBits(i / 32, input_summary);
284 int L1Summary_psnm = 0;
286 std::vector<std::string> algs;
287 std::ifstream isload(_algFilePath.c_str(), std::ios::in);
289 while (std::getline(isload, str)) {
295 for (
int i = 0; i < N_OutputBits; i++) {
296 if (
_debugLevel > 89) printf(
"TRGGDL:ABB:i(%d)\n", i);
299 GDLResult->setFtdlBits(i / 32 - 1, L1Summary);
300 GDLResult->setPsnmBits(i / 32 - 1, L1Summary_psnm);
305 std::string alg = _algFromDB ? db_algs->getalg(i) : algs[i];
306 if (
_debugLevel > 89) printf(
"TRGGDL:i(%d), alg(%s)\n", i, db_algs->getalg(i).c_str());
307 if (isFiredFTDL(_inpBits, alg)) {
308 L1Summary |= (1 << (i % 32));
309 if (doprescale(m_PrescalesDB->getprescales(i))) {
310 L1Summary_psnm |= (1 << (i % 32));
313 GDLResult->setPreScale((i / 32), (i % 32), m_PrescalesDB->getprescales(i));
314 if (i == N_OutputBits - 1) {
315 GDLResult->setFtdlBits(i / 32, L1Summary);
316 GDLResult->setPsnmBits(i / 32, L1Summary_psnm);
331 if (
_debugLevel > 9) printf(
"TRGGDL:dataSimulation Start\n");
332 unsigned _evt = bevt->getEvent();
340 if (!m_FTDLBitsDB) B2INFO(
"no database of gdl ftdl bits");
341 int N_OutputBits = m_FTDLBitsDB->getnoutbit();
342 if (!m_PrescalesDB) B2INFO(
"no database of gdl prescale");
345 printf(
"TRGGDL:N_InputBits(%d), N_OutputBits(%d)\n", N_InputBits, N_OutputBits);
349 B2WARNING(
"TRGGDL::dataSimulation(): TRGSummary not found. Check it!!!!");
353 if (
_debugLevel > 89) printf(
"TRGGDL:TRGSummary Found.\n");
357 for (
int i = 0; i < N_InputBits; i++) {
358 _inpBits.push_back(GDLResult->testInput(i));
364 int L1Summary_psnm = 0;
366 for (
int i = 0; i < N_OutputBits; i++) {
367 bool ftdl_fired = isFiredFTDL(_inpBits, db_algs->getalg(i));
368 bool psnm_fired =
false;
369 _ftdBits.push_back(ftdl_fired);
371 L1Summary |= (1 << i);
372 if (doprescale(m_PrescalesDB->getprescales(i))) {
373 L1Summary_psnm |= (1 << i);
377 psnm_fired ? _psnBits.push_back(
true) : _psnBits.push_back(
false);
378 GDLResult->setPreScale((i / 32), (i % 32), m_PrescalesDB->getprescales(i));
379 if (! strcmp(m_FTDLBitsDB->getoutbitname(i),
"hie")) {
383 printf(
"TRGGDL:hie:i=%d,evt=%d,ps=%d,ehigh=%d,bha_veto=%d,ftdl_fired=%d,psnm_fired=%d,i_ehigh=%d,i_bha_veto=%d,obitname=%s\n",
384 i, _evt, m_PrescalesDB->getprescales(i),
385 _inpBits[i_ehigh] ? 1 : 0,
386 _inpBits[i_bha_veto] ? 1 : 0,
391 m_FTDLBitsDB->getoutbitname(i));
399 int L1Summary_psnm = 0;
401 std::vector<std::string> algs;
402 std::ifstream isload(_algFilePath.c_str(), std::ios::in);
404 while (std::getline(isload, str)) {
410 for (
int i = 0; i < N_OutputBits; i++) {
411 bool ftdl_fired = isFiredFTDL(_inpBits, algs[i]);
412 bool psnm_fired =
false;
413 _ftdBits.push_back(ftdl_fired);
415 L1Summary |= (1 << i);
416 if (doprescale(m_PrescalesDB->getprescales(i))) {
417 L1Summary_psnm |= (1 << i);
421 psnm_fired ? _psnBits.push_back(
true) : _psnBits.push_back(
false);
422 GDLResult->setPreScale((i / 32), (i % 32), m_PrescalesDB->getprescales(i));
432 TRGGDL::isFiredFTDL(std::vector<bool> input, std::string alg)
434 if (alg.length() == 0)
return true;
435 const char* cst = alg.c_str();
436 bool reading_word =
false;
437 bool result_the_term =
true;
438 bool not_flag =
false;
439 unsigned begin_word = 0;
440 unsigned word_length = 0;
443 for (
unsigned i = 0; i < alg.length(); i++) {
444 if ((
'0' <= cst[i] && cst[i] <=
'9') ||
445 (
'_' == cst[i]) || (
'!' == cst[i])) {
448 if (i == alg.length() - 1) {
449 bool fired = input[atoi(alg.substr(begin_word, word_length).c_str())];
451 alg.substr(begin_word, word_length).c_str()
452 <<
"(" << fired <<
")");
453 if (((!not_flag && fired) || (not_flag && !fired)) && result_the_term) {
468 if (i == alg.length() - 1) {
470 bool fired = input[atoi(alg.substr(begin_word, word_length).c_str())];
472 alg.substr(begin_word, word_length).c_str()
473 <<
"(" << fired <<
")");
474 if (((!not_flag && fired) || (not_flag && !fired)) && result_the_term) {
480 }
else if (
'+' == cst[i] || i == alg.length() - 1) {
483 if (result_the_term) {
484 bool fired = input[atoi(alg.substr(begin_word, word_length).c_str())];
486 alg.substr(begin_word, word_length).c_str()
487 <<
"(" << fired <<
")");
488 if ((!not_flag && fired) || (not_flag && !fired)) {
496 reading_word =
false;
499 if (result_the_term) {
505 result_the_term =
true;
510 reading_word =
false;
511 if (result_the_term) {
513 bool fired = input[atoi(alg.substr(begin_word, word_length).c_str())];
515 alg.substr(begin_word, word_length).c_str()
516 <<
"(" << fired <<
")");
517 if ((!not_flag && fired) || (not_flag && !fired)) {
520 result_the_term =
false;
525 reading_word =
false;
534 bool TRGGDL::doprescale(
int f)
536 if (f == 0)
return false;
537 if (f == 1)
return true;
538 double ran = gRandom->Uniform(f);
539 return (ceil(ran) == f);
553 for (
unsigned i = 0; i <
_isb->size(); i++)
558 for (
unsigned i = 0; i <
_osb->size(); i++)
563 for (
unsigned i = 0; i <
_tsb->size(); i++)
568 for (
unsigned i = 0; i <
_tosb->size(); i++)
574 const unsigned nInput =
_input.size();
579 for (
unsigned i = 0; i < nInput; i++) {
608 (i == 14) || (i == 15) || (i == 16) || (i == 17) ||
609 (i == 41) || (i == 42) || (i == 43) || (i == 44) ||
610 (i == 49) || (i == 50) || (i == 51) || (i == 52) || (i == 53);
616 const string ni =
name() +
"InputSignalBundle";
618 _isb->push_back(& input);
621 const string no =
name() +
"OutputSignalBundle";
635 const string nt =
name() +
"TimingSignalBundle";
637 _tsb->push_back(& timing);
638 _tsb->push_back(& ftd);
641 const string nto =
name() +
"TimingOutSignalBundle";
650 if (input.active()) {
667 ifstream infile(fni.c_str(), ios::in);
669 cout <<
"TRGGDL !!! can not open file : " << fni << endl;
677 ifstream outfile(fno.c_str(), ios::in);
678 if (outfile.fail()) {
679 cout <<
"TRGGDL !!! can not open file : " << fno << endl;
687 ifstream algfile(fna.c_str(), ios::in);
688 if (algfile.fail()) {
689 cout <<
"TRGGDL !!! can not open file : " << fna << endl;
697 string::size_type s = ftd.find_last_of(
"/");
698 if (s != string::npos)
699 ftd = ftd.substr(s + 1);
700 if (ftd ==
"ftd_0.01") {
707 cout <<
" ftd=" << ftd << endl;
708 cout <<
"TRGGDL Input Bits" << endl;
709 for (
unsigned i = 0; i <
_input.size(); i++)
711 cout <<
"TRGGDL Output Bits" << endl;
712 for (
unsigned i = 0; i <
_output.size(); i++)
722 cout <<
"TRGGDL::getInput ... reading input data" << endl;
727 while (! ifs.eof()) {
750 cout <<
"TRGGDL::getOutput ... reading output data" << endl;
755 while (! ifs.eof()) {
778 cout <<
"TRGGDL::getAlgorithm ... reading algorithm data" << endl;
783 while (! ifs.eof()) {
795 const string w2 = cdr;
813 bool* in =
new bool[input.size()];
814 bool* ou =
new bool[14];
826 for (
unsigned i = 0; i < 13; i++) {
905 unsigned state = unsigned(reg.
subset(1, 3));
906 unsigned count = unsigned(reg.
subset(4, 3));
919 }
else if (state == 1) {
931 reg.
set(1, 3, state);
932 reg.
set(4, 3, count);
936 out.set(0, 1, timing);
937 out.set(1, 2, source);
942 cout <<
TRGDebug::tab(4) <<
"ftd,active,state,count=" << ftd <<
","
943 << active <<
"," << state
944 <<
"," << count << endl;
956 for (std::size_t i = 0; i < _inpBits.size(); i++) {
957 if (_inpBits[i]) h->Fill(i);
964 for (std::size_t i = 0; i < _ftdBits.size(); i++) {
965 if (_ftdBits[i]) h->Fill(i);
972 for (std::size_t i = 0; i < _psnBits.size(); i++) {
973 if (_psnBits[i]) h->Fill(i);