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>
30 #include "AliTPCBuffer160.h"
31 #include "AliTPCHuffman.h"
32 #include "AliTPCCompression.h"
34 ClassImp(AliTPCCompression)
35 //////////////////////////////////////////////////////////////////////////////////////////////////
36 AliTPCCompression::AliTPCCompression(){
38 fDimBuffer=sizeof(UInt_t)*8;
39 fFreeBitsBuffer=fDimBuffer;
48 //////////////////////////////////////////////////////////////////////////////////////////////////
49 AliTPCCompression::AliTPCCompression(const AliTPCCompression &source)
52 this->fDimBuffer=source.fDimBuffer;
53 this->fFreeBitsBuffer=source.fFreeBitsBuffer;
54 this->fReadBits=source.fReadBits;
55 this->fPos=source.fPos;
56 this->fBuffer=source.fBuffer;
57 this->fVerbose=source.fVerbose;
58 this->fFillWords=source.fFillWords;
59 this->fPointBuffer=source.fPointBuffer;
62 //////////////////////////////////////////////////////////////////////////////////////////////////
63 AliTPCCompression& AliTPCCompression::operator=(const AliTPCCompression &source){
64 //Redefinition of the assignment operator
65 this->fDimBuffer=source.fDimBuffer;
66 this->fFreeBitsBuffer=source.fFreeBitsBuffer;
67 this->fReadBits=source.fReadBits;
68 this->fPos=source.fPos;
69 this->fBuffer=source.fBuffer;
70 this->fVerbose=source.fVerbose;
71 this->fFillWords=source.fFillWords;
72 this->fPointBuffer=source.fPointBuffer;
75 //////////////////////////////////////////////////////////////////////////////////////////////////
76 void AliTPCCompression::NextTable(Int_t Val,Int_t &NextTableType,Int_t &BunchLen,Int_t &Count)const{
77 //Depending on the data type (5 types of data) a specific table is called
80 0==> Bunch length value
86 switch (NextTableType){
93 if (BunchLen==1)NextTableType=2;
106 if (Count==(BunchLen-1)){
127 /////////////////////////////////////////////////////////////////////////////////////////////////////
129 Int_t AliTPCCompression::FillTables(const char* fSource,AliTPCHTable* table[],Int_t /*NumTables*/){
130 //This method is used to compute the frequencies of the symbols in the source file
131 AliTPCBuffer160 buff(fSource,0);
133 UInt_t countTrailer=0;
134 Int_t numWords,padNum,rowNum,secNum=0;
136 UInt_t stat[5]={0,0,0,0,0};
139 while(buff.ReadTrailerBackward(numWords,padNum,rowNum,secNum) !=-1 ){
141 endFill=buff.GetFillWordsNum();
146 fFillWords+=4-numWords%4;
147 for(Int_t j=0;j<(4-numWords%4);j++){
148 value=buff.GetNextBackWord();
155 for(Int_t i=0;i<345;i++)timePos[i]=0;
156 for(Int_t i=0;i<1024;i++)packet[i]=0;
158 Int_t nextTableType=0;
161 for(Int_t i=0;i<numWords;i++){
162 value=buff.GetNextBackWord();
164 if(nextTableType==1){
168 NextTable(value,nextTableType,bunchLen,count);
170 //computing the Time gap between two bunches
173 Int_t previousTime=packet[timePos[tp]];
174 for(Int_t i=tp-1;i>=0;i--){
175 Int_t timPos=timePos[i];
176 Int_t bunchLen=packet[timPos-1]-2;
178 packet[timPos]=packet[timPos]-previousTime-bunchLen;
184 for(Int_t i=0;i<numWords;i++){
186 table[nextTableType]->SetFrequency(value);
187 stat[nextTableType]++;
188 NextTable(value,nextTableType,bunchLen,count);
192 cout<<"Number of words: "<<countWords<<endl;
193 cout<<"Number of trailers: "<<countTrailer<<endl;
194 cout<<"Number of fill words "<<fFillWords+endFill<<endl;
195 cout<<"Total number of words: "<<countWords+countTrailer*4+fFillWords<<endl;
197 fStat.open("Statistics");
198 fStat<<"Number of words:..........................................."<<countWords<<endl;
199 fStat<<"Number of trailers (4 10 bits words in each one)..........."<<countTrailer<<endl;
200 fStat<<"Number of fill words:......................................"<<fFillWords+endFill<<endl;
201 fStat<<"Total number of words:....................................."<<countWords+countTrailer*4+fFillWords+endFill<<endl;
202 fStat<<"-----------------------------------------"<<endl;
203 fStat<<"Number of Bunches............."<<stat[0]<<endl;
204 fStat<<"Number of Time bin............"<<stat[1]<<endl;
205 fStat<<"Number of One Samples Bunch..."<<stat[2]<<endl;
206 fStat<<"Number of Central Samples....."<<stat[3]<<endl;
207 fStat<<"Number of Border Samples......"<<stat[4]<<endl;
208 fStat<<"-----------------------------------------"<<endl;
209 UInt_t fileDimension=(UInt_t)TMath::Ceil(double((countTrailer*4+countWords+fFillWords+endFill)*10/8));
210 fStat<<"Total file Size in bytes.."<<fileDimension<<endl;
211 Double_t percentage=TMath::Ceil((fFillWords+endFill)*125)/fileDimension;
212 fStat<<"Fill Words................"<<(UInt_t)TMath::Ceil((fFillWords+endFill)*10/8)<<" bytes "<<percentage<<"%"<<endl;
213 percentage=(Double_t)countTrailer*500/fileDimension;
214 fStat<<"Trailer..................."<<countTrailer*5<<" bytes "<<percentage<<"%"<<endl;
216 percentage=(Double_t)((stat[0]+stat[1]+stat[2]+stat[3]+stat[4])) *125/fileDimension;
217 fStat<<"Data......................"<<(UInt_t)TMath::Ceil((stat[0]+stat[1]+stat[2]+stat[3]+stat[4])*10/8)<<" bytes "<<percentage<<"%"<<endl;
219 percentage=(Double_t)(stat[0]*125)/fileDimension;
220 fStat<<"Bunch....................."<<(UInt_t)TMath::Ceil(stat[0]*10/8)<<" bytes "<<percentage<<"%"<<endl; //
221 percentage=(Double_t)(stat[1]*125)/fileDimension;
222 fStat<<"Time......................"<<(UInt_t)TMath::Ceil(stat[1]*10/8)<<" bytes "<<percentage<<"%"<<endl; //
225 percentage=(Double_t)((stat[2]+stat[3]+stat[4])) *125/fileDimension;
226 fStat<<"Amplitude values.........."<<(UInt_t)TMath::Ceil((stat[2]+stat[3]+stat[4])*10/8)<<" bytes "<<percentage<<"%"<<endl;
227 percentage=(Double_t)(stat[2]*125)/fileDimension;
228 fStat<<" One Samples..............."<<(UInt_t)TMath::Ceil(stat[2]*10/8)<<" bytes "<<percentage<<"%"<<endl; //
229 percentage=(Double_t)(stat[3]*125)/fileDimension;
230 fStat<<" Central Samples..........."<<(UInt_t)TMath::Ceil(stat[3]*10/8)<<" bytes "<<percentage<<"%"<<endl; //
231 percentage=(Double_t)(stat[4]*125)/fileDimension;
232 fStat<<" Border Samples............"<<(UInt_t)TMath::Ceil(stat[4]*10/8)<<" bytes "<<percentage<<"%"<<endl; //
236 ////////////////////////////////////////////////////////////////////////////////////////
237 Int_t AliTPCCompression::StoreTables(AliTPCHTable* table[],const Int_t NumTable){
238 //This method stores the tables in a sequence of binary file
241 for(Int_t k=0;k<NumTable;k++){
242 sprintf(filename,"Table%d.dat",k);
244 fTable.open(filename,ios::binary);
246 fTable.open(filename);
248 Int_t dim=table[k]->Size();
249 //Table dimension is written into a file
250 fTable.write((char*)(&dim),sizeof(Int_t));
251 //One table is written into a file
252 for(Int_t i=0;i<dim;i++){
253 UChar_t codeLen=table[k]->CodeLen()[i];
254 // UInt_t code=(UInt_t)table[k]->Code()[i];
255 Double_t code=table[k]->Code()[i];
256 fTable.write((char*)(&codeLen),sizeof(UChar_t));
257 //fTable.write((char*)(&code),sizeof(UInt_t));
258 fTable.write((char*)(&code),sizeof(Double_t));
264 ////////////////////////////////////////////////////////////////////////////////////////
265 Int_t AliTPCCompression::CreateTableFormula(Double_t beta,UInt_t M,Int_t dim,Int_t Type){
266 // Type = 0 for Bunch length
267 // Type = 1 for Time Gap
273 AliTPCHTable *Table=new AliTPCHTable(dim);
276 Double_t FreqArray[1024];
277 for(Int_t i=0;i<1024;i++){
280 alpha=M*0.000000602+0.0104;
282 cout<<"alpha "<<alpha<<endl;
283 for(Int_t x=0;x<dim;x++){
285 FreqArray[x]=TMath::Power((x+1),-beta)*TMath::Exp(-alpha*(x+1));
287 FreqArray[x]=TMath::Power((x+1),-beta);
289 if (FreqArray[x]<min)min=FreqArray[x];
292 cout<<"Minimun Value "<<min<<endl;
295 cout<<"A Value: "<<A<<endl;
296 for(Int_t x=0;x<dim;x++){
297 if (Type==0)//Bunch length
298 if (x>=3)//minimum bunch length
299 Table->SetValFrequency(x,A*FreqArray[x]*1000);
301 Table->SetValFrequency(x,0);
303 Table->SetValFrequency(x,A*FreqArray[x]);
305 Table->BuildHTable();
308 sprintf(filename,"Table%d.dat",Type);
310 fTable.open(filename,ios::binary);
312 fTable.open(filename);
314 Int_t dimTable=Table->Size();
315 //Table dimension is written into a file
316 fTable.write((char*)(&dimTable),sizeof(Int_t));
317 //One table is written into a file
318 for(Int_t i=0;i<dimTable;i++){
319 UChar_t CodeLen=Table->CodeLen()[i];
320 Double_t Code=Table->Code()[i];
321 fTable.write((char*)(&CodeLen),sizeof(UChar_t));
322 fTable.write((char*)(&Code),sizeof(Double_t));
328 ////////////////////////////////////////////////////////////////////////////////////////
329 Int_t AliTPCCompression::CreateTables(const char* fSource,Int_t NumTables){
333 0==> Bunch length values
339 Int_t n=10;// 10 bits per symbol
340 AliTPCHTable ** table = new AliTPCHTable*[NumTables];
341 //The table is inizialized with the rigth number of rows
342 for(Int_t i=0;i<NumTables;i++){
343 table[i]=new AliTPCHTable((Int_t)(TMath::Power(2,n)));
344 table[i]->SetVerbose(fVerbose);
346 //The frequencies are calculated and the tables are filled
348 cout<<"Filling tables...\n";
349 //The get the frequencies
350 FillTables(fSource,table,NumTables);
352 //This part will be used in the table optimization phase
354 for(Int_t i=0;i<NumTables;i++){
355 table[i]->CompleteTable(i);
359 cout<<"Entropy of Bunch length table........."<<table[0]->GetEntropy()<<endl;
360 cout<<"Entropy of Time bin table............."<<table[1]->GetEntropy()<<endl;
361 cout<<"Entropy of one Sample bunch table....."<<table[2]->GetEntropy()<<endl;
362 cout<<"Entropy of Central Sample table......."<<table[3]->GetEntropy()<<endl;
363 cout<<"Entropy Border Samples table.........."<<table[4]->GetEntropy()<<endl;
365 fStat.open("Statistics",ios::app);
367 fStat<<"----------------- ENTROPY for castomized tables --------------------------"<<endl;
368 fStat<<"Entropy of Bunch length table......."<<table[0]->GetEntropy()<<endl;
369 fStat<<"Entropy of Time bin table..........."<<table[1]->GetEntropy()<<endl;
370 fStat<<"Entropy of one Sample bunch table..."<<table[2]->GetEntropy()<<endl;
371 fStat<<"Entropy of Central Sample table....."<<table[3]->GetEntropy()<<endl;
372 fStat<<"Entropy Border Samples table........"<<table[4]->GetEntropy()<<endl;
376 cout<<"Tables filled \n";
378 //Frequencies normalization
379 table[0]->NormalizeFrequencies();
380 table[1]->NormalizeFrequencies();
381 table[2]->NormalizeFrequencies();
382 table[3]->NormalizeFrequencies();
383 table[4]->NormalizeFrequencies();
385 //Tables are saved in a sequence of text file and using the macro Histo.C is it possible to get
386 //a series of histograms rappresenting the frequency distribution
387 table[0]->StoreFrequencies("BunchLenFreq.txt");
388 table[1]->StoreFrequencies("TimeFreq.txt");
389 table[2]->StoreFrequencies("Sample1Freq.txt");
390 table[3]->StoreFrequencies("SCentralFreq.txt");
391 table[4]->StoreFrequencies("SBorderFreq.txt");
393 cout<<"Creating Tables..\n";
394 //One Huffman tree is created for each table starting from the frequencies of the symbols
395 for(Int_t i=0;i<NumTables;i++){
396 table[i]->BuildHTable();
398 cout<<"Number of elements inside the table:"<<table[i]->GetWordsNumber();
401 cout<<" (Bunch Length)"<<endl;
405 cout<<" (Time Bin)"<<endl;
409 cout<<" (1 Samples Bunch)"<<endl;
413 cout<<" (Central Samples)"<<endl;
417 cout<<" (Border Samples)"<<endl;
421 table[i]->PrintTable();
424 //The tables are saved ad binary files
425 StoreTables(table,NumTables);
426 //The tables stored in memory are deleted;
427 for(Int_t i=0;i<NumTables;i++)delete table[i];
431 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
432 Int_t AliTPCCompression::RetrieveTables(AliTPCHTable* table[],Int_t NumTable){
433 //This method retrieve the Huffman tables from a sequence of binary files
435 cout<<"Retrieving tables from files \n";
441 //The following for loop is used to generate the Huffman trees acording to the tables
442 for(Int_t k=0;k<NumTable;k++){
443 Int_t dim;//this variable contains the table dimension
444 sprintf(filename,"Table%d.dat",k);
446 fTable.open(filename,ios::binary);
448 fTable.open(filename);
450 if(!fTable && gSystem->Getenv("ALICE_ROOT")){
452 sprintf(filename,"%s/RAW/Table%d.dat",gSystem->Getenv("ALICE_ROOT"),k);
454 fTable.open(filename,ios::binary);
456 fTable.open(filename);
460 Error("RetrieveTables", "File doesn't exist: %s", filename);
463 fTable.read((char*)(&dim),sizeof(Int_t));
465 cout<<"Table dimension: "<<dim<<endl;
466 table[k]=new AliTPCHTable(dim);
467 for(Int_t i=0;i<dim;i++){
468 fTable.read((char*)(&codeLen),sizeof(UChar_t));
469 table[k]->SetCodeLen(codeLen,i);
470 // fTable.read((char*)(&code),sizeof(UInt_t));
471 fTable.read((char*)(&code),sizeof(Double_t));
472 table[k]->SetCode(Mirror((UInt_t)code,codeLen),i);
477 cout<<"Trees generated \n";
478 //At this point the trees are been built
482 Int_t AliTPCCompression::CreateTablesFromTxtFiles(Int_t NumTable){
483 //This method creates a set of binary tables, needed by the Huffman
484 //algorith, starting from a set of frequencies tables stored in form of
487 cout<<"Retrieving frequencies from txt files \n";
490 //Tables are read from the files (Each codeword has been "Mirrored")
491 AliTPCHTable **table = new AliTPCHTable*[NumTable];
492 for(Int_t k=0;k<NumTable;k++){
493 sprintf(filename,"Table%d.txt",k);
494 cout<<filename<<endl;
495 fTable.open(filename);
497 Error("CreateTablesFromTxtFiles", "File doesn't exist: %s", filename);
502 table[k]=new AliTPCHTable(1024);
503 while(!fTable.eof()){
507 cout<<"Frequency cannot be negative !!!\n";
510 table[k]->SetValFrequency(symbol,freq);
517 fStat.open("Statistics",ios::app);
519 fStat<<"----------------- ENTROPY for external txt tables --------------------------"<<endl;
520 fStat<<"Entropy of Bunch length table......."<<table[0]->GetEntropy()<<endl;
521 fStat<<"Entropy of Time bin table..........."<<table[1]->GetEntropy()<<endl;
522 fStat<<"Entropy of one Sample bunch table..."<<table[2]->GetEntropy()<<endl;
523 fStat<<"Entropy of Central Sample table....."<<table[3]->GetEntropy()<<endl;
524 fStat<<"Entropy Border Samples table........"<<table[4]->GetEntropy()<<endl;
526 for(Int_t k=0;k<NumTable;k++){
527 table[k]->BuildHTable();
529 //The tables are saved ad binary files
530 StoreTables(table,NumTable);
531 //The tables stored in memory are deleted;
532 for(Int_t i=0;i<NumTable;i++)delete table[i];
537 ////////////////////////////////////////////////////////////////////////////////////////
539 ////////////////////////////////////////////////////////////////////////////////////////
541 void AliTPCCompression::StoreValue(UInt_t val,UChar_t len){
542 //This method stores the value "val" of "len" bits into the internal buffer "fBuffer"
543 if (len<=fFreeBitsBuffer){ // val is not splitted in two buffer
544 fFreeBitsBuffer-=len;
545 fBuffer=fBuffer<<len;
547 if(!fFreeBitsBuffer){ // if the buffer is full it is written into a file
548 f.write((char*)(&fBuffer),sizeof(UInt_t));
549 fFreeBitsBuffer=fDimBuffer;
553 else{ //val has to be splitted in two buffers
554 fBuffer=fBuffer<<fFreeBitsBuffer;
557 temp=temp>>(len-fFreeBitsBuffer);
558 fBuffer=fBuffer|temp;
559 f.write((char*)(&fBuffer),sizeof(UInt_t));
560 fFreeBitsBuffer=fDimBuffer-(len-fFreeBitsBuffer);
561 val=val<<fFreeBitsBuffer;
562 val=val>>fFreeBitsBuffer;
567 //////////////////////////////////////////////////////////////////////////////////////////////////
568 void AliTPCCompression::Flush(){
569 //The last buffer cannot be completely full so to save it
570 //into the output file it is first necessary to fill it with an hexadecimal pattern
571 if(fFreeBitsBuffer<fDimBuffer){
572 fBuffer=fBuffer<<fFreeBitsBuffer;
573 f.write((char*)(&fBuffer),sizeof(UInt_t));
577 //////////////////////////////////////////////////////////////////////////////////////////////////
578 UInt_t AliTPCCompression::Mirror(UInt_t val,UChar_t len)const{
579 //This method inverts the digits of the number "val" and length "len"
580 //indicates the number of digits of the number considered in binary notation
584 for(Int_t i=0;i<len;i++){
587 specular=specular<<1;
588 specular=specular|bit;
594 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
595 Int_t AliTPCCompression::CompressDataOptTables(Int_t NumTable,const char* fSource,const char* fDest){
596 //This method compress an Altro format file using a general set of tables stored as binary files to be provided
598 cout<<" BackWord COMPRESSION "<<endl;
599 cout<<"compression of the file "<<fSource<<" Output File: "<<fDest<<endl;
601 //Tables are read from the files (Each codeword has been "Mirrored")
602 AliTPCHTable **table = new AliTPCHTable*[NumTable];
603 for(Int_t i=0;i<NumTable;i++) table[i] = NULL;
604 if (RetrieveTables(table,NumTable) != 0) {
605 for(Int_t i=0;i<NumTable;i++) delete table[i];
609 //the output file is open
612 f.open(fDest,ios::binary|ios::out);
614 f.open(fDest,ios::out);
616 // Source file is open
617 AliTPCBuffer160 buff(fSource,0);
618 //coded words are written into a file
619 Int_t numWords,padNum,rowNum,secNum=0;
620 UInt_t storedWords=0;
623 Double_t stat[5]={0.,0.,0.,0.,0.};
624 UInt_t trailerNumbers=0;
625 Double_t numElem[5]={0,0,0,0,0};
626 Double_t fillWords=0.;
628 fStat.open("Statistics",ios::app);
630 fStat<<"-------------------COMPRESSION STATISTICS----------"<<endl;
632 while(buff.ReadTrailerBackward(numWords,padNum,rowNum,secNum) !=-1 ){
634 fillWords=buff.GetFillWordsNum();
640 fillWords+=4-numWords%4;
641 for(Int_t j=0;j<(4-numWords%4);j++){
642 value=buff.GetNextBackWord();
649 for(Int_t i=0;i<345;i++)timePos[i]=0;
650 for(Int_t i=0;i<1024;i++)packet[i]=0;
652 Int_t nextTableType=0;
655 for(Int_t i=0;i<numWords;i++){
656 value=buff.GetNextBackWord();
658 if(nextTableType==1){
662 NextTable(value,nextTableType,bunchLen,count);
664 //computing the Time gap between two bunches
667 Int_t previousTime=packet[timePos[tp]];
668 for(Int_t i=tp-1;i>=0;i--){
669 Int_t timPos=timePos[i];
670 Int_t bunchLen=packet[timPos-1]-2;
672 packet[timPos]=packet[timPos]-previousTime-bunchLen;
680 for(Int_t i=0;i<numWords;i++){
682 if(nextTableType==1)timeBin=value;
684 //UInt_t val=(UInt_t)table[nextTableType]->Code()[value]; // val is the code
685 Double_t val=table[nextTableType]->Code()[value]; // val is the code
686 UChar_t len=table[nextTableType]->CodeLen()[value]; // len is the length (number of bits)of val
687 stat[nextTableType]+=len;
688 numElem[nextTableType]++;
689 StoreValue((UInt_t)val,len);
692 NextTable(value,nextTableType,bunchLen,count);
693 if(nextTableType==0){
694 // UInt_t val=(UInt_t)table[1]->Code()[timeBin]; // val is the code
695 Double_t val=table[1]->Code()[timeBin]; // val is the code
696 UChar_t len=table[1]->CodeLen()[timeBin]; // len is the length (number of bits)of val
699 StoreValue((UInt_t)val,len);
700 // val=(UInt_t)table[nextTableType]->Code()[(bunchLen+2)]; // val is the code
701 val=table[nextTableType]->Code()[(bunchLen+2)]; // val is the code
702 len=table[nextTableType]->CodeLen()[(bunchLen+2)]; // len is the length (number of bits)of val
703 StoreValue((UInt_t)val,len);
704 stat[nextTableType]+=len;
705 numElem[nextTableType]++;
710 StoreValue(numWords,10);
711 StoreValue(padNum,10);
712 StoreValue(rowNum,10);
713 StoreValue(secNum,9);
718 StoreValue(numPackets,32);
720 cout<<"Number of strored packets: "<<numPackets<<endl;
722 //The last buffen cannot be completely full
725 cout<<"Number of stored words: "<<storedWords<<endl;
728 for(Int_t i=0;i<NumTable;i++){
732 Double_t dimension=(UInt_t)TMath::Ceil((stat[0]+stat[1]+stat[2]+stat[3]+stat[4])/8)+trailerNumbers*5;
733 fStat<<"Trailer Dimension in bytes......"<<trailerNumbers*5<<endl;
734 fStat<<"Data Dimension in bytes........."<<(UInt_t)TMath::Ceil((stat[0]+stat[1]+stat[2]+stat[3]+stat[4])/8)<<endl;
735 fStat<<"Compressed file dimension......."<<(UInt_t)dimension<<endl;
737 fStat<<(UInt_t)trailerNumbers<<endl;
738 fStat<<(UInt_t)fillWords<<endl;
739 fStat<<(UInt_t)numElem[0]<<endl;
740 fStat<<(UInt_t)numElem[1]<<endl;
741 fStat<<(UInt_t)numElem[2]<<endl;
742 fStat<<(UInt_t)numElem[3]<<endl;
743 fStat<<(UInt_t)numElem[4]<<endl;
745 fillWords=(fillWords+numElem[0]+numElem[1]+numElem[2]+numElem[3]+numElem[4]+trailerNumbers*4)*10/8;
746 fStat<<"Original file dimension........."<<(UInt_t)fillWords<<endl;
748 Double_t ratio=(dimension/fillWords)*100;
749 fStat<<"Compression ratio (Compressed/Uncompressed)..."<<ratio<<"%"<<endl;
752 fStat<<"Bunch length size in bytes......"<<(UInt_t)TMath::Ceil(stat[0]/8)<<" Comppression.."<<(stat[0]/numElem[0])*10<<"%"<<endl;
754 fStat<<"Time gap size in bytes.........."<<(UInt_t)TMath::Ceil(stat[1]/8)<<" Comppression.."<<(stat[1]/numElem[1])*10<<"%"<<endl;
755 if (numElem[2]+numElem[3]+numElem[4])
756 fStat<<"Amplitude values in bytes......."<<(UInt_t)TMath::Ceil((stat[2]+stat[3]+stat[4])/8)<<" Comppression.."<<
757 ((stat[2]+stat[3]+stat[4])/(numElem[2]+numElem[3]+numElem[4]))*10<<"%"<<endl;
759 fStat<<" One Samples in bytes............"<<(UInt_t)TMath::Ceil(stat[2]/8)<<" Comppression.."<<(stat[2]/numElem[2])*10<<"%"<<endl;
761 fStat<<" Central Samples size in bytes..."<<(UInt_t)TMath::Ceil(stat[3]/8)<<" Comppression.."<<(stat[3]/numElem[3])*10<<"%"<<endl;
763 fStat<<" Border Samples size in bytes...."<<(UInt_t)TMath::Ceil(stat[4]/8)<<" Comppression.."<<(stat[4]/numElem[4])*10<<"%"<<endl;
765 fStat<<"Average number of bits per word"<<endl;
767 fStat<<"Bunch length ......"<<stat[0]/numElem[0]<<endl;
769 fStat<<"Time gap .........."<<stat[1]/numElem[1]<<endl;
771 fStat<<"One Samples........"<<stat[2]/numElem[2]<<endl;
773 fStat<<"Central Samples ..."<<stat[3]/numElem[3]<<endl;
775 fStat<<"Border Samples....."<<stat[4]/numElem[4]<<endl;
780 ////////////////////////////////////////////////////////////////////////////////////////
782 ////////////////////////////////////////////////////////////////////////////////////////
784 ////////////////////////////////////////////////////////////////////////////////////////
786 Int_t AliTPCCompression::CreateTreesFromFile(AliTPCHNode *RootNode[],Int_t NumTables){
787 //For each table this method builds the associate Huffman tree starting from the codeword and
788 //the codelength of each symbol
790 cout<<"Creating the Huffman trees \n";
797 //The following for loop is used to generate the Huffman trees acording to the tables
798 //loop over the tables
799 for(Int_t k=0;k<NumTables;k++){
800 RootNode[k]=new AliTPCHNode(); //RootNode is the root of the tree
801 Int_t dim=0;//this variable contains the table dimension
802 sprintf(filename,"Table%d.dat",k);
804 fTable.open(filename,ios::binary);
806 fTable.open(filename);
808 if(!fTable && gSystem->Getenv("ALICE_ROOT")){
810 sprintf(filename,"%s/RAW/Table%d.dat",gSystem->Getenv("ALICE_ROOT"),k);
812 fTable.open(filename,ios::binary);
814 fTable.open(filename);
818 Error("CreateTreesFromFile", "File doesn't exist: %s", filename);
821 fTable.read((char*)(&dim),sizeof(Int_t));
823 cout<<"Table dimension: "<<dim<<endl;
824 //loop over the words of one table
825 for(Int_t i=0;i<dim;i++){
826 fTable.read((char*)(&codeLen),sizeof(UChar_t));
827 //fTable.read((char*)(&code),sizeof(UInt_t));
828 fTable.read((char*)(&code),sizeof(Double_t));
830 for(Int_t j=1;j<=codeLen;j++){
832 val=(UInt_t)TMath::Power(2,codeLen-j);
833 bit=(UInt_t)code&val;
834 AliTPCHNode *temp=node;
836 node=node->GetRight();
838 node=new AliTPCHNode();
839 temp->SetRight(node);
843 node=node->GetLeft();
845 node=new AliTPCHNode();
852 node->SetFrequency(codeLen);
858 cout<<"Trees generated \n";
859 //At this point the trees are been built
862 //////////////////////////////////////////////////////////////////////////////////////////////////
863 void AliTPCCompression::DeleteHuffmanTree(AliTPCHNode* node){
864 //This function deletes all the nodes of an Huffman tree
865 //In an Huffman tree any internal node has always two children
867 DeleteHuffmanTree(node->GetLeft());
868 DeleteHuffmanTree(node->GetRight());
869 // cout<<node->GetSymbol()<<" "<<(Int_t)node->GetFrequency()<<endl;
873 //////////////////////////////////////////////////////////////////////////////////////////////////
874 void AliTPCCompression::VisitHuffmanTree(AliTPCHNode* node){
875 //This function realizes an in order visit of a binary tree
877 cout<<node->GetSymbol()<<" "<<node->GetFrequency()<<endl;
878 VisitHuffmanTree(node->GetLeft());
879 VisitHuffmanTree(node->GetRight());
882 //////////////////////////////////////////////////////////////////////////////////////////////////
883 UInt_t AliTPCCompression::ReadWord(Int_t NumberOfBit){
884 //This method retrieves a word of a specific number of bits from the file through the internal buffer
887 for (Int_t i=0;i<NumberOfBit;i++){
889 fPos-=sizeof(UInt_t);
891 f.read((char*)(&fBuffer),sizeof(UInt_t));
895 mask=(UInt_t)TMath::Power(2,fReadBits);
904 //////////////////////////////////////////////////////////////////////////////////////////////////
905 UInt_t AliTPCCompression::ReadWordBuffer(Int_t NumberOfBit){
906 //This method retrieves a word of a specific number of bits from the file through the buffer
909 for (Int_t i=0;i<NumberOfBit;i++){
913 for(Int_t i=0;i<4;i++){
924 mask=(UInt_t)TMath::Power(2,fReadBits);
934 //////////////////////////////////////////////////////////////////////////////////////////////////
935 void AliTPCCompression::ReadTrailer(Int_t &WordsNumber,Int_t &PadNumber,Int_t &RowNumber,Int_t &SecNumber,Bool_t Memory){
936 //It retrieves a trailer
939 SecNumber=ReadWordBuffer(9);
940 RowNumber=ReadWordBuffer(10);
941 PadNumber=ReadWordBuffer(10);
942 WordsNumber=ReadWordBuffer(10);
946 SecNumber=ReadWord(9);
947 RowNumber=ReadWord(10);
948 PadNumber=ReadWord(10);
949 WordsNumber=ReadWord(10);
953 //////////////////////////////////////////////////////////////////////////////////////////////////
954 UInt_t AliTPCCompression::GetDecodedWord(AliTPCHNode* root,Bool_t Memory){
955 //This method retrieves a decoded word.
956 AliTPCHNode *node=root;
962 bit=ReadWordBuffer(1);
966 node=node->GetRight();
968 node=node->GetLeft();
969 if (!(node->GetLeft())){
970 symbol=node->GetSymbol();
976 //////////////////////////////////////////////////////////////////////////////////////////////////
978 Int_t AliTPCCompression::DecompressDataOptTables(Int_t NumTables,const char* fname, const char* fDest){
979 //This method decompress a file using separate Huffman tables
981 cout<<" DECOMPRESSION:"<<endl;
982 cout<<"Source File "<<fname<<" Destination File "<<fDest<<endl;
984 AliTPCHNode ** rootNode = new AliTPCHNode*[NumTables];
985 for(Int_t i=0;i<NumTables;i++) rootNode[i] = NULL;
986 //Creation of the Huffman trees
987 if (CreateTreesFromFile(rootNode,NumTables) != 0) {
988 for(Int_t i=0;i<NumTables;i++) {
989 if (rootNode[i]) DeleteHuffmanTree(rootNode[i]);
996 f.open(fname,ios::binary|ios::in);
998 f.open(fname,ios::in);
1001 Error("DecompressDataOptTables", "File doesn't exist:",fname);
1004 //to go to the end of the file
1005 f.seekg(0,ios::end);
1006 //to get the file dimension in byte
1008 fPos-=sizeof(UInt_t);
1012 f.read((char*)(&fBuffer),sizeof(UInt_t));
1020 UInt_t packetNumber=ReadWord(sizeof(UInt_t)*8);
1022 cout<<"Number of Packect: "<<packetNumber<<endl;
1024 AliTPCBuffer160 bufferFile(fDest,1);
1026 UInt_t wordsRead=0; //number of read coded words
1027 while(k<packetNumber){
1028 Int_t numWords,padNumber,rowNumber,secNumber=0;
1029 ReadTrailer(numWords,padNumber,rowNumber,secNumber,kFALSE);
1032 Int_t previousTime=-1;
1034 Int_t nextTableType=0;
1037 for(Int_t i=0;i<numWords;i++){
1038 UInt_t symbol=GetDecodedWord(rootNode[nextTableType],kFALSE);
1040 //Time reconstruction
1041 if (nextTableType==1){
1042 if (previousTime!=-1){
1043 previousTime=symbol+previousTime+bunchLen;
1045 else previousTime=symbol;
1049 bufferFile.FillBuffer(symbol);
1050 NextTable(symbol,nextTableType,bunchLen,count);
1051 if(nextTableType==0){
1052 bufferFile.FillBuffer(time);
1053 bufferFile.FillBuffer(bunchLen+2);
1057 bufferFile.WriteTrailer(numWords,padNumber,rowNumber,secNumber);
1060 cout<<"Number of decoded words:"<<wordsRead<<endl;
1063 //The trees are deleted
1064 for(Int_t j=0;j<NumTables;j++){
1065 DeleteHuffmanTree(rootNode[j]);
1071 //////////////////////////////////////////////////////////////////////////////////////////////////
1072 Int_t AliTPCCompression::Decompress(AliTPCHNode *RootNode[],Int_t /*NumTables*/,char* PointBuffer,UInt_t BufferSize,UShort_t out[],UInt_t &dim){
1073 //This method decompress a file using separate Huffman tables
1075 fPointBuffer=PointBuffer+BufferSize-4;
1079 for(Int_t i=0;i<4;i++){
1085 fBuffer=fBuffer|val;
1094 UInt_t packetNumber=ReadWordBuffer(sizeof(UInt_t)*8); //32 bits
1096 cout<<"First one has been found "<<endl;
1097 cout<<"Number of packets:"<<packetNumber<<endl;
1100 UInt_t wordsRead=0; //number of read coded words
1101 while(k<packetNumber){
1102 Int_t numWords,padNumber,rowNumber,secNumber=0;
1103 ReadTrailer(numWords,padNumber,rowNumber,secNumber,kTRUE);
1112 //ftxt<<"S:"<<secNumber<<" R:"<<rowNumber<<" P:"<<padNumber<<" W:"<<numWords<<endl;
1113 // padDigits->SetPadID(padNumber,rowNumber,secNumber,DDL);
1116 Int_t previousTime=-1;
1118 Int_t nextTableType=0;
1122 for(Int_t i=0;i<numWords;i++){
1123 UInt_t symbol=GetDecodedWord(RootNode[nextTableType],kTRUE);
1125 //Time reconstruction
1126 if (nextTableType==1){
1127 if (previousTime!=-1){
1128 previousTime=symbol+previousTime+bunchLen;
1130 else previousTime=symbol;
1132 out[dim]=bunchLen+2;
1136 timeDigit=time-bunchLen;
1138 if(nextTableType>1){
1140 //ftxt<<symbol<<endl;
1144 //padDigits->SetDigits(symbol,timeDigit);
1146 NextTable(symbol,nextTableType,bunchLen,count);
1147 if(nextTableType==0){
1150 // ftxt<<(bunchLen+2)<<endl;
1158 //////////////////////////////////////////////////////////////////////////////////////////////////
1159 Int_t AliTPCCompression::DestroyTables(AliTPCHNode *RootNode[],Int_t NumTables){
1160 //The trees are deleted
1161 for(Int_t j=0;j<NumTables;j++){
1162 DeleteHuffmanTree(RootNode[j]);
1165 cout<<"Huffman trees destroyed"<<endl;
1168 //////////////////////////////////////////////////////////////////////////////////////////////////
1170 void AliTPCCompression::ReadAltroFormat(char* fileOut,char* fileIn)const{
1171 //This method creates a text file containing the same information stored in
1172 //an Altro file. The information in the text file is organized pad by pad and
1173 //and for each pad it consists in a sequence of bunches (Bunch length +2,
1174 //Time bin of the last amplitude sample in the bunch, amplitude values)
1175 //It is used mainly for debugging
1176 ofstream ftxt(fileOut);
1177 AliTPCBuffer160 buff(fileIn,0);
1178 Int_t numWords,padNum,rowNum,secNum=0;
1180 if (fVerbose) cout<<"Creating a txt file from an Altro Format file"<<endl;
1181 while(buff.ReadTrailerBackward(numWords,padNum,rowNum,secNum) !=-1 ){
1182 ftxt<<"S:"<<secNum<<" R:"<<rowNum<<" P:"<<padNum<<" W:"<<numWords<<endl;
1184 for(Int_t j=0;j<(4-numWords%4);j++){
1185 value=buff.GetNextBackWord();
1188 for(Int_t i=0;i<numWords;i++){
1189 value=buff.GetNextBackWord();
1197 //////////////////////////////////////////////////////////////////////////////////////////