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