1 /**************************************************************************
2 * Copyright(c) 1998-2003, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
17 // This class contains the implementation of the
18 // compression and decompression algorithms
19 // Compression is performed reading the Altro data block (called packet) backward.
20 // Similarly decompression is also done backward so that the final file is restored
21 // after the compression and decompression phase.
24 #include <TObjArray.h>
25 #include "Riostream.h"
27 #include "AliTPCCompression.h"
28 #include "AliTPCBuffer160.h"
29 #include "AliTPCHuffman.h"
31 ClassImp(AliTPCCompression)
32 //////////////////////////////////////////////////////////////////////////////////////////////////
33 AliTPCCompression::AliTPCCompression(){
35 fDimBuffer=sizeof(ULong_t)*8;
36 fFreeBitsBuffer=fDimBuffer;
44 //////////////////////////////////////////////////////////////////////////////////////////////////
45 AliTPCCompression::AliTPCCompression(const AliTPCCompression &source){
47 this->fDimBuffer=source.fDimBuffer;
48 this->fFreeBitsBuffer=source.fFreeBitsBuffer;
49 this->fReadBits=source.fReadBits;
50 this->fPos=source.fPos;
51 this->fBuffer=source.fBuffer;
52 this->fVerbose=source.fVerbose;
53 this->fFillWords=source.fFillWords;
56 //////////////////////////////////////////////////////////////////////////////////////////////////
57 AliTPCCompression& AliTPCCompression::operator=(const AliTPCCompression &source){
58 //Redefinition of the assignment operator
59 this->fDimBuffer=source.fDimBuffer;
60 this->fFreeBitsBuffer=source.fFreeBitsBuffer;
61 this->fReadBits=source.fReadBits;
62 this->fPos=source.fPos;
63 this->fBuffer=source.fBuffer;
64 this->fVerbose=source.fVerbose;
65 this->fFillWords=source.fFillWords;
68 //////////////////////////////////////////////////////////////////////////////////////////////////
69 void AliTPCCompression::NextTable(Int_t Val,Int_t &NextTableType,Int_t &BunchLen,Int_t &Count)const{
70 //Depending on the data type (5 types of data) a specific table is called
73 0==> Bunch length value
79 switch (NextTableType){
86 if (BunchLen==1)NextTableType=2;
99 if (Count==(BunchLen-1)){
119 /////////////////////////////////////////////////////////////////////////////////////////////////////
121 /////////////////////////////////////////////////////////////////////////////////////////////////////
123 Int_t AliTPCCompression::FillTables(const char* fSource,AliTPCHTable* table[],const Int_t NumTables){
124 //This method is used to compute the frequencies of the symbols in the source file
125 AliTPCBuffer160 buff(fSource,0);
126 ULong_t countWords=0;
127 ULong_t countTrailer=0;
128 Int_t numWords,padNum,rowNum,secNum=0;
130 ULong_t stat[5]={0,0,0,0,0};
133 while(buff.ReadTrailerBackward(numWords,padNum,rowNum,secNum) !=-1 ){
135 endFill=buff.GetFillWordsNum();
140 fFillWords+=4-numWords%4;
141 for(Int_t j=0;j<(4-numWords%4);j++){
142 value=buff.GetNextBackWord();
149 for(Int_t i=0;i<345;i++)timePos[i]=0;
150 for(Int_t i=0;i<1024;i++)packet[i]=0;
152 Int_t nextTableType=0;
155 for(Int_t i=0;i<numWords;i++){
156 value=buff.GetNextBackWord();
158 if(nextTableType==1){
162 NextTable(value,nextTableType,bunchLen,count);
164 //computing the Time gap between two bunches
167 Int_t previousTime=packet[timePos[tp]];
168 for(Int_t i=tp-1;i>=0;i--){
169 Int_t timPos=timePos[i];
170 Int_t bunchLen=packet[timPos-1]-2;
172 packet[timPos]=packet[timPos]-previousTime-bunchLen;
178 for(Int_t i=0;i<numWords;i++){
180 table[nextTableType]->SetFrequency(value);
181 stat[nextTableType]++;
182 NextTable(value,nextTableType,bunchLen,count);
186 cout<<"Number of words: "<<countWords<<endl;
187 cout<<"Number of trailers: "<<countTrailer<<endl;
188 cout<<"Number of fill words "<<fFillWords+endFill<<endl;
189 cout<<"Total number of words: "<<countWords+countTrailer*4+fFillWords<<endl;
191 fStat.open("Statistics");
192 fStat<<"Number of words:..........................................."<<countWords<<endl;
193 fStat<<"Number of trailers (4 10 bits words in each one)..........."<<countTrailer<<endl;
194 fStat<<"Number of fill words:......................................"<<fFillWords+endFill<<endl;
195 fStat<<"Total number of words:....................................."<<countWords+countTrailer*4+fFillWords+endFill<<endl;
196 fStat<<"-----------------------------------------"<<endl;
197 fStat<<"Number of Bunches............."<<stat[0]<<endl;
198 fStat<<"Number of Time bin............"<<stat[1]<<endl;
199 fStat<<"Number of One Samples Bunch..."<<stat[2]<<endl;
200 fStat<<"Number of Central Samples....."<<stat[3]<<endl;
201 fStat<<"Number of Border Samples......"<<stat[4]<<endl;
202 fStat<<"-----------------------------------------"<<endl;
203 ULong_t fileDimension=(ULong_t)TMath::Ceil(double((countTrailer*4+countWords+fFillWords+endFill)*10/8));
204 fStat<<"Total file Size in bytes.."<<fileDimension<<endl;
205 Double_t percentage=TMath::Ceil((fFillWords+endFill)*125)/fileDimension;
206 fStat<<"Fill Words................"<<(ULong_t)TMath::Ceil((fFillWords+endFill)*10/8)<<" bytes "<<percentage<<"%"<<endl;
207 percentage=(Double_t)countTrailer*500/fileDimension;
208 fStat<<"Trailer..................."<<countTrailer*5<<" bytes "<<percentage<<"%"<<endl;
210 percentage=(Double_t)((stat[0]+stat[1]+stat[2]+stat[3]+stat[4])) *125/fileDimension;
211 fStat<<"Data......................"<<(ULong_t)TMath::Ceil((stat[0]+stat[1]+stat[2]+stat[3]+stat[4])*10/8)<<" bytes "<<percentage<<"%"<<endl;
213 percentage=(Double_t)(stat[0]*125)/fileDimension;
214 fStat<<"Bunch....................."<<(ULong_t)TMath::Ceil(stat[0]*10/8)<<" bytes "<<percentage<<"%"<<endl; //
215 percentage=(Double_t)(stat[1]*125)/fileDimension;
216 fStat<<"Time......................"<<(ULong_t)TMath::Ceil(stat[1]*10/8)<<" bytes "<<percentage<<"%"<<endl; //
219 percentage=(Double_t)((stat[2]+stat[3]+stat[4])) *125/fileDimension;
220 fStat<<"Amplitude values.........."<<(ULong_t)TMath::Ceil((stat[2]+stat[3]+stat[4])*10/8)<<" bytes "<<percentage<<"%"<<endl;
221 percentage=(Double_t)(stat[2]*125)/fileDimension;
222 fStat<<" One Samples..............."<<(ULong_t)TMath::Ceil(stat[2]*10/8)<<" bytes "<<percentage<<"%"<<endl; //
223 percentage=(Double_t)(stat[3]*125)/fileDimension;
224 fStat<<" Central Samples..........."<<(ULong_t)TMath::Ceil(stat[3]*10/8)<<" bytes "<<percentage<<"%"<<endl; //
225 percentage=(Double_t)(stat[4]*125)/fileDimension;
226 fStat<<" Border Samples............"<<(ULong_t)TMath::Ceil(stat[4]*10/8)<<" bytes "<<percentage<<"%"<<endl; //
230 ////////////////////////////////////////////////////////////////////////////////////////
231 Int_t AliTPCCompression::StoreTables(AliTPCHTable* table[],const Int_t NumTable){
232 //This method stores the tables in a sequence of binary file
235 for(Int_t k=0;k<NumTable;k++){
236 sprintf(filename,"Table%d.dat",k);
238 fTable.open(filename,ios::binary);
240 fTable.open(filename);
242 Int_t dim=table[k]->Size();
243 //Table dimension is written into a file
244 fTable.write((char*)(&dim),sizeof(Int_t));
245 //One table is written into a file
246 for(Int_t i=0;i<dim;i++){
247 UChar_t codeLen=table[k]->CodeLen()[i];
248 // ULong_t code=(ULong_t)table[k]->Code()[i];
249 Double_t code=table[k]->Code()[i];
250 fTable.write((char*)(&codeLen),sizeof(UChar_t));
251 //fTable.write((char*)(&code),sizeof(ULong_t));
252 fTable.write((char*)(&code),sizeof(Double_t));
258 ////////////////////////////////////////////////////////////////////////////////////////
259 Int_t AliTPCCompression::CreateTables(const char* fSource,const Int_t NumTables){
263 0==> Bunch length values
269 Int_t n=10;// 10 bits per symbol
270 AliTPCHTable ** table = new AliTPCHTable*[NumTables];
271 //The table is inizialized with the rigth number of rows
272 for(Int_t i=0;i<NumTables;i++){table[i]=new AliTPCHTable((Int_t)(TMath::Power(2,n)));}
273 //The frequencies are calculated and the tables are filled
275 cout<<"Filling tables...\n";
276 //The get the frequencies
277 FillTables(fSource,table,NumTables);
279 //This part will be used in the table optimization phase
281 for(Int_t i=0;i<NumTables;i++){
282 table[i]->CompleteTable(i);
286 cout<<"Entropy of Bunch length table........."<<table[0]->GetEntropy()<<endl;
287 cout<<"Entropy of Time bin table............."<<table[1]->GetEntropy()<<endl;
288 cout<<"Entropy of one Sample bunch table....."<<table[2]->GetEntropy()<<endl;
289 cout<<"Entropy of Central Sample table......."<<table[3]->GetEntropy()<<endl;
290 cout<<"Entropy Border Samples table.........."<<table[4]->GetEntropy()<<endl;
292 fStat.open("Statistics",ios::app);
294 fStat<<"----------------- ENTROPY for castomized tables --------------------------"<<endl;
295 fStat<<"Entropy of Bunch length table......."<<table[0]->GetEntropy()<<endl;
296 fStat<<"Entropy of Time bin table..........."<<table[1]->GetEntropy()<<endl;
297 fStat<<"Entropy of one Sample bunch table..."<<table[2]->GetEntropy()<<endl;
298 fStat<<"Entropy of Central Sample table....."<<table[3]->GetEntropy()<<endl;
299 fStat<<"Entropy Border Samples table........"<<table[4]->GetEntropy()<<endl;
303 cout<<"Tables filled \n";
304 //Tables are saved in a sequence of text file and using the macro Histo.C is it possible to get
305 //a series of histograms rappresenting the frequency distribution
306 table[0]->StoreFrequencies("BunchLenFreq.txt");
307 table[1]->StoreFrequencies("TimeFreq.txt");
308 table[2]->StoreFrequencies("Sample1Freq.txt");
309 table[3]->StoreFrequencies("SCentralFreq.txt");
310 table[4]->StoreFrequencies("SBorderFreq.txt");
312 cout<<"Creating Tables..\n";
313 //One Huffman tree is created for each table starting from the frequencies of the symbols
314 for(Int_t i=0;i<NumTables;i++){
315 table[i]->BuildHTable();
317 cout<<"Number of elements inside the table:"<<table[i]->GetWordsNumber();
320 cout<<" (Bunch Length)"<<endl;
324 cout<<" (Time Bin)"<<endl;
328 cout<<" (1 Samples Bunch)"<<endl;
332 cout<<" (Central Samples)"<<endl;
336 cout<<" (Border Samples)"<<endl;
340 table[i]->PrintTable();
343 //The tables are saved ad binary files
344 StoreTables(table,NumTables);
345 //The tables stored in memory are deleted;
346 for(Int_t i=0;i<NumTables;i++)delete table[i];
350 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
351 Int_t AliTPCCompression::RetrieveTables(AliTPCHTable* table[],Int_t NumTable){
352 //This method retrieve the Huffman tables from a sequence of binary files
354 cout<<"Retrieving tables from files \n";
360 //The following for loop is used to generate the Huffman trees acording to the tables
361 for(Int_t k=0;k<NumTable;k++){
362 Int_t dim;//this variable contains the table dimension
363 sprintf(filename,"Table%d.dat",k);
365 fTable.open(filename,ios::binary);
367 fTable.open(filename);
369 fTable.read((char*)(&dim),sizeof(Int_t));
371 cout<<"Table dimension: "<<dim<<endl;
372 table[k]=new AliTPCHTable(dim);
373 for(Int_t i=0;i<dim;i++){
374 fTable.read((char*)(&codeLen),sizeof(UChar_t));
375 table[k]->SetCodeLen(codeLen,i);
376 // fTable.read((char*)(&code),sizeof(ULong_t));
377 fTable.read((char*)(&code),sizeof(Double_t));
378 table[k]->SetCode(Mirror((ULong_t)code,codeLen),i);
383 cout<<"Trees generated \n";
384 //At this point the trees are been built
387 ////////////////////////////////////////////////////////////////////////////////////////
389 ////////////////////////////////////////////////////////////////////////////////////////
391 void AliTPCCompression::StoreValue(ULong_t val,UChar_t len){
392 //This method stores the value "val" of "len" bits into the internal buffer "fBuffer"
393 if (len<=fFreeBitsBuffer){ // val is not splitted in two buffer
394 fFreeBitsBuffer-=len;
395 fBuffer=fBuffer<<len;
397 if(!fFreeBitsBuffer){ // if the buffer is full it is written into a file
398 f.write((char*)(&fBuffer),sizeof(ULong_t));
399 fFreeBitsBuffer=fDimBuffer;
403 else{ //val has to be splitted in two buffers
404 fBuffer=fBuffer<<fFreeBitsBuffer;
407 temp=temp>>(len-fFreeBitsBuffer);
408 fBuffer=fBuffer|temp;
409 f.write((char*)(&fBuffer),sizeof(ULong_t));
410 fFreeBitsBuffer=fDimBuffer-(len-fFreeBitsBuffer);
411 val=val<<fFreeBitsBuffer;
412 val=val>>fFreeBitsBuffer;
417 //////////////////////////////////////////////////////////////////////////////////////////////////
418 void AliTPCCompression::Flush(){
419 //The last buffer cannot be completely full so to save it
420 //into the output file it is first necessary to fill it with an hexadecimal pattern
421 if(fFreeBitsBuffer<fDimBuffer){
422 fBuffer=fBuffer<<fFreeBitsBuffer;
423 f.write((char*)(&fBuffer),sizeof(ULong_t));
427 //////////////////////////////////////////////////////////////////////////////////////////////////
428 ULong_t AliTPCCompression::Mirror(ULong_t val,UChar_t len)const{
429 //This method inverts the digits of the number "val" and length "len"
430 //indicates the number of digits of the number considered in binary notation
434 for(Int_t i=0;i<len;i++){
437 specular=specular<<1;
438 specular=specular|bit;
443 //////////////////////////////////////////////////////////////////////////////////////////////////
444 Int_t AliTPCCompression::CompressData(AliTPCHTable* table[],Int_t NumTable,const char* fSource,const char* fDest){
445 //This method is used to compress the data stored in the Altro format file using specific tables
446 //calculated considering the frequencies of the symbol of the file that has to be compressed
447 cout<<" COMPRESSION "<<endl;
448 cout<<"compression of the file "<<fSource<<" Output File: "<<fDest<<endl;
449 //the output file is open
451 f.open(fDest,ios::binary|ios::out);
453 f.open(fDest,ios::out);
455 //Tables are written into the output file
456 for(Int_t k=0;k<NumTable;k++){
457 Int_t dim=table[k]->Size();
458 //Table dimension is written into a file
459 f.write((char*)(&dim),sizeof(Int_t));
460 //One table is written into a file
461 for(Int_t i=0;i<dim;i++){
462 UChar_t codeLen=table[k]->CodeLen()[i];
463 ULong_t code=(ULong_t)table[k]->Code()[i];
464 f.write((char*)(&codeLen),sizeof(UChar_t));
465 f.write((char*)(&code),sizeof(ULong_t));
469 // Source file is open
470 AliTPCBuffer160 buff(fSource,0);
471 //coded words are written into the output file
472 Int_t numWords,padNum,rowNum,secNum=0;
473 ULong_t storedWords=0;
475 ULong_t numPackets=0;
476 while(buff.ReadTrailerBackward(numWords,padNum,rowNum,secNum) !=-1 ){
479 for(Int_t j=0;j<(4-numWords%4);j++){
480 value=buff.GetNextBackWord();
487 for(Int_t i=0;i<345;i++)timePos[i]=0;
488 for(Int_t i=0;i<1024;i++)packet[i]=0;
490 Int_t nextTableType=0;
493 for(Int_t i=0;i<numWords;i++){
494 value=buff.GetNextBackWord();
496 if(nextTableType==1){
500 NextTable(value,nextTableType,bunchLen,count);
502 //computing the Time gap between two bunches
505 Int_t previousTime=packet[timePos[tp]];
506 for(Int_t i=tp-1;i>=0;i--){
507 Int_t timPos=timePos[i];
508 Int_t bunchLen=packet[timPos-1]-2;
510 packet[timPos]=packet[timPos]-previousTime-bunchLen;
517 //All the words for one pad are compressed and stored in the compress file
518 for(Int_t i=0;i<numWords;i++){
520 if(nextTableType==1)timeBin=value;
522 // ULong_t val=(ULong_t)table[nextTableType]->Code()[value]; // val is the code
523 Double_t val=table[nextTableType]->Code()[value]; // val is the code
524 UChar_t len=table[nextTableType]->CodeLen()[value]; // len is the length (number of bits)of val
525 StoreValue(Mirror((ULong_t)val,len),len);
528 NextTable(value,nextTableType,bunchLen,count);
529 if(nextTableType==0){
530 // ULong_t val=(ULong_t)table[1]->Code()[timeBin]; // val is the code
531 Double_t val=table[1]->Code()[timeBin]; // val is the code
532 UChar_t len=table[1]->CodeLen()[timeBin]; // len is the length (number of bits)of val
533 StoreValue(Mirror((ULong_t)val,len),len);
534 //val=(ULong_t)table[nextTableType]->Code()[(bunchLen+2)]; // val is the code
535 val=table[nextTableType]->Code()[(bunchLen+2)]; // val is the code
536 len=table[nextTableType]->CodeLen()[(bunchLen+2)]; // len is the length (number of bits)of val
537 StoreValue(Mirror((ULong_t)val,len),len);
542 StoreValue(numWords,10);
543 StoreValue(padNum,10);
544 StoreValue(rowNum,10);
545 StoreValue(secNum,9);
549 StoreValue(numPackets,32);
550 cout<<"Number of strored packet: "<<numPackets<<endl;
552 //The last buffen cannot be completely full
554 cout<<"Number of stored words: "<<storedWords<<endl;
559 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
560 Int_t AliTPCCompression::CompressDataOptTables(Int_t NumTable,const char* fSource,const char* fDest){
561 //This method compress an Altro format file using a general set of tables stored as binary files to be provided
563 cout<<" BackWord COMPRESSION "<<endl;
564 cout<<"compression of the file "<<fSource<<" Output File: "<<fDest<<endl;
566 //Tables are read from the files (Each codeword has been "Mirrored")
567 AliTPCHTable **table = new AliTPCHTable*[NumTable];
568 RetrieveTables(table,NumTable);
569 //the output file is open
571 f.open(fDest,ios::binary|ios::out);
573 f.open(fDest,ios::out);
575 // Source file is open
576 AliTPCBuffer160 buff(fSource,0);
577 //coded words are written into a file
578 Int_t numWords,padNum,rowNum,secNum=0;
579 ULong_t storedWords=0;
581 ULong_t numPackets=0;
582 Double_t stat[5]={0.,0.,0.,0.,0.};
583 ULong_t trailerNumbers=0;
584 Double_t numElem[5]={0,0,0,0,0};
585 Double_t fillWords=0.;
586 fStat.open("Statistics",ios::app);
588 fStat<<"-------------------COMPRESSION STATISTICS----------"<<endl;
590 while(buff.ReadTrailerBackward(numWords,padNum,rowNum,secNum) !=-1 ){
592 fillWords=buff.GetFillWordsNum();
598 fillWords+=4-numWords%4;
599 for(Int_t j=0;j<(4-numWords%4);j++){
600 value=buff.GetNextBackWord();
607 for(Int_t i=0;i<345;i++)timePos[i]=0;
608 for(Int_t i=0;i<1024;i++)packet[i]=0;
610 Int_t nextTableType=0;
613 for(Int_t i=0;i<numWords;i++){
614 value=buff.GetNextBackWord();
616 if(nextTableType==1){
620 NextTable(value,nextTableType,bunchLen,count);
622 //computing the Time gap between two bunches
625 Int_t previousTime=packet[timePos[tp]];
626 for(Int_t i=tp-1;i>=0;i--){
627 Int_t timPos=timePos[i];
628 Int_t bunchLen=packet[timPos-1]-2;
630 packet[timPos]=packet[timPos]-previousTime-bunchLen;
638 for(Int_t i=0;i<numWords;i++){
640 if(nextTableType==1)timeBin=value;
642 //ULong_t val=(ULong_t)table[nextTableType]->Code()[value]; // val is the code
643 Double_t val=table[nextTableType]->Code()[value]; // val is the code
644 UChar_t len=table[nextTableType]->CodeLen()[value]; // len is the length (number of bits)of val
645 stat[nextTableType]+=len;
646 numElem[nextTableType]++;
647 StoreValue((ULong_t)val,len);
650 NextTable(value,nextTableType,bunchLen,count);
651 if(nextTableType==0){
652 // ULong_t val=(ULong_t)table[1]->Code()[timeBin]; // val is the code
653 Double_t val=table[1]->Code()[timeBin]; // val is the code
654 UChar_t len=table[1]->CodeLen()[timeBin]; // len is the length (number of bits)of val
657 StoreValue((ULong_t)val,len);
658 // val=(ULong_t)table[nextTableType]->Code()[(bunchLen+2)]; // val is the code
659 val=table[nextTableType]->Code()[(bunchLen+2)]; // val is the code
660 len=table[nextTableType]->CodeLen()[(bunchLen+2)]; // len is the length (number of bits)of val
661 StoreValue((ULong_t)val,len);
662 stat[nextTableType]+=len;
663 numElem[nextTableType]++;
668 StoreValue(numWords,10);
669 StoreValue(padNum,10);
670 StoreValue(rowNum,10);
671 StoreValue(secNum,9);
676 StoreValue(numPackets,32);
678 cout<<"Number of strored packets: "<<numPackets<<endl;
680 //The last buffen cannot be completely full
683 cout<<"Number of stored words: "<<storedWords<<endl;
686 for(Int_t i=0;i<NumTable;i++){
690 Double_t dimension=(ULong_t)TMath::Ceil((stat[0]+stat[1]+stat[2]+stat[3]+stat[4])/8)+trailerNumbers*5;
691 fStat<<"Trailer Dimension in bytes......"<<trailerNumbers*5<<endl;
692 fStat<<"Data Dimension in bytes........."<<(ULong_t)TMath::Ceil((stat[0]+stat[1]+stat[2]+stat[3]+stat[4])/8)<<endl;
693 fStat<<"Compressed file dimension......."<<(ULong_t)dimension<<endl;
695 fStat<<(ULong_t)trailerNumbers<<endl;
696 fStat<<(ULong_t)fillWords<<endl;
697 fStat<<(ULong_t)numElem[0]<<endl;
698 fStat<<(ULong_t)numElem[1]<<endl;
699 fStat<<(ULong_t)numElem[2]<<endl;
700 fStat<<(ULong_t)numElem[3]<<endl;
701 fStat<<(ULong_t)numElem[4]<<endl;
703 fillWords=(fillWords+numElem[0]+numElem[1]+numElem[2]+numElem[3]+numElem[4]+trailerNumbers*4)*10/8;
704 fStat<<"Original file dimension........."<<(ULong_t)fillWords<<endl;
706 Double_t ratio=(dimension/fillWords)*100;
707 fStat<<"Compression ratio (Compressed/Uncompressed)..."<<ratio<<"%"<<endl;
709 fStat<<"Bunch length size in bytes......"<<(ULong_t)TMath::Ceil(stat[0]/8)<<" Comppression.."<<(stat[0]/numElem[0])*10<<"%"<<endl;
711 fStat<<"Time gap size in bytes.........."<<(ULong_t)TMath::Ceil(stat[1]/8)<<" Comppression.."<<(stat[1]/numElem[1])*10<<"%"<<endl;
712 fStat<<"Amplitude values in bytes......."<<(ULong_t)TMath::Ceil((stat[2]+stat[3]+stat[4])/8)<<" Comppression.."<<
713 ((stat[2]+stat[3]+stat[4])/(numElem[2]+numElem[3]+numElem[4]))*10<<"%"<<endl;
714 fStat<<" One Samples in bytes............"<<(ULong_t)TMath::Ceil(stat[2]/8)<<" Comppression.."<<(stat[2]/numElem[2])*10<<"%"<<endl;
715 fStat<<" Central Samples size in bytes..."<<(ULong_t)TMath::Ceil(stat[3]/8)<<" Comppression.."<<(stat[3]/numElem[3])*10<<"%"<<endl;
716 fStat<<" Border Samples size in bytes...."<<(ULong_t)TMath::Ceil(stat[4]/8)<<" Comppression.."<<(stat[4]/numElem[4])*10<<"%"<<endl;
718 fStat<<"Average number of bits per word"<<endl;
719 fStat<<"Bunch length ......"<<stat[0]/numElem[0]<<endl;
720 fStat<<"Time gap .........."<<stat[1]/numElem[1]<<endl;
721 fStat<<"One Samples........"<<stat[2]/numElem[2]<<endl;
722 fStat<<"Central Samples ..."<<stat[3]/numElem[3]<<endl;
723 fStat<<"Border Samples....."<<stat[4]/numElem[4]<<endl;
728 ////////////////////////////////////////////////////////////////////////////////////////
730 ////////////////////////////////////////////////////////////////////////////////////////
732 ////////////////////////////////////////////////////////////////////////////////////////
733 void AliTPCCompression::CreateTrees(AliTPCHNode *RootNode[],const Int_t NumTables){
734 //The first part of the compressed file cotains the tables
735 //The following for loop is used to generate the Huffman trees acording to the tables
737 cout<<"Creating the Huffman trees \n";
742 //loop over the numbero of tables
743 for(Int_t k=0;k<NumTables;k++){
744 RootNode[k]=new AliTPCHNode(); //RootNode is the root of the tree
745 Int_t dim;//this variable contains the table dimension
746 f.read((char*)(&dim),sizeof(Int_t));
748 cout<<"Table dimension: "<<dim<<endl;
749 //loop over the words of a table
750 for(Int_t i=0;i<dim;i++){
751 f.read((char*)(&codeLen),sizeof(UChar_t));
752 //f.read((char*)(&code),sizeof(ULong_t));
753 f.read((char*)(&code),sizeof(Double_t));
755 for(Int_t j=1;j<=codeLen;j++){
757 val=(ULong_t)TMath::Power(2,codeLen-j);
758 bit=(ULong_t)code&val;
759 AliTPCHNode *temp=node;
761 node=node->GetRight();
763 node=new AliTPCHNode();
764 temp->SetRight(node);
768 node=node->GetLeft();
770 node=new AliTPCHNode();
777 node->SetFrequency(codeLen);
779 //cout<<node->GetSymbol()<<" "<<(Int_t)node->GetFrequency()<<endl;
783 cout<<"Trees generated \n";
784 //At this point the trees are been built
786 //////////////////////////////////////////////////////////////////////////////////////////////////
787 void AliTPCCompression::CreateTreesFromFile(AliTPCHNode *RootNode[],const Int_t NumTables){
788 //For each table this method builds the associate Huffman tree starting from the codeword and
789 //the codelength of each symbol
791 cout<<"Creating the Huffman trees \n";
798 //The following for loop is used to generate the Huffman trees acording to the tables
799 //loop over the tables
800 for(Int_t k=0;k<NumTables;k++){
801 RootNode[k]=new AliTPCHNode(); //RootNode is the root of the tree
802 Int_t dim=0;//this variable contains the table dimension
803 sprintf(filename,"Table%d.dat",k);
805 fTable.open(filename,ios::binary);
807 fTable.open(filename);
809 fTable.read((char*)(&dim),sizeof(Int_t));
811 cout<<"Table dimension: "<<dim<<endl;
812 //loop over the words of one table
813 for(Int_t i=0;i<dim;i++){
814 fTable.read((char*)(&codeLen),sizeof(UChar_t));
815 //fTable.read((char*)(&code),sizeof(ULong_t));
816 fTable.read((char*)(&code),sizeof(Double_t));
818 for(Int_t j=1;j<=codeLen;j++){
820 val=(ULong_t)TMath::Power(2,codeLen-j);
821 bit=(ULong_t)code&val;
822 AliTPCHNode *temp=node;
824 node=node->GetRight();
826 node=new AliTPCHNode();
827 temp->SetRight(node);
831 node=node->GetLeft();
833 node=new AliTPCHNode();
840 node->SetFrequency(codeLen);
846 cout<<"Trees generated \n";
847 //At this point the trees are been built
849 //////////////////////////////////////////////////////////////////////////////////////////////////
850 void AliTPCCompression::DeleteHuffmanTree(AliTPCHNode* node){
851 //This function deletes all the nodes of an Huffman tree
852 //In an Huffman tree any internal node has always two children
854 DeleteHuffmanTree(node->GetLeft());
855 DeleteHuffmanTree(node->GetRight());
856 // cout<<node->GetSymbol()<<" "<<(Int_t)node->GetFrequency()<<endl;
860 //////////////////////////////////////////////////////////////////////////////////////////////////
861 void AliTPCCompression::VisitHuffmanTree(AliTPCHNode* node){
862 //This function realizes an in order visit of a binary tree
864 cout<<node->GetSymbol()<<" "<<node->GetFrequency()<<endl;
865 VisitHuffmanTree(node->GetLeft());
866 VisitHuffmanTree(node->GetRight());
869 //////////////////////////////////////////////////////////////////////////////////////////////////
870 ULong_t AliTPCCompression::ReadWord(Int_t NumberOfBit){
871 //This method retrieves a word of a specific number of bits from the file through the internal buffer
874 for (Int_t i=0;i<NumberOfBit;i++){
876 fPos-=sizeof(ULong_t);
878 f.read((char*)(&fBuffer),sizeof(ULong_t));
882 mask=(ULong_t)TMath::Power(2,fReadBits);
891 //////////////////////////////////////////////////////////////////////////////////////////////////
892 void AliTPCCompression::ReadTrailer(Int_t &WordsNumber,Int_t &PadNumber,Int_t &RowNumber,Int_t &SecNumber){
893 //It retrieves a trailer
895 SecNumber=ReadWord(9);
896 RowNumber=ReadWord(10);
897 PadNumber=ReadWord(10);
898 WordsNumber=ReadWord(10);
901 //////////////////////////////////////////////////////////////////////////////////////////////////
902 ULong_t AliTPCCompression::GetDecodedWord(AliTPCHNode* root){
903 //This method retrieves a decoded word.
904 AliTPCHNode *node=root;
908 ULong_t bit=ReadWord(1);
910 node=node->GetRight();
912 node=node->GetLeft();
913 if (!(node->GetLeft())){
914 symbol=node->GetSymbol();
920 //////////////////////////////////////////////////////////////////////////////////////////////////
921 Int_t AliTPCCompression::DecompressData(Int_t NumTables,const char* fname,char* fDest){
922 //Decompression method
923 cout<<" DECOMPRESSION:"<<endl;
924 cout<<"Source File "<<fname<<" Destination File "<<fDest<<endl;
926 f.open(fname,ios::binary|ios::in);
928 f.open(fname,ios::in);
930 if(!f){cout<<"File doesn't exist\n";return -1;}
931 AliTPCHNode ** rootNode = new AliTPCHNode*[NumTables];
932 //Creation of the Huffman trees
933 CreateTrees(rootNode,NumTables);
934 //to go to the end of the file
936 //to get the file dimension in byte
938 fPos-=sizeof(ULong_t);
942 f.read((char*)(&fBuffer),sizeof(ULong_t));
950 ULong_t packetNumber=ReadWord(sizeof(ULong_t)*8);
951 cout<<"Number of Packect: "<<packetNumber<<endl;
952 AliTPCBuffer160 bufferFile(fDest,1);
954 ULong_t wordsRead=0; //number of read coded words
955 while(k<packetNumber){
956 Int_t numWords,padNumber,rowNumber,secNumber=0;
957 ReadTrailer(numWords,padNumber,rowNumber,secNumber);
960 Int_t previousTime=-1;
962 Int_t nextTableType=0;
965 for(Int_t i=0;i<numWords;i++){
966 ULong_t symbol=GetDecodedWord(rootNode[nextTableType]);
968 //Time reconstruction
969 if (nextTableType==1){
970 if (previousTime!=-1){
971 previousTime=symbol+previousTime+bunchLen;
973 else previousTime=symbol;
977 bufferFile.FillBuffer(symbol);
978 NextTable(symbol,nextTableType,bunchLen,count);
979 if(nextTableType==0){
980 bufferFile.FillBuffer(time);
981 bufferFile.FillBuffer(bunchLen+2);
985 bufferFile.WriteTrailer(numWords,padNumber,rowNumber,secNumber);
987 cout<<"Number of decoded words:"<<wordsRead<<endl;
989 //The trees are deleted
990 for(Int_t j=0;j<NumTables;j++){
991 DeleteHuffmanTree(rootNode[j]);
997 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
999 Int_t AliTPCCompression::DecompressDataOptTables(Int_t NumTables,const char* fname,char* fDest){
1000 //This method decompress a file using separate Huffman tables
1002 cout<<" DECOMPRESSION:"<<endl;
1003 cout<<"Source File "<<fname<<" Destination File "<<fDest<<endl;
1005 AliTPCHNode ** rootNode = new AliTPCHNode*[NumTables];
1006 //Creation of the Huffman trees
1007 CreateTreesFromFile(rootNode,NumTables);
1009 f.open(fname,ios::binary|ios::in);
1011 f.open(fname,ios::in);
1013 if(!f){cout<<"File doesn't exist\n";return -1;}
1014 //to go to the end of the file
1015 f.seekg(0,ios::end);
1016 //to get the file dimension in byte
1018 fPos-=sizeof(ULong_t);
1022 f.read((char*)(&fBuffer),sizeof(ULong_t));
1030 ULong_t packetNumber=ReadWord(sizeof(ULong_t)*8);
1032 cout<<"Number of Packect: "<<packetNumber<<endl;
1034 AliTPCBuffer160 bufferFile(fDest,1);
1036 ULong_t wordsRead=0; //number of read coded words
1037 while(k<packetNumber){
1038 Int_t numWords,padNumber,rowNumber,secNumber=0;
1039 ReadTrailer(numWords,padNumber,rowNumber,secNumber);
1042 Int_t previousTime=-1;
1044 Int_t nextTableType=0;
1047 for(Int_t i=0;i<numWords;i++){
1048 ULong_t symbol=GetDecodedWord(rootNode[nextTableType]);
1050 //Time reconstruction
1051 if (nextTableType==1){
1052 if (previousTime!=-1){
1053 previousTime=symbol+previousTime+bunchLen;
1055 else previousTime=symbol;
1059 bufferFile.FillBuffer(symbol);
1060 NextTable(symbol,nextTableType,bunchLen,count);
1061 if(nextTableType==0){
1062 bufferFile.FillBuffer(time);
1063 bufferFile.FillBuffer(bunchLen+2);
1067 bufferFile.WriteTrailer(numWords,padNumber,rowNumber,secNumber);
1070 cout<<"Number of decoded words:"<<wordsRead<<endl;
1073 //The trees are deleted
1074 for(Int_t j=0;j<NumTables;j++){
1075 DeleteHuffmanTree(rootNode[j]);
1081 ///////////////////////////////////////////////////////////////////////////////////////////
1083 void AliTPCCompression::ReadAltroFormat(char* fileOut,char* fileIn)const{
1084 //This method creates a text file containing the same information stored in
1085 //an Altro file. The information in the text file is organized pad by pad and
1086 //and for each pad it consists in a sequence of bunches (Bunch length +2,
1087 //Time bin of the last amplitude sample in the bunch, amplitude values)
1088 //It is used mainly for debugging
1089 ofstream ftxt(fileOut);
1090 AliTPCBuffer160 buff(fileIn,0);
1091 Int_t numWords,padNum,rowNum,secNum=0;
1093 while(buff.ReadTrailerBackward(numWords,padNum,rowNum,secNum) !=-1 ){
1094 ftxt<<"W:"<<numWords<<" P:"<<padNum<<" R:"<<rowNum<<" S:"<<secNum<<endl;
1096 for(Int_t j=0;j<(4-numWords%4);j++){
1097 value=buff.GetNextBackWord();
1100 for(Int_t i=0;i<numWords;i++){
1101 value=buff.GetNextBackWord();
1109 //////////////////////////////////////////////////////////////////////////////////////////