]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - RAW/AliTPCCompression.cxx
Added type which is not used yet, but will be for mixed events.
[u/mrichter/AliRoot.git] / RAW / AliTPCCompression.cxx
index 54286601c74fd8e7e01ad781c5775c316a221106..e994a13ee5b84481857a9fddd0ba2d3a9d418fd8 100644 (file)
 #include <TObjArray.h>
 #include <Riostream.h>
 #include <TMath.h>
-#include "AliTPCBuffer160.h"
-#include "AliTPCHuffman.h"
+#include <TSystem.h>
+#include "AliAltroBuffer.h"
+#include "AliTPCHNode.h"
+#include "AliTPCHTable.h"
 #include "AliTPCCompression.h"
 
 ClassImp(AliTPCCompression)
@@ -127,7 +129,7 @@ void AliTPCCompression::NextTable(Int_t Val,Int_t &NextTableType,Int_t &BunchLen
 
 Int_t AliTPCCompression::FillTables(const char* fSource,AliTPCHTable* table[],Int_t /*NumTables*/){
   //This method is used to compute the frequencies of the symbols in the source file
-  AliTPCBuffer160 buff(fSource,0);
+  AliAltroBuffer buff(fSource,0);
   UInt_t countWords=0;
   UInt_t countTrailer=0;
   Int_t numWords,padNum,rowNum,secNum=0;
@@ -135,7 +137,7 @@ Int_t AliTPCCompression::FillTables(const char* fSource,AliTPCHTable* table[],In
   UInt_t stat[5]={0,0,0,0,0};
   Int_t endFill=0;
   Int_t end=1;
-  while(buff.ReadTrailerBackward(numWords,padNum,rowNum,secNum) !=-1 ){
+  while(buff.ReadTrailerBackward(numWords,padNum,rowNum,secNum)){
     if(end){
       endFill=buff.GetFillWordsNum();
       end=0;
@@ -268,40 +270,40 @@ Int_t AliTPCCompression::CreateTableFormula(Double_t beta,UInt_t M,Int_t dim,Int
   Double_t sum=0;
   Double_t min=10;
   Double_t alpha=0;
-  Double_t A=0;
-  AliTPCHTable *Table=new AliTPCHTable(dim);
+  Double_t a=0;
+  AliTPCHTable *table=new AliTPCHTable(dim);
   
   freq=1;
-  Double_t FreqArray[1024];
+  Double_t freqArray[1024];
   for(Int_t i=0;i<1024;i++){
-    FreqArray[i]=0;
+    freqArray[i]=0;
   }
   alpha=M*0.000000602+0.0104;
   if (fVerbose)
     cout<<"alpha "<<alpha<<endl;
   for(Int_t x=0;x<dim;x++){
     if (Type==1)
-      FreqArray[x]=TMath::Power((x+1),-beta)*TMath::Exp(-alpha*(x+1));
+      freqArray[x]=TMath::Power((x+1),-beta)*TMath::Exp(-alpha*(x+1));
     else
-      FreqArray[x]=TMath::Power((x+1),-beta);
-    sum+=FreqArray[x];
-    if (FreqArray[x]<min)min=FreqArray[x];
+      freqArray[x]=TMath::Power((x+1),-beta);
+    sum+=freqArray[x];
+    if (freqArray[x]<min)min=freqArray[x];
   }//end for
   if (fVerbose)
     cout<<"Minimun Value "<<min<<endl;
-  A=1/sum;
+  a=1/sum;
   if (fVerbose)
-    cout<<"A Value: "<<A<<endl;
+    cout<<"a Value: "<<a<<endl;
   for(Int_t x=0;x<dim;x++){
     if (Type==0)//Bunch length
       if (x>=3)//minimum bunch length
-       Table->SetValFrequency(x,A*FreqArray[x]*1000);
+       table->SetValFrequency(x,a*freqArray[x]*1000);
       else
-       Table->SetValFrequency(x,0);
+       table->SetValFrequency(x,0);
     else //Time table
-      Table->SetValFrequency(x,A*FreqArray[x]);
+      table->SetValFrequency(x,a*freqArray[x]);
   }
-  Table->BuildHTable();
+  table->BuildHTable();
   ofstream fTable;
   char filename[15];
   sprintf(filename,"Table%d.dat",Type); 
@@ -310,18 +312,18 @@ Int_t AliTPCCompression::CreateTableFormula(Double_t beta,UInt_t M,Int_t dim,Int
 #else
   fTable.open(filename);
 #endif
-  Int_t dimTable=Table->Size();
+  Int_t dimTable=table->Size();
   //Table dimension is written into a file
   fTable.write((char*)(&dimTable),sizeof(Int_t));
   //One table is written into a file
   for(Int_t i=0;i<dimTable;i++){
-    UChar_t CodeLen=Table->CodeLen()[i];
-    Double_t Code=Table->Code()[i];
-    fTable.write((char*)(&CodeLen),sizeof(UChar_t));
-    fTable.write((char*)(&Code),sizeof(Double_t));
+    UChar_t codeLen=table->CodeLen()[i];
+    Double_t code=table->Code()[i];
+    fTable.write((char*)(&codeLen),sizeof(UChar_t));
+    fTable.write((char*)(&code),sizeof(Double_t));
   } //end for
   fTable.close();
-  delete Table;
+  delete table;
   return 0;
 }
 ////////////////////////////////////////////////////////////////////////////////////////
@@ -436,7 +438,7 @@ Int_t AliTPCCompression::RetrieveTables(AliTPCHTable* table[],Int_t NumTable){
   Double_t code;
   UChar_t codeLen;
   ifstream fTable;  
-  char filename[15];
+  char filename[256];
   //The following for loop is used to generate the Huffman trees acording to the tables
   for(Int_t k=0;k<NumTable;k++){
     Int_t dim;//this variable contains the table dimension
@@ -446,7 +448,19 @@ Int_t AliTPCCompression::RetrieveTables(AliTPCHTable* table[],Int_t NumTable){
 #else
     fTable.open(filename);
 #endif
-    if(!fTable){cout<<"File doesn't exist:"<<filename<<endl; exit(1);}
+    if(!fTable && gSystem->Getenv("ALICE_ROOT")){
+      fTable.clear();
+      sprintf(filename,"%s/RAW/Table%d.dat",gSystem->Getenv("ALICE_ROOT"),k); 
+#ifndef __DECCXX 
+      fTable.open(filename,ios::binary);
+#else
+      fTable.open(filename);
+#endif
+    }
+    if(!fTable){
+      Error("RetrieveTables", "File doesn't exist: %s", filename);
+      return 1;
+    }
     fTable.read((char*)(&dim),sizeof(Int_t));
     if (fVerbose)
       cout<<"Table dimension: "<<dim<<endl;
@@ -480,7 +494,10 @@ Int_t AliTPCCompression::CreateTablesFromTxtFiles(Int_t NumTable){
     sprintf(filename,"Table%d.txt",k); 
     cout<<filename<<endl;
     fTable.open(filename);
-    if(!fTable){cout<<"File doesn't exist: "<<filename<<endl; exit(1);}
+    if(!fTable){
+      Error("CreateTablesFromTxtFiles", "File doesn't exist: %s", filename);
+      return 1;
+    }
     Int_t symbol=0;
     Double_t freq=0;
     table[k]=new AliTPCHTable(1024);
@@ -584,15 +601,21 @@ Int_t AliTPCCompression::CompressDataOptTables(Int_t NumTable,const char* fSourc
   }
   //Tables are read from the files (Each codeword has been "Mirrored")
   AliTPCHTable **table = new AliTPCHTable*[NumTable];
-  RetrieveTables(table,NumTable);
+  for(Int_t i=0;i<NumTable;i++) table[i] = NULL;
+  if (RetrieveTables(table,NumTable) != 0) {
+    for(Int_t i=0;i<NumTable;i++) delete table[i];
+    delete [] table;
+    return 1;
+  }
   //the output file is open
+  f.clear();
 #ifndef __DECCXX 
   f.open(fDest,ios::binary|ios::out);
 #else
   f.open(fDest,ios::out);
 #endif
   // Source file is open
-  AliTPCBuffer160 buff(fSource,0);
+  AliAltroBuffer buff(fSource,0);
   //coded words are written into a file
   Int_t numWords,padNum,rowNum,secNum=0;
   UInt_t  storedWords=0;
@@ -602,11 +625,12 @@ Int_t AliTPCCompression::CompressDataOptTables(Int_t NumTable,const char* fSourc
   UInt_t  trailerNumbers=0;
   Double_t numElem[5]={0,0,0,0,0};
   Double_t fillWords=0.;
+  fStat.clear();
   fStat.open("Statistics",ios::app);
   fStat<<endl;
   fStat<<"-------------------COMPRESSION STATISTICS----------"<<endl;
   Int_t end=1;
-  while(buff.ReadTrailerBackward(numWords,padNum,rowNum,secNum) !=-1 ){
+  while(buff.ReadTrailerBackward(numWords,padNum,rowNum,secNum)){
     if(end){
       fillWords=buff.GetFillWordsNum();
       end=0;
@@ -760,7 +784,7 @@ Int_t AliTPCCompression::CompressDataOptTables(Int_t NumTable,const char* fSourc
 /*                               DECOMPRESSION                                        */
 ////////////////////////////////////////////////////////////////////////////////////////
 
-void AliTPCCompression::CreateTreesFromFile(AliTPCHNode *RootNode[],Int_t NumTables){
+Int_t AliTPCCompression::CreateTreesFromFile(AliTPCHNode *RootNode[],Int_t NumTables){
   //For each table this method builds the associate Huffman tree starting from the codeword and 
   //the codelength of each symbol 
   if(fVerbose)
@@ -770,7 +794,7 @@ void AliTPCCompression::CreateTreesFromFile(AliTPCHNode *RootNode[],Int_t NumTab
   Double_t code;
   UChar_t codeLen;
   ifstream fTable;  
-  char filename[15];
+  char filename[256];
   //The following for loop is used to generate the Huffman trees acording to the tables
   //loop over the tables
   for(Int_t k=0;k<NumTables;k++){
@@ -782,7 +806,19 @@ void AliTPCCompression::CreateTreesFromFile(AliTPCHNode *RootNode[],Int_t NumTab
 #else
     fTable.open(filename);
 #endif
-    if(!fTable){cout<<"Tables don't exist !!!"<<endl;exit(1);}
+    if(!fTable && gSystem->Getenv("ALICE_ROOT")){
+      fTable.clear();
+      sprintf(filename,"%s/RAW/Table%d.dat",gSystem->Getenv("ALICE_ROOT"),k); 
+#ifndef __DECCXX 
+      fTable.open(filename,ios::binary);
+#else
+      fTable.open(filename);
+#endif
+    }
+    if(!fTable){
+      Error("CreateTreesFromFile", "File doesn't exist: %s", filename);
+      return 1;
+    }
     fTable.read((char*)(&dim),sizeof(Int_t));
     if (fVerbose)
       cout<<"Table dimension: "<<dim<<endl;
@@ -822,6 +858,7 @@ void AliTPCCompression::CreateTreesFromFile(AliTPCHNode *RootNode[],Int_t NumTab
   if (fVerbose)
     cout<<"Trees generated \n";
   //At this point the trees are been built
+  return 0;
 }
 //////////////////////////////////////////////////////////////////////////////////////////////////
 void AliTPCCompression::DeleteHuffmanTree(AliTPCHNode* node){
@@ -946,14 +983,25 @@ Int_t AliTPCCompression::DecompressDataOptTables(Int_t NumTables,const char* fna
     cout<<"Source File "<<fname<<" Destination File "<<fDest<<endl; 
   }
   AliTPCHNode ** rootNode = new AliTPCHNode*[NumTables];
+  for(Int_t i=0;i<NumTables;i++) rootNode[i] = NULL;
   //Creation of the Huffman trees
-  CreateTreesFromFile(rootNode,NumTables);
+  if (CreateTreesFromFile(rootNode,NumTables) != 0) {
+    for(Int_t i=0;i<NumTables;i++) {
+      if (rootNode[i]) DeleteHuffmanTree(rootNode[i]);
+    }
+    delete [] rootNode;
+    return 1;
+  }
+  f.clear();
 #ifndef __DECCXX
   f.open(fname,ios::binary|ios::in);
 #else
   f.open(fname,ios::in);
 #endif
-  if(!f){cout<<"File doesn't exist:"<<fname<<endl;;return -1;}
+  if(!f){
+    Error("DecompressDataOptTables", "File doesn't exist:",fname);
+    return -1;
+  }
   //to go to the end of the file
   f.seekg(0,ios::end);
   //to get the file dimension in byte
@@ -974,7 +1022,7 @@ Int_t AliTPCCompression::DecompressDataOptTables(Int_t NumTables,const char* fna
   if(fVerbose){
     cout<<"Number of Packect: "<<packetNumber<<endl;
   }
-  AliTPCBuffer160 bufferFile(fDest,1);
+  AliAltroBuffer bufferFile(fDest,1);
   UInt_t k=0;
   UInt_t wordsRead=0; //number of read coded words 
   while(k<packetNumber){
@@ -1127,11 +1175,11 @@ void AliTPCCompression::ReadAltroFormat(char* fileOut,char* fileIn)const{
   //Time bin of the last amplitude sample in the bunch, amplitude values)
   //It is used mainly for debugging
   ofstream ftxt(fileOut);
-  AliTPCBuffer160 buff(fileIn,0);
+  AliAltroBuffer buff(fileIn,0);
   Int_t numWords,padNum,rowNum,secNum=0;
   Int_t value=0;
   if (fVerbose) cout<<"Creating a txt file from an Altro Format file"<<endl;
-  while(buff.ReadTrailerBackward(numWords,padNum,rowNum,secNum) !=-1 ){
+  while(buff.ReadTrailerBackward(numWords,padNum,rowNum,secNum)){
     ftxt<<"S:"<<secNum<<" R:"<<rowNum<<" P:"<<padNum<<" W:"<<numWords<<endl;
     if (numWords%4){
       for(Int_t j=0;j<(4-numWords%4);j++){