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