]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG2/RESONANCES/AliRsnCutSet.cxx
Fixed all fixable coding conventions violations
[u/mrichter/AliRoot.git] / PWG2 / RESONANCES / AliRsnCutSet.cxx
1 //
2 // Class AliRsnCutSet
3 //
4 // This is the front-end for cut management and checking.
5 // It must be prepared by adding all required single cuts,
6 // and then with a logical expression which combines all cuts
7 // with the "AND", "OR" and "NOT" operators.
8 //
9
10 #include "AliLog.h"
11
12 #include "AliRsnExpression.h"
13 #include "AliRsnCut.h"
14
15 #include "AliRsnCutSet.h"
16
17 ClassImp(AliRsnCutSet)
18
19 //_____________________________________________________________________________
20 AliRsnCutSet::AliRsnCutSet() :
21    AliRsnTarget(),
22    fCuts(0),
23    fNumOfCuts(0),
24    fCutScheme(""),
25    fCutSchemeIndexed(""),
26    fBoolValues(0),
27    fIsScheme(kFALSE),
28    fExpression(0)
29 {
30 //
31 // Constructor without name (not recommended)
32 //
33
34    fBoolValues = new Bool_t[1];
35    AliRsnExpression::fgCutSet = this;
36 }
37
38 //_____________________________________________________________________________
39 AliRsnCutSet::AliRsnCutSet(const char *name, RSNTARGET target) :
40    AliRsnTarget(name, target),
41    fCuts(0),
42    fNumOfCuts(0),
43    fCutScheme(""),
44    fCutSchemeIndexed(""),
45    fBoolValues(0),
46    fIsScheme(kFALSE),
47    fExpression(0)
48 {
49 //
50 // Constructor with argument name (recommended)
51 //
52
53    fBoolValues = new Bool_t[1];
54    fExpression = 0;
55    AliRsnExpression::fgCutSet = this;
56 }
57
58 //_____________________________________________________________________________
59 AliRsnCutSet::AliRsnCutSet(const AliRsnCutSet & copy) :
60    AliRsnTarget(copy),
61    fCuts(copy.fCuts),
62    fNumOfCuts(copy.fNumOfCuts),
63    fCutScheme(copy.fCutScheme),
64    fCutSchemeIndexed(copy.fCutSchemeIndexed),
65    fBoolValues(0),
66    fIsScheme(copy.fIsScheme),
67    fExpression(copy.fExpression)
68 {
69 //
70 // Copy constructor
71 //
72
73    Int_t i;
74    fBoolValues = new Bool_t[fNumOfCuts];
75    for (i = 0; i < fNumOfCuts; ++i) {
76       fBoolValues[i] = copy.fBoolValues[i];
77    }
78
79    AliRsnExpression::fgCutSet = this;
80 }
81
82 //_____________________________________________________________________________
83 AliRsnCutSet& AliRsnCutSet::operator=(const AliRsnCutSet & copy)
84 {
85 //
86 // Assignment operator.
87 //
88
89    AliRsnTarget::operator=(copy);
90
91    fCuts = copy.fCuts;
92    fNumOfCuts = copy.fNumOfCuts;
93    fCutScheme = copy.fCutScheme;
94    fCutSchemeIndexed = copy.fCutSchemeIndexed;
95    fIsScheme = copy.fIsScheme;
96    fExpression = copy.fExpression;
97
98    if (fBoolValues) delete [] fBoolValues;
99
100    Int_t i;
101    fBoolValues = new Bool_t[fNumOfCuts];
102    for (i = 0; i < fNumOfCuts; ++i) {
103       fBoolValues[i] = copy.fBoolValues[i];
104    }
105
106    AliRsnExpression::fgCutSet = this;
107
108    return (*this);
109 }
110
111 //_____________________________________________________________________________
112 AliRsnCutSet::~AliRsnCutSet()
113 {
114 //
115 // Destructor
116 //
117
118    delete fExpression;
119    delete [] fBoolValues;
120 }
121
122 //_____________________________________________________________________________
123 void AliRsnCutSet::AddCut(AliRsnCut *cut)
124 {
125 //
126 // Add a new cut.
127 // This must be done for all components of the final expression.
128 // If the target of the cut does not match the target of this,
129 // the cut is not added.
130 //
131
132    if (!cut->IsTarget(GetTargetType())) {
133       AliError(Form("Cannot add this cut (cut set name,target = [%s],[%s] --- cut name,target = [%s],[%s]", GetName(), GetTargetTypeName(), cut->GetName(), cut->GetTargetTypeName()));
134       return;
135    }
136
137    Int_t i;
138
139    AliDebug(AliLog::kDebug, "<-");
140    fCuts.Add(cut);
141    AliInfo(Form("====> Adding a new cut: [%s]", cut->GetName()));
142    //cut->Print();
143    fNumOfCuts++;
144
145    if (fBoolValues) delete [] fBoolValues;
146
147    fBoolValues = new Bool_t[fNumOfCuts];
148    for (i = 0; i < fNumOfCuts; i++) {
149       fBoolValues[i] = kTRUE;
150    }
151
152    AliDebug(AliLog::kDebug, Form("%d", fCuts.GetEntriesFast()));
153    AliDebug(AliLog::kDebug, "->");
154 }
155
156 //_____________________________________________________________________________
157 void AliRsnCutSet::ShowCuts() const
158 {
159 //
160 // Prints all cuts
161 //
162    AliRsnCut *cut;
163
164    for (Int_t i = 0; i < fCuts.GetEntriesFast() ; i++) {
165       cut = (AliRsnCut*)fCuts.At(i);
166       cut->Print();
167    }
168 }
169
170 //_____________________________________________________________________________
171 Bool_t AliRsnCutSet::IsSelected(TObject *object)
172 {
173 //
174 // Checks an object according to the cut expression defined here.
175 //
176
177    Int_t i;
178
179    if (!fNumOfCuts) return kTRUE;
180
181    Bool_t boolReturn = kTRUE;
182    AliRsnCut *cut;
183    for (i = 0; i < fNumOfCuts; i++) {
184       cut = (AliRsnCut*)fCuts.At(i);
185       fBoolValues[i] = cut->IsSelected(object);
186    }
187
188    if (fIsScheme) boolReturn = Passed();
189    return boolReturn;
190 }
191
192 //_____________________________________________________________________________
193 void AliRsnCutSet::SetCutScheme(const char *theValue)
194 {
195 //
196 // Define the combination of cuts with logical operators
197 // and using the names given to all defined cuts.
198 //
199
200    AliDebug(AliLog::kDebug, "<-");
201
202    fCutScheme = theValue;
203    SetCutSchemeIndexed(theValue);
204    fIsScheme = kTRUE;
205    AliDebug(AliLog::kDebug, "->");
206 }
207
208 //_____________________________________________________________________________
209 void AliRsnCutSet::SetCutSchemeIndexed(TString theValue)
210 {
211 //
212 // Internal method which indexes all cuts to organize their combo
213 //
214
215    AliDebug(AliLog::kDebug, "<-");
216    theValue.Append(" ");
217    // fCutSchemeIndexed = theValue;
218    fCutSchemeIndexed = GetCutSchemeIndexed();
219    AliDebug(AliLog::kDebug, "->");
220 }
221
222 //_____________________________________________________________________________
223 Int_t AliRsnCutSet::GetIndexByCutName(TString s)
224 {
225 //
226 // Retrieve the cut index from its name
227 //
228
229    Int_t i;
230    AliRsnCut *cut;
231
232    for (i = 0; i < fCuts.GetEntriesFast(); i++) {
233       cut = (AliRsnCut*) fCuts.At(i);
234       if (!s.CompareTo(cut->GetName())) return i;
235    }
236
237    return -1;
238 }
239
240 //_____________________________________________________________________________
241 Bool_t AliRsnCutSet::Passed()
242 {
243 //
244 // Combines the cuts according to expression
245 // and gives a global response to the cut check
246 //
247
248    AliRsnExpression::fgCutSet = this;
249    if (!fExpression) {
250       fExpression = new AliRsnExpression(fCutSchemeIndexed);
251       AliDebug(AliLog::kDebug, "fExpression was created.");
252    }
253
254    if (fCuts.IsEmpty()) return kTRUE;
255
256    return fExpression->Value(*GetCuts());
257 }
258
259 //_____________________________________________________________________________
260 Bool_t AliRsnCutSet::IsValidScheme()
261 {
262 //
263 // Validity check on cut expression specified by user
264 //
265
266    TString str(fCutScheme);
267    AliRsnCut *cut;
268    for (Int_t i = 0; i < fNumOfCuts; i++) {
269       cut = (AliRsnCut*)fCuts.At(i);
270       str.ReplaceAll(cut->GetName(), "");
271    }
272    str.ReplaceAll("&", "");
273    str.ReplaceAll("!", "");
274    str.ReplaceAll("|", "");
275    str.ReplaceAll("(", "");
276    str.ReplaceAll(")", "");
277
278    if (!str.IsNull()) {
279       AliError(Form("Cut scheme '%s' is not valid !!!", fCutScheme.Data()));
280       return kFALSE;
281    }
282
283    return kTRUE;
284 //   return (!(ShowCutScheme().Contains("Error")));
285 }
286
287 //_____________________________________________________________________________
288 TString AliRsnCutSet::ShowCutScheme() const
289 {
290 //
291 // Utility method to check validity of expression
292 //
293
294    return fCutScheme;
295 //   return fExpression->Unparse();
296 }
297
298 //_____________________________________________________________________________
299 Int_t AliRsnCutSet::TestExpression(TString opt)
300 {
301
302 //   AliRsnCut *cut1 = new AliRsnCut ("aaa","aaa");
303 //   cut1->SetCutValues (AliRsnCut::kEsdPt,0.0,1.0);
304 //   AliRsnCut *cut2 = new AliRsnCut ("bbb","bbb");
305 //   cut2->SetCutValues (AliRsnCut::kEsdPt,1.,2.0);
306 //   AliRsnCut *cut3 = new AliRsnCut ("ccc","ccc");
307 //   cut3->SetCutValues (AliRsnCut::kEsdPt,2.0,3.0);
308 //
309 //   AliRsnCutSet* set  = new AliRsnCutSet ("setOne");
310 //   set->AddCut (cut1);
311 //   set->AddCut (cut2);
312 //   set->AddCut (cut3);
313 //
314 //   set->SetCutScheme ("(aaa&!(ccc))&(bbb&!(ccc))");
315 //
316 //   set->ShowCuts ();
317
318    AliDebug(1, opt.Data());
319    return 0;
320 }
321
322 //_____________________________________________________________________________
323 void AliRsnCutSet::PrintSetInfo()
324 {
325 //
326 // Show data about the cut set
327 //
328
329    Int_t i;
330
331    AliInfo("========== Rsn Cut Set info ==============");
332    AliInfo(Form("Scheme : %s", fCutScheme.Data()));
333    AliInfo(Form("Scheme : %s", fCutSchemeIndexed.Data()));
334    AliInfo(Form("Num of Cuts: %d", fCuts.GetEntriesFast()));
335    AliInfo("====== Cuts ======");
336    AliRsnCut *cut;
337    for (i = 0; i < fCuts.GetEntriesFast(); i++) {
338       cut = (AliRsnCut*) fCuts.At(i);
339       if (cut) AliInfo(Form("%d %d", i, fBoolValues[i]));
340    }
341    AliInfo("========== END Rsn Cut Mgr info ==============");
342 }
343
344 //_____________________________________________________________________________
345 TString AliRsnCutSet::GetCutSchemeIndexed()
346 {
347 //
348 // Internal method to retrieve the list of cuts with their indexes
349 // for evaluation of cut expression
350 //
351
352    AliDebug(AliLog::kDebug, "<-");
353    Int_t i;
354    TString str(fCutScheme);
355    AliDebug(AliLog::kDebug, Form("Num of cuts %d", fCuts.GetEntriesFast()));
356    AliRsnCut *cut;
357    for (i = 0; i < fCuts.GetEntriesFast(); i++) {
358       cut = (AliRsnCut*) fCuts.At(i);
359       str.ReplaceAll(cut->GetName(), Form("%d", i));
360    }
361    AliDebug(AliLog::kDebug, "->");
362    return str;
363 }