]> git.uio.no Git - u/mrichter/AliRoot.git/blob - CORRFW/AliCFFrame.cxx
Compliance with AliAnalysisTaskSE ; upgraded support for QA.
[u/mrichter/AliRoot.git] / CORRFW / AliCFFrame.cxx
1 /* $Id$ */
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  **************************************************************************/
16 //--------------------------------------------------------------------//
17 //                                                                    //
18 // AliCFFrame Class                                                 //
19 // Class to accumulate data on an N-dimensional grid, to be used      //
20 // as input to get corrections for Reconstruction & Trigger efficiency// 
21 //                                                                    //
22 // -- Author : S.Arcelli                                              //
23 // Still to be done:                                                  //
24 // --Implement methods to merge cells                                 //
25 // --Interpolate among bins in a range                                // 
26 //--------------------------------------------------------------------//
27 //
28 //
29
30 #include "TSystem.h"
31 #include "TFile.h"
32 #include "AliLog.h"
33 #include "AliCFFrame.h"
34
35 //____________________________________________________________________
36 ClassImp(AliCFFrame)
37
38 //____________________________________________________________________
39 AliCFFrame::AliCFFrame() : 
40   TNamed(),
41   fNVar(0),
42   fNDim(0),
43   fNVarBinLimits(0),
44   fNVarBins(0x0),
45   fIndex(0x0),
46   fProduct(0x0),
47   fOffset(0x0),
48   fVarBinLimits(0x0)
49 {
50   // default constructor
51 }
52 //____________________________________________________________________
53 AliCFFrame::AliCFFrame(const Char_t* name, const Char_t* title) : 
54   TNamed(name,title),
55   fNVar(0),
56   fNDim(0),
57   fNVarBinLimits(0),
58   fNVarBins(0x0),
59   fIndex(0x0),
60   fProduct(0x0),
61   fOffset(0x0),
62   fVarBinLimits(0x0)
63 {
64   // named constructor
65 }
66 //____________________________________________________________________
67 AliCFFrame::AliCFFrame(const Char_t* name, const Char_t* title, const Int_t nVarIn, const Int_t * nBinIn, const Double_t *binLimitsIn) :  
68   TNamed(name,title),
69   fNVar(0),
70   fNDim(0),
71   fNVarBinLimits(0),
72   fNVarBins(0x0),
73   fIndex(0x0),
74   fProduct(0x0),
75   fOffset(0x0),
76   fVarBinLimits(0x0)
77 {
78   //
79   // main constructor
80   //
81
82   //the number of variables on the grid
83   fNVar=nVarIn;
84
85   // the binning in each variable
86   fNVarBins= new Int_t[fNVar];
87
88   fIndex= new Int_t[fNVar];
89
90   Int_t ndimTot=1;
91   Int_t nbinTot=0;
92
93
94   //Calculate total number of elements and bins...
95
96   for(Int_t i=0;i<fNVar;i++){
97     fNVarBins[i]=nBinIn[i];
98     ndimTot*=nBinIn[i];
99     nbinTot+=(nBinIn[i]+1);
100   } 
101
102
103   //total number of elements
104
105   fNDim=ndimTot;
106
107   fOffset= new Int_t[fNVar];
108   fProduct= new Int_t[fNVar];
109
110   for(Int_t ivar=0;ivar<fNVar;ivar++){
111     Int_t offset=0;
112     for(Int_t i =0;i<ivar;i++)offset+=(fNVarBins[i]+1);      
113     fOffset[ivar]=offset;
114     Int_t prod=1;
115     for(Int_t i=0;i<ivar;i++)prod*=fNVarBins[i];
116     fProduct[ivar]=prod;
117   }
118
119   //The bin limits
120
121   fNVarBinLimits=nbinTot;
122   fVarBinLimits=new Double_t[fNVarBinLimits];
123   if(binLimitsIn){
124     for(Int_t i=0;i<fNVarBinLimits;i++){
125       fVarBinLimits[i] =binLimitsIn[i];
126     } 
127   }
128 }
129 //____________________________________________________________________
130 AliCFFrame::AliCFFrame(const AliCFFrame& c) : 
131   TNamed(),
132   fNVar(0),
133   fNDim(0),
134   fNVarBinLimits(0),
135   fNVarBins(0x0),
136   fIndex(0x0),
137   fProduct(0x0),
138   fOffset(0x0),
139   fVarBinLimits(0x0)
140 {
141   //
142   // copy constructor
143   //
144   ((AliCFFrame &)c).Copy(*this);
145 }
146 //____________________________________________________________________
147 AliCFFrame::~AliCFFrame()
148 {
149   //
150   // destructor
151   //
152   if(fNVarBins)delete [] fNVarBins;
153   if(fVarBinLimits)delete [] fVarBinLimits;
154   if(fIndex)delete [] fIndex;
155   if(fProduct)delete [] fProduct;
156   if(fOffset)delete [] fOffset;
157
158 }
159 //____________________________________________________________________
160 AliCFFrame &AliCFFrame::operator=(const AliCFFrame &c)
161 {
162   //
163   // assigment operator
164   //
165   if (this != &c)
166     ((AliCFFrame &) c).Copy(*this);
167   return *this;
168
169 //____________________________________________________________________
170 void AliCFFrame::SetBinLimits(Int_t ivar, Double_t *array)
171 {
172   //
173   // setting the arrays containing the bin limits 
174   //
175   Int_t nbins=fNVarBins[ivar]+1;
176   for(Int_t i=0;i<nbins;i++){
177     fVarBinLimits[fOffset[ivar]+i] =array[i];
178   } 
179
180 //____________________________________________________________________
181 void AliCFFrame::GetBinLimits(Int_t ivar, Double_t *array) const
182 {
183   //
184   // getting the arrays containing the bin limits 
185   //
186   Int_t nbins=fNVarBins[ivar]+1;
187   for(Int_t i=0;i<nbins;i++){
188     array[i]=fVarBinLimits[fOffset[ivar]+i];
189   } 
190
191 //____________________________________________________________________
192 Int_t AliCFFrame::GetBinIndex(Int_t *ibin) const
193 {
194   //
195   // getting the element number on the grid for a given set of bin indeces 
196   //(here the numbering of bin indeces ranges from 0 to N-1)
197   //
198   Int_t ind=ibin[fNVar-1];
199   for(Int_t i=fNVar-1;i>0;i--){
200     ind=ibin[i-1]+ind*fNVarBins[i-1]; 
201   }
202   return ind;
203
204 //____________________________________________________________________
205 Int_t AliCFFrame::GetBinIndex(Int_t ivar, Int_t ind) const
206 {
207   //
208   // getting the bin index on a given dim. ivar for a given element ind 
209   // on the grid
210   //(here the numbering of bin indeces ranges from 0 to N-1)
211   //
212   Int_t index=-1;
213   for(Int_t i=fNVar-1;i>=0;i--){
214     index=ind/fProduct[i];
215     fIndex[i]=index;
216     ind-=index*fProduct[i];
217   }
218   
219   index=fIndex[ivar];
220   return index;
221 }
222 //____________________________________________________________________
223 void AliCFFrame::GetBinIndex(Int_t ind, Int_t *bins ) const
224 {
225   //
226   // getting the set of bin indeces for a given element ind on the grid
227   // (here the numbering of bin indeces ranges from 0 to N-1)
228   //
229   Int_t index=-1;
230   for(Int_t i=fNVar-1;i>=0;i--){
231     index=ind/fProduct[i];
232     bins[i]=index;
233     ind-=index*fProduct[i];
234   }
235   return;
236 }
237 //____________________________________________________________________
238 Double_t AliCFFrame::GetBinCenter(Int_t ivar, Int_t ibin) const
239 {
240   //
241   // getting the bin center of a given bin ibin along variable ivar
242   // (here the numbering of bin indeces in input 
243   //  ranges from 1 to N, TH1/2/3 and THnSparse convention)
244   //
245  
246   if(ibin>fNVarBins[ivar] || ibin==0){
247     AliWarning(Form("bin index out of range, number of bins in variable %i ranges from 1 to %i", ivar,fNVarBins[ivar]));
248     return -1;
249   } 
250   Double_t binMin=fVarBinLimits[fOffset[ivar]+ibin-1];
251   Double_t binMax=fVarBinLimits[fOffset[ivar]+ibin];
252   Double_t val=0.5*(binMin+binMax);
253   return val;
254 }
255 //____________________________________________________________________
256 void AliCFFrame::GetBinCenters(Int_t *ibin, Double_t *binCenter) const
257 {
258   //
259   // gives the centers of the N-dim bin identified by a set of bin indeces 
260   // invar
261   // (here the numbering of bin indeces in input 
262   //  ranges from 1 to N, TH1/2/3 and THnSparse convention)
263   //
264   for(Int_t i=0;i<fNVar;i++){
265     binCenter[i]=GetBinCenter(i,ibin[i]);
266   }
267 } //____________________________________________________________________
268 Double_t AliCFFrame::GetBinSize(Int_t ivar, Int_t ibin) const
269 {
270   //
271   // getting the bin size on axis ivar of a given bin ibin 
272   // (here the numbering of bin indeces in input 
273   //  ranges from 1 to N, TH1/2/3 and THnSparse convention)
274   //
275
276   if(ibin>fNVarBins[ivar] || ibin==0){
277     AliWarning(Form("bin index out of range, number of bins in variable %i ranges from 1 to %i", ivar,fNVarBins[ivar]));
278     return -1;
279   } 
280
281   Double_t binMin=fVarBinLimits[fOffset[ivar]+ibin-1];
282   Double_t binMax=fVarBinLimits[fOffset[ivar]+ibin];
283   Double_t val=binMax-binMin;
284   return val;
285 }
286 //____________________________________________________________________
287 void AliCFFrame::GetBinSizes(Int_t *ibin, Double_t *binSizes) const
288 {
289   //
290   // gives the sizes of the N-dim bin identified by a set of bin indeces 
291   // ibin
292   // (here the numbering of bin indeces in input 
293   //  ranges from 1 to N, TH1/2/3 and THnSparse convention)
294   //
295   for(Int_t i=0;i<fNVar;i++){
296     binSizes[i]=GetBinSize(i,ibin[i]);
297   }
298
299
300 //____________________________________________________________________
301 void AliCFFrame::Copy(TObject& c) const
302 {
303   //
304   // copy function
305   //
306   AliCFFrame& target = (AliCFFrame &) c;
307
308   target.fNVar=fNVar;
309   target.fNDim=fNDim;
310   target.fNVarBinLimits=fNVarBinLimits;
311   if (fNVarBins)
312     target.fNVarBins = fNVarBins;
313   if (fVarBinLimits)
314     target.fVarBinLimits = fVarBinLimits;
315   if (fProduct)
316     target.fProduct = fProduct;
317   if (fOffset)
318     target.fOffset = fOffset;  
319 }
320 //____________________________________________________________________
321 void AliCFFrame::PrintBinLimits()
322 {
323   //
324   // printing the array containing the bin limits 
325   //
326   for(Int_t i=0;i<fNVarBinLimits;i++){
327     AliInfo(Form("bin limit index %i is: %f",i, fVarBinLimits[i]));
328   } 
329
330 //____________________________________________________________________
331 void AliCFFrame::PrintNBins()
332 {
333   //
334   // printing the array containing the # of bins  
335   //
336   for(Int_t i=0;i<fNVar;i++){
337     AliInfo(Form("bins in var %i are: %i",i, fNVarBins[i]));
338   } 
339
340 //____________________________________________________________________
341 void AliCFFrame::Save(const Char_t *outfile) const
342 {
343   //
344   // Save the grid to a root file
345   //
346
347   const char *dirname = "./";
348   TString filename = outfile;
349   TFile *file=0x0;
350   if((gSystem->FindFile(dirname,filename))!=NULL){
351       file = new TFile( outfile,"UPDATE");
352   }
353   else{
354     file = new TFile( outfile,"RECREATE");
355   } 
356   file->cd();
357   //write the object to a file
358   this->Write(GetName(),TObject::kSingleKey);
359   file->Close();
360   delete file;
361 }