13 #include "eformat/eformat.h"
14 #include "eformat/SourceIdentifier.h"
21 #include "CLHEP/Random/RandomEngine.h"
22 #include "CLHEP/Random/RandFlat.h"
34 const std::string&
name,
43 delete m_ctpDataFormat;
44 for(
auto &
x : m_internalTrigger) {
53 ATH_MSG_DEBUG(
"Set configuration with CTP version " << m_ctpVersionNumber );
55 return createTriggerConfigMaps(
l1menu);
62 ATH_MSG_DEBUG(
"Creating trigger configuration maps from run-3-style menu");
64 std::vector<unsigned int>
bg{1};
65 std::vector<unsigned int> bgEmpty{1};
68 for (
size_t i = 0;
i < 16; ++
i) {
70 m_internalTrigger[ bgrp->name() ] = bgrp;
74 for(
int rndmIdx = 0; rndmIdx<4; rndmIdx++) {
76 m_internalTrigger[ rndm->name() ] = rndm;
80 m_thrConfigMap = std::make_unique<ThresholdMap>( &
l1menu );
83 m_itemConfigMap = std::make_unique<ItemMap>( &
l1menu );
85 return StatusCode::SUCCESS;
90 std::vector<uint32_t> & tip )
const
92 tip.resize( m_ctpDataFormat->getTIPwords(), 0 );
94 for(
auto &
entry : thrMultiMap ) {
95 const std::string & thrName =
entry.first;
96 size_t multiplicity =
entry.second;
103 auto x = m_internalTrigger.find(thrName);
104 if(
x != m_internalTrigger.end()) {
107 size_t wordNr = m_ctpDataFormat->getTIPwords() - 1;
108 size_t posWithinWord =
x->second->pitPos() % 32;
110 tip[wordNr] |= ( 1L << posWithinWord );
117 size_t nBits = ctpTT.
endBit() - startBit + 1;
120 if(multiplicity >= (1U<<nBits)) {
121 multiplicity = (1U<<nBits)-1;
124 size_t wordNr = startBit / 32;
125 size_t posWithinWord = startBit % 32;
130 tip[wordNr] |=
result & 0xFFFFFFFF;
131 if(wordNr+1 < m_ctpDataFormat->getTIPwords()-1) {
139 ATH_MSG_DEBUG(
"REGTEST - " <<
"TIP word #" << std::dec << wrdNr++
140 <<
" is: 0x" << std::hex << std::setw( 8 ) << std::setfill(
'0' ) << word );
143 return StatusCode::SUCCESS;
150 std::map<std::string, unsigned int> & itemDecisionMap,
151 CLHEP::HepRandomEngine* rndmEngine )
const
154 itemDecisionMap.clear();
157 for(
const auto & itemName : m_itemConfigMap->itemNames() ) {
158 auto ctpItem = m_itemConfigMap->getItem(itemName);
160 bool pass_beforePrescale = ctpItem->evaluate(thrMultiMap);
161 bool pass_afterPrescale =
false;
162 bool pass_afterVeto =
false;
164 if ( pass_beforePrescale ) {
165 long random = CLHEP::RandFlat::shootInt( rndmEngine,
pow(2,24) );
167 pass_afterPrescale = (random >=
cut) && (
cut > 0);
168 pass_afterVeto = pass_afterPrescale;
171 unsigned int result = (pass_beforePrescale ? TBP : 0) + (pass_afterPrescale ? TAP : 0) + (pass_afterVeto ? TAV : 0);
173 itemDecisionMap[itemName] =
result;
177 return StatusCode::FAILURE;
179 return StatusCode::SUCCESS;
184 std::vector<uint32_t> & tbp, std::vector<uint32_t> &
tap, std::vector<uint32_t> & tav,
187 tbp.resize( m_ctpDataFormat->getTBPwords(), 0 );
188 tap.resize( m_ctpDataFormat->getTAPwords(), 0 );
189 tav.resize( m_ctpDataFormat->getTAVwords(), 0 );
192 for(
auto &
entry : itemDecisionMap ) {
193 const std::string & itemName =
entry.first;
195 bool passBP = (
result & TBP) != 0;
196 bool passAP = (
result & TAP) != 0;
197 bool passAV = (
result & TAV) != 0;
202 auto l1Item = m_itemConfigMap->getItem( itemName );
208 auto ctpId = l1Item->ctpId();
209 unsigned int wordNr = ctpId / 32;
210 unsigned int posWithinWord = ctpId % 32;
212 if( passBP ) { tbp[wordNr] |= 1L << posWithinWord; }
213 if( passAP ) {
tap[wordNr] |= 1L << posWithinWord; }
214 if( passAV ) { tav[wordNr] |= 1L << posWithinWord; }
217 " is " << ( !passBP ?
"INACTIVE" : ( passAV ?
"ACTIVE" :
"ACTIVE (but PRESCALED)" ) ) );
219 ATH_MSG_DEBUG(
"REGTEST - " <<
"TriggerType byte is: 0x" << std::setw( 2 ) << std::setfill(
'0' ) << std::hex <<
int(
triggerType) );
221 return StatusCode::SUCCESS;
227 std::unique_ptr<LVL1CTP::CTPSLink>
229 const std::vector<uint32_t> & tbp,
230 const std::vector<uint32_t> &
tap,
231 const std::vector<uint32_t> & tav,
232 const std::vector<uint32_t> & tip,
233 const std::vector<uint32_t> &
extra,
239 std::vector<uint32_t> roi_vector;
240 roi_vector.push_back( eventID.time_stamp_ns_offset() );
241 roi_vector.push_back( eventID.time_stamp() );
243 roi_vector.insert(roi_vector.end(), tip.begin(), tip.end());
244 roi_vector.insert(roi_vector.end(), tbp.begin(), tbp.end());
245 roi_vector.insert(roi_vector.end(),
tap.begin(),
tap.end());
246 roi_vector.insert(roi_vector.end(), tav.begin(), tav.end());
247 roi_vector.insert(roi_vector.end(),
extra.begin(),
extra.end());
256 roi_vector.insert(roi_vector.end(), trailer.begin(), trailer.end());
264 const uint32_t source_id{eformat::helper::SourceIdentifier(eformat::TDAQ_CTP, 1).code()};
267 uint32_t version_word = eformat::DEFAULT_ROD_VERSION;
269 version_word |= ((
extra.size() & m_ctpDataFormat->getProgrammableExtraWordsMask()) << m_ctpDataFormat->getProgrammableExtraWordsShift());
270 version_word |= ((l1a_pos & m_ctpDataFormat->getL1APositionMask()) << m_ctpDataFormat->getL1APositionShift());
271 version_word |= ((m_ctpVersionNumber & m_ctpDataFormat->getCTPFormatVersionMask()) << m_ctpDataFormat->getCTPFormatVersionShift());
277 helperHeader.
setBCID( eventID.bunch_crossing_id());
281 std::vector<unsigned int>
header(helperHeader.
header());
282 roi_vector.insert(roi_vector.begin(),
header.begin(),
header.end());
289 std::unique_ptr<CTPSLink>
result (
new CTPSLink( roi_vector, m_ctpVersionNumber ));
297 for(
auto & itemName : firedItems(tbp) ) {
298 ATH_MSG_VERBOSE(
"REGTEST - Items fired before prescale: " << itemName );
300 for(
auto & itemName : firedItems(
tap) ) {
301 ATH_MSG_VERBOSE(
"REGTEST - Items fired after prescale: " << itemName );
303 for(
auto & itemName : firedItems(tav) ) {
311 std::unique_ptr<CTP_RDO>
313 const std::vector<uint32_t> & tbp,
314 const std::vector<uint32_t> &
tap,
315 const std::vector<uint32_t> & tav,
316 const std::vector<uint32_t> & tip,
317 const std::vector<uint32_t> &
extra )
const
319 auto wrongSize = [
this](
const std::vector<uint32_t> &
vec,
uint32_t exp, std::string_view
name) {
320 if (
vec.size() ==
exp) {
return false;}
321 ATH_MSG_ERROR(
"Wrong " <<
name <<
" vector size passed to constructRDOResult, " <<
vec.size() <<
" instead of " <<
exp);
324 if (wrongSize(tip, m_ctpDataFormat->getTIPwords(),
"TIP")
325 || wrongSize(tbp, m_ctpDataFormat->getTBPwords(),
"TBP")
326 || wrongSize(
tap, m_ctpDataFormat->getTAPwords(),
"TAP")
327 || wrongSize(tav, m_ctpDataFormat->getTAVwords(),
"TAV")) {
331 std::vector<uint32_t>
data(
static_cast<size_t>(m_ctpDataFormat->getNumberTimeWords()),
uint32_t{0});
332 data.reserve(m_ctpDataFormat->getNumberTimeWords() + m_ctpDataFormat->getDAQwordsPerBunch() +
extra.size());
333 data.insert(
data.end(),tip.begin(),tip.end());
334 data.insert(
data.end(),tbp.begin(),tbp.end());
336 data.insert(
data.end(),tav.begin(),tav.end());
340 result->setTimeSec(eventID.time_stamp());
341 result->setTimeNanoSec(eventID.time_stamp_ns_offset());
351 std::vector<std::string>
353 std::vector<std::string> passedItems;
354 for(
const std::string & itemName : m_itemConfigMap->itemNames() ) {
355 auto item = m_itemConfigMap->getItem(itemName);
356 size_t idx =
item->ctpId() / 32;
357 size_t bit =
item->ctpId() % 32;
358 if ( triggerWords[
idx] % (1L << bit) ) {
359 passedItems.push_back(
item->name());
362 std::sort(passedItems.begin(), passedItems.end());