]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/comp/AliL3Compress.cxx
Merged HLT tag v1-2 with ALIROOT tag v3-09-Release.
[u/mrichter/AliRoot.git] / HLT / comp / AliL3Compress.cxx
1 // @(#) $Id$
2
3 // Author: Anders Vestbo <mailto:vestbo$fi.uib.no>
4 //*-- Copyright &copy ALICE HLT Group
5
6 #include "AliL3StandardIncludes.h"
7
8 #include "bitio.h"
9 #include "AliL3RootTypes.h"
10 #include "AliL3Models.h"
11 #include "AliL3DigitData.h"
12 #include "AliL3Logging.h"
13 #include "AliL3TrackArray.h"
14 #include "AliL3ModelTrack.h"
15 #include "AliL3Transform.h"
16 #include "AliL3MemHandler.h"
17 #include "AliL3DataCompressor.h"
18
19 #if 0
20 #ifdef use_root
21 #include <TH1.h>
22 #include <TH2.h>
23 #include <TRandom.h>
24 #endif
25 #ifdef use_aliroot
26 #include "AliL3FileHandler.h"
27 #endif
28 #endif
29
30 #include "AliL3Compress.h"
31
32 #if GCCVERSION == 3
33 using namespace std;
34 #endif
35
36 //_____________________________________________________________
37 //
38 //  AliL3Compress
39 //
40 // Class for compressing and uncompressing data.
41
42 ClassImp(AliL3Compress)
43
44 AliL3Compress::AliL3Compress()
45 {
46   fTracks=0;
47   fSlice =0;
48   fPatch=0;
49   fWriteShape=kFALSE;
50   fEvent=-1;
51 }
52
53 AliL3Compress::AliL3Compress(Int_t slice,Int_t patch,Char_t *path,Bool_t writeshape,Int_t event)
54 {
55   fEvent=event;
56   fSlice=slice;
57   fPatch=patch;
58   fTracks=0;
59   sprintf(fPath,"%s",path);
60   fWriteShape=writeshape;
61 }
62
63 AliL3Compress::~AliL3Compress()
64 {
65   if(fTracks)
66     delete fTracks;
67 }
68
69 Bool_t AliL3Compress::WriteFile(AliL3TrackArray *tracks,Char_t *filename)
70 {
71   Char_t fname[1024];
72   if(filename)
73     sprintf(fname,"%s/comp/%s",fPath,filename);
74   else if(fEvent<0)
75     sprintf(fname,"%s/comp/tracks_m_%d_%d.raw",fPath,fSlice,fPatch);
76   else
77     sprintf(fname,"%s/comp/tracks_m_%d_%d_%d.raw",fPath,fEvent,fSlice,fPatch);
78
79   FILE *file = fopen(fname,"w");
80   if(!file)
81     {
82       cerr<<"AliL3Compress::WriteFile : Error opening file "<<fname<<endl;
83       return kFALSE;
84     }
85   Short_t ntracks = tracks->GetNTracks();
86     
87   Int_t count=0;
88   AliL3ClusterModel *clusters=0;
89   AliL3TrackModel *model=0;
90   for(Int_t i=0; i<ntracks; i++)
91     {
92       AliL3ModelTrack *track = (AliL3ModelTrack*)tracks->GetCheckedTrack(i);
93       if(!track) continue;
94
95       //Do not save useless tracks or clusters:
96       //if(track->GetNPresentClusters() == 0)
97       //continue;
98       
99       track->FillModel();
100       model = track->GetModel();
101       if(model->fNClusters==0) continue;
102       clusters = track->GetClusters();
103       if(fwrite(model,sizeof(AliL3TrackModel),1,file)!=1) break;
104       if(fwrite(clusters,model->fNClusters*sizeof(AliL3ClusterModel),1,file)!=1) break;
105       count++;
106       
107     }
108   fclose(file);
109   return kTRUE;
110 }
111
112 Bool_t AliL3Compress::ReadFile(Char_t which,Char_t *filename)
113 {
114   //Read the trackfile.
115
116   Char_t fname[1024];
117   if(filename)
118     sprintf(fname,"%s/comp/%s",fPath,filename);
119   else
120     {
121       if(which == 'm')
122         {
123           if(fEvent<0)
124             sprintf(fname,"%s/comp/tracks_m_%d_%d.raw",fPath,fSlice,fPatch);
125           else
126             sprintf(fname,"%s/comp/tracks_m_%d_%d_%d.raw",fPath,fEvent,fSlice,fPatch);
127         }
128       else if(which == 'u')
129         {
130           if(fEvent<0)
131             sprintf(fname,"%s/comp/tracks_u_%d_%d.raw",fPath,fSlice,fPatch);
132           else
133             sprintf(fname,"%s/comp/tracks_u_%d_%d_%d.raw",fPath,fEvent,fSlice,fPatch);
134         }
135       else
136         {
137           cerr<<"AliL3Compress::ReadFile() : Wrong option"<<endl;
138           return kFALSE;
139         }
140     }
141
142   FILE *file = fopen(fname,"r");
143   if(!file)
144     {
145       cerr<<"AliL3Compress::ReadFile : Cannot open file "<<fname<<endl;
146       return kFALSE;
147     }
148
149   if(fTracks)
150     delete fTracks;
151   fTracks = new AliL3TrackArray("AliL3ModelTrack");
152   
153   while(!feof(file))
154     {
155       AliL3ModelTrack *track = (AliL3ModelTrack*)fTracks->NextTrack();
156       track->Init(fSlice,fPatch);
157       AliL3TrackModel *model = track->GetModel();
158       AliL3ClusterModel *clusters = track->GetClusters();
159       if(fread(model,sizeof(AliL3TrackModel),1,file)!=1) break;
160       if(fread(clusters,model->fNClusters*sizeof(AliL3ClusterModel),1,file)!=1) break;
161       track->FillTrack();
162     }
163
164   fTracks->RemoveLast();
165   fclose(file);
166   return kTRUE;
167 }
168
169 Bool_t AliL3Compress::CompressFile()
170 {
171   Char_t fname[100];
172   if(fEvent<0)
173     sprintf(fname,"%s/comp/tracks_c_%d_%d.raw",fPath,fSlice,fPatch);
174   else
175     sprintf(fname,"%s/comp/tracks_c_%d_%d_%d.raw",fPath,fEvent,fSlice,fPatch);
176   BIT_FILE *output = OpenOutputBitFile(fname);
177   
178   if(fEvent<0)
179     sprintf(fname,"%s/comp/tracks_m_%d_%d.raw",fPath,fSlice,fPatch);
180   else
181     sprintf(fname,"%s/comp/tracks_m_%d_%d_%d.raw",fPath,fEvent,fSlice,fPatch);
182   
183   FILE *input = fopen(fname,"r");
184   if(!input)
185     {
186       cerr<<"AliL3Compress::CompressFile() : Error opening file: "<<fname<<endl;
187       return kFALSE;
188     }
189
190   AliL3TrackModel track;
191   AliL3ClusterModel cluster;
192   Int_t temp;
193   Int_t power;
194   
195   Int_t timeo,pado,chargeo,shapeo;
196   timeo=pado=chargeo=shapeo=0;
197   while(!feof(input))
198     {
199       if(fread(&track,sizeof(AliL3TrackModel),1,input)!=1) break;
200       
201       if(output->mask != 0x80) //Write the current byte to file.
202         {
203           //cerr<<"\nAliL3Compress::CompressFile() : Writing overhead bits!!!"<<endl;
204           if(putc(output->rack,output->file )!=output->rack)
205             cerr<<"AliL3Compress::ComressFile : Error writing to bitfile"<<endl;
206           output->mask=0x80;
207           output->rack=0;
208         }
209       
210       //Write track parameters:
211       fwrite(&track,sizeof(AliL3TrackModel),1,output->file);
212       
213       Int_t origslice=-1,slice,clustercount=0;
214       for(Int_t i=0; i<track.fNClusters; i++)
215         {
216           if(fread(&cluster,sizeof(AliL3ClusterModel),1,input)!=1) break;
217           
218           //Write empty flag:
219           temp = (Int_t)cluster.fPresent;
220           OutputBit(output,temp);
221           if(!temp) continue;
222           
223           if(cluster.fSlice<0 || cluster.fSlice>35)
224             {
225               cerr<<"AliL3DataCompress::CompressFile : Fucked up slice number :"<<cluster.fSlice<<endl;
226               exit(5);
227             }
228           
229           //Write slice number of first point
230           if(clustercount==0)
231             {
232               origslice = cluster.fSlice;
233               OutputBits(output,origslice,6); //Need 6 bits to encode slice number
234             }
235           else
236             {
237               slice = cluster.fSlice;
238               if(slice == origslice)
239                 OutputBit(output,0);
240               else
241                 {
242                   OutputBit(output,1);
243                   OutputBits(output,slice,6);
244                   origslice=slice;
245                 }
246             }
247           
248           //Write time information:
249           temp = (Int_t)rint(cluster.fDTime);
250           if(temp<0)
251             OutputBit(output,0);
252           else
253             OutputBit(output,1);
254           power = 1<<(AliL3DataCompressor::GetNTimeBits()-1);
255           if(abs(temp)>=power)
256             {
257               timeo++;
258               temp=power - 1;
259             }
260           OutputBits(output,abs(temp),(AliL3DataCompressor::GetNTimeBits()-1));
261           
262           //Write pad information:
263           temp = (Int_t)rint(cluster.fDPad);
264           if(temp<0)
265             OutputBit(output,0);
266           else
267             OutputBit(output,1);
268           power = 1<<(AliL3DataCompressor::GetNPadBits()-1);
269           if(abs(temp)>=power)
270             {
271               pado++;
272               temp=power - 1;
273             }
274           OutputBits(output,abs(temp),(AliL3DataCompressor::GetNPadBits()-1));
275           
276           //Write charge information:
277           temp = (Int_t)cluster.fDCharge;
278           power = 1<<(AliL3DataCompressor::GetNChargeBits());
279           if(abs(temp)>=power)
280             {
281               chargeo++;
282               temp=power - 1;
283             }
284           OutputBits(output,abs(temp),(AliL3DataCompressor::GetNChargeBits()));
285           
286           if(fWriteShape)
287             {
288               //Write shape information:
289               temp = (Int_t)cluster.fDSigmaY2;
290               if(temp<0)
291                 OutputBit(output,0);
292               else
293                 OutputBit(output,1);
294               power = 1<<(AliL3DataCompressor::GetNShapeBits()-1);
295               if(abs(temp) >= power)
296                 {
297                   shapeo++;
298                   temp = power - 1;
299                 }
300               OutputBits(output,abs(temp),(AliL3DataCompressor::GetNShapeBits()-1));
301               
302               temp = (Int_t)cluster.fDSigmaZ2;
303               if(temp<0)
304                 OutputBit(output,0);
305               else
306                 OutputBit(output,1);
307               power = 1<<(AliL3DataCompressor::GetNShapeBits()-1);
308               if(abs(temp) >= power)
309                 {
310                   shapeo++;
311                   temp=power - 1;
312                 }
313               OutputBits(output,abs(temp),(AliL3DataCompressor::GetNShapeBits()-1));
314             }
315           
316           clustercount++;
317         }
318     }
319   
320   fclose(input);
321   CloseOutputBitFile(output);
322   if(pado || timeo || chargeo || shapeo)
323     {
324       cout<<endl<<"Saturations: "<<endl
325           <<"Pad "<<pado<<endl
326           <<"Time "<<timeo<<endl
327           <<"Charge "<<chargeo<<endl
328           <<"Shape "<<shapeo<<endl<<endl;
329     }
330   return kTRUE;
331 }
332
333 Bool_t AliL3Compress::ExpandFile()
334 {
335   Char_t fname[100];
336   if(fEvent<0)
337     sprintf(fname,"%s/comp/tracks_c_%d_%d.raw",fPath,fSlice,fPatch);
338   else
339     sprintf(fname,"%s/comp/tracks_c_%d_%d_%d.raw",fPath,fEvent,fSlice,fPatch);
340   BIT_FILE *input = OpenInputBitFile(fname);
341   
342   if(fEvent<0)
343     sprintf(fname,"%s/comp/tracks_u_%d_%d.raw",fPath,fSlice,fPatch);
344   else
345     sprintf(fname,"%s/comp/tracks_u_%d_%d_%d.raw",fPath,fEvent,fSlice,fPatch);
346   FILE *output = fopen(fname,"w");
347   if(!output)
348     {
349       cerr<<"AliL3Compress::ExpandFile() : Error opening file: "<<fname<<endl;
350       return kFALSE;
351     }
352
353   AliL3TrackModel trackmodel;
354   AliL3ClusterModel *clusters=0;
355   Int_t count=0;
356   
357   clusters = new AliL3ClusterModel[(AliL3Transform::GetNRows(fPatch))];
358   while(!feof(input->file))
359     {
360       input->mask=0x80;//make sure we read a new byte from file.
361       
362       //Read and write track:
363       if(fread(&trackmodel,sizeof(AliL3TrackModel),1,input->file)!=1) break;
364       fwrite(&trackmodel,sizeof(AliL3TrackModel),1,output);
365
366       memset(clusters,0,AliL3Transform::GetNRows(fPatch)*sizeof(AliL3ClusterModel));
367       Int_t origslice=-1,clustercount=0;
368       for(Int_t i=0; i<trackmodel.fNClusters; i++)
369         {
370           Int_t temp,sign;
371           
372           //Read empty flag:
373           temp = InputBit(input);
374           if(!temp) 
375             {
376               clusters[i].fPresent=kFALSE;
377               continue;
378             }
379           clusters[i].fPresent=kTRUE;
380           
381           //Read slice information
382           if(clustercount==0)
383             {
384               temp = InputBits(input,6);
385               clusters[i].fSlice = temp;
386               origslice = temp;
387             }
388           else
389             {
390               temp = InputBit(input);
391               if(!temp)//no change
392                 clusters[i].fSlice = origslice;
393               else
394                 {
395                   temp = InputBits(input,6);//read new slice
396                   clusters[i].fSlice = temp;
397                   origslice = temp;//store new slice
398                 }
399             }
400           
401           //Read time information:
402           sign=InputBit(input);
403           temp = InputBits(input,(AliL3DataCompressor::GetNTimeBits()-1));
404           if(!sign)
405             temp*=-1;
406           clusters[i].fDTime = temp;
407           
408           //Read pad information:
409           sign=InputBit(input);
410           temp = InputBits(input,(AliL3DataCompressor::GetNPadBits()-1));
411           if(!sign)
412             temp*=-1;
413           clusters[i].fDPad = temp;
414           
415           //Read charge information:
416           temp=InputBits(input,(AliL3DataCompressor::GetNChargeBits()));
417           clusters[i].fDCharge = temp;
418           
419           if(fWriteShape)
420             {
421               //Read shape information:
422               sign = InputBit(input);
423               temp = InputBits(input,(AliL3DataCompressor::GetNShapeBits()-1));
424               if(!sign)
425                 temp*=-1;
426               clusters[i].fDSigmaY2 = temp;
427               
428               sign = InputBit(input);
429               temp = InputBits(input,(AliL3DataCompressor::GetNShapeBits()-1));
430               if(!sign)
431                 temp*=-1;
432               clusters[i].fDSigmaZ2 = temp;
433             }
434           clustercount++;
435         }
436       count++;
437       fwrite(clusters,(trackmodel.fNClusters)*sizeof(AliL3ClusterModel),1,output);
438       
439     }
440   
441   delete [] clusters;
442   fclose(output);
443   CloseInputBitFile(input);
444   return kTRUE;
445 }
446
447 void AliL3Compress::PrintCompRatio(ofstream *outfile)
448 {
449   AliL3MemHandler *mem = new AliL3MemHandler();
450   Char_t fname[1024];
451   UInt_t remain_size=0,digit_size=0;
452   for(Int_t i=0; i<36; i++)
453     {
454       if(fEvent<0)
455         sprintf(fname,"%s/comp/remains_%d_%d.raw",fPath,i,-1);
456       else
457         sprintf(fname,"%s/comp/remains_%d_%d_%d.raw",fPath,fEvent,i,-1);
458       mem->SetBinaryInput(fname);
459       remain_size += mem->GetFileSize();
460       mem->CloseBinaryInput();
461
462       sprintf(fname,"%s/binaries/digits_c8_%d_%d_%d.raw",fPath,fEvent,i,-1);
463       mem->SetBinaryInput(fname);
464       digit_size += mem->GetFileSize();
465       mem->CloseBinaryInput();
466     }
467   
468   
469   if(fEvent<0)
470     sprintf(fname,"%s/comp/tracks_c_%d_%d.raw",fPath,fSlice,fPatch);
471   else
472     sprintf(fname,"%s/comp/tracks_c_%d_%d_%d.raw",fPath,fEvent,fSlice,fPatch);
473
474   mem->SetBinaryInput(fname);
475   UInt_t compress_size = mem->GetFileSize();
476   mem->CloseBinaryInput();
477   
478   if(digit_size==0)
479     {
480       cerr<<"AliL3Compress::PrintCompRatio : Zero digit size "<<endl;
481       exit(5);
482     }
483   
484   Float_t compratio = (Float_t)(compress_size + remain_size)/(Float_t)digit_size;
485   if(outfile)
486     {
487       ofstream &out = *outfile;
488       out<<compress_size<<' '<<remain_size<<' '<<digit_size<<endl;
489     }
490
491   cout<<"=========================================="<<endl;
492   cout<<"Original digits size : "<<digit_size/1000<<" kByte ( 100 % )"<<endl;
493   cout<<"Compressed file size : "<<compress_size/1000<<" kByte ( "<<(Float_t)compress_size*100/(Float_t)digit_size<<" % )"<<endl;
494   cout<<"Remainig file size   : "<<remain_size/1000<<" kByte ( "<<(Float_t)remain_size*100/(Float_t)digit_size<<" % )"<<endl;
495   cout<<"---------------------- "<<endl;
496   cout<<"Compression ratio    : "<<compratio*100<<" %"<<endl;
497   cout<<"=========================================="<<endl;
498 }