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"
34 #include "AliL3HoughKalmanTrack.h"
44 //_____________________________________________________________
47 // Interface class for the Hough transform
49 // Example how to use:
51 // AliL3Hough *hough = new AliL3Hough(path,kTRUE,NumberOfEtaSegments);
52 // hough->ReadData(slice);
53 // hough->Transform();
54 // hough->FindTrackCandidates();
56 // AliL3TrackArray *tracks = hough->GetTracks(patch);
63 AliL3Hough::AliL3Hough()
68 fAddHistograms = kFALSE;
69 fDoIterative = kFALSE;
70 fWriteDigits = kFALSE;
74 fHoughTransformer = 0;
97 SetTransformerParams();
102 //just be sure that index is empty for new event
103 AliL3FileHandler::CleanStaticIndex();
111 AliL3Hough::AliL3Hough(Char_t *path,Bool_t binary,Int_t netasegments,Bool_t bit8,Int_t tv,Char_t *infile,Char_t *ptr)
116 fNEtaSegments = netasegments;
117 fAddHistograms = kFALSE;
118 fDoIterative = kFALSE;
119 fWriteDigits = kFALSE;
139 //just be sure that index is empty for new event
140 AliL3FileHandler::CleanStaticIndex();
148 AliL3Hough::~AliL3Hough()
155 //cout << "Cleaned class merger " << endl;
158 //cout << "Cleaned class inter " << endl;
161 //cout << "Cleaned class peak " << endl;
163 delete fGlobalMerger;
164 //cout << "Cleaned class global " << endl;
167 //cout << "Cleaned class bench " << endl;
169 delete fGlobalTracks;
170 //cout << "Cleaned class globaltracks " << endl;
172 // fThread->Delete();
178 void AliL3Hough::CleanUp()
182 for(Int_t i=0; i<fNPatches; i++)
184 if(fTracks[i]) delete fTracks[i];
185 //cout << "Cleaned tracks " << i << endl;
186 if(fEval[i]) delete fEval[i];
187 //cout << "Cleaned eval " << i << endl;
188 if(fHoughTransformer[i]) delete fHoughTransformer[i];
189 //cout << "Cleaned traf " << i << endl;
190 if(fMemHandler[i]) delete fMemHandler[i];
191 //cout << "Cleaned mem " << i << endl;
194 if(fTracks) delete [] fTracks;
195 //cout << "Cleaned class tracks " << endl;
196 if(fEval) delete [] fEval;
197 //cout << "Cleaned class eval " << endl;
198 if(fHoughTransformer) delete [] fHoughTransformer;
199 //cout << "Cleaned cleass trafo " << endl;
200 if(fMemHandler) delete [] fMemHandler;
201 //cout << "Cleaned class mem " << endl;
204 void AliL3Hough::Init(Int_t netasegments,Int_t tv,AliRawEvent *rawevent,Float_t zvertex)
207 fNEtaSegments = netasegments;
209 fRawEvent = rawevent;
215 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)
217 //Normal init of the AliL3Hough
220 fNEtaSegments = netasegments;
221 fWriteDigits = kFALSE;
240 Init(); //do the rest
243 void AliL3Hough::Init(Bool_t doit, Bool_t addhists)
247 fAddHistograms = addhists;
249 fNPatches = AliL3Transform::GetNPatches();
250 fHoughTransformer = new AliL3HoughBaseTransformer*[fNPatches];
251 fMemHandler = new AliL3MemHandler*[fNPatches];
253 fTracks = new AliL3TrackArray*[fNPatches];
254 fEval = new AliL3HoughEval*[fNPatches];
256 fGlobalTracks = new AliL3TrackArray("AliL3HoughTrack");
258 AliL3HoughBaseTransformer *lasttransformer = 0;
260 for(Int_t i=0; i<fNPatches; i++)
262 switch (fVersion){ //choose Transformer
264 fHoughTransformer[i] = new AliL3HoughTransformerLUT(0,i,fNEtaSegments);
267 fHoughTransformer[i] = new AliL3HoughClusterTransformer(0,i,fNEtaSegments);
270 fHoughTransformer[i] = new AliL3HoughTransformerVhdl(0,i,fNEtaSegments,fNSaveIterations);
273 fHoughTransformer[i] = new AliL3HoughTransformerRow(0,i,fNEtaSegments,kFALSE,fZVertex);
276 fHoughTransformer[i] = new AliL3HoughTransformer(0,i,fNEtaSegments,kFALSE,kFALSE);
279 fHoughTransformer[i]->SetLastTransformer(lasttransformer);
280 lasttransformer = fHoughTransformer[i];
281 // fHoughTransformer[i]->CreateHistograms(fNBinX[i],fLowPt[i],fNBinY[i],-fPhi[i],fPhi[i]);
282 fHoughTransformer[i]->CreateHistograms(fNBinX[i],-fLowPt[i],fLowPt[i],fNBinY[i],-fPhi[i],fPhi[i]);
283 //fHoughTransformer[i]->CreateHistograms(fLowPt[i],fUpperPt[i],fPtRes[i],fNBinY[i],fPhi[i]);
285 fHoughTransformer[i]->SetLowerThreshold(fThreshold[i]);
286 fHoughTransformer[i]->SetUpperThreshold(100);
288 LOG(AliL3Log::kInformational,"AliL3Hough::Init","Version")
289 <<"Initializing Hough transformer version "<<fVersion<<ENDLOG;
291 fEval[i] = new AliL3HoughEval();
292 fTracks[i] = new AliL3TrackArray("AliL3HoughTrack");
294 fMemHandler[i] = new AliL3DataHandler();
301 /* In case of reading digits file */
302 fMemHandler[i] = new AliL3FileHandler(kTRUE); //use static index
307 Char_t filename[1024];
308 sprintf(filename,"%s/digitfile.root",fPath);
309 fMemHandler[i]->SetAliInput(filename);
313 fMemHandler[i]->SetAliInput(fRunLoader);
319 /* In case of reading from DATE */
320 fMemHandler[i] = new AliL3DDLDataFileHandler();
321 fMemHandler[i]->SetReaderInput(fInputPtr,-1);
325 /* In case of reading rawdata from ROOT file */
326 fMemHandler[i] = new AliL3DDLDataFileHandler();
327 fMemHandler[i]->SetReaderInput(fInputFile);
331 /* In case of reading rawdata using AliRawEvent */
332 fMemHandler[i] = new AliL3DDLDataFileHandler();
333 fMemHandler[i]->SetReaderInput(fRawEvent);
337 fMemHandler[i] = new AliL3MemHandler();
341 fPeakFinder = new AliL3HoughMaxFinder("KappaPhi",50000);
343 fMerger = new AliL3HoughMerger(fNPatches);
344 fInterMerger = new AliL3HoughIntMerger();
351 fBenchmark = new AliL3Benchmark();
354 void AliL3Hough::SetTransformerParams(Float_t ptres,Float_t ptmin,Float_t ptmax,Int_t ny,Int_t patch)
356 // Setup the parameters for the Hough Transformer
357 // This includes the bin size and limits for
358 // the parameter space histograms
365 mrow = AliL3Transform::GetLastRow(patch);
368 Double_t lineradius = sqrt(pow(AliL3Transform::Row2X(mrow),2) + pow(AliL3Transform::GetMaxY(mrow),2));
369 Double_t kappa = -1*AliL3Transform::GetBField()*AliL3Transform::GetBFact()/ptmin;
370 psi = AliL3Transform::Deg2Rad(10) - asin(lineradius*kappa/2);
371 cout<<"Calculated psi range "<<psi<<" in patch "<<patch<<endl;
390 fPtRes[patch] = ptres;
391 fLowPt[patch] = ptmin;
392 fUpperPt[patch] = ptmax;
397 void AliL3Hough::SetTransformerParams(Int_t nx,Int_t ny,Float_t ptmin,Int_t patch)
399 // Setup the parameters for the Hough Transformer
402 Double_t lineradius = sqrt(pow(AliL3Transform::Row2X(mrow),2) + pow(AliL3Transform::GetMaxY(mrow),2));
403 Double_t kappa = -1*AliL3Transform::GetBField()*AliL3Transform::GetBFact()/ptmin;
404 Double_t psi = AliL3Transform::Deg2Rad(10) - asin(lineradius*kappa/2);
405 cout<<"Calculated psi range "<<psi<<" in patch "<<patch<<endl;
418 void AliL3Hough::SetTransformerParams(Int_t nx,Int_t ny,Float_t ptmin,Int_t /*patch*/)
420 // Setup the parameters for the Hough Transformer
422 Double_t lineradius = 1.0/(AliL3HoughTransformerRow::GetBeta1()*sqrt(1.0+tan(AliL3Transform::Pi()*10/180)*tan(AliL3Transform::Pi()*10/180)));
423 Double_t alpha1 = AliL3HoughTransformerRow::GetBeta1()*tan(AliL3Transform::Pi()*10/180);
424 Double_t kappa = 1*AliL3Transform::GetBField()*AliL3Transform::GetBFact()/(ptmin*0.9);
425 Double_t psi = AliL3Transform::Deg2Rad(10) - asin(lineradius*kappa/2);
426 // cout<<"Calculated psi range "<<psi<<" in patch "<<patch<<endl;
427 Double_t alpha2 = alpha1 - (AliL3HoughTransformerRow::GetBeta1()-AliL3HoughTransformerRow::GetBeta2())*tan(psi);
428 // cout<<"Calculated alphas range "<<alpha1<<" "<<alpha2<<" in patch "<<patch<<endl;
433 fLowPt[i] = 1.1*alpha1;
441 void AliL3Hough::CalcTransformerParams(Float_t ptmin)
443 // Setup the parameters for the Row Hough Transformer
444 // Automatically adjusts the number of bins in X and Y in a way
445 // that the size of the hough bin is 2x (in X) and 2.5 (in Y) the
446 // size of the tpc pads
448 Double_t lineradius = 1.0/(AliL3HoughTransformerRow::GetBeta1()*sqrt(1.0+tan(AliL3Transform::Pi()*10/180)*tan(AliL3Transform::Pi()*10/180)));
449 Double_t alpha1 = AliL3HoughTransformerRow::GetBeta1()*tan(AliL3Transform::Pi()*10/180);
450 Double_t kappa = 1*AliL3Transform::GetBField()*AliL3Transform::GetBFact()/(ptmin*0.9);
451 Double_t psi = AliL3Transform::Deg2Rad(10) - asin(lineradius*kappa/2);
452 // cout<<"Calculated psi range "<<psi<<endl;
453 Double_t alpha2 = alpha1 - (AliL3HoughTransformerRow::GetBeta1()-AliL3HoughTransformerRow::GetBeta2())*tan(psi);
455 // cout<<"Calculated alphas range "<<alpha1<<" "<<alpha2<<endl;
457 Double_t sizex = 2.0*AliL3Transform::GetPadPitchWidthLow()*AliL3HoughTransformerRow::GetBeta1()*AliL3HoughTransformerRow::GetBeta1();
458 Double_t sizey = 2.5*AliL3Transform::GetPadPitchWidthUp()*AliL3HoughTransformerRow::GetBeta2()*AliL3HoughTransformerRow::GetBeta2();
460 Int_t nx = 2*(Int_t)(alpha1/sizex)+1;
461 Int_t ny = 2*(Int_t)(alpha2/sizey)+1;
462 // cout<<"Calculated number of bins "<<nx<<" "<<ny<<endl;
475 void AliL3Hough::SetTransformerParams(Int_t nx,Int_t ny,Float_t lpt,Float_t phi)
488 void AliL3Hough::SetThreshold(Int_t t3,Int_t patch)
490 // Set digits threshold
498 fThreshold[patch]=t3;
501 void AliL3Hough::SetPeakThreshold(Int_t threshold,Int_t patch)
503 // Set Peak Finder threshold
508 fPeakThreshold[i++]=threshold;
511 fPeakThreshold[patch]=threshold;
514 void AliL3Hough::DoBench(Char_t *name)
516 fBenchmark->Analyze(name);
519 void AliL3Hough::Process(Int_t minslice,Int_t maxslice)
521 //Process all slices [minslice,maxslice].
522 fGlobalMerger = new AliL3HoughGlobalMerger(minslice,maxslice);
524 for(Int_t i=minslice; i<=maxslice; i++)
532 AddAllHistogramsRows();
534 FindTrackCandidates();
536 //fGlobalMerger->FillTracks(fTracks[0],i);
540 void AliL3Hough::ReadData(Int_t slice,Int_t eventnr)
542 //Read data from files, binary or root.
545 if(fEvent!=eventnr) //just be sure that index is empty for new event
546 AliL3FileHandler::CleanStaticIndex();
548 fCurrentSlice = slice;
550 for(Int_t i=0; i<fNPatches; i++)
552 fMemHandler[i]->Free();
554 AliL3DigitRowData *digits =0;
556 fMemHandler[i]->Init(slice,i);
557 if(fBinary)//take input data from binary files
560 sprintf(name,"%s/binaries/digits_c8_%d_%d_%d.raw",fPath,eventnr,slice,i);
562 sprintf(name,"%s/binaries/digits_%d_%d_%d.raw",fPath,eventnr,slice,i);
564 fMemHandler[i]->SetBinaryInput(name);
565 digits = (AliL3DigitRowData *)fMemHandler[i]->CompBinary2Memory(ndigits);
566 fMemHandler[i]->CloseBinaryInput();
568 else //read data from root file
572 fMemHandler[i]->FreeDigitsTree();//or else the new event is not loaded
573 digits=(AliL3DigitRowData *)fMemHandler[i]->AliAltroDigits2Memory(ndigits,eventnr);
575 cerr<<"You cannot read from rootfile now"<<endl;
579 //Set the pointer to the TPCRawStream in case of fast raw data reading
580 fHoughTransformer[i]->SetTPCRawStream(fMemHandler[i]->GetTPCRawStream());
582 //set input data and init transformer
583 fHoughTransformer[i]->SetInputData(ndigits,digits);
584 fHoughTransformer[i]->Init(slice,i,fNEtaSegments);
590 void AliL3Hough::Transform(Int_t *rowrange)
592 //Transform all data given to the transformer within the given slice
593 //(after ReadData(slice))
595 Double_t initTime,cpuTime;
596 initTime = GetCpuTime();
597 Int_t patchorder[6] = {5,2,0,1,3,4}; //The order in which patches are processed
598 // Int_t patchorder[6] = {0,1,2,3,4,5}; //The order in which patches are processed
599 // Int_t patchorder[6] = {5,4,3,2,1,0}; //The order in which patches are processed
600 // Int_t patchorder[6] = {5,2,4,3,1,0}; //The order in which patches are processed
602 for(Int_t i=0; i<fNPatches; i++)
604 // In case of Row transformer reset the arrays only once
605 if((fVersion != 4) || (i == 0)) {
606 fBenchmark->Start("Hough Reset");
607 fHoughTransformer[0]->Reset();//Reset the histograms
608 fBenchmark->Stop("Hough Reset");
610 fBenchmark->Start("Hough Transform");
611 PrepareForNextPatch(patchorder[i]);
614 sprintf(buf,"Patch %d",patchorder[i]);
615 fBenchmark->Start(buf);
616 fHoughTransformer[patchorder[i]]->SetLastPatch(fLastPatch);
617 fHoughTransformer[patchorder[i]]->TransformCircle();
618 fBenchmark->Stop(buf);
621 fHoughTransformer[i]->TransformCircleC(rowrange,1);
622 fBenchmark->Stop("Hough Transform");
623 fLastPatch=patchorder[i];
625 cpuTime = GetCpuTime() - initTime;
626 LOG(AliL3Log::kInformational,"AliL3Hough::Transform()","Timing")
627 <<"Transform done in average per patch of "<<cpuTime*1000/fNPatches<<" ms"<<ENDLOG;
630 void AliL3Hough::MergePatches()
632 // Merge patches if they are not summed
633 if(fAddHistograms) //Nothing to merge here
635 fMerger->MergePatches(kTRUE);
638 void AliL3Hough::MergeInternally()
640 // Merge patches internally
642 fInterMerger->FillTracks(fTracks[0]);
644 fInterMerger->FillTracks(fMerger->GetOutTracks());
646 fInterMerger->MMerge();
649 void AliL3Hough::ProcessSliceIter()
651 //Process current slice (after ReadData(slice)) iteratively.
655 for(Int_t i=0; i<fNPatches; i++)
658 fMerger->FillTracks(fTracks[i],i); //Copy tracks to merger
663 for(Int_t i=0; i<10; i++)
668 AliL3HoughBaseTransformer *tr = fHoughTransformer[0];
669 for(Int_t j=0; j<fNEtaSegments; j++)
671 AliL3Histogram *hist = tr->GetHistogram(j);
672 if(hist->GetNEntries()==0) continue;
673 fPeakFinder->Reset();
674 fPeakFinder->SetHistogram(hist);
675 fPeakFinder->FindAbsMaxima();
676 AliL3HoughTrack *track = (AliL3HoughTrack*)fTracks[0]->NextTrack();
677 track->SetTrackParameters(fPeakFinder->GetXPeak(0),fPeakFinder->GetYPeak(0),fPeakFinder->GetWeight(0));
678 track->SetEtaIndex(j);
679 track->SetEta(tr->GetEta(j,fCurrentSlice));
680 for(Int_t k=0; k<fNPatches; k++)
682 fEval[i]->SetNumOfPadsToLook(2);
683 fEval[i]->SetNumOfRowsToMiss(2);
684 fEval[i]->RemoveFoundTracks();
687 if(!fEval[i]->LookInsideRoad(track,nrows))
689 fTracks[0]->Remove(fTracks[0]->GetNTracks()-1);
690 fTracks[0]->Compress();
701 void AliL3Hough::ProcessPatchIter(Int_t patch)
703 //Process patch in a iterative way.
704 //transform + peakfinding + evaluation + transform +...
706 Int_t numoftries = 5;
707 AliL3HoughBaseTransformer *tr = fHoughTransformer[patch];
708 AliL3TrackArray *tracks = fTracks[patch];
710 AliL3HoughEval *ev = fEval[patch];
711 ev->InitTransformer(tr);
712 //ev->RemoveFoundTracks();
713 ev->SetNumOfRowsToMiss(3);
714 ev->SetNumOfPadsToLook(2);
715 AliL3Histogram *hist;
716 for(Int_t t=0; t<numoftries; t++)
719 tr->TransformCircle();
720 for(Int_t i=0; i<fNEtaSegments; i++)
722 hist = tr->GetHistogram(i);
723 if(hist->GetNEntries()==0) continue;
724 fPeakFinder->Reset();
725 fPeakFinder->SetHistogram(hist);
726 fPeakFinder->FindAbsMaxima();
727 //fPeakFinder->FindPeak1();
728 AliL3HoughTrack *track = (AliL3HoughTrack*)tracks->NextTrack();
729 track->SetTrackParameters(fPeakFinder->GetXPeak(0),fPeakFinder->GetYPeak(0),fPeakFinder->GetWeight(0));
730 track->SetEtaIndex(i);
731 track->SetEta(tr->GetEta(i,fCurrentSlice));
734 if(!ev->LookInsideRoad(track,nrows))
736 tracks->Remove(tracks->GetNTracks()-1);
743 LOG(AliL3Log::kInformational,"AliL3Hough::ProcessPatch","NTracks")
744 <<AliL3Log::kDec<<"Found "<<tracks->GetNTracks()<<" tracks in patch "<<patch<<ENDLOG;
747 void AliL3Hough::AddAllHistograms()
749 //Add the histograms within one etaslice.
750 //Resulting histogram are in patch=0.
752 Double_t initTime,cpuTime;
753 initTime = GetCpuTime();
754 fBenchmark->Start("Add Histograms");
755 for(Int_t i=0; i<fNEtaSegments; i++)
757 AliL3Histogram *hist0 = fHoughTransformer[0]->GetHistogram(i);
758 for(Int_t j=1; j<fNPatches; j++)
760 AliL3Histogram *hist = fHoughTransformer[j]->GetHistogram(i);
764 fBenchmark->Stop("Add Histograms");
765 fAddHistograms = kTRUE;
766 cpuTime = GetCpuTime() - initTime;
767 LOG(AliL3Log::kInformational,"AliL3Hough::AddAllHistograms()","Timing")
768 <<"Adding histograms in "<<cpuTime*1000<<" ms"<<ENDLOG;
771 void AliL3Hough::AddAllHistogramsRows()
773 //Add the histograms within one etaslice.
774 //Resulting histogram are in patch=0.
776 Double_t initTime,cpuTime;
777 initTime = GetCpuTime();
778 fBenchmark->Start("Add HistogramsRows");
780 UChar_t lastpatchlastrow = AliL3Transform::GetLastRowOnDDL(fLastPatch)+1;
782 UChar_t *tracklastrow = ((AliL3HoughTransformerRow *)fHoughTransformer[0])->GetTrackLastRow();
784 for(Int_t i=0; i<fNEtaSegments; i++)
786 UChar_t *gapcount = ((AliL3HoughTransformerRow *)fHoughTransformer[0])->GetGapCount(i);
787 UChar_t *currentrowcount = ((AliL3HoughTransformerRow *)fHoughTransformer[0])->GetCurrentRowCount(i);
789 AliL3Histogram *hist = fHoughTransformer[0]->GetHistogram(i);
790 Int_t xmin = hist->GetFirstXbin();
791 Int_t xmax = hist->GetLastXbin();
792 Int_t ymin = hist->GetFirstYbin();
793 Int_t ymax = hist->GetLastYbin();
794 Int_t nxbins = hist->GetNbinsX()+2;
796 for(Int_t ybin=ymin; ybin<=ymax; ybin++)
798 for(Int_t xbin=xmin; xbin<=xmax; xbin++)
800 Int_t bin = xbin + ybin*nxbins; //Int_t bin = hist->GetBin(xbin,ybin);
801 if(gapcount[bin] < MAX_N_GAPS) {
802 if(tracklastrow[bin] > lastpatchlastrow) {
803 if(lastpatchlastrow > currentrowcount[bin])
804 gapcount[bin] += (lastpatchlastrow-currentrowcount[bin]-1);
807 if(tracklastrow[bin] > currentrowcount[bin])
808 gapcount[bin] += (tracklastrow[bin]-currentrowcount[bin]-1);
810 if(gapcount[bin] < MAX_N_GAPS)
811 hist->AddBinContent(bin,(159-gapcount[bin]));
817 fBenchmark->Stop("Add HistogramsRows");
818 fAddHistograms = kTRUE;
819 cpuTime = GetCpuTime() - initTime;
820 LOG(AliL3Log::kInformational,"AliL3Hough::AddAllHistogramsRows()","Timing")
821 <<"Adding histograms in "<<cpuTime*1000<<" ms"<<ENDLOG;
824 void AliL3Hough::PrepareForNextPatch(Int_t nextpatch)
826 // Prepare the parameter space for the processing of
827 // the next read patch. According to the already
828 // accumulated number of gaps in parameter space
829 // bins, the routine updates the dynamic
830 // pointers used in order to jump rapidly during the
831 // filling of the parameter space.
834 sprintf(buf,"Prepare For Patch %d",nextpatch);
835 fBenchmark->Start(buf);
837 UChar_t lastpatchlastrow;
839 lastpatchlastrow = 0;
841 lastpatchlastrow = AliL3Transform::GetLastRowOnDDL(fLastPatch)+1;
842 UChar_t nextpatchfirstrow;
844 nextpatchfirstrow = 0;
846 nextpatchfirstrow = AliL3Transform::GetFirstRowOnDDL(nextpatch)-1;
848 UChar_t *trackfirstrow = ((AliL3HoughTransformerRow *)fHoughTransformer[0])->GetTrackFirstRow();
849 UChar_t *tracklastrow = ((AliL3HoughTransformerRow *)fHoughTransformer[0])->GetTrackLastRow();
851 for(Int_t i=0; i<fNEtaSegments; i++)
853 UChar_t *gapcount = ((AliL3HoughTransformerRow *)fHoughTransformer[0])->GetGapCount(i);
854 UChar_t *currentrowcount = ((AliL3HoughTransformerRow *)fHoughTransformer[0])->GetCurrentRowCount(i);
855 UChar_t *prevbin = ((AliL3HoughTransformerRow *)fHoughTransformer[0])->GetPrevBin(i);
856 UChar_t *nextbin = ((AliL3HoughTransformerRow *)fHoughTransformer[0])->GetNextBin(i);
857 UChar_t *nextrow = ((AliL3HoughTransformerRow *)fHoughTransformer[0])->GetNextRow(i);
859 AliL3Histogram *hist = fHoughTransformer[0]->GetHistogram(i);
860 Int_t xmin = hist->GetFirstXbin();
861 Int_t xmax = hist->GetLastXbin();
862 Int_t ymin = hist->GetFirstYbin();
863 Int_t ymax = hist->GetLastYbin();
864 Int_t nxbins = hist->GetNbinsX()+2;
866 if(fLastPatch != -1) {
867 UChar_t lastyvalue = 0;
868 Int_t endybin = ymin - 1;
869 for(Int_t ybin=nextrow[ymin]; ybin<=ymax; ybin = nextrow[++ybin])
871 UChar_t lastxvalue = 0;
872 UChar_t maxvalue = 0;
873 Int_t endxbin = xmin - 1;
874 for(Int_t xbin=xmin; xbin<=xmax; xbin++)
876 Int_t bin = xbin + ybin*nxbins;
878 if(gapcount[bin] < MAX_N_GAPS) {
879 if(tracklastrow[bin] > lastpatchlastrow) {
880 if(lastpatchlastrow > currentrowcount[bin])
881 gapcount[bin] += (lastpatchlastrow-currentrowcount[bin]-1);
884 if(tracklastrow[bin] > currentrowcount[bin])
885 gapcount[bin] += (tracklastrow[bin]-currentrowcount[bin]-1);
887 if(gapcount[bin] < MAX_N_GAPS) {
890 if(trackfirstrow[bin] < nextpatchfirstrow)
891 currentrowcount[bin] = nextpatchfirstrow;
893 currentrowcount[bin] = trackfirstrow[bin];
898 nextbin[xbin + ybin*nxbins] = (UChar_t)xbin;
899 prevbin[xbin + ybin*nxbins] = (UChar_t)xbin;
900 if(value > lastxvalue)
902 UChar_t *tempnextbin = nextbin + endxbin + 1 + ybin*nxbins;
903 memset(tempnextbin,(UChar_t)xbin,xbin-endxbin-1);
909 prevbin[xbin + ybin*nxbins] = (UChar_t)endxbin;
913 UChar_t *tempnextbin = nextbin + endxbin + 1 + ybin*nxbins;
914 memset(tempnextbin,(UChar_t)(xmax+1),xmax-endxbin);
917 nextrow[ybin] = (UChar_t)ybin;
918 if(maxvalue > lastyvalue)
920 UChar_t *tempnextrow = nextrow + endybin + 1;
921 memset(tempnextrow,(UChar_t)ybin,ybin-endybin-1);
925 lastyvalue = maxvalue;
927 UChar_t *tempnextrow = nextrow + endybin + 1;
928 memset(tempnextrow,(UChar_t)(ymax+1),ymax-endybin+1);
931 UChar_t lastyvalue = 0;
932 Int_t endybin = ymin - 1;
933 for(Int_t ybin=ymin; ybin<=ymax; ybin++)
935 UChar_t maxvalue = 0;
936 for(Int_t xbin=xmin; xbin<=xmax; xbin++)
938 Int_t bin = xbin + ybin*nxbins;
939 if(gapcount[bin] < MAX_N_GAPS) {
941 if(trackfirstrow[bin] < nextpatchfirstrow)
942 currentrowcount[bin] = nextpatchfirstrow;
944 currentrowcount[bin] = trackfirstrow[bin];
949 nextrow[ybin] = (UChar_t)ybin;
950 if(maxvalue > lastyvalue)
952 UChar_t *tempnextrow = nextrow + endybin + 1;
953 memset(tempnextrow,(UChar_t)ybin,ybin-endybin-1);
957 lastyvalue = maxvalue;
959 UChar_t *tempnextrow = nextrow + endybin + 1;
960 memset(tempnextrow,(UChar_t)(ymax+1),ymax-endybin+1);
964 fBenchmark->Stop(buf);
967 void AliL3Hough::AddTracks()
969 // Add current slice slice tracks to the global list of found tracks
972 cerr<<"AliL3Hough::AddTracks : No tracks"<<endl;
975 AliL3TrackArray *tracks = fTracks[0];
976 for(Int_t i=0; i<tracks->GetNTracks(); i++)
978 AliL3Track *track = tracks->GetCheckedTrack(i);
980 if(track->GetNHits()!=1) cerr<<"NHITS "<<track->GetNHits()<<endl;
981 UInt_t *ids = track->GetHitNumbers();
982 ids[0] = (fCurrentSlice&0x7f)<<25;
985 fGlobalTracks->AddTracks(fTracks[0],0,fCurrentSlice);
988 void AliL3Hough::FindTrackCandidatesRow()
990 // Find AliL3HoughTransformerRow track candidates
992 LOG(AliL3Log::kError,"AliL3Hough::FindTrackCandidatesRow()","")
993 <<"Incompatible Peak Finder version!"<<ENDLOG;
997 //Look for peaks in histograms, and find the track candidates
1000 npatches = 1; //Histograms have been added.
1002 npatches = fNPatches;
1004 Double_t initTime,cpuTime;
1005 initTime = GetCpuTime();
1006 fBenchmark->Start("Find Maxima");
1007 for(Int_t i=0; i<npatches; i++)
1009 AliL3HoughBaseTransformer *tr = fHoughTransformer[i];
1010 AliL3Histogram *h = tr->GetHistogram(0);
1011 Float_t deltax = h->GetBinWidthX()*AliL3HoughTransformerRow::GetDAlpha();
1012 Float_t deltay = h->GetBinWidthY()*AliL3HoughTransformerRow::GetDAlpha();
1013 Float_t deltaeta = (tr->GetEtaMax()-tr->GetEtaMin())/tr->GetNEtaSegments()*AliL3HoughTransformerRow::GetDEta();
1014 Float_t zvertex = tr->GetZVertex();
1015 fTracks[i]->Reset();
1016 fPeakFinder->Reset();
1018 for(Int_t j=0; j<fNEtaSegments; j++)
1020 AliL3Histogram *hist = tr->GetHistogram(j);
1021 if(hist->GetNEntries()==0) continue;
1022 fPeakFinder->SetHistogram(hist);
1023 fPeakFinder->SetEtaSlice(j);
1024 fPeakFinder->SetTrackLUTs(((AliL3HoughTransformerRow *)tr)->GetTrackNRows(),((AliL3HoughTransformerRow *)tr)->GetTrackFirstRow(),((AliL3HoughTransformerRow *)tr)->GetTrackLastRow(),((AliL3HoughTransformerRow *)tr)->GetNextRow(j));
1026 LOG(AliL3Log::kInformational,"AliL3Hough::FindTrackCandidates()","")
1027 <<"Starting "<<j<<" etaslice"<<ENDLOG;
1029 fPeakFinder->SetThreshold(fPeakThreshold[i]);
1030 fPeakFinder->FindAdaptedRowPeaks(1,0,0);//Maxima finder for HoughTransformerRow
1033 for(Int_t k=0; k<fPeakFinder->GetEntries(); k++)
1035 // if(fPeakFinder->GetWeight(k) < 0) continue;
1036 AliL3HoughTrack *track = (AliL3HoughTrack*)fTracks[i]->NextTrack();
1037 Double_t starteta = tr->GetEta(fPeakFinder->GetStartEta(k),fCurrentSlice);
1038 Double_t endeta = tr->GetEta(fPeakFinder->GetEndEta(k),fCurrentSlice);
1039 Double_t eta = (starteta+endeta)/2.0;
1040 track->SetTrackParametersRow(fPeakFinder->GetXPeak(k),fPeakFinder->GetYPeak(k),eta,fPeakFinder->GetWeight(k));
1041 track->SetPterr(deltax); track->SetPsierr(deltay); track->SetTglerr(deltaeta);
1042 track->SetBinXY(fPeakFinder->GetXPeak(k),fPeakFinder->GetYPeak(k),fPeakFinder->GetXPeakSize(k),fPeakFinder->GetYPeakSize(k));
1043 track->SetZ0(zvertex);
1044 Int_t etaindex = (fPeakFinder->GetStartEta(k)+fPeakFinder->GetEndEta(k))/2;
1045 track->SetEtaIndex(etaindex);
1047 ((AliL3HoughTransformerRow *)tr)->GetTrackLength(fPeakFinder->GetXPeak(k),fPeakFinder->GetYPeak(k),rows);
1048 track->SetRowRange(rows[0],rows[1]);
1049 track->SetSector(fCurrentSlice);
1050 track->SetSlice(fCurrentSlice);
1052 Int_t label = tr->GetTrackID(etaindex,fPeakFinder->GetXPeak(k),fPeakFinder->GetYPeak(k));
1053 track->SetMCid(label);
1056 LOG(AliL3Log::kInformational,"AliL3Hough::FindTrackCandidates()","")
1057 <<"Found "<<fTracks[i]->GetNTracks()<<" tracks in slice "<<fCurrentSlice<<ENDLOG;
1058 fTracks[i]->QSort();
1060 fBenchmark->Stop("Find Maxima");
1061 cpuTime = GetCpuTime() - initTime;
1062 LOG(AliL3Log::kInformational,"AliL3Hough::FindTrackCandidates()","Timing")
1063 <<"Maxima finding done in "<<cpuTime*1000<<" ms"<<ENDLOG;
1066 void AliL3Hough::FindTrackCandidates()
1068 // Find AliL3HoughTransformer track candidates
1070 LOG(AliL3Log::kError,"AliL3Hough::FindTrackCandidatesRow()","")
1071 <<"Incompatible Peak Finder version!"<<ENDLOG;
1077 npatches = 1; //Histograms have been added.
1079 npatches = fNPatches;
1081 Double_t initTime,cpuTime;
1082 initTime = GetCpuTime();
1083 fBenchmark->Start("Find Maxima");
1084 for(Int_t i=0; i<npatches; i++)
1086 AliL3HoughBaseTransformer *tr = fHoughTransformer[i];
1087 fTracks[i]->Reset();
1089 for(Int_t j=0; j<fNEtaSegments; j++)
1091 AliL3Histogram *hist = tr->GetHistogram(j);
1092 if(hist->GetNEntries()==0) continue;
1093 fPeakFinder->Reset();
1094 fPeakFinder->SetHistogram(hist);
1096 cout<<"Starting "<<j<<" etaslice"<<endl;
1098 fPeakFinder->SetThreshold(fPeakThreshold[i]);
1099 fPeakFinder->FindAdaptedPeaks(fKappaSpread,fPeakRatio);
1101 for(Int_t k=0; k<fPeakFinder->GetEntries(); k++)
1103 AliL3HoughTrack *track = (AliL3HoughTrack*)fTracks[i]->NextTrack();
1104 track->SetTrackParameters(fPeakFinder->GetXPeak(k),fPeakFinder->GetYPeak(k),fPeakFinder->GetWeight(k));
1105 track->SetEtaIndex(j);
1106 track->SetEta(tr->GetEta(j,fCurrentSlice));
1107 track->SetRowRange(AliL3Transform::GetFirstRow(0),AliL3Transform::GetLastRow(5));
1110 cout<<"Found "<<fTracks[i]->GetNTracks()<<" tracks in patch "<<i<<endl;
1111 fTracks[i]->QSort();
1113 fBenchmark->Stop("Find Maxima");
1114 cpuTime = GetCpuTime() - initTime;
1115 LOG(AliL3Log::kInformational,"AliL3Hough::FindTrackCandidates()","Timing")
1116 <<"Maxima finding done in "<<cpuTime*1000<<" ms"<<ENDLOG;
1119 void AliL3Hough::InitEvaluate()
1121 //Pass the transformer objects to the AliL3HoughEval objects:
1122 //This will provide the evaluation objects with all the necessary
1123 //data and parameters it needs.
1125 for(Int_t i=0; i<fNPatches; i++)
1126 fEval[i]->InitTransformer(fHoughTransformer[i]);
1129 Int_t AliL3Hough::Evaluate(Int_t roadwidth,Int_t nrowstomiss)
1131 //Evaluate the tracks, by looking along the road in the raw data.
1132 //If track does not cross all padrows - rows2miss, it is removed from the arrray.
1133 //If histograms were not added, the check is done locally in patch,
1134 //meaning that nrowstomiss is the number of padrows the road can miss with respect
1135 //to the number of rows in the patch.
1136 //If the histograms were added, the comparison is done globally in the _slice_,
1137 //meaing that nrowstomiss is the number of padrows the road can miss with
1138 //respect to the total number of padrows in the slice.
1140 //Return value = number of tracks which were removed (only in case of fAddHistograms)
1144 LOG(AliL3Log::kError,"AliL3Hough::Evaluate","Track Array")
1145 <<"No tracks to work with..."<<ENDLOG;
1149 Int_t removedtracks=0;
1150 AliL3TrackArray *tracks=0;
1154 tracks = fTracks[0];
1155 for(Int_t i=0; i<tracks->GetNTracks(); i++)
1157 AliL3Track *track = tracks->GetCheckedTrack(i);
1158 if(!track) continue;
1163 for(Int_t i=0; i<fNPatches; i++)
1164 EvaluatePatch(i,roadwidth,nrowstomiss);
1166 //Here we check the tracks globally;
1167 //how many good rows (padrows with signal)
1168 //did it cross in the slice
1171 for(Int_t j=0; j<tracks->GetNTracks(); j++)
1173 AliL3HoughTrack *track = (AliL3HoughTrack*)tracks->GetCheckedTrack(j);
1175 if(track->GetNHits() < AliL3Transform::GetNRows() - nrowstomiss)
1185 return removedtracks;
1188 void AliL3Hough::EvaluatePatch(Int_t i,Int_t roadwidth,Int_t nrowstomiss)
1192 fEval[i]->InitTransformer(fHoughTransformer[i]);
1193 fEval[i]->SetNumOfPadsToLook(roadwidth);
1194 fEval[i]->SetNumOfRowsToMiss(nrowstomiss);
1195 //fEval[i]->RemoveFoundTracks();
1197 AliL3TrackArray *tracks=0;
1200 tracks = fTracks[i];
1202 tracks = fTracks[0];
1205 for(Int_t j=0; j<tracks->GetNTracks(); j++)
1207 AliL3HoughTrack *track = (AliL3HoughTrack*)tracks->GetCheckedTrack(j);
1210 LOG(AliL3Log::kWarning,"AliL3Hough::EvaluatePatch","Track array")
1211 <<"Track object missing!"<<ENDLOG;
1215 Int_t rowrange[2] = {AliL3Transform::GetFirstRow(i),AliL3Transform::GetLastRow(i)};
1216 Bool_t result = fEval[i]->LookInsideRoad(track,nrows,rowrange);
1219 Int_t pre=track->GetNHits();
1220 track->SetNHits(pre+nrows);
1222 else//the track crossed too few good padrows (padrows with signal) in the patch, so remove it
1224 if(result == kFALSE)
1233 void AliL3Hough::MergeEtaSlices()
1235 //Merge tracks found in neighbouring eta slices.
1236 //Removes the track with the lower weight.
1238 fBenchmark->Start("Merge Eta-slices");
1239 AliL3TrackArray *tracks = fTracks[0];
1242 cerr<<"AliL3Hough::MergeEtaSlices : No tracks "<<endl;
1245 for(Int_t j=0; j<tracks->GetNTracks(); j++)
1247 AliL3HoughTrack *track1 = (AliL3HoughTrack*)tracks->GetCheckedTrack(j);
1248 if(!track1) continue;
1249 for(Int_t k=j+1; k<tracks->GetNTracks(); k++)
1251 AliL3HoughTrack *track2 = (AliL3HoughTrack*)tracks->GetCheckedTrack(k);
1252 if(!track2) continue;
1253 if(abs(track1->GetEtaIndex() - track2->GetEtaIndex()) != 1) continue;
1254 if(fabs(track1->GetKappa()-track2->GetKappa()) < 0.006 &&
1255 fabs(track1->GetPsi()- track2->GetPsi()) < 0.1)
1257 //cout<<"Merging track in slices "<<track1->GetEtaIndex()<<" "<<track2->GetEtaIndex()<<endl;
1258 if(track1->GetWeight() > track2->GetWeight())
1265 fBenchmark->Stop("Merge Eta-slices");
1269 void AliL3Hough::WriteTracks(Char_t *path)
1271 // Write found tracks into file
1272 //cout<<"AliL3Hough::WriteTracks : Sorting the tracsk"<<endl;
1273 //fGlobalTracks->QSort();
1275 Char_t filename[1024];
1276 sprintf(filename,"%s/tracks_%d.raw",path,fEvent);
1277 AliL3MemHandler mem;
1278 mem.SetBinaryOutput(filename);
1279 mem.TrackArray2Binary(fGlobalTracks);
1280 mem.CloseBinaryOutput();
1281 fGlobalTracks->Reset();
1284 void AliL3Hough::WriteTracks(Int_t slice,Char_t *path)
1286 // Write found tracks slice by slice into file
1288 AliL3MemHandler mem;
1292 sprintf(fname,"%s/tracks_ho_%d_%d.raw",path,fEvent,slice);
1293 mem.SetBinaryOutput(fname);
1294 mem.TrackArray2Binary(fTracks[0]);
1295 mem.CloseBinaryOutput();
1299 for(Int_t i=0; i<fNPatches; i++)
1301 sprintf(fname,"%s/tracks_ho_%d_%d_%d.raw",path,fEvent,slice,i);
1302 mem.SetBinaryOutput(fname);
1303 mem.TrackArray2Binary(fTracks[i]);
1304 mem.CloseBinaryOutput();
1310 Int_t AliL3Hough::FillESD(AliESD *esd)
1312 // Fill the found hough transform tracks
1313 // into the ESD. The tracks are stored as
1314 // AliESDHLTtrack objects.
1316 if(!fGlobalTracks) return 0;
1317 Int_t nglobaltracks = 0;
1318 for(Int_t i=0; i<fGlobalTracks->GetNTracks(); i++)
1320 AliL3HoughTrack *tpt = (AliL3HoughTrack *)fGlobalTracks->GetCheckedTrack(i);
1323 if(tpt->GetWeight()<0) continue;
1324 AliL3HoughKalmanTrack *tpctrack = new AliL3HoughKalmanTrack(*tpt);
1325 if(!tpctrack) continue;
1326 AliESDtrack *esdtrack2 = new AliESDtrack() ;
1327 esdtrack2->UpdateTrackParams(tpctrack,AliESDtrack::kTPCin);
1328 esd->AddTrack(esdtrack2);
1333 return nglobaltracks;
1337 void AliL3Hough::WriteDigits(Char_t *outfile)
1339 //Write the current data to a new rootfile.
1342 for(Int_t i=0; i<fNPatches; i++)
1344 AliL3DigitRowData *tempPt = (AliL3DigitRowData*)fHoughTransformer[i]->GetDataPointer();
1345 fMemHandler[i]->AliDigits2RootFile(tempPt,outfile);
1348 cerr<<"AliL3Hough::WriteDigits : You need to compile with AliROOT!"<<endl;
1353 Double_t AliL3Hough::GetCpuTime()
1355 //Return the Cputime in seconds.
1357 gettimeofday( &tv, NULL );
1358 return tv.tv_sec+(((Double_t)tv.tv_usec)/1000000.);
1361 void *AliL3Hough::ProcessInThread(void *args)
1363 // Called in case Hough transform tracking
1364 // is executed in a thread
1366 AliL3Hough *instance = (AliL3Hough *)args;
1367 Int_t minslice = instance->GetMinSlice();
1368 Int_t maxslice = instance->GetMaxSlice();
1369 for(Int_t i=minslice; i<=maxslice; i++)
1371 instance->ReadData(i,0);
1372 instance->Transform();
1373 instance->AddAllHistogramsRows();
1374 instance->FindTrackCandidatesRow();
1375 instance->AddTracks();
1380 void AliL3Hough::StartProcessInThread(Int_t minslice,Int_t maxslice)
1382 // Starts the Hough transform tracking as a
1383 // separate thread. Takes as parameters the
1384 // range of TPC slices (sectors) to be reconstructed
1388 sprintf(buf,"houghtrans_%d_%d",minslice,maxslice);
1389 SetMinMaxSlices(minslice,maxslice);
1390 // fThread = new TThread(buf,(void (*) (void *))&ProcessInThread,(void *)this);
1391 fThread = new TThread(buf,&ProcessInThread,(void *)this);
1397 Int_t AliL3Hough::WaitForThreadFinish()
1399 // Routine is used in case we run the
1400 // Hough transform tracking in several
1401 // threads and want to sync them before
1402 // writing the results to the ESD
1404 #if ROOT_VERSION_CODE < 262403
1405 return TThread::Join(fThread->GetId());
1407 return fThread->Join(fThread->GetId());