AliPhysics  a8fcd8c (a8fcd8c)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AliTHn.cxx
Go to the documentation of this file.
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  * *
4  * Author: The ALICE Off-line Project. *
5  * Contributors are mentioned in the code where appropriate. *
6  * *
7  * Permission to use, copy, modify and distribute this software and its *
8  * documentation strictly for non-commercial purposes is hereby granted *
9  * without fee, provided that the above copyright notice appears in all *
10  * copies and that both the copyright notice and this permission notice *
11  * appear in the supporting documentation. The authors make no claims *
12  * about the suitability of this software for any purpose. It is *
13  * provided "as is" without express or implied warranty. *
14  **************************************************************************/
15 
16 /* $Id: AliTHnT.cxx 20164 2007-08-14 15:31:50Z morsch $ */
17 
18 // Use AliTHnT instead of AliCFContainer and your memory consumption will be drastically reduced
19 // As AliTHnT derives from AliCFContainer, you can just replace your current AliCFContainer object by AliTHnT
20 // Once you have the merged output, call FillParent() and you can use AliCFContainer as usual
21 //
22 // this storage container is optimized for small memory usage
23 // under/over flow bins do not exist
24 // sumw2 structure is float only and only create when the weight != 1
25 // all histogram functionality (projections, axis, ranges, etc) are taken from THnSparse by propagating
26 // the information up into the THnSparse structure (in the ananalysis *after* data processing and merging)
27 //
28 // the derivation from THnSparse is obviously against many OO rules. correct would be a common baseclass of THnSparse and THn.
29 //
30 // Templated version allows also the use of double as storage container
31 //
32 // Author: Jan Fiete Grosse-Oetringhaus
33 
34 #include "AliTHn.h"
35 #include "TList.h"
36 #include "TCollection.h"
37 #include "AliLog.h"
38 #include "TArrayF.h"
39 #include "TArrayD.h"
40 #include "THnSparse.h"
41 #include "TMath.h"
42 
44 
45 template <class TemplateArray, typename TemplateType>
46 AliTHnT<TemplateArray, TemplateType>::AliTHnT() :
47  AliTHnBase(),
48  fNBins(0),
49  fNVars(0),
50  fNSteps(0),
51  fValues(0),
52  fSumw2(0),
53  axisCache(0),
54  fNbinsCache(0),
55  fLastVars(0),
56  fLastBins(0)
57 {
58  // Constructor
59 }
60 
61 template <class TemplateArray, typename TemplateType>
62 AliTHnT<TemplateArray, TemplateType>::AliTHnT(const Char_t* name, const Char_t* title,const Int_t nSelStep, const Int_t nVarIn, const Int_t* nBinIn) :
63  AliTHnBase(name, title, nSelStep, nVarIn, nBinIn),
64  fNBins(0),
65  fNVars(nVarIn),
66  fNSteps(nSelStep),
67  fValues(0),
68  fSumw2(0),
69  axisCache(0),
70  fNbinsCache(0),
71  fLastVars(0),
72  fLastBins(0)
73 {
74  // Constructor
75 
76  fNBins = 1;
77  for (Int_t i=0; i<fNVars; i++)
78  fNBins *= nBinIn[i];
79 
80  Init();
81 }
82 
83 template <class TemplateArray, typename TemplateType>
85 {
86  // initialize
87 
88  fValues = new TemplateArray*[fNSteps];
89  fSumw2 = new TemplateArray*[fNSteps];
90 
91  for (Int_t i=0; i<fNSteps; i++)
92  {
93  fValues[i] = 0;
94  fSumw2[i] = 0;
95  }
96 }
97 
98 template <class TemplateArray, typename TemplateType>
100  AliTHnBase(c),
101  fNBins(c.fNBins),
102  fNVars(c.fNVars),
103  fNSteps(c.fNSteps),
104  fValues(new TemplateArray*[c.fNSteps]),
105  fSumw2(new TemplateArray*[c.fNSteps]),
106  axisCache(0),
107  fNbinsCache(0),
108  fLastVars(0),
109  fLastBins(0)
110 {
111  //
112  // AliTHnT copy constructor
113  //
114 
115  memset(fValues,0,fNSteps*sizeof(TemplateArray*));
116  memset(fSumw2,0,fNSteps*sizeof(TemplateArray*));
117 
118  for (Int_t i=0; i<fNSteps; i++) {
119  if (c.fValues[i]) fValues[i] = new TemplateArray(*(c.fValues[i]));
120  if (c.fSumw2[i]) fSumw2[i] = new TemplateArray(*(c.fSumw2[i]));
121  }
122 
123 }
124 
125 template <class TemplateArray, typename TemplateType>
127 {
128  // Destructor
129 
130  DeleteContainers();
131 
132  delete[] fValues;
133  delete[] fSumw2;
134  delete[] axisCache;
135  delete[] fNbinsCache;
136  delete[] fLastVars;
137  delete[] fLastBins;
138 }
139 
140 template <class TemplateArray, typename TemplateType>
142 {
143  // delete data containers
144 
145  for (Int_t i=0; i<fNSteps; i++)
146  {
147  if (fValues && fValues[i])
148  {
149  delete fValues[i];
150  fValues[i] = 0;
151  }
152 
153  if (fSumw2 && fSumw2[i])
154  {
155  delete fSumw2[i];
156  fSumw2[i] = 0;
157  }
158  }
159 }
160 
161 //____________________________________________________________________
162 template <class TemplateArray, typename TemplateType>
164 {
165  // assigment operator
166 
167  if (this != &c) {
168  AliCFContainer::operator=(c);
169  fNBins=c.fNBins;
170  fNVars=c.fNVars;
171  if(fNSteps) {
172  for(Int_t i=0; i< fNSteps; ++i) {
173  delete fValues[i];
174  delete fSumw2[i];
175  }
176  delete [] fValues;
177  delete [] fSumw2;
178  }
179  fNSteps=c.fNSteps;
180  if(fNSteps) {
181  fValues=new TemplateArray*[fNSteps];
182  fSumw2=new TemplateArray*[fNSteps];
183  memset(fValues,0,fNSteps*sizeof(TemplateArray*));
184  memset(fSumw2,0,fNSteps*sizeof(TemplateArray*));
185 
186  for (Int_t i=0; i<fNSteps; i++) {
187  if (c.fValues[i]) fValues[i] = new TemplateArray(*(c.fValues[i]));
188  if (c.fSumw2[i]) fSumw2[i] = new TemplateArray(*(c.fSumw2[i]));
189  }
190  } else {
191  fValues = 0;
192  fSumw2 = 0;
193  }
194  delete [] axisCache;
195  axisCache = new TAxis*[fNVars];
196  memcpy(axisCache, c.axisCache, fNVars*sizeof(TAxis*));
197  }
198  return *this;
199 }
200 
201 //____________________________________________________________________
202 template <class TemplateArray, typename TemplateType>
204 {
205  // copy function
206 
207  AliTHnT& target = (AliTHnT &) c;
208 
209  AliCFContainer::Copy(target);
210 
211  target.fNSteps = fNSteps;
212  target.fNBins = fNBins;
213  target.fNVars = fNVars;
214 
215  target.Init();
216 
217  for (Int_t i=0; i<fNSteps; i++)
218  {
219  if (fValues[i])
220  target.fValues[i] = new TemplateArray(*(fValues[i]));
221  else
222  target.fValues[i] = 0;
223 
224  if (fSumw2[i])
225  target.fSumw2[i] = new TemplateArray(*(fSumw2[i]));
226  else
227  target.fSumw2[i] = 0;
228  }
229 }
230 
231 //____________________________________________________________________
232 template <class TemplateArray, typename TemplateType>
234 {
235  // Merge a list of AliTHnT objects with this (needed for
236  // PROOF).
237  // Returns the number of merged objects (including this).
238 
239  if (!list)
240  return 0;
241 
242  if (list->IsEmpty())
243  return 1;
244 
245  AliCFContainer::Merge(list);
246 
247  TIterator* iter = list->MakeIterator();
248  TObject* obj;
249 
250  Int_t count = 0;
251  while ((obj = iter->Next())) {
252 
253  AliTHnT* entry = dynamic_cast<AliTHnT*> (obj);
254  if (entry == 0)
255  continue;
256 
257  for (Int_t i=0; i<fNSteps; i++)
258  {
259  if (entry->fValues[i])
260  {
261  if (!fValues[i])
262  fValues[i] = new TemplateArray(fNBins);
263 
264  for (Long64_t l = 0; l<fNBins; l++)
265  fValues[i]->GetArray()[l] += entry->fValues[i]->GetArray()[l];
266  }
267 
268  if (entry->fSumw2[i])
269  {
270  if (!fSumw2[i])
271  fSumw2[i] = new TemplateArray(fNBins);
272 
273  for (Long64_t l = 0; l<fNBins; l++)
274  fSumw2[i]->GetArray()[l] += entry->fSumw2[i]->GetArray()[l];
275  }
276  }
277 
278  count++;
279  }
280 
281  return count+1;
282 }
283 
284 template <class TemplateArray, typename TemplateType>
286 {
287  // fills an entry
288 
289  // fill axis cache
290  if (!axisCache)
291  {
292  axisCache = new TAxis*[fNVars];
293  fNbinsCache = new Int_t[fNVars];
294  for (Int_t i=0; i<fNVars; i++)
295  {
296  axisCache[i] = GetAxis(i, 0);
297  fNbinsCache[i] = axisCache[i]->GetNbins();
298  }
299 
300  fLastVars = new Double_t[fNVars];
301  fLastBins = new Int_t[fNVars];
302 
303  // initial values to prevent checking for 0 below
304  for (Int_t i=0; i<fNVars; i++)
305  {
306  fLastBins[i] = axisCache[i]->FindBin(var[i]);
307  fLastVars[i] = var[i];
308  }
309  }
310 
311  // calculate global bin index
312  Long64_t bin = 0;
313  for (Int_t i=0; i<fNVars; i++)
314  {
315  bin *= fNbinsCache[i];
316 
317  Int_t tmpBin = 0;
318  if (fLastVars[i] == var[i])
319  tmpBin = fLastBins[i];
320  else
321  {
322  tmpBin = axisCache[i]->FindBin(var[i]);
323  fLastBins[i] = tmpBin;
324  fLastVars[i] = var[i];
325  }
326  //Printf("%d", tmpBin);
327 
328  // under/overflow not supported
329  if (tmpBin < 1 || tmpBin > fNbinsCache[i])
330  return;
331 
332  // bins start from 0 here
333  bin += tmpBin - 1;
334 // Printf("%lld", bin);
335  }
336 
337  if (!fValues[istep])
338  {
339  fValues[istep] = new TemplateArray(fNBins);
340  AliInfo(Form("Created values container for step %d", istep));
341  }
342 
343  if (weight != 1)
344  {
345  // initialize with already filled entries (which have been filled with weight == 1), in this case fSumw2 := fValues
346  if (!fSumw2[istep])
347  {
348  fSumw2[istep] = new TemplateArray(*fValues[istep]);
349  AliInfo(Form("Created sumw2 container for step %d", istep));
350  }
351  }
352 
353  fValues[istep]->GetArray()[bin] += weight;
354  if (fSumw2[istep])
355  fSumw2[istep]->GetArray()[bin] += weight * weight;
356 
357 // Printf("%f", fValues[istep][bin]);
358 
359  // debug
360 // AliCFContainer::Fill(var, istep, weight);
361 }
362 
363 template <class TemplateArray, typename TemplateType>
365 {
366  // calculates global bin index
367  // binIdx contains TAxis bin indexes
368  // here bin count starts at 0 because we do not have over/underflow bins
369 
370  Long64_t bin = 0;
371  for (Int_t i=0; i<fNVars; i++)
372  {
373  bin *= GetAxis(i, 0)->GetNbins();
374  bin += binIdx[i] - 1;
375  }
376 
377  return bin;
378 }
379 
380 template <class TemplateArray, typename TemplateType>
382 {
383  // fills the information stored in the buffer in this class into the container <cont>
384 
385  for (Int_t i=0; i<fNSteps; i++)
386  {
387  if (!fValues[i])
388  continue;
389 
390  TemplateType* source = fValues[i]->GetArray();
391  // if fSumw2 is not stored, the sqrt of the number of bin entries in source is filled below; otherwise we use fSumw2
392  TemplateType* sourceSumw2 = source;
393  if (fSumw2[i])
394  sourceSumw2 = fSumw2[i]->GetArray();
395 
396  THnSparse* target = cont->GetGrid(i)->GetGrid();
397 
398  Int_t* binIdx = new Int_t[fNVars];
399  Int_t* nBins = new Int_t[fNVars];
400  for (Int_t j=0; j<fNVars; j++)
401  {
402  binIdx[j] = 1;
403  nBins[j] = target->GetAxis(j)->GetNbins();
404  }
405 
406  Long64_t count = 0;
407 
408  while (1)
409  {
410 // for (Int_t j=0; j<fNVars; j++)
411 // printf("%d ", binIdx[j]);
412 
413  Long64_t globalBin = GetGlobalBinIndex(binIdx);
414 // Printf(" --> %lld", globalBin);
415 
416  if (source[globalBin] != 0)
417  {
418  target->SetBinContent(binIdx, source[globalBin]);
419  target->SetBinError(binIdx, TMath::Sqrt(sourceSumw2[globalBin]));
420 
421  count++;
422  }
423 
424  binIdx[fNVars-1]++;
425 
426  for (Int_t j=fNVars-1; j>0; j--)
427  {
428  if (binIdx[j] > nBins[j])
429  {
430  binIdx[j] = 1;
431  binIdx[j-1]++;
432  }
433  }
434 
435  if (binIdx[0] > nBins[0])
436  break;
437  }
438 
439  AliInfo(Form("Step %d: copied %lld entries out of %lld bins", i, count, GetGlobalBinIndex(binIdx)));
440 
441  delete[] binIdx;
442  delete[] nBins;
443  }
444 }
445 
446 template <class TemplateArray, typename TemplateType>
448 {
449  // fills the information stored in the buffer in this class into the baseclass containers
450 
451  FillContainer(this);
452 }
453 
454 template <class TemplateArray, typename TemplateType>
456 {
457  // "removes" one axis by summing over the axis and putting the entry to bin 1
458  // TODO presently only implemented for the last axis
459 
460  Int_t axis = fNVars-1;
461 
462  for (Int_t i=0; i<fNSteps; i++)
463  {
464  if (!fValues[i])
465  continue;
466 
467  TemplateType* source = fValues[i]->GetArray();
468  TemplateType* sourceSumw2 = 0;
469  if (fSumw2[i])
470  sourceSumw2 = fSumw2[i]->GetArray();
471 
472  THnSparse* target = GetGrid(i)->GetGrid();
473 
474  Int_t* binIdx = new Int_t[fNVars];
475  Int_t* nBins = new Int_t[fNVars];
476  for (Int_t j=0; j<fNVars; j++)
477  {
478  binIdx[j] = 1;
479  nBins[j] = target->GetAxis(j)->GetNbins();
480  }
481 
482  Long64_t count = 0;
483 
484  while (1)
485  {
486  // sum over axis <axis>
487  TemplateType sumValues = 0;
488  TemplateType sumSumw2 = 0;
489  for (Int_t j=1; j<=nBins[axis]; j++)
490  {
491  binIdx[axis] = j;
492  Long64_t globalBin = GetGlobalBinIndex(binIdx);
493  sumValues += source[globalBin];
494  source[globalBin] = 0;
495 
496  if (sourceSumw2)
497  {
498  sumSumw2 += sourceSumw2[globalBin];
499  sourceSumw2[globalBin] = 0;
500  }
501  }
502  binIdx[axis] = 1;
503 
504  Long64_t globalBin = GetGlobalBinIndex(binIdx);
505  source[globalBin] = sumValues;
506  if (sourceSumw2)
507  sourceSumw2[globalBin] = sumSumw2;
508 
509  count++;
510 
511  // next bin
512  binIdx[fNVars-2]++;
513 
514  for (Int_t j=fNVars-2; j>0; j--)
515  {
516  if (binIdx[j] > nBins[j])
517  {
518  binIdx[j] = 1;
519  binIdx[j-1]++;
520  }
521  }
522 
523  if (binIdx[0] > nBins[0])
524  break;
525  }
526 
527  AliInfo(Form("Step %d: reduced %lld bins to %lld entries", i, GetGlobalBinIndex(binIdx), count));
528 
529  delete[] binIdx;
530  delete[] nBins;
531  }
532 }
533 
534 template class AliTHnT<TArrayF, Float_t>;
535 template class AliTHnT<TArrayD, Double_t>;
double Double_t
Definition: External.C:58
templateClassImp(AliTHnT) template< class TemplateArray
const char * title
Definition: MakeQAPdf.C:27
virtual void FillParent()
Definition: AliTHn.cxx:447
virtual Long64_t Merge(TCollection *list)
Definition: AliTHn.cxx:233
long long Long64_t
Definition: External.C:43
virtual void DeleteContainers()
Definition: AliTHn.cxx:141
char Char_t
Definition: External.C:18
TList * list
TDirectory file where lists per trigger are stored in train ouput.
TCanvas * c
Definition: TestFitELoss.C:172
TAxis * GetAxis(TDirectory *dir, const char *name, Bool_t verbose=true)
virtual void Copy(TObject &c) const
Definition: AliTHn.cxx:203
TemplateArray ** fSumw2
Definition: AliTHn.h:74
Long64_t GetGlobalBinIndex(const Int_t *binIdx)
Definition: AliTHn.cxx:364
int Int_t
Definition: External.C:63
TemplateArray ** fValues
Definition: AliTHn.h:73
Definition: AliTHn.h:42
Int_t fNSteps
Definition: AliTHn.h:72
virtual void FillContainer(AliCFContainer *cont)
Definition: AliTHn.cxx:381
virtual void Fill(const Double_t *var, Int_t istep, Double_t weight=1.)
Definition: AliTHn.cxx:285
AliTHnT & operator=(const AliTHnT &corr)
Definition: AliTHn.cxx:163
virtual ~AliTHnT()
Definition: AliTHn.cxx:126
TAxis ** axisCache
Definition: AliTHn.h:76
TH1 * Merge(const TH1 *cen, const TH1 *fwd, Double_t &xlow, Double_t &xhigh)
Int_t fNVars
Definition: AliTHn.h:71
Long64_t fNBins
Definition: AliTHn.h:70
AliTHnT()
Definition: AliTHn.cxx:46
void Init()
virtual void ReduceAxis()
Definition: AliTHn.cxx:455