]>
Commit | Line | Data |
---|---|---|
baca856a | 1 | #include <Riostream.h> |
2 | #include <TString.h> | |
3 | #include <TObjString.h> | |
4 | #include <TObjArray.h> | |
5 | ||
6 | #include "AliLog.h" | |
7 | ||
8 | #include "AliRsnCut.h" | |
9 | #include "AliRsnCutSet.h" | |
10 | #include "AliRsnExpression.h" | |
11 | ||
aec0ec32 | 12 | ClassImp(AliRsnExpression) |
baca856a | 13 | |
14 | AliRsnCutSet *AliRsnExpression::sCutSet = 0; | |
15 | ||
16 | //______________________________________________________________________________ | |
aec0ec32 | 17 | AliRsnExpression::AliRsnExpression(TString exp) : |
baca856a | 18 | TObject(), |
aec0ec32 | 19 | fVname(""), |
20 | fArg1(0x0), | |
21 | fArg2(0x0), | |
22 | fOperator(0) | |
baca856a | 23 | { |
24 | // Default constructor | |
aec0ec32 | 25 | TObjArray* tokens = Tokenize(exp); |
baca856a | 26 | |
27 | Int_t i = -1; | |
aec0ec32 | 28 | AliRsnExpression* e = Expression(*tokens, i); |
baca856a | 29 | // Copy !!! |
30 | fArg1 = e->fArg1; e->fArg1 = 0; | |
31 | fArg2 = e->fArg2; e->fArg2 = 0; | |
32 | fOperator = e->fOperator; | |
33 | fVname = e->fVname; | |
34 | delete e; | |
35 | delete tokens; | |
36 | } | |
37 | ||
38 | //______________________________________________________________________________ | |
39 | AliRsnExpression::~AliRsnExpression() | |
40 | { | |
aec0ec32 | 41 | if (fArg1) delete fArg1; |
42 | if (fArg2) delete fArg2; | |
baca856a | 43 | } |
44 | ||
aec0ec32 | 45 | AliRsnExpression::AliRsnExpression(const AliRsnExpression & exp) : TObject(exp), |
46 | fVname(exp.fVname), | |
47 | fArg1(exp.fArg1), | |
48 | fArg2(exp.fArg2), | |
49 | fOperator(exp.fOperator) | |
baca856a | 50 | {} |
51 | ||
52 | //______________________________________________________________________________ | |
aec0ec32 | 53 | AliRsnExpression& AliRsnExpression::operator= (const AliRsnExpression& e) |
baca856a | 54 | { |
55 | // AliRsnExpression assignment operator. | |
56 | ||
aec0ec32 | 57 | if (this != &e) |
baca856a | 58 | { |
aec0ec32 | 59 | TObject::operator= (e); |
baca856a | 60 | fArg1 = e.fArg1; |
61 | fArg2 = e.fArg2; | |
62 | fOperator = e.fOperator; | |
63 | fVname = e.fVname; | |
64 | } | |
65 | return *this; | |
66 | } | |
67 | ||
68 | //______________________________________________________________________________ | |
aec0ec32 | 69 | AliRsnExpression::AliRsnExpression(int op, AliRsnExpression* a, AliRsnExpression* b) : |
baca856a | 70 | TObject(), |
aec0ec32 | 71 | fVname(""), |
72 | fArg1(a), | |
73 | fArg2(b), | |
74 | fOperator(op) | |
baca856a | 75 | { |
76 | // Create a new expression | |
77 | } | |
78 | ||
79 | //______________________________________________________________________________ | |
aec0ec32 | 80 | AliRsnExpression::AliRsnExpression(int op, AliRsnExpression* a) : |
baca856a | 81 | TObject(), |
aec0ec32 | 82 | fVname(""), |
83 | fArg1(0), | |
84 | fArg2(a), | |
85 | fOperator(op) | |
baca856a | 86 | { |
87 | // Create a unary expression. | |
88 | } | |
89 | ||
90 | //______________________________________________________________________________ | |
aec0ec32 | 91 | Bool_t AliRsnExpression::Value(TObjArray &vars) |
baca856a | 92 | { |
93 | // Evaluate the expression | |
aec0ec32 | 94 | if (fArg2 == 0 && fVname.IsNull()) |
baca856a | 95 | { |
aec0ec32 | 96 | AliError("Expression undefined."); |
baca856a | 97 | return kFALSE; |
98 | } | |
99 | ||
100 | // AliDebug(AliLog::kDebug,Form("fOperator %d",fOperator)); | |
8ecf98c4 | 101 | |
aec0ec32 | 102 | switch (fOperator) |
baca856a | 103 | { |
104 | ||
105 | case kOpOR : | |
aec0ec32 | 106 | return fArg1->Value(vars) || fArg2->Value(vars); |
baca856a | 107 | |
108 | case kOpAND : | |
aec0ec32 | 109 | return fArg1->Value(vars) && fArg2->Value(vars); |
baca856a | 110 | |
111 | case kOpNOT : | |
aec0ec32 | 112 | return !(fArg2->Value(vars)); |
baca856a | 113 | |
114 | case 0 : | |
115 | { | |
116 | // Int_t indexx = sCutSet->GetIndexByCutName ( fVname.Data() ); | |
117 | AliDebug(AliLog::kDebug,Form("Vname %s",fVname.Data())); | |
118 | // return sCutSet->GetBoolValue ( indexx ); | |
aec0ec32 | 119 | return sCutSet->GetBoolValue(fVname.Atoi()); |
baca856a | 120 | } |
121 | ||
122 | default: | |
aec0ec32 | 123 | AliError("Illegal operator in expression!"); |
baca856a | 124 | |
125 | } | |
126 | return kFALSE; | |
127 | } | |
128 | ||
129 | ||
130 | //______________________________________________________________________________ | |
131 | TString AliRsnExpression::Unparse() const | |
132 | { | |
133 | // Unparse the expression | |
134 | ||
135 | TString opVals[4] = { "", "&", "|","!" }; | |
aec0ec32 | 136 | if (fArg2 == 0 && fVname.IsNull()) |
baca856a | 137 | { |
aec0ec32 | 138 | AliError("Expression undefined."); |
baca856a | 139 | return "Error"; |
140 | } | |
141 | ||
aec0ec32 | 142 | if (fArg2 == 0 && !fVname.IsNull()) return fVname; |
baca856a | 143 | |
aec0ec32 | 144 | if (fArg1 == 0 && fArg2) |
baca856a | 145 | { |
146 | return opVals[fOperator]+fArg2->Unparse(); | |
147 | } | |
148 | return "("+fArg1->Unparse() +" "+opVals[fOperator]+" "+fArg2->Unparse() +")"; | |
149 | } | |
150 | ||
151 | //______________________________________________________________________________ | |
aec0ec32 | 152 | TObjArray* AliRsnExpression::Tokenize(TString str) const |
baca856a | 153 | { |
154 | // tokenize the expression | |
155 | ||
156 | // Remove spaces | |
157 | TString str1; | |
aec0ec32 | 158 | for (Int_t i=0; i<str.Length(); i++) |
baca856a | 159 | { |
aec0ec32 | 160 | if (str[i] == ' ') continue; |
161 | str1.Append(str[i]); | |
baca856a | 162 | } |
163 | // get variable tokens | |
aec0ec32 | 164 | TObjArray* valtok = str1.Tokenize("!&|()"); |
baca856a | 165 | // put all variables together |
166 | Int_t nvt = valtok->GetEntriesFast(); | |
167 | TString sumval; | |
aec0ec32 | 168 | for (Int_t i=0; i<nvt; i++) |
baca856a | 169 | { |
aec0ec32 | 170 | TObjString* val = (TObjString*) valtok->At(i); |
171 | sumval.Append(val->String()); | |
baca856a | 172 | } |
173 | // get the operator tokens | |
aec0ec32 | 174 | TObjArray* optok = str1.Tokenize(sumval.Data()); |
baca856a | 175 | // put all operator in one string |
176 | TString operators; | |
177 | Int_t nopt = optok->GetEntriesFast(); | |
aec0ec32 | 178 | for (Int_t i=0; i<nopt; i++) |
baca856a | 179 | { |
aec0ec32 | 180 | TObjString* val1 = (TObjString*) optok->At(i); |
181 | operators.Append(val1->String()); | |
baca856a | 182 | } |
183 | // add more room to be safe | |
aec0ec32 | 184 | TObjString* blank = new TObjString(" "); |
185 | operators.Append(" "); | |
186 | valtok->AddLast(blank); | |
baca856a | 187 | // Now put var. and oper. together |
aec0ec32 | 188 | TObjArray* tokens = new TObjArray(valtok->GetEntriesFast() + operators.Length()); |
baca856a | 189 | int io = 0,iv = 0; |
190 | int index = 0; | |
aec0ec32 | 191 | while (1) |
baca856a | 192 | { |
193 | TString so = operators[io]; | |
aec0ec32 | 194 | int indexO = str1.Index(so, index); |
195 | TString val2 = ((TObjString*) valtok->At(iv))->String(); | |
196 | int indexV = str1.Index(val2, index); | |
197 | if ((indexO < indexV || indexV < 0) && indexO >=0) | |
baca856a | 198 | { |
aec0ec32 | 199 | tokens->AddLast(new TObjString(so)); |
baca856a | 200 | index += so.Length(); |
201 | io++; | |
202 | } | |
aec0ec32 | 203 | if ((indexV < indexO || indexO < 0) && indexV >=0) |
baca856a | 204 | { |
aec0ec32 | 205 | tokens->AddLast(new TObjString(val2)); |
baca856a | 206 | index += val2.Length(); |
207 | iv++; | |
208 | } | |
aec0ec32 | 209 | if (index >= str1.Length()) break; |
baca856a | 210 | } |
211 | ||
212 | // // Debug -> Print the tokens | |
213 | // Int_t nt = tokens->GetEntriesFast(); | |
214 | // for ( Int_t i=0; i<nt; i++ ) | |
215 | // { | |
216 | // TObjString* val3 = ( TObjString* ) tokens->At ( i ); | |
217 | // AliInfo ( Form ( "%d %s",i,val3->String().Data() ) ); | |
218 | // } | |
8ecf98c4 | 219 | // |
220 | // | |
baca856a | 221 | delete valtok; |
222 | delete optok; | |
223 | ||
224 | return tokens; | |
225 | } | |
226 | ||
227 | ||
228 | //______________________________________________________________________________ | |
aec0ec32 | 229 | AliRsnExpression* AliRsnExpression::Element(TObjArray &st, Int_t &i) |
baca856a | 230 | { |
231 | // create an element | |
232 | ||
233 | AliRsnExpression* result = 0; | |
234 | ||
235 | Int_t nt = st.GetEntriesFast(); | |
236 | TString token = "@"; | |
237 | TObjString* valt; | |
238 | // next token | |
aec0ec32 | 239 | if (i < nt-1) |
baca856a | 240 | { |
241 | i++; | |
aec0ec32 | 242 | valt = (TObjString*) st.At(i); |
baca856a | 243 | token = valt->String(); |
244 | } | |
245 | // token type | |
aec0ec32 | 246 | char ttok = (token[0]!='|' && token[0]!='&' && |
247 | token[0]!='!' && token[0]!='('&& token[0]!=')') ? 'w' : token[0]; | |
248 | switch (ttok) | |
baca856a | 249 | { |
250 | case 'w' : | |
251 | { | |
aec0ec32 | 252 | result = new AliRsnVariableExpression(token); |
baca856a | 253 | break; |
254 | } | |
255 | case '(' : | |
aec0ec32 | 256 | result = Expression(st, i); |
baca856a | 257 | // next token |
aec0ec32 | 258 | if (i < nt-1) |
baca856a | 259 | { |
260 | i++; | |
aec0ec32 | 261 | valt = (TObjString*) st.At(i); |
baca856a | 262 | token = valt->String(); |
263 | } | |
aec0ec32 | 264 | if (token[0] != ')') |
baca856a | 265 | { |
266 | // i--; // push back | |
aec0ec32 | 267 | AliErrorGeneral("AliRsnExpression::Element", "Mismatched parenthesis."); |
baca856a | 268 | delete result; |
269 | result = new AliRsnExpression; | |
270 | } | |
271 | break; | |
272 | default: | |
273 | i--; // push back | |
aec0ec32 | 274 | AliErrorGeneral("AliRsnExpression::Element", Form("Unexpected symbol on input. %s", token.Data())); |
275 | if (result) delete result; | |
baca856a | 276 | result = new AliRsnExpression; |
277 | } | |
278 | return result; | |
279 | } | |
280 | ||
281 | //______________________________________________________________________________ | |
aec0ec32 | 282 | AliRsnExpression* AliRsnExpression::Primary(TObjArray &st, Int_t &i) |
baca856a | 283 | { |
284 | // create a primary | |
285 | ||
286 | Int_t nt = st.GetEntriesFast(); | |
287 | TString token = "@"; | |
288 | TObjString* valt; | |
289 | // next token | |
aec0ec32 | 290 | if (i < nt-1) |
baca856a | 291 | { |
292 | i++; | |
aec0ec32 | 293 | valt = (TObjString*) st.At(i); |
baca856a | 294 | token = valt->String(); |
295 | } | |
296 | ||
aec0ec32 | 297 | switch (token[0]) |
baca856a | 298 | { |
299 | case '!' : | |
aec0ec32 | 300 | return new AliRsnExpression(kOpNOT, Primary(st, i)); |
baca856a | 301 | default: |
302 | i--; // push back | |
aec0ec32 | 303 | return Element(st, i); |
baca856a | 304 | } |
305 | } | |
306 | ||
307 | //______________________________________________________________________________ | |
aec0ec32 | 308 | AliRsnExpression* AliRsnExpression::Expression(TObjArray &st,Int_t &i) |
baca856a | 309 | { |
310 | // create an expression | |
311 | ||
312 | AliRsnExpression* result = 0; | |
313 | Bool_t done = kFALSE; | |
314 | TString token; | |
315 | TObjString* valt; | |
316 | ||
317 | static int stack = 0; | |
318 | stack++; | |
319 | Int_t nt = st.GetEntriesFast(); | |
320 | ||
aec0ec32 | 321 | result = Primary(st, i); |
baca856a | 322 | // cout <<"i "<<i<< "Primary " << result->Unparse() << endl; |
aec0ec32 | 323 | while (! done) |
baca856a | 324 | { |
325 | // next token | |
aec0ec32 | 326 | if (i < nt-1) i++; |
baca856a | 327 | else break; |
aec0ec32 | 328 | valt = (TObjString*) st.At(i); |
baca856a | 329 | token = valt->String(); |
aec0ec32 | 330 | switch (token[0]) |
baca856a | 331 | { |
332 | case '&' : | |
aec0ec32 | 333 | result = new AliRsnExpression(kOpAND, result, Primary(st, i)); |
baca856a | 334 | // cout <<"i "<<i<< " Expression AND " << result->Unparse() << endl; |
335 | break; | |
336 | case '|' : | |
aec0ec32 | 337 | result = new AliRsnExpression(kOpOR, result, Primary(st, i)); |
baca856a | 338 | // cout <<"i "<<i<< " Expression OR " << result->Unparse() << endl; |
339 | break; | |
340 | default: | |
341 | done = kTRUE; | |
342 | i--; // push back | |
343 | break; | |
344 | } | |
345 | } | |
346 | stack--; | |
aec0ec32 | 347 | if (stack == 0 && !token.IsNull() && token[0] == ')') |
baca856a | 348 | { |
aec0ec32 | 349 | AliErrorGeneral("AliRsnExpression::Expression", "To many closing parenthesis."); |
baca856a | 350 | delete result; |
351 | result = new AliRsnExpression; | |
352 | } | |
353 | else | |
aec0ec32 | 354 | if (stack == 0 && i< nt-1) |
baca856a | 355 | { |
aec0ec32 | 356 | AliErrorGeneral("AliRsnExpression::Expression", Form("Unexpected symbol on input. %s", token.Data())); |
baca856a | 357 | delete result; |
358 | result = new AliRsnExpression; | |
359 | } | |
360 | return result; | |
361 | } | |
362 | ||
363 | //////////////////////////////////////////////////////////////////////////////// | |
364 | ||
aec0ec32 | 365 | ClassImp(AliRsnVariableExpression) |
baca856a | 366 | |
367 | //______________________________________________________________________________ | |
aec0ec32 | 368 | Bool_t AliRsnVariableExpression::Value(TObjArray& /*pgm*/) |
baca856a | 369 | { |
8ecf98c4 | 370 | |
baca856a | 371 | // Int_t indexx = sCutSet->GetIndexByCutName ( fVname.Data() ); |
372 | AliDebug(AliLog::kDebug,Form("Vname %s",fVname.Data())); | |
373 | // return sCutSet->GetBoolValue ( indexx ); | |
374 | ||
aec0ec32 | 375 | return sCutSet->GetBoolValue(fVname.Atoi()); |
baca856a | 376 | } |
377 |