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