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