]> git.uio.no Git - u/mrichter/AliRoot.git/blame - HLT/comp/AliL3Compress.cxx
Obsolete - removed
[u/mrichter/AliRoot.git] / HLT / comp / AliL3Compress.cxx
CommitLineData
3e87ef69 1// @(#) $Id$
5bf93292 2
3// Author: Anders Vestbo <mailto:vestbo$fi.uib.no>
3e87ef69 4//*-- Copyright &copy ALICE HLT Group
5bf93292 5
4994a25d 6#include "AliL3StandardIncludes.h"
5bf93292 7
3e87ef69 8#include "bitio.h"
9#include "AliL3RootTypes.h"
10#include "AliL3Models.h"
11#include "AliL3DigitData.h"
4994a25d 12#include "AliL3Logging.h"
5bf93292 13#include "AliL3TrackArray.h"
14#include "AliL3ModelTrack.h"
029912b7 15#include "AliL3Transform.h"
16#include "AliL3MemHandler.h"
3e87ef69 17#include "AliL3DataCompressor.h"
18
19#if 0
20#ifdef use_root
21#include <TH1.h>
22#include <TH2.h>
23#include <TRandom.h>
24#endif
92a876e2 25#ifdef use_aliroot
be6ddb10 26#include "AliL3FileHandler.h"
92a876e2 27#endif
3e87ef69 28#endif
29
30#include "AliL3Compress.h"
31
32#if GCCVERSION == 3
33using namespace std;
34#endif
5bf93292 35
029912b7 36//_____________________________________________________________
37//
38// AliL3Compress
39//
40// Class for compressing and uncompressing data.
41
5bf93292 42ClassImp(AliL3Compress)
43
44AliL3Compress::AliL3Compress()
45{
95a00d93 46 fTracks=0;
029912b7 47 fSlice =0;
48 fPatch=0;
2357bb38 49 fWriteShape=kFALSE;
3e87ef69 50 fEvent=-1;
029912b7 51}
52
3e87ef69 53AliL3Compress::AliL3Compress(Int_t slice,Int_t patch,Char_t *path,Bool_t writeshape,Int_t event)
029912b7 54{
3e87ef69 55 fEvent=event;
029912b7 56 fSlice=slice;
57 fPatch=patch;
029912b7 58 fTracks=0;
be6ddb10 59 sprintf(fPath,"%s",path);
2357bb38 60 fWriteShape=writeshape;
5bf93292 61}
62
63AliL3Compress::~AliL3Compress()
64{
95a00d93 65 if(fTracks)
66 delete fTracks;
029912b7 67}
68
3e87ef69 69Bool_t AliL3Compress::WriteFile(AliL3TrackArray *tracks,Char_t *filename)
029912b7 70{
3e87ef69 71 Char_t fname[1024];
72 if(filename)
73 sprintf(fname,"%s/comp/%s",fPath,filename);
74 else if(fEvent<0)
75 sprintf(fname,"%s/comp/tracks_m_%d_%d.raw",fPath,fSlice,fPatch);
76 else
77 sprintf(fname,"%s/comp/tracks_m_%d_%d_%d.raw",fPath,fEvent,fSlice,fPatch);
5bf93292 78
be6ddb10 79 FILE *file = fopen(fname,"w");
2357bb38 80 if(!file)
81 {
82 cerr<<"AliL3Compress::WriteFile : Error opening file "<<fname<<endl;
3e87ef69 83 return kFALSE;
2357bb38 84 }
5bf93292 85 Short_t ntracks = tracks->GetNTracks();
95a00d93 86
87 Int_t count=0;
88 AliL3ClusterModel *clusters=0;
89 AliL3TrackModel *model=0;
5bf93292 90 for(Int_t i=0; i<ntracks; i++)
91 {
92 AliL3ModelTrack *track = (AliL3ModelTrack*)tracks->GetCheckedTrack(i);
93 if(!track) continue;
3e87ef69 94
4a838220 95 //Do not save useless tracks or clusters:
3e87ef69 96 //if(track->GetNPresentClusters() == 0)
97 //continue;
4a838220 98
029912b7 99 track->FillModel();
95a00d93 100 model = track->GetModel();
0a86fbb7 101 //if(model->fNClusters==0) continue;
95a00d93 102 clusters = track->GetClusters();
5bf93292 103 if(fwrite(model,sizeof(AliL3TrackModel),1,file)!=1) break;
0a86fbb7 104 //if(fwrite(clusters,model->fNClusters*sizeof(AliL3ClusterModel),1,file)!=1) break;
105 if(fwrite(clusters,AliL3Transform::GetNRows(fPatch)*sizeof(AliL3ClusterModel),1,file)!=1) break;
95a00d93 106 count++;
029912b7 107
5bf93292 108 }
109 fclose(file);
3e87ef69 110 return kTRUE;
5bf93292 111}
112
3e87ef69 113Bool_t AliL3Compress::ReadFile(Char_t which,Char_t *filename)
5bf93292 114{
be6ddb10 115 //Read the trackfile.
116
3e87ef69 117 Char_t fname[1024];
118 if(filename)
119 sprintf(fname,"%s/comp/%s",fPath,filename);
be6ddb10 120 else
121 {
3e87ef69 122 if(which == 'm')
123 {
124 if(fEvent<0)
125 sprintf(fname,"%s/comp/tracks_m_%d_%d.raw",fPath,fSlice,fPatch);
126 else
127 sprintf(fname,"%s/comp/tracks_m_%d_%d_%d.raw",fPath,fEvent,fSlice,fPatch);
128 }
129 else if(which == 'u')
130 {
131 if(fEvent<0)
132 sprintf(fname,"%s/comp/tracks_u_%d_%d.raw",fPath,fSlice,fPatch);
133 else
134 sprintf(fname,"%s/comp/tracks_u_%d_%d_%d.raw",fPath,fEvent,fSlice,fPatch);
135 }
136 else
137 {
138 cerr<<"AliL3Compress::ReadFile() : Wrong option"<<endl;
139 return kFALSE;
140 }
be6ddb10 141 }
142
143 FILE *file = fopen(fname,"r");
029912b7 144 if(!file)
145 {
2357bb38 146 cerr<<"AliL3Compress::ReadFile : Cannot open file "<<fname<<endl;
3e87ef69 147 return kFALSE;
029912b7 148 }
149
95a00d93 150 if(fTracks)
151 delete fTracks;
152 fTracks = new AliL3TrackArray("AliL3ModelTrack");
5bf93292 153
154 while(!feof(file))
155 {
95a00d93 156 AliL3ModelTrack *track = (AliL3ModelTrack*)fTracks->NextTrack();
029912b7 157 track->Init(fSlice,fPatch);
5bf93292 158 AliL3TrackModel *model = track->GetModel();
159 AliL3ClusterModel *clusters = track->GetClusters();
160 if(fread(model,sizeof(AliL3TrackModel),1,file)!=1) break;
0a86fbb7 161 //if(fread(clusters,model->fNClusters*sizeof(AliL3ClusterModel),1,file)!=1) break;
162 if(fread(clusters,AliL3Transform::GetNRows(fPatch)*sizeof(AliL3ClusterModel),1,file)!=1) break;
029912b7 163 track->FillTrack();
5bf93292 164 }
029912b7 165
166 fTracks->RemoveLast();
5bf93292 167 fclose(file);
3e87ef69 168 return kTRUE;
5bf93292 169}
170
3e87ef69 171Bool_t AliL3Compress::CompressFile()
5bf93292 172{
be6ddb10 173 Char_t fname[100];
3e87ef69 174 if(fEvent<0)
175 sprintf(fname,"%s/comp/tracks_c_%d_%d.raw",fPath,fSlice,fPatch);
176 else
177 sprintf(fname,"%s/comp/tracks_c_%d_%d_%d.raw",fPath,fEvent,fSlice,fPatch);
be6ddb10 178 BIT_FILE *output = OpenOutputBitFile(fname);
179
3e87ef69 180 if(fEvent<0)
181 sprintf(fname,"%s/comp/tracks_m_%d_%d.raw",fPath,fSlice,fPatch);
182 else
183 sprintf(fname,"%s/comp/tracks_m_%d_%d_%d.raw",fPath,fEvent,fSlice,fPatch);
184
be6ddb10 185 FILE *input = fopen(fname,"r");
186 if(!input)
187 {
188 cerr<<"AliL3Compress::CompressFile() : Error opening file: "<<fname<<endl;
3e87ef69 189 return kFALSE;
be6ddb10 190 }
5bf93292 191
192 AliL3TrackModel track;
193 AliL3ClusterModel cluster;
194 Int_t temp;
2357bb38 195 Int_t power;
029912b7 196
197 Int_t timeo,pado,chargeo,shapeo;
198 timeo=pado=chargeo=shapeo=0;
5bf93292 199 while(!feof(input))
200 {
201 if(fread(&track,sizeof(AliL3TrackModel),1,input)!=1) break;
95a00d93 202
203 if(output->mask != 0x80) //Write the current byte to file.
204 {
92a876e2 205 //cerr<<"\nAliL3Compress::CompressFile() : Writing overhead bits!!!"<<endl;
95a00d93 206 if(putc(output->rack,output->file )!=output->rack)
207 cerr<<"AliL3Compress::ComressFile : Error writing to bitfile"<<endl;
208 output->mask=0x80;
029912b7 209 output->rack=0;
95a00d93 210 }
029912b7 211
212 //Write track parameters:
5bf93292 213 fwrite(&track,sizeof(AliL3TrackModel),1,output->file);
3e87ef69 214
215 Int_t origslice=-1,slice,clustercount=0;
0a86fbb7 216 for(Int_t i=0; i<AliL3Transform::GetNRows(fPatch); i++)
5bf93292 217 {
218 if(fread(&cluster,sizeof(AliL3ClusterModel),1,input)!=1) break;
029912b7 219
220 //Write empty flag:
221 temp = (Int_t)cluster.fPresent;
222 OutputBit(output,temp);
223 if(!temp) continue;
224
3e87ef69 225 if(cluster.fSlice<0 || cluster.fSlice>35)
226 {
227 cerr<<"AliL3DataCompress::CompressFile : Fucked up slice number :"<<cluster.fSlice<<endl;
228 exit(5);
229 }
230
231 //Write slice number of first point
232 if(clustercount==0)
233 {
234 origslice = cluster.fSlice;
235 OutputBits(output,origslice,6); //Need 6 bits to encode slice number
236 }
237 else
238 {
239 slice = cluster.fSlice;
240 if(slice == origslice)
241 OutputBit(output,0);
242 else
243 {
244 OutputBit(output,1);
245 OutputBits(output,slice,6);
246 origslice=slice;
247 }
248 }
249
029912b7 250 //Write time information:
3e87ef69 251 temp = (Int_t)rint(cluster.fDTime);
5bf93292 252 if(temp<0)
253 OutputBit(output,0);
254 else
255 OutputBit(output,1);
3e87ef69 256 power = 1<<(AliL3DataCompressor::GetNTimeBits()-1);
029912b7 257 if(abs(temp)>=power)
258 {
259 timeo++;
260 temp=power - 1;
261 }
3e87ef69 262 OutputBits(output,abs(temp),(AliL3DataCompressor::GetNTimeBits()-1));
029912b7 263
264 //Write pad information:
3e87ef69 265 temp = (Int_t)rint(cluster.fDPad);
5bf93292 266 if(temp<0)
267 OutputBit(output,0);
268 else
269 OutputBit(output,1);
3e87ef69 270 power = 1<<(AliL3DataCompressor::GetNPadBits()-1);
029912b7 271 if(abs(temp)>=power)
272 {
273 pado++;
274 temp=power - 1;
275 }
3e87ef69 276 OutputBits(output,abs(temp),(AliL3DataCompressor::GetNPadBits()-1));
029912b7 277
278 //Write charge information:
5bf93292 279 temp = (Int_t)cluster.fDCharge;
3e87ef69 280 power = 1<<(AliL3DataCompressor::GetNChargeBits());
029912b7 281 if(abs(temp)>=power)
282 {
283 chargeo++;
284 temp=power - 1;
285 }
3e87ef69 286 OutputBits(output,abs(temp),(AliL3DataCompressor::GetNChargeBits()));
029912b7 287
2357bb38 288 if(fWriteShape)
029912b7 289 {
2357bb38 290 //Write shape information:
291 temp = (Int_t)cluster.fDSigmaY2;
292 if(temp<0)
293 OutputBit(output,0);
294 else
295 OutputBit(output,1);
3e87ef69 296 power = 1<<(AliL3DataCompressor::GetNShapeBits()-1);
2357bb38 297 if(abs(temp) >= power)
298 {
299 shapeo++;
300 temp = power - 1;
301 }
3e87ef69 302 OutputBits(output,abs(temp),(AliL3DataCompressor::GetNShapeBits()-1));
2357bb38 303
304 temp = (Int_t)cluster.fDSigmaZ2;
305 if(temp<0)
306 OutputBit(output,0);
307 else
308 OutputBit(output,1);
3e87ef69 309 power = 1<<(AliL3DataCompressor::GetNShapeBits()-1);
2357bb38 310 if(abs(temp) >= power)
311 {
312 shapeo++;
313 temp=power - 1;
314 }
3e87ef69 315 OutputBits(output,abs(temp),(AliL3DataCompressor::GetNShapeBits()-1));
029912b7 316 }
3e87ef69 317
318 clustercount++;
5bf93292 319 }
5bf93292 320 }
95a00d93 321
5bf93292 322 fclose(input);
323 CloseOutputBitFile(output);
f64c4364 324 if(pado || timeo || chargeo || shapeo)
325 {
326 cout<<endl<<"Saturations: "<<endl
327 <<"Pad "<<pado<<endl
328 <<"Time "<<timeo<<endl
329 <<"Charge "<<chargeo<<endl
3e87ef69 330 <<"Shape "<<shapeo<<endl<<endl;
f64c4364 331 }
3e87ef69 332 return kTRUE;
5bf93292 333}
334
3e87ef69 335Bool_t AliL3Compress::ExpandFile()
5bf93292 336{
be6ddb10 337 Char_t fname[100];
3e87ef69 338 if(fEvent<0)
339 sprintf(fname,"%s/comp/tracks_c_%d_%d.raw",fPath,fSlice,fPatch);
340 else
341 sprintf(fname,"%s/comp/tracks_c_%d_%d_%d.raw",fPath,fEvent,fSlice,fPatch);
be6ddb10 342 BIT_FILE *input = OpenInputBitFile(fname);
343
3e87ef69 344 if(fEvent<0)
345 sprintf(fname,"%s/comp/tracks_u_%d_%d.raw",fPath,fSlice,fPatch);
346 else
347 sprintf(fname,"%s/comp/tracks_u_%d_%d_%d.raw",fPath,fEvent,fSlice,fPatch);
be6ddb10 348 FILE *output = fopen(fname,"w");
349 if(!output)
350 {
351 cerr<<"AliL3Compress::ExpandFile() : Error opening file: "<<fname<<endl;
3e87ef69 352 return kFALSE;
be6ddb10 353 }
95a00d93 354
355 AliL3TrackModel trackmodel;
356 AliL3ClusterModel *clusters=0;
029912b7 357 Int_t count=0;
5bf93292 358
4a838220 359 clusters = new AliL3ClusterModel[(AliL3Transform::GetNRows(fPatch))];
95a00d93 360 while(!feof(input->file))
5bf93292 361 {
029912b7 362 input->mask=0x80;//make sure we read a new byte from file.
95a00d93 363
029912b7 364 //Read and write track:
95a00d93 365 if(fread(&trackmodel,sizeof(AliL3TrackModel),1,input->file)!=1) break;
366 fwrite(&trackmodel,sizeof(AliL3TrackModel),1,output);
2357bb38 367
368 memset(clusters,0,AliL3Transform::GetNRows(fPatch)*sizeof(AliL3ClusterModel));
3e87ef69 369 Int_t origslice=-1,clustercount=0;
0a86fbb7 370 for(Int_t i=0; i<AliL3Transform::GetNRows(fPatch); i++)
95a00d93 371 {
372 Int_t temp,sign;
029912b7 373
374 //Read empty flag:
95a00d93 375 temp = InputBit(input);
376 if(!temp)
377 {
378 clusters[i].fPresent=kFALSE;
379 continue;
380 }
381 clusters[i].fPresent=kTRUE;
029912b7 382
3e87ef69 383 //Read slice information
384 if(clustercount==0)
385 {
386 temp = InputBits(input,6);
387 clusters[i].fSlice = temp;
388 origslice = temp;
389 }
390 else
391 {
392 temp = InputBit(input);
393 if(!temp)//no change
394 clusters[i].fSlice = origslice;
395 else
396 {
397 temp = InputBits(input,6);//read new slice
398 clusters[i].fSlice = temp;
399 origslice = temp;//store new slice
400 }
401 }
402
029912b7 403 //Read time information:
95a00d93 404 sign=InputBit(input);
3e87ef69 405 temp = InputBits(input,(AliL3DataCompressor::GetNTimeBits()-1));
95a00d93 406 if(!sign)
407 temp*=-1;
408 clusters[i].fDTime = temp;
029912b7 409
410 //Read pad information:
95a00d93 411 sign=InputBit(input);
3e87ef69 412 temp = InputBits(input,(AliL3DataCompressor::GetNPadBits()-1));
95a00d93 413 if(!sign)
414 temp*=-1;
415 clusters[i].fDPad = temp;
029912b7 416
417 //Read charge information:
3e87ef69 418 temp=InputBits(input,(AliL3DataCompressor::GetNChargeBits()));
95a00d93 419 clusters[i].fDCharge = temp;
029912b7 420
2357bb38 421 if(fWriteShape)
422 {
423 //Read shape information:
424 sign = InputBit(input);
3e87ef69 425 temp = InputBits(input,(AliL3DataCompressor::GetNShapeBits()-1));
2357bb38 426 if(!sign)
427 temp*=-1;
428 clusters[i].fDSigmaY2 = temp;
429
430 sign = InputBit(input);
3e87ef69 431 temp = InputBits(input,(AliL3DataCompressor::GetNShapeBits()-1));
2357bb38 432 if(!sign)
433 temp*=-1;
434 clusters[i].fDSigmaZ2 = temp;
435 }
3e87ef69 436 clustercount++;
95a00d93 437 }
029912b7 438 count++;
0a86fbb7 439 //fwrite(clusters,(trackmodel.fNClusters)*sizeof(AliL3ClusterModel),1,output);
440 fwrite(clusters,AliL3Transform::GetNRows(fPatch)*sizeof(AliL3ClusterModel),1,output);
5bf93292 441 }
029912b7 442
443 delete [] clusters;
95a00d93 444 fclose(output);
5bf93292 445 CloseInputBitFile(input);
3e87ef69 446 return kTRUE;
5bf93292 447}
029912b7 448
3e87ef69 449void AliL3Compress::PrintCompRatio(ofstream *outfile)
029912b7 450{
029912b7 451 AliL3MemHandler *mem = new AliL3MemHandler();
3e87ef69 452 Char_t fname[1024];
453 UInt_t remain_size=0,digit_size=0;
454 for(Int_t i=0; i<36; i++)
029912b7 455 {
3e87ef69 456 if(fEvent<0)
457 sprintf(fname,"%s/comp/remains_%d_%d.raw",fPath,i,-1);
458 else
459 sprintf(fname,"%s/comp/remains_%d_%d_%d.raw",fPath,fEvent,i,-1);
460 mem->SetBinaryInput(fname);
461 remain_size += mem->GetFileSize();
462 mem->CloseBinaryInput();
463
464 sprintf(fname,"%s/binaries/digits_c8_%d_%d_%d.raw",fPath,fEvent,i,-1);
465 mem->SetBinaryInput(fname);
466 digit_size += mem->GetFileSize();
467 mem->CloseBinaryInput();
029912b7 468 }
469
4a838220 470
3e87ef69 471 if(fEvent<0)
472 sprintf(fname,"%s/comp/tracks_c_%d_%d.raw",fPath,fSlice,fPatch);
473 else
474 sprintf(fname,"%s/comp/tracks_c_%d_%d_%d.raw",fPath,fEvent,fSlice,fPatch);
029912b7 475
3e87ef69 476 mem->SetBinaryInput(fname);
477 UInt_t compress_size = mem->GetFileSize();
92a876e2 478 mem->CloseBinaryInput();
92a876e2 479
3e87ef69 480 if(digit_size==0)
92a876e2 481 {
0a86fbb7 482 cerr<<"AliL3Compress::PrintCompRatio : Zero digit size, not able to obtain comp. ratios!"<<endl;
483 return;
92a876e2 484 }
92a876e2 485
3e87ef69 486 Float_t compratio = (Float_t)(compress_size + remain_size)/(Float_t)digit_size;
f64c4364 487 if(outfile)
f64c4364 488 {
3e87ef69 489 ofstream &out = *outfile;
490 out<<compress_size<<' '<<remain_size<<' '<<digit_size<<endl;
f64c4364 491 }
92a876e2 492
3e87ef69 493 cout<<"=========================================="<<endl;
494 cout<<"Original digits size : "<<digit_size/1000<<" kByte ( 100 % )"<<endl;
495 cout<<"Compressed file size : "<<compress_size/1000<<" kByte ( "<<(Float_t)compress_size*100/(Float_t)digit_size<<" % )"<<endl;
496 cout<<"Remainig file size : "<<remain_size/1000<<" kByte ( "<<(Float_t)remain_size*100/(Float_t)digit_size<<" % )"<<endl;
497 cout<<"---------------------- "<<endl;
498 cout<<"Compression ratio : "<<compratio*100<<" %"<<endl;
499 cout<<"=========================================="<<endl;
be6ddb10 500}