5 #include <unordered_map> 8 #include "tmEventSetup/tmEventSetup.hh" 9 #include "tmEventSetup/esTriggerMenu.hh" 10 #include "tmEventSetup/esAlgorithm.hh" 11 #include "tmEventSetup/esCondition.hh" 12 #include "tmEventSetup/esObject.hh" 13 #include "tmEventSetup/esCut.hh" 14 #include "tmEventSetup/esScale.hh" 15 #include "tmGrammar/Algorithm.hh" 45 edm::LogInfo(
"L1-O2O: L1TGlobalPrescalesVetosOnlineProd" ) <<
"Producing L1TGlobalPrescalesVetos with TSC:RS key = " << objectKey ;
47 if( objectKey.empty() ){
48 edm::LogError(
"L1-O2O: L1TGlobalPrescalesVetosOnlineProd" ) <<
"Key is empty";
49 throw std::runtime_error(
"Empty objecKey");
52 unsigned int m_numberPhysTriggers = 512;
55 std::unordered_map<std::string, int, std::hash<std::string>> algoName2bit;
57 std::string uGTtscKey = objectKey.substr(0, objectKey.find(
":"));
58 std::string uGTrsKey = objectKey.substr(objectKey.find(
":")+1, std::string::npos);
65 std::vector< std::string > queryStrings ;
66 queryStrings.push_back(
"L1_MENU" ) ;
78 edm::LogError(
"L1-O2O" ) <<
"Cannot get UGT_KEYS.L1_MENU for ID = " << uGTtscKey <<
" " ;
79 throw std::runtime_error(
"Broken key");
82 if( !queryResult.
fillVariable(
"L1_MENU", l1_menu_key) ) l1_menu_key =
"";
84 edm::LogInfo(
"L1-O2O: L1TGlobalPrescalesVetosOnlineProd" ) <<
"Producing L1TUtmTriggerMenu with key =" << l1_menu_key;
86 if( uGTtscKey.empty() ){
87 edm::LogError(
"L1-O2O: L1TGlobalPrescalesVetosOnlineProd" ) <<
"TSC key is empty, returning";
88 throw std::runtime_error(
"Empty objectKey");
91 std::vector< std::string > queryColumns;
92 queryColumns.push_back(
"CONF" ) ;
103 edm::LogError(
"L1-O2O: L1TGlobalPrescalesVetosOnlineProd" ) <<
"Cannot get UGT_L1_MENU.CONF for ID = " << l1_menu_key;
104 throw std::runtime_error(
"Broken key");
110 std::istringstream iss(l1Menu);
112 std::shared_ptr<L1TUtmTriggerMenu> pMenu(
113 const_cast<L1TUtmTriggerMenu*>(
114 reinterpret_cast<const L1TUtmTriggerMenu*>(tmeventsetup::getTriggerMenu(iss))
118 for(
auto algo : pMenu->getAlgorithmMap())
119 algoName2bit[
algo.first] =
algo.second.getIndex();
123 for(
unsigned int algoBit = 0; algoBit < m_numberPhysTriggers; algoBit++)
124 algoName2bit[std::to_string(algoBit)] = algoBit;
128 std::vector< std::string > queryColumns;
129 queryColumns.push_back(
"ALGOBX_MASK" ) ;
130 queryColumns.push_back(
"ALGO_FINOR_MASK" ) ;
131 queryColumns.push_back(
"ALGO_FINOR_VETO" ) ;
132 queryColumns.push_back(
"ALGO_PRESCALE" ) ;
134 std::string prescale_key, bxmask_key, mask_key, vetomask_key;
135 std::string xmlPayload_prescale, xmlPayload_mask_algobx, xmlPayload_mask_finor, xmlPayload_mask_veto;
138 prescale_key = subKeys[
"ALGO_PRESCALE"];
139 bxmask_key = subKeys[
"ALGOBX_MASK"];
140 mask_key = subKeys[
"ALGO_FINOR_MASK"];
141 vetomask_key = subKeys[
"ALGO_FINOR_VETO"];
146 }
catch ( std::runtime_error &
e ) {
147 edm::LogError(
"L1-O2O: L1TGlobalPrescalesVetosOnlineProd" ) << e.what();
148 throw std::runtime_error(
"Broken key");
153 std::ofstream output1(
std::string(
"/tmp/").
append(prescale_key.substr(0,prescale_key.find(
"/"))).append(
".xml"));
154 output1<<xmlPayload_prescale;
156 std::ofstream output2(
std::string(
"/tmp/").
append(mask_key.substr(0,mask_key.find(
"/"))).append(
".xml"));
157 output2<<xmlPayload_mask_finor;
159 std::ofstream output3(
std::string(
"/tmp/").
append(bxmask_key.substr(0,bxmask_key.find(
"/"))).append(
".xml"));
160 output3<<xmlPayload_mask_algobx;
162 std::ofstream output4(
std::string(
"/tmp/").
append(vetomask_key.substr(0,vetomask_key.find(
"/"))).append(
".xml"));
163 output4<<xmlPayload_mask_veto;
168 std::vector<std::vector<int> > prescales;
169 std::vector<unsigned int> triggerMasks;
170 std::vector<int> triggerVetoMasks;
171 std::map<int, std::vector<int> > triggerAlgoBxMaskAlgoTrig;
176 ts_prescale.
addProcessor(
"uGtProcessor",
"uGtProcessor",
"-1",
"-1");
183 const std::map<std::string, l1t::Parameter> &settings_prescale = ts_prescale.
getParameters(
"uGtProcessor");
184 std::map<std::string,unsigned int> prescaleColumns = settings_prescale.at(
"prescales").getColumnIndices();
186 unsigned int numColumns_prescale = prescaleColumns.size();
187 int nPrescaleSets = numColumns_prescale - 1;
188 std::vector<std::string> algoNames = settings_prescale.at(
"prescales").getTableColumn<
std::string>(
"algo/prescale-index");
190 if( nPrescaleSets > 0 ){
192 for(
int iSet=0; iSet<nPrescaleSets; iSet++ ){
193 prescales.push_back(std::vector<int>());
194 for(
unsigned int iBit = 0; iBit < m_numberPhysTriggers; ++iBit ){
195 int inputDefaultPrescale = 0;
196 prescales[iSet].push_back(inputDefaultPrescale);
200 for(
auto &
col : prescaleColumns){
201 if(
col.second<1 )
continue;
202 int iSet =
col.second - 1;
203 std::vector<unsigned int> prescalesForSet = settings_prescale.at(
"prescales").getTableColumn<
unsigned int>(
col.first.c_str());
204 for(
unsigned int row=0; row<prescalesForSet.size(); row++){
205 unsigned int prescale = prescalesForSet[row];
207 unsigned int algoBit = algoName2bit[
algoName];
208 prescales[iSet][algoBit] = prescale;
219 ts_mask_finor.
addProcessor(
"uGtProcessor",
"uGtProcessor",
"-1",
"-1");
226 const std::map<std::string, l1t::Parameter>& settings_mask_finor = ts_mask_finor.
getParameters(
"uGtProcessor");
228 std::vector<std::string> algo_mask_finor = settings_mask_finor.at(
"finorMask").getTableColumn<
std::string>(
"algo");
229 std::vector<unsigned int> mask_mask_finor = settings_mask_finor.at(
"finorMask").getTableColumn<
unsigned int>(
"mask");
232 unsigned int default_finor_mask = 1;
233 auto default_finor_row =
234 std::find_if( algo_mask_finor.cbegin(),
235 algo_mask_finor.cend(),
238 return strcasecmp(
"all",
s.c_str()) == 0;
241 if( default_finor_row == algo_mask_finor.cend() ){
243 <<
"\nWarning: No default found in FinOR mask xml, use 1 (unmasked) as default" 246 default_finor_mask = mask_mask_finor[
std::distance( algo_mask_finor.cbegin(), default_finor_row ) ];
249 for(
unsigned int iAlg=0; iAlg < m_numberPhysTriggers; iAlg++ )
250 triggerMasks.push_back(default_finor_mask);
252 for(
unsigned int row=0; row<algo_mask_finor.size(); row++){
254 if( strcasecmp(
"all", algoName.c_str()) == 0 )
continue;
255 unsigned int algoBit = algoName2bit[
algoName];
256 unsigned int mask = mask_mask_finor[row];
257 if( algoBit < m_numberPhysTriggers ) triggerMasks[algoBit] =
mask;
265 ts_mask_veto.
addProcessor(
"uGtProcessor",
"uGtProcessor",
"-1",
"-1");
272 const std::map<std::string, l1t::Parameter>& settings_mask_veto = ts_mask_veto.
getParameters(
"uGtProcessor");
273 std::vector<std::string> algo_mask_veto = settings_mask_veto.at(
"vetoMask").getTableColumn<
std::string>(
"algo");
274 std::vector<unsigned int> veto_mask_veto = settings_mask_veto.at(
"vetoMask").getTableColumn<
unsigned int>(
"veto");
277 unsigned int default_veto_mask = 1;
278 auto default_veto_row =
279 std::find_if( algo_mask_veto.cbegin(),
280 algo_mask_veto.cend(),
283 return strcasecmp(
"all",
s.c_str()) == 0;
286 if( default_veto_row == algo_mask_veto.cend() ){
288 <<
"\nWarning: No default found in Veto mask xml, use 0 (unvetoed) as default" 291 default_veto_mask = veto_mask_veto[
std::distance( algo_mask_veto.cbegin(), default_veto_row ) ];
294 for(
unsigned int iAlg=0; iAlg < m_numberPhysTriggers; iAlg++ )
295 triggerVetoMasks.push_back(default_veto_mask);
297 for(
unsigned int row=0; row<algo_mask_veto.size(); row++){
299 if( strcasecmp(
"all", algoName.c_str()) == 0 )
continue;
300 unsigned int algoBit = algoName2bit[
algoName];
301 unsigned int veto = veto_mask_veto[row];
302 if( algoBit < m_numberPhysTriggers ) triggerVetoMasks[algoBit] =
int(veto);
308 unsigned int m_bx_mask_default = 1;
314 ts_mask_algobx.
addProcessor(
"uGtProcessor",
"uGtProcessor",
"-1",
"-1");
321 const std::map<std::string, l1t::Parameter>& settings_mask_algobx = ts_mask_algobx.
getParameters(
"uGtProcessor");
322 std::vector<std::string> bx_algo_name = settings_mask_algobx.at(
"algorithmBxMask").getTableColumn<
std::string>(
"algo");
323 std::vector<std::string> bx_range = settings_mask_algobx.at(
"algorithmBxMask").getTableColumn<
std::string>(
"range");
324 std::vector<unsigned int> bx_mask = settings_mask_algobx.at(
"algorithmBxMask").getTableColumn<
unsigned int>(
"mask");
326 int default_bxmask_row = -1;
327 typedef std::pair<unsigned long,unsigned long> Range_t;
329 struct RangeComp_t {
bool operator()(
const Range_t&
a,
const Range_t&
b)
const {
return a.first < b.first; } };
330 std::map<std::string, std::set<Range_t,RangeComp_t>> non_default_bx_ranges;
332 for(
unsigned int row = 0; row < bx_algo_name.size(); row++){
336 bool broadcastAlgo =
false;
337 bool broadcastRange =
false;
338 if( strcasecmp(
"all", s1.c_str()) == 0 ) broadcastAlgo =
true;
339 if( strcasecmp(
"all", s2.c_str()) == 0 ) broadcastRange =
true;
341 if( broadcastAlgo && broadcastRange ){
344 <<
"\nWarning: ALL-ALL row is not the first one, ignore it assuming 1 (unmasked) as the default" 348 if( default_bxmask_row >= 0 ){
350 <<
"\nWarning: multiple ALL-ALL rows found, using the first" 354 default_bxmask_row = row;
355 m_bx_mask_default = bx_mask[row];
360 if( broadcastRange ){
365 first = strtoul(s2.data(), &dash, 0);
366 while( *dash !=
'\0' && *dash !=
'-' ) ++dash;
367 last = (*dash !=
'\0' ? strtoul(++dash, &dash, 0) :
first);
368 if( first == 3564 ) first = 0;
373 <<
"\nWarning: parsing " << s2 <<
" as [" << first <<
"," <<
last <<
"] range" 378 <<
"\nWarning: start of interval is out of range: " << s2 <<
", skipping the row" 385 <<
"\nWarning: end of interval is out of range: " << s2 <<
", force [" << first <<
"," <<
last <<
"] range" 390 <<
"\nWarning: inverse/spillover range "<< s2 <<
", accounting for circular orbit as [0," <<
last <<
"] & [" << first <<
",3563]" 395 std::vector<std::string>
algos;
396 std::vector<std::pair<unsigned long,unsigned long>> orderedRanges;
398 if( !broadcastAlgo ){
399 algos.push_back( bx_algo_name[row] );
400 orderedRanges.push_back( std::make_pair(first,
last) );
402 for(
const auto &
i: non_default_bx_ranges){
403 algos.push_back(
i.first);
404 orderedRanges.push_back( std::make_pair(first,
last) );
408 if( !broadcastAlgo ){
409 algos.push_back( bx_algo_name[row] );
410 algos.push_back( bx_algo_name[row] );
411 orderedRanges.push_back( std::make_pair(0,
last) );
412 orderedRanges.push_back( std::make_pair(first,3563) );
414 for(
const auto &
i: non_default_bx_ranges){
415 algos.push_back(
i.first);
416 algos.push_back(
i.first);
417 orderedRanges.push_back( std::make_pair(0,
last) );
418 orderedRanges.push_back( std::make_pair(first,3563) );
423 for(
unsigned int item=0; item < algos.size(); item++){
425 unsigned int first = orderedRanges[item].first;
426 unsigned int last = orderedRanges[item].second;
428 std::set<Range_t,RangeComp_t> &
ranges = non_default_bx_ranges[
algoName];
442 std::set<Range_t>::iterator curr = ranges.end();
443 std::set<Range_t>::iterator succ = ranges.lower_bound(std::make_pair(first,last));
444 std::set<Range_t>::iterator pred = succ;
445 if( pred != ranges.begin() ) pred--;
else pred = ranges.end();
447 if( (pred == ranges.end() || pred->second <
first) &&
448 (succ == ranges.end() || succ->first >
last) ){
450 if( m_bx_mask_default != bx_mask[row] )
451 curr = ranges.insert(std::make_pair(first,last)).first;
455 Range_t newPred, newSucc;
456 bool modifiedPred =
false, gapInPred =
false, modifiedSucc =
false, dropSucc =
false;
458 if( pred != ranges.end() && pred->second >= first && pred->second <=
last ){
459 if( m_bx_mask_default != bx_mask[row] ){
460 if( last == pred->second ){
462 modifiedPred =
false;
465 newPred.first = pred->first;
466 newPred.second =
last;
471 newPred.first = pred->first;
472 newPred.second = first-1;
478 if( pred != ranges.end() && pred->second > first && pred->second >
last ){
479 if( m_bx_mask_default != bx_mask[row] ){
481 modifiedPred =
false;
484 newPred.first =
first;
485 newPred.second =
last;
491 if( succ != ranges.end() && succ->first <=
last ){
492 if( m_bx_mask_default != bx_mask[row] ){
494 newSucc.first =
first;
495 newSucc.second = succ->second;
498 newSucc.first = last+1;
499 newSucc.second = succ->second;
500 if( newSucc.first > 3563 || newSucc.first > newSucc.second )
506 if( modifiedPred && modifiedSucc && newPred.second >= newSucc.first ){
508 newPred.second = newSucc.second;
509 newSucc.first = newPred.first;
510 ranges.erase(pred,++succ);
511 curr = ranges.insert(newPred).first;
517 curr = ranges.insert(newPred).first;
520 Range_t
r1(pred->first, newPred.first-1);
521 Range_t
r2(newPred.second+1, pred->second);
531 curr = ranges.insert(newSucc).first;
538 if( curr != ranges.end() ){
539 std::set<Range_t,RangeComp_t>::iterator last_covered = ranges.upper_bound(std::make_pair(curr->second,0));
540 if( last_covered != ranges.begin() ) last_covered--;
else last_covered = ranges.end();
542 if( last_covered != ranges.end() && last_covered->first != curr->first ){
544 if( curr->second < last_covered->second ){
546 Range_t newRange(curr->first, last_covered->second);
548 curr = ranges.insert(newRange).first;
550 ranges.erase(++curr,last_covered);
556 if( default_bxmask_row < 0 ){
558 <<
"\nWarning: No default found in BX mask xml, used 1 (unmasked) as default" 562 for(
const std::pair<
std::string, std::set<Range_t,RangeComp_t>> &
algo : non_default_bx_ranges){
564 unsigned int algoBit = algoName2bit[
algoName];
565 for(
auto range :
algo.second)
566 for(
unsigned int bx = range.first; bx <= range.second; bx++){
567 triggerAlgoBxMaskAlgoTrig[bx].push_back(algoBit);
573 for(
unsigned int iSet=0; iSet < prescales.size(); iSet++ ){
574 for(
unsigned int iBit=0; iBit < prescales[iSet].size(); iBit++ ){
576 if( iBit >= triggerMasks.size() ){
578 <<
"\nWarning: algoBit in prescale table >= triggerMasks.size() " 579 <<
"\nWarning: no information on masking bit or not, setting as unmasked " 582 prescales[iSet][iBit] *= triggerMasks[iBit];
592 ts_mask_algobx.
addProcessor(
"uGtProcessor",
"uGtProcessor",
"-1",
"-1");
599 const std::map<std::string, l1t::Parameter>& settings_mask_algobx = ts_mask_algobx.
getParameters(
"uGtProcessor");
600 std::map<std::string,unsigned int> mask_algobx_columns = settings_mask_algobx.at(
"algorithmBxMask").getColumnIndices();
601 std::vector<unsigned int> bunches = settings_mask_algobx.at(
"algorithmBxMask").getTableColumn<
unsigned int>(
"bx/algo");
603 unsigned int numCol_mask_algobx = mask_algobx_columns.size();
605 int NumAlgoBitsInMask = numCol_mask_algobx - 1;
606 for(
int iBit=0; iBit<NumAlgoBitsInMask; iBit++ ){
607 std::vector<unsigned int>
algo = settings_mask_algobx.at(
"algorithmBxMask").getTableColumn<
unsigned int>(std::to_string(iBit).c_str());
608 for(
unsigned int bx=0; bx<bunches.size(); bx++){
609 if( algo[bx]!=m_bx_mask_default ) triggerAlgoBxMaskAlgoTrig[ bunches[bx] ].push_back(iBit);
T getParameter(std::string const &) const
L1TGlobalPrescalesVetosOnlineProd(const edm::ParameterSet &)
void setConfigured(bool state=true) noexcept
bool fillVariable(const std::string &columnName, T &outputVariable) const
void setPrescaleFactorTable(std::vector< std::vector< int > > value)
const QueryResults singleAttribute(const T &data) const
virtual std::shared_ptr< L1TGlobalPrescalesVetos > newObject(const std::string &objectKey, const L1TGlobalPrescalesVetosO2ORcd &record) override
l1t::OMDSReader m_omdsReader
payload
payload postfix for testing
void readDOMFromString(const std::string &str, xercesc::DOMDocument *&doc)
static std::map< std::string, std::string > fetch(const std::vector< std::string > &queryColumns, const std::string &table, const std::string &key, l1t::OMDSReader &m_omdsReader)
const QueryResults basicQuery(const std::vector< std::string > &columnNames, const std::string &schemaName, const std::string &tableName, const std::string &conditionLHS="", const QueryResults conditionRHS=QueryResults(), const std::string &conditionRHSName="")
void setTriggerMaskVeto(std::vector< int > value)
#define DEFINE_FWK_EVENTSETUP_MODULE(type)
const std::map< std::string, Parameter > & getParameters(const char *processor) const
void setBxMaskDefault(int value)
void readRootElement(TriggerSystem &aTriggerSystem, const std::string &sysId="")
void addProcessor(const char *processor, const char *role, const char *crate, const char *slot)
~L1TGlobalPrescalesVetosOnlineProd(void)
void setTriggerAlgoBxMask(std::map< int, std::vector< int > > value)
L1TGlobalPrescalesVetos * getWriteInstance()