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