CMS 3D CMS Logo

L1TGlobalPrescalesVetosESProducer.cc
Go to the documentation of this file.
1 
10 
11 // system include files
12 #include <memory>
13 #include <iostream>
14 #include <fstream>
15 #include <unistd.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 
19 #include "tmEventSetup/tmEventSetup.hh"
20 #include "tmEventSetup/esTriggerMenu.hh"
21 #include "tmEventSetup/esAlgorithm.hh"
22 #include "tmEventSetup/esCondition.hh"
23 #include "tmEventSetup/esObject.hh"
24 #include "tmEventSetup/esCut.hh"
25 #include "tmEventSetup/esScale.hh"
26 #include "tmGrammar/Algorithm.hh"
27 
34 
36 
39 
44 
45 using namespace std;
46 using namespace edm;
47 using namespace l1t;
48 //
49 // class declaration
50 //
51 
53 public:
56 
57  typedef std::shared_ptr<L1TGlobalPrescalesVetos> ReturnType;
58 
59  ReturnType produce(const L1TGlobalPrescalesVetosRcd&);
60 
61 private:
62 
64 
65  unsigned int m_numberPhysTriggers;
68 
69  std::vector<std::vector<int> > m_initialPrescaleFactorsAlgoTrig;
70  std::vector<unsigned int> m_initialTriggerMaskAlgoTrig;
72  std::map<int, std::vector<int> > m_initialTriggerAlgoBxMaskAlgoTrig;
73 };
74 
75 //
76 // constants, enums and typedefs
77 //
78 
79 //
80 // static data member definitions
81 //
82 
83 //
84 // constructors and destructor
85 //
87  data_(new L1TGlobalPrescalesVetos())
88 {
89  //the following line is needed to tell the framework what
90  // data is being produced
91  setWhatProduced(this);
92  //setWhatProduced(this, conf.getParameter<std::string>("label"));
93 
95 
96  // directory in /data/Luminosity for the trigger menu
97  std::string menuDir = conf.getParameter<std::string>("TriggerMenuLuminosity");
98  //std::string menuDir = "startup";
99 
100  m_verbosity = conf.getParameter<int>("Verbosity");
101  m_bx_mask_default = conf.getParameter<int>("AlgoBxMaskDefault");
102 
103  // XML files
104  std::string prescalesFileName = conf.getParameter<std::string>("PrescaleXMLFile");
105  std::string algobxmaskFileName = conf.getParameter<std::string>("AlgoBxMaskXMLFile");
106  std::string finormaskFileName = conf.getParameter<std::string>("FinOrMaskXMLFile");
107  std::string vetomaskFileName = conf.getParameter<std::string>("VetoMaskXMLFile");
108 
109  // Full path
110  edm::FileInPath f1_prescale("L1Trigger/L1TGlobal/data/Luminosity/" + menuDir + "/" + prescalesFileName);
111  std::string m_prescaleFile = f1_prescale.fullPath();
112 
113  edm::FileInPath f1_mask_algobx("L1Trigger/L1TGlobal/data/Luminosity/" + menuDir + "/" + algobxmaskFileName);
114  std::string m_algobxmaskFile = f1_mask_algobx.fullPath();
115 
116  edm::FileInPath f1_mask_finor("L1Trigger/L1TGlobal/data/Luminosity/" + menuDir + "/" + finormaskFileName);
117  std::string m_finormaskFile = f1_mask_finor.fullPath();
118 
119  edm::FileInPath f1_mask_veto("L1Trigger/L1TGlobal/data/Luminosity/" + menuDir + "/" + vetomaskFileName);
120  std::string m_vetomaskFile = f1_mask_veto.fullPath();
121 
122  // XML payloads
123  std::string xmlPayload_prescale;
124  std::string xmlPayload_mask_algobx;
125  std::string xmlPayload_mask_finor;
126  std::string xmlPayload_mask_veto;
127 
128  std::vector<std::vector<int> > prescales;
129  std::vector<unsigned int> triggerMasks;
130  std::vector<int> triggerVetoMasks;
131  std::map<int, std::vector<int> > triggerAlgoBxMaskAlgoTrig;
132 
133  // Prescales
134  std::ifstream input_prescale;
135  input_prescale.open( m_prescaleFile );
136  if (not m_prescaleFile.empty() and not input_prescale) {
137  edm::LogError("L1TGlobalPrescalesVetosESProducer")
138  << "\nCould not find prescale file: " << m_prescaleFile
139  << "\nDeafulting to a single prescale column, with all prescales set to 1 (unprescaled)";
140 
141  const int inputDefaultPrescale = 1;
142  // by default, fill a single prescale column
143  prescales.push_back(std::vector<int>(m_numberPhysTriggers, inputDefaultPrescale));
144  }
145  else {
146  while( !input_prescale.eof() ){
147  string tmp;
148  getline( input_prescale, tmp, '\n' );
149  xmlPayload_prescale.append( tmp );
150  }
151 
152  XmlConfigReader xmlReader_prescale;
153  l1t::TrigSystem ts_prescale;
154  ts_prescale.addProcRole("uGtProcessor", "uGtProcessor");
155 
156  // run the parser
157  xmlReader_prescale.readDOMFromString( xmlPayload_prescale ); // initialize it
158  xmlReader_prescale.readRootElement( ts_prescale, "uGT" ); // extract all of the relevant context
159  ts_prescale.setConfigured();
160 
161  std::map<string, l1t::Setting> settings_prescale = ts_prescale.getSettings("uGtProcessor");
162  std::vector<l1t::TableRow> tRow_prescale = settings_prescale["prescales"].getTableRows();
163 
164  unsigned int numColumns_prescale = 0;
165  if( tRow_prescale.size()>0 ){
166  std::vector<std::string> firstRow_prescale = tRow_prescale[0].getRow();
167  numColumns_prescale = firstRow_prescale.size() - 1;
168  }
169 
170  if (numColumns_prescale > 0) {
171  // set all prescales to 1 (unprescaled) by default
172  const int inputDefaultPrescale = 1;
173  for (unsigned int iSet=0; iSet<numColumns_prescale; ++iSet)
174  prescales.push_back(std::vector<int>(m_numberPhysTriggers, inputDefaultPrescale));
175 
176  for (auto & row : tRow_prescale) {
177  unsigned int algoBit = row.getRowValue<unsigned int>("algo/prescale-index");
178  for (unsigned int iSet=0; iSet<numColumns_prescale; ++iSet) {
179  int prescale = row.getRowValue<unsigned int>(std::to_string(iSet));
180  prescales[iSet][algoBit] = prescale;
181  }
182  }
183  }
184 
185  }
186  input_prescale.close();
187 
188 
189 
190 
192 
193  // finor mask
194  // set all masks to 1 (unmasked) by default
195  triggerMasks.insert(triggerMasks.end(), m_numberPhysTriggers, 1);
196 
197  std::ifstream input_mask_finor;
198  input_mask_finor.open( m_finormaskFile );
199  if (not m_finormaskFile.empty() and not input_mask_finor) {
200  edm::LogError("L1TGlobalPrescalesVetosESProducer")
201  << "\nCould not find finor mask file: " << m_finormaskFile
202  << "\nDeafulting the finor mask for all triggers to 1 (unmasked)";
203  }
204  else {
205  while( !input_mask_finor.eof() ){
206  string tmp;
207  getline( input_mask_finor, tmp, '\n' );
208  xmlPayload_mask_finor.append( tmp );
209  }
210 
211  XmlConfigReader xmlReader_mask_finor;
212  l1t::TrigSystem ts_mask_finor;
213  ts_mask_finor.addProcRole("uGtProcessor", "uGtProcessor");
214 
215  // run the parser
216  xmlReader_mask_finor.readDOMFromString( xmlPayload_mask_finor ); // initialize it
217  xmlReader_mask_finor.readRootElement( ts_mask_finor, "uGT" ); // extract all of the relevant context
218  ts_mask_finor.setConfigured();
219 
220  std::map<string, l1t::Setting> settings_mask_finor = ts_mask_finor.getSettings("uGtProcessor");
221 
222  std::vector<l1t::TableRow> tRow_mask_finor = settings_mask_finor["finorMask"].getTableRows();
223 
224  for (auto & row : tRow_mask_finor) {
225  unsigned int algoBit = row.getRowValue<unsigned int>("algo");
226  unsigned int mask = row.getRowValue<unsigned int>("mask");
227  if (algoBit < m_numberPhysTriggers)
228  triggerMasks[algoBit] = mask;
229  }
230  }
231  input_mask_finor.close();
232 
233 
235 
236  // veto mask
237  // Setting veto mask to default 0 (no veto)
238  for (unsigned int iAlg=0; iAlg < m_numberPhysTriggers; iAlg++)
239  triggerVetoMasks.push_back(0);
240 
241  std::ifstream input_mask_veto;
242  input_mask_veto.open( m_vetomaskFile );
243  if (not m_vetomaskFile.empty() and not input_mask_veto) {
244  edm::LogError("L1TGlobalPrescalesVetosESProducer")
245  << "\nCould not find veto mask file: " << m_vetomaskFile
246  << "\nDeafulting the veto mask for all triggers to 1 (unmasked)";
247  }
248  else {
249  while( !input_mask_veto.eof() ){
250  string tmp;
251  getline( input_mask_veto, tmp, '\n' );
252  xmlPayload_mask_veto.append( tmp );
253  }
254 
255  XmlConfigReader xmlReader_mask_veto;
256  l1t::TrigSystem ts_mask_veto;
257  ts_mask_veto.addProcRole("uGtProcessor", "uGtProcessor");
258 
259  // run the parser
260  xmlReader_mask_veto.readDOMFromString( xmlPayload_mask_veto ); // initialize it
261  xmlReader_mask_veto.readRootElement( ts_mask_veto, "uGT" ); // extract all of the relevant context
262  ts_mask_veto.setConfigured();
263 
264  std::map<string, l1t::Setting> settings_mask_veto = ts_mask_veto.getSettings("uGtProcessor");
265  std::vector<l1t::TableRow> tRow_mask_veto = settings_mask_veto["vetoMask"].getTableRows();
266 
267  for(auto & row : tRow_mask_veto){
268  unsigned int algoBit = row.getRowValue<unsigned int>("algo");
269  unsigned int veto = row.getRowValue<unsigned int>("veto");
270  if (algoBit < m_numberPhysTriggers) triggerVetoMasks[algoBit] = int(veto);
271  }
272  }
273  input_mask_veto.close();
274 
276 
277  // Algo bx mask
278  std::ifstream input_mask_algobx;
279  input_mask_algobx.open( m_algobxmaskFile );
280  if (not m_algobxmaskFile.empty() and not input_mask_algobx) {
281  edm::LogError("L1TGlobalPrescalesVetosESProducer")
282  << "\nCould not find bx mask file: " << m_algobxmaskFile
283  << "\nNot filling the bx mask map";
284  }
285  else {
286  while( !input_mask_algobx.eof() ){
287  string tmp;
288  getline( input_mask_algobx, tmp, '\n' );
289  xmlPayload_mask_algobx.append( tmp );
290  }
291 
292  XmlConfigReader xmlReader_mask_algobx;
293  l1t::TrigSystem ts_mask_algobx;
294  ts_mask_algobx.addProcRole("uGtProcessor", "uGtProcessor");
295 
296  // run the parser
297  xmlReader_mask_algobx.readDOMFromString( xmlPayload_mask_algobx ); // initialize it
298  xmlReader_mask_algobx.readRootElement( ts_mask_algobx, "uGT" ); // extract all of the relevant context
299  ts_mask_algobx.setConfigured();
300 
301  std::map<string, l1t::Setting> settings_mask_algobx = ts_mask_algobx.getSettings("uGtProcessor");
302  std::vector<l1t::TableRow> tRow_mask_algobx = settings_mask_algobx["algorithmBxMask"].getTableRows();
303 
304  unsigned int numCol_mask_algobx = 0;
305  if( tRow_mask_algobx.size()>0 ){
306  std::vector<std::string> firstRow_mask_algobx = tRow_mask_algobx[0].getRow();
307  numCol_mask_algobx = firstRow_mask_algobx.size();
308  }
309 
310  int NumAlgoBitsInMask = numCol_mask_algobx - 1;
311  if( NumAlgoBitsInMask > 0 ){
312  for(auto & row : tRow_mask_algobx){
313  int bx = row.getRowValue<unsigned int>("bx/algo");
314  std::vector<int> maskedBits;
315  for( int iBit=0; iBit<NumAlgoBitsInMask; iBit++ ){
316  unsigned int maskBit = row.getRowValue<unsigned int>(std::to_string(iBit));
317  if( int(maskBit)!=m_bx_mask_default ) maskedBits.push_back(iBit);
318  }
319  if( maskedBits.size()>0 ) triggerAlgoBxMaskAlgoTrig[bx] = maskedBits;
320  }
321  }
322 
323  }
324  input_mask_algobx.close();
325 
326 
327  // Set prescales to zero if masked
328  for(auto & prescale : prescales){
329  for( unsigned int iBit=0; iBit < prescale.size(); iBit++ ){
330  // Add protection in case prescale table larger than trigger mask size
331  if( iBit >= triggerMasks.size() ){
332  edm::LogWarning("L1TGlobal")
333  << "\nWarning: algoBit in prescale table >= triggerMasks.size() "
334  << "\nWarning: no information on masking bit or not, setting as unmasked "
335  << std::endl;
336  }
337  else {
338  prescale[iBit] *= triggerMasks[iBit];
339  }
340  }
341  }
342 
344  m_initialTriggerMaskAlgoTrig = triggerMasks;
345  m_initialTriggerMaskVetoAlgoTrig = triggerVetoMasks;
346  m_initialTriggerAlgoBxMaskAlgoTrig = triggerAlgoBxMaskAlgoTrig;
347 
348 }
349 
350 
352 {
353 
354  // do anything here that needs to be done at desctruction time
355  // (e.g. close files, deallocate resources etc.)
356 
357 }
358 
359 
360 //
361 // member functions
362 //
363 
364 // ------------ method called to produce the data ------------
367 {
368  // configure the helper class parameters via its set funtions, e.g.:
373 
374  if( m_verbosity ){
375  LogDebug("L1TGlobal") << " ====> Prescales table <=== " << std::endl;
376  for( unsigned int ix=0; ix < m_initialPrescaleFactorsAlgoTrig.size(); ix++ ){
377  LogDebug("L1TGlobal") << " Prescale set = " << ix << std::endl;
378  for( unsigned int iy=0; iy < m_initialPrescaleFactorsAlgoTrig[ix].size(); iy++ ){
379  LogDebug("L1TGlobal") << "\t Algo " << iy << ": " << m_initialPrescaleFactorsAlgoTrig[ix][iy] << std::endl;
380  }
381  }
382 
383  LogDebug("L1TGlobal") << " ====> Trigger mask veto <=== " << std::endl;
384  for( unsigned int ix=0; ix < m_initialTriggerMaskVetoAlgoTrig.size(); ix++ ){
385  LogDebug("L1TGlobal") << "\t Algo " << ix << ": " << m_initialTriggerMaskVetoAlgoTrig[ix] << std::endl;
386  }
387 
388  LogDebug("L1TGlobal") << " ====> Algo bx mask <=== " << std::endl;
389  if( m_initialTriggerAlgoBxMaskAlgoTrig.size()==0 ) LogDebug("L1TGlobal") << "\t(empty map)" << std::endl;
390  for( auto& it: m_initialTriggerAlgoBxMaskAlgoTrig ){
391  LogDebug("L1TGlobal") << " bx = " << it.first << " : iAlg =";
392  std::vector<int> masked = it.second;
393  for(int & iAlg : masked){
394  LogDebug("L1TGlobal") << " " << iAlg;
395  }
396  LogDebug("L1TGlobal") << " " << std::endl;
397  }
398  }
399 
400  // write the condition format to the event setup via the helper:
401  using namespace edm::es;
402  std::shared_ptr<L1TGlobalPrescalesVetos> pMenu(data_.getWriteInstance());
403  return pMenu;
404 }
405 
406 //define this as a plug-in
#define LogDebug(id)
T getParameter(std::string const &) const
void setPrescaleFactorTable(std::vector< std::vector< int > > value)
std::vector< std::vector< int > > m_initialPrescaleFactorsAlgoTrig
void readRootElement(TrigSystem &aTrigSystem, const std::string &sysId="")
delete x;
Definition: CaloConfig.h:22
std::shared_ptr< L1TGlobalPrescalesVetos > ReturnType
L1TGlobalPrescalesVetosESProducer(const edm::ParameterSet &)
void setWhatProduced(T *iThis, const es::Label &iLabel=es::Label())
Definition: ESProducer.h:115
void addProcRole(const std::string &processor, const std::string &role)
Definition: TrigSystem.cc:39
void setConfigured(const bool state=true)
Definition: TrigSystem.h:39
void setTriggerMaskVeto(std::vector< int > value)
std::map< std::string, Setting > getSettings(const std::string &processor)
Definition: TrigSystem.cc:179
std::map< int, std::vector< int > > m_initialTriggerAlgoBxMaskAlgoTrig
std::vector< unsigned int > m_initialTriggerMaskAlgoTrig
#define DEFINE_FWK_EVENTSETUP_MODULE(type)
Definition: ModuleFactory.h:60
ReturnType produce(const L1TGlobalPrescalesVetosRcd &)
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
HLT enums.
void readDOMFromString(const std::string &str, xercesc::DOMDocument *&doc)
std::string fullPath() const
Definition: FileInPath.cc:184
void setTriggerAlgoBxMask(std::map< int, std::vector< int > > value)
L1TGlobalPrescalesVetos * getWriteInstance()