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);
86 //Do not save useless tracks or clusters:
87 //if(track->CheckClustersQuality(2) == 0)
91 model = track->GetModel();
92 if(model->fNClusters==0) continue;
93 clusters = track->GetClusters();
94 //cout<<"Writing "<<(int)model->fNClusters<<" clusters"<<endl;
95 if(fwrite(model,sizeof(AliL3TrackModel),1,file)!=1) break;
96 //cout<<"Writing "<<(int)model->fNClusters<<" clusters to file"<<endl;
97 if(fwrite(clusters,model->fNClusters*sizeof(AliL3ClusterModel),1,file)!=1) break;
102 cout<<"Wrote "<<count<<" tracks "<<endl;
106 void AliL3Compress::ReadFile(Char_t which)
108 //Read the trackfile.
112 sprintf(fname,"%s/tracks_m_%d_%d.raw",fPath,fSlice,fPatch);
113 else if(which == 'u')
114 sprintf(fname,"%s/tracks_u_%d_%d.raw",fPath,fSlice,fPatch);
117 cerr<<"AliL3Compress::ReadFile() : Wrong option"<<endl;
121 FILE *file = fopen(fname,"r");
124 cerr<<"Cannot open file "<<fname<<endl;
130 fTracks = new AliL3TrackArray("AliL3ModelTrack");
132 cout<<"Reading file "<<fname<<endl;
135 AliL3ModelTrack *track = (AliL3ModelTrack*)fTracks->NextTrack();
136 track->Init(fSlice,fPatch);
137 AliL3TrackModel *model = track->GetModel();
138 AliL3ClusterModel *clusters = track->GetClusters();
139 //cout<<"Reading model "<<(int)model<<endl;
140 if(fread(model,sizeof(AliL3TrackModel),1,file)!=1) break;
141 //cout<<"Reading clusters "<<(int)clusters<<endl;
142 if(fread(clusters,(model->fNClusters)*sizeof(AliL3ClusterModel),1,file)!=1) break;
143 //cout<<"Filling track"<<endl;
148 fTracks->RemoveLast();
149 cout<<"Read "<<fTracks->GetNTracks()<<" tracks from file"<<endl;
153 void AliL3Compress::CompressFile()
157 cerr<<"AliL3Compress::CompressFile() : Bitnumbers not set"<<endl;
162 sprintf(fname,"%s/tracks_c_%d_%d.raw",fPath,fSlice,fPatch);
163 BIT_FILE *output = OpenOutputBitFile(fname);
165 sprintf(fname,"%s/tracks_m_%d_%d.raw",fPath,fSlice,fPatch);
166 FILE *input = fopen(fname,"r");
169 cerr<<"AliL3Compress::CompressFile() : Error opening file: "<<fname<<endl;
173 AliL3TrackModel track;
174 AliL3ClusterModel cluster;
178 Int_t timeo,pado,chargeo,shapeo;
179 timeo=pado=chargeo=shapeo=0;
182 if(fread(&track,sizeof(AliL3TrackModel),1,input)!=1) break;
184 if(output->mask != 0x80) //Write the current byte to file.
186 //cerr<<"\nAliL3Compress::CompressFile() : Writing overhead bits!!!"<<endl;
187 if(putc(output->rack,output->file )!=output->rack)
188 cerr<<"AliL3Compress::ComressFile : Error writing to bitfile"<<endl;
193 //Write track parameters:
194 fwrite(&track,sizeof(AliL3TrackModel),1,output->file);
195 for(Int_t i=0; i<track.fNClusters; i++)
197 if(fread(&cluster,sizeof(AliL3ClusterModel),1,input)!=1) break;
200 temp = (Int_t)cluster.fPresent;
201 OutputBit(output,temp);
204 //Write time information:
205 temp = (Int_t)cluster.fDTime;
210 power = 1<<(fNumTimeBits-1);
216 OutputBits(output,abs(temp),(fNumTimeBits-1));
218 //Write pad information:
219 temp = (Int_t)cluster.fDPad;
224 power = 1<<(fNumPadBits-1);
230 OutputBits(output,abs(temp),(fNumPadBits-1));
232 //Write charge information:
233 temp = (Int_t)cluster.fDCharge;
238 power = 1<<(fNumChargeBits-1);
244 OutputBits(output,abs(temp),(fNumChargeBits-1));
246 //Write shape information:
247 temp = (Int_t)cluster.fDSigmaY2;
252 power = 1<<(fNumShapeBits-1);
253 if(abs(temp) >= power)
258 OutputBits(output,abs(temp),(fNumShapeBits-1));
260 temp = (Int_t)cluster.fDSigmaZ2;
265 power = 1<<(fNumShapeBits-1);
266 if(abs(temp) >= power)
271 OutputBits(output,abs(temp),(fNumShapeBits-1));
276 CloseOutputBitFile(output);
278 cout<<endl<<"Saturations: "<<endl
280 <<"Time "<<timeo<<endl
281 <<"Charge "<<chargeo<<endl
282 <<"Shape "<<shapeo<<endl;
285 void AliL3Compress::ExpandFile()
289 cerr<<"AliL3Compress::ExpandFile() : Bitnumbers not set"<<endl;
294 sprintf(fname,"%s/tracks_c_%d_%d.raw",fPath,fSlice,fPatch);
295 BIT_FILE *input = OpenInputBitFile(fname);
297 sprintf(fname,"%s/tracks_u_%d_%d.raw",fPath,fSlice,fPatch);
298 FILE *output = fopen(fname,"w");
301 cerr<<"AliL3Compress::ExpandFile() : Error opening file: "<<fname<<endl;
305 AliL3TrackModel trackmodel;
306 AliL3ClusterModel *clusters=0;
309 clusters = new AliL3ClusterModel[(AliL3Transform::GetNRows(fPatch))];
310 while(!feof(input->file))
312 input->mask=0x80;//make sure we read a new byte from file.
314 //Read and write track:
315 if(fread(&trackmodel,sizeof(AliL3TrackModel),1,input->file)!=1) break;
316 fwrite(&trackmodel,sizeof(AliL3TrackModel),1,output);
318 for(Int_t i=0; i<AliL3Transform::GetNRows(fPatch); i++)
323 temp = InputBit(input);
326 clusters[i].fPresent=kFALSE;
329 clusters[i].fPresent=kTRUE;
331 //Read time information:
332 sign=InputBit(input);
333 temp = InputBits(input,(fNumTimeBits-1));
336 clusters[i].fDTime = temp;
338 //Read pad information:
339 sign=InputBit(input);
340 temp = InputBits(input,(fNumPadBits-1));
343 clusters[i].fDPad = temp;
345 //Read charge information:
346 sign = InputBit(input);
347 temp=InputBits(input,(fNumChargeBits-1));
350 clusters[i].fDCharge = temp;
352 //Read shape information:
353 sign = InputBit(input);
354 temp = InputBits(input,(fNumShapeBits-1));
357 clusters[i].fDSigmaY2 = temp;
359 sign = InputBit(input);
360 temp = InputBits(input,(fNumShapeBits-1));
363 clusters[i].fDSigmaZ2 = temp;
368 fwrite(clusters,(trackmodel.fNClusters)*sizeof(AliL3ClusterModel),1,output);
374 CloseInputBitFile(input);
377 void AliL3Compress::CreateDigitArray(Int_t maxnumber)
381 fMaxDigits=maxnumber;
382 if(fDigits) delete [] fDigits;
383 fDigits = new AliL3RandomDigitData[maxnumber];
384 if(fDPt) delete [] fDPt;
385 fDPt = new AliL3RandomDigitData*[maxnumber];
388 void AliL3Compress::RestoreData(Char_t which='u')
391 //which == u : restore compressed data
392 //which == m : restore uncompressed data
397 CreateDigitArray(10000000);
399 Float_t pad,time,sigmaY2,sigmaZ2;
401 for(Int_t j=AliL3Transform::GetFirstRow(fPatch); j<=AliL3Transform::GetLastRow(fPatch); j++)
403 cout<<"Building clusters on row "<<j<<endl;
404 for(Int_t i=0; i<fTracks->GetNTracks(); i++)
406 AliL3ModelTrack *track = (AliL3ModelTrack*)fTracks->GetCheckedTrack(i);
408 if(!track->GetPad(j,pad) ||
409 !track->GetTime(j,time) ||
410 !track->GetClusterCharge(j,charge) ||
411 !track->GetXYWidth(j,sigmaY2) ||
412 !track->GetZWidth(j,sigmaZ2))
414 npads = track->GetNPads(j);
415 CreateDigits(j,npads,pad,time,charge,sigmaY2,sigmaZ2);
419 QSort(fDPt,0,fNDigits);
422 void AliL3Compress::PrintDigits(Int_t padrow=-1)
424 Int_t pad,time,charge,row;
425 for(Int_t i=0; i<fNDigits; i++)
429 if(row != padrow) continue;
431 time = fDPt[i]->fTime;
432 charge = fDPt[i]->fCharge;
433 if(i>0 && row != fDPt[i-1]->fRow)
434 cout<<"---Padrow "<<row<<"---"<<endl;
435 cout<<"Padrow "<<row<<" Pad "<<pad<<" time "<<time<<" charge "<<charge<<endl;
439 void AliL3Compress::WriteRestoredData()
443 //Get the remaining raw data array:
444 AliL3MemHandler *mem = new AliL3MemHandler();
445 sprintf(fname,"%s/remains_%d_%d.raw",fPath,fSlice,fPatch);
446 mem->SetBinaryInput(fname);
448 AliL3DigitRowData *origRow = mem->CompBinary2Memory(numdigits);
449 mem->CloseBinaryInput();
451 //Allocate memory for the merged data:
452 UInt_t size = mem->GetAllocatedSize() + fNDigits*sizeof(AliL3DigitData);
453 cout<<"Allocating "<<size<<" bytes for merged data array "<<endl;
454 Byte_t *data = new Byte_t[size];
456 AliL3DigitRowData *tempRow = (AliL3DigitRowData*)data;
458 Int_t ndigits,action,charge;
461 UInt_t digit_counter;
463 for(Int_t i=AliL3Transform::GetFirstRow(fPatch); i<=AliL3Transform::GetLastRow(fPatch); i++)
467 AliL3DigitData *origDig = origRow->fDigitData;
468 AliL3DigitData *tempDig = tempRow->fDigitData;
469 if((Int_t)origRow->fRow != i)
470 cerr<<"AliL3Compress::WriteRestoredData() : Mismatching row numbering "<<(Int_t)origRow->fRow<<" "<<i<<endl;
472 //cout<<"Writing row "<<i<<" with "<<(Int_t)origRow->fNDigit<<" old digits"<<endl;
477 while(digit_counter < origRow->fNDigit)
479 pad = origDig[digit_counter].fPad;
480 time = origDig[digit_counter].fTime;
481 charge = origDig[digit_counter].fCharge;
483 while( (action=ComparePoints(i,pad,time)) == 1)
485 tempDig[ndigits].fPad = fDPt[fNUsed]->fPad;
486 tempDig[ndigits].fTime = fDPt[fNUsed]->fTime;
487 tempDig[ndigits].fCharge = fDPt[fNUsed]->fCharge;
494 tempDig[ndigits].fPad = pad;
495 tempDig[ndigits].fTime = time;
496 tempDig[ndigits].fCharge = charge;
501 if(fNUsed >= fNDigits)
503 //cerr<<"AliL3Compress::WriteRestoredData() : Array out of range : "<<fNUsed<<" "<<fNDigits<<endl;
506 if(fDPt[fNUsed]->fRow != i) //we are on a new row
508 tempDig[ndigits].fPad = fDPt[fNUsed]->fPad;
509 tempDig[ndigits].fTime = fDPt[fNUsed]->fTime;
510 tempDig[ndigits].fCharge = fDPt[fNUsed]->fCharge;
514 //cout<<"Writing "<<ndigits<<" digits on row "<<i<<endl;
519 tempRow->fNDigit = ndigits;
520 Int_t size = sizeof(AliL3DigitData)*tempRow->fNDigit + sizeof(AliL3DigitRowData);
521 Byte_t *byte_pt = (Byte_t*)tempRow;
523 tempRow = (AliL3DigitRowData*)byte_pt;
524 mem->UpdateRowPointer(origRow);
527 if(row_counter != AliL3Transform::GetNRows(fPatch))
528 cerr<<"AliL3Compress::WriteRestoredData() : Written rows: "<<row_counter<<" total rows "<<AliL3Transform::GetNRows(fPatch)<<endl;
531 sprintf(fname,"%s/restored_%d_%d.raw",fPath,fSlice,fPatch);
532 mem->SetBinaryOutput(fname);
533 mem->Memory2CompBinary((UInt_t)AliL3Transform::GetNRows(fPatch),(AliL3DigitRowData*)data);
534 mem->CloseBinaryOutput();
541 void AliL3Compress::CreateDigits(Int_t row,Int_t npads,Float_t pad,Float_t time,Int_t charge,Float_t sigmaY2,Float_t sigmaZ2)
543 //Create raw data out of the cluster.
545 if(npads == 1)//If there was only 1 pad, the xywidth is set to zero.
548 if(sigmaY2 < 0 || sigmaZ2 <= 0)
550 cerr<<"AliL3Compress::CreateDigits() : Wrong sigmas : "<<sigmaY2<<" "<<sigmaZ2;
551 cerr<<" on row "<<row<<" pad "<<pad<<" time "<<time<<endl;
555 TRandom *random = new TRandom();
558 TH1F *hist1 = new TH1F("hist1","",AliL3Transform::GetNPads(row),0,AliL3Transform::GetNPads(row)-1);
559 TH1F *hist2 = new TH1F("hist2","",AliL3Transform::GetNTimeBins(),0,AliL3Transform::GetNTimeBins()-1);
560 TH2F *hist3 = new TH2F("hist3","",AliL3Transform::GetNPads(row),0,AliL3Transform::GetNPads(row)-1,AliL3Transform::GetNTimeBins(),0,AliL3Transform::GetNTimeBins()-1);
564 //Create the distributions in pad and time:
565 for(Int_t i=0; i<entries; i++)
567 hist1->Fill(random->Gaus(pad,sqrt(sigmaY2)));
568 hist2->Fill(random->Gaus(time,sqrt(sigmaZ2)));
571 //Create the cluster:
573 Double_t content1,content2,dpad,dtime;
574 for(Int_t i=0; i<hist1->GetEntries(); i++)
576 bin1 = hist1->GetBin(i);
577 content1 = hist1->GetBinContent(bin1);
578 if((Int_t)content1==0) continue;
579 content1 = charge*content1/entries;
580 dpad = hist1->GetBinCenter(bin1);
581 for(Int_t j=0; j<hist2->GetEntries(); j++)
583 bin2 = hist2->GetBin(j);
584 content2 = hist2->GetBinContent(bin2);
585 if((Int_t)content2==0) continue;
586 content2 = content1*content2/entries;
587 dtime = hist2->GetBinCenter(bin2);
588 hist3->Fill(dpad,dtime,content2);
593 //Fill it into the digit array:
594 for(Int_t i=0; i<hist3->GetNbinsX(); i++)
596 for(Int_t j=0; j<hist3->GetNbinsY(); j++)
598 bin1 = hist3->GetBin(i,j);
599 content1 = hist3->GetBinContent(bin1);
600 if((Int_t)content1 < 3) continue;
603 if(fNDigits >= fMaxDigits)
605 cerr<<"AliL3Compress::CreateDigits() : Array index out of range : "<<fNDigits<<endl;
608 fDigits[fNDigits].fCharge=(Int_t)content1;
609 fDigits[fNDigits].fRow = row;
610 fDigits[fNDigits].fPad = (Int_t)hist3->GetXaxis()->GetBinCenter(i);
611 fDigits[fNDigits].fTime = (Int_t)hist3->GetYaxis()->GetBinCenter(j);
612 fDPt[fNDigits] = &fDigits[fNDigits];
618 // cout<<"Small cluster "<<local_dig<<" pad "<<(Int_t)fDigits[fNDigits-1].fPad<<" time "<<(Int_t)fDigits[fNDigits-1].fTime<<endl;
626 void AliL3Compress::PrintCompRatio()
629 sprintf(fname,"%s/remains_%d_%d.raw",fPath,fSlice,fPatch);
630 AliL3MemHandler *mem = new AliL3MemHandler();
631 if(!mem->SetBinaryInput(fname))
633 cerr<<"AliL3Compress::PrintCompRatio(): Error opening file: "<<fname<<endl;
637 AliL3DigitRowData *rowPt = (AliL3DigitRowData*)mem->CompBinary2Memory(ndigits);
638 mem->CloseBinaryInput();
640 Int_t digit_counter=0;
641 for(Int_t i=AliL3Transform::GetFirstRow(fPatch); i<=AliL3Transform::GetLastRow(fPatch); i++)
643 digit_counter += rowPt->fNDigit;
644 mem->UpdateRowPointer(rowPt);
648 sprintf(fname,"%s/tracks_c_%d_%d.raw",fPath,fSlice,fPatch);
649 FILE *file1 = fopen(fname,"r");
652 cerr<<"AliL3Compress::PrintCompRatio(): Error opening file: "<<fname<<endl;
655 fseek(file1,0,SEEK_END);
656 UInt_t filesize1 = (UInt_t)ftell(file1);
659 sprintf(fname,"%s/digits_%d_%d.raw",fPath,fSlice,fPatch);
660 FILE *file2 = fopen(fname,"r");
663 cerr<<"AliL3Compress::PrintCompRatio(): Error opening file: "<<fname<<endl;
666 fseek(file2,0,SEEK_END);
667 UInt_t filesize2 = (UInt_t)ftell(file2);
670 cout<<"----------------------"<<endl;
671 cout<<"Original file size : "<<filesize2<<endl;
672 cout<<"Compressed file size : "<<filesize1<<endl;
673 cout<<"Remaining digits : "<<digit_counter<<endl;
674 cout<<"Compression ratio : "<<(Float_t)(filesize1 + (10*digit_counter)/8)/(Float_t)(filesize2)<<endl;
678 void AliL3Compress::QSort(AliL3RandomDigitData **a, Int_t first, Int_t last)
681 // Sort array of AliL3RandomDigitData pointers using a quicksort algorithm.
682 // Uses CompareDigits() to compare objects.
685 static AliL3RandomDigitData *tmp;
686 static int i; // "static" to save stack space
689 while (last - first > 1) {
693 while (++i < last && CompareDigits(a[i], a[first]) < 0)
695 while (--j > first && CompareDigits(a[j], a[first]) > 0)
711 if (j - first < last - (j + 1)) {
713 first = j + 1; // QSort(j + 1, last);
715 QSort(a, j + 1, last);
716 last = j; // QSort(first, j);
721 void AliL3Compress::WriteRootFile(Char_t *newrootfile)
725 AliL3MemHandler *mem = new AliL3MemHandler();
726 sprintf(fname,"%s/restored_%d_%d.raw",fPath,fSlice,fPatch);
727 mem->SetBinaryInput(fname);
729 AliL3DigitRowData *rowPt = (AliL3DigitRowData*)mem->CompBinary2Memory(ndigits);
730 mem->CloseBinaryInput();
732 sprintf(fname,"%s/digitfile",fPath);
734 AliL3FileHandler *file = new AliL3FileHandler();
735 if(!file->SetAliInput(fname))
737 cerr<<"AliL3Compress::WriteRootFile() : Error opening file: "<<fname<<endl;
741 file->Init(fSlice,fPatch);
742 file->AliDigits2RootFile(rowPt,newrootfile);
743 file->CloseAliInput();