Update in the interface for HLT and for visualization
[u/mrichter/AliRoot.git] / TRD / AliTRDtrackerV1.cxx
CommitLineData
bcb6fb78 1
e4f2f73d 2/**************************************************************************
972ef65e 3* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4* *
5* Author: The ALICE Off-line Project. *
6* Contributors are mentioned in the code where appropriate. *
7* *
8* Permission to use, copy, modify and distribute this software and its *
9* documentation strictly for non-commercial purposes is hereby granted *
10* without fee, provided that the above copyright notice appears in all *
11* copies and that both the copyright notice and this permission notice *
12* appear in the supporting documentation. The authors make no claims *
13* about the suitability of this software for any purpose. It is *
14* provided "as is" without express or implied warranty. *
15**************************************************************************/
e4f2f73d 16
17/* $Id$ */
18
19///////////////////////////////////////////////////////////////////////////////
20// //
21// Track finder //
22// //
23// Authors: //
24// Alex Bercuci <A.Bercuci@gsi.de> //
25// Markus Fasel <M.Fasel@gsi.de> //
26// //
27///////////////////////////////////////////////////////////////////////////////
28
bb56afff 29// #include <Riostream.h>
30// #include <stdio.h>
31// #include <string.h>
e4f2f73d 32
33#include <TBranch.h>
bb56afff 34#include <TDirectory.h>
e4f2f73d 35#include <TLinearFitter.h>
e4f2f73d 36#include <TTree.h>
37#include <TClonesArray.h>
e4f2f73d 38#include <TTreeStream.h>
39
40#include "AliLog.h"
41#include "AliESDEvent.h"
bb56afff 42#include "AliGeomManager.h"
e4f2f73d 43#include "AliRieman.h"
44#include "AliTrackPointArray.h"
45
e4f2f73d 46#include "AliTRDgeometry.h"
47#include "AliTRDpadPlane.h"
e4f2f73d 48#include "AliTRDcalibDB.h"
e4f2f73d 49#include "AliTRDReconstructor.h"
50#include "AliTRDCalibraFillHisto.h"
e4f2f73d 51#include "AliTRDrecoParam.h"
bb56afff 52
53#include "AliTRDcluster.h"
e4f2f73d 54#include "AliTRDseedV1.h"
0906e73e 55#include "AliTRDtrackV1.h"
bb56afff 56#include "AliTRDtrackerV1.h"
57#include "AliTRDtrackerDebug.h"
58#include "AliTRDtrackingChamber.h"
59#include "AliTRDchamberTimeBin.h"
60
e4f2f73d 61
e4f2f73d 62
63ClassImp(AliTRDtrackerV1)
eb38ed55 64
65
66const Float_t AliTRDtrackerV1::fgkMinClustersInTrack = 0.5; //
67const Float_t AliTRDtrackerV1::fgkLabelFraction = 0.8; //
68const Double_t AliTRDtrackerV1::fgkMaxChi2 = 12.0; //
69const Double_t AliTRDtrackerV1::fgkMaxSnp = 0.95; // Maximum local sine of the azimuthal angle
70const Double_t AliTRDtrackerV1::fgkMaxStep = 2.0; // Maximal step size in propagation
d76231c8 71Double_t AliTRDtrackerV1::fgTopologicQA[kNConfigs] = {
972ef65e 72 0.1112, 0.1112, 0.1112, 0.0786, 0.0786,
73 0.0786, 0.0786, 0.0579, 0.0579, 0.0474,
74 0.0474, 0.0408, 0.0335, 0.0335, 0.0335
e4f2f73d 75};
2985ffcb 76Int_t AliTRDtrackerV1::fgNTimeBins = 0;
eb38ed55 77TTreeSRedirector *AliTRDtrackerV1::fgDebugStreamer = 0x0;
78AliRieman* AliTRDtrackerV1::fgRieman = 0x0;
79TLinearFitter* AliTRDtrackerV1::fgTiltedRieman = 0x0;
80TLinearFitter* AliTRDtrackerV1::fgTiltedRiemanConstrained = 0x0;
e4f2f73d 81
82//____________________________________________________________________
eb38ed55 83AliTRDtrackerV1::AliTRDtrackerV1()
972ef65e 84 :AliTracker()
85 ,fGeom(new AliTRDgeometry())
86 ,fClusters(0x0)
87 ,fTracklets(0x0)
88 ,fTracks(0x0)
89 ,fSieveSeeding(0)
e4f2f73d 90{
972ef65e 91 //
92 // Default constructor.
93 //
94 if (!AliTRDcalibDB::Instance()) {
95 AliFatal("Could not get calibration object");
96 }
97 fgNTimeBins = AliTRDcalibDB::Instance()->GetNumberOfTimeBins();
98
99 for (Int_t isector = 0; isector < AliTRDgeometry::kNsect; isector++) new(&fTrSec[isector]) AliTRDtrackingSector(fGeom, isector);
100
101 if(AliTRDReconstructor::StreamLevel() > 1){
102 TDirectory *savedir = gDirectory;
103 fgDebugStreamer = new TTreeSRedirector("TRD.TrackerDebug.root");
104 savedir->cd();
105 }
eb38ed55 106}
107
e4f2f73d 108//____________________________________________________________________
109AliTRDtrackerV1::~AliTRDtrackerV1()
110{
972ef65e 111 //
112 // Destructor
113 //
114
115 if(fgDebugStreamer) delete fgDebugStreamer;
116 if(fgRieman) delete fgRieman;
117 if(fgTiltedRieman) delete fgTiltedRieman;
118 if(fgTiltedRiemanConstrained) delete fgTiltedRiemanConstrained;
119 if(fTracks) {fTracks->Delete(); delete fTracks;}
120 if(fTracklets) {fTracklets->Delete(); delete fTracklets;}
121 if(fClusters) {fClusters->Delete(); delete fClusters;}
122 if(fGeom) delete fGeom;
e4f2f73d 123}
124
125//____________________________________________________________________
126Int_t AliTRDtrackerV1::Clusters2Tracks(AliESDEvent *esd)
127{
972ef65e 128 //
129 // Steering stand alone tracking for full TRD detector
130 //
131 // Parameters :
132 // esd : The ESD event. On output it contains
133 // the ESD tracks found in TRD.
134 //
135 // Output :
136 // Number of tracks found in the TRD detector.
137 //
138 // Detailed description
139 // 1. Launch individual SM trackers.
140 // See AliTRDtrackerV1::Clusters2TracksSM() for details.
141 //
142
143 if(!AliTRDReconstructor::RecoParam()){
144 AliError("Reconstruction configuration not initialized. Call first AliTRDReconstructor::SetRecoParam().");
145 return 0;
146 }
147
148 //AliInfo("Start Track Finder ...");
149 Int_t ntracks = 0;
150 for(int ism=0; ism<AliTRDgeometry::kNsect; ism++){
151 // for(int ism=1; ism<2; ism++){
152 //AliInfo(Form("Processing supermodule %i ...", ism));
153 ntracks += Clusters2TracksSM(ism, esd);
154 }
155 AliInfo(Form("Number of found tracks : %d", ntracks));
156 return ntracks;
e4f2f73d 157}
158
0906e73e 159
160//_____________________________________________________________________________
eb38ed55 161Bool_t AliTRDtrackerV1::GetTrackPoint(Int_t index, AliTrackPoint &p) const
0906e73e 162{
972ef65e 163 //AliInfo(Form("Asking for tracklet %d", index));
0906e73e 164
972ef65e 165 if(index<0) return kFALSE;
166 AliTRDseedV1 *tracklet = 0x0;
167 if(!(tracklet = (AliTRDseedV1*)fTracklets->UncheckedAt(index))) return kFALSE;
eb38ed55 168
972ef65e 169 // get detector for this tracklet
170 AliTRDcluster *cl = 0x0;
171 Int_t ic = 0; do; while(!(cl = tracklet->GetClusters(ic++)));
172 Int_t idet = cl->GetDetector();
eb38ed55 173
972ef65e 174 Double_t local[3];
175 local[0] = tracklet->GetX0();
176 local[1] = tracklet->GetYfit(0);
177 local[2] = tracklet->GetZfit(0);
178 Double_t global[3];
179 fGeom->RotateBack(idet, local, global);
180 p.SetXYZ(global[0],global[1],global[2]);
181
182
183 // setting volume id
184 AliGeomManager::ELayerID iLayer = AliGeomManager::kTRD1;
185 switch (fGeom->GetPlane(idet)) {
186 case 0:
187 iLayer = AliGeomManager::kTRD1;
188 break;
189 case 1:
190 iLayer = AliGeomManager::kTRD2;
191 break;
192 case 2:
193 iLayer = AliGeomManager::kTRD3;
194 break;
195 case 3:
196 iLayer = AliGeomManager::kTRD4;
197 break;
198 case 4:
199 iLayer = AliGeomManager::kTRD5;
200 break;
201 case 5:
202 iLayer = AliGeomManager::kTRD6;
203 break;
204 };
205 Int_t modId = fGeom->GetSector(idet) * fGeom->Ncham() + fGeom->GetChamber(idet);
206 UShort_t volid = AliGeomManager::LayerToVolUID(iLayer, modId);
207 p.SetVolumeID(volid);
eb38ed55 208
972ef65e 209 return kTRUE;
0906e73e 210}
211
eb38ed55 212//____________________________________________________________________
213TLinearFitter* AliTRDtrackerV1::GetTiltedRiemanFitter()
214{
972ef65e 215 if(!fgTiltedRieman) fgTiltedRieman = new TLinearFitter(4, "hyp4");
216 return fgTiltedRieman;
eb38ed55 217}
0906e73e 218
eb38ed55 219//____________________________________________________________________
220TLinearFitter* AliTRDtrackerV1::GetTiltedRiemanFitterConstraint()
221{
972ef65e 222 if(!fgTiltedRiemanConstrained) fgTiltedRiemanConstrained = new TLinearFitter(2, "hyp2");
223 return fgTiltedRiemanConstrained;
eb38ed55 224}
225
226//____________________________________________________________________
227AliRieman* AliTRDtrackerV1::GetRiemanFitter()
228{
972ef65e 229 if(!fgRieman) fgRieman = new AliRieman(AliTRDtrackingChamber::kNTimeBins * AliTRDgeometry::kNplan);
230 return fgRieman;
eb38ed55 231}
232
0906e73e 233//_____________________________________________________________________________
234Int_t AliTRDtrackerV1::PropagateBack(AliESDEvent *event)
235{
972ef65e 236 //
237 // Gets seeds from ESD event. The seeds are AliTPCtrack's found and
238 // backpropagated by the TPC tracker. Each seed is first propagated
239 // to the TRD, and then its prolongation is searched in the TRD.
240 // If sufficiently long continuation of the track is found in the TRD
241 // the track is updated, otherwise it's stored as originaly defined
242 // by the TPC tracker.
243 //
244
245 // Calibration monitor
246 AliTRDCalibraFillHisto *calibra = AliTRDCalibraFillHisto::Instance();
247 if (!calibra) AliInfo("Could not get Calibra instance\n");
248
249 Int_t found = 0; // number of tracks found
250 Float_t foundMin = 20.0;
251
252 Int_t nSeed = event->GetNumberOfTracks();
253 if(!nSeed){
254 // run stand alone tracking
255 if (AliTRDReconstructor::SeedingOn()) Clusters2Tracks(event);
256 return 0;
257 }
258
259 Float_t *quality = new Float_t[nSeed];
260 Int_t *index = new Int_t[nSeed];
261 for (Int_t iSeed = 0; iSeed < nSeed; iSeed++) {
262 AliESDtrack *seed = event->GetTrack(iSeed);
263 Double_t covariance[15];
264 seed->GetExternalCovariance(covariance);
265 quality[iSeed] = covariance[0] + covariance[2];
266 }
267 // Sort tracks according to covariance of local Y and Z
268 TMath::Sort(nSeed,quality,index,kFALSE);
269
270 // Backpropagate all seeds
271 Int_t expectedClr;
272 AliTRDtrackV1 track;
273 for (Int_t iSeed = 0; iSeed < nSeed; iSeed++) {
274
275 // Get the seeds in sorted sequence
276 AliESDtrack *seed = event->GetTrack(index[iSeed]);
277
278 // Check the seed status
279 ULong_t status = seed->GetStatus();
280 if ((status & AliESDtrack::kTPCout) == 0) continue;
281 if ((status & AliESDtrack::kTRDout) != 0) continue;
282
283 // Do the back prolongation
284 Int_t lbl = seed->GetLabel();
285 new(&track) AliTRDtrackV1(*seed);
286 //track->Print();
287 track.SetSeedLabel(lbl);
288 seed->UpdateTrackParams(&track, AliESDtrack::kTRDbackup); // Make backup
289 Float_t p4 = track.GetC();
290 if((expectedClr = FollowBackProlongation(track))){
291 // computes PID for track
292 track.CookPID();
293 // update calibration references using this track
294 if(calibra->GetHisto2d()) calibra->UpdateHistogramsV1(&track);
0c23ac49 295 // save calibration object
296 if ((track.GetNumberOfClusters() > 15) && (track.GetNumberOfClusters() > 0.5*expectedClr)) {
297 seed->UpdateTrackParams(&track, AliESDtrack::kTRDout);
298
299 track.UpdateESDtrack(seed);
300
301 // Add TRD track to ESDfriendTrack
302 if (AliTRDReconstructor::StreamLevel() > 0 /*&& quality TODO*/){
303 AliTRDtrackV1 *calibTrack = new AliTRDtrackV1(track);
304 calibTrack->SetOwner();
305 seed->AddCalibObject(calibTrack);
306 }
307 }
972ef65e 308 }
309
310 if ((TMath::Abs(track.GetC() - p4) / TMath::Abs(p4) < 0.2) ||(track.Pt() > 0.8)) {
311 //
312 // Make backup for back propagation
313 //
314 Int_t foundClr = track.GetNumberOfClusters();
315 if (foundClr >= foundMin) {
316 //AliInfo(Form("Making backup track ncls [%d]...", foundClr));
317 track.CookdEdx();
318 track.CookdEdxTimBin(seed->GetID());
319 track.CookLabel(1. - fgkLabelFraction);
320 if(track.GetBackupTrack()) UseClusters(track.GetBackupTrack());
0906e73e 321
0906e73e 322
972ef65e 323 // Sign only gold tracks
324 if (track.GetChi2() / track.GetNumberOfClusters() < 4) {
325 if ((seed->GetKinkIndex(0) == 0) && (track.Pt() < 1.5)) UseClusters(&track);
326 }
327 Bool_t isGold = kFALSE;
0906e73e 328
972ef65e 329 // Full gold track
330 if (track.GetChi2() / track.GetNumberOfClusters() < 5) {
331 if (track.GetBackupTrack()) seed->UpdateTrackParams(track.GetBackupTrack(),AliESDtrack::kTRDbackup);
0906e73e 332
972ef65e 333 isGold = kTRUE;
334 }
0906e73e 335
972ef65e 336 // Almost gold track
337 if ((!isGold) && (track.GetNCross() == 0) && (track.GetChi2() / track.GetNumberOfClusters() < 7)) {
338 //seed->UpdateTrackParams(track, AliESDtrack::kTRDbackup);
339 if (track.GetBackupTrack()) seed->UpdateTrackParams(track.GetBackupTrack(),AliESDtrack::kTRDbackup);
340
341 isGold = kTRUE;
342 }
0906e73e 343
972ef65e 344 if ((!isGold) && (track.GetBackupTrack())) {
345 if ((track.GetBackupTrack()->GetNumberOfClusters() > foundMin) && ((track.GetBackupTrack()->GetChi2()/(track.GetBackupTrack()->GetNumberOfClusters()+1)) < 7)) {
346 seed->UpdateTrackParams(track.GetBackupTrack(),AliESDtrack::kTRDbackup);
347 isGold = kTRUE;
348 }
349 }
350
351 //if ((track->StatusForTOF() > 0) && (track->GetNCross() == 0) && (Float_t(track->GetNumberOfClusters()) / Float_t(track->GetNExpected()) > 0.4)) {
352 //seed->UpdateTrackParams(track->GetBackupTrack(), AliESDtrack::kTRDbackup);
353 //}
354 }
355 }
0c23ac49 356
972ef65e 357 // Propagation to the TOF (I.Belikov)
358 if (track.GetStop() == kFALSE) {
359 Double_t xtof = 371.0;
360 Double_t xTOF0 = 370.0;
0906e73e 361
972ef65e 362 Double_t c2 = track.GetSnp() + track.GetC() * (xtof - track.GetX());
363 if (TMath::Abs(c2) >= 0.99) continue;
0906e73e 364
972ef65e 365 PropagateToX(track, xTOF0, fgkMaxStep);
0906e73e 366
972ef65e 367 // Energy losses taken to the account - check one more time
368 c2 = track.GetSnp() + track.GetC() * (xtof - track.GetX());
369 if (TMath::Abs(c2) >= 0.99) continue;
0906e73e 370
972ef65e 371 //if (!PropagateToX(*track,xTOF0,fgkMaxStep)) {
372 // fHBackfit->Fill(7);
373 //delete track;
374 // continue;
375 //}
376
377 Double_t ymax = xtof * TMath::Tan(0.5 * AliTRDgeometry::GetAlpha());
378 Double_t y;
379 track.GetYAt(xtof,GetBz(),y);
380 if (y > ymax) {
0c23ac49 381 if (!track.Rotate( AliTRDgeometry::GetAlpha())) continue;
972ef65e 382 }else if (y < -ymax) {
383 if (!track.Rotate(-AliTRDgeometry::GetAlpha())) continue;
384 }
0906e73e 385
972ef65e 386 if (track.PropagateTo(xtof)) {
387 seed->UpdateTrackParams(&track, AliESDtrack::kTRDout);
388 track.UpdateESDtrack(seed);
a23edf15 389
972ef65e 390 // Add TRD track to ESDfriendTrack
0c23ac49 391// if (AliTRDReconstructor::StreamLevel() > 0 /*&& quality TODO*/){
392// AliTRDtrackV1 *calibTrack = new AliTRDtrackV1(track);
393// calibTrack->SetOwner();
394// seed->AddCalibObject(calibTrack);
395// }
972ef65e 396 found++;
397 }
398 } else {
399 if ((track.GetNumberOfClusters() > 15) && (track.GetNumberOfClusters() > 0.5*expectedClr)) {
400 seed->UpdateTrackParams(&track, AliESDtrack::kTRDout);
0906e73e 401
972ef65e 402 track.UpdateESDtrack(seed);
a23edf15 403
972ef65e 404 // Add TRD track to ESDfriendTrack
0c23ac49 405// if (AliTRDReconstructor::StreamLevel() > 0 /*&& quality TODO*/){
406// AliTRDtrackV1 *calibTrack = new AliTRDtrackV1(track);
407// calibTrack->SetOwner();
408// seed->AddCalibObject(calibTrack);
409// }
972ef65e 410 found++;
411 }
412 }
d739fd4e 413
972ef65e 414 seed->SetTRDQuality(track.StatusForTOF());
415 seed->SetTRDBudget(track.GetBudget(0));
416 }
0906e73e 417
418
972ef65e 419 AliInfo(Form("Number of seeds: %d", nSeed));
420 AliInfo(Form("Number of back propagated TRD tracks: %d", found));
eb38ed55 421
972ef65e 422 delete [] index;
423 delete [] quality;
0906e73e 424
972ef65e 425 return 0;
0906e73e 426}
427
428
429//____________________________________________________________________
430Int_t AliTRDtrackerV1::RefitInward(AliESDEvent *event)
431{
972ef65e 432 //
433 // Refits tracks within the TRD. The ESD event is expected to contain seeds
434 // at the outer part of the TRD.
435 // The tracks are propagated to the innermost time bin
436 // of the TRD and the ESD event is updated
437 // Origin: Thomas KUHR (Thomas.Kuhr@cern.ch)
438 //
439
440 Int_t nseed = 0; // contor for loaded seeds
441 Int_t found = 0; // contor for updated TRD tracks
442
443
444 AliTRDtrackV1 track;
445 for (Int_t itrack = 0; itrack < event->GetNumberOfTracks(); itrack++) {
446 AliESDtrack *seed = event->GetTrack(itrack);
447 new(&track) AliTRDtrackV1(*seed);
448
449 if (track.GetX() < 270.0) {
450 seed->UpdateTrackParams(&track, AliESDtrack::kTRDbackup);
451 continue;
452 }
453
454 ULong_t status = seed->GetStatus();
455 if((status & AliESDtrack::kTRDout) == 0) continue;
456 if((status & AliESDtrack::kTRDin) != 0) continue;
457 nseed++;
458
459 track.ResetCovariance(50.0);
460
461 // do the propagation and processing
462 Bool_t kUPDATE = kFALSE;
463 Double_t xTPC = 250.0;
464 if(FollowProlongation(track)){
465 // Prolongate to TPC
466 if (PropagateToX(track, xTPC, fgkMaxStep)) { // -with update
d739fd4e 467 seed->UpdateTrackParams(&track, AliESDtrack::kTRDrefit);
468 found++;
469 kUPDATE = kTRUE;
972ef65e 470 }
471 }
bcb6fb78 472
972ef65e 473 // Prolongate to TPC without update
474 if(!kUPDATE) {
475 AliTRDtrackV1 tt(*seed);
476 if (PropagateToX(tt, xTPC, fgkMaxStep)) seed->UpdateTrackParams(&tt, AliESDtrack::kTRDrefit);
477 }
478 }
479 AliInfo(Form("Number of loaded seeds: %d",nseed));
480 AliInfo(Form("Number of found tracks from loaded seeds: %d",found));
481
482 return 0;
0906e73e 483}
484
485
486//____________________________________________________________________
487Int_t AliTRDtrackerV1::FollowProlongation(AliTRDtrackV1 &t)
488{
972ef65e 489 // Extrapolates the TRD track in the TPC direction.
490 //
491 // Parameters
492 // t : the TRD track which has to be extrapolated
493 //
494 // Output
495 // number of clusters attached to the track
496 //
497 // Detailed description
498 //
499 // Starting from current radial position of track <t> this function
500 // extrapolates the track through the 6 TRD layers. The following steps
501 // are being performed for each plane:
502 // 1. prepare track:
503 // a. get plane limits in the local x direction
504 // b. check crossing sectors
505 // c. check track inclination
506 // 2. search tracklet in the tracker list (see GetTracklet() for details)
507 // 3. evaluate material budget using the geo manager
508 // 4. propagate and update track using the tracklet information.
509 //
510 // Debug level 2
511 //
512
513 Int_t nClustersExpected = 0;
514 Int_t lastplane = 5; //GetLastPlane(&t);
515 for (Int_t iplane = lastplane; iplane >= 0; iplane--) {
516 Int_t index = 0;
517 AliTRDseedV1 *tracklet = GetTracklet(&t, iplane, index);
518 if(!tracklet) continue;
519 if(!tracklet->IsOK()) AliWarning("tracklet not OK");
eb38ed55 520
972ef65e 521 Double_t x = tracklet->GetX0();
522 // reject tracklets which are not considered for inward refit
523 if(x > t.GetX()+fgkMaxStep) continue;
a23edf15 524
972ef65e 525 // append tracklet to track
526 t.SetTracklet(tracklet, iplane, index);
527
528 if (x < (t.GetX()-fgkMaxStep) && !PropagateToX(t, x+fgkMaxStep, fgkMaxStep)) break;
529 if (!AdjustSector(&t)) break;
eb38ed55 530
972ef65e 531 // Start global position
532 Double_t xyz0[3];
533 t.GetXYZ(xyz0);
534
535 // End global position
536 Double_t alpha = t.GetAlpha(), y, z;
537 if (!t.GetProlongation(x,y,z)) break;
538 Double_t xyz1[3];
539 xyz1[0] = x * TMath::Cos(alpha) - y * TMath::Sin(alpha);
540 xyz1[1] = x * TMath::Sin(alpha) + y * TMath::Cos(alpha);
541 xyz1[2] = z;
eb38ed55 542
972ef65e 543 // Get material budget
544 Double_t param[7];
545 AliTracker::MeanMaterialBudget(xyz0, xyz1, param);
546 Double_t xrho= param[0]*param[4];
547 Double_t xx0 = param[1]; // Get mean propagation parameters
548
549 // Propagate and update
550 t.PropagateTo(x, xx0, xrho);
551 if (!AdjustSector(&t)) break;
552
553 Double_t maxChi2 = t.GetPredictedChi2(tracklet);
554 if (maxChi2 < 1e+10 && t.Update(tracklet, maxChi2)){
555 nClustersExpected += tracklet->GetN();
556 }
557 }
558
559 if(AliTRDReconstructor::StreamLevel() > 1){
560 Int_t index;
561 for(int iplane=0; iplane<6; iplane++){
562 AliTRDseedV1 *tracklet = GetTracklet(&t, iplane, index);
563 if(!tracklet) continue;
564 t.SetTracklet(tracklet, iplane, index);
565 }
566
567 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
568 TTreeSRedirector &cstreamer = *fgDebugStreamer;
569 cstreamer << "FollowProlongation"
570 << "EventNumber=" << eventNumber
571 << "ncl=" << nClustersExpected
572 << "track.=" << &t
573 << "\n";
574 }
575
576 return nClustersExpected;
0906e73e 577
578}
579
580//_____________________________________________________________________________
581Int_t AliTRDtrackerV1::FollowBackProlongation(AliTRDtrackV1 &t)
582{
972ef65e 583 // Extrapolates the TRD track in the TOF direction.
584 //
585 // Parameters
586 // t : the TRD track which has to be extrapolated
587 //
588 // Output
589 // number of clusters attached to the track
590 //
591 // Detailed description
592 //
593 // Starting from current radial position of track <t> this function
594 // extrapolates the track through the 6 TRD layers. The following steps
595 // are being performed for each plane:
596 // 1. prepare track:
597 // a. get plane limits in the local x direction
598 // b. check crossing sectors
599 // c. check track inclination
600 // 2. build tracklet (see AliTRDseed::AttachClusters() for details)
601 // 3. evaluate material budget using the geo manager
602 // 4. propagate and update track using the tracklet information.
603 //
604 // Debug level 2
605 //
606
607 Int_t nClustersExpected = 0;
608 Double_t clength = AliTRDgeometry::AmThick() + AliTRDgeometry::DrThick();
609 AliTRDtrackingChamber *chamber = 0x0;
610
611 // Loop through the TRD planes
612 for (Int_t iplane = 0; iplane < AliTRDgeometry::Nplan(); iplane++) {
613 // BUILD TRACKLET IF NOT ALREADY BUILT
614 Double_t x = 0., y, z, alpha;
615 AliTRDseedV1 tracklet(*t.GetTracklet(iplane));
616 if(!tracklet.IsOK()){
617 alpha = t.GetAlpha();
618 Int_t sector = Int_t(alpha/AliTRDgeometry::GetAlpha() + (alpha>0. ? 0 : AliTRDgeometry::kNsect));
619
620 if(!fTrSec[sector].GetNChambers()) continue;
621
622 if((x = fTrSec[sector].GetX(iplane)) < 1.) continue;
eb38ed55 623
972ef65e 624 if (!t.GetProlongation(x, y, z)) break;
625 Int_t stack = fGeom->GetChamber(z, iplane);
626 Int_t nCandidates = stack >= 0 ? 1 : 2;
627 z -= stack >= 0 ? 0. : 4.;
eb38ed55 628
972ef65e 629 for(int icham=0; icham<nCandidates; icham++, z+=8){
51863bc0 630 if((stack = fGeom->GetChamber(z, iplane)) < 0) continue;
972ef65e 631
51863bc0 632 if(!(chamber = fTrSec[sector].GetChamber(stack, iplane))) continue;
972ef65e 633
51863bc0 634 if(chamber->GetNClusters() < fgNTimeBins*AliTRDReconstructor::RecoParam()->GetFindableClusters()) continue;
972ef65e 635
51863bc0 636 x = chamber->GetX();
972ef65e 637
51863bc0 638 AliTRDpadPlane *pp = fGeom->GetPadPlane(iplane, stack);
639 tracklet.SetTilt(TMath::Tan(-TMath::DegToRad()*pp->GetTiltingAngle()));
640 tracklet.SetPadLength(pp->GetLengthIPad());
641 tracklet.SetPlane(iplane);
642 tracklet.SetX0(x);
0c23ac49 643 if(!tracklet.Init(&t)){
644 t.SetStop(kTRUE);
645 return nClustersExpected;
646 }
51863bc0 647 if(!tracklet.AttachClustersIter(chamber, 1000.)) continue;
648 tracklet.Init(&t);
972ef65e 649
51863bc0 650 if(tracklet.GetN() < fgNTimeBins * AliTRDReconstructor::RecoParam()->GetFindableClusters()) continue;
972ef65e 651
51863bc0 652 break;
653 }
972ef65e 654 }
655 if(!tracklet.IsOK()){
656 if(x < 1.) continue; //temporary
657 if(!PropagateToX(t, x-fgkMaxStep, fgkMaxStep)) break;
658 if(!AdjustSector(&t)) break;
659 if(TMath::Abs(t.GetSnp()) > fgkMaxSnp) break;
660 continue;
661 }
0906e73e 662
972ef65e 663 // Propagate closer to the current chamber if neccessary
664 x -= clength;
665 if (x > (fgkMaxStep + t.GetX()) && !PropagateToX(t, x-fgkMaxStep, fgkMaxStep)) break;
666 if (!AdjustSector(&t)) break;
667 if (TMath::Abs(t.GetSnp()) > fgkMaxSnp) break;
0906e73e 668
972ef65e 669 // load tracklet to the tracker and the track
670 Int_t index = SetTracklet(&tracklet);
671 t.SetTracklet(&tracklet, iplane, index);
672
673
674 // Calculate the mean material budget along the path inside the chamber
675 //Calculate global entry and exit positions of the track in chamber (only track prolongation)
676 Double_t xyz0[3]; // entry point
677 t.GetXYZ(xyz0);
678 alpha = t.GetAlpha();
679 x = tracklet.GetX0();
680 if (!t.GetProlongation(x, y, z)) break;
681 Double_t xyz1[3]; // exit point
682 xyz1[0] = x * TMath::Cos(alpha) - y * TMath::Sin(alpha);
683 xyz1[1] = +x * TMath::Sin(alpha) + y * TMath::Cos(alpha);
684 xyz1[2] = z;
685 Double_t param[7];
686 AliTracker::MeanMaterialBudget(xyz0, xyz1, param);
687 // The mean propagation parameters
688 Double_t xrho = param[0]*param[4]; // density*length
689 Double_t xx0 = param[1]; // radiation length
690
691 // Propagate and update track
692 t.PropagateTo(x, xx0, xrho);
693 if (!AdjustSector(&t)) break;
694 Double_t maxChi2 = t.GetPredictedChi2(&tracklet);
695 if (maxChi2<1e+10 && t.Update(&tracklet, maxChi2)){
696 nClustersExpected += tracklet.GetN();
697 t.SetTracklet(&tracklet, iplane, index);
698 UpdateTracklet(&tracklet, index);
699 }
700 // Reset material budget if 2 consecutive gold
701 if(iplane>0 && tracklet.GetN() + t.GetTracklet(iplane-1)->GetN() > 20) t.SetBudget(2, 0.);
702
703 // Make backup of the track until is gold
704 // TO DO update quality check of the track.
705 // consider comparison with fTimeBinsRange
706 Float_t ratio0 = tracklet.GetN() / Float_t(fgNTimeBins);
707 //Float_t ratio1 = Float_t(t.GetNumberOfClusters()+1) / Float_t(t.GetNExpected()+1);
708 //printf("tracklet.GetChi2() %f [< 18.0]\n", tracklet.GetChi2());
709 //printf("ratio0 %f [> 0.8]\n", ratio0);
710 //printf("ratio1 %f [> 0.6]\n", ratio1);
711 //printf("ratio0+ratio1 %f [> 1.5]\n", ratio0+ratio1);
712 //printf("t.GetNCross() %d [== 0]\n", t.GetNCross());
713 //printf("TMath::Abs(t.GetSnp()) %f [< 0.85]\n", TMath::Abs(t.GetSnp()));
714 //printf("t.GetNumberOfClusters() %d [> 20]\n", t.GetNumberOfClusters());
0906e73e 715
972ef65e 716 if (//(tracklet.GetChi2() < 18.0) && TO DO check with FindClusters and move it to AliTRDseed::Update
717 (ratio0 > 0.8) &&
718 //(ratio1 > 0.6) &&
719 //(ratio0+ratio1 > 1.5) &&
720 (t.GetNCross() == 0) &&
721 (TMath::Abs(t.GetSnp()) < 0.85) &&
722 (t.GetNumberOfClusters() > 20)) t.MakeBackupTrack();
723
724 } // end planes loop
725
726 if(AliTRDReconstructor::StreamLevel() > 1){
727 TTreeSRedirector &cstreamer = *fgDebugStreamer;
728 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
729 cstreamer << "FollowBackProlongation"
730 << "EventNumber=" << eventNumber
731 << "ncl=" << nClustersExpected
732 << "track.=" << &t
733 << "\n";
734 }
735
736 return nClustersExpected;
0906e73e 737}
738
eb38ed55 739//_________________________________________________________________________
740Float_t AliTRDtrackerV1::FitRieman(AliTRDseedV1 *tracklets, Double_t *chi2, Int_t *planes){
972ef65e 741 //
742 // Fits a Riemann-circle to the given points without tilting pad correction.
743 // The fit is performed using an instance of the class AliRieman (equations
744 // and transformations see documentation of this class)
745 // Afterwards all the tracklets are Updated
746 //
747 // Parameters: - Array of tracklets (AliTRDseedV1)
748 // - Storage for the chi2 values (beginning with direction z)
749 // - Seeding configuration
750 // Output: - The curvature
751 //
752 AliRieman *fitter = AliTRDtrackerV1::GetRiemanFitter();
753 fitter->Reset();
754 Int_t allplanes[] = {0, 1, 2, 3, 4, 5};
755 Int_t *ppl = &allplanes[0];
756 Int_t maxLayers = 6;
757 if(planes){
758 maxLayers = 4;
759 ppl = planes;
760 }
761 for(Int_t il = 0; il < maxLayers; il++){
762 if(!tracklets[ppl[il]].IsOK()) continue;
763 fitter->AddPoint(tracklets[ppl[il]].GetX0(), tracklets[ppl[il]].GetYfitR(0), tracklets[ppl[il]].GetZProb(),1,10);
764 }
765 fitter->Update();
766 // Set the reference position of the fit and calculate the chi2 values
767 memset(chi2, 0, sizeof(Double_t) * 2);
768 for(Int_t il = 0; il < maxLayers; il++){
769 // Reference positions
770 tracklets[ppl[il]].Init(fitter);
eb38ed55 771
972ef65e 772 // chi2
773 if((!tracklets[ppl[il]].IsOK()) && (!planes)) continue;
774 chi2[0] += tracklets[ppl[il]].GetChi2Y();
775 chi2[1] += tracklets[ppl[il]].GetChi2Z();
776 }
777 return fitter->GetC();
eb38ed55 778}
779
780//_________________________________________________________________________
781void AliTRDtrackerV1::FitRieman(AliTRDcluster **seedcl, Double_t chi2[2])
782{
972ef65e 783 //
784 // Performs a Riemann helix fit using the seedclusters as spacepoints
785 // Afterwards the chi2 values are calculated and the seeds are updated
786 //
787 // Parameters: - The four seedclusters
788 // - The tracklet array (AliTRDseedV1)
789 // - The seeding configuration
790 // - Chi2 array
791 //
792 // debug level 2
793 //
794 AliRieman *fitter = AliTRDtrackerV1::GetRiemanFitter();
795 fitter->Reset();
796 for(Int_t i = 0; i < 4; i++)
797 fitter->AddPoint(seedcl[i]->GetX(), seedcl[i]->GetY(), seedcl[i]->GetZ(), 1, 10);
798 fitter->Update();
799
800
801 // Update the seed and calculated the chi2 value
802 chi2[0] = 0; chi2[1] = 0;
803 for(Int_t ipl = 0; ipl < kNSeedPlanes; ipl++){
804 // chi2
805 chi2[0] += (seedcl[ipl]->GetZ() - fitter->GetZat(seedcl[ipl]->GetX())) * (seedcl[ipl]->GetZ() - fitter->GetZat(seedcl[ipl]->GetX()));
806 chi2[1] += (seedcl[ipl]->GetY() - fitter->GetYat(seedcl[ipl]->GetX())) * (seedcl[ipl]->GetY() - fitter->GetYat(seedcl[ipl]->GetX()));
807 }
eb38ed55 808}
809
810
811//_________________________________________________________________________
812Float_t AliTRDtrackerV1::FitTiltedRiemanConstraint(AliTRDseedV1 *tracklets, Double_t zVertex)
813{
972ef65e 814 //
815 // Fits a helix to the clusters. Pad tilting is considered. As constraint it is
816 // assumed that the vertex position is set to 0.
817 // This method is very usefull for high-pt particles
818 // Basis for the fit: (x - x0)^2 + (y - y0)^2 - R^2 = 0
819 // x0, y0: Center of the circle
820 // Measured y-position: ymeas = y - tan(phiT)(zc - zt)
821 // zc: center of the pad row
822 // Equation which has to be fitted (after transformation):
823 // a + b * u + e * v + 2*(ymeas + tan(phiT)(z - zVertex))*t = 0
824 // Transformation:
825 // t = 1/(x^2 + y^2)
826 // u = 2 * x * t
827 // v = 2 * x * tan(phiT) * t
828 // Parameters in the equation:
829 // a = -1/y0, b = x0/y0, e = dz/dx
830 //
831 // The Curvature is calculated by the following equation:
832 // - curv = a/Sqrt(b^2 + 1) = 1/R
833 // Parameters: - the 6 tracklets
834 // - the Vertex constraint
835 // Output: - the Chi2 value of the track
836 //
837 // debug level 5
838 //
839
840 TLinearFitter *fitter = GetTiltedRiemanFitterConstraint();
841 fitter->StoreData(kTRUE);
842 fitter->ClearPoints();
843 AliTRDcluster *cl = 0x0;
844
845 Float_t x, y, z, w, t, error, tilt;
846 Double_t uvt[2];
847 Int_t nPoints = 0;
848 for(Int_t ipl = 0; ipl < AliTRDgeometry::kNplan; ipl++){
849 if(!tracklets[ipl].IsOK()) continue;
850 for(Int_t itb = 0; itb < fgNTimeBins; itb++){
851 if(!tracklets[ipl].IsUsable(itb)) continue;
852 cl = tracklets[ipl].GetClusters(itb);
853 x = cl->GetX();
854 y = cl->GetY();
855 z = cl->GetZ();
856 tilt = tracklets[ipl].GetTilt();
857 // Transformation
858 t = 1./(x * x + y * y);
859 uvt[0] = 2. * x * t;
860 uvt[1] = 2. * x * t * tilt ;
861 w = 2. * (y + tilt * (z - zVertex)) * t;
862 error = 2. * 0.2 * t;
863 fitter->AddPoint(uvt, w, error);
864 nPoints++;
865 }
866 }
867 fitter->Eval();
868
869 // Calculate curvature
870 Double_t a = fitter->GetParameter(0);
871 Double_t b = fitter->GetParameter(1);
872 Double_t curvature = a/TMath::Sqrt(b*b + 1);
873
874 Float_t chi2track = fitter->GetChisquare()/Double_t(nPoints);
875 for(Int_t ip = 0; ip < AliTRDtrackerV1::kNPlanes; ip++)
876 tracklets[ip].SetCC(curvature);
877
878 if(AliTRDReconstructor::StreamLevel() >= 5){
879 //Linear Model on z-direction
880 Double_t xref = CalculateReferenceX(tracklets); // Relative to the middle of the stack
881 Double_t slope = fitter->GetParameter(2);
882 Double_t zref = slope * xref;
883 Float_t chi2Z = CalculateChi2Z(tracklets, zref, slope, xref);
884 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
885 Int_t candidateNumber = AliTRDtrackerDebug::GetCandidateNumber();
886 TTreeSRedirector &treeStreamer = *fgDebugStreamer;
887 treeStreamer << "FitTiltedRiemanConstraint"
888 << "EventNumber=" << eventNumber
889 << "CandidateNumber=" << candidateNumber
890 << "Curvature=" << curvature
891 << "Chi2Track=" << chi2track
892 << "Chi2Z=" << chi2Z
893 << "zref=" << zref
894 << "\n";
895 }
896 return chi2track;
eb38ed55 897}
898
899//_________________________________________________________________________
900Float_t AliTRDtrackerV1::FitTiltedRieman(AliTRDseedV1 *tracklets, Bool_t sigError)
901{
972ef65e 902 //
903 // Performs a Riemann fit taking tilting pad correction into account
904 // The equation of a Riemann circle, where the y position is substituted by the
905 // measured y-position taking pad tilting into account, has to be transformed
906 // into a 4-dimensional hyperplane equation
907 // Riemann circle: (x-x0)^2 + (y-y0)^2 -R^2 = 0
908 // Measured y-Position: ymeas = y - tan(phiT)(zc - zt)
909 // zc: center of the pad row
910 // zt: z-position of the track
911 // The z-position of the track is assumed to be linear dependent on the x-position
912 // Transformed equation: a + b * u + c * t + d * v + e * w - 2 * (ymeas + tan(phiT) * zc) * t = 0
913 // Transformation: u = 2 * x * t
914 // v = 2 * tan(phiT) * t
915 // w = 2 * tan(phiT) * (x - xref) * t
916 // t = 1 / (x^2 + ymeas^2)
917 // Parameters: a = -1/y0
918 // b = x0/y0
919 // c = (R^2 -x0^2 - y0^2)/y0
920 // d = offset
921 // e = dz/dx
922 // If the offset respectively the slope in z-position is impossible, the parameters are fixed using
923 // results from the simple riemann fit. Afterwards the fit is redone.
924 // The curvature is calculated according to the formula:
925 // curv = a/(1 + b^2 + c*a) = 1/R
926 //
927 // Paramters: - Array of tracklets (connected to the track candidate)
928 // - Flag selecting the error definition
929 // Output: - Chi2 values of the track (in Parameter list)
930 //
931 TLinearFitter *fitter = GetTiltedRiemanFitter();
932 fitter->StoreData(kTRUE);
933 fitter->ClearPoints();
934 AliTRDLeastSquare zfitter;
935 AliTRDcluster *cl = 0x0;
936
937 Double_t xref = CalculateReferenceX(tracklets);
938 Double_t x, y, z, t, tilt, dx, w, we;
939 Double_t uvt[4];
940 Int_t nPoints = 0;
941 // Containers for Least-square fitter
942 for(Int_t ipl = 0; ipl < kNPlanes; ipl++){
943 if(!tracklets[ipl].IsOK()) continue;
944 for(Int_t itb = 0; itb < fgNTimeBins; itb++){
945 if(!(cl = tracklets[ipl].GetClusters(itb))) continue;
946 if (!tracklets[ipl].IsUsable(itb)) continue;
947 x = cl->GetX();
948 y = cl->GetY();
949 z = cl->GetZ();
950 tilt = tracklets[ipl].GetTilt();
951 dx = x - xref;
952 // Transformation
953 t = 1./(x*x + y*y);
954 uvt[0] = 2. * x * t;
955 uvt[1] = t;
956 uvt[2] = 2. * tilt * t;
957 uvt[3] = 2. * tilt * dx * t;
958 w = 2. * (y + tilt*z) * t;
959 // error definition changes for the different calls
960 we = 2. * t;
961 we *= sigError ? tracklets[ipl].GetSigmaY() : 0.2;
962 fitter->AddPoint(uvt, w, we);
963 zfitter.AddPoint(&x, z, static_cast<Double_t>(TMath::Sqrt(cl->GetSigmaZ2())));
964 nPoints++;
965 }
966 }
967 fitter->Eval();
968 zfitter.Eval();
969
970 Double_t offset = fitter->GetParameter(3);
971 Double_t slope = fitter->GetParameter(4);
972
973 // Linear fitter - not possible to make boundaries
974 // Do not accept non possible z and dzdx combinations
975 Bool_t acceptablez = kTRUE;
976 Double_t zref = 0.0;
977 for (Int_t iLayer = 0; iLayer < kNPlanes; iLayer++) {
978 if(!tracklets[iLayer].IsOK()) continue;
979 zref = offset + slope * (tracklets[iLayer].GetX0() - xref);
980 if (TMath::Abs(tracklets[iLayer].GetZProb() - zref) > tracklets[iLayer].GetPadLength() * 0.5 + 1.0)
981 acceptablez = kFALSE;
982 }
983 if (!acceptablez) {
984 Double_t dzmf = zfitter.GetFunctionParameter(1);
985 Double_t zmf = zfitter.GetFunctionValue(&xref);
986 fgTiltedRieman->FixParameter(3, zmf);
987 fgTiltedRieman->FixParameter(4, dzmf);
988 fitter->Eval();
989 fitter->ReleaseParameter(3);
990 fitter->ReleaseParameter(4);
991 offset = fitter->GetParameter(3);
992 slope = fitter->GetParameter(4);
993 }
994
995 // Calculate Curvarture
996 Double_t a = fitter->GetParameter(0);
997 Double_t b = fitter->GetParameter(1);
998 Double_t c = fitter->GetParameter(2);
999 Double_t curvature = 1.0 + b*b - c*a;
1000 if (curvature > 0.0)
1001 curvature = a / TMath::Sqrt(curvature);
1002
1003 Double_t chi2track = fitter->GetChisquare()/Double_t(nPoints);
1004
1005 // Update the tracklets
1006 Double_t dy, dz;
1007 for(Int_t iLayer = 0; iLayer < AliTRDtrackerV1::kNPlanes; iLayer++) {
1008
1009 x = tracklets[iLayer].GetX0();
1010 y = 0;
1011 z = 0;
1012 dy = 0;
1013 dz = 0;
1014
1015 // y: R^2 = (x - x0)^2 + (y - y0)^2
1016 // => y = y0 +/- Sqrt(R^2 - (x - x0)^2)
1017 // R = Sqrt() = 1/Curvature
1018 // => y = y0 +/- Sqrt(1/Curvature^2 - (x - x0)^2)
1019 Double_t res = (x * a + b); // = (x - x0)/y0
1020 res *= res;
1021 res = 1.0 - c * a + b * b - res; // = (R^2 - (x - x0)^2)/y0^2
1022 if (res >= 0) {
1023 res = TMath::Sqrt(res);
1024 y = (1.0 - res) / a;
1025 }
1026
1027 // dy: R^2 = (x - x0)^2 + (y - y0)^2
1028 // => y = +/- Sqrt(R^2 - (x - x0)^2) + y0
1029 // => dy/dx = (x - x0)/Sqrt(R^2 - (x - x0)^2)
1030 // Curvature: cr = 1/R = a/Sqrt(1 + b^2 - c*a)
1031 // => dy/dx = (x - x0)/(1/(cr^2) - (x - x0)^2)
1032 Double_t x0 = -b / a;
1033 if (-c * a + b * b + 1 > 0) {
1034 if (1.0/(curvature * curvature) - (x - x0) * (x - x0) > 0.0) {
d739fd4e 1035 Double_t yderiv = (x - x0) / TMath::Sqrt(1.0/(curvature * curvature) - (x - x0) * (x - x0));
1036 if (a < 0) yderiv *= -1.0;
1037 dy = yderiv;
972ef65e 1038 }
1039 }
1040 z = offset + slope * (x - xref);
1041 dz = slope;
1042 tracklets[iLayer].SetYref(0, y);
1043 tracklets[iLayer].SetYref(1, dy);
1044 tracklets[iLayer].SetZref(0, z);
1045 tracklets[iLayer].SetZref(1, dz);
1046 tracklets[iLayer].SetC(curvature);
1047 tracklets[iLayer].SetChi2(chi2track);
1048 }
1049
1050 if(AliTRDReconstructor::StreamLevel() >=5){
1051 TTreeSRedirector &cstreamer = *fgDebugStreamer;
1052 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
1053 Int_t candidateNumber = AliTRDtrackerDebug::GetCandidateNumber();
1054 Double_t chi2z = CalculateChi2Z(tracklets, offset, slope, xref);
1055 cstreamer << "FitTiltedRieman0"
1056 << "EventNumber=" << eventNumber
1057 << "CandidateNumber=" << candidateNumber
1058 << "xref=" << xref
1059 << "Chi2Z=" << chi2z
1060 << "\n";
1061 }
1062 return chi2track;
eb38ed55 1063}
1064
1065//_________________________________________________________________________
bb56afff 1066Float_t AliTRDtrackerV1::CalculateChi2Z(AliTRDseedV1 *tracklets, Double_t offset, Double_t slope, Double_t xref)
eb38ed55 1067{
972ef65e 1068 //
1069 // Calculates the chi2-value of the track in z-Direction including tilting pad correction.
1070 // A linear dependence on the x-value serves as a model.
1071 // The parameters are related to the tilted Riemann fit.
1072 // Parameters: - Array of tracklets (AliTRDseedV1) related to the track candidate
1073 // - the offset for the reference x
1074 // - the slope
1075 // - the reference x position
1076 // Output: - The Chi2 value of the track in z-Direction
1077 //
1078 Float_t chi2Z = 0, nLayers = 0;
1079 for (Int_t iLayer = 0; iLayer < AliTRDgeometry::kNplan; iLayer++) {
1080 if(!tracklets[iLayer].IsOK()) continue;
1081 Double_t z = offset + slope * (tracklets[iLayer].GetX0() - xref);
1082 chi2Z += TMath::Abs(tracklets[iLayer].GetMeanz() - z);
1083 nLayers++;
1084 }
1085 chi2Z /= TMath::Max((nLayers - 3.0),1.0);
1086 return chi2Z;
eb38ed55 1087}
1088
bccda319 1089//_____________________________________________________________________________
1090Int_t AliTRDtrackerV1::PropagateToX(AliTRDtrackV1 &t, Double_t xToGo, Double_t maxStep)
1091{
972ef65e 1092 //
1093 // Starting from current X-position of track <t> this function
1094 // extrapolates the track up to radial position <xToGo>.
1095 // Returns 1 if track reaches the plane, and 0 otherwise
1096 //
bccda319 1097
972ef65e 1098 const Double_t kEpsilon = 0.00001;
bccda319 1099
972ef65e 1100 // Current track X-position
1101 Double_t xpos = t.GetX();
bccda319 1102
972ef65e 1103 // Direction: inward or outward
1104 Double_t dir = (xpos < xToGo) ? 1.0 : -1.0;
bccda319 1105
972ef65e 1106 while (((xToGo - xpos) * dir) > kEpsilon) {
bccda319 1107
972ef65e 1108 Double_t xyz0[3];
1109 Double_t xyz1[3];
1110 Double_t param[7];
1111 Double_t x;
1112 Double_t y;
1113 Double_t z;
bccda319 1114
972ef65e 1115 // The next step size
1116 Double_t step = dir * TMath::Min(TMath::Abs(xToGo-xpos),maxStep);
bccda319 1117
972ef65e 1118 // Get the global position of the starting point
1119 t.GetXYZ(xyz0);
bccda319 1120
972ef65e 1121 // X-position after next step
1122 x = xpos + step;
bccda319 1123
972ef65e 1124 // Get local Y and Z at the X-position of the next step
1125 if (!t.GetProlongation(x,y,z)) {
1126 return 0; // No prolongation possible
1127 }
bccda319 1128
972ef65e 1129 // The global position of the end point of this prolongation step
1130 xyz1[0] = x * TMath::Cos(t.GetAlpha()) - y * TMath::Sin(t.GetAlpha());
1131 xyz1[1] = +x * TMath::Sin(t.GetAlpha()) + y * TMath::Cos(t.GetAlpha());
1132 xyz1[2] = z;
bccda319 1133
972ef65e 1134 // Calculate the mean material budget between start and
1135 // end point of this prolongation step
1136 AliTracker::MeanMaterialBudget(xyz0, xyz1, param);
bccda319 1137
972ef65e 1138 // Propagate the track to the X-position after the next step
1139 if (!t.PropagateTo(x,param[1],param[0]*param[4])) {
1140 return 0;
1141 }
bccda319 1142
972ef65e 1143 // Rotate the track if necessary
1144 AdjustSector(&t);
bccda319 1145
972ef65e 1146 // New track X-position
1147 xpos = t.GetX();
bccda319 1148
972ef65e 1149 }
bccda319 1150
972ef65e 1151 return 1;
bccda319 1152
1153}
1154
eb38ed55 1155
1156//_____________________________________________________________________________
1157Int_t AliTRDtrackerV1::ReadClusters(TClonesArray* &array, TTree *clusterTree) const
1158{
972ef65e 1159 //
1160 // Reads AliTRDclusters from the file.
1161 // The names of the cluster tree and branches
1162 // should match the ones used in AliTRDclusterizer::WriteClusters()
1163 //
1164
1165 Int_t nsize = Int_t(clusterTree->GetTotBytes() / (sizeof(AliTRDcluster)));
1166 TObjArray *clusterArray = new TObjArray(nsize+1000);
1167
1168 TBranch *branch = clusterTree->GetBranch("TRDcluster");
1169 if (!branch) {
1170 AliError("Can't get the branch !");
1171 return 1;
1172 }
1173 branch->SetAddress(&clusterArray);
1174
1175 if(!fClusters){
1176 array = new TClonesArray("AliTRDcluster", nsize);
1177 array->SetOwner(kTRUE);
1178 }
1179
1180 // Loop through all entries in the tree
1181 Int_t nEntries = (Int_t) clusterTree->GetEntries();
1182 Int_t nbytes = 0;
1183 Int_t ncl = 0;
1184 AliTRDcluster *c = 0x0;
1185 for (Int_t iEntry = 0; iEntry < nEntries; iEntry++) {
1186 // Import the tree
1187 nbytes += clusterTree->GetEvent(iEntry);
1188
1189 // Get the number of points in the detector
1190 Int_t nCluster = clusterArray->GetEntriesFast();
1191 for (Int_t iCluster = 0; iCluster < nCluster; iCluster++) {
1192 if(!(c = (AliTRDcluster *) clusterArray->UncheckedAt(iCluster))) continue;
1193 c->SetInChamber();
1194 new((*fClusters)[ncl++]) AliTRDcluster(*c);
87a7fa94 1195 delete (clusterArray->RemoveAt(iCluster));
972ef65e 1196 }
1197
1198 }
1199 delete clusterArray;
1200
1201 return 0;
eb38ed55 1202}
1203
1204//_____________________________________________________________________________
1205Int_t AliTRDtrackerV1::LoadClusters(TTree *cTree)
1206{
972ef65e 1207 //
1208 // Fills clusters into TRD tracking_sectors
1209 // Note that the numbering scheme for the TRD tracking_sectors
1210 // differs from that of TRD sectors
1211 //
1212
1213
1214 if (ReadClusters(fClusters, cTree)) {
1215 AliError("Problem with reading the clusters !");
1216 return 1;
1217 }
1218 Int_t ncl = fClusters->GetEntriesFast(), nin = 0;
1219 if(!ncl){
1220 AliInfo("Clusters 0");
1221 return 1;
1222 }
1223
1224 Int_t icl = ncl;
1225 while (icl--) {
1226 AliTRDcluster *c = (AliTRDcluster *) fClusters->UncheckedAt(icl);
1227 if(c->IsInChamber()) nin++;
1228 Int_t detector = c->GetDetector();
1229 Int_t sector = fGeom->GetSector(detector);
1230 Int_t stack = fGeom->GetChamber(detector);
1231 Int_t plane = fGeom->GetPlane(detector);
eb38ed55 1232
972ef65e 1233 fTrSec[sector].GetChamber(stack, plane, kTRUE)->InsertCluster(c, icl);
1234 }
1235 AliInfo(Form("Clusters %d in %6.2f %%", ncl, 100.*float(nin)/ncl));
1236
1237 for(int isector =0; isector<AliTRDgeometry::kNsect; isector++){
1238 if(!fTrSec[isector].GetNChambers()) continue;
1239 fTrSec[isector].Init();
1240 }
1241
1242 return 0;
eb38ed55 1243}
1244
1245
0906e73e 1246//____________________________________________________________________
1247void AliTRDtrackerV1::UnloadClusters()
1248{
972ef65e 1249 //
1250 // Clears the arrays of clusters and tracks. Resets sectors and timebins
1251 //
0906e73e 1252
972ef65e 1253 if(fTracks) fTracks->Delete();
1254 if(fTracklets) fTracklets->Delete();
1255 if(fClusters) fClusters->Delete();
0906e73e 1256
972ef65e 1257 for (int i = 0; i < AliTRDgeometry::kNsect; i++) fTrSec[i].Clear();
0906e73e 1258
972ef65e 1259 // Increment the Event Number
1260 AliTRDtrackerDebug::SetEventNumber(AliTRDtrackerDebug::GetEventNumber() + 1);
eb38ed55 1261}
0906e73e 1262
eb38ed55 1263//_____________________________________________________________________________
1264Bool_t AliTRDtrackerV1::AdjustSector(AliTRDtrackV1 *track)
1265{
972ef65e 1266 //
1267 // Rotates the track when necessary
1268 //
1269
1270 Double_t alpha = AliTRDgeometry::GetAlpha();
1271 Double_t y = track->GetY();
1272 Double_t ymax = track->GetX()*TMath::Tan(0.5*alpha);
1273
1274 if (y > ymax) {
1275 if (!track->Rotate( alpha)) {
1276 return kFALSE;
1277 }
1278 }
1279 else if (y < -ymax) {
1280 if (!track->Rotate(-alpha)) {
1281 return kFALSE;
1282 }
1283 }
1284
1285 return kTRUE;
0906e73e 1286
1287}
1288
eb38ed55 1289
0906e73e 1290//____________________________________________________________________
1291AliTRDseedV1* AliTRDtrackerV1::GetTracklet(AliTRDtrackV1 *track, Int_t p, Int_t &idx)
1292{
972ef65e 1293 // Find tracklet for TRD track <track>
1294 // Parameters
1295 // - track
1296 // - sector
1297 // - plane
1298 // - index
1299 // Output
1300 // tracklet
1301 // index
1302 // Detailed description
1303 //
1304 idx = track->GetTrackletIndex(p);
1305 AliTRDseedV1 *tracklet = idx<0 ? 0x0 : (AliTRDseedV1*)fTracklets->UncheckedAt(idx);
1306
1307 return tracklet;
0906e73e 1308}
1309
1310//____________________________________________________________________
bc11c056 1311Int_t AliTRDtrackerV1::SetTracklet(AliTRDseedV1 *tracklet)
0906e73e 1312{
972ef65e 1313 // Add this tracklet to the list of tracklets stored in the tracker
1314 //
1315 // Parameters
1316 // - tracklet : pointer to the tracklet to be added to the list
1317 //
1318 // Output
1319 // - the index of the new tracklet in the tracker tracklets list
1320 //
1321 // Detailed description
1322 // Build the tracklets list if it is not yet created (late initialization)
1323 // and adds the new tracklet to the list.
1324 //
1325 if(!fTracklets){
1326 fTracklets = new TClonesArray("AliTRDseedV1", AliTRDgeometry::Nsect()*kMaxTracksStack);
1327 fTracklets->SetOwner(kTRUE);
1328 }
1329 Int_t nentries = fTracklets->GetEntriesFast();
1330 new ((*fTracklets)[nentries]) AliTRDseedV1(*tracklet);
1331 return nentries;
1332}
1333
1334//____________________________________________________________________
1335Bool_t AliTRDtrackerV1::UpdateTracklet(AliTRDseedV1 *tracklet, Int_t index){
1336 //
1337 // Update Tracklet in the tracklet container
1338 //
1339 // Parameters:
1340 // - the tracklet information
1341 // - the index of the tracklet which has to be updated
1342 //
1343 // Output:
1344 // - True if successfull
1345 // - false if the container doesn't exist or the index is out of range
1346 //
1347 if(!fTracklets || index >= fTracklets->GetEntriesFast()) return kFALSE;
1348
1349 new((*fTracklets)[index]) AliTRDseedV1(*tracklet);
1350 return kTRUE;
0906e73e 1351}
1352
e4f2f73d 1353//____________________________________________________________________
eb38ed55 1354Int_t AliTRDtrackerV1::Clusters2TracksSM(Int_t sector, AliESDEvent *esd)
e4f2f73d 1355{
972ef65e 1356 //
1357 // Steer tracking for one SM.
1358 //
1359 // Parameters :
1360 // sector : Array of (SM) propagation layers containing clusters
1361 // esd : The current ESD event. On output it contains the also
1362 // the ESD (TRD) tracks found in this SM.
1363 //
1364 // Output :
1365 // Number of tracks found in this TRD supermodule.
1366 //
1367 // Detailed description
1368 //
1369 // 1. Unpack AliTRDpropagationLayers objects for each stack.
1370 // 2. Launch stack tracking.
1371 // See AliTRDtrackerV1::Clusters2TracksStack() for details.
1372 // 3. Pack results in the ESD event.
1373 //
1374
1375 // allocate space for esd tracks in this SM
1376 TClonesArray esdTrackList("AliESDtrack", 2*kMaxTracksStack);
1377 esdTrackList.SetOwner();
1378
1379 Int_t nTracks = 0;
1380 Int_t nChambers = 0;
1381 AliTRDtrackingChamber **stack = 0x0, *chamber = 0x0;
1382 for(int istack = 0; istack<AliTRDgeometry::kNcham; istack++){
1383 if(!(stack = fTrSec[sector].GetStack(istack))) continue;
1384 nChambers = 0;
1385 for(int iplane=0; iplane<AliTRDgeometry::kNplan; iplane++){
1386 if(!(chamber = stack[iplane])) continue;
1387 if(chamber->GetNClusters() < fgNTimeBins * AliTRDReconstructor::RecoParam()->GetFindableClusters()) continue;
1388 nChambers++;
1389 //AliInfo(Form("sector %d stack %d plane %d clusters %d", sector, istack, iplane, chamber->GetNClusters()));
1390 }
1391 if(nChambers < 4) continue;
1392 //AliInfo(Form("Doing stack %d", istack));
1393 nTracks += Clusters2TracksStack(stack, &esdTrackList);
1394 }
1395 //AliInfo(Form("Found %d tracks in SM %d [%d]\n", nTracks, sector, esd->GetNumberOfTracks()));
1396
1397 for(int itrack=0; itrack<nTracks; itrack++)
1398 esd->AddTrack((AliESDtrack*)esdTrackList[itrack]);
1399
1400 // Reset Track and Candidate Number
1401 AliTRDtrackerDebug::SetCandidateNumber(0);
1402 AliTRDtrackerDebug::SetTrackNumber(0);
1403 return nTracks;
e4f2f73d 1404}
1405
1406//____________________________________________________________________
eb38ed55 1407Int_t AliTRDtrackerV1::Clusters2TracksStack(AliTRDtrackingChamber **stack, TClonesArray *esdTrackList)
e4f2f73d 1408{
972ef65e 1409 //
1410 // Make tracks in one TRD stack.
1411 //
1412 // Parameters :
1413 // layer : Array of stack propagation layers containing clusters
1414 // esdTrackList : Array of ESD tracks found by the stand alone tracker.
1415 // On exit the tracks found in this stack are appended.
1416 //
1417 // Output :
1418 // Number of tracks found in this stack.
1419 //
1420 // Detailed description
1421 //
1422 // 1. Find the 3 most useful seeding chambers. See BuildSeedingConfigs() for details.
1423 // 2. Steer AliTRDtrackerV1::MakeSeeds() for 3 seeding layer configurations.
1424 // See AliTRDtrackerV1::MakeSeeds() for more details.
1425 // 3. Arrange track candidates in decreasing order of their quality
1426 // 4. Classify tracks in 5 categories according to:
1427 // a) number of layers crossed
1428 // b) track quality
1429 // 5. Sign clusters by tracks in decreasing order of track quality
1430 // 6. Build AliTRDtrack out of seeding tracklets
1431 // 7. Cook MC label
1432 // 8. Build ESD track and register it to the output list
1433 //
1434
972ef65e 1435 AliTRDtrackingChamber *chamber = 0x0;
1436 AliTRDseedV1 sseed[kMaxTracksStack*6]; // to be initialized
1437 Int_t pars[4]; // MakeSeeds parameters
1438
1439 //Double_t alpha = AliTRDgeometry::GetAlpha();
1440 //Double_t shift = .5 * alpha;
1441 Int_t configs[kNConfigs];
1442
1443 // Build initial seeding configurations
1444 Double_t quality = BuildSeedingConfigs(stack, configs);
1445 if(AliTRDReconstructor::StreamLevel() > 1){
1446 AliInfo(Form("Plane config %d %d %d Quality %f"
1447 , configs[0], configs[1], configs[2], quality));
1448 }
1449
1450 // Initialize contors
1451 Int_t ntracks, // number of TRD track candidates
1452 ntracks1, // number of registered TRD tracks/iter
1453 ntracks2 = 0; // number of all registered TRD tracks in stack
1454 fSieveSeeding = 0;
1455 do{
1456 // Loop over seeding configurations
1457 ntracks = 0; ntracks1 = 0;
1458 for (Int_t iconf = 0; iconf<3; iconf++) {
1459 pars[0] = configs[iconf];
1460 pars[1] = ntracks;
1461 ntracks = MakeSeeds(stack, &sseed[6*ntracks], pars);
1462 if(ntracks == kMaxTracksStack) break;
1463 }
1464 if(AliTRDReconstructor::StreamLevel() > 1) AliInfo(Form("Candidate TRD tracks %d in iteration %d.", ntracks, fSieveSeeding));
eb38ed55 1465
972ef65e 1466 if(!ntracks) break;
e4f2f73d 1467
972ef65e 1468 // Sort the seeds according to their quality
1469 Int_t sort[kMaxTracksStack];
1470 TMath::Sort(ntracks, fTrackQuality, sort, kTRUE);
1471
1472 // Initialize number of tracks so far and logic switches
1473 Int_t ntracks0 = esdTrackList->GetEntriesFast();
1474 Bool_t signedTrack[kMaxTracksStack];
1475 Bool_t fakeTrack[kMaxTracksStack];
1476 for (Int_t i=0; i<ntracks; i++){
1477 signedTrack[i] = kFALSE;
1478 fakeTrack[i] = kFALSE;
1479 }
1480 //AliInfo("Selecting track candidates ...");
e4f2f73d 1481
972ef65e 1482 // Sieve clusters in decreasing order of track quality
1483 Double_t trackParams[7];
1484 // AliTRDseedV1 *lseed = 0x0;
1485 Int_t jSieve = 0, candidates;
1486 do{
1487 //AliInfo(Form("\t\tITER = %i ", jSieve));
1488
1489 // Check track candidates
1490 candidates = 0;
1491 for (Int_t itrack = 0; itrack < ntracks; itrack++) {
d739fd4e 1492 Int_t trackIndex = sort[itrack];
1493 if (signedTrack[trackIndex] || fakeTrack[trackIndex]) continue;
e4f2f73d 1494
1495
d739fd4e 1496 // Calculate track parameters from tracklets seeds
1497 Int_t labelsall[1000];
1498 Int_t nlabelsall = 0;
1499 Int_t naccepted = 0;
1500 Int_t ncl = 0;
1501 Int_t nused = 0;
1502 Int_t nlayers = 0;
1503 Int_t findable = 0;
1504 for (Int_t jLayer = 0; jLayer < kNPlanes; jLayer++) {
972ef65e 1505 Int_t jseed = kNPlanes*trackIndex+jLayer;
1506 if(!sseed[jseed].IsOK()) continue;
1507 if (TMath::Abs(sseed[jseed].GetYref(0) / sseed[jseed].GetX0()) < 0.15) findable++;
1508
1509 sseed[jseed].UpdateUsed();
1510 ncl += sseed[jseed].GetN2();
1511 nused += sseed[jseed].GetNUsed();
1512 nlayers++;
1513
1514 // Cooking label
1515 for (Int_t itime = 0; itime < fgNTimeBins; itime++) {
1516 if(!sseed[jseed].IsUsable(itime)) continue;
1517 naccepted++;
1518 Int_t tindex = 0, ilab = 0;
1519 while(ilab<3 && (tindex = sseed[jseed].GetClusters(itime)->GetLabel(ilab)) >= 0){
1520 labelsall[nlabelsall++] = tindex;
1521 ilab++;
1522 }
1523 }
d739fd4e 1524 }
1525 // Filter duplicated tracks
1526 if (nused > 30){
972ef65e 1527 //printf("Skip %d nused %d\n", trackIndex, nused);
1528 fakeTrack[trackIndex] = kTRUE;
1529 continue;
d739fd4e 1530 }
1531 if (Float_t(nused)/ncl >= .25){
972ef65e 1532 //printf("Skip %d nused/ncl >= .25\n", trackIndex);
1533 fakeTrack[trackIndex] = kTRUE;
1534 continue;
d739fd4e 1535 }
e4f2f73d 1536
d739fd4e 1537 // Classify tracks
1538 Bool_t skip = kFALSE;
1539 switch(jSieve){
1540 case 0:
972ef65e 1541 if(nlayers < 6) {skip = kTRUE; break;}
1542 if(TMath::Log(1.E-9+fTrackQuality[trackIndex]) < -5.){skip = kTRUE; break;}
1543 break;
e4f2f73d 1544
d739fd4e 1545 case 1:
972ef65e 1546 if(nlayers < findable){skip = kTRUE; break;}
1547 if(TMath::Log(1.E-9+fTrackQuality[trackIndex]) < -4.){skip = kTRUE; break;}
1548 break;
e4f2f73d 1549
d739fd4e 1550 case 2:
972ef65e 1551 if ((nlayers == findable) || (nlayers == 6)) { skip = kTRUE; break;}
1552 if (TMath::Log(1.E-9+fTrackQuality[trackIndex]) < -6.0){skip = kTRUE; break;}
1553 break;
e4f2f73d 1554
d739fd4e 1555 case 3:
972ef65e 1556 if (TMath::Log(1.E-9+fTrackQuality[trackIndex]) < -5.){skip = kTRUE; break;}
1557 break;
e4f2f73d 1558
d739fd4e 1559 case 4:
972ef65e 1560 if (nlayers == 3){skip = kTRUE; break;}
1561 //if (TMath::Log(1.E-9+fTrackQuality[trackIndex]) - nused/(nlayers-3.0) < -15.0){skip = kTRUE; break;}
1562 break;
d739fd4e 1563 }
1564 if(skip){
972ef65e 1565 candidates++;
1566 //printf("REJECTED : %d [%d] nlayers %d trackQuality = %e nused %d\n", itrack, trackIndex, nlayers, fTrackQuality[trackIndex], nused);
1567 continue;
d739fd4e 1568 }
1569 signedTrack[trackIndex] = kTRUE;
e4f2f73d 1570
1571
d739fd4e 1572 // Build track label - what happens if measured data ???
1573 Int_t labels[1000];
1574 Int_t outlab[1000];
1575 Int_t nlab = 0;
1576 for (Int_t iLayer = 0; iLayer < 6; iLayer++) {
972ef65e 1577 Int_t jseed = kNPlanes*trackIndex+iLayer;
1578 if(!sseed[jseed].IsOK()) continue;
1579 for(int ilab=0; ilab<2; ilab++){
1580 if(sseed[jseed].GetLabels(ilab) < 0) continue;
1581 labels[nlab] = sseed[jseed].GetLabels(ilab);
1582 nlab++;
1583 }
d739fd4e 1584 }
1585 Freq(nlab,labels,outlab,kFALSE);
1586 Int_t label = outlab[0];
1587 Int_t frequency = outlab[1];
1588 Freq(nlabelsall,labelsall,outlab,kFALSE);
1589 Int_t label1 = outlab[0];
1590 Int_t label2 = outlab[2];
1591 Float_t fakeratio = (naccepted - outlab[1]) / Float_t(naccepted);
e4f2f73d 1592
1593
d739fd4e 1594 // Sign clusters
1595 AliTRDcluster *cl = 0x0; Int_t clusterIndex = -1;
1596 for (Int_t jLayer = 0; jLayer < 6; jLayer++) {
972ef65e 1597 Int_t jseed = kNPlanes*trackIndex+jLayer;
1598 if(!sseed[jseed].IsOK()) continue;
1599 if(TMath::Abs(sseed[jseed].GetYfit(1) - sseed[jseed].GetYfit(1)) >= .2) continue; // check this condition with Marian
1600 sseed[jseed].UseClusters();
1601 if(!cl){
1602 Int_t ic = 0;
1603 while(!(cl = sseed[jseed].GetClusters(ic))) ic++;
1604 clusterIndex = sseed[jseed].GetIndexes(ic);
1605 }
d739fd4e 1606 }
1607 if(!cl) continue;
e4f2f73d 1608
1609
d739fd4e 1610 // Build track parameters
1611 AliTRDseedV1 *lseed =&sseed[trackIndex*6];
1612 Int_t idx = 0;
1613 while(idx<3 && !lseed->IsOK()) {
972ef65e 1614 idx++;
1615 lseed++;
d739fd4e 1616 }
1617 Double_t cR = lseed->GetC();
1618 trackParams[1] = lseed->GetYref(0);
1619 trackParams[2] = lseed->GetZref(0);
1620 trackParams[3] = lseed->GetX0() * cR - TMath::Sin(TMath::ATan(lseed->GetYref(1)));
1621 trackParams[4] = lseed->GetZref(1) / TMath::Sqrt(1. + lseed->GetYref(1) * lseed->GetYref(1));
1622 trackParams[5] = cR;
1623 trackParams[0] = lseed->GetX0();
1624 Int_t ich = 0; while(!(chamber = stack[ich])) ich++;
1625 trackParams[6] = fGeom->GetSector(chamber->GetDetector());/* *alpha+shift; // Supermodule*/
1626
1627 if(AliTRDReconstructor::StreamLevel() > 1){
972ef65e 1628 AliInfo(Form("Track %d [%d] nlayers %d trackQuality = %e nused %d, yref = %3.3f", itrack, trackIndex, nlayers, fTrackQuality[trackIndex], nused, trackParams[1]));
eb38ed55 1629
972ef65e 1630 Int_t nclusters = 0;
1631 AliTRDseedV1 *dseed[6];
1632 for(int is=0; is<6; is++){
1633 dseed[is] = new AliTRDseedV1(sseed[trackIndex*6+is]);
1634 dseed[is]->SetOwner();
1635 nclusters += sseed[is].GetN2();
1636 }
1637 //Int_t eventNrInFile = esd->GetEventNumberInFile();
1638 //AliInfo(Form("Number of clusters %d.", nclusters));
1639 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
1640 Int_t trackNumber = AliTRDtrackerDebug::GetTrackNumber();
1641 Int_t candidateNumber = AliTRDtrackerDebug::GetCandidateNumber();
1642 TTreeSRedirector &cstreamer = *fgDebugStreamer;
1643 cstreamer << "Clusters2TracksStack"
1644 << "EventNumber=" << eventNumber
1645 << "TrackNumber=" << trackNumber
1646 << "CandidateNumber=" << candidateNumber
1647 << "Iter=" << fSieveSeeding
1648 << "Like=" << fTrackQuality[trackIndex]
1649 << "S0.=" << dseed[0]
1650 << "S1.=" << dseed[1]
1651 << "S2.=" << dseed[2]
1652 << "S3.=" << dseed[3]
1653 << "S4.=" << dseed[4]
1654 << "S5.=" << dseed[5]
1655 << "p0=" << trackParams[0]
1656 << "p1=" << trackParams[1]
1657 << "p2=" << trackParams[2]
1658 << "p3=" << trackParams[3]
1659 << "p4=" << trackParams[4]
1660 << "p5=" << trackParams[5]
1661 << "p6=" << trackParams[6]
1662 << "Label=" << label
1663 << "Label1=" << label1
1664 << "Label2=" << label2
1665 << "FakeRatio=" << fakeratio
1666 << "Freq=" << frequency
1667 << "Ncl=" << ncl
1668 << "NLayers=" << nlayers
1669 << "Findable=" << findable
1670
1671 << "NUsed=" << nused
1672 << "\n";
d739fd4e 1673 }
e4f2f73d 1674
d739fd4e 1675 AliTRDtrackV1 *track = MakeTrack(&sseed[trackIndex*kNPlanes], trackParams);
d739fd4e 1676 if(!track){
972ef65e 1677 //AliWarning("Fail to build a TRD Track.");
1678 continue;
d739fd4e 1679 }
1680 //AliInfo("End of MakeTrack()");
1681 AliESDtrack esdTrack;
1682 esdTrack.UpdateTrackParams(track, AliESDtrack::kTRDout);
1683 esdTrack.SetLabel(track->GetLabel());
749f58fa 1684 track->UpdateESDtrack(&esdTrack);
d739fd4e 1685 // write ESD-friends if neccessary
1686 if (AliTRDReconstructor::StreamLevel() > 0){
972ef65e 1687 //printf("Creating Calibrations Object\n");
1688 AliTRDtrackV1 *calibTrack = new AliTRDtrackV1(*track);
1689 calibTrack->SetOwner();
1690 esdTrack.AddCalibObject(calibTrack);
d739fd4e 1691 }
1692 new ((*esdTrackList)[ntracks0++]) AliESDtrack(esdTrack);
1693 ntracks1++;
1694 AliTRDtrackerDebug::SetTrackNumber(AliTRDtrackerDebug::GetTrackNumber() + 1);
972ef65e 1695 }
d739fd4e 1696
972ef65e 1697 jSieve++;
1698 } while(jSieve<5 && candidates); // end track candidates sieve
1699 if(!ntracks1) break;
d739fd4e 1700
972ef65e 1701 // increment counters
1702 ntracks2 += ntracks1;
1703 fSieveSeeding++;
d739fd4e 1704
972ef65e 1705 // Rebuild plane configurations and indices taking only unused clusters into account
1706 quality = BuildSeedingConfigs(stack, configs);
1707 if(quality < 1.E-7) break; //AliTRDReconstructor::RecoParam()->GetPlaneQualityThreshold()) break;
e4f2f73d 1708
972ef65e 1709 for(Int_t ip = 0; ip < kNPlanes; ip++){
1710 if(!(chamber = stack[ip])) continue;
1711 chamber->Build(fGeom);//Indices(fSieveSeeding);
1712 }
1713
1714 if(AliTRDReconstructor::StreamLevel() > 1){
1715 AliInfo(Form("Sieve level %d Plane config %d %d %d Quality %f", fSieveSeeding, configs[0], configs[1], configs[2], quality));
1716 }
1717 } while(fSieveSeeding<10); // end stack clusters sieve
e4f2f73d 1718
1719
1720
972ef65e 1721 //AliInfo(Form("Registered TRD tracks %d in stack %d.", ntracks2, pars[1]));
e4f2f73d 1722
972ef65e 1723 return ntracks2;
e4f2f73d 1724}
1725
1726//___________________________________________________________________
eb38ed55 1727Double_t AliTRDtrackerV1::BuildSeedingConfigs(AliTRDtrackingChamber **stack, Int_t *configs)
e4f2f73d 1728{
972ef65e 1729 //
1730 // Assign probabilities to chambers according to their
1731 // capability of producing seeds.
1732 //
1733 // Parameters :
1734 //
1735 // layers : Array of stack propagation layers for all 6 chambers in one stack
1736 // configs : On exit array of configuration indexes (see GetSeedingConfig()
1737 // for details) in the decreasing order of their seeding probabilities.
1738 //
1739 // Output :
1740 //
1741 // Return top configuration quality
1742 //
1743 // Detailed description:
1744 //
1745 // To each chamber seeding configuration (see GetSeedingConfig() for
1746 // the list of all configurations) one defines 2 quality factors:
1747 // - an apriori topological quality (see GetSeedingConfig() for details) and
1748 // - a data quality based on the uniformity of the distribution of
1749 // clusters over the x range (time bins population). See CookChamberQA() for details.
1750 // The overall chamber quality is given by the product of this 2 contributions.
1751 //
1752
1753 Double_t chamberQ[kNPlanes];
1754 AliTRDtrackingChamber *chamber = 0x0;
1755 for(int iplane=0; iplane<kNPlanes; iplane++){
1756 if(!(chamber = stack[iplane])) continue;
1757 chamberQ[iplane] = (chamber = stack[iplane]) ? chamber->GetQuality() : 0.;
1758 }
1759
1760 Double_t tconfig[kNConfigs];
1761 Int_t planes[4];
1762 for(int iconf=0; iconf<kNConfigs; iconf++){
1763 GetSeedingConfig(iconf, planes);
1764 tconfig[iconf] = fgTopologicQA[iconf];
1765 for(int iplane=0; iplane<4; iplane++) tconfig[iconf] *= chamberQ[planes[iplane]];
1766 }
1767
1768 TMath::Sort((Int_t)kNConfigs, tconfig, configs, kTRUE);
1769 // AliInfo(Form("q[%d] = %f", configs[0], tconfig[configs[0]]));
1770 // AliInfo(Form("q[%d] = %f", configs[1], tconfig[configs[1]]));
1771 // AliInfo(Form("q[%d] = %f", configs[2], tconfig[configs[2]]));
1772
1773 return tconfig[configs[0]];
e4f2f73d 1774}
1775
1776//____________________________________________________________________
eb38ed55 1777Int_t AliTRDtrackerV1::MakeSeeds(AliTRDtrackingChamber **stack, AliTRDseedV1 *sseed, Int_t *ipar)
e4f2f73d 1778{
972ef65e 1779 //
1780 // Make tracklet seeds in the TRD stack.
1781 //
1782 // Parameters :
1783 // layers : Array of stack propagation layers containing clusters
1784 // sseed : Array of empty tracklet seeds. On exit they are filled.
1785 // ipar : Control parameters:
1786 // ipar[0] -> seeding chambers configuration
1787 // ipar[1] -> stack index
1788 // ipar[2] -> number of track candidates found so far
1789 //
1790 // Output :
1791 // Number of tracks candidates found.
1792 //
1793 // Detailed description
1794 //
1795 // The following steps are performed:
1796 // 1. Select seeding layers from seeding chambers
1797 // 2. Select seeding clusters from the seeding AliTRDpropagationLayerStack.
1798 // The clusters are taken from layer 3, layer 0, layer 1 and layer 2, in
1799 // this order. The parameters controling the range of accepted clusters in
1800 // layer 0, 1, and 2 are defined in AliTRDchamberTimeBin::BuildCond().
1801 // 3. Helix fit of the cluster set. (see AliTRDtrackerFitter::FitRieman(AliTRDcluster**))
1802 // 4. Initialize seeding tracklets in the seeding chambers.
1803 // 5. Filter 0.
1804 // Chi2 in the Y direction less than threshold ... (1./(3. - sLayer))
1805 // Chi2 in the Z direction less than threshold ... (1./(3. - sLayer))
1806 // 6. Attach clusters to seeding tracklets and find linear approximation of
1807 // the tracklet (see AliTRDseedV1::AttachClustersIter()). The number of used
1808 // clusters used by current seeds should not exceed ... (25).
1809 // 7. Filter 1.
1810 // All 4 seeding tracklets should be correctly constructed (see
1811 // AliTRDseedV1::AttachClustersIter())
1812 // 8. Helix fit of the seeding tracklets
1813 // 9. Filter 2.
1814 // Likelihood calculation of the fit. (See AliTRDtrackerV1::CookLikelihood() for details)
1815 // 10. Extrapolation of the helix fit to the other 2 chambers:
1816 // a) Initialization of extrapolation tracklet with fit parameters
1817 // b) Helix fit of tracklets
1818 // c) Attach clusters and linear interpolation to extrapolated tracklets
1819 // d) Helix fit of tracklets
1820 // 11. Improve seeding tracklets quality by reassigning clusters.
1821 // See AliTRDtrackerV1::ImproveSeedQuality() for details.
1822 // 12. Helix fit of all 6 seeding tracklets and chi2 calculation
1823 // 13. Hyperplane fit and track quality calculation. See AliTRDtrackerFitter::FitHyperplane() for details.
1824 // 14. Cooking labels for tracklets. Should be done only for MC
1825 // 15. Register seeds.
1826 //
1827
1828 AliTRDtrackingChamber *chamber = 0x0;
1829 AliTRDcluster *c[4] = {0x0, 0x0, 0x0, 0x0}; // initilize seeding clusters
1830 AliTRDseedV1 *cseed = &sseed[0]; // initialize tracklets for first track
1831 Int_t ncl, mcl; // working variable for looping over clusters
1832 Int_t index[AliTRDchamberTimeBin::kMaxClustersLayer], jndex[AliTRDchamberTimeBin::kMaxClustersLayer];
1833 // chi2 storage
1834 // chi2[0] = tracklet chi2 on the Z direction
1835 // chi2[1] = tracklet chi2 on the R direction
1836 Double_t chi2[4];
1837
1838
1839 // this should be data member of AliTRDtrack
1840 Double_t seedQuality[kMaxTracksStack];
1841
1842 // unpack control parameters
1843 Int_t config = ipar[0];
1844 Int_t ntracks = ipar[1];
1845 Int_t planes[kNSeedPlanes]; GetSeedingConfig(config, planes);
1846
1847 // Init chambers geometry
1848 Int_t ic = 0; while(!(chamber = stack[ic])) ic++;
1849 Int_t istack = fGeom->GetChamber(chamber->GetDetector());
1850 Double_t hL[kNPlanes]; // Tilting angle
1851 Float_t padlength[kNPlanes]; // pad lenghts
1852 AliTRDpadPlane *pp = 0x0;
1853 for(int iplane=0; iplane<kNPlanes; iplane++){
1854 pp = fGeom->GetPadPlane(iplane, istack);
1855 hL[iplane] = TMath::Tan(-TMath::DegToRad()*pp->GetTiltingAngle());
1856 padlength[iplane] = pp->GetLengthIPad();
1857 }
1858
1859 if(AliTRDReconstructor::StreamLevel() > 1){
1860 AliInfo(Form("Making seeds Stack[%d] Config[%d] Tracks[%d]...", istack, config, ntracks));
1861 }
1862
1863 Int_t nlayers = 0;
1864 AliTRDchamberTimeBin *layer[] = {0x0, 0x0, 0x0, 0x0};
1865 for(int isl=0; isl<kNSeedPlanes; isl++){
1866 if(!(chamber = stack[planes[isl]])) continue;
1867 if(!(layer[isl] = chamber->GetSeedingLayer(fGeom))) continue;
1868 nlayers++;
1869 //AliInfo(Form("seeding plane %d clusters %d", planes[isl], Int_t(*layer[isl])));
1870 }
1871 if(nlayers < 4) return 0;
1872
1873
1874 // Start finding seeds
1875 Double_t cond0[4], cond1[4], cond2[4];
1876 Int_t icl = 0;
1877 while((c[3] = (*layer[3])[icl++])){
1878 if(!c[3]) continue;
1879 layer[0]->BuildCond(c[3], cond0, 0);
1880 layer[0]->GetClusters(cond0, index, ncl);
1881 //printf("Found c[3] candidates 0 %d\n", ncl);
1882 Int_t jcl = 0;
1883 while(jcl<ncl) {
1884 c[0] = (*layer[0])[index[jcl++]];
1885 if(!c[0]) continue;
1886 Double_t dx = c[3]->GetX() - c[0]->GetX();
1887 Double_t theta = (c[3]->GetZ() - c[0]->GetZ())/dx;
1888 Double_t phi = (c[3]->GetY() - c[0]->GetY())/dx;
1889 layer[1]->BuildCond(c[0], cond1, 1, theta, phi);
1890 layer[1]->GetClusters(cond1, jndex, mcl);
1891 //printf("Found c[0] candidates 1 %d\n", mcl);
1892
1893 Int_t kcl = 0;
1894 while(kcl<mcl) {
d739fd4e 1895 c[1] = (*layer[1])[jndex[kcl++]];
1896 if(!c[1]) continue;
1897 layer[2]->BuildCond(c[1], cond2, 2, theta, phi);
1898 c[2] = layer[2]->GetNearestCluster(cond2);
1899 //printf("Found c[1] candidate 2 %p\n", c[2]);
1900 if(!c[2]) continue;
e4f2f73d 1901
d739fd4e 1902 // AliInfo("Seeding clusters found. Building seeds ...");
1903 // for(Int_t i = 0; i < kNSeedPlanes; i++) printf("%i. coordinates: x = %6.3f, y = %6.3f, z = %6.3f\n", i, c[i]->GetX(), c[i]->GetY(), c[i]->GetZ());
eb38ed55 1904
d739fd4e 1905 for (Int_t il = 0; il < 6; il++) cseed[il].Reset();
1906
1907 FitRieman(c, chi2);
1908
1909 AliTRDseedV1 *tseed = 0x0;
1910 for(int iLayer=0; iLayer<kNSeedPlanes; iLayer++){
972ef65e 1911 Int_t jLayer = planes[iLayer];
1912 tseed = &cseed[jLayer];
1913 tseed->SetPlane(jLayer);
1914 tseed->SetTilt(hL[jLayer]);
1915 tseed->SetPadLength(padlength[jLayer]);
1916 tseed->SetX0(stack[jLayer]->GetX());
1917 tseed->Init(GetRiemanFitter());
d739fd4e 1918 }
1919
1920 Bool_t isFake = kFALSE;
1921 if(AliTRDReconstructor::StreamLevel() >= 2){
972ef65e 1922 if (c[0]->GetLabel(0) != c[3]->GetLabel(0)) isFake = kTRUE;
1923 if (c[1]->GetLabel(0) != c[3]->GetLabel(0)) isFake = kTRUE;
1924 if (c[2]->GetLabel(0) != c[3]->GetLabel(0)) isFake = kTRUE;
1925
1926 Double_t xpos[4];
1927 for(Int_t l = 0; l < kNSeedPlanes; l++) xpos[l] = layer[l]->GetX();
1928 Float_t yref[4];
1929 for(int il=0; il<4; il++) yref[il] = cseed[planes[il]].GetYref(0);
1930 Int_t ll = c[3]->GetLabel(0);
1931 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
1932 Int_t candidateNumber = AliTRDtrackerDebug::GetCandidateNumber();
1933 AliRieman *rim = GetRiemanFitter();
1934 TTreeSRedirector &cs0 = *fgDebugStreamer;
1935 cs0 << "MakeSeeds0"
1936 <<"EventNumber=" << eventNumber
1937 <<"CandidateNumber=" << candidateNumber
1938 <<"isFake=" << isFake
1939 <<"config=" << config
1940 <<"label=" << ll
1941 <<"chi2z=" << chi2[0]
1942 <<"chi2y=" << chi2[1]
1943 <<"Y2exp=" << cond2[0]
1944 <<"Z2exp=" << cond2[1]
1945 <<"X0=" << xpos[0] //layer[sLayer]->GetX()
1946 <<"X1=" << xpos[1] //layer[sLayer + 1]->GetX()
1947 <<"X2=" << xpos[2] //layer[sLayer + 2]->GetX()
1948 <<"X3=" << xpos[3] //layer[sLayer + 3]->GetX()
1949 <<"yref0=" << yref[0]
1950 <<"yref1=" << yref[1]
1951 <<"yref2=" << yref[2]
1952 <<"yref3=" << yref[3]
1953 <<"c0.=" << c[0]
1954 <<"c1.=" << c[1]
1955 <<"c2.=" << c[2]
1956 <<"c3.=" << c[3]
1957 <<"Seed0.=" << &cseed[planes[0]]
1958 <<"Seed1.=" << &cseed[planes[1]]
1959 <<"Seed2.=" << &cseed[planes[2]]
1960 <<"Seed3.=" << &cseed[planes[3]]
1961 <<"RiemanFitter.=" << rim
1962 <<"\n";
d739fd4e 1963 }
1964
1965 if(chi2[0] > AliTRDReconstructor::RecoParam()->GetChi2Z()/*7./(3. - sLayer)*//*iter*/){
972ef65e 1966 //AliInfo(Form("Failed chi2 filter on chi2Z [%f].", chi2[0]));
1967 AliTRDtrackerDebug::SetCandidateNumber(AliTRDtrackerDebug::GetCandidateNumber() + 1);
1968 continue;
d739fd4e 1969 }
1970 if(chi2[1] > AliTRDReconstructor::RecoParam()->GetChi2Y()/*1./(3. - sLayer)*//*iter*/){
972ef65e 1971 //AliInfo(Form("Failed chi2 filter on chi2Y [%f].", chi2[1]));
1972 AliTRDtrackerDebug::SetCandidateNumber(AliTRDtrackerDebug::GetCandidateNumber() + 1);
1973 continue;
d739fd4e 1974 }
1975 //AliInfo("Passed chi2 filter.");
1976
1977 // try attaching clusters to tracklets
1978 Int_t nUsedCl = 0;
7bce990c 1979 Int_t mlayers = 0;
d739fd4e 1980 for(int iLayer=0; iLayer<kNSeedPlanes; iLayer++){
972ef65e 1981 Int_t jLayer = planes[iLayer];
1982 if(!cseed[jLayer].AttachClustersIter(stack[jLayer], 5., kFALSE, c[iLayer])) continue;
1983 nUsedCl += cseed[jLayer].GetNUsed();
1984 if(nUsedCl > 25) break;
7bce990c 1985 mlayers++;
d739fd4e 1986 }
7bce990c 1987 if(mlayers < kNSeedPlanes){
1988 //AliInfo(Form("Failed updating all seeds %d [%d].", mlayers, kNSeedPlanes));
972ef65e 1989 AliTRDtrackerDebug::SetCandidateNumber(AliTRDtrackerDebug::GetCandidateNumber() + 1);
1990 continue;
d739fd4e 1991 }
1992 // fit tracklets and cook likelihood
1993 FitTiltedRieman(&cseed[0], kTRUE);// Update Seeds and calculate Likelihood
1994 chi2[0] = GetChi2Y(&cseed[0]);
1995 chi2[1] = GetChi2Z(&cseed[0]);
1996 //Chi2 definitions in testing stage
1997 //chi2[0] = GetChi2YTest(&cseed[0]);
1998 //chi2[1] = GetChi2ZTest(&cseed[0]);
1999 Double_t like = CookLikelihood(&cseed[0], planes, chi2); // to be checked
2000
2001 if (TMath::Log(1.E-9 + like) < AliTRDReconstructor::RecoParam()->GetTrackLikelihood()){
972ef65e 2002 //AliInfo(Form("Failed likelihood %f[%e].", TMath::Log(1.E-9 + like), like));
2003 AliTRDtrackerDebug::SetCandidateNumber(AliTRDtrackerDebug::GetCandidateNumber() + 1);
2004 continue;
d739fd4e 2005 }
2006 //AliInfo(Form("Passed likelihood %f[%e].", TMath::Log(1.E-9 + like), like));
2007
2008 // book preliminary results
2009 seedQuality[ntracks] = like;
2010 fSeedLayer[ntracks] = config;/*sLayer;*/
2011
2012 // attach clusters to the extrapolation seeds
2013 Int_t lextrap[2];
2014 GetExtrapolationConfig(config, lextrap);
2015 Int_t nusedf = 0; // debug value
2016 for(int iLayer=0; iLayer<2; iLayer++){
972ef65e 2017 Int_t jLayer = lextrap[iLayer];
2018 if(!(chamber = stack[jLayer])) continue;
eb38ed55 2019
972ef65e 2020 // prepare extrapolated seed
2021 cseed[jLayer].Reset();
2022 cseed[jLayer].SetPlane(jLayer);
2023 cseed[jLayer].SetTilt(hL[jLayer]);
2024 cseed[jLayer].SetX0(chamber->GetX());
2025 cseed[jLayer].SetPadLength(padlength[jLayer]);
2026
2027 // fit extrapolated seed
2028 if ((jLayer == 0) && !(cseed[1].IsOK())) continue;
2029 if ((jLayer == 5) && !(cseed[4].IsOK())) continue;
7bce990c 2030 AliTRDseedV1 pseed = cseed[jLayer];
2031 if(!pseed.AttachClustersIter(chamber, 1000.)) continue;
2032 cseed[jLayer] = pseed;
972ef65e 2033 nusedf += cseed[jLayer].GetNUsed(); // debug value
2034 FitTiltedRieman(cseed, kTRUE);
d739fd4e 2035 }
2036
2037 // AliInfo("Extrapolation done.");
2038 // Debug Stream containing all the 6 tracklets
2039 if(AliTRDReconstructor::StreamLevel() >= 2){
972ef65e 2040 TTreeSRedirector &cstreamer = *fgDebugStreamer;
2041 TLinearFitter *tiltedRieman = GetTiltedRiemanFitter();
2042 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
2043 Int_t candidateNumber = AliTRDtrackerDebug::GetCandidateNumber();
2044 cstreamer << "MakeSeeds1"
2045 << "EventNumber=" << eventNumber
2046 << "CandidateNumber=" << candidateNumber
2047 << "S0.=" << &cseed[0]
2048 << "S1.=" << &cseed[1]
2049 << "S2.=" << &cseed[2]
2050 << "S3.=" << &cseed[3]
2051 << "S4.=" << &cseed[4]
2052 << "S5.=" << &cseed[5]
2053 << "FitterT.=" << tiltedRieman
2054 << "\n";
d739fd4e 2055 }
bb56afff 2056
d739fd4e 2057 if(ImproveSeedQuality(stack, cseed) < 4){
972ef65e 2058 AliTRDtrackerDebug::SetCandidateNumber(AliTRDtrackerDebug::GetCandidateNumber() + 1);
2059 continue;
d739fd4e 2060 }
2061 //AliInfo("Improve seed quality done.");
2062
2063 // fit full track and cook likelihoods
2064 // Double_t curv = FitRieman(&cseed[0], chi2);
7bce990c 2065 // Double_t chi2ZF = chi2[0] / TMath::Max((mlayers - 3.), 1.);
2066 // Double_t chi2RF = chi2[1] / TMath::Max((mlayers - 3.), 1.);
d739fd4e 2067
2068 // do the final track fitting (Once with vertex constraint and once without vertex constraint)
2069 Double_t chi2Vals[3];
2070 chi2Vals[0] = FitTiltedRieman(&cseed[0], kFALSE);
2071 chi2Vals[1] = FitTiltedRiemanConstraint(&cseed[0], GetZ());
7bce990c 2072 chi2Vals[2] = GetChi2Z(&cseed[0]) / TMath::Max((mlayers - 3.), 1.);
d739fd4e 2073 // Chi2 definitions in testing stage
2074 //chi2Vals[2] = GetChi2ZTest(&cseed[0]);
2075 fTrackQuality[ntracks] = CalculateTrackLikelihood(&cseed[0], &chi2Vals[0]);
2076 //AliInfo("Hyperplane fit done\n");
2077
2078 // finalize tracklets
2079 Int_t labels[12];
2080 Int_t outlab[24];
2081 Int_t nlab = 0;
2082 for (Int_t iLayer = 0; iLayer < 6; iLayer++) {
972ef65e 2083 if (!cseed[iLayer].IsOK()) continue;
d739fd4e 2084
972ef65e 2085 if (cseed[iLayer].GetLabels(0) >= 0) {
2086 labels[nlab] = cseed[iLayer].GetLabels(0);
2087 nlab++;
2088 }
d739fd4e 2089
972ef65e 2090 if (cseed[iLayer].GetLabels(1) >= 0) {
2091 labels[nlab] = cseed[iLayer].GetLabels(1);
2092 nlab++;
2093 }
d739fd4e 2094 }
2095 Freq(nlab,labels,outlab,kFALSE);
2096 Int_t label = outlab[0];
2097 Int_t frequency = outlab[1];
2098 for (Int_t iLayer = 0; iLayer < 6; iLayer++) {
972ef65e 2099 cseed[iLayer].SetFreq(frequency);
2100 cseed[iLayer].SetChi2Z(chi2[1]);
d739fd4e 2101 }
972ef65e 2102
d739fd4e 2103 if(AliTRDReconstructor::StreamLevel() >= 2){
972ef65e 2104 TTreeSRedirector &cstreamer = *fgDebugStreamer;
2105 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
2106 Int_t candidateNumber = AliTRDtrackerDebug::GetCandidateNumber();
2107 TLinearFitter *fitterTC = GetTiltedRiemanFitterConstraint();
2108 TLinearFitter *fitterT = GetTiltedRiemanFitter();
2109 cstreamer << "MakeSeeds2"
2110 << "EventNumber=" << eventNumber
2111 << "CandidateNumber=" << candidateNumber
2112 << "Chi2TR=" << chi2Vals[0]
2113 << "Chi2TC=" << chi2Vals[1]
7bce990c 2114 << "Nlayers=" << mlayers
972ef65e 2115 << "NUsedS=" << nUsedCl
2116 << "NUsed=" << nusedf
2117 << "Like=" << like
2118 << "S0.=" << &cseed[0]
2119 << "S1.=" << &cseed[1]
2120 << "S2.=" << &cseed[2]
2121 << "S3.=" << &cseed[3]
2122 << "S4.=" << &cseed[4]
2123 << "S5.=" << &cseed[5]
2124 << "Label=" << label
2125 << "Freq=" << frequency
2126 << "FitterT.=" << fitterT
2127 << "FitterTC.=" << fitterTC
2128 << "\n";
d739fd4e 2129 }
e4f2f73d 2130
d739fd4e 2131 ntracks++;
2132 AliTRDtrackerDebug::SetCandidateNumber(AliTRDtrackerDebug::GetCandidateNumber() + 1);
2133 if(ntracks == kMaxTracksStack){
972ef65e 2134 AliWarning(Form("Number of seeds reached maximum allowed (%d) in stack.", kMaxTracksStack));
2135 for(int isl=0; isl<4; isl++) delete layer[isl];
2136 return ntracks;
e4f2f73d 2137 }
d739fd4e 2138 cseed += 6;
972ef65e 2139 }
2140 }
2141 }
2142 for(int isl=0; isl<4; isl++) delete layer[isl];
e4f2f73d 2143
972ef65e 2144 return ntracks;
e4f2f73d 2145}
2146
2147//_____________________________________________________________________________
0906e73e 2148AliTRDtrackV1* AliTRDtrackerV1::MakeTrack(AliTRDseedV1 *seeds, Double_t *params)
e4f2f73d 2149{
972ef65e 2150 //
2151 // Build a TRD track out of tracklet candidates
2152 //
2153 // Parameters :
2154 // seeds : array of tracklets
2155 // params : track parameters (see MakeSeeds() function body for a detailed description)
2156 //
2157 // Output :
2158 // The TRD track.
2159 //
2160 // Detailed description
2161 //
2162 // To be discussed with Marian !!
2163 //
2164
0c23ac49 2165 AliTRDCalibraFillHisto *calibra = AliTRDCalibraFillHisto::Instance();
2166 if (!calibra) AliInfo("Could not get Calibra instance\n");
2167
972ef65e 2168 Double_t alpha = AliTRDgeometry::GetAlpha();
2169 Double_t shift = AliTRDgeometry::GetAlpha()/2.0;
2170 Double_t c[15];
2171
2172 c[ 0] = 0.2;
2173 c[ 1] = 0.0; c[ 2] = 2.0;
2174 c[ 3] = 0.0; c[ 4] = 0.0; c[ 5] = 0.02;
2175 c[ 6] = 0.0; c[ 7] = 0.0; c[ 8] = 0.0; c[ 9] = 0.1;
2176 c[10] = 0.0; c[11] = 0.0; c[12] = 0.0; c[13] = 0.0; c[14] = params[5]*params[5]*0.01;
2177
2178 AliTRDtrackV1 *track = new AliTRDtrackV1(seeds, &params[1], c, params[0], params[6]*alpha+shift);
2179 track->PropagateTo(params[0]-5.0);
2180 track->ResetCovariance(1);
2181 Int_t nc = FollowBackProlongation(*track);
2182 //AliInfo(Form("N clusters for track %d", nc));
2183 if (nc < 30) {
2184 delete track;
2185 track = 0x0;
2186 } else {
2187 track->CookdEdx();
2188 track->CookdEdxTimBin(-1);
2189 track->CookLabel(.9);
0c23ac49 2190 // computes PID for track
2191 track->CookPID();
2192 // update calibration references using this track
2193 if(calibra->GetHisto2d()) calibra->UpdateHistogramsV1(track);
972ef65e 2194 }
2195
2196 return track;
e4f2f73d 2197}
2198
0906e73e 2199
2200//____________________________________________________________________
eb38ed55 2201Int_t AliTRDtrackerV1::ImproveSeedQuality(AliTRDtrackingChamber **stack, AliTRDseedV1 *cseed)
e4f2f73d 2202{
972ef65e 2203 //
2204 // Sort tracklets according to "quality" and try to "improve" the first 4 worst
2205 //
2206 // Parameters :
2207 // layers : Array of propagation layers for a stack/supermodule
2208 // cseed : Array of 6 seeding tracklets which has to be improved
2209 //
2210 // Output :
2211 // cssed : Improved seeds
2212 //
2213 // Detailed description
2214 //
2215 // Iterative procedure in which new clusters are searched for each
2216 // tracklet seed such that the seed quality (see AliTRDseed::GetQuality())
2217 // can be maximized. If some optimization is found the old seeds are replaced.
2218 //
2219 // debug level: 7
2220 //
2221
2222 // make a local working copy
2223 AliTRDtrackingChamber *chamber = 0x0;
2224 AliTRDseedV1 bseed[6];
2225 Int_t nLayers = 0;
2226 for (Int_t jLayer = 0; jLayer < 6; jLayer++) bseed[jLayer] = cseed[jLayer];
2227
2228 Float_t lastquality = 10000.0;
2229 Float_t lastchi2 = 10000.0;
2230 Float_t chi2 = 1000.0;
2231
2232 for (Int_t iter = 0; iter < 4; iter++) {
2233 Float_t sumquality = 0.0;
2234 Float_t squality[6];
2235 Int_t sortindexes[6];
2236
2237 for (Int_t jLayer = 0; jLayer < 6; jLayer++) {
2238 squality[jLayer] = bseed[jLayer].IsOK() ? bseed[jLayer].GetQuality(kTRUE) : -1.;
2239 sumquality += squality[jLayer];
2240 }
2241 if ((sumquality >= lastquality) || (chi2 > lastchi2)) break;
2242
2243 nLayers = 0;
2244 lastquality = sumquality;
2245 lastchi2 = chi2;
2246 if (iter > 0) for (Int_t jLayer = 0; jLayer < 6; jLayer++) cseed[jLayer] = bseed[jLayer];
2247
2248 TMath::Sort(6, squality, sortindexes, kFALSE);
2249 for (Int_t jLayer = 5; jLayer > 1; jLayer--) {
2250 Int_t bLayer = sortindexes[jLayer];
2251 if(!(chamber = stack[bLayer])) continue;
2252 bseed[bLayer].AttachClustersIter(chamber, squality[bLayer], kTRUE);
2253 if(bseed[bLayer].IsOK()) nLayers++;
2254 }
2255
2256 chi2 = FitTiltedRieman(bseed, kTRUE);
2257 if(AliTRDReconstructor::StreamLevel() >= 7){
2258 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
2259 Int_t candidateNumber = AliTRDtrackerDebug::GetCandidateNumber();
2260 TLinearFitter *tiltedRieman = GetTiltedRiemanFitter();
2261 TTreeSRedirector &cstreamer = *fgDebugStreamer;
2262 cstreamer << "ImproveSeedQuality"
d739fd4e 2263 << "EventNumber=" << eventNumber
2264 << "CandidateNumber=" << candidateNumber
2265 << "Iteration=" << iter
2266 << "S0.=" << &bseed[0]
2267 << "S1.=" << &bseed[1]
2268 << "S2.=" << &bseed[2]
2269 << "S3.=" << &bseed[3]
2270 << "S4.=" << &bseed[4]
2271 << "S5.=" << &bseed[5]
2272 << "FitterT.=" << tiltedRieman
2273 << "\n";
972ef65e 2274 }
2275 } // Loop: iter
eb38ed55 2276
972ef65e 2277 // we are sure that at least 2 tracklets are OK !
2278 return nLayers+2;
e4f2f73d 2279}
2280
eb38ed55 2281//_________________________________________________________________________
2282Double_t AliTRDtrackerV1::CalculateTrackLikelihood(AliTRDseedV1 *tracklets, Double_t *chi2){
972ef65e 2283 //
2284 // Calculates the Track Likelihood value. This parameter serves as main quality criterion for
2285 // the track selection
2286 // The likelihood value containes:
2287 // - The chi2 values from the both fitters and the chi2 values in z-direction from a linear fit
2288 // - The Sum of the Parameter |slope_ref - slope_fit|/Sigma of the tracklets
2289 // For all Parameters an exponential dependency is used
2290 //
2291 // Parameters: - Array of tracklets (AliTRDseedV1) related to the track candidate
2292 // - Array of chi2 values:
2293 // * Non-Constrained Tilted Riemann fit
2294 // * Vertex-Constrained Tilted Riemann fit
2295 // * z-Direction from Linear fit
2296 // Output: - The calculated track likelihood
2297 //
2298 // debug level 2
2299 //
2300
2301 Double_t sumdaf = 0, nLayers = 0;
2302 for (Int_t iLayer = 0; iLayer < kNPlanes; iLayer++) {
2303 if(!tracklets[iLayer].IsOK()) continue;
2304 sumdaf += TMath::Abs((tracklets[iLayer].GetYfit(1) - tracklets[iLayer].GetYref(1))/ tracklets[iLayer].GetSigmaY2());
2305 nLayers++;
2306 }
2307 sumdaf /= Float_t (nLayers - 2.0);
2308
2309 Double_t likeChi2Z = TMath::Exp(-chi2[2] * 0.14); // Chi2Z
2310 Double_t likeChi2TC = TMath::Exp(-chi2[1] * 0.677); // Constrained Tilted Riemann
2311 Double_t likeChi2TR = TMath::Exp(-chi2[0] * 0.78); // Non-constrained Tilted Riemann
2312 Double_t likeAF = TMath::Exp(-sumdaf * 3.23);
2313 Double_t trackLikelihood = likeChi2Z * likeChi2TR * likeAF;
2314
2315 if(AliTRDReconstructor::StreamLevel() >= 2){
2316 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
2317 Int_t candidateNumber = AliTRDtrackerDebug::GetCandidateNumber();
2318 TTreeSRedirector &cstreamer = *fgDebugStreamer;
2319 cstreamer << "CalculateTrackLikelihood0"
2320 << "EventNumber=" << eventNumber
2321 << "CandidateNumber=" << candidateNumber
2322 << "LikeChi2Z=" << likeChi2Z
2323 << "LikeChi2TR=" << likeChi2TR
2324 << "LikeChi2TC=" << likeChi2TC
2325 << "LikeAF=" << likeAF
2326 << "TrackLikelihood=" << trackLikelihood
2327 << "\n";
2328 }
2329
2330 return trackLikelihood;
e4f2f73d 2331}
2332
2333//____________________________________________________________________
eb38ed55 2334Double_t AliTRDtrackerV1::CookLikelihood(AliTRDseedV1 *cseed, Int_t planes[4]
972ef65e 2335 , Double_t *chi2)
e4f2f73d 2336{
972ef65e 2337 //
2338 // Calculate the probability of this track candidate.
2339 //
2340 // Parameters :
2341 // cseeds : array of candidate tracklets
2342 // planes : array of seeding planes (see seeding configuration)
2343 // chi2 : chi2 values (on the Z and Y direction) from the rieman fit of the track.
2344 //
2345 // Output :
2346 // likelihood value
2347 //
2348 // Detailed description
2349 //
2350 // The track quality is estimated based on the following 4 criteria:
2351 // 1. precision of the rieman fit on the Y direction (likea)
2352 // 2. chi2 on the Y direction (likechi2y)
2353 // 3. chi2 on the Z direction (likechi2z)
2354 // 4. number of attached clusters compared to a reference value
2355 // (see AliTRDrecoParam::fkFindable) (likeN)
2356 //
2357 // The distributions for each type of probabilities are given below as of
2358 // (date). They have to be checked to assure consistency of estimation.
2359 //
2360
2361 // ratio of the total number of clusters/track which are expected to be found by the tracker.
2362 Float_t fgFindable = AliTRDReconstructor::RecoParam()->GetFindableClusters();
2363
2364
2365 Int_t nclusters = 0;
2366 Double_t sumda = 0.;
2367 for(UChar_t ilayer = 0; ilayer < 4; ilayer++){
2368 Int_t jlayer = planes[ilayer];
2369 nclusters += cseed[jlayer].GetN2();
2370 sumda += TMath::Abs(cseed[jlayer].GetYfitR(1) - cseed[jlayer].GetYref(1));
2371 }
2372 Double_t likea = TMath::Exp(-sumda*10.6);
2373 Double_t likechi2y = 0.0000000001;
2374 if (chi2[0] < 0.5) likechi2y += TMath::Exp(-TMath::Sqrt(chi2[0]) * 7.73);
2375 Double_t likechi2z = TMath::Exp(-chi2[1] * 0.088) / TMath::Exp(-chi2[1] * 0.019);
2376 Int_t enc = Int_t(fgFindable*4.*fgNTimeBins); // Expected Number Of Clusters, normally 72
2377 Double_t likeN = TMath::Exp(-(enc - nclusters) * 0.19);
2378
2379 Double_t like = likea * likechi2y * likechi2z * likeN;
2380
2381 // AliInfo(Form("sumda(%f) chi2[0](%f) chi2[1](%f) likea(%f) likechi2y(%f) likechi2z(%f) nclusters(%d) likeN(%f)", sumda, chi2[0], chi2[1], likea, likechi2y, likechi2z, nclusters, likeN));
2382 if(AliTRDReconstructor::StreamLevel() >= 2){
2383 Int_t eventNumber = AliTRDtrackerDebug::GetEventNumber();
2384 Int_t candidateNumber = AliTRDtrackerDebug::GetCandidateNumber();
2385 // The Debug Stream contains the seed
2386 TTreeSRedirector &cstreamer = *fgDebugStreamer;
2387 cstreamer << "CookLikelihood"
2388 << "EventNumber=" << eventNumber
2389 << "CandidateNumber=" << candidateNumber
2390 << "tracklet0.=" << &cseed[0]
2391 << "tracklet1.=" << &cseed[1]
2392 << "tracklet2.=" << &cseed[2]
2393 << "tracklet3.=" << &cseed[3]
2394 << "tracklet4.=" << &cseed[4]
2395 << "tracklet5.=" << &cseed[5]
2396 << "sumda=" << sumda
2397 << "chi0=" << chi2[0]
2398 << "chi1=" << chi2[1]
2399 << "likea=" << likea
2400 << "likechi2y=" << likechi2y
2401 << "likechi2z=" << likechi2z
2402 << "nclusters=" << nclusters
2403 << "likeN=" << likeN
2404 << "like=" << like
2405 << "\n";
2406 }
2407
2408 return like;
e4f2f73d 2409}
2410
eb38ed55 2411
e4f2f73d 2412
2413//____________________________________________________________________
0906e73e 2414void AliTRDtrackerV1::GetSeedingConfig(Int_t iconfig, Int_t planes[4])
e4f2f73d 2415{
972ef65e 2416 //
2417 // Map seeding configurations to detector planes.
2418 //
2419 // Parameters :
2420 // iconfig : configuration index
2421 // planes : member planes of this configuration. On input empty.
2422 //
2423 // Output :
2424 // planes : contains the planes which are defining the configuration
2425 //
2426 // Detailed description
2427 //
2428 // Here is the list of seeding planes configurations together with
2429 // their topological classification:
2430 //
2431 // 0 - 5432 TQ 0
2432 // 1 - 4321 TQ 0
2433 // 2 - 3210 TQ 0
2434 // 3 - 5321 TQ 1
2435 // 4 - 4210 TQ 1
2436 // 5 - 5431 TQ 1
2437 // 6 - 4320 TQ 1
2438 // 7 - 5430 TQ 2
2439 // 8 - 5210 TQ 2
2440 // 9 - 5421 TQ 3
2441 // 10 - 4310 TQ 3
2442 // 11 - 5410 TQ 4
2443 // 12 - 5420 TQ 5
2444 // 13 - 5320 TQ 5
2445 // 14 - 5310 TQ 5
2446 //
2447 // The topologic quality is modeled as follows:
2448 // 1. The general model is define by the equation:
2449 // p(conf) = exp(-conf/2)
2450 // 2. According to the topologic classification, configurations from the same
2451 // class are assigned the agerage value over the model values.
2452 // 3. Quality values are normalized.
2453 //
2454 // The topologic quality distribution as function of configuration is given below:
2455 //Begin_Html
2456 // <img src="gif/topologicQA.gif">
2457 //End_Html
2458 //
2459
2460 switch(iconfig){
2461 case 0: // 5432 TQ 0
2462 planes[0] = 2;
2463 planes[1] = 3;
2464 planes[2] = 4;
2465 planes[3] = 5;
2466 break;
2467 case 1: // 4321 TQ 0
2468 planes[0] = 1;
2469 planes[1] = 2;
2470 planes[2] = 3;
2471 planes[3] = 4;
2472 break;
2473 case 2: // 3210 TQ 0
2474 planes[0] = 0;
2475 planes[1] = 1;
2476 planes[2] = 2;
2477 planes[3] = 3;
2478 break;
2479 case 3: // 5321 TQ 1
2480 planes[0] = 1;
2481 planes[1] = 2;
2482 planes[2] = 3;
2483 planes[3] = 5;
2484 break;
2485 case 4: // 4210 TQ 1
2486 planes[0] = 0;
2487 planes[1] = 1;
2488 planes[2] = 2;
2489 planes[3] = 4;
2490 break;
2491 case 5: // 5431 TQ 1
2492 planes[0] = 1;
2493 planes[1] = 3;
2494 planes[2] = 4;
2495 planes[3] = 5;
2496 break;
2497 case 6: // 4320 TQ 1
2498 planes[0] = 0;
2499 planes[1] = 2;
2500 planes[2] = 3;
2501 planes[3] = 4;
2502 break;
2503 case 7: // 5430 TQ 2
2504 planes[0] = 0;
2505 planes[1] = 3;
2506 planes[2] = 4;
2507 planes[3] = 5;
2508 break;
2509 case 8: // 5210 TQ 2
2510 planes[0] = 0;
2511 planes[1] = 1;
2512 planes[2] = 2;
2513 planes[3] = 5;
2514 break;
2515 case 9: // 5421 TQ 3
2516 planes[0] = 1;
2517 planes[1] = 2;
2518 planes[2] = 4;
2519 planes[3] = 5;
2520 break;
2521 case 10: // 4310 TQ 3
2522 planes[0] = 0;
2523 planes[1] = 1;
2524 planes[2] = 3;
2525 planes[3] = 4;
2526 break;
2527 case 11: // 5410 TQ 4
2528 planes[0] = 0;
2529 planes[1] = 1;
2530 planes[2] = 4;
2531 planes[3] = 5;
2532 break;
2533 case 12: // 5420 TQ 5
2534 planes[0] = 0;
2535 planes[1] = 2;
2536 planes[2] = 4;
2537 planes[3] = 5;
2538 break;
2539 case 13: // 5320 TQ 5
2540 planes[0] = 0;
2541 planes[1] = 2;
2542 planes[2] = 3;
2543 planes[3] = 5;
2544 break;
2545 case 14: // 5310 TQ 5
2546 planes[0] = 0;
2547 planes[1] = 1;
2548 planes[2] = 3;
2549 planes[3] = 5;
2550 break;
2551 }
e4f2f73d 2552}
2553
2554//____________________________________________________________________
0906e73e 2555void AliTRDtrackerV1::GetExtrapolationConfig(Int_t iconfig, Int_t planes[2])
e4f2f73d 2556{
972ef65e 2557 //
2558 // Returns the extrapolation planes for a seeding configuration.
2559 //
2560 // Parameters :
2561 // iconfig : configuration index
2562 // planes : planes which are not in this configuration. On input empty.
2563 //
2564 // Output :
2565 // planes : contains the planes which are not in the configuration
2566 //
2567 // Detailed description
2568 //
2569
2570 switch(iconfig){
2571 case 0: // 5432 TQ 0
2572 planes[0] = 1;
2573 planes[1] = 0;
2574 break;
2575 case 1: // 4321 TQ 0
2576 planes[0] = 5;
2577 planes[1] = 0;
2578 break;
2579 case 2: // 3210 TQ 0
2580 planes[0] = 4;
2581 planes[1] = 5;
2582 break;
2583 case 3: // 5321 TQ 1
2584 planes[0] = 4;
2585 planes[1] = 0;
2586 break;
2587 case 4: // 4210 TQ 1
2588 planes[0] = 5;
2589 planes[1] = 3;
2590 break;
2591 case 5: // 5431 TQ 1
2592 planes[0] = 2;
2593 planes[1] = 0;
2594 break;
2595 case 6: // 4320 TQ 1
2596 planes[0] = 5;
2597 planes[1] = 1;
2598 break;
2599 case 7: // 5430 TQ 2
2600 planes[0] = 2;
2601 planes[1] = 1;
2602 break;
2603 case 8: // 5210 TQ 2
2604 planes[0] = 4;
2605 planes[1] = 3;
2606 break;
2607 case 9: // 5421 TQ 3
2608 planes[0] = 3;
2609 planes[1] = 0;
2610 break;
2611 case 10: // 4310 TQ 3
2612 planes[0] = 5;
2613 planes[1] = 2;
2614 break;
2615 case 11: // 5410 TQ 4
2616 planes[0] = 3;
2617 planes[1] = 2;
2618 break;
2619 case 12: // 5420 TQ 5
2620 planes[0] = 3;
2621 planes[1] = 1;
2622 break;
2623 case 13: // 5320 TQ 5
2624 planes[0] = 4;
2625 planes[1] = 1;
2626 break;
2627 case 14: // 5310 TQ 5
2628 planes[0] = 4;
2629 planes[1] = 2;
2630 break;
2631 }
e4f2f73d 2632}
eb38ed55 2633
2634//____________________________________________________________________
2635AliCluster* AliTRDtrackerV1::GetCluster(Int_t idx) const
2636{
972ef65e 2637 Int_t ncls = fClusters->GetEntriesFast();
2638 return idx >= 0 || idx < ncls ? (AliCluster*)fClusters->UncheckedAt(idx) : 0x0;
eb38ed55 2639}
2640
bb56afff 2641//____________________________________________________________________
2642Float_t AliTRDtrackerV1::CalculateReferenceX(AliTRDseedV1 *tracklets){
972ef65e 2643 //
2644 // Calculates the reference x-position for the tilted Rieman fit defined as middle
2645 // of the stack (middle between layers 2 and 3). For the calculation all the tracklets
2646 // are taken into account
2647 //
2648 // Parameters: - Array of tracklets(AliTRDseedV1)
2649 //
2650 // Output: - The reference x-position(Float_t)
2651 //
2652 Int_t nDistances = 0;
2653 Float_t meanDistance = 0.;
2654 Int_t startIndex = 5;
2655 for(Int_t il =5; il > 0; il--){
2656 if(tracklets[il].IsOK() && tracklets[il -1].IsOK()){
2657 Float_t xdiff = tracklets[il].GetX0() - tracklets[il -1].GetX0();
2658 meanDistance += xdiff;
2659 nDistances++;
2660 }
2661 if(tracklets[il].IsOK()) startIndex = il;
2662 }
2663 if(tracklets[0].IsOK()) startIndex = 0;
2664 if(!nDistances){
2665 // We should normally never get here
2666 Float_t xpos[2]; memset(xpos, 0, sizeof(Float_t) * 2);
2667 Int_t iok = 0, idiff = 0;
2668 // This attempt is worse and should be avoided:
2669 // check for two chambers which are OK and repeat this without taking the mean value
2670 // Strategy avoids a division by 0;
2671 for(Int_t il = 5; il >= 0; il--){
2672 if(tracklets[il].IsOK()){
d739fd4e 2673 xpos[iok] = tracklets[il].GetX0();
2674 iok++;
2675 startIndex = il;
972ef65e 2676 }
2677 if(iok) idiff++; // to get the right difference;
2678 if(iok > 1) break;
2679 }
2680 if(iok > 1){
2681 meanDistance = (xpos[0] - xpos[1])/idiff;
2682 }
2683 else{
2684 // we have do not even have 2 layers which are OK? The we do not need to fit at all
2685 return 331.;
2686 }
2687 }
2688 else{
2689 meanDistance /= nDistances;
2690 }
2691 return tracklets[startIndex].GetX0() + (2.5 - startIndex) * meanDistance - 0.5 * (AliTRDgeometry::AmThick() + AliTRDgeometry::DrThick());
bb56afff 2692}
eb38ed55 2693
2694//_____________________________________________________________________________
2695Int_t AliTRDtrackerV1::Freq(Int_t n, const Int_t *inlist
972ef65e 2696 , Int_t *outlist, Bool_t down)
eb38ed55 2697{
972ef65e 2698 //
2699 // Sort eleements according occurancy
2700 // The size of output array has is 2*n
2701 //
2702
2703 if (n <= 0) {
2704 return 0;
2705 }
2706
2707 Int_t *sindexS = new Int_t[n]; // Temporary array for sorting
2708 Int_t *sindexF = new Int_t[2*n];
2709 for (Int_t i = 0; i < n; i++) {
2710 sindexF[i] = 0;
2711 }
2712
2713 TMath::Sort(n,inlist,sindexS,down);
2714
2715 Int_t last = inlist[sindexS[0]];
2716 Int_t val = last;
2717 sindexF[0] = 1;
2718 sindexF[0+n] = last;
2719 Int_t countPos = 0;
2720
2721 // Find frequency
2722 for (Int_t i = 1; i < n; i++) {
2723 val = inlist[sindexS[i]];
2724 if (last == val) {
2725 sindexF[countPos]++;
2726 }
2727 else {
2728 countPos++;
2729 sindexF[countPos+n] = val;
2730 sindexF[countPos]++;
2731 last = val;
2732 }
2733 }
2734 if (last == val) {
2735 countPos++;
2736 }
2737
2738 // Sort according frequency
2739 TMath::Sort(countPos,sindexF,sindexS,kTRUE);
2740
2741 for (Int_t i = 0; i < countPos; i++) {
2742 outlist[2*i ] = sindexF[sindexS[i]+n];
2743 outlist[2*i+1] = sindexF[sindexS[i]];
2744 }
2745
2746 delete [] sindexS;
2747 delete [] sindexF;
2748
2749 return countPos;
eb38ed55 2750
2751}
bb56afff 2752
2753//_____________________________________________________________________________
2754Float_t AliTRDtrackerV1::GetChi2Y(AliTRDseedV1 *tracklets) const
2755{
972ef65e 2756 // Chi2 definition on y-direction
2757
2758 Float_t chi2 = 0;
2759 for(Int_t ipl = 0; ipl < kNPlanes; ipl++){
2760 if(!tracklets[ipl].IsOK()) continue;
2761 Double_t distLayer = tracklets[ipl].GetYfit(0) - tracklets[ipl].GetYref(0);
2762 chi2 += distLayer * distLayer;
2763 }
2764 return chi2;
bb56afff 2765}
2766
2767//_____________________________________________________________________________
2768Float_t AliTRDtrackerV1::GetChi2Z(AliTRDseedV1 *tracklets) const
2769{
972ef65e 2770 // Chi2 definition on z-direction
2771
2772 Float_t chi2 = 0;
2773 for(Int_t ipl = 0; ipl < kNPlanes; ipl++){
2774 if(!tracklets[ipl].IsOK()) continue;
2775 Double_t distLayer = tracklets[ipl].GetMeanz() - tracklets[ipl].GetZref(0);
2776 chi2 += distLayer * distLayer;
2777 }
2778 return chi2;
bb56afff 2779}
8acca6a3 2780
2781///////////////////////////////////////////////////////
2782// //
2783// Resources of class AliTRDLeastSquare //
2784// //
2785///////////////////////////////////////////////////////
2786
2787//_____________________________________________________________________________
2788AliTRDtrackerV1::AliTRDLeastSquare::AliTRDLeastSquare(){
972ef65e 2789 //
2790 // Constructor of the nested class AliTRDtrackFitterLeastSquare
2791 //
2792 memset(fParams, 0, sizeof(Double_t) * 2);
2793 memset(fSums, 0, sizeof(Double_t) * 5);
2794 memset(fCovarianceMatrix, 0, sizeof(Double_t) * 3);
8acca6a3 2795
2796}
2797
2798//_____________________________________________________________________________
2799void AliTRDtrackerV1::AliTRDLeastSquare::AddPoint(Double_t *x, Double_t y, Double_t sigmaY){
972ef65e 2800 //
2801 // Adding Point to the fitter
2802 //
2803 Double_t weight = 1/(sigmaY * sigmaY);
2804 Double_t &xpt = *x;
2805 // printf("Adding point x = %f, y = %f, sigma = %f\n", xpt, y, sigmaY);
2806 fSums[0] += weight;
2807 fSums[1] += weight * xpt;
2808 fSums[2] += weight * y;
2809 fSums[3] += weight * xpt * y;
2810 fSums[4] += weight * xpt * xpt;
2811 fSums[5] += weight * y * y;
8acca6a3 2812}
2813
2814//_____________________________________________________________________________
2815void AliTRDtrackerV1::AliTRDLeastSquare::RemovePoint(Double_t *x, Double_t y, Double_t sigmaY){
972ef65e 2816 //
2817 // Remove Point from the sample
2818 //
2819 Double_t weight = 1/(sigmaY * sigmaY);
2820 Double_t &xpt = *x;
2821 fSums[0] -= weight;
2822 fSums[1] -= weight * xpt;
2823 fSums[2] -= weight * y;
2824 fSums[3] -= weight * xpt * y;
2825 fSums[4] -= weight * xpt * xpt;
2826 fSums[5] -= weight * y * y;
8acca6a3 2827}
2828
2829//_____________________________________________________________________________
2830void AliTRDtrackerV1::AliTRDLeastSquare::Eval(){
972ef65e 2831 //
2832 // Evaluation of the fit:
2833 // Calculation of the parameters
2834 // Calculation of the covariance matrix
2835 //
2836
2837 Double_t denominator = fSums[0] * fSums[4] - fSums[1] *fSums[1];
2838 // for(Int_t isum = 0; isum < 5; isum++)
2839 // printf("fSums[%d] = %f\n", isum, fSums[isum]);
2840 // printf("denominator = %f\n", denominator);
2841 fParams[0] = (fSums[2] * fSums[4] - fSums[1] * fSums[3])/ denominator;
2842 fParams[1] = (fSums[0] * fSums[3] - fSums[1] * fSums[2]) / denominator;
2843 // printf("fParams[0] = %f, fParams[1] = %f\n", fParams[0], fParams[1]);
2844
2845 // Covariance matrix
2846 fCovarianceMatrix[0] = fSums[4] - fSums[1] * fSums[1] / fSums[0];
2847 fCovarianceMatrix[1] = fSums[5] - fSums[2] * fSums[2] / fSums[0];
2848 fCovarianceMatrix[2] = fSums[3] - fSums[1] * fSums[2] / fSums[0];
8acca6a3 2849}
2850
46b6abd7 2851//_____________________________________________________________________________
2852Double_t AliTRDtrackerV1::AliTRDLeastSquare::GetFunctionValue(Double_t *xpos) const {
972ef65e 2853 //
2854 // Returns the Function value of the fitted function at a given x-position
2855 //
2856 return fParams[0] + fParams[1] * (*xpos);
46b6abd7 2857}
2858
2859//_____________________________________________________________________________
2860void AliTRDtrackerV1::AliTRDLeastSquare::GetCovarianceMatrix(Double_t *storage) const {
972ef65e 2861 //
2862 // Copies the values of the covariance matrix into the storage
2863 //
2864 memcpy(storage, fCovarianceMatrix, sizeof(Double_t) * 3);
46b6abd7 2865}
2866