3 // Author: Anders Vestbo <mailto:vestbo@fi.uib.no>
4 //*-- Copyright © ASV
6 #include "AliL3StandardIncludes.h"
8 #include "AliL3DataHandler.h"
9 #include "AliL3Logging.h"
10 #include "AliL3TransBit.h"
11 #include "AliL3Transform.h"
17 //_____________________________________________________________
20 // HLT Binary file handler.
22 // This class have more or less the same functionality as AliL3MemHandler,
23 // except that it handles 8 bit ADC-values. Reading and writing is done in the same way
24 // as illustrated in example 1) and 2) in AliL3MemHandler.
26 // For converting 10 bit data files to 8 bit data files, do:
28 // AliL3MemHandler *file = new AliL3DataHandler();
29 // file->Init(slice,patch);
30 // file->SetBinaryInput(inputfile); //10 bit data file
31 // file->SetBinaryOutput(outputfile); //8 bit data file
32 // file->Convert10to8Bit();
33 // file->CloseBinaryInput();
34 // file->CloseBinaryOutput();
37 // Compress data format
38 // --------------------
40 // The data is RLE encoded, using _8_bit representation of the ADC-values.
41 // Conversion is done in the class AliL3TransBit.
43 // In the beginning of every row, the row number if written and the number of pads
44 // containing data on that row. For every pad with data the pad number is written,
45 // and then comes the ADC-values on that pad. When a serie of zeros occure, a zero
46 // is written followed by the number of zeros. If the number of zeros is more than
47 // 255 (8 bit), another 8 bit word is written for the remaining. At the end of one
48 // pad, 2 zeros are written. Example:
50 // ROW NPADSWITHDATA PAD 0 NZEROS ADC ADC ADC ADC 0 NZEROS ADC ADC 0 0
52 // Everything is written using 8 bit;
53 // (ROW < 176, PAD < 200, ADC < 255, if(NZEROS > 255) write 2 words;)
55 ClassImp(AliL3DataHandler)
57 AliL3DataHandler::AliL3DataHandler()
60 LOG(AliL3Log::kInformational,"AliL3DataHandler::AliL3DataHandler","Data format")
61 <<"8 bit data handler initialized"<<ENDLOG;
64 AliL3DataHandler::~AliL3DataHandler()
67 delete fBitTransformer;
70 void AliL3DataHandler::Convert10to8Bit()
72 //Convert from 10 bit data in inputfile, to 8 bit data written to outputfile.
76 LOG(AliL3Log::kError,"AliL3DataHandler::Convert10to8Bit","File")
77 <<AliL3Log::kHex<<"Pointer to input file : "<<(Int_t)fInBinary<<ENDLOG;
82 LOG(AliL3Log::kError,"AliL3DataHandler::Convert10to8Bit","File")
83 <<AliL3Log::kHex<<"Pointer to output file : "<<(Int_t)fOutBinary<<ENDLOG;
88 //Initialize the bit transformation class:
89 fBitTransformer = new AliL3TransBit_v1();
90 Int_t b0=10; // original number of bits
91 Int_t b1=8; // compressed
92 fBitTransformer->SetBits(b0,b1);
93 fBitTransformer->FindOptimumX0();
94 fBitTransformer->Update();
96 AliL3MemHandler *memory = new AliL3MemHandler();
97 memory->Init(fSlice,fPatch);
98 memory->SetBinaryInput(fInBinary);
100 AliL3DigitRowData *data = (AliL3DigitRowData*)memory->CompBinary2Memory(nrow);
102 Memory2CompBinary(nrow,data);
107 Bool_t AliL3DataHandler::Memory2CompBinary(UInt_t nrow,AliL3DigitRowData *data)
109 //Compress data by RLE, and write to a binary file.
111 UInt_t size = GetCompMemorySize(nrow,data);
112 Byte_t *comp = Allocate(size);
113 Memory2CompMemory(nrow,data,comp);
114 if(!CompMemory2CompBinary(nrow,comp,size))
116 LOG(AliL3Log::kError,"AliL3DataHandler::Memory2CompBinary","File")
117 <<"Error writing to file "<<ENDLOG;
124 AliL3DigitRowData *AliL3DataHandler::CompBinary2Memory(UInt_t &nrow)
126 //Read RLE compressed binary file, unpack it and return pointer to it.
128 AliL3MemHandler *memory = new AliL3MemHandler();
129 memory->SetBinaryInput(fInBinary);
130 Byte_t *comp = memory->Allocate();
132 if(!CompBinary2CompMemory(nrow,comp))
134 LOG(AliL3Log::kError,"AliL3DataHandler::CompBinary2Memory","File")
135 <<"Error reading from file "<<ENDLOG;
139 UInt_t size = GetMemorySize(nrow,comp);
140 AliL3DigitRowData *data = (AliL3DigitRowData*)Allocate(size);
141 CompMemory2Memory(nrow,data,comp);
146 void AliL3DataHandler::Write(Byte_t *comp,UInt_t &index,UShort_t value)
148 //Write one value (=1 byte) to array comp.
152 LOG(AliL3Log::kFatal,"AliL3DataHandler::Write","Bitnumbers")
153 <<"Value too big for storing in 1 byte, something is wrong: "<<value<<" "<<index<<ENDLOG;
155 comp[index] = (Byte_t)value;
159 Short_t AliL3DataHandler::Read(Byte_t *comp,UInt_t &index)
161 //Read one value (=1 byte) from array comp
163 Short_t value = (Short_t)comp[index];
168 Short_t AliL3DataHandler::Test(Byte_t *comp,UInt_t index)
170 //Check the value (=1 byte) in array comp, but not read.
172 Short_t value = (Short_t)comp[index];
176 Bool_t AliL3DataHandler::Memory2CompMemory(UInt_t nrow,AliL3DigitRowData *data,Byte_t *comp)
182 LOG(AliL3Log::kError,"AliL3DataHandler::Memory2CompMemory","Data")
183 <<AliL3Log::kHex<<" Pointer to data = "<<(Int_t)data<<ENDLOG;
188 LOG(AliL3Log::kError,"AliL3DataHandler::Memory2CompMemory","Data")
189 <<AliL3Log::kHex<<" Pointer to compressed data = "<<(Int_t)comp<<ENDLOG;
193 AliL3DigitRowData *rowPt = data;
198 for(UInt_t i=0; i<nrow; i++)
200 //Write the row number:
201 UShort_t value = rowPt->fRow;
202 Write(comp,index,value);
204 UShort_t number_of_pads=0;
205 UShort_t max_pad = 0;
207 for(Int_t j=0; j<200; j++)
209 for(UInt_t dig=0; dig<rowPt->fNDigit; dig++)
211 if(rowPt->fDigitData[dig].fPad < 200)
212 npads[rowPt->fDigitData[dig].fPad]++;
214 for(Int_t j=0; j<200; j++)
223 //Write the number of pads on this row:
224 Write(comp,index,number_of_pads);
227 for(UShort_t pad=0; pad <= max_pad; pad++)
230 if(digit >= rowPt->fNDigit || rowPt->fDigitData[digit].fPad != pad)
233 //Write the current pad:
234 Write(comp,index,pad);
236 if(digit < rowPt->fNDigit && rowPt->fDigitData[digit].fPad == pad)
238 if(rowPt->fDigitData[digit].fTime > 0)
240 //If first time!=0, write the number of following zeros,
241 //and then the first timebin:
244 //Check if we have to use more than 1 byte to write the zeros:
245 Int_t number_of_zero_intervals=0;
246 if(rowPt->fDigitData[digit].fTime >= 255)
248 number_of_zero_intervals++;
249 Write(comp,index,255);
250 if(rowPt->fDigitData[digit].fTime >= 2*255)
252 cerr<<"AliL3DataHandler::Memory2CompMemory : Should not happen "<<(Int_t)rowPt->fDigitData[digit].fTime<<endl;
253 Write(comp,index,255);
254 number_of_zero_intervals++;
257 Write(comp,index,(rowPt->fDigitData[digit].fTime - number_of_zero_intervals*255));
261 while(digit < rowPt->fNDigit && rowPt->fDigitData[digit].fPad == pad)
263 UShort_t charge = rowPt->fDigitData[digit].fCharge;
266 charge = fBitTransformer->Get0to1(charge); //Transform 10 to 8 bit.
268 //Check for saturation:
271 LOG(AliL3Log::kWarning,"AliL3DataHandler::Memory2CompMemory","Digit")
272 <<"ADC-value saturated : "<<charge<<ENDLOG;
277 Write(comp,index,charge);
279 //Check if the next digit is zero:
280 if(digit+1 < rowPt->fNDigit && rowPt->fDigitData[digit+1].fPad == pad)
282 if(rowPt->fDigitData[digit].fTime + 1 != rowPt->fDigitData[digit+1].fTime)
285 UShort_t nzero = rowPt->fDigitData[digit+1].fTime - (rowPt->fDigitData[digit].fTime + 1);
287 //Check if we have to use more than one byte to write the zeros:
288 Int_t number_of_zero_intervals=0;
291 number_of_zero_intervals++;
292 Write(comp,index,255);
295 cerr<<"AliL3DataHandler::Memory2CompMemory : Should not happen "<<(Int_t)rowPt->fDigitData[digit].fTime<<endl;
296 Write(comp,index,255);
297 number_of_zero_intervals++;
300 Write(comp,index,(nzero - number_of_zero_intervals*255));
306 //This is the end of the pad, state it with 2 zeros:
311 UpdateRowPointer(rowPt);
315 return index * sizeof(Byte_t);
319 UInt_t AliL3DataHandler::GetCompMemorySize(UInt_t nrow,AliL3DigitRowData *data)
321 //Calculate the size (in bytes) of RLE data.
325 LOG(AliL3Log::kError,"AliL3DataHandler::GetCompMemorySize","Data")
326 <<AliL3Log::kHex<<" Data pointer = "<<(Int_t)data<<ENDLOG;
330 AliL3DigitRowData *rowPt = data;
335 for(UInt_t i=0;i<nrow;i++)
337 //Write the row number:
341 UShort_t number_of_pads = 0;
343 for(Int_t j=0; j<200; j++)
346 for(UInt_t dig=0; dig<rowPt->fNDigit; dig++)
348 if(rowPt->fDigitData[dig].fPad <200)
349 npads[rowPt->fDigitData[dig].fPad]++;
351 for(Int_t j=0; j<200; j++)
360 //Write the number of pads on this row:
364 for(UShort_t pad=0; pad <= max_pad; pad++)
366 if(digit>=rowPt->fNDigit || rowPt->fDigitData[digit].fPad != pad)
369 //Write the current pad:
373 if(digit<rowPt->fNDigit && rowPt->fDigitData[digit].fPad == pad)
375 if(rowPt->fDigitData[digit].fTime > 0)
377 //If first time!=0, write the number of following zeros,
378 //and then the first timebin:
383 //Check if we have to use more than 1 byte to write the zeros:
384 if(rowPt->fDigitData[digit].fTime >= 255)
386 if(rowPt->fDigitData[digit].fTime >= 2*255)
391 while(digit < rowPt->fNDigit && rowPt->fDigitData[digit].fPad == pad)
396 //Check if the next digit is zero:
397 if(digit+1 < rowPt->fNDigit && rowPt->fDigitData[digit+1].fPad == pad)
399 if(rowPt->fDigitData[digit].fTime +1 != rowPt->fDigitData[digit+1].fTime)
404 //Check if we have to use more than 1 byte to write the zeros:
405 UInt_t nzeros = rowPt->fDigitData[digit+1].fTime - rowPt->fDigitData[digit].fTime + 1;
415 //Mark the end of the pad with 2 zeros:
420 UpdateRowPointer(rowPt);
423 return index * sizeof(Byte_t);
427 UInt_t AliL3DataHandler::CompMemory2Memory(UInt_t nrow,AliL3DigitRowData *data,Byte_t *comp)
429 //Uncompress RLE data.
433 LOG(AliL3Log::kError,"AliL3DataHandler::CompMemory2Memory","Array")
434 <<AliL3Log::kHex<<"Pointer to data: "<<(Int_t)data<<ENDLOG;
439 LOG(AliL3Log::kError,"AliL3DataHandler::CompMemory2Memory","Array")
440 <<AliL3Log::kHex<<"Pointer to compressed data: "<<(Int_t)data<<ENDLOG;
446 AliL3DigitRowData *rowPt = data;
449 UShort_t pad,time,charge;
450 for(UInt_t i=0; i<nrow; i++)
455 rowPt->fRow = Read(comp,index);
457 //Read the number of pads:
458 UShort_t npads = Read(comp,index);
460 for(UShort_t p=0; p<npads; p++)
462 //Read the current pad:
463 pad = Read(comp,index);
468 if(Test(comp,index) == 0) //Zeros
470 //Read the first zero
474 if(Test(comp,index) == 0)//end of pad.
476 time = Read(comp,index);
479 if( (time = Read(comp,index)) == 255 )
480 if( (time += Read(comp,index)) == 2*255)
481 time += Read(comp,index);
486 while( (charge = Read(comp,index)) != 0)
488 if(time >= AliL3Transform::GetNTimeBins())
489 cerr<<"AliL3DataHandler::CompMemory2Memory : Time out of range "<<time<<endl;
490 rowPt->fDigitData[ndigit].fPad = pad;
491 rowPt->fDigitData[ndigit].fTime = time;
492 rowPt->fDigitData[ndigit].fCharge = charge;
494 if(Test(comp,index) != 0)
497 if(Test(comp,index) == 0)
499 Read(comp,index); //end of pad
503 if( (time_shift = Read(comp,index)) == 255)
504 if( (time_shift += Read(comp,index)) == 2*255)
505 time_shift += Read(comp,index);
510 rowPt->fNDigit = ndigit;
511 UpdateRowPointer(rowPt);
512 outsize += sizeof(AliL3DigitData)*ndigit + sizeof(AliL3DigitRowData);
518 UInt_t AliL3DataHandler::GetMemorySize(UInt_t nrow,Byte_t *comp)
520 //Calculate size (in bytes) of unpacked data.
525 for(UInt_t i=0; i<nrow; i++)
527 UInt_t ndigit=0;//Digits on this row.
532 UShort_t npad = Read(comp,index);
534 for(UShort_t pad=0; pad<npad; pad++)
536 //Read the pad number:
540 if(Test(comp,index)==0) //Zeros are coming
543 if(Test(comp,index) == 0)
545 Read(comp,index); //This was the end of pad.
548 if(Read(comp,index) == 255) //There can be up to 3 bytes with zero coding.
549 if(Read(comp,index) == 255)
555 while(Read(comp,index) != 0) ndigit++;
557 if(Test(comp,index) == 0)
559 Read(comp,index); //2 zeros = end of pad.
562 if(Read(comp,index) == 255) //There can be up to 3 bytes with zero coding.
563 if(Read(comp,index) == 255)
569 Int_t size = sizeof(AliL3DigitData)*ndigit + sizeof(AliL3DigitRowData);
575 Bool_t AliL3DataHandler::CompBinary2CompMemory(UInt_t &nrow,Byte_t *comp)
577 //Read RLE data from binary file into array comp.
579 UInt_t size = GetFileSize() - 2;
581 if(fread(&type,1,1,fInBinary)!=1) return kFALSE;
584 LOG(AliL3Log::kError,"AliL3DataHandler::CompBinary2CompMemory","Filetype")
585 <<"Inputfile does not seem to contain 8 bit data : "<<type<<ENDLOG;
588 if(fread(&nrow,1,1,fInBinary)!=1) return kFALSE;
589 if(fread(comp,size,1,fInBinary)!=1) return kFALSE;
594 Bool_t AliL3DataHandler::CompMemory2CompBinary(UInt_t nrow,Byte_t *comp,UInt_t size)
596 //Write RLE data in comp to binary file.
597 //In order to distinguish these files from 10 bit data,
598 //a zero is written to the beginning of the file.
600 Byte_t length = (Byte_t)nrow;
602 if(fwrite(&type,1,1,fOutBinary)!=1) return kFALSE; //Write a zero, to mark that this file contains 8 bit data.
603 if(fwrite(&length,1,1,fOutBinary)!=1) return kFALSE;
604 if(fwrite(comp,size,1,fOutBinary)!=1) return kFALSE;