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