ATLAS Offline Software
RegularFormula.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
8 
9 #include "TRegexp.h"
10 #include "TString.h"
11 #include "TObjArray.h"
12 #include "TObjString.h"
13 #include "TROOT.h"
14 
15 #include <iostream>
16 #include <stdlib.h>
17 #include <algorithm>
18 
20 
21 
23  : TFormula()
24 {
25 }
26 
27 
29  : TFormula(name,"1")
30 {
31  (void) setFormula(expression);
32 }
33 
35 {
36 }
37 
38 
40  : TFormula(other)
41  , m_expr(other.m_expr)
42  , m_par(other.m_par)
43 {
44 }
45 
46 
49 {
50  if (&other==this) {
51  return *this;
52  }
53 
54  TFormula::operator=(other) ;
55  m_expr = other.m_expr;
56  m_par = other.m_par;
57 
58  return *this ;
59 }
60 
61 
62 void
64 {
65  expr = expression;
66  TString parStr(expression);
67 
68  TRegexp specialChars("[+-/*%&=!><()|]+");
69  int lastIdx = -1;
70  do {
71  //search for special characters and determine if they're escaped
72  lastIdx = parStr.Index(specialChars, lastIdx+1);
73  if (lastIdx >= 0) {
74  bool needsReplacement = false;
75  if (lastIdx > 0) {
76  //if a match is found and it's not the first character of the string
77  char isEscape = parStr[lastIdx-1];
78  if (isEscape == '\\') {
79  //this is an escpaed special character, keep it but remove escape
80  // both on parStr as well as in expr
81  parStr = parStr.Remove(lastIdx-1, 1);
82  expr = expr.Remove(lastIdx-1, 1);
83  needsReplacement = false;
84  lastIdx--; //we removed one character from the string
85  } else {
86  needsReplacement = true;
87  }
88  } else {
89  //matched the first character
90  needsReplacement = true;
91  }
92 
93  if (needsReplacement) {
94  //not escaped, just replace with a space to allow parameters parsing below
95  parStr = parStr.Replace(lastIdx, 1, " ");
96  }
97  }
98  } while (lastIdx >= 0);
99 
100  TMsgLogger mylogger( "RegularFormula" );
101  mylogger << kINFO << "Now parsing regular expression : " << expression << GEndl;
102  mylogger << kINFO << "Please be aware that Cling errors are expected and aren't inherently a problem" << GEndl;
103 
104  TFormula analyzer("analyzer","1");
105  TObjArray* parArr = parStr.Tokenize(" ");
106  for (int count(0), i(0); i<parArr->GetEntries(); ++i) {
107  TString myPar = ((TObjString*)parArr->At(i))->GetString();
108  if ( 0==analyzer.Compile(myPar.Data()) ||
109  0==analyzer.Compile(Form("%s(1)",myPar.Data())) ) {
110  continue;
111  } else {
112  std::list<TString>::iterator itrF = std::find(m_par.begin(),m_par.end(),myPar);
113  if (itrF==m_par.end()) {
114  expr = expr.ReplaceAll(myPar,Form("[%d]",count));
115  m_par.push_back(myPar);
116  count++;
117  }
118  }
119  }
120  delete parArr;
121 
122  mylogger << kINFO << "Number of interpreted input parameters : " << m_par.size() << GEndl;
123  mylogger << kINFO << "Parsed regular expression : " << expr << GEndl;
124 }
125 
126 
127 Int_t
129 {
130  m_par.clear();
131  this->parseExpression(expression,m_expr);
132  return this->Compile(m_expr.Data());
133 }
134 
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
RegularFormula.h
ClassImp
ClassImp(Root::RegularFormula) Root
Definition: RegularFormula.cxx:19
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
Root::RegularFormula::parseExpression
void parseExpression(const char *expression, TString &expr)
Definition: RegularFormula.cxx:63
XMLtoHeader.count
count
Definition: XMLtoHeader.py:85
GEndl
#define GEndl
Definition: TMsgLogger.h:151
Root::RegularFormula::RegularFormula
RegularFormula()
Root::RegularFormula::~RegularFormula
virtual ~RegularFormula()
Definition: RegularFormula.cxx:34
HION12.expression
string expression
Definition: HION12.py:56
lumiFormat.i
int i
Definition: lumiFormat.py:92
Root::kINFO
@ kINFO
Definition: TMsgLogger.h:50
Root::RegularFormula::setFormula
Int_t setFormula(const char *expression)
Definition: RegularFormula.cxx:128
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
InDetDD::other
@ other
Definition: InDetDD_Defs.h:16
TMsgLogger.h
Root::RegularFormula::operator=
RegularFormula & operator=(const RegularFormula &other)
Definition: RegularFormula.cxx:48
Root::TMsgLogger
Definition: TMsgLogger.h:52
Root::RegularFormula
Definition: RegularFormula.h:27