]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/TPCLib/AliHLTTPCMemHandler.cxx
corrected compilation warnings
[u/mrichter/AliRoot.git] / HLT / TPCLib / AliHLTTPCMemHandler.cxx
1 // @(#) $Id$
2 // Original: AliHLTMemHandler.cxx,v 1.52 2005/06/14 10:55:21 cvetan 
3
4 /**************************************************************************
5  * This file is property of and copyright by the ALICE HLT Project        * 
6  * ALICE Experiment at CERN, All rights reserved.                         *
7  *                                                                        *
8  * Primary Authors: U. Frankenfeld, A. Vestbo, C. Loizides                *
9  *                  Matthias Richter <Matthias.Richter@ift.uib.no>        *
10  *                  for The ALICE HLT Project.                            *
11  *                                                                        *
12  * Permission to use, copy, modify and distribute this software and its   *
13  * documentation strictly for non-commercial purposes is hereby granted   *
14  * without fee, provided that the above copyright notice appears in all   *
15  * copies and that both the copyright notice and this permission notice   *
16  * appear in the supporting documentation. The authors make no claims     *
17  * about the suitability of this software for any purpose. It is          *
18  * provided "as is" without express or implied warranty.                  *
19  **************************************************************************/
20
21 /** @file   AliHLTTPCMemHandler.cxx
22     @author U. Frankenfeld, A. Vestbo, C. Loizides, maintained by
23             Matthias Richter
24     @date   
25     @brief  input interface base class for the TPC tracking code before
26             migration to the HLT component framework
27
28 // see below for class documentation
29 // or
30 // refer to README to build package
31 // or
32 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
33                                                                           */
34 /** \class AliHLTTPCMemHandler 
35 <pre>
36 //_____________________________________________________________
37 // AliHLTTPCMemHandler
38 //
39 // The HLT Binary File handler 
40 //
41 //  This class does all the memory I/O handling of HLT binary files.
42 //  
43 //  Examples:
44 //  ---------
45 //
46 //  1) Reading a binary file:
47 //  
48 //  AliHLTTPCMemHandler file;
49 //  file.SetBinaryInput(filename);
50 //  file.Init(slice,patch);
51 //
52 //  UInt_t nrowss;
53 //  AliHLTTPCDigitRowData *data = file.CompBinary2Memory(nrows);
54 //  
55 //  for(int i=0; i<nrows; i++) 
56 //    {
57 //    
58 //    AliHLTTPCDigitData *dataPt = (AliHLTTPCDigitData*)data->fDigitData;
59 //    for(int j=0; j<data->fNDigit; j++) 
60 //      {
61 //        pad = dataPt[j].fPad;
62 //        time = dataPt[j].fTime;
63 //        charge = dataPt[j].fCharge;
64 //      }
65 //     
66 //    file.UpdateRowPointer(data);
67 //  
68 //    }
69 //  file.CloseBinaryInput();
70 //  ________________________
71 //  
72 //  2) Writing a binary file:
73 //  
74 //  //First of all you need to store the data in memory,
75 //  //and have a pointer to it of type AliHLTTPCDigitRowData.
76 //  //E.g. if you just want to write the data you read in example 1)
77 //  //into a new file, you can do the following:
78 //  
79 //  AliHLTTPCMemHandler newfile;
80 //  newfile.Init(slice,patch);
81 //  newfile.SetBinaryOutput(newfilename);
82 //  newfile.Memory2CompBinary((UInt_t)NumberOfRowsInPatch,(AliHLTTPCDigitRowData*)data);
83 //  newfile.CloseBinaryOutput();
84 //
85 //
86 // Compressed file format:
87 // -----------------------
88 //
89 // The data is RLE encoded and currently using _10_ bit range for the ADC-values.
90 </pre>
91 */  
92
93 #include "AliHLTTPCRootTypes.h"
94 #include "AliHLTTPCDigitData.h"
95 #include "AliHLTTPCLogging.h"
96 #include "AliHLTTPCTransform.h"
97 #include "AliHLTTPCTrackSegmentData.h"
98 #include "AliHLTTPCSpacePointData.h"
99 #include "AliHLTTPCTrackArray.h"
100 #include "AliHLTTPCMemHandler.h"
101
102 #if __GNUC__ >= 3
103 using namespace std;
104 #endif
105   
106 ClassImp(AliHLTTPCMemHandler)
107   
108 AliHLTTPCMemHandler::AliHLTTPCMemHandler()
109   :
110   fRowMin(0),
111   fRowMax(0),
112   fSlice(0),
113   fPatch(0),
114   fInBinary(NULL),
115   fOutBinary(NULL),
116   fPt(NULL),
117   fSize(0),
118   fIsRandom(kFALSE),
119   fNRandom(0),
120   fNGenerate(0),
121   fNUsed(0),
122   fNDigits(0),
123   fDPt(NULL),
124   fRandomDigits(NULL),
125   fDummy(0)
126
127   //Constructor
128   Init(0,0);
129   ResetROI();
130 }
131
132 AliHLTTPCMemHandler::~AliHLTTPCMemHandler()
133 {
134   //Destructor
135   if(fPt) delete[] fPt;
136   if(fRandomDigits) delete [] fRandomDigits;
137   if(fDPt) delete [] fDPt;
138 }
139
140 void AliHLTTPCMemHandler::Init(Int_t s,Int_t p, Int_t *r)
141 {
142   //init handler
143   fSlice=s;fPatch=p;
144   if(r) {
145     fRowMin=r[0];
146     fRowMax=r[1];
147   }else{
148      fRowMin=AliHLTTPCTransform::GetFirstRow(p);
149      fRowMax=AliHLTTPCTransform::GetLastRow(p); 
150   }
151   ResetROI();
152 }
153
154 void AliHLTTPCMemHandler::ResetROI()
155 {
156   //Resets the Look-up table for Region of Interest mode.
157   for(Int_t i=fRowMin; i<=fRowMax; i++)
158     {
159       fEtaMinTimeBin[i] = 0;
160       fEtaMaxTimeBin[i] = AliHLTTPCTransform::GetNTimeBins()-1;
161     }
162 }
163
164 void AliHLTTPCMemHandler::SetROI(Float_t *eta,Int_t */*slice*/)
165 {
166   // Init the Look-up table for the Region of Interest mode.
167   //   Here you can specify a certain etaregion, - all data
168   //   outside this region will be discarded:
169   //   eta[0] = mimium eta
170   //   eta[1] = maximum eta
171   //   slice[0] = mimumum slice
172   //   slice[1] = maximum slice
173
174
175   if(eta[1]==0)
176     {
177       LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::SetROI","Eta Values")
178         <<"Bad ROI parameters. IDIOT! "<<ENDLOG;
179       for(Int_t i=fRowMin; i<=fRowMax; i++)
180         {
181           fEtaMinTimeBin[i]=0;
182           fEtaMaxTimeBin[i]=0;
183         }
184       return;
185     }
186   
187   for(Int_t i=fRowMin; i<=fRowMax; i++)
188     {
189       Int_t sector,row;
190       Float_t xyz[3];
191       
192       Float_t thetamax = 2*atan(exp(-1.*eta[1]));
193       
194       xyz[0] = AliHLTTPCTransform::Row2X(i);
195       xyz[1]=0;
196       xyz[2] = xyz[0]/tan(thetamax);
197       AliHLTTPCTransform::Slice2Sector(fSlice,i,sector,row);
198       AliHLTTPCTransform::Local2Raw(xyz,sector,row);
199       
200       fEtaMinTimeBin[i] = (Int_t)xyz[2];
201       
202       if(eta[0]==0)
203         fEtaMaxTimeBin[i] = 445;
204       else
205         {
206           Float_t thetamin = 2*atan(exp(-1.*eta[0]));
207           xyz[0] = AliHLTTPCTransform::Row2X(i);
208           xyz[1] = AliHLTTPCTransform::GetMaxY(i);
209           Float_t radii = sqrt(pow(xyz[0],2) + pow(xyz[1],2));
210           xyz[2] = radii/tan(thetamin);
211           AliHLTTPCTransform::Local2Raw(xyz,sector,row);
212           fEtaMaxTimeBin[i] = (Int_t)xyz[2];
213         }
214     }
215   
216 }
217
218 Bool_t AliHLTTPCMemHandler::SetBinaryInput(char *name)
219 {
220   //Set the input binary file.
221   fInBinary = fopen(name,"r");
222   if(!fInBinary){
223     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::SetBinaryInput","File Open")
224       <<"Error opening file "<<name<<ENDLOG;
225     return kFALSE;
226   }
227   return kTRUE;
228 }
229
230 Bool_t AliHLTTPCMemHandler::SetBinaryInput(FILE *file)
231 {
232   //Set the input binary file.
233   fInBinary = file;
234   if(!fInBinary){
235     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::SetBinaryInput","File Open")
236     <<"Pointer to File = 0x0 "<<ENDLOG;
237     return kFALSE;
238   }
239   return kTRUE;
240 }
241
242 void AliHLTTPCMemHandler::CloseBinaryInput()
243 {
244   //Close the input file.
245   if(!fInBinary){
246     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::CloseBinaryInput","File Close")
247       <<"Nothing to Close"<<ENDLOG;
248     return;
249   }
250   fclose(fInBinary);
251   fInBinary =0;
252 }
253
254 Bool_t AliHLTTPCMemHandler::SetBinaryOutput(char *name)
255 {
256   //Set the binary output file.
257     fOutBinary = fopen(name,"w");
258   if(!fOutBinary){
259     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::SetBinaryOutput","File Open")
260       <<"Pointer to File = 0x0 "<<ENDLOG;
261     return kFALSE;
262   }
263   return kTRUE;
264 }
265
266 Bool_t AliHLTTPCMemHandler::SetBinaryOutput(FILE *file)
267 {
268   //Set the binary output file.
269     fOutBinary = file;
270   if(!fOutBinary){
271     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::SetBinaryOutput","File Open")
272       <<"Pointer to File = 0x0 "<<ENDLOG;
273     return kFALSE;
274   }
275   return kTRUE;
276 }
277
278 void AliHLTTPCMemHandler::CloseBinaryOutput()
279 {
280   //close binary  
281   if(!fOutBinary){
282     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::CloseBinaryOutPut","File Close")
283       <<"Nothing to Close"<<ENDLOG;
284     return;
285   }
286   fclose(fOutBinary);
287   fOutBinary =0;
288 }
289
290 UInt_t AliHLTTPCMemHandler::GetFileSize()
291 {
292   //Returns the file size in bytes of the input file.
293   if(!fInBinary){
294     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::GetFileSize","File")
295       <<"No Input File"<<ENDLOG;
296     return 0;
297   }
298   fseek(fInBinary,0,SEEK_END);
299   UInt_t size = (UInt_t) ftell(fInBinary);
300   rewind(fInBinary);
301   return size; 
302 }
303
304 Byte_t *AliHLTTPCMemHandler::Allocate()
305 {
306   //Allocate
307   return Allocate(GetFileSize()); 
308 }
309
310 Byte_t *AliHLTTPCMemHandler::Allocate(AliHLTTPCTrackArray *array)
311 {
312   //Allocate memory for tracks in memory. Used by TrackArray2Binary()
313   if(!array){
314     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Allocate","Memory")
315       <<"Pointer to AliHLTTPCTrackArray = 0x0 "<<ENDLOG;
316     return 0;
317   }
318   return Allocate(array->GetOutSize()); 
319 }
320
321 Byte_t *AliHLTTPCMemHandler::Allocate(UInt_t size)
322 {
323   //Allocate memory of size in bytes.
324   if(fPt){
325     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Allocate","Memory")
326       <<"Delete Memory"<<ENDLOG;
327     Free();
328   } 
329   fPt = new Byte_t[size];
330   fSize = size;
331   memset(fPt,0,fSize);
332   LOG(AliHLTTPCLog::kDebug,"AliHLTTPCMemHandler::Allocate","Memory")
333   <<AliHLTTPCLog::kDec<<"Allocate "<<size<<" Bytes of Memory"<<ENDLOG;
334   return fPt;
335 }
336
337 void AliHLTTPCMemHandler::Free()
338 {
339   //Clear the memory, if allocated.
340   if(!fPt){
341     //    LOG(AliHLTTPCLog::kInformational,"AliHLTTPCMemHandler::Free","Memory")
342     //      <<"No Memory allocated - can't Free"<<ENDLOG;
343     return;
344   }  
345   delete[] fPt;
346   fPt = 0;
347   fSize =0;
348 }
349
350 ///////////////////////////////////////// Random
351 void AliHLTTPCMemHandler::SetRandomSeed()
352 {
353   //If you are adding random data to the original data.
354   time_t *tp=0;
355   SetRandomSeed(time(tp));
356 }
357
358 void AliHLTTPCMemHandler::SetRandomCluster(Int_t maxnumber)
359 {
360   //If you are adding random data to the original data.
361   
362   fIsRandom = kTRUE;
363   fNRandom = maxnumber;
364   fNDigits = 0;
365   if(fRandomDigits) delete [] fRandomDigits;
366   fRandomDigits = new AliHLTTPCRandomDigitData[fNRandom*9];
367   if(fDPt) delete [] fDPt;
368   fDPt = new AliHLTTPCRandomDigitData *[fNRandom*9];
369 }
370
371 void AliHLTTPCMemHandler::QSort(AliHLTTPCRandomDigitData **a, Int_t first, Int_t last)
372 {
373
374    // Sort array of AliHLTTPCRandomDigitData pointers using a quicksort algorithm.
375    // Uses CompareDigits() to compare objects.
376    // Thanks to Root!
377
378    static AliHLTTPCRandomDigitData *tmp;
379    static int i;           // "static" to save stack space
380    int j;
381
382    while (last - first > 1) {
383       i = first;
384       j = last;
385       for (;;) {
386          while (++i < last && CompareDigits(a[i], a[first]) < 0)
387             ;
388          while (--j > first && CompareDigits(a[j], a[first]) > 0)
389             ;
390          if (i >= j)
391             break;
392
393          tmp  = a[i];
394          a[i] = a[j];
395          a[j] = tmp;
396       }
397       if (j == first) {
398          ++first;
399          continue;
400       }
401       tmp = a[first];
402       a[first] = a[j];
403       a[j] = tmp;
404
405       if (j - first < last - (j + 1)) {
406         QSort(a, first, j);
407         first = j + 1;   // QSort(j + 1, last);
408       } else {
409         QSort(a, j + 1, last);
410         last = j;        // QSort(first, j);
411       }
412    }
413 }
414
415 UInt_t AliHLTTPCMemHandler::GetRandomSize() const
416 {
417   //get random size
418   Int_t nrandom = 0;
419   for(Int_t r=fRowMin;r<=fRowMax;r++){
420     Int_t npad=AliHLTTPCTransform::GetNPads(r);
421     nrandom  += Int_t (fNGenerate * ((Double_t) npad/141.));
422   }
423   return 9 * nrandom * sizeof(AliHLTTPCDigitData);
424 }
425
426 void AliHLTTPCMemHandler::Generate(Int_t row)
427 {
428   //Generate random data on row, if you didn't 
429   //ask for this, nothing happens here.
430   
431   if(!fIsRandom) return;
432   ResetRandom();
433   fNDigits = 0;
434   Int_t npad=AliHLTTPCTransform::GetNPads(row);
435   Int_t ntime = fEtaMaxTimeBin[row] - fEtaMinTimeBin[row];
436   Int_t nrandom  = Int_t (fNGenerate * ((Double_t) npad/141.) * 
437                           (Double_t) ntime/(Double_t) AliHLTTPCTransform::GetNTimeBins() );
438   
439   for(Int_t n=0;n<nrandom;n++){
440     Int_t pad = (int)((float)rand()/RAND_MAX*npad);
441     Int_t time =(int)((float)rand()/RAND_MAX*ntime+fEtaMinTimeBin[row] );
442     Int_t charge = (int)((float)rand()/RAND_MAX*AliHLTTPCTransform::GetADCSat());
443     DigitizePoint(row,pad,time,charge);
444   }
445   QSort(fDPt,0,fNDigits);
446 }
447
448
449 void AliHLTTPCMemHandler::DigitizePoint(Int_t row, Int_t pad, 
450                                     Int_t time,Int_t charge)
451 {
452   //Making one single random cluster.
453   for(Int_t j=-1;j<2;j++){
454     for(Int_t k=-1;k<2;k++){
455       Int_t dcharge = charge;
456       if(j) dcharge /=2;
457       if(k) dcharge /=2;
458       if(dcharge<10) continue;
459       Int_t dpad  = j + pad;
460       Int_t dtime = k + time;
461       
462       if(dpad<0||dpad>=AliHLTTPCTransform::GetNPads(row))  continue;
463       if(dtime<0||dtime>=AliHLTTPCTransform::GetNTimeBins()) continue;
464       
465       fRandomDigits[fNDigits].fCharge = dcharge;
466       fRandomDigits[fNDigits].fRow = row;
467       fRandomDigits[fNDigits].fPad = dpad;
468       fRandomDigits[fNDigits].fTime = dtime;
469       fDPt[fNDigits] = &fRandomDigits[fNDigits];
470       fNDigits++;
471     }
472   }
473 }
474
475 ///////////////////////////////////////// Digit IO  
476 Bool_t AliHLTTPCMemHandler::Memory2BinaryFile(UInt_t nrow,AliHLTTPCDigitRowData *data)
477 {
478   //Write data to the outputfile as is. No run-length encoding is done.
479
480   if(!fOutBinary){
481     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Memory2Binary","File")
482       <<"No Output File"<<ENDLOG;
483     return kFALSE;
484   }
485   if(!data){
486     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Memory2Binary","Memory")
487       <<"Pointer to AliHLTTPCDigitRowData = 0x0 "<<ENDLOG;
488     return kFALSE;
489   }
490   
491   AliHLTTPCDigitRowData *rowPt = data; 
492   Int_t outsize = 0;
493   for(UInt_t i=0;i<nrow;i++){
494     Int_t size = sizeof(AliHLTTPCDigitData) * rowPt->fNDigit 
495       + sizeof(AliHLTTPCDigitRowData);
496     outsize += size;
497     fwrite(rowPt,size,1,fOutBinary);
498     Byte_t  *bytePt =(Byte_t *) rowPt;
499     bytePt += size;
500     rowPt = (AliHLTTPCDigitRowData *) bytePt;
501   }
502   LOG(AliHLTTPCLog::kDebug,"AliHLTTPCMemHandler::Memory2Binary","Memory")
503     <<AliHLTTPCLog::kDec<<"Wrote "<<outsize<<" Bytes to Memory ("
504     <<nrow<<" Rows)"<<ENDLOG;
505   return kTRUE;
506 }
507
508 Bool_t AliHLTTPCMemHandler::Binary2Memory(UInt_t & nrow,AliHLTTPCDigitRowData *data,UInt_t& sz)
509 {
510   //Read inputfile into memory as is, and store it in data. 
511   // No run-length encoding is assumed.
512
513   if(!fInBinary){
514     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Binary2Memory","File")
515       <<"No Input File"<<ENDLOG;
516     return kFALSE;
517   }
518   if(!data){
519     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Binary2Memory","Memory")
520       <<"Pointer to AliHLTTPCDigitRowData = 0x0 "<<ENDLOG;
521     return kFALSE;
522   }
523   rewind(fInBinary);
524   AliHLTTPCDigitRowData *rowPt = data;
525   UInt_t rowcount = 0;
526   UInt_t outsize =0;
527   while(!feof(fInBinary)){
528     Byte_t  *bytePt =(Byte_t *) rowPt;
529
530     if (sz<outsize+sizeof(AliHLTTPCDigitRowData)) {
531       LOG(AliHLTTPCLog::kFatal,"AliHLTTPCMemHandler::Binary2Memory","Memory")
532         << "target data buffer too small" <<ENDLOG;
533       return kFALSE;
534     }
535     if(fread(rowPt,sizeof(AliHLTTPCDigitRowData),1,fInBinary)!=1) break;
536
537     bytePt += sizeof(AliHLTTPCDigitRowData);
538     outsize += sizeof(AliHLTTPCDigitRowData);
539
540     UInt_t size = sizeof(AliHLTTPCDigitData) * rowPt->fNDigit;
541
542     if (sz<outsize+size) {
543       LOG(AliHLTTPCLog::kFatal,"AliHLTTPCMemHandler::Binary2Memory","Memory")
544         << "target data buffer too small" <<ENDLOG;
545       return kFALSE;
546     }
547     //if(fread(bytePt,size,1,fInBinary)!=1) break;
548     fread(bytePt,size,1,fInBinary);
549     bytePt += size;
550     outsize += size;
551     rowPt = (AliHLTTPCDigitRowData *) bytePt;
552     rowcount++;
553   }  
554   nrow= rowcount;
555     LOG(AliHLTTPCLog::kDebug,"AliHLTTPCMemHandler::Binary2Memory","Memory")
556     <<AliHLTTPCLog::kDec<<"Wrote "<<outsize<<" Bytes to Memory ("
557     <<rowcount<<" Rows)"<<ENDLOG;
558   sz = outsize;
559   return kTRUE;
560 }
561
562 void AliHLTTPCMemHandler::AddData(AliHLTTPCDigitData *data,UInt_t & ndata,
563                               UInt_t /*row*/,UShort_t pad,UShort_t time,UShort_t charge) const
564 {
565   //add some data
566   data[ndata].fPad = pad;
567   data[ndata].fTime = time;
568   data[ndata].fCharge = charge;
569   ndata++;
570 }
571
572 void AliHLTTPCMemHandler::AddRandom(AliHLTTPCDigitData *data, UInt_t & ndata)
573 {
574   //add some random data
575   data[ndata].fPad = fDPt[fNUsed]->fPad;
576   data[ndata].fTime = fDPt[fNUsed]->fTime;
577   data[ndata].fCharge = fDPt[fNUsed]->fCharge;
578   ndata++;
579   fNUsed++;
580 }
581
582 void AliHLTTPCMemHandler::MergeDataRandom(AliHLTTPCDigitData *data, UInt_t & ndata,
583                                       UInt_t row, UShort_t pad, UShort_t time, UShort_t charge)
584 {
585   //merge random data
586   data[ndata].fPad = pad;
587   data[ndata].fTime = time;
588   data[ndata].fCharge = charge;
589   while(ComparePoints(row,pad,time)==0){
590     Int_t ch = data[ndata].fCharge + fDPt[fNUsed]->fCharge;
591     if(charge>=AliHLTTPCTransform::GetADCSat()) ch = AliHLTTPCTransform::GetADCSat();
592     data[ndata].fCharge = ch;
593     fNUsed++;
594   }
595   ndata++;
596 }
597
598 void AliHLTTPCMemHandler::AddDataRandom(AliHLTTPCDigitData *data, UInt_t & ndata,
599                    UInt_t row, UShort_t pad, UShort_t time, UShort_t charge)
600 {
601   //add data random
602   Int_t action;
603   while((action=ComparePoints(row,pad,time))==1){
604     AddRandom(data,ndata);
605   }
606   if(action==0){
607     MergeDataRandom(data,ndata,row,pad,time,charge);
608   }
609   if(action<0){
610     AddData(data,ndata,row,pad,time,charge);
611   }  
612 }
613
614 void AliHLTTPCMemHandler::Write(UInt_t *comp, UInt_t & index, 
615                             UInt_t & subindex, UShort_t value) const
616 {
617   //write compressed data
618   UInt_t shift[3] = {0,10,20};
619   if(subindex==0) comp[index] =0; //clean up memory
620   comp[index] |= (value&0x03ff)<<shift[subindex];
621   if(subindex == 2){
622     subindex = 0;
623     index++;
624   }
625   else subindex++;
626 }
627
628 UShort_t AliHLTTPCMemHandler::Read(UInt_t *comp, UInt_t & index, UInt_t & subindex) const
629
630   //read compressed data
631   UInt_t shift[3] = {0,10,20};
632   UShort_t value = (comp[index]>>shift[subindex])&0x03ff;
633   if(subindex == 2){
634     subindex = 0;
635     index++;
636   }
637   else subindex++;
638   
639   return value;
640 }
641
642 UShort_t AliHLTTPCMemHandler::Test(UInt_t *comp, 
643                                UInt_t index, UInt_t  subindex) const
644 {
645   //supi dupi test
646   UInt_t shift[3] = {0,10,20};
647   return (comp[index]>>shift[subindex])&0x03ff;
648 }
649
650 Int_t AliHLTTPCMemHandler::Memory2CompMemory(UInt_t nrow,
651                                          AliHLTTPCDigitRowData *data,UInt_t *comp)
652 {
653   //Performs run-length encoding on data stored in memory pointed to by data.
654   //The compressed data is written to comp.
655   if(!comp){
656     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Memory2CompMemory","Memory")
657       <<"Pointer to compressed data = 0x0 "<<ENDLOG;
658     return 0;
659   }
660   if(!data){
661     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Memory2CompMemory","Memory")
662       <<"Pointer to AliHLTTPCDigitRowData = 0x0 "<<ENDLOG;
663     return 0;
664   }
665   AliHLTTPCDigitRowData *rowPt = data;
666   UInt_t index=0;
667   UInt_t subindex=0;
668   
669   for(UInt_t i=0;i<nrow;i++){
670     UShort_t value = rowPt->fRow;
671     Write(comp,index,subindex,value);
672     UShort_t maxpad=0; 
673     UShort_t npad=0;
674     Int_t ddd[1000];
675     for(Int_t d=0;d<200;d++) ddd[d]=0;
676     for(UInt_t dig=0;dig<rowPt->fNDigit;dig++){
677       if(rowPt->fDigitData[dig].fPad <200){ 
678         ddd[rowPt->fDigitData[dig].fPad]++;
679       }
680     }
681     for(Int_t d=0;d<200;d++){ 
682       if(ddd[d]){
683         npad++;
684         maxpad =d;
685       }
686     }
687     Write(comp,index,subindex,npad);
688     UInt_t digit=0;
689     for(UShort_t pad=0;pad <= maxpad;pad++){
690       if(digit>=rowPt->fNDigit || rowPt->fDigitData[digit].fPad !=  pad)
691         continue;
692       Write(comp,index,subindex,pad);
693 //    write zero if time != 0
694       if(digit<rowPt->fNDigit && rowPt->fDigitData[digit].fPad == pad){
695         if(rowPt->fDigitData[digit].fTime>0){
696           Write(comp,index,subindex,0);
697           Write(comp,index,subindex,rowPt->fDigitData[digit].fTime);
698         }
699       }
700       while(digit<rowPt->fNDigit && rowPt->fDigitData[digit].fPad == pad){
701         UShort_t charge = rowPt->fDigitData[digit].fCharge;
702         if(charge>=1023){
703           charge=1023;
704         }
705         Write(comp,index,subindex,charge);
706         if(digit+1<rowPt->fNDigit&&rowPt->fDigitData[digit+1].fPad == pad){
707           if(rowPt->fDigitData[digit].fTime +1 !=
708                      rowPt->fDigitData[digit+1].fTime){
709             Write(comp,index,subindex,0);
710             UShort_t nzero = rowPt->fDigitData[digit+1].fTime - 
711                              (rowPt->fDigitData[digit].fTime +1);
712             Write(comp,index,subindex,nzero);
713           }  
714         }
715         digit++;
716       }
717       Write(comp,index,subindex,0);
718       Write(comp,index,subindex,0);
719     }
720     
721     Int_t size = sizeof(AliHLTTPCDigitData) * rowPt->fNDigit+
722                                             sizeof(AliHLTTPCDigitRowData);
723     Byte_t  *bytePt =(Byte_t *) rowPt;
724     bytePt += size;
725     rowPt = (AliHLTTPCDigitRowData *) bytePt;
726   }
727   while(subindex)
728     Write(comp,index,subindex,0);
729   return index * sizeof(UInt_t);
730 }
731
732 Int_t AliHLTTPCMemHandler::CompMemory2Memory(UInt_t  nrow,
733                                              AliHLTTPCDigitRowData *data,UInt_t *comp, UInt_t& sz)
734 {
735   //Uncompress the run-length encoded data in memory pointed to by comp, and
736   //  store it in data.
737
738   if(!comp){
739     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::CompMemory2Memory","Memory")
740       <<"Pointer to compressed data = 0x0 "<<ENDLOG;
741     return 0;
742   }
743   if(!data){
744     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::CompMemory2Memory","Memory")
745       <<"Pointer to AliHLTTPCDigitRowData = 0x0 "<<ENDLOG;
746     return 0;
747   }
748   Int_t outsize=0;
749   
750   AliHLTTPCDigitRowData *rowPt = data;
751   UInt_t index=0;
752   UInt_t subindex=0;
753   
754   for(UInt_t i=0;i<nrow;i++){
755     UInt_t ndigit=0;
756     UInt_t row =Read(comp,index,subindex);
757     rowPt->fRow=row;
758     Generate(row);
759     UShort_t npad = Read(comp,index,subindex);
760     for(UShort_t p=0;p<npad;p++){
761       UShort_t charge;
762       UShort_t time =0;
763       UShort_t pad = Read(comp,index,subindex);
764       if(Test(comp,index,subindex)==0){
765         Read(comp,index,subindex);
766         if( (time = Read(comp,index,subindex)) == 0 ){
767           continue;
768         }
769       }
770       for(;;){
771         while( (charge=Read(comp,index,subindex)) != 0){
772           if(time>=fEtaMinTimeBin[row]&&time<=fEtaMaxTimeBin[row])
773             //AddData(rowPt->fDigitData,ndigit,row,pad,time,charge);
774             //seems we are using this function... but dont know why
775             AddDataRandom(rowPt->fDigitData,ndigit,row,pad,time,charge);
776           time++;
777         }
778         UShort_t tshift = Read(comp,index,subindex);
779         if(tshift == 0) break;
780         time += tshift;
781       }
782     }
783     rowPt->fNDigit = ndigit;
784     Int_t size = sizeof(AliHLTTPCDigitData) * rowPt->fNDigit+
785       sizeof(AliHLTTPCDigitRowData);
786     Byte_t  *bytePt =(Byte_t *) rowPt;
787     bytePt += size;
788     outsize += size;
789     rowPt = (AliHLTTPCDigitRowData *) bytePt;
790   }
791   sz = outsize;
792   return outsize;
793 }
794
795 UInt_t AliHLTTPCMemHandler::GetCompMemorySize(UInt_t nrow,AliHLTTPCDigitRowData *data) const
796 {
797   //Return the size of RLE data, after compressing data.
798   
799   if(!data){
800     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::GetCompMemorySize","Memory")
801       <<"Pointer to AliHLTTPCDigitRowData = 0x0 "<<ENDLOG;
802     return 0;
803   }
804   AliHLTTPCDigitRowData *rowPt = data;
805   UInt_t index=0;
806   
807   for(UInt_t i=0;i<nrow;i++){
808     index++;
809     UShort_t maxpad=0; 
810     UShort_t npad=0;
811     Int_t ddd[1000];
812     for(Int_t d=0;d<200;d++) ddd[d]=0;
813     for(UInt_t dig=0;dig<rowPt->fNDigit;dig++){
814       if(rowPt->fDigitData[dig].fPad <200){ 
815         ddd[rowPt->fDigitData[dig].fPad]++;
816       }
817     }
818     for(Int_t d=0;d<200;d++){ 
819       if(ddd[d]){
820         npad++;
821         maxpad =d;
822       }
823     }
824     index++;
825     UInt_t digit=0;
826     for(UShort_t pad=0;pad <= maxpad;pad++){
827       if(digit>=rowPt->fNDigit || rowPt->fDigitData[digit].fPad !=  pad)
828         continue;
829       index++;
830       //    write zero if time != 0
831       if(digit<rowPt->fNDigit && rowPt->fDigitData[digit].fPad == pad){
832         if(rowPt->fDigitData[digit].fTime>0){
833           index++;
834           index++;
835         }
836       }
837       while(digit<rowPt->fNDigit && rowPt->fDigitData[digit].fPad == pad){
838         index++;
839         if(digit+1<rowPt->fNDigit&&rowPt->fDigitData[digit+1].fPad == pad){
840           if(rowPt->fDigitData[digit].fTime +1 !=
841                      rowPt->fDigitData[digit+1].fTime){
842             index++;
843             index++;
844           }  
845         }
846         digit++;
847       }
848       index++;
849       index++;
850     }
851
852     Int_t size = sizeof(AliHLTTPCDigitData) * rowPt->fNDigit+
853                                             sizeof(AliHLTTPCDigitRowData);
854     Byte_t  *bytePt =(Byte_t *) rowPt;
855     bytePt += size;
856     rowPt = (AliHLTTPCDigitRowData *) bytePt;
857   }
858   while(index%3)
859     index++;
860   return (index/3) * sizeof(UInt_t);
861 }
862
863 UInt_t AliHLTTPCMemHandler::GetMemorySize(UInt_t nrow,UInt_t *comp) const
864 {
865   //get memory size
866   if(!comp){
867     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::GetMemorySize","Memory")
868     <<"Pointer to compressed data = 0x0 "<<ENDLOG;
869     return 0;
870   }
871   Int_t outsize=0;
872
873   UInt_t index=0;
874   UInt_t subindex=0;
875
876   for(UInt_t i=0;i<nrow;i++){
877     UInt_t ndigit=0;
878     Read(comp,index,subindex);
879     UShort_t npad = Read(comp,index,subindex);
880     for(UShort_t p=0;p<npad;p++){
881       Read(comp,index,subindex);
882       if(Test(comp,index,subindex)==0){
883         Read(comp,index,subindex);
884         if(Read(comp,index,subindex)== 0) continue;
885       }
886       for(;;){
887         while(Read(comp,index,subindex)!=0) ndigit++;
888         if(Read(comp,index,subindex)==0) break;
889       }
890     }
891     Int_t size = sizeof(AliHLTTPCDigitData) * ndigit+
892                                         sizeof(AliHLTTPCDigitRowData);
893     outsize += size;
894   }
895    
896   return outsize;
897 }
898
899 UInt_t AliHLTTPCMemHandler::GetNRow(UInt_t *comp,UInt_t size)
900 {
901   //get number of rows
902   if(!comp){
903     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::GetNRow","Memory")
904       <<"Pointer to compressed data = 0x0 "<<ENDLOG;
905     return 0;
906   }
907   size = size /4;
908   UInt_t nrow=0;
909   UInt_t index=0;
910   UInt_t subindex=0;
911   while(index<size-1){ //don't start with last word
912     nrow++;
913     UInt_t ndigit=0;
914     Read(comp,index,subindex);
915     UShort_t npad = Read(comp,index,subindex);
916     for(UShort_t p=0;p<npad;p++){
917       Read(comp,index,subindex);
918       if(Test(comp,index,subindex)==0){
919         Read(comp,index,subindex);
920         if(Read(comp,index,subindex)==0)continue;
921       }
922       for(;;){
923         while(Read(comp,index,subindex)!=0) ndigit++;
924         if(Read(comp,index,subindex)==0) break;
925       }
926     }
927   }
928   if(index==size-1){  //last word
929     if(subindex<2){
930       if(Read(comp,index,subindex)!=0) nrow++;
931     }
932   }
933   return nrow;
934 }
935
936 Bool_t AliHLTTPCMemHandler::CompMemory2CompBinary(UInt_t nrow,UInt_t *comp,
937                                               UInt_t size)
938 {
939   //Write the RLE data in comp to the output file.
940   
941   if(!fOutBinary){
942     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::CompMemory2CompBinary","File")
943     <<"No Output File"<<ENDLOG;
944     return kFALSE;
945   }
946   if(!comp){
947     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::CompMemory2CompBinary","Memory")
948     <<"Pointer to compressed data = 0x0 "<<ENDLOG;
949     return kFALSE;
950   }
951   if(size==0)
952     size=GetMemorySize(nrow,comp);
953   if(!size){
954     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::CompMemory2CompBinary","Memory")
955     <<"Memory size = 0 "<<ENDLOG;
956     return kFALSE;
957   }
958   UInt_t length = size/sizeof(UInt_t);
959   fwrite(&length,sizeof(UInt_t),1,fOutBinary);  
960   fwrite(comp,size,1,fOutBinary);
961   return kTRUE;
962 }
963
964 Bool_t AliHLTTPCMemHandler::CompBinary2CompMemory(UInt_t & nrow,UInt_t *comp)
965 {
966   //Read the RLE data from file, and store it in comp. No unpacking yet.
967
968   if(!fInBinary){
969     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::CompBinary2CompMemory","File")
970       <<"No Output File"<<ENDLOG;
971     return kFALSE;
972   }
973   if(!comp){
974     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::CompBinary2CompMemory","Memory")
975       <<"Pointer to compressed data = 0x0 "<<ENDLOG;
976     return kFALSE;
977   }
978   rewind(fInBinary);
979   UInt_t length;
980   if(fread(&length,sizeof(UInt_t),1,fInBinary)!=1) return kFALSE;
981   UInt_t size = length*sizeof(UInt_t);
982   if(fread(comp,size,1,fInBinary)!=1) return kFALSE;
983   // now find the number of dig
984   nrow =  GetNRow(comp,size);
985   return kTRUE;
986 }
987
988 AliHLTTPCDigitRowData *AliHLTTPCMemHandler::CompBinary2Memory(UInt_t & nrow, UInt_t& sz )
989 {
990   // Read the RLE inputfile, unpack it and return the pointer to it.
991   AliHLTTPCMemHandler * handler = new AliHLTTPCMemHandler();
992   handler->SetBinaryInput(fInBinary);
993   UInt_t *comp =(UInt_t *)handler->Allocate();
994   handler->CompBinary2CompMemory(nrow,comp);
995   UInt_t size = GetMemorySize(nrow,comp);
996   sz = size;
997   AliHLTTPCDigitRowData *data = (AliHLTTPCDigitRowData *)Allocate(size);
998   CompMemory2Memory(nrow,data,comp);
999   handler->Free();
1000   delete handler;
1001   return data;  
1002 }
1003
1004 Bool_t AliHLTTPCMemHandler::Memory2CompBinary(UInt_t nrow,AliHLTTPCDigitRowData *data)
1005 {
1006   //Perform RLE on the data, and write it to the output file.
1007   Bool_t out = kTRUE;
1008   AliHLTTPCMemHandler * handler = new AliHLTTPCMemHandler();
1009   UInt_t size = GetCompMemorySize(nrow,data);
1010   UInt_t *comp =(UInt_t *)handler->Allocate(size);
1011   Memory2CompMemory(nrow,data,comp);
1012   CompMemory2CompBinary(nrow,comp,size);
1013   handler->Free();
1014   delete handler;
1015   return out;
1016 }
1017
1018
1019 ///////////////////////////////////////// Point IO  
1020 Bool_t AliHLTTPCMemHandler::Memory2Binary(UInt_t npoint,AliHLTTPCSpacePointData *data)
1021 {
1022   //Writing spacepoints stored in data to the outputfile.
1023   if(!fOutBinary){
1024     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Memory2Binary","File")
1025       <<"No Output File"<<ENDLOG;
1026     return kFALSE;
1027   }
1028   if(!data){
1029     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Memory2Binary","Memory")
1030       <<"Pointer to AliHLTTPCSpacePointData = 0x0 "<<ENDLOG;
1031     return kFALSE;
1032   }
1033   UInt_t size = npoint*sizeof(AliHLTTPCSpacePointData);
1034   fwrite(data,size,1,fOutBinary);
1035   
1036   return kTRUE;
1037 }
1038
1039 Bool_t AliHLTTPCMemHandler::Transform(UInt_t npoint,AliHLTTPCSpacePointData *data,Int_t slice)
1040 {
1041   //Transform the space points in data, to global coordinates in slice.
1042   if(!data){
1043     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Transform","Memory")
1044     <<"Pointer to AliHLTTPCSpacePointData = 0x0 "<<ENDLOG;
1045     return kFALSE;
1046   }
1047   
1048   for(UInt_t i=0;i<npoint;i++){
1049     Float_t xyz[3];
1050     xyz[0] = data[i].fX;
1051     xyz[1] = data[i].fY;
1052     xyz[2] = data[i].fZ;
1053     AliHLTTPCTransform::Local2Global(xyz,slice);
1054     data[i].fX = xyz[0];
1055     data[i].fY = xyz[1];
1056     data[i].fZ = xyz[2];
1057   }
1058   return kTRUE;
1059 }
1060
1061 Bool_t AliHLTTPCMemHandler::Binary2Memory(UInt_t & npoint,AliHLTTPCSpacePointData *data, UInt_t& sz)
1062 {
1063   //Read the space points in inputfile, and store it in data.
1064   if(!fInBinary){
1065     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Binary2Memory","File")
1066     <<"No Input File"<<ENDLOG;
1067     return kFALSE;
1068   }
1069   if(!data){
1070     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Binary2Memory","Memory")
1071     <<"Pointer to AliHLTTPCSpacePointData = 0x0 "<<ENDLOG;
1072     return kFALSE;
1073   }
1074
1075   Int_t size = GetFileSize(); 
1076   sz = size;
1077   npoint = size/sizeof(AliHLTTPCSpacePointData);
1078   if(size==0) {
1079     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Binary2Memory","File")
1080     <<"File Size == 0"<<ENDLOG;
1081     return kFALSE;
1082   }
1083
1084   if(fread(data,size,1,fInBinary)!=1){
1085     LOG(AliHLTTPCLog::kFatal,"AliHLTTPCMemHandler::Binary2Memory","File")
1086     <<"File Read Error "<<ENDLOG;
1087     return kFALSE;
1088   }
1089   if(size%sizeof(AliHLTTPCSpacePointData)){
1090     LOG(AliHLTTPCLog::kFatal,"AliHLTTPCMemHandler::Binary2Memory","File Size")
1091     <<"File Size wrong "<<ENDLOG;
1092     return kFALSE; 
1093   }
1094   LOG(AliHLTTPCLog::kDebug,"AliHLTTPCMemHandler::Binary2Memory","File")
1095   <<AliHLTTPCLog::kDec<<"Wrote  "<<size<<" Bytes to Memory"<<ENDLOG;
1096   return kTRUE;
1097 }
1098
1099 ///////////////////////////////////////// Track IO  
1100 Bool_t AliHLTTPCMemHandler::Memory2Binary(UInt_t ntrack,AliHLTTPCTrackSegmentData *data)
1101 {
1102   //Write the tracks stored in data, to outputfile.
1103   if(!fOutBinary){
1104     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Memory2Binary","File")
1105     <<"No Output File"<<ENDLOG;
1106     return kFALSE;
1107   }
1108   if(!data){
1109     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Memory2Binary","Memory")
1110     <<"Pointer to AliHLTTPCTrackSegmentData = 0x0 "<<ENDLOG;
1111     return kFALSE;
1112   }
1113   AliHLTTPCTrackSegmentData *trackPt = data;
1114   for(UInt_t i=0;i<ntrack;i++){
1115     Int_t size=sizeof(AliHLTTPCTrackSegmentData)+trackPt->fNPoints*sizeof(UInt_t); 
1116     fwrite(trackPt,size,1,fOutBinary);
1117     Byte_t *bytePt = (Byte_t*) trackPt;
1118     bytePt += size; 
1119     trackPt = (AliHLTTPCTrackSegmentData*) bytePt;
1120   }
1121   LOG(AliHLTTPCLog::kDebug,"AliHLTTPCMemHandler::Memory2Binary","File")
1122   <<AliHLTTPCLog::kDec<<"Wrote  "<<ntrack<<" Tracks to File"<<ENDLOG;
1123   
1124   return kTRUE;
1125 }
1126
1127 Bool_t AliHLTTPCMemHandler::Binary2Memory(UInt_t & ntrack,AliHLTTPCTrackSegmentData *data)
1128 {
1129   //Read the tracks in inputfile, and store it in data.
1130   if(!fInBinary){
1131     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Binary2Memory","File")
1132     <<"No Input File"<<ENDLOG;
1133     return kFALSE;
1134   }
1135   if(!data){
1136     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Binary2Memory","Memory")
1137     <<"Pointer to AliHLTTPCTrackSegmentData = 0x0 "<<ENDLOG;
1138     return kFALSE;
1139   }
1140
1141   ntrack=0;
1142   AliHLTTPCTrackSegmentData *trackPt = data;
1143   rewind(fInBinary);
1144
1145   while(!feof(fInBinary)){
1146     if(fread(trackPt,sizeof(AliHLTTPCTrackSegmentData),1,fInBinary)!=1) break;
1147     Int_t size=trackPt->fNPoints*sizeof(UInt_t);
1148     if(fread(trackPt->fPointIDs,size,1,fInBinary)!=1) break;
1149     Byte_t *bytePt = (Byte_t*) trackPt;
1150     bytePt += sizeof(AliHLTTPCTrackSegmentData)+size;
1151     trackPt = (AliHLTTPCTrackSegmentData*) bytePt;
1152     ntrack++; 
1153   }
1154   LOG(AliHLTTPCLog::kDebug,"AliHLTTPCMemHandler::Binary2Memory","File")
1155   <<AliHLTTPCLog::kDec<<"Wrote  "<<ntrack<<" Tracks to Memory"<<ENDLOG;
1156   return kTRUE;
1157 }
1158
1159 Bool_t AliHLTTPCMemHandler::TrackArray2Binary(AliHLTTPCTrackArray *array)
1160 {
1161   //Write the trackarray to the outputfile.
1162   if(!fOutBinary){
1163     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::TrackArray2Binary","File")
1164     <<"No Output File"<<ENDLOG;
1165     return kFALSE;
1166   }
1167   if(!array){
1168     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::TrackArray2Binary","Memory")
1169     <<"Pointer to AliHLTTPCTrackArray = 0x0 "<<ENDLOG;
1170     return kFALSE;
1171   }
1172   AliHLTTPCTrackSegmentData *data = (AliHLTTPCTrackSegmentData *)Allocate(array);
1173
1174   UInt_t ntrack;
1175   TrackArray2Memory(ntrack,data,array);
1176   Memory2Binary(ntrack,data);
1177   Free();
1178   return kTRUE;
1179 }
1180
1181 Bool_t AliHLTTPCMemHandler::Binary2TrackArray(AliHLTTPCTrackArray *array)
1182 {
1183   //Read the tracks in inputfile, and fill it in trackarray. 
1184   //array should already be constructed.
1185   if(!fInBinary){
1186     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Binary2TrackArray","File")
1187     <<"No Input File"<<ENDLOG;
1188     return kFALSE;
1189   }
1190   if(!array){
1191     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Binary2TrackArray","Memory")
1192     <<"Pointer to AliHLTTPCTrackArray = 0x0 "<<ENDLOG;
1193     return kFALSE;
1194   }
1195   AliHLTTPCTrackSegmentData *data = (AliHLTTPCTrackSegmentData *)Allocate();
1196   UInt_t ntrack;
1197   Binary2Memory(ntrack,data);
1198   Memory2TrackArray(ntrack,data,array);  
1199   Free();
1200   return kTRUE;
1201 }
1202
1203 Bool_t AliHLTTPCMemHandler::TrackArray2Memory(UInt_t & ntrack,AliHLTTPCTrackSegmentData *data,AliHLTTPCTrackArray *array) const
1204 {
1205   //Fill the trackarray into the AliTrackSegmentData structures before writing to outputfile.
1206   if(!data){
1207     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::TrackArray2Memory","Memory")
1208     <<"Pointer to AliHLTTPCTrackSegmentData = 0x0 "<<ENDLOG;
1209     return kFALSE;
1210   }
1211   if(!array){
1212     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::TrackArray2Memory","Memory")
1213     <<"Pointer to AliHLTTPCTrackArray = 0x0 "<<ENDLOG;
1214     return kFALSE;
1215   }
1216
1217   array->WriteTracks(ntrack,data);
1218   return kTRUE;
1219 }
1220
1221 Bool_t AliHLTTPCMemHandler::Memory2TrackArray(UInt_t ntrack,AliHLTTPCTrackSegmentData *data,AliHLTTPCTrackArray *array) const
1222 {
1223   //Fill the tracks in data into trackarray.
1224   
1225   if(!data){
1226     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Memory2TrackArray","Memory")
1227     <<"Pointer to AliHLTTPCTrackSegmentData = 0x0 "<<ENDLOG;
1228     return kFALSE;
1229   }
1230   if(!array){
1231     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Memory2TrackArray","Memory")
1232     <<"Pointer to AliHLTTPCTrackArray = 0x0 "<<ENDLOG;
1233     return kFALSE;
1234   }
1235   array->FillTracks(ntrack,data);
1236   return kTRUE;
1237 }
1238
1239 Bool_t AliHLTTPCMemHandler::Memory2TrackArray(UInt_t ntrack,AliHLTTPCTrackSegmentData *data,AliHLTTPCTrackArray *array,Int_t slice) const
1240 {
1241   //Fill the tracks in data into trackarray, and rotate the tracks to global coordinates.
1242     
1243   if(!data){
1244     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Memory2TrackArray","Memory")
1245     <<"Pointer to AliHLTTPCTrackSegmentData = 0x0 "<<ENDLOG;
1246     return kFALSE;
1247   }
1248   if(!array){
1249     LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Memory2TrackArray","Memory")
1250     <<"Pointer to AliHLTTPCTrackArray = 0x0 "<<ENDLOG;
1251     return kFALSE;
1252   }
1253   array->FillTracks(ntrack,data,slice);
1254   return kTRUE;
1255 }
1256
1257 void AliHLTTPCMemHandler::UpdateRowPointer(AliHLTTPCDigitRowData *&tempPt)
1258 {
1259   //Update the data pointer to the next padrow in memory.
1260   
1261   Byte_t *tmp = (Byte_t*)tempPt;
1262   Int_t size = sizeof(AliHLTTPCDigitRowData) + tempPt->fNDigit*sizeof(AliHLTTPCDigitData);
1263   tmp += size;
1264   tempPt = (AliHLTTPCDigitRowData*)tmp;
1265 }
1266
1267 Int_t  AliHLTTPCMemHandler::ComparePoints(UInt_t /*row*/,UShort_t pad,UShort_t time) const
1268 {
1269   //compare two points
1270   if(fNUsed>=fNDigits) return -2;
1271
1272   if(pad==fDPt[fNUsed]->fPad&&time==fDPt[fNUsed]->fTime) return 0;
1273
1274   if(pad<fDPt[fNUsed]->fPad) return -1;
1275   if(pad==fDPt[fNUsed]->fPad&&time<fDPt[fNUsed]->fTime)  return -1;
1276
1277   return 1;
1278 }
1279
1280 Int_t AliHLTTPCMemHandler::CompareDigits(AliHLTTPCRandomDigitData *a,AliHLTTPCRandomDigitData *b) const
1281 {
1282   //compare two digits
1283   if(a->fPad==b->fPad && a->fTime == b->fTime) return 0;
1284
1285   if(a->fPad<b->fPad) return -1;
1286   if(a->fPad==b->fPad && a->fTime<b->fTime) return -1;
1287   
1288   return 1;
1289 }