Speed up of Expand function (Hermes)
[u/mrichter/AliRoot.git] / TRD / AliTRDarrayDictionary.cxx
1 /************************************************************************* 
2 * Copyright(c) 1998-2008, 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 /* $Id: AliTRDarrayDictionary.cxx 25392 2008-04-23 19:40:29Z cblume $ */
17
18 /////////////////////////////////////////////////////////
19 //                                                     //
20 // Container Class for Dictionary Info                 //
21 //                                                     //
22 // Author:                                             //
23 //   Hermes Leon Vargas (hleon@ikf.uni-frankfurt.de)   //
24 //                                                     //
25 /////////////////////////////////////////////////////////
26
27 #include <TArray.h>
28
29 #include "AliTRDarrayDictionary.h"
30 #include "AliTRDfeeParam.h"
31
32 ClassImp(AliTRDarrayDictionary)
33
34 Short_t *AliTRDarrayDictionary::fgLutPadNumbering = 0x0;
35
36 //________________________________________________________________________________
37 AliTRDarrayDictionary::AliTRDarrayDictionary()
38                       :TObject()
39                       ,fNdet(0)
40                       ,fNrow(0)
41                       ,fNcol(0)
42                       ,fNumberOfChannels(0)
43                       ,fNtime(0)
44                       ,fNDdim(0)
45                       ,fDictionary(0)
46 {
47   //
48   // AliTRDarrayDictionary default contructor
49   //
50
51   CreateLut();
52
53 }
54
55 //________________________________________________________________________________
56 AliTRDarrayDictionary::AliTRDarrayDictionary(Int_t nrow, Int_t ncol, Int_t ntime)
57                       :TObject()
58                       ,fNdet(0)
59                       ,fNrow(0)
60                       ,fNcol(0)
61                       ,fNumberOfChannels(0)
62                       ,fNtime(0)
63                       ,fNDdim(0)
64                       ,fDictionary(0)
65
66 {
67   //
68   // AliTRDarrayDictionary contructor
69   //
70
71   CreateLut();
72   Allocate(nrow,ncol,ntime);
73
74 }
75
76 //________________________________________________________________________________
77 AliTRDarrayDictionary::AliTRDarrayDictionary(const AliTRDarrayDictionary &a)
78                       :TObject()
79                       ,fNdet(a.fNdet)
80                       ,fNrow(a.fNrow)
81                       ,fNcol(a.fNcol)
82                       ,fNumberOfChannels(a.fNumberOfChannels)
83                       ,fNtime(a.fNtime)
84                       ,fNDdim(a.fNDdim)
85                       ,fDictionary(0)
86 {
87   //
88   // AliTRDarrayDictionary copy constructor
89   //
90
91   fDictionary = new Int_t[fNDdim];
92   for(Int_t i=0; i<fNDdim; i++)
93     {
94       fDictionary[i]=a.fDictionary[i];
95     }
96
97 }
98
99 //________________________________________________________________________________
100 AliTRDarrayDictionary::~AliTRDarrayDictionary()
101 {
102   //
103   //   AliTRDarrayDictionary destructor
104   //
105
106   if(fDictionary)
107     {
108       delete [] fDictionary;
109       fDictionary=0;
110     }
111
112 }
113
114 //________________________________________________________________________________
115 AliTRDarrayDictionary &AliTRDarrayDictionary::operator=(const AliTRDarrayDictionary &a)
116 {
117   //
118   // Assignment operator
119   //
120
121   if(this==&a)
122     {
123       return *this;
124     }
125
126   if(fDictionary)
127     {
128       delete [] fDictionary;
129     }
130   fNdet=a.fNdet;
131   fNDdim=a.fNDdim;
132   fNrow=a.fNrow;
133   fNcol=a.fNcol;
134   fNumberOfChannels = a.fNumberOfChannels;
135   fNtime=a.fNtime;
136   fDictionary = new Int_t[fNDdim];
137   for(Int_t i=0; i<fNDdim; i++)
138     {
139       fDictionary[i]=a.fDictionary[i];
140     }
141   return *this;
142
143 }
144
145 //________________________________________________________________________________
146 void AliTRDarrayDictionary::Allocate(Int_t nrow, Int_t ncol, Int_t ntime)
147 {
148   //
149   // Allocates memory for the dictionary array with dimensions
150   // Row*NumberOfNecessaryMCMs*ADCchannelsInMCM*Time
151   // To be consistent with AliTRDarrayADC
152   // Object initialized to -1
153   //
154
155   fNrow=nrow;
156   fNcol=ncol;
157   fNtime=ntime;
158   Int_t adcchannelspermcm = AliTRDfeeParam::GetNadcMcm(); 
159   Int_t padspermcm = AliTRDfeeParam::GetNcolMcm(); 
160   Int_t numberofmcms = fNcol/padspermcm;
161   fNumberOfChannels = numberofmcms*adcchannelspermcm;
162   fNDdim=nrow*fNumberOfChannels*ntime;
163   if(fDictionary)
164     {
165       delete [] fDictionary;
166       fDictionary=0;
167     }
168   fDictionary = new Int_t[fNDdim];
169   for(Int_t i=0; i<fNDdim; i++)
170     {
171       fDictionary[i] = -1; 
172     }
173
174 }
175
176 //________________________________________________________________________________
177 void AliTRDarrayDictionary::Compress()
178 {
179   //
180   // Compress the array
181   //
182
183
184   //  AliDebug(1,"Compressing");
185   Int_t counter=0;
186   Int_t newDim=0;
187   Int_t j;                 
188   Int_t r=0;
189   Int_t *longArr;            
190   longArr = new Int_t[fNDdim];  
191   Int_t k=0;
192   memset(longArr,0,sizeof(Int_t)*fNDdim);
193
194   for(Int_t i=0;i<fNDdim; i++)
195     {
196       j=0;
197       if(fDictionary[i]==-1)
198         {
199           for(k=i;k<fNDdim;k++)
200             {
201               if(fDictionary[k]==-1)
202                 {
203                   j=j+1;
204                   longArr[r]=j;
205                 }
206               else
207                 {
208                   break;
209                 }
210             } 
211           r=r+1;    
212         }
213       i=i+j;
214     }
215   //Calculate the size of the compressed array
216   for(Int_t i=0; i<fNDdim;i++)
217     {
218       if(longArr[i]!=0)  
219         {
220           counter=counter+longArr[i]-1;
221         }
222     }
223   newDim=fNDdim-counter;   //Size of the compressed array
224   //Fill the buffer of the compressed array
225   Int_t* buffer;
226   buffer = new Int_t[newDim];
227   Int_t counterTwo=0;
228   Int_t g=0;
229   for(Int_t i=0; i<newDim; i++)
230     {
231       if(counterTwo<fNDdim)
232         {
233           if(fDictionary[counterTwo]!=-1)
234             {
235               buffer[i]=fDictionary[counterTwo];
236             }
237           if(fDictionary[counterTwo]==-1)
238             {
239               buffer[i]=-(longArr[g]);
240               counterTwo=counterTwo+longArr[g]-1;
241               g++;
242             }  
243           counterTwo++;
244         }
245     }
246
247   //Copy the buffer
248   if(fDictionary)
249     {
250       delete [] fDictionary;
251       fDictionary=0;
252     }
253   fDictionary = new Int_t[newDim];
254   fNDdim = newDim;
255   for(Int_t i=0; i<newDim; i++)
256     {
257       fDictionary[i] = buffer[i]; 
258     }
259   if(buffer)
260     {
261       delete [] buffer;
262       buffer=0;
263     }
264   if(longArr) 
265     {
266       delete [] longArr;
267       longArr=0;
268     }
269
270 }
271
272 //________________________________________________________________________________
273 void AliTRDarrayDictionary::Expand()
274 {
275   //  
276   //  Expand the array
277   //  
278
279   Int_t *longArr;
280   longArr = new Int_t[fNDdim];
281   Int_t dimexp=0;
282
283   //Initialize the array
284   memset(longArr,0,sizeof(Int_t)*fNDdim);
285
286   Int_t r2=0;
287   for(Int_t i=0; i<fNDdim;i++)
288     {
289       if((fDictionary[i]<0)&&(fDictionary[i]!=-1))  
290         {
291           longArr[r2]=-fDictionary[i]; 
292           r2++;
293         }
294     }
295
296   //Calculate new dimensions
297   for(Int_t i=0; i<fNDdim;i++)
298     {
299       if(longArr[i]!=0)      
300         {
301           dimexp=dimexp+longArr[i]-1;
302         }
303     }
304   dimexp=dimexp+fNDdim;  
305
306   //Write in the buffer the new array
307   Int_t* bufferE;
308   bufferE = new Int_t[dimexp];
309   Int_t contaexp =0;    
310   Int_t h=0;
311   for(Int_t i=0; i<dimexp; i++)
312     {
313       if(fDictionary[contaexp]>=-1)  
314         {
315           bufferE[i]=fDictionary[contaexp];
316         }
317       if(fDictionary[contaexp]<-1)  
318         {
319           for(Int_t j=0; j<longArr[h];j++)
320             {
321               bufferE[i+j]=-1;
322             }
323           i=i+longArr[h]-1;
324           h++;
325         }
326       contaexp++;
327     }
328
329   //Copy the buffer
330   if(fDictionary)
331     {
332       delete [] fDictionary;
333       fDictionary=0;
334     }
335
336   fDictionary = new Int_t[dimexp];
337   fNDdim = dimexp;
338   for(Int_t i=0; i<dimexp; i++)
339     {
340       fDictionary[i] = bufferE[i]; 
341     }
342   if(bufferE) delete [] bufferE;
343   if(longArr) delete [] longArr;
344
345 }
346 //________________________________________________________________________________
347 void AliTRDarrayDictionary::Reset()
348 {
349   //
350   // Reset the array, the old contents are deleted
351   // and the data array elements are set to zero.
352   //
353
354   memset(fDictionary,0,sizeof(Int_t)*fNDdim);
355
356 }
357 //________________________________________________________________________________
358 Int_t AliTRDarrayDictionary::GetData(Int_t nrow, Int_t ncol, Int_t ntime) const
359 {
360   //
361   // Get the data using the pad numbering.
362   // To access data using the mcm scheme use instead
363   // the method GetDataByAdcCol
364   //
365
366   Int_t corrcolumn = fgLutPadNumbering[ncol];
367
368   return fDictionary[(nrow*fNumberOfChannels+corrcolumn)*fNtime+ntime];
369
370 }
371 //________________________________________________________________________________
372 void AliTRDarrayDictionary::SetData(Int_t nrow, Int_t ncol, Int_t ntime, Int_t value)
373 {
374   //
375   // Set the data using the pad numbering.
376   // To write data using the mcm scheme use instead
377   // the method SetDataByAdcCol
378   //
379
380   Int_t colnumb = fgLutPadNumbering[ncol];
381
382   fDictionary[(nrow*fNumberOfChannels+colnumb)*fNtime+ntime]=value;
383
384 }
385
386 //________________________________________________________________________________
387 void AliTRDarrayDictionary::CreateLut()
388 {
389   //
390   // Initializes the Look Up Table to relate
391   // pad numbering and mcm channel numbering
392   //
393
394   if(fgLutPadNumbering)  return;
395   
396    fgLutPadNumbering = new Short_t[AliTRDfeeParam::GetNcol()];
397    memset(fgLutPadNumbering,0,sizeof(Short_t)*AliTRDfeeParam::GetNcol());
398
399   for(Int_t mcm=0; mcm<8; mcm++)
400     {
401       Int_t lowerlimit=0+mcm*18;
402       Int_t upperlimit=18+mcm*18;
403       Int_t shiftposition = 1+3*mcm;
404       for(Int_t index=lowerlimit;index<upperlimit;index++)
405         {
406           fgLutPadNumbering[index]=index+shiftposition;
407         }
408     }
409 }