]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TPC/AliDigits.cxx
Change of argument list of methods GetPadCxy, GetPadIxy, SetHit and FirstPad
[u/mrichter/AliRoot.git] / TPC / 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 $Log$
18 Revision 1.1.4.2  2000/04/10 11:37:42  kowal2
19
20 Digits handling in a new data structure
21
22 */
23
24 ///////////////////////////////////////////////////////////////////////////////
25 //                                                                           //
26 //   Alice  digits array  object  AliDigits                                  //
27 //                                                                           //
28 //                                                                           //
29 //                                                                           //
30 ///////////////////////////////////////////////////////////////////////////////
31
32
33 #include "TClass.h"
34 #include <iostream.h>
35 #include "TError.h"
36 #include "AliSegmentID.h"
37 #include "AliH2F.h"
38 #include "AliArrayI.h"
39 #include "AliArrayS.h"
40 #include "AliDigits.h"
41
42
43
44 //_____________________________________________________________________________
45 //_____________________________________________________________________________
46 //_____________________________________________________________________________
47 ClassImp(AliDigits)
48
49
50 AliDigits::AliDigits()
51 {
52   fIndex = 0;
53   fElements = 0;
54   fThreshold =0;
55   Invalidate();
56 }
57
58 AliDigits::~AliDigits()
59 {
60   //
61   //defaulta destructor
62   if (fIndex !=0 ) fIndex->Delete();;
63   if (fElements != 0) fElements->Delete();
64   
65 }
66
67
68 Bool_t AliDigits::OutOfBoundsError(const char *where, Int_t row, Int_t column) 
69 {
70    // Generate an out-of-bounds error. Always returns false.
71    ::Error(where, "row %d  col %d out of bounds (size: %d x %d, this: 0x%08x)", 
72            row, column, fNrows, fNcols, this);
73    return kFALSE;
74 }
75
76
77 void AliDigits::Invalidate() 
78
79   //
80   //set default (invalid parameters)
81   if (fIndex != 0)  delete  fIndex;
82   fIndex = new AliArrayI;
83   
84   if (fElements!= 0)     delete  fElements;
85   
86   fElements = new AliArrayS;
87   
88   fNrows = fNcols =fNelems= -1; 
89   fElements->Set(0); 
90   fIndex->Set(0); 
91   fBufType = -1;
92 }
93
94 void AliDigits::Allocate(Int_t rows, Int_t columns)
95 {
96   //
97   //construct empty buffer fDigits with size rows x columns
98   Invalidate();
99   if (rows <= 0) {
100       Error("Allocate", "no of rows has to be positive");
101       return;
102    }
103    if (columns <= 0) {
104       Error("Allocate", "no of columns has to be positive");
105       return;
106    }
107   fNrows = rows;
108   fNcols=columns;
109   fNelems = fNrows * fNcols;
110   fElements->Set(fNelems);
111   fIndex->Set(fNcols);
112   for (Int_t i =0,k=0; i<fNcols;i++,k+=fNrows) 
113   (*fIndex)[i]=k;
114   fBufType =0;
115 }
116
117
118 Short_t AliDigits::GetDigit(Int_t row, Int_t column)
119 {
120   if (fBufType ==0) return GetDigitFast(row,column);
121   if (fBufType ==1) return GetDigit1(row,column);
122
123   return 0;
124 }
125
126
127 void AliDigits::ExpandBuffer()
128 {  
129   //
130   //expand buffer to two dimensional array
131   if (fBufType<0)  {
132     Error("ExpandBuffer", "buffer doesn't exist");
133     return;
134   }
135   if (fBufType==0)      return;
136   
137   //expanding of buffer type 1
138   if (fBufType==1) ExpandBuffer1();
139   
140   fBufType = 0;
141 }
142
143 void AliDigits::CompresBuffer(Int_t bufferType,Int_t threshold)
144 {
145   //
146   //compres buffer according buffertype algorithm
147   if (fBufType<0)  {
148     Error("CompressBuffer", "buffer doesn't exist");
149     return;
150   }
151   if (fBufType == bufferType) return;
152   //
153   if (fBufType>0) ExpandBuffer();
154   if (fBufType !=0)  {
155     Error("CompressBuffer", "buffer doesn't exist");
156     return;
157   }
158   fThreshold = threshold;
159   //compress buffer of type 1
160   if ( bufferType == 1) CompresBuffer1();//end of compresing bufer of type 1 
161 }
162
163 Bool_t AliDigits::First()
164 {
165   //adjust  first valid current digit
166   if (fBufType ==0) return First0();
167   if (fBufType ==1) return First1();
168   return kFALSE;
169 }
170
171 Bool_t  AliDigits::Next()
172 {
173   //addjust next valid current digit
174   if (fBufType ==0) return Next0();
175   if (fBufType ==1) return Next1();
176   return kFALSE;
177 }
178  
179 void AliDigits::AcceptHisto(AliH2F * his)
180 {
181   //
182   //make digits buffer with value according histograms values
183   //for testing purpose  
184   Int_t idim =his->GetNbinsX();
185   Int_t jdim =his->GetNbinsY();
186   if ( (idim<1)|| (jdim<1)) {
187     return;
188   }
189   //allocate proper buffer size
190   Allocate(idim,jdim);
191   //set digits values
192   for (Int_t i = 0; i<idim;i++)    
193     for (Int_t j = 0; j<jdim;j++)
194       {
195         Int_t index = his->GetBin(i+1,j+1);     
196         SetDigitFast((Short_t)his->GetBinContent(index),i,j);
197       }   
198 }
199
200 AliH2F *  AliDigits::GenerHisto()
201 {
202   //
203   //make digits histo 
204   char ch[30];
205   sprintf(ch,"Segment_%d ",GetID());
206   if ( (fNrows<1)|| (fNcols<1)) {
207     return 0;
208   }
209   AliH2F * his  = new AliH2F("Digit histo",ch,fNrows,0,fNrows,fNcols,0,fNcols);
210   ExpandBuffer();
211   //set histogram  values
212   for (Int_t i = 0; i<fNrows;i++)    
213     for (Int_t j = 0; j<fNcols;j++)
214         his->Fill(i,j,GetDigitFast(i,j));
215   return his;
216 }
217
218 AliH2F *  AliDigits::Draw(const char *option,Float_t x1, Float_t x2, Float_t y1, Float_t y2)
219 {
220   //
221   //draw digits in given array
222   //
223   AliH2F *h2f = GenerHisto();
224   if (x1>=0) {
225       AliH2F *h2fsub = h2f->GetSubrange2d(x1,x2,y1,y2);
226       delete h2f;
227       h2f=h2fsub;
228   }
229   if (h2f==0) return 0;
230   if (option!=0) h2f->Draw(option);
231   else h2f->Draw();
232   return h2f;  
233 }
234
235 void AliDigits::ExpandBuffer1()
236 {
237   //
238   //expand buffer of type to twodimensional array
239   Int_t i,k;
240   fNelems = fNrows*fNcols;
241   Short_t * buf = new Short_t[fNelems];
242   fIndex->Set(fNcols);
243   for (i =0,k=0 ;i<fNcols;i++,k+=fNrows) (*fIndex)[i]=k;
244   Int_t col=0;
245   Int_t row = 0;
246   Int_t N=fElements->fN;
247   for (i=0;i<N;i++){
248     //oposite signa means how many unwrited (under threshold) values
249     if ((*fElements)[i]<0) row-=fElements->At(i); 
250     else {
251       buf[(*fIndex)[col]+row]=fElements->At(i);
252       row++;
253     }
254     if (row==fNrows) {
255       row=0;
256       col++;
257     }else 
258       if (row>fNrows){
259         Invalidate();
260         return;
261       }      
262   }
263   fElements->Adopt(fNelems,buf);    
264 }
265 void AliDigits::CompresBuffer1()
266 {
267   //
268   //compres buffer according  algorithm 1
269   //
270   AliArrayS  buf;  //lets have the nearly the "worst case"
271   buf.Set(fNelems);
272   AliArrayI  index;
273   index.Set(fNcols);
274   Int_t icurrent=-1;
275   Int_t izero;
276   for (Int_t col = 0; col<fNcols; col++){      
277     index[col]=icurrent+1;//set collumn pointer
278     izero = 0;  //reset zer counter at the begining of the column
279     for (Int_t row = 0; row< fNrows;row++){
280       //if under threshold
281       if (GetDigitFast(row,col)<=fThreshold)  izero++;
282       else{
283         if (izero>0) {
284           //if we have currently izero count under threshold
285           icurrent++;     
286           if (icurrent>=buf.fN) buf.Expand(icurrent*2);
287           buf[icurrent]= -izero;  //write how many under zero
288           izero = 0;
289         } //end of reseting izero
290         icurrent++;
291         if (icurrent>=buf.fN) buf.Expand(icurrent*2);
292         buf[icurrent] = GetDigitFast(row,col);      
293       }//if signal bigger then threshold                
294     } //end of loop over rows
295     if (izero>0) {
296       icurrent++;         
297       if (icurrent>=buf.fN) buf.Expand(icurrent*2);
298       buf[icurrent]= -izero;  //write how many under zero
299     }
300   }//end of lopping over digits
301   buf.Expand(icurrent+1);
302   (*fElements)=buf;
303   fNelems = fElements->fN;
304   fBufType = 1;
305   (*fIndex) =index;
306   //end of compresing bufer of type 1 
307 }
308
309
310
311 Bool_t AliDigits::First0()
312 {
313   //
314   //first for the buffer type 0
315   fCurrentRow = -1;
316   fCurrentCol = -1;
317   fCurrentIndex = -1;
318   Int_t i;
319   for (i=0; (( i<fNelems) && (fElements->At(i)<=fThreshold));i++)
320   if (i == fNelems) return kFALSE;
321   fCurrentCol =i/fNrows;
322   fCurrentRow =i%fNrows;
323   fCurrentIndex = i;
324   return kTRUE; 
325 }
326
327 Bool_t AliDigits::Next0()
328 {
329   //
330   //next for the buffer type 0
331   //
332   if (fCurrentIndex<0) return kFALSE;  // if we didn't adjust first 
333   Int_t i;
334   for (i=fCurrentIndex+1; ( (i<fNelems) && (fElements->At(i)<=fThreshold) ) ;i++);
335   if (i >= fNelems)  {
336     fCurrentIndex = -1;
337     return kFALSE;
338   }
339   fCurrentCol =i/fNrows;
340   fCurrentRow =i%fNrows;
341   fCurrentIndex = i;
342   return kTRUE; 
343 }
344
345 Bool_t AliDigits::First1()
346 {
347   //
348   //first for the buffer type 1
349   fCurrentRow = -1;
350   fCurrentCol = 0;
351   fCurrentIndex = -1;
352   Int_t i;
353   for (i=0; i<fNelems; i++){
354     if (fElements->At(i) < 0) fCurrentRow-=fElements->At(i);
355     else      
356       fCurrentRow++;
357     if (fCurrentRow>=fNrows) {
358        fCurrentCol++;
359        fCurrentRow-=fNrows;
360     }
361     if (fElements->At(i)>fThreshold) break;
362   }
363   fCurrentIndex = i;
364   if (fCurrentIndex>=0) return kTRUE;
365   fCurrentRow =-1;
366   fCurrentCol =-1;
367   return kFALSE;        
368 }
369
370 Bool_t AliDigits::Next1()
371 {
372   //
373   //next for the buffer type 1
374   if (fCurrentIndex<0) return kFALSE;  // if we didn't adjust first 
375   Int_t i;
376   for (i=fCurrentIndex+1; i<fNelems;i++){
377     if (fElements->At(i) < 0) fCurrentRow-=fElements->At(i);
378     else      
379       fCurrentRow++;
380     if (fCurrentRow>=fNrows) {
381       fCurrentCol++;
382       fCurrentRow-=fNrows;
383     }
384     if (fElements->At(i)>fThreshold) break;
385   }
386   fCurrentIndex = i;
387   if ( (i>=0) && (i<fNelems) ) return kTRUE;
388   fCurrentRow =-1;
389   fCurrentCol =-1;
390   return kFALSE;
391 }
392
393 Short_t AliDigits::GetDigit1(Int_t row, Int_t column)
394 {
395   //
396   //return digit for given row and column  the buffer type 1
397   //no control performed
398   
399   Int_t i,n2;
400   if ( (column+1)>=fNcols) n2 = fNelems;
401   else
402     n2 = fIndex->At(column+1);
403   Int_t irow = 0; //current row    
404  
405   for (i=fIndex->At(column); ( (i<n2) && (irow<row) );i++){
406     if (fElements->At(i) < 0) irow-=fElements->At(i);
407     else      
408       irow++;
409   }
410   if ( irow == row ) return fElements->At(i);
411   return -1;
412 }
413