]> git.uio.no Git - u/mrichter/AliRoot.git/blame - PWG2/RESONANCES/AliRsnFunction.cxx
First version of tasks for central train
[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
2dab9030 27#include "AliRsnValue.h"
13f28255 28
29#include "AliRsnFunction.h"
30
31ClassImp(AliRsnFunction)
32
33//________________________________________________________________________________________
d7712d44 34AliRsnFunction::AliRsnFunction(Bool_t useTH1) :
2a1c7696 35 TObject(),
2a1c7696 36 fAxisList("AliRsnValue", 0),
2a1c7696 37 fUseTH1(useTH1),
38 fSize(0),
39 fH1(0x0),
d7712d44 40 fHSparse(0x0),
41 fValues(0)
13f28255 42{
5eb970a4 43//
b2028424 44// Constructor.
5eb970a4 45//
8a22fe6f 46}
13f28255 47
48//________________________________________________________________________________________
5eb970a4 49AliRsnFunction::AliRsnFunction(const AliRsnFunction &copy) :
2a1c7696 50 TObject(copy),
2a1c7696 51 fAxisList(copy.fAxisList),
2a1c7696 52 fUseTH1(copy.fUseTH1),
53 fSize(copy.fSize),
54 fH1(0x0),
d7712d44 55 fHSparse(0x0),
56 fValues(copy.fValues)
13f28255 57{
5eb970a4 58//
59// Copy constructor.
60//
13f28255 61}
62
63//________________________________________________________________________________________
5eb970a4 64const AliRsnFunction& AliRsnFunction::operator=(const AliRsnFunction& copy)
13f28255 65{
5eb970a4 66//
67// Assignment operator.
68//
69
d7712d44 70 fAxisList = copy.fAxisList;
2a1c7696 71 fUseTH1 = copy.fUseTH1;
72 fSize = copy.fSize;
d7712d44 73 fValues = copy.fValues;
5eb970a4 74
2a1c7696 75 if (fH1) delete fH1;
2a1c7696 76 if (fHSparse) delete fHSparse;
2a1c7696 77
78 return (*this);
13f28255 79}
80
81//________________________________________________________________________________________
b2028424 82const char* AliRsnFunction::GetName() const
13f28255 83{
5eb970a4 84//
85// Defines the name of this object according to
86// the function type and binning
87//
13f28255 88
2a1c7696 89 TString name("");
e0baff8c 90
2a1c7696 91 TObjArrayIter next(&fAxisList);
92 AliRsnValue *axis = 0;
13f28255 93
2a1c7696 94 while ((axis = (AliRsnValue*)next())) {
95 if (name.Length() > 1) name += '_';
96 name += axis->GetName();
97 }
13f28255 98
2a1c7696 99 return name.Data();
5eb970a4 100}
101
b2028424 102//________________________________________________________________________________________
32992791 103Bool_t AliRsnFunction::AddAxis(AliRsnValue *const axis)
5eb970a4 104{
32992791 105//
106// Try to add a new axis to this function.
107// The operation succeeds only if the related value object
11ed73f6 108// has a target compatible with the function type:
109// -- 'single' functions, for tracks/events: AliRsnDaughter or AliRsnEvent
110// -- 'not single' functions, for pairs/events : AliRsnMother or AliRsnEvent
32992791 111// otherwise the axis is not added.
112//
113// If more than 3 axes are added, switch to THnSparseF output.
114// NOTE: this can cause large files and high memory occupancy.
115//
116
2a1c7696 117 Int_t size = fAxisList.GetEntries();
d7712d44 118 new (fAxisList[size]) AliRsnValue(*axis);
2a1c7696 119
120 if (fAxisList.GetEntries() > 3) {
121 AliWarning("Adding more than 3 axes will produce a THnSparseD output.");
122 fUseTH1 = kFALSE;
123 }
124
125 return kTRUE;
13f28255 126}
127
128//________________________________________________________________________________________
eb079724 129TH1* AliRsnFunction::CreateHistogram(const char *histoName, const char *histoTitle)
13f28255 130{
5eb970a4 131//
132// Creates and returns the histogram defined using
133// arguments fo name and title, and the first histoDef for binning.
134// Variable-sized histogram binning is always called, due to use of histoDef,
135// even if the bins are equal, since they are defined in this class.
136// Eventually present histoDef's in other slots of array (1, 2) are ignored.
eb079724 137//
0d73200d 138// This version produces a THnSparseF.
5eb970a4 139//
13f28255 140
2a1c7696 141 fSize = fAxisList.GetEntries();
142 if (!fSize) {
143 AliError("No axes defined");
144 return 0x0;
145 } else if (fSize > 3) {
146 AliError("Too many axes defined for a TH1 object");
147 return 0x0;
148 }
d7712d44 149
150 // initialize the size of values container
151 fValues.Set(fSize);
2a1c7696 152
153 // retrieve binnings for main and secondary axes
154 AliRsnValue *fcnAxis;
155 TArrayD array[3];
156 for (Int_t i = 0; i < fSize; i++) {
157 fcnAxis = (AliRsnValue*)fAxisList.At(i);
158 if (!fcnAxis) {
159 AliError("Empty axis");
160 array[i].Set(2);
161 array[i][0] = -1E5;
162 array[i][1] = -1E5;
163 continue;
164 } else {
165 array[i] = fcnAxis->GetArray();
166 }
167 }
168
169 // create histogram depending on the number of axes
170 switch (fSize) {
171 case 1:
172 fH1 = new TH1F(histoName, histoTitle, array[0].GetSize() - 1, array[0].GetArray());
173 break;
174 case 2:
175 fH1 = new TH2F(histoName, histoTitle, array[0].GetSize() - 1, array[0].GetArray(), array[1].GetSize() - 1, array[1].GetArray());
176 break;
177 case 3:
178 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());
179 break;
180 }
181 fH1->Sumw2();
182
183 return fH1;
eb079724 184}
185
186//________________________________________________________________________________________
0d73200d 187THnSparseF* AliRsnFunction::CreateHistogramSparse(const char *histoName, const char *histoTitle)
eb079724 188{
189//
190// Creates and returns the histogram defined using
191// arguments fo name and title, and the first histoDef for binning.
192// Variable-sized histogram binning is always called, due to use of histoDef,
193// even if the bins are equal, since they are defined in this class.
194// Eventually present histoDef's in other slots of array (1, 2) are ignored.
195//
0d73200d 196// This version produces a THnSparseF.
eb079724 197//
198
2a1c7696 199 fSize = fAxisList.GetEntries();
200 if (!fSize) {
201 AliError("No axes defined");
202 return 0x0;
203 }
d7712d44 204
205 // initialize the size of values container
206 fValues.Set(fSize);
2a1c7696 207
208 // initialize the array of number of bins for each axis
209 // taking it from the stored values, while for the bins
210 // they are set as summied and defined later
2a1c7696 211 Int_t *nbins = new Int_t[fSize];
212 AliRsnValue *fcnAxis = 0;
213 for (Int_t i = 0; i < fSize; i++) {
214 fcnAxis = (AliRsnValue*)fAxisList.At(i);
215 if (!fcnAxis) {
216 nbins[i] = 1;
217 AliError("Empty axis");
218 continue;
219 }
220 nbins[i] = fcnAxis->GetArray().GetSize() - 1;
221 }
222
223 // create histogram
d7712d44 224 fHSparse = new THnSparseF(histoName, histoTitle, fSize, nbins);
2a1c7696 225 fHSparse->Sumw2();
226
227 // update the various axes using the definitions given in the array of axes here
228 for (Int_t i = 0; i < fSize; i++) {
229 fcnAxis = (AliRsnValue*)fAxisList.At(i);
230 if (!fcnAxis) {
231 AliError("Empty axis: doing unique bin betweeen -100000 and 100000");
232 continue;
233 }
d7712d44 234 TAxis* axis = fHSparse->GetAxis(i);
235 axis->Set(nbins[i], fcnAxis->GetArray().GetArray());
2a1c7696 236 }
237
238 delete [] nbins;
239
240 return fHSparse;
5eb970a4 241}
242
eb079724 243
5eb970a4 244//________________________________________________________________________________________
d7712d44 245Bool_t AliRsnFunction::Fill(TObject *object)
5eb970a4 246{
247//
d7712d44 248// Fill function histogram using the passed object
5eb970a4 249//
b2028424 250
2a1c7696 251 AliDebug(AliLog::kDebug + 2, "->");
252
d7712d44 253 // loop on axes and try to compute values
254 // using this object or, as an alternative
255 // its reference event
2a1c7696 256 Int_t i;
d7712d44 257 Bool_t globalOK = kTRUE, computeOK;
2a1c7696 258 AliRsnValue *fcnAxis = 0;
259 for (i = 0; i < fSize; i++) {
d7712d44 260 fValues[i] = 0.0;
261 computeOK = kFALSE;
2a1c7696 262 fcnAxis = (AliRsnValue*)fAxisList.At(i);
d7712d44 263 if (fcnAxis) {
264 computeOK = fcnAxis->Eval(object);
265 if (computeOK) fValues[i] = ((Float_t)fcnAxis->GetComputedValue());
2a1c7696 266 }
d7712d44 267 if (!computeOK) globalOK = kFALSE;
2a1c7696 268 }
d7712d44 269
270 // if even one of the computations has failes, the histograms are not filled
271 if (!globalOK) return kFALSE;
2a1c7696 272
273 // fill histogram
274 if (fUseTH1) {
d7712d44 275
2a1c7696 276 // check presence of output histogram
277 if (!fH1) {
278 AliError("Required a TH1 which is not initialized");
2a1c7696 279 return kFALSE;
280 }
281
282 // fill according to dimensions
283 switch (fSize) {
284 case 1: {
285 TH1F *h1 = (TH1F*)fH1;
d7712d44 286 h1->Fill(fValues[0]);
2a1c7696 287 }
288 break;
289 case 2: {
290 TH2F *h2 = (TH2F*)fH1;
d7712d44 291 h2->Fill(fValues[0], fValues[1]);
2a1c7696 292 }
293 break;
294 case 3: {
295 TH3F *h3 = (TH3F*)fH1;
d7712d44 296 h3->Fill(fValues[0], fValues[1], fValues[2]);
2a1c7696 297 }
298 break;
299 default:
300 AliError(Form("Wrong size : %d", fSize));
2a1c7696 301 return kFALSE;
302 }
303 } else {
d7712d44 304
2a1c7696 305 // check presence of output histogram
306 if (!fHSparse) {
307 AliError("Required a THnSparseF which is not initialized");
2a1c7696 308 return kFALSE;
309 }
310
d7712d44 311 fHSparse->Fill(fValues.GetArray());
2a1c7696 312 }
313
2a1c7696 314 AliDebug(AliLog::kDebug + 2, "->");
315 return kTRUE;
13f28255 316}