No ios::binary on Alpha with cxx
[u/mrichter/AliRoot.git] / TPC / AliTPCDDLRawData.cxx
CommitLineData
2e9f335b 1/**************************************************************************
2 * Copyright(c) 1998-2003, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
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 **************************************************************************/
30c1018e 15/* $Id$ */
a79660fb 16
17//This class conteins all the methods to create raw data
18//as par a given DDL.
19//It produces DDL with both compressed and uncompressed format.
20//For compression we use the optimized table wich needs
21//to be provided.
22
2e9f335b 23
24#include "TObjArray.h"
25#include "Riostream.h"
26#include <stdio.h>
27#include <stdlib.h>
28#include "AliTPCCompression.h"
29#include "AliTPCBuffer160.h"
30#include "AliTPCDDLRawData.h"
2e9f335b 31
32ClassImp(AliTPCDDLRawData)
33////////////////////////////////////////////////////////////////////////////////////////
34
35AliTPCDDLRawData::AliTPCDDLRawData(const AliTPCDDLRawData &source){
36 // Copy Constructor
37 return;
38}
39
40AliTPCDDLRawData& AliTPCDDLRawData::operator=(const AliTPCDDLRawData &source){
41 //Assigment operator
42 return *this;
43}
44
45
46////////////////////////////////////////////////////////////////////////////
47void AliTPCDDLRawData::RawData(Int_t LDCsNumber){
a79660fb 48 //Raw data slides generation
2e9f335b 49 //Number of DDL=2*36+4*36=216
50 //2 DDL for each inner sector
51 //4 DDL for each outer sector
a79660fb 52 Int_t ddlPerFile=216/LDCsNumber;
2e9f335b 53 Int_t offset=1;
a79660fb 54 if (216%LDCsNumber) ddlPerFile++;
55 cout<<"Number of DDL per slide: "<<ddlPerFile<<endl;
2e9f335b 56 ifstream f;
30c1018e 57#ifndef __DECCXX
2e9f335b 58 f.open("AliTPCDDL.dat",ios::binary);
30c1018e 59#else
60 f.open("AliTPCDDL.dat");
61#endif
2e9f335b 62 if(!f){cout<<"File doesn't exist !!"<<endl;return;}
63 struct DataPad{
64 Int_t Sec;
65 Int_t SubSec;
66 Int_t Row;
67 Int_t Pad;
68 Int_t Dig;
69 Int_t Time;
70 };
71 DataPad data;
72
73 //AliTPCBuffer160 is used in write mode to generate AltroFormat.dat file
a79660fb 74 Int_t sliceNumber=1;
2e9f335b 75 char filename[15];
a79660fb 76 sprintf(filename,"TPCslice%d",sliceNumber);
2e9f335b 77 cout<<" Creating "<<filename<<endl;
a79660fb 78 AliTPCBuffer160 *buffer=new AliTPCBuffer160(filename,1);
2e9f335b 79
a79660fb 80 ULong_t count=0;
81 Int_t pSecNumber=-1; //Previous Sector number
82 Int_t pRowNumber=-1; //Previous Row number
83 Int_t pPadNumber=-1; //Previous Pad number
84 Int_t pTimeBin=-1; //Previous Time-Bin
85 Int_t pSubSector=-1; //Previous Sub Sector
86 Int_t bunchLength=0;
87 Int_t countDDL=0;
2e9f335b 88 Int_t nwords=0;
89 ULong_t numPackets=0;
90 while (f.read((char*)(&data),sizeof(data))){
a79660fb 91 count++;
92 if (pPadNumber==-1){
93 pSecNumber=data.Sec;
94 pRowNumber=data.Row;
95 pPadNumber=data.Pad;
96 pTimeBin=data.Time;
97 pSubSector=data.SubSec;
2e9f335b 98 //size magic word sector number sub-sector number 0 for TPC 0 for uncompressed
a79660fb 99 buffer->WriteMiniHeader(0,pSecNumber,pSubSector,0,0);//Dummy;
100 bunchLength=1;
101 buffer->FillBuffer(data.Dig-offset);
2e9f335b 102 nwords++;
103 }//end if
104 else{
a79660fb 105 if ( (data.Time==(pTimeBin+1)) &&
106 (pPadNumber==data.Pad) &&
107 (pRowNumber==data.Row) &&
108 (pSecNumber==data.Sec)){
109 bunchLength++;
2e9f335b 110 }//end if
111 else{
a79660fb 112 buffer->FillBuffer(pTimeBin);
113 buffer->FillBuffer(bunchLength+2);
2e9f335b 114 nwords+=2;
a79660fb 115 if ((pPadNumber!=data.Pad)||(pRowNumber!=data.Row)||(pSecNumber!=data.Sec)){
2e9f335b 116 //Trailer is formatted and inserted!!
a79660fb 117 buffer->WriteTrailer(nwords,pPadNumber,pRowNumber,pSecNumber);
2e9f335b 118 numPackets++;
119 nwords=0;
120
a79660fb 121 if(pSubSector!=data.SubSec){
122 countDDL++;
123 if(countDDL==ddlPerFile){
2e9f335b 124 //size magic word sector number sub-sector number 0 for TPC 0 for uncompressed
a79660fb 125 buffer->Flush();
126 buffer->WriteMiniHeader(1,pSecNumber,pSubSector,0,0);
2e9f335b 127 //cout<<"Mini header for DDL:"<<PSecNumber<<" Sub-sec:"<<PSubSector<<endl;
a79660fb 128 delete buffer;
129 sliceNumber++;
130 sprintf(filename,"TPCslice%d",sliceNumber);
2e9f335b 131 cout<<" Creating "<<filename<<endl;
a79660fb 132 buffer=new AliTPCBuffer160(filename,1);
133 buffer->WriteMiniHeader(0,data.Sec,data.SubSec,0,0);//Dummy;
134 countDDL=0;
2e9f335b 135 }//end if
136 else{
a79660fb 137 buffer->Flush();
138 buffer->WriteMiniHeader(1,pSecNumber,pSubSector,0,0);
139 buffer->WriteMiniHeader(0,data.Sec,data.SubSec,0,0);//Dummy;
2e9f335b 140 }
a79660fb 141 pSubSector=data.SubSec;
2e9f335b 142 }//end if
2e9f335b 143 }//end if
144
a79660fb 145 bunchLength=1;
146 pPadNumber=data.Pad;
147 pRowNumber=data.Row;
148 pSecNumber=data.Sec;
2e9f335b 149 }//end else
a79660fb 150 pTimeBin=data.Time;
151 buffer->FillBuffer(data.Dig-offset);
2e9f335b 152 nwords++;
153 }//end else
154 }//end while
a79660fb 155 buffer->FillBuffer(pTimeBin);
156 buffer->FillBuffer(bunchLength+2);
2e9f335b 157 nwords+=2;
a79660fb 158 buffer->WriteTrailer(nwords,pPadNumber,pRowNumber,pSecNumber);
2e9f335b 159 //write the M.H.
a79660fb 160 buffer->Flush();
161 buffer->WriteMiniHeader(1,pSecNumber,pSubSector,0,0);
162 //cout<<"Mini header for D D L:"<<pSecNumber<<" Sub-sec:"<<pSubSector<<endl;
163 delete buffer;
164 cout<<"Number of digits: "<<count<<endl;
2e9f335b 165 f.close();
166 return;
167}
168////////////////////////////////////////////////////////////////////////////
169////////////////////////////////////////////////////////////////////////////
a79660fb 170
2e9f335b 171
172Int_t AliTPCDDLRawData::RawDataCompDecompress(Int_t LDCsNumber,Int_t Comp){
a79660fb 173 //This method is used to compress and decompress the slides
174 static const Int_t kNumTables=5;
2e9f335b 175 char filename[20];
176 char dest[20];
177 fstream f;
a79660fb 178 ULong_t size=0;
2e9f335b 179 //Int_t MagicWord,DDLNumber,SecNumber,SubSector,Detector;
a79660fb 180 Int_t flag=0;
2e9f335b 181 for(Int_t i=1;i<=LDCsNumber;i++){
182 if(!Comp){
183 sprintf(filename,"TPCslice%d",i);
184 sprintf(dest,"TPCslice%d.comp",i);
185 }
186 else{
187 sprintf(filename,"TPCslice%d.comp",i);
188 sprintf(dest,"TPCslice%d.decomp",i);
189 }
30c1018e 190#ifndef __DECCXX
2e9f335b 191 f.open(filename,ios::binary|ios::in);
30c1018e 192#else
193 f.open(filename,ios::in);
194#endif
2e9f335b 195 if(!f){cout<<"File doesn't exist \n";exit(1);}
196 cout<<filename<<" "<<dest<<endl;
197 ofstream fdest;
30c1018e 198#ifndef __DECCXX
2e9f335b 199 fdest.open(dest,ios::binary);
30c1018e 200#else
201 fdest.open(dest);
202#endif
2e9f335b 203 //loop over the DDL block
204 //Each block contains a Mini Header followed by raw data (ALTRO FORMAT)
205 //The number of block is ceil(216/LDCsNumber)
a79660fb 206 ULong_t miniHeader[3];
2e9f335b 207 //here the Mini Header is read
a79660fb 208 while( (f.read((char*)(miniHeader),sizeof(ULong_t)*3)) ){
209 size=miniHeader[0];
2e9f335b 210 //Int_t dim=sizeof(ULong_t)+sizeof(Int_t)*5;
a79660fb 211 //cout<<" Sec "<<SecNumber<<" SubSector "<<SubSector<<" size "<<size<<endl;
2e9f335b 212 //open the temporay File
213 ofstream fo;
214 char temp[15]="TempFile";
30c1018e 215#ifndef __DECCXX
2e9f335b 216 fo.open(temp,ios::binary);
30c1018e 217#else
218 fo.open(temp);
219#endif
2e9f335b 220 Int_t car=0;
a79660fb 221 for(ULong_t j=0;j<size;j++){
2e9f335b 222 f.read((char*)(&car),1);
223 fo.write((char*)(&car),1);
224 }//end for
225 fo.close();
226 //The temp file is compressed or decompressed
227 AliTPCCompression *util = new AliTPCCompression();
228 if(!Comp)
a79660fb 229 util->CompressDataOptTables(kNumTables,temp,"TempCompDecomp");
2e9f335b 230 else
a79660fb 231 util->DecompressDataOptTables(kNumTables,temp,"TempCompDecomp");
2e9f335b 232 delete util;
233 //the temp compressed file is open and copied to the final file fdest
234 ifstream fi;
30c1018e 235#ifndef __DECCXX
2e9f335b 236 fi.open("TempCompDecomp",ios::binary);
30c1018e 237#else
238 fi.open("TempCompDecomp");
239#endif
2e9f335b 240 fi.seekg(0,ios::end);
a79660fb 241 size=fi.tellg();
2e9f335b 242 fi.seekg(0);
a79660fb 243 //The Mini Header is updated (size and Compressed flag)
2e9f335b 244 //and written into the output file
a79660fb 245 miniHeader[0]=size;
2e9f335b 246 if(!Comp)
a79660fb 247 flag=1;
2e9f335b 248 else
a79660fb 249 flag=0;
2e9f335b 250 ULong_t aux=0xFFFF;
251 aux<<=16;
a79660fb 252 aux|=flag;
2e9f335b 253 aux|=0xFF;
a79660fb 254 miniHeader[2]=miniHeader[2]&aux;
255 fdest.write((char*)(miniHeader),sizeof(ULong_t)*3);
2e9f335b 256 //The compressem temp file is copied into the output file fdest
a79660fb 257 for(ULong_t j=0;j<size;j++){
2e9f335b 258 fi.read((char*)(&car),1);
259 fdest.write((char*)(&car),1);
260 }//end for
261 fi.close();
262 }//end while
cd43b5e1 263 f.clear();
2e9f335b 264 f.close();
265 fdest.close();
266 remove("TempFile");
267 remove("TempCompDecomp");
268 }//end for
269 return 0;
270}
271
272/////////////////////////////////////////////////////////////////////////////////
a79660fb 273void AliTPCDDLRawData::RawDataAltro()const{
274 //This method is used to build the Altro format from AliTPCDDL.dat
275 //It is used to debug the code and create the tables used in the compresseion phase
2e9f335b 276 Int_t offset=1;
277 ifstream f;
30c1018e 278#ifndef __DECCXX
2e9f335b 279 f.open("AliTPCDDL.dat",ios::binary);
30c1018e 280#else
281 f.open("AliTPCDDL.dat");
282#endif
2e9f335b 283 if(!f){cout<<"File doesn't exist !!"<<endl;return;}
284 struct DataPad{
285 Int_t Sec;
286 Int_t SubSec;
287 Int_t Row;
288 Int_t Pad;
289 Int_t Dig;
290 Int_t Time;
291 };
292 DataPad data;
293
294 //AliTPCBuffer160 is used in write mode to generate AltroFormat.dat file
295 char filename[30]="AltroFormatDDL.dat";
296 cout<<" Creating "<<filename<<endl;
a79660fb 297 AliTPCBuffer160 *buffer=new AliTPCBuffer160(filename,1);
2e9f335b 298
a79660fb 299 ULong_t count=0;
300 Int_t pSecNumber=-1; //Previous Sector number
301 Int_t pRowNumber=-1; //Previous Row number
302 Int_t pPadNumber=-1; //Previous Pad number
303 Int_t pTimeBin=-1; //Previous Time-Bin
304 Int_t bunchLength=0;
2e9f335b 305 Int_t nwords=0;
306 ULong_t numPackets=0;
307 while (f.read((char*)(&data),sizeof(data))){
a79660fb 308 count++;
309 if (pPadNumber==-1){
310 pSecNumber=data.Sec;
311 pRowNumber=data.Row;
312 pPadNumber=data.Pad;
313 pTimeBin=data.Time;
314 bunchLength=1;
315 buffer->FillBuffer(data.Dig-offset);
2e9f335b 316 nwords++;
317 }//end if
318 else{
a79660fb 319 if ( (data.Time==(pTimeBin+1)) &&
320 (pPadNumber==data.Pad) &&
321 (pRowNumber==data.Row) &&
322 (pSecNumber==data.Sec)){
323 bunchLength++;
2e9f335b 324 }//end if
325 else{
a79660fb 326 buffer->FillBuffer(pTimeBin);
327 buffer->FillBuffer(bunchLength+2);
2e9f335b 328 nwords+=2;
a79660fb 329 if ((pPadNumber!=data.Pad)||(pRowNumber!=data.Row)||(pSecNumber!=data.Sec)){
2e9f335b 330 //Trailer is formatted and inserted!!
a79660fb 331 buffer->WriteTrailer(nwords,pPadNumber,pRowNumber,pSecNumber);
2e9f335b 332 numPackets++;
333 nwords=0;
334 }//end if
335
a79660fb 336 bunchLength=1;
337 pPadNumber=data.Pad;
338 pRowNumber=data.Row;
339 pSecNumber=data.Sec;
2e9f335b 340 }//end else
a79660fb 341 pTimeBin=data.Time;
342 buffer->FillBuffer(data.Dig-offset);
2e9f335b 343 nwords++;
344 }//end else
345 }//end while
a79660fb 346 buffer->FillBuffer(pTimeBin);
347 buffer->FillBuffer(bunchLength+2);
2e9f335b 348 nwords+=2;
a79660fb 349 buffer->WriteTrailer(nwords,pPadNumber,pRowNumber,pSecNumber);
350 delete buffer;
351 cout<<"Number of digits: "<<count<<endl;
2e9f335b 352 f.close();
353 return;
354}
355
a79660fb 356/////////////////////////////////////////////////////////////////////////
2e9f335b 357void AliTPCDDLRawData::RawDataAltroDecode(Int_t LDCsNumber,Int_t Comp){
a79660fb 358 //This method merges the slides in only one file removing at the same
359 //time all the mini headers. The file so obtained must be Altro format
360 //complaiant.
361 //It is used mainly in the debugging phase
2e9f335b 362 char filename[15];
363 char dest[30];
364 fstream f;
365 if(!Comp)
366 sprintf(dest,"AltroDDLRecomposed.dat");
367 else
368 sprintf(dest,"AltroDDLRecomposedDec.dat");
369 ofstream fdest;
cd43b5e1 370
30c1018e 371#ifndef __DECCXX
2e9f335b 372 fdest.open(dest,ios::binary);
30c1018e 373#else
374 fdest.open(dest);
375#endif
a79660fb 376 ULong_t size=0;
377 //Int_t MagicWord,DDLNumber,SecNumber,SubSector,Detector,flag=0;
2e9f335b 378 for(Int_t i=1;i<=LDCsNumber;i++){
cd43b5e1 379 if(!Comp)
2e9f335b 380 sprintf(filename,"TPCslice%d",i);
381 else
382 sprintf(filename,"TPCslice%d.decomp",i);
30c1018e 383#ifndef __DECCXX
2e9f335b 384 f.open(filename,ios::binary|ios::in);
30c1018e 385#else
386 f.open(filename,ios::in);
387#endif
cd43b5e1 388 if(!f){cout<<"The file doesn't exist"<<endl;exit(1);}
2e9f335b 389 //loop over the DDL block
390 //Each block contains a Mini Header followed by raw data (ALTRO FORMAT)
391 //The number of block is ceil(216/LDCsNumber)
a79660fb 392 ULong_t miniHeader[3];
2e9f335b 393 //here the Mini Header is read
a79660fb 394 while( (f.read((char*)(miniHeader),sizeof(ULong_t)*3)) ){
395 //cout<<"Mini header dimension "<<miniHeader[0]<<endl;
2e9f335b 396 Int_t car=0;
a79660fb 397 size=miniHeader[0];
398 for(ULong_t j=0;j<size;j++){
2e9f335b 399 f.read((char*)(&car),1);
400 fdest.write((char*)(&car),1);
401 }//end for
402 }//end while
cd43b5e1 403 f.clear();
2e9f335b 404 f.close();
405 }//end for
406 fdest.close();
407 return;
408}
409