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