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