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"
18 #include "AliL3FileHandler.h"
21 //_____________________________________________________________
25 // Class for compressing and uncompressing data.
27 ClassImp(AliL3Compress)
29 AliL3Compress::AliL3Compress()
32 SetBitNumbers(0,0,0,0);
39 AliL3Compress::AliL3Compress(Int_t slice,Int_t patch,Char_t *path)
43 SetBitNumbers(0,0,0,0);
47 sprintf(fPath,"%s",path);
50 AliL3Compress::~AliL3Compress()
60 void AliL3Compress::SetBitNumbers(Int_t pad,Int_t time,Int_t charge,Int_t shape)
64 fNumChargeBits=charge;
68 void AliL3Compress::WriteFile(AliL3TrackArray *tracks)
71 sprintf(fname,"%s/tracks_m_%d_%d.raw",fPath,fSlice,fPatch);
72 FILE *file = fopen(fname,"w");
73 Short_t ntracks = tracks->GetNTracks();
74 //cout<<"Writing "<<ntracks<<" tracks to file"<<endl;
77 AliL3ClusterModel *clusters=0;
78 AliL3TrackModel *model=0;
79 for(Int_t i=0; i<ntracks; i++)
81 AliL3ModelTrack *track = (AliL3ModelTrack*)tracks->GetCheckedTrack(i);
85 model = track->GetModel();
86 if(model->fNClusters==0) continue;
87 clusters = track->GetClusters();
88 //cout<<"Writing "<<(int)model->fNClusters<<" clusters"<<endl;
89 if(fwrite(model,sizeof(AliL3TrackModel),1,file)!=1) break;
90 //cout<<"Writing "<<(int)model->fNClusters<<" clusters to file"<<endl;
91 if(fwrite(clusters,model->fNClusters*sizeof(AliL3ClusterModel),1,file)!=1) break;
96 cout<<"Wrote "<<count<<" tracks "<<endl;
100 void AliL3Compress::ReadFile(Char_t which)
102 //Read the trackfile.
106 sprintf(fname,"%s/tracks_m_%d_%d.raw",fPath,fSlice,fPatch);
107 else if(which == 'u')
108 sprintf(fname,"%s/tracks_u_%d_%d.raw",fPath,fSlice,fPatch);
111 cerr<<"AliL3Compress::ReadFile() : Wrong option"<<endl;
115 FILE *file = fopen(fname,"r");
118 cerr<<"Cannot open file "<<fname<<endl;
124 fTracks = new AliL3TrackArray("AliL3ModelTrack");
126 cout<<"Reading file "<<fname<<endl;
129 AliL3ModelTrack *track = (AliL3ModelTrack*)fTracks->NextTrack();
130 track->Init(fSlice,fPatch);
131 AliL3TrackModel *model = track->GetModel();
132 AliL3ClusterModel *clusters = track->GetClusters();
133 //cout<<"Reading model "<<(int)model<<endl;
134 if(fread(model,sizeof(AliL3TrackModel),1,file)!=1) break;
135 //cout<<"Reading clusters "<<(int)clusters<<endl;
136 if(fread(clusters,(model->fNClusters)*sizeof(AliL3ClusterModel),1,file)!=1) break;
137 //cout<<"Filling track"<<endl;
142 fTracks->RemoveLast();
143 cout<<"Read "<<fTracks->GetNTracks()<<" tracks from file"<<endl;
147 void AliL3Compress::CompressFile()
151 cerr<<"AliL3Compress::CompressFile() : Bitnumbers not set"<<endl;
156 sprintf(fname,"%s/tracks_c_%d_%d.raw",fPath,fSlice,fPatch);
157 BIT_FILE *output = OpenOutputBitFile(fname);
159 sprintf(fname,"%s/tracks_m_%d_%d.raw",fPath,fSlice,fPatch);
160 FILE *input = fopen(fname,"r");
163 cerr<<"AliL3Compress::CompressFile() : Error opening file: "<<fname<<endl;
167 AliL3TrackModel track;
168 AliL3ClusterModel cluster;
172 Int_t timeo,pado,chargeo,shapeo;
173 timeo=pado=chargeo=shapeo=0;
176 if(fread(&track,sizeof(AliL3TrackModel),1,input)!=1) break;
178 if(output->mask != 0x80) //Write the current byte to file.
180 if(putc(output->rack,output->file )!=output->rack)
181 cerr<<"AliL3Compress::ComressFile : Error writing to bitfile"<<endl;
186 //Write track parameters:
187 fwrite(&track,sizeof(AliL3TrackModel),1,output->file);
188 for(Int_t i=0; i<track.fNClusters; i++)
190 if(fread(&cluster,sizeof(AliL3ClusterModel),1,input)!=1) break;
193 temp = (Int_t)cluster.fPresent;
194 OutputBit(output,temp);
197 //Write time information:
198 temp = (Int_t)cluster.fDTime;
203 power = 1<<fNumTimeBits;
209 OutputBits(output,abs(temp),fNumTimeBits);
211 //Write pad information:
212 temp = (Int_t)cluster.fDPad;
217 power = 1<<fNumPadBits;
223 OutputBits(output,abs(temp),fNumPadBits);
225 //Write charge information:
226 temp = (Int_t)cluster.fDCharge;
231 power = 1<<fNumChargeBits;
237 OutputBits(output,abs(temp),fNumChargeBits);
239 //Write shape information:
240 temp = (Int_t)cluster.fDSigmaY2;
241 power = 1<<fNumShapeBits;
242 if(abs(temp) >= power)
247 OutputBits(output,abs(temp),fNumShapeBits);
249 temp = (Int_t)cluster.fDSigmaZ2;
250 if(abs(temp) >= power)
255 OutputBits(output,abs(temp),fNumShapeBits);
260 CloseOutputBitFile(output);
262 cout<<endl<<"There was following number of overflows: "<<endl
264 <<"Time "<<timeo<<endl
265 <<"Charge "<<chargeo<<endl
266 <<"Shape "<<shapeo<<endl;
269 void AliL3Compress::ExpandFile()
273 cerr<<"AliL3Compress::ExpandFile() : Bitnumbers not set"<<endl;
278 sprintf(fname,"%s/tracks_c_%d_%d.raw",fPath,fSlice,fPatch);
279 BIT_FILE *input = OpenInputBitFile(fname);
281 sprintf(fname,"%s/tracks_u_%d_%d.raw",fPath,fSlice,fPatch);
282 FILE *output = fopen(fname,"w");
285 cerr<<"AliL3Compress::ExpandFile() : Error opening file: "<<fname<<endl;
289 AliL3TrackModel trackmodel;
290 AliL3ClusterModel *clusters=0;
293 clusters = new AliL3ClusterModel[(NumRows[fPatch])];
294 while(!feof(input->file))
296 input->mask=0x80;//make sure we read a new byte from file.
298 //Read and write track:
299 if(fread(&trackmodel,sizeof(AliL3TrackModel),1,input->file)!=1) break;
300 fwrite(&trackmodel,sizeof(AliL3TrackModel),1,output);
302 for(Int_t i=0; i<NumRows[fPatch]; i++)
307 temp = InputBit(input);
310 clusters[i].fPresent=kFALSE;
313 clusters[i].fPresent=kTRUE;
315 //Read time information:
316 sign=InputBit(input);
317 temp = InputBits(input,fNumTimeBits);
320 clusters[i].fDTime = temp;
322 //Read pad information:
323 sign=InputBit(input);
324 temp = InputBits(input,fNumPadBits);
327 clusters[i].fDPad = temp;
329 //Read charge information:
330 sign = InputBit(input);
331 temp=InputBits(input,fNumChargeBits);
334 clusters[i].fDCharge = temp;
336 //Read shape information:
337 temp = InputBits(input,fNumShapeBits);
338 clusters[i].fDSigmaY2 = temp;
340 temp = InputBits(input,fNumShapeBits);
341 clusters[i].fDSigmaZ2 = temp;
346 fwrite(clusters,(trackmodel.fNClusters)*sizeof(AliL3ClusterModel),1,output);
352 CloseInputBitFile(input);
355 void AliL3Compress::CreateDigitArray(Int_t maxnumber)
359 fMaxDigits=maxnumber;
360 if(fDigits) delete [] fDigits;
361 fDigits = new AliL3RandomDigitData[maxnumber];
362 if(fDPt) delete [] fDPt;
363 fDPt = new AliL3RandomDigitData*[maxnumber];
366 void AliL3Compress::RestoreData()
369 //Read the uncompressed file:
372 CreateDigitArray(100000);
374 Float_t pad,time,sigmaY2,sigmaZ2;
376 for(Int_t j=NRows[fPatch][0]; j<=NRows[fPatch][1]; j++)
378 for(Int_t i=0; i<fTracks->GetNTracks(); i++)
380 AliL3ModelTrack *track = (AliL3ModelTrack*)fTracks->GetCheckedTrack(i);
382 if(!track->GetPad(j,pad) ||
383 !track->GetTime(j,time) ||
384 !track->GetClusterCharge(j,charge) ||
385 !track->GetXYWidth(j,sigmaY2) ||
386 !track->GetZWidth(j,sigmaZ2))
389 CreateDigits(j,pad,time,charge,sigmaY2,sigmaZ2);
393 QSort(fDPt,0,fNDigits);
396 void AliL3Compress::PrintDigits()
398 Int_t pad,time,charge,row;
399 for(Int_t i=0; i<fNDigits; i++)
403 time = fDPt[i]->fTime;
404 charge = fDPt[i]->fCharge;
405 if(i>0 && row != fDPt[i-1]->fRow)
406 cout<<"---Padrow "<<row<<"---"<<endl;
407 cout<<"Pad "<<pad<<" time "<<time<<" charge "<<charge<<endl;
411 void AliL3Compress::WriteRestoredData()
415 //Get the remaining raw data array:
416 AliL3MemHandler *mem = new AliL3MemHandler();
417 sprintf(fname,"%s/remains_%d_%d.raw",fPath,fSlice,fPatch);
418 mem->SetBinaryInput(fname);
420 AliL3DigitRowData *origRow = mem->CompBinary2Memory(numdigits);
421 mem->CloseBinaryInput();
423 //Allocate memory for the merged data:
424 UInt_t size = mem->GetAllocatedSize() + fNDigits*sizeof(AliL3DigitData);
425 cout<<"Allocating "<<size<<" bytes for merged data array "<<endl;
426 Byte_t *data = new Byte_t[size];
428 AliL3DigitRowData *tempRow = (AliL3DigitRowData*)data;
430 Int_t ndigits,action,charge;
433 UInt_t digit_counter;
435 for(Int_t i=NRows[fPatch][0]; i<=NRows[fPatch][1]; i++)
439 AliL3DigitData *origDig = origRow->fDigitData;
440 AliL3DigitData *tempDig = tempRow->fDigitData;
441 if((Int_t)origRow->fRow != i)
442 cerr<<"AliL3Compress::WriteRestoredData() : Mismatching row numbering "<<(Int_t)origRow->fRow<<" "<<i<<endl;
444 //cout<<"Writing row "<<i<<" with "<<(Int_t)origRow->fNDigit<<" old digits"<<endl;
449 while(digit_counter < origRow->fNDigit)
451 pad = origDig[digit_counter].fPad;
452 time = origDig[digit_counter].fTime;
453 charge = origDig[digit_counter].fCharge;
455 while((action=ComparePoints(i,pad,time)) == 1)
457 tempDig[ndigits].fPad = fDPt[fNUsed]->fPad;
458 tempDig[ndigits].fTime = fDPt[fNUsed]->fTime;
459 tempDig[ndigits].fCharge = fDPt[fNUsed]->fCharge;
466 tempDig[ndigits].fPad = pad;
467 tempDig[ndigits].fTime = time;
468 tempDig[ndigits].fCharge = charge;
473 if(fNUsed >= fNDigits)
475 if(fDPt[fNUsed]->fRow != i) //we are on a new row
477 tempDig[ndigits].fPad = fDPt[fNUsed]->fPad;
478 tempDig[ndigits].fTime = fDPt[fNUsed]->fTime;
479 tempDig[ndigits].fCharge = fDPt[fNUsed]->fCharge;
483 //cout<<"Writing "<<ndigits<<" digits on row "<<i<<endl;
487 cout<<"Few digits on row "<<i<<endl;
489 tempRow->fNDigit = ndigits;
490 Int_t size = sizeof(AliL3DigitData)*tempRow->fNDigit + sizeof(AliL3DigitRowData);
491 Byte_t *byte_pt = (Byte_t*)tempRow;
493 tempRow = (AliL3DigitRowData*)byte_pt;
494 mem->UpdateRowPointer(origRow);
497 if(row_counter != NumRows[fPatch])
498 cerr<<"AliL3Compress::WriteRestoredData() : Written rows: "<<row_counter<<" total rows "<<NumRows[fPatch]<<endl;
501 sprintf(fname,"%s/restored_%d_%d.raw",fPath,fSlice,fPatch);
502 mem->SetBinaryOutput(fname);
503 mem->Memory2CompBinary((UInt_t)NumRows[fPatch],(AliL3DigitRowData*)data);
504 mem->CloseBinaryOutput();
511 void AliL3Compress::CreateDigits(Int_t row,Float_t pad,Float_t time,Int_t charge,Float_t sigmaY2,Float_t sigmaZ2)
513 //Create raw data out of the cluster.
515 AliL3Transform *tr = new AliL3Transform();
516 TRandom *random = new TRandom();
519 TH1F *hist1 = new TH1F("hist1","",tr->GetNPads(row),0,tr->GetNPads(row)-1);
520 TH1F *hist2 = new TH1F("hist2","",tr->GetNTimeBins(),0,tr->GetNTimeBins()-1);
521 TH2F *hist3 = new TH2F("hist3","",tr->GetNPads(row),0,tr->GetNPads(row)-1,tr->GetNTimeBins(),0,tr->GetNTimeBins()-1);
523 //Convert back the sigmas:
526 padw = tr->GetPadPitchWidthLow();
528 padw = tr->GetPadPitchWidthUp();
529 timew = tr->GetZWidth();
532 sigmaY2 = sigmaY2/2.07;
533 sigmaY2 = sigmaY2/0.108;
534 sigmaY2 = sigmaY2/(padw*padw);
535 sigmaY2 = sigmaY2 - 1./12;
538 sigmaZ2 = sigmaZ2/1.77;
539 sigmaZ2 = sigmaZ2/0.169;
540 sigmaZ2 = sigmaZ2/(timew*timew);
541 sigmaZ2 = sigmaZ2 - 1./12;
543 if(sigmaY2 <= 0 || sigmaZ2 <= 0)
545 cerr<<"AliL3Compress::CreateDigits() : Wrong sigmas : "<<sigmaY2<<" "<<sigmaZ2<<endl;
549 //Create the distributions in pad and time:
550 for(Int_t i=0; i<entries; i++)
552 hist1->Fill(random->Gaus(pad,sqrt(sigmaY2)));
553 hist2->Fill(random->Gaus(time,sqrt(sigmaZ2)));
556 //Create the cluster:
558 Double_t content1,content2,dpad,dtime;
559 for(Int_t i=0; i<hist1->GetEntries(); i++)
561 bin1 = hist1->GetBin(i);
562 content1 = hist1->GetBinContent(bin1);
563 if((Int_t)content1==0) continue;
564 content1 = charge*content1/entries;
565 dpad = hist1->GetBinCenter(bin1);
566 for(Int_t j=0; j<hist2->GetEntries(); j++)
568 bin2 = hist2->GetBin(j);
569 content2 = hist2->GetBinContent(bin2);
570 if((Int_t)content2==0) continue;
571 content2 = content1*content2/entries;
572 dtime = hist2->GetBinCenter(bin2);
573 hist3->Fill(dpad,dtime,content2);
577 //Fill it into the digit array:
578 for(Int_t i=0; i<hist3->GetNbinsX(); i++)
580 for(Int_t j=0; j<hist3->GetNbinsY(); j++)
582 bin1 = hist3->GetBin(i,j);
583 content1 = hist3->GetBinContent(bin1);
584 if((Int_t)content1 < 3) continue;
587 if(fNDigits >= fMaxDigits)
589 cerr<<"AliL3Compress::CreateDigits() : Array index out of range : "<<fNDigits<<endl;
592 fDigits[fNDigits].fCharge=(Int_t)content1;
593 fDigits[fNDigits].fRow = row;
594 fDigits[fNDigits].fPad = (Int_t)hist3->GetXaxis()->GetBinCenter(i);
595 fDigits[fNDigits].fTime = (Int_t)hist3->GetYaxis()->GetBinCenter(j);
596 fDPt[fNDigits] = &fDigits[fNDigits];
608 void AliL3Compress::QSort(AliL3RandomDigitData **a, Int_t first, Int_t last)
611 // Sort array of AliL3RandomDigitData pointers using a quicksort algorithm.
612 // Uses CompareDigits() to compare objects.
615 static AliL3RandomDigitData *tmp;
616 static int i; // "static" to save stack space
619 while (last - first > 1) {
623 while (++i < last && CompareDigits(a[i], a[first]) < 0)
625 while (--j > first && CompareDigits(a[j], a[first]) > 0)
641 if (j - first < last - (j + 1)) {
643 first = j + 1; // QSort(j + 1, last);
645 QSort(a, j + 1, last);
646 last = j; // QSort(first, j);
651 void AliL3Compress::WriteRootFile(Char_t *digitsfile,Char_t *rootfile)
654 AliL3MemHandler *mem = new AliL3MemHandler();
655 sprintf(fname,"%s/restored_%d_%d.raw",fPath,fSlice,fPatch);
656 mem->SetBinaryInput(fname);
658 AliL3DigitRowData *rowPt = (AliL3DigitRowData*)mem->CompBinary2Memory(ndigits);
659 mem->CloseBinaryInput();
661 AliL3FileHandler *file = new AliL3FileHandler();
662 if(!file->SetAliInput(digitsfile))
664 cerr<<"AliL3Compress::WriteRootFile() : Error opening file: "<<digitsfile<<endl;
667 file->Init(fSlice,fPatch,NRows[fPatch]);
668 file->AliDigits2RootFile(rowPt,rootfile);
669 file->CloseAliInput();