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