CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
L1TGlobalUtil.cc
Go to the documentation of this file.
1 // L1TGlobalUtil
2 //
3 // author: Brian Winer Ohio State
4 //
5 
7 
8 #include <iostream>
9 #include <fstream>
10 
15 
23 
24 
27 
28 
29 // constructor
31  // initialize cached IDs
32  m_l1GtMenuCacheID = 0ULL;
33  m_l1GtPfAlgoCacheID = 0ULL;
34  m_filledPrescales = false;
37 
38  edm::FileInPath f1("L1Trigger/L1TGlobal/data/Luminosity/startup/prescale_L1TGlobal.csv");
40  m_numberPhysTriggers = 512; //need to get this out of the EventSetup
41  m_PreScaleColumn = 1;
42 
43 }
44 
47  L1TGlobalUtil(pset, iC) { }
48 
51  L1TGlobalUtil() {
52  m_l1tGlobalUtilHelper.reset(new L1TGlobalUtilHelper(pset, iC));
53 }
54 
55 // destructor
57 
58  // empty
59 
60 }
61 
63  edm::FileInPath f1("L1Trigger/L1TGlobal/data/Luminosity/startup/" + filename);
64  m_preScaleFileName = f1.fullPath();
65  m_PreScaleColumn = psColumn;
66 }
67 
69  // typically, the L1T menu and prescale table (may change only between Runs)
70  retrieveL1Setup(evSetup);
71  // typically the prescale set index used and the event by event accept/reject info (changes between Events)
72  retrieveL1Event(iEvent,evSetup);
73 }
74 
76  edm::EDGetToken gtAlgToken) {
77  // typically, the L1T menu and prescale table (may change only between Runs)
78  retrieveL1Setup(evSetup);
79  // typically the prescale set index used and the event by event accept/reject info (changes between Events)
80  retrieveL1Event(iEvent,evSetup,gtAlgToken);
81 }
82 
84 
85  // get / update the trigger menu from the EventSetup
86  // local cache & check on cacheIdentifier
87  unsigned long long l1GtMenuCacheID = evSetup.get<L1TUtmTriggerMenuRcd>().cacheIdentifier();
88 
89  if (m_l1GtMenuCacheID != l1GtMenuCacheID) {
90 
92  evSetup.get< L1TUtmTriggerMenuRcd>().get(l1GtMenu) ;
93  m_l1GtMenu = l1GtMenu.product();
94 
95  //std::cout << "Attempting to fill the map " << std::endl;
96  m_algorithmMap = &(m_l1GtMenu->getAlgorithmMap());
97 
98  //reset vectors since we have new menu
99  resetDecisionVectors();
100 
101  m_l1GtMenuCacheID = l1GtMenuCacheID;
102  }
103 
104  // Fill the mask and prescales (dummy for now)
105  if(!m_filledPrescales) {
106 
107  // clear and dimension
108  resetPrescaleVectors();
109  resetMaskVectors();
110 
111  // Only get event record if not unprescaled and not unmasked
112  if( !( m_algorithmTriggersUnprescaled && m_algorithmTriggersUnmasked ) ){
113  unsigned long long l1GtPfAlgoCacheID = evSetup.get<L1TGlobalPrescalesVetosRcd>().cacheIdentifier();
114 
115  if (m_l1GtPfAlgoCacheID != l1GtPfAlgoCacheID) {
116  edm::ESHandle< L1TGlobalPrescalesVetos > l1GtPrescalesVetoes;
117  evSetup.get< L1TGlobalPrescalesVetosRcd >().get( l1GtPrescalesVetoes );
118  const L1TGlobalPrescalesVetos * es = l1GtPrescalesVetoes.product();
119  m_l1GtPrescalesVetoes = PrescalesVetosHelper::readFromEventSetup(es);
120 
121  m_prescaleFactorsAlgoTrig = &(m_l1GtPrescalesVetoes->prescaleTable());
122  m_triggerMaskVetoAlgoTrig = &(m_l1GtPrescalesVetoes->triggerMaskVeto());
123 
124  m_l1GtPfAlgoCacheID = l1GtPfAlgoCacheID;
125  }
126  } else {
127  //Load the full prescale set for use
128  loadPrescalesAndMasks();
129 
130  // Set Prescale factors to initial
131  m_prescaleFactorsAlgoTrig = &m_initialPrescaleFactorsAlgoTrig;
132  m_triggerMaskAlgoTrig = &m_initialTriggerMaskAlgoTrig;
133  m_triggerMaskVetoAlgoTrig = &m_initialTriggerMaskVetoAlgoTrig;
134  }
135 
136  //Pick which set we are using
137  if(m_PreScaleColumn > m_prescaleFactorsAlgoTrig->size() || m_PreScaleColumn < 1) {
138  LogTrace("l1t|Global")
139  << "\nNo Prescale Set: " << m_PreScaleColumn
140  << "\nMax Prescale Set value : " << m_prescaleFactorsAlgoTrig->size()
141  << "\nSetting prescale column to 1"
142  << std::endl;
143  m_PreScaleColumn = 1;
144  }
145  LogDebug("l1t|Global") << "Grabing prescale column "<< m_PreScaleColumn << endl;
146  const std::vector<int>& prescaleSet = (*m_prescaleFactorsAlgoTrig)[m_PreScaleColumn-1];
147 
148  // If masks or prescales enabled, get mask decision from prescale column
149  if( !( m_algorithmTriggersUnprescaled && m_algorithmTriggersUnmasked ) ){
150  // For now, set masks according to prescale value of 0
151  m_initialTriggerMaskAlgoTrig.clear();
152  for( unsigned int iAlgo=0; iAlgo < prescaleSet.size(); iAlgo++ ){
153  unsigned int value = prescaleSet[iAlgo];
154  value = ( value==0 ) ? 0 : 1;
155  m_initialTriggerMaskAlgoTrig.push_back(value);
156  }
157  m_triggerMaskAlgoTrig = &m_initialTriggerMaskAlgoTrig;
158  }
159 
160  for (std::map<std::string, L1TUtmAlgorithm>::const_iterator itAlgo = m_algorithmMap->begin(); itAlgo != m_algorithmMap->end(); itAlgo++) {
161 
162  // Get the algorithm name
163  std::string algName = itAlgo->first;
164  int algBit = (itAlgo->second).getIndex(); //algoBitNumber();
165 
166  (m_prescales[algBit]).first = algName;
167  (m_prescales[algBit]).second = prescaleSet[algBit];
168 
169  (m_masks[algBit]).first = algName;
170  (m_masks[algBit]).second = (*m_triggerMaskAlgoTrig)[algBit];
171 
172  (m_vetoMasks[algBit]).first = algName;
173  (m_vetoMasks[algBit]).second = (*m_triggerMaskVetoAlgoTrig)[algBit];
174  }
175 
176  m_filledPrescales = true;
177  }
178 }
179 
181  retrieveL1Event(iEvent, evSetup, m_l1tGlobalUtilHelper->l1tAlgBlkToken());
182 }
183 
185  edm::EDGetToken gtAlgToken) {
186 
187 // Get the Global Trigger Output Algorithm block
188  iEvent.getByToken(gtAlgToken,m_uGtAlgBlk);
189  m_finalOR = false;
190 
191  //Make sure we have a valid AlgBlk
192  if(m_uGtAlgBlk.isValid()) {
193  // get the GlabalAlgBlk (Stupid find better way) of BX=0
194  std::vector<GlobalAlgBlk>::const_iterator algBlk = m_uGtAlgBlk->begin(0);
195  if (algBlk != m_uGtAlgBlk->end(0)){
196  m_PreScaleColumn = static_cast<unsigned int>(algBlk->getPreScColumn());
197  // Grab the final OR from the AlgBlk,
198  m_finalOR = algBlk->getFinalOR();
199 
200  // Make a map of the trigger name and whether it passed various stages (initial,prescale,final)
201  // Note: might be able to improve performance by not full remaking map with names each time
202  for (std::map<std::string, L1TUtmAlgorithm>::const_iterator itAlgo = m_algorithmMap->begin(); itAlgo != m_algorithmMap->end(); itAlgo++) {
203 
204  // Get the algorithm name
205  std::string algName = itAlgo->first;
206  int algBit = (itAlgo->second).getIndex(); //algoBitNumber();
207 
208  bool decisionInitial = algBlk->getAlgoDecisionInitial(algBit);
209  (m_decisionsInitial[algBit]).first = algName;
210  (m_decisionsInitial[algBit]).second = decisionInitial;
211 
212  bool decisionInterm = algBlk->getAlgoDecisionInterm(algBit);
213  (m_decisionsInterm[algBit]).first = algName;
214  (m_decisionsInterm[algBit]).second = decisionInterm;
215 
216  bool decisionFinal = algBlk->getAlgoDecisionFinal(algBit);
217  (m_decisionsFinal[algBit]).first = algName;
218  (m_decisionsFinal[algBit]).second = decisionFinal;
219  }
220  } else {
221  //cout << "Error empty AlgBlk recovered.\n";
222  }
223  } else {
224  //cout<< "Error no valid uGT Algorithm Data with Token provided " << endl;
225  }
226 }
227 
229 
230  std::ifstream inputPrescaleFile;
231  inputPrescaleFile.open(m_preScaleFileName);
232 
233  std::vector<std::vector<int> > vec;
234  std::vector<std::vector<int> > prescale_vec;
235 
236  std::vector<unsigned int> temp_triggerMask;
237  std::vector<int> temp_triggerVetoMask;
238 
239  if( inputPrescaleFile ){
240  std::string prefix1("#");
241  std::string prefix2("-1");
242 
243  std::string line;
244 
245  bool first = true;
246 
247  while( getline(inputPrescaleFile,line) ){
248 
249  if( !line.compare(0, prefix1.size(), prefix1) ) continue;
250  //if( !line.compare(0, prefix2.size(), prefix2) ) continue;
251 
252  istringstream split(line);
253  int value;
254  int col = 0;
255  char sep;
256 
257  while( split >> value ){
258  if( first ){
259  // Each new value read on line 1 should create a new inner vector
260  vec.push_back(std::vector<int>());
261  }
262 
263  vec[col].push_back(value);
264  ++col;
265 
266  // read past the separator
267  split>>sep;
268  }
269 
270  // Finished reading line 1 and creating as many inner
271  // vectors as required
272  first = false;
273  }
274 
275 
276  int NumPrescaleSets = 0;
277 
278  int maskColumn = -1;
279  int maskVetoColumn = -1;
280  for( int iCol=0; iCol<int(vec.size()); iCol++ ){
281  if( vec[iCol].size() > 0 ){
282  int firstRow = vec[iCol][0];
283 
284  if( firstRow > 0 ) NumPrescaleSets++;
285  else if( firstRow==-2 ) maskColumn = iCol;
286  else if( firstRow==-3 ) maskVetoColumn = iCol;
287  }
288  }
289 
290  // Fill default values for mask and veto mask
291  for( unsigned int iBit = 0; iBit < m_numberPhysTriggers; ++iBit ){
292  unsigned int inputDefaultMask = 1;
293  unsigned int inputDefaultVetoMask = 0;
294  temp_triggerMask.push_back(inputDefaultMask);
295  temp_triggerVetoMask.push_back(inputDefaultVetoMask);
296  }
297 
298 // cout << " Mask Column " << maskColumn << " VetoColumn " << maskVetoColumn << endl;
299 
300  // Fill non-trivial mask and veto mask
301  if( maskColumn>=0 || maskVetoColumn>=0 ){
302  for( int iBit=1; iBit<int(vec[0].size()); iBit++ ){
303  unsigned int algoBit = vec[0][iBit];
304  // algoBit must be less than the number of triggers
305  if( algoBit < m_numberPhysTriggers ){
306  if( maskColumn>=0 ){
307  unsigned int triggerMask = vec[maskColumn][iBit];
308  temp_triggerMask[algoBit] = triggerMask;
309 // cout << "Settting Mask for bit " << algoBit << " to " << triggerMask << endl;
310  }
311  if( maskVetoColumn>=0 ){
312  int triggerVetoMask = vec[maskVetoColumn][iBit];
313  temp_triggerVetoMask[algoBit] = triggerVetoMask;
314  }
315  }
316  }
317  }
318 
319 
320  if( NumPrescaleSets > 0 ){
321  // Fill default prescale set
322  for( int iSet=0; iSet<NumPrescaleSets; iSet++ ){
323  prescale_vec.push_back(std::vector<int>());
324  for( unsigned int iBit = 0; iBit < m_numberPhysTriggers; ++iBit ){
325  int inputDefaultPrescale = 1;
326  prescale_vec[iSet].push_back(inputDefaultPrescale);
327  }
328  }
329 
330  // Fill non-trivial prescale set
331  for( int iBit=1; iBit<int(vec[0].size()); iBit++ ){
332  unsigned int algoBit = vec[0][iBit];
333  // algoBit must be less than the number of triggers
334  if( algoBit < m_numberPhysTriggers ){
335  for( int iSet=0; iSet<int(vec.size()); iSet++ ){
336  int useSet = -1;
337  if( vec[iSet].size() > 0 ){
338  useSet = vec[iSet][0];
339  }
340  useSet -= 1;
341 
342  if( useSet<0 ) continue;
343 
344  int prescale = vec[iSet][iBit];
345  prescale_vec[useSet][algoBit] = prescale;
346  }
347  }
348  else{
349  LogTrace("l1t|Global")
350  << "\nPrescale file has algo bit: " << algoBit
351  << "\nThis is larger than the number of triggers: " << m_numberPhysTriggers
352  << "\nSomething is wrong. Ignoring."
353  << std::endl;
354  }
355  }
356  }
357 
358  }
359  else {
360  LogTrace("l1t|Global")
361  << "\nCould not find file: " << m_preScaleFileName
362  << "\nFilling the prescale vectors with prescale 1"
363  << "\nSetting prescale set to 1"
364  << std::endl;
365 
366  m_PreScaleColumn = 1;
367 
368  for( int col=0; col < 1; col++ ){
369  prescale_vec.push_back(std::vector<int>());
370  for( unsigned int iBit = 0; iBit < m_numberPhysTriggers; ++iBit ){
371  int inputDefaultPrescale = 1;
372  prescale_vec[col].push_back(inputDefaultPrescale);
373  }
374  }
375  }
376 
377  inputPrescaleFile.close();
378 
379  m_initialPrescaleFactorsAlgoTrig = prescale_vec;
380  m_initialTriggerMaskAlgoTrig = temp_triggerMask;
381  m_initialTriggerMaskVetoAlgoTrig = temp_triggerVetoMask;
382 
383 }
384 
386 
387  // Reset all the vector contents with null information
388  m_decisionsInitial.clear();
389  m_decisionsInitial.resize(m_numberPhysTriggers);
390  m_decisionsInterm.clear();
391  m_decisionsInterm.resize(m_numberPhysTriggers);
392  m_decisionsFinal.clear();
393  m_decisionsFinal.resize(m_numberPhysTriggers);
394 
395 
396  for(unsigned int algBit = 0; algBit< m_numberPhysTriggers; algBit++) {
397 
398  (m_decisionsInitial[algBit]).first = "NULL";
399  (m_decisionsInitial[algBit]).second = false;
400 
401  (m_decisionsInterm[algBit]).first = "NULL";
402  (m_decisionsInterm[algBit]).second = false;
403 
404  (m_decisionsFinal[algBit]).first = "NULL";
405  (m_decisionsFinal[algBit]).second = false;
406 
407  }
408 
409 
410 }
411 
413 
414  // Reset all the vector contents with null information
415  m_prescales.clear();
416  m_prescales.resize(m_numberPhysTriggers);
417 
418  for(unsigned int algBit = 0; algBit< m_numberPhysTriggers; algBit++) {
419 
420  (m_prescales[algBit]).first = "NULL";
421  (m_prescales[algBit]).second = 1;
422 
423  }
424 
425 }
426 
428 
429  // Reset all the vector contents with null information
430  m_masks.clear();
431  m_masks.resize(m_numberPhysTriggers);
432  m_vetoMasks.clear();
433  m_vetoMasks.resize(m_numberPhysTriggers);
434 
435  for(unsigned int algBit = 0; algBit< m_numberPhysTriggers; algBit++) {
436 
437  (m_masks[algBit]).first = "NULL";
438  (m_masks[algBit]).second = true;
439 
440  (m_vetoMasks[algBit]).first = "NULL";
441  (m_vetoMasks[algBit]).second = false;
442 
443  }
444 
445 }
446 
447 const bool l1t::L1TGlobalUtil::getAlgBitFromName(const std::string& algName, int& bit) const {
448 
449  std::map<std::string, L1TUtmAlgorithm>::const_iterator itAlgo = m_algorithmMap->find(algName);
450  if(itAlgo != m_algorithmMap->end()) {
451  bit = (itAlgo->second).getIndex(); //algoBitNumber();
452  return true;
453  }
454 
455  return false; //did not find anything by that name
456 }
457 
458 const bool l1t::L1TGlobalUtil::getAlgNameFromBit(int& bit, std::string& algName) const {
459 
460  // since we are just looking up the name, doesn't matter which vector we get it from
461  if((m_decisionsInitial[bit]).first != "NULL") {
462  algName = (m_decisionsInitial[bit]).first;
463  return true;
464  }
465  return false; //No name associated with this bit
466 
467 }
468 
469 const bool l1t::L1TGlobalUtil::getInitialDecisionByBit(int& bit, bool& decision) const {
470 
471  /*
472  for(std::vector<GlobalAlgBlk>::const_iterator algBlk = m_uGtAlgBlk->begin(0); algBlk != m_uGtAlgBlk->end(0); ++algBlk) {
473  decision = algBlk->getAlgoDecisionFinal(bit);
474  }
475  */
476  // Need some check that this is a valid bit
477  if((m_decisionsInitial[bit]).first != "NULL") {
478  decision = (m_decisionsInitial[bit]).second;
479  return true;
480  }
481 
482  return false; //couldn't get the information requested.
483 }
484 const bool l1t::L1TGlobalUtil::getIntermDecisionByBit(int& bit, bool& decision) const {
485 
486  // Need some check that this is a valid bit
487  if((m_decisionsInterm[bit]).first != "NULL") {
488  decision = (m_decisionsInterm[bit]).second;
489  return true;
490  }
491 
492  return false; //couldn't get the information requested.
493 }
494 const bool l1t::L1TGlobalUtil::getFinalDecisionByBit(int& bit, bool& decision) const {
495 
496  // Need some check that this is a valid bit
497  if((m_decisionsFinal[bit]).first != "NULL") {
498  decision = (m_decisionsFinal[bit]).second;
499  return true;
500  }
501 
502  return false; //couldn't get the information requested.
503 }
504 const bool l1t::L1TGlobalUtil::getPrescaleByBit(int& bit, int& prescale) const {
505 
506  // Need some check that this is a valid bit
507  if((m_prescales[bit]).first != "NULL") {
508  prescale = (m_prescales[bit]).second;
509  return true;
510  }
511 
512  return false; //couldn't get the information requested.
513 }
514 const bool l1t::L1TGlobalUtil::getMaskByBit(int& bit, bool& mask) const {
515 
516  // Need some check that this is a valid bit
517  if((m_masks[bit]).first != "NULL") {
518  mask = (m_masks[bit]).second;
519  return true;
520  }
521 
522  return false; //couldn't get the information requested.
523 }
524 
525 const bool l1t::L1TGlobalUtil::getVetoMaskByBit(int& bit, bool& veto) const {
526 
527  // Need some check that this is a valid bit
528  if((m_vetoMasks[bit]).first != "NULL") {
529  veto = (m_vetoMasks[bit]).second;
530  return true;
531  }
532 
533  return false; //couldn't get the information requested.
534 }
535 
536 const bool l1t::L1TGlobalUtil::getInitialDecisionByName(const std::string& algName, bool& decision) const {
537 
538  int bit = -1;
539  if(getAlgBitFromName(algName,bit)) {
540  decision = (m_decisionsInitial[bit]).second;
541  return true;
542  }
543 
544  return false; //trigger name was not the menu.
545 }
546 
547 const bool l1t::L1TGlobalUtil::getIntermDecisionByName(const std::string& algName, bool& decision) const {
548 
549  int bit = -1;
550  if(getAlgBitFromName(algName,bit)) {
551  decision = (m_decisionsInterm[bit]).second;
552  return true;
553  }
554 
555  return false; //trigger name was not the menu.
556 }
557 
558 const bool l1t::L1TGlobalUtil::getFinalDecisionByName(const std::string& algName, bool& decision) const {
559 
560  int bit = -1;
561  if(getAlgBitFromName(algName,bit)) {
562  decision = (m_decisionsFinal[bit]).second;
563  return true;
564  }
565 
566  return false; //trigger name was not the menu.
567 }
568 const bool l1t::L1TGlobalUtil::getPrescaleByName(const std::string& algName, int& prescale) const {
569 
570  int bit = -1;
571  if(getAlgBitFromName(algName,bit)) {
572  prescale = (m_prescales[bit]).second;
573  return true;
574  }
575 
576  return false; //trigger name was not the menu.
577 }
578 const bool l1t::L1TGlobalUtil::getMaskByName(const std::string& algName, bool& mask) const {
579 
580  int bit = -1;
581  if(getAlgBitFromName(algName,bit)) {
582  mask = (m_masks[bit]).second;
583  return true;
584  }
585 
586  return false; //trigger name was not the menu.
587 }
588 const bool l1t::L1TGlobalUtil::getVetoMaskByName(const std::string& algName, bool& veto) const {
589 
590  int bit = -1;
591  if(getAlgBitFromName(algName,bit)) {
592  veto = (m_vetoMasks[bit]).second;
593  return true;
594  }
595 
596  return false; //trigger name was not the menu.
597 }
#define LogDebug(id)
const bool getInitialDecisionByBit(int &bit, bool &decision) const
std::string m_preScaleFileName
const bool getAlgBitFromName(const std::string &AlgName, int &bit) const
void retrieveL1Event(const edm::Event &iEvent, const edm::EventSetup &evSetup)
bool m_algorithmTriggersUnmasked
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:462
unsigned long long m_l1GtPfAlgoCacheID
const bool getPrescaleByBit(int &bit, int &prescale) const
const bool getIntermDecisionByBit(int &bit, bool &decision) const
unsigned int m_PreScaleColumn
std::unique_ptr< L1TGlobalUtilHelper > m_l1tGlobalUtilHelper
const bool getInitialDecisionByName(const std::string &algName, bool &decision) const
void resetDecisionVectors()
clear decision vectors on a menu change
const bool getMaskByName(const std::string &algName, bool &mask) const
U second(std::pair< T, U > const &p)
static const PrescalesVetosHelper * readFromEventSetup(const L1TGlobalPrescalesVetos *es)
int iEvent
Definition: GenABIO.cc:230
const bool getFinalDecisionByName(const std::string &algName, bool &decision) const
void OverridePrescalesAndMasks(std::string filename, unsigned int psColumn=1)
#define LogTrace(id)
const bool getAlgNameFromBit(int &bit, std::string &AlgName) const
const bool getVetoMaskByBit(int &bit, bool &veto) const
unsigned long long m_l1GtMenuCacheID
const bool getMaskByBit(int &bit, bool &mask) const
const T & get() const
Definition: EventSetup.h:56
T const * product() const
Definition: ESHandle.h:86
const bool getVetoMaskByName(const std::string &algName, bool &veto) const
const bool getFinalDecisionByBit(int &bit, bool &decision) const
tuple filename
Definition: lut2db_cfg.py:20
const bool getPrescaleByName(const std::string &algName, int &prescale) const
bool m_algorithmTriggersUnprescaled
std::string fullPath() const
Definition: FileInPath.cc:184
const bool getIntermDecisionByName(const std::string &algName, bool &decision) const
int col
Definition: cuy.py:1008
virtual ~L1TGlobalUtil()
destructor
void retrieveL1Setup(const edm::EventSetup &evSetup)
double split
Definition: MVATrainer.cc:139
tuple size
Write out results.
unsigned int m_numberPhysTriggers
void retrieveL1(const edm::Event &iEvent, const edm::EventSetup &evSetup)
initialize the class (mainly reserve)