]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG2/RESONANCES/AliRsnCutSet.cxx
Fix for coverity
[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    if (this == &copy)
91      return *this;
92    
93    fCuts = copy.fCuts;
94    fNumOfCuts = copy.fNumOfCuts;
95    fCutScheme = copy.fCutScheme;
96    fCutSchemeIndexed = copy.fCutSchemeIndexed;
97    fIsScheme = copy.fIsScheme;
98    fExpression = copy.fExpression;
99
100    if (fBoolValues) delete [] fBoolValues;
101
102    Int_t i;
103    fBoolValues = new Bool_t[fNumOfCuts];
104    for (i = 0; i < fNumOfCuts; ++i) {
105       fBoolValues[i] = copy.fBoolValues[i];
106    }
107
108    AliRsnExpression::fgCutSet = this;
109
110    return (*this);
111 }
112
113 //_____________________________________________________________________________
114 AliRsnCutSet::~AliRsnCutSet()
115 {
116 //
117 // Destructor
118 //
119
120    delete fExpression;
121    delete [] fBoolValues;
122 }
123
124 //_____________________________________________________________________________
125 void AliRsnCutSet::AddCut(AliRsnCut *cut)
126 {
127 //
128 // Add a new cut.
129 // This must be done for all components of the final expression.
130 // If the target of the cut does not match the target of this,
131 // the cut is not added.
132 //
133
134    if (!cut->IsTarget(GetTargetType())) {
135       AliError(Form("Cannot add this cut (cut set name,target = [%s],[%s] --- cut name,target = [%s],[%s]", GetName(), GetTargetTypeName(), cut->GetName(), cut->GetTargetTypeName()));
136       return;
137    }
138
139    Int_t i;
140
141    AliDebug(AliLog::kDebug, "<-");
142    fCuts.Add(cut);
143    AliInfo(Form("====> Adding a new cut: [%s]", cut->GetName()));
144    //cut->Print();
145    fNumOfCuts++;
146
147    if (fBoolValues) delete [] fBoolValues;
148
149    fBoolValues = new Bool_t[fNumOfCuts];
150    for (i = 0; i < fNumOfCuts; i++) {
151       fBoolValues[i] = kTRUE;
152    }
153
154    AliDebug(AliLog::kDebug, Form("%d", fCuts.GetEntriesFast()));
155    AliDebug(AliLog::kDebug, "->");
156 }
157
158 //_____________________________________________________________________________
159 void AliRsnCutSet::ShowCuts() const
160 {
161 //
162 // Prints all cuts
163 //
164    AliRsnCut *cut;
165
166    for (Int_t i = 0; i < fCuts.GetEntriesFast() ; i++) {
167       cut = (AliRsnCut*)fCuts.At(i);
168       cut->Print();
169    }
170 }
171
172 //_____________________________________________________________________________
173 Bool_t AliRsnCutSet::IsSelected(TObject *object)
174 {
175 //
176 // Checks an object according to the cut expression defined here.
177 //
178
179    Int_t i;
180
181    if (!fNumOfCuts) return kTRUE;
182
183    Bool_t boolReturn = kTRUE;
184    AliRsnCut *cut;
185    for (i = 0; i < fNumOfCuts; i++) {
186       cut = (AliRsnCut*)fCuts.At(i);
187       fBoolValues[i] = cut->IsSelected(object);
188    }
189
190    if (fIsScheme) boolReturn = Passed();
191    return boolReturn;
192 }
193
194 //_____________________________________________________________________________
195 void AliRsnCutSet::SetCutScheme(const char *theValue)
196 {
197 //
198 // Define the combination of cuts with logical operators
199 // and using the names given to all defined cuts.
200 //
201
202    AliDebug(AliLog::kDebug, "<-");
203
204    fCutScheme = theValue;
205    SetCutSchemeIndexed(theValue);
206    fIsScheme = kTRUE;
207    AliDebug(AliLog::kDebug, "->");
208 }
209
210 //_____________________________________________________________________________
211 void AliRsnCutSet::SetCutSchemeIndexed(TString theValue)
212 {
213 //
214 // Internal method which indexes all cuts to organize their combo
215 //
216
217    AliDebug(AliLog::kDebug, "<-");
218    theValue.Append(" ");
219    // fCutSchemeIndexed = theValue;
220    fCutSchemeIndexed = GetCutSchemeIndexed();
221    AliDebug(AliLog::kDebug, "->");
222 }
223
224 //_____________________________________________________________________________
225 Int_t AliRsnCutSet::GetIndexByCutName(TString s)
226 {
227 //
228 // Retrieve the cut index from its name
229 //
230
231    Int_t i;
232    AliRsnCut *cut;
233
234    for (i = 0; i < fCuts.GetEntriesFast(); i++) {
235       cut = (AliRsnCut*) fCuts.At(i);
236       if (!s.CompareTo(cut->GetName())) return i;
237    }
238
239    return -1;
240 }
241
242 //_____________________________________________________________________________
243 Bool_t AliRsnCutSet::Passed()
244 {
245 //
246 // Combines the cuts according to expression
247 // and gives a global response to the cut check
248 //
249
250    AliRsnExpression::fgCutSet = this;
251    if (!fExpression) {
252       fExpression = new AliRsnExpression(fCutSchemeIndexed);
253       AliDebug(AliLog::kDebug, "fExpression was created.");
254    }
255
256    if (fCuts.IsEmpty()) return kTRUE;
257
258    return fExpression->Value(*GetCuts());
259 }
260
261 //_____________________________________________________________________________
262 Bool_t AliRsnCutSet::IsValidScheme()
263 {
264 //
265 // Validity check on cut expression specified by user
266 //
267
268    TString str(fCutScheme);
269    AliRsnCut *cut;
270    for (Int_t i = 0; i < fNumOfCuts; i++) {
271       cut = (AliRsnCut*)fCuts.At(i);
272       str.ReplaceAll(cut->GetName(), "");
273    }
274    str.ReplaceAll("&", "");
275    str.ReplaceAll("!", "");
276    str.ReplaceAll("|", "");
277    str.ReplaceAll("(", "");
278    str.ReplaceAll(")", "");
279
280    if (!str.IsNull()) {
281       AliError(Form("Cut scheme '%s' is not valid !!!", fCutScheme.Data()));
282       return kFALSE;
283    }
284
285    return kTRUE;
286 //   return (!(ShowCutScheme().Contains("Error")));
287 }
288
289 //_____________________________________________________________________________
290 TString AliRsnCutSet::ShowCutScheme() const
291 {
292 //
293 // Utility method to check validity of expression
294 //
295
296    return fCutScheme;
297 //   return fExpression->Unparse();
298 }
299
300 //_____________________________________________________________________________
301 Int_t AliRsnCutSet::TestExpression(TString opt)
302 {
303
304 //   AliRsnCut *cut1 = new AliRsnCut ("aaa","aaa");
305 //   cut1->SetCutValues (AliRsnCut::kEsdPt,0.0,1.0);
306 //   AliRsnCut *cut2 = new AliRsnCut ("bbb","bbb");
307 //   cut2->SetCutValues (AliRsnCut::kEsdPt,1.,2.0);
308 //   AliRsnCut *cut3 = new AliRsnCut ("ccc","ccc");
309 //   cut3->SetCutValues (AliRsnCut::kEsdPt,2.0,3.0);
310 //
311 //   AliRsnCutSet* set  = new AliRsnCutSet ("setOne");
312 //   set->AddCut (cut1);
313 //   set->AddCut (cut2);
314 //   set->AddCut (cut3);
315 //
316 //   set->SetCutScheme ("(aaa&!(ccc))&(bbb&!(ccc))");
317 //
318 //   set->ShowCuts ();
319
320    AliDebug(1, opt.Data());
321    return 0;
322 }
323
324 //_____________________________________________________________________________
325 void AliRsnCutSet::PrintSetInfo()
326 {
327 //
328 // Show data about the cut set
329 //
330
331    Int_t i;
332
333    AliInfo("========== Rsn Cut Set info ==============");
334    AliInfo(Form("Scheme : %s", fCutScheme.Data()));
335    AliInfo(Form("Scheme : %s", fCutSchemeIndexed.Data()));
336    AliInfo(Form("Num of Cuts: %d", fCuts.GetEntriesFast()));
337    AliInfo("====== Cuts ======");
338    AliRsnCut *cut;
339    for (i = 0; i < fCuts.GetEntriesFast(); i++) {
340       cut = (AliRsnCut*) fCuts.At(i);
341       if (cut) AliInfo(Form("%d %d", i, fBoolValues[i]));
342    }
343    AliInfo("========== END Rsn Cut Mgr info ==============");
344 }
345
346 //_____________________________________________________________________________
347 TString AliRsnCutSet::GetCutSchemeIndexed()
348 {
349 //
350 // Internal method to retrieve the list of cuts with their indexes
351 // for evaluation of cut expression
352 //
353
354    AliDebug(AliLog::kDebug, "<-");
355    Int_t i;
356    TString str(fCutScheme);
357    AliDebug(AliLog::kDebug, Form("Num of cuts %d", fCuts.GetEntriesFast()));
358    AliRsnCut *cut;
359    for (i = 0; i < fCuts.GetEntriesFast(); i++) {
360       cut = (AliRsnCut*) fCuts.At(i);
361       str.ReplaceAll(cut->GetName(), Form("%d", i));
362    }
363    AliDebug(AliLog::kDebug, "->");
364    return str;
365 }