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