CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
L1TGlobalPrescalesVetosOnlineProd.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <stdexcept>
4 
11 
12 
13 
14 class L1TGlobalPrescalesVetosOnlineProd : public L1ConfigOnlineProdBaseExt<L1TGlobalPrescalesVetosO2ORcd,L1TGlobalPrescalesVetos> {
15 private:
16 public:
17  virtual boost::shared_ptr<L1TGlobalPrescalesVetos> newObject(const std::string& objectKey, const L1TGlobalPrescalesVetosO2ORcd& record) override ;
18 
21 };
22 
24 
25 boost::shared_ptr<L1TGlobalPrescalesVetos> L1TGlobalPrescalesVetosOnlineProd::newObject(const std::string& objectKey, const L1TGlobalPrescalesVetosO2ORcd& record) {
26  using namespace edm::es;
27 
28  edm::LogInfo( "L1-O2O: L1TGlobalPrescalesVetosOnlineProd" ) << "Producing L1TGlobalPrescalesVetos with RS key =" << objectKey ;
29 
30  if( objectKey.empty() ){
31  edm::LogInfo( "L1-O2O: L1TGlobalPrescalesVetosOnlineProd" ) << "Key is empty";
32  throw std::runtime_error("Empty objecKey");
34  }
35 
36  std::string stage2Schema = "CMS_TRG_L1_CONF" ;
37 
38  std::vector< std::string > queryStrings ;
39  queryStrings.push_back( "MP7" ) ;
40  queryStrings.push_back( "AMC502_EXTCOND" ) ;
41  queryStrings.push_back( "AMC502_FINOR" ) ;
42  queryStrings.push_back( "ALGOBX_MASK" ) ;
43  queryStrings.push_back( "ALGO_FINOR_MASK" ) ;
44  queryStrings.push_back( "ALGO_FINOR_VETO" ) ;
45  queryStrings.push_back( "ALGO_PRESCALE" ) ;
46 
47  std::string prescale_key, bxmask_key, mask_key, vetomask_key;
48 
49  // select MP7, AMC502_EXTCOND, AMC502_FINOR, ALGOBX_MASK, ALGO_FINOR_MASK, ALGO_FINOR_VETO, ALGO_PRESCALE from CMS_TRG_L1_CONF.UGT_RS_KEYS where ID = objectKey ;
50  l1t::OMDSReader::QueryResults queryResult =
51  m_omdsReader.basicQuery( queryStrings,
52  stage2Schema,
53  "UGT_RS_KEYS",
54  "UGT_RS_KEYS.ID",
55  m_omdsReader.singleAttribute(objectKey)
56  ) ;
57 
58  if( queryResult.queryFailed() || queryResult.numberRows() != 1 ){
59  edm::LogError( "L1-O2O" ) << "Cannot get UGT_RS_KEYS.{MP7,AMC502_EXTCOND,AMC502_FINOR,ALGOBX_MASK,ALGO_FINOR_MASK,ALGO_FINOR_VETO,ALGO_PRESCALE} for ID = " << objectKey;
60  throw std::runtime_error("Broken key");
62  }
63 
64  if( !queryResult.fillVariable( "ALGO_PRESCALE", prescale_key) ) prescale_key = "";
65  if( !queryResult.fillVariable( "ALGOBX_MASK", bxmask_key) ) bxmask_key = "";
66  if( !queryResult.fillVariable( "ALGO_FINOR_MASK", mask_key) ) mask_key = "";
67  if( !queryResult.fillVariable( "ALGO_FINOR_VETO", vetomask_key) ) vetomask_key = "";
68 
69  queryStrings.clear();
70  queryStrings.push_back( "CONF" ) ;
71 
72 
73  std::string xmlPayload_prescale, xmlPayload_mask_algobx, xmlPayload_mask_finor, xmlPayload_mask_veto;
74 
75  queryResult =
76  m_omdsReader.basicQuery( queryStrings,
77  stage2Schema,
78  "UGT_RS",
79  "UGT_RS.ID",
80  m_omdsReader.singleAttribute(prescale_key)
81  ) ;
82 
83  if( queryResult.queryFailed() || queryResult.numberRows() != 1 ){
84  edm::LogError( "L1-O2O: L1TGlobalPrescalesVetosOnlineProd" ) << "Cannot get UGT_RS.CONF for prescale_key = "<<prescale_key ;
85  throw std::runtime_error("Broken key");
87  }
88 
89  if( !queryResult.fillVariable( "CONF", xmlPayload_prescale ) ) xmlPayload_prescale = "";
90 
91 
92  queryResult =
93  m_omdsReader.basicQuery( queryStrings,
94  stage2Schema,
95  "UGT_RS",
96  "UGT_RS.ID",
98  ) ;
99 
100  if( queryResult.queryFailed() || queryResult.numberRows() != 1 ){
101  edm::LogError( "L1-O2O: L1TGlobalPrescalesVetosOnlineProd" ) << "Cannot get UGT_RS.CONF for mask_key = "<<mask_key ;
102  throw std::runtime_error("Broken key");
104  }
105 
106  if( !queryResult.fillVariable( "CONF", xmlPayload_mask_finor ) ) xmlPayload_mask_finor = "";
107 
108 
109  queryResult =
110  m_omdsReader.basicQuery( queryStrings,
111  stage2Schema,
112  "UGT_RS",
113  "UGT_RS.ID",
114  m_omdsReader.singleAttribute(bxmask_key)
115  ) ;
116 
117  if( queryResult.queryFailed() || queryResult.numberRows() != 1 ){
118  edm::LogError( "L1-O2O: L1TGlobalPrescalesVetosOnlineProd" ) << "Cannot get UGT_RS.CONF for bxmask_key = "<<bxmask_key ;
119  throw std::runtime_error("Broken key");
121  }
122 
123  if( !queryResult.fillVariable( "CONF", xmlPayload_mask_algobx ) ) xmlPayload_mask_algobx = "";
124 
125 
126  queryResult =
127  m_omdsReader.basicQuery( queryStrings,
128  stage2Schema,
129  "UGT_RS",
130  "UGT_RS.ID",
131  m_omdsReader.singleAttribute(vetomask_key)
132  ) ;
133 
134  if( queryResult.queryFailed() || queryResult.numberRows() != 1 ){
135  edm::LogError( "L1-O2O: L1TGlobalPrescalesVetosOnlineProd" ) << "Cannot get UGT_RS.CONF for vetomask_key = "<<vetomask_key ;
136  throw std::runtime_error("Broken key");
138  }
139 
140  if( !queryResult.fillVariable( "CONF", xmlPayload_mask_veto ) ) xmlPayload_mask_veto = "";
141 
142  // for debugging purposes dump the payloads to /tmp
143  std::ofstream output1(std::string("/tmp/").append(prescale_key.substr(0,prescale_key.find("/"))).append(".xml"));
144  output1<<xmlPayload_prescale;
145  output1.close();
146  std::ofstream output2(std::string("/tmp/").append(mask_key.substr(0,mask_key.find("/"))).append(".xml"));
147  output2<<xmlPayload_mask_finor;
148  output2.close();
149  std::ofstream output3(std::string("/tmp/").append(bxmask_key.substr(0,bxmask_key.find("/"))).append(".xml"));
150  output3<<xmlPayload_mask_algobx;
151  output3.close();
152  std::ofstream output4(std::string("/tmp/").append(vetomask_key.substr(0,vetomask_key.find("/"))).append(".xml"));
153  output4<<xmlPayload_mask_veto;
154  output4.close();
155 
157 
158  unsigned int m_numberPhysTriggers = 512;
159  unsigned int m_bx_mask_default = 1;
160 
161 
162  std::vector<std::vector<int> > prescales;
163  std::vector<unsigned int> triggerMasks;
164  std::vector<int> triggerVetoMasks;
165  std::map<int, std::vector<int> > triggerAlgoBxMaskAlgoTrig;
166 
167  // Prescales
168  l1t::XmlConfigReader xmlReader_prescale;
169  l1t::TrigSystem ts_prescale;
170  ts_prescale.addProcRole("uGtProcessor", "uGtProcessor");
171 
172  // run the parser
173  xmlReader_prescale.readDOMFromString( xmlPayload_prescale ); // initialize it
174  xmlReader_prescale.readRootElement( ts_prescale, "uGT" ); // extract all of the relevant context
175  ts_prescale.setConfigured();
176 
177  std::map<std::string, l1t::Setting> settings_prescale = ts_prescale.getSettings("uGtProcessor");
178  std::vector<l1t::TableRow> tRow_prescale = settings_prescale["prescales"].getTableRows();
179 
180  unsigned int numColumns_prescale = 0;
181  if( tRow_prescale.size()>0 ){
182  std::vector<std::string> firstRow_prescale = tRow_prescale[0].getRow();
183  numColumns_prescale = firstRow_prescale.size();
184  }
185 
186  int NumPrescaleSets = numColumns_prescale - 1;
189  unsigned int NumAlgos_prescale = 0;
190  for( auto it=tRow_prescale.begin(); it!=tRow_prescale.end(); it++ ){
191  unsigned int algoBit = it->getRowValue<unsigned int>("algo/prescale-index");
192  if( NumAlgos_prescale < algoBit+1 ) NumAlgos_prescale = algoBit+1;
193  }
194 
195  if( NumPrescaleSets > 0 ){
196  // Fill default prescale set
197  for( int iSet=0; iSet<NumPrescaleSets; iSet++ ){
198  prescales.push_back(std::vector<int>());
199  for( unsigned int iBit = 0; iBit < NumAlgos_prescale; ++iBit ){
200  int inputDefaultPrescale = 0; // only prescales that are set in the block below are used
201  prescales[iSet].push_back(inputDefaultPrescale);
202  }
203  }
204 
205  for( auto it=tRow_prescale.begin(); it!=tRow_prescale.end(); it++ ){
206  unsigned int algoBit = it->getRowValue<unsigned int>("algo/prescale-index");
207  for( int iSet=0; iSet<NumPrescaleSets; iSet++ ){
208  int prescale = 0;
209  try{
210  prescale = it->getRowValue<unsigned int>(std::to_string(iSet));
211  } catch (std::runtime_error &e){
212  edm::LogError( "L1-O2O: L1TGlobalPrescalesVetosOnlineProd" )
213  << "\nWarning: missing value for algoBit " << algoBit << " (row)"
214  << " in prescale set " << iSet << " (column) of " << prescale_key
215  << " also stored in /tmp/" << prescale_key.substr(0,prescale_key.find("/")).append(".xml")
216  << "\nWarning: no information on algoBit, setting to 0 "
217  << std::endl;
218  }
219  prescales[iSet][algoBit] = prescale;
220  }
221  }
222  }
223 
224 
226 
227  // finor mask
228  // Setting mask to default 1 (unmask)
229  for( unsigned int iAlg=0; iAlg < m_numberPhysTriggers; iAlg++ )
230  triggerMasks.push_back(1);
231 
232  l1t::XmlConfigReader xmlReader_mask_finor;
233  l1t::TrigSystem ts_mask_finor;
234  ts_mask_finor.addProcRole("uGtProcessor", "uGtProcessor");
235 
236  // run the parser
237  xmlReader_mask_finor.readDOMFromString( xmlPayload_mask_finor ); // initialize it
238  xmlReader_mask_finor.readRootElement( ts_mask_finor, "uGT" ); // extract all of the relevant context
239  ts_mask_finor.setConfigured();
240 
241  std::map<std::string, l1t::Setting> settings_mask_finor = ts_mask_finor.getSettings("uGtProcessor");
242  std::vector<l1t::TableRow> tRow_mask_finor = settings_mask_finor["finorMask"].getTableRows();
243 
244  for( auto it=tRow_mask_finor.begin(); it!=tRow_mask_finor.end(); it++ ){
245  unsigned int algoBit = it->getRowValue<unsigned int>("algo");
246  unsigned int mask = it->getRowValue<unsigned int>("mask");
247  if( algoBit < m_numberPhysTriggers ) triggerMasks[algoBit] = mask;
248  }
249 
251 
252  // veto mask
253  // Setting veto mask to default 0 (no veto)
254  for( unsigned int iAlg=0; iAlg < m_numberPhysTriggers; iAlg++ )
255  triggerVetoMasks.push_back(0);
256 
257  l1t::XmlConfigReader xmlReader_mask_veto;
258  l1t::TrigSystem ts_mask_veto;
259  ts_mask_veto.addProcRole("uGtProcessor", "uGtProcessor");
260 
261  // run the parser
262  xmlReader_mask_veto.readDOMFromString( xmlPayload_mask_veto ); // initialize it
263  xmlReader_mask_veto.readRootElement( ts_mask_veto, "uGT" ); // extract all of the relevant context
264  ts_mask_veto.setConfigured();
265 
266  std::map<std::string, l1t::Setting> settings_mask_veto = ts_mask_veto.getSettings("uGtProcessor");
267  std::vector<l1t::TableRow> tRow_mask_veto = settings_mask_veto["vetoMask"].getTableRows();
268 
269  for( auto it=tRow_mask_veto.begin(); it!=tRow_mask_veto.end(); it++ ){
270  unsigned int algoBit = it->getRowValue<unsigned int>("algo");
271  unsigned int veto = it->getRowValue<unsigned int>("veto");
272  if( algoBit < m_numberPhysTriggers ) triggerVetoMasks[algoBit] = int(veto);
273  }
274 
276 
277  // Algo bx mask
278  l1t::XmlConfigReader xmlReader_mask_algobx;
279  l1t::TrigSystem ts_mask_algobx;
280  ts_mask_algobx.addProcRole("uGtProcessor", "uGtProcessor");
281 
282  // run the parser
283  xmlReader_mask_algobx.readDOMFromString( xmlPayload_mask_algobx ); // initialize it
284  xmlReader_mask_algobx.readRootElement( ts_mask_algobx, "uGT" ); // extract all of the relevant context
285  ts_mask_algobx.setConfigured();
286 
287  std::map<std::string, l1t::Setting> settings_mask_algobx = ts_mask_algobx.getSettings("uGtProcessor");
288  std::vector<l1t::TableRow> tRow_mask_algobx = settings_mask_algobx["algorithmBxMask"].getTableRows();
289 
290  unsigned int numCol_mask_algobx = 0;
291  if( tRow_mask_algobx.size()>0 ){
292  std::vector<std::string> firstRow_mask_algobx = tRow_mask_algobx[0].getRow();
293  numCol_mask_algobx = firstRow_mask_algobx.size();
294  }
295 
296  int NumAlgoBitsInMask = numCol_mask_algobx - 1;
297  if( NumAlgoBitsInMask > 0 ){
298  for( auto it=tRow_mask_algobx.begin(); it!=tRow_mask_algobx.end(); it++ ){
299  int bx = it->getRowValue<unsigned int>("bx/algo");
300  std::vector<int> maskedBits;
301  for( int iBit=0; iBit<NumAlgoBitsInMask; iBit++ ){
302  unsigned int maskBit = it->getRowValue<unsigned int>(std::to_string(iBit));
303  if( maskBit!=m_bx_mask_default ) maskedBits.push_back(iBit);
304  }
305  if( maskedBits.size()>0 ) triggerAlgoBxMaskAlgoTrig[bx] = maskedBits;
306  }
307  }
308 
309 
310  // Set prescales to zero if masked
311  for( unsigned int iSet=0; iSet < prescales.size(); iSet++ ){
312  for( unsigned int iBit=0; iBit < prescales[iSet].size(); iBit++ ){
313  // Add protection in case prescale table larger than trigger mask size
314  if( iBit >= triggerMasks.size() ){
315  edm::LogError( "L1-O2O: L1TGlobalPrescalesVetosOnlineProd" )
316  << "\nWarning: algoBit in prescale table >= triggerMasks.size() "
317  << "\nWarning: no information on masking bit or not, setting as unmasked "
318  << std::endl;
319  }
320  else {
321  prescales[iSet][iBit] *= triggerMasks[iBit];
322  }
323  }
324  }
325 
327 
329 
330  data_.setBxMaskDefault ( m_bx_mask_default );
332  data_.setTriggerMaskVeto ( triggerVetoMasks );
333  data_.setTriggerAlgoBxMask ( triggerAlgoBxMaskAlgoTrig );
334 
335  using namespace edm::es;
336  boost::shared_ptr<L1TGlobalPrescalesVetos> pMenu = boost::shared_ptr< L1TGlobalPrescalesVetos >(data_.getWriteInstance());
337  return pMenu;
338 
339 }
340 
341 //define this as a plug-in
L1TGlobalPrescalesVetosOnlineProd(const edm::ParameterSet &)
void setPrescaleFactorTable(std::vector< std::vector< int > > value)
const QueryResults singleAttribute(const T &data) const
Definition: OMDSReader.h:296
JetCorrectorParameters::Record record
Definition: classes.h:7
boost::dynamic_bitset append(const boost::dynamic_bitset<> &bs1, const boost::dynamic_bitset<> &bs2)
this method takes two bitsets bs1 and bs2 and returns result of bs2 appended to the end of bs1 ...
void addProcRole(const std::string &processor, const std::string &role)
Definition: TrigSystem.cc:39
virtual boost::shared_ptr< L1TGlobalPrescalesVetos > newObject(const std::string &objectKey, const L1TGlobalPrescalesVetosO2ORcd &record) override
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:86
void setTriggerMaskVeto(std::vector< int > value)
#define DEFINE_FWK_EVENTSETUP_MODULE(type)
Definition: ModuleFactory.h:60
void setTriggerAlgoBxMask(std::map< int, std::vector< int > > value)
L1TGlobalPrescalesVetos * getWriteInstance()