simplified efficiency projection
[u/mrichter/AliRoot.git] / CORRFW / AliCFEffGrid.cxx
CommitLineData
563113d0 1/* $Id$ */
1e9dad92 2/**************************************************************************
3 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * *
5 * Author: The ALICE Off-line Project. *
6 * Contributors are mentioned in the code where appropriate. *
7 * *
8 * Permission to use, copy, modify and distribute this software and its *
9 * documentation strictly for non-commercial purposes is hereby granted *
10 * without fee, provided that the above copyright notice appears in all *
11 * copies and that both the copyright notice and this permission notice *
12 * appear in the supporting documentation. The authors make no claims *
13 * about the suitability of this software for any purpose. It is *
14 * provided "as is" without express or implied warranty. *
15 **************************************************************************/
563113d0 16//--------------------------------------------------------------------//
17// //
18// AliCFEffGrid Class //
19// Class to handle efficiency grids //
20// //
21// -- Author : S.Arcelli //
22// //
23// //
24// //
25//--------------------------------------------------------------------//
26//
27//
9f6be3a2 28#include "TMath.h"
29#include "AliLog.h"
563113d0 30#include "AliCFEffGrid.h"
9f6be3a2 31#include "TH1D.h"
32#include "TH2D.h"
33#include "TH3D.h"
563113d0 34
35//____________________________________________________________________
36ClassImp(AliCFEffGrid)
37
38//____________________________________________________________________
39AliCFEffGrid::AliCFEffGrid() :
25488e18 40 AliCFGridSparse(),
563113d0 41 fContainer(0x0),
42 fSelNum(-1),
43 fSelDen(-1)
44{
45 //
46 // default constructor
47 //
48}
49
50//____________________________________________________________________
1e9dad92 51AliCFEffGrid::AliCFEffGrid(const Char_t* name, const Char_t* title, const Int_t nVarIn, const Int_t * nBinIn, const Double_t *binLimitsIn) :
25488e18 52 AliCFGridSparse(name,title,nVarIn,nBinIn,binLimitsIn),
563113d0 53 fContainer(0x0),
54 fSelNum(-1),
55 fSelDen(-1)
56{
57 //
58 // ctor
59 //
60 SumW2();
61}
62//____________________________________________________________________
63AliCFEffGrid::AliCFEffGrid(const Char_t* name, const Char_t* title, const AliCFContainer &c) :
25488e18 64 AliCFGridSparse(name,title,c.GetNVar(),c.GetNBins(),c.GetBinLimits()),
5188c2a7 65 fContainer(NULL),
563113d0 66 fSelNum(-1),
67 fSelDen(-1)
68{
69 //
70 // main constructor
71 //
72 SumW2();
73 //assign the container;
74 fContainer=&c;
75}
76//____________________________________________________________________
25488e18 77AliCFEffGrid::AliCFEffGrid(const AliCFEffGrid& eff) : AliCFGridSparse(),
563113d0 78 fContainer(0x0),
79 fSelNum(-1),
80 fSelDen(-1)
81{
82 //
83 // copy constructor
84 //
85 ((AliCFEffGrid &)eff).Copy(*this);
86}
87
88//____________________________________________________________________
89AliCFEffGrid::~AliCFEffGrid()
90{
91 //
92 // destructor
93 //
94}
95
96//____________________________________________________________________
97AliCFEffGrid &AliCFEffGrid::operator=(const AliCFEffGrid &eff)
98{
99 //
100 // assigment operator
101 //
102 if (this != &eff)
103 ((AliCFEffGrid &) eff).Copy(*this);
104 return *this;
105}
106//____________________________________________________________________
107
5d14ed73 108void AliCFEffGrid::CalculateEfficiency(Int_t istep1,Int_t istep2, Option_t *option)
563113d0 109{
110 //
111 // Calculate the efficiency matrix and its error between selection
112 // Steps istep1 and istep2
113 //
114
115 fSelNum=istep1;
116 fSelDen=istep2;
ac6b9aed 117 AliCFVGrid *num=GetNum();
118 AliCFVGrid *den=GetDen();
563113d0 119 num->SumW2();
120 den->SumW2();
1e9dad92 121 this->SumW2();
5d14ed73 122 this->Divide(num,den,1.,1.,option);
563113d0 123
124 Int_t nEmptyBinsNum=0;
125 Int_t nEmptyBinsNumAndDen=0;
126 for(Int_t iel=0;iel<fNDim;iel++){
127 if(den->GetElement(iel)>0){
128 if(num->GetElement(iel)==0)nEmptyBinsNum++; //num==0,den!=0
1e9dad92 129 }
563113d0 130 else{
131 nEmptyBinsNumAndDen++;
132 }
133 }
134 // Some monitoring printout:
135 AliInfo(Form("Efficiency calculated for steps %i and %i: %i empty bins in the numerator && !denominator and %i empty bins in numerator && denominator were found.",fSelNum,fSelDen,nEmptyBinsNumAndDen,nEmptyBinsNum));
136 AliInfo(Form("The correction map contains %i empty bins ",nEmptyBinsNum+nEmptyBinsNumAndDen));
137}
138//_____________________________________________________________________
1e9dad92 139Double_t AliCFEffGrid::GetAverage() const
563113d0 140{
141 //
142 // Get the average efficiency
143 //
144
1e9dad92 145 Double_t val=0;
146 Double_t valnum=0;
147 Double_t valden=0;
563113d0 148 for(Int_t i=0;i<fNDim;i++){
ac6b9aed 149 valnum+=GetNum()->GetElement(i);
150 valden+=GetDen()->GetElement(i);
563113d0 151 }
152 if(valden>0)val=valnum/valden;
153 AliInfo(Form(" The Average Efficiency = %f ",val));
154
155 return val;
156}
157//_____________________________________________________________________
1e9dad92 158Double_t AliCFEffGrid::GetAverage(Double_t *varMin, Double_t* varMax ) const
563113d0 159{
160 //
161 // Get ave efficiency in a range
162 //
163
164
1e9dad92 165 Double_t val=0;
563113d0 166 Int_t *indexMin = new Int_t[fNVar];
167 Int_t *indexMax = new Int_t[fNVar];
168 Int_t *index = new Int_t[fNVar];
169
170 //Find out the min and max bins
171
172 for(Int_t i=0;i<fNVar;i++){
1e9dad92 173 Double_t xmin=varMin[i]; // the min values
174 Double_t xmax=varMax[i]; // the max values
563113d0 175 Int_t nbins=fNVarBins[i]+1;
365c0ea4 176 Double_t *bins=new Double_t[nbins];
563113d0 177 for(Int_t ibin =0;ibin<nbins;ibin++){
178 bins[ibin] = fVarBinLimits[ibin+fOffset[i]];
179 }
180 indexMin[i] = TMath::BinarySearch(nbins,bins,xmin);
181 indexMax[i] = TMath::BinarySearch(nbins,bins,xmax);
182 if(xmax>=bins[nbins-1]){
183 indexMax[i]=indexMax[i]-1;
184 }
185 delete [] bins;
186 }
187
1e9dad92 188 Double_t valnum=0;
189 Double_t valden=0;
563113d0 190 for(Int_t i=0;i<fNDim;i++){
191 for (Int_t j=0;j<fNVar;j++)index[j]=GetBinIndex(j,i);
192 Bool_t isIn=kTRUE;
193 for (Int_t j=0;j<fNVar;j++){
194 if(!(index[j]>=indexMin[j] && index[j]<=indexMax[j]))isIn=kFALSE;
195 }
196 if(isIn){
ac6b9aed 197 valnum+=GetNum()->GetElement(i);
198 valden+=GetDen()->GetElement(i);
563113d0 199 }
200 }
201 delete [] index;
202 delete [] indexMin;
203 delete [] indexMax;
204 if(valden>0)val=valnum/valden;
205 AliInfo(Form(" the Average Efficiency = %f ",val));
206 return val;
207}
208//____________________________________________________________________
209void AliCFEffGrid::Copy(TObject& eff) const
210{
211 //
212 // copy function
213 //
214 Copy(eff);
215 AliCFEffGrid& target = (AliCFEffGrid &) eff;
216
217 target.fSelNum=fSelNum;
218 target.fSelDen=fSelDen;
219 if(fContainer)
220 target.fContainer=fContainer;
221}
222//___________________________________________________________________
1e9dad92 223TH1D *AliCFEffGrid::Project(Int_t ivar) const
563113d0 224{
225 //
226 // Make a 1D projection along variable ivar
227 //
228
ac6b9aed 229 const Int_t nDim = 1 ;
230 Int_t dim[nDim] = {ivar} ;
231 THnSparse* hNum = ((AliCFGridSparse*)GetNum())->GetGrid()->Projection(nDim,dim);
232 THnSparse* hDen = ((AliCFGridSparse*)GetDen())->GetGrid()->Projection(nDim,dim);
233 THnSparse* ratio = (THnSparse*)hNum->Clone();
234 ratio->Divide(hNum,hDen,1.,1.,"B");
235 delete hNum; delete hDen;
236 return ratio->Projection(ivar);
563113d0 237}
238//___________________________________________________________________
1e9dad92 239TH2D *AliCFEffGrid::Project(Int_t ivar1,Int_t ivar2) const
563113d0 240{
241 //
242 // Make a 2D projection along variable ivar1,ivar2
243 //
563113d0 244
ac6b9aed 245 const Int_t nDim = 2 ;
246 Int_t dim[nDim] = {ivar1,ivar2} ;
247 THnSparse* hNum = ((AliCFGridSparse*)GetNum())->GetGrid()->Projection(nDim,dim);
248 THnSparse* hDen = ((AliCFGridSparse*)GetDen())->GetGrid()->Projection(nDim,dim);
249 THnSparse* ratio = (THnSparse*)hNum->Clone();
250 ratio->Divide(hNum,hDen,1.,1.,"B");
251 delete hNum; delete hDen;
252 return ratio->Projection(ivar1,ivar2);
563113d0 253}
254//___________________________________________________________________
1e9dad92 255TH3D *AliCFEffGrid::Project(Int_t ivar1, Int_t ivar2, Int_t ivar3) const
563113d0 256{
257 //
258 // Make a 3D projection along variable ivar1,ivar2,ivar3
259 //
260
ac6b9aed 261 const Int_t nDim = 3 ;
262 Int_t dim[nDim] = {ivar1,ivar2,ivar3} ;
263 THnSparse* hNum = ((AliCFGridSparse*)GetNum())->GetGrid()->Projection(nDim,dim);
264 THnSparse* hDen = ((AliCFGridSparse*)GetDen())->GetGrid()->Projection(nDim,dim);
265 THnSparse* ratio = (THnSparse*)hNum->Clone();
266 ratio->Divide(hNum,hDen,1.,1.,"B");
267 delete hNum; delete hDen;
268 return ratio->Projection(ivar1,ivar2,ivar3);
563113d0 269}
9105291d 270