Corrected UInt_t <-> Int_t conversion
[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.
60721370 129 ::Error(where, "row %d col %d out of bounds (size: %d x %d, this: 0x%08lx)",
130 row, column, fNrows, fNcols, (ULong_t) this);
a46e9031 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 //
81b42d36 179 //return size of object as represented in the memory
a46e9031 180 //
81b42d36 181 // Int_t size = sizeof(this);
182 Int_t size = 0; // COVERITY consider the previous statment as bug
183 //
a46e9031 184 if (fIndex!=0) size+= sizeof(fIndex)+fIndex->GetSize()*sizeof(Int_t);
185 if (fElements!=0) size+= sizeof(fElements)+fElements->GetSize()*sizeof(Short_t);
186 return size;
187}
188
189Int_t AliDigits::GetDigitSize() //return total size of pure digit
190{
191 //
192 //return size of PURE DIGITS
193 //
194 if (fElements==0) return 0;
195 else return sizeof(fElements)+fElements->GetSize()*sizeof(Short_t);
196}
197
198Int_t AliDigits::GetOverTh(Float_t threshold,Float_t x1, Float_t x2, Float_t y1, Float_t y2)
199{
200 //
201 //return number of digits over threshold
202 //
203 if ( (fElements==0) || (fElements->GetSize()<=0)) return 0;
204
205 if (x1<=x2) {
206 x1=0;
207 x2=fNrows;
208 }
209 if (y1<=y2) {
210 y1=0;
211 y2=fNcols;
212 }
213 Int_t over=0;
214
215 Bool_t cont=First();
216 for ( cont=First(); cont==kTRUE;cont=Next()) {
217 if ( (CurrentRow()<x1) || (CurrentRow()>x2)) continue;
218 if ( (CurrentColumn()<y1) || (CurrentColumn()>y2)) continue;
219 if (CurrentDigit()>threshold) over++;
220 }
221 return over;
222}
223
224
225Short_t AliDigits::GetDigit(Int_t row, Int_t column)
226{
227 //
228 // return digit for given row and collumn
229 if (fBufType ==0) return GetDigitFast(row,column);
230 if (fBufType ==1) return GetDigit1(row,column);
231
232 return 0;
233}
234
235
236void AliDigits::ExpandBuffer()
237{
238 //
239 //expand buffer to two dimensional array
240 if (fBufType<0) {
241 Error("ExpandBuffer", "buffer doesn't exist");
242 return;
243 }
244 if (fBufType==0) return;
245
246 //expanding of buffer type 1
247 if (fBufType==1) ExpandBuffer1();
248
249 fBufType = 0;
250}
251
252void AliDigits::CompresBuffer(Int_t bufferType,Int_t threshold)
253{
254 //
255 //compres buffer according buffertype algorithm
256 if (fBufType<0) {
257 Error("CompressBuffer", "buffer doesn't exist");
258 return;
259 }
260 if (fBufType == bufferType) return;
261 //
262 if (fBufType>0) ExpandBuffer();
263 if (fBufType !=0) {
264 Error("CompressBuffer", "buffer doesn't exist");
265 return;
266 }
267 fThreshold = threshold;
268 //compress buffer of type 1
269 if ( bufferType == 1) CompresBuffer1();//end of compresing bufer of type 1
270}
271
272Bool_t AliDigits::First()
273{
274 //adjust first valid current digit
275 if (fBufType ==0) return First0();
276 if (fBufType ==1) return First1();
277 return kFALSE;
278}
279
280Bool_t AliDigits::Next()
281{
282 //addjust next valid current digit
283 if (fBufType ==0) return Next0();
284 if (fBufType ==1) return Next1();
285 return kFALSE;
286}
287
288void AliDigits::AcceptHisto(AliH2F * his)
289{
290 //
291 //make digits buffer with value according histograms values
292 //for testing purpose
293 Int_t idim =his->GetNbinsX();
294 Int_t jdim =his->GetNbinsY();
295 if ( (idim<1)|| (jdim<1)) {
296 return;
297 }
298 //allocate proper buffer size
299 Allocate(idim,jdim);
300 //set digits values
301 for (Int_t i = 0; i<idim;i++)
302 for (Int_t j = 0; j<jdim;j++)
303 {
304 Int_t index = his->GetBin(i+1,j+1);
305 SetDigitFast((Short_t)his->GetBinContent(index),i,j);
306 }
307}
308
309AliH2F * AliDigits::GenerHisto()
310{
311 //
312 //make digits histo
313 char ch[30];
50791637 314 snprintf(ch,30, "Segment_%d ",GetID());
a46e9031 315 if ( (fNrows<1)|| (fNcols<1)) {
316 return 0;
317 }
318 AliH2F * his = new AliH2F("Digit histo",ch,fNrows,0,fNrows,fNcols,0,fNcols);
319 ExpandBuffer();
320 //set histogram values
321 for (Int_t i = 0; i<fNrows;i++)
322 for (Int_t j = 0; j<fNcols;j++)
323 his->Fill(i,j,GetDigitFast(i,j));
324 return his;
325}
326
327AliH2F *AliDigits::DrawDigits(const char *option,Float_t x1, Float_t x2, Float_t y1, Float_t y2)
328{
329 //
330 //draw digits in given array
331 //
332 AliH2F *h2f = GenerHisto();
333 if (x1>=0) {
334 AliH2F *h2fsub = h2f->GetSubrange2d(x1,x2,y1,y2);
335 delete h2f;
336 h2f=h2fsub;
337 }
338 if (h2f==0) return 0;
339 if (option!=0) h2f->Draw(option);
340 else h2f->Draw();
341 return h2f;
342}
343
344void AliDigits::ExpandBuffer1()
345{
346 //
347 //expand buffer of type to twodimensional array
348 Int_t i,k;
349 fNelems = fNrows*fNcols;
350 Short_t * buf = new Short_t[fNelems];
557a9086 351 memset(buf,0,fNelems*sizeof(Short_t)); //MI change - 4.12.2000
a46e9031 352 fIndex->Set(fNcols);
353 for (i =0,k=0 ;i<fNcols;i++,k+=fNrows) (*fIndex)[i]=k;
354 Int_t col=0;
355 Int_t row = 0;
356 Int_t n=fElements->fN;
357 for (i=0;i<n;i++){
358 //oposite signa means how many unwrited (under threshold) values
359 if ((*fElements)[i]<0) row-=fElements->At(i);
360 else {
361 buf[(*fIndex)[col]+row]=fElements->At(i);
362 row++;
363 }
364 if (row==fNrows) {
365 row=0;
366 col++;
367 }else
368 if (row>fNrows){
369 Invalidate();
50791637 370 delete [] buf;
a46e9031 371 return;
372 }
373 }
374 fElements->Adopt(fNelems,buf);
375}
51a4705b 376
a46e9031 377void AliDigits::CompresBuffer1()
378{
379 //
380 //compres buffer according algorithm 1
381 //
e756ece8 382 TArrayS buf; //lets have the nearly the "worst case"
a46e9031 383 buf.Set(fNelems);
e756ece8 384 TArrayI index;
a46e9031 385 index.Set(fNcols);
386 Int_t icurrent=-1;
387 Int_t izero;
51a4705b 388 Short_t * cbuff = fElements->GetArray(); //MI change
389
a46e9031 390 for (Int_t col = 0; col<fNcols; col++){
391 index[col]=icurrent+1;//set collumn pointer
392 izero = 0; //reset zer counter at the begining of the column
393 for (Int_t row = 0; row< fNrows;row++){
394 //if under threshold
51a4705b 395 //if (GetDigitFast(row,col)<=fThreshold) izero++;
396 if (*cbuff<=fThreshold) izero++;
397
a46e9031 398 else{
399 if (izero>0) {
400 //if we have currently izero count under threshold
401 icurrent++;
e756ece8 402 if (icurrent>=buf.fN) buf.Set(icurrent*2);
a46e9031 403 buf[icurrent]= -izero; //write how many under zero
404 izero = 0;
405 } //end of reseting izero
406 icurrent++;
e756ece8 407 if (icurrent>=buf.fN) buf.Set(icurrent*2);
51a4705b 408 //buf[icurrent] = GetDigitFast(row,col);
409 buf[icurrent] = *cbuff;
410 }//if signal bigger then threshold
411 cbuff++;
a46e9031 412 } //end of loop over rows
413 if (izero>0) {
414 icurrent++;
e756ece8 415 if (icurrent>=buf.fN) buf.Set(icurrent*2);
a46e9031 416 buf[icurrent]= -izero; //write how many under zero
417 }
418 }//end of lopping over digits
e756ece8 419 buf.Set(icurrent+1);
a46e9031 420 (*fElements)=buf;
421 fNelems = fElements->fN;
422 fBufType = 1;
423 (*fIndex) =index;
424 //end of compresing bufer of type 1
425}
426
427
428
429Bool_t AliDigits::First0()
430{
431 //
432 //first for the buffer type 0
433 fCurrentRow = -1;
434 fCurrentCol = -1;
435 fCurrentIndex = -1;
436 Int_t i;
4f57cad7 437 for (i=0; (( i<fNelems) && (fElements->At(i)<=fThreshold));i++) {} //MI1211
a46e9031 438 if (i == fNelems) return kFALSE;
439 fCurrentCol =i/fNrows;
440 fCurrentRow =i%fNrows;
441 fCurrentIndex = i;
442 return kTRUE;
443}
444
445Bool_t AliDigits::Next0()
446{
447 //
448 //next for the buffer type 0
449 //
450 if (fCurrentIndex<0) return kFALSE; // if we didn't adjust first
451 Int_t i;
4f57cad7 452 for (i=fCurrentIndex+1; ( (i<fNelems) && (fElements->At(i)<=fThreshold) ) ;i++) {}
a46e9031 453 if (i >= fNelems) {
454 fCurrentIndex = -1;
455 return kFALSE;
456 }
457 fCurrentCol =i/fNrows;
458 fCurrentRow =i%fNrows;
459 fCurrentIndex = i;
460 return kTRUE;
461}
462
463Bool_t AliDigits::First1()
464{
465 //
466 //first for the buffer type 1
467 fCurrentRow = -1;
468 fCurrentCol = 0;
469 fCurrentIndex = -1;
470 Int_t i;
471 for (i=0; i<fNelems; i++){
472 if (fElements->At(i) < 0) fCurrentRow-=fElements->At(i);
473 else
474 fCurrentRow++;
475 if (fCurrentRow>=fNrows) {
476 fCurrentCol++;
477 fCurrentRow-=fNrows;
478 }
479 if (fElements->At(i)>fThreshold) break;
480 }
481 fCurrentIndex = i;
a1681743 482 if (fCurrentIndex>=0&&i<fNelems) return kTRUE;
a46e9031 483 fCurrentRow =-1;
484 fCurrentCol =-1;
485 return kFALSE;
486}
487
488Bool_t AliDigits::Next1()
489{
490 //
491 //next for the buffer type 1
492 if (fCurrentIndex<0) return kFALSE; // if we didn't adjust first
493 Int_t i;
494 for (i=fCurrentIndex+1; i<fNelems;i++){
495 if (fElements->At(i) < 0) fCurrentRow-=fElements->At(i);
496 else
497 fCurrentRow++;
498 if (fCurrentRow>=fNrows) {
499 fCurrentCol++;
500 fCurrentRow-=fNrows;
501 }
502 if (fElements->At(i)>fThreshold) break;
503 }
504 fCurrentIndex = i;
505 if ( (i>=0) && (i<fNelems) ) return kTRUE;
506 fCurrentRow =-1;
507 fCurrentCol =-1;
508 return kFALSE;
509}
510
511Short_t AliDigits::GetDigit1(Int_t row, Int_t column)
512{
513 //
514 //return digit for given row and column the buffer type 1
515 //no control performed
516
517 Int_t i,n2;
518 if ( (column+1)>=fNcols) n2 = fNelems;
519 else
520 n2 = fIndex->At(column+1);
521 Int_t irow = 0; //current row
522
523 for (i=fIndex->At(column); ( (i<n2) && (irow<row) );i++){
524 if (fElements->At(i) < 0) irow-=fElements->At(i);
525 else
526 irow++;
527 }
528 if ( irow == row ) return fElements->At(i);
529 return -1;
530}
531