3 // Author: Anders Vestbo <mailto:vestbo$fi.uib.no>
4 //*-- Copyright © ALICE HLT Group
6 #include "AliL3StandardIncludes.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"
26 #include "AliL3FileHandler.h"
30 #include "AliL3Compress.h"
36 //_____________________________________________________________
40 // Class for compressing and uncompressing data.
42 ClassImp(AliL3Compress)
44 AliL3Compress::AliL3Compress()
53 AliL3Compress::AliL3Compress(Int_t slice,Int_t patch,Char_t *path,Bool_t writeshape,Int_t event)
59 sprintf(fPath,"%s",path);
60 fWriteShape=writeshape;
63 AliL3Compress::~AliL3Compress()
69 Bool_t AliL3Compress::WriteFile(AliL3TrackArray *tracks,Char_t *filename)
73 sprintf(fname,"%s/comp/%s",fPath,filename);
75 sprintf(fname,"%s/comp/tracks_m_%d_%d.raw",fPath,fSlice,fPatch);
77 sprintf(fname,"%s/comp/tracks_m_%d_%d_%d.raw",fPath,fEvent,fSlice,fPatch);
79 FILE *file = fopen(fname,"w");
82 cerr<<"AliL3Compress::WriteFile : Error opening file "<<fname<<endl;
85 Short_t ntracks = tracks->GetNTracks();
88 AliL3ClusterModel *clusters=0;
89 AliL3TrackModel *model=0;
90 for(Int_t i=0; i<ntracks; i++)
92 AliL3ModelTrack *track = (AliL3ModelTrack*)tracks->GetCheckedTrack(i);
95 //Do not save useless tracks or clusters:
96 //if(track->GetNPresentClusters() == 0)
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 if(fwrite(clusters,AliL3Transform::GetNRows(fPatch)*sizeof(AliL3ClusterModel),1,file)!=1) break;
113 Bool_t AliL3Compress::ReadFile(Char_t which,Char_t *filename)
115 //Read the trackfile.
119 sprintf(fname,"%s/comp/%s",fPath,filename);
125 sprintf(fname,"%s/comp/tracks_m_%d_%d.raw",fPath,fSlice,fPatch);
127 sprintf(fname,"%s/comp/tracks_m_%d_%d_%d.raw",fPath,fEvent,fSlice,fPatch);
129 else if(which == 'u')
132 sprintf(fname,"%s/comp/tracks_u_%d_%d.raw",fPath,fSlice,fPatch);
134 sprintf(fname,"%s/comp/tracks_u_%d_%d_%d.raw",fPath,fEvent,fSlice,fPatch);
138 cerr<<"AliL3Compress::ReadFile() : Wrong option"<<endl;
143 FILE *file = fopen(fname,"r");
146 cerr<<"AliL3Compress::ReadFile : Cannot open file "<<fname<<endl;
152 fTracks = new AliL3TrackArray("AliL3ModelTrack");
156 AliL3ModelTrack *track = (AliL3ModelTrack*)fTracks->NextTrack();
157 track->Init(fSlice,fPatch);
158 AliL3TrackModel *model = track->GetModel();
159 AliL3ClusterModel *clusters = track->GetClusters();
160 if(fread(model,sizeof(AliL3TrackModel),1,file)!=1) break;
161 //if(fread(clusters,model->fNClusters*sizeof(AliL3ClusterModel),1,file)!=1) break;
162 if(fread(clusters,AliL3Transform::GetNRows(fPatch)*sizeof(AliL3ClusterModel),1,file)!=1) break;
166 fTracks->RemoveLast();
171 Bool_t AliL3Compress::CompressFile()
175 sprintf(fname,"%s/comp/tracks_c_%d_%d.raw",fPath,fSlice,fPatch);
177 sprintf(fname,"%s/comp/tracks_c_%d_%d_%d.raw",fPath,fEvent,fSlice,fPatch);
178 BIT_FILE *output = OpenOutputBitFile(fname);
181 sprintf(fname,"%s/comp/tracks_m_%d_%d.raw",fPath,fSlice,fPatch);
183 sprintf(fname,"%s/comp/tracks_m_%d_%d_%d.raw",fPath,fEvent,fSlice,fPatch);
185 FILE *input = fopen(fname,"r");
188 cerr<<"AliL3Compress::CompressFile() : Error opening file: "<<fname<<endl;
192 AliL3TrackModel track;
193 AliL3ClusterModel cluster;
197 Int_t timeo,pado,chargeo,shapeo;
198 timeo=pado=chargeo=shapeo=0;
201 if(fread(&track,sizeof(AliL3TrackModel),1,input)!=1) break;
203 if(output->mask != 0x80) //Write the current byte to file.
205 //cerr<<"\nAliL3Compress::CompressFile() : Writing overhead bits!!!"<<endl;
206 if(putc(output->rack,output->file )!=output->rack)
207 cerr<<"AliL3Compress::ComressFile : Error writing to bitfile"<<endl;
212 //Write track parameters:
213 fwrite(&track,sizeof(AliL3TrackModel),1,output->file);
215 Int_t origslice=-1,slice,clustercount=0;
216 for(Int_t i=0; i<AliL3Transform::GetNRows(fPatch); i++)
218 if(fread(&cluster,sizeof(AliL3ClusterModel),1,input)!=1) break;
221 temp = (Int_t)cluster.fPresent;
222 OutputBit(output,temp);
225 if(cluster.fSlice<0 || cluster.fSlice>35)
227 cerr<<"AliL3DataCompress::CompressFile : Fucked up slice number :"<<cluster.fSlice<<endl;
231 //Write slice number of first point
234 origslice = cluster.fSlice;
235 OutputBits(output,origslice,6); //Need 6 bits to encode slice number
239 slice = cluster.fSlice;
240 if(slice == origslice)
245 OutputBits(output,slice,6);
250 //Write time information:
251 temp = (Int_t)rint(cluster.fDTime);
256 power = 1<<(AliL3DataCompressor::GetNTimeBits()-1);
262 OutputBits(output,abs(temp),(AliL3DataCompressor::GetNTimeBits()-1));
264 //Write pad information:
265 temp = (Int_t)rint(cluster.fDPad);
270 power = 1<<(AliL3DataCompressor::GetNPadBits()-1);
276 OutputBits(output,abs(temp),(AliL3DataCompressor::GetNPadBits()-1));
278 //Write charge information:
279 temp = (Int_t)cluster.fDCharge;
280 power = 1<<(AliL3DataCompressor::GetNChargeBits());
286 OutputBits(output,abs(temp),(AliL3DataCompressor::GetNChargeBits()));
290 //Write shape information:
291 temp = (Int_t)cluster.fDSigmaY2;
296 power = 1<<(AliL3DataCompressor::GetNShapeBits()-1);
297 if(abs(temp) >= power)
302 OutputBits(output,abs(temp),(AliL3DataCompressor::GetNShapeBits()-1));
304 temp = (Int_t)cluster.fDSigmaZ2;
309 power = 1<<(AliL3DataCompressor::GetNShapeBits()-1);
310 if(abs(temp) >= power)
315 OutputBits(output,abs(temp),(AliL3DataCompressor::GetNShapeBits()-1));
323 CloseOutputBitFile(output);
324 if(pado || timeo || chargeo || shapeo)
326 cout<<endl<<"Saturations: "<<endl
328 <<"Time "<<timeo<<endl
329 <<"Charge "<<chargeo<<endl
330 <<"Shape "<<shapeo<<endl<<endl;
335 Bool_t AliL3Compress::ExpandFile()
339 sprintf(fname,"%s/comp/tracks_c_%d_%d.raw",fPath,fSlice,fPatch);
341 sprintf(fname,"%s/comp/tracks_c_%d_%d_%d.raw",fPath,fEvent,fSlice,fPatch);
342 BIT_FILE *input = OpenInputBitFile(fname);
345 sprintf(fname,"%s/comp/tracks_u_%d_%d.raw",fPath,fSlice,fPatch);
347 sprintf(fname,"%s/comp/tracks_u_%d_%d_%d.raw",fPath,fEvent,fSlice,fPatch);
348 FILE *output = fopen(fname,"w");
351 cerr<<"AliL3Compress::ExpandFile() : Error opening file: "<<fname<<endl;
355 AliL3TrackModel trackmodel;
356 AliL3ClusterModel *clusters=0;
359 clusters = new AliL3ClusterModel[(AliL3Transform::GetNRows(fPatch))];
360 while(!feof(input->file))
362 input->mask=0x80;//make sure we read a new byte from file.
364 //Read and write track:
365 if(fread(&trackmodel,sizeof(AliL3TrackModel),1,input->file)!=1) break;
366 fwrite(&trackmodel,sizeof(AliL3TrackModel),1,output);
368 memset(clusters,0,AliL3Transform::GetNRows(fPatch)*sizeof(AliL3ClusterModel));
369 Int_t origslice=-1,clustercount=0;
370 for(Int_t i=0; i<AliL3Transform::GetNRows(fPatch); i++)
375 temp = InputBit(input);
378 clusters[i].fPresent=kFALSE;
381 clusters[i].fPresent=kTRUE;
383 //Read slice information
386 temp = InputBits(input,6);
387 clusters[i].fSlice = temp;
392 temp = InputBit(input);
394 clusters[i].fSlice = origslice;
397 temp = InputBits(input,6);//read new slice
398 clusters[i].fSlice = temp;
399 origslice = temp;//store new slice
403 //Read time information:
404 sign=InputBit(input);
405 temp = InputBits(input,(AliL3DataCompressor::GetNTimeBits()-1));
408 clusters[i].fDTime = temp;
410 //Read pad information:
411 sign=InputBit(input);
412 temp = InputBits(input,(AliL3DataCompressor::GetNPadBits()-1));
415 clusters[i].fDPad = temp;
417 //Read charge information:
418 temp=InputBits(input,(AliL3DataCompressor::GetNChargeBits()));
419 clusters[i].fDCharge = temp;
423 //Read shape information:
424 sign = InputBit(input);
425 temp = InputBits(input,(AliL3DataCompressor::GetNShapeBits()-1));
428 clusters[i].fDSigmaY2 = temp;
430 sign = InputBit(input);
431 temp = InputBits(input,(AliL3DataCompressor::GetNShapeBits()-1));
434 clusters[i].fDSigmaZ2 = temp;
439 //fwrite(clusters,(trackmodel.fNClusters)*sizeof(AliL3ClusterModel),1,output);
440 fwrite(clusters,AliL3Transform::GetNRows(fPatch)*sizeof(AliL3ClusterModel),1,output);
445 CloseInputBitFile(input);
449 void AliL3Compress::PrintCompRatio(ofstream *outfile)
451 AliL3MemHandler *mem = new AliL3MemHandler();
453 UInt_t remain_size=0,digit_size=0;
454 for(Int_t i=0; i<36; i++)
457 sprintf(fname,"%s/comp/remains_%d_%d.raw",fPath,i,-1);
459 sprintf(fname,"%s/comp/remains_%d_%d_%d.raw",fPath,fEvent,i,-1);
460 mem->SetBinaryInput(fname);
461 remain_size += mem->GetFileSize();
462 mem->CloseBinaryInput();
464 sprintf(fname,"%s/binaries/digits_c8_%d_%d_%d.raw",fPath,fEvent,i,-1);
465 mem->SetBinaryInput(fname);
466 digit_size += mem->GetFileSize();
467 mem->CloseBinaryInput();
472 sprintf(fname,"%s/comp/tracks_c_%d_%d.raw",fPath,fSlice,fPatch);
474 sprintf(fname,"%s/comp/tracks_c_%d_%d_%d.raw",fPath,fEvent,fSlice,fPatch);
476 mem->SetBinaryInput(fname);
477 UInt_t compress_size = mem->GetFileSize();
478 mem->CloseBinaryInput();
482 cerr<<"AliL3Compress::PrintCompRatio : Zero digit size, not able to obtain comp. ratios!"<<endl;
486 Float_t compratio = (Float_t)(compress_size + remain_size)/(Float_t)digit_size;
489 ofstream &out = *outfile;
490 out<<compress_size<<' '<<remain_size<<' '<<digit_size<<endl;
493 cout<<"=========================================="<<endl;
494 cout<<"Original digits size : "<<digit_size/1000<<" kByte ( 100 % )"<<endl;
495 cout<<"Compressed file size : "<<compress_size/1000<<" kByte ( "<<(Float_t)compress_size*100/(Float_t)digit_size<<" % )"<<endl;
496 cout<<"Remainig file size : "<<remain_size/1000<<" kByte ( "<<(Float_t)remain_size*100/(Float_t)digit_size<<" % )"<<endl;
497 cout<<"---------------------- "<<endl;
498 cout<<"Compression ratio : "<<compratio*100<<" %"<<endl;
499 cout<<"=========================================="<<endl;