CMS 3D CMS Logo

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 = 0;
43 
45  m_triggerMaskAlgoTrig = 0ULL;
46 }
47 
50  L1TGlobalUtil(pset, iC) {
51 }
52 
55  L1TGlobalUtil() {
56  m_l1tGlobalUtilHelper.reset(new L1TGlobalUtilHelper(pset, iC));
57  m_readPrescalesFromFile=m_l1tGlobalUtilHelper->readPrescalesFromFile();
58 }
59 
60 // destructor
62 
63  // empty
64 
65 }
66 
68  edm::FileInPath f1("L1Trigger/L1TGlobal/data/Luminosity/startup/" + filename);
70  m_PreScaleColumn = psColumn;
71 }
72 
74  // typically, the L1T menu and prescale table (may change only between Runs)
75  retrieveL1Setup(evSetup);
76  // typically the prescale set index used and the event by event accept/reject info (changes between Events)
77  retrieveL1Event(iEvent,evSetup);
78 }
79 
81  edm::EDGetToken gtAlgToken) {
82  // typically, the L1T menu and prescale table (may change only between Runs)
83  retrieveL1Setup(evSetup);
84  // typically the prescale set index used and the event by event accept/reject info (changes between Events)
85  retrieveL1Event(iEvent,evSetup,gtAlgToken);
86 }
87 
89 
90  // get / update the trigger menu from the EventSetup
91  // local cache & check on cacheIdentifier
92  unsigned long long l1GtMenuCacheID = evSetup.get<L1TUtmTriggerMenuRcd>().cacheIdentifier();
93 
94  if (m_l1GtMenuCacheID != l1GtMenuCacheID) {
95 
97  evSetup.get< L1TUtmTriggerMenuRcd>().get(l1GtMenu) ;
98  m_l1GtMenu = l1GtMenu.product();
99 
100  //std::cout << "Attempting to fill the map " << std::endl;
102 
103  //reset vectors since we have new menu
105 
106  m_l1GtMenuCacheID = l1GtMenuCacheID;
107  }
108 
110  unsigned long long l1GtPfAlgoCacheID = evSetup.get<L1TGlobalPrescalesVetosRcd>().cacheIdentifier();
111 
112  if (m_l1GtPfAlgoCacheID != l1GtPfAlgoCacheID) {
113  //std::cout << "Reading Prescales and Masks from dB" << std::endl;
114 
115  // clear and dimension
118 
119  edm::ESHandle< L1TGlobalPrescalesVetos > l1GtPrescalesVetoes;
120  evSetup.get< L1TGlobalPrescalesVetosRcd >().get( l1GtPrescalesVetoes );
121  const L1TGlobalPrescalesVetos * es = l1GtPrescalesVetoes.product();
123 
125  m_numberPhysTriggers = (*m_prescaleFactorsAlgoTrig)[0].size(); // assumes all prescale columns are the same length
126 
128 
129  m_l1GtPfAlgoCacheID = l1GtPfAlgoCacheID;
130  }
131  } else {
132  //Load the prescales from external file
133  // clear and dimension
134  if (!m_filledPrescales){
137 
139 
140  // Set Prescale factors to initial
143  m_filledPrescales=true;
144  }
145  }
146 
147  //Protect against poor prescale column choice (I don't think there is a way this happen as currently structured)
149  LogTrace("l1t|Global")
150  << "\nNo Prescale Set: " << m_PreScaleColumn
151  << "\nMax Prescale Set value : " << m_prescaleFactorsAlgoTrig->size()
152  << "\nSetting prescale column to 0"
153  << std::endl;
154  m_PreScaleColumn = 0;
155  }
156  //std::cout << "Using prescale column: " << m_PreScaleColumn << std::endl;
157  const std::vector<int>& prescaleSet = (*m_prescaleFactorsAlgoTrig)[m_PreScaleColumn];
158 
159  for (std::map<std::string, L1TUtmAlgorithm>::const_iterator itAlgo = m_algorithmMap->begin(); itAlgo != m_algorithmMap->end(); itAlgo++) {
160 
161  // Get the algorithm name
162  std::string algName = itAlgo->first;
163  int algBit = (itAlgo->second).getIndex(); //algoBitNumber();
164 
165  (m_prescales[algBit]).first = algName;
166  (m_prescales[algBit]).second = prescaleSet[algBit];
167 
168  LogDebug("l1t|Global")<< "Number of bunch crossings stored: " << (*m_triggerMaskAlgoTrig).size() << endl;
169 
170  const std::map<int, std::vector<int> >* triggerAlgoMaskAlgoTrig = m_triggerMaskAlgoTrig;
171  std::map<int, std::vector<int> >::const_iterator it=triggerAlgoMaskAlgoTrig->begin();
172 
173  std::vector<int> maskedBxs;
174  (m_masks[algBit]).first = algName;
175  (m_masks[algBit]).second = maskedBxs;
176  while(it != triggerAlgoMaskAlgoTrig->end())
177  {
178  std::vector<int> masks = it->second;
179  //std::cout<< "BX: " << it->first<<" VecSize: "<< masks.size();
180  //std::cout << "\tMasked algos: ";
181  for ( unsigned int imask=0; imask< masks.size(); imask++){
182  if (masks.at(imask) == algBit) maskedBxs.push_back(it->first);
183  // std::cout << "\t" << masks.at(imask);
184  }
185  //std::cout << "\n";
186  it++;
187  }
188 
189  if (maskedBxs.size()>0){
190  LogDebug("l1t|Global") << "i Algo: "<< algBit << "\t" << algName << " masked\n";
191  for ( unsigned int ibx=0; ibx< maskedBxs.size(); ibx++){
192  // std::cout << "\t" << maskedBxs.at(ibx);
193  (m_masks[algBit]).second = maskedBxs;
194  }
195  }
196  }
197 
198 }
199 
201  retrieveL1Event(iEvent, evSetup, m_l1tGlobalUtilHelper->l1tAlgBlkToken());
202 }
203 
205  edm::EDGetToken gtAlgToken) {
206 
207 // Get the Global Trigger Output Algorithm block
208  iEvent.getByToken(gtAlgToken,m_uGtAlgBlk);
209  m_finalOR = false;
210 
211  //Make sure we have a valid AlgBlk
212  if(m_uGtAlgBlk.isValid()) {
213  // get the GlabalAlgBlk (Stupid find better way) of BX=0
214  std::vector<GlobalAlgBlk>::const_iterator algBlk = m_uGtAlgBlk->begin(0);
215  if (algBlk != m_uGtAlgBlk->end(0)){
217  m_PreScaleColumn = static_cast<unsigned int>(algBlk->getPreScColumn());
218  }
219  const std::vector<int>& prescaleSet = (*m_prescaleFactorsAlgoTrig)[m_PreScaleColumn];
220 
221  // Grab the final OR from the AlgBlk,
222  m_finalOR = algBlk->getFinalOR();
223 
224  // Make a map of the trigger name and whether it passed various stages (initial,prescale,final)
225  // Note: might be able to improve performance by not full remaking map with names each time
226  for (std::map<std::string, L1TUtmAlgorithm>::const_iterator itAlgo = m_algorithmMap->begin(); itAlgo != m_algorithmMap->end(); itAlgo++) {
227 
228  // Get the algorithm name
229  std::string algName = itAlgo->first;
230  int algBit = (itAlgo->second).getIndex(); //algoBitNumber();
231 
232  bool decisionInitial = algBlk->getAlgoDecisionInitial(algBit);
233  (m_decisionsInitial[algBit]).first = algName;
234  (m_decisionsInitial[algBit]).second = decisionInitial;
235 
236  bool decisionInterm = algBlk->getAlgoDecisionInterm(algBit);
237  (m_decisionsInterm[algBit]).first = algName;
238  (m_decisionsInterm[algBit]).second = decisionInterm;
239 
240  bool decisionFinal = algBlk->getAlgoDecisionFinal(algBit);
241  (m_decisionsFinal[algBit]).first = algName;
242  (m_decisionsFinal[algBit]).second = decisionFinal;
243 
244  (m_prescales[algBit]).first = algName;
245  (m_prescales[algBit]).second = prescaleSet[algBit];
246 
247  LogDebug("l1t|Global") << "Number of bunch crossings stored: " << (*m_triggerMaskAlgoTrig).size() << endl;
248 
249  const std::map<int, std::vector<int> >* triggerAlgoMaskAlgoTrig = m_triggerMaskAlgoTrig;
250  std::map<int, std::vector<int> >::const_iterator it=triggerAlgoMaskAlgoTrig->begin();
251 
252  std::vector<int> maskedBxs;
253  (m_masks[algBit]).first = algName;
254  (m_masks[algBit]).second = maskedBxs;
255 
256  while(it != triggerAlgoMaskAlgoTrig->end())
257  {
258  std::vector<int> masks = it->second;
259  //std::cout<< "BX: " << it->first<<" VecSize: "<< masks.size();
260  //std::cout << "\tMasked algos: ";
261  for ( unsigned int imask=0; imask< masks.size(); imask++){
262  if (masks.at(imask) == algBit) maskedBxs.push_back(it->first);
263  // std::cout << "\t" << masks.at(imask);
264  }
265  it++;
266  }
267 
268  if (maskedBxs.size()>0){
269  LogDebug("l1t|Global") << "Algo: "<< algBit << "\t" << algName << " masked\n";
270  for ( unsigned int ibx=0; ibx< maskedBxs.size(); ibx++){
271  // std::cout << "\t" << maskedBxs.at(ibx);
272  (m_masks[algBit]).second = maskedBxs;
273  }
274  }
275  }
276  } else {
277  //cout << "Error empty AlgBlk recovered.\n";
278  }
279  } else {
280  //cout<< "Error no valid uGT Algorithm Data with Token provided " << endl;
281  }
282 }
283 
285 
286  std::ifstream inputPrescaleFile;
287  //std::cout << "Reading prescales from file: " << m_preScaleFileName << std::endl;
288  inputPrescaleFile.open(m_preScaleFileName);
289 
290  std::vector<std::vector<int> > vec;
291  std::vector<std::vector<int> > prescale_vec;
292 
293  if( inputPrescaleFile ){
294  std::string prefix1("#");
295  std::string prefix2("-1");
296 
298 
299  bool first = true;
300 
301  while( getline(inputPrescaleFile,line) ){
302 
303  if( !line.compare(0, prefix1.size(), prefix1) ) continue;
304  //if( !line.compare(0, prefix2.size(), prefix2) ) continue;
305 
306  istringstream split(line);
307  int value;
308  int col = 0;
309  char sep;
310 
311  while( split >> value ){
312  if( first ){
313  // Each new value read on line 1 should create a new inner vector
314  vec.push_back(std::vector<int>());
315  }
316 
317  vec[col].push_back(value);
318  ++col;
319 
320  // read past the separator
321  split>>sep;
322  }
323 
324  // Finished reading line 1 and creating as many inner
325  // vectors as required
326  first = false;
327  }
328 
329 
330  int NumPrescaleSets = 0;
331  for( int iCol=0; iCol<int(vec.size()); iCol++ ){
332  if( vec[iCol].size() > 0 ){
333  int firstRow = vec[iCol][0];
334 
335  if( firstRow >= 0 ) NumPrescaleSets++;
336  //else if( firstRow==-2 ) maskColumn = iCol;
337  //else if( firstRow==-3 ) maskVetoColumn = iCol;
338  }
339  }
340 
341  //std::cout << "NumPrescaleSets= " << NumPrescaleSets << std::endl;
342  if( NumPrescaleSets > 0 ){
343  // Fill default prescale set
344  for( int iSet=0; iSet<NumPrescaleSets; iSet++ ){
345  prescale_vec.push_back(std::vector<int>());
346  for( unsigned int iBit = 0; iBit < m_numberPhysTriggers; ++iBit ){
347  int inputDefaultPrescale = 1;
348  prescale_vec[iSet].push_back(inputDefaultPrescale);
349  }
350  }
351 
352  // Fill non-trivial prescale set
353  for( int iBit=1; iBit<int(vec[0].size()); iBit++ ){
354  unsigned int algoBit = vec[0][iBit];
355  // algoBit must be less than the number of triggers
356  if( algoBit < m_numberPhysTriggers ){
357  for( int iSet=0; iSet<int(vec.size()); iSet++ ){
358  int useSet = -1;
359  if( vec[iSet].size() > 0 ){
360  useSet = vec[iSet][0];
361  }
362  useSet -= 1;
363 
364  if( useSet<0 ) continue;
365 
366  int prescale = vec[iSet][iBit];
367  prescale_vec[useSet][algoBit] = prescale;
368  }
369  }
370  else{
371  LogTrace("l1t|Global")
372  << "\nPrescale file has algo bit: " << algoBit
373  << "\nThis is larger than the number of triggers: " << m_numberPhysTriggers
374  << "\nSomething is wrong. Ignoring."
375  << std::endl;
376  }
377  }
378  }
379 
380  }
381  else {
382  LogTrace("l1t|Global")
383  << "\nCould not find file: " << m_preScaleFileName
384  << "\nFilling the prescale vectors with prescale 1"
385  << "\nSetting prescale set to 0"
386  << std::endl;
387 
388  m_PreScaleColumn = 0;
389 
390  for( int col=0; col < 1; col++ ){
391  prescale_vec.push_back(std::vector<int>());
392  for( unsigned int iBit = 0; iBit < m_numberPhysTriggers; ++iBit ){
393  int inputDefaultPrescale = 0;
394  prescale_vec[col].push_back(inputDefaultPrescale);
395  }
396  }
397  }
398 
399  inputPrescaleFile.close();
400 
401  m_initialPrescaleFactorsAlgoTrig = prescale_vec;
402  // setting of bx masks from an input file not enabled; do not see a use case at the moment
403  std::map<int, std::vector<int> > m_initialTriggerMaskAlgoTrig;
404 
405 }
406 
408 
409  // Reset all the vector contents with null information
410  m_decisionsInitial.clear();
412  m_decisionsInterm.clear();
414  m_decisionsFinal.clear();
416 
417 
418  for(unsigned int algBit = 0; algBit< m_maxNumberPhysTriggers; algBit++) {
419 
420  (m_decisionsInitial[algBit]).first = "NULL";
421  (m_decisionsInitial[algBit]).second = false;
422 
423  (m_decisionsInterm[algBit]).first = "NULL";
424  (m_decisionsInterm[algBit]).second = false;
425 
426  (m_decisionsFinal[algBit]).first = "NULL";
427  (m_decisionsFinal[algBit]).second = false;
428 
429  }
430 
431 
432 }
433 
435 
436  // Reset all the vector contents with null information
437  m_prescales.clear();
439 
440  for(unsigned int algBit = 0; algBit< m_maxNumberPhysTriggers; algBit++) {
441 
442  (m_prescales[algBit]).first = "NULL";
443  (m_prescales[algBit]).second = 1;
444 
445  }
446 
447 }
448 
450 
451  // Reset all the vector contents with null information
452  m_masks.clear();
454 
455  for(unsigned int algBit = 0; algBit< m_maxNumberPhysTriggers; algBit++) {
456 
457  (m_masks[algBit]).first = "NULL";
458  // ccla (m_masks[algBit]).second = true;
459 
460  }
461 
462 }
463 
464 const bool l1t::L1TGlobalUtil::getAlgBitFromName(const std::string& algName, int& bit) const {
465 
466  std::map<std::string, L1TUtmAlgorithm>::const_iterator itAlgo = m_algorithmMap->find(algName);
467  if(itAlgo != m_algorithmMap->end()) {
468  bit = (itAlgo->second).getIndex(); //algoBitNumber();
469  return true;
470  }
471 
472  return false; //did not find anything by that name
473 }
474 
475 const bool l1t::L1TGlobalUtil::getAlgNameFromBit(int& bit, std::string& algName) const {
476 
477  // since we are just looking up the name, doesn't matter which vector we get it from
478  if((m_decisionsInitial[bit]).first != "NULL") {
479  algName = (m_decisionsInitial[bit]).first;
480  return true;
481  }
482  return false; //No name associated with this bit
483 
484 }
485 
486 const bool l1t::L1TGlobalUtil::getInitialDecisionByBit(int& bit, bool& decision) const {
487 
488  /*
489  for(std::vector<GlobalAlgBlk>::const_iterator algBlk = m_uGtAlgBlk->begin(0); algBlk != m_uGtAlgBlk->end(0); ++algBlk) {
490  decision = algBlk->getAlgoDecisionFinal(bit);
491  }
492  */
493  // Need some check that this is a valid bit
494  if((m_decisionsInitial[bit]).first != "NULL") {
495  decision = (m_decisionsInitial[bit]).second;
496  return true;
497  }
498 
499  return false; //couldn't get the information requested.
500 }
501 const bool l1t::L1TGlobalUtil::getIntermDecisionByBit(int& bit, bool& decision) const {
502 
503  // Need some check that this is a valid bit
504  if((m_decisionsInterm[bit]).first != "NULL") {
505  decision = (m_decisionsInterm[bit]).second;
506  return true;
507  }
508 
509  return false; //couldn't get the information requested.
510 }
511 const bool l1t::L1TGlobalUtil::getFinalDecisionByBit(int& bit, bool& decision) const {
512 
513  // Need some check that this is a valid bit
514  if((m_decisionsFinal[bit]).first != "NULL") {
515  decision = (m_decisionsFinal[bit]).second;
516  return true;
517  }
518 
519  return false; //couldn't get the information requested.
520 }
521 const bool l1t::L1TGlobalUtil::getPrescaleByBit(int& bit, int& prescale) const {
522 
523  // Need some check that this is a valid bit
524  if((m_prescales[bit]).first != "NULL") {
525  prescale = (m_prescales[bit]).second;
526  return true;
527  }
528 
529  return false; //couldn't get the information requested.
530 }
531 const bool l1t::L1TGlobalUtil::getMaskByBit(int& bit, std::vector<int>& mask) const {
532 
533  // Need some check that this is a valid bit
534  if((m_masks[bit]).first != "NULL") {
535  mask = (m_masks[bit]).second;
536  return true;
537  }
538 
539  return false; //couldn't get the information requested.
540 }
541 
542 const bool l1t::L1TGlobalUtil::getInitialDecisionByName(const std::string& algName, bool& decision) const {
543 
544  int bit = -1;
545  if(getAlgBitFromName(algName,bit)) {
546  decision = (m_decisionsInitial[bit]).second;
547  return true;
548  }
549 
550  return false; //trigger name was not the menu.
551 }
552 
553 const bool l1t::L1TGlobalUtil::getIntermDecisionByName(const std::string& algName, bool& decision) const {
554 
555  int bit = -1;
556  if(getAlgBitFromName(algName,bit)) {
557  decision = (m_decisionsInterm[bit]).second;
558  return true;
559  }
560 
561  return false; //trigger name was not the menu.
562 }
563 
564 const bool l1t::L1TGlobalUtil::getFinalDecisionByName(const std::string& algName, bool& decision) const {
565 
566  int bit = -1;
567  if(getAlgBitFromName(algName,bit)) {
568  decision = (m_decisionsFinal[bit]).second;
569  return true;
570  }
571 
572  return false; //trigger name was not the menu.
573 }
574 const bool l1t::L1TGlobalUtil::getPrescaleByName(const std::string& algName, int& prescale) const {
575 
576  int bit = -1;
577  if(getAlgBitFromName(algName,bit)) {
578  prescale = (m_prescales[bit]).second;
579  return true;
580  }
581 
582  return false; //trigger name was not the menu.
583 }
584 const bool l1t::L1TGlobalUtil::getMaskByName(const std::string& algName, std::vector<int>& mask) const {
585 
586  int bit = -1;
587  if(getAlgBitFromName(algName,bit)) {
588  mask = (m_masks[bit]).second;
589  return true;
590  }
591 
592  return false; //trigger name was not the menu.
593 }
#define LogDebug(id)
size
Write out results.
const_iterator end(int bx) const
const bool getInitialDecisionByBit(int &bit, bool &decision) const
const L1TUtmTriggerMenu * m_l1GtMenu
std::string m_preScaleFileName
const bool getAlgBitFromName(const std::string &AlgName, int &bit) const
const std::vector< std::vector< int > > & prescaleTable() 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:460
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::vector< std::vector< int > > m_initialPrescaleFactorsAlgoTrig
std::unique_ptr< L1TGlobalUtilHelper > m_l1tGlobalUtilHelper
const bool getInitialDecisionByName(const std::string &algName, bool &decision) const
std::vector< std::pair< std::string, bool > > m_decisionsInitial
void resetDecisionVectors()
clear decision vectors on a menu change
const std::map< int, std::vector< int > > & triggerAlgoBxMask() const
const std::map< std::string, L1TUtmAlgorithm > * m_algorithmMap
U second(std::pair< T, U > const &p)
const std::map< int, std::vector< int > > m_initialTriggerMaskAlgoTrig
static const PrescalesVetosHelper * readFromEventSetup(const L1TGlobalPrescalesVetos *es)
std::vector< std::pair< std::string, std::vector< int > > > m_masks
int iEvent
Definition: GenABIO.cc:230
const std::map< int, std::vector< int > > * m_triggerMaskAlgoTrig
const std::vector< std::vector< int > > * m_prescaleFactorsAlgoTrig
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
bool isValid() const
Definition: HandleBase.h:74
const bool getFinalDecisionByName(const std::string &algName, bool &decision) const
void OverridePrescalesAndMasks(std::string filename, unsigned int psColumn=1)
#define LogTrace(id)
const bool getMaskByName(const std::string &algName, std::vector< int > &mask) const
std::vector< std::pair< std::string, bool > > m_decisionsInterm
const bool getAlgNameFromBit(int &bit, std::string &AlgName) const
unsigned long long m_l1GtMenuCacheID
const T & get() const
Definition: EventSetup.h:56
const l1t::PrescalesVetosHelper * m_l1GtPrescalesVetoes
const bool getFinalDecisionByBit(int &bit, bool &decision) const
const std::map< std::string, L1TUtmAlgorithm > & getAlgorithmMap() const
std::vector< std::pair< std::string, bool > > m_decisionsFinal
col
Definition: cuy.py:1008
const bool getPrescaleByName(const std::string &algName, int &prescale) const
const bool getMaskByBit(int &bit, std::vector< int > &mask) const
bool m_algorithmTriggersUnprescaled
edm::Handle< BXVector< GlobalAlgBlk > > m_uGtAlgBlk
std::string fullPath() const
Definition: FileInPath.cc:184
const std::vector< std::pair< std::string, std::vector< int > > > & masks()
const bool getIntermDecisionByName(const std::string &algName, bool &decision) const
const unsigned int m_maxNumberPhysTriggers
const_iterator begin(int bx) const
virtual ~L1TGlobalUtil()
destructor
void retrieveL1Setup(const edm::EventSetup &evSetup)
T const * product() const
Definition: ESHandle.h:86
std::vector< std::pair< std::string, int > > m_prescales
double split
Definition: MVATrainer.cc:139
unsigned int m_numberPhysTriggers
void retrieveL1(const edm::Event &iEvent, const edm::EventSetup &evSetup)
initialize the class (mainly reserve)