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