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