]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDSignalIndex.cxx
memset only the necessary part of the sorted array
[u/mrichter/AliRoot.git] / TRD / AliTRDSignalIndex.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 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 //  General container for data from TRD detector segments                    //
21 //  Adapted from AliDigits, origin M.Ivanov                                  //
22 //                                                                           //
23 //  Author:                                                                  //
24 //    Mateusz Ploskon (ploskon@ikf.uni-frankfurt.de)                         //
25 //                                                                           //
26 ///////////////////////////////////////////////////////////////////////////////
27
28 #include <algorithm>
29 #include "TObject.h"
30 #include "AliLog.h"
31 #include "AliTRDSignalIndex.h"
32
33 ClassImp(AliTRDSignalIndex)
34
35 //_____________________________________________________________________________
36 AliTRDSignalIndex::AliTRDSignalIndex()
37   :TObject()
38   ,fDet(-1)
39   ,fLayer(-1)
40   ,fStack(-1)
41   ,fSM(-1)
42   ,fBoolIndex(NULL)
43   ,fSortedIndex(NULL)
44   ,fMaxLimit(0)
45   ,fPositionRC(0)
46   ,fCountRC(1)
47   ,fSortedWasInit(kFALSE)
48   ,fCurrRow(0)
49   ,fCurrCol(0)
50   ,fCurrTbin(0)
51   ,fNrows(0)
52   ,fNcols(0)
53   ,fNtbins(0)
54 {
55   //
56   // Default contructor
57   //
58
59   ResetCounters();
60
61 }
62
63 //_____________________________________________________________________________
64 AliTRDSignalIndex::AliTRDSignalIndex(Int_t nrow, Int_t ncol,Int_t ntime)
65   :TObject()
66   ,fDet(-1)
67   ,fLayer(-1)
68   ,fStack(-1)
69   ,fSM(-1)
70   ,fBoolIndex(NULL)
71   ,fSortedIndex(NULL)
72   ,fMaxLimit(0)
73   ,fPositionRC(0)
74   ,fCountRC(1)
75   ,fSortedWasInit(kFALSE)
76   ,fCurrRow(0)
77   ,fCurrCol(0)
78   ,fCurrTbin(0)
79   ,fNrows(0)
80   ,fNcols(0)
81   ,fNtbins(0)
82 {
83   //
84   // Not the default contructor... hmmm...
85   //
86
87   Allocate(nrow, ncol, ntime);  
88
89 }
90
91 //_____________________________________________________________________________
92 AliTRDSignalIndex::AliTRDSignalIndex(const AliTRDSignalIndex &a)
93   :TObject(a)
94   ,fDet(a.fDet)
95   ,fLayer(a.fLayer)
96   ,fStack(a.fStack)
97   ,fSM(a.fSM)
98   ,fBoolIndex(NULL)
99   ,fSortedIndex(NULL)
100   ,fMaxLimit(a.fMaxLimit)
101   ,fPositionRC(a.fPositionRC)
102   ,fCountRC(a.fCountRC)
103   ,fSortedWasInit(a.fSortedWasInit)
104   ,fCurrRow(a.fCurrRow)
105   ,fCurrCol(a.fCurrCol)
106   ,fCurrTbin(a.fCurrTbin)
107   ,fNrows(a.fNrows)
108   ,fNcols(a.fNcols)
109   ,fNtbins(a.fNtbins)
110 {
111   //
112   // Copy constructor
113   //
114
115   fBoolIndex = new Bool_t[fMaxLimit];
116   memcpy(fBoolIndex, a.fBoolIndex, fMaxLimit*sizeof(Bool_t));
117
118   fSortedIndex = new RowCol[fMaxLimit+1];
119   memcpy(fSortedIndex, a.fSortedIndex, (fMaxLimit+1)*sizeof(RowCol));
120 }
121
122 //_____________________________________________________________________________
123 AliTRDSignalIndex::~AliTRDSignalIndex()
124 {
125   //
126   // Destructor
127   //
128
129   if (fBoolIndex) {
130     delete [] fBoolIndex;
131     fBoolIndex = NULL;
132   }
133
134 if (fSortedIndex) {
135     delete [] fSortedIndex;
136     fSortedIndex = NULL;
137   }
138
139 }
140
141 //_____________________________________________________________________________
142 void AliTRDSignalIndex::Copy(TObject &a) const
143 {
144   //
145   // Copy function
146   //
147
148   ((AliTRDSignalIndex &)a).fDet           = fDet;
149   ((AliTRDSignalIndex &)a).fLayer         = fLayer;
150   ((AliTRDSignalIndex &)a).fStack         = fStack;
151   ((AliTRDSignalIndex &)a).fSM            = fSM;
152   ((AliTRDSignalIndex &)a).fMaxLimit      = fMaxLimit;
153   ((AliTRDSignalIndex &)a).fPositionRC    = fPositionRC;
154   ((AliTRDSignalIndex &)a).fCountRC       = fCountRC;
155   ((AliTRDSignalIndex &)a).fSortedWasInit = fSortedWasInit;
156   ((AliTRDSignalIndex &)a).fCurrRow       = fCurrRow;
157   ((AliTRDSignalIndex &)a).fCurrCol       = fCurrCol;
158   ((AliTRDSignalIndex &)a).fCurrTbin      = fCurrTbin;
159   ((AliTRDSignalIndex &)a).fNrows         = fNrows;
160   ((AliTRDSignalIndex &)a).fNcols         = fNcols;
161   ((AliTRDSignalIndex &)a).fNtbins        = fNtbins;
162
163   if(((AliTRDSignalIndex &)a).fBoolIndex)
164     {
165       delete [] ((AliTRDSignalIndex &)a).fBoolIndex;
166     }
167   ((AliTRDSignalIndex &)a).fBoolIndex = new Bool_t[fMaxLimit];
168   memcpy(((AliTRDSignalIndex &)a).fBoolIndex, fBoolIndex, fMaxLimit*sizeof(Bool_t));
169
170   if(((AliTRDSignalIndex &)a).fSortedIndex)
171     {
172       delete [] ((AliTRDSignalIndex &)a).fSortedIndex;
173     }
174   ((AliTRDSignalIndex &)a).fSortedIndex = new RowCol[fMaxLimit+1];
175   memcpy(((AliTRDSignalIndex &)a).fSortedIndex, fSortedIndex, (fMaxLimit+1)*sizeof(RowCol));
176
177 }
178
179 //_____________________________________________________________________________
180 AliTRDSignalIndex& AliTRDSignalIndex::operator = (const AliTRDSignalIndex& a)
181 {
182   //
183   // Assignment operator
184   //
185
186   if (this != &a) ((AliTRDSignalIndex &) a).Copy(*this);
187   return *this;
188
189 }
190
191 //_____________________________________________________________________________
192 void AliTRDSignalIndex::Allocate(const Int_t nrow, const Int_t ncol, const Int_t ntime)
193 {
194   //
195   // Create the arrays
196   //
197
198   fNrows = nrow;
199   fNcols = ncol;
200   fNtbins = ntime;
201
202   fMaxLimit = nrow * ncol + 1;
203
204   if (fBoolIndex) {
205     delete [] fBoolIndex;
206     fBoolIndex = NULL;
207   }
208   if (fSortedIndex) {
209     delete [] fSortedIndex;
210     fSortedIndex = NULL;
211   }
212
213   fBoolIndex = new Bool_t[fMaxLimit];
214   fSortedIndex = new RowCol[fMaxLimit+1];
215
216   fCountRC = fMaxLimit+1;
217
218   ResetArrays();
219   ResetCounters();
220
221   fCountRC = 1;
222
223 }
224
225 //_____________________________________________________________________________
226 void AliTRDSignalIndex::ResetArrays()
227 {
228   memset(fBoolIndex,0x00,sizeof(Bool_t)*fMaxLimit);
229   memset(fSortedIndex,0xFF,sizeof(RowCol)*fCountRC); 
230   fSortedWasInit = kFALSE;
231 }
232
233 //_____________________________________________________________________________
234 void AliTRDSignalIndex::Reset()
235 {
236   //
237   // Reset the array but keep the size - realloc
238   //
239
240   fDet   = -1;
241   fLayer = -1;
242   fStack = -1;
243   fSM    = -1;
244
245   // All will be lost 
246   Allocate(fNrows, fNcols, fNtbins);
247
248 }
249
250 //_____________________________________________________________________________
251 void AliTRDSignalIndex::ResetContent()
252 {
253   //
254   // Reset the array but keep the size - no realloc
255   //
256
257   fDet   = -1;
258   fLayer = -1;
259   fStack = -1;
260   fSM    = -1;
261
262   ResetArrays();
263   ResetCounters();
264
265   fCountRC = 1;
266
267 }
268
269 //_____________________________________________________________________________
270 void AliTRDSignalIndex::ResetContentConditional(const Int_t nrow, const Int_t ncol, const Int_t ntime)
271 {
272   //
273   // Reset the array but keep the size if no need to enlarge - no realloc
274   //
275
276   fDet   = -1;
277   fLayer = -1;
278   fStack = -1;
279   fSM    = -1;
280
281   if ((nrow  > fNrows) || 
282       (ncol  > fNcols) || 
283       (ntime > fNtbins)) {
284     Allocate(nrow, ncol, ntime);
285   }
286   else {
287     ResetArrays();
288     ResetCounters();
289     fCountRC = 1;
290   }
291
292 }
293
294 //_____________________________________________________________________________
295 void AliTRDSignalIndex::ClearAll()
296 {
297   //
298   // Reset the values - clear all!
299   //
300
301   fDet    = -1;
302   fLayer  = -1;
303   fStack  = -1;
304   fSM     = -1;
305
306   fNrows  = -1;
307   fNcols  = -1;
308   fNtbins = -1;
309
310   if (fBoolIndex) {
311     delete [] fBoolIndex;
312     fBoolIndex = NULL;
313   }
314
315   if (fSortedIndex) {
316     delete [] fSortedIndex;
317     fSortedIndex = NULL;
318   }
319   
320   ResetCounters();
321
322   fCountRC = 1;
323   fSortedWasInit = kFALSE;
324   fMaxLimit = 0;
325
326 }
327
328 //_____________________________________________________________________________
329 Bool_t  AliTRDSignalIndex::NextRCIndex(Int_t &row, Int_t &col)
330 {
331   //
332   // Returns next used RC combination
333   //
334
335   if(fSortedIndex[fPositionRC].rc>-1){
336     row = fCurrRow = fSortedIndex[fPositionRC].s.row;
337     col = fCurrCol = fSortedIndex[fPositionRC].s.col;
338     fPositionRC++;
339     return kTRUE;
340   }
341   else {
342     if(fSortedWasInit || fCountRC==1)
343       { //we already reached the end of the array
344         ResetCounters();
345         row = fCurrRow;
346         col = fCurrCol;
347         return kFALSE;
348       }
349     else
350       { //we have not created the sorted array up to now, let's do so
351         InitSortedIndex();
352         return NextRCIndex(row, col);
353       }
354   }
355
356 }
357
358 //_____________________________________________________________________________
359 Bool_t AliTRDSignalIndex::NextRCTbinIndex(Int_t &row, Int_t &col, Int_t &tbin)
360 {
361   //
362   // Returns the next tbin, or if there is no next time bin, it returns the
363   // next used RC combination.
364   //  
365
366   if (NextTbinIndex(tbin)) {
367     row = fCurrRow;
368     col = fCurrCol;
369     return kTRUE;
370   }
371   else {
372     if (NextRCIndex(row, col)) {
373       return NextRCTbinIndex(row, col, tbin);
374     }
375   }
376
377   return kFALSE;
378
379 }
380
381 //_____________________________________________________________________________
382 Bool_t AliTRDSignalIndex::NextTbinIndex(Int_t &tbin)
383 {
384   //
385   // Returns the next tbin of the current RC combination
386   //
387   
388   if(fCurrTbin<fNtbins)
389     {
390       tbin = fCurrTbin++;
391       return kTRUE;
392     }
393
394   return kFALSE;
395
396 }
397
398 //_____________________________________________________________________________
399 void AliTRDSignalIndex::InitSortedIndex()
400 {
401   //
402   // Creates the SortedIndex
403   //
404
405   fSortedWasInit = kTRUE;
406   std::sort((UShort_t*)fSortedIndex, ((UShort_t*)fSortedIndex) + fCountRC);
407
408 }
409
410 //_____________________________________________________________________________
411 void AliTRDSignalIndex::ResetCounters()    
412
413   //
414   // Reset the counters/iterators
415   //
416
417   fCurrRow    = -1;
418   fCurrCol    = -1;
419   fCurrTbin   = -1;
420   fPositionRC =  0;
421 }