ATLAS Offline Software
Trigger/TrigConfiguration/TrigConfL1Data/Root/HelperFunctions.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
6 #include <iostream>
7 #include <sstream>
8 #include <cassert>
9 #include <cstdlib>
10 
15 
16 #include "boost/algorithm/string.hpp"
17 #include "boost/lexical_cast.hpp"
18 #include <algorithm>
19 #include <boost/algorithm/string/trim.hpp>
20 using namespace std;
21 using namespace TrigConf;
22 
23 
24 
25 std::vector<std::string>
26 TrigConf::split(const std::string& line, const std::string& del) {
27  std::vector<std::string> res;
28  boost::split(res, line, boost::is_any_of(del));
29  return res;
30 }
31 
32 // helper method: removing all spaces at beginning and end of a string
33 void
34 TrigConf::strip(std::string& str) {
36 }
37 
38 // helper method: replace tabs by single space
39 void TrigConf::replaceTabs(std::string& str) {
40  std::replace(str.begin(), str.end(), '\t', ' ');
41 }
42 
43 
44 // helper function: all chars to lower chars
45 void
46 TrigConf::toLower(std::string& s) {
47  for(size_t i=0; i<s.size(); i++) s[i] += (s[i]>='A'&&s[i]<='Z')?'a'-'A':0;
48 }
49 
50 
51 
52 namespace {
53 
55  parseToken(const std::string& givenlogic,
56  std::string::size_type& pos,
57  const std::vector<std::string>& conditions,
58  const std::vector<TrigConf::TriggerThreshold*>& thrs);
59 
60 
62  parseExpr(const std::string& expr,
63  const std::vector<std::string>& conditions,
64  const std::vector<TrigConf::TriggerThreshold*>& thrs)
65  {
66 
67 
68  std::string::size_type pos = 0;
69  std::string::size_type last = expr.size();
70 
71  TrigConf::TriggerItemNode * newNode = 0;
72 
73  while(pos!=last) {
74 
75  TrigConf::TriggerItemNode * nodeForToken = parseToken(expr, pos, conditions, thrs);
76 
77  // deal with the end of the expression
78  if(pos == last) {
79  if(newNode==0)
80  return nodeForToken; // single token
81  newNode->addChild(nodeForToken);
82  return newNode;
83  }
84 
85 
86  // determine the &/| type from the next character
87  TriggerItemNode::NodeType typeFromChar;
88  if(expr[pos]=='&' || expr[pos]=='|') {
89  typeFromChar = TriggerItemNode::typeFromChar(expr[pos]);
90  } else {
91  throw std::logic_error(string("Unexpected character '") + expr[pos] + "' in expression '" + expr
92  + "' at position" + std::to_string(pos) + " [b]");
93  }
94 
95 
96  if(newNode==0) {
97  // create Node upon first encounter of & or |
98  newNode = new TriggerItemNode(typeFromChar);
99  } else {
100  // check that current character matches the type of the node
101  if(newNode->type() != typeFromChar) {
102  throw std::logic_error(string("Unexpected character '") + expr[pos] + "' in expression '" + expr
103  + "' at position" + std::to_string(pos) + " [c] Expected "
104  + TriggerItemNode::typeAsString(newNode->type()) );
105  }
106  }
107 
108  newNode->addChild(nodeForToken);
109  pos++; // move beyond the &/|
110  }
111 
112  return newNode;
113  }
114 
115  std::string getSubExpr(const std::string &exp, const std::string::size_type begin) {
116  std::string::size_type pos = begin;
117  std::string::size_type last = exp.size();
118 
119  uint32_t openBrackets = 0;
120  while(pos!=last) {
121  char cc = exp[pos];
122  if(cc=='(') openBrackets++;
123  if(cc==')') openBrackets--;
124  if(openBrackets==0 && (cc=='&' or cc=='|') ) break;
125  pos++;
126  }
127  if (openBrackets>0) {
128  std::cout << "ERROR: No matching closing bracket in '" << exp << "'" << std::endl;
129  assert(0);
130  }
131  std::string se(exp, begin, pos-begin);
132  // std::cout << "Subexp of '" << exp << "'[" << begin<<"] returns " << se << std::endl;
133  return se;
134  }
135 
136 
138  parseToken(const std::string& logic,
139  std::string::size_type& pos,
140  const std::vector<std::string>& conditions,
141  const std::vector<TrigConf::TriggerThreshold*>& thrs) {
142 
143  // logic should be the definition of a node
144  //
145  // this means it needs to start with a number, '(', or '!'
146 
147  //std::cout << "parse '" << givenlogic << "'" << std::endl;
148  //for(uint32_t i =0; i<conditions.size(); i++) std::cout << conditions[i] << std::endl;
149  //std::cout << thrs.size() << ")" << std::endl;
150 
151 
152  //std::string::size_type last = logic.size();
153 
154  TriggerItemNode* thisNode(0);
155  // tokenize
156 
157  // need three tokens to build a TriggerItemNode
158  char cur = logic[pos];
159  switch(cur) {
160  case '!':
161  {
162  pos++;
164  std::string se = getSubExpr(logic,pos);
165  thisNode->addChild( parse(se,conditions,thrs) );
166  pos += se.size();
167  break;
168  }
169  case '(':
170  {
171  std::string se = getSubExpr(logic,pos);
172  pos += se.size();
173  // should have outer parentheses (...)
174  std::string senop(se, 1, se.size()-2);
175  thisNode = parse(senop,conditions,thrs);
176  break;
177  }
178  case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
179  {
180  std::string se = getSubExpr(logic,pos);
181  pos += se.size();
182  uint32_t condIdx = boost::lexical_cast<uint32_t, std::string>(se);
183  thisNode = buildObjNode(condIdx, conditions, thrs);
184  break;
185  }
186  default:
187  {
188  std::string errMsg = "Unexpected character '";
189  errMsg += logic[pos];
190  errMsg += "' in expression '" + logic + "' at position" + std::to_string(pos) + " [a]";
191  throw std::logic_error(errMsg);
192  }
193  break;
194  }
195 
196 
197  return thisNode;
198  }
199 }
200 
201 
202 
205  const vector<string>& conditions,
206  const vector<TrigConf::TriggerThreshold*>& thrs) {
207 
208  TriggerItemNode* newNode = new TriggerItemNode(TriggerItemNode::OBJ);
209 
210  try {
211  vector<string> condDef = split(conditions[condIdx-1], ",");
212 
213  if(condDef.size()==1) { // internal trigger (condDef contains internal trigger name)
214 
215  newNode->setInternalTrigger( condDef[0] );
216 
217  } else { // threshold (condDef contains multiplicity, condName, and threshold name)
218  // set multiplicity
219  newNode->setMultiplicity(boost::lexical_cast<int,std::string>(condDef[0]));
220  // find trigger threshold in list of all thresholds and set it in the TriggerItemNode
221  std::string& name = condDef[2];
222  if(thrs.size()>0) {
223  std::vector<TrigConf::TriggerThreshold*>::const_iterator thrIt = thrs.begin();
224  for(;thrIt!=thrs.end(); ++thrIt ) {
225  if((*thrIt)->name()==name) {
226  newNode->setTriggerThreshold(*thrIt);
227  break;
228  }
229  }
230  } else {
231  newNode->setThresholdName(name);
232  }
233  }
234  }
235  catch(const std::exception& e) {
236  std::cout << "Exeption caught in buildObjNode for " << conditions[condIdx-1] << ": " << e.what() << std::endl;
237  throw;
238  }
239  return newNode;
240 }
241 
242 
244 TrigConf::parse(const std::string& givenlogic,
245  const std::vector<std::string>& conditions,
246  const std::vector<TrigConf::TriggerThreshold*>& thrs) {
247 
248  // balance expression
249  std::string expr = insertParenthesis(givenlogic);
250 
251  // parse
252  TriggerItemNode* topNode = parseExpr(expr, conditions, thrs);
253 
254  // wrap in an AND
255  if( topNode->type() != TriggerItemNode::AND) {
257  newNode->addChild(topNode);
258  topNode = newNode;
259  }
260 
261  return topNode;
262 }
263 
264 
265 
266 
267 
268 
269 // pos: position of the current opening bracket
270 std::string::size_type
271 TrigConf::findClosingBracket(std::string::size_type pos,
272  const std::string& logic) {
273 
274  pos++;
275  uint32_t openBrackets = 1;
276  std::string::size_type last = logic.size();
277  while(openBrackets>0 && pos!=last) {
278  if(logic[pos]==')') openBrackets--;
279  if(logic[pos]=='(') openBrackets++;
280  pos++;
281  }
282  if (openBrackets>0) {
283  std::string errMsg = "No matching closing bracket in '";
284  errMsg += logic;
285  errMsg += "'";
286  throw std::logic_error(errMsg);
287  }
288  return pos-1;
289 }
290 
291 std::string
292 TrigConf::insertParenthesis(const std::string& givenlogic) {
293  std::string logicWithPars(givenlogic);
294  size_t last = givenlogic.size()-1;
295  bool leadingAnd(false);
296  for(size_t pos = 0;pos<last;pos++) {
297  char c = logicWithPars[pos];
298  if(c=='(') { pos=findClosingBracket(pos,givenlogic); continue; }
299  if(c=='&') leadingAnd=true;
300  if(c=='|') {
301  if(leadingAnd) {
302  logicWithPars.insert(pos,")");
303  logicWithPars.insert(0,"(");
304  }
305  break;
306  }
307  }
308  return logicWithPars;
309 }
310 
311 
312 
313 uint32_t
314 TrigConf::bin2uint(const std::string& binary) {
315  uint32_t value(0);
316  for(char c: binary) {
317  value <<= 1;
318  if(c=='1') value += 1;
319  }
320  return value;
321 }
322 
323 
324 std::string
326  stringstream ss;
327  for(uint32_t mask = 1 << (width-1); mask>0; mask>>=1)
328  ss << ( (value & mask) == 0 ? '0' : '1' );
329 
330  return ss.str();
331 }
TrigConf::TriggerItemNode::setInternalTrigger
void setInternalTrigger(L1DataDef::TriggerType x, unsigned int thresholdNumber)
Definition: TriggerItemNode.cxx:87
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
replace
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition: hcg.cxx:307
TrigConf::findClosingBracket
std::string::size_type findClosingBracket(std::string::size_type pos, const std::string &logic)
Definition: Trigger/TrigConfiguration/TrigConfL1Data/Root/HelperFunctions.cxx:271
PowhegControl_ttHplus_NLO.ss
ss
Definition: PowhegControl_ttHplus_NLO.py:83
TrigConf::parse
std::vector< std::string > parse(std::string names)
Definition: TrigConfHLTData/Root/HLTChain.cxx:117
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
beamspotman.cur
def cur
Definition: beamspotman.py:667
TrigConf::TriggerItemNode::type
NodeType type() const
Definition: TriggerItemNode.h:50
parse
std::map< std::string, std::string > parse(const std::string &list)
Definition: egammaLayerRecalibTool.cxx:1113
TrigConf::TriggerItemNode::setTriggerThreshold
void setTriggerThreshold(TriggerThreshold *thr)
Definition: TriggerItemNode.cxx:74
PlotCalibFromCool.begin
begin
Definition: PlotCalibFromCool.py:94
Root::AND
@ AND
Definition: TGRLCollection.h:32
athena.value
value
Definition: athena.py:124
TrigConf::TriggerItemNode::addChild
void addChild(TriggerItemNode *node)
Definition: TriggerItemNode.cxx:114
TrigConf::bin2uint
uint32_t bin2uint(const std::string &binary)
Definition: Trigger/TrigConfiguration/TrigConfL1Data/Root/HelperFunctions.cxx:314
TrigConf::TriggerItemNode
Definition: TriggerItemNode.h:22
drawFromPickle.exp
exp
Definition: drawFromPickle.py:36
keylayer_zslicemap.se
se
Definition: keylayer_zslicemap.py:194
TrigConf::toLower
void toLower(std::string &)
Definition: Trigger/TrigConfiguration/TrigConfL1Data/Root/HelperFunctions.cxx:46
TrigConf::replaceTabs
void replaceTabs(std::string &str)
Definition: Trigger/TrigConfiguration/TrigConfL1Data/Root/HelperFunctions.cxx:39
dq_defect_bulk_create_defects.line
line
Definition: dq_defect_bulk_create_defects.py:27
HelperFunctions.h
python.utils.AtlRunQueryLookup.mask
string mask
Definition: AtlRunQueryLookup.py:459
TrigConf::buildObjNode
TrigConf::TriggerItemNode * buildObjNode(uint32_t condIdx, const std::vector< std::string > &conditions, const std::vector< TrigConf::TriggerThreshold * > &thrs)
Definition: Trigger/TrigConfiguration/TrigConfL1Data/Root/HelperFunctions.cxx:204
TrigConf
Forward iterator to traverse the main components of the trigger configuration.
Definition: Config.h:22
xAOD::uint16_t
setWord1 uint16_t
Definition: eFexEMRoI_v1.cxx:93
lumiFormat.i
int i
Definition: lumiFormat.py:85
TrigConf::TriggerItemNode::NodeType
NodeType
Definition: TriggerItemNode.h:24
calibdata.exception
exception
Definition: calibdata.py:495
res
std::pair< std::vector< unsigned int >, bool > res
Definition: JetGroupProductTest.cxx:11
TriggerThreshold.h
TrigConf::uint2bin
std::string uint2bin(uint32_t uinteger, uint16_t width)
Definition: Trigger/TrigConfiguration/TrigConfL1Data/Root/HelperFunctions.cxx:325
TrigConf::name
Definition: HLTChainList.h:35
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
TrigConf::TriggerItemNode::setMultiplicity
void setMultiplicity(int mult)
Definition: TriggerItemNode.h:44
TrigConf::strip
void strip(std::string &str)
Definition: Trigger/TrigConfiguration/TrigConfL1Data/Root/HelperFunctions.cxx:34
python.LumiBlobConversion.pos
pos
Definition: LumiBlobConversion.py:16
L1DataDef.h
TrigConf::split
std::vector< std::string > split(const std::string &line, const std::string &del=" ")
Definition: Trigger/TrigConfiguration/TrigConfL1Data/Root/HelperFunctions.cxx:26
Base_Fragment.width
width
Definition: Sherpa_i/share/common/Base_Fragment.py:59
GRLStrUtil::trim
void trim(std::string &input)
Definition: StrUtil.cxx:12
python.SystemOfUnits.s
float s
Definition: SystemOfUnits.py:147
str
Definition: BTagTrackIpAccessor.cxx:11
TrigConf::insertParenthesis
std::string insertParenthesis(const std::string &givenlogic)
Definition: Trigger/TrigConfiguration/TrigConfL1Data/Root/HelperFunctions.cxx:292
TrigConf::TriggerItemNode::setThresholdName
void setThresholdName(const std::string &thrname)
Definition: TriggerItemNode.h:42
python.compressB64.c
def c
Definition: compressB64.py:93
Trk::split
@ split
Definition: LayerMaterialProperties.h:38
TrigConf::TriggerItemNode::NOT
@ NOT
Definition: TriggerItemNode.h:24
python.handimod.cc
int cc
Definition: handimod.py:522
TriggerItemNode.h