2 // Original: AliHLTMemHandler.cxx,v 1.52 2005/06/14 10:55:21 cvetan
4 /**************************************************************************
5 * This file is property of and copyright by the ALICE HLT Project *
6 * ALICE Experiment at CERN, All rights reserved. *
8 * Primary Authors: U. Frankenfeld, A. Vestbo, C. Loizides *
9 * Matthias Richter <Matthias.Richter@ift.uib.no> *
10 * for The ALICE HLT Project. *
12 * Permission to use, copy, modify and distribute this software and its *
13 * documentation strictly for non-commercial purposes is hereby granted *
14 * without fee, provided that the above copyright notice appears in all *
15 * copies and that both the copyright notice and this permission notice *
16 * appear in the supporting documentation. The authors make no claims *
17 * about the suitability of this software for any purpose. It is *
18 * provided "as is" without express or implied warranty. *
19 **************************************************************************/
21 /** @file AliHLTTPCMemHandler.cxx
22 @author U. Frankenfeld, A. Vestbo, C. Loizides, maintained by
25 @brief input interface base class for the TPC tracking code before
26 migration to the HLT component framework
28 // see below for class documentation
30 // refer to README to build package
32 // visit http://web.ift.uib.no/~kjeks/doc/alice-hlt
34 /** \class AliHLTTPCMemHandler
36 //_____________________________________________________________
37 // AliHLTTPCMemHandler
39 // The HLT Binary File handler
41 // This class does all the memory I/O handling of HLT binary files.
46 // 1) Reading a binary file:
48 // AliHLTTPCMemHandler file;
49 // file.SetBinaryInput(filename);
50 // file.Init(slice,patch);
53 // AliHLTTPCDigitRowData *data = file.CompBinary2Memory(nrows);
55 // for(int i=0; i<nrows; i++)
58 // AliHLTTPCDigitData *dataPt = (AliHLTTPCDigitData*)data->fDigitData;
59 // for(int j=0; j<data->fNDigit; j++)
61 // pad = dataPt[j].fPad;
62 // time = dataPt[j].fTime;
63 // charge = dataPt[j].fCharge;
66 // file.UpdateRowPointer(data);
69 // file.CloseBinaryInput();
70 // ________________________
72 // 2) Writing a binary file:
74 // //First of all you need to store the data in memory,
75 // //and have a pointer to it of type AliHLTTPCDigitRowData.
76 // //E.g. if you just want to write the data you read in example 1)
77 // //into a new file, you can do the following:
79 // AliHLTTPCMemHandler newfile;
80 // newfile.Init(slice,patch);
81 // newfile.SetBinaryOutput(newfilename);
82 // newfile.Memory2CompBinary((UInt_t)NumberOfRowsInPatch,(AliHLTTPCDigitRowData*)data);
83 // newfile.CloseBinaryOutput();
86 // Compressed file format:
87 // -----------------------
89 // The data is RLE encoded and currently using _10_ bit range for the ADC-values.
94 #include "AliHLTTPCRootTypes.h"
95 #include "AliHLTTPCDigitData.h"
96 #include "AliHLTTPCLogging.h"
97 #include "AliHLTTPCTransform.h"
98 #include "AliHLTTPCTrackSegmentData.h"
99 #include "AliHLTTPCSpacePointData.h"
100 #include "AliHLTTPCTrackArray.h"
101 #include "AliHLTTPCMemHandler.h"
107 ClassImp(AliHLTTPCMemHandler)
109 AliHLTTPCMemHandler::AliHLTTPCMemHandler()
133 AliHLTTPCMemHandler::~AliHLTTPCMemHandler()
136 if(fPt) delete[] fPt;
137 if(fRandomDigits) delete [] fRandomDigits;
138 if(fDPt) delete [] fDPt;
141 void AliHLTTPCMemHandler::Init(Int_t s,Int_t p, Int_t *r)
151 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Init","sector coordinates")
152 <<"Invalid slice no " << s <<ENDLOG;
160 fRowMin=AliHLTTPCTransform::GetFirstRow(p);
161 fRowMax=AliHLTTPCTransform::GetLastRow(p);
166 void AliHLTTPCMemHandler::ResetROI()
168 //Resets the Look-up table for Region of Interest mode.
169 for(Int_t i=fRowMin; i<=fRowMax; i++)
171 fEtaMinTimeBin[i] = 0;
172 fEtaMaxTimeBin[i] = AliHLTTPCTransform::GetNTimeBins()-1;
176 void AliHLTTPCMemHandler::SetROI(Float_t *eta,Int_t */*slice*/)
178 // Init the Look-up table for the Region of Interest mode.
179 // Here you can specify a certain etaregion, - all data
180 // outside this region will be discarded:
181 // eta[0] = mimium eta
182 // eta[1] = maximum eta
183 // slice[0] = mimumum slice
184 // slice[1] = maximum slice
189 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::SetROI","Eta Values")
190 <<"Bad ROI parameters."<<ENDLOG;
191 for(Int_t i=fRowMin; i<=fRowMax; i++)
199 for(Int_t i=fRowMin; i<=fRowMax; i++)
204 Float_t thetamax = 2*atan(exp(-1.*eta[1]));
206 xyz[0] = AliHLTTPCTransform::Row2X(i);
208 xyz[2] = xyz[0]/tan(thetamax);
209 AliHLTTPCTransform::Slice2Sector(fSlice,i,sector,row);
210 AliHLTTPCTransform::Local2Raw(xyz,sector,row);
212 fEtaMinTimeBin[i] = (Int_t)xyz[2];
215 fEtaMaxTimeBin[i] = 445;
218 Float_t thetamin = 2*atan(exp(-1.*eta[0]));
219 xyz[0] = AliHLTTPCTransform::Row2X(i);
220 xyz[1] = AliHLTTPCTransform::GetMaxY(i);
221 Float_t radii = sqrt(pow(xyz[0],2) + pow(xyz[1],2));
222 xyz[2] = radii/tan(thetamin);
223 AliHLTTPCTransform::Local2Raw(xyz,sector,row);
224 fEtaMaxTimeBin[i] = (Int_t)xyz[2];
230 Bool_t AliHLTTPCMemHandler::SetBinaryInput(char *name)
232 //Set the input binary file.
233 fInBinary = fopen(name,"r");
235 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::SetBinaryInput","File Open")
236 <<"Error opening file "<<name<<ENDLOG;
242 Bool_t AliHLTTPCMemHandler::SetBinaryInput(FILE *file)
244 //Set the input binary file.
247 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::SetBinaryInput","File Open")
248 <<"Pointer to File = 0x0 "<<ENDLOG;
254 void AliHLTTPCMemHandler::CloseBinaryInput()
256 //Close the input file.
258 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::CloseBinaryInput","File Close")
259 <<"Nothing to Close"<<ENDLOG;
266 Bool_t AliHLTTPCMemHandler::SetBinaryOutput(char *name)
268 //Set the binary output file.
269 fOutBinary = fopen(name,"w");
271 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::SetBinaryOutput","File Open")
272 <<"Pointer to File = 0x0 "<<ENDLOG;
278 Bool_t AliHLTTPCMemHandler::SetBinaryOutput(FILE *file)
280 //Set the binary output file.
283 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::SetBinaryOutput","File Open")
284 <<"Pointer to File = 0x0 "<<ENDLOG;
290 void AliHLTTPCMemHandler::CloseBinaryOutput()
294 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::CloseBinaryOutPut","File Close")
295 <<"Nothing to Close"<<ENDLOG;
302 UInt_t AliHLTTPCMemHandler::GetFileSize()
304 //Returns the file size in bytes of the input file.
306 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::GetFileSize","File")
307 <<"No Input File"<<ENDLOG;
310 fseek(fInBinary,0,SEEK_END);
311 UInt_t size = (UInt_t) ftell(fInBinary);
316 Byte_t *AliHLTTPCMemHandler::Allocate()
319 return Allocate(GetFileSize());
322 Byte_t *AliHLTTPCMemHandler::Allocate(AliHLTTPCTrackArray *array)
324 //Allocate memory for tracks in memory. Used by TrackArray2Binary()
326 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Allocate","Memory")
327 <<"Pointer to AliHLTTPCTrackArray = 0x0 "<<ENDLOG;
330 return Allocate(array->GetOutSize());
333 Byte_t *AliHLTTPCMemHandler::Allocate(UInt_t size)
335 //Allocate memory of size in bytes.
337 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Allocate","Memory")
338 <<"Delete Memory"<<ENDLOG;
341 fPt = new Byte_t[size];
344 LOG(AliHLTTPCLog::kDebug,"AliHLTTPCMemHandler::Allocate","Memory")
345 <<AliHLTTPCLog::kDec<<"Allocate "<<size<<" Bytes of Memory"<<ENDLOG;
349 void AliHLTTPCMemHandler::Free()
351 //Clear the memory, if allocated.
353 // LOG(AliHLTTPCLog::kInformational,"AliHLTTPCMemHandler::Free","Memory")
354 // <<"No Memory allocated - can't Free"<<ENDLOG;
362 ///////////////////////////////////////// Random
363 void AliHLTTPCMemHandler::SetRandomSeed()
365 //If you are adding random data to the original data.
367 SetRandomSeed(time(tp));
370 void AliHLTTPCMemHandler::SetRandomCluster(Int_t maxnumber)
372 //If you are adding random data to the original data.
375 fNRandom = maxnumber;
377 if(fRandomDigits) delete [] fRandomDigits;
378 fRandomDigits = new AliHLTTPCRandomDigitData[fNRandom*9];
379 if(fDPt) delete [] fDPt;
380 fDPt = new AliHLTTPCRandomDigitData *[fNRandom*9];
383 void AliHLTTPCMemHandler::QSort(AliHLTTPCRandomDigitData **a, Int_t first, Int_t last)
386 // Sort array of AliHLTTPCRandomDigitData pointers using a quicksort algorithm.
387 // Uses CompareDigits() to compare objects.
390 static AliHLTTPCRandomDigitData *tmp;
391 static int i; // "static" to save stack space
394 while (last - first > 1) {
398 while (++i < last && CompareDigits(a[i], a[first]) < 0)
400 while (--j > first && CompareDigits(a[j], a[first]) > 0)
417 if (j - first < last - (j + 1)) {
419 first = j + 1; // QSort(j + 1, last);
421 QSort(a, j + 1, last);
422 last = j; // QSort(first, j);
427 UInt_t AliHLTTPCMemHandler::GetRandomSize() const
431 for(Int_t r=fRowMin;r<=fRowMax;r++){
432 Int_t npad=AliHLTTPCTransform::GetNPads(r);
433 nrandom += Int_t (fNGenerate * ((Double_t) npad/141.));
435 return 9 * nrandom * sizeof(AliHLTTPCDigitData);
438 void AliHLTTPCMemHandler::Generate(Int_t row)
440 //Generate random data on row, if you didn't
441 //ask for this, nothing happens here.
443 if(!fIsRandom) return;
446 Int_t npad=AliHLTTPCTransform::GetNPads(row);
447 Int_t ntime = fEtaMaxTimeBin[row] - fEtaMinTimeBin[row];
448 Int_t nrandom = Int_t (fNGenerate * ((Double_t) npad/141.) *
449 (Double_t) ntime/(Double_t) AliHLTTPCTransform::GetNTimeBins() );
451 for(Int_t n=0;n<nrandom;n++){
452 Int_t pad = (int)((float)rand()/RAND_MAX*npad);
453 Int_t time =(int)((float)rand()/RAND_MAX*ntime+fEtaMinTimeBin[row] );
454 Int_t charge = (int)((float)rand()/RAND_MAX*AliHLTTPCTransform::GetADCSat());
455 DigitizePoint(row,pad,time,charge);
457 QSort(fDPt,0,fNDigits);
461 void AliHLTTPCMemHandler::DigitizePoint(Int_t row, Int_t pad,
462 Int_t time,Int_t charge)
464 //Making one single random cluster.
465 for(Int_t j=-1;j<2;j++){
466 for(Int_t k=-1;k<2;k++){
467 Int_t dcharge = charge;
470 if(dcharge<10) continue;
471 Int_t dpad = j + pad;
472 Int_t dtime = k + time;
474 if(dpad<0||dpad>=AliHLTTPCTransform::GetNPads(row)) continue;
475 if(dtime<0||dtime>=AliHLTTPCTransform::GetNTimeBins()) continue;
477 fRandomDigits[fNDigits].fCharge = dcharge;
478 fRandomDigits[fNDigits].fRow = row;
479 fRandomDigits[fNDigits].fPad = dpad;
480 fRandomDigits[fNDigits].fTime = dtime;
481 fDPt[fNDigits] = &fRandomDigits[fNDigits];
487 ///////////////////////////////////////// Digit IO
488 Bool_t AliHLTTPCMemHandler::Memory2BinaryFile(UInt_t nrow,AliHLTTPCDigitRowData *data)
490 //Write data to the outputfile as is. No run-length encoding is done.
493 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Memory2Binary","File")
494 <<"No Output File"<<ENDLOG;
498 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Memory2Binary","Memory")
499 <<"Pointer to AliHLTTPCDigitRowData = 0x0 "<<ENDLOG;
503 AliHLTTPCDigitRowData *rowPt = data;
505 for(UInt_t i=0;i<nrow;i++){
506 Int_t size = sizeof(AliHLTTPCDigitData) * rowPt->fNDigit
507 + sizeof(AliHLTTPCDigitRowData);
509 fwrite(rowPt,size,1,fOutBinary);
510 Byte_t *bytePt =(Byte_t *) rowPt;
512 rowPt = (AliHLTTPCDigitRowData *) bytePt;
514 LOG(AliHLTTPCLog::kDebug,"AliHLTTPCMemHandler::Memory2Binary","Memory")
515 <<AliHLTTPCLog::kDec<<"Wrote "<<outsize<<" Bytes to Memory ("
516 <<nrow<<" Rows)"<<ENDLOG;
520 Bool_t AliHLTTPCMemHandler::Binary2Memory(UInt_t & nrow,AliHLTTPCDigitRowData *data,UInt_t& sz)
522 //Read inputfile into memory as is, and store it in data.
523 // No run-length encoding is assumed.
526 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Binary2Memory","File")
527 <<"No Input File"<<ENDLOG;
531 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Binary2Memory","Memory")
532 <<"Pointer to AliHLTTPCDigitRowData = 0x0 "<<ENDLOG;
536 AliHLTTPCDigitRowData *rowPt = data;
539 while(!feof(fInBinary)){
540 Byte_t *bytePt =(Byte_t *) rowPt;
542 if (sz<outsize+sizeof(AliHLTTPCDigitRowData)) {
543 LOG(AliHLTTPCLog::kFatal,"AliHLTTPCMemHandler::Binary2Memory","Memory")
544 << "target data buffer too small" <<ENDLOG;
547 if(fread(rowPt,sizeof(AliHLTTPCDigitRowData),1,fInBinary)!=1) break;
549 bytePt += sizeof(AliHLTTPCDigitRowData);
550 outsize += sizeof(AliHLTTPCDigitRowData);
552 UInt_t size = sizeof(AliHLTTPCDigitData) * rowPt->fNDigit;
554 if (sz<outsize+size) {
555 LOG(AliHLTTPCLog::kFatal,"AliHLTTPCMemHandler::Binary2Memory","Memory")
556 << "target data buffer too small" <<ENDLOG;
559 //if(fread(bytePt,size,1,fInBinary)!=1) break;
560 fread(bytePt,size,1,fInBinary);
563 rowPt = (AliHLTTPCDigitRowData *) bytePt;
567 LOG(AliHLTTPCLog::kDebug,"AliHLTTPCMemHandler::Binary2Memory","Memory")
568 <<AliHLTTPCLog::kDec<<"Wrote "<<outsize<<" Bytes to Memory ("
569 <<rowcount<<" Rows)"<<ENDLOG;
574 void AliHLTTPCMemHandler::AddData(AliHLTTPCDigitData *data,UInt_t & ndata,
575 UInt_t /*row*/,UShort_t pad,UShort_t time,UShort_t charge) const
578 data[ndata].fPad = pad;
579 data[ndata].fTime = time;
580 data[ndata].fCharge = charge;
584 void AliHLTTPCMemHandler::AddRandom(AliHLTTPCDigitData *data, UInt_t & ndata)
586 //add some random data
587 data[ndata].fPad = fDPt[fNUsed]->fPad;
588 data[ndata].fTime = fDPt[fNUsed]->fTime;
589 data[ndata].fCharge = fDPt[fNUsed]->fCharge;
594 void AliHLTTPCMemHandler::MergeDataRandom(AliHLTTPCDigitData *data, UInt_t & ndata,
595 UInt_t row, UShort_t pad, UShort_t time, UShort_t charge)
598 data[ndata].fPad = pad;
599 data[ndata].fTime = time;
600 data[ndata].fCharge = charge;
601 while(ComparePoints(row,pad,time)==0){
602 Int_t ch = data[ndata].fCharge + fDPt[fNUsed]->fCharge;
603 if(charge>=AliHLTTPCTransform::GetADCSat()) ch = AliHLTTPCTransform::GetADCSat();
604 data[ndata].fCharge = ch;
610 void AliHLTTPCMemHandler::AddDataRandom(AliHLTTPCDigitData *data, UInt_t & ndata,
611 UInt_t row, UShort_t pad, UShort_t time, UShort_t charge)
615 while((action=ComparePoints(row,pad,time))==1){
616 AddRandom(data,ndata);
619 MergeDataRandom(data,ndata,row,pad,time,charge);
622 AddData(data,ndata,row,pad,time,charge);
626 void AliHLTTPCMemHandler::Write(UInt_t *comp, UInt_t & index,
627 UInt_t & subindex, UShort_t value) const
629 //write compressed data
630 UInt_t shift[3] = {0,10,20};
631 if(subindex==0) comp[index] =0; //clean up memory
632 comp[index] |= (value&0x03ff)<<shift[subindex];
640 UShort_t AliHLTTPCMemHandler::Read(UInt_t *comp, UInt_t & index, UInt_t & subindex) const
642 //read compressed data
643 UInt_t shift[3] = {0,10,20};
644 UShort_t value = (comp[index]>>shift[subindex])&0x03ff;
654 UShort_t AliHLTTPCMemHandler::Test(UInt_t *comp,
655 UInt_t index, UInt_t subindex) const
658 UInt_t shift[3] = {0,10,20};
659 return (comp[index]>>shift[subindex])&0x03ff;
662 Int_t AliHLTTPCMemHandler::Memory2CompMemory(UInt_t nrow,
663 AliHLTTPCDigitRowData *data,UInt_t *comp)
665 //Performs run-length encoding on data stored in memory pointed to by data.
666 //The compressed data is written to comp.
668 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Memory2CompMemory","Memory")
669 <<"Pointer to compressed data = 0x0 "<<ENDLOG;
673 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Memory2CompMemory","Memory")
674 <<"Pointer to AliHLTTPCDigitRowData = 0x0 "<<ENDLOG;
677 AliHLTTPCDigitRowData *rowPt = data;
681 for(UInt_t i=0;i<nrow;i++){
682 UShort_t value = rowPt->fRow;
683 Write(comp,index,subindex,value);
687 for(Int_t d=0;d<200;d++) ddd[d]=0;
688 for(UInt_t dig=0;dig<rowPt->fNDigit;dig++){
689 if(rowPt->fDigitData[dig].fPad <200){
690 ddd[rowPt->fDigitData[dig].fPad]++;
693 for(Int_t d=0;d<200;d++){
699 Write(comp,index,subindex,npad);
701 for(UShort_t pad=0;pad <= maxpad;pad++){
702 if(digit>=rowPt->fNDigit || rowPt->fDigitData[digit].fPad != pad)
704 Write(comp,index,subindex,pad);
705 // write zero if time != 0
706 if(digit<rowPt->fNDigit && rowPt->fDigitData[digit].fPad == pad){
707 if(rowPt->fDigitData[digit].fTime>0){
708 Write(comp,index,subindex,0);
709 Write(comp,index,subindex,rowPt->fDigitData[digit].fTime);
712 while(digit<rowPt->fNDigit && rowPt->fDigitData[digit].fPad == pad){
713 UShort_t charge = rowPt->fDigitData[digit].fCharge;
717 Write(comp,index,subindex,charge);
718 if(digit+1<rowPt->fNDigit&&rowPt->fDigitData[digit+1].fPad == pad){
719 if(rowPt->fDigitData[digit].fTime +1 !=
720 rowPt->fDigitData[digit+1].fTime){
721 Write(comp,index,subindex,0);
722 UShort_t nzero = rowPt->fDigitData[digit+1].fTime -
723 (rowPt->fDigitData[digit].fTime +1);
724 Write(comp,index,subindex,nzero);
729 Write(comp,index,subindex,0);
730 Write(comp,index,subindex,0);
733 Int_t size = sizeof(AliHLTTPCDigitData) * rowPt->fNDigit+
734 sizeof(AliHLTTPCDigitRowData);
735 Byte_t *bytePt =(Byte_t *) rowPt;
737 rowPt = (AliHLTTPCDigitRowData *) bytePt;
740 Write(comp,index,subindex,0);
741 return index * sizeof(UInt_t);
744 Int_t AliHLTTPCMemHandler::CompMemory2Memory(UInt_t nrow,
745 AliHLTTPCDigitRowData *data,UInt_t *comp, UInt_t& sz)
747 //Uncompress the run-length encoded data in memory pointed to by comp, and
751 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::CompMemory2Memory","Memory")
752 <<"Pointer to compressed data = 0x0 "<<ENDLOG;
756 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::CompMemory2Memory","Memory")
757 <<"Pointer to AliHLTTPCDigitRowData = 0x0 "<<ENDLOG;
762 AliHLTTPCDigitRowData *rowPt = data;
766 for(UInt_t i=0;i<nrow;i++){
768 UInt_t row =Read(comp,index,subindex);
771 UShort_t npad = Read(comp,index,subindex);
772 for(UShort_t p=0;p<npad;p++){
775 UShort_t pad = Read(comp,index,subindex);
776 if(Test(comp,index,subindex)==0){
777 Read(comp,index,subindex);
778 if( (time = Read(comp,index,subindex)) == 0 ){
783 while( (charge=Read(comp,index,subindex)) != 0){
784 if(time>=fEtaMinTimeBin[row]&&time<=fEtaMaxTimeBin[row])
785 //AddData(rowPt->fDigitData,ndigit,row,pad,time,charge);
786 //seems we are using this function... but dont know why
787 AddDataRandom(rowPt->fDigitData,ndigit,row,pad,time,charge);
790 UShort_t tshift = Read(comp,index,subindex);
791 if(tshift == 0) break;
795 rowPt->fNDigit = ndigit;
796 Int_t size = sizeof(AliHLTTPCDigitData) * rowPt->fNDigit+
797 sizeof(AliHLTTPCDigitRowData);
798 Byte_t *bytePt =(Byte_t *) rowPt;
801 rowPt = (AliHLTTPCDigitRowData *) bytePt;
807 UInt_t AliHLTTPCMemHandler::GetCompMemorySize(UInt_t nrow,AliHLTTPCDigitRowData *data) const
809 //Return the size of RLE data, after compressing data.
812 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::GetCompMemorySize","Memory")
813 <<"Pointer to AliHLTTPCDigitRowData = 0x0 "<<ENDLOG;
816 AliHLTTPCDigitRowData *rowPt = data;
819 for(UInt_t i=0;i<nrow;i++){
824 for(Int_t d=0;d<200;d++) ddd[d]=0;
825 for(UInt_t dig=0;dig<rowPt->fNDigit;dig++){
826 if(rowPt->fDigitData[dig].fPad <200){
827 ddd[rowPt->fDigitData[dig].fPad]++;
830 for(Int_t d=0;d<200;d++){
838 for(UShort_t pad=0;pad <= maxpad;pad++){
839 if(digit>=rowPt->fNDigit || rowPt->fDigitData[digit].fPad != pad)
842 // write zero if time != 0
843 if(digit<rowPt->fNDigit && rowPt->fDigitData[digit].fPad == pad){
844 if(rowPt->fDigitData[digit].fTime>0){
849 while(digit<rowPt->fNDigit && rowPt->fDigitData[digit].fPad == pad){
851 if(digit+1<rowPt->fNDigit&&rowPt->fDigitData[digit+1].fPad == pad){
852 if(rowPt->fDigitData[digit].fTime +1 !=
853 rowPt->fDigitData[digit+1].fTime){
864 Int_t size = sizeof(AliHLTTPCDigitData) * rowPt->fNDigit+
865 sizeof(AliHLTTPCDigitRowData);
866 Byte_t *bytePt =(Byte_t *) rowPt;
868 rowPt = (AliHLTTPCDigitRowData *) bytePt;
872 return (index/3) * sizeof(UInt_t);
875 UInt_t AliHLTTPCMemHandler::GetMemorySize(UInt_t nrow,UInt_t *comp) const
879 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::GetMemorySize","Memory")
880 <<"Pointer to compressed data = 0x0 "<<ENDLOG;
888 for(UInt_t i=0;i<nrow;i++){
890 Read(comp,index,subindex);
891 UShort_t npad = Read(comp,index,subindex);
892 for(UShort_t p=0;p<npad;p++){
893 Read(comp,index,subindex);
894 if(Test(comp,index,subindex)==0){
895 Read(comp,index,subindex);
896 if(Read(comp,index,subindex)== 0) continue;
899 while(Read(comp,index,subindex)!=0) ndigit++;
900 if(Read(comp,index,subindex)==0) break;
903 Int_t size = sizeof(AliHLTTPCDigitData) * ndigit+
904 sizeof(AliHLTTPCDigitRowData);
911 UInt_t AliHLTTPCMemHandler::GetNRow(UInt_t *comp,UInt_t size)
915 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::GetNRow","Memory")
916 <<"Pointer to compressed data = 0x0 "<<ENDLOG;
923 while(index<size-1){ //don't start with last word
926 Read(comp,index,subindex);
927 UShort_t npad = Read(comp,index,subindex);
928 for(UShort_t p=0;p<npad;p++){
929 Read(comp,index,subindex);
930 if(Test(comp,index,subindex)==0){
931 Read(comp,index,subindex);
932 if(Read(comp,index,subindex)==0)continue;
935 while(Read(comp,index,subindex)!=0) ndigit++;
936 if(Read(comp,index,subindex)==0) break;
940 if(index==size-1){ //last word
942 if(Read(comp,index,subindex)!=0) nrow++;
948 Bool_t AliHLTTPCMemHandler::CompMemory2CompBinary(UInt_t nrow,UInt_t *comp,
951 //Write the RLE data in comp to the output file.
954 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::CompMemory2CompBinary","File")
955 <<"No Output File"<<ENDLOG;
959 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::CompMemory2CompBinary","Memory")
960 <<"Pointer to compressed data = 0x0 "<<ENDLOG;
964 size=GetMemorySize(nrow,comp);
966 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::CompMemory2CompBinary","Memory")
967 <<"Memory size = 0 "<<ENDLOG;
970 UInt_t length = size/sizeof(UInt_t);
971 fwrite(&length,sizeof(UInt_t),1,fOutBinary);
972 fwrite(comp,size,1,fOutBinary);
976 Bool_t AliHLTTPCMemHandler::CompBinary2CompMemory(UInt_t & nrow,UInt_t *comp)
978 //Read the RLE data from file, and store it in comp. No unpacking yet.
981 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::CompBinary2CompMemory","File")
982 <<"No Output File"<<ENDLOG;
986 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::CompBinary2CompMemory","Memory")
987 <<"Pointer to compressed data = 0x0 "<<ENDLOG;
992 if(fread(&length,sizeof(UInt_t),1,fInBinary)!=1) return kFALSE;
993 UInt_t size = length*sizeof(UInt_t);
994 if(fread(comp,size,1,fInBinary)!=1) return kFALSE;
995 // now find the number of dig
996 nrow = GetNRow(comp,size);
1000 AliHLTTPCDigitRowData *AliHLTTPCMemHandler::CompBinary2Memory(UInt_t & nrow, UInt_t& sz )
1002 // Read the RLE inputfile, unpack it and return the pointer to it.
1003 AliHLTTPCMemHandler * handler = new AliHLTTPCMemHandler();
1004 handler->SetBinaryInput(fInBinary);
1005 UInt_t *comp =(UInt_t *)handler->Allocate();
1006 handler->CompBinary2CompMemory(nrow,comp);
1007 UInt_t size = GetMemorySize(nrow,comp);
1009 AliHLTTPCDigitRowData *data = (AliHLTTPCDigitRowData *)Allocate(size);
1010 CompMemory2Memory(nrow,data,comp);
1016 Bool_t AliHLTTPCMemHandler::Memory2CompBinary(UInt_t nrow,AliHLTTPCDigitRowData *data)
1018 //Perform RLE on the data, and write it to the output file.
1020 AliHLTTPCMemHandler * handler = new AliHLTTPCMemHandler();
1021 UInt_t size = GetCompMemorySize(nrow,data);
1022 UInt_t *comp =(UInt_t *)handler->Allocate(size);
1023 Memory2CompMemory(nrow,data,comp);
1024 CompMemory2CompBinary(nrow,comp,size);
1031 ///////////////////////////////////////// Point IO
1032 Bool_t AliHLTTPCMemHandler::Memory2Binary(UInt_t npoint,AliHLTTPCSpacePointData *data)
1034 //Writing spacepoints stored in data to the outputfile.
1036 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Memory2Binary","File")
1037 <<"No Output File"<<ENDLOG;
1041 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Memory2Binary","Memory")
1042 <<"Pointer to AliHLTTPCSpacePointData = 0x0 "<<ENDLOG;
1045 UInt_t size = npoint*sizeof(AliHLTTPCSpacePointData);
1046 fwrite(data,size,1,fOutBinary);
1051 Bool_t AliHLTTPCMemHandler::Transform(UInt_t npoint,AliHLTTPCSpacePointData *data,Int_t slice)
1053 //Transform the space points in data, to global coordinates in slice.
1055 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Transform","Memory")
1056 <<"Pointer to AliHLTTPCSpacePointData = 0x0 "<<ENDLOG;
1060 for(UInt_t i=0;i<npoint;i++){
1062 xyz[0] = data[i].fX;
1063 xyz[1] = data[i].fY;
1064 xyz[2] = data[i].fZ;
1065 AliHLTTPCTransform::Local2Global(xyz,slice);
1066 data[i].fX = xyz[0];
1067 data[i].fY = xyz[1];
1068 data[i].fZ = xyz[2];
1073 Bool_t AliHLTTPCMemHandler::Binary2Memory(UInt_t & npoint,AliHLTTPCSpacePointData *data, UInt_t& sz)
1075 //Read the space points in inputfile, and store it in data.
1077 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Binary2Memory","File")
1078 <<"No Input File"<<ENDLOG;
1082 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Binary2Memory","Memory")
1083 <<"Pointer to AliHLTTPCSpacePointData = 0x0 "<<ENDLOG;
1087 Int_t size = GetFileSize();
1089 npoint = size/sizeof(AliHLTTPCSpacePointData);
1091 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Binary2Memory","File")
1092 <<"File Size == 0"<<ENDLOG;
1096 if(fread(data,size,1,fInBinary)!=1){
1097 LOG(AliHLTTPCLog::kFatal,"AliHLTTPCMemHandler::Binary2Memory","File")
1098 <<"File Read Error "<<ENDLOG;
1101 if(size%sizeof(AliHLTTPCSpacePointData)){
1102 LOG(AliHLTTPCLog::kFatal,"AliHLTTPCMemHandler::Binary2Memory","File Size")
1103 <<"File Size wrong "<<ENDLOG;
1106 LOG(AliHLTTPCLog::kDebug,"AliHLTTPCMemHandler::Binary2Memory","File")
1107 <<AliHLTTPCLog::kDec<<"Wrote "<<size<<" Bytes to Memory"<<ENDLOG;
1111 ///////////////////////////////////////// Track IO
1112 Bool_t AliHLTTPCMemHandler::Memory2Binary(UInt_t ntrack,AliHLTTPCTrackSegmentData *data)
1114 //Write the tracks stored in data, to outputfile.
1116 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Memory2Binary","File")
1117 <<"No Output File"<<ENDLOG;
1121 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Memory2Binary","Memory")
1122 <<"Pointer to AliHLTTPCTrackSegmentData = 0x0 "<<ENDLOG;
1125 AliHLTTPCTrackSegmentData *trackPt = data;
1126 for(UInt_t i=0;i<ntrack;i++){
1127 Int_t size=sizeof(AliHLTTPCTrackSegmentData)+trackPt->fNPoints*sizeof(UInt_t);
1128 fwrite(trackPt,size,1,fOutBinary);
1129 Byte_t *bytePt = (Byte_t*) trackPt;
1131 trackPt = (AliHLTTPCTrackSegmentData*) bytePt;
1133 LOG(AliHLTTPCLog::kDebug,"AliHLTTPCMemHandler::Memory2Binary","File")
1134 <<AliHLTTPCLog::kDec<<"Wrote "<<ntrack<<" Tracks to File"<<ENDLOG;
1139 Bool_t AliHLTTPCMemHandler::Binary2Memory(UInt_t & ntrack,AliHLTTPCTrackSegmentData *data)
1141 //Read the tracks in inputfile, and store it in data.
1143 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Binary2Memory","File")
1144 <<"No Input File"<<ENDLOG;
1148 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Binary2Memory","Memory")
1149 <<"Pointer to AliHLTTPCTrackSegmentData = 0x0 "<<ENDLOG;
1154 AliHLTTPCTrackSegmentData *trackPt = data;
1157 while(!feof(fInBinary)){
1158 if(fread(trackPt,sizeof(AliHLTTPCTrackSegmentData),1,fInBinary)!=1) break;
1159 Int_t size=trackPt->fNPoints*sizeof(UInt_t);
1160 if(fread(trackPt->fPointIDs,size,1,fInBinary)!=1) break;
1161 Byte_t *bytePt = (Byte_t*) trackPt;
1162 bytePt += sizeof(AliHLTTPCTrackSegmentData)+size;
1163 trackPt = (AliHLTTPCTrackSegmentData*) bytePt;
1166 LOG(AliHLTTPCLog::kDebug,"AliHLTTPCMemHandler::Binary2Memory","File")
1167 <<AliHLTTPCLog::kDec<<"Wrote "<<ntrack<<" Tracks to Memory"<<ENDLOG;
1171 Bool_t AliHLTTPCMemHandler::TrackArray2Binary(AliHLTTPCTrackArray *array)
1173 //Write the trackarray to the outputfile.
1175 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::TrackArray2Binary","File")
1176 <<"No Output File"<<ENDLOG;
1180 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::TrackArray2Binary","Memory")
1181 <<"Pointer to AliHLTTPCTrackArray = 0x0 "<<ENDLOG;
1184 AliHLTTPCTrackSegmentData *data = (AliHLTTPCTrackSegmentData *)Allocate(array);
1187 TrackArray2Memory(ntrack,data,array);
1188 Memory2Binary(ntrack,data);
1193 Bool_t AliHLTTPCMemHandler::Binary2TrackArray(AliHLTTPCTrackArray *array)
1195 //Read the tracks in inputfile, and fill it in trackarray.
1196 //array should already be constructed.
1198 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Binary2TrackArray","File")
1199 <<"No Input File"<<ENDLOG;
1203 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Binary2TrackArray","Memory")
1204 <<"Pointer to AliHLTTPCTrackArray = 0x0 "<<ENDLOG;
1207 AliHLTTPCTrackSegmentData *data = (AliHLTTPCTrackSegmentData *)Allocate();
1209 Binary2Memory(ntrack,data);
1210 Memory2TrackArray(ntrack,data,array);
1215 Bool_t AliHLTTPCMemHandler::TrackArray2Memory(UInt_t & ntrack,AliHLTTPCTrackSegmentData *data,AliHLTTPCTrackArray *array) const
1217 //Fill the trackarray into the AliTrackSegmentData structures before writing to outputfile.
1219 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::TrackArray2Memory","Memory")
1220 <<"Pointer to AliHLTTPCTrackSegmentData = 0x0 "<<ENDLOG;
1224 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::TrackArray2Memory","Memory")
1225 <<"Pointer to AliHLTTPCTrackArray = 0x0 "<<ENDLOG;
1229 array->WriteTracks(ntrack,data);
1233 Bool_t AliHLTTPCMemHandler::Memory2TrackArray(UInt_t ntrack,AliHLTTPCTrackSegmentData *data,AliHLTTPCTrackArray *array) const
1235 //Fill the tracks in data into trackarray.
1238 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Memory2TrackArray","Memory")
1239 <<"Pointer to AliHLTTPCTrackSegmentData = 0x0 "<<ENDLOG;
1243 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Memory2TrackArray","Memory")
1244 <<"Pointer to AliHLTTPCTrackArray = 0x0 "<<ENDLOG;
1247 array->FillTracks(ntrack,data);
1251 Bool_t AliHLTTPCMemHandler::Memory2TrackArray(UInt_t ntrack,AliHLTTPCTrackSegmentData *data,AliHLTTPCTrackArray *array,Int_t slice) const
1253 //Fill the tracks in data into trackarray, and rotate the tracks to global coordinates.
1256 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Memory2TrackArray","Memory")
1257 <<"Pointer to AliHLTTPCTrackSegmentData = 0x0 "<<ENDLOG;
1261 LOG(AliHLTTPCLog::kWarning,"AliHLTTPCMemHandler::Memory2TrackArray","Memory")
1262 <<"Pointer to AliHLTTPCTrackArray = 0x0 "<<ENDLOG;
1265 array->FillTracks(ntrack,data,slice);
1269 void AliHLTTPCMemHandler::UpdateRowPointer(AliHLTTPCDigitRowData *&tempPt)
1271 //Update the data pointer to the next padrow in memory.
1273 Byte_t *tmp = (Byte_t*)tempPt;
1274 Int_t size = sizeof(AliHLTTPCDigitRowData) + tempPt->fNDigit*sizeof(AliHLTTPCDigitData);
1276 tempPt = (AliHLTTPCDigitRowData*)tmp;
1279 Int_t AliHLTTPCMemHandler::ComparePoints(UInt_t /*row*/,UShort_t pad,UShort_t time) const
1281 //compare two points
1282 if(fNUsed>=fNDigits) return -2;
1284 if(pad==fDPt[fNUsed]->fPad&&time==fDPt[fNUsed]->fTime) return 0;
1286 if(pad<fDPt[fNUsed]->fPad) return -1;
1287 if(pad==fDPt[fNUsed]->fPad&&time<fDPt[fNUsed]->fTime) return -1;
1292 Int_t AliHLTTPCMemHandler::CompareDigits(AliHLTTPCRandomDigitData *a,AliHLTTPCRandomDigitData *b) const
1294 //compare two digits
1295 if(a->fPad==b->fPad && a->fTime == b->fTime) return 0;
1297 if(a->fPad<b->fPad) return -1;
1298 if(a->fPad==b->fPad && a->fTime<b->fTime) return -1;