1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
16 /* $Id: AliTHn.cxx 20164 2007-08-14 15:31:50Z morsch $ */
19 // this storage container is optimized for small memory usage
20 // under/over flow bins do not exist
21 // sumw2 structure is float only and only create when the weight != 1
22 // all histogram functionality (projections, axis, ranges, etc) are taken from THnSparse by propagating
23 // the information up into the THnSparse structure (in the ananalysis *after* data processing and merging)
25 // the derivation from THnSparse is obviously against many OO rules. correct would be a common baseclass of THnSparse and THn.
27 // Author: Jan Fiete Grosse-Oetringhaus
31 #include "TCollection.h"
34 #include "THnSparse.h"
51 AliTHn::AliTHn(const Char_t* name, const Char_t* title,const Int_t nSelStep, const Int_t nVarIn, const Int_t* nBinIn) :
52 AliCFContainer(name, title, nSelStep, nVarIn, nBinIn),
63 for (Int_t i=0; i<fNVars; i++)
73 fValues = new TArrayF*[fNSteps];
74 fSumw2 = new TArrayF*[fNSteps];
76 for (Int_t i=0; i<fNSteps; i++)
83 AliTHn::AliTHn(const AliTHn &c) :
88 fValues(new TArrayF*[c.fNSteps]),
89 fSumw2(new TArrayF*[c.fNSteps]),
93 // AliTHn copy constructor
96 memset(fValues,0,fNSteps*sizeof(TArrayF*));
97 memset(fSumw2,0,fNSteps*sizeof(TArrayF*));
99 for (Int_t i=0; i<fNSteps; i++) {
100 if (c.fValues[i]) fValues[i] = new TArrayF(*(c.fValues[i]));
101 if (c.fSumw2[i]) fSumw2[i] = new TArrayF(*(c.fSumw2[i]));
118 void AliTHn::DeleteContainers()
120 // delete data containers
122 for (Int_t i=0; i<fNSteps; i++)
124 if (fValues && fValues[i])
130 if (fSumw2 && fSumw2[i])
138 //____________________________________________________________________
139 AliTHn &AliTHn::operator=(const AliTHn &c)
141 // assigment operator
144 AliCFContainer::operator=(c);
151 for(Int_t i=0; i< fNSteps; ++i) {
158 fValues=new TArrayF*[fNSteps];
159 fSumw2=new TArrayF*[fNSteps];
160 memset(fValues,0,fNSteps*sizeof(TArrayF*));
161 memset(fSumw2,0,fNSteps*sizeof(TArrayF*));
163 for (Int_t i=0; i<fNSteps; i++) {
164 if (c.fValues[i]) fValues[i] = new TArrayF(*(c.fValues[i]));
165 if (c.fSumw2[i]) fSumw2[i] = new TArrayF(*(c.fSumw2[i]));
174 //____________________________________________________________________
175 void AliTHn::Copy(TObject& c) const
179 AliTHn& target = (AliTHn &) c;
181 AliCFContainer::Copy(target);
183 target.fNSteps = fNSteps;
184 target.fNBins = fNBins;
185 target.fNVars = fNVars;
189 for (Int_t i=0; i<fNSteps; i++)
192 target.fValues[i] = new TArrayF(*(fValues[i]));
194 target.fValues[i] = 0;
197 target.fSumw2[i] = new TArrayF(*(fSumw2[i]));
199 target.fSumw2[i] = 0;
203 //____________________________________________________________________
204 Long64_t AliTHn::Merge(TCollection* list)
206 // Merge a list of AliTHn objects with this (needed for
208 // Returns the number of merged objects (including this).
216 AliCFContainer::Merge(list);
218 TIterator* iter = list->MakeIterator();
222 while ((obj = iter->Next())) {
224 AliTHn* entry = dynamic_cast<AliTHn*> (obj);
228 for (Int_t i=0; i<fNSteps; i++)
230 if (entry->fValues[i])
233 fValues[i] = new TArrayF(fNBins);
235 for (Long64_t l = 0; l<fNBins; l++)
236 fValues[i]->GetArray()[l] += entry->fValues[i]->GetArray()[l];
239 if (entry->fSumw2[i])
242 fSumw2[i] = new TArrayF(fNBins);
244 for (Long64_t l = 0; l<fNBins; l++)
245 fSumw2[i]->GetArray()[l] += entry->fSumw2[i]->GetArray()[l];
255 void AliTHn::Fill(const Double_t *var, Int_t istep, Double_t weight)
262 axisCache = new TAxis*[fNVars];
263 for (Int_t i=0; i<fNVars; i++)
264 axisCache[i] = GetAxis(i, 0);
267 // calculate global bin index
269 for (Int_t i=0; i<fNVars; i++)
271 bin *= axisCache[i]->GetNbins();
273 Int_t tmpBin = axisCache[i]->FindBin(var[i]);
274 // Printf("%d", tmpBin);
275 // under/overflow not supported
276 if (tmpBin < 1 || tmpBin > axisCache[i]->GetNbins())
279 // bins start from 0 here
281 // Printf("%lld", bin);
286 fValues[istep] = new TArrayF(fNBins);
287 AliInfo(Form("Created values container for step %d", istep));
292 // initialize with already filled entries (which have been filled with weight == 1), in this case fSumw2 := fValues
295 fSumw2[istep] = new TArrayF(*fValues[istep]);
296 AliInfo(Form("Created sumw2 container for step %d", istep));
300 fValues[istep]->GetArray()[bin] += weight;
302 fSumw2[istep]->GetArray()[bin] += weight * weight;
304 // Printf("%f", fValues[istep][bin]);
307 // AliCFContainer::Fill(var, istep, weight);
310 Long64_t AliTHn::GetGlobalBinIndex(const Int_t* binIdx)
312 // calculates global bin index
313 // binIdx contains TAxis bin indexes
314 // here bin count starts at 0 because we do not have over/underflow bins
317 for (Int_t i=0; i<fNVars; i++)
319 bin *= GetAxis(i, 0)->GetNbins();
320 bin += binIdx[i] - 1;
326 void AliTHn::FillContainer(AliCFContainer* cont)
328 // fills the information stored in the buffer in this class into the container <cont>
330 for (Int_t i=0; i<fNSteps; i++)
335 Float_t* source = fValues[i]->GetArray();
336 // if fSumw2 is not stored, the sqrt of the number of bin entries in source is filled below; otherwise we use fSumw2
337 Float_t* sourceSumw2 = source;
339 sourceSumw2 = fSumw2[i]->GetArray();
341 THnSparse* target = cont->GetGrid(i)->GetGrid();
343 Int_t* binIdx = new Int_t[fNVars];
344 Int_t* nBins = new Int_t[fNVars];
345 for (Int_t j=0; j<fNVars; j++)
348 nBins[j] = target->GetAxis(j)->GetNbins();
355 // for (Int_t j=0; j<fNVars; j++)
356 // printf("%d ", binIdx[j]);
358 Long64_t globalBin = GetGlobalBinIndex(binIdx);
359 // Printf(" --> %lld", globalBin);
361 if (source[globalBin] != 0)
363 target->SetBinContent(binIdx, source[globalBin]);
364 target->SetBinError(binIdx, TMath::Sqrt(sourceSumw2[globalBin]));
371 for (Int_t j=fNVars-1; j>0; j--)
373 if (binIdx[j] > nBins[j])
380 if (binIdx[0] > nBins[0])
384 AliInfo(Form("Step %d: copied %lld entries out of %lld bins", i, count, GetGlobalBinIndex(binIdx)));
391 void AliTHn::FillParent()
393 // fills the information stored in the buffer in this class into the baseclass containers
398 void AliTHn::ReduceAxis()
400 // "removes" one axis by summing over the axis and putting the entry to bin 1
401 // TODO presently only implemented for the last axis
403 Int_t axis = fNVars-1;
405 for (Int_t i=0; i<fNSteps; i++)
410 Float_t* source = fValues[i]->GetArray();
411 Float_t* sourceSumw2 = 0;
413 sourceSumw2 = fSumw2[i]->GetArray();
415 THnSparse* target = GetGrid(i)->GetGrid();
417 Int_t* binIdx = new Int_t[fNVars];
418 Int_t* nBins = new Int_t[fNVars];
419 for (Int_t j=0; j<fNVars; j++)
422 nBins[j] = target->GetAxis(j)->GetNbins();
429 // sum over axis <axis>
430 Float_t sumValues = 0;
431 Float_t sumSumw2 = 0;
432 for (Int_t j=1; j<=nBins[axis]; j++)
435 Long64_t globalBin = GetGlobalBinIndex(binIdx);
436 sumValues += source[globalBin];
437 source[globalBin] = 0;
441 sumSumw2 += sourceSumw2[globalBin];
442 sourceSumw2[globalBin] = 0;
447 Long64_t globalBin = GetGlobalBinIndex(binIdx);
448 source[globalBin] = sumValues;
450 sourceSumw2[globalBin] = sumSumw2;
457 for (Int_t j=fNVars-2; j>0; j--)
459 if (binIdx[j] > nBins[j])
466 if (binIdx[0] > nBins[0])
470 AliInfo(Form("Step %d: reduced %lld bins to %lld entries", i, GetGlobalBinIndex(binIdx), count));