Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:20:00

0001 /**
0002  * \class L1GtAlgorithmEvaluation
0003  *
0004  *
0005  * Description: Evaluation of a L1 Global Trigger algorithm.
0006  *
0007  * Implementation:
0008  *    <TODO: enter implementation details>
0009  *
0010  * \author: Vasile Mihai Ghete   - HEPHY Vienna
0011  *
0012  *
0013  */
0014 
0015 // this class header
0016 #include "L1Trigger/GlobalTrigger/interface/L1GtAlgorithmEvaluation.h"
0017 
0018 // system include files
0019 #include <string>
0020 
0021 #include <queue>
0022 #include <stack>
0023 #include <vector>
0024 
0025 #include <iomanip>
0026 #include <iostream>
0027 
0028 #include <boost/algorithm/string.hpp>
0029 
0030 // user include files
0031 
0032 //
0033 #include "CondFormats/L1TObjects/interface/L1GtAlgorithm.h"
0034 #include "DataFormats/L1GlobalTrigger/interface/L1GlobalTriggerObjectMapFwd.h"
0035 
0036 #include "L1Trigger/GlobalTrigger/interface/L1GtConditionEvaluation.h"
0037 
0038 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0039 #include "FWCore/Utilities/interface/Exception.h"
0040 
0041 /// constructor from an algorithm from event setup
0042 L1GtAlgorithmEvaluation::L1GtAlgorithmEvaluation(const L1GtAlgorithm &alg)
0043     : m_algoResult(false), m_logicalExpression(alg.algoLogicalExpression()), m_rpnVector(alg.algoRpnVector()) {
0044   // the rest is properly initialized by default
0045 }
0046 
0047 // methods
0048 
0049 /// evaluate an algorithm
0050 void L1GtAlgorithmEvaluation::evaluateAlgorithm(const int chipNumber,
0051                                                 const std::vector<ConditionEvaluationMap> &conditionResultMaps) {
0052   // set result to false if there is no expression
0053   if (m_rpnVector.empty()) {
0054     m_algoResult = false;
0055 
0056     // it should never be happen
0057     throw cms::Exception("FailModule") << "\nEmpty RPN vector for the logical expression = " << m_logicalExpression
0058                                        << std::endl;
0059   }
0060 
0061   // reserve memory
0062   int rpnVectorSize = m_rpnVector.size();
0063 
0064   m_algoCombinationVector.reserve(rpnVectorSize);
0065   m_operandTokenVector.reserve(rpnVectorSize);
0066 
0067   // stack containing temporary results
0068   // FIXME we shall find a better solution than static
0069   std::stack<bool, std::vector<bool>> resultStack;
0070   bool b1, b2;
0071 
0072   int opNumber = 0;
0073 
0074   for (RpnVector::const_iterator it = m_rpnVector.begin(); it != m_rpnVector.end(); it++) {
0075     // LogTrace("L1GlobalTrigger")
0076     //<< "\nit->operation = " << it->operation
0077     //<< "\nit->operand =   '" << it->operand << "'\n"
0078     //<< std::endl;
0079 
0080     switch (it->operation) {
0081       case L1GtLogicParser::OP_OPERAND: {
0082         CItEvalMap itCond = (conditionResultMaps.at(chipNumber)).find(it->operand);
0083         if (itCond != (conditionResultMaps[chipNumber]).end()) {
0084           if (nullptr == itCond->second) {
0085             // it should never be happen, only valid conditions are in the maps
0086             throw cms::Exception("FailModule")
0087                 << "\nCondition " << (it->operand) << " NULL pointer found in condition map" << std::endl;
0088           }
0089 
0090           //
0091           bool condResult = (itCond->second)->condLastResult();
0092 
0093           resultStack.push(condResult);
0094 
0095           // only conditions are added to /counted in m_operandTokenVector
0096           // opNumber is the index of the condition in the logical expression
0097           OperandToken opToken;
0098           opToken.tokenName = it->operand;
0099           opToken.tokenNumber = opNumber;
0100           opToken.tokenResult = condResult;
0101 
0102           m_operandTokenVector.push_back(opToken);
0103           opNumber++;
0104 
0105           //
0106           CombinationsInCond const &combInCondition = (itCond->second)->getCombinationsInCond();
0107           m_algoCombinationVector.push_back(combInCondition);
0108 
0109         } else {
0110           // it should never be happen, all conditions are in the maps
0111           throw cms::Exception("FailModule")
0112               << "\nCondition " << (it->operand) << " not found in condition map" << std::endl;
0113         }
0114 
0115       }
0116 
0117       break;
0118       case L1GtLogicParser::OP_NOT: {
0119         b1 = resultStack.top();
0120         resultStack.pop();      // pop the top
0121         resultStack.push(!b1);  // and push the result
0122       }
0123 
0124       break;
0125       case L1GtLogicParser::OP_OR: {
0126         b1 = resultStack.top();
0127         resultStack.pop();
0128         b2 = resultStack.top();
0129         resultStack.pop();
0130         resultStack.push(b1 || b2);
0131       }
0132 
0133       break;
0134       case L1GtLogicParser::OP_AND: {
0135         b1 = resultStack.top();
0136         resultStack.pop();
0137         b2 = resultStack.top();
0138         resultStack.pop();
0139         resultStack.push(b1 && b2);
0140       }
0141 
0142       break;
0143       default: {
0144         // should not arrive here
0145       }
0146 
0147       break;
0148     }
0149   }
0150 
0151   // get the result in the top of the stack
0152 
0153   m_algoResult = resultStack.top();
0154 
0155   // clear resultStack not needed since it is now a function temporary
0156 }
0157 
0158 // print algorithm evaluation
0159 void L1GtAlgorithmEvaluation::print(std::ostream &myCout) const {
0160   myCout << std::endl;
0161 
0162   myCout << "    Algorithm result:          " << m_algoResult << std::endl;
0163 
0164   myCout << "    CombinationVector size:    " << m_algoCombinationVector.size() << std::endl;
0165 
0166   int operandTokenVectorSize = m_operandTokenVector.size();
0167 
0168   myCout << "    Operand token vector size: " << operandTokenVectorSize;
0169 
0170   if (operandTokenVectorSize == 0) {
0171     myCout << "   - not properly initialized! " << std::endl;
0172   } else {
0173     myCout << std::endl;
0174 
0175     for (int i = 0; i < operandTokenVectorSize; ++i) {
0176       myCout << "      " << std::setw(5) << (m_operandTokenVector[i]).tokenNumber << "\t" << std::setw(25)
0177              << (m_operandTokenVector[i]).tokenName << "\t" << (m_operandTokenVector[i]).tokenResult << std::endl;
0178     }
0179   }
0180 
0181   myCout << std::endl;
0182 }