3 // Author: Anders Vestbo <mailto:vestbo@fi.uib.no>
4 //*-- Copyright © ASV
6 #include "AliL3DataHandler.h"
7 #include "AliL3Logging.h"
8 #include "AliTransBit.h"
12 //_____________________________________________________________
15 // HLT Binary file handler.
17 // This class have more or less the same functionality as AliL3MemHandler,
18 // except that it handles 8 bit ADC-values. Reading and writing is done in the same way
19 // as illustrated in example 1) and 2) in AliL3MemHandler.
21 // For converting 10 bit data files to 8 bit data files, do:
23 // AliL3MemHandler *file = new AliL3DataHandler();
24 // file->Init(slice,patch);
25 // file->SetBinaryInput(inputfile); //10 bit data file
26 // file->SetBinaryOutput(outputfile); //8 bit data file
27 // file->Convert10to8Bit();
28 // file->CloseBinaryInput();
29 // file->CloseBinaryOutput();
32 // Compress data format
33 // --------------------
35 // The data is RLE encoded, using _8_bit representation of the ADC-values.
36 // Conversion is done in the class AliTransBit.
38 // In the beginning of every row, the row number if written and the number of pads
39 // containing data on that row. For every pad with data the pad number is written,
40 // and then comes the ADC-values on that pad. When a serie of zeros occure, a zero
41 // is written followed by the number of zeros. If the number of zeros is more than
42 // 255 (8 bit), another 8 bit word is written for the remaining. At the end of one
43 // pad, 2 zeros are written. Example:
45 // ROW PAD 0 NZEROS ADC ADC ADC ADC 0 NZEROS ADC ADC 0 0
47 // Everything is written using 8 bit;
48 // (ROW < 176, PAD < 200, ADC < 255, if(NZEROS > 255) write 2 words;)
50 ClassImp(AliL3DataHandler)
53 AliL3DataHandler::AliL3DataHandler()
58 AliL3DataHandler::~AliL3DataHandler()
61 delete fBitTransformer;
65 void AliL3DataHandler::Convert10to8Bit()
67 //Convert from 10 bit data in inputfile, to 8 bit data written to outputfile.
71 LOG(AliL3Log::kError,"AliL3DataHandler::Convert10to8Bit","File")
72 <<AliL3Log::kHex<<"Pointer to input file : "<<(Int_t)fInBinary<<ENDLOG;
77 LOG(AliL3Log::kError,"AliL3DataHandler::Convert10to8Bit","File")
78 <<AliL3Log::kHex<<"Pointer to output file : "<<(Int_t)fOutBinary<<ENDLOG;
83 //Initialize the bit transformation class:
84 fBitTransformer = new AliTransBit_v1();
85 Int_t b0=10; // original number of bits
86 Int_t b1=8; // compressed
87 fBitTransformer->SetBits(b0,b1);
88 fBitTransformer->FindOptimumX0();
89 fBitTransformer->Update();
91 AliL3MemHandler *memory = new AliL3MemHandler();
92 memory->Init(fSlice,fPatch);
93 memory->SetBinaryInput(fInBinary);
95 AliL3DigitRowData *data = (AliL3DigitRowData*)memory->CompBinary2Memory(nrow);
97 Memory2CompBinary(nrow,data);
102 Bool_t AliL3DataHandler::Memory2CompBinary(UInt_t nrow,AliL3DigitRowData *data)
104 //Compress data by RLE, and write to a binary file.
106 UInt_t size = GetCompMemorySize(nrow,data);
107 Byte_t *comp = Allocate(size);
108 Memory2CompMemory(nrow,data,comp);
109 if(!CompMemory2CompBinary(nrow,comp,size))
111 LOG(AliL3Log::kError,"AliL3DataHandler::Memory2CompBinary","File")
112 <<"Error writing to file "<<ENDLOG;
119 AliL3DigitRowData *AliL3DataHandler::CompBinary2Memory(UInt_t &nrow)
121 //Read RLE compressed binary file, unpack it and return pointer to it.
123 AliL3MemHandler *memory = new AliL3MemHandler();
124 memory->SetBinaryInput(fInBinary);
125 Byte_t *comp = memory->Allocate();
127 if(!CompBinary2CompMemory(nrow,comp))
129 LOG(AliL3Log::kError,"AliL3DataHandler::CompBinary2Memory","File")
130 <<"Error reading from file "<<ENDLOG;
133 UInt_t size = GetMemorySize(nrow,comp);
134 AliL3DigitRowData *data = (AliL3DigitRowData*)Allocate(size);
135 CompMemory2Memory(nrow,data,comp);
140 void AliL3DataHandler::Write(Byte_t *comp,UInt_t &index,UShort_t value)
142 //Write one value (=1 byte) to array comp.
146 LOG(AliL3Log::kFatal,"AliL3DataHandler::Write","Bitnumbers")
147 <<"Value too big for storing in 1 byte, something is wrong: "<<value<<" "<<index<<ENDLOG;
149 comp[index] = (Byte_t)value;
153 Short_t AliL3DataHandler::Read(Byte_t *comp,UInt_t &index)
155 //Read one value (=1 byte) from array comp
157 Short_t value = (Short_t)comp[index];
162 Short_t AliL3DataHandler::Test(Byte_t *comp,UInt_t index)
164 //Check the value (=1 byte) in array comp, but not read.
166 Short_t value = (Short_t)comp[index];
170 Bool_t AliL3DataHandler::Memory2CompMemory(UInt_t nrow,AliL3DigitRowData *data,Byte_t *comp)
176 LOG(AliL3Log::kError,"AliL3DataHandler::Memory2CompMemory","Data")
177 <<AliL3Log::kHex<<" Pointer to data = "<<(Int_t)data<<ENDLOG;
182 LOG(AliL3Log::kError,"AliL3DataHandler::Memory2CompMemory","Data")
183 <<AliL3Log::kHex<<" Pointer to compressed data = "<<(Int_t)comp<<ENDLOG;
187 AliL3DigitRowData *rowPt = data;
192 for(UInt_t i=0; i<nrow; i++)
194 //Write the row number:
195 UShort_t value = rowPt->fRow;
196 Write(comp,index,value);
198 UShort_t number_of_pads=0;
199 UShort_t max_pad = 0;
201 for(Int_t j=0; j<200; j++)
203 for(UInt_t dig=0; dig<rowPt->fNDigit; dig++)
205 if(rowPt->fDigitData[dig].fPad < 200)
206 npads[rowPt->fDigitData[dig].fPad]++;
208 for(Int_t j=0; j<200; j++)
217 //Write the number of pads on this row:
218 Write(comp,index,number_of_pads);
221 for(UShort_t pad=0; pad <= max_pad; pad++)
224 if(digit >= rowPt->fNDigit || rowPt->fDigitData[digit].fPad != pad)
227 //Write the current pad:
228 Write(comp,index,pad);
230 if(digit < rowPt->fNDigit && rowPt->fDigitData[digit].fPad == pad)
232 if(rowPt->fDigitData[digit].fTime > 0)
234 //If first time!=0, write the number of following zeros,
235 //and then the first timebin:
238 //Check if we have to use more than 1 byte to write the zeros:
239 Int_t number_of_zero_intervals=0;
240 if(rowPt->fDigitData[digit].fTime > 255)
242 number_of_zero_intervals++;
243 Write(comp,index,255);
244 if(rowPt->fDigitData[digit].fTime > 2*255)
246 Write(comp,index,255);
247 number_of_zero_intervals++;
250 Write(comp,index,(rowPt->fDigitData[digit].fTime - number_of_zero_intervals*255));
254 while(digit < rowPt->fNDigit && rowPt->fDigitData[digit].fPad == pad)
256 UShort_t charge = rowPt->fDigitData[digit].fCharge;
259 charge = fBitTransformer->Get0to1(charge); //Transform 10 to 8 bit.
261 //Check for saturation:
264 LOG(AliL3Log::kWarning,"AliL3DataHandler::Memory2CompMemory","Digit")
265 <<"ADC-value saturated : "<<charge<<ENDLOG;
270 Write(comp,index,charge);
272 //Check if the next digit is zero:
273 if(digit+1 < rowPt->fNDigit && rowPt->fDigitData[digit+1].fPad == pad)
275 if(rowPt->fDigitData[digit].fTime + 1 != rowPt->fDigitData[digit+1].fTime)
278 UShort_t nzero = rowPt->fDigitData[digit+1].fTime - (rowPt->fDigitData[digit].fTime + 1);
280 //Check if we have to use more than one byte to write the zeros:
281 Int_t number_of_zero_intervals=0;
284 number_of_zero_intervals++;
285 Write(comp,index,255);
288 Write(comp,index,255);
289 number_of_zero_intervals++;
292 Write(comp,index,(nzero - number_of_zero_intervals*255));
298 //This is the end of the pad, state it with 2 zeros:
303 UpdateRowPointer(rowPt);
307 return index * sizeof(Byte_t);
311 UInt_t AliL3DataHandler::GetCompMemorySize(UInt_t nrow,AliL3DigitRowData *data)
313 //Calculate the size (in bytes) of RLE data.
317 LOG(AliL3Log::kError,"AliL3DataHandler::GetCompMemorySize","Data")
318 <<AliL3Log::kHex<<" Data pointer = "<<(Int_t)data<<ENDLOG;
322 AliL3DigitRowData *rowPt = data;
327 for(UInt_t i=0;i<nrow;i++)
329 //Write the row number:
333 UShort_t number_of_pads = 0;
335 for(Int_t j=0; j<200; j++)
338 for(UInt_t dig=0; dig<rowPt->fNDigit; dig++)
340 if(rowPt->fDigitData[dig].fPad <200)
341 npads[rowPt->fDigitData[dig].fPad]++;
343 for(Int_t j=0; j<200; j++)
352 //Write the number of pads on this row:
356 for(UShort_t pad=0; pad <= max_pad; pad++)
358 if(digit>=rowPt->fNDigit || rowPt->fDigitData[digit].fPad != pad)
361 //Write the current pad:
365 if(digit<rowPt->fNDigit && rowPt->fDigitData[digit].fPad == pad)
367 if(rowPt->fDigitData[digit].fTime > 0)
369 //If first time!=0, write the number of following zeros,
370 //and then the first timebin:
375 //Check if we have to use more than 1 byte to write the zeros:
376 if(rowPt->fDigitData[digit].fTime > 255)
378 if(rowPt->fDigitData[digit].fTime > 2*255)
383 while(digit < rowPt->fNDigit && rowPt->fDigitData[digit].fPad == pad)
388 //Check if the next digit is zero:
389 if(digit+1 < rowPt->fNDigit && rowPt->fDigitData[digit+1].fPad == pad)
391 if(rowPt->fDigitData[digit].fTime +1 != rowPt->fDigitData[digit+1].fTime)
396 //Check if we have to use more than 1 byte to write the zeros:
397 UInt_t nzeros = rowPt->fDigitData[digit+1].fTime - rowPt->fDigitData[digit].fTime + 1;
407 //Mark the end of the pad with 2 zeros:
412 UpdateRowPointer(rowPt);
415 return index * sizeof(Byte_t);
419 UInt_t AliL3DataHandler::CompMemory2Memory(UInt_t nrow,AliL3DigitRowData *data,Byte_t *comp)
421 //Uncompress RLE data.
425 LOG(AliL3Log::kError,"AliL3DataHandler::CompMemory2Memory","Array")
426 <<AliL3Log::kHex<<"Pointer to data: "<<(Int_t)data<<ENDLOG;
431 LOG(AliL3Log::kError,"AliL3DataHandler::CompMemory2Memory","Array")
432 <<AliL3Log::kHex<<"Pointer to compressed data: "<<(Int_t)data<<ENDLOG;
438 AliL3DigitRowData *rowPt = data;
441 UShort_t pad,time,charge;
442 for(UInt_t i=0; i<nrow; i++)
447 rowPt->fRow = Read(comp,index);
449 //Read the number of pads:
450 UShort_t npads = Read(comp,index);
452 for(UShort_t p=0; p<npads; p++)
454 //Read the current pad:
455 pad = Read(comp,index);
460 if(Test(comp,index) == 0) //Zeros
462 //Read the first zero
464 if(Test(comp,index) == 0)//end of pad.
466 time = Read(comp,index);
469 if( (time = Read(comp,index)) == 255 )
470 if( (time += Read(comp,index)) == 2*255)
471 time += Read(comp,index);
475 while( (charge = Read(comp,index)) != 0)
477 rowPt->fDigitData[ndigit].fPad = pad;
478 rowPt->fDigitData[ndigit].fTime = time;
479 rowPt->fDigitData[ndigit].fCharge = charge;
483 if(Test(comp,index) == 0)
485 Read(comp,index); //end of pad
489 if( (time_shift = Read(comp,index)) == 255)
490 if( (time_shift += Read(comp,index)) == 2*255)
491 time_shift += Read(comp,index);
495 rowPt->fNDigit = ndigit;
496 UpdateRowPointer(rowPt);
497 outsize += sizeof(AliL3DigitData)*ndigit + sizeof(AliL3DigitRowData);
503 UInt_t AliL3DataHandler::GetMemorySize(UInt_t nrow,Byte_t *comp)
505 //Calculate size (in bytes) of unpacked data.
510 for(UInt_t i=0; i<nrow; i++)
512 UInt_t ndigit=0;//Digits on this row.
517 UShort_t npad = Read(comp,index);
519 for(UShort_t pad=0; pad<npad; pad++)
521 //Read the pad number:
525 if(Test(comp,index)==0) //Zeros are coming
528 if(Test(comp,index) == 0)
530 Read(comp,index); //This was the end of pad.
533 if(Read(comp,index) == 255) //There can be up to 3 bytes with zero coding.
534 if(Read(comp,index) == 255)
540 while(Read(comp,index) != 0) ndigit++;
542 if(Test(comp,index) == 0)
544 Read(comp,index); //2 zeros = end of pad.
547 if(Read(comp,index) == 255) //There can be up to 3 bytes with zero coding.
548 if(Read(comp,index) == 255)
554 Int_t size = sizeof(AliL3DigitData)*ndigit + sizeof(AliL3DigitRowData);
560 Bool_t AliL3DataHandler::CompBinary2CompMemory(UInt_t &nrow,Byte_t *comp)
562 //Read RLE data from binary file into array comp.
565 UInt_t size = GetFileSize() - 2;
567 if(fread(&type,1,1,fInBinary)!=1) return kFALSE;
570 LOG(AliL3Log::kError,"AliL3DataHandler::CompBinary2CompMemory","Filetype")
571 <<"Inputfile does not seem to contain 8 bit data : "<<type<<ENDLOG;
574 if(fread(&nrow,1,1,fInBinary)!=1) return kFALSE;
575 if(fread(comp,size,1,fInBinary)!=1) return kFALSE;
580 Bool_t AliL3DataHandler::CompMemory2CompBinary(UInt_t nrow,Byte_t *comp,UInt_t size)
582 //Write RLE data in comp to binary file.
583 //In order to distinguish these files from 10 bit data,
584 //a zero is written to the beginning of the file.
586 Byte_t length = (Byte_t)nrow;
588 if(fwrite(&type,1,1,fOutBinary)!=1) return kFALSE; //Write a zero, to mark that this file contains 8 bit data.
589 if(fwrite(&length,1,1,fOutBinary)!=1) return kFALSE;
590 if(fwrite(comp,size,1,fOutBinary)!=1) return kFALSE;