]> git.uio.no Git - u/mrichter/AliRoot.git/blob - CORRFW/AliCFFrame.cxx
final updates and fixups in the container classes
[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   //
197   Int_t ind=ibin[fNVar-1];
198   for(Int_t i=fNVar-1;i>0;i--){
199     ind=ibin[i-1]+ind*fNVarBins[i-1]; 
200   }
201   return ind;
202
203 //____________________________________________________________________
204 Int_t AliCFFrame::GetBinIndex(Int_t ivar, Int_t ind) const
205 {
206   //
207   // getting the bin index on a given dim. ivar for a given element ind on the grid
208   //
209   Int_t index=-1;
210   for(Int_t i=fNVar-1;i>=0;i--){
211     index=ind/fProduct[i];
212     fIndex[i]=index;
213     ind-=index*fProduct[i];
214   }
215   
216   index=fIndex[ivar];
217   return index;
218 }
219 //____________________________________________________________________
220 void AliCFFrame::GetBinIndex(Int_t ind, Int_t *bins ) const
221 {
222   //
223   // getting the set of bin indeces for a given element ind on the grid
224   //
225   Int_t index=-1;
226   for(Int_t i=fNVar-1;i>=0;i--){
227     index=ind/fProduct[i];
228     bins[i]=index;
229     ind-=index*fProduct[i];
230   }
231   return;
232 }
233 //____________________________________________________________________
234 Double_t AliCFFrame::GetBinCenter(Int_t ivar, Int_t ibin) const
235 {
236   //
237   // getting the bin center of a given bin ibin along variable ivar
238   //
239  
240   Double_t binMin=fVarBinLimits[fOffset[ivar]+ibin];
241   Double_t binMax=fVarBinLimits[fOffset[ivar]+ibin+1];
242   Double_t val=0.5*(binMin+binMax);
243   return val;
244 }
245 //____________________________________________________________________
246 void AliCFFrame::GetBinCenters(Int_t *ibin, Double_t *binCenter) const
247 {
248   //
249   // gives the centers of the N-dim bin identified by a set of bin indeces 
250   // invar
251   //
252   for(Int_t i=0;i<fNVar;i++){
253     binCenter[i]=GetBinCenter(i,ibin[i]);
254   }
255 } //____________________________________________________________________
256 Double_t AliCFFrame::GetBinSize(Int_t ivar, Int_t ibin) const
257 {
258   //
259   // getting the bin size on axis ivar of a given bin ibin 
260   //
261
262   if(ibin>=fNVarBins[ivar]){
263     AliWarning(Form("bin index out of range, number of bins in variable %i is %i", ivar,fNVarBins[ivar]));
264     return -1;
265   } 
266
267   Double_t binMin=fVarBinLimits[fOffset[ivar]+ibin];
268   Double_t binMax=fVarBinLimits[fOffset[ivar]+ibin+1];
269   Double_t val=binMax-binMin;
270   return val;
271 }
272 //____________________________________________________________________
273 void AliCFFrame::GetBinSizes(Int_t *ibin, Double_t *binSizes) const
274 {
275   //
276   // gives the sizes of the N-dim bin identified by a set of bin indeces 
277   // ibin
278   //
279   for(Int_t i=0;i<fNVar;i++){
280     binSizes[i]=GetBinSize(i,ibin[i]);
281   }
282
283
284 //____________________________________________________________________
285 void AliCFFrame::Copy(TObject& c) const
286 {
287   //
288   // copy function
289   //
290   AliCFFrame& target = (AliCFFrame &) c;
291
292   target.fNVar=fNVar;
293   target.fNDim=fNDim;
294   target.fNVarBinLimits=fNVarBinLimits;
295   if (fNVarBins)
296     target.fNVarBins = fNVarBins;
297   if (fVarBinLimits)
298     target.fVarBinLimits = fVarBinLimits;
299   if (fProduct)
300     target.fProduct = fProduct;
301   if (fOffset)
302     target.fOffset = fOffset;  
303 }
304 //____________________________________________________________________
305 void AliCFFrame::PrintBinLimits()
306 {
307   //
308   // printing the array containing the bin limits 
309   //
310   for(Int_t i=0;i<fNVarBinLimits;i++){
311     AliInfo(Form("bin limit index %i is: %f",i, fVarBinLimits[i]));
312   } 
313
314 //____________________________________________________________________
315 void AliCFFrame::PrintNBins()
316 {
317   //
318   // printing the array containing the # of bins  
319   //
320   for(Int_t i=0;i<fNVar;i++){
321     AliInfo(Form("bins in var %i are: %i",i, fNVarBins[i]));
322   } 
323
324 //____________________________________________________________________
325 void AliCFFrame::Save(const Char_t *outfile) const
326 {
327   //
328   // Save the grid to a root file
329   //
330
331   const char *dirname = "./";
332   TString filename = outfile;
333   TFile *file=0x0;
334   if((gSystem->FindFile(dirname,filename))!=NULL){
335       file = new TFile( outfile,"UPDATE");
336   }
337   else{
338     file = new TFile( outfile,"RECREATE");
339   } 
340   file->cd();
341   //write the object to a file
342   this->Write(GetName(),TObject::kSingleKey);
343   file->Close();
344   delete file;
345 }