CMS 3D CMS Logo

L1TGlobalPrescalesVetosOnlineProd.cc
Go to the documentation of this file.
1 #include <strings.h> // strcasecmp
2 #include <iostream>
3 #include <fstream>
4 #include <stdexcept>
5 #include <unordered_map>
6 #include <utility>
7 
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"
16 
18 
26 #include "OnlineDBqueryHelper.h"
27 
29  : public L1ConfigOnlineProdBaseExt<L1TGlobalPrescalesVetosO2ORcd, L1TGlobalPrescalesVetos> {
30 private:
32 
33 public:
34  std::unique_ptr<const L1TGlobalPrescalesVetos> newObject(const std::string &objectKey,
35  const L1TGlobalPrescalesVetosO2ORcd &record) override;
36 
39 };
40 
43  transactionSafe = iConfig.getParameter<bool>("transactionSafe");
44 }
45 
46 std::unique_ptr<const L1TGlobalPrescalesVetos> L1TGlobalPrescalesVetosOnlineProd::newObject(
47  const std::string &objectKey, const L1TGlobalPrescalesVetosO2ORcd &record) {
48  edm::LogInfo("L1-O2O: L1TGlobalPrescalesVetosOnlineProd")
49  << "Producing L1TGlobalPrescalesVetos with TSC:RS key = " << objectKey;
50 
51  if (objectKey.empty()) {
52  if (transactionSafe)
53  throw std::runtime_error("SummaryForFunctionManager: uGTrs | Faulty | Empty objectKey");
54  else {
55  edm::LogError("L1-O2O: L1TGlobalPrescalesVetosOnlineProd") << "returning empty L1TGlobalPrescalesVetos object";
56  return std::make_unique<const L1TGlobalPrescalesVetos>();
57  }
58  }
59 
60  unsigned int m_numberPhysTriggers = 512;
61 
62  // dictionary that maps algorithm name to it's index
63  std::unordered_map<std::string, int, std::hash<std::string>> algoName2bit;
64 
65  std::string uGTtscKey = objectKey.substr(0, objectKey.find(":"));
66  std::string uGTrsKey = objectKey.substr(objectKey.find(":") + 1, std::string::npos);
67 
68  std::string stage2Schema = "CMS_TRG_L1_CONF";
69 
70  std::string l1_menu_key;
71  std::vector<std::string> queryStrings;
72  queryStrings.push_back("L1_MENU");
73 
74  // select L1_MENU from CMS_TRG_L1_CONF.UGT_KEYS where ID = objectKey ;
76  queryStrings, stage2Schema, "UGT_KEYS", "UGT_KEYS.ID", m_omdsReader.singleAttribute(uGTtscKey));
77 
78  if (queryResult.queryFailed() || queryResult.numberRows() != 1) {
79  edm::LogError("L1-O2O") << "Cannot get UGT_KEYS.L1_MENU for ID = " << uGTtscKey << " ";
80  if (transactionSafe)
81  throw std::runtime_error("SummaryForFunctionManager: uGTrs | Faulty | Broken key");
82  else {
83  edm::LogError("L1-O2O: L1TGlobalPrescalesVetosOnlineProd") << "returning empty L1TGlobalPrescalesVetos object";
84  return std::make_unique<const L1TGlobalPrescalesVetos>();
85  }
86  }
87 
88  if (!queryResult.fillVariable("L1_MENU", l1_menu_key))
89  l1_menu_key = "";
90 
91  edm::LogInfo("L1-O2O: L1TGlobalPrescalesVetosOnlineProd") << "Producing L1TUtmTriggerMenu with key =" << l1_menu_key;
92 
93  if (uGTtscKey.empty()) {
94  edm::LogError("L1-O2O: L1TGlobalPrescalesVetosOnlineProd") << "TSC key is empty, returning";
95  if (transactionSafe)
96  throw std::runtime_error("SummaryForFunctionManager: uGTrs | Faulty | Empty objectKey");
97  else {
98  edm::LogError("L1-O2O: L1TGlobalPrescalesVetosOnlineProd") << "returning empty L1TGlobalPrescalesVetos object";
99  return std::make_unique<const L1TGlobalPrescalesVetos>();
100  }
101  }
102 
103  std::vector<std::string> queryColumns;
104  queryColumns.push_back("CONF");
105 
106  queryResult = m_omdsReader.basicQuery(
107  queryColumns, stage2Schema, "UGT_L1_MENU", "UGT_L1_MENU.ID", m_omdsReader.singleAttribute(l1_menu_key));
108 
109  if (queryResult.queryFailed() || queryResult.numberRows() != 1) {
110  edm::LogError("L1-O2O: L1TGlobalPrescalesVetosOnlineProd")
111  << "Cannot get UGT_L1_MENU.CONF for ID = " << l1_menu_key;
112  if (transactionSafe)
113  throw std::runtime_error("SummaryForFunctionManager: uGTrs | Faulty | Broken key");
114  else {
115  edm::LogError("L1-O2O: L1TGlobalPrescalesVetosOnlineProd") << "returning empty L1TGlobalPrescalesVetos object";
116  return std::make_unique<const L1TGlobalPrescalesVetos>();
117  }
118  }
119 
120  std::string l1Menu;
121  queryResult.fillVariable("CONF", l1Menu);
123  std::istringstream iss(l1Menu);
124 
125  std::shared_ptr<L1TUtmTriggerMenu> pMenu(
126  const_cast<L1TUtmTriggerMenu *>(reinterpret_cast<const L1TUtmTriggerMenu *>(tmeventsetup::getTriggerMenu(iss))));
127 
128  for (auto algo : pMenu->getAlgorithmMap())
129  algoName2bit[algo.first] = algo.second.getIndex();
130 
132  queryColumns.clear();
133  queryColumns.push_back("ALGOBX_MASK");
134  queryColumns.push_back("ALGO_FINOR_MASK");
135  queryColumns.push_back("ALGO_FINOR_VETO");
136  queryColumns.push_back("ALGO_PRESCALE");
137 
138  std::string prescale_key, bxmask_key, mask_key, vetomask_key;
139  std::string xmlPayload_prescale, xmlPayload_mask_algobx, xmlPayload_mask_finor, xmlPayload_mask_veto;
140  try {
141  std::map<std::string, std::string> subKeys =
142  l1t::OnlineDBqueryHelper::fetch(queryColumns, "UGT_RS_KEYS", uGTrsKey, m_omdsReader);
143  prescale_key = subKeys["ALGO_PRESCALE"];
144  bxmask_key = subKeys["ALGOBX_MASK"];
145  mask_key = subKeys["ALGO_FINOR_MASK"];
146  vetomask_key = subKeys["ALGO_FINOR_VETO"];
147  xmlPayload_prescale = l1t::OnlineDBqueryHelper::fetch({"CONF"}, "UGT_RS_CLOBS", prescale_key, m_omdsReader)["CONF"];
148  xmlPayload_mask_algobx =
149  l1t::OnlineDBqueryHelper::fetch({"CONF"}, "UGT_RS_CLOBS", bxmask_key, m_omdsReader)["CONF"];
150  xmlPayload_mask_finor = l1t::OnlineDBqueryHelper::fetch({"CONF"}, "UGT_RS_CLOBS", mask_key, m_omdsReader)["CONF"];
151  xmlPayload_mask_veto =
152  l1t::OnlineDBqueryHelper::fetch({"CONF"}, "UGT_RS_CLOBS", vetomask_key, m_omdsReader)["CONF"];
153  } catch (std::runtime_error &e) {
154  edm::LogError("L1-O2O: L1TGlobalPrescalesVetosOnlineProd") << e.what();
155  if (transactionSafe)
156  throw std::runtime_error(std::string("SummaryForFunctionManager: uGTrs | Faulty | ") + e.what());
157  else {
158  edm::LogError("L1-O2O: L1TGlobalPrescalesVetosOnlineProd") << "returning empty L1TGlobalPrescalesVetos object";
159  return std::make_unique<const L1TGlobalPrescalesVetos>();
160  }
161  }
162 
163  // for debugging purposes dump the payloads to /tmp
164  std::ofstream output1(std::string("/tmp/").append(prescale_key.substr(0, prescale_key.find("/"))).append(".xml"));
165  output1 << xmlPayload_prescale;
166  output1.close();
167  std::ofstream output2(std::string("/tmp/").append(mask_key.substr(0, mask_key.find("/"))).append(".xml"));
168  output2 << xmlPayload_mask_finor;
169  output2.close();
170  std::ofstream output3(std::string("/tmp/").append(bxmask_key.substr(0, bxmask_key.find("/"))).append(".xml"));
171  output3 << xmlPayload_mask_algobx;
172  output3.close();
173  std::ofstream output4(std::string("/tmp/").append(vetomask_key.substr(0, vetomask_key.find("/"))).append(".xml"));
174  output4 << xmlPayload_mask_veto;
175  output4.close();
176 
178 
179  std::vector<std::vector<int>> prescales;
180  std::vector<unsigned int> triggerMasks;
181  std::vector<int> triggerVetoMasks;
182  std::map<int, std::vector<int>> triggerAlgoBxMaskAlgoTrig;
183 
184  // Prescales
185  try {
186  l1t::XmlConfigParser xmlReader_prescale;
187  l1t::TriggerSystem ts_prescale;
188  ts_prescale.addProcessor("uGtProcessor", "uGtProcessor", "-1", "-1");
189 
190  // run the parser
191  xmlReader_prescale.readDOMFromString(xmlPayload_prescale); // initialize it
192  xmlReader_prescale.readRootElement(ts_prescale, "uGT"); // extract all of the relevant context
193  ts_prescale.setConfigured();
194 
195  const std::map<std::string, l1t::Parameter> &settings_prescale = ts_prescale.getParameters("uGtProcessor");
196  std::map<std::string, unsigned int> prescaleColumns = settings_prescale.at("prescales").getColumnIndices();
197 
198  unsigned int numColumns_prescale = prescaleColumns.size();
199  int nPrescaleSets = numColumns_prescale - 1;
200  std::vector<std::string> algoNames =
201  settings_prescale.at("prescales").getTableColumn<std::string>("algo/prescale-index");
202 
203  if (nPrescaleSets > 0) {
204  // Fill default prescale set
205  for (int iSet = 0; iSet < nPrescaleSets; iSet++) {
206  prescales.push_back(std::vector<int>());
207  for (unsigned int iBit = 0; iBit < m_numberPhysTriggers; ++iBit) {
208  int inputDefaultPrescale = 0; // only prescales that are set in the block below are used
209  prescales[iSet].push_back(inputDefaultPrescale);
210  }
211  }
212 
213  for (auto &col : prescaleColumns) {
214  if (col.second < 1)
215  continue; // we don't care for the algorithms' indicies in 0th column
216  int iSet = col.second - 1;
217  std::vector<unsigned int> prescalesForSet =
218  settings_prescale.at("prescales").getTableColumn<unsigned int>(col.first.c_str());
219  for (unsigned int row = 0; row < prescalesForSet.size(); row++) {
220  unsigned int prescale = prescalesForSet[row];
221  std::string algoName = algoNames[row];
222  unsigned int algoBit = algoName2bit[algoName];
223  prescales[iSet][algoBit] = prescale;
224  }
225  }
226  }
227  } catch (std::runtime_error &e) {
228  if (transactionSafe)
229  throw std::runtime_error(std::string("SummaryForFunctionManager: uGTrs | Faulty | ") + e.what());
230  else {
231  edm::LogError("L1-O2O: L1TGlobalPrescalesVetosOnlineProd") << "returning empty L1TGlobalPrescalesVetos object";
232  return std::make_unique<const L1TGlobalPrescalesVetos>();
233  }
234  }
235 
237 
238  // finor mask
239  try {
240  l1t::XmlConfigParser xmlReader_mask_finor;
241  l1t::TriggerSystem ts_mask_finor;
242  ts_mask_finor.addProcessor("uGtProcessor", "uGtProcessor", "-1", "-1");
243 
244  // run the parser
245  xmlReader_mask_finor.readDOMFromString(xmlPayload_mask_finor); // initialize it
246  xmlReader_mask_finor.readRootElement(ts_mask_finor, "uGT"); // extract all of the relevant context
247  ts_mask_finor.setConfigured();
248 
249  const std::map<std::string, l1t::Parameter> &settings_mask_finor = ts_mask_finor.getParameters("uGtProcessor");
250 
251  std::vector<std::string> algo_mask_finor = settings_mask_finor.at("finorMask").getTableColumn<std::string>("algo");
252  std::vector<unsigned int> mask_mask_finor =
253  settings_mask_finor.at("finorMask").getTableColumn<unsigned int>("mask");
254 
255  // mask (default=1 - unmask)
256  unsigned int default_finor_mask = 1;
257  auto default_finor_row = std::find_if(algo_mask_finor.cbegin(), algo_mask_finor.cend(), [](const std::string &s) {
258  // simpler than overweight std::tolower(s[], std::locale()) POSIX solution (thx to BA):
259  return strcasecmp("all", s.c_str()) == 0;
260  });
261  if (default_finor_row == algo_mask_finor.cend()) {
262  edm::LogError("L1-O2O: L1TGlobalPrescalesVetosOnlineProd")
263  << "\nWarning: No default found in FinOR mask xml, use 1 (unmasked) as default" << std::endl;
264  } else {
265  default_finor_mask = mask_mask_finor[std::distance(algo_mask_finor.cbegin(), default_finor_row)];
266  }
267 
268  for (unsigned int iAlg = 0; iAlg < m_numberPhysTriggers; iAlg++)
269  triggerMasks.push_back(default_finor_mask);
270 
271  for (unsigned int row = 0; row < algo_mask_finor.size(); row++) {
272  std::string algoName = algo_mask_finor[row];
273  if (strcasecmp("all", algoName.c_str()) == 0)
274  continue;
275  unsigned int algoBit = algoName2bit[algoName];
276  unsigned int mask = mask_mask_finor[row];
277  if (algoBit < m_numberPhysTriggers)
278  triggerMasks[algoBit] = mask;
279  }
280  } catch (std::runtime_error &e) {
281  if (transactionSafe)
282  throw std::runtime_error(std::string("SummaryForFunctionManager: uGTrs | Faulty | ") + e.what());
283  else {
284  edm::LogError("L1-O2O: L1TGlobalPrescalesVetosOnlineProd") << "returning empty L1TGlobalPrescalesVetos object";
285  return std::make_unique<const L1TGlobalPrescalesVetos>();
286  }
287  }
288 
290 
291  // veto mask
292  try {
293  l1t::XmlConfigParser xmlReader_mask_veto;
294  l1t::TriggerSystem ts_mask_veto;
295  ts_mask_veto.addProcessor("uGtProcessor", "uGtProcessor", "-1", "-1");
296 
297  // run the parser
298  xmlReader_mask_veto.readDOMFromString(xmlPayload_mask_veto); // initialize it
299  xmlReader_mask_veto.readRootElement(ts_mask_veto, "uGT"); // extract all of the relevant context
300  ts_mask_veto.setConfigured();
301 
302  const std::map<std::string, l1t::Parameter> &settings_mask_veto = ts_mask_veto.getParameters("uGtProcessor");
303  std::vector<std::string> algo_mask_veto = settings_mask_veto.at("vetoMask").getTableColumn<std::string>("algo");
304  std::vector<unsigned int> veto_mask_veto = settings_mask_veto.at("vetoMask").getTableColumn<unsigned int>("veto");
305 
306  // veto mask (default=0 - no veto)
307  unsigned int default_veto_mask = 1;
308  auto default_veto_row = std::find_if(algo_mask_veto.cbegin(), algo_mask_veto.cend(), [](const std::string &s) {
309  // simpler than overweight std::tolower(s[], std::locale()) POSIX solution (thx to BA):
310  return strcasecmp("all", s.c_str()) == 0;
311  });
312  if (default_veto_row == algo_mask_veto.cend()) {
313  edm::LogError("L1-O2O: L1TGlobalPrescalesVetosOnlineProd")
314  << "\nWarning: No default found in Veto mask xml, use 0 (unvetoed) as default" << std::endl;
315  } else {
316  default_veto_mask = veto_mask_veto[std::distance(algo_mask_veto.cbegin(), default_veto_row)];
317  }
318 
319  for (unsigned int iAlg = 0; iAlg < m_numberPhysTriggers; iAlg++)
320  triggerVetoMasks.push_back(default_veto_mask);
321 
322  for (unsigned int row = 0; row < algo_mask_veto.size(); row++) {
323  std::string algoName = algo_mask_veto[row];
324  if (strcasecmp("all", algoName.c_str()) == 0)
325  continue;
326  unsigned int algoBit = algoName2bit[algoName];
327  unsigned int veto = veto_mask_veto[row];
328  if (algoBit < m_numberPhysTriggers)
329  triggerVetoMasks[algoBit] = int(veto);
330  }
331  } catch (std::runtime_error &e) {
332  if (transactionSafe)
333  throw std::runtime_error(std::string("SummaryForFunctionManager: uGTrs | Faulty | ") + e.what());
334  else {
335  edm::LogError("L1-O2O: L1TGlobalPrescalesVetosOnlineProd") << "returning empty L1TGlobalPrescalesVetos object";
336  return std::make_unique<const L1TGlobalPrescalesVetos>();
337  }
338  }
339 
341 
342  // Algo bx mask
343  unsigned int m_bx_mask_default = 1;
344 
345  std::vector<std::string> bx_algo_name;
346  std::vector<std::string> bx_range;
347  std::vector<unsigned int> bx_mask;
348 
349  try {
350  l1t::XmlConfigParser xmlReader_mask_algobx;
351  l1t::TriggerSystem ts_mask_algobx;
352  ts_mask_algobx.addProcessor("uGtProcessor", "uGtProcessor", "-1", "-1");
353 
354  // run the parser
355  xmlReader_mask_algobx.readDOMFromString(xmlPayload_mask_algobx); // initialize it
356  xmlReader_mask_algobx.readRootElement(ts_mask_algobx, "uGT"); // extract all of the relevant context
357  ts_mask_algobx.setConfigured();
358 
359  const std::map<std::string, l1t::Parameter> &settings_mask_algobx = ts_mask_algobx.getParameters("uGtProcessor");
360  bx_algo_name = settings_mask_algobx.at("algorithmBxMask").getTableColumn<std::string>("algo");
361  bx_range = settings_mask_algobx.at("algorithmBxMask").getTableColumn<std::string>("range");
362  bx_mask = settings_mask_algobx.at("algorithmBxMask").getTableColumn<unsigned int>("mask");
363  } catch (std::runtime_error &e) {
364  if (transactionSafe)
365  throw std::runtime_error(std::string("SummaryForFunctionManager: uGTrs | Faulty | ") + e.what());
366  else {
367  edm::LogError("L1-O2O: L1TGlobalPrescalesVetosOnlineProd") << "returning empty L1TGlobalPrescalesVetos object";
368  return std::make_unique<const L1TGlobalPrescalesVetos>();
369  }
370  }
371 
372  int default_bxmask_row = -1;
373  typedef std::pair<unsigned long, unsigned long> Range_t;
374  // auto comp = [] (Range_t a, Range_t b){ return a.first < b.first; };
375  struct RangeComp_t {
376  bool operator()(const Range_t &a, const Range_t &b) const { return a.first < b.first; }
377  };
378  std::map<std::string, std::set<Range_t, RangeComp_t>> non_default_bx_ranges;
379 
380  for (unsigned int row = 0; row < bx_algo_name.size(); row++) {
381  const std::string &s1 = bx_algo_name[row];
382  const std::string &s2 = bx_range[row];
383  // find "all" broadcast keywords
384  bool broadcastAlgo = false;
385  bool broadcastRange = false;
386  if (strcasecmp("all", s1.c_str()) == 0)
387  broadcastAlgo = true;
388  if (strcasecmp("all", s2.c_str()) == 0)
389  broadcastRange = true;
390  // ALL-ALL-default:
391  if (broadcastAlgo && broadcastRange) {
392  if (row != 0) {
393  edm::LogError("L1-O2O: L1TGlobalPrescalesVetosOnlineProd")
394  << "\nWarning: ALL-ALL row is not the first one, ignore it assuming 1 (unmasked) as the default"
395  << std::endl;
396  continue;
397  }
398  if (default_bxmask_row >= 0) {
399  edm::LogError("L1-O2O: L1TGlobalPrescalesVetosOnlineProd")
400  << "\nWarning: multiple ALL-ALL rows found, using the first" << std::endl;
401  continue;
402  }
403  default_bxmask_row = row;
404  m_bx_mask_default = bx_mask[row];
405  continue;
406  }
407  // interpret the range
408  unsigned long first = 0, last = 0;
409  if (broadcastRange) {
410  first = 0;
411  last = 3563;
412  } else {
413  char *dash = nullptr;
414  first = strtoul(s2.data(), &dash, 0);
415  while (*dash != '\0' && *dash != '-')
416  ++dash;
417  last = (*dash != '\0' ? strtoul(++dash, &dash, 0) : first);
418  if (first == 3564)
419  first = 0;
420  if (last == 3564)
421  last = 0;
422  // what could possibly go wrong?
423  if (*dash != '\0') {
424  edm::LogError("L1-O2O: L1TGlobalPrescalesVetosOnlineProd")
425  << "\nWarning: parsing " << s2 << " as [" << first << "," << last << "] range" << std::endl;
426  }
427  if (first > 3563) {
428  edm::LogError("L1-O2O: L1TGlobalPrescalesVetosOnlineProd")
429  << "\nWarning: start of interval is out of range: " << s2 << ", skipping the row" << std::endl;
430  continue;
431  }
432  if (last > 3563) {
433  last = 3563;
434  edm::LogError("L1-O2O: L1TGlobalPrescalesVetosOnlineProd")
435  << "\nWarning: end of interval is out of range: " << s2 << ", force [" << first << "," << last << "] range"
436  << std::endl;
437  }
438  if (first > last) {
439  edm::LogError("L1-O2O: L1TGlobalPrescalesVetosOnlineProd")
440  << "\nWarning: inverse/spillover range " << s2 << ", accounting for circular orbit as [0," << last
441  << "] & [" << first << ",3563]" << std::endl;
442  }
443  }
444  // {algo,ALL}-{range,ALL}-{0,1}:
445  std::vector<std::string> algos;
446  std::vector<std::pair<unsigned long, unsigned long>> orderedRanges;
447  if (first <= last) {
448  if (!broadcastAlgo) {
449  algos.push_back(bx_algo_name[row]);
450  orderedRanges.push_back(std::make_pair(first, last));
451  } else {
452  for (const auto &i : non_default_bx_ranges) {
453  algos.push_back(i.first);
454  orderedRanges.push_back(std::make_pair(first, last));
455  }
456  }
457  } else {
458  if (!broadcastAlgo) {
459  algos.push_back(bx_algo_name[row]);
460  algos.push_back(bx_algo_name[row]);
461  orderedRanges.push_back(std::make_pair(0, last));
462  orderedRanges.push_back(std::make_pair(first, 3563));
463  } else {
464  for (const auto &i : non_default_bx_ranges) {
465  algos.push_back(i.first);
466  algos.push_back(i.first);
467  orderedRanges.push_back(std::make_pair(0, last));
468  orderedRanges.push_back(std::make_pair(first, 3563));
469  }
470  }
471  }
472 
473  for (unsigned int item = 0; item < algos.size(); item++) {
474  const std::string &algoName = algos[item];
475  unsigned int first = orderedRanges[item].first;
476  unsigned int last = orderedRanges[item].second;
477 
478  std::set<Range_t, RangeComp_t> &ranges = non_default_bx_ranges[algoName];
479  // .insert
480  // (
481  // std::pair< std::string, std::set<Range_t,RangeComp_t> >
482  // (
483  // algoName, std::set<Range_t,RangeComp_t>()
484  // )
485  // ).first->second; // I don't care if insert was successfull or if I've got a hold on existing range
486 
487  // current range may or may not overlap with the already present ranges
488  // if end of the predecessor starts before begin of the current range and begin
489  // of the successor starts after end of the current range there is no overlap
490  // and I save this range only if it has mask different from the default
491  // otherwise modify predecessor/successor ranges accordingly
492  std::set<Range_t>::iterator curr = ranges.end(); // inserted range
493  std::set<Range_t>::iterator succ =
494  ranges.lower_bound(std::make_pair(first, last)); // successor starts at current or later
495  std::set<Range_t>::iterator pred = succ;
496  if (pred != ranges.begin())
497  pred--;
498  else
499  pred = ranges.end();
500 
501  if ((pred == ranges.end() || pred->second < first) && (succ == ranges.end() || succ->first > last)) {
502  // no overlap
503  if (m_bx_mask_default != bx_mask[row])
504  curr = ranges.insert(std::make_pair(first, last)).first;
505  // do nothing if this is a default-mask interval
506  } else {
507  // pred/succ iterators are read-only, create intermediate adjusted copies
508  Range_t newPred, newSucc;
509  bool modifiedPred = false, gapInPred = false, modifiedSucc = false, dropSucc = false;
510  // overlap found with predecessor range
511  if (pred != ranges.end() && pred->second >= first && pred->second <= last) {
512  if (m_bx_mask_default != bx_mask[row]) {
513  if (last == pred->second) {
514  // both ranges end in the same place - nothing to do
515  modifiedPred = false;
516  } else {
517  // extend predecessor range
518  newPred.first = pred->first;
519  newPred.second = last;
520  modifiedPred = true;
521  }
522  } else {
523  // shrink predecessor range
524  newPred.first = pred->first;
525  newPred.second = first - 1; // non-negative for the predecessor by design
526  // by design pred->first < first, so the interval above is always valid
527  modifiedPred = true;
528  }
529  }
530  // current range is fully contained in predecessor
531  if (pred != ranges.end() && pred->second > first && pred->second > last) {
532  if (m_bx_mask_default != bx_mask[row]) {
533  // no change to the predecessor range
534  modifiedPred = false;
535  } else {
536  // make a "gap" in predecessor range
537  newPred.first = first;
538  newPred.second = last;
539  gapInPred = true;
540  modifiedPred = true;
541  }
542  }
543  // overlap found with successor range
544  if (succ != ranges.end() && succ->first <= last) {
545  if (m_bx_mask_default != bx_mask[row]) {
546  // extend successor range
547  newSucc.first = first;
548  newSucc.second = succ->second;
549  } else {
550  // shrink successor range
551  newSucc.first = last + 1;
552  newSucc.second = succ->second;
553  if (newSucc.first > 3563 || newSucc.first > newSucc.second)
554  dropSucc = true;
555  }
556  modifiedSucc = true;
557  }
558  // overlap found with both, predecessor and successor, such that I need to merge them
559  if (modifiedPred && modifiedSucc && newPred.second >= newSucc.first) {
560  // make newPred and newSucc identical just in case
561  newPred.second = newSucc.second;
562  newSucc.first = newPred.first;
563  ranges.erase(pred, ++succ);
564  curr = ranges.insert(newPred).first;
565  } else {
566  // merging is not the case, but I still need to propagate the new ranges back to the source
567  if (modifiedPred) {
568  if (!gapInPred) {
569  ranges.erase(pred);
570  curr = ranges.insert(newPred).first;
571  } else {
572  // make a gap by splitting predecessor into two ranges
573  Range_t r1(pred->first, newPred.first - 1); // non-negative for the predecessor by design
574  Range_t r2(newPred.second + 1, pred->second);
575  ranges.erase(pred);
576  ranges.insert(r1);
577  ranges.insert(r2);
578  curr = ranges.end(); // gap cannot cover any additional ranges
579  }
580  }
581  if (modifiedSucc) {
582  ranges.erase(succ);
583  if (!dropSucc)
584  curr = ranges.insert(newSucc).first;
585  }
586  }
587  }
588  // if current range spans over few more ranges after the successor
589  // remove those from the consideration up until the last covered range
590  // that may or may not extend beyond the current range end
591  if (curr != ranges.end()) { // insertion took place
592  std::set<Range_t, RangeComp_t>::iterator last_covered = ranges.upper_bound(std::make_pair(curr->second, 0));
593  if (last_covered != ranges.begin())
594  last_covered--;
595  else
596  last_covered = ranges.end();
597 
598  if (last_covered != ranges.end() && last_covered->first != curr->first) {
599  // ranges is not empty and last_covered is not current itself (i.e. it is different)
600  if (curr->second < last_covered->second) {
601  // the range needs to be extended
602  Range_t newRange(curr->first, last_covered->second);
603  ranges.erase(curr);
604  curr = ranges.insert(newRange).first;
605  }
606  ranges.erase(++curr, last_covered);
607  }
608  }
609  }
610  }
611 
612  if (default_bxmask_row < 0) {
613  edm::LogError("L1-O2O: L1TGlobalPrescalesVetosOnlineProd")
614  << "\nWarning: No default found in BX mask xml, used 1 (unmasked) as default" << std::endl;
615  }
616 
617  for (const std::pair<std::string, std::set<Range_t, RangeComp_t>> &algo : non_default_bx_ranges) {
618  const std::string &algoName = algo.first;
619  unsigned int algoBit = algoName2bit[algoName];
620  for (auto range : algo.second)
621  for (unsigned int bx = range.first; bx <= range.second; bx++) {
622  triggerAlgoBxMaskAlgoTrig[bx].push_back(algoBit);
623  }
624  }
625 
626  // Set prescales to zero if masked
627  for (unsigned int iSet = 0; iSet < prescales.size(); iSet++) {
628  for (unsigned int iBit = 0; iBit < prescales[iSet].size(); iBit++) {
629  // Add protection in case prescale table larger than trigger mask size
630  if (iBit >= triggerMasks.size()) {
631  edm::LogError("L1-O2O: L1TGlobalPrescalesVetosOnlineProd")
632  << "\nWarning: algoBit in prescale table >= triggerMasks.size() "
633  << "\nWarning: no information on masking bit or not, setting as unmasked " << std::endl;
634  } else {
635  prescales[iSet][iBit] *= triggerMasks[iBit];
636  }
637  }
638  }
639 
641 
643 
644  data_.setBxMaskDefault(m_bx_mask_default);
645  data_.setPrescaleFactorTable(prescales);
646  data_.setTriggerMaskVeto(triggerVetoMasks);
647  data_.setTriggerAlgoBxMask(triggerAlgoBxMaskAlgoTrig);
648 
649  auto payload = std::make_unique<const L1TGlobalPrescalesVetos>(*data_.getWriteInstance());
650 
651  edm::LogInfo("L1-O2O: L1TCaloParamsOnlineProd") << "SummaryForFunctionManager: uGTrs | OK | All looks good";
652 
653  return payload;
654 }
655 
656 //define this as a plug-in
T getParameter(std::string const &) const
L1TGlobalPrescalesVetosOnlineProd(const edm::ParameterSet &)
void setConfigured(bool state=true) noexcept
Definition: TriggerSystem.h:74
std::unique_ptr< const L1TGlobalPrescalesVetos > newObject(const std::string &objectKey, const L1TGlobalPrescalesVetosO2ORcd &record) override
bool fillVariable(const std::string &columnName, T &outputVariable) const
Definition: OMDSReader.h:274
void setPrescaleFactorTable(std::vector< std::vector< int > > value)
const QueryResults singleAttribute(const T &data) const
Definition: OMDSReader.h:259
JetCorrectorParameters::Record record
Definition: classes.h:7
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="")
Definition: OMDSReader.cc:75
void setTriggerMaskVeto(std::vector< int > value)
string ranges
Definition: diffTwoXMLs.py:79
#define DEFINE_FWK_EVENTSETUP_MODULE(type)
Definition: ModuleFactory.h:60
double b
Definition: hdecay.h:118
const std::map< std::string, Parameter > & getParameters(const char *processor) const
void readRootElement(TriggerSystem &aTriggerSystem, const std::string &sysId="")
double a
Definition: hdecay.h:119
col
Definition: cuy.py:1010
void addProcessor(const char *processor, const char *role, const char *crate, const char *slot)
void setTriggerAlgoBxMask(std::map< int, std::vector< int > > value)
L1TGlobalPrescalesVetos * getWriteInstance()