CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
L1GtAlgorithmEvaluation.cc
Go to the documentation of this file.
1 
17 // this class header
19 
20 // system include files
21 #include <string>
22 
23 #include <stack>
24 #include <queue>
25 #include <vector>
26 
27 #include <iostream>
28 #include <iomanip>
29 
30 #include <boost/algorithm/string.hpp>
31 #include <ext/hash_map>
32 
33 // user include files
34 
35 // base class
37 
38 //
41 
43 
46 
47 // constructor
49  L1GtLogicParser() {
50 
51  m_algoResult = false;
52 
53  // the rest is properly initialized by default
54 }
55 
58  L1GtLogicParser() {
59 
61  m_rpnVector = alg.algoRpnVector();
62 
63  m_algoResult = false;
64 
65  // the rest is properly initialized by default
66 
67 }
68 
69 // copy constructor
71 
72  // parser part
75 
76  // L1GtAlgorithmEvaluation part
79 
80 }
81 
82 // destructor
84 
85  // empty
86 
87 }
88 
89 // methods
90 
93  const std::vector<ConditionEvaluationMap>& conditionResultMaps) {
94 
95  // set result to false if there is no expression
96  if (m_rpnVector.empty() ) {
97  m_algoResult = false;
98 
99  // it should never be happen
100  throw cms::Exception("FailModule")
101  << "\nEmpty RPN vector for the logical expression = "
103  << std::endl;
104 
105  }
106 
107  // reserve memory
108  int rpnVectorSize = m_rpnVector.size();
109 
110  m_algoCombinationVector.reserve(rpnVectorSize);
111  m_operandTokenVector.reserve(rpnVectorSize);
112 
113  // stack containing temporary results
114  std::stack<bool> resultStack;
115  bool b1, b2;
116 
117  int opNumber = 0;
118 
119  for (RpnVector::const_iterator it = m_rpnVector.begin(); it != m_rpnVector.end(); it++) {
120 
121  //LogTrace("L1GtAlgorithmEvaluation")
122  //<< "\nit->operation = " << it->operation
123  //<< "\nit->operand = '" << it->operand << "'\n"
124  //<< std::endl;
125 
126  switch (it->operation) {
127 
128  case OP_OPERAND: {
129 
130  CItEvalMap itCond = (conditionResultMaps.at(chipNumber)).find(it->operand);
131  if (itCond != (conditionResultMaps[chipNumber]).end()) {
132 
133  //
134  bool condResult = (itCond->second)->condLastResult();
135 
136  resultStack.push(condResult);
137 
138  // only conditions are added to /counted in m_operandTokenVector
139  // opNumber is the index of the condition in the logical expression
140  OperandToken opToken;
141  opToken.tokenName = it->operand;
142  opToken.tokenNumber = opNumber;
143  opToken.tokenResult = condResult;
144 
145  m_operandTokenVector.push_back(opToken);
146  opNumber++;
147 
148  //
149  CombinationsInCond* combInCondition = (itCond->second)->getCombinationsInCond();
150  m_algoCombinationVector.push_back(*combInCondition);
151 
152  }
153  else {
154 
155  // it should never be happen, all conditions are in the maps
156  throw cms::Exception("FailModule")
157  << "\nCondition " << (it->operand) << " not found in condition map"
158  << std::endl;
159 
160  }
161 
162  }
163 
164  break;
165  case OP_NOT: {
166  b1 = resultStack.top();
167  resultStack.pop(); // pop the top
168  resultStack.push(!b1); // and push the result
169  }
170 
171  break;
172  case OP_OR: {
173  b1 = resultStack.top();
174  resultStack.pop();
175  b2 = resultStack.top();
176  resultStack.pop();
177  resultStack.push(b1 || b2);
178  }
179 
180  break;
181  case OP_AND: {
182  b1 = resultStack.top();
183  resultStack.pop();
184  b2 = resultStack.top();
185  resultStack.pop();
186  resultStack.push(b1 && b2);
187  }
188 
189  break;
190  default: {
191  // should not arrive here
192  }
193 
194  break;
195  }
196 
197  }
198 
199  // get the result in the top of the stack
200 
201  m_algoResult = resultStack.top();
202 
203 
204 }
205 
206 // print algorithm evaluation
207 void L1GtAlgorithmEvaluation::print(std::ostream& myCout) const {
208 
209  myCout << std::endl;
210 
211  myCout << " Algorithm result: " << m_algoResult << std::endl;
212 
213  myCout << " CombinationVector size: " << m_algoCombinationVector.size() << std::endl;
214 
215  int operandTokenVectorSize = m_operandTokenVector.size();
216 
217  myCout << " Operand token vector size: " << operandTokenVectorSize;
218 
219  if (operandTokenVectorSize == 0) {
220  myCout << " - not properly initialized! " << std::endl;
221  }
222  else {
223  myCout << std::endl;
224 
225  for (int i = 0; i < operandTokenVectorSize; ++i) {
226 
227  myCout << " " << std::setw(5) << (m_operandTokenVector[i]).tokenNumber << "\t"
228  << std::setw(25) << (m_operandTokenVector[i]).tokenName << "\t"
229  << (m_operandTokenVector[i]).tokenResult
230  << std::endl;
231 
232  }
233 
234  }
235 
236  myCout << std::endl;
237 }
238 
std::string logicalExpression() const
return the logical expression
int i
Definition: DBlmapReader.cc:9
virtual ~L1GtAlgorithmEvaluation()
destructor
bool m_algoResult
algorithm result
RpnVector rpnVector() const
return the RPN vector
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
const std::vector< L1GtLogicParser::TokenRPN > & algoRpnVector() const
return the RPN vector
Definition: L1GtAlgorithm.h:87
std::vector< OperandToken > m_operandTokenVector
vector of operand tokens
const std::vector< CombinationsInCond > * gtAlgoCombinationVector() const
std::string const & algoLogicalExpression() const
get / set the logical expression for the algorithm
Definition: L1GtAlgorithm.h:76
std::vector< CombinationsInCond > m_algoCombinationVector
std::string m_logicalExpression
logical expression to be parsed
const bool & gtAlgoResult() const
get / set the result of the algorithm
RpnVector m_rpnVector
RPN vector - equivalent to the logical expression.
void print(std::ostream &myCout) const
std::vector< TokenRPN > RpnVector
std::vector< SingleCombInCond > CombinationsInCond
all the object combinations evaluated to true in the condition
ConditionEvaluationMap::const_iterator CItEvalMap
void evaluateAlgorithm(const int chipNumber, const std::vector< ConditionEvaluationMap > &)
evaluate an algorithm