CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ConditionEvaluation.h
Go to the documentation of this file.
1 #ifndef GlobalTrigger_ConditionEvaluation_h
2 #define GlobalTrigger_ConditionEvaluation_h
3 
18 // system include files
19 #include <iostream>
20 
21 #include <string>
22 #include <vector>
23 
24 #include <boost/cstdint.hpp>
25 
26 // user include files
27 
28 // base class
29 
30 //
33 
34 // forward declarations
35 
36 namespace l1t {
37 
38 // class interface
40 {
41 
42 public:
43 
48  m_verbosity(0) {}
49 
50 
53 
54 public:
55 
58  inline int condMaxNumberObjects() const {
60  }
61 
62  inline void setCondMaxNumberObjects(int condMaxNumberObjectsValue) {
63  m_condMaxNumberObjects = condMaxNumberObjectsValue;
64  }
65 
67  inline bool condLastResult() const {
68  return m_condLastResult;
69  }
70 
72  inline void evaluateConditionStoreResult(const int bxEval) {
74  }
75 
77  virtual const bool evaluateCondition(const int bxEval) const = 0;
78 
81  if (m_condLastResult) {
82  return "1";
83  }
84  else {
85  return "0";
86  }
87  }
88 
90  inline CombinationsInCond const & getCombinationsInCond() const {
91  return m_combinationsInCond;
92  }
93 
94 
96  virtual void print(std::ostream& myCout) const;
97 
98  inline void setVerbosity(const int verbosity) {
100  }
101 
102 protected:
103 
106  return m_combinationsInCond;
107  }
108 
111  template<class Type1, class Type2> const bool checkThreshold(const Type1& threshold,
112  const Type2& value, bool condGEqValue) const;
113 
115  template<class Type1> const bool checkBit(const Type1& mask, const unsigned int bitNumber) const;
116 
118  template<class Type1> const bool checkRangeEta(const unsigned int bitNumber,
119  const Type1& beginR, const Type1& endR,
120  const Type1& beginVetoR, const Type1& endVetoR,
121  const unsigned int nEtaBits ) const;
122 
124  template<class Type1> const bool checkRangePhi(const unsigned int bitNumber,
125  const Type1& beginR, const Type1& endR,
126  const Type1& beginVetoR, const Type1& endVetoR ) const;
127 
128 
130  template<class Type1> const bool checkRangeDeltaEta(const unsigned int obj1Eta, const unsigned int obj2Eta,
131  const Type1& lowerR, const Type1& upperR,
132  const unsigned int nEtaBits ) const;
133 
135  template<class Type1> const bool checkRangeDeltaPhi(const unsigned int obj1Phi, const unsigned int obj2Phi,
136  const Type1& lowerR, const Type1& upperR ) const;
137 
138 
139 protected:
140 
144 
147 
150 
153 
154 };
155 
156 // define templated methods
157 
158 // check if a value is greater than a threshold or
159 // greater-or-equal depending on the value of the condGEqValue flag
160 template<class Type1, class Type2> const bool ConditionEvaluation::checkThreshold(
161  const Type1& threshold, const Type2& value, const bool condGEqValue) const {
162 
163  //if (value > 0) {
164  // LogTrace("L1GlobalTrigger") << " threshold check for condGEqValue = "
165  // << condGEqValue << "\n hex: " << std::hex << "threshold = " << threshold
166  // << " value = " << value << "\n dec: " << std::dec << "threshold = " << threshold
167  // << " value = " << value << std::endl;
168  //}
169 
170  if (condGEqValue) {
171  if (value >= (Type2) threshold) {
172 
173  //LogTrace("L1GlobalTrigger") << " condGEqValue: value >= threshold"
174  // << std::endl;
175 
176  return true;
177  }
178 
179  return false;
180 
181  }
182  else {
183 
184  if (value == (Type2) threshold) {
185 
186  //LogTrace("L1GlobalTrigger") << " condGEqValue: value = threshold"
187  // << std::endl;
188 
189  return true;
190  }
191 
192  return false;
193  }
194 }
195 
196 // check if a bit with a given number is set in a mask
197 template<class Type1> const bool ConditionEvaluation::checkBit(const Type1& mask,
198  const unsigned int bitNumber) const {
199 
200  boost::uint64_t oneBit = 1ULL;
201 
202  if (bitNumber >= (sizeof(oneBit)*8)) {
203 
204  if (m_verbosity) {
205 
206  LogTrace("L1GlobalTrigger")
207  << " checkBit " << "\n Bit number = "
208  << bitNumber << " larger than maximum allowed " << sizeof ( oneBit ) * 8
209  << std::endl;
210  }
211 
212  return false;
213  }
214 
215  oneBit <<= bitNumber;
216 
217  //LogTrace("L1GlobalTrigger") << " checkBit " << "\n mask address = " << &mask
218  // << std::dec << "\n dec: " << "mask = " << mask << " oneBit = " << oneBit
219  // << " bitNumber = " << bitNumber << std::hex << "\n hex: " << "mask = " << mask
220  // << " oneBit = " << oneBit << " bitNumber = " << bitNumber << std::dec
221  // << "\n mask & oneBit result = " << bool ( mask & oneBit ) << std::endl;
222 
223  return (mask & oneBit);
224 }
225 
226 
228  template<class Type1> const bool ConditionEvaluation::checkRangeEta(const unsigned int bitNumber,
229  const Type1& beginR, const Type1& endR,
230  const Type1& beginVetoR, const Type1& endVetoR,
231  const unsigned int nEtaBits) const {
232 
233  // set condtion to true if beginR==endR = default -1
234  if( beginR==endR && beginR==(Type1)-1 ){
235  return true;
236  }
237 
238  unsigned int diff1 = endR - beginR;
239  unsigned int diff2 = bitNumber - beginR;
240  unsigned int diff3 = endR - bitNumber;
241 
242  bool cond1 = ( (diff1>>nEtaBits) & 1 ) ? false : true;
243  bool cond2 = ( (diff2>>nEtaBits) & 1 ) ? false : true;
244  bool cond3 = ( (diff3>>nEtaBits) & 1 ) ? false : true;
245 
246  LogDebug("l1t|Global")
247  << "\n l1t::ConditionEvaluation"
248  << "\n\t bitNumber = " << bitNumber
249  << "\n\t beginR = " << beginR
250  << "\n\t endR = " << endR
251  << "\n\t beginVetoR = " << beginVetoR
252  << "\n\t endVetoR = " << endVetoR
253  << "\n\t diff1 = " << diff1
254  << "\n\t cond1 = " << cond1
255  << "\n\t diff2 = " << diff2
256  << "\n\t cond2 = " << cond2
257  << "\n\t diff3 = " << diff3
258  << "\n\t cond3 = " << cond3
259  << std::endl;
260 
261  // check if value is in range
262  // for begin <= end takes [begin, end]
263  // for begin >= end takes [begin, end] over zero angle!
264  bool passWindow = false;
265  if( cond1 && (cond2 && cond3 ) ) passWindow=true;
266  else if( !cond1 && (cond2 || cond3) ) passWindow=true;
267  else{
268  return false;
269  }
270 
271  if( passWindow ){
272  if( beginVetoR==endVetoR && beginVetoR==(Type1)-1 ) return true;
273 
274  unsigned int diffV1 = endVetoR - beginVetoR;
275  unsigned int diffV2 = bitNumber - beginVetoR;
276  unsigned int diffV3 = endVetoR - bitNumber;
277 
278  bool condV1 = ( (diffV1>>nEtaBits) & 1 ) ? false : true;
279  bool condV2 = ( (diffV2>>nEtaBits) & 1 ) ? false : true;
280  bool condV3 = ( (diffV3>>nEtaBits) & 1 ) ? false : true;
281 
282  if( condV1 && !(condV2 && condV3) ) return true;
283  else if( !condV1 && !(condV2 || condV3) ) return true;
284  else{
285  return false;
286  }
287  }
288  else{
289  LogDebug("l1t|Global") << "=====> ConditionEvaluation::checkRange: I should never be here." << std::endl;
290  return false;
291  }
292 
293  LogDebug("l1t|Global") << "=====> HELP!! I'm trapped and I cannot escape! AHHHHHH" << std::endl;
294 
295  }
296 
297 
298 
300 template<class Type1> const bool ConditionEvaluation::checkRangePhi(const unsigned int bitNumber,
301  const Type1& beginR, const Type1& endR,
302  const Type1& beginVetoR, const Type1& endVetoR ) const {
303 
304  // set condtion to true if beginR==endR = default -1
305  if( beginR==endR && beginR==(Type1)-1 ){
306  return true;
307  }
308 
309  int diff1 = endR - beginR;
310  int diff2 = bitNumber - beginR;
311  int diff3 = endR - bitNumber;
312 
313  bool cond1 = ( diff1<0 ) ? false : true;
314  bool cond2 = ( diff2<0 ) ? false : true;
315  bool cond3 = ( diff3<0 ) ? false : true;
316 
317  LogDebug("l1t|Global")
318  << "\n l1t::ConditionEvaluation"
319  << "\n\t bitNumber = " << bitNumber
320  << "\n\t beginR = " << beginR
321  << "\n\t endR = " << endR
322  << "\n\t beginVetoR = " << beginVetoR
323  << "\n\t endVetoR = " << endVetoR
324  << "\n\t diff1 = " << diff1
325  << "\n\t cond1 = " << cond1
326  << "\n\t diff2 = " << diff2
327  << "\n\t cond2 = " << cond2
328  << "\n\t diff3 = " << diff3
329  << "\n\t cond3 = " << cond3
330  << std::endl;
331 
332  // check if value is in range
333  // for begin <= end takes [begin, end]
334  // for begin >= end takes [begin, end] over zero angle!
335  bool passWindow = false;
336  if( cond1 && (cond2 && cond3 ) ) passWindow=true;
337  else if( !cond1 && (cond2 || cond3) ) passWindow=true;
338  else{
339  return false;
340  }
341 
342  if( passWindow ){
343  if( beginVetoR==endVetoR && beginVetoR==(Type1)-1 ) return true;
344 
345  int diffV1 = endVetoR - beginVetoR;
346  int diffV2 = bitNumber - beginVetoR;
347  int diffV3 = endVetoR - bitNumber;
348 
349  bool condV1 = ( diffV1<0 ) ? false : true;
350  bool condV2 = ( diffV2<0 ) ? false : true;
351  bool condV3 = ( diffV3<0 ) ? false : true;
352 
353  if( condV1 && !(condV2 && condV3) ) return true;
354  else if( !condV1 && !(condV2 || condV3) ) return true;
355  else{
356  return false;
357  }
358  }
359  else{
360  LogDebug("l1t|Global") << "=====> ConditionEvaluation::checkRange: I should never be here." << std::endl;
361  return false;
362  }
363 
364  LogDebug("l1t|Global") << "=====> HELP!! I'm trapped and I cannot escape! AHHHHHH" << std::endl;
365 
366  }
367 
368  template<class Type1> const bool ConditionEvaluation::checkRangeDeltaEta(const unsigned int obj1Eta, const unsigned int obj2Eta,
369  const Type1& lowerR, const Type1& upperR,
370  const unsigned int nEtaBits ) const {
371 
372 /* // set condtion to true if beginR==endR = default -1 */
373 /* if( beginR==endR && beginR==-1 ){ */
374 /* return true; */
375 /* } */
376 
377  unsigned int compare = obj1Eta - obj2Eta;
378  bool cond = ( (compare>>nEtaBits) & 1 ) ? false : true;
379 
380  unsigned int larger, smaller;
381  if( cond ){
382  larger = obj1Eta;
383  smaller= obj2Eta;
384  }
385  else{
386  larger = obj2Eta;
387  smaller= obj1Eta;
388  }
389 
390  unsigned int diff = ( ( larger + ((~smaller + 1) & 255) ) & 255);
391 
392  unsigned int diff1 = upperR - lowerR;
393  unsigned int diff2 = diff - lowerR;
394  unsigned int diff3 = upperR - diff;
395 
396  bool cond1 = ( (diff1>>nEtaBits) & 1 ) ? false : true;
397  bool cond2 = ( (diff2>>nEtaBits) & 1 ) ? false : true;
398  bool cond3 = ( (diff3>>nEtaBits) & 1 ) ? false : true;
399 
400  LogDebug("l1t|Global")
401  << "\n l1t::ConditionEvaluation"
402  << "\n\t obj1Eta = " << obj1Eta
403  << "\n\t obj2Eta = " << obj2Eta
404  << "\n\t lowerR = " << lowerR
405  << "\n\t upperR = " << upperR
406  << "\n\t compare = " << compare
407  << "\n\t cond = " << cond
408  << "\n\t diff = " << diff
409  << "\n\t diff1 = " << diff1
410  << "\n\t cond1 = " << cond1
411  << "\n\t diff2 = " << diff2
412  << "\n\t cond2 = " << cond2
413  << "\n\t diff3 = " << diff3
414  << "\n\t cond3 = " << cond3
415  << std::endl;
416 
417  if( cond1 && (cond2 && cond3 ) ) return true;
418  else if( !cond1 && (cond2 || cond3) ) return true;
419  else{
420  return false;
421  }
422 
423  }
424 
425 
426 
427 template<class Type1> const bool ConditionEvaluation::checkRangeDeltaPhi(const unsigned int obj1Phi, const unsigned int obj2Phi,
428  const Type1& lowerR, const Type1& upperR ) const {
429 
430  int deltaPhi = abs(obj1Phi-obj2Phi);
431  if( deltaPhi>71 ) deltaPhi = 143 - deltaPhi + 1; // Add +1 if the calculation is over 0
432 
433  int diff1 = upperR - lowerR;
434  int diff2 = deltaPhi - lowerR;
435  int diff3 = upperR - deltaPhi;
436 
437  bool cond1 = ( diff1<0 ) ? false : true;
438  bool cond2 = ( diff2<0 ) ? false : true;
439  bool cond3 = ( diff3<0 ) ? false : true;
440 
441  LogDebug("l1t|Global")
442  << "\n l1t::ConditionEvaluation"
443  << "\n\t obj1Phi = " << obj1Phi
444  << "\n\t obj2Phi = " << obj2Phi
445  << "\n\t deltaPhi = " << deltaPhi
446  << "\n\t lowerR = " << lowerR
447  << "\n\t upperR = " << upperR
448  << "\n\t diff1 = " << diff1
449  << "\n\t cond1 = " << cond1
450  << "\n\t diff2 = " << diff2
451  << "\n\t cond2 = " << cond2
452  << "\n\t diff3 = " << diff3
453  << "\n\t cond3 = " << cond3
454  << std::endl;
455 
456  // check if value is in range
457  // for begin <= end takes [begin, end]
458  // for begin >= end takes [begin, end] over zero angle!
459  if( cond1 && (cond2 && cond3 ) ) return true;
460  else if( !cond1 && (cond2 || cond3) ) return true;
461  else{
462  return false;
463  }
464 
465  }
466 
467 
468 }
469 #endif
#define LogDebug(id)
CombinationsInCond const & getCombinationsInCond() const
get all the object combinations evaluated to true in the condition
void evaluateConditionStoreResult(const int bxEval)
call evaluateCondition and save last result
const bool checkRangeDeltaPhi(const unsigned int obj1Phi, const unsigned int obj2Phi, const Type1 &lowerR, const Type1 &upperR) const
check if a value is in a given range
void setCondMaxNumberObjects(int condMaxNumberObjectsValue)
void setVerbosity(const int verbosity)
virtual void print(std::ostream &myCout) const
print condition
bool condLastResult() const
get the latest result for the condition
const bool checkRangePhi(const unsigned int bitNumber, const Type1 &beginR, const Type1 &endR, const Type1 &beginVetoR, const Type1 &endVetoR) const
check if a value is in a given range and outside of a veto range
virtual std::string getNumericExpression() const
get numeric expression
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
const bool checkBit(const Type1 &mask, const unsigned int bitNumber) const
check if a bit with a given number is set in a mask
CombinationsInCond m_combinationsInCond
store all the object combinations evaluated to true in the condition
virtual ~ConditionEvaluation()
destructor
#define LogTrace(id)
const bool checkThreshold(const Type1 &threshold, const Type2 &value, bool condGEqValue) const
int m_verbosity
verbosity level
unsigned long long uint64_t
Definition: Time.h:15
bool m_condLastResult
the last result of evaluateCondition()
virtual const bool evaluateCondition(const int bxEval) const =0
the core function to check if the condition matches
const bool checkRangeEta(const unsigned int bitNumber, const Type1 &beginR, const Type1 &endR, const Type1 &beginVetoR, const Type1 &endVetoR, const unsigned int nEtaBits) const
check if a value is in a given range and outside of a veto range
const bool checkRangeDeltaEta(const unsigned int obj1Eta, const unsigned int obj2Eta, const Type1 &lowerR, const Type1 &upperR, const unsigned int nEtaBits) const
check if a value is in a given range
volatile std::atomic< bool > shutdown_flag false
std::vector< SingleCombInCond > CombinationsInCond
all the object combinations evaluated to true in the condition
CombinationsInCond & combinationsInCond() const
get all the object combinations (to fill it...)