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,
const bool* i);
62 return string(
"TRGGDL 0.01");
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)
91 timquality_threshold_sfin,
92 timquality_threshold_fine);
94 cout <<
"TRGGDL::getTRGGDL ... good-bye" << endl;
106 cout <<
"TRGGDL::getTRGGDL !!! TRGGDL is not created yet" << endl;
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);
300 while (std::getline(isload, str)) {
305 for (
int i = 0; i < N_OutputBits; i++) {
306 if (
_debugLevel > 89) printf(
"TRGGDL:ABB:i(%d)\n", i);
309 GDLResult->setFtdlBits(i / 32 - 1, L1Summary);
310 GDLResult->setPsnmBits(i / 32 - 1, L1Summary_psnm);
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));
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);
341 timQuality = TRGSummary::TTYQ_SFIN;
344 timQuality = TRGSummary::TTYQ_FINE;
346 timQuality = TRGSummary::TTYQ_CORS;
350 B2DEBUG(20,
"TRGGDL::set timing quality, jitter = " << jitter <<
": timQuality = " << timQuality <<
" sfin threshold = " <<
355 GDLResult->setTimQuality(timQuality);
368 if (
_debugLevel > 9) printf(
"TRGGDL:dataSimulation Start\n");
369 unsigned _evt = bevt->getEvent();
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;
388 printf(
"TRGGDL:N_InputBits(%d), N_OutputBits(%d)\n", N_InputBits, N_OutputBits);
392 B2WARNING(
"TRGGDL::dataSimulation(): TRGSummary not found. Check it!!!!");
396 if (
_debugLevel > 89) printf(
"TRGGDL:TRGSummary Found.\n");
400 for (
int i = 0; i < N_InputBits; i++) {
402 try { inputBit = GDLResult->testInput(i); }
403 catch (
const std::exception&) { inputBit =
false; }
404 _inpBits.push_back(inputBit);
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);
413 if (doprescale(m_PrescalesDB->getprescales(i))) {
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")) {
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,
431 m_FTDLBitsDB->getoutbitname(i));
438 std::vector<std::string> algs;
439 std::ifstream isload(_algFilePath.c_str(), std::ios::in);
440 while (std::getline(isload, str)) {
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);
450 if (doprescale(m_PrescalesDB->getprescales(i))) {
454 psnm_fired ? _psnBits.push_back(
true) : _psnBits.push_back(
false);
455 GDLResult->setPreScale((i / 32), (i % 32), m_PrescalesDB->getprescales(i));
465 TRGGDL::isFiredFTDL(std::vector<bool> input, std::string alg)
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;
471 bool not_flag =
false;
472 unsigned begin_word = 0;
473 unsigned word_length = 0;
476 for (
unsigned i = 0; i < alg.length(); i++) {
477 if ((
'0' <= cst[i] && cst[i] <=
'9') ||
478 (
'_' == cst[i]) || (
'!' == cst[i])) {
481 if (i == alg.length() - 1) {
482 bool fired = input[atoi(alg.substr(begin_word, word_length).c_str())];
484 alg.substr(begin_word, word_length).c_str()
485 <<
"(" << fired <<
")");
486 if (((!not_flag && fired) || (not_flag && !fired)) && result_the_term) {
501 if (i == alg.length() - 1) {
503 bool fired = input[atoi(alg.substr(begin_word, word_length).c_str())];
505 alg.substr(begin_word, word_length).c_str()
506 <<
"(" << fired <<
")");
508 if (((!not_flag && fired) || (not_flag && !fired)) && result_the_term) {
514 }
else if (
'+' == cst[i] || i == alg.length() - 1) {
517 if (result_the_term) {
518 bool fired = input[atoi(alg.substr(begin_word, word_length).c_str())];
520 alg.substr(begin_word, word_length).c_str()
521 <<
"(" << fired <<
")");
522 if ((!not_flag && fired) || (not_flag && !fired)) {
530 reading_word =
false;
533 if (result_the_term) {
539 result_the_term =
true;
544 if (result_the_term) {
546 bool fired = input[atoi(alg.substr(begin_word, word_length).c_str())];
548 alg.substr(begin_word, word_length).c_str()
549 <<
"(" << fired <<
")");
550 if ((!not_flag && fired) || (not_flag && !fired)) {
553 result_the_term =
false;
558 reading_word =
false;
567 bool TRGGDL::doprescale(
int f)
569 if (f == 0)
return false;
570 if (f == 1)
return true;
571 double ran = gRandom->Uniform(f);
572 return (ceil(ran) == f);
586 for (
unsigned i = 0; i <
_isb->size(); i++)
591 for (
unsigned i = 0; i <
_osb->size(); i++)
596 for (
unsigned i = 0; i <
_tsb->size(); i++)
601 for (
unsigned i = 0; i <
_tosb->size(); i++)
607 const unsigned nInput =
_input.size();
612 for (
unsigned i = 0; i < nInput; i++) {
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);
649 const string ni =
name() +
"InputSignalBundle";
651 _isb->push_back(& input);
654 const string no =
name() +
"OutputSignalBundle";
668 const string nt =
name() +
"TimingSignalBundle";
670 _tsb->push_back(& timing);
671 _tsb->push_back(& ftd);
674 const string nto =
name() +
"TimingOutSignalBundle";
683 if (input.active()) {
700 ifstream infile(fni.c_str(), ios::in);
702 cout <<
"TRGGDL !!! can not open file : " << fni << endl;
710 ifstream outfile(fno.c_str(), ios::in);
711 if (outfile.fail()) {
712 cout <<
"TRGGDL !!! can not open file : " << fno << endl;
720 ifstream algfile(fna.c_str(), ios::in);
721 if (algfile.fail()) {
722 cout <<
"TRGGDL !!! can not open file : " << fna << endl;
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") {
740 cout <<
" ftd=" << ftd << endl;
741 cout <<
"TRGGDL Input Bits" << endl;
742 for (
unsigned i = 0; i <
_input.size(); i++)
744 cout <<
"TRGGDL Output Bits" << endl;
745 for (
unsigned i = 0; i <
_output.size(); i++)
755 cout <<
"TRGGDL::getInput ... reading input data" << endl;
759 while (! ifs.eof()) {
780 cout <<
"TRGGDL::getOutput ... reading output data" << endl;
784 while (! ifs.eof()) {
805 cout <<
"TRGGDL::getAlgorithm ... reading algorithm data" << endl;
809 while (! ifs.eof()) {
821 const string w2 = cdr;
837 bool* in =
new bool[input.size()];
838 bool* ou =
new bool[14];
850 for (
unsigned i = 0; i < 13; i++) {
929 unsigned state = unsigned(reg.
subset(1, 3));
930 unsigned count = unsigned(reg.
subset(4, 3));
943 }
else if (state == 1) {
955 reg.
set(1, 3, state);
956 reg.
set(4, 3, count);
960 out.set(0, 1, timing);
961 out.set(1, 2, source);
966 cout <<
TRGDebug::tab(4) <<
"ftd,active,state,count=" << ftd <<
","
967 << active <<
"," << state
968 <<
"," << count << endl;
980 for (std::size_t i = 0; i < _inpBits.size(); i++) {
981 if (_inpBits[i]) h->Fill(i);
988 for (std::size_t i = 0; i < _ftdBits.size(); i++) {
989 if (_ftdBits[i]) h->Fill(i);
996 for (std::size_t i = 0; i < _psnBits.size(); i++) {
997 if (_psnBits[i]) h->Fill(i);
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()));
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.
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.
int debugLevel(void) const
returns debug level.
static std::string tab(void)
returns tab spaces.
static TRGState decision(const TRGState &input)
Makes bit pattern(state) using input bit pattern(state).
static void(* _ftd)(bool *out, const bool *in)
Function to simulate final trigger decision.
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
static TRGGDL * getTRGGDL(void)
returns TRGGDL object.
TRGTime & reverse(void)
reverse edge.
void accumulateFtd(TH1I *)
Accumulate bit info in histogram.
static std::string cdrstring(const std::string &s)
CERNLIB cdr.
static void enterStage(const std::string &stageName)
Declare that you enter new stage.
static TRGGDL * _gdl
GDL singleton.
const TRGState & set(unsigned position, bool state=true)
sets state at bit i.
void getOutput(std::ifstream &ifs)
Read output data definition.
TRGTime & shift(int unit)
delays by clock unit.
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.
unsigned firmwareSimulationMode(void) const
returns firmware simulation mode.
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.
static std::string carstring(const std::string &s)
CERNLIB car.
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.
std::string version(void) const
returns version.
std::string configFile(void) const
returns configuration file name.
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.