]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/comp/AliL3Compress.cxx
Some changes resulting from last week work. The mc stuff has been removed.
[u/mrichter/AliRoot.git] / HLT / comp / AliL3Compress.cxx
1 //$Id$
2
3 // Author: Anders Vestbo <mailto:vestbo$fi.uib.no>
4 //*-- Copyright &copy ASV
5
6 #include "AliL3StandardIncludes.h"
7 #include <TH1.h>
8 #include <TH2.h>
9 #include <TRandom.h>
10
11 #include "AliL3Logging.h"
12 #include "AliL3Compress.h"
13 #include "AliL3TrackArray.h"
14 #include "AliL3ModelTrack.h"
15 #include "AliL3Transform.h"
16 #include "AliL3MemHandler.h"
17 #ifdef use_aliroot
18 #include "AliL3FileHandler.h"
19 #endif
20 #include "bitio.h"
21
22 //_____________________________________________________________
23 //
24 //  AliL3Compress
25 //
26 // Class for compressing and uncompressing data.
27
28 ClassImp(AliL3Compress)
29
30 AliL3Compress::AliL3Compress()
31 {
32   fTracks=0;
33   SetBitNumbers(0,0,0,0);
34   fSlice =0;
35   fPatch=0;
36   fDigits=0;
37   fDPt=0;
38   fWriteShape=kFALSE;
39 }
40
41 AliL3Compress::AliL3Compress(Int_t slice,Int_t patch,Char_t *path,Bool_t writeshape)
42 {
43   fSlice=slice;
44   fPatch=patch;
45   SetBitNumbers(0,0,0,0);
46   fTracks=0;
47   fDigits=0;
48   fDPt=0;
49   sprintf(fPath,"%s",path);
50   fWriteShape=writeshape;
51 }
52
53 AliL3Compress::~AliL3Compress()
54 {
55   if(fTracks)
56     delete fTracks;
57   if(fDigits)
58     delete [] fDigits;
59   if(fDPt)
60     delete [] fDPt;
61 }
62
63 void AliL3Compress::SetBitNumbers(Int_t pad,Int_t time,Int_t charge,Int_t shape)
64 {
65   fNumPadBits=pad;
66   fNumTimeBits=time;
67   fNumChargeBits=charge;
68   fNumShapeBits=shape;
69 }
70
71 void AliL3Compress::WriteFile(AliL3TrackArray *tracks)
72 {
73   Char_t fname[100];
74   sprintf(fname,"%s/comp/tracks_m_%d_%d.raw",fPath,fSlice,fPatch);
75   FILE *file = fopen(fname,"w");
76   if(!file)
77     {
78       cerr<<"AliL3Compress::WriteFile : Error opening file "<<fname<<endl;
79       return;
80     }
81   Short_t ntracks = tracks->GetNTracks();
82   //cout<<"Writing "<<ntracks<<" tracks to file"<<endl;
83     
84   Int_t count=0;
85   AliL3ClusterModel *clusters=0;
86   AliL3TrackModel *model=0;
87   for(Int_t i=0; i<ntracks; i++)
88     {
89       AliL3ModelTrack *track = (AliL3ModelTrack*)tracks->GetCheckedTrack(i);
90       if(!track) continue;
91       
92       //Do not save useless tracks or clusters:
93       if(track->GetNPresentClusters() == 0)
94         continue;
95       
96       track->FillModel();
97       model = track->GetModel();
98       if(model->fNClusters==0) continue;
99       clusters = track->GetClusters();
100       //cout<<"Writing "<<(int)model->fNClusters<<" clusters"<<endl;
101       if(fwrite(model,sizeof(AliL3TrackModel),1,file)!=1) break;
102       //cout<<"Writing "<<(int)model->fNClusters<<" clusters to file"<<endl;
103       if(fwrite(clusters,model->fNClusters*sizeof(AliL3ClusterModel),1,file)!=1) break;
104       //track->Print();
105       count++;
106       
107     }
108   cout<<"Wrote "<<count<<" tracks "<<endl;
109   fclose(file);
110 }
111
112 void AliL3Compress::ReadFile(Char_t which)
113 {
114   //Read the trackfile.
115
116   Char_t fname[100];
117   if(which == 'm')
118     sprintf(fname,"%s/comp/tracks_m_%d_%d.raw",fPath,fSlice,fPatch);
119   else if(which == 'u')
120     sprintf(fname,"%s/comp/tracks_u_%d_%d.raw",fPath,fSlice,fPatch);
121   else
122     {
123       cerr<<"AliL3Compress::ReadFile() : Wrong option"<<endl;
124       return;
125     }
126
127   FILE *file = fopen(fname,"r");
128   if(!file)
129     {
130       cerr<<"AliL3Compress::ReadFile : Cannot open file "<<fname<<endl;
131       return;
132     }
133
134   if(fTracks)
135     delete fTracks;
136   fTracks = new AliL3TrackArray("AliL3ModelTrack");
137   
138   //cout<<"Reading file "<<fname<<endl;
139   while(!feof(file))
140     {
141       AliL3ModelTrack *track = (AliL3ModelTrack*)fTracks->NextTrack();
142       track->Init(fSlice,fPatch);
143       AliL3TrackModel *model = track->GetModel();
144       AliL3ClusterModel *clusters = track->GetClusters();
145       if(fread(model,sizeof(AliL3TrackModel),1,file)!=1) break;
146       if(fread(clusters,(model->fNClusters)*sizeof(AliL3ClusterModel),1,file)!=1) break;
147       track->FillTrack();
148     }
149
150   fTracks->RemoveLast();
151   //cout<<"Read "<<fTracks->GetNTracks()<<" tracks from file"<<endl;
152   fclose(file);
153 }
154
155 void AliL3Compress::CompressFile()
156 {
157   if(fNumTimeBits==0)
158     {
159       cerr<<"AliL3Compress::CompressFile() : Bitnumbers not set"<<endl;
160       return;
161     }
162   
163   Char_t fname[100];
164   sprintf(fname,"%s/comp/tracks_c_%d_%d.raw",fPath,fSlice,fPatch);
165   BIT_FILE *output = OpenOutputBitFile(fname);
166   
167   sprintf(fname,"%s/comp/tracks_m_%d_%d.raw",fPath,fSlice,fPatch);
168   FILE *input = fopen(fname,"r");
169   if(!input)
170     {
171       cerr<<"AliL3Compress::CompressFile() : Error opening file: "<<fname<<endl;
172       return;
173     }
174
175   AliL3TrackModel track;
176   AliL3ClusterModel cluster;
177   Int_t temp;
178   Int_t power;
179   
180   Int_t timeo,pado,chargeo,shapeo;
181   timeo=pado=chargeo=shapeo=0;
182   while(!feof(input))
183     {
184       if(fread(&track,sizeof(AliL3TrackModel),1,input)!=1) break;
185       
186       if(output->mask != 0x80) //Write the current byte to file.
187         {
188           //cerr<<"\nAliL3Compress::CompressFile() : Writing overhead bits!!!"<<endl;
189           if(putc(output->rack,output->file )!=output->rack)
190             cerr<<"AliL3Compress::ComressFile : Error writing to bitfile"<<endl;
191           output->mask=0x80;
192           output->rack=0;
193         }
194       
195       //Write track parameters:
196       fwrite(&track,sizeof(AliL3TrackModel),1,output->file);
197       for(Int_t i=0; i<track.fNClusters; i++)
198         {
199           if(fread(&cluster,sizeof(AliL3ClusterModel),1,input)!=1) break;
200           
201           //Write empty flag:
202           temp = (Int_t)cluster.fPresent;
203           OutputBit(output,temp);
204           if(!temp) continue;
205           
206           //Write time information:
207           temp = (Int_t)cluster.fDTime;
208           if(temp<0)
209             OutputBit(output,0);
210           else
211             OutputBit(output,1);
212           power = 1<<(fNumTimeBits-1);
213           if(abs(temp)>=power)
214             {
215               timeo++;
216               temp=power - 1;
217             }
218           OutputBits(output,abs(temp),(fNumTimeBits-1));
219           
220           //Write pad information:
221           temp = (Int_t)cluster.fDPad;
222           if(temp<0)
223             OutputBit(output,0);
224           else
225             OutputBit(output,1);
226           power = 1<<(fNumPadBits-1);
227           if(abs(temp)>=power)
228             {
229               pado++;
230               temp=power - 1;
231             }
232           OutputBits(output,abs(temp),(fNumPadBits-1));
233           
234           //Write charge information:
235           temp = (Int_t)cluster.fDCharge;
236           power = 1<<(fNumChargeBits);
237           if(abs(temp)>=power)
238             {
239               chargeo++;
240               temp=power - 1;
241             }
242           OutputBits(output,abs(temp),(fNumChargeBits));
243           
244           if(fWriteShape)
245             {
246               //Write shape information:
247               temp = (Int_t)cluster.fDSigmaY2;
248               if(temp<0)
249                 OutputBit(output,0);
250               else
251                 OutputBit(output,1);
252               power = 1<<(fNumShapeBits-1);
253               if(abs(temp) >= power)
254                 {
255                   shapeo++;
256                   temp = power - 1;
257                 }
258               OutputBits(output,abs(temp),(fNumShapeBits-1));
259               
260               temp = (Int_t)cluster.fDSigmaZ2;
261               if(temp<0)
262                 OutputBit(output,0);
263               else
264                 OutputBit(output,1);
265               power = 1<<(fNumShapeBits-1);
266               if(abs(temp) >= power)
267                 {
268                   shapeo++;
269                   temp=power - 1;
270                 }
271               OutputBits(output,abs(temp),(fNumShapeBits-1));
272             }
273         }
274     }
275   
276   fclose(input);
277   CloseOutputBitFile(output);
278
279   cout<<endl<<"Saturations: "<<endl
280       <<"Pad "<<pado<<endl
281       <<"Time "<<timeo<<endl
282       <<"Charge "<<chargeo<<endl
283       <<"Shape "<<shapeo<<endl;
284 }
285
286 void AliL3Compress::ExpandFile()
287 {
288   if(fNumTimeBits==0)
289     {
290       cerr<<"AliL3Compress::ExpandFile() : Bitnumbers not set"<<endl;
291       return;
292     }
293   
294   Char_t fname[100];
295   sprintf(fname,"%s/comp/tracks_c_%d_%d.raw",fPath,fSlice,fPatch);
296   BIT_FILE *input = OpenInputBitFile(fname);
297   
298   sprintf(fname,"%s/comp/tracks_u_%d_%d.raw",fPath,fSlice,fPatch);
299   FILE *output = fopen(fname,"w");
300   if(!output)
301     {
302       cerr<<"AliL3Compress::ExpandFile() : Error opening file: "<<fname<<endl;
303       return;
304     }
305
306   AliL3TrackModel trackmodel;
307   AliL3ClusterModel *clusters=0;
308   Int_t count=0;
309   
310   clusters = new AliL3ClusterModel[(AliL3Transform::GetNRows(fPatch))];
311   while(!feof(input->file))
312     {
313       input->mask=0x80;//make sure we read a new byte from file.
314       
315       //Read and write track:
316       if(fread(&trackmodel,sizeof(AliL3TrackModel),1,input->file)!=1) break;
317       fwrite(&trackmodel,sizeof(AliL3TrackModel),1,output);
318
319       memset(clusters,0,AliL3Transform::GetNRows(fPatch)*sizeof(AliL3ClusterModel));
320       for(Int_t i=0; i<AliL3Transform::GetNRows(fPatch); i++)
321         {
322           Int_t temp,sign;
323           
324           //Read empty flag:
325           temp = InputBit(input);
326           if(!temp) 
327             {
328               clusters[i].fPresent=kFALSE;
329               continue;
330             }
331           clusters[i].fPresent=kTRUE;
332           
333           //Read time information:
334           sign=InputBit(input);
335           temp = InputBits(input,(fNumTimeBits-1));
336           if(!sign)
337             temp*=-1;
338           clusters[i].fDTime = temp;
339           
340           //Read pad information:
341           sign=InputBit(input);
342           temp = InputBits(input,(fNumPadBits-1));
343           if(!sign)
344             temp*=-1;
345           clusters[i].fDPad = temp;
346           
347           //Read charge information:
348           temp=InputBits(input,(fNumChargeBits));
349           clusters[i].fDCharge = temp;
350           
351           if(fWriteShape)
352             {
353               //Read shape information:
354               sign = InputBit(input);
355               temp = InputBits(input,(fNumShapeBits-1));
356               if(!sign)
357                 temp*=-1;
358               clusters[i].fDSigmaY2 = temp;
359               
360               sign = InputBit(input);
361               temp = InputBits(input,(fNumShapeBits-1));
362               if(!sign)
363                 temp*=-1;
364               clusters[i].fDSigmaZ2 = temp;
365             }
366         }
367       count++;
368       fwrite(clusters,(trackmodel.fNClusters)*sizeof(AliL3ClusterModel),1,output);
369       
370     }
371   
372   delete [] clusters;
373   fclose(output);
374   CloseInputBitFile(input);
375 }
376
377 void AliL3Compress::CreateDigitArray(Int_t maxnumber)
378 {
379   fNUsed=0;
380   fNDigits = 0;
381   fMaxDigits=maxnumber;
382   if(fDigits) delete [] fDigits;
383   fDigits = new AliL3RandomDigitData[maxnumber];
384   if(fDPt) delete [] fDPt;
385   fDPt = new AliL3RandomDigitData*[maxnumber];
386 }
387
388 void AliL3Compress::RestoreData(Char_t which)
389 {
390   //Restore the data.
391   //which == u : restore compressed data
392   //which == m : restore uncompressed data
393   
394   if(!fWriteShape)
395     {
396       cerr<<"AliL3Compress::RestoreData : Not implemented without shape info "<<endl;
397       return;
398     }
399
400   ReadFile(which);
401   
402   CreateDigitArray(10000000);
403   
404   Float_t pad,time,sigmaY2,sigmaZ2;
405   Int_t charge,npads;
406   for(Int_t j=AliL3Transform::GetFirstRow(fPatch); j<=AliL3Transform::GetLastRow(fPatch); j++)
407     {
408       cout<<"Building clusters on row "<<j<<endl;
409       for(Int_t i=0; i<fTracks->GetNTracks(); i++)
410         {
411           AliL3ModelTrack *track = (AliL3ModelTrack*)fTracks->GetCheckedTrack(i);
412           if(!track) continue;
413           if(!track->GetPad(j,pad) || 
414              !track->GetTime(j,time) || 
415              !track->GetClusterCharge(j,charge) ||
416              !track->GetXYWidth(j,sigmaY2) || 
417              !track->GetZWidth(j,sigmaZ2))
418             continue;
419           npads = track->GetNPads(j);
420           CreateDigits(j,npads,pad,time,charge,sigmaY2,sigmaZ2);
421         }
422     }
423   
424   QSort(fDPt,0,fNDigits);
425 }
426
427 void AliL3Compress::PrintDigits(Int_t padrow)
428 {
429   Int_t pad,time,charge,row;
430   for(Int_t i=0; i<fNDigits; i++)
431     {
432       row = fDPt[i]->fRow;
433       if(padrow > 0)
434         if(row != padrow) continue;
435       pad = fDPt[i]->fPad;
436       time = fDPt[i]->fTime;
437       charge = fDPt[i]->fCharge;
438       if(i>0 && row != fDPt[i-1]->fRow)
439         cout<<"---Padrow "<<row<<"---"<<endl;
440       cout<<"Padrow "<<row<<" Pad "<<pad<<" time "<<time<<" charge "<<charge<<endl;
441     }
442 }
443
444 void AliL3Compress::WriteRestoredData()
445 {
446     Char_t fname[100];
447   
448   //Get the remaining raw data array:
449   AliL3MemHandler *mem = new AliL3MemHandler();
450   sprintf(fname,"%s/comp/remains_%d_%d.raw",fPath,fSlice,fPatch);
451   mem->SetBinaryInput(fname);
452   UInt_t numdigits;
453   AliL3DigitRowData *origRow = mem->CompBinary2Memory(numdigits);
454   mem->CloseBinaryInput();
455   
456   //Allocate memory for the merged data:
457   UInt_t size = mem->GetAllocatedSize() + fNDigits*sizeof(AliL3DigitData);
458   cout<<"Allocating "<<size<<" bytes for merged data array "<<endl;
459   Byte_t *data = new Byte_t[size];
460   memset(data,0,size);
461   AliL3DigitRowData *tempRow = (AliL3DigitRowData*)data;
462
463   Int_t ndigits,action,charge;
464   UShort_t pad,time;
465       
466   UInt_t digit_counter;
467   Int_t row_counter=0;
468   for(Int_t i=AliL3Transform::GetFirstRow(fPatch); i<=AliL3Transform::GetLastRow(fPatch); i++)
469     {
470       tempRow->fRow = i;
471       ndigits=0;
472       AliL3DigitData *origDig = origRow->fDigitData;
473       AliL3DigitData *tempDig = tempRow->fDigitData;
474       if((Int_t)origRow->fRow != i)
475         cerr<<"AliL3Compress::WriteRestoredData() : Mismatching row numbering "<<(Int_t)origRow->fRow<<" "<<i<<endl;
476
477       //cout<<"Writing row "<<i<<" with "<<(Int_t)origRow->fNDigit<<" old digits"<<endl;
478       digit_counter=0;
479       
480       while(1)
481         {
482           while(digit_counter < origRow->fNDigit)
483             {
484               pad = origDig[digit_counter].fPad;
485               time = origDig[digit_counter].fTime;
486               charge = origDig[digit_counter].fCharge;
487               digit_counter++;
488               while( (action=ComparePoints(i,pad,time)) == 1)
489                 {
490                   tempDig[ndigits].fPad = fDPt[fNUsed]->fPad;
491                   tempDig[ndigits].fTime = fDPt[fNUsed]->fTime;
492                   tempDig[ndigits].fCharge = fDPt[fNUsed]->fCharge;
493                   ndigits++;
494                   fNUsed++;
495
496                 }
497               if(action == 0)
498                 {
499                   tempDig[ndigits].fPad = pad;
500                   tempDig[ndigits].fTime = time;
501                   tempDig[ndigits].fCharge = charge;
502                   ndigits++;
503                 }
504             }
505           
506           if(fNUsed >= fNDigits) 
507             {
508               //cerr<<"AliL3Compress::WriteRestoredData() : Array out of range : "<<fNUsed<<" "<<fNDigits<<endl;
509               break;
510             }
511           if(fDPt[fNUsed]->fRow != i) //we are on a new row
512             break;
513           tempDig[ndigits].fPad = fDPt[fNUsed]->fPad;
514           tempDig[ndigits].fTime = fDPt[fNUsed]->fTime;
515           tempDig[ndigits].fCharge = fDPt[fNUsed]->fCharge;
516           ndigits++;
517           fNUsed++;
518         }
519       //cout<<"Writing "<<ndigits<<" digits on row "<<i<<endl;
520       if(ndigits > 4)
521         {
522           row_counter++;
523         }
524       tempRow->fNDigit = ndigits;
525       Int_t size = sizeof(AliL3DigitData)*tempRow->fNDigit + sizeof(AliL3DigitRowData);
526       Byte_t *byte_pt = (Byte_t*)tempRow;
527       byte_pt += size;
528       tempRow = (AliL3DigitRowData*)byte_pt;
529       mem->UpdateRowPointer(origRow);
530     }
531   
532   if(row_counter != AliL3Transform::GetNRows(fPatch))
533     cerr<<"AliL3Compress::WriteRestoredData() : Written rows: "<<row_counter<<" total rows "<<AliL3Transform::GetNRows(fPatch)<<endl;
534   
535   mem->Free();  
536   sprintf(fname,"%s/comp/restored_%d_%d.raw",fPath,fSlice,fPatch);
537   mem->SetBinaryOutput(fname);
538   mem->Memory2CompBinary((UInt_t)AliL3Transform::GetNRows(fPatch),(AliL3DigitRowData*)data);
539   mem->CloseBinaryOutput();
540   
541   delete [] data;
542   delete mem;
543   
544 }
545
546 void AliL3Compress::CreateDigits(Int_t row,Int_t npads,Float_t pad,Float_t time,Int_t charge,Float_t sigmaY2,Float_t sigmaZ2)
547 {
548   //Create raw data out of the cluster.
549     
550   if(npads == 1)//If there was only 1 pad, the xywidth is set to zero.
551     sigmaY2 = 0;
552   
553   if(sigmaY2 < 0 || sigmaZ2 <= 0)
554     {
555       cerr<<"AliL3Compress::CreateDigits() : Wrong sigmas : "<<sigmaY2<<" "<<sigmaZ2;
556       cerr<<" on row "<<row<<" pad "<<pad<<" time "<<time<<endl;
557       return;
558     }
559   
560   TRandom *random = new TRandom();
561   
562   Int_t entries=2000;
563   TH1F *hist1 = new TH1F("hist1","",AliL3Transform::GetNPads(row),0,AliL3Transform::GetNPads(row)-1);
564   TH1F *hist2 = new TH1F("hist2","",AliL3Transform::GetNTimeBins(),0,AliL3Transform::GetNTimeBins()-1);
565   TH2F *hist3 = new TH2F("hist3","",AliL3Transform::GetNPads(row),0,AliL3Transform::GetNPads(row)-1,AliL3Transform::GetNTimeBins(),0,AliL3Transform::GetNTimeBins()-1);
566
567
568   
569   //Create the distributions in pad and time:
570   for(Int_t i=0; i<entries; i++)
571     {
572       hist1->Fill(random->Gaus(pad,sqrt(sigmaY2)));
573       hist2->Fill(random->Gaus(time,sqrt(sigmaZ2)));
574     }
575     
576   //Create the cluster:
577   Int_t bin1,bin2;
578   Double_t content1,content2,dpad,dtime;
579   for(Int_t i=0; i<hist1->GetEntries(); i++)
580     {
581       bin1 = hist1->GetBin(i);
582       content1 = hist1->GetBinContent(bin1);
583       if((Int_t)content1==0) continue;
584       content1 = charge*content1/entries;
585       dpad = hist1->GetBinCenter(bin1);
586       for(Int_t j=0; j<hist2->GetEntries(); j++)
587         {
588           bin2 = hist2->GetBin(j);
589           content2 = hist2->GetBinContent(bin2);
590           if((Int_t)content2==0) continue;
591           content2 = content1*content2/entries;
592           dtime = hist2->GetBinCenter(bin2);
593           hist3->Fill(dpad,dtime,content2);
594         }
595     }
596   
597   Int_t local_dig=0;
598   //Fill it into the digit array:
599   for(Int_t i=0; i<hist3->GetNbinsX(); i++)
600     {
601       for(Int_t j=0; j<hist3->GetNbinsY(); j++)
602         {
603           bin1 = hist3->GetBin(i,j);
604           content1 = hist3->GetBinContent(bin1);
605           if((Int_t)content1 < 3) continue;
606           if(content1 >= 1024)
607             content1 = 1023;
608           if(fNDigits >= fMaxDigits)
609             {
610               cerr<<"AliL3Compress::CreateDigits() : Array index out of range : "<<fNDigits<<endl;
611               return;
612             }
613           fDigits[fNDigits].fCharge=(Int_t)content1;
614           fDigits[fNDigits].fRow = row;
615           fDigits[fNDigits].fPad = (Int_t)hist3->GetXaxis()->GetBinCenter(i);
616           fDigits[fNDigits].fTime = (Int_t)hist3->GetYaxis()->GetBinCenter(j);
617           fDPt[fNDigits] = &fDigits[fNDigits];
618           fNDigits++;
619           local_dig++;
620         }
621     }
622   //if(local_dig < 5)
623   //  cout<<"Small cluster "<<local_dig<<" pad "<<(Int_t)fDigits[fNDigits-1].fPad<<" time "<<(Int_t)fDigits[fNDigits-1].fTime<<endl;
624   
625   delete random;
626   delete hist1;
627   delete hist2;
628   delete hist3;
629 }
630
631 void AliL3Compress::PrintCompRatio()
632 {
633   Char_t fname[100];
634   sprintf(fname,"%s/comp/remains_%d_%d.raw",fPath,fSlice,fPatch);
635   AliL3MemHandler *mem = new AliL3MemHandler();
636   if(!mem->SetBinaryInput(fname))
637     {
638       cerr<<"AliL3Compress::PrintCompRatio(): Error opening file: "<<fname<<endl;
639       return;
640     } 
641   UInt_t ndigits;
642   AliL3DigitRowData *rowPt = (AliL3DigitRowData*)mem->CompBinary2Memory(ndigits);
643   mem->CloseBinaryInput();
644
645   Int_t digit_counter=0;
646   for(Int_t i=AliL3Transform::GetFirstRow(fPatch); i<=AliL3Transform::GetLastRow(fPatch); i++)
647     {
648       digit_counter += rowPt->fNDigit;
649       mem->UpdateRowPointer(rowPt);
650     }
651   delete mem;
652   
653   sprintf(fname,"%s/comp/tracks_c_%d_%d.raw",fPath,fSlice,fPatch);
654   FILE *file1 = fopen(fname,"r");
655   if(!file1)
656     {
657       cerr<<"AliL3Compress::PrintCompRatio(): Error opening file: "<<fname<<endl;
658       return;
659     }
660   fseek(file1,0,SEEK_END);
661   UInt_t filesize1 = (UInt_t)ftell(file1);
662   fclose(file1);
663   
664   sprintf(fname,"%s/digits_%d_%d.raw",fPath,fSlice,fPatch);
665   FILE *file2 = fopen(fname,"r");
666   if(!file2)
667     {
668       cerr<<"AliL3Compress::PrintCompRatio(): Error opening file: "<<fname<<endl;
669       return;
670     }
671   fseek(file2,0,SEEK_END);
672   UInt_t filesize2 = (UInt_t)ftell(file2);
673   fclose(file2);
674   
675   cout<<"----------------------"<<endl;
676   cout<<"Original file size   : "<<filesize2<<endl;
677   cout<<"Compressed file size : "<<filesize1<<endl;
678   cout<<"Remaining digits     : "<<digit_counter<<endl;
679   cout<<"Compression ratio    : "<<(Float_t)(filesize1 + (10*digit_counter)/8)/(Float_t)(filesize2)<<endl;
680   
681 }
682
683 void AliL3Compress::QSort(AliL3RandomDigitData **a, Int_t first, Int_t last)
684 {
685   
686   // Sort array of AliL3RandomDigitData pointers using a quicksort algorithm.
687   // Uses CompareDigits() to compare objects.
688   // Thanks to Root!
689   
690   static AliL3RandomDigitData *tmp;
691   static int i;           // "static" to save stack space
692   int j;
693   
694   while (last - first > 1) {
695     i = first;
696     j = last;
697     for (;;) {
698       while (++i < last && CompareDigits(a[i], a[first]) < 0)
699         ;
700       while (--j > first && CompareDigits(a[j], a[first]) > 0)
701         ;
702       if (i >= j)
703         break;
704       
705       tmp  = a[i];
706       a[i] = a[j];
707       a[j] = tmp;
708     }
709     if (j == first) {
710       ++first;
711       continue;
712     }
713     tmp = a[first];
714     a[first] = a[j];
715     a[j] = tmp;
716     if (j - first < last - (j + 1)) {
717       QSort(a, first, j);
718       first = j + 1;   // QSort(j + 1, last);
719     } else {
720       QSort(a, j + 1, last);
721       last = j;        // QSort(first, j);
722     }
723   }
724 }
725
726 void AliL3Compress::WriteRootFile(Char_t *newrootfile)
727 {
728 #ifdef use_aliroot
729   Char_t fname[100];
730   AliL3MemHandler *mem = new AliL3MemHandler();
731   sprintf(fname,"%s/comp/restored_%d_%d.raw",fPath,fSlice,fPatch);
732   mem->SetBinaryInput(fname);
733   UInt_t ndigits;
734   AliL3DigitRowData *rowPt = (AliL3DigitRowData*)mem->CompBinary2Memory(ndigits);
735   mem->CloseBinaryInput();
736   
737   sprintf(fname,"%s/digitfile.root",fPath);
738   
739   AliL3FileHandler *file = new AliL3FileHandler();
740   if(!file->SetAliInput(fname))
741     {
742       cerr<<"AliL3Compress::WriteRootFile() : Error opening file: "<<fname<<endl;
743       return;
744     }
745
746   file->Init(fSlice,fPatch);
747   file->AliDigits2RootFile(rowPt,newrootfile);
748   file->CloseAliInput();
749
750   delete mem;
751   delete file;
752 #endif
753   return;
754 }