Always take the number of time bins from OCDB, unless explicitly overwritten
[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 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                ,fADC(0)
45 {
46   //
47   // AliTRDarrayADC default constructor
48   //
49
50   CreateLut();
51
52 }
53
54 //____________________________________________________________________________________
55 AliTRDarrayADC::AliTRDarrayADC(Int_t nrow, Int_t ncol, Int_t ntime)
56                :TObject()
57                ,fNdet(0)               
58                ,fNrow(0)
59                ,fNcol(0)
60                ,fNumberOfChannels(0)
61                ,fNtime(0) 
62                ,fNAdim(0)
63                ,fADC(0)
64 {
65   //
66   // AliTRDarrayADC constructor
67   //
68
69   CreateLut();
70   Allocate(nrow,ncol,ntime);
71
72 }
73
74 //____________________________________________________________________________________
75 AliTRDarrayADC::AliTRDarrayADC(const AliTRDarrayADC &b)
76                :TObject()
77                ,fNdet(b.fNdet)
78                ,fNrow(b.fNrow)
79                ,fNcol(b.fNcol)
80                ,fNumberOfChannels(b.fNumberOfChannels)
81                ,fNtime(b.fNtime) 
82                ,fNAdim(b.fNAdim)
83                ,fADC(0)  
84 {
85   //
86   // AliTRDarrayADC copy constructor
87   //
88
89   fADC =  new Short_t[fNAdim];
90   memcpy(fADC,b.fADC, fNAdim*sizeof(Short_t));  
91
92 }
93
94 //____________________________________________________________________________________
95 AliTRDarrayADC::~AliTRDarrayADC()
96 {
97   //
98   // AliTRDarrayADC destructor
99   //
100
101   if(fADC)
102     {
103       delete [] fADC;
104       fADC=0;
105     }
106
107 }
108
109 //____________________________________________________________________________________
110 AliTRDarrayADC &AliTRDarrayADC::operator=(const AliTRDarrayADC &b)
111 {
112   //
113   // Assignment operator
114   //
115
116   if(this==&b)
117     {
118       return *this;
119     }
120   if(fADC)
121     {
122       delete [] fADC;
123     }
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 AliTRDdataArrayDigits 
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 AliTRDdataArrayDigits
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 AliTRDdataArrayDigits
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 AliTRDdataArrayDigits
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   Int_t counter=0;
300   Int_t newDim=0;
301   Int_t j;                  
302   Int_t l;                  
303   Int_t r=0;                
304   Int_t s=0;                
305   Int_t k=0;
306
307   Int_t *longm = new Int_t[fNAdim];  
308   Int_t *longz = new Int_t[fNAdim];
309
310   if(longz && longm && fADC)
311     {
312
313       memset(longz,0,sizeof(Int_t)*fNAdim);
314       memset(longm,0,sizeof(Int_t)*fNAdim);
315
316       for(Int_t i=0;i<fNAdim; i++)
317         {
318           j=0;
319           if(fADC[i]==-1)
320             {
321               for(k=i;k<fNAdim;k++)
322                 {
323                   if((fADC[k]==-1)&&(j<16000))   
324                     {
325                       j=j+1;
326                       longm[r]=j;                
327                     }
328                   else
329                     {
330                       break;
331                     }
332                 }
333               r=r+1;            
334             }
335           l=16001;
336           if(fADC[i]==0)
337             {
338               for(k=i;k<fNAdim;k++)
339                 {
340                   if((fADC[k]==0)&&(l<32767))     
341                     {                             
342                       l=l+1;
343                       longz[s]=l;                
344                     }
345                   else
346                     {
347                       break;
348                     }
349                 }
350               s=s+1;         
351             }
352           if(fADC[i]>0)
353             {
354               i=i+1;
355             }
356           i=i+j+(l-16001-1); 
357         }
358
359       //Calculate the size of the compressed array
360       for(Int_t i=0; i<fNAdim;i++)
361         {
362           if(longm[i]!=0)   
363             {
364               counter=counter+longm[i]-1;
365             }
366           if(longz[i]!=0)  
367             {
368               counter=counter+(longz[i]-16001)-1;
369             }
370         }
371
372       Int_t counterTwo=0;
373       newDim = fNAdim-counter;   //Dimension of the compressed array
374       Short_t* buffer = new Short_t[newDim];
375
376       if(buffer)
377         {
378
379           //Fill the buffer of the compressed array
380           Int_t g=0;
381           Int_t h=0; 
382           for(Int_t i=0; i<newDim; i++)
383             {
384               if(counterTwo<fNAdim)
385                 {
386                   if(fADC[counterTwo]>0)
387                     {
388                       buffer[i]=fADC[counterTwo];
389                     }
390                   if(fADC[counterTwo]==-1)
391                     {
392                       buffer[i]=-(longm[g]);
393                       counterTwo=counterTwo+longm[g]-1;
394                       g++;
395                     }  
396                   if(fADC[counterTwo]==0)
397                     {
398                       buffer[i]=-(longz[h]); 
399                       counterTwo=counterTwo+(longz[h]-16001)-1;
400                       h++;
401                     }  
402                   counterTwo++;
403                 }
404             }
405
406           //Copy the buffer
407           delete [] fADC;
408           fADC=0;
409           fADC = new Short_t[newDim];
410           fNAdim = newDim;
411           for(Int_t i=0; i<newDim; i++)
412             {
413               fADC[i] = buffer[i]; 
414             }
415
416           //Delete auxiliary arrays
417           delete [] buffer;
418           buffer=0;
419         } 
420
421     }
422
423   if (longz)
424     {
425       delete [] longz;
426       longz=0;
427     }
428   if (longm)
429     {
430       delete [] longm;
431       longm=0;
432
433     }
434
435 }
436
437 //____________________________________________________________________________________
438 void AliTRDarrayADC::Expand()
439 {
440   //
441   // Expand the array
442   //
443
444   if (fADC)
445     {
446
447       //Check if the array has not been already expanded
448       Int_t verif=0;
449       for(Int_t i=0; i<fNAdim; i++)
450         {
451           if(fADC[i]<-1)
452             {
453               verif++;
454             }
455         }
456   
457       if(verif==0)
458         {
459           //AliDebug(1,"Nothing to expand");
460           return;
461         }
462
463       Int_t dimexp=0;
464       Int_t *longz = new Int_t[fNAdim];
465       Int_t *longm = new Int_t[fNAdim];
466
467       if (longz && longm)
468         {
469
470           //Initialize arrays
471           memset(longz,0,sizeof(Int_t)*fNAdim);
472           memset(longm,0,sizeof(Int_t)*fNAdim);
473           Int_t r2=0; 
474           Int_t r3=0; 
475           for(Int_t i=0; i<fNAdim;i++)
476             {
477               if((fADC[i]<0)&&(fADC[i]>=-16000))      
478                 {
479                   longm[r2]=-fADC[i];
480                   r2++;
481                 }
482               if(fADC[i]<-16000)  
483                 {
484                   longz[r3]=-fADC[i]-16001;  
485                   r3++;
486                 }
487             }
488
489           //Calculate the new dimensions of the array
490           for(Int_t i=0; i<fNAdim;i++)
491             {
492               if(longm[i]!=0)       
493                 {
494                   dimexp=dimexp+longm[i]-1;
495                 }
496               if(longz[i]!=0)      
497                 {
498                   dimexp=dimexp+longz[i]-1;
499                 }
500             }
501           dimexp=dimexp+fNAdim;   
502
503           //Write in the buffer the new array
504           Int_t contaexp =0;     
505           Int_t h=0;
506           Int_t l=0;  
507           Short_t* bufferE = new Short_t[dimexp];
508           if(bufferE)
509             {
510               for(Int_t i=0; i<dimexp; i++)
511                 {
512                   if(fADC[contaexp]>0)  
513                     {
514                       bufferE[i]=fADC[contaexp];
515                     }
516                   if((fADC[contaexp]<0)&&(fADC[contaexp]>=-16000))  
517                     {
518                       for(Int_t j=0; j<longm[h];j++)
519                         {
520                           bufferE[i+j]=-1;
521                         }
522                       i=i+longm[h]-1;
523                       h++;
524                     }
525                   if(fADC[contaexp]<-16000)  
526                     {
527                       for(Int_t j=0; j<longz[l];j++)
528                         {
529                           bufferE[i+j]=0;  
530                         }
531                       i=i+longz[l]-1;
532                       l++;
533                     }
534                   contaexp++;
535                 }
536               //Copy the buffer
537               delete [] fADC;
538               fADC = new Short_t[dimexp];
539               fNAdim = dimexp;
540               for(Int_t i=0; i<dimexp; i++)
541                 {
542                   fADC[i] = bufferE[i]; 
543                 }
544
545               delete [] bufferE;
546
547             }
548
549           //Delete auxiliary arrays
550           delete [] longm;
551           delete [] longz;
552
553         }
554
555     }
556
557 }
558 //____________________________________________________________________________________
559 void AliTRDarrayADC::DeleteNegatives()
560 {
561
562   //
563   //This method modifies the digits array, changing the negative values (-1)
564   //Produced during digitization into zero.
565   //
566
567   for(Int_t a=0; a<fNAdim; a++)
568     {
569       if(fADC[a]==-1)
570         {
571           fADC[a]=0;
572         }
573     }
574 }
575 //________________________________________________________________________________
576 void AliTRDarrayADC::Reset()
577 {
578   //
579   // Reset the array, the old contents are deleted
580   // The array keeps the same dimensions as before
581   //
582  
583   memset(fADC,0,sizeof(Short_t)*fNAdim);
584 }
585 //________________________________________________________________________________
586 void AliTRDarrayADC::ConditionalReset(AliTRDSignalIndex* idx)
587 {
588   //
589   // Reset the array, the old contents are deleted
590   // The array keeps the same dimensions as before
591   //
592  
593   if(idx->GetNoOfIndexes()>25)
594     memset(fADC,0,sizeof(Short_t)*fNAdim);
595   else
596     {
597       Int_t row, col;
598       while(idx->NextRCIndex(row, col)){
599         Int_t colnumb = fgLutPadNumbering[col];
600         memset(&fADC[(row*fNumberOfChannels+colnumb)*fNtime],0,fNtime);
601       }
602     }
603
604 }
605
606 //________________________________________________________________________________
607 void AliTRDarrayADC::CreateLut()
608 {
609   //
610   // Initializes the Look Up Table to relate
611   // pad numbering and mcm channel numbering
612   //
613
614   if(fgLutPadNumbering)  return;
615   
616    fgLutPadNumbering = new Short_t[AliTRDfeeParam::GetNcol()];
617    memset(fgLutPadNumbering,0,sizeof(Short_t)*AliTRDfeeParam::GetNcol());
618
619   for(Int_t mcm=0; mcm<8; mcm++)
620     {
621       Int_t lowerlimit=0+mcm*18;
622       Int_t upperlimit=18+mcm*18;
623       Int_t shiftposition = 1+3*mcm;
624       for(Int_t index=lowerlimit;index<upperlimit;index++)
625         {
626           fgLutPadNumbering[index]=index+shiftposition;
627         }
628     }
629 }