ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
DataQuality
GoodRunsLists
Root
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
6
#include "
GoodRunsLists/RegularFormula.h
"
7
#include "
GoodRunsLists/TMsgLogger.h
"
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
19
ClassImp
(
Root::RegularFormula
)
20
21
22
Root::RegularFormula::RegularFormula
()
23
: TFormula()
24
{
25
}
26
27
28
Root::RegularFormula::RegularFormula
(
const
char
* name,
const
char
* expression)
29
: TFormula(name,
"1"
)
30
{
31
(void)
setFormula
(expression);
32
}
33
34
Root::RegularFormula::~RegularFormula
()
35
{
36
}
37
38
39
Root::RegularFormula::RegularFormula
(
const
Root::RegularFormula
& other)
40
: TFormula(other)
41
,
m_expr
(other.
m_expr
)
42
,
m_par
(other.
m_par
)
43
{
44
}
45
46
47
Root::RegularFormula
&
48
Root::RegularFormula::operator=
(
const
Root::RegularFormula
& other)
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
63
Root::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
127
Int_t
128
Root::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
ClassImp(Root::RegularFormula) Root
Definition
RegularFormula.cxx:19
RegularFormula.h
TMsgLogger.h
GEndl
#define GEndl
Definition
TMsgLogger.h:147
Root::RegularFormula
Definition
RegularFormula.h:22
Root::RegularFormula::m_par
std::list< TString > m_par
Definition
RegularFormula.h:43
Root::RegularFormula::operator=
RegularFormula & operator=(const RegularFormula &other)
Definition
RegularFormula.cxx:48
Root::RegularFormula::parseExpression
void parseExpression(const char *expression, TString &expr)
Definition
RegularFormula.cxx:63
Root::RegularFormula::~RegularFormula
virtual ~RegularFormula()
Definition
RegularFormula.cxx:34
Root::RegularFormula::setFormula
Int_t setFormula(const char *expression)
Definition
RegularFormula.cxx:128
Root::RegularFormula::RegularFormula
RegularFormula()
Root::RegularFormula::m_expr
TString m_expr
Definition
RegularFormula.h:42
Root::TMsgLogger
Definition
TMsgLogger.h:47
count
int count(std::string s, const std::string ®x)
count how many occurances of a regx are in a string
Definition
hcg.cxx:148
Root::kINFO
@ kINFO
Definition
TMsgLogger.h:40
Generated on
for ATLAS Offline Software by
1.17.0