Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-06-26 02:09:43

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