ATLAS Offline Software
Loading...
Searching...
No Matches
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
26
27
28Root::RegularFormula::RegularFormula(const char* name,const char* expression)
29 : TFormula(name,"1")
30{
31 (void) setFormula(expression);
32}
33
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
62void
63Root::RegularFormula::parseExpression(const char* expression, TString& expr)
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
127Int_t
128Root::RegularFormula::setFormula(const char *expression)
129{
130 m_par.clear();
131 this->parseExpression(expression,m_expr);
132 return this->Compile(m_expr.Data());
133}
134
ClassImp(Root::RegularFormula) Root
#define GEndl
Definition TMsgLogger.h:147
std::list< TString > m_par
RegularFormula & operator=(const RegularFormula &other)
void parseExpression(const char *expression, TString &expr)
Int_t setFormula(const char *expression)
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146
@ kINFO
Definition TMsgLogger.h:40