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 **************************************************************************/
16 #include "TObjArray.h"
17 #include "Riostream.h"
20 #include "AliTPCCompression.h"
21 #include "AliTPCBuffer160.h"
22 #include "AliTPCDDLRawData.h"
24 ClassImp(AliTPCDDLRawData)
25 ////////////////////////////////////////////////////////////////////////////////////////
27 AliTPCDDLRawData::AliTPCDDLRawData(const AliTPCDDLRawData &source){
32 AliTPCDDLRawData& AliTPCDDLRawData::operator=(const AliTPCDDLRawData &source){
38 ////////////////////////////////////////////////////////////////////////////
39 void AliTPCDDLRawData::RawData(Int_t LDCsNumber){
40 //Number of DDL=2*36+4*36=216
41 //2 DDL for each inner sector
42 //4 DDL for each outer sector
43 Int_t DDLPerFile=216/LDCsNumber;
45 if (216%LDCsNumber) DDLPerFile++;
46 cout<<"Number of DDL per slide: "<<DDLPerFile<<endl;
48 f.open("AliTPCDDL.dat",ios::binary);
49 if(!f){cout<<"File doesn't exist !!"<<endl;return;}
60 //AliTPCBuffer160 is used in write mode to generate AltroFormat.dat file
63 sprintf(filename,"TPCslice%d",SliceNumber);
64 cout<<" Creating "<<filename<<endl;
65 AliTPCBuffer160 *Buffer=new AliTPCBuffer160(filename,1);
68 Int_t PSecNumber=-1; //Previous Sector number
69 Int_t PRowNumber=-1; //Previous Row number
70 Int_t PPadNumber=-1; //Previous Pad number
71 Int_t PTimeBin=-1; //Previous Time-Bin
72 Int_t PSubSector=-1; //Previous Sub Sector
77 while (f.read((char*)(&data),sizeof(data))){
84 PSubSector=data.SubSec;
85 //size magic word sector number sub-sector number 0 for TPC 0 for uncompressed
86 Buffer->WriteMiniHeader(0,PSecNumber,PSubSector,0,0);//Dummy;
88 Buffer->FillBuffer(data.Dig-offset);
92 if ( (data.Time==(PTimeBin+1)) &&
93 (PPadNumber==data.Pad) &&
94 (PRowNumber==data.Row) &&
95 (PSecNumber==data.Sec)){
99 Buffer->FillBuffer(PTimeBin);
100 Buffer->FillBuffer(BunchLength+2);
102 if ((PPadNumber!=data.Pad)||(PRowNumber!=data.Row)||(PSecNumber!=data.Sec)){
103 //Trailer is formatted and inserted!!
104 Buffer->WriteTrailer(nwords,PPadNumber,PRowNumber,PSecNumber);
108 if(PSubSector!=data.SubSec){
110 if(CountDDL==DDLPerFile){
111 //size magic word sector number sub-sector number 0 for TPC 0 for uncompressed
113 Buffer->WriteMiniHeader(1,PSecNumber,PSubSector,0,0);
114 //cout<<"Mini header for DDL:"<<PSecNumber<<" Sub-sec:"<<PSubSector<<endl;
117 sprintf(filename,"TPCslice%d",SliceNumber);
118 cout<<" Creating "<<filename<<endl;
119 Buffer=new AliTPCBuffer160(filename,1);
120 Buffer->WriteMiniHeader(0,data.Sec,data.SubSec,0,0);//Dummy;
125 Buffer->WriteMiniHeader(1,PSecNumber,PSubSector,0,0);
126 Buffer->WriteMiniHeader(0,data.Sec,data.SubSec,0,0);//Dummy;
128 PSubSector=data.SubSec;
138 Buffer->FillBuffer(data.Dig-offset);
142 Buffer->FillBuffer(PTimeBin);
143 Buffer->FillBuffer(BunchLength+2);
145 Buffer->WriteTrailer(nwords,PPadNumber,PRowNumber,PSecNumber);
148 Buffer->WriteMiniHeader(1,PSecNumber,PSubSector,0,0);
149 //cout<<"Mini header for D D L:"<<PSecNumber<<" Sub-sec:"<<PSubSector<<endl;
151 cout<<"Number of digits: "<<Count<<endl;
155 ////////////////////////////////////////////////////////////////////////////
156 ////////////////////////////////////////////////////////////////////////////
157 //This method is used to Compress and decompress the slides
159 Int_t AliTPCDDLRawData::RawDataCompDecompress(Int_t LDCsNumber,Int_t Comp){
160 static const Int_t NumTable=5;
165 //Int_t MagicWord,DDLNumber,SecNumber,SubSector,Detector;
167 for(Int_t i=1;i<=LDCsNumber;i++){
169 sprintf(filename,"TPCslice%d",i);
170 sprintf(dest,"TPCslice%d.comp",i);
173 sprintf(filename,"TPCslice%d.comp",i);
174 sprintf(dest,"TPCslice%d.decomp",i);
176 f.open(filename,ios::binary|ios::in);
177 if(!f){cout<<"File doesn't exist \n";exit(1);}
178 cout<<filename<<" "<<dest<<endl;
180 fdest.open(dest,ios::binary);
181 //loop over the DDL block
182 //Each block contains a Mini Header followed by raw data (ALTRO FORMAT)
183 //The number of block is ceil(216/LDCsNumber)
184 ULong_t MiniHeader[3];
185 //here the Mini Header is read
186 while( (f.read((char*)(MiniHeader),sizeof(ULong_t)*3)) ){
188 //Int_t dim=sizeof(ULong_t)+sizeof(Int_t)*5;
189 //cout<<" Sec "<<SecNumber<<" SubSector "<<SubSector<<" Size "<<Size<<endl;
190 //open the temporay File
192 char temp[15]="TempFile";
193 fo.open(temp,ios::binary);
195 for(ULong_t j=0;j<Size;j++){
196 f.read((char*)(&car),1);
197 fo.write((char*)(&car),1);
200 //The temp file is compressed or decompressed
201 AliTPCCompression *util = new AliTPCCompression();
203 util->CompressDataOptTables(NumTable,temp,"TempCompDecomp");
205 util->DecompressDataOptTables(NumTable,temp,"TempCompDecomp");
207 //the temp compressed file is open and copied to the final file fdest
209 fi.open("TempCompDecomp",ios::binary);
210 fi.seekg(0,ios::end);
213 //The Mini Header is updated (Size and Compressed flag)
214 //and written into the output file
224 MiniHeader[2]=MiniHeader[2]&aux;
225 fdest.write((char*)(MiniHeader),sizeof(ULong_t)*3);
226 //The compressem temp file is copied into the output file fdest
227 for(ULong_t j=0;j<Size;j++){
228 fi.read((char*)(&car),1);
229 fdest.write((char*)(&car),1);
237 remove("TempCompDecomp");
242 /////////////////////////////////////////////////////////////////////////////////
243 //This method is used to build the Altro format from AliTPCDDL.dat
244 //It is used to debug the code and create the tables used in the compresseion phase
245 void AliTPCDDLRawData::RawDataAltro(){
248 f.open("AliTPCDDL.dat",ios::binary);
249 if(!f){cout<<"File doesn't exist !!"<<endl;return;}
260 //AliTPCBuffer160 is used in write mode to generate AltroFormat.dat file
261 char filename[30]="AltroFormatDDL.dat";
262 cout<<" Creating "<<filename<<endl;
263 AliTPCBuffer160 *Buffer=new AliTPCBuffer160(filename,1);
266 Int_t PSecNumber=-1; //Previous Sector number
267 Int_t PRowNumber=-1; //Previous Row number
268 Int_t PPadNumber=-1; //Previous Pad number
269 Int_t PTimeBin=-1; //Previous Time-Bin
272 ULong_t numPackets=0;
273 while (f.read((char*)(&data),sizeof(data))){
281 Buffer->FillBuffer(data.Dig-offset);
285 if ( (data.Time==(PTimeBin+1)) &&
286 (PPadNumber==data.Pad) &&
287 (PRowNumber==data.Row) &&
288 (PSecNumber==data.Sec)){
292 Buffer->FillBuffer(PTimeBin);
293 Buffer->FillBuffer(BunchLength+2);
295 if ((PPadNumber!=data.Pad)||(PRowNumber!=data.Row)||(PSecNumber!=data.Sec)){
296 //Trailer is formatted and inserted!!
297 Buffer->WriteTrailer(nwords,PPadNumber,PRowNumber,PSecNumber);
308 Buffer->FillBuffer(data.Dig-offset);
312 Buffer->FillBuffer(PTimeBin);
313 Buffer->FillBuffer(BunchLength+2);
315 Buffer->WriteTrailer(nwords,PPadNumber,PRowNumber,PSecNumber);
317 cout<<"Number of digits: "<<Count<<endl;
323 void AliTPCDDLRawData::RawDataAltroDecode(Int_t LDCsNumber,Int_t Comp){
328 sprintf(dest,"AltroDDLRecomposed.dat");
330 sprintf(dest,"AltroDDLRecomposedDec.dat");
333 fdest.open(dest,ios::binary);
335 //Int_t MagicWord,DDLNumber,SecNumber,SubSector,Detector,Flag=0;
336 for(Int_t i=1;i<=LDCsNumber;i++){
338 sprintf(filename,"TPCslice%d",i);
340 sprintf(filename,"TPCslice%d.decomp",i);
341 f.open(filename,ios::binary|ios::in);
342 if(!f){cout<<"The file doesn't exist"<<endl;exit(1);}
343 //loop over the DDL block
344 //Each block contains a Mini Header followed by raw data (ALTRO FORMAT)
345 //The number of block is ceil(216/LDCsNumber)
346 ULong_t MiniHeader[3];
347 //here the Mini Header is read
348 while( (f.read((char*)(MiniHeader),sizeof(ULong_t)*3)) ){
349 //cout<<"Mini header dimension "<<MiniHeader[0]<<endl;
352 for(ULong_t j=0;j<Size;j++){
353 f.read((char*)(&car),1);
354 fdest.write((char*)(&car),1);