Splitted the PID cuts into three classes for ITS, TPC and TOF, and added the possibil...
[u/mrichter/AliRoot.git] / PWG2 / RESONANCES / AliRsnFunction.cxx
CommitLineData
13f28255 1//
2// Class AliRsnFunction
3//
4// This class defines a base classe to implement a function
5// which uses the internal RSN package event format (AliRsnEvent).
6// It contains some default flags which turn out to be useful:
7// - a flag to select only the "true" pairs (tracks from same resonance)
8// - a flag to know if the computation is done over two events (mixing)
9//
10// Any kind of analysis object should be implemented as inheriting from this
11// because the AliRsnAnalyzer which executes the analysis will accept a collection
12// of such objects, in order to have a unique format of processing method
13//
e0baff8c 14// The user who implements a kind of computation type should inherit from
15// this class and override the virtual functions defined in it, which
13f28255 16// initialize the final output histogram and define how to process data.
17//
18//
19// author: A. Pulvirenti (email: alberto.pulvirenti@ct.infn.it)
20//
21
13f28255 22#include <TString.h>
b2b08ca2 23#include <TAxis.h>
13f28255 24
25#include "AliLog.h"
26
13f28255 27#include "AliRsnDaughter.h"
28#include "AliRsnEvent.h"
29#include "AliRsnPairDef.h"
2dab9030 30#include "AliRsnMother.h"
31#include "AliRsnValue.h"
13f28255 32
33#include "AliRsnFunction.h"
34
35ClassImp(AliRsnFunction)
36
37//________________________________________________________________________________________
eb079724 38AliRsnFunction::AliRsnFunction(Bool_t useTH1) :
32992791 39 TObject(),
40 fPairDef(0x0),
41 fAxisList("AliRsnValue", 0),
42 fPair(0x0),
43 fEvent(0x0),
44 fUseTH1(useTH1),
45 fSize(0),
46 fH1(0x0),
47 fHSparse(0x0)
13f28255 48{
5eb970a4 49//
b2028424 50// Constructor.
5eb970a4 51//
8a22fe6f 52}
13f28255 53
54//________________________________________________________________________________________
5eb970a4 55AliRsnFunction::AliRsnFunction(const AliRsnFunction &copy) :
32992791 56 TObject(copy),
57 fPairDef(copy.fPairDef),
58 fAxisList(copy.fAxisList),
59 fPair(copy.fPair),
60 fEvent(copy.fEvent),
61 fUseTH1(copy.fUseTH1),
62 fSize(copy.fSize),
63 fH1(0x0),
64 fHSparse(0x0)
13f28255 65{
5eb970a4 66//
67// Copy constructor.
68//
13f28255 69}
70
71//________________________________________________________________________________________
5eb970a4 72const AliRsnFunction& AliRsnFunction::operator=(const AliRsnFunction& copy)
13f28255 73{
5eb970a4 74//
75// Assignment operator.
76//
77
5eb970a4 78 fPairDef = copy.fPairDef;
5eb970a4 79 fPair = copy.fPair;
80 fEvent = copy.fEvent;
eb079724 81 fUseTH1 = copy.fUseTH1;
82 fSize = copy.fSize;
5eb970a4 83
eb079724 84 if (fH1) delete fH1;
85 fH1 = 0x0;
86
87 if (fHSparse) delete fHSparse;
88 fHSparse = 0x0;
5eb970a4 89
5eb970a4 90 return (*this);
13f28255 91}
92
93//________________________________________________________________________________________
b2028424 94const char* AliRsnFunction::GetName() const
13f28255 95{
5eb970a4 96//
97// Defines the name of this object according to
98// the function type and binning
99//
13f28255 100
b2028424 101 TString name("");
e0baff8c 102
b2028424 103 TObjArrayIter next(&fAxisList);
2dab9030 104 AliRsnValue *axis = 0;
13f28255 105
32992791 106 while ((axis = (AliRsnValue*)next()))
107 {
b2028424 108 if (name.Length() > 1) name += '_';
109 name += axis->GetName();
13f28255 110 }
13f28255 111
b2028424 112 return name.Data();
5eb970a4 113}
114
b2028424 115//________________________________________________________________________________________
32992791 116Bool_t AliRsnFunction::AddAxis(AliRsnValue *const axis)
5eb970a4 117{
32992791 118//
119// Try to add a new axis to this function.
120// The operation succeeds only if the related value object
121// has a target amon those allowed here (AliRsnMother, AliRsnEvent),
122// otherwise the axis is not added.
123//
124// If more than 3 axes are added, switch to THnSparseF output.
125// NOTE: this can cause large files and high memory occupancy.
126//
127
128 RSNTARGET target = axis->GetTargetType();
129 if (target != AliRsnTarget::kMother && target != AliRsnTarget::kEvent)
130 {
131 AliError(Form("Allowed targets are mothers and events; cannot use axis '%s' which has target '%s'", axis->GetName(), axis->GetTargetTypeName()));
132 return kFALSE;
133 }
134
b2028424 135 Int_t size = fAxisList.GetEntries();
32992791 136 new (fAxisList[size]) AliRsnValue(*axis);
2dab9030 137
138 if (fAxisList.GetEntries() > 3)
eb079724 139 {
32992791 140 AliWarning("Adding more than 3 axes will produce a THnSparseD output.");
eb079724 141 fUseTH1 = kFALSE;
142 }
32992791 143
144 return kTRUE;
13f28255 145}
146
147//________________________________________________________________________________________
eb079724 148TH1* AliRsnFunction::CreateHistogram(const char *histoName, const char *histoTitle)
13f28255 149{
5eb970a4 150//
151// Creates and returns the histogram defined using
152// arguments fo name and title, and the first histoDef for binning.
153// Variable-sized histogram binning is always called, due to use of histoDef,
154// even if the bins are equal, since they are defined in this class.
155// Eventually present histoDef's in other slots of array (1, 2) are ignored.
156//
0d73200d 157// This version produces a THnSparseF.
eb079724 158//
13f28255 159
eb079724 160 fSize = fAxisList.GetEntries();
32992791 161 if (!fSize)
162 {
eb079724 163 AliError("No axes defined");
164 return 0x0;
165 }
32992791 166 else if (fSize > 3)
eb079724 167 {
32992791 168 AliError("Too many axes defined for a TH1 object");
eb079724 169 return 0x0;
170 }
171
eb079724 172 // retrieve binnings for main and secondary axes
b2b08ca2 173 AliRsnValue *fcnAxis;
174 TArrayD array[3];
175 for (Int_t i = 0; i < fSize; i++)
176 {
2dab9030 177 fcnAxis = (AliRsnValue*)fAxisList.At(i);
b2b08ca2 178 if (!fcnAxis)
179 {
eb079724 180 AliError("Empty axis");
b2b08ca2 181 array[i].Set(2);
182 array[i][0] = -1E5;
183 array[i][1] = -1E5;
eb079724 184 continue;
185 }
b2b08ca2 186 else
187 {
188 array[i] = fcnAxis->GetArray();
189 }
eb079724 190 }
191
192 // create histogram depending on the number of axes
193 switch (fSize)
194 {
195 case 1:
b2b08ca2 196 fH1 = new TH1F(histoName, histoTitle, array[0].GetSize() - 1, array[0].GetArray());
eb079724 197 break;
198 case 2:
b2b08ca2 199 fH1 = new TH2F(histoName, histoTitle, array[0].GetSize() - 1, array[0].GetArray(), array[1].GetSize() - 1, array[1].GetArray());
eb079724 200 break;
201 case 3:
b2b08ca2 202 fH1 = new TH3F(histoName, histoTitle, array[0].GetSize() - 1, array[0].GetArray(), array[1].GetSize() - 1, array[1].GetArray(), array[2].GetSize() - 1, array[2].GetArray());
eb079724 203 break;
204 }
205 fH1->Sumw2();
206
207 return fH1;
208}
209
210//________________________________________________________________________________________
0d73200d 211THnSparseF* AliRsnFunction::CreateHistogramSparse(const char *histoName, const char *histoTitle)
eb079724 212{
213//
214// Creates and returns the histogram defined using
215// arguments fo name and title, and the first histoDef for binning.
216// Variable-sized histogram binning is always called, due to use of histoDef,
217// even if the bins are equal, since they are defined in this class.
218// Eventually present histoDef's in other slots of array (1, 2) are ignored.
219//
0d73200d 220// This version produces a THnSparseF.
eb079724 221//
222
223 fSize = fAxisList.GetEntries();
32992791 224 if (!fSize)
225 {
b2028424 226 AliError("No axes defined");
227 return 0x0;
228 }
b2b08ca2 229
cc4a35d5 230 // initialize the array of number of bins for each axis
231 // taking it from the stored values, while for the bins
232 // they are set as summied and defined later
ceb7ce00 233 Double_t dummyD;
234 Int_t *nbins = new Int_t[fSize];
235 AliRsnValue *fcnAxis = 0;
cc4a35d5 236 for (Int_t i = 0; i < fSize; i++)
237 {
ceb7ce00 238 fcnAxis = (AliRsnValue*)fAxisList.At(i);
cc4a35d5 239 if (!fcnAxis)
240 {
241 nbins[i] = 1;
242 AliError("Empty axis");
243 continue;
244 }
ceb7ce00 245 nbins[i] = fcnAxis->GetArray().GetSize() - 1;
cc4a35d5 246 }
b2028424 247
b2b08ca2 248 // create histogram
cc4a35d5 249 fHSparse = new THnSparseF(histoName, histoTitle, fSize, nbins, &dummyD, &dummyD);
b2b08ca2 250 fHSparse->Sumw2();
251
252 // update the various axes using the definitions given in the array of axes here
b2b08ca2 253 for (Int_t i = 0; i < fSize; i++)
254 {
2dab9030 255 fcnAxis = (AliRsnValue*)fAxisList.At(i);
b2028424 256 if (!fcnAxis) {
b2b08ca2 257 AliError("Empty axis: doing unique bin betweeen -100000 and 100000");
b2028424 258 continue;
5eb970a4 259 }
2467e7c3 260 fHSparse->SetBinEdges(i, fcnAxis->GetArray().GetArray());
eb079724 261 }
740e7310 262
263 delete [] nbins;
eb079724 264
eb079724 265 return fHSparse;
5eb970a4 266}
267
eb079724 268
5eb970a4 269//________________________________________________________________________________________
270Bool_t AliRsnFunction::Fill()
271{
272//
273// Fill function histogram with values computed from given input object.
274//
b2028424 275
5eb970a4 276 AliDebug(AliLog::kDebug +2,"->");
277
eb079724 278 Int_t i;
279 Double_t *values = new Double_t[fSize];
32992791 280 Bool_t computeOK = kFALSE;
b2028424 281
2dab9030 282 AliRsnValue *fcnAxis = 0;
32992791 283 for (i = 0; i < fSize; i++)
284 {
285 values[i] = 0.0;
2dab9030 286 fcnAxis = (AliRsnValue*)fAxisList.At(i);
32992791 287 if (!fcnAxis) continue;
288 switch (fcnAxis->GetTargetType())
96e9d35d 289 {
32992791 290 case AliRsnTarget::kMother:
291 fcnAxis->SetSupportObject(fPairDef);
292 computeOK = fcnAxis->Eval(fPair);
293 break;
294 case AliRsnTarget::kEvent:
295 computeOK = fcnAxis->Eval(fEvent);
296 break;
297 default:
298 AliError(Form("Allowed targets are mothers and events; cannot use axis '%s' which has target '%s'", fcnAxis->GetName(), fcnAxis->GetTargetTypeName()));
299 computeOK = kFALSE;
b2028424 300 }
32992791 301 if (computeOK) values[i] = ((Float_t)fcnAxis->GetComputedValue());
5eb970a4 302 }
eb079724 303
304 // fill histogram
305 if (fUseTH1)
306 {
307 // check presence of output histogram
32992791 308 if (!fH1)
309 {
96e9d35d 310 AliError("Required a TH1 which is not initialized");
311 delete [] values;
eb079724 312 return kFALSE;
313 }
314
315 // fill according to dimensions
316 switch (fSize)
317 {
318 case 1:
319 {
0d73200d 320 TH1F *h1 = (TH1F*)fH1;
eb079724 321 h1->Fill(values[0]);
322 }
323 break;
324 case 2:
325 {
0d73200d 326 TH2F *h2 = (TH2F*)fH1;
eb079724 327 h2->Fill(values[0], values[1]);
328 }
329 break;
330 case 3:
331 {
0d73200d 332 TH3F *h3 = (TH3F*)fH1;
eb079724 333 h3->Fill(values[0], values[1], values[2]);
334 }
335 break;
336 default:
337 AliError(Form("Wrong size : %d", fSize));
96e9d35d 338 delete [] values;
eb079724 339 return kFALSE;
340 }
341 }
342 else
343 {
344 // check presence of output histogram
32992791 345 if (!fHSparse)
346 {
0d73200d 347 AliError("Required a THnSparseF which is not initialized");
d0282f3d 348 delete [] values;
eb079724 349 return kFALSE;
350 }
351
352 fHSparse->Fill(values);
5eb970a4 353 }
eb079724 354
355 delete [] values;
e0baff8c 356
5eb970a4 357 AliDebug(AliLog::kDebug +2,"->");
358 return kTRUE;
13f28255 359}