Removing useless flag.
[u/mrichter/AliRoot.git] / TRD / AliTRDarrayADC.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: AliTRDarrayADC.cxx 25392 2008-04-23 19:40:29Z cblume $ */
17
18 ////////////////////////////////////////////////////////
19 //                                                    //
20 // Container class for ADC values                     //
21 //                                                    //
22 // Author:                                            //
23 // Hermes Leon Vargas  (hleon@ikf.uni-frankfurt.de)   //
24 //                                                    // 
25 ////////////////////////////////////////////////////////
26
27 #include "AliTRDarrayADC.h"
28 #include "Cal/AliTRDCalPadStatus.h"
29 #include "AliTRDfeeParam.h"
30 #include "AliTRDSignalIndex.h"
31 #include "AliLog.h"
32
33 ClassImp(AliTRDarrayADC)
34
35 Short_t *AliTRDarrayADC::fgLutPadNumbering = 0x0;
36
37 //____________________________________________________________________________________
38 AliTRDarrayADC::AliTRDarrayADC()
39                :TObject()
40                ,fNdet(0)
41                ,fNrow(0)
42                ,fNcol(0)
43                ,fNumberOfChannels(0)
44                ,fNtime(0) 
45                ,fNAdim(0)
46                ,fADC(0)
47 {
48   //
49   // AliTRDarrayADC default constructor
50   //
51
52   CreateLut();
53
54 }
55
56 //____________________________________________________________________________________
57 AliTRDarrayADC::AliTRDarrayADC(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                ,fNAdim(0)
65                ,fADC(0)
66 {
67   //
68   // AliTRDarrayADC constructor
69   //
70
71   CreateLut();
72   Allocate(nrow,ncol,ntime);
73
74 }
75
76 //____________________________________________________________________________________
77 AliTRDarrayADC::AliTRDarrayADC(const AliTRDarrayADC &b)
78                :TObject(b)
79                ,fNdet(b.fNdet)
80                ,fNrow(b.fNrow)
81                ,fNcol(b.fNcol)
82                ,fNumberOfChannels(b.fNumberOfChannels)
83                ,fNtime(b.fNtime) 
84                ,fNAdim(b.fNAdim)
85                ,fADC(0)  
86 {
87   //
88   // AliTRDarrayADC copy constructor
89   //
90
91   fADC =  new Short_t[fNAdim];
92   memcpy(fADC,b.fADC, fNAdim*sizeof(Short_t));  
93
94 }
95
96 //____________________________________________________________________________________
97 AliTRDarrayADC::~AliTRDarrayADC()
98 {
99   //
100   // AliTRDarrayADC destructor
101   //
102
103   delete [] fADC;
104   fADC=0;
105
106 }
107
108 //____________________________________________________________________________________
109 AliTRDarrayADC &AliTRDarrayADC::operator=(const AliTRDarrayADC &b)
110 {
111   //
112   // Assignment operator
113   //
114
115   if(this==&b)
116     {
117       return *this;
118     }
119   if(fADC)
120     {
121       delete [] fADC;
122     }
123   TObject::operator=(b);
124   fNdet=b.fNdet;
125   fNrow=b.fNrow;
126   fNcol=b.fNcol;
127   fNumberOfChannels = b.fNumberOfChannels;
128   fNtime=b.fNtime;
129   fNAdim=b.fNAdim;
130   fADC = new Short_t[fNAdim];
131   memcpy(fADC,b.fADC, fNAdim*sizeof(Short_t));
132
133   return *this;
134
135 }
136
137 //____________________________________________________________________________________
138 void AliTRDarrayADC::Allocate(Int_t nrow, Int_t ncol, Int_t ntime)
139 {
140   //
141   // Allocate memory for an AliTRDarrayADC array with dimensions
142   // Row*NumberOfNecessaryMCMs*ADCchannelsInMCM*Time
143   //
144   
145   fNrow=nrow;
146   fNcol=ncol;
147   fNtime=ntime;
148   Int_t adcchannelspermcm = AliTRDfeeParam::GetNadcMcm(); 
149   Int_t padspermcm = AliTRDfeeParam::GetNcolMcm(); 
150   Int_t numberofmcms = fNcol/padspermcm; 
151   fNumberOfChannels = numberofmcms*adcchannelspermcm; 
152   fNAdim=nrow*fNumberOfChannels*ntime;
153
154   if(fADC)
155     {
156       delete [] fADC;
157     }
158   
159   fADC = new Short_t[fNAdim];
160   memset(fADC,0,sizeof(Short_t)*fNAdim);
161
162 }
163
164 //____________________________________________________________________________________
165 Short_t AliTRDarrayADC::GetDataBits(Int_t row, Int_t col, Int_t time) const
166 {
167   //
168   // Get the ADC value for a given position: row, col, time
169   // Taking bit masking into account
170   //
171   // Adapted from code of the class AliTRDclusterizer 
172   //
173
174   Short_t tempval = GetData(row,col,time);
175   // Be aware of manipulations introduced by pad masking in the RawReader
176   // Only output the manipulated Value
177   CLRBIT(tempval, 10);
178   CLRBIT(tempval, 11);
179   CLRBIT(tempval, 12);
180   return tempval;
181
182 }
183
184 //____________________________________________________________________________________
185 UChar_t AliTRDarrayADC::GetPadStatus(Int_t row, Int_t col, Int_t time) const
186 {
187   // 
188   // Returns the pad status stored in the pad signal
189   //
190   // Output is a UChar_t value
191   // Status Codes:
192   //               Noisy Masking:           2
193   //               Bridged Left Masking     8
194   //               Bridged Right Masking    8
195   //               Not Connected Masking Digits
196   //
197   // Adapted from code of the class AliTRDclusterizer
198   //
199
200   UChar_t padstatus = 0;
201   Short_t signal = GetData(row,col,time);
202   if(signal > 0 && TESTBIT(signal, 10)){
203     if(TESTBIT(signal, 11))
204       if(TESTBIT(signal, 12))
205         padstatus = AliTRDCalPadStatus::kPadBridgedRight;
206       else
207         padstatus = AliTRDCalPadStatus::kNotConnected;
208     else
209       if(TESTBIT(signal, 12))
210         padstatus = AliTRDCalPadStatus::kPadBridgedLeft;
211       else
212         padstatus = AliTRDCalPadStatus::kMasked;
213   }
214
215   return padstatus;
216
217 }
218
219 //____________________________________________________________________________________
220 void AliTRDarrayADC::SetPadStatus(Int_t row, Int_t col, Int_t time, UChar_t status)
221 {
222   //
223   // Setting the pad status into the signal using the Bits 10 to 14 
224   // (currently used: 10 to 12)
225   //
226   // Input codes (Unsigned char):
227   //               Noisy Masking:           2
228   //               Bridged Left Masking     8
229   //               Bridged Right Masking    8
230   //               Not Connected Masking    32
231   //
232   // Status codes: Any masking:             Bit 10(1)
233   //               Noisy masking:           Bit 11(0), Bit 12(0)
234   //               No Connection masking:   Bit 11(1), Bit 12(0)
235   //               Bridged Left masking:    Bit 11(0), Bit 12(1)
236   //               Bridged Right masking:   Bit 11(1), Bit 12(1)
237   // 
238   // Adapted from code of the class AliTRDclusterizer
239   //
240
241   Short_t signal = GetData(row,col,time);
242
243   // Only set the Pad Status if the signal is > 0
244   if(signal > 0)
245     {
246       switch(status)
247         {
248         case AliTRDCalPadStatus::kMasked:
249           SETBIT(signal, 10);
250           CLRBIT(signal, 11);
251           CLRBIT(signal, 12);
252           break;
253         case AliTRDCalPadStatus::kNotConnected:
254           SETBIT(signal, 10);
255           SETBIT(signal, 11);
256           CLRBIT(signal, 12);
257           break;
258         case AliTRDCalPadStatus::kPadBridgedLeft:
259           SETBIT(signal, 10);
260           CLRBIT(signal, 11);
261           SETBIT(signal, 12);
262           break;
263         case AliTRDCalPadStatus::kPadBridgedRight:
264           SETBIT(signal, 10);
265           SETBIT(signal, 11);
266           SETBIT(signal, 12);
267           break;
268         default:
269           CLRBIT(signal, 10);
270           CLRBIT(signal, 11);
271           CLRBIT(signal, 12);
272         }
273       SetData(row, col, time, signal);
274     }
275
276 }
277
278 //____________________________________________________________________________________
279 Bool_t AliTRDarrayADC::IsPadCorrupted(Int_t row, Int_t col, Int_t time)
280 {
281   // 
282   // Checks if the pad has any masking as corrupted (Bit 10 in signal set)
283   // 
284   // Adapted from code of the class AliTRDclusterizer
285   //
286
287   Short_t signal = GetData(row,col,time);
288   return (signal > 0 && TESTBIT(signal, 10)) ? kTRUE : kFALSE;
289
290 }
291
292 //____________________________________________________________________________________
293 void AliTRDarrayADC::Compress()
294 {
295   //
296   // Compress the array
297   //
298
299   if(fNAdim!=fNrow*fNumberOfChannels*fNtime)
300     {
301       AliDebug(1,"The ADC array is already compressed");
302       return;
303     }
304
305   Int_t counter=0;
306   Int_t newDim=0;
307   Int_t j;                  
308   Int_t l;                  
309   Int_t r=0;                
310   Int_t s=0;                
311   Int_t k=0;
312
313   Int_t *longm = new Int_t[fNAdim];  
314   Int_t *longz = new Int_t[fNAdim];
315
316   if(longz && longm && fADC)
317     {
318
319       memset(longz,0,sizeof(Int_t)*fNAdim);
320       memset(longm,0,sizeof(Int_t)*fNAdim);
321
322       for(Int_t i=0;i<fNAdim; i++)
323         {
324           j=0;
325           if(fADC[i]==-1)
326             {
327               for(k=i;k<fNAdim;k++)
328                 {
329                   if((fADC[k]==-1)&&(j<16000))   
330                     {
331                       j=j+1;
332                       longm[r]=j;                
333                     }
334                   else
335                     {
336                       break;
337                     }
338                 }
339               r=r+1;            
340             }
341           l=16001;
342           if(fADC[i]==0)
343             {
344               for(k=i;k<fNAdim;k++)
345                 {
346                   if((fADC[k]==0)&&(l<32767))     
347                     {                             
348                       l=l+1;
349                       longz[s]=l;                
350                     }
351                   else
352                     {
353                       break;
354                     }
355                 }
356               s=s+1;         
357             }
358           if(fADC[i]>0)
359             {
360               i=i+1;
361             }
362           i=i+j+(l-16001-1); 
363         }
364
365       //Calculate the size of the compressed array
366       for(Int_t i=0; i<fNAdim;i++)
367         {
368           if(longm[i]!=0)   
369             {
370               counter=counter+longm[i]-1;
371             }
372           if(longz[i]!=0)  
373             {
374               counter=counter+(longz[i]-16001)-1;
375             }
376         }
377
378       Int_t counterTwo=0;
379       newDim = fNAdim-counter;   //Dimension of the compressed array
380       Short_t* buffer = new Short_t[newDim];
381
382       if(buffer)
383         {
384
385           //Fill the buffer of the compressed array
386           Int_t g=0;
387           Int_t h=0; 
388           for(Int_t i=0; i<newDim; i++)
389             {
390               if(counterTwo<fNAdim)
391                 {
392                   if(fADC[counterTwo]>0)
393                     {
394                       buffer[i]=fADC[counterTwo];
395                     }
396                   if(fADC[counterTwo]==-1)
397                     {
398                       buffer[i]=-(longm[g]);
399                       counterTwo=counterTwo+longm[g]-1;
400                       g++;
401                     }  
402                   if(fADC[counterTwo]==0)
403                     {
404                       buffer[i]=-(longz[h]); 
405                       counterTwo=counterTwo+(longz[h]-16001)-1;
406                       h++;
407                     }  
408                   counterTwo++;
409                 }
410             }
411
412           //Copy the buffer
413           delete [] fADC;
414           fADC=0;
415           fADC = new Short_t[newDim];
416           fNAdim = newDim;
417           for(Int_t i=0; i<newDim; i++)
418             {
419               fADC[i] = buffer[i]; 
420             }
421
422           //Delete auxiliary arrays
423           delete [] buffer;
424           buffer=0;
425         } 
426
427     }
428
429   if (longz)
430     {
431       delete [] longz;
432       longz=0;
433     }
434   if (longm)
435     {
436       delete [] longm;
437       longm=0;
438
439     }
440
441 }
442
443 //____________________________________________________________________________________
444 void AliTRDarrayADC::Expand()
445 {
446   //
447   // Expand the array
448   //
449
450   if (fADC)
451     {
452
453       //Check if the array has not been already expanded
454       Int_t verif=0;
455       for(Int_t i=0; i<fNAdim; i++)
456         {
457           if(fADC[i]<-1)
458             {
459               verif++;
460             }
461         }
462   
463       if(verif==0)
464         {
465           AliDebug(1,"Nothing to expand");
466           return;
467         }
468
469       Int_t dimexp=0;
470       Int_t *longz = new Int_t[fNAdim];
471       Int_t *longm = new Int_t[fNAdim];
472
473       if (longz && longm)
474         {
475
476           //Initialize arrays
477           memset(longz,0,sizeof(Int_t)*fNAdim);
478           memset(longm,0,sizeof(Int_t)*fNAdim);
479           Int_t r2=0; 
480           Int_t r3=0; 
481           for(Int_t i=0; i<fNAdim;i++)
482             {
483               if((fADC[i]<0)&&(fADC[i]>=-16000))      
484                 {
485                   longm[r2]=-fADC[i];
486                   r2++;
487                 }
488               if(fADC[i]<-16000)  
489                 {
490                   longz[r3]=-fADC[i]-16001;  
491                   r3++;
492                 }
493             }
494
495           //Calculate the new dimensions of the array
496           for(Int_t i=0; i<fNAdim;i++)
497             {
498               if(longm[i]!=0)       
499                 {
500                   dimexp=dimexp+longm[i]-1;
501                 }
502               if(longz[i]!=0)      
503                 {
504                   dimexp=dimexp+longz[i]-1;
505                 }
506             }
507           dimexp=dimexp+fNAdim;   
508
509           //Write in the buffer the new array
510           Int_t contaexp =0;     
511           Int_t h=0;
512           Int_t l=0;  
513           Short_t* bufferE = new Short_t[dimexp];
514           if(bufferE)
515             {
516               for(Int_t i=0; i<dimexp; i++)
517                 {
518                   if(fADC[contaexp]>0)  
519                     {
520                       bufferE[i]=fADC[contaexp];
521                     }
522                   if((fADC[contaexp]<0)&&(fADC[contaexp]>=-16000))  
523                     {
524                       for(Int_t j=0; j<longm[h];j++)
525                         {
526                           bufferE[i+j]=-1;
527                         }
528                       i=i+longm[h]-1;
529                       h++;
530                     }
531                   if(fADC[contaexp]<-16000)  
532                     {
533                       for(Int_t j=0; j<longz[l];j++)
534                         {
535                           bufferE[i+j]=0;  
536                         }
537                       i=i+longz[l]-1;
538                       l++;
539                     }
540                   contaexp++;
541                 }
542               //Copy the buffer
543               delete [] fADC;
544               fADC = new Short_t[dimexp];
545               fNAdim = dimexp;
546               for(Int_t i=0; i<dimexp; i++)
547                 {
548                   fADC[i] = bufferE[i]; 
549                 }
550
551               delete [] bufferE;
552
553             }
554
555           //Delete auxiliary arrays
556           delete [] longm;
557           delete [] longz;
558
559         }
560
561     }
562
563 }
564 //____________________________________________________________________________________
565 void AliTRDarrayADC::DeleteNegatives()
566 {
567
568   //
569   //This method modifies the digits array, changing the negative values (-1)
570   //Produced during digitization into zero.
571   //
572
573   for(Int_t a=0; a<fNAdim; a++)
574     {
575       if(fADC[a]==-1)
576         {
577           fADC[a]=0;
578         }
579     }
580 }
581 //________________________________________________________________________________
582 void AliTRDarrayADC::Reset()
583 {
584   //
585   // Reset the array, the old contents are deleted
586   // The array keeps the same dimensions as before
587   //
588  
589   memset(fADC,0,sizeof(Short_t)*fNAdim);
590 }
591 //________________________________________________________________________________
592 void AliTRDarrayADC::ConditionalReset(AliTRDSignalIndex* idx)
593 {
594   //
595   // Reset the array, the old contents are deleted
596   // The array keeps the same dimensions as before
597   //
598  
599   if(idx->GetNoOfIndexes()>25)
600     memset(fADC,0,sizeof(Short_t)*fNAdim);
601   else
602     {
603       Int_t row, col;
604       while(idx->NextRCIndex(row, col)){
605         Int_t colnumb = fgLutPadNumbering[col];
606         memset(&fADC[(row*fNumberOfChannels+colnumb)*fNtime],0,fNtime);
607       }
608     }
609
610 }
611
612 //________________________________________________________________________________
613 void AliTRDarrayADC::CreateLut()
614 {
615   //
616   // Initializes the Look Up Table to relate
617   // pad numbering and mcm channel numbering
618   //
619
620   if(fgLutPadNumbering)  return;
621   
622    fgLutPadNumbering = new Short_t[AliTRDfeeParam::GetNcol()];
623    memset(fgLutPadNumbering,0,sizeof(Short_t)*AliTRDfeeParam::GetNcol());
624
625   for(Int_t mcm=0; mcm<8; mcm++)
626     {
627       Int_t lowerlimit=0+mcm*18;
628       Int_t upperlimit=18+mcm*18;
629       Int_t shiftposition = 1+3*mcm;
630       for(Int_t index=lowerlimit;index<upperlimit;index++)
631         {
632           fgLutPadNumbering[index]=index+shiftposition;
633         }
634     }
635 }