]> git.uio.no Git - u/mrichter/AliRoot.git/blame - PWG2/RESONANCES/AliRsnFunction.cxx
fix for bug #70582 (change from L. Molnar)
[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>
23
24#include "AliLog.h"
25
13f28255 26#include "AliRsnDaughter.h"
27#include "AliRsnEvent.h"
28#include "AliRsnPairDef.h"
4fbb2459 29#include "AliRsnPairParticle.h"
30#include "AliRsnFunctionAxis.h"
13f28255 31
32#include "AliRsnFunction.h"
33
34ClassImp(AliRsnFunction)
35
36//________________________________________________________________________________________
eb079724 37AliRsnFunction::AliRsnFunction(Bool_t useTH1) :
4fbb2459 38 TNamed(),
39 fPairDef(0x0),
40 fAxisList("AliRsnFunctionAxis", 0),
41 fTrack(0x0),
42 fPair(0x0),
43 fEvent(0x0),
eb079724 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) :
4fbb2459 56 TNamed(copy),
57 fPairDef(copy.fPairDef),
58 fAxisList(copy.fAxisList),
59 fTrack(copy.fTrack),
60 fPair(copy.fPair),
61 fEvent(copy.fEvent),
eb079724 62 fUseTH1(copy.fUseTH1),
63 fSize(copy.fSize),
64 fH1(0x0),
65 fHSparse(0x0)
13f28255 66{
5eb970a4 67//
68// Copy constructor.
69//
13f28255 70}
71
72//________________________________________________________________________________________
5eb970a4 73const AliRsnFunction& AliRsnFunction::operator=(const AliRsnFunction& copy)
13f28255 74{
5eb970a4 75//
76// Assignment operator.
77//
78
79 SetName(copy.GetName());
80 SetTitle(copy.GetTitle());
81
5eb970a4 82 fPairDef = copy.fPairDef;
5eb970a4 83 fTrack = copy.fTrack;
84 fPair = copy.fPair;
85 fEvent = copy.fEvent;
eb079724 86 fUseTH1 = copy.fUseTH1;
87 fSize = copy.fSize;
5eb970a4 88
eb079724 89 if (fH1) delete fH1;
90 fH1 = 0x0;
91
92 if (fHSparse) delete fHSparse;
93 fHSparse = 0x0;
5eb970a4 94
5eb970a4 95 return (*this);
13f28255 96}
97
98//________________________________________________________________________________________
b2028424 99const char* AliRsnFunction::GetName() const
13f28255 100{
5eb970a4 101//
102// Defines the name of this object according to
103// the function type and binning
104//
13f28255 105
b2028424 106 TString name("");
e0baff8c 107
b2028424 108 TObjArrayIter next(&fAxisList);
109 AliRsnFunctionAxis *axis = 0;
13f28255 110
4fbb2459 111 while ((axis = (AliRsnFunctionAxis*)next())) {
b2028424 112 if (name.Length() > 1) name += '_';
113 name += axis->GetName();
13f28255 114 }
13f28255 115
b2028424 116 return name.Data();
5eb970a4 117}
118
b2028424 119//________________________________________________________________________________________
4fbb2459 120void AliRsnFunction::AddAxis(AliRsnFunctionAxis *const axis)
5eb970a4 121{
b2028424 122 Int_t size = fAxisList.GetEntries();
eb079724 123 if (size >= 3 && fUseTH1)
124 {
125 AliWarning("A TH1-type output cannot add more than 3 axes: switching to THnSparse -- THIS COULD CAUSE VERY LARGE FILES!!!");
126 fUseTH1 = kFALSE;
127 }
4fbb2459 128 new(fAxisList[size]) AliRsnFunctionAxis(*axis);
13f28255 129}
130
131//________________________________________________________________________________________
eb079724 132TH1* AliRsnFunction::CreateHistogram(const char *histoName, const char *histoTitle)
13f28255 133{
5eb970a4 134//
135// Creates and returns the histogram defined using
136// arguments fo name and title, and the first histoDef for binning.
137// Variable-sized histogram binning is always called, due to use of histoDef,
138// even if the bins are equal, since they are defined in this class.
139// Eventually present histoDef's in other slots of array (1, 2) are ignored.
eb079724 140//
141// This version produces a THnSparseD.
5eb970a4 142//
13f28255 143
eb079724 144 fSize = fAxisList.GetEntries();
145 if (!fSize) {
146 AliError("No axes defined");
147 return 0x0;
148 }
149 else if (fSize < 1 || fSize > 3)
150 {
151 AliError("Too few or too many axes defined");
152 return 0x0;
153 }
154
155 Int_t *nbins = new Int_t [fSize];
156 Double_t *min = new Double_t[fSize];
157 Double_t *max = new Double_t[fSize];
158
159 // retrieve binnings for main and secondary axes
160 AliRsnFunctionAxis *fcnAxis = 0;
161 for (Int_t i = 0; i < fSize; i++) {
162 fcnAxis = (AliRsnFunctionAxis*)fAxisList.At(i);
163 if (!fcnAxis) {
164 nbins[i] = 0;
165 min[i] = 0.0;
166 max[i] = 0.0;
167 AliError("Empty axis");
168 continue;
169 }
170 nbins[i] = fcnAxis->GetNBins();
171 min[i] = fcnAxis->GetMin();
172 max[i] = fcnAxis->GetMax();
173 }
174
175 // create histogram depending on the number of axes
176 switch (fSize)
177 {
178 case 1:
179 fH1 = new TH1D(histoName, histoTitle, nbins[0], min[0], max[0]);
180 break;
181 case 2:
182 fH1 = new TH2D(histoName, histoTitle, nbins[0], min[0], max[0], nbins[1], min[1], max[1]);
183 break;
184 case 3:
185 fH1 = new TH3D(histoName, histoTitle, nbins[0], min[0], max[0], nbins[1], min[1], max[1], nbins[2], min[2], max[2]);
186 break;
187 }
188 fH1->Sumw2();
189
190 return fH1;
191}
192
193//________________________________________________________________________________________
194THnSparseD* AliRsnFunction::CreateHistogramSparse(const char *histoName, const char *histoTitle)
195{
196//
197// Creates and returns the histogram defined using
198// arguments fo name and title, and the first histoDef for binning.
199// Variable-sized histogram binning is always called, due to use of histoDef,
200// even if the bins are equal, since they are defined in this class.
201// Eventually present histoDef's in other slots of array (1, 2) are ignored.
202//
203// This version produces a THnSparseD.
204//
205
206 fSize = fAxisList.GetEntries();
207 if (!fSize) {
b2028424 208 AliError("No axes defined");
209 return 0x0;
210 }
211
eb079724 212 Int_t *nbins = new Int_t [fSize];
213 Double_t *min = new Double_t[fSize];
214 Double_t *max = new Double_t[fSize];
5eb970a4 215
216 // retrieve binnings for main and secondary axes
b2028424 217 AliRsnFunctionAxis *fcnAxis = 0;
eb079724 218 for (Int_t i = 0; i < fSize; i++) {
b2028424 219 fcnAxis = (AliRsnFunctionAxis*)fAxisList.At(i);
220 if (!fcnAxis) {
221 nbins[i] = 0;
eb079724 222 min[i] = 0.0;
223 max[i] = 0.0;
b2028424 224 AliError("Empty axis");
225 continue;
5eb970a4 226 }
b2028424 227 nbins[i] = fcnAxis->GetNBins();
eb079724 228 min[i] = fcnAxis->GetMin();
229 max[i] = fcnAxis->GetMax();
230 }
231
232 Int_t size = fAxisList.GetEntries();
233 if (!size) {
234 AliError("No axes defined");
235 return 0x0;
5eb970a4 236 }
8a22fe6f 237
b2028424 238 // create histogram
eb079724 239 fHSparse = new THnSparseD(histoName, histoTitle, size, nbins, min, max);
240 fHSparse->Sumw2();
241
242 // clean heap
243 delete [] nbins;
244 delete [] min;
245 delete [] max;
5eb970a4 246
eb079724 247 return fHSparse;
5eb970a4 248}
249
eb079724 250
5eb970a4 251//________________________________________________________________________________________
252Bool_t AliRsnFunction::Fill()
253{
254//
255// Fill function histogram with values computed from given input object.
256//
b2028424 257
5eb970a4 258 AliDebug(AliLog::kDebug +2,"->");
259
eb079724 260 Int_t i;
261 Double_t *values = new Double_t[fSize];
b2028424 262
263 AliRsnFunctionAxis *fcnAxis = 0;
eb079724 264 for (i = 0; i < fSize; i++) {
b2028424 265 fcnAxis = (AliRsnFunctionAxis*)fAxisList.At(i);
266 if (!fcnAxis) {
267 values[i] = 0.0;
268 continue;
269 }
4fbb2459 270 switch (fcnAxis->GetAxisObject()) {
271 case AliRsnFunctionAxis::kParticle:
272 values[i] = fcnAxis->Eval(fTrack);
273 break;
274 case AliRsnFunctionAxis::kPair:
275 values[i] = fcnAxis->Eval(fPair, fPairDef);
276 break;
277 case AliRsnFunctionAxis::kEvent:
278 values[i] = fcnAxis->Eval(fEvent);
279 break;
280 default:
281 values[i] = 0.0;
b2028424 282 }
5eb970a4 283 }
eb079724 284
285 // fill histogram
286 if (fUseTH1)
287 {
288 // check presence of output histogram
289 if (!fH1) {
290 AliError("Required a TH1 whish is not initialized");
291 return kFALSE;
292 }
293
294 // fill according to dimensions
295 switch (fSize)
296 {
297 case 1:
298 {
299 TH1D *h1 = (TH1D*)fH1;
300 h1->Fill(values[0]);
301 }
302 break;
303 case 2:
304 {
305 TH2D *h2 = (TH2D*)fH1;
306 h2->Fill(values[0], values[1]);
307 }
308 break;
309 case 3:
310 {
311 TH3D *h3 = (TH3D*)fH1;
312 h3->Fill(values[0], values[1], values[2]);
313 }
314 break;
315 default:
316 AliError(Form("Wrong size : %d", fSize));
317 return kFALSE;
318 }
319 }
320 else
321 {
322 // check presence of output histogram
323 if (!fHSparse) {
324 AliError("Required a THnSparseD whish is not initialized");
325 return kFALSE;
326 }
327
328 fHSparse->Fill(values);
5eb970a4 329 }
eb079724 330
331 delete [] values;
e0baff8c 332
5eb970a4 333 AliDebug(AliLog::kDebug +2,"->");
334 return kTRUE;
13f28255 335}