3 // Author: Anders Vestbo <mailto:vestbo$fi.uib.no>
4 //*-- Copyright © ASV
13 #include "AliL3Compress.h"
14 #include "AliL3TrackArray.h"
15 #include "AliL3ModelTrack.h"
16 #include "AliL3Transform.h"
17 #include "AliL3MemHandler.h"
19 #include "AliL3FileHandler.h"
23 //_____________________________________________________________
27 // Class for compressing and uncompressing data.
29 ClassImp(AliL3Compress)
31 AliL3Compress::AliL3Compress()
34 SetBitNumbers(0,0,0,0);
41 AliL3Compress::AliL3Compress(Int_t slice,Int_t patch,Char_t *path)
45 SetBitNumbers(0,0,0,0);
49 sprintf(fPath,"%s",path);
52 AliL3Compress::~AliL3Compress()
62 void AliL3Compress::SetBitNumbers(Int_t pad,Int_t time,Int_t charge,Int_t shape)
66 fNumChargeBits=charge;
70 void AliL3Compress::WriteFile(AliL3TrackArray *tracks)
73 sprintf(fname,"%s/tracks_m_%d_%d.raw",fPath,fSlice,fPatch);
74 FILE *file = fopen(fname,"w");
75 Short_t ntracks = tracks->GetNTracks();
76 //cout<<"Writing "<<ntracks<<" tracks to file"<<endl;
79 AliL3ClusterModel *clusters=0;
80 AliL3TrackModel *model=0;
81 for(Int_t i=0; i<ntracks; i++)
83 AliL3ModelTrack *track = (AliL3ModelTrack*)tracks->GetCheckedTrack(i);
87 model = track->GetModel();
88 if(model->fNClusters==0) continue;
89 clusters = track->GetClusters();
90 //cout<<"Writing "<<(int)model->fNClusters<<" clusters"<<endl;
91 if(fwrite(model,sizeof(AliL3TrackModel),1,file)!=1) break;
92 //cout<<"Writing "<<(int)model->fNClusters<<" clusters to file"<<endl;
93 if(fwrite(clusters,model->fNClusters*sizeof(AliL3ClusterModel),1,file)!=1) break;
98 cout<<"Wrote "<<count<<" tracks "<<endl;
102 void AliL3Compress::ReadFile(Char_t which)
104 //Read the trackfile.
108 sprintf(fname,"%s/tracks_m_%d_%d.raw",fPath,fSlice,fPatch);
109 else if(which == 'u')
110 sprintf(fname,"%s/tracks_u_%d_%d.raw",fPath,fSlice,fPatch);
113 cerr<<"AliL3Compress::ReadFile() : Wrong option"<<endl;
117 FILE *file = fopen(fname,"r");
120 cerr<<"Cannot open file "<<fname<<endl;
126 fTracks = new AliL3TrackArray("AliL3ModelTrack");
128 cout<<"Reading file "<<fname<<endl;
131 AliL3ModelTrack *track = (AliL3ModelTrack*)fTracks->NextTrack();
132 track->Init(fSlice,fPatch);
133 AliL3TrackModel *model = track->GetModel();
134 AliL3ClusterModel *clusters = track->GetClusters();
135 //cout<<"Reading model "<<(int)model<<endl;
136 if(fread(model,sizeof(AliL3TrackModel),1,file)!=1) break;
137 //cout<<"Reading clusters "<<(int)clusters<<endl;
138 if(fread(clusters,(model->fNClusters)*sizeof(AliL3ClusterModel),1,file)!=1) break;
139 //cout<<"Filling track"<<endl;
144 fTracks->RemoveLast();
145 cout<<"Read "<<fTracks->GetNTracks()<<" tracks from file"<<endl;
149 void AliL3Compress::CompressFile()
153 cerr<<"AliL3Compress::CompressFile() : Bitnumbers not set"<<endl;
158 sprintf(fname,"%s/tracks_c_%d_%d.raw",fPath,fSlice,fPatch);
159 BIT_FILE *output = OpenOutputBitFile(fname);
161 sprintf(fname,"%s/tracks_m_%d_%d.raw",fPath,fSlice,fPatch);
162 FILE *input = fopen(fname,"r");
165 cerr<<"AliL3Compress::CompressFile() : Error opening file: "<<fname<<endl;
169 AliL3TrackModel track;
170 AliL3ClusterModel cluster;
174 Int_t timeo,pado,chargeo,shapeo;
175 timeo=pado=chargeo=shapeo=0;
178 if(fread(&track,sizeof(AliL3TrackModel),1,input)!=1) break;
180 if(output->mask != 0x80) //Write the current byte to file.
182 //cerr<<"\nAliL3Compress::CompressFile() : Writing overhead bits!!!"<<endl;
183 if(putc(output->rack,output->file )!=output->rack)
184 cerr<<"AliL3Compress::ComressFile : Error writing to bitfile"<<endl;
189 //Write track parameters:
190 fwrite(&track,sizeof(AliL3TrackModel),1,output->file);
191 for(Int_t i=0; i<track.fNClusters; i++)
193 if(fread(&cluster,sizeof(AliL3ClusterModel),1,input)!=1) break;
196 temp = (Int_t)cluster.fPresent;
197 OutputBit(output,temp);
200 //Write time information:
201 temp = (Int_t)cluster.fDTime;
206 power = 1<<(fNumTimeBits-1);
212 OutputBits(output,abs(temp),(fNumTimeBits-1));
214 //Write pad information:
215 temp = (Int_t)cluster.fDPad;
220 power = 1<<(fNumPadBits-1);
226 OutputBits(output,abs(temp),(fNumPadBits-1));
228 //Write charge information:
229 temp = (Int_t)cluster.fDCharge;
234 power = 1<<(fNumChargeBits-1);
240 OutputBits(output,abs(temp),(fNumChargeBits-1));
242 //Write shape information:
243 temp = (Int_t)cluster.fDSigmaY2;
244 power = 1<<fNumShapeBits;
245 if(abs(temp) >= power)
250 OutputBits(output,abs(temp),fNumShapeBits);
252 temp = (Int_t)cluster.fDSigmaZ2;
253 if(abs(temp) >= power)
258 OutputBits(output,abs(temp),fNumShapeBits);
263 CloseOutputBitFile(output);
265 cout<<endl<<"There was following number of overflows: "<<endl
267 <<"Time "<<timeo<<endl
268 <<"Charge "<<chargeo<<endl
269 <<"Shape "<<shapeo<<endl;
272 void AliL3Compress::ExpandFile()
276 cerr<<"AliL3Compress::ExpandFile() : Bitnumbers not set"<<endl;
281 sprintf(fname,"%s/tracks_c_%d_%d.raw",fPath,fSlice,fPatch);
282 BIT_FILE *input = OpenInputBitFile(fname);
284 sprintf(fname,"%s/tracks_u_%d_%d.raw",fPath,fSlice,fPatch);
285 FILE *output = fopen(fname,"w");
288 cerr<<"AliL3Compress::ExpandFile() : Error opening file: "<<fname<<endl;
292 AliL3TrackModel trackmodel;
293 AliL3ClusterModel *clusters=0;
296 clusters = new AliL3ClusterModel[(NumRows[fPatch])];
297 while(!feof(input->file))
299 input->mask=0x80;//make sure we read a new byte from file.
301 //Read and write track:
302 if(fread(&trackmodel,sizeof(AliL3TrackModel),1,input->file)!=1) break;
303 fwrite(&trackmodel,sizeof(AliL3TrackModel),1,output);
305 for(Int_t i=0; i<NumRows[fPatch]; i++)
310 temp = InputBit(input);
313 clusters[i].fPresent=kFALSE;
316 clusters[i].fPresent=kTRUE;
318 //Read time information:
319 sign=InputBit(input);
320 temp = InputBits(input,(fNumTimeBits-1));
323 clusters[i].fDTime = temp;
325 //Read pad information:
326 sign=InputBit(input);
327 temp = InputBits(input,(fNumPadBits-1));
330 clusters[i].fDPad = temp;
332 //Read charge information:
333 sign = InputBit(input);
334 temp=InputBits(input,(fNumChargeBits-1));
337 clusters[i].fDCharge = temp;
339 //Read shape information:
340 temp = InputBits(input,fNumShapeBits);
341 clusters[i].fDSigmaY2 = temp;
343 temp = InputBits(input,fNumShapeBits);
344 clusters[i].fDSigmaZ2 = temp;
349 fwrite(clusters,(trackmodel.fNClusters)*sizeof(AliL3ClusterModel),1,output);
355 CloseInputBitFile(input);
358 void AliL3Compress::CreateDigitArray(Int_t maxnumber)
362 fMaxDigits=maxnumber;
363 if(fDigits) delete [] fDigits;
364 fDigits = new AliL3RandomDigitData[maxnumber];
365 if(fDPt) delete [] fDPt;
366 fDPt = new AliL3RandomDigitData*[maxnumber];
369 void AliL3Compress::RestoreData()
372 //Read the uncompressed file:
375 CreateDigitArray(100000);
377 Float_t pad,time,sigmaY2,sigmaZ2;
379 for(Int_t j=NRows[fPatch][0]; j<=NRows[fPatch][1]; j++)
381 cout<<"Building clusters on row "<<j<<endl;
382 for(Int_t i=0; i<fTracks->GetNTracks(); i++)
384 AliL3ModelTrack *track = (AliL3ModelTrack*)fTracks->GetCheckedTrack(i);
386 if(!track->GetPad(j,pad) ||
387 !track->GetTime(j,time) ||
388 !track->GetClusterCharge(j,charge) ||
389 !track->GetXYWidth(j,sigmaY2) ||
390 !track->GetZWidth(j,sigmaZ2))
393 CreateDigits(j,pad,time,charge,sigmaY2,sigmaZ2);
397 QSort(fDPt,0,fNDigits);
400 void AliL3Compress::PrintDigits()
402 Int_t pad,time,charge,row;
403 for(Int_t i=0; i<fNDigits; i++)
407 time = fDPt[i]->fTime;
408 charge = fDPt[i]->fCharge;
409 if(i>0 && row != fDPt[i-1]->fRow)
410 cout<<"---Padrow "<<row<<"---"<<endl;
411 cout<<"Pad "<<pad<<" time "<<time<<" charge "<<charge<<endl;
415 void AliL3Compress::WriteRestoredData()
419 //Get the remaining raw data array:
420 AliL3MemHandler *mem = new AliL3MemHandler();
421 sprintf(fname,"%s/remains_%d_%d.raw",fPath,fSlice,fPatch);
422 mem->SetBinaryInput(fname);
424 AliL3DigitRowData *origRow = mem->CompBinary2Memory(numdigits);
425 mem->CloseBinaryInput();
427 //Allocate memory for the merged data:
428 UInt_t size = mem->GetAllocatedSize() + fNDigits*sizeof(AliL3DigitData);
429 cout<<"Allocating "<<size<<" bytes for merged data array "<<endl;
430 Byte_t *data = new Byte_t[size];
432 AliL3DigitRowData *tempRow = (AliL3DigitRowData*)data;
434 Int_t ndigits,action,charge;
437 UInt_t digit_counter;
439 for(Int_t i=NRows[fPatch][0]; i<=NRows[fPatch][1]; i++)
443 AliL3DigitData *origDig = origRow->fDigitData;
444 AliL3DigitData *tempDig = tempRow->fDigitData;
445 if((Int_t)origRow->fRow != i)
446 cerr<<"AliL3Compress::WriteRestoredData() : Mismatching row numbering "<<(Int_t)origRow->fRow<<" "<<i<<endl;
448 //cout<<"Writing row "<<i<<" with "<<(Int_t)origRow->fNDigit<<" old digits"<<endl;
453 while(digit_counter < origRow->fNDigit)
455 pad = origDig[digit_counter].fPad;
456 time = origDig[digit_counter].fTime;
457 charge = origDig[digit_counter].fCharge;
459 while((action=ComparePoints(i,pad,time)) == 1)
461 tempDig[ndigits].fPad = fDPt[fNUsed]->fPad;
462 tempDig[ndigits].fTime = fDPt[fNUsed]->fTime;
463 tempDig[ndigits].fCharge = fDPt[fNUsed]->fCharge;
470 tempDig[ndigits].fPad = pad;
471 tempDig[ndigits].fTime = time;
472 tempDig[ndigits].fCharge = charge;
477 if(fNUsed >= fNDigits)
479 if(fDPt[fNUsed]->fRow != i) //we are on a new row
481 tempDig[ndigits].fPad = fDPt[fNUsed]->fPad;
482 tempDig[ndigits].fTime = fDPt[fNUsed]->fTime;
483 tempDig[ndigits].fCharge = fDPt[fNUsed]->fCharge;
487 //cout<<"Writing "<<ndigits<<" digits on row "<<i<<endl;
491 cout<<"Few digits on row "<<i<<endl;
493 tempRow->fNDigit = ndigits;
494 Int_t size = sizeof(AliL3DigitData)*tempRow->fNDigit + sizeof(AliL3DigitRowData);
495 Byte_t *byte_pt = (Byte_t*)tempRow;
497 tempRow = (AliL3DigitRowData*)byte_pt;
498 mem->UpdateRowPointer(origRow);
501 if(row_counter != NumRows[fPatch])
502 cerr<<"AliL3Compress::WriteRestoredData() : Written rows: "<<row_counter<<" total rows "<<NumRows[fPatch]<<endl;
505 sprintf(fname,"%s/restored_%d_%d.raw",fPath,fSlice,fPatch);
506 mem->SetBinaryOutput(fname);
507 mem->Memory2CompBinary((UInt_t)NumRows[fPatch],(AliL3DigitRowData*)data);
508 mem->CloseBinaryOutput();
515 void AliL3Compress::CreateDigits(Int_t row,Float_t pad,Float_t time,Int_t charge,Float_t sigmaY2,Float_t sigmaZ2)
517 //Create raw data out of the cluster.
519 AliL3Transform *tr = new AliL3Transform();
520 TRandom *random = new TRandom();
523 TH1F *hist1 = new TH1F("hist1","",tr->GetNPads(row),0,tr->GetNPads(row)-1);
524 TH1F *hist2 = new TH1F("hist2","",tr->GetNTimeBins(),0,tr->GetNTimeBins()-1);
525 TH2F *hist3 = new TH2F("hist3","",tr->GetNPads(row),0,tr->GetNPads(row)-1,tr->GetNTimeBins(),0,tr->GetNTimeBins()-1);
527 //Convert back the sigmas:
530 padw = tr->GetPadPitchWidthLow();
532 padw = tr->GetPadPitchWidthUp();
533 timew = tr->GetZWidth();
536 sigmaY2 = sigmaY2/2.07;
537 sigmaY2 = sigmaY2/0.108;
538 sigmaY2 = sigmaY2/(padw*padw);
539 sigmaY2 = sigmaY2 - 1./12;
542 sigmaZ2 = sigmaZ2/1.77;
543 sigmaZ2 = sigmaZ2/0.169;
544 sigmaZ2 = sigmaZ2/(timew*timew);
545 sigmaZ2 = sigmaZ2 - 1./12;
547 if(sigmaY2 <= 0 || sigmaZ2 <= 0)
549 cerr<<"AliL3Compress::CreateDigits() : Wrong sigmas : "<<sigmaY2<<" "<<sigmaZ2;
550 cerr<<" on row "<<row<<" pad "<<pad<<" time "<<time<<endl;
554 //Create the distributions in pad and time:
555 for(Int_t i=0; i<entries; i++)
557 hist1->Fill(random->Gaus(pad,sqrt(sigmaY2)));
558 hist2->Fill(random->Gaus(time,sqrt(sigmaZ2)));
561 //Create the cluster:
563 Double_t content1,content2,dpad,dtime;
564 for(Int_t i=0; i<hist1->GetEntries(); i++)
566 bin1 = hist1->GetBin(i);
567 content1 = hist1->GetBinContent(bin1);
568 if((Int_t)content1==0) continue;
569 content1 = charge*content1/entries;
570 dpad = hist1->GetBinCenter(bin1);
571 for(Int_t j=0; j<hist2->GetEntries(); j++)
573 bin2 = hist2->GetBin(j);
574 content2 = hist2->GetBinContent(bin2);
575 if((Int_t)content2==0) continue;
576 content2 = content1*content2/entries;
577 dtime = hist2->GetBinCenter(bin2);
578 hist3->Fill(dpad,dtime,content2);
582 //Fill it into the digit array:
583 for(Int_t i=0; i<hist3->GetNbinsX(); i++)
585 for(Int_t j=0; j<hist3->GetNbinsY(); j++)
587 bin1 = hist3->GetBin(i,j);
588 content1 = hist3->GetBinContent(bin1);
589 if((Int_t)content1 < 3) continue;
592 if(fNDigits >= fMaxDigits)
594 cerr<<"AliL3Compress::CreateDigits() : Array index out of range : "<<fNDigits<<endl;
597 fDigits[fNDigits].fCharge=(Int_t)content1;
598 fDigits[fNDigits].fRow = row;
599 fDigits[fNDigits].fPad = (Int_t)hist3->GetXaxis()->GetBinCenter(i);
600 fDigits[fNDigits].fTime = (Int_t)hist3->GetYaxis()->GetBinCenter(j);
601 fDPt[fNDigits] = &fDigits[fNDigits];
613 void AliL3Compress::PrintCompRatio()
616 sprintf(fname,"%s/remains_%d_%d.raw",fPath,fSlice,fPatch);
617 AliL3MemHandler *mem = new AliL3MemHandler();
618 if(!mem->SetBinaryInput(fname))
620 cerr<<"AliL3Compress::PrintCompRatio(): Error opening file: "<<fname<<endl;
624 AliL3DigitRowData *rowPt = (AliL3DigitRowData*)mem->CompBinary2Memory(ndigits);
625 mem->CloseBinaryInput();
627 Int_t digit_counter=0;
628 for(Int_t i=NRows[fPatch][0]; i<=NRows[fPatch][1]; i++)
630 digit_counter += rowPt->fNDigit;
631 mem->UpdateRowPointer(rowPt);
635 sprintf(fname,"%s/tracks_c_%d_%d.raw",fPath,fSlice,fPatch);
636 FILE *file1 = fopen(fname,"r");
639 cerr<<"AliL3Compress::PrintCompRatio(): Error opening file: "<<fname<<endl;
642 fseek(file1,0,SEEK_END);
643 UInt_t filesize1 = (UInt_t)ftell(file1);
646 sprintf(fname,"%s/digits_%d_%d.raw",fPath,fSlice,fPatch);
647 FILE *file2 = fopen(fname,"r");
650 cerr<<"AliL3Compress::PrintCompRatio(): Error opening file: "<<fname<<endl;
653 fseek(file2,0,SEEK_END);
654 UInt_t filesize2 = (UInt_t)ftell(file2);
657 cout<<"----------------------"<<endl;
658 cout<<"Original file size : "<<filesize2<<endl;
659 cout<<"Compressed file size : "<<filesize1<<endl;
660 cout<<"Remaining digits : "<<digit_counter<<endl;
661 cout<<"Compression ratio : "<<(Float_t)(filesize1 + (10*digit_counter)/8)/(Float_t)(filesize2)<<endl;
665 void AliL3Compress::QSort(AliL3RandomDigitData **a, Int_t first, Int_t last)
668 // Sort array of AliL3RandomDigitData pointers using a quicksort algorithm.
669 // Uses CompareDigits() to compare objects.
672 static AliL3RandomDigitData *tmp;
673 static int i; // "static" to save stack space
676 while (last - first > 1) {
680 while (++i < last && CompareDigits(a[i], a[first]) < 0)
682 while (--j > first && CompareDigits(a[j], a[first]) > 0)
698 if (j - first < last - (j + 1)) {
700 first = j + 1; // QSort(j + 1, last);
702 QSort(a, j + 1, last);
703 last = j; // QSort(first, j);
708 void AliL3Compress::WriteRootFile(Char_t *digitsfile,Char_t *rootfile)
712 AliL3MemHandler *mem = new AliL3MemHandler();
713 sprintf(fname,"%s/restored_%d_%d.raw",fPath,fSlice,fPatch);
714 mem->SetBinaryInput(fname);
716 AliL3DigitRowData *rowPt = (AliL3DigitRowData*)mem->CompBinary2Memory(ndigits);
717 mem->CloseBinaryInput();
719 AliL3FileHandler *file = new AliL3FileHandler();
720 if(!file->SetAliInput(digitsfile))
722 cerr<<"AliL3Compress::WriteRootFile() : Error opening file: "<<digitsfile<<endl;
725 file->Init(fSlice,fPatch,NRows[fPatch]);
726 file->AliDigits2RootFile(rowPt,rootfile);
727 file->CloseAliInput();