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 **************************************************************************/
19 // This class contains the implementation of the
20 // compression and decompression algorithms
21 // Compression is performed reading the Altro data block (called packet) backward.
22 // Similarly decompression is also done backward so that the final file is restored
23 // after the compression and decompression phase.
26 #include <TObjArray.h>
27 #include <Riostream.h>
29 #include "AliTPCCompression.h"
30 #include "AliTPCBuffer160.h"
31 #include "AliTPCHuffman.h"
33 ClassImp(AliTPCCompression)
34 //////////////////////////////////////////////////////////////////////////////////////////////////
35 AliTPCCompression::AliTPCCompression(){
37 fDimBuffer=sizeof(ULong_t)*8;
38 fFreeBitsBuffer=fDimBuffer;
46 //////////////////////////////////////////////////////////////////////////////////////////////////
47 AliTPCCompression::AliTPCCompression(const AliTPCCompression &source){
49 this->fDimBuffer=source.fDimBuffer;
50 this->fFreeBitsBuffer=source.fFreeBitsBuffer;
51 this->fReadBits=source.fReadBits;
52 this->fPos=source.fPos;
53 this->fBuffer=source.fBuffer;
54 this->fVerbose=source.fVerbose;
55 this->fFillWords=source.fFillWords;
58 //////////////////////////////////////////////////////////////////////////////////////////////////
59 AliTPCCompression& AliTPCCompression::operator=(const AliTPCCompression &source){
60 //Redefinition of the assignment operator
61 this->fDimBuffer=source.fDimBuffer;
62 this->fFreeBitsBuffer=source.fFreeBitsBuffer;
63 this->fReadBits=source.fReadBits;
64 this->fPos=source.fPos;
65 this->fBuffer=source.fBuffer;
66 this->fVerbose=source.fVerbose;
67 this->fFillWords=source.fFillWords;
70 //////////////////////////////////////////////////////////////////////////////////////////////////
71 void AliTPCCompression::NextTable(Int_t Val,Int_t &NextTableType,Int_t &BunchLen,Int_t &Count)const{
72 //Depending on the data type (5 types of data) a specific table is called
75 0==> Bunch length value
81 switch (NextTableType){
88 if (BunchLen==1)NextTableType=2;
101 if (Count==(BunchLen-1)){
121 /////////////////////////////////////////////////////////////////////////////////////////////////////
123 /////////////////////////////////////////////////////////////////////////////////////////////////////
125 Int_t AliTPCCompression::FillTables(const char* fSource,AliTPCHTable* table[],const Int_t NumTables){
126 //This method is used to compute the frequencies of the symbols in the source file
127 AliTPCBuffer160 buff(fSource,0);
128 ULong_t countWords=0;
129 ULong_t countTrailer=0;
130 Int_t numWords,padNum,rowNum,secNum=0;
132 ULong_t stat[5]={0,0,0,0,0};
135 while(buff.ReadTrailerBackward(numWords,padNum,rowNum,secNum) !=-1 ){
137 endFill=buff.GetFillWordsNum();
142 fFillWords+=4-numWords%4;
143 for(Int_t j=0;j<(4-numWords%4);j++){
144 value=buff.GetNextBackWord();
151 for(Int_t i=0;i<345;i++)timePos[i]=0;
152 for(Int_t i=0;i<1024;i++)packet[i]=0;
154 Int_t nextTableType=0;
157 for(Int_t i=0;i<numWords;i++){
158 value=buff.GetNextBackWord();
160 if(nextTableType==1){
164 NextTable(value,nextTableType,bunchLen,count);
166 //computing the Time gap between two bunches
169 Int_t previousTime=packet[timePos[tp]];
170 for(Int_t i=tp-1;i>=0;i--){
171 Int_t timPos=timePos[i];
172 Int_t bunchLen=packet[timPos-1]-2;
174 packet[timPos]=packet[timPos]-previousTime-bunchLen;
180 for(Int_t i=0;i<numWords;i++){
182 table[nextTableType]->SetFrequency(value);
183 stat[nextTableType]++;
184 NextTable(value,nextTableType,bunchLen,count);
188 cout<<"Number of words: "<<countWords<<endl;
189 cout<<"Number of trailers: "<<countTrailer<<endl;
190 cout<<"Number of fill words "<<fFillWords+endFill<<endl;
191 cout<<"Total number of words: "<<countWords+countTrailer*4+fFillWords<<endl;
193 fStat.open("Statistics");
194 fStat<<"Number of words:..........................................."<<countWords<<endl;
195 fStat<<"Number of trailers (4 10 bits words in each one)..........."<<countTrailer<<endl;
196 fStat<<"Number of fill words:......................................"<<fFillWords+endFill<<endl;
197 fStat<<"Total number of words:....................................."<<countWords+countTrailer*4+fFillWords+endFill<<endl;
198 fStat<<"-----------------------------------------"<<endl;
199 fStat<<"Number of Bunches............."<<stat[0]<<endl;
200 fStat<<"Number of Time bin............"<<stat[1]<<endl;
201 fStat<<"Number of One Samples Bunch..."<<stat[2]<<endl;
202 fStat<<"Number of Central Samples....."<<stat[3]<<endl;
203 fStat<<"Number of Border Samples......"<<stat[4]<<endl;
204 fStat<<"-----------------------------------------"<<endl;
205 ULong_t fileDimension=(ULong_t)TMath::Ceil(double((countTrailer*4+countWords+fFillWords+endFill)*10/8));
206 fStat<<"Total file Size in bytes.."<<fileDimension<<endl;
207 Double_t percentage=TMath::Ceil((fFillWords+endFill)*125)/fileDimension;
208 fStat<<"Fill Words................"<<(ULong_t)TMath::Ceil((fFillWords+endFill)*10/8)<<" bytes "<<percentage<<"%"<<endl;
209 percentage=(Double_t)countTrailer*500/fileDimension;
210 fStat<<"Trailer..................."<<countTrailer*5<<" bytes "<<percentage<<"%"<<endl;
212 percentage=(Double_t)((stat[0]+stat[1]+stat[2]+stat[3]+stat[4])) *125/fileDimension;
213 fStat<<"Data......................"<<(ULong_t)TMath::Ceil((stat[0]+stat[1]+stat[2]+stat[3]+stat[4])*10/8)<<" bytes "<<percentage<<"%"<<endl;
215 percentage=(Double_t)(stat[0]*125)/fileDimension;
216 fStat<<"Bunch....................."<<(ULong_t)TMath::Ceil(stat[0]*10/8)<<" bytes "<<percentage<<"%"<<endl; //
217 percentage=(Double_t)(stat[1]*125)/fileDimension;
218 fStat<<"Time......................"<<(ULong_t)TMath::Ceil(stat[1]*10/8)<<" bytes "<<percentage<<"%"<<endl; //
221 percentage=(Double_t)((stat[2]+stat[3]+stat[4])) *125/fileDimension;
222 fStat<<"Amplitude values.........."<<(ULong_t)TMath::Ceil((stat[2]+stat[3]+stat[4])*10/8)<<" bytes "<<percentage<<"%"<<endl;
223 percentage=(Double_t)(stat[2]*125)/fileDimension;
224 fStat<<" One Samples..............."<<(ULong_t)TMath::Ceil(stat[2]*10/8)<<" bytes "<<percentage<<"%"<<endl; //
225 percentage=(Double_t)(stat[3]*125)/fileDimension;
226 fStat<<" Central Samples..........."<<(ULong_t)TMath::Ceil(stat[3]*10/8)<<" bytes "<<percentage<<"%"<<endl; //
227 percentage=(Double_t)(stat[4]*125)/fileDimension;
228 fStat<<" Border Samples............"<<(ULong_t)TMath::Ceil(stat[4]*10/8)<<" bytes "<<percentage<<"%"<<endl; //
232 ////////////////////////////////////////////////////////////////////////////////////////
233 Int_t AliTPCCompression::StoreTables(AliTPCHTable* table[],const Int_t NumTable){
234 //This method stores the tables in a sequence of binary file
237 for(Int_t k=0;k<NumTable;k++){
238 sprintf(filename,"Table%d.dat",k);
240 fTable.open(filename,ios::binary);
242 fTable.open(filename);
244 Int_t dim=table[k]->Size();
245 //Table dimension is written into a file
246 fTable.write((char*)(&dim),sizeof(Int_t));
247 //One table is written into a file
248 for(Int_t i=0;i<dim;i++){
249 UChar_t codeLen=table[k]->CodeLen()[i];
250 // ULong_t code=(ULong_t)table[k]->Code()[i];
251 Double_t code=table[k]->Code()[i];
252 fTable.write((char*)(&codeLen),sizeof(UChar_t));
253 //fTable.write((char*)(&code),sizeof(ULong_t));
254 fTable.write((char*)(&code),sizeof(Double_t));
260 ////////////////////////////////////////////////////////////////////////////////////////
261 Int_t AliTPCCompression::CreateTableFormula(Double_t beta,ULong_t M,Int_t dim,Int_t Type){
262 // Type = 0 for Bunch length
263 // Type = 1 for Time Gap
269 AliTPCHTable *Table=new AliTPCHTable(dim);
272 Double_t FreqArray[1024];
273 for(Int_t i=0;i<1024;i++){
276 alpha=M*0.000000602+0.0104;
278 cout<<"alpha "<<alpha<<endl;
279 for(Int_t x=0;x<dim;x++){
281 FreqArray[x]=TMath::Power((x+1),-beta)*TMath::Exp(-alpha*(x+1));
283 FreqArray[x]=TMath::Power((x+1),-beta);
285 if (FreqArray[x]<min)min=FreqArray[x];
288 cout<<"Minimun Value "<<min<<endl;
291 cout<<"A Value: "<<A<<endl;
292 for(Int_t x=0;x<dim;x++){
293 if (Type==0)//Bunch length
294 if (x>=3)//minimum bunch length
295 Table->SetValFrequency(x,A*FreqArray[x]*1000);
297 Table->SetValFrequency(x,0);
299 Table->SetValFrequency(x,A*FreqArray[x]);
301 Table->BuildHTable();
304 sprintf(filename,"Table%d.dat",Type);
306 fTable.open(filename,ios::binary);
308 fTable.open(filename);
310 Int_t dimTable=Table->Size();
311 //Table dimension is written into a file
312 fTable.write((char*)(&dimTable),sizeof(Int_t));
313 //One table is written into a file
314 for(Int_t i=0;i<dimTable;i++){
315 UChar_t CodeLen=Table->CodeLen()[i];
316 Double_t Code=Table->Code()[i];
317 fTable.write((char*)(&CodeLen),sizeof(UChar_t));
318 fTable.write((char*)(&Code),sizeof(Double_t));
324 ////////////////////////////////////////////////////////////////////////////////////////
325 Int_t AliTPCCompression::CreateTables(const char* fSource,const Int_t NumTables){
329 0==> Bunch length values
335 Int_t n=10;// 10 bits per symbol
336 AliTPCHTable ** table = new AliTPCHTable*[NumTables];
337 //The table is inizialized with the rigth number of rows
338 for(Int_t i=0;i<NumTables;i++){
339 table[i]=new AliTPCHTable((Int_t)(TMath::Power(2,n)));
340 table[i]->SetVerbose(fVerbose);
342 //The frequencies are calculated and the tables are filled
344 cout<<"Filling tables...\n";
345 //The get the frequencies
346 FillTables(fSource,table,NumTables);
348 //This part will be used in the table optimization phase
350 for(Int_t i=0;i<NumTables;i++){
351 table[i]->CompleteTable(i);
355 cout<<"Entropy of Bunch length table........."<<table[0]->GetEntropy()<<endl;
356 cout<<"Entropy of Time bin table............."<<table[1]->GetEntropy()<<endl;
357 cout<<"Entropy of one Sample bunch table....."<<table[2]->GetEntropy()<<endl;
358 cout<<"Entropy of Central Sample table......."<<table[3]->GetEntropy()<<endl;
359 cout<<"Entropy Border Samples table.........."<<table[4]->GetEntropy()<<endl;
361 fStat.open("Statistics",ios::app);
363 fStat<<"----------------- ENTROPY for castomized tables --------------------------"<<endl;
364 fStat<<"Entropy of Bunch length table......."<<table[0]->GetEntropy()<<endl;
365 fStat<<"Entropy of Time bin table..........."<<table[1]->GetEntropy()<<endl;
366 fStat<<"Entropy of one Sample bunch table..."<<table[2]->GetEntropy()<<endl;
367 fStat<<"Entropy of Central Sample table....."<<table[3]->GetEntropy()<<endl;
368 fStat<<"Entropy Border Samples table........"<<table[4]->GetEntropy()<<endl;
372 cout<<"Tables filled \n";
374 //Frequencies normalization
375 table[0]->NormalizeFrequencies();
376 table[1]->NormalizeFrequencies();
377 table[2]->NormalizeFrequencies();
378 table[3]->NormalizeFrequencies();
379 table[4]->NormalizeFrequencies();
381 //Tables are saved in a sequence of text file and using the macro Histo.C is it possible to get
382 //a series of histograms rappresenting the frequency distribution
383 table[0]->StoreFrequencies("BunchLenFreq.txt");
384 table[1]->StoreFrequencies("TimeFreq.txt");
385 table[2]->StoreFrequencies("Sample1Freq.txt");
386 table[3]->StoreFrequencies("SCentralFreq.txt");
387 table[4]->StoreFrequencies("SBorderFreq.txt");
389 cout<<"Creating Tables..\n";
390 //One Huffman tree is created for each table starting from the frequencies of the symbols
391 for(Int_t i=0;i<NumTables;i++){
392 table[i]->BuildHTable();
394 cout<<"Number of elements inside the table:"<<table[i]->GetWordsNumber();
397 cout<<" (Bunch Length)"<<endl;
401 cout<<" (Time Bin)"<<endl;
405 cout<<" (1 Samples Bunch)"<<endl;
409 cout<<" (Central Samples)"<<endl;
413 cout<<" (Border Samples)"<<endl;
417 table[i]->PrintTable();
420 //The tables are saved ad binary files
421 StoreTables(table,NumTables);
422 //The tables stored in memory are deleted;
423 for(Int_t i=0;i<NumTables;i++)delete table[i];
427 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
428 Int_t AliTPCCompression::RetrieveTables(AliTPCHTable* table[],Int_t NumTable){
429 //This method retrieve the Huffman tables from a sequence of binary files
431 cout<<"Retrieving tables from files \n";
437 //The following for loop is used to generate the Huffman trees acording to the tables
438 for(Int_t k=0;k<NumTable;k++){
439 Int_t dim;//this variable contains the table dimension
440 sprintf(filename,"Table%d.dat",k);
442 fTable.open(filename,ios::binary);
444 fTable.open(filename);
446 if(!fTable){cout<<"File doesn't exist:"<<filename<<endl; exit(1);}
447 fTable.read((char*)(&dim),sizeof(Int_t));
449 cout<<"Table dimension: "<<dim<<endl;
450 table[k]=new AliTPCHTable(dim);
451 for(Int_t i=0;i<dim;i++){
452 fTable.read((char*)(&codeLen),sizeof(UChar_t));
453 table[k]->SetCodeLen(codeLen,i);
454 // fTable.read((char*)(&code),sizeof(ULong_t));
455 fTable.read((char*)(&code),sizeof(Double_t));
456 table[k]->SetCode(Mirror((ULong_t)code,codeLen),i);
461 cout<<"Trees generated \n";
462 //At this point the trees are been built
466 Int_t AliTPCCompression::CreateTablesFromTxtFiles(Int_t NumTable){
467 //This method creates a set of binary tables, needed by the Huffman
468 //algorith, starting from a set of frequencies tables stored in form of
471 cout<<"Retrieving frequencies from txt files \n";
474 //Tables are read from the files (Each codeword has been "Mirrored")
475 AliTPCHTable **table = new AliTPCHTable*[NumTable];
476 for(Int_t k=0;k<NumTable;k++){
477 sprintf(filename,"Table%d.txt",k);
478 cout<<filename<<endl;
479 fTable.open(filename);
480 if(!fTable){cout<<"File doesn't exist: "<<filename<<endl; exit(1);}
483 table[k]=new AliTPCHTable(1024);
484 while(!fTable.eof()){
488 cout<<"Frequency cannot be negative !!!\n";
491 table[k]->SetValFrequency(symbol,freq);
498 fStat.open("Statistics",ios::app);
500 fStat<<"----------------- ENTROPY for external txt tables --------------------------"<<endl;
501 fStat<<"Entropy of Bunch length table......."<<table[0]->GetEntropy()<<endl;
502 fStat<<"Entropy of Time bin table..........."<<table[1]->GetEntropy()<<endl;
503 fStat<<"Entropy of one Sample bunch table..."<<table[2]->GetEntropy()<<endl;
504 fStat<<"Entropy of Central Sample table....."<<table[3]->GetEntropy()<<endl;
505 fStat<<"Entropy Border Samples table........"<<table[4]->GetEntropy()<<endl;
507 for(Int_t k=0;k<NumTable;k++){
508 table[k]->BuildHTable();
510 //The tables are saved ad binary files
511 StoreTables(table,NumTable);
512 //The tables stored in memory are deleted;
513 for(Int_t i=0;i<NumTable;i++)delete table[i];
518 ////////////////////////////////////////////////////////////////////////////////////////
520 ////////////////////////////////////////////////////////////////////////////////////////
522 void AliTPCCompression::StoreValue(ULong_t val,UChar_t len){
523 //This method stores the value "val" of "len" bits into the internal buffer "fBuffer"
524 if (len<=fFreeBitsBuffer){ // val is not splitted in two buffer
525 fFreeBitsBuffer-=len;
526 fBuffer=fBuffer<<len;
528 if(!fFreeBitsBuffer){ // if the buffer is full it is written into a file
529 f.write((char*)(&fBuffer),sizeof(ULong_t));
530 fFreeBitsBuffer=fDimBuffer;
534 else{ //val has to be splitted in two buffers
535 fBuffer=fBuffer<<fFreeBitsBuffer;
538 temp=temp>>(len-fFreeBitsBuffer);
539 fBuffer=fBuffer|temp;
540 f.write((char*)(&fBuffer),sizeof(ULong_t));
541 fFreeBitsBuffer=fDimBuffer-(len-fFreeBitsBuffer);
542 val=val<<fFreeBitsBuffer;
543 val=val>>fFreeBitsBuffer;
548 //////////////////////////////////////////////////////////////////////////////////////////////////
549 void AliTPCCompression::Flush(){
550 //The last buffer cannot be completely full so to save it
551 //into the output file it is first necessary to fill it with an hexadecimal pattern
552 if(fFreeBitsBuffer<fDimBuffer){
553 fBuffer=fBuffer<<fFreeBitsBuffer;
554 f.write((char*)(&fBuffer),sizeof(ULong_t));
558 //////////////////////////////////////////////////////////////////////////////////////////////////
559 ULong_t AliTPCCompression::Mirror(ULong_t val,UChar_t len)const{
560 //This method inverts the digits of the number "val" and length "len"
561 //indicates the number of digits of the number considered in binary notation
565 for(Int_t i=0;i<len;i++){
568 specular=specular<<1;
569 specular=specular|bit;
574 //////////////////////////////////////////////////////////////////////////////////////////////////
575 Int_t AliTPCCompression::CompressData(AliTPCHTable* table[],Int_t NumTable,const char* fSource,const char* fDest){
576 //This method is used to compress the data stored in the Altro format file using specific tables
577 //calculated considering the frequencies of the symbol of the file that has to be compressed
578 cout<<" COMPRESSION "<<endl;
579 cout<<"compression of the file "<<fSource<<" Output File: "<<fDest<<endl;
580 //the output file is open
582 f.open(fDest,ios::binary|ios::out);
584 f.open(fDest,ios::out);
586 //Tables are written into the output file
587 for(Int_t k=0;k<NumTable;k++){
588 Int_t dim=table[k]->Size();
589 //Table dimension is written into a file
590 f.write((char*)(&dim),sizeof(Int_t));
591 //One table is written into a file
592 for(Int_t i=0;i<dim;i++){
593 UChar_t codeLen=table[k]->CodeLen()[i];
594 ULong_t code=(ULong_t)table[k]->Code()[i];
595 f.write((char*)(&codeLen),sizeof(UChar_t));
596 f.write((char*)(&code),sizeof(ULong_t));
600 // Source file is open
601 AliTPCBuffer160 buff(fSource,0);
602 //coded words are written into the output file
603 Int_t numWords,padNum,rowNum,secNum=0;
604 ULong_t storedWords=0;
606 ULong_t numPackets=0;
607 while(buff.ReadTrailerBackward(numWords,padNum,rowNum,secNum) !=-1 ){
610 for(Int_t j=0;j<(4-numWords%4);j++){
611 value=buff.GetNextBackWord();
618 for(Int_t i=0;i<345;i++)timePos[i]=0;
619 for(Int_t i=0;i<1024;i++)packet[i]=0;
621 Int_t nextTableType=0;
624 for(Int_t i=0;i<numWords;i++){
625 value=buff.GetNextBackWord();
627 if(nextTableType==1){
631 NextTable(value,nextTableType,bunchLen,count);
633 //computing the Time gap between two bunches
636 Int_t previousTime=packet[timePos[tp]];
637 for(Int_t i=tp-1;i>=0;i--){
638 Int_t timPos=timePos[i];
639 Int_t bunchLen=packet[timPos-1]-2;
641 packet[timPos]=packet[timPos]-previousTime-bunchLen;
648 //All the words for one pad are compressed and stored in the compress file
649 for(Int_t i=0;i<numWords;i++){
651 if(nextTableType==1)timeBin=value;
653 // ULong_t val=(ULong_t)table[nextTableType]->Code()[value]; // val is the code
654 Double_t val=table[nextTableType]->Code()[value]; // val is the code
655 UChar_t len=table[nextTableType]->CodeLen()[value]; // len is the length (number of bits)of val
656 StoreValue(Mirror((ULong_t)val,len),len);
659 NextTable(value,nextTableType,bunchLen,count);
660 if(nextTableType==0){
661 // ULong_t val=(ULong_t)table[1]->Code()[timeBin]; // val is the code
662 Double_t val=table[1]->Code()[timeBin]; // val is the code
663 UChar_t len=table[1]->CodeLen()[timeBin]; // len is the length (number of bits)of val
664 StoreValue(Mirror((ULong_t)val,len),len);
665 //val=(ULong_t)table[nextTableType]->Code()[(bunchLen+2)]; // val is the code
666 val=table[nextTableType]->Code()[(bunchLen+2)]; // val is the code
667 len=table[nextTableType]->CodeLen()[(bunchLen+2)]; // len is the length (number of bits)of val
668 StoreValue(Mirror((ULong_t)val,len),len);
673 StoreValue(numWords,10);
674 StoreValue(padNum,10);
675 StoreValue(rowNum,10);
676 StoreValue(secNum,9);
680 StoreValue(numPackets,32);
681 cout<<"Number of strored packet: "<<numPackets<<endl;
683 //The last buffen cannot be completely full
685 cout<<"Number of stored words: "<<storedWords<<endl;
690 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
691 Int_t AliTPCCompression::CompressDataOptTables(Int_t NumTable,const char* fSource,const char* fDest){
692 //This method compress an Altro format file using a general set of tables stored as binary files to be provided
694 cout<<" BackWord COMPRESSION "<<endl;
695 cout<<"compression of the file "<<fSource<<" Output File: "<<fDest<<endl;
697 //Tables are read from the files (Each codeword has been "Mirrored")
698 AliTPCHTable **table = new AliTPCHTable*[NumTable];
699 RetrieveTables(table,NumTable);
700 //the output file is open
702 f.open(fDest,ios::binary|ios::out);
704 f.open(fDest,ios::out);
706 // Source file is open
707 AliTPCBuffer160 buff(fSource,0);
708 //coded words are written into a file
709 Int_t numWords,padNum,rowNum,secNum=0;
710 ULong_t storedWords=0;
712 ULong_t numPackets=0;
713 Double_t stat[5]={0.,0.,0.,0.,0.};
714 ULong_t trailerNumbers=0;
715 Double_t numElem[5]={0,0,0,0,0};
716 Double_t fillWords=0.;
717 fStat.open("Statistics",ios::app);
719 fStat<<"-------------------COMPRESSION STATISTICS----------"<<endl;
721 while(buff.ReadTrailerBackward(numWords,padNum,rowNum,secNum) !=-1 ){
723 fillWords=buff.GetFillWordsNum();
729 fillWords+=4-numWords%4;
730 for(Int_t j=0;j<(4-numWords%4);j++){
731 value=buff.GetNextBackWord();
738 for(Int_t i=0;i<345;i++)timePos[i]=0;
739 for(Int_t i=0;i<1024;i++)packet[i]=0;
741 Int_t nextTableType=0;
744 for(Int_t i=0;i<numWords;i++){
745 value=buff.GetNextBackWord();
747 if(nextTableType==1){
751 NextTable(value,nextTableType,bunchLen,count);
753 //computing the Time gap between two bunches
756 Int_t previousTime=packet[timePos[tp]];
757 for(Int_t i=tp-1;i>=0;i--){
758 Int_t timPos=timePos[i];
759 Int_t bunchLen=packet[timPos-1]-2;
761 packet[timPos]=packet[timPos]-previousTime-bunchLen;
769 for(Int_t i=0;i<numWords;i++){
771 if(nextTableType==1)timeBin=value;
773 //ULong_t val=(ULong_t)table[nextTableType]->Code()[value]; // val is the code
774 Double_t val=table[nextTableType]->Code()[value]; // val is the code
775 UChar_t len=table[nextTableType]->CodeLen()[value]; // len is the length (number of bits)of val
776 stat[nextTableType]+=len;
777 numElem[nextTableType]++;
778 StoreValue((ULong_t)val,len);
781 NextTable(value,nextTableType,bunchLen,count);
782 if(nextTableType==0){
783 // ULong_t val=(ULong_t)table[1]->Code()[timeBin]; // val is the code
784 Double_t val=table[1]->Code()[timeBin]; // val is the code
785 UChar_t len=table[1]->CodeLen()[timeBin]; // len is the length (number of bits)of val
788 StoreValue((ULong_t)val,len);
789 // val=(ULong_t)table[nextTableType]->Code()[(bunchLen+2)]; // val is the code
790 val=table[nextTableType]->Code()[(bunchLen+2)]; // val is the code
791 len=table[nextTableType]->CodeLen()[(bunchLen+2)]; // len is the length (number of bits)of val
792 StoreValue((ULong_t)val,len);
793 stat[nextTableType]+=len;
794 numElem[nextTableType]++;
799 StoreValue(numWords,10);
800 StoreValue(padNum,10);
801 StoreValue(rowNum,10);
802 StoreValue(secNum,9);
807 StoreValue(numPackets,32);
809 cout<<"Number of strored packets: "<<numPackets<<endl;
811 //The last buffen cannot be completely full
814 cout<<"Number of stored words: "<<storedWords<<endl;
817 for(Int_t i=0;i<NumTable;i++){
821 Double_t dimension=(ULong_t)TMath::Ceil((stat[0]+stat[1]+stat[2]+stat[3]+stat[4])/8)+trailerNumbers*5;
822 fStat<<"Trailer Dimension in bytes......"<<trailerNumbers*5<<endl;
823 fStat<<"Data Dimension in bytes........."<<(ULong_t)TMath::Ceil((stat[0]+stat[1]+stat[2]+stat[3]+stat[4])/8)<<endl;
824 fStat<<"Compressed file dimension......."<<(ULong_t)dimension<<endl;
826 fStat<<(ULong_t)trailerNumbers<<endl;
827 fStat<<(ULong_t)fillWords<<endl;
828 fStat<<(ULong_t)numElem[0]<<endl;
829 fStat<<(ULong_t)numElem[1]<<endl;
830 fStat<<(ULong_t)numElem[2]<<endl;
831 fStat<<(ULong_t)numElem[3]<<endl;
832 fStat<<(ULong_t)numElem[4]<<endl;
834 fillWords=(fillWords+numElem[0]+numElem[1]+numElem[2]+numElem[3]+numElem[4]+trailerNumbers*4)*10/8;
835 fStat<<"Original file dimension........."<<(ULong_t)fillWords<<endl;
837 Double_t ratio=(dimension/fillWords)*100;
838 fStat<<"Compression ratio (Compressed/Uncompressed)..."<<ratio<<"%"<<endl;
841 fStat<<"Bunch length size in bytes......"<<(ULong_t)TMath::Ceil(stat[0]/8)<<" Comppression.."<<(stat[0]/numElem[0])*10<<"%"<<endl;
843 fStat<<"Time gap size in bytes.........."<<(ULong_t)TMath::Ceil(stat[1]/8)<<" Comppression.."<<(stat[1]/numElem[1])*10<<"%"<<endl;
844 if (numElem[2]+numElem[3]+numElem[4])
845 fStat<<"Amplitude values in bytes......."<<(ULong_t)TMath::Ceil((stat[2]+stat[3]+stat[4])/8)<<" Comppression.."<<
846 ((stat[2]+stat[3]+stat[4])/(numElem[2]+numElem[3]+numElem[4]))*10<<"%"<<endl;
848 fStat<<" One Samples in bytes............"<<(ULong_t)TMath::Ceil(stat[2]/8)<<" Comppression.."<<(stat[2]/numElem[2])*10<<"%"<<endl;
850 fStat<<" Central Samples size in bytes..."<<(ULong_t)TMath::Ceil(stat[3]/8)<<" Comppression.."<<(stat[3]/numElem[3])*10<<"%"<<endl;
852 fStat<<" Border Samples size in bytes...."<<(ULong_t)TMath::Ceil(stat[4]/8)<<" Comppression.."<<(stat[4]/numElem[4])*10<<"%"<<endl;
854 fStat<<"Average number of bits per word"<<endl;
856 fStat<<"Bunch length ......"<<stat[0]/numElem[0]<<endl;
858 fStat<<"Time gap .........."<<stat[1]/numElem[1]<<endl;
860 fStat<<"One Samples........"<<stat[2]/numElem[2]<<endl;
862 fStat<<"Central Samples ..."<<stat[3]/numElem[3]<<endl;
864 fStat<<"Border Samples....."<<stat[4]/numElem[4]<<endl;
869 ////////////////////////////////////////////////////////////////////////////////////////
871 ////////////////////////////////////////////////////////////////////////////////////////
873 ////////////////////////////////////////////////////////////////////////////////////////
874 void AliTPCCompression::CreateTrees(AliTPCHNode *RootNode[],const Int_t NumTables){
875 //The first part of the compressed file cotains the tables
876 //The following for loop is used to generate the Huffman trees acording to the tables
878 cout<<"Creating the Huffman trees \n";
883 //loop over the numbero of tables
884 for(Int_t k=0;k<NumTables;k++){
885 RootNode[k]=new AliTPCHNode(); //RootNode is the root of the tree
886 Int_t dim;//this variable contains the table dimension
887 f.read((char*)(&dim),sizeof(Int_t));
889 cout<<"Table dimension: "<<dim<<endl;
890 //loop over the words of a table
891 for(Int_t i=0;i<dim;i++){
892 f.read((char*)(&codeLen),sizeof(UChar_t));
893 //f.read((char*)(&code),sizeof(ULong_t));
894 f.read((char*)(&code),sizeof(Double_t));
896 for(Int_t j=1;j<=codeLen;j++){
898 val=(ULong_t)TMath::Power(2,codeLen-j);
899 bit=(ULong_t)code&val;
900 AliTPCHNode *temp=node;
902 node=node->GetRight();
904 node=new AliTPCHNode();
905 temp->SetRight(node);
909 node=node->GetLeft();
911 node=new AliTPCHNode();
918 node->SetFrequency(codeLen);
920 //cout<<node->GetSymbol()<<" "<<(Int_t)node->GetFrequency()<<endl;
924 cout<<"Trees generated \n";
925 //At this point the trees are been built
927 //////////////////////////////////////////////////////////////////////////////////////////////////
928 void AliTPCCompression::CreateTreesFromFile(AliTPCHNode *RootNode[],const Int_t NumTables){
929 //For each table this method builds the associate Huffman tree starting from the codeword and
930 //the codelength of each symbol
932 cout<<"Creating the Huffman trees \n";
939 //The following for loop is used to generate the Huffman trees acording to the tables
940 //loop over the tables
941 for(Int_t k=0;k<NumTables;k++){
942 RootNode[k]=new AliTPCHNode(); //RootNode is the root of the tree
943 Int_t dim=0;//this variable contains the table dimension
944 sprintf(filename,"Table%d.dat",k);
946 fTable.open(filename,ios::binary);
948 fTable.open(filename);
950 fTable.read((char*)(&dim),sizeof(Int_t));
952 cout<<"Table dimension: "<<dim<<endl;
953 //loop over the words of one table
954 for(Int_t i=0;i<dim;i++){
955 fTable.read((char*)(&codeLen),sizeof(UChar_t));
956 //fTable.read((char*)(&code),sizeof(ULong_t));
957 fTable.read((char*)(&code),sizeof(Double_t));
959 for(Int_t j=1;j<=codeLen;j++){
961 val=(ULong_t)TMath::Power(2,codeLen-j);
962 bit=(ULong_t)code&val;
963 AliTPCHNode *temp=node;
965 node=node->GetRight();
967 node=new AliTPCHNode();
968 temp->SetRight(node);
972 node=node->GetLeft();
974 node=new AliTPCHNode();
981 node->SetFrequency(codeLen);
987 cout<<"Trees generated \n";
988 //At this point the trees are been built
990 //////////////////////////////////////////////////////////////////////////////////////////////////
991 void AliTPCCompression::DeleteHuffmanTree(AliTPCHNode* node){
992 //This function deletes all the nodes of an Huffman tree
993 //In an Huffman tree any internal node has always two children
995 DeleteHuffmanTree(node->GetLeft());
996 DeleteHuffmanTree(node->GetRight());
997 // cout<<node->GetSymbol()<<" "<<(Int_t)node->GetFrequency()<<endl;
1001 //////////////////////////////////////////////////////////////////////////////////////////////////
1002 void AliTPCCompression::VisitHuffmanTree(AliTPCHNode* node){
1003 //This function realizes an in order visit of a binary tree
1005 cout<<node->GetSymbol()<<" "<<node->GetFrequency()<<endl;
1006 VisitHuffmanTree(node->GetLeft());
1007 VisitHuffmanTree(node->GetRight());
1010 //////////////////////////////////////////////////////////////////////////////////////////////////
1011 ULong_t AliTPCCompression::ReadWord(Int_t NumberOfBit){
1012 //This method retrieves a word of a specific number of bits from the file through the internal buffer
1015 for (Int_t i=0;i<NumberOfBit;i++){
1017 fPos-=sizeof(ULong_t);
1019 f.read((char*)(&fBuffer),sizeof(ULong_t));
1023 mask=(ULong_t)TMath::Power(2,fReadBits);
1032 //////////////////////////////////////////////////////////////////////////////////////////////////
1033 void AliTPCCompression::ReadTrailer(Int_t &WordsNumber,Int_t &PadNumber,Int_t &RowNumber,Int_t &SecNumber){
1034 //It retrieves a trailer
1036 SecNumber=ReadWord(9);
1037 RowNumber=ReadWord(10);
1038 PadNumber=ReadWord(10);
1039 WordsNumber=ReadWord(10);
1042 //////////////////////////////////////////////////////////////////////////////////////////////////
1043 ULong_t AliTPCCompression::GetDecodedWord(AliTPCHNode* root){
1044 //This method retrieves a decoded word.
1045 AliTPCHNode *node=root;
1049 ULong_t bit=ReadWord(1);
1051 node=node->GetRight();
1053 node=node->GetLeft();
1054 if (!(node->GetLeft())){
1055 symbol=node->GetSymbol();
1061 //////////////////////////////////////////////////////////////////////////////////////////////////
1062 Int_t AliTPCCompression::DecompressData(Int_t NumTables,const char* fname,char* fDest){
1063 //Decompression method
1064 cout<<" DECOMPRESSION:"<<endl;
1065 cout<<"Source File "<<fname<<" Destination File "<<fDest<<endl;
1067 f.open(fname,ios::binary|ios::in);
1069 f.open(fname,ios::in);
1071 if(!f){cout<<"File doesn't exist:"<<fname<<endl;;return -1;}
1072 AliTPCHNode ** rootNode = new AliTPCHNode*[NumTables];
1073 //Creation of the Huffman trees
1074 CreateTrees(rootNode,NumTables);
1075 //to go to the end of the file
1076 f.seekg(0,ios::end);
1077 //to get the file dimension in byte
1079 fPos-=sizeof(ULong_t);
1083 f.read((char*)(&fBuffer),sizeof(ULong_t));
1091 ULong_t packetNumber=ReadWord(sizeof(ULong_t)*8);
1092 cout<<"Number of Packect: "<<packetNumber<<endl;
1093 AliTPCBuffer160 bufferFile(fDest,1);
1095 ULong_t wordsRead=0; //number of read coded words
1096 while(k<packetNumber){
1097 Int_t numWords,padNumber,rowNumber,secNumber=0;
1098 ReadTrailer(numWords,padNumber,rowNumber,secNumber);
1101 Int_t previousTime=-1;
1103 Int_t nextTableType=0;
1106 for(Int_t i=0;i<numWords;i++){
1107 ULong_t symbol=GetDecodedWord(rootNode[nextTableType]);
1109 //Time reconstruction
1110 if (nextTableType==1){
1111 if (previousTime!=-1){
1112 previousTime=symbol+previousTime+bunchLen;
1114 else previousTime=symbol;
1118 bufferFile.FillBuffer(symbol);
1119 NextTable(symbol,nextTableType,bunchLen,count);
1120 if(nextTableType==0){
1121 bufferFile.FillBuffer(time);
1122 bufferFile.FillBuffer(bunchLen+2);
1126 bufferFile.WriteTrailer(numWords,padNumber,rowNumber,secNumber);
1128 cout<<"Number of decoded words:"<<wordsRead<<endl;
1130 //The trees are deleted
1131 for(Int_t j=0;j<NumTables;j++){
1132 DeleteHuffmanTree(rootNode[j]);
1138 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1140 Int_t AliTPCCompression::DecompressDataOptTables(Int_t NumTables,const char* fname,char* fDest){
1141 //This method decompress a file using separate Huffman tables
1143 cout<<" DECOMPRESSION:"<<endl;
1144 cout<<"Source File "<<fname<<" Destination File "<<fDest<<endl;
1146 AliTPCHNode ** rootNode = new AliTPCHNode*[NumTables];
1147 //Creation of the Huffman trees
1148 CreateTreesFromFile(rootNode,NumTables);
1150 f.open(fname,ios::binary|ios::in);
1152 f.open(fname,ios::in);
1154 if(!f){cout<<"File doesn't exist:"<<fname<<endl;;return -1;}
1155 //to go to the end of the file
1156 f.seekg(0,ios::end);
1157 //to get the file dimension in byte
1159 fPos-=sizeof(ULong_t);
1163 f.read((char*)(&fBuffer),sizeof(ULong_t));
1171 ULong_t packetNumber=ReadWord(sizeof(ULong_t)*8);
1173 cout<<"Number of Packect: "<<packetNumber<<endl;
1175 AliTPCBuffer160 bufferFile(fDest,1);
1177 ULong_t wordsRead=0; //number of read coded words
1178 while(k<packetNumber){
1179 Int_t numWords,padNumber,rowNumber,secNumber=0;
1180 ReadTrailer(numWords,padNumber,rowNumber,secNumber);
1183 Int_t previousTime=-1;
1185 Int_t nextTableType=0;
1188 for(Int_t i=0;i<numWords;i++){
1189 ULong_t symbol=GetDecodedWord(rootNode[nextTableType]);
1191 //Time reconstruction
1192 if (nextTableType==1){
1193 if (previousTime!=-1){
1194 previousTime=symbol+previousTime+bunchLen;
1196 else previousTime=symbol;
1200 bufferFile.FillBuffer(symbol);
1201 NextTable(symbol,nextTableType,bunchLen,count);
1202 if(nextTableType==0){
1203 bufferFile.FillBuffer(time);
1204 bufferFile.FillBuffer(bunchLen+2);
1208 bufferFile.WriteTrailer(numWords,padNumber,rowNumber,secNumber);
1211 cout<<"Number of decoded words:"<<wordsRead<<endl;
1214 //The trees are deleted
1215 for(Int_t j=0;j<NumTables;j++){
1216 DeleteHuffmanTree(rootNode[j]);
1222 ///////////////////////////////////////////////////////////////////////////////////////////
1224 void AliTPCCompression::ReadAltroFormat(char* fileOut,char* fileIn)const{
1225 //This method creates a text file containing the same information stored in
1226 //an Altro file. The information in the text file is organized pad by pad and
1227 //and for each pad it consists in a sequence of bunches (Bunch length +2,
1228 //Time bin of the last amplitude sample in the bunch, amplitude values)
1229 //It is used mainly for debugging
1230 ofstream ftxt(fileOut);
1231 AliTPCBuffer160 buff(fileIn,0);
1232 Int_t numWords,padNum,rowNum,secNum=0;
1234 if (fVerbose) cout<<"Creating a txt file from an Altro Format file"<<endl;
1235 while(buff.ReadTrailerBackward(numWords,padNum,rowNum,secNum) !=-1 ){
1236 ftxt<<"S:"<<secNum<<" R:"<<rowNum<<" P:"<<padNum<<" W:"<<numWords<<endl;
1238 for(Int_t j=0;j<(4-numWords%4);j++){
1239 value=buff.GetNextBackWord();
1242 for(Int_t i=0;i<numWords;i++){
1243 value=buff.GetNextBackWord();
1251 //////////////////////////////////////////////////////////////////////////////////////////