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