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 **************************************************************************/
15 //--------------------------------------------------------------------//
17 // AliCFGridSparse Class //
18 // Class to accumulate data on an N-dimensional grid, to be used //
19 // as input to get corrections for Reconstruction & Trigger efficiency//
20 // Based on root THnSparse //
21 // -- Author : S.Arcelli //
22 // Still to be done: //
23 // --Interpolate among bins in a range //
24 //--------------------------------------------------------------------//
27 #include "AliCFGridSparse.h"
28 #include "THnSparse.h"
37 //____________________________________________________________________
38 ClassImp(AliCFGridSparse)
40 //____________________________________________________________________
41 AliCFGridSparse::AliCFGridSparse() :
45 // default constructor
47 //____________________________________________________________________
48 AliCFGridSparse::AliCFGridSparse(const Char_t* name, const Char_t* title) :
49 AliCFVGrid(name,title),
52 // default constructor
54 //____________________________________________________________________
55 AliCFGridSparse::AliCFGridSparse(const Char_t* name, const Char_t* title, const Int_t nVarIn, const Int_t * nBinIn, const Double_t *binLimitsIn) :
56 AliCFVGrid(name,title,nVarIn,nBinIn,binLimitsIn),
63 fData=new THnSparseF(name,title,fNVar,fNVarBins);
66 for(Int_t ivar=0;ivar<fNVar;ivar++){
67 Int_t nbins=fNVarBins[ivar]+1;
68 Double_t *array= new Double_t[nbins];
69 for(Int_t i=0;i<nbins;i++){
70 array[i]=fVarBinLimits[fOffset[ivar]+i];
72 fData->SetBinEdges(ivar, array);
77 //____________________________________________________________________
78 AliCFGridSparse::AliCFGridSparse(const AliCFGridSparse& c) :
85 ((AliCFGridSparse &)c).Copy(*this);
88 //____________________________________________________________________
89 AliCFGridSparse::~AliCFGridSparse()
94 if(fData) delete fData;
97 //____________________________________________________________________
98 AliCFGridSparse &AliCFGridSparse::operator=(const AliCFGridSparse &c)
101 // assigment operator
104 ((AliCFGridSparse &) c).Copy(*this);
108 //____________________________________________________________________
109 void AliCFGridSparse::SetBinLimits(Int_t ivar, Double_t *array)
112 // setting the arrays containing the bin limits
114 fData->SetBinEdges(ivar, array);
115 //then fill the appropriate array in ALICFFrame, to be able to use
116 //the getter, in case....
117 Int_t nbins=fNVarBins[ivar]+1;
118 for(Int_t i=0;i<nbins;i++){
119 fVarBinLimits[fOffset[ivar]+i] =array[i];
123 //____________________________________________________________________
124 void AliCFGridSparse::Fill(Double_t *var, Double_t weight)
128 // given a set of values of the input variable,
129 // with weight (by default w=1)
131 fData->Fill(var,weight);
134 //___________________________________________________________________
135 TH1D *AliCFGridSparse::Project(Int_t ivar) const
138 // Make a 1D projection along variable ivar
141 //exclude overflows in hidden dimesniosn by default (used in projections)
142 if(fExclOffEntriesInProj){
143 for(Int_t i=0;i<fNVar;i++){
145 fData->GetAxis(i)->SetBit(TAxis::kAxisRange);
148 TH1D *hist=fData->Projection(ivar);
151 //___________________________________________________________________
152 TH2D *AliCFGridSparse::Project(Int_t ivar1, Int_t ivar2) const
155 // Make a 2D projection along variables ivar1 & ivar2
158 //exclude overflows by default (used in projections)
159 if(fExclOffEntriesInProj){
160 for(Int_t i=0;i<fNVar;i++){
161 if(i!=ivar1 && i!=ivar2)
162 fData->GetAxis(i)->SetBit(TAxis::kAxisRange);
165 TH2D *hist=fData->Projection(ivar2,ivar1); //notice inverted axis (THnSparse uses TH3 2d-projection convention...)
169 //___________________________________________________________________
170 TH3D *AliCFGridSparse::Project(Int_t ivar1, Int_t ivar2, Int_t ivar3) const
173 // Make a 3D projection along variables ivar1 & ivar2 & ivar3
175 //exclude overflows by default (used in projections)
176 if(fExclOffEntriesInProj){
177 for(Int_t i=0;i<fNVar;i++){
178 if(i!=ivar1 && i!=ivar2 && i!=ivar3)
179 fData->GetAxis(i)->SetBit(TAxis::kAxisRange);
183 TH3D *hist=fData->Projection(ivar1,ivar2,ivar3);
188 //____________________________________________________________________
189 Float_t AliCFGridSparse::GetOverFlows(Int_t ivar) const
192 // Returns exclusive overflows in variable ivar
194 Int_t* bin = new Int_t[fNVar];
195 memset(bin, 0, sizeof(Int_t) * fNVar);
197 for (Long64_t i = 0; i < fData->GetNbins(); ++i) {
198 Double_t v = fData->GetBinContent(i, bin);
200 for(Int_t j=0;j<fNVar;j++){
202 if((bin[j]==0) || (bin[j]==fNVarBins[j]+1))add=kFALSE;
204 if(bin[ivar]==fNVarBins[ivar]+1 && add) ovfl+=v;
211 //____________________________________________________________________
212 Float_t AliCFGridSparse::GetUnderFlows(Int_t ivar) const
215 // Returns exclusive overflows in variable ivar
217 Int_t* bin = new Int_t[fNVar];
218 memset(bin, 0, sizeof(Int_t) * fNVar);
220 for (Long64_t i = 0; i < fData->GetNbins(); ++i) {
221 Double_t v = fData->GetBinContent(i, bin);
223 for(Int_t j=0;j<fNVar;j++){
225 if((bin[j]==0) || (bin[j]==fNVarBins[j]+1))add=kFALSE;
227 if(bin[ivar]==0 && add) unfl+=v;
235 //____________________________________________________________________
236 Float_t AliCFGridSparse::GetEntries() const
239 // total entries (including overflows and underflows)
242 return fData->GetEntries();
245 //____________________________________________________________________
246 Float_t AliCFGridSparse::GetElement(Int_t index) const
249 // Returns content of grid element index according to the
250 // linear indexing in AliCFFrame
252 Int_t *bin = new Int_t[fNVar];
253 GetBinIndex(index, bin);
254 for(Int_t i=0;i<fNVar;i++)fIndex[i]=bin[i]+1; //consistency with AliCFGrid
255 Float_t val=GetElement(fIndex);
260 //____________________________________________________________________
261 Float_t AliCFGridSparse::GetElement(Int_t *bin) const
264 // Get the content in a bin corresponding to a set of bin indexes
266 return fData->GetBinContent(bin);
269 //____________________________________________________________________
270 Float_t AliCFGridSparse::GetElement(Double_t *var) const
273 // Get the content in a bin corresponding to a set of input variables
276 Long_t index=fData->GetBin(var,kFALSE); //this is the THnSparse index (do not allocate new cells if content is empty)
280 return fData->GetBinContent(index);
284 //____________________________________________________________________
285 Float_t AliCFGridSparse::GetElementError(Int_t index) const
288 // Returns the error on the content of a bin according to a linear
289 // indexing in AliCFFrame
292 Int_t *bin = new Int_t[fNVar];
293 GetBinIndex(index, bin);
294 for(Int_t i=0;i<fNVar;i++)fIndex[i]=bin[i]+1; //consistency with AliCFGrid
295 Float_t val=GetElementError(fIndex);
300 //____________________________________________________________________
301 Float_t AliCFGridSparse::GetElementError(Int_t *bin) const
304 // Get the error in a bin corresponding to a set of bin indexes
306 return fData->GetBinError(bin);
309 //____________________________________________________________________
310 Float_t AliCFGridSparse::GetElementError(Double_t *var) const
313 // Get the error in a bin corresponding to a set of input variables
316 Long_t index=fData->GetBin(var,kFALSE); //this is the THnSparse index (do not allocate new cells if content is empy)
320 return fData->GetBinError(index);
325 //____________________________________________________________________
326 void AliCFGridSparse::SetElement(Int_t index, Float_t val)
329 // Sets grid element iel to val (linear indexing) in AliCFFrame
331 Int_t *bin = new Int_t[fNVar];
332 GetBinIndex(index, bin);
333 for(Int_t i=0;i<fNVar;i++)fIndex[i]=bin[i]+1;
334 SetElement(fIndex,val);
337 //____________________________________________________________________
338 void AliCFGridSparse::SetElement(Int_t *bin, Float_t val)
341 // Sets grid element of bin indeces bin to val
343 fData->SetBinContent(bin,val);
345 //____________________________________________________________________
346 void AliCFGridSparse::SetElement(Double_t *var, Float_t val)
349 // Set the content in a bin to value val corresponding to a set of input variables
351 Long_t index=fData->GetBin(var); //THnSparse index: allocate the cell
352 Int_t *bin = new Int_t[fNVar];
353 fData->GetBinContent(index,bin); //trick to access the array of bins
354 fData->SetBinContent(bin,val);
359 //____________________________________________________________________
360 void AliCFGridSparse::SetElementError(Int_t index, Float_t val)
363 // Sets grid element iel error to val (linear indexing) in AliCFFrame
365 Int_t *bin = new Int_t[fNVar];
366 GetBinIndex(index, bin);
367 for(Int_t i=0;i<fNVar;i++)fIndex[i]=bin[i]+1;
368 SetElementError(fIndex,val);
371 //____________________________________________________________________
372 void AliCFGridSparse::SetElementError(Int_t *bin, Float_t val)
375 // Sets grid element error of bin indeces bin to val
377 fData->SetBinError(bin,val);
379 //____________________________________________________________________
380 void AliCFGridSparse::SetElementError(Double_t *var, Float_t val)
383 // Set the error in a bin to value val corresponding to a set of input variables
385 Long_t index=fData->GetBin(var); //THnSparse index
386 Int_t *bin = new Int_t[fNVar];
387 fData->GetBinContent(index,bin); //trick to access the array of bins
388 fData->SetBinError(bin,val);
392 //____________________________________________________________________
393 void AliCFGridSparse::SumW2()
396 //set calculation of the squared sum of the weighted entries
399 fData->CalculateErrors(kTRUE);
405 //____________________________________________________________________
406 void AliCFGridSparse::Add(AliCFVGrid* aGrid, Double_t c)
409 //add aGrid to the current one
413 if(aGrid->GetNVar()!=fNVar){
414 AliInfo("Different number of variables, cannot add the grids");
417 if(aGrid->GetNDim()!=fNDim){
418 AliInfo("Different number of dimensions, cannot add the grids!");
422 if(!fSumW2 && aGrid->GetSumW2())SumW2();
425 fData->Add(((AliCFGridSparse*)aGrid)->GetGrid(),c);
429 //____________________________________________________________________
430 void AliCFGridSparse::Add(AliCFVGrid* aGrid1, AliCFVGrid* aGrid2, Double_t c1,Double_t c2)
433 //Add aGrid1 and aGrid2 and deposit the result into the current one
436 if(fNVar!=aGrid1->GetNVar()|| fNVar!=aGrid2->GetNVar()){
437 AliInfo("Different number of variables, cannot add the grids");
440 if(fNDim!=aGrid1->GetNDim()|| fNDim!=aGrid2->GetNDim()){
441 AliInfo("Different number of dimensions, cannot add the grids!");
445 if(!fSumW2 && (aGrid1->GetSumW2() || aGrid2->GetSumW2()))SumW2();
449 fData->Add(((AliCFGridSparse*)aGrid1)->GetGrid(),c1);
450 fData->Add(((AliCFGridSparse*)aGrid2)->GetGrid(),c2);
454 //____________________________________________________________________
455 void AliCFGridSparse::Multiply(AliCFVGrid* aGrid, Double_t c)
458 // Multiply aGrid to the current one
462 if(aGrid->GetNVar()!=fNVar){
463 AliInfo("Different number of variables, cannot multiply the grids");
466 if(aGrid->GetNDim()!=fNDim){
467 AliInfo("Different number of dimensions, cannot multiply the grids!");
471 if(!fSumW2 && aGrid->GetSumW2())SumW2();
473 THnSparse *h= ((AliCFGridSparse*)aGrid)->GetGrid();
480 //____________________________________________________________________
481 void AliCFGridSparse::Multiply(AliCFVGrid* aGrid1, AliCFVGrid* aGrid2, Double_t c1,Double_t c2)
484 //Multiply aGrid1 and aGrid2 and deposit the result into the current one
487 if(fNVar!=aGrid1->GetNVar()|| fNVar!=aGrid2->GetNVar()){
488 AliInfo("Different number of variables, cannot multiply the grids");
491 if(fNDim!=aGrid1->GetNDim()|| fNDim!=aGrid2->GetNDim()){
492 AliInfo("Different number of dimensions, cannot multiply the grids!");
496 if(!fSumW2 && (aGrid1->GetSumW2() || aGrid2->GetSumW2()))SumW2();
500 THnSparse *h1= ((AliCFGridSparse*)aGrid1)->GetGrid();
501 THnSparse *h2= ((AliCFGridSparse*)aGrid2)->GetGrid();
509 //____________________________________________________________________
510 void AliCFGridSparse::Divide(AliCFVGrid* aGrid, Double_t c)
513 // Divide aGrid to the current one
517 if(aGrid->GetNVar()!=fNVar){
518 AliInfo("Different number of variables, cannot divide the grids");
521 if(aGrid->GetNDim()!=fNDim){
522 AliInfo("Different number of dimensions, cannot divide the grids!");
526 if(!fSumW2 && aGrid->GetSumW2())SumW2();
528 THnSparse *h= ((AliCFGridSparse*)aGrid)->GetGrid();
535 //____________________________________________________________________
536 void AliCFGridSparse::Divide(AliCFVGrid* aGrid1, AliCFVGrid* aGrid2, Double_t c1,Double_t c2, Option_t *option)
539 //Divide aGrid1 and aGrid2 and deposit the result into the current one
540 //bynomial errors are supported
543 if(fNVar!=aGrid1->GetNVar()|| fNVar!=aGrid2->GetNVar()){
544 AliInfo("Different number of variables, cannot divide the grids");
547 if(fNDim!=aGrid1->GetNDim()|| fNDim!=aGrid2->GetNDim()){
548 AliInfo("Different number of dimensions, cannot divide the grids!");
552 if(!fSumW2 && (aGrid1->GetSumW2() || aGrid2->GetSumW2()))SumW2();
555 THnSparse *h1= ((AliCFGridSparse*)aGrid1)->GetGrid();
556 THnSparse *h2= ((AliCFGridSparse*)aGrid2)->GetGrid();
557 fData->Divide(h1,h2,c1,c2,option);
561 //____________________________________________________________________
562 void AliCFGridSparse::Rebin(const Int_t* group)
565 // rebin the grid according to Rebin() as in THnSparse
566 // Please notice that the original number of bins on
567 // a given axis has to be divisible by the rebin group.
570 for(Int_t i=0;i<fNVar;i++){
571 if(group[i]!=1)AliInfo(Form(" merging bins along dimension %i in groups of %i bins", i,group[i]));
574 THnSparse *rebinned =fData->Rebin(group);
578 //redefine the needed stuff
583 //number of bins in each dimension, auxiliary variables
585 for(Int_t ivar=0;ivar<fNVar;ivar++){
586 Int_t nbins = fData->GetAxis(ivar)->GetNbins();
587 fNVarBins[ivar]=nbins;
588 ndimTot*=fNVarBins[ivar];
589 nbinTot+=(fNVarBins[ivar]+1);
591 for(Int_t i =0;i<ivar;i++)offset+=(fNVarBins[i]+1);
592 fOffset[ivar]=offset;
594 for(Int_t i=0;i<ivar;i++)prod*=fNVarBins[i];
600 //now the array of bin limits
602 delete fVarBinLimits;
603 fNVarBinLimits=nbinTot;
604 fVarBinLimits=new Double_t[fNVarBinLimits];
606 for(Int_t ivar=0;ivar<fNVar;ivar++){
607 Double_t low = fData->GetAxis(ivar)->GetXmin();
608 Double_t high = fData->GetAxis(ivar)->GetXmax();
609 const TArrayD *xbins = fData->GetAxis(ivar)->GetXbins();
611 for(Int_t ibin=0;ibin<=fNVarBins[ivar];ibin++){
612 fVarBinLimits[ibin+fOffset[ivar]] = low + ibin*(high-low)/((Double_t) fNVarBins[ivar]);
617 for(Int_t ibin=0;ibin<=fNVarBins[ivar];ibin++) {
618 fVarBinLimits[ibin+fOffset[ivar]] = xbins->At(ibin);
624 //____________________________________________________________________
625 void AliCFGridSparse::Copy(TObject& c) const
630 AliCFGridSparse& target = (AliCFGridSparse &) c;
632 if(fData)target.fData = fData;