Added the USEPACKAGE option in Makefile.
[u/mrichter/AliRoot.git] / HLT / comp / AliL3Compress.cxx
index 1e6fa8e113f40fa6cca49e34cfb8b52fd778f218..53043b35ea3c2fe770325442bba77d691880d095 100644 (file)
@@ -15,6 +15,9 @@
 #include "AliL3ModelTrack.h"
 #include "AliL3Transform.h"
 #include "AliL3MemHandler.h"
+#ifdef use_aliroot
+#include "AliL3FileHandler.h"
+#endif
 #include "bitio.h"
 
 //_____________________________________________________________
@@ -28,21 +31,22 @@ ClassImp(AliL3Compress)
 AliL3Compress::AliL3Compress()
 {
   fTracks=0;
-  SetBitNumbers(7,7,10,4);
+  SetBitNumbers(0,0,0,0);
   fSlice =0;
   fPatch=0;
   fDigits=0;
   fDPt=0;
 }
 
-AliL3Compress::AliL3Compress(Int_t slice,Int_t patch,Int_t pad,Int_t time,Int_t charge,Int_t shape)
+AliL3Compress::AliL3Compress(Int_t slice,Int_t patch,Char_t *path)
 {
   fSlice=slice;
   fPatch=patch;
-  SetBitNumbers(pad,time,charge,shape);
+  SetBitNumbers(0,0,0,0);
   fTracks=0;
   fDigits=0;
   fDPt=0;
+  sprintf(fPath,"%s",path);
 }
 
 AliL3Compress::~AliL3Compress()
@@ -63,9 +67,11 @@ void AliL3Compress::SetBitNumbers(Int_t pad,Int_t time,Int_t charge,Int_t shape)
   fNumShapeBits=shape;
 }
 
-void AliL3Compress::WriteFile(AliL3TrackArray *tracks,Char_t *filename)
+void AliL3Compress::WriteFile(AliL3TrackArray *tracks)
 {
-  FILE *file = fopen(filename,"w");
+  Char_t fname[100];
+  sprintf(fname,"%s/tracks_m_%d_%d.raw",fPath,fSlice,fPatch);
+  FILE *file = fopen(fname,"w");
   Short_t ntracks = tracks->GetNTracks();
   //cout<<"Writing "<<ntracks<<" tracks to file"<<endl;
     
@@ -93,12 +99,25 @@ void AliL3Compress::WriteFile(AliL3TrackArray *tracks,Char_t *filename)
   fclose(file);
 }
 
-void AliL3Compress::ReadFile(Char_t *filename)
+void AliL3Compress::ReadFile(Char_t which)
 {
-  FILE *file = fopen(filename,"r");
+  //Read the trackfile.
+
+  Char_t fname[100];
+  if(which == 'm')
+    sprintf(fname,"%s/tracks_m_%d_%d.raw",fPath,fSlice,fPatch);
+  else if(which == 'u')
+    sprintf(fname,"%s/tracks_u_%d_%d.raw",fPath,fSlice,fPatch);
+  else
+    {
+      cerr<<"AliL3Compress::ReadFile() : Wrong option"<<endl;
+      return;
+    }
+
+  FILE *file = fopen(fname,"r");
   if(!file)
     {
-      cerr<<"Cannot open file "<<filename<<endl;
+      cerr<<"Cannot open file "<<fname<<endl;
       return;
     }
 
@@ -106,7 +125,7 @@ void AliL3Compress::ReadFile(Char_t *filename)
     delete fTracks;
   fTracks = new AliL3TrackArray("AliL3ModelTrack");
   
-  cout<<"Reading file "<<filename<<endl;
+  cout<<"Reading file "<<fname<<endl;
   while(!feof(file))
     {
       AliL3ModelTrack *track = (AliL3ModelTrack*)fTracks->NextTrack();
@@ -127,11 +146,25 @@ void AliL3Compress::ReadFile(Char_t *filename)
   fclose(file);
 }
 
-void AliL3Compress::CompressFile(Char_t *infile,Char_t *outfile)
+void AliL3Compress::CompressFile()
 {
+  if(fNumTimeBits==0)
+    {
+      cerr<<"AliL3Compress::CompressFile() : Bitnumbers not set"<<endl;
+      return;
+    }
   
-  BIT_FILE *output = OpenOutputBitFile(outfile);
-  FILE *input = fopen(infile,"r");
+  Char_t fname[100];
+  sprintf(fname,"%s/tracks_c_%d_%d.raw",fPath,fSlice,fPatch);
+  BIT_FILE *output = OpenOutputBitFile(fname);
+  
+  sprintf(fname,"%s/tracks_m_%d_%d.raw",fPath,fSlice,fPatch);
+  FILE *input = fopen(fname,"r");
+  if(!input)
+    {
+      cerr<<"AliL3Compress::CompressFile() : Error opening file: "<<fname<<endl;
+      return;
+    }
 
   AliL3TrackModel track;
   AliL3ClusterModel cluster;
@@ -146,6 +179,7 @@ void AliL3Compress::CompressFile(Char_t *infile,Char_t *outfile)
       
       if(output->mask != 0x80) //Write the current byte to file.
        {
+         //cerr<<"\nAliL3Compress::CompressFile() : Writing overhead bits!!!"<<endl;
          if(putc(output->rack,output->file )!=output->rack)
            cerr<<"AliL3Compress::ComressFile : Error writing to bitfile"<<endl;
          output->mask=0x80;
@@ -169,13 +203,13 @@ void AliL3Compress::CompressFile(Char_t *infile,Char_t *outfile)
            OutputBit(output,0);
          else
            OutputBit(output,1);
-         power = 1<<fNumTimeBits;
+         power = 1<<(fNumTimeBits-1);
          if(abs(temp)>=power)
            {
              timeo++;
              temp=power - 1;
            }
-         OutputBits(output,abs(temp),fNumTimeBits);
+         OutputBits(output,abs(temp),(fNumTimeBits-1));
          
          //Write pad information:
          temp = (Int_t)cluster.fDPad;
@@ -183,13 +217,13 @@ void AliL3Compress::CompressFile(Char_t *infile,Char_t *outfile)
            OutputBit(output,0);
          else
            OutputBit(output,1);
-         power = 1<<fNumPadBits;
+         power = 1<<(fNumPadBits-1);
          if(abs(temp)>=power)
            {
              pado++;
              temp=power - 1;
            }
-         OutputBits(output,abs(temp),fNumPadBits);
+         OutputBits(output,abs(temp),(fNumPadBits-1));
          
          //Write charge information:
          temp = (Int_t)cluster.fDCharge;
@@ -197,14 +231,14 @@ void AliL3Compress::CompressFile(Char_t *infile,Char_t *outfile)
            OutputBit(output,0);
          else
            OutputBit(output,1);
-         power = 1<<fNumChargeBits;
+         power = 1<<(fNumChargeBits-1);
          if(abs(temp)>=power)
            {
              chargeo++;
              temp=power - 1;
            }
-         OutputBits(output,abs(temp),fNumChargeBits);
-         
+         OutputBits(output,abs(temp),(fNumChargeBits-1));
+                 
          //Write shape information:
          temp = (Int_t)cluster.fDSigmaY2;
          power = 1<<fNumShapeBits;
@@ -235,10 +269,25 @@ void AliL3Compress::CompressFile(Char_t *infile,Char_t *outfile)
       <<"Shape "<<shapeo<<endl;
 }
 
-void AliL3Compress::ExpandFile(Char_t *infile,Char_t *outfile)
+void AliL3Compress::ExpandFile()
 {
-  BIT_FILE *input = OpenInputBitFile(infile);
-  FILE *output = fopen(outfile,"w");
+  if(fNumTimeBits==0)
+    {
+      cerr<<"AliL3Compress::ExpandFile() : Bitnumbers not set"<<endl;
+      return;
+    }
+  
+  Char_t fname[100];
+  sprintf(fname,"%s/tracks_c_%d_%d.raw",fPath,fSlice,fPatch);
+  BIT_FILE *input = OpenInputBitFile(fname);
+  
+  sprintf(fname,"%s/tracks_u_%d_%d.raw",fPath,fSlice,fPatch);
+  FILE *output = fopen(fname,"w");
+  if(!output)
+    {
+      cerr<<"AliL3Compress::ExpandFile() : Error opening file: "<<fname<<endl;
+      return;
+    }
 
   AliL3TrackModel trackmodel;
   AliL3ClusterModel *clusters=0;
@@ -268,21 +317,21 @@ void AliL3Compress::ExpandFile(Char_t *infile,Char_t *outfile)
          
          //Read time information:
          sign=InputBit(input);
-         temp = InputBits(input,fNumTimeBits);
+         temp = InputBits(input,(fNumTimeBits-1));
          if(!sign)
            temp*=-1;
          clusters[i].fDTime = temp;
          
          //Read pad information:
          sign=InputBit(input);
-         temp = InputBits(input,fNumPadBits);
+         temp = InputBits(input,(fNumPadBits-1));
          if(!sign)
            temp*=-1;
          clusters[i].fDPad = temp;
          
          //Read charge information:
          sign = InputBit(input);
-         temp=InputBits(input,fNumChargeBits);
+         temp=InputBits(input,(fNumChargeBits-1));
          if(!sign)
            temp*=-1;
          clusters[i].fDCharge = temp;
@@ -317,11 +366,11 @@ void AliL3Compress::CreateDigitArray(Int_t maxnumber)
   fDPt = new AliL3RandomDigitData*[maxnumber];
 }
 
-void AliL3Compress::RestoreData(Char_t *uncompfile)
+void AliL3Compress::RestoreData()
 {
   
   //Read the uncompressed file:
-  ReadFile(uncompfile);
+  ReadFile('u');
   
   CreateDigitArray(100000);
   
@@ -329,6 +378,7 @@ void AliL3Compress::RestoreData(Char_t *uncompfile)
   Int_t charge;
   for(Int_t j=NRows[fPatch][0]; j<=NRows[fPatch][1]; j++)
     {
+      cout<<"Building clusters on row "<<j<<endl;
       for(Int_t i=0; i<fTracks->GetNTracks(); i++)
        {
          AliL3ModelTrack *track = (AliL3ModelTrack*)fTracks->GetCheckedTrack(i);
@@ -362,37 +412,50 @@ void AliL3Compress::PrintDigits()
     }
 }
 
-void AliL3Compress::WriteRestoredData(Char_t *remainfile,Char_t *restoredfile)
+void AliL3Compress::WriteRestoredData()
 {
+  Char_t fname[100];
+  
   //Get the remaining raw data array:
   AliL3MemHandler *mem = new AliL3MemHandler();
-  mem->SetBinaryInput(remainfile);
+  sprintf(fname,"%s/remains_%d_%d.raw",fPath,fSlice,fPatch);
+  mem->SetBinaryInput(fname);
   UInt_t numdigits;
   AliL3DigitRowData *origRow = mem->CompBinary2Memory(numdigits);
   mem->CloseBinaryInput();
   
   //Allocate memory for the merged data:
   UInt_t size = mem->GetAllocatedSize() + fNDigits*sizeof(AliL3DigitData);
+  cout<<"Allocating "<<size<<" bytes for merged data array "<<endl;
   Byte_t *data = new Byte_t[size];
   memset(data,0,size);
   AliL3DigitRowData *tempRow = (AliL3DigitRowData*)data;
 
   Int_t ndigits,action,charge;
   UShort_t pad,time;
+      
+  UInt_t digit_counter;
+  Int_t row_counter=0;
   for(Int_t i=NRows[fPatch][0]; i<=NRows[fPatch][1]; i++)
     {
       tempRow->fRow = i;
       ndigits=0;
       AliL3DigitData *origDig = origRow->fDigitData;
       AliL3DigitData *tempDig = tempRow->fDigitData;
+      if((Int_t)origRow->fRow != i)
+       cerr<<"AliL3Compress::WriteRestoredData() : Mismatching row numbering "<<(Int_t)origRow->fRow<<" "<<i<<endl;
+
+      //cout<<"Writing row "<<i<<" with "<<(Int_t)origRow->fNDigit<<" old digits"<<endl;
+      digit_counter=0;
       
       while(1)
        {
-         for(UInt_t j=0; j<origRow->fNDigit; j++)
+         while(digit_counter < origRow->fNDigit)
            {
-             pad = origDig[j].fPad;
-             time = origDig[j].fTime;
-             charge = origDig[j].fCharge;
+             pad = origDig[digit_counter].fPad;
+             time = origDig[digit_counter].fTime;
+             charge = origDig[digit_counter].fCharge;
+             digit_counter++;
              while((action=ComparePoints(i,pad,time)) == 1)
                {
                  tempDig[ndigits].fPad = fDPt[fNUsed]->fPad;
@@ -400,6 +463,7 @@ void AliL3Compress::WriteRestoredData(Char_t *remainfile,Char_t *restoredfile)
                  tempDig[ndigits].fCharge = fDPt[fNUsed]->fCharge;
                  ndigits++;
                  fNUsed++;
+
                }
              if(action == 0)
                {
@@ -409,7 +473,9 @@ void AliL3Compress::WriteRestoredData(Char_t *remainfile,Char_t *restoredfile)
                  ndigits++;
                }
            }
-         if(fNUsed >= fNDigits) break;
+         
+         if(fNUsed >= fNDigits) 
+           break;
          if(fDPt[fNUsed]->fRow != i) //we are on a new row
            break;
          tempDig[ndigits].fPad = fDPt[fNUsed]->fPad;
@@ -418,6 +484,12 @@ void AliL3Compress::WriteRestoredData(Char_t *remainfile,Char_t *restoredfile)
          ndigits++;
          fNUsed++;
        }
+      //cout<<"Writing "<<ndigits<<" digits on row "<<i<<endl;
+      if(ndigits < 4)
+       {
+         row_counter++;
+         cout<<"Few digits on row "<<i<<endl;
+       }
       tempRow->fNDigit = ndigits;
       Int_t size = sizeof(AliL3DigitData)*tempRow->fNDigit + sizeof(AliL3DigitRowData);
       Byte_t *byte_pt = (Byte_t*)tempRow;
@@ -426,13 +498,18 @@ void AliL3Compress::WriteRestoredData(Char_t *remainfile,Char_t *restoredfile)
       mem->UpdateRowPointer(origRow);
     }
   
-  mem->Free();
-  mem->SetBinaryOutput(restoredfile);
+  if(row_counter != NumRows[fPatch])
+    cerr<<"AliL3Compress::WriteRestoredData() : Written rows: "<<row_counter<<" total rows "<<NumRows[fPatch]<<endl;
+  
+  mem->Free();  
+  sprintf(fname,"%s/restored_%d_%d.raw",fPath,fSlice,fPatch);
+  mem->SetBinaryOutput(fname);
   mem->Memory2CompBinary((UInt_t)NumRows[fPatch],(AliL3DigitRowData*)data);
   mem->CloseBinaryOutput();
   
   delete [] data;
   delete mem;
+  
 }
 
 void AliL3Compress::CreateDigits(Int_t row,Float_t pad,Float_t time,Int_t charge,Float_t sigmaY2,Float_t sigmaZ2)
@@ -442,7 +519,7 @@ void AliL3Compress::CreateDigits(Int_t row,Float_t pad,Float_t time,Int_t charge
   AliL3Transform *tr = new AliL3Transform();
   TRandom *random = new TRandom();
   
-  Int_t entries=10000;
+  Int_t entries=1000;
   TH1F *hist1 = new TH1F("hist1","",tr->GetNPads(row),0,tr->GetNPads(row)-1);
   TH1F *hist2 = new TH1F("hist2","",tr->GetNTimeBins(),0,tr->GetNTimeBins()-1);
   TH2F *hist3 = new TH2F("hist3","",tr->GetNPads(row),0,tr->GetNPads(row)-1,tr->GetNTimeBins(),0,tr->GetNTimeBins()-1);
@@ -469,7 +546,8 @@ void AliL3Compress::CreateDigits(Int_t row,Float_t pad,Float_t time,Int_t charge
   
   if(sigmaY2 <= 0 || sigmaZ2 <= 0)
     {
-      cerr<<"AliL3Compress::CreateDigits() : Wrong sigmas : "<<sigmaY2<<" "<<sigmaZ2<<endl;
+      cerr<<"AliL3Compress::CreateDigits() : Wrong sigmas : "<<sigmaY2<<" "<<sigmaZ2;
+      cerr<<" on row "<<row<<" pad "<<pad<<" time "<<time<<endl;
       return;
     }
   
@@ -532,6 +610,58 @@ void AliL3Compress::CreateDigits(Int_t row,Float_t pad,Float_t time,Int_t charge
   delete tr;
 }
 
+void AliL3Compress::PrintCompRatio()
+{
+  Char_t fname[100];
+  sprintf(fname,"%s/remains_%d_%d.raw",fPath,fSlice,fPatch);
+  AliL3MemHandler *mem = new AliL3MemHandler();
+  if(!mem->SetBinaryInput(fname))
+    {
+      cerr<<"AliL3Compress::PrintCompRatio(): Error opening file: "<<fname<<endl;
+      return;
+    } 
+  UInt_t ndigits;
+  AliL3DigitRowData *rowPt = (AliL3DigitRowData*)mem->CompBinary2Memory(ndigits);
+  mem->CloseBinaryInput();
+
+  Int_t digit_counter=0;
+  for(Int_t i=NRows[fPatch][0]; i<=NRows[fPatch][1]; i++)
+    {
+      digit_counter += rowPt->fNDigit;
+      mem->UpdateRowPointer(rowPt);
+    }
+  delete mem;
+  
+  sprintf(fname,"%s/tracks_c_%d_%d.raw",fPath,fSlice,fPatch);
+  FILE *file1 = fopen(fname,"r");
+  if(!file1)
+    {
+      cerr<<"AliL3Compress::PrintCompRatio(): Error opening file: "<<fname<<endl;
+      return;
+    }
+  fseek(file1,0,SEEK_END);
+  UInt_t filesize1 = (UInt_t)ftell(file1);
+  fclose(file1);
+  
+  sprintf(fname,"%s/digits_%d_%d.raw",fPath,fSlice,fPatch);
+  FILE *file2 = fopen(fname,"r");
+  if(!file2)
+    {
+      cerr<<"AliL3Compress::PrintCompRatio(): Error opening file: "<<fname<<endl;
+      return;
+    }
+  fseek(file2,0,SEEK_END);
+  UInt_t filesize2 = (UInt_t)ftell(file2);
+  fclose(file2);
+  
+  cout<<"----------------------"<<endl;
+  cout<<"Original file size   : "<<filesize2<<endl;
+  cout<<"Compressed file size : "<<filesize1<<endl;
+  cout<<"Remaining digits     : "<<digit_counter<<endl;
+  cout<<"Compression ratio    : "<<(Float_t)(filesize1 + (10*digit_counter)/8)/(Float_t)(filesize2)<<endl;
+  
+}
+
 void AliL3Compress::QSort(AliL3RandomDigitData **a, Int_t first, Int_t last)
 {
   
@@ -574,3 +704,31 @@ void AliL3Compress::QSort(AliL3RandomDigitData **a, Int_t first, Int_t last)
     }
   }
 }
+
+void AliL3Compress::WriteRootFile(Char_t *digitsfile,Char_t *rootfile)
+{
+#ifdef use_aliroot
+  Char_t fname[100];
+  AliL3MemHandler *mem = new AliL3MemHandler();
+  sprintf(fname,"%s/restored_%d_%d.raw",fPath,fSlice,fPatch);
+  mem->SetBinaryInput(fname);
+  UInt_t ndigits;
+  AliL3DigitRowData *rowPt = (AliL3DigitRowData*)mem->CompBinary2Memory(ndigits);
+  mem->CloseBinaryInput();
+
+  AliL3FileHandler *file = new AliL3FileHandler();
+  if(!file->SetAliInput(digitsfile))
+    {
+      cerr<<"AliL3Compress::WriteRootFile() : Error opening file: "<<digitsfile<<endl;
+      return;
+    }
+  file->Init(fSlice,fPatch,NRows[fPatch]);
+  file->AliDigits2RootFile(rowPt,rootfile);
+  file->CloseAliInput();
+
+  delete mem;
+  delete file;
+#endif
+
+  return;
+}