]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TPC/AliTPCDDLRawData.cxx
Renaming AliTPCBuffer160 and AliAltroBuffer (T.Kuhr)
[u/mrichter/AliRoot.git] / TPC / AliTPCDDLRawData.cxx
index c362bdf5dd873c57707968211501fa676de21171..6df02cdbebb554199edd28ba18a2ea33a2ba14d2 100644 (file)
  * about the suitability of this software for any purpose. It is          *
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
+/* $Id$ */
 
-#include "TObjArray.h"
-#include "Riostream.h"
+
+//This class conteins all the methods to create raw data 
+//as par a given DDL.
+//It produces DDL with both compressed and uncompressed format.
+//For compression we use the optimized table wich needs 
+//to be provided.
+
+#include <TObjArray.h>
+#include <Riostream.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include "AliTPCCompression.h"
-#include "AliTPCBuffer160.h"
+#include "AliAltroBuffer.h"
 #include "AliTPCDDLRawData.h"
-#include "TFile.h"
-#include "TTree.h"
-
-
+#include "AliRawDataHeader.h"
 
 ClassImp(AliTPCDDLRawData)
 ////////////////////////////////////////////////////////////////////////////////////////
 
-AliTPCDDLRawData::AliTPCDDLRawData(const AliTPCDDLRawData &source){
+AliTPCDDLRawData::AliTPCDDLRawData(const AliTPCDDLRawData &source):
+  TObject(source)
+{
   // Copy Constructor
+  fVerbose=source.fVerbose;
   return;
 }
 
 AliTPCDDLRawData& AliTPCDDLRawData::operator=(const AliTPCDDLRawData &source){
   //Assigment operator
+  fVerbose=source.fVerbose;
   return *this;
 }
 
 
 ////////////////////////////////////////////////////////////////////////////
-void AliTPCDDLRawData::RawData(Int_t LDCsNumber){
+void AliTPCDDLRawData::RawData(const char* inputFileName){
+  //Raw data generation
   //Number of DDL=2*36+4*36=216
   //2 DDL for each inner sector
   //4 DDL for each outer sector
-  Int_t DDLPerFile=216/LDCsNumber;
   Int_t offset=1;
-  if (216%LDCsNumber) DDLPerFile++;
-  cout<<"Number of DDL per slide: "<<DDLPerFile<<endl;
   ifstream f;
-  f.open("AliTPCDDL.dat",ios::binary);
-  if(!f){cout<<"File doesn't exist !!"<<endl;return;}
+#ifndef __DECCXX
+  f.open(inputFileName,ios::binary);
+#else
+  f.open(inputFileName);
+#endif
+  if(!f){Error("RawData", "File doesn't exist !!");return;}
   struct DataPad{
     Int_t Sec;
     Int_t SubSec;
@@ -61,196 +72,214 @@ void AliTPCDDLRawData::RawData(Int_t LDCsNumber){
   };
   DataPad data;
 
-  //AliTPCBuffer160 is used in write mode to generate AltroFormat.dat file
-  Int_t SliceNumber=1;
+  //AliAltroBuffer is used in write mode to generate raw data file
   char  filename[15];
-  sprintf(filename,"TPCslice%d",SliceNumber); 
-  cout<<"   Creating "<<filename<<endl;
-  AliTPCBuffer160 *Buffer=new AliTPCBuffer160(filename,1);
-
-  ULong_t Count=0;
-  Int_t PSecNumber=-1;  //Previous Sector number
-  Int_t PRowNumber=-1;  //Previous Row number  
-  Int_t PPadNumber=-1;  //Previous Pad number
-  Int_t PTimeBin=-1;    //Previous Time-Bin
-  Int_t PSubSector=-1;  //Previous Sub Sector
-  Int_t BunchLength=0;
-  Int_t CountDDL=0;
+  Int_t ddlNumber=0;
+  AliAltroBuffer *buffer=NULL;
+  Int_t pSecNumber=-1;  //Previous Sector number
+  Int_t pRowNumber=-1;  //Previous Row number  
+  Int_t pPadNumber=-1;  //Previous Pad number
+  Int_t pTimeBin=-1;    //Previous Time-Bin
+  Int_t pSubSector=-1;  //Previous Sub Sector
+  Int_t bunchLength=0;
   Int_t nwords=0;
-  ULong_t numPackets=0;
+  UInt_t numPackets=0;
+
   while (f.read((char*)(&data),sizeof(data))){
-    Count++;
-    if (PPadNumber==-1){
-      PSecNumber=data.Sec;
-      PRowNumber=data.Row;
-      PPadNumber=data.Pad;
-      PTimeBin=data.Time;
-      PSubSector=data.SubSec;
+    if (pPadNumber==-1){
+      pSecNumber=data.Sec;
+      pRowNumber=data.Row;
+      pPadNumber=data.Pad;
+      pTimeBin=data.Time;
+      pSubSector=data.SubSec;
+
+      if(data.Sec<36)
+       ddlNumber=data.Sec*2+data.SubSec;
+      else
+       ddlNumber=72+(data.Sec-36)*4+data.SubSec;
+      sprintf(filename,"TPC_%d.ddl",ddlNumber+kDDLOffset); 
+      buffer=new AliAltroBuffer(filename,1);
       //size magic word sector number sub-sector number 0 for TPC 0 for uncompressed
-      Buffer->WriteMiniHeader(0,PSecNumber,PSubSector,0,0);//Dummy;
-      BunchLength=1;
-      Buffer->FillBuffer(data.Dig-offset);
+      buffer->WriteDataHeader(kTRUE,kFALSE);//Dummy;
+      bunchLength=1;
+      buffer->FillBuffer(data.Dig-offset);
       nwords++;
     }//end if
     else{
-      if ( (data.Time==(PTimeBin+1)) &&
-          (PPadNumber==data.Pad) &&
-          (PRowNumber==data.Row) &&
-          (PSecNumber==data.Sec)){
-       BunchLength++;
+      if ( (data.Time==(pTimeBin+1)) &&
+          (pPadNumber==data.Pad) &&
+          (pRowNumber==data.Row) &&
+          (pSecNumber==data.Sec)){
+       bunchLength++;
       }//end if
       else{
-       Buffer->FillBuffer(PTimeBin);
-       Buffer->FillBuffer(BunchLength+2);
+       buffer->FillBuffer(pTimeBin);
+       buffer->FillBuffer(bunchLength+2);
        nwords+=2;
-       if ((PPadNumber!=data.Pad)||(PRowNumber!=data.Row)||(PSecNumber!=data.Sec)){
+       if ((pPadNumber!=data.Pad)||(pRowNumber!=data.Row)||(pSecNumber!=data.Sec)){
          //Trailer is formatted and inserted!!
-         Buffer->WriteTrailer(nwords,PPadNumber,PRowNumber,PSecNumber);
+         buffer->WriteTrailer(nwords,pPadNumber,pRowNumber,pSecNumber);
          numPackets++;
          nwords=0;
 
-         if(PSubSector!=data.SubSec){
-           CountDDL++;
-           if(CountDDL==(DDLPerFile+1)){
-             //size magic word sector number sub-sector number 0 for TPC 0 for uncompressed
-             Buffer->Flush();
-             Buffer->WriteMiniHeader(1,PSecNumber,PSubSector,0,0);
-             //cout<<"Mini header for DDL:"<<PSecNumber<<" Sub-sec:"<<PSubSector<<endl;
-             delete Buffer;
-             SliceNumber++;
-             sprintf(filename,"TPCslice%d",SliceNumber);
-             cout<<"   Creating "<<filename<<endl;
-             Buffer=new AliTPCBuffer160(filename,1);
-             Buffer->WriteMiniHeader(0,data.Sec,data.SubSec,0,0);//Dummy;
-             CountDDL=1;
-           }//end if
-           else{
-             Buffer->Flush();
-             Buffer->WriteMiniHeader(1,PSecNumber,PSubSector,0,0);
-             Buffer->WriteMiniHeader(0,data.Sec,data.SubSec,0,0);//Dummy;
-           }
-           PSubSector=data.SubSec;
+         if(pSubSector!=data.SubSec){
+           //size magic word sector number sub-sector number 0 for TPC 0 for uncompressed
+           buffer->Flush();
+           buffer->WriteDataHeader(kFALSE,kFALSE);
+           //cout<<"Data header for DDL:"<<PSecNumber<<" Sub-sec:"<<PSubSector<<endl;
+           delete buffer;
+
+           if(data.Sec<36)
+             ddlNumber=data.Sec*2+data.SubSec;
+           else
+             ddlNumber=72+(data.Sec-36)*4+data.SubSec;
+           sprintf(filename,"TPC_%d.ddl",ddlNumber+kDDLOffset); 
+           buffer=new AliAltroBuffer(filename,1);
+           buffer->WriteDataHeader(kTRUE,kFALSE);//Dummy;
+           pSubSector=data.SubSec;
          }//end if
-         
        }//end if
        
-       BunchLength=1;
-       PPadNumber=data.Pad;
-       PRowNumber=data.Row;
-       PSecNumber=data.Sec;
+       bunchLength=1;
+       pPadNumber=data.Pad;
+       pRowNumber=data.Row;
+       pSecNumber=data.Sec;
       }//end else
-      PTimeBin=data.Time;
-      Buffer->FillBuffer(data.Dig-offset);
+      pTimeBin=data.Time;
+      buffer->FillBuffer(data.Dig-offset);
       nwords++;
     }//end else
   }//end while
-  Buffer->FillBuffer(PTimeBin);
-  Buffer->FillBuffer(BunchLength+2);
-  nwords+=2;
-  Buffer->WriteTrailer(nwords,PPadNumber,PRowNumber,PSecNumber);
-  //write the  M.H.
-  Buffer->Flush();
-  Buffer->WriteMiniHeader(1,PSecNumber,PSubSector,0,0);
-  //cout<<"Mini header for D D L:"<<PSecNumber<<" Sub-sec:"<<PSubSector<<endl;
-  delete Buffer;
-  cout<<"Number of digits: "<<Count<<endl;
+  if (buffer) {
+    buffer->FillBuffer(pTimeBin);
+    buffer->FillBuffer(bunchLength+2);
+    nwords+=2;
+    buffer->WriteTrailer(nwords,pPadNumber,pRowNumber,pSecNumber);
+    //write the  D.H.
+    buffer->Flush();
+    buffer->WriteDataHeader(kFALSE,kFALSE);
+    //cout<<"Data header for D D L:"<<pSecNumber<<" Sub-sec:"<<pSubSector<<endl;
+    delete buffer;
+  }
   f.close();
   return;
 }
 ////////////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////////////
-//This method is used to Compress and decompress the slides
 
-Int_t AliTPCDDLRawData::RawDataCompDecompress(Int_t LDCsNumber,Int_t Comp){
-  static const Int_t NumTable=5;
+
+Int_t AliTPCDDLRawData::RawDataCompDecompress(Bool_t compress){
+  //This method is used to compress and decompress the slides
+  static const Int_t kNumTables=5;
   char filename[20];
-  char dest[20];
   fstream f;
-  ULong_t Size=0;
-  //Int_t MagicWord,DDLNumber,SecNumber,SubSector,Detector;
-  Int_t Flag=0;
-  for(Int_t i=1;i<=LDCsNumber;i++){
-    if(!Comp){
-      sprintf(filename,"TPCslice%d",i);
-      sprintf(dest,"TPCslice%d.comp",i);
-    }
-    else{
-      sprintf(filename,"TPCslice%d.comp",i);
-      sprintf(dest,"TPCslice%d.decomp",i);
-    }
+  UInt_t size=0;
+  AliTPCCompression util;
+  util.SetVerbose(0);
+
+  for(Int_t i=0;i<216;i++){
+    sprintf(filename,"TPC_%d.ddl",i+kDDLOffset);
+#ifndef __DECCXX
     f.open(filename,ios::binary|ios::in);
-    if(!f){cout<<"File doesn't exist \n";exit(1);}
-    cout<<filename<<"  "<<dest<<endl;
+#else
+    f.open(filename,ios::in);
+#endif
+#if defined(__HP_aCC) || defined(__DECCXX)
+    if(!f.rdbuf()->is_open()){f.clear(); continue;}
+#else
+    if(!f.is_open()){f.clear(); continue;}
+#endif
+    if (fVerbose)
+      Info("RawDataCompDecompress", "&s -> dest.ddl", filename);
     ofstream fdest;
-    fdest.open(dest,ios::binary);
+#ifndef __DECCXX
+    fdest.open("dest.ddl",ios::binary);
+#else
+    fdest.open("dest.ddl");
+#endif
     //loop over the DDL block 
-    //Each block contains a Mini Header followed by raw data (ALTRO FORMAT)
+    //Each block contains a Data Header followed by raw data (ALTRO FORMAT)
     //The number of block is ceil(216/LDCsNumber)
-    ULong_t MiniHeader[3];
-    //here the Mini Header is read
-    while( (f.read((char*)(MiniHeader),sizeof(ULong_t)*3)) ){
-      Size=MiniHeader[0];
-      //Int_t dim=sizeof(ULong_t)+sizeof(Int_t)*5;
-      //cout<<" Sec "<<SecNumber<<" SubSector "<<SubSector<<" Size "<<Size<<endl;
+    AliRawDataHeader header;
+    //here the Data Header is read
+    while( f.read((char*)(&header),sizeof(header)) ){
+      size=header.fSize-sizeof(header);
+      // cout<<"Data size:"<<size<<endl;
+      //Int_t dim=sizeof(UInt_t)+sizeof(Int_t)*5;
+      //cout<<" Sec "<<SecNumber<<" SubSector "<<SubSector<<" size "<<size<<endl;
+      Bool_t compressed = header.TestAttribute(1);
+      if ((compressed && compress) ||
+         (!compressed && !compress)) continue;
       //open the temporay File
       ofstream fo;
       char temp[15]="TempFile";
+#ifndef __DECCXX
       fo.open(temp,ios::binary);
+#else
+      fo.open(temp);
+#endif
       Int_t car=0;
-      for(ULong_t j=0;j<Size;j++){
+      for(UInt_t j=0;j<size;j++){
        f.read((char*)(&car),1);
        fo.write((char*)(&car),1);
       }//end for
       fo.close();
       //The temp file is compressed or decompressed
-      AliTPCCompression *util = new AliTPCCompression();
-      if(!Comp)
-       util->CompressDataOptTables(NumTable,temp,"TempCompDecomp");
+      Int_t result=0;
+      if(compress){
+       result=util.CompressDataOptTables(kNumTables,temp,"TempCompDecomp");
+      }
       else
-       util->DecompressDataOptTables(NumTable,temp,"TempCompDecomp");
-      delete util;
+       result=util.DecompressDataOptTables(kNumTables,temp,"TempCompDecomp");
+      if (result != 0) break;
       //the temp compressed file is open and copied to the final file fdest
       ifstream fi;
+#ifndef __DECCXX
       fi.open("TempCompDecomp",ios::binary);
+#else
+      fi.open("TempCompDecomp");
+#endif
       fi.seekg(0,ios::end);
-      Size=fi.tellg();
+      size=fi.tellg();
       fi.seekg(0);
-      //The Mini Header is updated (Size and Compressed flag) 
+      //The Data Header is updated (size and Compressed flag) 
       //and written into the output file
-      MiniHeader[0]=Size;
-      if(!Comp)
-       Flag=1;
-      else
-       Flag=0;
-      ULong_t aux=0xFFFF;
-      aux<<=16;
-      aux|=Flag;
-      aux|=0xFF;
-      MiniHeader[2]=MiniHeader[2]&aux;
-      fdest.write((char*)(MiniHeader),sizeof(ULong_t)*3);
+      header.fSize=size+sizeof(header);
+      if (compress) header.SetAttribute(1);
+      else header.ResetAttribute(1);
+      fdest.write((char*)(&header),sizeof(header));
       //The compressem temp file is copied into the output file fdest
-      for(ULong_t j=0;j<Size;j++){
+      for(UInt_t j=0;j<size;j++){
        fi.read((char*)(&car),1);
        fdest.write((char*)(&car),1);
       }//end for
       fi.close();
     }//end while
+    f.clear();
     f.close();
     fdest.close();
     remove("TempFile");
     remove("TempCompDecomp");
+    rename("dest.ddl",filename);
   }//end for
   return 0;
 }
 
 /////////////////////////////////////////////////////////////////////////////////
-//This method is used to build the Altro format from AliTPCDDL.dat
-//It is used to debug the code and create the tables used in the compresseion phase
-void AliTPCDDLRawData::RawDataAltro(){
+void AliTPCDDLRawData::RawDataAltro(const char* inputFileName, const char* outputFileName)const{
+  //This method is used to build the Altro format from AliTPCDDL.dat
+  //It is used to debug the code and creates the tables used in the compresseion phase
   Int_t offset=1;
   ifstream f;
-  f.open("AliTPCDDL.dat",ios::binary);
-  if(!f){cout<<"File doesn't exist !!"<<endl;return;}
+#ifndef __DECCXX
+  f.open(inputFileName,ios::binary);
+#else
+  f.open(inputFileName);
+#endif
+  if(!f){
+    Error("RawDataAltro", "File doesn't exist !!");
+    return;
+  }
   struct DataPad{
     Int_t Sec;
     Int_t SubSec;
@@ -261,105 +290,103 @@ void AliTPCDDLRawData::RawDataAltro(){
   };
   DataPad data;
 
-  //AliTPCBuffer160 is used in write mode to generate AltroFormat.dat file
-  char  filename[30]="AltroFormatDDL.dat";
-  cout<<"   Creating "<<filename<<endl;
-  AliTPCBuffer160 *Buffer=new AliTPCBuffer160(filename,1);
+  //AliAltroBuffer is used in write mode to generate AltroFormat.dat file
+  Info("RawDataAltro", "Creating &s", outputFileName);
+  AliAltroBuffer *buffer=new AliAltroBuffer(outputFileName,1);
 
-  ULong_t Count=0;
-  Int_t PSecNumber=-1;  //Previous Sector number
-  Int_t PRowNumber=-1;  //Previous Row number  
-  Int_t PPadNumber=-1;  //Previous Pad number
-  Int_t PTimeBin=-1;    //Previous Time-Bin
-  Int_t BunchLength=0;
+  UInt_t count=0;
+  Int_t pSecNumber=-1;  //Previous Sector number
+  Int_t pRowNumber=-1;  //Previous Row number  
+  Int_t pPadNumber=-1;  //Previous Pad number
+  Int_t pTimeBin=-1;    //Previous Time-Bin
+  Int_t bunchLength=0;
   Int_t nwords=0;
-  ULong_t numPackets=0;
+  UInt_t numPackets=0;
   while (f.read((char*)(&data),sizeof(data))){
-    Count++;
-    if (PPadNumber==-1){
-      PSecNumber=data.Sec;
-      PRowNumber=data.Row;
-      PPadNumber=data.Pad;
-      PTimeBin=data.Time;
-      BunchLength=1;
-      Buffer->FillBuffer(data.Dig-offset);
+    count++;
+    if (pPadNumber==-1){
+      pSecNumber=data.Sec;
+      pRowNumber=data.Row;
+      pPadNumber=data.Pad;
+      pTimeBin=data.Time;
+      bunchLength=1;
+      buffer->FillBuffer(data.Dig-offset);
       nwords++;
     }//end if
     else{
-      if ( (data.Time==(PTimeBin+1)) &&
-          (PPadNumber==data.Pad) &&
-          (PRowNumber==data.Row) &&
-          (PSecNumber==data.Sec)){
-       BunchLength++;
+      if ( (data.Time==(pTimeBin+1)) &&
+          (pPadNumber==data.Pad) &&
+          (pRowNumber==data.Row) &&
+          (pSecNumber==data.Sec)){
+       bunchLength++;
       }//end if
       else{
-       Buffer->FillBuffer(PTimeBin);
-       Buffer->FillBuffer(BunchLength+2);
+       buffer->FillBuffer(pTimeBin);
+       buffer->FillBuffer(bunchLength+2);
        nwords+=2;
-       if ((PPadNumber!=data.Pad)||(PRowNumber!=data.Row)||(PSecNumber!=data.Sec)){
+       if ((pPadNumber!=data.Pad)||(pRowNumber!=data.Row)||(pSecNumber!=data.Sec)){
          //Trailer is formatted and inserted!!
-         Buffer->WriteTrailer(nwords,PPadNumber,PRowNumber,PSecNumber);
+         buffer->WriteTrailer(nwords,pPadNumber,pRowNumber,pSecNumber);
          numPackets++;
          nwords=0;
        }//end if
        
-       BunchLength=1;
-       PPadNumber=data.Pad;
-       PRowNumber=data.Row;
-       PSecNumber=data.Sec;
+       bunchLength=1;
+       pPadNumber=data.Pad;
+       pRowNumber=data.Row;
+       pSecNumber=data.Sec;
       }//end else
-      PTimeBin=data.Time;
-      Buffer->FillBuffer(data.Dig-offset);
+      pTimeBin=data.Time;
+      buffer->FillBuffer(data.Dig-offset);
       nwords++;
     }//end else
   }//end while
-  Buffer->FillBuffer(PTimeBin);
-  Buffer->FillBuffer(BunchLength+2);
+  buffer->FillBuffer(pTimeBin);
+  buffer->FillBuffer(bunchLength+2);
   nwords+=2;
-  Buffer->WriteTrailer(nwords,PPadNumber,PRowNumber,PSecNumber);
-  delete Buffer;
-  cout<<"Number of digits: "<<Count<<endl;
+  buffer->WriteTrailer(nwords,pPadNumber,pRowNumber,pSecNumber);
+  delete buffer;
+  Info("RawDataAltro", "Number of digits: %d", count);
   f.close(); 
   return;
 }
 
-
-void AliTPCDDLRawData::RawDataAltroDecode(Int_t LDCsNumber,Int_t Comp){
+/////////////////////////////////////////////////////////////////////////
+void AliTPCDDLRawData::RawDataAltroDecode(const char* outputFileName){
+  //This method merges the slides in only one file removing at the same 
+  //time all the data headers. The file so obtained must be Altro format
+  //complaiant.
+  //It is used mainly in the debugging phase 
   char filename[15];
-  char dest[30];
   fstream f;
-  if(!Comp)
-    sprintf(dest,"AltroDDLRecomposed.dat");
-  else
-    sprintf(dest,"AltroDDLRecomposedDec.dat");
   ofstream fdest;
-  fdest.open(dest,ios::binary);
-  
-  ULong_t Size=0;
-  //Int_t MagicWord,DDLNumber,SecNumber,SubSector,Detector,Flag=0;
-  for(Int_t i=1;i<=LDCsNumber;i++){
-     if(!Comp)
-      sprintf(filename,"TPCslice%d",i);  
-    else
-      sprintf(filename,"TPCslice%d.decomp",i);  
-    
+
+#ifndef __DECCXX
+  fdest.open(outputFileName,ios::binary);
+#else
+  fdest.open(outputFileName);
+#endif
+  UInt_t size=0;
+  AliRawDataHeader header;
+  for(Int_t i=0;i<216;i++){
+    sprintf(filename,"TPC_%d.ddl",i+kDDLOffset);
+#ifndef __DECCXX
     f.open(filename,ios::binary|ios::in);
-    if(!f){exit(1);}
+#else
+    f.open(filename,ios::in);
+#endif
+    if(!f)continue;
     //loop over the DDL block 
-    //Each block contains a Mini Header followed by raw data (ALTRO FORMAT)
-    //The number of block is ceil(216/LDCsNumber)
-    ULong_t MiniHeader[3];
-    //here the Mini Header is read
-    //    for(Int_t j=0;j<3;j++)MiniHeader[j]=0;
-    while( (f.read((char*)(MiniHeader),sizeof(ULong_t)*3)) ){
-      //cout<<"Mini header dimension "<<MiniHeader[0]<<endl;
+    //Each block contains a Data Header followed by raw data (ALTRO FORMAT)
+    while( (f.read((char*)(&header),sizeof(header))) ){
       Int_t car=0;
-      Size=MiniHeader[0];
-      for(ULong_t j=0;j<Size;j++){
+      size=header.fSize-sizeof(header);
+      for(UInt_t j=0;j<size;j++){
        f.read((char*)(&car),1);
        fdest.write((char*)(&car),1);
       }//end for
     }//end while
+    f.clear();
     f.close();
   }//end for
   fdest.close();