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