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