]> git.uio.no Git - u/mrichter/AliRoot.git/blob - CONTAINERS/AliDigits.cxx
TPC branches now correctly diverted into Digits.TPS.root file
[u/mrichter/AliRoot.git] / CONTAINERS / AliDigits.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, 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 /*
17
18 $Log$
19 Revision 1.1  2000/11/01 15:57:13  kowal2
20 Moved from the TPC directory
21
22 Revision 1.4  2000/10/05 16:01:49  kowal2
23 Corrected for memory leaks.
24
25 Revision 1.3  2000/06/30 12:07:49  kowal2
26 Updated from the TPC-PreRelease branch
27
28 Revision 1.2.4.3  2000/06/26 07:39:42  kowal2
29 Changes to obey the coding rules
30
31 Revision 1.2.4.2  2000/06/25 08:38:41  kowal2
32 Splitted from AliTPCtracking
33
34 Revision 1.2.4.1  2000/06/14 16:45:13  kowal2
35 Improved algorithms. Compiler warnings removed.
36
37 Revision 1.2  2000/04/17 09:37:33  kowal2
38 removed obsolete AliTPCDigitsDisplay.C
39
40 Revision 1.1.4.2  2000/04/10 11:37:42  kowal2
41
42 Digits handling in a new data structure
43
44 */
45
46 /*MI change -- for Rule checker
47           -- added copy constructor and assignmet operator 
48           -- new GetSize return size of object in Bytes
49           -- added GetDigitSize and GetOverTh function
50           -- added GetNRows, GetNCols function
51           -- for Marek -I had it in my code  
52 */ 
53
54 ///////////////////////////////////////////////////////////////////////////////
55 //                                                                           //
56 //   Alice  digits array  object  AliDigits                                  //
57 //                                                                           //
58 //                                                                           //
59 //                                                                           //
60 ///////////////////////////////////////////////////////////////////////////////
61
62
63 #include "TClass.h"
64 #include <iostream.h>
65 #include "TError.h"
66 #include "AliSegmentID.h"
67 #include "AliH2F.h"
68 #include "AliArrayI.h"
69 #include "AliArrayS.h"
70 #include "AliDigits.h"
71
72
73
74 //_____________________________________________________________________________
75 //_____________________________________________________________________________
76 //_____________________________________________________________________________
77 ClassImp(AliDigits)
78
79
80 AliDigits::AliDigits()
81 {
82   // 
83   //default constructor
84   fIndex = 0;
85   fElements = 0;
86   fThreshold =0;
87   Invalidate();
88 }
89
90 AliDigits::AliDigits(const AliDigits& digits)
91 {
92   //
93   //copy constructor
94   fNrows = digits.fNrows;
95   fNcols = digits.fNcols;
96   fElements = new AliArrayS(*(digits.fElements));
97   fIndex = new AliArrayI(*(digits.fIndex));
98   fBufType = digits.fBufType;
99   fThreshold = digits.fThreshold;
100   fNelems    = digits.fNelems;
101 }
102
103 AliDigits & AliDigits::operator =(const AliDigits & digits)
104 {
105  //assignment operator
106   fNrows = digits.fNrows;
107   fNcols = digits.fNcols;
108   if (fElements) delete fElements;
109   fElements = new AliArrayS(*(digits.fElements));
110   if (fIndex) delete fIndex;
111   fIndex = new AliArrayI(*(digits.fIndex));
112   fBufType = digits.fBufType;
113   fThreshold = digits.fThreshold;
114   fNelems    = digits.fNelems; 
115   return (*this);
116 }
117
118 AliDigits::~AliDigits()
119 {
120   //
121   //default destructor
122   if (fIndex !=0 ) {
123     delete fIndex;
124   }
125   if (fElements != 0) {
126     delete fElements;
127   }
128   
129   
130 }
131
132
133 Bool_t AliDigits::OutOfBoundsError(const char *where, Int_t row, Int_t column) 
134 {
135    // Generate an out-of-bounds error. Always returns false.
136    ::Error(where, "row %d  col %d out of bounds (size: %d x %d, this: 0x%08x)", 
137            row, column, fNrows, fNcols, this);
138    return kFALSE;
139 }
140
141
142 void AliDigits::Invalidate() 
143
144   //
145   //set default (invalid parameters)
146   if (fIndex != 0)  delete  fIndex;
147   fIndex = new AliArrayI;
148   
149   if (fElements!= 0)     delete  fElements;
150   
151   fElements = new AliArrayS;
152   
153   fNrows = fNcols =fNelems= -1; 
154   fElements->Set(0); 
155   fIndex->Set(0); 
156   fBufType = -1;
157 }
158
159 void AliDigits::Allocate(Int_t rows, Int_t columns)
160 {
161   //
162   //construct empty buffer fDigits with size rows x columns
163   Invalidate();
164   if (rows <= 0) {
165       Error("Allocate", "no of rows has to be positive");
166       return;
167    }
168    if (columns <= 0) {
169       Error("Allocate", "no of columns has to be positive");
170       return;
171    }
172   fNrows = rows;
173   fNcols=columns;
174   fNelems = fNrows * fNcols;
175   fElements->Set(fNelems);
176   fIndex->Set(fNcols);
177   for (Int_t i =0,k=0; i<fNcols;i++,k+=fNrows) 
178   (*fIndex)[i]=k;
179   fBufType =0;
180 }
181
182
183 Int_t AliDigits::GetSize()
184 {
185   //
186   //return size of object
187   //
188   Int_t size = sizeof(this);
189   if (fIndex!=0) size+= sizeof(fIndex)+fIndex->GetSize()*sizeof(Int_t);
190   if (fElements!=0) size+= sizeof(fElements)+fElements->GetSize()*sizeof(Short_t);
191   return size;
192 }
193
194 Int_t AliDigits::GetDigitSize() //return total size of pure digit
195 {
196   //
197   //return size of PURE DIGITS
198   //
199   if (fElements==0) return 0;
200   else return sizeof(fElements)+fElements->GetSize()*sizeof(Short_t);
201 }
202
203 Int_t AliDigits::GetOverTh(Float_t threshold,Float_t x1, Float_t x2, Float_t y1, Float_t y2)
204 {
205   //
206   //return number of digits over threshold
207   // 
208  if ( (fElements==0) || (fElements->GetSize()<=0)) return 0;
209  
210  if (x1<=x2) {
211     x1=0;
212     x2=fNrows;
213   }
214   if (y1<=y2) {
215      y1=0;
216      y2=fNcols;
217   }
218   Int_t over=0;
219
220   Bool_t cont=First();
221   for ( cont=First(); cont==kTRUE;cont=Next()) {
222     if ( (CurrentRow()<x1) || (CurrentRow()>x2)) continue;
223     if ( (CurrentColumn()<y1) || (CurrentColumn()>y2)) continue;
224     if (CurrentDigit()>threshold) over++;
225   }
226   return over;
227 }
228
229
230 Short_t AliDigits::GetDigit(Int_t row, Int_t column)
231 {
232   //
233   // return digit for given row and collumn
234   if (fBufType ==0) return GetDigitFast(row,column);
235   if (fBufType ==1) return GetDigit1(row,column);
236
237   return 0;
238 }
239
240
241 void AliDigits::ExpandBuffer()
242 {  
243   //
244   //expand buffer to two dimensional array
245   if (fBufType<0)  {
246     Error("ExpandBuffer", "buffer doesn't exist");
247     return;
248   }
249   if (fBufType==0)      return;
250   
251   //expanding of buffer type 1
252   if (fBufType==1) ExpandBuffer1();
253   
254   fBufType = 0;
255 }
256
257 void AliDigits::CompresBuffer(Int_t bufferType,Int_t threshold)
258 {
259   //
260   //compres buffer according buffertype algorithm
261   if (fBufType<0)  {
262     Error("CompressBuffer", "buffer doesn't exist");
263     return;
264   }
265   if (fBufType == bufferType) return;
266   //
267   if (fBufType>0) ExpandBuffer();
268   if (fBufType !=0)  {
269     Error("CompressBuffer", "buffer doesn't exist");
270     return;
271   }
272   fThreshold = threshold;
273   //compress buffer of type 1
274   if ( bufferType == 1) CompresBuffer1();//end of compresing bufer of type 1 
275 }
276
277 Bool_t AliDigits::First()
278 {
279   //adjust  first valid current digit
280   if (fBufType ==0) return First0();
281   if (fBufType ==1) return First1();
282   return kFALSE;
283 }
284
285 Bool_t  AliDigits::Next()
286 {
287   //addjust next valid current digit
288   if (fBufType ==0) return Next0();
289   if (fBufType ==1) return Next1();
290   return kFALSE;
291 }
292  
293 void AliDigits::AcceptHisto(AliH2F * his)
294 {
295   //
296   //make digits buffer with value according histograms values
297   //for testing purpose  
298   Int_t idim =his->GetNbinsX();
299   Int_t jdim =his->GetNbinsY();
300   if ( (idim<1)|| (jdim<1)) {
301     return;
302   }
303   //allocate proper buffer size
304   Allocate(idim,jdim);
305   //set digits values
306   for (Int_t i = 0; i<idim;i++)    
307     for (Int_t j = 0; j<jdim;j++)
308       {
309         Int_t index = his->GetBin(i+1,j+1);     
310         SetDigitFast((Short_t)his->GetBinContent(index),i,j);
311       }   
312 }
313
314 AliH2F *  AliDigits::GenerHisto()
315 {
316   //
317   //make digits histo 
318   char ch[30];
319   sprintf(ch,"Segment_%d ",GetID());
320   if ( (fNrows<1)|| (fNcols<1)) {
321     return 0;
322   }
323   AliH2F * his  = new AliH2F("Digit histo",ch,fNrows,0,fNrows,fNcols,0,fNcols);
324   ExpandBuffer();
325   //set histogram  values
326   for (Int_t i = 0; i<fNrows;i++)    
327     for (Int_t j = 0; j<fNcols;j++)
328         his->Fill(i,j,GetDigitFast(i,j));
329   return his;
330 }
331
332 AliH2F *AliDigits::DrawDigits(const char *option,Float_t x1, Float_t x2, Float_t y1, Float_t y2)
333 {
334   //
335   //draw digits in given array
336   //
337   AliH2F *h2f = GenerHisto();
338   if (x1>=0) {
339       AliH2F *h2fsub = h2f->GetSubrange2d(x1,x2,y1,y2);
340       delete h2f;
341       h2f=h2fsub;
342   }
343   if (h2f==0) return 0;
344   if (option!=0) h2f->Draw(option);
345   else h2f->Draw();
346   return h2f;  
347 }
348
349 void AliDigits::ExpandBuffer1()
350 {
351   //
352   //expand buffer of type to twodimensional array
353   Int_t i,k;
354   fNelems = fNrows*fNcols;
355   Short_t * buf = new Short_t[fNelems];
356   memset(buf,0,fNelems*sizeof(Short_t)); //MI change - 4.12.2000
357   fIndex->Set(fNcols);
358   for (i =0,k=0 ;i<fNcols;i++,k+=fNrows) (*fIndex)[i]=k;
359   Int_t col=0;
360   Int_t row = 0;
361   Int_t n=fElements->fN;
362   for (i=0;i<n;i++){
363     //oposite signa means how many unwrited (under threshold) values
364     if ((*fElements)[i]<0) row-=fElements->At(i); 
365     else {
366       buf[(*fIndex)[col]+row]=fElements->At(i);
367       row++;
368     }
369     if (row==fNrows) {
370       row=0;
371       col++;
372     }else 
373       if (row>fNrows){
374         Invalidate();
375         return;
376       }      
377   }
378   fElements->Adopt(fNelems,buf);    
379 }
380 void AliDigits::CompresBuffer1()
381 {
382   //
383   //compres buffer according  algorithm 1
384   //
385   AliArrayS  buf;  //lets have the nearly the "worst case"
386   buf.Set(fNelems);
387   AliArrayI  index;
388   index.Set(fNcols);
389   Int_t icurrent=-1;
390   Int_t izero;
391   for (Int_t col = 0; col<fNcols; col++){      
392     index[col]=icurrent+1;//set collumn pointer
393     izero = 0;  //reset zer counter at the begining of the column
394     for (Int_t row = 0; row< fNrows;row++){
395       //if under threshold
396       if (GetDigitFast(row,col)<=fThreshold)  izero++;
397       else{
398         if (izero>0) {
399           //if we have currently izero count under threshold
400           icurrent++;     
401           if (icurrent>=buf.fN) buf.Expand(icurrent*2);
402           buf[icurrent]= -izero;  //write how many under zero
403           izero = 0;
404         } //end of reseting izero
405         icurrent++;
406         if (icurrent>=buf.fN) buf.Expand(icurrent*2);
407         buf[icurrent] = GetDigitFast(row,col);      
408       }//if signal bigger then threshold                
409     } //end of loop over rows
410     if (izero>0) {
411       icurrent++;         
412       if (icurrent>=buf.fN) buf.Expand(icurrent*2);
413       buf[icurrent]= -izero;  //write how many under zero
414     }
415   }//end of lopping over digits
416   buf.Expand(icurrent+1);
417   (*fElements)=buf;
418   fNelems = fElements->fN;
419   fBufType = 1;
420   (*fIndex) =index;
421   //end of compresing bufer of type 1 
422 }
423
424
425
426 Bool_t AliDigits::First0()
427 {
428   //
429   //first for the buffer type 0
430   fCurrentRow = -1;
431   fCurrentCol = -1;
432   fCurrentIndex = -1;
433   Int_t i;
434   for (i=0; (( i<fNelems) && (fElements->At(i)<=fThreshold));i++)
435   if (i == fNelems) return kFALSE;
436   fCurrentCol =i/fNrows;
437   fCurrentRow =i%fNrows;
438   fCurrentIndex = i;
439   return kTRUE; 
440 }
441
442 Bool_t AliDigits::Next0()
443 {
444   //
445   //next for the buffer type 0
446   //
447   if (fCurrentIndex<0) return kFALSE;  // if we didn't adjust first 
448   Int_t i;
449   for (i=fCurrentIndex+1; ( (i<fNelems) && (fElements->At(i)<=fThreshold) ) ;i++);
450   if (i >= fNelems)  {
451     fCurrentIndex = -1;
452     return kFALSE;
453   }
454   fCurrentCol =i/fNrows;
455   fCurrentRow =i%fNrows;
456   fCurrentIndex = i;
457   return kTRUE; 
458 }
459
460 Bool_t AliDigits::First1()
461 {
462   //
463   //first for the buffer type 1
464   fCurrentRow = -1;
465   fCurrentCol = 0;
466   fCurrentIndex = -1;
467   Int_t i;
468   for (i=0; i<fNelems; i++){
469     if (fElements->At(i) < 0) fCurrentRow-=fElements->At(i);
470     else      
471       fCurrentRow++;
472     if (fCurrentRow>=fNrows) {
473        fCurrentCol++;
474        fCurrentRow-=fNrows;
475     }
476     if (fElements->At(i)>fThreshold) break;
477   }
478   fCurrentIndex = i;
479   if (fCurrentIndex>=0) return kTRUE;
480   fCurrentRow =-1;
481   fCurrentCol =-1;
482   return kFALSE;        
483 }
484
485 Bool_t AliDigits::Next1()
486 {
487   //
488   //next for the buffer type 1
489   if (fCurrentIndex<0) return kFALSE;  // if we didn't adjust first 
490   Int_t i;
491   for (i=fCurrentIndex+1; i<fNelems;i++){
492     if (fElements->At(i) < 0) fCurrentRow-=fElements->At(i);
493     else      
494       fCurrentRow++;
495     if (fCurrentRow>=fNrows) {
496       fCurrentCol++;
497       fCurrentRow-=fNrows;
498     }
499     if (fElements->At(i)>fThreshold) break;
500   }
501   fCurrentIndex = i;
502   if ( (i>=0) && (i<fNelems) ) return kTRUE;
503   fCurrentRow =-1;
504   fCurrentCol =-1;
505   return kFALSE;
506 }
507
508 Short_t AliDigits::GetDigit1(Int_t row, Int_t column)
509 {
510   //
511   //return digit for given row and column  the buffer type 1
512   //no control performed
513   
514   Int_t i,n2;
515   if ( (column+1)>=fNcols) n2 = fNelems;
516   else
517     n2 = fIndex->At(column+1);
518   Int_t irow = 0; //current row    
519  
520   for (i=fIndex->At(column); ( (i<n2) && (irow<row) );i++){
521     if (fElements->At(i) < 0) irow-=fElements->At(i);
522     else      
523       irow++;
524   }
525   if ( irow == row ) return fElements->At(i);
526   return -1;
527 }
528