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