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 
43 
44 using namespace std;
45 using namespace edm;
46 using namespace l1t;
47 //
48 // class declaration
49 //
50 
52 public:
55 
56  typedef std::shared_ptr<L1TGlobalPrescalesVetos> ReturnType;
57 
58  ReturnType produce(const L1TGlobalPrescalesVetosRcd&);
59 
60 private:
61 
63 
64  unsigned int m_numberPhysTriggers;
67 
68  std::vector<std::vector<int> > m_initialPrescaleFactorsAlgoTrig;
69  std::vector<unsigned int> m_initialTriggerMaskAlgoTrig;
71  std::map<int, std::vector<int> > m_initialTriggerAlgoBxMaskAlgoTrig;
72 };
73 
74 //
75 // constants, enums and typedefs
76 //
77 
78 //
79 // static data member definitions
80 //
81 
82 //
83 // constructors and destructor
84 //
86  data_(new L1TGlobalPrescalesVetos())
87 {
88  //the following line is needed to tell the framework what
89  // data is being produced
90  setWhatProduced(this);
91  //setWhatProduced(this, conf.getParameter<std::string>("label"));
92 
94 
95  // directory in /data/Luminosity for the trigger menu
96  std::string menuDir = conf.getParameter<std::string>("TriggerMenuLuminosity");
97  //std::string menuDir = "startup";
98 
99  m_verbosity = conf.getParameter<int>("Verbosity");
100  m_bx_mask_default = conf.getParameter<int>("AlgoBxMaskDefault");
101 
102  // XML files
103  std::string prescalesFileName = conf.getParameter<std::string>("PrescaleXMLFile");
104  std::string algobxmaskFileName = conf.getParameter<std::string>("AlgoBxMaskXMLFile");
105  std::string finormaskFileName = conf.getParameter<std::string>("FinOrMaskXMLFile");
106  std::string vetomaskFileName = conf.getParameter<std::string>("VetoMaskXMLFile");
107 
108  // Full path
109  edm::FileInPath f1_prescale("L1Trigger/L1TGlobal/data/Luminosity/" + menuDir + "/" + prescalesFileName);
110  std::string m_prescaleFile = f1_prescale.fullPath();
111 
112  edm::FileInPath f1_mask_algobx("L1Trigger/L1TGlobal/data/Luminosity/" + menuDir + "/" + algobxmaskFileName);
113  std::string m_algobxmaskFile = f1_mask_algobx.fullPath();
114 
115  edm::FileInPath f1_mask_finor("L1Trigger/L1TGlobal/data/Luminosity/" + menuDir + "/" + finormaskFileName);
116  std::string m_finormaskFile = f1_mask_finor.fullPath();
117 
118  edm::FileInPath f1_mask_veto("L1Trigger/L1TGlobal/data/Luminosity/" + menuDir + "/" + vetomaskFileName);
119  std::string m_vetomaskFile = f1_mask_veto.fullPath();
120 
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  l1t::XmlConfigParser xmlReader_prescale;
153  l1t::TriggerSystem ts_prescale;
154  ts_prescale.addProcessor("uGtProcessor", "uGtProcessor","-1","-1");
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  const std::map<std::string, l1t::Parameter> &settings_prescale = ts_prescale.getParameters("uGtProcessor");
162  std::map<std::string,unsigned int> prescaleColumns = settings_prescale.at("prescales").getColumnIndices();
163 
164  unsigned int numColumns_prescale = prescaleColumns.size();
165 
166  int NumPrescaleSets = numColumns_prescale - 1;
168  std::vector<unsigned int> algoBits = settings_prescale.at("prescales").getTableColumn<unsigned int>("algo/prescale-index");
169  int NumAlgos_prescale = *std::max_element(algoBits.begin(), algoBits.end()) + 1;
170 
171  if( NumPrescaleSets > 0 ){
172  // Fill default prescale set
173  for( int iSet=0; iSet<NumPrescaleSets; iSet++ ){
174  prescales.push_back(std::vector<int>());
175  for( int iBit = 0; iBit < NumAlgos_prescale; ++iBit ){
176  int inputDefaultPrescale = 0; // only prescales that are set in the block below are used
177  prescales[iSet].push_back(inputDefaultPrescale);
178  }
179  }
180 
181  for(auto &col : prescaleColumns){
182  if( col.second<1 ) continue; // we don't care for the algorithms' indicies in 0th column
183  int iSet = col.second - 1;
184  std::vector<unsigned int> prescalesForSet = settings_prescale.at("prescales").getTableColumn<unsigned int>(col.first.c_str());
185  for(unsigned int row=0; row<prescalesForSet.size(); row++){
186  unsigned int prescale = prescalesForSet[row];
187  unsigned int algoBit = algoBits[row];
188  prescales[iSet][algoBit] = prescale;
189  }
190  }
191  }
192  }
193  input_prescale.close();
194 
195 
197 
198  // finor mask
199  // set all masks to 1 (unmasked) by default
200  triggerMasks.insert(triggerMasks.end(), m_numberPhysTriggers, 1);
201 
202  std::ifstream input_mask_finor;
203  input_mask_finor.open( m_finormaskFile );
204  if (not m_finormaskFile.empty() and not input_mask_finor) {
205  edm::LogError("L1TGlobalPrescalesVetosESProducer")
206  << "\nCould not find finor mask file: " << m_finormaskFile
207  << "\nDeafulting the finor mask for all triggers to 1 (unmasked)";
208  }
209  else {
210  while( !input_mask_finor.eof() ){
211  string tmp;
212  getline( input_mask_finor, tmp, '\n' );
213  xmlPayload_mask_finor.append( tmp );
214  }
215 
216  l1t::XmlConfigParser xmlReader_mask_finor;
217  l1t::TriggerSystem ts_mask_finor;
218  ts_mask_finor.addProcessor("uGtProcessor", "uGtProcessor","-1","-1");
219 
220  // run the parser
221  xmlReader_mask_finor.readDOMFromString( xmlPayload_mask_finor ); // initialize it
222  xmlReader_mask_finor.readRootElement( ts_mask_finor, "uGT" ); // extract all of the relevant context
223  ts_mask_finor.setConfigured();
224 
225  const std::map<std::string, l1t::Parameter>& settings_mask_finor = ts_mask_finor.getParameters("uGtProcessor");
226 
227  std::vector<unsigned int> algo_mask_finor = settings_mask_finor.at("finorMask").getTableColumn<unsigned int>("algo");
228  std::vector<unsigned int> mask_mask_finor = settings_mask_finor.at("finorMask").getTableColumn<unsigned int>("mask");
229 
230  for(unsigned int row=0; row<algo_mask_finor.size(); row++){
231  unsigned int algoBit = algo_mask_finor[row];
232  unsigned int mask = mask_mask_finor[row];
233  if( algoBit < m_numberPhysTriggers ) triggerMasks[algoBit] = mask;
234  }
235  }
236  input_mask_finor.close();
237 
238 
240 
241  // veto mask
242  // Setting veto mask to default 0 (no veto)
243  for (unsigned int iAlg=0; iAlg < m_numberPhysTriggers; iAlg++)
244  triggerVetoMasks.push_back(0);
245 
246  std::ifstream input_mask_veto;
247  input_mask_veto.open( m_vetomaskFile );
248  if (not m_vetomaskFile.empty() and not input_mask_veto) {
249  edm::LogError("L1TGlobalPrescalesVetosESProducer")
250  << "\nCould not find veto mask file: " << m_vetomaskFile
251  << "\nDeafulting the veto mask for all triggers to 1 (unmasked)";
252  }
253  else {
254  while( !input_mask_veto.eof() ){
255  string tmp;
256  getline( input_mask_veto, tmp, '\n' );
257  xmlPayload_mask_veto.append( tmp );
258  }
259 
260  l1t::XmlConfigParser xmlReader_mask_veto;
261  l1t::TriggerSystem ts_mask_veto;
262  ts_mask_veto.addProcessor("uGtProcessor", "uGtProcessor","-1","-1");
263 
264  // run the parser
265  xmlReader_mask_veto.readDOMFromString( xmlPayload_mask_veto ); // initialize it
266  xmlReader_mask_veto.readRootElement( ts_mask_veto, "uGT" ); // extract all of the relevant context
267  ts_mask_veto.setConfigured();
268 
269  const std::map<std::string, l1t::Parameter>& settings_mask_veto = ts_mask_veto.getParameters("uGtProcessor");
270  std::vector<unsigned int> algo_mask_veto = settings_mask_veto.at("vetoMask").getTableColumn<unsigned int>("algo");
271  std::vector<unsigned int> veto_mask_veto = settings_mask_veto.at("vetoMask").getTableColumn<unsigned int>("veto");
272 
273  for(unsigned int row=0; row<algo_mask_veto.size(); row++){
274  unsigned int algoBit = algo_mask_veto[row];
275  unsigned int veto = veto_mask_veto[row];
276  if( algoBit < m_numberPhysTriggers ) triggerVetoMasks[algoBit] = int(veto);
277  }
278  }
279  input_mask_veto.close();
280 
282 
283  // Algo bx mask
284  std::ifstream input_mask_algobx;
285  input_mask_algobx.open( m_algobxmaskFile );
286  if (not m_algobxmaskFile.empty() and not input_mask_algobx) {
287  edm::LogError("L1TGlobalPrescalesVetosESProducer")
288  << "\nCould not find bx mask file: " << m_algobxmaskFile
289  << "\nNot filling the bx mask map";
290  }
291  else {
292  while( !input_mask_algobx.eof() ){
293  string tmp;
294  getline( input_mask_algobx, tmp, '\n' );
295  xmlPayload_mask_algobx.append( tmp );
296  }
297 
298  l1t::XmlConfigParser xmlReader_mask_algobx;
299  l1t::TriggerSystem ts_mask_algobx;
300  ts_mask_algobx.addProcessor("uGtProcessor", "uGtProcessor","-1","-1");
301 
302  // run the parser
303  xmlReader_mask_algobx.readDOMFromString( xmlPayload_mask_algobx ); // initialize it
304  xmlReader_mask_algobx.readRootElement( ts_mask_algobx, "uGT" ); // extract all of the relevant context
305  ts_mask_algobx.setConfigured();
306 
307  const std::map<std::string, l1t::Parameter>& settings_mask_algobx = ts_mask_algobx.getParameters("uGtProcessor");
308  std::map<std::string,unsigned int> mask_algobx_columns = settings_mask_algobx.at("algorithmBxMask").getColumnIndices();
309  std::vector<unsigned int> bunches = settings_mask_algobx.at("algorithmBxMask").getTableColumn<unsigned int>("bx/algo");
310 
311  unsigned int numCol_mask_algobx = mask_algobx_columns.size();
312 
313  int NumAlgoBitsInMask = numCol_mask_algobx - 1;
314  for( int iBit=0; iBit<NumAlgoBitsInMask; iBit++ ){
315  std::vector<unsigned int> algo = settings_mask_algobx.at("algorithmBxMask").getTableColumn<unsigned int>(std::to_string(iBit).c_str());
316  for(unsigned int bx=0; bx<bunches.size(); bx++){
317  if( algo[bx]!=unsigned(m_bx_mask_default) ) triggerAlgoBxMaskAlgoTrig[ bunches[bx] ].push_back(iBit);
318  }
319  }
320  }
321  input_mask_algobx.close();
322 
323 
324  // Set prescales to zero if masked
325  for(auto & prescale : prescales){
326  for( unsigned int iBit=0; iBit < prescale.size(); iBit++ ){
327  // Add protection in case prescale table larger than trigger mask size
328  if( iBit >= triggerMasks.size() ){
329  edm::LogWarning("L1TGlobal")
330  << "\nWarning: algoBit in prescale table >= triggerMasks.size() "
331  << "\nWarning: no information on masking bit or not, setting as unmasked "
332  << std::endl;
333  }
334  else {
335  prescale[iBit] *= triggerMasks[iBit];
336  }
337  }
338  }
339 
341  m_initialTriggerMaskAlgoTrig = triggerMasks;
342  m_initialTriggerMaskVetoAlgoTrig = triggerVetoMasks;
343  m_initialTriggerAlgoBxMaskAlgoTrig = triggerAlgoBxMaskAlgoTrig;
344 
345 }
346 
347 
349 {
350 
351  // do anything here that needs to be done at desctruction time
352  // (e.g. close files, deallocate resources etc.)
353 
354 }
355 
356 
357 //
358 // member functions
359 //
360 
361 // ------------ method called to produce the data ------------
364 {
365  // configure the helper class parameters via its set funtions, e.g.:
370 
371  if( m_verbosity ){
372  LogDebug("L1TGlobal") << " ====> Prescales table <=== " << std::endl;
373  for( unsigned int ix=0; ix < m_initialPrescaleFactorsAlgoTrig.size(); ix++ ){
374  LogDebug("L1TGlobal") << " Prescale set = " << ix << std::endl;
375  for( unsigned int iy=0; iy < m_initialPrescaleFactorsAlgoTrig[ix].size(); iy++ ){
376  LogDebug("L1TGlobal") << "\t Algo " << iy << ": " << m_initialPrescaleFactorsAlgoTrig[ix][iy] << std::endl;
377  }
378  }
379 
380  LogDebug("L1TGlobal") << " ====> Trigger mask veto <=== " << std::endl;
381  for( unsigned int ix=0; ix < m_initialTriggerMaskVetoAlgoTrig.size(); ix++ ){
382  LogDebug("L1TGlobal") << "\t Algo " << ix << ": " << m_initialTriggerMaskVetoAlgoTrig[ix] << std::endl;
383  }
384 
385  LogDebug("L1TGlobal") << " ====> Algo bx mask <=== " << std::endl;
386  if( m_initialTriggerAlgoBxMaskAlgoTrig.size()==0 ) LogDebug("L1TGlobal") << "\t(empty map)" << std::endl;
387  for( auto& it: m_initialTriggerAlgoBxMaskAlgoTrig ){
388  LogDebug("L1TGlobal") << " bx = " << it.first << " : iAlg =";
389  std::vector<int> masked = it.second;
390  for(int & iAlg : masked){
391  LogDebug("L1TGlobal") << " " << iAlg;
392  }
393  LogDebug("L1TGlobal") << " " << std::endl;
394  }
395  }
396 
397  // write the condition format to the event setup via the helper:
398  using namespace edm::es;
399  std::shared_ptr<L1TGlobalPrescalesVetos> pMenu(data_.getWriteInstance());
400  return pMenu;
401 }
402 
403 //define this as a plug-in
#define LogDebug(id)
T getParameter(std::string const &) const
void setConfigured(bool state=true) noexcept
Definition: TriggerSystem.h:73
void setPrescaleFactorTable(std::vector< std::vector< int > > value)
std::vector< std::vector< int > > m_initialPrescaleFactorsAlgoTrig
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 readDOMFromString(const std::string &str, xercesc::DOMDocument *&doc)
void setTriggerMaskVeto(std::vector< int > value)
std::map< int, std::vector< int > > m_initialTriggerAlgoBxMaskAlgoTrig
std::vector< unsigned int > m_initialTriggerMaskAlgoTrig
#define DEFINE_FWK_EVENTSETUP_MODULE(type)
Definition: ModuleFactory.h:60
const std::map< std::string, Parameter > & getParameters(const char *processor) const
ReturnType produce(const L1TGlobalPrescalesVetosRcd &)
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
void readRootElement(TriggerSystem &aTriggerSystem, const std::string &sysId="")
HLT enums.
col
Definition: cuy.py:1008
void addProcessor(const char *processor, const char *role, const char *crate, const char *slot)
std::string fullPath() const
Definition: FileInPath.cc:184
void setTriggerAlgoBxMask(std::map< int, std::vector< int > > value)
L1TGlobalPrescalesVetos * getWriteInstance()