-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilter.cc
623 lines (523 loc) · 18.9 KB
/
Filter.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#include <iostream>
#include <cstdlib>
#include "Filter.h"
#include "Config.h"
#include "Event.h"
#include "GlobalParameters.h"
#include "Selection.h"
#include "Variable.h"
std::vector<Filter*> Filter::garbage_;
TString Filter::offset_ = " ";
// ---------------------------------------------------------------
const Filter* Filter::create(const TString &expr, const std::vector<TString> &dataSetLabels, unsigned int lineNum, bool firstIteration, const TString &label) {
if( GlobalParameters::debug() ) {
if( firstIteration ) {
std::cout << "\nBuilding selection '" << label << "'" << std::endl;
std::cout << "Filter::create(): '" << expr << "'" << std::endl;
} else {
std::cout << "Filter::create(): '" << expr << "'" << std::endl;
}
}
// Syntax checks
if( firstIteration ) {
checkForDanglingOperators(expr,lineNum);
checkForMismatchingParentheses(expr,lineNum);
checkForInvalidBooleanOperators(expr,lineNum);
}
const Filter* filter = 0;
// If this selection is only to be applied to specific datasets
// create dataset filter
if( firstIteration && !dataSetLabels.empty() ) {
filter = new FilterDataSet(create(expr,dataSetLabels,lineNum,false,label),dataSetLabels);
} else {
TString cfg = cleanExpression(expr);
if( GlobalParameters::debug() ) std::cout << " Cleaned expr: '" << cfg << "'" << std::endl;
// Check if whole expression is negated
if( isNegated(cfg) ) {
if( GlobalParameters::debug() ) std::cout << " Negated expression: remember NOT" << std::endl;
filter = new FilterNOT(create(cfg,dataSetLabels,lineNum,false,""));
} else {
// Possibly, split expression
TString expr1 = "";
TString expr2 = "";
TString op = "";
if( isSplit(cfg,expr1,expr2,op) ) {
const Filter* filter1 = create(expr1,dataSetLabels,lineNum,false,"");
const Filter* filter2 = create(expr2,dataSetLabels,lineNum,false,"");
if( op == "AND" ) {
filter = new FilterAND(filter1,filter2);
} else if( op == "OR" ) {
filter = new FilterOR(filter1,filter2);
}
} else {
// Loop over previously defined selections
// and check whether cfg corresponds to one of their labels
for(SelectionIt itS = Selection::begin(); itS != Selection::end(); ++itS) {
if( (*itS)->uid() == cfg ) { // A selection with this name exists; reuse it
if( GlobalParameters::debug() ) std::cout << " Reusing selection '" << cfg << "'" << std::endl;
filter = (*itS)->filter();
break;
}
}
if( filter == 0 ) {
filter = Cut::create(cfg,lineNum);
}
}
}
}
return filter;
}
// ---------------------------------------------------------------
TString Filter::cleanExpression(const TString &expr) {
TString cfg = expr;
// Remove all spaces
cfg.ReplaceAll(" ","");
// Remove parentheses enclosing the whole expression
bool checkAgain = true;
while( checkAgain ) {
unsigned int nOpenBrackets = 0;
unsigned int pos = 0;
if( cfg(pos) == '(' ) {
++nOpenBrackets;
++pos;
for(; pos < cfg.Length()-1; ++pos) {
if( cfg(pos) == '(' ) {
++nOpenBrackets;
} else if( cfg(pos) == ')' ) {
--nOpenBrackets;
}
if( nOpenBrackets == 0 ) {
checkAgain = false;
break;
}
}
if( nOpenBrackets > 0 ) {
cfg = cfg(1,cfg.Length()-2);
}
} else {
checkAgain = false;
}
}
return cfg;
}
// ---------------------------------------------------------------
void Filter::checkForDanglingOperators(const TString &expr, unsigned int lineNum) {
bool syntaxError = false;
if( expr.BeginsWith("&") || expr.BeginsWith("|") || expr.BeginsWith(">") || expr.BeginsWith("<") || expr.BeginsWith("=") || expr.BeginsWith("+") || expr.BeginsWith("-") ) syntaxError = true;
if( expr.EndsWith("&") || expr.EndsWith("|") || expr.EndsWith(">") || expr.EndsWith("<") || expr.EndsWith("=") || expr.EndsWith("+") || expr.EndsWith("-") ) syntaxError = true;
if( syntaxError ) {
std::cerr << "\n\nERROR: Wrong syntax when specifying cut in line " << lineNum << std::endl;
std::cerr << "Invalid cut expression '" << expr << "'" << std::endl;
std::cerr << "Dangling operator" << std::endl;
exit(-1);
}
}
// ---------------------------------------------------------------
void Filter::checkForMismatchingParentheses(const TString &expr, unsigned int lineNum) {
if( expr.Contains("(") || expr.Contains(")") ) {
unsigned int nOpenBrackets = 0;
bool syntaxError = false;
for(unsigned int pos = 0; pos < expr.Length(); ++pos) {
if( expr(pos) == '(' ) {
++nOpenBrackets;
} else if( expr(pos) == ')' ) {
if( nOpenBrackets > 0 ) {
--nOpenBrackets;
} else {
syntaxError = true;
break;
}
}
}
if( nOpenBrackets > 0 || syntaxError ) {
std::cerr << "\n\nERROR: Wrong syntax when specifying cut in line " << lineNum << std::endl;
std::cerr << "Mismatching parentheses in cut expression '" << expr << "'" << std::endl;
exit(-1);
}
}
}
// ---------------------------------------------------------------
void Filter::checkForInvalidBooleanOperators(const TString &expr, unsigned int lineNum) {
bool syntaxError = false;
for(unsigned int pos = 0; pos < expr.Length(); ++pos) {
if( expr(pos) == '&' ) {
if( expr(pos+1) == '&' ) {
++pos;
} else {
syntaxError = true;
break;
}
}
if( expr(pos) == '|' ) {
if( expr(pos+1) == '|' ) {
++pos;
} else {
syntaxError = true;
break;
}
}
}
if( syntaxError ) {
std::cerr << "\n\nERROR: Wrong syntax when specifying cut in line " << lineNum << std::endl;
std::cerr << "Invalid cut expression '" << expr << "'" << std::endl;
std::cerr << "Contains incomplete boolean operator '&' or '|'" << std::endl;
exit(-1);
}
}
// ---------------------------------------------------------------
bool Filter::isNegated(TString &expr) {
bool isNOT = false;
if( expr(0) == '!' ) {
if( expr.Contains("&&") || expr.Contains("||") ) { // It's not a simple cut; check if NOT refers to the whole, composite expression, i.e. if expr is !(..)
if( expr(1) == '(' && expr(expr.Length()-1) == ')' ) {
unsigned int nOpenBrackets = 1;
bool bracketsClosedBeforeEndOfExpr = false;
for(unsigned int pos = 2; pos < expr.Length()-1; ++pos) {
if( expr(pos) == '(' ) {
++nOpenBrackets;
} else if( expr(pos) == ')' ) {
--nOpenBrackets;
}
if( nOpenBrackets == 0 ) { // !( does not span the whole expression
bracketsClosedBeforeEndOfExpr = true;
break;
}
}
if( !bracketsClosedBeforeEndOfExpr ) {
isNOT = true;
expr = expr(1,expr.Length()-1); // Remove '!'
expr = cleanExpression(expr); // Expression is enclosed in parentheses
}
}
} else { // It's a sinlge cut, and it's negated
isNOT = true;
expr = expr(1,expr.Length()-1); // Remove '!'
expr = cleanExpression(expr); // Expression might possibly be enclosed in parentheses
}
}
return isNOT;
}
// ---------------------------------------------------------------
bool Filter::isSplit(const TString &expr, TString &expr1, TString &expr2, TString &op) {
bool split = false;
unsigned int nOpenBrackets = 0;
for(unsigned int pos = 0; pos < expr.Length(); ++pos) {
// Check for expressions in brackets
if( expr(pos) == '(' ) {
++nOpenBrackets;
} else if( expr(pos) == ')' ) {
if( nOpenBrackets > 0 ) {
--nOpenBrackets;
}
}
// If no bracketed expression, check for boolean operators
if( nOpenBrackets == 0 ) {
if( expr(pos) == '&' ) { // Case: two expressions, AND
if( expr(pos+1) == '&' ) {
split = true;
op = "AND";
expr1 = expr(0,pos);
pos = pos+2;
expr2 = expr(pos,expr.Length()-pos);
if( GlobalParameters::debug() ) {
std::cout << " Split into: '" << expr1 << "' && '" << expr2 << "'" << std::endl;
}
}
break;
}
if( expr(pos) == '|' ) { // Case: two expressions, OR
if( expr(pos+1) == '|' ) {
split = true;
op = "OR";
expr1 = expr(0,pos);
pos = pos+2;
expr2 = expr(pos,expr.Length()-pos);
if( GlobalParameters::debug() ) {
std::cout << " Split into: '" << expr1 << "' || '" << expr2 << "'" << std::endl;
}
}
break;
}
}
}
return split;
}
// Use this method to delete all existing selections
// Reused selections are treated correctly
// ---------------------------------------------------------------
void Filter::clear() {
for(std::vector<Filter*>::iterator it = garbage_.begin();
it != garbage_.end(); ++it) {
delete *it;
}
}
// ---------------------------------------------------------------
Cut* Cut::create(const TString &expr, unsigned int lineNum) {
if( GlobalParameters::debug() ) std::cout << " Cut::create(): '" << expr << "'" << std::flush;
TString cfg = cleanExpression(expr);
Cut* cut = 0;
std::vector<TString> compOps;
compOps.push_back(">=");
compOps.push_back(">");
compOps.push_back("<=");
compOps.push_back("<");
compOps.push_back("==");
compOps.push_back("!=");
std::vector<TString> parts;
TString op;
// Split into VAR, VAL, and comparison operator
for(std::vector<TString>::const_iterator oit = compOps.begin();
oit != compOps.end(); ++oit) {
if( cfg.Contains(*oit) ) {
Config::split(cfg,*oit,parts);
op = *oit;
break;
}
}
// Syntax check
if( parts.size() == 0 ) {
std::cerr << "\n\nERROR: Wrong syntax when specifying cut in line " << lineNum << std::endl;
std::cerr << "Cut expression '" << expr << "' contains none" << std::endl;
std::cerr << "of the valid comparison operators ('>','>=','<','<=','==','!=')" << std::endl;
exit(-1);
}
if( parts.size() == 1 || parts.size() > 3 ) {
std::cerr << "\n\nERROR: Wrong syntax when specifying cut in line " << lineNum << std::endl;
std::cerr << "Invalid cut expression '" << expr << "'" << std::endl;
exit(-1);
}
if( parts.size() == 3 && !( op == ">" || op == ">=" || op == "<" || op == "<=" ) ) {
std::cerr << "\n\nERROR: Wrong syntax when specifying cut in line " << lineNum << std::endl;
std::cerr << "Invalid cut expression '" << expr << "'" << std::endl;
exit(-1);
}
if( parts.size() == 2 ) {
TString var;
double val;
if( Variable::exists(parts.at(0)) && parts.at(1).IsFloat() ) {
var = parts.at(0);
val = parts.at(1).Atof();
} else if( Variable::exists(parts.at(1)) && parts.at(0).IsFloat() ) {
var = parts.at(1);
val = parts.at(0).Atof();
if( op == "<" ) op = ">";
else if( op == "<=" ) op = ">=";
else if( op == ">" ) op = "<";
else if( op == ">=" ) op = "<=";
} else {
std::cerr << "\n\nERROR: Wrong syntax when specifying cut in line " << lineNum << std::endl;
std::cerr << "Invalid cut expression '" << parts.at(0) << " " << op << " " << parts.at(1) << "'" << std::endl;
if( !(Variable::exists(parts.at(0)) && Variable::exists(parts.at(0))) ) {
std::cerr << "No known variables specified" << std::endl;
} else if( !(parts.at(0).IsFloat() && parts.at(1).IsFloat()) ) {
std::cerr << "No valid numbers specified" << std::endl;
}
exit(-1);
}
if( op == ">" ) cut = new CutGreaterThan(var,val);
else if( op == ">=" ) cut = new CutGreaterEqualThan(var,val);
else if( op == "<" ) cut = new CutLessThan(var,val);
else if( op == "<=" ) cut = new CutLessEqualThan(var,val);
else if( op == "==" ) cut = new CutEqual(var,val);
else if( op == "!=" ) cut = new CutNotEqual(var,val);
} else {
TString var = parts.at(1);
double val1 = parts.at(0).Atof();
double val2 = parts.at(2).Atof();
if( !Variable::exists(var) ) {
std::cerr << "\n\nERROR: Wrong syntax when specifying cut in line " << lineNum << std::endl;
std::cerr << "Invalid cut expression '" << expr << "'" << std::endl;
std::cerr << "Variable '" << var << "' does not exist" << std::endl;
} else if( !(parts.at(0).IsFloat() && parts.at(2).IsFloat()) ) {
std::cerr << "\n\nERROR: Wrong syntax when specifying cut in line " << lineNum << std::endl;
std::cerr << "Invalid cut expression '" << expr << "'" << std::endl;
std::cerr << "'" << parts.at(0) << "' and '" << parts.at(2) << "' are no numbers" << std::endl;
}
if( op == "<" ) cut = new CutLessThanLessThan(val1,var,val2);
else if( op == "<=" ) cut = new CutLessEqualThanLessEqualThan(val1,var,val2);
else if( op == ">" ) cut = new CutLessThanLessThan(val2,var,val1);
else if( op == ">=" ) cut = new CutLessEqualThanLessEqualThan(val2,var,val1);
}
return cut;
}
// ---------------------------------------------------------------
CutGreaterThan::CutGreaterThan(const TString &var, double val)
: Cut("") {
var_ = var;
val_ = val;
uid_ = var_+" > ";
uid_ += val_;
if( GlobalParameters::debug() ) std::cout << " (CutGreaterThan)" << std::endl;
}
// ---------------------------------------------------------------
CutGreaterEqualThan::CutGreaterEqualThan(const TString &var, double val)
: Cut("") {
var_ = var;
val_ = val;
uid_ = var_+" >= ";
uid_ += val_;
if( GlobalParameters::debug() ) std::cout << " (CutGreaterEqualThan)" << std::endl;
}
// ---------------------------------------------------------------
CutLessThan::CutLessThan(const TString &var, double val)
: Cut("") {
var_ = var;
val_ = val;
uid_ = var_+" < ";
uid_ += val_;
if( GlobalParameters::debug() ) std::cout << " (CutLessThan)" << std::endl;
}
// ---------------------------------------------------------------
CutLessEqualThan::CutLessEqualThan(const TString &var, double val)
: Cut("") {
var_ = var;
val_ = val;
uid_ = var_+" <= ";
uid_ += val_;
if( GlobalParameters::debug() ) std::cout << " (CutLessEqualThan)" << std::endl;
}
// ---------------------------------------------------------------
CutEqual::CutEqual(const TString &var, double val)
: Cut("") {
var_ = var;
val_ = val;
uid_ = var_+" == ";
uid_ += val_;
if( GlobalParameters::debug() ) std::cout << " (CutEqual)" << std::endl;
}
// ---------------------------------------------------------------
CutNotEqual::CutNotEqual(const TString &var, double val)
: Cut("") {
var_ = var;
val_ = val;
uid_ = var_+" != ";
uid_ += val_;
if( GlobalParameters::debug() ) std::cout << " (CutNotEqual)" << std::endl;
}
// ---------------------------------------------------------------
CutLessThanLessThan::CutLessThanLessThan(double val1, const TString &var, double val2)
: Cut("") {
var_ = var;
val_ = val1;
val2_ = val2;
uid_ = "";
uid_ += val_;
uid_ += " < "+var_+" < ";
uid_ += val2_;
if( GlobalParameters::debug() ) std::cout << " (CutLessThanLessThan)" << std::endl;
}
// ---------------------------------------------------------------
bool CutLessThanLessThan::passes(const Event* evt, const TString &dataSetLabel) const {
double x = evt->get(var_);
return x > val_ && x < val2_;
}
// ---------------------------------------------------------------
CutLessEqualThanLessEqualThan::CutLessEqualThanLessEqualThan(double val1, const TString &var, double val2)
: Cut("") {
var_ = var;
val_ = val1;
val2_ = val2;
uid_ = "";
uid_ += val_;
uid_ += " <= "+var_+" <= ";
uid_ += val2_;
if( GlobalParameters::debug() ) std::cout << " (CutLessEqualThanLessEqualThan)" << std::endl;
}
// ---------------------------------------------------------------
bool CutLessEqualThanLessEqualThan::passes(const Event* evt, const TString &dataSetLabel) const {
double x = evt->get(var_);
return x >= val_ && x <= val2_;
}
// ---------------------------------------------------------------
BooleanOperator::BooleanOperator(const Filter* filter1, const Filter* filter2, const TString &name)
: Filter("["+filter1->uid()+"] "+name+" ["+filter2->uid()+"]"),
filter1_(filter1), filter2_(filter2), name_(name) {
if( GlobalParameters::debug() ) std::cout << " BooleanOperator::BooleanOperator() '" << uid() << "'" << std::flush;
}
// ---------------------------------------------------------------
TString BooleanOperator::printOut() const {
TString txt = "";
offset_+="|";
txt += offset_+"-- "+name_+"\n";
txt += offset_+" |\n";
offset_ += " ";
txt += filter1_->printOut()+"\n";
txt += filter2_->printOut();
offset_.Chop();
offset_.Chop();
offset_.Chop();
offset_.Chop();
offset_.Chop();
return txt;
}
// ---------------------------------------------------------------
FilterAND::FilterAND(const Filter* filter1, const Filter* filter2)
: BooleanOperator(filter1,filter2,"AND") {
if( GlobalParameters::debug() ) std::cout << " (AND)" << std::endl;
}
// ---------------------------------------------------------------
bool FilterAND::passes(const Event* evt, const TString &dataSetLabel) const {
if( filter1_->passes(evt,dataSetLabel) ) {
if( filter2_->passes(evt,dataSetLabel) ) {
return true;
} else {
return false;
}
} else {
return false;
}
}
// ---------------------------------------------------------------
FilterOR::FilterOR(const Filter* filter1, const Filter* filter2)
: BooleanOperator(filter1,filter2,"OR") {
if( GlobalParameters::debug() ) std::cout << " (OR)" << std::endl;
}
// ---------------------------------------------------------------
bool FilterOR::passes(const Event* evt, const TString &dataSetLabel) const {
if( filter1_->passes(evt,dataSetLabel) ) {
return true;
} else if( filter2_->passes(evt,dataSetLabel) ) {
return true;
} else {
return false;
}
}
// ---------------------------------------------------------------
FilterNOT::FilterNOT(const Filter* filter)
: Filter("NOT["+filter->uid()+"]"), filter_(filter) {
if( GlobalParameters::debug() ) std::cout << " FilterNOT::FilterNOT() '" << uid() << "'" << std::endl;
}
// ---------------------------------------------------------------
FilterDataSet::FilterDataSet(const Filter* filter, const std::vector<TString> &applyToDataSets)
: Filter("FilterDataSet"), filter_(filter), applyToDataSets_(applyToDataSets) {
if( GlobalParameters::debug() ) std::cout << " FilterDataSet::FilterDataSet() (" << applyToDataSets_.front() << std::flush;
for(std::vector<TString>::const_iterator it = applyToDataSets_.begin()+1;
it != applyToDataSets_.end(); ++it) {
std::cout << ", " << *it;
}
std::cout << ")" << std::endl;
}
// ---------------------------------------------------------------
TString FilterDataSet::printOut() const {
TString txt = offset_+"(";
for(std::vector<TString>::const_iterator it = applyToDataSets_.begin();
it != applyToDataSets_.end(); ++it) {
txt += (*it)+", ";
}
txt.Chop();
txt.Chop();
txt += ")";
return txt;
}
// ---------------------------------------------------------------
bool FilterDataSet::passes(const Event* evt, const TString &dataSetLabel) const {
bool applyFilter = false;
for(std::vector<TString>::const_iterator it = applyToDataSets_.begin();
it != applyToDataSets_.end(); ++it) {
if( *it == dataSetLabel ) {
applyFilter = true;
break;
}
}
return applyFilter ? filter_->passes(evt,dataSetLabel) : true;
}