]> git.uio.no Git - u/mrichter/AliRoot.git/blame - TRD/AliTRDdataArrayS.cxx
Coding rules
[u/mrichter/AliRoot.git] / TRD / AliTRDdataArrayS.cxx
CommitLineData
625f5260 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/* $Id: AliTRDdataArrayS.cxx,v 1.17 2006/08/28 14:37:05 cblume Exp $ */
17
18///////////////////////////////////////////////////////////////////////////////
19// //
20// General container for integer data of a TRD detector segment. //
21// Adapted from AliDigits (origin: M.Ivanov). //
22// //
23///////////////////////////////////////////////////////////////////////////////
24
25#include "AliLog.h"
26
27#include "AliTRDdataArrayS.h"
28#include "AliTRDarrayI.h"
29#include "AliTRDarrayS.h"
30
31ClassImp(AliTRDdataArrayS)
32
33//_____________________________________________________________________________
34AliTRDdataArrayS::AliTRDdataArrayS()
35 :AliTRDdataArray()
36 ,fElements(0)
37 ,fThreshold(0)
38{
39 //
40 // Default constructor
41 //
42
43}
44
45//_____________________________________________________________________________
46AliTRDdataArrayS::AliTRDdataArrayS(Int_t nrow, Int_t ncol, Int_t ntime)
47 :AliTRDdataArray(nrow,ncol,ntime)
48 ,fElements(0)
49 ,fThreshold(0)
50{
51 //
52 // Creates a AliTRDdataArrayS with the dimensions <nrow>, <ncol>, and <ntime>.
53 // The row- and column dimensions are compressible.
54 //
55
56 Allocate(nrow,ncol,ntime);
57
58}
59
60//_____________________________________________________________________________
61AliTRDdataArrayS::AliTRDdataArrayS(const AliTRDdataArrayS &a)
62 :AliTRDdataArray(a)
63 ,fElements(a.fElements)
64 ,fThreshold(a.fThreshold)
65{
66 //
67 // AliTRDdataArrayS copy constructor
68 //
69
70}
71
72//_____________________________________________________________________________
73AliTRDdataArrayS::~AliTRDdataArrayS()
74{
75 //
76 // Destructor
77 //
78
79 if (fElements) {
80 delete fElements;
81 fElements = 0;
82 }
83
84}
85
86//_____________________________________________________________________________
87void AliTRDdataArrayS::Allocate(Int_t nrow, Int_t ncol, Int_t ntime)
88{
89 //
90 // Allocates memory for a AliTRDdataArrayS with the dimensions
91 // <nrow>, <ncol>, and <ntime>.
92 // The row- and column dimensions are compressible.
93 //
94
95 if (fNelems < 0) {
96 AliTRDdataArray::Allocate(nrow,ncol,ntime);
97 }
98
99 if (fElements) {
100 delete fElements;
101 }
102
103 fElements = new AliTRDarrayS();
104 fElements->Set(fNelems);
105 fElements->Reset();
106
107}
108
109//_____________________________________________________________________________
110void AliTRDdataArrayS::Copy(TObject &a) const
111{
112 //
113 // Copy function
114 //
115
116 fElements->Copy(*((AliTRDdataArrayS &) a).fElements);
117
118 ((AliTRDdataArrayS &) a).fThreshold = fThreshold;
119
120 AliTRDdataArray::Copy(a);
121
122}
123
124//_____________________________________________________________________________
125void AliTRDdataArrayS::Reset()
126{
127 //
128 // Reset the array (old content gets deleted)
129 //
130
131 if (fElements) {
132 delete fElements;
133 }
134
135
136 fElements = new AliTRDarrayS();
137 fElements->Set(0);
138
139 AliTRDdataArray::Reset();
140
141
142}
143
144//_____________________________________________________________________________
145Int_t AliTRDdataArrayS::GetSize() const
146{
147 //
148 // Returns the size of the complete object
149 //
150
151 Int_t size = sizeof(this);
152
153 if (fIndex) size += sizeof(fIndex)
154 + fIndex->GetSize() * sizeof(Int_t);
155 if (fElements) size += sizeof(fElements)
156 + fElements->GetSize() * sizeof(Short_t);
157
158 return size;
159
160}
161
162//_____________________________________________________________________________
163Int_t AliTRDdataArrayS::GetDataSize() const
164{
165 //
166 // Returns the size of only the data part
167 //
168
169 if (fElements == 0)
170 return 0;
171 else
172 return sizeof(fElements) + fElements->GetSize() * sizeof(Short_t);
173
174}
175
176//_____________________________________________________________________________
177Int_t AliTRDdataArrayS::GetOverThreshold(Short_t threshold)
178{
179 //
180 // Returns the number of entries over threshold
181 //
182
183 if ((fElements == 0) || (fElements->GetSize() <= 0))
184 return 0;
185
186 Int_t over = 0;
187
188 for (Bool_t cont = First(); cont == kTRUE; cont = Next()) {
189 if ((fCurrentIdx1 < 0) || (fCurrentIdx1 >= fNdim1)) continue;
190 if ((fCurrentIdx2 < 0) || (fCurrentIdx2 >= fNdim2)) continue;
191 if (fElements->At(fCurrentIndex) > threshold) over++;
192 }
193
194 return over;
195
196}
197
198//_____________________________________________________________________________
199Short_t AliTRDdataArrayS::GetData(Int_t row, Int_t col, Int_t time) const
200{
201 //
202 // Returns the data value at a given position of the array
203 //
204
205 if ((row >= 0) && (col >= 0) && (time >= 0)) {
206 Int_t idx1 = GetIdx1(row,col);
207 if ((idx1 >= 0) && (time < fNdim2)) {
208 if (fBufType == 0) return GetDataFast(idx1,time);
209 if (fBufType == 1) return GetData1(idx1,time);
210 }
211 else {
212 if (idx1 >= 0) {
213 AliError(Form("time %d out of bounds (size: %d, this: 0x%08x)"
214 ,time,fNdim2,this));
215 }
216 }
217 }
218
219 return -1;
220
221}
222
223//_____________________________________________________________________________
224Short_t AliTRDdataArrayS::GetDataFast(Int_t idx1, Int_t idx2) const
225{
226 //
227 // Returns the data value at a given position of the array
228 // No boundary checking
229 //
230
231 return fElements->At(fIndex->At(idx2)+idx1);
232
233}
234
235//_____________________________________________________________________________
236void AliTRDdataArrayS::Compress(Int_t bufferType, Short_t threshold)
237{
238 //
239 // Compresses the buffer
240 //
241
242 fThreshold = threshold;
243 Compress(bufferType);
244
245}
246
247//_____________________________________________________________________________
248void AliTRDdataArrayS::Compress(Int_t bufferType)
249{
250 //
251 // Compresses the buffer
252 //
253
254 if (fBufType < 0) {
255 AliError("Buffer does not exist");
256 return;
257 }
258 if (fBufType == bufferType) {
259 return;
260 }
261 if (fBufType > 0) {
262 Expand();
263 }
264 if (fBufType !=0) {
265 AliError("Buffer does not exist");
266 return;
267 }
268
269 // Compress a buffer of type 1
270 if (bufferType == 1) {
271 Compress1();
272 }
273
274}
275
276//_____________________________________________________________________________
277void AliTRDdataArrayS::Expand()
278{
279 //
280 // Expands the compressed buffer
281 //
282
283 if (fBufType < 0) {
284 AliError("Buffer does not exist");
285 return;
286 }
287 if (fBufType == 0) {
288 return;
289 }
290
291 // Expand a buffer of type 1
292 if (fBufType == 1) Expand1();
293
294 fBufType = 0;
295
296}
297
298//_____________________________________________________________________________
299Bool_t AliTRDdataArrayS::First()
300{
301 //
302 // Returns the position of the first valid data value
303 //
304
305 if (fBufType == 0) return First0();
306 if (fBufType == 1) return First1();
307 return kFALSE;
308
309}
310
311//_____________________________________________________________________________
312Bool_t AliTRDdataArrayS::Next()
313{
314 //
315 // Returns the position of the next valid data value
316 //
317
318 if (fBufType == 0) return Next0();
319 if (fBufType == 1) return Next1();
320 return kFALSE;
321
322}
323
324//_____________________________________________________________________________
325void AliTRDdataArrayS::Expand1()
326{
327 //
328 // Expands a buffer of type 1
329 //
330
331 Int_t i, k;
332
333 fNelems = fNdim1 * fNdim2;
334
335 Short_t *buf = new Short_t[fNelems];
336 memset(buf,0,fNelems*sizeof(Short_t));
337
338 fIndex->Set(fNdim2);
339
340 for (i = 0, k = 0; i < fNdim2; i++, k += fNdim1) (*fIndex)[i] = k;
341
342 Int_t idx1 = 0;
343 Int_t idx2 = 0;
344 Int_t n = fElements->fN;
345
346 for (i = 0; i < n; i++){
347
348 // Negative sign counts the unwritten values (under threshold)
349 if ((*fElements)[i] < 0) {
350 idx1 -= (*fElements)[i];
351 }
352 else {
353 buf[(*fIndex)[idx2] + idx1] = (*fElements)[i];
354 idx1++;
355 }
356 if (idx1 == fNdim1) {
357 idx1 = 0;
358 idx2++;
359 }
360 else {
361 if (idx1 > fNdim1){
362 Reset();
363 return;
364 }
365 }
366 }
367
368 fElements->Adopt(fNelems,buf);
369
370}
371
372//_____________________________________________________________________________
373void AliTRDdataArrayS::Compress1()
374{
375 //
376 // Compress a buffer of type 1
377 //
378
379 AliTRDarrayS *buf = new AliTRDarrayS();
380 buf->Set(fNelems);
381 AliTRDarrayI *index = new AliTRDarrayI();
382 index->Set(fNdim2);
383
384 Int_t icurrent = -1;
385 Int_t izero;
386 for (Int_t idx2 = 0; idx2 < fNdim2; idx2++){
387
388 // Set the idx2 pointer
389 (*index)[idx2] = icurrent + 1;
390
391 // Reset the zero counter
392 izero = 0;
393
394 for (Int_t idx1 = 0; idx1 < fNdim1; idx1++){
395 // If below threshold
396 if (GetDataFast(idx1,idx2) <= fThreshold) {
397 izero++;
398 }
399 else {
400 if (izero > 0) {
401 // If we have currently izero counts under threshold
402 icurrent++;
403 if (icurrent >= buf->fN) buf->Expand(icurrent*2);
404 // Store the number of entries below zero
405 (*buf)[icurrent] = -izero;
406 izero = 0;
407 }
408 icurrent++;
409 if (icurrent >= buf->fN) buf->Expand(icurrent*2);
410 (*buf)[icurrent] = GetDataFast(idx1,idx2);
411 } // If signal larger than threshold
412 } // End of loop over idx1
413
414 if (izero > 0) {
415 icurrent++;
416 if (icurrent >= buf->fN) buf->Expand(icurrent*2);
417 // Store the number of entries below zero
418 (*buf)[icurrent] = -izero;
419 }
420
421 }
422
423 buf->Expand(icurrent+1);
424 if (fElements) delete fElements;
425 fElements = buf;
426 fNelems = fElements->fN;
427 fBufType = 1;
428 if (fIndex) delete fIndex;
429 fIndex = index;
430
431}
432
433//_____________________________________________________________________________
434void AliTRDdataArrayS::Expand2()
435{
436 //
437 // Expands a buffer of type 2
438 //
439
440 Int_t i, k;
441
442 Short_t *buf = new Short_t[fNelems];
443 memset(buf,0,fNelems*sizeof(Short_t));
444
445 fNelems = fNdim1 * fNdim2;
446 fIndex->Set(fNdim2);
447
448 for (i = 0, k = 0; i < fNdim2; i++, k += fNdim1) (*fIndex)[i] = k;
449
450 Int_t idx1 = 0;
451 Int_t idx2 = 0;
452 Int_t n = fElements->fN;
453 for (i = 0; i < n; i++){
454 // Negative sign counts the unwritten values (under threshold)
455 if ((*fElements)[i] < 0) {
456 idx1 -= fElements->At(i);
457 }
458 else {
459 buf[(*fIndex)[idx2]+idx1] = fElements->At(i);
460 idx1++;
461 }
462 if (idx1 == fNdim1) {
463 idx1 = 0;
464 idx2++;
465 }
466 else {
467 if (idx1 > fNdim1){
468 Reset();
469 return;
470 }
471 }
472 }
473
474 fElements->Adopt(fNelems,buf);
475
476}
477
478//_____________________________________________________________________________
479void AliTRDdataArrayS::Compress2()
480{
481 //
482 // Compress a buffer of type 2 - not implemented!
483 //
484
485}
486
487//_____________________________________________________________________________
488Bool_t AliTRDdataArrayS::First0()
489{
490 //
491 // Returns the first entry for a buffer of type 0
492 //
493
494 fCurrentIdx1 = -1;
495 fCurrentIdx2 = -1;
496 fCurrentIndex = -1;
497
498 Int_t i;
499 for (i = 0; ((i < fNelems) && (fElements->At(i) <= fThreshold)); i++)
500 if (i == fNelems) return kFALSE;
501
502 fCurrentIdx1 = i % fNdim1;
503 fCurrentIdx2 = i / fNdim1;
504 fCurrentIndex = i;
505 return kTRUE;
506
507}
508
509//_____________________________________________________________________________
510Bool_t AliTRDdataArrayS::Next0()
511{
512 //
513 // Returns the next entry for a buffer of type 0
514 //
515
516 if (fCurrentIndex < 0) return kFALSE;
517
518 Int_t i;
519 for (i = fCurrentIndex + 1;
520 ((i < fNelems) && (fElements->At(i) <= fThreshold));
4f57cad7 521 i++) {}
625f5260 522 if (i >= fNelems) {
523 fCurrentIndex = -1;
524 return kFALSE;
525 }
526
527 fCurrentIdx1 = i % fNdim1;
528 fCurrentIdx2 = i / fNdim1;
529 fCurrentIndex = i;
530 return kTRUE;
531
532}
533
534//_____________________________________________________________________________
535Bool_t AliTRDdataArrayS::First1()
536{
537 //
538 // Returns the first entry for a buffer of type 1
539 //
540
541 fCurrentIdx1 = -1;
542 fCurrentIdx2 = 0;
543 fCurrentIndex = -1;
544
545 Int_t i;
546 for (i = 0; i < fNelems; i++){
547 if (fElements->At(i) < 0) {
548 fCurrentIdx1 -= fElements->At(i);
549 }
550 else {
551 fCurrentIdx1++;
552 }
553 if (fCurrentIdx1 >= fNdim1) {
554 fCurrentIdx2++;
555 fCurrentIdx1 -= fNdim1;
556 }
557 if (fElements->At(i) > fThreshold) break;
558 }
559
560 fCurrentIndex = i;
561 if (fCurrentIndex >= 0) return kTRUE;
562 fCurrentIdx1 = -1;
563 fCurrentIdx2 = -1;
564 return kFALSE;
565
566}
567
568//_____________________________________________________________________________
569Bool_t AliTRDdataArrayS::Next1()
570{
571 //
572 // Returns the next entry for a buffer of type 1
573 //
574
575 if (fCurrentIndex < 0) return kFALSE;
576
577 Int_t i;
578 for (i = fCurrentIndex + 1; i < fNelems; i++){
579 if (fElements->At(i) < 0) {
580 fCurrentIdx1 -= fElements->At(i);
581 }
582 else {
583 fCurrentIdx1++;
584 }
585 if (fCurrentIdx1 >= fNdim1) {
586 fCurrentIdx2++;
587 fCurrentIdx1 -= fNdim1;
588 }
589 if (fElements->At(i) > fThreshold) break;
590 }
591
592 fCurrentIndex = i;
593 if ((i >= 0) && (i < fNelems)) return kTRUE;
594 fCurrentIdx1 = -1;
595 fCurrentIdx2 = -1;
596 return kFALSE;
597
598}
599
600//_____________________________________________________________________________
601Short_t AliTRDdataArrayS::GetData1(Int_t idx1, Int_t idx2) const
602{
603 //
604 // Returns the value at a given position of the array
605 //
606
607 Int_t i, n2;
608
609 if ((idx2 + 1) >= fNdim2) {
610 n2 = fNelems;
611 }
612 else {
613 n2 = fIndex->At(idx2 + 1);
614 }
615
616 // Current idx1
617 Int_t curidx1 = 0;
618
619 for (i = fIndex->At(idx2); ((i < n2) && (curidx1 < idx1)); i++){
620 if (fElements->At(i) < 0) {
621 curidx1 -= fElements->At(i);
622 }
623 else {
624 curidx1++;
625 }
626 }
627
628 if ((curidx1 == idx1) && (fElements->At(i) > 0)) {
629 return fElements->At(i);
630 }
631 else {
632 return 0;
633 }
634
635}
636
637//_____________________________________________________________________________
638void AliTRDdataArrayS::SetData(Int_t row, Int_t col, Int_t time, Short_t value)
639{
640 //
641 // Sets the data value at a given position of the array
642 // Includes boundary checking
643 //
644
645 if ((row >= 0) && (col >= 0) && (time >= 0)) {
646 Int_t idx1 = GetIdx1(row,col);
647 if ((idx1 >= 0) && (time < fNdim2)) {
648 SetDataFast(idx1,time,value);
649 }
650 else {
651 if (idx1 >= 0) {
652 AliError(Form("time %d out of bounds (size: %d, this: 0x%08x)"
653 ,time,fNdim2,this));
654 }
655 }
656 }
657
658}
659
660//_____________________________________________________________________________
661void AliTRDdataArrayS::SetDataFast(Int_t idx1, Int_t idx2, Short_t value)
662{
663 //
664 // Sets the data value at a given position of the array
665 // No boundary checking
666 //
667
668 (*fElements)[fIndex->fArray[idx2]+idx1] = value;
669
670}
671
672//_____________________________________________________________________________
673AliTRDdataArrayS &AliTRDdataArrayS::operator=(const AliTRDdataArrayS &a)
674{
675 //
676 // Assignment operator
677 //
678
679 if (this != &a) ((AliTRDdataArrayS &) a).Copy(*this);
680 return *this;
681
682}