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