3 // Author: Anders Vestbo <mailto:vestbo@fi.uib.no>
4 //*-- Copyright © ALICE HLT Group
6 #include "AliL3StandardIncludes.h"
9 #include "AliL3Logging.h"
10 #include "AliL3HoughMerger.h"
11 #include "AliL3HoughIntMerger.h"
12 #include "AliL3HoughGlobalMerger.h"
13 #include "AliL3Histogram.h"
14 #include "AliL3Hough.h"
15 #include "AliL3HoughTransformer.h"
16 #include "AliL3HoughClusterTransformer.h"
17 #include "AliL3HoughTransformerLUT.h"
18 #include "AliL3HoughTransformerVhdl.h"
19 #include "AliL3HoughTransformerRow.h"
20 #include "AliL3HoughMaxFinder.h"
21 #include "AliL3Benchmark.h"
23 #include "AliL3FileHandler.h"
25 #include "AliL3MemHandler.h"
27 #include "AliL3DataHandler.h"
28 #include "AliL3DigitData.h"
29 #include "AliL3HoughEval.h"
30 #include "AliL3Transform.h"
31 #include "AliL3TrackArray.h"
32 #include "AliL3HoughTrack.h"
33 #include "AliL3DDLDataFileHandler.h"
43 //_____________________________________________________________
46 // Interface class for the Hough transform
48 // Example how to use:
50 // AliL3Hough *hough = new AliL3Hough(path,kTRUE,NumberOfEtaSegments);
51 // hough->ReadData(slice);
52 // hough->Transform();
53 // hough->FindTrackCandidates();
55 // AliL3TrackArray *tracks = hough->GetTracks(patch);
62 AliL3Hough::AliL3Hough()
67 fAddHistograms = kFALSE;
68 fDoIterative = kFALSE;
69 fWriteDigits = kFALSE;
73 fHoughTransformer = 0;
95 SetTransformerParams();
100 //just be sure that index is empty for new event
101 AliL3FileHandler::CleanStaticIndex();
109 AliL3Hough::AliL3Hough(Char_t *path,Bool_t binary,Int_t netasegments,Bool_t bit8,Int_t tv,Char_t *infile,Char_t *ptr)
114 fNEtaSegments = netasegments;
115 fAddHistograms = kFALSE;
116 fDoIterative = kFALSE;
117 fWriteDigits = kFALSE;
137 //just be sure that index is empty for new event
138 AliL3FileHandler::CleanStaticIndex();
146 AliL3Hough::~AliL3Hough()
153 //cout << "Cleaned class merger " << endl;
156 //cout << "Cleaned class inter " << endl;
159 //cout << "Cleaned class peak " << endl;
161 delete fGlobalMerger;
162 //cout << "Cleaned class global " << endl;
165 //cout << "Cleaned class bench " << endl;
167 delete fGlobalTracks;
168 //cout << "Cleaned class globaltracks " << endl;
170 // fThread->Delete();
176 void AliL3Hough::CleanUp()
180 for(Int_t i=0; i<fNPatches; i++)
182 if(fTracks[i]) delete fTracks[i];
183 //cout << "Cleaned tracks " << i << endl;
184 if(fEval[i]) delete fEval[i];
185 //cout << "Cleaned eval " << i << endl;
186 if(fHoughTransformer[i]) delete fHoughTransformer[i];
187 //cout << "Cleaned traf " << i << endl;
188 if(fMemHandler[i]) delete fMemHandler[i];
189 //cout << "Cleaned mem " << i << endl;
192 if(fTracks) delete [] fTracks;
193 //cout << "Cleaned class tracks " << endl;
194 if(fEval) delete [] fEval;
195 //cout << "Cleaned class eval " << endl;
196 if(fHoughTransformer) delete [] fHoughTransformer;
197 //cout << "Cleaned cleass trafo " << endl;
198 if(fMemHandler) delete [] fMemHandler;
199 //cout << "Cleaned class mem " << endl;
202 void AliL3Hough::Init(Char_t *path,Bool_t binary,Int_t netasegments,Bool_t bit8,Int_t tv,Char_t *infile,Char_t *ptr,Float_t zvertex)
204 //Normal init of the AliL3Hough
207 fNEtaSegments = netasegments;
208 fWriteDigits = kFALSE;
227 Init(); //do the rest
230 void AliL3Hough::Init(Bool_t doit, Bool_t addhists)
234 fAddHistograms = addhists;
236 fNPatches = AliL3Transform::GetNPatches();
237 fHoughTransformer = new AliL3HoughBaseTransformer*[fNPatches];
238 fMemHandler = new AliL3MemHandler*[fNPatches];
240 fTracks = new AliL3TrackArray*[fNPatches];
241 fEval = new AliL3HoughEval*[fNPatches];
243 fGlobalTracks = new AliL3TrackArray("AliL3HoughTrack");
245 AliL3HoughBaseTransformer *lasttransformer = 0;
247 for(Int_t i=0; i<fNPatches; i++)
249 switch (fVersion){ //choose Transformer
251 fHoughTransformer[i] = new AliL3HoughTransformerLUT(0,i,fNEtaSegments);
254 fHoughTransformer[i] = new AliL3HoughClusterTransformer(0,i,fNEtaSegments);
257 fHoughTransformer[i] = new AliL3HoughTransformerVhdl(0,i,fNEtaSegments,fNSaveIterations);
260 fHoughTransformer[i] = new AliL3HoughTransformerRow(0,i,fNEtaSegments,kFALSE,fZVertex);
263 fHoughTransformer[i] = new AliL3HoughTransformer(0,i,fNEtaSegments,kFALSE,kFALSE);
266 fHoughTransformer[i]->SetLastTransformer(lasttransformer);
267 lasttransformer = fHoughTransformer[i];
268 // fHoughTransformer[i]->CreateHistograms(fNBinX[i],fLowPt[i],fNBinY[i],-fPhi[i],fPhi[i]);
269 fHoughTransformer[i]->CreateHistograms(fNBinX[i],-fLowPt[i],fLowPt[i],fNBinY[i],-fPhi[i],fPhi[i]);
270 //fHoughTransformer[i]->CreateHistograms(fLowPt[i],fUpperPt[i],fPtRes[i],fNBinY[i],fPhi[i]);
272 fHoughTransformer[i]->SetLowerThreshold(fThreshold[i]);
273 fHoughTransformer[i]->SetUpperThreshold(100);
275 LOG(AliL3Log::kInformational,"AliL3Hough::Init","Version")
276 <<"Initializing Hough transformer version "<<fVersion<<ENDLOG;
278 fEval[i] = new AliL3HoughEval();
279 fTracks[i] = new AliL3TrackArray("AliL3HoughTrack");
281 fMemHandler[i] = new AliL3DataHandler();
287 /* In case of reading digits file */
288 fMemHandler[i] = new AliL3FileHandler(kTRUE); //use static index
293 Char_t filename[1024];
294 sprintf(filename,"%s/digitfile.root",fPath);
295 fMemHandler[i]->SetAliInput(filename);
299 fMemHandler[i]->SetAliInput(fRunLoader);
305 /* In case of reading from DATE */
306 fMemHandler[i] = new AliL3DDLDataFileHandler();
307 fMemHandler[i]->SetReaderInput(fInputPtr,-1);
311 /* In case of reading rawdata from ROOT file */
312 fMemHandler[i] = new AliL3DDLDataFileHandler();
313 fMemHandler[i]->SetReaderInput(fInputFile);
317 fMemHandler[i] = new AliL3MemHandler();
321 fPeakFinder = new AliL3HoughMaxFinder("KappaPhi",50000);
322 fMerger = new AliL3HoughMerger(fNPatches);
323 fInterMerger = new AliL3HoughIntMerger();
325 fBenchmark = new AliL3Benchmark();
328 void AliL3Hough::SetTransformerParams(Float_t ptres,Float_t ptmin,Float_t ptmax,Int_t ny,Int_t patch)
330 // Setup the parameters for the Hough Transformer
336 mrow = AliL3Transform::GetLastRow(patch);
339 Double_t lineradius = sqrt(pow(AliL3Transform::Row2X(mrow),2) + pow(AliL3Transform::GetMaxY(mrow),2));
340 Double_t kappa = -1*AliL3Transform::GetBField()*AliL3Transform::GetBFact()/ptmin;
341 psi = AliL3Transform::Deg2Rad(10) - asin(lineradius*kappa/2);
342 cout<<"Calculated psi range "<<psi<<" in patch "<<patch<<endl;
361 fPtRes[patch] = ptres;
362 fLowPt[patch] = ptmin;
363 fUpperPt[patch] = ptmax;
368 void AliL3Hough::SetTransformerParams(Int_t nx,Int_t ny,Float_t ptmin,Int_t patch)
370 // Setup the parameters for the Hough Transformer
373 Double_t lineradius = sqrt(pow(AliL3Transform::Row2X(mrow),2) + pow(AliL3Transform::GetMaxY(mrow),2));
374 Double_t kappa = -1*AliL3Transform::GetBField()*AliL3Transform::GetBFact()/ptmin;
375 Double_t psi = AliL3Transform::Deg2Rad(10) - asin(lineradius*kappa/2);
376 cout<<"Calculated psi range "<<psi<<" in patch "<<patch<<endl;
389 void AliL3Hough::SetTransformerParams(Int_t nx,Int_t ny,Float_t ptmin,Int_t /*patch*/)
391 // Setup the parameters for the Hough Transformer
395 Double_t lineradius = sqrt(pow(AliL3Transform::Row2X(mrow),2) + pow(AliL3Transform::GetMaxY(mrow),2));
396 Double_t alpha1 = AliL3Transform::GetMaxY(mrow)/pow(lineradius,2);
397 Double_t kappa = 1*AliL3Transform::GetBField()*AliL3Transform::GetBFact()/ptmin;
398 Double_t psi = AliL3Transform::Deg2Rad(10) - asin(lineradius*kappa/2);
399 // cout<<"Calculated psi range "<<psi<<" in patch "<<patch<<endl;
400 AliL3HoughTrack track;
401 track.SetTrackParameters(kappa,psi,1);
404 track.GetCrossingPoint(mrow2,hit);
405 Double_t lineradius2 = sqrt(pow(AliL3Transform::Row2X(mrow2),2) + pow(AliL3Transform::GetMaxY(mrow2),2));
406 Double_t alpha2 = hit[1]/pow(lineradius2,2);
407 // cout<<"Calculated alphas range "<<alpha1<<" "<<alpha2<<" in patch "<<patch<<endl;
412 fLowPt[i] = 1.15*alpha1;
415 fPhi[i] = 1.15*alpha2;
420 void AliL3Hough::SetTransformerParams(Int_t nx,Int_t ny,Float_t lpt,Float_t phi)
433 void AliL3Hough::SetThreshold(Int_t t3,Int_t patch)
435 // Set digits threshold
443 fThreshold[patch]=t3;
446 void AliL3Hough::SetPeakThreshold(Int_t threshold,Int_t patch)
448 // Set Peak Finder threshold
453 fPeakThreshold[i++]=threshold;
456 fPeakThreshold[patch]=threshold;
459 void AliL3Hough::DoBench(Char_t *name)
461 fBenchmark->Analyze(name);
464 void AliL3Hough::Process(Int_t minslice,Int_t maxslice)
466 //Process all slices [minslice,maxslice].
467 fGlobalMerger = new AliL3HoughGlobalMerger(minslice,maxslice);
469 for(Int_t i=minslice; i<=maxslice; i++)
477 AddAllHistogramsRows();
479 FindTrackCandidates();
481 //fGlobalMerger->FillTracks(fTracks[0],i);
485 void AliL3Hough::ReadData(Int_t slice,Int_t eventnr)
487 //Read data from files, binary or root.
490 if(fEvent!=eventnr) //just be sure that index is empty for new event
491 AliL3FileHandler::CleanStaticIndex();
493 fCurrentSlice = slice;
495 for(Int_t i=0; i<fNPatches; i++)
497 fMemHandler[i]->Free();
499 AliL3DigitRowData *digits =0;
501 fMemHandler[i]->Init(slice,i);
502 if(fBinary)//take input data from binary files
505 sprintf(name,"%s/binaries/digits_c8_%d_%d_%d.raw",fPath,eventnr,slice,i);
507 sprintf(name,"%s/binaries/digits_%d_%d_%d.raw",fPath,eventnr,slice,i);
509 fMemHandler[i]->SetBinaryInput(name);
510 digits = (AliL3DigitRowData *)fMemHandler[i]->CompBinary2Memory(ndigits);
511 fMemHandler[i]->CloseBinaryInput();
513 else //read data from root file
517 fMemHandler[i]->FreeDigitsTree();//or else the new event is not loaded
518 digits=(AliL3DigitRowData *)fMemHandler[i]->AliAltroDigits2Memory(ndigits,eventnr);
520 cerr<<"You cannot read from rootfile now"<<endl;
524 //Set the pointer to the TPCRawStream in case of fast raw data reading
525 fHoughTransformer[i]->SetTPCRawStream(fMemHandler[i]->GetTPCRawStream());
527 //set input data and init transformer
528 fHoughTransformer[i]->SetInputData(ndigits,digits);
529 fHoughTransformer[i]->Init(slice,i,fNEtaSegments);
535 void AliL3Hough::Transform(Int_t *rowrange)
537 //Transform all data given to the transformer within the given slice
538 //(after ReadData(slice))
540 Double_t initTime,cpuTime;
541 initTime = GetCpuTime();
542 Int_t patchorder[6] = {5,2,0,1,3,4}; //The order in which patches are processed
543 // Int_t patchorder[6] = {0,1,2,3,4,5}; //The order in which patches are processed
544 // Int_t patchorder[6] = {5,4,3,2,1,0}; //The order in which patches are processed
545 // Int_t patchorder[6] = {5,2,4,3,1,0}; //The order in which patches are processed
547 for(Int_t i=0; i<fNPatches; i++)
549 // In case of Row transformer reset the arrays only once
550 if((fVersion != 4) || (i == 0)) {
551 fBenchmark->Start("Hough Reset");
552 fHoughTransformer[0]->Reset();//Reset the histograms
553 fBenchmark->Stop("Hough Reset");
555 fBenchmark->Start("Hough Transform");
556 PrepareForNextPatch(patchorder[i]);
559 sprintf(buf,"Patch %d",patchorder[i]);
560 fBenchmark->Start(buf);
561 fHoughTransformer[patchorder[i]]->SetLastPatch(fLastPatch);
562 fHoughTransformer[patchorder[i]]->TransformCircle();
563 fBenchmark->Stop(buf);
566 fHoughTransformer[i]->TransformCircleC(rowrange,1);
567 fBenchmark->Stop("Hough Transform");
568 fLastPatch=patchorder[i];
570 cpuTime = GetCpuTime() - initTime;
571 LOG(AliL3Log::kInformational,"AliL3Hough::Transform()","Timing")
572 <<"Transform done in average per patch of "<<cpuTime*1000/fNPatches<<" ms"<<ENDLOG;
575 void AliL3Hough::MergePatches()
577 // Merge patches if they are not summed
578 if(fAddHistograms) //Nothing to merge here
580 fMerger->MergePatches(kTRUE);
583 void AliL3Hough::MergeInternally()
585 // Merge patches internally
587 fInterMerger->FillTracks(fTracks[0]);
589 fInterMerger->FillTracks(fMerger->GetOutTracks());
591 fInterMerger->MMerge();
594 void AliL3Hough::ProcessSliceIter()
596 //Process current slice (after ReadData(slice)) iteratively.
600 for(Int_t i=0; i<fNPatches; i++)
603 fMerger->FillTracks(fTracks[i],i); //Copy tracks to merger
608 for(Int_t i=0; i<10; i++)
613 AliL3HoughBaseTransformer *tr = fHoughTransformer[0];
614 for(Int_t j=0; j<fNEtaSegments; j++)
616 AliL3Histogram *hist = tr->GetHistogram(j);
617 if(hist->GetNEntries()==0) continue;
618 fPeakFinder->Reset();
619 fPeakFinder->SetHistogram(hist);
620 fPeakFinder->FindAbsMaxima();
621 AliL3HoughTrack *track = (AliL3HoughTrack*)fTracks[0]->NextTrack();
622 track->SetTrackParameters(fPeakFinder->GetXPeak(0),fPeakFinder->GetYPeak(0),fPeakFinder->GetWeight(0));
623 track->SetEtaIndex(j);
624 track->SetEta(tr->GetEta(j,fCurrentSlice));
625 for(Int_t k=0; k<fNPatches; k++)
627 fEval[i]->SetNumOfPadsToLook(2);
628 fEval[i]->SetNumOfRowsToMiss(2);
629 fEval[i]->RemoveFoundTracks();
632 if(!fEval[i]->LookInsideRoad(track,nrows))
634 fTracks[0]->Remove(fTracks[0]->GetNTracks()-1);
635 fTracks[0]->Compress();
646 void AliL3Hough::ProcessPatchIter(Int_t patch)
648 //Process patch in a iterative way.
649 //transform + peakfinding + evaluation + transform +...
651 Int_t numoftries = 5;
652 AliL3HoughBaseTransformer *tr = fHoughTransformer[patch];
653 AliL3TrackArray *tracks = fTracks[patch];
655 AliL3HoughEval *ev = fEval[patch];
656 ev->InitTransformer(tr);
657 //ev->RemoveFoundTracks();
658 ev->SetNumOfRowsToMiss(3);
659 ev->SetNumOfPadsToLook(2);
660 AliL3Histogram *hist;
661 for(Int_t t=0; t<numoftries; t++)
664 tr->TransformCircle();
665 for(Int_t i=0; i<fNEtaSegments; i++)
667 hist = tr->GetHistogram(i);
668 if(hist->GetNEntries()==0) continue;
669 fPeakFinder->Reset();
670 fPeakFinder->SetHistogram(hist);
671 fPeakFinder->FindAbsMaxima();
672 //fPeakFinder->FindPeak1();
673 AliL3HoughTrack *track = (AliL3HoughTrack*)tracks->NextTrack();
674 track->SetTrackParameters(fPeakFinder->GetXPeak(0),fPeakFinder->GetYPeak(0),fPeakFinder->GetWeight(0));
675 track->SetEtaIndex(i);
676 track->SetEta(tr->GetEta(i,fCurrentSlice));
679 if(!ev->LookInsideRoad(track,nrows))
681 tracks->Remove(tracks->GetNTracks()-1);
688 LOG(AliL3Log::kInformational,"AliL3Hough::ProcessPatch","NTracks")
689 <<AliL3Log::kDec<<"Found "<<tracks->GetNTracks()<<" tracks in patch "<<patch<<ENDLOG;
692 void AliL3Hough::AddAllHistograms()
694 //Add the histograms within one etaslice.
695 //Resulting histogram are in patch=0.
697 Double_t initTime,cpuTime;
698 initTime = GetCpuTime();
699 fBenchmark->Start("Add Histograms");
700 for(Int_t i=0; i<fNEtaSegments; i++)
702 AliL3Histogram *hist0 = fHoughTransformer[0]->GetHistogram(i);
703 for(Int_t j=1; j<fNPatches; j++)
705 AliL3Histogram *hist = fHoughTransformer[j]->GetHistogram(i);
709 fBenchmark->Stop("Add Histograms");
710 fAddHistograms = kTRUE;
711 cpuTime = GetCpuTime() - initTime;
712 LOG(AliL3Log::kInformational,"AliL3Hough::AddAllHistograms()","Timing")
713 <<"Adding histograms in "<<cpuTime*1000<<" ms"<<ENDLOG;
716 void AliL3Hough::AddAllHistogramsRows()
718 //Add the histograms within one etaslice.
719 //Resulting histogram are in patch=0.
721 Double_t initTime,cpuTime;
722 initTime = GetCpuTime();
723 fBenchmark->Start("Add HistogramsRows");
725 UChar_t lastpatchlastrow = AliL3Transform::GetLastRowOnDDL(fLastPatch)+1;
727 UChar_t *tracklastrow = ((AliL3HoughTransformerRow *)fHoughTransformer[0])->GetTrackLastRow();
729 for(Int_t i=0; i<fNEtaSegments; i++)
731 UChar_t *gapcount = ((AliL3HoughTransformerRow *)fHoughTransformer[0])->GetGapCount(i);
732 UChar_t *currentrowcount = ((AliL3HoughTransformerRow *)fHoughTransformer[0])->GetCurrentRowCount(i);
734 AliL3Histogram *hist = fHoughTransformer[0]->GetHistogram(i);
735 Int_t xmin = hist->GetFirstXbin();
736 Int_t xmax = hist->GetLastXbin();
737 Int_t ymin = hist->GetFirstYbin();
738 Int_t ymax = hist->GetLastYbin();
739 Int_t nxbins = hist->GetNbinsX()+2;
741 for(Int_t ybin=ymin; ybin<=ymax; ybin++)
743 for(Int_t xbin=xmin; xbin<=xmax; xbin++)
745 Int_t bin = xbin + ybin*nxbins; //Int_t bin = hist->GetBin(xbin,ybin);
746 if(gapcount[bin] < MAX_N_GAPS) {
747 if(tracklastrow[bin] > lastpatchlastrow) {
748 if(lastpatchlastrow > currentrowcount[bin])
749 gapcount[bin] += (lastpatchlastrow-currentrowcount[bin]-1);
752 if(tracklastrow[bin] > currentrowcount[bin])
753 gapcount[bin] += (tracklastrow[bin]-currentrowcount[bin]-1);
755 if(gapcount[bin] < MAX_N_GAPS)
756 hist->AddBinContent(bin,(159-gapcount[bin]));
762 fBenchmark->Stop("Add HistogramsRows");
763 fAddHistograms = kTRUE;
764 cpuTime = GetCpuTime() - initTime;
765 LOG(AliL3Log::kInformational,"AliL3Hough::AddAllHistogramsRows()","Timing")
766 <<"Adding histograms in "<<cpuTime*1000<<" ms"<<ENDLOG;
769 void AliL3Hough::PrepareForNextPatch(Int_t nextpatch)
772 sprintf(buf,"Prepare For Patch %d",nextpatch);
773 fBenchmark->Start(buf);
775 UChar_t lastpatchlastrow;
777 lastpatchlastrow = 0;
779 lastpatchlastrow = AliL3Transform::GetLastRowOnDDL(fLastPatch)+1;
780 UChar_t nextpatchfirstrow;
782 nextpatchfirstrow = 0;
784 nextpatchfirstrow = AliL3Transform::GetFirstRowOnDDL(nextpatch)-1;
786 UChar_t *trackfirstrow = ((AliL3HoughTransformerRow *)fHoughTransformer[0])->GetTrackFirstRow();
787 UChar_t *tracklastrow = ((AliL3HoughTransformerRow *)fHoughTransformer[0])->GetTrackLastRow();
789 for(Int_t i=0; i<fNEtaSegments; i++)
791 UChar_t *gapcount = ((AliL3HoughTransformerRow *)fHoughTransformer[0])->GetGapCount(i);
792 UChar_t *currentrowcount = ((AliL3HoughTransformerRow *)fHoughTransformer[0])->GetCurrentRowCount(i);
793 UChar_t *prevbin = ((AliL3HoughTransformerRow *)fHoughTransformer[0])->GetPrevBin(i);
794 UChar_t *nextbin = ((AliL3HoughTransformerRow *)fHoughTransformer[0])->GetNextBin(i);
795 UChar_t *nextrow = ((AliL3HoughTransformerRow *)fHoughTransformer[0])->GetNextRow(i);
797 AliL3Histogram *hist = fHoughTransformer[0]->GetHistogram(i);
798 Int_t xmin = hist->GetFirstXbin();
799 Int_t xmax = hist->GetLastXbin();
800 Int_t ymin = hist->GetFirstYbin();
801 Int_t ymax = hist->GetLastYbin();
802 Int_t nxbins = hist->GetNbinsX()+2;
804 UChar_t lastyvalue = 0;
805 Int_t endybin = ymin - 1;
806 for(Int_t ybin=ymin; ybin<=ymax; ybin++)
808 UChar_t lastxvalue = 0;
809 UChar_t maxvalue = 0;
810 Int_t endxbin = xmin - 1;
811 for(Int_t xbin=xmin; xbin<=xmax; xbin++)
813 Int_t bin = xbin + ybin*nxbins;
815 if(gapcount[bin] < MAX_N_GAPS) {
818 if(tracklastrow[bin] > lastpatchlastrow) {
819 if(lastpatchlastrow > currentrowcount[bin])
820 gapcount[bin] += (lastpatchlastrow-currentrowcount[bin]-1);
823 if(tracklastrow[bin] > currentrowcount[bin])
824 gapcount[bin] += (tracklastrow[bin]-currentrowcount[bin]-1);
826 if(trackfirstrow[bin] < nextpatchfirstrow)
827 currentrowcount[bin] = nextpatchfirstrow;
829 currentrowcount[bin] = trackfirstrow[bin];
831 if(fLastPatch != -1) {
834 nextbin[xbin + ybin*nxbins] = (UChar_t)xbin;
835 prevbin[xbin + ybin*nxbins] = (UChar_t)xbin;
836 if(value > lastxvalue)
838 UChar_t *tempnextbin = nextbin + endxbin + 1 + ybin*nxbins;
839 memset(tempnextbin,(UChar_t)xbin,xbin-endxbin-1);
845 prevbin[xbin + ybin*nxbins] = (UChar_t)endxbin;
850 if(fLastPatch != -1) {
851 UChar_t *tempnextbin = nextbin + endxbin + 1 + ybin*nxbins;
852 memset(tempnextbin,(UChar_t)(xmax+1),xmax-endxbin);
855 nextrow[ybin] = (UChar_t)ybin;
856 if(maxvalue > lastyvalue)
858 UChar_t *tempnextrow = nextrow + endybin + 1;
859 memset(tempnextrow,(UChar_t)ybin,ybin-endybin-1);
863 lastyvalue = maxvalue;
866 if(fLastPatch != -1) {
867 UChar_t *tempnextrow = nextrow + endybin + 1;
868 memset(tempnextrow,(UChar_t)(ymax+1),ymax-endybin);
872 fBenchmark->Stop(buf);
875 void AliL3Hough::AddTracks()
877 // Add current slice slice tracks to the global list of found tracks
880 cerr<<"AliL3Hough::AddTracks : No tracks"<<endl;
883 AliL3TrackArray *tracks = fTracks[0];
884 for(Int_t i=0; i<tracks->GetNTracks(); i++)
886 AliL3Track *track = tracks->GetCheckedTrack(i);
888 if(track->GetNHits()!=1) cerr<<"NHITS "<<track->GetNHits()<<endl;
889 UInt_t *ids = track->GetHitNumbers();
890 ids[0] = (fCurrentSlice&0x7f)<<25;
893 fGlobalTracks->AddTracks(fTracks[0],0,fCurrentSlice);
896 void AliL3Hough::FindTrackCandidatesRow()
898 // Find AliL3HoughTransformerRow track candidates
900 LOG(AliL3Log::kError,"AliL3Hough::FindTrackCandidatesRow()","")
901 <<"Incompatible Peak Finder version!"<<ENDLOG;
905 //Look for peaks in histograms, and find the track candidates
908 npatches = 1; //Histograms have been added.
910 npatches = fNPatches;
912 Double_t initTime,cpuTime;
913 initTime = GetCpuTime();
914 fBenchmark->Start("Find Maxima");
915 for(Int_t i=0; i<npatches; i++)
917 AliL3HoughBaseTransformer *tr = fHoughTransformer[i];
919 fPeakFinder->Reset();
921 for(Int_t j=0; j<fNEtaSegments; j++)
923 AliL3Histogram *hist = tr->GetHistogram(j);
924 if(hist->GetNEntries()==0) continue;
925 fPeakFinder->SetHistogram(hist);
926 fPeakFinder->SetEtaSlice(j);
927 fPeakFinder->SetTrackLUTs(((AliL3HoughTransformerRow *)tr)->fTrackNRows,((AliL3HoughTransformerRow *)tr)->fTrackFirstRow,((AliL3HoughTransformerRow *)tr)->fTrackLastRow);
929 LOG(AliL3Log::kInformational,"AliL3Hough::FindTrackCandidates()","")
930 <<"Starting "<<j<<" etaslice"<<ENDLOG;
932 fPeakFinder->SetThreshold(fPeakThreshold[i]);
933 fPeakFinder->FindAdaptedRowPeaks(1,0,0);//Maxima finder for HoughTransformerRow
935 //fPeakFinder->FindMaxima(fPeakThreshold[i]); //Simple maxima finder
938 for(Int_t k=0; k<fPeakFinder->GetEntries(); k++)
940 if(fPeakFinder->GetWeight(k) < 0) continue;
941 AliL3HoughTrack *track = (AliL3HoughTrack*)fTracks[i]->NextTrack();
942 Float_t psi = atan((fPeakFinder->GetXPeak(k)-fPeakFinder->GetYPeak(k))/(AliL3HoughTransformerRow::GetBeta1()-AliL3HoughTransformerRow::GetBeta2()));
943 Float_t kappa = 2.0*(fPeakFinder->GetXPeak(k)*cos(psi)-AliL3HoughTransformerRow::GetBeta1()*sin(psi));
944 // track->SetTrackParameters(fPeakFinder->GetXPeak(k),fPeakFinder->GetYPeak(k),fPeakFinder->GetWeight(k));
945 track->SetTrackParameters(kappa,psi,fPeakFinder->GetWeight(k));
946 track->SetBinXY(fPeakFinder->GetXPeak(k),fPeakFinder->GetYPeak(k),fPeakFinder->GetXPeakSize(k),fPeakFinder->GetYPeakSize(k));
947 Int_t etaindex = (fPeakFinder->GetStartEta(k)+fPeakFinder->GetEndEta(k))/2;
948 track->SetEtaIndex(etaindex);
949 Float_t starteta = tr->GetEta(fPeakFinder->GetStartEta(k),fCurrentSlice);
950 Float_t endeta = tr->GetEta(fPeakFinder->GetEndEta(k),fCurrentSlice);
951 track->SetEta((starteta+endeta)/2.0);
952 track->SetRowRange(AliL3Transform::GetFirstRow(0),AliL3Transform::GetLastRow(5));
953 track->SetSector(fCurrentSlice);
954 track->SetSlice(fCurrentSlice);
956 Int_t label = tr->GetTrackID(etaindex,fPeakFinder->GetXPeak(k),fPeakFinder->GetYPeak(k));
957 track->SetMCid(label);
958 // cout<<"Track found with label "<<label<<" at "<<fPeakFinder->GetXPeak(k)<<" "<<fPeakFinder->GetYPeak(k)<<" with weight "<<fPeakFinder->GetWeight(k)<<endl;
961 LOG(AliL3Log::kInformational,"AliL3Hough::FindTrackCandidates()","")
962 <<"Found "<<fTracks[i]->GetNTracks()<<" tracks in slice "<<fCurrentSlice<<ENDLOG;
965 fBenchmark->Stop("Find Maxima");
966 cpuTime = GetCpuTime() - initTime;
967 LOG(AliL3Log::kInformational,"AliL3Hough::FindTrackCandidates()","Timing")
968 <<"Maxima finding done in "<<cpuTime*1000<<" ms"<<ENDLOG;
971 void AliL3Hough::FindTrackCandidates()
973 // Find AliL3HoughTransformer track candidates
975 LOG(AliL3Log::kError,"AliL3Hough::FindTrackCandidatesRow()","")
976 <<"Incompatible Peak Finder version!"<<ENDLOG;
982 npatches = 1; //Histograms have been added.
984 npatches = fNPatches;
986 Double_t initTime,cpuTime;
987 initTime = GetCpuTime();
988 fBenchmark->Start("Find Maxima");
989 for(Int_t i=0; i<npatches; i++)
991 AliL3HoughBaseTransformer *tr = fHoughTransformer[i];
994 for(Int_t j=0; j<fNEtaSegments; j++)
996 AliL3Histogram *hist = tr->GetHistogram(j);
997 if(hist->GetNEntries()==0) continue;
998 fPeakFinder->Reset();
999 fPeakFinder->SetHistogram(hist);
1001 cout<<"Starting "<<j<<" etaslice"<<endl;
1003 fPeakFinder->SetThreshold(fPeakThreshold[i]);
1004 fPeakFinder->FindAdaptedPeaks(fKappaSpread,fPeakRatio);
1006 for(Int_t k=0; k<fPeakFinder->GetEntries(); k++)
1008 AliL3HoughTrack *track = (AliL3HoughTrack*)fTracks[i]->NextTrack();
1009 track->SetTrackParameters(fPeakFinder->GetXPeak(k),fPeakFinder->GetYPeak(k),fPeakFinder->GetWeight(k));
1010 track->SetEtaIndex(j);
1011 track->SetEta(tr->GetEta(j,fCurrentSlice));
1012 track->SetRowRange(AliL3Transform::GetFirstRow(0),AliL3Transform::GetLastRow(5));
1015 cout<<"Found "<<fTracks[i]->GetNTracks()<<" tracks in patch "<<i<<endl;
1016 fTracks[i]->QSort();
1018 fBenchmark->Stop("Find Maxima");
1019 cpuTime = GetCpuTime() - initTime;
1020 LOG(AliL3Log::kInformational,"AliL3Hough::FindTrackCandidates()","Timing")
1021 <<"Maxima finding done in "<<cpuTime*1000<<" ms"<<ENDLOG;
1024 void AliL3Hough::InitEvaluate()
1026 //Pass the transformer objects to the AliL3HoughEval objects:
1027 //This will provide the evaluation objects with all the necessary
1028 //data and parameters it needs.
1030 for(Int_t i=0; i<fNPatches; i++)
1031 fEval[i]->InitTransformer(fHoughTransformer[i]);
1034 Int_t AliL3Hough::Evaluate(Int_t roadwidth,Int_t nrowstomiss)
1036 //Evaluate the tracks, by looking along the road in the raw data.
1037 //If track does not cross all padrows - rows2miss, it is removed from the arrray.
1038 //If histograms were not added, the check is done locally in patch,
1039 //meaning that nrowstomiss is the number of padrows the road can miss with respect
1040 //to the number of rows in the patch.
1041 //If the histograms were added, the comparison is done globally in the _slice_,
1042 //meaing that nrowstomiss is the number of padrows the road can miss with
1043 //respect to the total number of padrows in the slice.
1045 //Return value = number of tracks which were removed (only in case of fAddHistograms)
1049 LOG(AliL3Log::kError,"AliL3Hough::Evaluate","Track Array")
1050 <<"No tracks to work with..."<<ENDLOG;
1054 Int_t removedtracks=0;
1055 AliL3TrackArray *tracks=0;
1059 tracks = fTracks[0];
1060 for(Int_t i=0; i<tracks->GetNTracks(); i++)
1062 AliL3Track *track = tracks->GetCheckedTrack(i);
1063 if(!track) continue;
1068 for(Int_t i=0; i<fNPatches; i++)
1069 EvaluatePatch(i,roadwidth,nrowstomiss);
1071 //Here we check the tracks globally;
1072 //how many good rows (padrows with signal)
1073 //did it cross in the slice
1076 for(Int_t j=0; j<tracks->GetNTracks(); j++)
1078 AliL3HoughTrack *track = (AliL3HoughTrack*)tracks->GetCheckedTrack(j);
1080 if(track->GetNHits() < AliL3Transform::GetNRows() - nrowstomiss)
1090 return removedtracks;
1093 void AliL3Hough::EvaluatePatch(Int_t i,Int_t roadwidth,Int_t nrowstomiss)
1097 fEval[i]->InitTransformer(fHoughTransformer[i]);
1098 fEval[i]->SetNumOfPadsToLook(roadwidth);
1099 fEval[i]->SetNumOfRowsToMiss(nrowstomiss);
1100 //fEval[i]->RemoveFoundTracks();
1102 AliL3TrackArray *tracks=0;
1105 tracks = fTracks[i];
1107 tracks = fTracks[0];
1110 for(Int_t j=0; j<tracks->GetNTracks(); j++)
1112 AliL3HoughTrack *track = (AliL3HoughTrack*)tracks->GetCheckedTrack(j);
1115 LOG(AliL3Log::kWarning,"AliL3Hough::EvaluatePatch","Track array")
1116 <<"Track object missing!"<<ENDLOG;
1120 Int_t rowrange[2] = {AliL3Transform::GetFirstRow(i),AliL3Transform::GetLastRow(i)};
1121 Bool_t result = fEval[i]->LookInsideRoad(track,nrows,rowrange);
1124 Int_t pre=track->GetNHits();
1125 track->SetNHits(pre+nrows);
1127 else//the track crossed too few good padrows (padrows with signal) in the patch, so remove it
1129 if(result == kFALSE)
1138 void AliL3Hough::MergeEtaSlices()
1140 //Merge tracks found in neighbouring eta slices.
1141 //Removes the track with the lower weight.
1143 fBenchmark->Start("Merge Eta-slices");
1144 AliL3TrackArray *tracks = fTracks[0];
1147 cerr<<"AliL3Hough::MergeEtaSlices : No tracks "<<endl;
1150 for(Int_t j=0; j<tracks->GetNTracks(); j++)
1152 AliL3HoughTrack *track1 = (AliL3HoughTrack*)tracks->GetCheckedTrack(j);
1153 if(!track1) continue;
1154 for(Int_t k=j+1; k<tracks->GetNTracks(); k++)
1156 AliL3HoughTrack *track2 = (AliL3HoughTrack*)tracks->GetCheckedTrack(k);
1157 if(!track2) continue;
1158 if(abs(track1->GetEtaIndex() - track2->GetEtaIndex()) != 1) continue;
1159 if(fabs(track1->GetKappa()-track2->GetKappa()) < 0.006 &&
1160 fabs(track1->GetPsi()- track2->GetPsi()) < 0.1)
1162 //cout<<"Merging track in slices "<<track1->GetEtaIndex()<<" "<<track2->GetEtaIndex()<<endl;
1163 if(track1->GetWeight() > track2->GetWeight())
1170 fBenchmark->Stop("Merge Eta-slices");
1174 void AliL3Hough::WriteTracks(Char_t *path)
1176 // Write found tracks into file
1177 //cout<<"AliL3Hough::WriteTracks : Sorting the tracsk"<<endl;
1178 //fGlobalTracks->QSort();
1180 Char_t filename[1024];
1181 sprintf(filename,"%s/tracks_%d.raw",path,fEvent);
1182 AliL3MemHandler mem;
1183 mem.SetBinaryOutput(filename);
1184 mem.TrackArray2Binary(fGlobalTracks);
1185 mem.CloseBinaryOutput();
1186 fGlobalTracks->Reset();
1189 void AliL3Hough::WriteTracks(Int_t slice,Char_t *path)
1191 // Write found tracks slice by slice into file
1193 AliL3MemHandler mem;
1197 sprintf(fname,"%s/tracks_ho_%d_%d.raw",path,fEvent,slice);
1198 mem.SetBinaryOutput(fname);
1199 mem.TrackArray2Binary(fTracks[0]);
1200 mem.CloseBinaryOutput();
1204 for(Int_t i=0; i<fNPatches; i++)
1206 sprintf(fname,"%s/tracks_ho_%d_%d_%d.raw",path,fEvent,slice,i);
1207 mem.SetBinaryOutput(fname);
1208 mem.TrackArray2Binary(fTracks[i]);
1209 mem.CloseBinaryOutput();
1214 void AliL3Hough::WriteDigits(Char_t *outfile)
1216 //Write the current data to a new rootfile.
1219 for(Int_t i=0; i<fNPatches; i++)
1221 AliL3DigitRowData *tempPt = (AliL3DigitRowData*)fHoughTransformer[i]->GetDataPointer();
1222 fMemHandler[i]->AliDigits2RootFile(tempPt,outfile);
1225 cerr<<"AliL3Hough::WriteDigits : You need to compile with AliROOT!"<<endl;
1230 Double_t AliL3Hough::GetCpuTime()
1232 //Return the Cputime in seconds.
1234 gettimeofday( &tv, NULL );
1235 return tv.tv_sec+(((Double_t)tv.tv_usec)/1000000.);
1238 void *AliL3Hough::ProcessInThread(void *args)
1240 AliL3Hough *instance = (AliL3Hough *)args;
1241 Int_t minslice = instance->GetMinSlice();
1242 Int_t maxslice = instance->GetMaxSlice();
1243 for(Int_t i=minslice; i<=maxslice; i++)
1245 instance->ReadData(i,0);
1246 instance->Transform();
1247 instance->AddAllHistogramsRows();
1248 instance->FindTrackCandidatesRow();
1249 instance->AddTracks();
1254 void AliL3Hough::StartProcessInThread(Int_t minslice,Int_t maxslice)
1258 sprintf(buf,"houghtrans_%d_%d",minslice,maxslice);
1259 SetMinMaxSlices(minslice,maxslice);
1260 // fThread = new TThread(buf,(void (*) (void *))&ProcessInThread,(void *)this);
1261 fThread = new TThread(buf,&ProcessInThread,(void *)this);
1267 Int_t AliL3Hough::WaitForThreadFinish()
1269 return TThread::Join(fThread->GetId());