CMS 3D CMS Logo

L1GtAlgorithmEvaluation.cc
Go to the documentation of this file.
1 
15 // this class header
17 
18 // system include files
19 #include <string>
20 
21 #include <queue>
22 #include <stack>
23 #include <vector>
24 
25 #include <iomanip>
26 #include <iostream>
27 
28 #include <boost/algorithm/string.hpp>
29 #include <ext/hash_map>
30 
31 // user include files
32 
33 //
36 
38 
41 
44  : m_algoResult(false), m_logicalExpression(alg.algoLogicalExpression()),
45  m_rpnVector(alg.algoRpnVector()) {
46 
47  // the rest is properly initialized by default
48 }
49 
50 // methods
51 
54  const int chipNumber,
55  const std::vector<ConditionEvaluationMap> &conditionResultMaps) {
56 
57  // set result to false if there is no expression
58  if (m_rpnVector.empty()) {
59  m_algoResult = false;
60 
61  // it should never be happen
62  throw cms::Exception("FailModule")
63  << "\nEmpty RPN vector for the logical expression = "
64  << m_logicalExpression << std::endl;
65  }
66 
67  // reserve memory
68  int rpnVectorSize = m_rpnVector.size();
69 
70  m_algoCombinationVector.reserve(rpnVectorSize);
71  m_operandTokenVector.reserve(rpnVectorSize);
72 
73  // stack containing temporary results
74  // FIXME we shall find a better solution than static
75  std::stack<bool, std::vector<bool>> resultStack;
76  bool b1, b2;
77 
78  int opNumber = 0;
79 
80  for (RpnVector::const_iterator it = m_rpnVector.begin();
81  it != m_rpnVector.end(); it++) {
82 
83  // LogTrace("L1GlobalTrigger")
84  //<< "\nit->operation = " << it->operation
85  //<< "\nit->operand = '" << it->operand << "'\n"
86  //<< std::endl;
87 
88  switch (it->operation) {
89 
91 
92  CItEvalMap itCond =
93  (conditionResultMaps.at(chipNumber)).find(it->operand);
94  if (itCond != (conditionResultMaps[chipNumber]).end()) {
95 
96  if (nullptr == itCond->second) {
97  // it should never be happen, only valid conditions are in the maps
98  throw cms::Exception("FailModule")
99  << "\nCondition " << (it->operand)
100  << " NULL pointer found in condition map" << std::endl;
101  }
102 
103  //
104  bool condResult = (itCond->second)->condLastResult();
105 
106  resultStack.push(condResult);
107 
108  // only conditions are added to /counted in m_operandTokenVector
109  // opNumber is the index of the condition in the logical expression
110  OperandToken opToken;
111  opToken.tokenName = it->operand;
112  opToken.tokenNumber = opNumber;
113  opToken.tokenResult = condResult;
114 
115  m_operandTokenVector.push_back(opToken);
116  opNumber++;
117 
118  //
119  CombinationsInCond const &combInCondition =
120  (itCond->second)->getCombinationsInCond();
121  m_algoCombinationVector.push_back(combInCondition);
122 
123  } else {
124 
125  // it should never be happen, all conditions are in the maps
126  throw cms::Exception("FailModule")
127  << "\nCondition " << (it->operand) << " not found in condition map"
128  << std::endl;
129  }
130 
131  }
132 
133  break;
135  b1 = resultStack.top();
136  resultStack.pop(); // pop the top
137  resultStack.push(!b1); // and push the result
138  }
139 
140  break;
141  case L1GtLogicParser::OP_OR: {
142  b1 = resultStack.top();
143  resultStack.pop();
144  b2 = resultStack.top();
145  resultStack.pop();
146  resultStack.push(b1 || b2);
147  }
148 
149  break;
151  b1 = resultStack.top();
152  resultStack.pop();
153  b2 = resultStack.top();
154  resultStack.pop();
155  resultStack.push(b1 && b2);
156  }
157 
158  break;
159  default: {
160  // should not arrive here
161  }
162 
163  break;
164  }
165  }
166 
167  // get the result in the top of the stack
168 
169  m_algoResult = resultStack.top();
170 
171  // clear resultStack not needed since it is now a function temporary
172 }
173 
174 // print algorithm evaluation
175 void L1GtAlgorithmEvaluation::print(std::ostream &myCout) const {
176 
177  myCout << std::endl;
178 
179  myCout << " Algorithm result: " << m_algoResult << std::endl;
180 
181  myCout << " CombinationVector size: " << m_algoCombinationVector.size()
182  << std::endl;
183 
184  int operandTokenVectorSize = m_operandTokenVector.size();
185 
186  myCout << " Operand token vector size: " << operandTokenVectorSize;
187 
188  if (operandTokenVectorSize == 0) {
189  myCout << " - not properly initialized! " << std::endl;
190  } else {
191  myCout << std::endl;
192 
193  for (int i = 0; i < operandTokenVectorSize; ++i) {
194 
195  myCout << " " << std::setw(5)
196  << (m_operandTokenVector[i]).tokenNumber << "\t" << std::setw(25)
197  << (m_operandTokenVector[i]).tokenName << "\t"
198  << (m_operandTokenVector[i]).tokenResult << std::endl;
199  }
200  }
201 
202  myCout << std::endl;
203 }
bool m_algoResult
algorithm result
std::vector< OperandToken > m_operandTokenVector
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
std::vector< CombinationsInCond > m_algoCombinationVector
void print(std::ostream &myCout) const
L1GtAlgorithmEvaluation(const L1GtAlgorithm &)
constructor
std::vector< SingleCombInCond > CombinationsInCond
all the object combinations evaluated to true in the condition
std::string const & m_logicalExpression
ConditionEvaluationMap::const_iterator CItEvalMap
void evaluateAlgorithm(const int chipNumber, const std::vector< ConditionEvaluationMap > &)
evaluate an algorithm