bugfix: typo in id for TreeR data type; added unit test for default data types
[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() :
40 AliCFGrid(),
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) :
563113d0 52 AliCFGrid(name,title,nVarIn,nBinIn,binLimitsIn),
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) :
64 AliCFGrid(name,title,c.GetNVar(),c.GetNBins(),c.GetBinLimits()),
65 fContainer(0x0),
66 fSelNum(-1),
67 fSelDen(-1)
68{
69 //
70 // main constructor
71 //
72 SumW2();
73 //assign the container;
74 fContainer=&c;
75}
76//____________________________________________________________________
77AliCFEffGrid::AliCFEffGrid(const AliCFEffGrid& eff) : AliCFGrid(),
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
108void AliCFEffGrid::CalculateEfficiency(Int_t istep1,Int_t istep2)
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;
1e9dad92 117 AliCFVGrid *num=fContainer->GetGrid(fSelNum);
118 AliCFVGrid *den=fContainer->GetGrid(fSelDen);
563113d0 119 num->SumW2();
120 den->SumW2();
1e9dad92 121 this->SumW2();
563113d0 122 this->Divide(num,den,1.,1.,"B");
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++){
149 valnum+=fContainer->GetGrid(fSelNum)->GetElement(i);
150 valden+=fContainer->GetGrid(fSelDen)->GetElement(i);
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){
197 valnum+=fContainer->GetGrid(fSelNum)->GetElement(i);
198 valden+=fContainer->GetGrid(fSelDen)->GetElement(i);
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
1e9dad92 229 TH1D *proj1D=0;
563113d0 230 Int_t nbins =fNVarBins[ivar];
231 Float_t *bins = new Float_t[nbins+1];
232 for(Int_t ibin =0;ibin<nbins+1;ibin++){
233 bins[ibin] = fVarBinLimits[ibin+fOffset[ivar]];
234 }
235
236 char pname[30];
237 sprintf(pname,"%s%s%i%i%s%i",GetName(),"_SelStep",fSelNum,fSelDen,"_proj1D_var", ivar);
238 char htitle[30];
239 sprintf(htitle,"%s%s%i%i%s%i",GetName(),"_SelStep",fSelNum,fSelDen,"_proj1D_var", ivar);
240
241 if(!proj1D){
1e9dad92 242 proj1D =new TH1D(pname,htitle, nbins, bins);
563113d0 243 }
244
1e9dad92 245 proj1D->Sumw2();
563113d0 246 proj1D->Divide(fContainer->GetGrid(fSelNum)->Project(ivar),fContainer->GetGrid(fSelDen)->Project(ivar),1.,1.,"B");
247
248 delete [] bins;
249 return proj1D;
250}
251//___________________________________________________________________
1e9dad92 252TH2D *AliCFEffGrid::Project(Int_t ivar1,Int_t ivar2) const
563113d0 253{
254 //
255 // Make a 2D projection along variable ivar1,ivar2
256 //
257
1e9dad92 258 TH2D *proj2D=0;
563113d0 259
260 Int_t nbins1 =fNVarBins[ivar1];
261 Float_t *bins1 = new Float_t[nbins1+1];
262 Int_t nbins2 =fNVarBins[ivar2];
263 Float_t *bins2 = new Float_t[nbins2+1];
264 for(Int_t ibin1 =0;ibin1<nbins1+1;ibin1++){
265 bins1[ibin1] = fVarBinLimits[ibin1+fOffset[ivar1]];
266 }
267 for(Int_t ibin2 =0;ibin2<nbins2+1;ibin2++){
268 bins2[ibin2] = fVarBinLimits[ibin2+fOffset[ivar2]];
269 }
270
271 char pname[30];
272 sprintf(pname,"%s%s%i%i%s%i%i",GetName(),"_SelStep",fSelNum,fSelDen,"_proj2D_var", ivar1,ivar2);
273 char htitle[30];
274 sprintf(htitle,"%s%s%i%i%s%i%i",GetName(),"_SelStep",fSelNum,fSelDen,"_proj2D_var",ivar1,ivar2);
275
276 if(!proj2D){
1e9dad92 277 proj2D =new TH2D(pname,htitle, nbins1,bins1,nbins2,bins2);
563113d0 278 }
279
1e9dad92 280 proj2D->Sumw2();
563113d0 281 proj2D->Divide(fContainer->GetGrid(fSelNum)->Project(ivar1,ivar2),fContainer->GetGrid(fSelDen)->Project(ivar1,ivar2),1.,1.,"B");
282
283 delete [] bins1;
284 delete [] bins2;
285 return proj2D;
286}
287//___________________________________________________________________
1e9dad92 288TH3D *AliCFEffGrid::Project(Int_t ivar1, Int_t ivar2, Int_t ivar3) const
563113d0 289{
290 //
291 // Make a 3D projection along variable ivar1,ivar2,ivar3
292 //
293
1e9dad92 294 TH3D *proj3D=0;
563113d0 295
296 Int_t nbins1 =fNVarBins[ivar1];
297 Int_t nbins2 =fNVarBins[ivar2];
298 Int_t nbins3 =fNVarBins[ivar3];
299
300 Float_t *bins1 = new Float_t[nbins1+1];
301 Float_t *bins2 = new Float_t[nbins2+1];
302 Float_t *bins3 = new Float_t[nbins3+1];
303
304 for(Int_t ibin =0;ibin<nbins1+1;ibin++){
305 bins1[ibin] = fVarBinLimits[ibin+fOffset[ivar1]];
306 }
307 for(Int_t ibin =0;ibin<nbins2+1;ibin++){
308 bins2[ibin] = fVarBinLimits[ibin+fOffset[ivar2]];
309 }
310 for(Int_t ibin =0;ibin<nbins3+1;ibin++){
311 bins3[ibin] = fVarBinLimits[ibin+fOffset[ivar3]];
312 }
313
314 char pname[30];
315 sprintf(pname,"%s%s%i%i%s%i%i%i",GetName(),"_SelStep",fSelNum,fSelDen,"_proj3D_var",ivar1,ivar2,ivar3);
316 char htitle[30];
317 sprintf(htitle,"%s%s%i%i%s%i%i%i",GetName(),"_SelStep",fSelNum,fSelDen,"_proj3D_var",ivar1,ivar2,ivar3);
318
319
320 if(!proj3D){
1e9dad92 321 proj3D =new TH3D(pname,htitle, nbins1, bins1,nbins2,bins2,nbins3,bins3);
563113d0 322 }
323
1e9dad92 324 proj3D->Sumw2();
563113d0 325 proj3D->Divide(fContainer->GetGrid(fSelNum)->Project(ivar1,ivar2,ivar3),fContainer->GetGrid(fSelDen)->Project(ivar1,ivar2,ivar3),1.,1.,"B");
326
327 delete [] bins1;
328 delete [] bins2;
329 delete [] bins3;
330
331 return proj3D;
332}