9 #define TRG_SHORT_NAMES
10 #define TRGGDL_SHORT_NAMES
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"
22 #include <framework/datastore/StoreObjPtr.h>
24 #include <mdst/dataobjects/TRGSummary.h>
25 #include <trg/grl/dataobjects/TRGGRLInfo.h>
27 #include <framework/logging/Logger.h>
33 #define N_TIMING_REGISTERS 4
43 void ftd_0_01(
bool* b,
bool* i);
51 (* TRGGDL::_ftd)(
bool* b,
bool* i) = 0;
54 TRGGDL::name(
void)
const
60 TRGGDL::version(
void)
const
62 return string(
"TRGGDL 0.01");
66 TRGGDL::getTRGGDL(
const string& configFile,
67 unsigned simulationMode,
68 unsigned fastSimulationMode,
69 unsigned firmwareSimulationMode,
70 const std::string& Phase,
72 const std::string& algFilePath,
74 double timquality_threshold_sfin,
75 double timquality_threshold_fine)
82 if (configFile !=
"good-bye") {
83 _gdl =
new TRGGDL(configFile,
86 firmwareSimulationMode,
91 timquality_threshold_sfin,
92 timquality_threshold_fine);
94 cout <<
"TRGGDL::getTRGGDL ... good-bye" << endl;
103 TRGGDL::getTRGGDL(
void)
106 cout <<
"TRGGDL::getTRGGDL !!! TRGGDL is not created yet" << endl;
110 TRGGDL::TRGGDL(
const string& configFile,
111 unsigned simulationMode,
112 unsigned fastSimulationMode,
113 unsigned firmwareSimulationMode,
114 const std::string& Phase,
116 const std::string& algFilePath,
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),
126 _algFilePath(algFilePath),
127 _clock(Belle2_GDL::GDLSystemClock),
131 _timquality_threshold_sfin(timquality_threshold_sfin),
132 _timquality_threshold_fine(timquality_threshold_fine),
133 _algFromDB(algFromDB)
150 _inpBitNames.push_back(std::string(
m_InputBitsDB->getinbitname(i)));
153 for (
int i = 0; i < m_FTDLBitsDB->getnoutbit(); i++) {
154 _oupBitNames.push_back(std::string(m_FTDLBitsDB->getoutbitname(i)));
164 B2DEBUG(20,
"TRGGDL::initialize, inputBits: " << i <<
", " <<
m_InputBitsDB->getinbitname(i));
166 for (
int i = 0; i < m_FTDLBitsDB->getnoutbit(); i++) {
167 B2DEBUG(20,
"TRGGDL::initialize, outputBits: " << i <<
", " << m_FTDLBitsDB->getoutbitname(i));
169 for (
int i = 0; i < m_AlgsDB->getnalgs(); i++) {
170 B2DEBUG(20,
"TRGGDL::initialize, algs: " << i <<
", " << m_AlgsDB->getalg(i));
186 if (msg.find(
"name") != string::npos ||
187 msg.find(
"version") != string::npos ||
188 msg.find(
"detail") != string::npos ||
191 if (msg.find(
"detail") != string::npos ||
192 msg.find(
"state") != string::npos) {
243 B2DEBUG(20,
"TRGGDL::fastSimulation starts.");
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) {
260 B2DEBUG(20,
"#Algs and #Ftdl is different");
261 N_OutputBits = N_AlgsBits;
266 B2WARNING(
"TRGGDL::fastSimulation(): TRGSummary exist already, check it!!!!");
272 B2WARNING(
"TRGGRLInfo doesn't exist!!!!");
275 if (
_debugLevel > 89) printf(
"TRGGDL:TRGGRLInfo found.\n");
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);
284 GDLResult->setInputBits(i / 32 - 1, input_summary);
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);
296 int L1Summary_psnm = 0;
298 std::vector<std::string> algs;
299 std::ifstream isload(_algFilePath.c_str(), std::ios::in);
301 while (std::getline(isload, str)) {
307 for (
int i = 0; i < N_OutputBits; i++) {
308 if (
_debugLevel > 89) printf(
"TRGGDL:ABB:i(%d)\n", i);
311 GDLResult->setFtdlBits(i / 32 - 1, L1Summary);
312 GDLResult->setPsnmBits(i / 32 - 1, L1Summary_psnm);
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));
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);
343 timQuality = TRGSummary::TTYQ_SFIN;
346 timQuality = TRGSummary::TTYQ_FINE;
348 timQuality = TRGSummary::TTYQ_CORS;
352 B2DEBUG(20,
"TRGGDL::set timing quality, jitter = " << jitter <<
": timQuality = " << timQuality <<
" sfin threshold = " <<
357 GDLResult->setTimQuality(timQuality);
370 if (
_debugLevel > 9) printf(
"TRGGDL:dataSimulation Start\n");
371 unsigned _evt = bevt->getEvent();
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;
390 printf(
"TRGGDL:N_InputBits(%d), N_OutputBits(%d)\n", N_InputBits, N_OutputBits);
394 B2WARNING(
"TRGGDL::dataSimulation(): TRGSummary not found. Check it!!!!");
398 if (
_debugLevel > 89) printf(
"TRGGDL:TRGSummary Found.\n");
402 for (
int i = 0; i < N_InputBits; i++) {
404 try { inputBit = GDLResult->testInput(i); }
405 catch (
const std::exception&) { inputBit =
false; }
406 _inpBits.push_back(inputBit);
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);
415 if (doprescale(m_PrescalesDB->getprescales(i))) {
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")) {
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,
433 m_FTDLBitsDB->getoutbitname(i));
440 std::vector<std::string> algs;
441 std::ifstream isload(_algFilePath.c_str(), std::ios::in);
443 while (std::getline(isload, str)) {
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);
454 if (doprescale(m_PrescalesDB->getprescales(i))) {
458 psnm_fired ? _psnBits.push_back(
true) : _psnBits.push_back(
false);
459 GDLResult->setPreScale((i / 32), (i % 32), m_PrescalesDB->getprescales(i));
469 TRGGDL::isFiredFTDL(std::vector<bool> input, std::string alg)
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;
475 bool not_flag =
false;
476 unsigned begin_word = 0;
477 unsigned word_length = 0;
480 for (
unsigned i = 0; i < alg.length(); i++) {
481 if ((
'0' <= cst[i] && cst[i] <=
'9') ||
482 (
'_' == cst[i]) || (
'!' == cst[i])) {
485 if (i == alg.length() - 1) {
486 bool fired = input[atoi(alg.substr(begin_word, word_length).c_str())];
488 alg.substr(begin_word, word_length).c_str()
489 <<
"(" << fired <<
")");
490 if (((!not_flag && fired) || (not_flag && !fired)) && result_the_term) {
505 if (i == alg.length() - 1) {
507 bool fired = input[atoi(alg.substr(begin_word, word_length).c_str())];
509 alg.substr(begin_word, word_length).c_str()
510 <<
"(" << fired <<
")");
511 if (((!not_flag && fired) || (not_flag && !fired)) && result_the_term) {
517 }
else if (
'+' == cst[i] || i == alg.length() - 1) {
520 if (result_the_term) {
521 bool fired = input[atoi(alg.substr(begin_word, word_length).c_str())];
523 alg.substr(begin_word, word_length).c_str()
524 <<
"(" << fired <<
")");
525 if ((!not_flag && fired) || (not_flag && !fired)) {
533 reading_word =
false;
536 if (result_the_term) {
542 result_the_term =
true;
547 if (result_the_term) {
549 bool fired = input[atoi(alg.substr(begin_word, word_length).c_str())];
551 alg.substr(begin_word, word_length).c_str()
552 <<
"(" << fired <<
")");
553 if ((!not_flag && fired) || (not_flag && !fired)) {
556 result_the_term =
false;
561 reading_word =
false;
570 bool TRGGDL::doprescale(
int f)
572 if (f == 0)
return false;
573 if (f == 1)
return true;
574 double ran = gRandom->Uniform(f);
575 return (ceil(ran) == f);
589 for (
unsigned i = 0; i <
_isb->size(); i++)
594 for (
unsigned i = 0; i <
_osb->size(); i++)
599 for (
unsigned i = 0; i <
_tsb->size(); i++)
604 for (
unsigned i = 0; i <
_tosb->size(); i++)
610 const unsigned nInput =
_input.size();
615 for (
unsigned i = 0; i < nInput; i++) {
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);
652 const string ni =
name() +
"InputSignalBundle";
654 _isb->push_back(& input);
657 const string no =
name() +
"OutputSignalBundle";
671 const string nt =
name() +
"TimingSignalBundle";
673 _tsb->push_back(& timing);
674 _tsb->push_back(& ftd);
677 const string nto =
name() +
"TimingOutSignalBundle";
686 if (input.active()) {
703 ifstream infile(fni.c_str(), ios::in);
705 cout <<
"TRGGDL !!! can not open file : " << fni << endl;
713 ifstream outfile(fno.c_str(), ios::in);
714 if (outfile.fail()) {
715 cout <<
"TRGGDL !!! can not open file : " << fno << endl;
723 ifstream algfile(fna.c_str(), ios::in);
724 if (algfile.fail()) {
725 cout <<
"TRGGDL !!! can not open file : " << fna << endl;
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") {
743 cout <<
" ftd=" << ftd << endl;
744 cout <<
"TRGGDL Input Bits" << endl;
745 for (
unsigned i = 0; i <
_input.size(); i++)
747 cout <<
"TRGGDL Output Bits" << endl;
748 for (
unsigned i = 0; i <
_output.size(); i++)
758 cout <<
"TRGGDL::getInput ... reading input data" << endl;
763 while (! ifs.eof()) {
786 cout <<
"TRGGDL::getOutput ... reading output data" << endl;
791 while (! ifs.eof()) {
814 cout <<
"TRGGDL::getAlgorithm ... reading algorithm data" << endl;
819 while (! ifs.eof()) {
831 const string w2 = cdr;
849 bool* in =
new bool[input.size()];
850 bool* ou =
new bool[14];
862 for (
unsigned i = 0; i < 13; i++) {
941 unsigned state = unsigned(reg.
subset(1, 3));
942 unsigned count = unsigned(reg.
subset(4, 3));
955 }
else if (state == 1) {
967 reg.
set(1, 3, state);
968 reg.
set(4, 3, count);
972 out.set(0, 1, timing);
973 out.set(1, 2, source);
978 cout <<
TRGDebug::tab(4) <<
"ftd,active,state,count=" << ftd <<
","
979 << active <<
"," << state
980 <<
"," << count << endl;
992 for (std::size_t i = 0; i < _inpBits.size(); i++) {
993 if (_inpBits[i]) h->Fill(i);
1000 for (std::size_t i = 0; i < _ftdBits.size(); i++) {
1001 if (_ftdBits[i]) h->Fill(i);
1008 for (std::size_t i = 0; i < _psnBits.size(); i++) {
1009 if (_psnBits[i]) h->Fill(i);
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()));
bool create(bool replace=false)
Create a default object in the data store.
Type-safe access to single objects in the data store.
The instance of TRGGDL is a singleton.
TRGSignalBundle * _isb
Input signal bundle.
TRGSignalBundle * _tosb
Timing output signal bundle.
unsigned _simulationMode
Simulation mode.
std::string _configFilename
GDL configuration filename.
std::vector< std::string > _algorithm
Algorithm.
int _debugLevel
Debug level.
double _timquality_threshold_sfin
Threshold to determine timing quality flag with MC truth: super fine.
std::vector< std::string > _output
Output names.
const TRGClock & _clock
GDL trigger system clock.
DBObjPtr< TRGGDLDBInputBits > m_InputBitsDB
Data base of GDL input bits.
TRGSignalBundle * _tsb
Timing input signal bundle.
DBObjPtr< HardwareClockSettings > m_hwClock
Hardware Clocks.
double _timquality_threshold_fine
Threshold to determine timing quality flag with MC truth: fine.
std::vector< std::string > _input
Input names.
TRGSignalBundle * _osb
Output signal bundle.
StoreObjPtr< SimClockState > m_simClockState
generated hardware clock state
A class to represent a bundle of SignalVectors.
A class to represent a bundle of digitized signals.
A class to represent a digitized signal. Unit is nano second.
A class to represent a state of multi bits.
ETimingQuality
trigger timing type quality
A class to represent a signal timing in the trigger system.
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.
void configure(void)
configures trigger modules for firmware simulation.
static std::string tab(void)
returns tab spaces.
static TRGState decision(const TRGState &input)
Makes bit pattern(state) using input bit pattern(state).
std::string name(void) const
returns name.
void fastClear(void)
clears TRGGDL information.
TRGState subset(unsigned i, unsigned n) const
returns subset from i with n bits.
void accumulatePsn(TH1I *)
Accumulate bit info in histogram.
void terminate(void)
terminates when run is finished
TRGTime & reverse(void)
reverse edge.
void accumulateFtd(TH1I *)
Accumulate bit info in histogram.
static void enterStage(const std::string &stageName)
Declare that you enter new stage.
const TRGState & set(unsigned position, bool state=true)
sets state at bit i.
static void(* _ftd)(bool *out, bool *in)
Function to simulate final trigger decision.
void getOutput(std::ifstream &ifs)
Read output data definition.
TRGTime & shift(int unit)
delays by clock unit.
void initialize(void)
initializes GDL.
void dataSimulation(void)
Data simulation.
void dump(const std::string &message) const
dumps debug information.
static TRGState timingDecision(const TRGState &input, TRGState ®isters, bool &logicStillActive)
Makes timing decision.
static int level(void)
returns the debug level.
void update(bool mcAnalysis=true)
updates TRGGDL information.
static void leaveStage(const std::string &stageName)
Declare that you leave a stage.
void accumulateInp(TH1I *)
Accumulate bit info in histogram.
void getAlgorithm(std::ifstream &ifs)
Read algorithm data definition.
void fastSimulation(void)
Fast simulation.
void simulate(void)
fast trigger simulation.
void clear(void)
clears all TRGGDL information.
void firmwareSimulation(void)
Firmware simulation.
void getInput(std::ifstream &ifs)
Read input data definition.
virtual ~TRGGDL()
Destructor.
void dump(const std::string &message="", const std::string &pre="") const
dumps contents.
Abstract base class for different kinds of events.