]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliCheb3DCalc.cxx
Pedestal suppression implemented
[u/mrichter/AliRoot.git] / STEER / AliCheb3DCalc.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
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
16
17 // Author: ruben.shahoyan@cern.ch   09/09/2006
18 //
19 #include "AliCheb3DCalc.h"
20
21 ClassImp(AliCheb3DCalc)
22
23 //__________________________________________________________________________________________
24 AliCheb3DCalc::AliCheb3DCalc() :
25   fNCoefs(0), 
26   fNRows(0), 
27   fNCols(0), 
28   fNElemBound2D(0), 
29   fNColsAtRow(0), 
30   fColAtRowBg(0),
31   fCoefBound2D0(0), 
32   fCoefBound2D1(0), 
33   fCoefs(0), 
34   fTmpCf1(0), 
35   fTmpCf0(0)
36 {}
37
38 //__________________________________________________________________________________________
39 AliCheb3DCalc::AliCheb3DCalc(const AliCheb3DCalc& src) :
40   TNamed(src), 
41   fNCoefs(src.fNCoefs), 
42   fNRows(src.fNRows), 
43   fNCols(src.fNCols),
44   fNElemBound2D(src.fNElemBound2D), 
45   fNColsAtRow(0), 
46   fColAtRowBg(0), 
47   fCoefBound2D0(0), 
48   fCoefBound2D1(0), 
49   fCoefs(0), 
50   fTmpCf1(0), 
51   fTmpCf0(0)
52 {
53   if (src.fNColsAtRow) {
54     fNColsAtRow = new Int_t[fNRows]; 
55     for (int i=fNRows;i--;) fNColsAtRow[i] = src.fNColsAtRow[i];
56   }
57   if (src.fColAtRowBg) {
58     fColAtRowBg = new Int_t[fNRows]; 
59     for (int i=fNRows;i--;) fColAtRowBg[i] = src.fColAtRowBg[i];
60   }
61   if (src.fCoefBound2D0) {
62     fCoefBound2D0 = new Int_t[fNElemBound2D];
63     for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = src.fCoefBound2D0[i];
64   }
65   if (src.fCoefBound2D1) {
66     fCoefBound2D1 = new Int_t[fNElemBound2D];
67     for (int i=fNElemBound2D;i--;) fCoefBound2D1[i] = src.fCoefBound2D1[i];
68   }
69   if (src.fCoefs) {
70     fCoefs = new Float_t[fNCoefs];
71     for (int i=fNCoefs;i--;) fCoefs[i] = src.fCoefs[i];
72   }
73   if (src.fTmpCf1) fTmpCf1 = new Float_t[fNCols];
74   if (src.fTmpCf0) fTmpCf0 = new Float_t[fNRows];
75 }
76
77 //__________________________________________________________________________________________
78 AliCheb3DCalc::AliCheb3DCalc(FILE* stream) :
79   fNCoefs(0), 
80   fNRows(0), 
81   fNCols(0), 
82   fNElemBound2D(0), 
83   fNColsAtRow(0), 
84   fColAtRowBg(0), 
85   fCoefBound2D0(0), 
86   fCoefBound2D1(0), 
87   fCoefs(0), 
88   fTmpCf1(0), 
89   fTmpCf0(0)
90 {
91   LoadData(stream);
92 }
93
94 //__________________________________________________________________________________________
95 AliCheb3DCalc& AliCheb3DCalc::operator=(const AliCheb3DCalc& rhs)
96 {
97   if (this != &rhs) {
98     Clear();
99     SetName(rhs.GetName());
100     SetTitle(rhs.GetTitle());
101     fNCoefs = rhs.fNCoefs;
102     fNRows  = rhs.fNRows;
103     fNCols  = rhs.fNCols;    
104     if (rhs.fNColsAtRow) {
105       fNColsAtRow = new Int_t[fNRows]; 
106       for (int i=fNRows;i--;) fNColsAtRow[i] = rhs.fNColsAtRow[i];
107     }
108     if (rhs.fColAtRowBg) {
109       fColAtRowBg = new Int_t[fNRows]; 
110       for (int i=fNRows;i--;) fColAtRowBg[i] = rhs.fColAtRowBg[i];
111     }
112     if (rhs.fCoefBound2D0) {
113       fCoefBound2D0 = new Int_t[fNElemBound2D];
114       for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = rhs.fCoefBound2D0[i];
115     }
116     if (rhs.fCoefBound2D1) {
117       fCoefBound2D1 = new Int_t[fNElemBound2D];
118       for (int i=fNElemBound2D;i--;) fCoefBound2D1[i] = rhs.fCoefBound2D1[i];
119     }
120     if (rhs.fCoefs) {
121       fCoefs = new Float_t[fNCoefs];
122       for (int i=fNCoefs;i--;) fCoefs[i] = rhs.fCoefs[i];
123     }
124     if (rhs.fTmpCf1) fTmpCf1 = new Float_t[fNCols];
125     if (rhs.fTmpCf0) fTmpCf0 = new Float_t[fNRows];    
126   }
127   return *this;
128 }
129
130 //__________________________________________________________________________________________
131 void AliCheb3DCalc::Clear(Option_t*)
132 {
133   // delete all dynamycally allocated structures
134   if (fTmpCf1)       { delete[] fTmpCf1;  fTmpCf1 = 0;}
135   if (fTmpCf0)       { delete[] fTmpCf0;  fTmpCf0 = 0;}
136   if (fCoefs)        { delete[] fCoefs;   fCoefs  = 0;}
137   if (fCoefBound2D0) { delete[] fCoefBound2D0; fCoefBound2D0 = 0; }
138   if (fCoefBound2D1) { delete[] fCoefBound2D1; fCoefBound2D1 = 0; }
139   if (fNColsAtRow)   { delete[] fNColsAtRow;   fNColsAtRow = 0; }
140   if (fColAtRowBg)   { delete[] fColAtRowBg;   fColAtRowBg = 0; }
141   //
142 }
143
144 //__________________________________________________________________________________________
145 void AliCheb3DCalc::Print(Option_t* ) const
146 {
147   printf("Chebyshev parameterization data %s for 3D->1 function.\n",GetName());
148   int nmax3d = 0; 
149   for (int i=fNElemBound2D;i--;) if (fCoefBound2D0[i]>nmax3d) nmax3d = fCoefBound2D0[i];
150   printf("%d coefficients in %dx%dx%d matrix\n",fNCoefs,fNRows,fNCols,nmax3d);
151   //
152 }
153
154 //__________________________________________________________________________________________
155 Float_t  AliCheb3DCalc::Eval(Float_t  *par) const
156 {
157   // evaluate Chebyshev parameterization for 3D function.
158   // VERY IMPORTANT: par must contain the function arguments ALREADY MAPPED to [-1:1] interval
159   Float_t  &z = par[2];
160   Float_t  &y = par[1];
161   Float_t  &x = par[0];
162   //
163   int ncfRC;
164   for (int id0=fNRows;id0--;) {
165     int nCLoc = fNColsAtRow[id0];                   // number of significant coefs on this row
166     int Col0  = fColAtRowBg[id0];                   // beginning of local column in the 2D boundary matrix
167     for (int id1=nCLoc;id1--;) {
168       int id = id1+Col0;
169       fTmpCf1[id1] = (ncfRC=fCoefBound2D0[id]) ? ChebEval1D(z,fCoefs + fCoefBound2D1[id], ncfRC) : 0.0;
170     }
171     fTmpCf0[id0] = nCLoc>0 ? ChebEval1D(y,fTmpCf1,nCLoc):0.0;
172   }
173   return ChebEval1D(x,fTmpCf0,fNRows);
174   //
175 }
176
177 //__________________________________________________________________________________________
178 Float_t  AliCheb3DCalc::EvalDeriv(int dim, Float_t  *par) const
179 {
180   // evaluate Chebyshev parameterization derivative in given dimension  for 3D function.
181   // VERY IMPORTANT: par must contain the function arguments ALREADY MAPPED to [-1:1] interval
182   Float_t  &z = par[2];
183   Float_t  &y = par[1];
184   Float_t  &x = par[0];
185   //
186   int ncfRC;
187   for (int id0=fNRows;id0--;) {
188     int nCLoc = fNColsAtRow[id0];                   // number of significant coefs on this row
189     int Col0  = fColAtRowBg[id0];                   // beginning of local column in the 2D boundary matrix
190     for (int id1=nCLoc;id1--;) {
191       int id = id1+Col0;
192       if (dim==2) fTmpCf1[id1] = (ncfRC=fCoefBound2D0[id]) ? ChebEval1Deriv(z,fCoefs + fCoefBound2D1[id], ncfRC) : 0.0;
193       else        fTmpCf1[id1] = (ncfRC=fCoefBound2D0[id]) ? ChebEval1D(z,fCoefs + fCoefBound2D1[id], ncfRC) : 0.0;
194     }
195     if (dim==1)   fTmpCf0[id0] = nCLoc>0 ? ChebEval1Deriv(y,fTmpCf1,nCLoc):0.0;
196     else          fTmpCf0[id0] = nCLoc>0 ? ChebEval1D(y,fTmpCf1,nCLoc):0.0;
197   }
198   return (dim==0) ? ChebEval1Deriv(x,fTmpCf0,fNRows) : ChebEval1D(x,fTmpCf0,fNRows);
199   //
200 }
201
202 //_______________________________________________
203 #ifdef _INC_CREATION_ALICHEB3D_
204 void AliCheb3DCalc::SaveData(const char* outfile,Bool_t append) const
205 {
206   // writes coefficients data to output text file, optionallt appending on the end of existing file
207   TString strf = outfile;
208   gSystem->ExpandPathName(strf);
209   FILE* stream = fopen(strf,append ? "a":"w");
210   SaveData(stream);
211   fclose(stream);
212   //
213 }
214 #endif
215
216 //_______________________________________________
217 #ifdef _INC_CREATION_ALICHEB3D_
218 void AliCheb3DCalc::SaveData(FILE* stream) const
219 {
220   // writes coefficients data to existing output stream
221   // Note: fNCols, fNElemBound2D and fColAtRowBg is not stored, will be computed on fly during the loading of this file
222   fprintf(stream,"#\nSTART %s\n",GetName());
223   fprintf(stream,"# Number of rows\n%d\n",fNRows);
224   //
225   fprintf(stream,"# Number of columns per row\n");
226   for (int i=0;i<fNRows;i++) fprintf(stream,"%d\n",fNColsAtRow[i]);
227   //
228   fprintf(stream,"# Number of Coefs in each significant block of third dimension\n");
229   for (int i=0;i<fNElemBound2D;i++) fprintf(stream,"%d\n",fCoefBound2D0[i]);
230   //
231   fprintf(stream,"# Coefficients\n");
232   for (int i=0;i<fNCoefs;i++) fprintf(stream,"%+.8e\n",fCoefs[i]);
233   fprintf(stream,"END %s\n",GetName());
234   //
235 }
236 #endif
237
238 //_______________________________________________
239 void AliCheb3DCalc::LoadData(FILE* stream)
240 {
241   // Load coefs. from the stream 
242   if (!stream) {Error("LoadData","No stream provided.\nStop"); exit(1);}
243   TString buffs;
244   Clear();
245   ReadLine(buffs,stream);
246   if (!buffs.BeginsWith("START")) {Error("LoadData","Expected: \"START <fit_name>\", found \"%s\"\nStop\n",buffs.Data());exit(1);}
247   if (buffs.First(' ')>0) SetName(buffs.Data()+buffs.First(' ')+1);
248   //
249   ReadLine(buffs,stream); // NRows
250   fNRows = buffs.Atoi(); 
251   if (fNRows<1) {Error("LoadData","Expected: '<number_of_rows>', found \"%s\"\nStop\n",buffs.Data());exit(1);}
252   //
253   fNCols = 0;
254   fNElemBound2D = 0;
255   InitRows(fNRows);
256   //
257   for (int id0=0;id0<fNRows;id0++) {
258     ReadLine(buffs,stream);               // n.cols at this row
259     fNColsAtRow[id0] = buffs.Atoi();
260     fColAtRowBg[id0] = fNElemBound2D;     // begining of this row in 2D boundary surface
261     fNElemBound2D   += fNColsAtRow[id0];
262     if (fNCols<fNColsAtRow[id0]) fNCols = fNColsAtRow[id0];
263   }
264   InitCols(fNCols);
265   //  
266   fNCoefs = 0; 
267   InitElemBound2D(fNElemBound2D);
268   //
269   for (int i=0;i<fNElemBound2D;i++) {
270     ReadLine(buffs,stream);               // n.coeffs at 3-d dimension for the given column/row
271     fCoefBound2D0[i] = buffs.Atoi();
272     fCoefBound2D1[i] = fNCoefs;
273     fNCoefs += fCoefBound2D0[i];
274   }
275   //
276   if (fNCoefs<=0) {Error("LoadData","Negtive (%d) number of Chebychef coeffs. is obtained.\nStop\n",fNCoefs);exit(1);}
277   //
278   InitCoefs(fNCoefs);
279   for (int i=0;i<fNCoefs;i++) {
280     ReadLine(buffs,stream);
281     fCoefs[i] = buffs.Atof();
282   }
283   // check end_of_data record
284   ReadLine(buffs,stream);
285   if (!buffs.BeginsWith("END") || !buffs.Contains(GetName())) {
286     Error("LoadData","Expected \"END %s\", found \"%s\".\nStop\n",GetName(),buffs.Data());
287     exit(1);
288   }
289   //
290 }
291
292 //_______________________________________________
293 void AliCheb3DCalc::ReadLine(TString& str,FILE* stream) 
294 {
295   // read single line from the stream, skipping empty and commented lines. EOF is not expected
296   while (str.Gets(stream)) {
297     str = str.Strip(TString::kBoth,' ');
298     if (str.IsNull()||str.BeginsWith("#")) continue;
299     return;
300   }
301   fprintf(stderr,"AliCheb3D::ReadLine: Failed to read from stream.\nStop");exit(1); // normally, should not reach here
302 }
303
304 //_______________________________________________
305 void AliCheb3DCalc::InitCols(int nc)
306 {
307   // Set max.number of significant columns in the coefs matrix
308   fNCols = nc;
309   if (fTmpCf1) delete[] fTmpCf1;
310   fTmpCf1 = new Float_t [fNCols];
311 }
312
313 //_______________________________________________
314 void AliCheb3DCalc::InitRows(int nr)
315 {
316   // Set max.number of significant rows in the coefs matrix
317   if (fNColsAtRow) delete[] fNColsAtRow;
318   if (fColAtRowBg) delete[] fColAtRowBg;
319   if (fTmpCf0)     delete[] fTmpCf0;
320   fNRows = nr;
321   fNColsAtRow = new Int_t[fNRows];
322   fTmpCf0     = new Float_t [fNRows];
323   fColAtRowBg = new Int_t[fNRows];
324   for (int i=fNRows;i--;) fNColsAtRow[i] = fColAtRowBg[i] = 0;
325 }
326
327 //_______________________________________________
328 void AliCheb3DCalc::InitElemBound2D(int ne)
329 {
330   // Set max number of significant coefs for given row/column of coefs 3D matrix
331   if (fCoefBound2D0) delete[] fCoefBound2D0; 
332   if (fCoefBound2D1) delete[] fCoefBound2D1; 
333   fNElemBound2D = ne;
334   fCoefBound2D0 = new Int_t[fNElemBound2D];
335   fCoefBound2D1 = new Int_t[fNElemBound2D];
336   for (int i=fNElemBound2D;i--;) fCoefBound2D0[i] = fCoefBound2D1[i] = 0;
337 }
338
339 //_______________________________________________
340 void AliCheb3DCalc::InitCoefs(int nc)
341 {
342   // Set total number of significant coefs
343   if (fCoefs) delete[] fCoefs; 
344   fNCoefs = nc;
345   fCoefs = new Float_t [fNCoefs];
346   for (int i=fNCoefs;i--;) fCoefs[i] = 0.0;
347 }
348
349 //__________________________________________________________________________________________
350 Float_t AliCheb3DCalc::ChebEval1Deriv(Float_t  x, const Float_t * array, int ncf )
351 {
352   // evaluate 1D Chebyshev parameterization's derivative. x is the argument mapped to [-1:1] interval
353   if (--ncf<2) return 0;
354   Float_t b0, b1, b2;
355   Float_t x2 = x+x;
356   b1 = b2 = 0;
357   float dcf0=0,dcf1,dcf2=0;
358   b0 = dcf1 = 2*ncf*array[ncf];
359   if (!(--ncf)) dcf0 = dcf1;
360   //
361   for (int i=ncf;i--;) {
362     b2 = b1;      
363     b1 = b0;
364     dcf0 = dcf2 + 2*(i+1)*array[i+1];
365     b0 = dcf0 + x2*b1 -b2;
366     dcf2 = dcf1;  
367     dcf1 = dcf0;
368   }
369   //
370   return b0 - x*b1 - dcf0/2;
371 }