Little changes to make g++ version 3.2 compile the src library. Problems remaining...
[u/mrichter/AliRoot.git] / HLT / src / AliL3ConfMapper.cxx
1 //$Id$
2
3 // Author: Anders Vestbo <mailto:vestbo@fi.uib.no>
4 //*-- Copyright &copy ASV 
5  
6 #include "AliL3StandardIncludes.h"
7 #include <sys/time.h>
8
9 #include "AliL3ConfMapper.h"
10 #include "AliL3Logging.h" 
11 #include "AliL3Vertex.h"
12 #include "AliL3ConfMapTrack.h"
13 #include "AliL3ConfMapPoint.h"
14 #include "AliL3TrackArray.h"
15 #include "AliL3Transform.h"
16
17 //_____________________________________________________________
18 // AliL3ConfMapper
19 //
20 // Conformal mapping base class
21 //
22 ClassImp(AliL3ConfMapper)
23
24 Double_t AliL3ConfMapper::pi=3.14159265358979323846;
25 Double_t AliL3ConfMapper::twopi=2*pi;
26 Double_t AliL3ConfMapper::todeg=180./pi;
27
28 AliL3ConfMapper::AliL3ConfMapper()
29 {
30   //Default constructor
31   fVertex = NULL;
32   fTrack = NULL;
33   fHit = NULL;
34   fVolume = NULL;
35   fRow = NULL;
36   fBench = (Bool_t)true;
37   fParamSet = (Bool_t)false;
38   fVertexConstraint = (Bool_t)true;
39   
40 }
41
42
43 AliL3ConfMapper::~AliL3ConfMapper()
44 {
45   // Destructor.
46
47   if(fVolume) {
48     delete [] fVolume;
49   }
50   if(fRow) {
51     delete [] fRow;
52   }
53   if(fHit) {
54     delete [] fHit;
55   }
56   if(fTrack) {
57     delete fTrack;
58   }
59
60 }
61  
62 void AliL3ConfMapper::InitVolumes()
63 {
64   //Data organization.
65   //Allocate volumes, set conformal coordinates and pointers.
66   
67   //Should be done after setting the track parameters
68   
69   fNumRowSegmentPlusOne = 176;//NumRows[0]; //Maximum 32.
70   fNumPhiSegmentPlusOne = fNumPhiSegment+1;
71   fNumEtaSegmentPlusOne = fNumEtaSegment+1;
72   fNumPhiEtaSegmentPlusOne = fNumPhiSegmentPlusOne*fNumEtaSegmentPlusOne;
73   fBounds = fNumRowSegmentPlusOne * fNumPhiSegmentPlusOne * fNumEtaSegmentPlusOne;
74   
75   //Allocate volumes:
76   if(fVolume) delete [] fVolume;
77   if(fRow) delete [] fRow;
78   
79   LOG(AliL3Log::kInformational,"AliL3ConfMapper::InitVolumes","Memory")<<AliL3Log::kDec<<
80     "Allocating "<<fBounds*sizeof(AliL3ConfMapContainer)<<" Bytes to fVolume"<<ENDLOG;
81   LOG(AliL3Log::kInformational,"AliL3ConfMapper::InitVolumes","Memory")<<AliL3Log::kDec<<
82     "Allocating "<<fNumRowSegmentPlusOne*sizeof(AliL3ConfMapContainer)<<" Bytes to fRow"<<ENDLOG;
83   
84   fVolume = new AliL3ConfMapContainer[fBounds];
85   fRow = new AliL3ConfMapContainer[fNumRowSegmentPlusOne];
86   
87   memset(fVolume,0,fBounds*sizeof(AliL3ConfMapContainer));
88   memset(fRow,0,fNumRowSegmentPlusOne*sizeof(AliL3ConfMapContainer));
89   
90   Int_t max_num_of_tracks = 1000;
91   Int_t max_num_of_hits = 50000;
92   
93   if(fHit)
94     delete [] fHit;
95   if(fTrack)
96     delete fTrack;
97     
98   fHit = new AliL3ConfMapPoint[max_num_of_hits];
99   fTrack = new AliL3TrackArray("AliL3ConfMapTrack",max_num_of_tracks);
100 }
101
102 void AliL3ConfMapper::InitSector(Int_t sector,Int_t *rowrange,Float_t *etarange)
103 {
104   //Initialize tracker for tracking in a given sector.
105   //Resets track and hit arrays.
106   //Here it is also possible to specify a subsector, by defining
107   //rowrange[0]=innermost row;
108   //rowrange[1]=outermostrow;
109   //Finally you can specify etaslices to save time (assuming a good seed from TRD...)
110     
111   //Define tracking area:
112   if(rowrange)
113     {
114       fRowMin = rowrange[0];
115       fRowMax = rowrange[1];
116     }
117   else //complete sector
118     {
119       fRowMin = 0;
120       fRowMax = 175;
121     }
122   if(etarange)
123     {
124       fEtaMin = etarange[0];
125       fEtaMax = etarange[1];
126     }
127   else
128     {
129       fEtaMin = 0;
130       //fEtaMax = sector < 18 ? 1 : -1;
131       fEtaMax = sector < 18 ? 0.9 : -0.9;
132     }
133   
134   //Set the angles to sector 2:
135   fPhiMin = -1.*10/todeg;//fParam->GetAngle(sector) - 10/todeg;
136   fPhiMax = 10/todeg;//fParam->GetAngle(sector) + 10/todeg;
137
138   nTracks=0;
139   fMainVertexTracks = 0;
140   fClustersUnused = 0;
141   
142   fNumRowSegment = fRowMax - fRowMin; //number of rows to be considered by tracker
143   LOG(AliL3Log::kInformational,"AliL3ConfMapper::InitSector","B-field")
144     <<"Tracker initializing with a magnetic field of "<<AliL3Transform::GetBField()<<ENDLOG;
145   
146   fTrack->Reset();
147 }
148
149
150
151 Bool_t AliL3ConfMapper::ReadHits(UInt_t count, AliL3SpacePointData* hits )
152 {
153   Int_t nhit=(Int_t)count; 
154   for (Int_t i=0;i<nhit;i++)
155     {
156       fHit[i].Reset();
157       fHit[i].ReadHits(&(hits[i]));
158     }
159   fClustersUnused += nhit;
160   LOG(AliL3Log::kInformational,"AliL3ConfMapper::ReadHits","#hits")
161     <<AliL3Log::kDec<<"hit_counter: "<<nhit<<" count: "<<count<<ENDLOG;
162   
163   return true;
164 }
165
166
167 void AliL3ConfMapper::SetPointers()
168 {
169   
170   //Reset detector volumes
171   memset(fVolume,0,fBounds*sizeof(AliL3ConfMapContainer));
172   memset(fRow,0,fNumRowSegmentPlusOne*sizeof(AliL3ConfMapContainer));
173   
174   Float_t phiSlice = (fPhiMax-fPhiMin)/fNumPhiSegment;
175   Float_t etaSlice = (fEtaMax-fEtaMin)/fNumEtaSegment;
176
177   Int_t volumeIndex;
178   Int_t local_counter=0;
179   for(Int_t j=0; j<fClustersUnused; j++)
180     {
181       
182       //AliL3ConfMapPoint *thisHit = (AliL3ConfMapPoint*)fHit->At(j);
183       AliL3ConfMapPoint *thisHit = &(fHit[j]);
184
185       thisHit->Setup(fVertex);
186       
187       Int_t localrow = thisHit->GetPadRow();
188       
189       if(localrow < fRowMin || localrow > fRowMax)
190         continue;
191
192       //Get indexes:
193       thisHit->phiIndex=(Int_t)((thisHit->GetPhi()-fPhiMin)/phiSlice +1);
194       
195       if(thisHit->phiIndex<1 || thisHit->phiIndex>fNumPhiSegment)
196         {
197           fPhiHitsOutOfRange++;
198           continue;
199         }
200       
201       thisHit->etaIndex=(Int_t)((thisHit->GetEta()-fEtaMin)/etaSlice + 1);
202       if(thisHit->etaIndex<1 || thisHit->etaIndex>fNumEtaSegment)
203         {
204           fEtaHitsOutOfRange++;
205           continue;
206         }
207       local_counter++;
208       
209       volumeIndex = (localrow-fRowMin)*fNumPhiEtaSegmentPlusOne+thisHit->phiIndex*fNumEtaSegmentPlusOne+thisHit->etaIndex;
210       
211       if(fVolume[volumeIndex].first == NULL)
212         fVolume[volumeIndex].first = (void *)thisHit;
213       else
214         ((AliL3ConfMapPoint *)fVolume[volumeIndex].last)->nextVolumeHit=thisHit;
215       fVolume[volumeIndex].last = (void *)thisHit;
216       
217       
218       //set row pointers
219       if(fRow[(localrow-fRowMin)].first == NULL)
220         fRow[(localrow-fRowMin)].first = (void *)thisHit;
221       else
222         ((AliL3ConfMapPoint *)(fRow[(localrow-fRowMin)].last))->nextRowHit = thisHit;
223         fRow[(localrow-fRowMin)].last = (void *)thisHit;
224         
225         
226     }
227   
228   if(fClustersUnused>0 && local_counter==0)
229     LOG(AliL3Log::kError,"AliL3ConfMapper::SetPointers","Parameters")
230       <<AliL3Log::kDec<<"No points passed to track finder, hits out of range: "
231       <<fEtaHitsOutOfRange+fPhiHitsOutOfRange<<ENDLOG;
232
233   LOG(AliL3Log::kInformational,"AliL3ConfMapper::SetPointers","Setup")
234     <<"Setup finished, hits out of range: "<<fEtaHitsOutOfRange+fPhiHitsOutOfRange
235     <<" hits accepted "<<fClustersUnused<<ENDLOG;
236 }
237
238 void AliL3ConfMapper::MainVertexTracking_a()
239 {
240   //Tracking with vertex constraint.
241
242   if(!fParamSet)
243     {
244       LOG(AliL3Log::kError,"AliL3ConfMapper::MainVertexTracking","Parameters")<<AliL3Log::kDec<<
245         "Tracking parameters not set!"<<ENDLOG;
246       return;
247     }
248
249   Double_t initCpuTime,cpuTime;
250   initCpuTime = CpuTime();
251   SetPointers();
252   SetVertexConstraint(true);
253   cpuTime = CpuTime() - initCpuTime;
254   if(fBench)
255     LOG(AliL3Log::kInformational,"AliL3ConfMapper::MainVertexTracking_a","Timing")
256       <<AliL3Log::kDec<<"Setup finished in "<<cpuTime*1000<<" ms"<<ENDLOG;
257   
258 }
259
260 void AliL3ConfMapper::MainVertexTracking_b()
261 {
262   //Tracking with vertex constraint.
263
264   if(!fParamSet)
265     {
266       LOG(AliL3Log::kError,"AliL3ConfMapper::MainVertexTracking","Parameters")<<AliL3Log::kDec<<
267         "Tracking parameters not set!"<<ENDLOG;
268       return;
269     }
270   Double_t initCpuTime,cpuTime;
271   initCpuTime = CpuTime();
272   
273   ClusterLoop();
274  
275   cpuTime = CpuTime() - initCpuTime;
276   if(fBench)
277     LOG(AliL3Log::kInformational,"AliL3ConfMapper::MainVertexTracking_b","Timing")
278       <<AliL3Log::kDec<<"Main Tracking finished in "<<cpuTime*1000<<" ms"<<ENDLOG;
279 }
280
281 void AliL3ConfMapper::MainVertexTracking()
282 {
283   //Tracking with vertex constraint.
284
285   if(!fParamSet)
286     {
287       LOG(AliL3Log::kError,"AliL3ConfMapper::MainVertexTracking","Parameters")<<AliL3Log::kDec<<
288         "Tracking parameters not set!"<<ENDLOG;
289       return;
290     }
291
292   Double_t initCpuTime,cpuTime;
293   initCpuTime = CpuTime();
294   
295   SetPointers();
296   SetVertexConstraint(true);
297       
298   ClusterLoop();
299
300   cpuTime = CpuTime() - initCpuTime;
301   if(fBench)
302     LOG(AliL3Log::kInformational,"AliL3ConfMapper::MainVertexTracking","Timing")<<AliL3Log::kDec<<
303       "Tracking finished in "<<cpuTime*1000<<" ms"<<ENDLOG;
304   
305   return;
306 }
307
308 void AliL3ConfMapper::NonVertexTracking()
309 {
310   //Tracking with no vertex constraint. This should be called after doing MainVertexTracking,
311   //in order to do tracking on the remaining clusters.
312   //The conformal mapping is now done with respect to the first cluster
313   //assosciated with this track.
314
315   SetVertexConstraint(false);
316   ClusterLoop();
317   LOG(AliL3Log::kInformational,"AliL3ConfMapper::NonVertexTracking","ntracks")<<AliL3Log::kDec<<
318     "Number of nonvertex tracks found: "<<(nTracks-fMainVertexTracks)<<ENDLOG;
319   return;
320 }
321
322 void AliL3ConfMapper::MainVertexSettings(Int_t trackletlength, Int_t tracklength,
323                                          Int_t rowscopetracklet, Int_t rowscopetrack,
324                                          Double_t maxphi,Double_t maxeta)
325 {
326   //Settings for main vertex tracking. The cuts are:
327   //TrackletLength:      #hits on segment, before trying to build a track
328   //TrackLength:         Minimum hits on a track
329   //RowScopeTracklet:    Row search range for segments
330   //RowScopeTrack:       Row search range for tracks
331   
332   SetTrackletLength(trackletlength,(Bool_t)true);
333   SetRowScopeTracklet(rowscopetracklet, (Bool_t) true);
334   SetRowScopeTrack(rowscopetrack, (Bool_t) true);
335   SetMinPoints(tracklength,(Bool_t)true);
336   fMaxPhi=maxphi;
337   fMaxEta=maxeta;
338 }
339
340 void AliL3ConfMapper::NonVertexSettings(Int_t trackletlength, Int_t tracklength,
341                                         Int_t rowscopetracklet, Int_t rowscopetrack)
342 {
343   SetTrackletLength(trackletlength,(Bool_t)false);
344   SetRowScopeTracklet(rowscopetracklet, (Bool_t)false);
345   SetRowScopeTrack(rowscopetrack, (Bool_t)false);
346   SetMinPoints(tracklength,(Bool_t)false);
347 }
348
349 void AliL3ConfMapper::SetTrackCuts(Double_t hitChi2Cut, Double_t goodHitChi2, Double_t trackChi2Cut,Int_t maxdist,
350                                    Bool_t vertexconstraint)
351 {
352   //Settings for tracks. The cuts are:
353   //HitChi2Cut:     Maximum hit chi2
354   //goodHitChi2:    Chi2 to stop look for next hit
355   //trackChi2Cut:   Maximum track chi2
356   //maxdist:        Maximum distance between two clusters when forming segments
357   
358   SetHitChi2Cut(hitChi2Cut,vertexconstraint);
359   SetGoodHitChi2(goodHitChi2,vertexconstraint);
360   SetTrackChi2Cut(trackChi2Cut,vertexconstraint);
361   SetMaxDist(maxdist,vertexconstraint);
362 }
363
364 void AliL3ConfMapper::SetTrackletCuts(Double_t maxangle,Double_t goodDist, Bool_t vertex_constraint)
365 {
366   //Sets cuts of tracklets. Right now this is only:
367   //maxangle:  Maximum angle when forming segments (if trackletlength > 2)
368  
369   fGoodDist=goodDist;
370   SetMaxAngleTracklet(maxangle, vertex_constraint);
371 }
372
373 void AliL3ConfMapper::ClusterLoop()
374 {
375   //Loop over hits, starting at outermost padrow, and trying to build segments.
376
377   Int_t row_segm,lastrow = fRowMin + fMinPoints[fVertexConstraint];
378   AliL3ConfMapPoint *hit;
379   
380   //Loop over rows, and try to create tracks from the hits.
381   //Starts at the outermost row, and loops as long as a track can be build, due to length.
382   
383   for(row_segm = fRowMax; row_segm >= lastrow; row_segm--)
384     {
385       if(fRow[(row_segm-fRowMin)].first && ((AliL3ConfMapPoint*)fRow[(row_segm-fRowMin)].first)->GetPadRow() < fRowMin + 1)
386         break;
387       for(hit = (AliL3ConfMapPoint*)fRow[(row_segm-fRowMin)].first; hit!=0; hit=hit->nextRowHit)
388         {
389           if(hit->GetUsage() == true)
390             continue;
391           else
392             CreateTrack(hit);
393         }
394     }
395   
396   return;
397 }
398
399
400 void AliL3ConfMapper::CreateTrack(AliL3ConfMapPoint *hit)
401 {
402   //Tries to create a track from the initial hit given by ClusterLoop()
403
404   AliL3ConfMapPoint *closest_hit = NULL;
405   AliL3ConfMapTrack *track = NULL;
406   
407   Int_t point;
408   Int_t tracks = nTracks;
409   nTracks++;
410
411   track = (AliL3ConfMapTrack*)fTrack->NextTrack();
412
413   //reset hit parameters:
414   track->Reset();
415   
416   UInt_t *trackhitnumber = track->GetHitNumbers();
417     
418   //set conformal coordinates if we are looking for non vertex tracks
419   if(!fVertexConstraint) 
420     {
421       hit->SetAllCoord(hit);
422     }
423   
424   //fill fit parameters of initial track:
425   track->UpdateParam(hit); //here the number of hits is incremented.
426   trackhitnumber[track->GetNumberOfPoints()-1] = hit->GetHitNumber();
427   
428   Double_t dx,dy;
429   //create tracklets:
430   
431   for(point=1; point<fTrackletLength[fVertexConstraint]; point++)
432     {
433       if((closest_hit = GetNextNeighbor(hit)))
434         {//closest hit exist
435           
436           //   Calculate track length in sz plane
437           dx = ((AliL3ConfMapPoint*)closest_hit)->GetX() - ((AliL3ConfMapPoint*)hit)->GetX();
438           dy = ((AliL3ConfMapPoint*)closest_hit)->GetY() - ((AliL3ConfMapPoint*)hit)->GetY();
439           //track->fLength += (Double_t)sqrt ( dx * dx + dy * dy ) ;
440           Double_t length = track->GetLength()+(Double_t)sqrt ( dx * dx + dy * dy );
441           track->SetLength(length);
442
443           //closest_hit->SetS(track->fLength);
444           closest_hit->SetS(track->GetLength());
445
446           //update fit parameters
447           track->UpdateParam(closest_hit);
448           trackhitnumber[track->GetNumberOfPoints()-1] = closest_hit->GetHitNumber();
449         
450           hit = closest_hit;
451         }
452       else
453         {
454           //closest hit does not exist:
455           track->DeleteCandidate();
456           fTrack->RemoveLast();
457           nTracks--;
458           point = fTrackletLength[fVertexConstraint];
459         }
460     }
461   
462   //tracklet is long enough to be extended to a track
463   if(track->GetNumberOfPoints() == fTrackletLength[fVertexConstraint])
464     {
465       
466       track->SetProperties(true);
467             
468       if(TrackletAngle(track) > fMaxAngleTracklet[fVertexConstraint])
469         {//proof if the first points seem to be a beginning of a track
470           track->SetProperties(false);
471           track->DeleteCandidate();
472           fTrack->RemoveLast();
473           nTracks--;
474         }
475       
476       else//good tracklet ->proceed, follow the trackfit
477         {
478           tracks++;
479                                   
480           //define variables to keep the total chi:
481           Double_t xyChi2 = track->fChiSq[0];
482           Double_t szChi2 = track->fChiSq[1];
483           
484           for(point = fTrackletLength[fVertexConstraint]; point <= fNumRowSegment; point++)
485             {
486               track->fChiSq[0] = fHitChi2Cut[fVertexConstraint];
487               closest_hit = GetNextNeighbor((AliL3ConfMapPoint*)track->lastHit,track);
488               
489               if(closest_hit)
490                 {
491                   
492                   //keep total chi:
493                   Double_t lxyChi2 = track->fChiSq[0]-track->fChiSq[1];
494                   xyChi2 += lxyChi2;
495                   closest_hit->xyChi2 = lxyChi2;
496                                   
497                   //update track length:
498                   //track->fLength = closest_hit->GetS();
499                   track->SetLength(closest_hit->GetS());
500                   szChi2 += track->fChiSq[1];
501                   closest_hit->szChi2 = track->fChiSq[1];
502                   
503                   track->UpdateParam(closest_hit);
504                   trackhitnumber[track->GetNumberOfPoints()-1] = closest_hit->GetHitNumber();
505                   
506                   //add closest hit to track
507                   closest_hit->SetUsage(true);
508                   closest_hit->SetTrackNumber(tracks-1);
509                   
510                 }//closest_hit
511               
512               else
513                 {
514                   //closest hit does not exist
515                   point = fNumRowSegment; //continue with next hit in segment
516                 }//else
517               
518             }//create tracks
519           
520           //store track chi2:
521           track->fChiSq[0] = xyChi2;
522           track->fChiSq[1] = szChi2;
523           Double_t normalized_chi2 = (track->fChiSq[0]+track->fChiSq[1])/track->GetNumberOfPoints();
524           
525           //remove tracks with not enough points already now
526           if(track->GetNumberOfPoints() < fMinPoints[fVertexConstraint] || normalized_chi2 > fTrackChi2Cut[fVertexConstraint])
527             {
528               track->SetProperties(false);
529               nTracks--;
530               track->DeleteCandidate();
531               fTrack->RemoveLast();
532               tracks--;
533             }
534           
535           else
536             {
537               fClustersUnused -= track->GetNumberOfPoints();
538               track->ComesFromMainVertex(fVertexConstraint);
539               //mark track as main vertex track or not
540               track->SetSector(2); //only needed for testing purposes.
541               track->SetRowRange(fRowMin,fRowMax);
542
543               if(fVertexConstraint) 
544                 fMainVertexTracks++;
545             }
546      
547         }//good tracklet
548       
549     }
550   
551   return;
552 }
553
554 AliL3ConfMapPoint *AliL3ConfMapper::GetNextNeighbor(AliL3ConfMapPoint *start_hit,
555                                           AliL3ConfMapTrack *track)
556 {
557   //When forming segments: Finds closest hit to input hit
558   //When forming tracks: Find closest hit to track fit.
559   
560   Double_t dist,closest_dist = fMaxDist[fVertexConstraint];
561   
562   AliL3ConfMapPoint *hit = NULL;
563   AliL3ConfMapPoint *closest_hit = NULL;
564     
565   Int_t sub_row_segm;
566   Int_t sub_phi_segm;
567   Int_t sub_eta_segm;
568   Int_t volumeIndex;
569   Int_t test_hit;
570
571   Int_t max_row = start_hit->GetPadRow()-1;
572   Int_t min_row;
573
574   if(track) //finding hit close to trackfit
575     {
576       min_row = start_hit->GetPadRow()-fRowScopeTrack[fVertexConstraint];
577     }
578   else
579     {
580       min_row = start_hit->GetPadRow()-fRowScopeTracklet[fVertexConstraint];
581     }
582
583   //make a smart loop
584   Int_t loop_eta[9] = {0,0,0,-1,-1,-1,1,1,1};
585   Int_t loop_phi[9] = {0,-1,1,0,-1,1,0,-1,1};
586   
587   if(min_row < fRowMin)
588     min_row = fRowMin;
589   if(max_row < fRowMin)
590     return 0;  //reached the last padrow under consideration
591
592   else
593     {
594       //loop over sub rows
595       for(sub_row_segm=max_row; sub_row_segm>=min_row; sub_row_segm--)
596         {
597           //loop over subsegments, in the order defined above.
598           for(Int_t i=0; i<9; i++)  
599             {
600               sub_phi_segm = start_hit->phiIndex + loop_phi[i];
601               
602               if(sub_phi_segm<0)
603                 sub_phi_segm += fNumPhiSegment;
604               
605               else if(sub_phi_segm >=fNumPhiSegment)
606                 sub_phi_segm -= fNumPhiSegment;
607               
608               //loop over sub eta segments
609               
610               sub_eta_segm = start_hit->etaIndex + loop_eta[i];
611               
612               if(sub_eta_segm < 0 || sub_eta_segm >=fNumEtaSegment)
613                 continue;//segment exceeds bounds->skip it
614               
615               //loop over hits in this sub segment:
616               volumeIndex= (sub_row_segm-fRowMin)*fNumPhiEtaSegmentPlusOne +
617                 sub_phi_segm*fNumEtaSegmentPlusOne + sub_eta_segm;
618               
619               if(volumeIndex<0)
620                 {//debugging
621                   LOG(AliL3Log::kError,"AliL3ConfMapper::GetNextNeighbor","Memory")<<AliL3Log::kDec<<
622                     "VolumeIndex error "<<volumeIndex<<ENDLOG;
623                 }
624               
625               for(hit = (AliL3ConfMapPoint*)fVolume[volumeIndex].first;
626                   hit!=0; hit = hit->nextVolumeHit)
627                 {
628                   
629                   if(!hit->GetUsage())
630                     {//hit was not used before
631                       
632                       //set conformal mapping if looking for nonvertex tracks:
633                       if(!fVertexConstraint)
634                         {
635                           hit->SetAllCoord(start_hit);
636                         }
637                      
638                       if(track)//track search - look for nearest neighbor to extrapolated track
639                         {
640                           if(!VerifyRange(start_hit,hit))
641                             continue;
642                                                   
643                           test_hit = EvaluateHit(start_hit,hit,track);
644                           
645                           if(test_hit == 0)//chi2 not good enough, keep looking
646                             continue;
647                           else if(test_hit==2)//chi2 good enough, return it
648                             return hit;
649                           else
650                             closest_hit = hit;//chi2 acceptable, but keep looking
651                           
652                         }//track search
653                       
654                       else //tracklet search, look for nearest neighbor
655                         {
656                           
657                           if((dist=CalcDistance(start_hit,hit)) < closest_dist)
658                             {
659                               if(!VerifyRange(start_hit,hit))
660                                 continue;
661                               closest_dist = dist;
662                               closest_hit = hit;
663                          
664                               //if this hit is good enough, return it:
665                               if(closest_dist < fGoodDist)
666                                 return closest_hit;
667                             }
668                           else
669                             continue;//sub hit was farther away than a hit before
670                           
671                         }//tracklet search
672                       
673                     }//hit not used before
674                   
675                   else continue; //sub hit was used before
676                   
677                 }//loop over hits in sub segment
678                       
679             }//loop over sub segments
680                   
681         }//loop over subrows
682       
683     }//else
684
685   //closest hit found:
686   if(closest_hit)// && closest_dist < mMaxDist)
687     return closest_hit;
688   else
689     return 0;
690 }
691
692 Int_t AliL3ConfMapper::EvaluateHit(AliL3ConfMapPoint *start_hit,AliL3ConfMapPoint *hit,AliL3ConfMapTrack *track) 
693 {
694   //Check if space point gives a fit with acceptable chi2.
695   
696   Double_t temp,dxy,lchi2,dx,dy,slocal,dsz,lszChi2;
697   temp = (track->a2Xy*hit->GetXprime()-hit->GetYprime()+track->a1Xy);
698   dxy = temp*temp/(track->a2Xy*track->a2Xy + 1.);
699   
700   //Calculate chi2
701   lchi2 = (dxy*hit->GetXYWeight());
702   
703   if(lchi2 > track->fChiSq[0])//chi2 was worse than before.
704     return 0;
705     
706   //calculate s and the distance hit-line
707   dx = start_hit->GetX()-hit->GetX();
708   dy = start_hit->GetY()-hit->GetY();
709   //slocal = track->fLength+sqrt(dx*dx+dy*dy);
710   slocal = track->GetLength()+sqrt(dx*dx+dy*dy);
711   
712   temp = (track->a2Sz*slocal-hit->GetZ()+track->a1Sz);
713   dsz = temp*temp/(track->a2Sz*track->a2Sz+1);
714   
715   //calculate chi2
716   lszChi2 = dsz*hit->GetZWeight();
717   lchi2 += lszChi2;
718   
719     
720   //check whether chi2 is better than previous one:
721   if(lchi2 < track->fChiSq[0])
722     {
723       track->fChiSq[0] = lchi2;
724       track->fChiSq[1] = lszChi2;
725     
726       hit->SetS(slocal);
727   
728       //if chi2 good enough, stop here:
729       if(lchi2 < fGoodHitChi2[fVertexConstraint]) 
730         return 2;
731       
732       return 1;
733     }
734   
735   return 0;
736   
737 }
738
739 Double_t AliL3ConfMapper::CalcDistance(const AliL3ConfMapPoint *hit1,const AliL3ConfMapPoint *hit2) const
740 {
741   //Return distance between two clusters, defined by Pablo
742   
743   Double_t phi_diff = fabs( hit1->GetPhi() - hit2->GetPhi() );
744   if (phi_diff > pi) phi_diff = twopi - phi_diff;
745   
746   return todeg*fabs((Float_t)((hit1->GetPadRow() - hit2->GetPadRow()) * (phi_diff + fabs( hit1->GetEta() - hit2->GetEta()))));
747 }
748
749 Bool_t AliL3ConfMapper::VerifyRange(const AliL3ConfMapPoint *hit1,const AliL3ConfMapPoint *hit2) const
750 {
751   //Check if the hit are within reasonable range in phi and eta
752   Double_t dphi,deta;//maxphi=0.1,maxeta=0.1;
753   dphi = fabs(hit1->GetPhi() - hit2->GetPhi());
754   if(dphi > pi) dphi = fabs(twopi - dphi);
755   if(dphi > fMaxPhi) return false;
756   
757   deta = fabs(hit1->GetEta() - hit2->GetEta());
758   if(deta > fMaxEta) return false;
759
760   return true;
761
762 }
763
764 Double_t AliL3ConfMapper::TrackletAngle(const AliL3ConfMapTrack *track,Int_t n) const
765 {
766   // Returns the angle 'between' the last three points (started at point number n) on this track.
767
768   return 0;
769   /*
770   Double_t x1[2];
771   Double_t x2[2];
772   Double_t angle1,angle2;
773   TObjArray *hits = track->GetHits();
774   
775   if (n > track->GetNumberOfPoints()) {
776     n = track->GetNumberOfPoints();
777   }
778
779   if (n<3) 
780     return 0;
781   
782
783   x1[0] = ((AliL3ConfMapPoint *)hits->At(n-2))->GetX() - ((AliL3ConfMapPoint *)hits->At(n-3))->GetX();
784   x1[1] = ((AliL3ConfMapPoint *)hits->At(n-2))->GetY() - ((AliL3ConfMapPoint *)hits->At(n-3))->GetY();
785
786   x2[0] = ((AliL3ConfMapPoint *)hits->At(n-1))->GetX() - ((AliL3ConfMapPoint *)hits->At(n-2))->GetX();
787   x2[1] = ((AliL3ConfMapPoint *)hits->At(n-1))->GetY() - ((AliL3ConfMapPoint *)hits->At(n-2))->GetY();
788   
789   angle1 = atan2(x1[1],x1[0]);
790   angle2 = atan2(x2[1],x1[0]);
791   return fabs(angle1-angle2);
792   */
793 }
794
795 Int_t AliL3ConfMapper::FillTracks()
796 {
797   //Fill track parameters. Which basically means do a fit of helix in real space,
798   //which should be done in order to get nice tracks.
799   
800   Int_t num_of_tracks = nTracks;
801   LOG(AliL3Log::kInformational,"AliL3ConfMapper::FillTracks","nTracks")<<AliL3Log::kDec<<
802     "Number of found tracks: "<<nTracks<<ENDLOG;
803   
804   if(nTracks == 0)
805     {
806       LOG(AliL3Log::kError,"AliL3ConfMapper::FillTracks","nTracks")<<AliL3Log::kDec<<
807         "No tracks found!!"<<ENDLOG;
808       return 0;
809     }
810   
811   //  fTrack->Sort();
812   for(int i=0; i<num_of_tracks; i++)
813     {
814       AliL3ConfMapTrack *track = (AliL3ConfMapTrack*)fTrack->GetTrack(i);
815       track->Fill(fVertex,fMaxDca);
816     }
817   return 1;
818
819 }
820
821 Double_t AliL3ConfMapper::CpuTime()
822 {
823   //Return the Cputime in seconds.
824  struct timeval tv;
825  gettimeofday( &tv, NULL );
826  return tv.tv_sec+(((Double_t)tv.tv_usec)/1000000.);
827  //return (Double_t)(clock()) / CLOCKS_PER_SEC;
828 }